summaryrefslogtreecommitdiffstats
path: root/drivers/s390/ccwcache.c
blob: e9e0b53ee67af5bca4a46303135e3d92efc9b3af (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
/* 
 * File...........: linux/drivers/s390/ccwcache.c
 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
 *                  Martin Schiwdefsky <schwidefsky@de.ibm.com>
 * Bugreports.to..: <Linux390@de.ibm.com>
 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 2000a
 
 * History of changes
 * 11/14/00 redesign by Martin Schwidefsky
 */

#include <linux/malloc.h>
#include <linux/version.h>

#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,3,98))
#include <linux/spinlock.h>
#else
#include <asm/spinlock.h>
#endif

#include <asm/debug.h>
#include <asm/ccwcache.h>
#include <asm/ebcdic.h>

#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,3,98))
#define CCW_CACHE_SLAB_TYPE (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA)
#define CCW_CACHE_TYPE (GFP_ATOMIC | GFP_DMA)
#else
#define CCW_CACHE_SLAB_TYPE (SLAB_HWCACHE_ALIGN)
#define CCW_CACHE_TYPE (GFP_ATOMIC)
#define kmem_cache_destroy(x) do {} while(0)
#endif

#ifdef PRINTK_HEADER
#undef PRINTK_HEADER
#endif
#define PRINTK_HEADER "ccwcache"

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

/* 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 enchain ( ccw_req_t *request )
 * enchains the request to the ringbuffer
 */
static inline void 
enchain ( ccw_req_t *request )
{
	unsigned long flags;

	/* Sanity checks */
	if ( request == NULL )
		BUG();
	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);
}

/* 
 * void dechain ( ccw_req_t *request )
 * dechains the request from the ringbuffer
 */
static inline void 
dechain ( ccw_req_t *request )
{
	unsigned long flags;

	/* Sanity checks */
	if ( request == NULL || 
	     request->int_next == NULL ||
	     request->int_prev == NULL)
		BUG();
	/* first deallocate request from list of allocates requests */
	spin_lock_irqsave(&ccwchain_lock,flags);
	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;
	}
	spin_unlock_irqrestore(&ccwchain_lock,flags);
}

/* 
 * 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 size_needed;
	int data_offset, ccw_offset;
	int cachind;

	/* Sanity checks */
	if ( magic == NULL || datasize > PAGE_SIZE ||
	     cplength == 0 || (cplength*sizeof(ccw1_t)) > PAGE_SIZE)
		BUG();
	debug_text_event ( debug_area, 1, "ALLC");
	debug_text_event ( debug_area, 1, magic);
	debug_int_event ( debug_area, 1, cplength);
	debug_int_event ( debug_area, 1, datasize);

	/* We try to keep things together in memory */
	size_needed = (sizeof (ccw_req_t) + 7) & -8;
	data_offset = ccw_offset = 0;
	if (size_needed + datasize <= PAGE_SIZE) {
		/* Keep data with the request */
		data_offset = size_needed;
		size_needed += (datasize + 7) & -8;
	}
	if (size_needed + cplength*sizeof(ccw1_t) <= PAGE_SIZE) {
		/* Keep CCWs with request */
		ccw_offset = size_needed;
		size_needed += cplength*sizeof(ccw1_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 */
	if ( ccw_cache[cachind] == NULL )
		BUG();
	request = kmem_cache_alloc ( ccw_cache[cachind], CCW_CACHE_TYPE );
	if (request == NULL)
		return NULL;
	memset ( request, 0, (SMALLEST_SLAB << cachind));
	request->cache = ccw_cache[cachind];

	/* Allocate memory for the extra data */
	if (data_offset == 0) {
		/* Allocated memory for extra data with kmalloc */
	    request->data = (void *) kmalloc(datasize, CCW_CACHE_TYPE );
		if (request->data == NULL) {
			printk(KERN_WARNING PRINTK_HEADER 
			       "Couldn't allocate data area\n");
			kmem_cache_free(request->cache, request);
			return NULL;
		}
	} else
		/* Extra data already allocated with the request */
		request->data = (void *) ((addr_t) request + data_offset);

	/* Allocate memory for the channel program */
	if (ccw_offset == 0) {
		/* Allocated memory for the channel program with kmalloc */
		request->cpaddr = (ccw1_t *) kmalloc(cplength*sizeof(ccw1_t),
						     CCW_CACHE_TYPE);
		if (request->cpaddr == NULL) {
			printk (KERN_DEBUG PRINTK_HEADER
				"Couldn't allocate ccw area\n");
			if (data_offset == 0)
				kfree(request->data);
			kmem_cache_free(request->cache, request);
			return NULL;
		}
	} else
		/* Channel program already allocated with the request */
		request->cpaddr = (ccw1_t *) ((addr_t) request + ccw_offset);

	memset ( request->data, 0, datasize );
	memset ( request->cpaddr, 0, cplength*sizeof(ccw1_t) );
	strncpy ( (char *)(&request->magic), magic, 4);

	ASCEBC((char *)(&request->magic),4);
	request -> cplength = cplength;
	request -> datasize = datasize;
	/* enqueue request to list of allocated requests */
	enchain(request);
	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 size_needed;

	debug_text_event ( debug_area, 1, "FREE");
	debug_int_event ( debug_area, 1, (long)request);

	/* Sanity checks */
	if ( request == NULL || request->cache == NULL)
                BUG();

        dechain ( request);
	/* Free memory allocated with kmalloc
         * make the same decisions as in ccw_alloc_requets */
	size_needed = (sizeof (ccw_req_t) + 7) & -8;
	if (size_needed + request->datasize <= PAGE_SIZE)
		/* We kept the data with the request */
		size_needed += (request->datasize + 7) & -8;
	else
		kfree(request->data);
	if (size_needed + request->cplength*sizeof(ccw1_t) > PAGE_SIZE)
		/* We kept the CCWs with request */
                kfree(request->cpaddr);
        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;

        /* initialize variables */
	spin_lock_init(&ccwchain_lock);

	/* allocate a debug area */
	debug_area = debug_register( "ccwcache", 2, 4,sizeof(void*));
	if ( debug_area == NULL )
                BUG();

        debug_register_view(debug_area,&debug_hex_ascii_view);
        debug_register_view(debug_area,&debug_raw_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,
					   CCW_CACHE_SLAB_TYPE,
					   NULL, NULL );
		debug_int_event ( debug_area, 1, (long)ccw_cache[cachind]);
		if (ccw_cache[cachind] == NULL)
			panic ("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;
			}
			kmem_cache_destroy(ccw_cache[cachind]);
		}
	}
	debug_unregister( debug_area );
}