rcv reorder queue bugfix
[cor_2_6_31.git] / drivers / s390 / block / xpram.c
blobdb442cd6621ec9d838325c15c2f47cbb102992a1
1 /*
2 * Xpram.c -- the S/390 expanded memory RAM-disk
3 *
4 * significant parts of this code are based on
5 * the sbull device driver presented in
6 * A. Rubini: Linux Device Drivers
8 * Author of XPRAM specific coding: Reinhard Buendgen
9 * buendgen@de.ibm.com
10 * Rewrite for 2.5: Martin Schwidefsky <schwidefsky@de.ibm.com>
12 * External interfaces:
13 * Interfaces to linux kernel
14 * xpram_setup: read kernel parameters
15 * Device specific file operations
16 * xpram_iotcl
17 * xpram_open
19 * "ad-hoc" partitioning:
20 * the expanded memory can be partitioned among several devices
21 * (with different minors). The partitioning set up can be
22 * set by kernel or module parameters (int devs & int sizes[])
24 * Potential future improvements:
25 * generic hard disk support to replace ad-hoc partitioning
28 #define KMSG_COMPONENT "xpram"
29 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/ctype.h> /* isdigit, isxdigit */
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/blkdev.h>
38 #include <linux/blkpg.h>
39 #include <linux/hdreg.h> /* HDIO_GETGEO */
40 #include <linux/sysdev.h>
41 #include <linux/bio.h>
42 #include <linux/suspend.h>
43 #include <linux/platform_device.h>
44 #include <asm/uaccess.h>
45 #include <asm/checksum.h>
47 #define XPRAM_NAME "xpram"
48 #define XPRAM_DEVS 1 /* one partition */
49 #define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
51 typedef struct {
52 unsigned int size; /* size of xpram segment in pages */
53 unsigned int offset; /* start page of xpram segment */
54 unsigned int csum; /* partition checksum for suspend */
55 } xpram_device_t;
57 static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
58 static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
59 static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
60 static struct request_queue *xpram_queues[XPRAM_MAX_DEVS];
61 static unsigned int xpram_pages;
62 static int xpram_devs;
65 * Parameter parsing functions.
67 static int __initdata devs = XPRAM_DEVS;
68 static char __initdata *sizes[XPRAM_MAX_DEVS];
70 module_param(devs, int, 0);
71 module_param_array(sizes, charp, NULL, 0);
73 MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
74 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
75 MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \
76 "the defaults are 0s \n" \
77 "All devices with size 0 equally partition the "
78 "remaining space on the expanded strorage not "
79 "claimed by explicit sizes\n");
80 MODULE_LICENSE("GPL");
83 * Copy expanded memory page (4kB) into main memory
84 * Arguments
85 * page_addr: address of target page
86 * xpage_index: index of expandeded memory page
87 * Return value
88 * 0: if operation succeeds
89 * -EIO: if pgin failed
90 * -ENXIO: if xpram has vanished
92 static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
94 int cc = 2; /* return unused cc 2 if pgin traps */
96 asm volatile(
97 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
98 "0: ipm %0\n"
99 " srl %0,28\n"
100 "1:\n"
101 EX_TABLE(0b,1b)
102 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
103 if (cc == 3)
104 return -ENXIO;
105 if (cc == 2)
106 return -ENXIO;
107 if (cc == 1)
108 return -EIO;
109 return 0;
113 * Copy a 4kB page of main memory to an expanded memory page
114 * Arguments
115 * page_addr: address of source page
116 * xpage_index: index of expandeded memory page
117 * Return value
118 * 0: if operation succeeds
119 * -EIO: if pgout failed
120 * -ENXIO: if xpram has vanished
122 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
124 int cc = 2; /* return unused cc 2 if pgin traps */
126 asm volatile(
127 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
128 "0: ipm %0\n"
129 " srl %0,28\n"
130 "1:\n"
131 EX_TABLE(0b,1b)
132 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
133 if (cc == 3)
134 return -ENXIO;
135 if (cc == 2)
136 return -ENXIO;
137 if (cc == 1)
138 return -EIO;
139 return 0;
143 * Check if xpram is available.
145 static int xpram_present(void)
147 unsigned long mem_page;
148 int rc;
150 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
151 if (!mem_page)
152 return -ENOMEM;
153 rc = xpram_page_in(mem_page, 0);
154 free_page(mem_page);
155 return rc ? -ENXIO : 0;
159 * Return index of the last available xpram page.
161 static unsigned long xpram_highest_page_index(void)
163 unsigned int page_index, add_bit;
164 unsigned long mem_page;
166 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
167 if (!mem_page)
168 return 0;
170 page_index = 0;
171 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
172 while (add_bit > 0) {
173 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
174 page_index |= add_bit;
175 add_bit >>= 1;
178 free_page (mem_page);
180 return page_index;
184 * Block device make request function.
186 static int xpram_make_request(struct request_queue *q, struct bio *bio)
188 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
189 struct bio_vec *bvec;
190 unsigned int index;
191 unsigned long page_addr;
192 unsigned long bytes;
193 int i;
195 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
196 /* Request is not page-aligned. */
197 goto fail;
198 if ((bio->bi_size >> 12) > xdev->size)
199 /* Request size is no page-aligned. */
200 goto fail;
201 if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
202 goto fail;
203 index = (bio->bi_sector >> 3) + xdev->offset;
204 bio_for_each_segment(bvec, bio, i) {
205 page_addr = (unsigned long)
206 kmap(bvec->bv_page) + bvec->bv_offset;
207 bytes = bvec->bv_len;
208 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
209 /* More paranoia. */
210 goto fail;
211 while (bytes > 0) {
212 if (bio_data_dir(bio) == READ) {
213 if (xpram_page_in(page_addr, index) != 0)
214 goto fail;
215 } else {
216 if (xpram_page_out(page_addr, index) != 0)
217 goto fail;
219 page_addr += 4096;
220 bytes -= 4096;
221 index++;
224 set_bit(BIO_UPTODATE, &bio->bi_flags);
225 bio_endio(bio, 0);
226 return 0;
227 fail:
228 bio_io_error(bio);
229 return 0;
232 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
234 unsigned long size;
237 * get geometry: we have to fake one... trim the size to a
238 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
239 * whatever cylinders. Tell also that data starts at sector. 4.
241 size = (xpram_pages * 8) & ~0x3f;
242 geo->cylinders = size >> 6;
243 geo->heads = 4;
244 geo->sectors = 16;
245 geo->start = 4;
246 return 0;
249 static struct block_device_operations xpram_devops =
251 .owner = THIS_MODULE,
252 .getgeo = xpram_getgeo,
256 * Setup xpram_sizes array.
258 static int __init xpram_setup_sizes(unsigned long pages)
260 unsigned long mem_needed;
261 unsigned long mem_auto;
262 unsigned long long size;
263 int mem_auto_no;
264 int i;
266 /* Check number of devices. */
267 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
268 pr_err("%d is not a valid number of XPRAM devices\n",devs);
269 return -EINVAL;
271 xpram_devs = devs;
274 * Copy sizes array to xpram_sizes and align partition
275 * sizes to page boundary.
277 mem_needed = 0;
278 mem_auto_no = 0;
279 for (i = 0; i < xpram_devs; i++) {
280 if (sizes[i]) {
281 size = simple_strtoull(sizes[i], &sizes[i], 0);
282 switch (sizes[i][0]) {
283 case 'g':
284 case 'G':
285 size <<= 20;
286 break;
287 case 'm':
288 case 'M':
289 size <<= 10;
291 xpram_sizes[i] = (size + 3) & -4UL;
293 if (xpram_sizes[i])
294 mem_needed += xpram_sizes[i];
295 else
296 mem_auto_no++;
299 pr_info(" number of devices (partitions): %d \n", xpram_devs);
300 for (i = 0; i < xpram_devs; i++) {
301 if (xpram_sizes[i])
302 pr_info(" size of partition %d: %u kB\n",
303 i, xpram_sizes[i]);
304 else
305 pr_info(" size of partition %d to be set "
306 "automatically\n",i);
308 pr_info(" memory needed (for sized partitions): %lu kB\n",
309 mem_needed);
310 pr_info(" partitions to be sized automatically: %d\n",
311 mem_auto_no);
313 if (mem_needed > pages * 4) {
314 pr_err("Not enough expanded memory available\n");
315 return -EINVAL;
319 * partitioning:
320 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
321 * else: ; all partitions with zero xpram_sizes[i]
322 * partition equally the remaining space
324 if (mem_auto_no) {
325 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
326 pr_info(" automatically determined "
327 "partition size: %lu kB\n", mem_auto);
328 for (i = 0; i < xpram_devs; i++)
329 if (xpram_sizes[i] == 0)
330 xpram_sizes[i] = mem_auto;
332 return 0;
335 static int __init xpram_setup_blkdev(void)
337 unsigned long offset;
338 int i, rc = -ENOMEM;
340 for (i = 0; i < xpram_devs; i++) {
341 xpram_disks[i] = alloc_disk(1);
342 if (!xpram_disks[i])
343 goto out;
344 xpram_queues[i] = blk_alloc_queue(GFP_KERNEL);
345 if (!xpram_queues[i]) {
346 put_disk(xpram_disks[i]);
347 goto out;
349 blk_queue_make_request(xpram_queues[i], xpram_make_request);
350 blk_queue_logical_block_size(xpram_queues[i], 4096);
354 * Register xpram major.
356 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
357 if (rc < 0)
358 goto out;
361 * Setup device structures.
363 offset = 0;
364 for (i = 0; i < xpram_devs; i++) {
365 struct gendisk *disk = xpram_disks[i];
367 xpram_devices[i].size = xpram_sizes[i] / 4;
368 xpram_devices[i].offset = offset;
369 offset += xpram_devices[i].size;
370 disk->major = XPRAM_MAJOR;
371 disk->first_minor = i;
372 disk->fops = &xpram_devops;
373 disk->private_data = &xpram_devices[i];
374 disk->queue = xpram_queues[i];
375 sprintf(disk->disk_name, "slram%d", i);
376 set_capacity(disk, xpram_sizes[i] << 1);
377 add_disk(disk);
380 return 0;
381 out:
382 while (i--) {
383 blk_cleanup_queue(xpram_queues[i]);
384 put_disk(xpram_disks[i]);
386 return rc;
390 * Save checksums for all partitions.
392 static int xpram_save_checksums(void)
394 unsigned long mem_page;
395 int rc, i;
397 rc = 0;
398 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
399 if (!mem_page)
400 return -ENOMEM;
401 for (i = 0; i < xpram_devs; i++) {
402 rc = xpram_page_in(mem_page, xpram_devices[i].offset);
403 if (rc)
404 goto fail;
405 xpram_devices[i].csum = csum_partial((const void *) mem_page,
406 PAGE_SIZE, 0);
408 fail:
409 free_page(mem_page);
410 return rc ? -ENXIO : 0;
414 * Verify checksums for all partitions.
416 static int xpram_validate_checksums(void)
418 unsigned long mem_page;
419 unsigned int csum;
420 int rc, i;
422 rc = 0;
423 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
424 if (!mem_page)
425 return -ENOMEM;
426 for (i = 0; i < xpram_devs; i++) {
427 rc = xpram_page_in(mem_page, xpram_devices[i].offset);
428 if (rc)
429 goto fail;
430 csum = csum_partial((const void *) mem_page, PAGE_SIZE, 0);
431 if (xpram_devices[i].csum != csum) {
432 rc = -EINVAL;
433 goto fail;
436 fail:
437 free_page(mem_page);
438 return rc ? -ENXIO : 0;
442 * Resume failed: Print error message and call panic.
444 static void xpram_resume_error(const char *message)
446 pr_err("Resuming the system failed: %s\n", message);
447 panic("xpram resume error\n");
451 * Check if xpram setup changed between suspend and resume.
453 static int xpram_restore(struct device *dev)
455 if (!xpram_pages)
456 return 0;
457 if (xpram_present() != 0)
458 xpram_resume_error("xpram disappeared");
459 if (xpram_pages != xpram_highest_page_index() + 1)
460 xpram_resume_error("Size of xpram changed");
461 if (xpram_validate_checksums())
462 xpram_resume_error("Data of xpram changed");
463 return 0;
467 * Save necessary state in suspend.
469 static int xpram_freeze(struct device *dev)
471 return xpram_save_checksums();
474 static struct dev_pm_ops xpram_pm_ops = {
475 .freeze = xpram_freeze,
476 .restore = xpram_restore,
479 static struct platform_driver xpram_pdrv = {
480 .driver = {
481 .name = XPRAM_NAME,
482 .owner = THIS_MODULE,
483 .pm = &xpram_pm_ops,
487 static struct platform_device *xpram_pdev;
490 * Finally, the init/exit functions.
492 static void __exit xpram_exit(void)
494 int i;
495 for (i = 0; i < xpram_devs; i++) {
496 del_gendisk(xpram_disks[i]);
497 blk_cleanup_queue(xpram_queues[i]);
498 put_disk(xpram_disks[i]);
500 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
501 platform_device_unregister(xpram_pdev);
502 platform_driver_unregister(&xpram_pdrv);
505 static int __init xpram_init(void)
507 int rc;
509 /* Find out size of expanded memory. */
510 if (xpram_present() != 0) {
511 pr_err("No expanded memory available\n");
512 return -ENODEV;
514 xpram_pages = xpram_highest_page_index() + 1;
515 pr_info(" %u pages expanded memory found (%lu KB).\n",
516 xpram_pages, (unsigned long) xpram_pages*4);
517 rc = xpram_setup_sizes(xpram_pages);
518 if (rc)
519 return rc;
520 rc = platform_driver_register(&xpram_pdrv);
521 if (rc)
522 return rc;
523 xpram_pdev = platform_device_register_simple(XPRAM_NAME, -1, NULL, 0);
524 if (IS_ERR(xpram_pdev)) {
525 rc = PTR_ERR(xpram_pdev);
526 goto fail_platform_driver_unregister;
528 rc = xpram_setup_blkdev();
529 if (rc)
530 goto fail_platform_device_unregister;
531 return 0;
533 fail_platform_device_unregister:
534 platform_device_unregister(xpram_pdev);
535 fail_platform_driver_unregister:
536 platform_driver_unregister(&xpram_pdrv);
537 return rc;
540 module_init(xpram_init);
541 module_exit(xpram_exit);