aboutsummaryrefslogtreecommitdiffstats
path: root/arch/ia64/sn/io/hwgfs/labelcl.c
blob: 0521b4c32955c5a36be1b997010e5fdbe1d298e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*  labelcl - SGI's Hwgraph Compatibility Layer.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (c) 2001-2003 Silicon Graphics, Inc.  All rights reserved.
*/

#include <linux/types.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/sched.h>                /* needed for smp_lock.h :( */
#include <linux/smp_lock.h>
#include <asm/sn/sgi.h>
#include <asm/sn/hwgfs.h>
#include <asm/sn/invent.h>
#include <asm/sn/hcl.h>
#include <asm/sn/labelcl.h>

/*
** Very simple and dumb string table that supports only find/insert.
** In practice, if this table gets too large, we may need a more
** efficient data structure.   Also note that currently there is no 
** way to delete an item once it's added.  Therefore, name collision 
** will return an error.
*/

struct string_table label_string_table;



/*
 * string_table_init - Initialize the given string table.
 */
void
string_table_init(struct string_table *string_table)
{
	string_table->string_table_head = NULL;
	string_table->string_table_generation = 0;

	/*
	 * We nedd to initialize locks here!
	 */

	return;
}


/*
 * string_table_destroy - Destroy the given string table.
 */
void
string_table_destroy(struct string_table *string_table)
{
	struct string_table_item *item, *next_item;

	item = string_table->string_table_head;
	while (item) {
		next_item = item->next;

		STRTBL_FREE(item);
		item = next_item;
	}

	/*
	 * We need to destroy whatever lock we have here
	 */

	return;
}



/*
 * string_table_insert - Insert an entry in the string table .. duplicate 
 *	names are not allowed.
 */
char *
string_table_insert(struct string_table *string_table, char *name)
{
	struct string_table_item *item, *new_item = NULL, *last_item = NULL;

again:
	/*
	 * Need to lock the table ..
	 */
	item = string_table->string_table_head;
	last_item = NULL;

	while (item) {
		if (!strcmp(item->string, name)) {
			/*
			 * If we allocated space for the string and the found that
			 * someone else already entered it into the string table,
			 * free the space we just allocated.
			 */
			if (new_item)
				STRTBL_FREE(new_item);


			/*
			 * Search optimization: move the found item to the head
			 * of the list.
			 */
			if (last_item != NULL) {
				last_item->next = item->next;
				item->next = string_table->string_table_head;
				string_table->string_table_head = item;
			}
			goto out;
		}
		last_item = item;
		item=item->next;
	}

	/*
	 * name was not found, so add it to the string table.
	 */
	if (new_item == NULL) {
		long old_generation = string_table->string_table_generation;

		new_item = STRTBL_ALLOC(strlen(name));

		strcpy(new_item->string, name);

		/*
		 * While we allocated memory for the new string, someone else 
		 * changed the string table.
		 */
		if (old_generation != string_table->string_table_generation) {
			goto again;
		}
	} else {
		/* At this we only have the string table lock in access mode.
		 * Promote the access lock to an update lock for the string
		 * table insertion below.
		 */
			long old_generation = 
				string_table->string_table_generation;

			/*
			 * After we did the unlock and wer waiting for update
			 * lock someone could have potentially updated
			 * the string table. Check the generation number
			 * for this case. If it is the case we have to
			 * try all over again.
			 */
			if (old_generation != 
			    string_table->string_table_generation) {
				goto again;
			}
		}

	/*
	 * At this point, we're committed to adding new_item to the string table.
	 */
	new_item->next = string_table->string_table_head;
	item = string_table->string_table_head = new_item;
	string_table->string_table_generation++;

out:
	/*
	 * Need to unlock here.
	 */
	return(item->string);
}

/*
 * labelcl_info_create - Creates the data structure that will hold the
 *	device private information asscoiated with a entry.
 *	The pointer to this structure is what gets stored in the 
 *	(void * info).
 */
labelcl_info_t *
labelcl_info_create()
{

	labelcl_info_t *new = NULL;

	/* Initial allocation does not include any area for labels */
	if ( ( new = (labelcl_info_t *)kmalloc (sizeof(labelcl_info_t), GFP_KERNEL) ) == NULL )
		return NULL;

	memset (new, 0, sizeof(labelcl_info_t));
	new->hwcl_magic = LABELCL_MAGIC;
	return( new);

}

/*
 * labelcl_info_destroy - Frees the data structure that holds the
 *      device private information asscoiated with a entry.  This 
 *	data structure was created by device_info_create().
 *
 *	The caller is responsible for nulling the (void *info) in the 
 *	corresponding entry.
 */
int
labelcl_info_destroy(labelcl_info_t *labelcl_info)
{

	if (labelcl_info == NULL)
		return(0);

	/* Free the label list */
	if (labelcl_info->label_list)
		kfree(labelcl_info->label_list);

	/* Now free the label info area */
	labelcl_info->hwcl_magic = 0;
	kfree(labelcl_info);

	return(0);
}

/*
 * labelcl_info_add_LBL - Adds a new label entry in the labelcl info 
 *	structure.
 *
 *	Error is returned if we find another label with the same name.
 */
int
labelcl_info_add_LBL(vertex_hdl_t de,
			char *info_name,
			arb_info_desc_t info_desc,
			arbitrary_info_t info)
{
	labelcl_info_t	*labelcl_info = NULL;
	int num_labels;
	int new_label_list_size;
	label_info_t *old_label_list, *new_label_list = NULL;
	char *name;
	int i;

	if (de == NULL)
		return(-1);

        labelcl_info = hwgfs_get_info(de);
	if (labelcl_info == NULL)
		return(-1);

	if (labelcl_info->hwcl_magic != LABELCL_MAGIC)
		return(-1);

	if (info_name == NULL)
		return(-1);

	if (strlen(info_name) >= LABEL_LENGTH_MAX)
		return(-1);

	name = string_table_insert(&label_string_table, info_name);

	num_labels = labelcl_info->num_labels;
	new_label_list_size = sizeof(label_info_t) * (num_labels+1);

	/*
	 * Create a new label info area.
	 */
	if (new_label_list_size != 0) {
		new_label_list = (label_info_t *) kmalloc(new_label_list_size, GFP_KERNEL);

		if (new_label_list == NULL)
			return(-1);
	}

	/*
	 * At this point, we are committed to adding the labelled info, 
	 * if there isn't already information there with the same name.
	 */
	old_label_list = labelcl_info->label_list;

	/* 
	 * Look for matching info name.
	 */
	for (i=0; i<num_labels; i++) {
		if (!strcmp(info_name, old_label_list[i].name)) {
			/* Not allowed to add duplicate labelled info names. */
			kfree(new_label_list);
			return(-1);
		}
		new_label_list[i] = old_label_list[i]; /* structure copy */
	}

	new_label_list[num_labels].name = name;
	new_label_list[num_labels].desc = info_desc;
	new_label_list[num_labels].info = info;

	labelcl_info->num_labels = num_labels+1;
	labelcl_info->label_list = new_label_list;

	if (old_label_list != NULL)
		kfree(old_label_list);

	return(0);
}

/*
 * labelcl_info_remove_LBL - Remove a label entry.
 */
int
labelcl_info_remove_LBL(vertex_hdl_t de,
			 char *info_name,
			 arb_info_desc_t *info_desc,
			 arbitrary_info_t *info)
{
	labelcl_info_t	*labelcl_info = NULL;
	int num_labels;
	int new_label_list_size;
	label_info_t *old_label_list, *new_label_list = NULL;
	arb_info_desc_t label_desc_found;
	arbitrary_info_t label_info_found;
	int i;

	if (de == NULL)
		return(-1);

	labelcl_info = hwgfs_get_info(de);
	if (labelcl_info == NULL)
		return(-1);

	if (labelcl_info->hwcl_magic != LABELCL_MAGIC)
		return(-1);

	num_labels = labelcl_info->num_labels;
	if (num_labels == 0) {
		return(-1);
	}

	/*
	 * Create a new info area.
	 */
	new_label_list_size = sizeof(label_info_t) * (num_labels-1);
	if (new_label_list_size) {
		new_label_list = (label_info_t *) kmalloc(new_label_list_size, GFP_KERNEL);
		if (new_label_list == NULL)
			return(-1);
	}

	/*
	 * At this point, we are committed to removing the labelled info, 
	 * if it still exists.
	 */
	old_label_list = labelcl_info->label_list;

	/* 
	 * Find matching info name.
	 */
	for (i=0; i<num_labels; i++) {
		if (!strcmp(info_name, old_label_list[i].name)) {
			label_desc_found = old_label_list[i].desc;
			label_info_found = old_label_list[i].info;
			goto found;
		}
		if (i < num_labels-1) /* avoid walking off the end of the new vertex */
			new_label_list[i] = old_label_list[i]; /* structure copy */
	}

	/* The named info doesn't exist. */
	if (new_label_list)
		kfree(new_label_list);

	return(-1);

found:
	/* Finish up rest of labelled info */
	for (i=i+1; i<num_labels; i++)
		new_label_list[i-1] = old_label_list[i]; /* structure copy */

	labelcl_info->num_labels = num_labels+1;
	labelcl_info->label_list = new_label_list;

	kfree(old_label_list);

	if (info != NULL)
		*info = label_info_found;

	if (info_desc != NULL)
		*info_desc = label_desc_found;

	return(0);
}


/*
 * labelcl_info_replace_LBL - Replace an existing label entry with the 
 *	given new information.
 *
 *	Label entry must exist.
 */
int
labelcl_info_replace_LBL(vertex_hdl_t de,
			char *info_name,
			arb_info_desc_t info_desc,
			arbitrary_info_t info,
			arb_info_desc_t *old_info_desc,
			arbitrary_info_t *old_info)
{
	labelcl_info_t	*labelcl_info = NULL;
	int num_labels;
	label_info_t *label_list;
	int i;

	if (de == NULL)
		return(-1);

	labelcl_info = hwgfs_get_info(de);
	if (labelcl_info == NULL)
		return(-1);

	if (labelcl_info->hwcl_magic != LABELCL_MAGIC)
		return(-1);

	num_labels = labelcl_info->num_labels;
	if (num_labels == 0) {
		return(-1);
	}

	if (info_name == NULL)
		return(-1);

	label_list = labelcl_info->label_list;

	/* 
	 * Verify that information under info_name already exists.
	 */
	for (i=0; i<num_labels; i++)
		if (!strcmp(info_name, label_list[i].name)) {
			if (old_info != NULL)
				*old_info = label_list[i].info;

			if (old_info_desc != NULL)
				*old_info_desc = label_list[i].desc;

			label_list[i].info = info;
			label_list[i].desc = info_desc;

			return(0);
		}


	return(-1);
}

/*
 * labelcl_info_get_LBL - Retrieve and return the information for the 
 *	given label entry.
 */
int
labelcl_info_get_LBL(vertex_hdl_t de,
		      char *info_name,
		      arb_info_desc_t *info_desc,
		      arbitrary_info_t *info)
{
	labelcl_info_t	*labelcl_info = NULL;
	int num_labels;
	label_info_t *label_list;
	int i;

	if (de == NULL)
		return(-1);

	labelcl_info = hwgfs_get_info(de);
	if (labelcl_info == NULL)
		return(-1);

	if (labelcl_info->hwcl_magic != LABELCL_MAGIC)
		return(-1);

	num_labels = labelcl_info->num_labels;
	if (num_labels == 0) {
		return(-1);
	}

	label_list = labelcl_info->label_list;

	/* 
	 * Find information under info_name.
	 */
	for (i=0; i<num_labels; i++)
		if (!strcmp(info_name, label_list[i].name)) {
			if (info != NULL)
				*info = label_list[i].info;
			if (info_desc != NULL)
				*info_desc = label_list[i].desc;

			return(0);
		}

	return(-1);
}

/*
 * labelcl_info_get_next_LBL - returns the next label entry on the list.
 */
int
labelcl_info_get_next_LBL(vertex_hdl_t de,
			   char *buffer,
			   arb_info_desc_t *info_descp,
			   arbitrary_info_t *infop,
			   labelcl_info_place_t *placeptr)
{
	labelcl_info_t	*labelcl_info = NULL;
	uint which_info;
	label_info_t *label_list;

	if ((buffer == NULL) && (infop == NULL))
		return(-1);

	if (placeptr == NULL)
		return(-1);

	if (de == NULL)
		return(-1);

	labelcl_info = hwgfs_get_info(de);
	if (labelcl_info == NULL)
		return(-1);

	if (labelcl_info->hwcl_magic != LABELCL_MAGIC)
		return(-1);

	which_info = *placeptr;

	if (which_info >= labelcl_info->num_labels) {
		return(-1);
	}

	label_list = (label_info_t *) labelcl_info->label_list;

	if (buffer != NULL)
		strcpy(buffer, label_list[which_info].name);

	if (infop)
		*infop = label_list[which_info].info;

	if (info_descp)
		*info_descp = label_list[which_info].desc;

	*placeptr = which_info + 1;

	return(0);
}


int
labelcl_info_replace_IDX(vertex_hdl_t de,
			int index,
			arbitrary_info_t info,
			arbitrary_info_t *old_info)
{
	arbitrary_info_t *info_list_IDX;
	labelcl_info_t	*labelcl_info = NULL;

	if (de == NULL) {
		printk(KERN_ALERT "labelcl: NULL handle given.\n");
		return(-1);
	}

	labelcl_info = hwgfs_get_info(de);
	if (labelcl_info == NULL) {
		printk(KERN_ALERT "labelcl: Entry %p does not have info pointer.\n", (void *)de);
		return(-1);
	}

	if (labelcl_info->hwcl_magic != LABELCL_MAGIC)
		return(-1);

	if ( (index < 0) || (index >= HWGRAPH_NUM_INDEX_INFO) )
		return(-1);

	/*
	 * Replace information at the appropriate index in this vertex with 
	 * the new info.
	 */
	info_list_IDX = labelcl_info->IDX_list;
	if (old_info != NULL)
		*old_info = info_list_IDX[index];
	info_list_IDX[index] = info;

	return(0);

}

/*
 * labelcl_info_connectpt_set - Sets the connectpt.
 */
int
labelcl_info_connectpt_set(hwgfs_handle_t de,
			  hwgfs_handle_t connect_de)
{
	arbitrary_info_t old_info;
	int	rv;

	rv = labelcl_info_replace_IDX(de, HWGRAPH_CONNECTPT, 
		(arbitrary_info_t) connect_de, &old_info);

	if (rv) {
		return(rv);
	}

	return(0);
}


/*
 * labelcl_info_get_IDX - Returns the information pointed at by index.
 *
 */
int
labelcl_info_get_IDX(vertex_hdl_t de,
			int index,
			arbitrary_info_t *info)
{
	arbitrary_info_t *info_list_IDX;
	labelcl_info_t	*labelcl_info = NULL;

	if (de == NULL)
		return(-1);

	labelcl_info = hwgfs_get_info(de);
	if (labelcl_info == NULL)
		return(-1);

	if (labelcl_info->hwcl_magic != LABELCL_MAGIC)
		return(-1);

	if ( (index < 0) || (index >= HWGRAPH_NUM_INDEX_INFO) )
		return(-1);

	/*
	 * Return information at the appropriate index in this vertex.
	 */
	info_list_IDX = labelcl_info->IDX_list;
	if (info != NULL)
		*info = info_list_IDX[index];

	return(0);
}

/*
 * labelcl_info_connectpt_get - Retrieve the connect point for a device entry.
 */
hwgfs_handle_t
labelcl_info_connectpt_get(hwgfs_handle_t de)
{
	int rv;
	arbitrary_info_t info;

	rv = labelcl_info_get_IDX(de, HWGRAPH_CONNECTPT, &info);
	if (rv)
		return(NULL);

	return((hwgfs_handle_t) info);
}