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 <linux/devfs_fs_kernel.h>
40 #include <asm/uaccess.h>
42 #define XPRAM_NAME "xpram"
43 #define XPRAM_DEVS 1 /* one partition */
44 #define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
46 #define PRINT_DEBUG(x...) printk(KERN_DEBUG XPRAM_NAME " debug:" x)
47 #define PRINT_INFO(x...) printk(KERN_INFO XPRAM_NAME " info:" x)
48 #define PRINT_WARN(x...) printk(KERN_WARNING XPRAM_NAME " warning:" x)
49 #define PRINT_ERR(x...) printk(KERN_ERR XPRAM_NAME " error:" x)
52 static struct sysdev_class xpram_sysclass
= {
53 set_kset_name("xpram"),
56 static struct sys_device xpram_sys_device
= {
58 .cls
= &xpram_sysclass
,
62 unsigned int size
; /* size of xpram segment in pages */
63 unsigned int offset
; /* start page of xpram segment */
66 static xpram_device_t xpram_devices
[XPRAM_MAX_DEVS
];
67 static unsigned int xpram_sizes
[XPRAM_MAX_DEVS
];
68 static struct gendisk
*xpram_disks
[XPRAM_MAX_DEVS
];
69 static unsigned int xpram_pages
;
70 static int xpram_devs
;
73 * Parameter parsing functions.
75 static int devs
= XPRAM_DEVS
;
76 static unsigned int sizes
[XPRAM_MAX_DEVS
];
78 module_param(devs
, int, 0);
79 module_param_array(sizes
, int, NULL
, 0);
81 MODULE_PARM_DESC(devs
, "number of devices (\"partitions\"), " \
82 "the default is " __MODULE_STRING(XPRAM_DEVS
) "\n");
83 MODULE_PARM_DESC(sizes
, "list of device (partition) sizes " \
84 "the defaults are 0s \n" \
85 "All devices with size 0 equally partition the "
86 "remaining space on the expanded strorage not "
87 "claimed by explicit sizes\n");
88 MODULE_LICENSE("GPL");
92 * Parses the kernel parameters given in the kernel parameter line.
93 * The expected format is
94 * <number_of_partitions>[","<partition_size>]*
96 * devices is a positive integer that initializes xpram_devs
97 * each size is a non-negative integer possibly followed by a
98 * magnitude (k,K,m,M,g,G), the list of sizes initialises
102 * str: substring of kernel parameter line that contains xprams
105 * Result 0 on success, -EINVAL else -- only for Version > 2.3
108 * the global variabls devs is set to the value of
109 * <number_of_partitions> and sizes[i] is set to the i-th
110 * partition size (if provided). A parsing error of a value
111 * results in this value being set to -EINVAL.
113 static int __init
xpram_setup (char *str
)
118 devs
= simple_strtoul(str
, &cp
, 10);
119 if (cp
<= str
|| devs
> XPRAM_MAX_DEVS
)
121 for (i
= 0; (i
< devs
) && (*cp
++ == ','); i
++) {
122 sizes
[i
] = simple_strtoul(cp
, &cp
, 10);
123 if (*cp
== 'g' || *cp
== 'G') {
126 } else if (*cp
== 'm' || *cp
== 'M') {
129 } else if (*cp
== 'k' || *cp
== 'K')
131 while (isspace(*cp
)) cp
++;
133 if (*cp
== ',' && i
>= devs
)
134 PRINT_WARN("partition sizes list has too many entries.\n");
136 PRINT_WARN("ignored '%s' at end of parameter string.\n", cp
);
140 __setup("xpram_parts=", xpram_setup
);
144 * Copy expanded memory page (4kB) into main memory
146 * page_addr: address of target page
147 * xpage_index: index of expandeded memory page
149 * 0: if operation succeeds
150 * -EIO: if pgin failed
151 * -ENXIO: if xpram has vanished
153 static int xpram_page_in (unsigned long page_addr
, unsigned int xpage_index
)
157 __asm__
__volatile__ (
158 " lhi %0,2\n" /* return unused cc 2 if pgin traps */
159 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
164 ".section __ex_table,\"a\"\n"
169 ".section __ex_table,\"a\"\n"
175 : "a" (__pa(page_addr
)), "a" (xpage_index
)
180 PRINT_ERR("expanded storage lost!\n");
184 PRINT_ERR("page in failed for page index %u.\n",
192 * Copy a 4kB page of main memory to an expanded memory page
194 * page_addr: address of source page
195 * xpage_index: index of expandeded memory page
197 * 0: if operation succeeds
198 * -EIO: if pgout failed
199 * -ENXIO: if xpram has vanished
201 static long xpram_page_out (unsigned long page_addr
, unsigned int xpage_index
)
205 __asm__
__volatile__ (
206 " lhi %0,2\n" /* return unused cc 2 if pgout traps */
207 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
212 ".section __ex_table,\"a\"\n"
217 ".section __ex_table,\"a\"\n"
223 : "a" (__pa(page_addr
)), "a" (xpage_index
)
228 PRINT_ERR("expanded storage lost!\n");
232 PRINT_ERR("page out failed for page index %u.\n",
240 * Check if xpram is available.
242 static int __init
xpram_present(void)
244 unsigned long mem_page
;
247 mem_page
= (unsigned long) __get_free_page(GFP_KERNEL
);
250 rc
= xpram_page_in(mem_page
, 0);
252 return rc
? -ENXIO
: 0;
256 * Return index of the last available xpram page.
258 static unsigned long __init
xpram_highest_page_index(void)
260 unsigned int page_index
, add_bit
;
261 unsigned long mem_page
;
263 mem_page
= (unsigned long) __get_free_page(GFP_KERNEL
);
268 add_bit
= 1ULL << (sizeof(unsigned int)*8 - 1);
269 while (add_bit
> 0) {
270 if (xpram_page_in(mem_page
, page_index
| add_bit
) == 0)
271 page_index
|= add_bit
;
275 free_page (mem_page
);
281 * Block device make request function.
283 static int xpram_make_request(request_queue_t
*q
, struct bio
*bio
)
285 xpram_device_t
*xdev
= bio
->bi_bdev
->bd_disk
->private_data
;
286 struct bio_vec
*bvec
;
288 unsigned long page_addr
;
292 if ((bio
->bi_sector
& 7) != 0 || (bio
->bi_size
& 4095) != 0)
293 /* Request is not page-aligned. */
295 if ((bio
->bi_size
>> 12) > xdev
->size
)
296 /* Request size is no page-aligned. */
298 if ((bio
->bi_sector
>> 3) > 0xffffffffU
- xdev
->offset
)
300 index
= (bio
->bi_sector
>> 3) + xdev
->offset
;
301 bio_for_each_segment(bvec
, bio
, i
) {
302 page_addr
= (unsigned long)
303 kmap(bvec
->bv_page
) + bvec
->bv_offset
;
304 bytes
= bvec
->bv_len
;
305 if ((page_addr
& 4095) != 0 || (bytes
& 4095) != 0)
309 if (bio_data_dir(bio
) == READ
) {
310 if (xpram_page_in(page_addr
, index
) != 0)
313 if (xpram_page_out(page_addr
, index
) != 0)
321 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
322 bytes
= bio
->bi_size
;
324 bio
->bi_end_io(bio
, bytes
, 0);
327 bio_io_error(bio
, bio
->bi_size
);
331 static int xpram_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
336 * get geometry: we have to fake one... trim the size to a
337 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
338 * whatever cylinders. Tell also that data starts at sector. 4.
340 size
= (xpram_pages
* 8) & ~0x3f;
341 geo
->cylinders
= size
>> 6;
348 static struct block_device_operations xpram_devops
=
350 .owner
= THIS_MODULE
,
351 .getgeo
= xpram_getgeo
,
355 * Setup xpram_sizes array.
357 static int __init
xpram_setup_sizes(unsigned long pages
)
359 unsigned long mem_needed
;
360 unsigned long mem_auto
;
364 /* Check number of devices. */
365 if (devs
<= 0 || devs
> XPRAM_MAX_DEVS
) {
366 PRINT_ERR("invalid number %d of devices\n",devs
);
372 * Copy sizes array to xpram_sizes and align partition
373 * sizes to page boundary.
377 for (i
= 0; i
< xpram_devs
; i
++) {
378 xpram_sizes
[i
] = (sizes
[i
] + 3) & -4UL;
380 mem_needed
+= xpram_sizes
[i
];
385 PRINT_INFO(" number of devices (partitions): %d \n", xpram_devs
);
386 for (i
= 0; i
< xpram_devs
; i
++) {
388 PRINT_INFO(" size of partition %d: %u kB\n",
391 PRINT_INFO(" size of partition %d to be set "
392 "automatically\n",i
);
394 PRINT_DEBUG(" memory needed (for sized partitions): %lu kB\n",
396 PRINT_DEBUG(" partitions to be sized automatically: %d\n",
399 if (mem_needed
> pages
* 4) {
400 PRINT_ERR("Not enough expanded memory available\n");
406 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
407 * else: ; all partitions with zero xpram_sizes[i]
408 * partition equally the remaining space
411 mem_auto
= ((pages
- mem_needed
/ 4) / mem_auto_no
) * 4;
412 PRINT_INFO(" automatically determined "
413 "partition size: %lu kB\n", mem_auto
);
414 for (i
= 0; i
< xpram_devs
; i
++)
415 if (xpram_sizes
[i
] == 0)
416 xpram_sizes
[i
] = mem_auto
;
421 static struct request_queue
*xpram_queue
;
423 static int __init
xpram_setup_blkdev(void)
425 unsigned long offset
;
428 for (i
= 0; i
< xpram_devs
; i
++) {
429 struct gendisk
*disk
= alloc_disk(1);
432 xpram_disks
[i
] = disk
;
436 * Register xpram major.
438 rc
= register_blkdev(XPRAM_MAJOR
, XPRAM_NAME
);
443 * Assign the other needed values: make request function, sizes and
444 * hardsect size. All the minor devices feature the same value.
446 xpram_queue
= blk_alloc_queue(GFP_KERNEL
);
451 blk_queue_make_request(xpram_queue
, xpram_make_request
);
452 blk_queue_hardsect_size(xpram_queue
, 4096);
455 * Setup device structures.
458 for (i
= 0; i
< xpram_devs
; i
++) {
459 struct gendisk
*disk
= xpram_disks
[i
];
461 xpram_devices
[i
].size
= xpram_sizes
[i
] / 4;
462 xpram_devices
[i
].offset
= offset
;
463 offset
+= xpram_devices
[i
].size
;
464 disk
->major
= XPRAM_MAJOR
;
465 disk
->first_minor
= i
;
466 disk
->fops
= &xpram_devops
;
467 disk
->private_data
= &xpram_devices
[i
];
468 disk
->queue
= xpram_queue
;
469 sprintf(disk
->disk_name
, "slram%d", i
);
470 sprintf(disk
->devfs_name
, "slram/%d", i
);
471 set_capacity(disk
, xpram_sizes
[i
] << 1);
477 unregister_blkdev(XPRAM_MAJOR
, XPRAM_NAME
);
480 put_disk(xpram_disks
[i
]);
485 * Finally, the init/exit functions.
487 static void __exit
xpram_exit(void)
490 for (i
= 0; i
< xpram_devs
; i
++) {
491 del_gendisk(xpram_disks
[i
]);
492 put_disk(xpram_disks
[i
]);
494 unregister_blkdev(XPRAM_MAJOR
, XPRAM_NAME
);
495 blk_cleanup_queue(xpram_queue
);
496 sysdev_unregister(&xpram_sys_device
);
497 sysdev_class_unregister(&xpram_sysclass
);
500 static int __init
xpram_init(void)
504 /* Find out size of expanded memory. */
505 if (xpram_present() != 0) {
506 PRINT_WARN("No expanded memory available\n");
509 xpram_pages
= xpram_highest_page_index();
510 PRINT_INFO(" %u pages expanded memory found (%lu KB).\n",
511 xpram_pages
, (unsigned long) xpram_pages
*4);
512 rc
= xpram_setup_sizes(xpram_pages
);
515 rc
= sysdev_class_register(&xpram_sysclass
);
519 rc
= sysdev_register(&xpram_sys_device
);
521 sysdev_class_unregister(&xpram_sysclass
);
524 rc
= xpram_setup_blkdev();
526 sysdev_unregister(&xpram_sys_device
);
530 module_init(xpram_init
);
531 module_exit(xpram_exit
);