2 * Xpram.c -- the S/390 expanded memory RAM-disk
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
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
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) */
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 */
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
85 * page_addr: address of target page
86 * xpage_index: index of expandeded memory page
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 */
97 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
102 : "+d" (cc
) : "a" (__pa(page_addr
)), "d" (xpage_index
) : "cc");
113 * Copy a 4kB page of main memory to an expanded memory page
115 * page_addr: address of source page
116 * xpage_index: index of expandeded memory page
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 */
127 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
132 : "+d" (cc
) : "a" (__pa(page_addr
)), "d" (xpage_index
) : "cc");
143 * Check if xpram is available.
145 static int xpram_present(void)
147 unsigned long mem_page
;
150 mem_page
= (unsigned long) __get_free_page(GFP_KERNEL
);
153 rc
= xpram_page_in(mem_page
, 0);
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
);
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
;
178 free_page (mem_page
);
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
;
191 unsigned long page_addr
;
195 if ((bio
->bi_sector
& 7) != 0 || (bio
->bi_size
& 4095) != 0)
196 /* Request is not page-aligned. */
198 if ((bio
->bi_size
>> 12) > xdev
->size
)
199 /* Request size is no page-aligned. */
201 if ((bio
->bi_sector
>> 3) > 0xffffffffU
- xdev
->offset
)
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)
212 if (bio_data_dir(bio
) == READ
) {
213 if (xpram_page_in(page_addr
, index
) != 0)
216 if (xpram_page_out(page_addr
, index
) != 0)
224 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
232 static int xpram_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
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;
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
;
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
);
274 * Copy sizes array to xpram_sizes and align partition
275 * sizes to page boundary.
279 for (i
= 0; i
< xpram_devs
; i
++) {
281 size
= simple_strtoull(sizes
[i
], &sizes
[i
], 0);
282 switch (sizes
[i
][0]) {
291 xpram_sizes
[i
] = (size
+ 3) & -4UL;
294 mem_needed
+= xpram_sizes
[i
];
299 pr_info(" number of devices (partitions): %d \n", xpram_devs
);
300 for (i
= 0; i
< xpram_devs
; i
++) {
302 pr_info(" size of partition %d: %u kB\n",
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",
310 pr_info(" partitions to be sized automatically: %d\n",
313 if (mem_needed
> pages
* 4) {
314 pr_err("Not enough expanded memory available\n");
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
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
;
335 static int __init
xpram_setup_blkdev(void)
337 unsigned long offset
;
340 for (i
= 0; i
< xpram_devs
; i
++) {
341 xpram_disks
[i
] = alloc_disk(1);
344 xpram_queues
[i
] = blk_alloc_queue(GFP_KERNEL
);
345 if (!xpram_queues
[i
]) {
346 put_disk(xpram_disks
[i
]);
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
);
361 * Setup device structures.
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);
383 blk_cleanup_queue(xpram_queues
[i
]);
384 put_disk(xpram_disks
[i
]);
390 * Save checksums for all partitions.
392 static int xpram_save_checksums(void)
394 unsigned long mem_page
;
398 mem_page
= (unsigned long) __get_free_page(GFP_KERNEL
);
401 for (i
= 0; i
< xpram_devs
; i
++) {
402 rc
= xpram_page_in(mem_page
, xpram_devices
[i
].offset
);
405 xpram_devices
[i
].csum
= csum_partial((const void *) 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
;
423 mem_page
= (unsigned long) __get_free_page(GFP_KERNEL
);
426 for (i
= 0; i
< xpram_devs
; i
++) {
427 rc
= xpram_page_in(mem_page
, xpram_devices
[i
].offset
);
430 csum
= csum_partial((const void *) mem_page
, PAGE_SIZE
, 0);
431 if (xpram_devices
[i
].csum
!= csum
) {
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
)
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");
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
= {
482 .owner
= THIS_MODULE
,
487 static struct platform_device
*xpram_pdev
;
490 * Finally, the init/exit functions.
492 static void __exit
xpram_exit(void)
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)
509 /* Find out size of expanded memory. */
510 if (xpram_present() != 0) {
511 pr_err("No expanded memory available\n");
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
);
520 rc
= platform_driver_register(&xpram_pdrv
);
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();
530 goto fail_platform_device_unregister
;
533 fail_platform_device_unregister
:
534 platform_device_unregister(xpram_pdev
);
535 fail_platform_driver_unregister
:
536 platform_driver_unregister(&xpram_pdrv
);
540 module_init(xpram_init
);
541 module_exit(xpram_exit
);