Merge commit 'origin/master' into next
[pandora-kernel.git] / drivers / s390 / block / xpram.c
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
7  *
8  * Author of XPRAM specific coding: Reinhard Buendgen
9  *                                  buendgen@de.ibm.com
10  * Rewrite for 2.5: Martin Schwidefsky <schwidefsky@de.ibm.com>
11  *
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
18  *
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[])
23  *
24  * Potential future improvements:
25  *   generic hard disk support to replace ad-hoc partitioning
26  */
27
28 #define KMSG_COMPONENT "xpram"
29 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30
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>
46
47 #define XPRAM_NAME      "xpram"
48 #define XPRAM_DEVS      1       /* one partition */
49 #define XPRAM_MAX_DEVS  32      /* maximal number of devices (partitions) */
50
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;
56
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;
63
64 /*
65  * Parameter parsing functions.
66  */
67 static int __initdata devs = XPRAM_DEVS;
68 static char __initdata *sizes[XPRAM_MAX_DEVS];
69
70 module_param(devs, int, 0);
71 module_param_array(sizes, charp, NULL, 0);
72
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");
81
82 /*
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
91  */
92 static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
93 {
94         int cc = 2;     /* return unused cc 2 if pgin traps */
95
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;
110 }
111
112 /*
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
121  */
122 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
123 {
124         int cc = 2;     /* return unused cc 2 if pgin traps */
125
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;
140 }
141
142 /*
143  * Check if xpram is available.
144  */
145 static int xpram_present(void)
146 {
147         unsigned long mem_page;
148         int rc;
149
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;
156 }
157
158 /*
159  * Return index of the last available xpram page.
160  */
161 static unsigned long xpram_highest_page_index(void)
162 {
163         unsigned int page_index, add_bit;
164         unsigned long mem_page;
165
166         mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
167         if (!mem_page)
168                 return 0;
169
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;
176         }
177
178         free_page (mem_page);
179
180         return page_index;
181 }
182
183 /*
184  * Block device make request function.
185  */
186 static int xpram_make_request(struct request_queue *q, struct bio *bio)
187 {
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;
194
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;
218                         }
219                         page_addr += 4096;
220                         bytes -= 4096;
221                         index++;
222                 }
223         }
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;
230 }
231
232 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
233 {
234         unsigned long size;
235
236         /*
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.
240          */
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;
247 }
248
249 static struct block_device_operations xpram_devops =
250 {
251         .owner  = THIS_MODULE,
252         .getgeo = xpram_getgeo,
253 };
254
255 /*
256  * Setup xpram_sizes array.
257  */
258 static int __init xpram_setup_sizes(unsigned long pages)
259 {
260         unsigned long mem_needed;
261         unsigned long mem_auto;
262         unsigned long long size;
263         int mem_auto_no;
264         int i;
265
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;
270         }
271         xpram_devs = devs;
272
273         /*
274          * Copy sizes array to xpram_sizes and align partition
275          * sizes to page boundary.
276          */
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;
290                         }
291                         xpram_sizes[i] = (size + 3) & -4UL;
292                 }
293                 if (xpram_sizes[i])
294                         mem_needed += xpram_sizes[i];
295                 else
296                         mem_auto_no++;
297         }
298         
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);
307         }
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);
312
313         if (mem_needed > pages * 4) {
314                 pr_err("Not enough expanded memory available\n");
315                 return -EINVAL;
316         }
317
318         /*
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
323          */
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;
331         }
332         return 0;
333 }
334
335 static int __init xpram_setup_blkdev(void)
336 {
337         unsigned long offset;
338         int i, rc = -ENOMEM;
339
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;
348                 }
349                 blk_queue_make_request(xpram_queues[i], xpram_make_request);
350                 blk_queue_logical_block_size(xpram_queues[i], 4096);
351         }
352
353         /*
354          * Register xpram major.
355          */
356         rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
357         if (rc < 0)
358                 goto out;
359
360         /*
361          * Setup device structures.
362          */
363         offset = 0;
364         for (i = 0; i < xpram_devs; i++) {
365                 struct gendisk *disk = xpram_disks[i];
366
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);
378         }
379
380         return 0;
381 out:
382         while (i--) {
383                 blk_cleanup_queue(xpram_queues[i]);
384                 put_disk(xpram_disks[i]);
385         }
386         return rc;
387 }
388
389 /*
390  * Save checksums for all partitions.
391  */
392 static int xpram_save_checksums(void)
393 {
394         unsigned long mem_page;
395         int rc, i;
396
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);
407         }
408 fail:
409         free_page(mem_page);
410         return rc ? -ENXIO : 0;
411 }
412
413 /*
414  * Verify checksums for all partitions.
415  */
416 static int xpram_validate_checksums(void)
417 {
418         unsigned long mem_page;
419         unsigned int csum;
420         int rc, i;
421
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;
434                 }
435         }
436 fail:
437         free_page(mem_page);
438         return rc ? -ENXIO : 0;
439 }
440
441 /*
442  * Resume failed: Print error message and call panic.
443  */
444 static void xpram_resume_error(const char *message)
445 {
446         pr_err("Resume error: %s\n", message);
447         panic("xpram resume error\n");
448 }
449
450 /*
451  * Check if xpram setup changed between suspend and resume.
452  */
453 static int xpram_restore(struct device *dev)
454 {
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;
464 }
465
466 /*
467  * Save necessary state in suspend.
468  */
469 static int xpram_freeze(struct device *dev)
470 {
471         return xpram_save_checksums();
472 }
473
474 static struct dev_pm_ops xpram_pm_ops = {
475         .freeze         = xpram_freeze,
476         .restore        = xpram_restore,
477 };
478
479 static struct platform_driver xpram_pdrv = {
480         .driver = {
481                 .name   = XPRAM_NAME,
482                 .owner  = THIS_MODULE,
483                 .pm     = &xpram_pm_ops,
484         },
485 };
486
487 static struct platform_device *xpram_pdev;
488
489 /*
490  * Finally, the init/exit functions.
491  */
492 static void __exit xpram_exit(void)
493 {
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]);
499         }
500         unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
501         platform_device_unregister(xpram_pdev);
502         platform_driver_unregister(&xpram_pdrv);
503 }
504
505 static int __init xpram_init(void)
506 {
507         int rc;
508
509         /* Find out size of expanded memory. */
510         if (xpram_present() != 0) {
511                 pr_err("No expanded memory available\n");
512                 return -ENODEV;
513         }
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;
527         }
528         rc = xpram_setup_blkdev();
529         if (rc)
530                 goto fail_platform_device_unregister;
531         return 0;
532
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;
538 }
539
540 module_init(xpram_init);
541 module_exit(xpram_exit);