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 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/ctype.h> /* isdigit, isxdigit */
31 #include <linux/errno.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/blkdev.h>
35 #include <linux/blkpg.h>
36 #include <linux/hdreg.h> /* HDIO_GETGEO */
37 #include <linux/sysdev.h>
38 #include <linux/bio.h>
39 #include <asm/uaccess.h>
41 #define XPRAM_NAME "xpram"
42 #define XPRAM_DEVS 1 /* one partition */
43 #define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
45 #define PRINT_DEBUG(x...) printk(KERN_DEBUG XPRAM_NAME " debug:" x)
46 #define PRINT_INFO(x...) printk(KERN_INFO XPRAM_NAME " info:" x)
47 #define PRINT_WARN(x...) printk(KERN_WARNING XPRAM_NAME " warning:" x)
48 #define PRINT_ERR(x...) printk(KERN_ERR XPRAM_NAME " error:" x)
51 static struct sysdev_class xpram_sysclass = {
52 set_kset_name("xpram"),
55 static struct sys_device xpram_sys_device = {
57 .cls = &xpram_sysclass,
61 unsigned int size; /* size of xpram segment in pages */
62 unsigned int offset; /* start page of xpram segment */
65 static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
66 static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
67 static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
68 static unsigned int xpram_pages;
69 static int xpram_devs;
72 * Parameter parsing functions.
74 static int devs = XPRAM_DEVS;
75 static unsigned int sizes[XPRAM_MAX_DEVS];
77 module_param(devs, int, 0);
78 module_param_array(sizes, int, NULL, 0);
80 MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
81 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
82 MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \
83 "the defaults are 0s \n" \
84 "All devices with size 0 equally partition the "
85 "remaining space on the expanded strorage not "
86 "claimed by explicit sizes\n");
87 MODULE_LICENSE("GPL");
91 * Parses the kernel parameters given in the kernel parameter line.
92 * The expected format is
93 * <number_of_partitions>[","<partition_size>]*
95 * devices is a positive integer that initializes xpram_devs
96 * each size is a non-negative integer possibly followed by a
97 * magnitude (k,K,m,M,g,G), the list of sizes initialises
101 * str: substring of kernel parameter line that contains xprams
104 * Result 0 on success, -EINVAL else -- only for Version > 2.3
107 * the global variabls devs is set to the value of
108 * <number_of_partitions> and sizes[i] is set to the i-th
109 * partition size (if provided). A parsing error of a value
110 * results in this value being set to -EINVAL.
112 static int __init xpram_setup (char *str)
117 devs = simple_strtoul(str, &cp, 10);
118 if (cp <= str || devs > XPRAM_MAX_DEVS)
120 for (i = 0; (i < devs) && (*cp++ == ','); i++) {
121 sizes[i] = simple_strtoul(cp, &cp, 10);
122 if (*cp == 'g' || *cp == 'G') {
125 } else if (*cp == 'm' || *cp == 'M') {
128 } else if (*cp == 'k' || *cp == 'K')
130 while (isspace(*cp)) cp++;
132 if (*cp == ',' && i >= devs)
133 PRINT_WARN("partition sizes list has too many entries.\n");
135 PRINT_WARN("ignored '%s' at end of parameter string.\n", cp);
139 __setup("xpram_parts=", xpram_setup);
143 * Copy expanded memory page (4kB) into main memory
145 * page_addr: address of target page
146 * xpage_index: index of expandeded memory page
148 * 0: if operation succeeds
149 * -EIO: if pgin failed
150 * -ENXIO: if xpram has vanished
152 static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
156 __asm__ __volatile__ (
157 " lhi %0,2\n" /* return unused cc 2 if pgin traps */
158 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
163 ".section __ex_table,\"a\"\n"
168 ".section __ex_table,\"a\"\n"
174 : "a" (__pa(page_addr)), "a" (xpage_index)
179 PRINT_ERR("expanded storage lost!\n");
183 PRINT_ERR("page in failed for page index %u.\n",
191 * Copy a 4kB page of main memory to an expanded memory page
193 * page_addr: address of source page
194 * xpage_index: index of expandeded memory page
196 * 0: if operation succeeds
197 * -EIO: if pgout failed
198 * -ENXIO: if xpram has vanished
200 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
204 __asm__ __volatile__ (
205 " lhi %0,2\n" /* return unused cc 2 if pgout traps */
206 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
211 ".section __ex_table,\"a\"\n"
216 ".section __ex_table,\"a\"\n"
222 : "a" (__pa(page_addr)), "a" (xpage_index)
227 PRINT_ERR("expanded storage lost!\n");
231 PRINT_ERR("page out failed for page index %u.\n",
239 * Check if xpram is available.
241 static int __init xpram_present(void)
243 unsigned long mem_page;
246 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
249 rc = xpram_page_in(mem_page, 0);
251 return rc ? -ENXIO : 0;
255 * Return index of the last available xpram page.
257 static unsigned long __init xpram_highest_page_index(void)
259 unsigned int page_index, add_bit;
260 unsigned long mem_page;
262 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
267 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
268 while (add_bit > 0) {
269 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
270 page_index |= add_bit;
274 free_page (mem_page);
280 * Block device make request function.
282 static int xpram_make_request(request_queue_t *q, struct bio *bio)
284 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
285 struct bio_vec *bvec;
287 unsigned long page_addr;
291 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
292 /* Request is not page-aligned. */
294 if ((bio->bi_size >> 12) > xdev->size)
295 /* Request size is no page-aligned. */
297 if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
299 index = (bio->bi_sector >> 3) + xdev->offset;
300 bio_for_each_segment(bvec, bio, i) {
301 page_addr = (unsigned long)
302 kmap(bvec->bv_page) + bvec->bv_offset;
303 bytes = bvec->bv_len;
304 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
308 if (bio_data_dir(bio) == READ) {
309 if (xpram_page_in(page_addr, index) != 0)
312 if (xpram_page_out(page_addr, index) != 0)
320 set_bit(BIO_UPTODATE, &bio->bi_flags);
321 bytes = bio->bi_size;
323 bio->bi_end_io(bio, bytes, 0);
326 bio_io_error(bio, bio->bi_size);
330 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
335 * get geometry: we have to fake one... trim the size to a
336 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
337 * whatever cylinders. Tell also that data starts at sector. 4.
339 size = (xpram_pages * 8) & ~0x3f;
340 geo->cylinders = size >> 6;
347 static struct block_device_operations xpram_devops =
349 .owner = THIS_MODULE,
350 .getgeo = xpram_getgeo,
354 * Setup xpram_sizes array.
356 static int __init xpram_setup_sizes(unsigned long pages)
358 unsigned long mem_needed;
359 unsigned long mem_auto;
363 /* Check number of devices. */
364 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
365 PRINT_ERR("invalid number %d of devices\n",devs);
371 * Copy sizes array to xpram_sizes and align partition
372 * sizes to page boundary.
376 for (i = 0; i < xpram_devs; i++) {
377 xpram_sizes[i] = (sizes[i] + 3) & -4UL;
379 mem_needed += xpram_sizes[i];
384 PRINT_INFO(" number of devices (partitions): %d \n", xpram_devs);
385 for (i = 0; i < xpram_devs; i++) {
387 PRINT_INFO(" size of partition %d: %u kB\n",
390 PRINT_INFO(" size of partition %d to be set "
391 "automatically\n",i);
393 PRINT_DEBUG(" memory needed (for sized partitions): %lu kB\n",
395 PRINT_DEBUG(" partitions to be sized automatically: %d\n",
398 if (mem_needed > pages * 4) {
399 PRINT_ERR("Not enough expanded memory available\n");
405 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
406 * else: ; all partitions with zero xpram_sizes[i]
407 * partition equally the remaining space
410 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
411 PRINT_INFO(" automatically determined "
412 "partition size: %lu kB\n", mem_auto);
413 for (i = 0; i < xpram_devs; i++)
414 if (xpram_sizes[i] == 0)
415 xpram_sizes[i] = mem_auto;
420 static struct request_queue *xpram_queue;
422 static int __init xpram_setup_blkdev(void)
424 unsigned long offset;
427 for (i = 0; i < xpram_devs; i++) {
428 struct gendisk *disk = alloc_disk(1);
431 xpram_disks[i] = disk;
435 * Register xpram major.
437 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
442 * Assign the other needed values: make request function, sizes and
443 * hardsect size. All the minor devices feature the same value.
445 xpram_queue = blk_alloc_queue(GFP_KERNEL);
450 blk_queue_make_request(xpram_queue, xpram_make_request);
451 blk_queue_hardsect_size(xpram_queue, 4096);
454 * Setup device structures.
457 for (i = 0; i < xpram_devs; i++) {
458 struct gendisk *disk = xpram_disks[i];
460 xpram_devices[i].size = xpram_sizes[i] / 4;
461 xpram_devices[i].offset = offset;
462 offset += xpram_devices[i].size;
463 disk->major = XPRAM_MAJOR;
464 disk->first_minor = i;
465 disk->fops = &xpram_devops;
466 disk->private_data = &xpram_devices[i];
467 disk->queue = xpram_queue;
468 sprintf(disk->disk_name, "slram%d", i);
469 set_capacity(disk, xpram_sizes[i] << 1);
475 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
478 put_disk(xpram_disks[i]);
483 * Finally, the init/exit functions.
485 static void __exit xpram_exit(void)
488 for (i = 0; i < xpram_devs; i++) {
489 del_gendisk(xpram_disks[i]);
490 put_disk(xpram_disks[i]);
492 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
493 blk_cleanup_queue(xpram_queue);
494 sysdev_unregister(&xpram_sys_device);
495 sysdev_class_unregister(&xpram_sysclass);
498 static int __init xpram_init(void)
502 /* Find out size of expanded memory. */
503 if (xpram_present() != 0) {
504 PRINT_WARN("No expanded memory available\n");
507 xpram_pages = xpram_highest_page_index();
508 PRINT_INFO(" %u pages expanded memory found (%lu KB).\n",
509 xpram_pages, (unsigned long) xpram_pages*4);
510 rc = xpram_setup_sizes(xpram_pages);
513 rc = sysdev_class_register(&xpram_sysclass);
517 rc = sysdev_register(&xpram_sys_device);
519 sysdev_class_unregister(&xpram_sysclass);
522 rc = xpram_setup_blkdev();
524 sysdev_unregister(&xpram_sys_device);
528 module_init(xpram_init);
529 module_exit(xpram_exit);