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/blkdev.h>
37 #include <linux/blkpg.h>
38 #include <linux/hdreg.h> /* HDIO_GETGEO */
39 #include <linux/sysdev.h>
40 #include <linux/bio.h>
41 #include <linux/suspend.h>
42 #include <linux/platform_device.h>
43 #include <linux/gfp.h>
44 #include <asm/uaccess.h>
46 #define XPRAM_NAME "xpram"
47 #define XPRAM_DEVS 1 /* one partition */
48 #define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
51 unsigned int size; /* size of xpram segment in pages */
52 unsigned int offset; /* start page of xpram segment */
55 static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
56 static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
57 static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
58 static struct request_queue *xpram_queues[XPRAM_MAX_DEVS];
59 static unsigned int xpram_pages;
60 static int xpram_devs;
63 * Parameter parsing functions.
65 static int __initdata devs = XPRAM_DEVS;
66 static char __initdata *sizes[XPRAM_MAX_DEVS];
68 module_param(devs, int, 0);
69 module_param_array(sizes, charp, NULL, 0);
71 MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
72 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
73 MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \
74 "the defaults are 0s \n" \
75 "All devices with size 0 equally partition the "
76 "remaining space on the expanded strorage not "
77 "claimed by explicit sizes\n");
78 MODULE_LICENSE("GPL");
81 * Copy expanded memory page (4kB) into main memory
83 * page_addr: address of target page
84 * xpage_index: index of expandeded memory page
86 * 0: if operation succeeds
87 * -EIO: if pgin failed
88 * -ENXIO: if xpram has vanished
90 static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
92 int cc = 2; /* return unused cc 2 if pgin traps */
95 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
100 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
111 * Copy a 4kB page of main memory to an expanded memory page
113 * page_addr: address of source page
114 * xpage_index: index of expandeded memory page
116 * 0: if operation succeeds
117 * -EIO: if pgout failed
118 * -ENXIO: if xpram has vanished
120 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
122 int cc = 2; /* return unused cc 2 if pgin traps */
125 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
130 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
141 * Check if xpram is available.
143 static int xpram_present(void)
145 unsigned long mem_page;
148 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
151 rc = xpram_page_in(mem_page, 0);
153 return rc ? -ENXIO : 0;
157 * Return index of the last available xpram page.
159 static unsigned long xpram_highest_page_index(void)
161 unsigned int page_index, add_bit;
162 unsigned long mem_page;
164 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
169 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
170 while (add_bit > 0) {
171 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
172 page_index |= add_bit;
176 free_page (mem_page);
182 * Block device make request function.
184 static int xpram_make_request(struct request_queue *q, struct bio *bio)
186 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
187 struct bio_vec *bvec;
189 unsigned long page_addr;
193 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
194 /* Request is not page-aligned. */
196 if ((bio->bi_size >> 12) > xdev->size)
197 /* Request size is no page-aligned. */
199 if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
201 index = (bio->bi_sector >> 3) + xdev->offset;
202 bio_for_each_segment(bvec, bio, i) {
203 page_addr = (unsigned long)
204 kmap(bvec->bv_page) + bvec->bv_offset;
205 bytes = bvec->bv_len;
206 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
210 if (bio_data_dir(bio) == READ) {
211 if (xpram_page_in(page_addr, index) != 0)
214 if (xpram_page_out(page_addr, index) != 0)
222 set_bit(BIO_UPTODATE, &bio->bi_flags);
230 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
235 * get geometry: we have to fake one... trim the size to a
236 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
237 * whatever cylinders. Tell also that data starts at sector. 4.
239 size = (xpram_pages * 8) & ~0x3f;
240 geo->cylinders = size >> 6;
247 static const struct block_device_operations xpram_devops =
249 .owner = THIS_MODULE,
250 .getgeo = xpram_getgeo,
254 * Setup xpram_sizes array.
256 static int __init xpram_setup_sizes(unsigned long pages)
258 unsigned long mem_needed;
259 unsigned long mem_auto;
260 unsigned long long size;
264 /* Check number of devices. */
265 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
266 pr_err("%d is not a valid number of XPRAM devices\n",devs);
272 * Copy sizes array to xpram_sizes and align partition
273 * sizes to page boundary.
277 for (i = 0; i < xpram_devs; i++) {
279 size = simple_strtoull(sizes[i], &sizes[i], 0);
280 switch (sizes[i][0]) {
289 xpram_sizes[i] = (size + 3) & -4UL;
292 mem_needed += xpram_sizes[i];
297 pr_info(" number of devices (partitions): %d \n", xpram_devs);
298 for (i = 0; i < xpram_devs; i++) {
300 pr_info(" size of partition %d: %u kB\n",
303 pr_info(" size of partition %d to be set "
304 "automatically\n",i);
306 pr_info(" memory needed (for sized partitions): %lu kB\n",
308 pr_info(" partitions to be sized automatically: %d\n",
311 if (mem_needed > pages * 4) {
312 pr_err("Not enough expanded memory available\n");
318 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
319 * else: ; all partitions with zero xpram_sizes[i]
320 * partition equally the remaining space
323 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
324 pr_info(" automatically determined "
325 "partition size: %lu kB\n", mem_auto);
326 for (i = 0; i < xpram_devs; i++)
327 if (xpram_sizes[i] == 0)
328 xpram_sizes[i] = mem_auto;
333 static int __init xpram_setup_blkdev(void)
335 unsigned long offset;
338 for (i = 0; i < xpram_devs; i++) {
339 xpram_disks[i] = alloc_disk(1);
342 xpram_queues[i] = blk_alloc_queue(GFP_KERNEL);
343 if (!xpram_queues[i]) {
344 put_disk(xpram_disks[i]);
347 blk_queue_make_request(xpram_queues[i], xpram_make_request);
348 blk_queue_logical_block_size(xpram_queues[i], 4096);
352 * Register xpram major.
354 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
359 * Setup device structures.
362 for (i = 0; i < xpram_devs; i++) {
363 struct gendisk *disk = xpram_disks[i];
365 xpram_devices[i].size = xpram_sizes[i] / 4;
366 xpram_devices[i].offset = offset;
367 offset += xpram_devices[i].size;
368 disk->major = XPRAM_MAJOR;
369 disk->first_minor = i;
370 disk->fops = &xpram_devops;
371 disk->private_data = &xpram_devices[i];
372 disk->queue = xpram_queues[i];
373 sprintf(disk->disk_name, "slram%d", i);
374 set_capacity(disk, xpram_sizes[i] << 1);
381 blk_cleanup_queue(xpram_queues[i]);
382 put_disk(xpram_disks[i]);
388 * Resume failed: Print error message and call panic.
390 static void xpram_resume_error(const char *message)
392 pr_err("Resuming the system failed: %s\n", message);
393 panic("xpram resume error\n");
397 * Check if xpram setup changed between suspend and resume.
399 static int xpram_restore(struct device *dev)
403 if (xpram_present() != 0)
404 xpram_resume_error("xpram disappeared");
405 if (xpram_pages != xpram_highest_page_index() + 1)
406 xpram_resume_error("Size of xpram changed");
410 static const struct dev_pm_ops xpram_pm_ops = {
411 .restore = xpram_restore,
414 static struct platform_driver xpram_pdrv = {
417 .owner = THIS_MODULE,
422 static struct platform_device *xpram_pdev;
425 * Finally, the init/exit functions.
427 static void __exit xpram_exit(void)
430 for (i = 0; i < xpram_devs; i++) {
431 del_gendisk(xpram_disks[i]);
432 blk_cleanup_queue(xpram_queues[i]);
433 put_disk(xpram_disks[i]);
435 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
436 platform_device_unregister(xpram_pdev);
437 platform_driver_unregister(&xpram_pdrv);
440 static int __init xpram_init(void)
444 /* Find out size of expanded memory. */
445 if (xpram_present() != 0) {
446 pr_err("No expanded memory available\n");
449 xpram_pages = xpram_highest_page_index() + 1;
450 pr_info(" %u pages expanded memory found (%lu KB).\n",
451 xpram_pages, (unsigned long) xpram_pages*4);
452 rc = xpram_setup_sizes(xpram_pages);
455 rc = platform_driver_register(&xpram_pdrv);
458 xpram_pdev = platform_device_register_simple(XPRAM_NAME, -1, NULL, 0);
459 if (IS_ERR(xpram_pdev)) {
460 rc = PTR_ERR(xpram_pdev);
461 goto fail_platform_driver_unregister;
463 rc = xpram_setup_blkdev();
465 goto fail_platform_device_unregister;
468 fail_platform_device_unregister:
469 platform_device_unregister(xpram_pdev);
470 fail_platform_driver_unregister:
471 platform_driver_unregister(&xpram_pdrv);
475 module_init(xpram_init);
476 module_exit(xpram_exit);