File: ccwcache.c

package info (click to toggle)
kernel-source-2.2.19 2.2.19.1-4woody1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 92,100 kB
  • ctags: 276,892
  • sloc: ansic: 1,710,384; asm: 58,709; makefile: 10,198; sh: 2,398; perl: 907; tcl: 570; lisp: 218; cpp: 186; awk: 133; sed: 72
file content (387 lines) | stat: -rw-r--r-- 11,323 bytes parent folder | download | duplicates (5)
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
/* 
   * File...........: linux/drivers/s390/ccwcache.c
   * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
   * Bugreports.to..: <Linux390@de.ibm.com>
   * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 2000a
   
   * History of changes
   * 07/10/00 moved ccwcache.h from linux/ to asm/
 */

#include <linux/module.h>
#include <linux/malloc.h>
#include <asm/debug.h>
#include <asm/ccwcache.h>

#include <asm/ebcdic.h>
#include <asm/spinlock.h>

#undef  PRINTK_HEADER
#define PRINTK_HEADER "ccwcache"

MODULE_AUTHOR ("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
MODULE_DESCRIPTION ("Linux on S/390 CCW cache,"
		    "Copyright 2000 IBM Corporation");
EXPORT_SYMBOL(ccw_alloc_request);
EXPORT_SYMBOL(ccw_free_request);


/* pointer to list of allocated requests */
static ccw_req_t *ccwreq_actual = NULL;
static spinlock_t ccwchain_lock = SPIN_LOCK_UNLOCKED;

/* pointer to debug area */
static debug_info_t *debug_area = NULL;

/* SECTION: Handling of the dynamically allocated kmem slabs */

/* a name template for the cache-names */
static char ccw_name_template[] = "ccwcache-\0\0\0\0"; /* fill name with zeroes! */
/* the cache's names */
static char ccw_cache_name[CCW_NUMBER_CACHES][sizeof(ccw_name_template)+1]; 
/* the caches itself*/
static kmem_cache_t *ccw_cache[CCW_NUMBER_CACHES]; 

/* SECTION: (de)allocation of ccw_req_t */

/* 
 * void dechain ( ccw_req_t *request )
 * dechains the request from the ringbuffer
 */

static void 
dechain ( ccw_req_t *request )
{
	long flags;

	/* Sanity checks */
	if ( request == NULL ) {
		printk( KERN_WARNING PRINTK_HEADER
			"Trying to deallocate NULL request\n");
		return;
	}

	spin_lock_irqsave(&ccwchain_lock,flags);
	/* first deallocate request from list of allocates requests */
	if ( request -> int_next && request -> int_prev ) {
		if ( request -> int_next == request -> int_prev ) {
			ccwreq_actual = NULL;
		} else {
			if ( ccwreq_actual == request ) {
				ccwreq_actual = request->int_next;
			}
			request->int_prev->int_next = request->int_next;
			request->int_next->int_prev = request->int_prev;
		}
	} else if ( request -> int_next || request -> int_prev ) {
	}
	spin_unlock_irqrestore(&ccwchain_lock,flags);
	return;
}

/* 
 * ccw_req_t *ccw_alloc_request ( int cplength, int datasize )
 * allocates a ccw_req_t, that 
 * - can hold a CP of cplength CCWS
 * - can hold additional data up to datasize 
 */
ccw_req_t *
ccw_alloc_request ( char *magic, int cplength, int datasize )
{
	ccw_req_t * request = NULL;
	int cachind = 0;
	int size_needed = 0;
	long flags;

	debug_text_event ( debug_area, 1, "ALLC");
	if ( magic ) {
		debug_text_event ( debug_area, 1, magic);
	}
	debug_int_event ( debug_area, 1, cplength);
	debug_int_event ( debug_area, 1, datasize);

	/* Sanity checks */
	if ( cplength == 0 ) {
		printk (KERN_DEBUG PRINTK_HEADER
			"called ccw_alloc_request with cplength of 0 from %p\n",
			     __builtin_return_address(0));
		return NULL;
	}
	if ( (cplength << 3 ) > PAGE_SIZE ) {
		printk(KERN_WARNING PRINTK_HEADER
		       "Channel program to large to fit on one page\n");
		return NULL;
	}
	if ( datasize > PAGE_SIZE ) {
		printk(KERN_WARNING PRINTK_HEADER
		       "Data size to large to fit on one page\n");
		return NULL;
	}
	
	/* Try to keep things together in memory */
	if ( sizeof (ccw_req_t) + 
	     (((datasize + 7) >> 3) << 3) +  /* align upper end of data to 8 */
	     (cplength << 3 ) < PAGE_SIZE ) {
		/* All fit on one page, is preferred. */
		size_needed = sizeof (ccw_req_t) + datasize + (cplength << 3 );
	} else if ( sizeof (ccw_req_t) + (cplength << 3 ) < PAGE_SIZE ) {
		/* Try to keep CCWs with request */
		size_needed = sizeof (ccw_req_t) + (cplength << 3 );
	} else if ( sizeof (ccw_req_t) + datasize < PAGE_SIZE ) {
		/* Try to keep at least the data with request */
		size_needed = sizeof (ccw_req_t) + datasize;
	} else {
		/* Fall back to hold only the request in cache */
		size_needed = sizeof (ccw_req_t);
	}
	/* determine cache index for the requested size */
	for (cachind = 0; cachind < CCW_NUMBER_CACHES; cachind ++ ) {
	   if ( size_needed < (SMALLEST_SLAB << cachind) ) 
			break;
	}
	/* Try to fulfill the request from a cache */
	while (  cachind < CCW_NUMBER_CACHES ) { /* Now try to get an entry from a cache above or equal to cachind */
	  if ( ccw_cache[cachind] == NULL ){
	    printk(KERN_WARNING PRINTK_HEADER "NULL cache found! cache=%p index %d\n",ccw_cache[cachind],cachind);
	  }
	  request = kmem_cache_alloc ( ccw_cache[cachind], GFP_ATOMIC );
	  if ( request != NULL ) {
	    memset ( request, 0, (SMALLEST_SLAB << cachind));
	    request->cache = ccw_cache[cachind];
	    break;
	  } else {
	    printk (KERN_DEBUG PRINTK_HEADER "Proceeding to next cache\n");
	  }
 	  cachind++;
	} 
	/* if no success, fall back to kmalloc */
	if ( request == NULL ) {
	  printk (KERN_DEBUG PRINTK_HEADER "Falling back to kmalloc\n");
		request = kmalloc ( sizeof(ccw_req_t), GFP_ATOMIC );
		if ( request != NULL ) {
			memset ( request, 0, sizeof(ccw_req_t));
		}
	}
	/* Initialize request */
	if ( request == NULL ) {  
		printk(KERN_WARNING PRINTK_HEADER "Couldn't allocate request\n");
	} else {
		if ( request -> cache != NULL ) {
			/* Three cases when coming from a cache */
			if ( sizeof (ccw_req_t) + 
			     (((datasize + 7) >> 3) << 3) +
			     (cplength << 3 ) < 
			     PAGE_SIZE ) {
				request->data= (void*)(request + 1);
				request->cpaddr= (ccw1_t *)(((((long)(request + 1))+ 
							     datasize + 7)
							    >> 3) << 3);
			} else if ( sizeof (ccw_req_t) + (cplength << 3 ) < 
				    PAGE_SIZE ) {
				request->cpaddr=(ccw1_t *)(((((long)(request)) + 
							     sizeof(ccw_req_t) + 7)
							    >> 3) << 3);
			} else if ( sizeof (ccw_req_t) + datasize < PAGE_SIZE ) {
				request->data= (void *)(request + 1);
			} else {} 
		}
		/* Have the rest be done by kmalloc */
		if ( request -> cpaddr == NULL ) {
			printk(KERN_DEBUG PRINTK_HEADER "Falling back to kmalloc for CCW area\n");
			request->cpaddr=(ccw1_t *) kmalloc(sizeof(ccw1_t) * cplength,
							  GFP_ATOMIC);
			if ( request -> cpaddr == NULL ) {
				printk(KERN_WARNING PRINTK_HEADER "Couldn't allocate ccw area\n");
				ccw_free_request(request);
				return NULL;
			}
		}
		if ( request -> data == NULL ) {
			printk(KERN_DEBUG PRINTK_HEADER"Falling back to kmalloc for data area\n");
			request->data=(void *)kmalloc(datasize, GFP_ATOMIC);
			if ( request -> data == NULL ) {
				printk(KERN_WARNING PRINTK_HEADER "Couldn't allocate data area\n");
				ccw_free_request(request);
				return NULL;
			}
		}
		memset ( request->data,0,datasize );
		memset ( request->cpaddr,0,cplength*sizeof(ccw1_t) );
		if ( magic ) {
			strncpy ( (char *)(&request->magic), magic, 4);
		}
		else {
			strncpy ( (char *)(&request->magic), "CCWC", 4);
		}
		ASCEBC((char *)(&request->magic),4);
		request -> cplength = cplength;
		request -> datasize = datasize;
		/* enqueue request to list of allocated requests */
		spin_lock_irqsave(&ccwchain_lock,flags);
		if ( ccwreq_actual == NULL ) { /* queue empty */
			ccwreq_actual = request;
			request->int_prev = ccwreq_actual;
			request->int_next = ccwreq_actual;
		} else {
			request->int_next = ccwreq_actual;
			request->int_prev = ccwreq_actual->int_prev;
			request->int_prev->int_next = request;
			request->int_next->int_prev = request;
		}
		spin_unlock_irqrestore(&ccwchain_lock,flags);
	}
	debug_int_event ( debug_area, 1, (long)request);
	return request;
}

/* 
 * void ccw_free_request ( ccw_req_t * )
 * deallocates the ccw_req_t, given as argument
 */

void
ccw_free_request ( ccw_req_t * request )
{
	int cachind;
	int slabsize;

	debug_text_event ( debug_area, 1, "FREE");
	debug_int_event ( debug_area, 1, (long)request);
	/* Sanity checks */
	if ( request == NULL ) {
		printk(KERN_DEBUG PRINTK_HEADER"Trying to deallocate NULL request\n");
		return;
	}
	if ( request -> cache == NULL ) {
		printk (KERN_WARNING PRINTK_HEADER "Deallocating uncached request\n");
		if ( request -> data ) {
			kfree ( request -> data );
		}
		if ( request ->cpaddr ) {
			kfree ( request -> cpaddr );
		}
		dechain (request);
		kfree ( request );
	} else {
		/* Find which area has been allocated by kmalloc */
		for (cachind = 0; cachind < CCW_NUMBER_CACHES; cachind ++ )
			if ( request->cache == ccw_cache[cachind] )
				break;
		if ( request->cache != ccw_cache[cachind] ) {
			printk(KERN_WARNING PRINTK_HEADER"Cannot find cache in list\n");
		}
		slabsize = SMALLEST_SLAB << cachind;
		if ( request -> data &&
		     (((unsigned long)(request -> data) >
		       ((unsigned long)request) + slabsize) ||
		      ((unsigned long)(request -> data) < 
		       ((unsigned long)request)))) {
			kfree ( request -> data );
		}
		if ( request ->cpaddr &&
		     (((unsigned long)(request -> cpaddr) >
		       ((unsigned long)request) + slabsize) ||
		      (unsigned long)(request -> cpaddr) < 
		      ((unsigned long)request))) {
			kfree ( request -> cpaddr );
		}
		dechain ( request);
		kmem_cache_free(request -> cache, request);
	}
}

/* SECTION: initialization and cleanup functions */

/* 
 * ccwcache_init
 * called as an initializer function for the ccw memory management
 */

int
ccwcache_init (void)
{
	int rc = 0;
	int cachind;

	/* allocate a debug area */
	debug_area = debug_register( "ccwcache", 2, 4,4);
	if ( ! debug_area ) {
		printk ( KERN_WARNING PRINTK_HEADER"cannot allocate debug area\n" );
	} else {
		printk (KERN_DEBUG PRINTK_HEADER "debug area is 0x%8p\n", debug_area );
	}
        debug_register_view(debug_area,&debug_hex_ascii_view);
	debug_text_event ( debug_area, 0, "INIT");
	
	/* First allocate the kmem caches */
	for ( cachind = 0; cachind < CCW_NUMBER_CACHES; cachind ++ ) {
		int slabsize = SMALLEST_SLAB << cachind;
		debug_text_event ( debug_area, 1, "allc");
		debug_int_event ( debug_area, 1, slabsize);
		sprintf ( ccw_cache_name[cachind], 
			  "%s%d%c", ccw_name_template, slabsize, 0);
		ccw_cache[cachind] = kmem_cache_create( ccw_cache_name[cachind], 
							slabsize, 0,
							SLAB_HWCACHE_ALIGN, 
							NULL, NULL );
		debug_int_event ( debug_area, 1, (long)ccw_cache[cachind]);
		if ( ! ccw_cache [cachind] ) {
			printk (KERN_WARNING PRINTK_HEADER "Allocation of CCW cache failed\n");
		}
	}
	return rc;
}

/* 
 * ccwcache_cleanup
 * called as a cleanup function for the ccw memory management
 */

void
ccwcache_cleanup (void)
{
	int cachind;

	/* Shrink the caches, if available */
	for ( cachind = 0; cachind < CCW_NUMBER_CACHES; cachind ++ ) {
		if ( ccw_cache[cachind] ) {
			if ( kmem_cache_shrink(ccw_cache[cachind]) == 0 ) {
				ccw_cache[cachind] = NULL;
			}
		}
	}
	debug_unregister( debug_area );
}

#ifdef MODULE
int
init_module ( void )
{
	int rc;
	int i;
	rc = ccwcache_init();
	for ( i = 0; i < 200; i ++) {
		PRINT_INFO ("allocated %p\n",ccw_alloc_request("test",i,200-i));
	}
	return rc;
}

void
cleanup_module ( void )
{
	while ( ccwreq_actual ) {
		PRINT_INFO ("freeing %p\n",ccwreq_actual -> int_next);
		ccw_free_request( ccwreq_actual -> int_next );
		if ( ccwreq_actual ) {
			PRINT_INFO ("freeing %p\n",ccwreq_actual -> int_prev);
			ccw_free_request( ccwreq_actual -> int_prev );
		}
		if ( ccwreq_actual ) {
			PRINT_INFO ("freeing %p\n",ccwreq_actual);
			ccw_free_request( ccwreq_actual);
		}
	}
	ccwcache_cleanup();
}
#endif /* MODULE */