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)
52 unsigned int size; /* size of xpram segment in pages */
53 unsigned int offset; /* start page of xpram segment */
56 static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
57 static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
58 static struct gendisk *xpram_disks[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)
94 __asm__ __volatile__ (
95 " lhi %0,2\n" /* return unused cc 2 if pgin traps */
96 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
101 ".section __ex_table,\"a\"\n"
106 ".section __ex_table,\"a\"\n"
112 : "a" (__pa(page_addr)), "a" (xpage_index)
117 PRINT_ERR("expanded storage lost!\n");
121 PRINT_ERR("page in failed for page index %u.\n",
129 * Copy a 4kB page of main memory to an expanded memory page
131 * page_addr: address of source page
132 * xpage_index: index of expandeded memory page
134 * 0: if operation succeeds
135 * -EIO: if pgout failed
136 * -ENXIO: if xpram has vanished
138 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
142 __asm__ __volatile__ (
143 " lhi %0,2\n" /* return unused cc 2 if pgout traps */
144 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
149 ".section __ex_table,\"a\"\n"
154 ".section __ex_table,\"a\"\n"
160 : "a" (__pa(page_addr)), "a" (xpage_index)
165 PRINT_ERR("expanded storage lost!\n");
169 PRINT_ERR("page out failed for page index %u.\n",
177 * Check if xpram is available.
179 static int __init xpram_present(void)
181 unsigned long mem_page;
184 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
187 rc = xpram_page_in(mem_page, 0);
189 return rc ? -ENXIO : 0;
193 * Return index of the last available xpram page.
195 static unsigned long __init xpram_highest_page_index(void)
197 unsigned int page_index, add_bit;
198 unsigned long mem_page;
200 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
205 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
206 while (add_bit > 0) {
207 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
208 page_index |= add_bit;
212 free_page (mem_page);
218 * Block device make request function.
220 static int xpram_make_request(request_queue_t *q, struct bio *bio)
222 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
223 struct bio_vec *bvec;
225 unsigned long page_addr;
229 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
230 /* Request is not page-aligned. */
232 if ((bio->bi_size >> 12) > xdev->size)
233 /* Request size is no page-aligned. */
235 if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
237 index = (bio->bi_sector >> 3) + xdev->offset;
238 bio_for_each_segment(bvec, bio, i) {
239 page_addr = (unsigned long)
240 kmap(bvec->bv_page) + bvec->bv_offset;
241 bytes = bvec->bv_len;
242 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
246 if (bio_data_dir(bio) == READ) {
247 if (xpram_page_in(page_addr, index) != 0)
250 if (xpram_page_out(page_addr, index) != 0)
258 set_bit(BIO_UPTODATE, &bio->bi_flags);
259 bytes = bio->bi_size;
261 bio->bi_end_io(bio, bytes, 0);
264 bio_io_error(bio, bio->bi_size);
268 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
273 * get geometry: we have to fake one... trim the size to a
274 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
275 * whatever cylinders. Tell also that data starts at sector. 4.
277 size = (xpram_pages * 8) & ~0x3f;
278 geo->cylinders = size >> 6;
285 static struct block_device_operations xpram_devops =
287 .owner = THIS_MODULE,
288 .getgeo = xpram_getgeo,
292 * Setup xpram_sizes array.
294 static int __init xpram_setup_sizes(unsigned long pages)
296 unsigned long mem_needed;
297 unsigned long mem_auto;
298 unsigned long long size;
302 /* Check number of devices. */
303 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
304 PRINT_ERR("invalid number %d of devices\n",devs);
310 * Copy sizes array to xpram_sizes and align partition
311 * sizes to page boundary.
315 for (i = 0; i < xpram_devs; i++) {
317 size = simple_strtoull(sizes[i], &sizes[i], 0);
318 switch (sizes[i][0]) {
327 xpram_sizes[i] = (size + 3) & -4UL;
330 mem_needed += xpram_sizes[i];
335 PRINT_INFO(" number of devices (partitions): %d \n", xpram_devs);
336 for (i = 0; i < xpram_devs; i++) {
338 PRINT_INFO(" size of partition %d: %u kB\n",
341 PRINT_INFO(" size of partition %d to be set "
342 "automatically\n",i);
344 PRINT_DEBUG(" memory needed (for sized partitions): %lu kB\n",
346 PRINT_DEBUG(" partitions to be sized automatically: %d\n",
349 if (mem_needed > pages * 4) {
350 PRINT_ERR("Not enough expanded memory available\n");
356 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
357 * else: ; all partitions with zero xpram_sizes[i]
358 * partition equally the remaining space
361 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
362 PRINT_INFO(" automatically determined "
363 "partition size: %lu kB\n", mem_auto);
364 for (i = 0; i < xpram_devs; i++)
365 if (xpram_sizes[i] == 0)
366 xpram_sizes[i] = mem_auto;
371 static struct request_queue *xpram_queue;
373 static int __init xpram_setup_blkdev(void)
375 unsigned long offset;
378 for (i = 0; i < xpram_devs; i++) {
379 struct gendisk *disk = alloc_disk(1);
382 xpram_disks[i] = disk;
386 * Register xpram major.
388 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
393 * Assign the other needed values: make request function, sizes and
394 * hardsect size. All the minor devices feature the same value.
396 xpram_queue = blk_alloc_queue(GFP_KERNEL);
401 blk_queue_make_request(xpram_queue, xpram_make_request);
402 blk_queue_hardsect_size(xpram_queue, 4096);
405 * Setup device structures.
408 for (i = 0; i < xpram_devs; i++) {
409 struct gendisk *disk = xpram_disks[i];
411 xpram_devices[i].size = xpram_sizes[i] / 4;
412 xpram_devices[i].offset = offset;
413 offset += xpram_devices[i].size;
414 disk->major = XPRAM_MAJOR;
415 disk->first_minor = i;
416 disk->fops = &xpram_devops;
417 disk->private_data = &xpram_devices[i];
418 disk->queue = xpram_queue;
419 sprintf(disk->disk_name, "slram%d", i);
420 set_capacity(disk, xpram_sizes[i] << 1);
426 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
429 put_disk(xpram_disks[i]);
434 * Finally, the init/exit functions.
436 static void __exit xpram_exit(void)
439 for (i = 0; i < xpram_devs; i++) {
440 del_gendisk(xpram_disks[i]);
441 put_disk(xpram_disks[i]);
443 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
444 blk_cleanup_queue(xpram_queue);
447 static int __init xpram_init(void)
451 /* Find out size of expanded memory. */
452 if (xpram_present() != 0) {
453 PRINT_WARN("No expanded memory available\n");
456 xpram_pages = xpram_highest_page_index();
457 PRINT_INFO(" %u pages expanded memory found (%lu KB).\n",
458 xpram_pages, (unsigned long) xpram_pages*4);
459 rc = xpram_setup_sizes(xpram_pages);
462 return xpram_setup_blkdev();
465 module_init(xpram_init);
466 module_exit(xpram_exit);