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 */
163 #ifndef CONFIG_ARCH_S390X
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 */
211 #ifndef CONFIG_ARCH_S390X
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_ioctl (struct inode
*inode
, struct file
*filp
,
332 unsigned int cmd
, unsigned long arg
)
334 struct hd_geometry __user
*geo
;
336 if (cmd
!= HDIO_GETGEO
)
339 * get geometry: we have to fake one... trim the size to a
340 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
341 * whatever cylinders. Tell also that data starts at sector. 4.
343 geo
= (struct hd_geometry __user
*) arg
;
344 size
= (xpram_pages
* 8) & ~0x3f;
345 put_user(size
>> 6, &geo
->cylinders
);
346 put_user(4, &geo
->heads
);
347 put_user(16, &geo
->sectors
);
348 put_user(4, &geo
->start
);
352 static struct block_device_operations xpram_devops
=
354 .owner
= THIS_MODULE
,
355 .ioctl
= xpram_ioctl
,
359 * Setup xpram_sizes array.
361 static int __init
xpram_setup_sizes(unsigned long pages
)
363 unsigned long mem_needed
;
364 unsigned long mem_auto
;
368 /* Check number of devices. */
369 if (devs
<= 0 || devs
> XPRAM_MAX_DEVS
) {
370 PRINT_ERR("invalid number %d of devices\n",devs
);
376 * Copy sizes array to xpram_sizes and align partition
377 * sizes to page boundary.
381 for (i
= 0; i
< xpram_devs
; i
++) {
382 xpram_sizes
[i
] = (sizes
[i
] + 3) & -4UL;
384 mem_needed
+= xpram_sizes
[i
];
389 PRINT_INFO(" number of devices (partitions): %d \n", xpram_devs
);
390 for (i
= 0; i
< xpram_devs
; i
++) {
392 PRINT_INFO(" size of partition %d: %u kB\n",
395 PRINT_INFO(" size of partition %d to be set "
396 "automatically\n",i
);
398 PRINT_DEBUG(" memory needed (for sized partitions): %lu kB\n",
400 PRINT_DEBUG(" partitions to be sized automatically: %d\n",
403 if (mem_needed
> pages
* 4) {
404 PRINT_ERR("Not enough expanded memory available\n");
410 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
411 * else: ; all partitions with zero xpram_sizes[i]
412 * partition equally the remaining space
415 mem_auto
= ((pages
- mem_needed
/ 4) / mem_auto_no
) * 4;
416 PRINT_INFO(" automatically determined "
417 "partition size: %lu kB\n", mem_auto
);
418 for (i
= 0; i
< xpram_devs
; i
++)
419 if (xpram_sizes
[i
] == 0)
420 xpram_sizes
[i
] = mem_auto
;
425 static struct request_queue
*xpram_queue
;
427 static int __init
xpram_setup_blkdev(void)
429 unsigned long offset
;
432 for (i
= 0; i
< xpram_devs
; i
++) {
433 struct gendisk
*disk
= alloc_disk(1);
436 xpram_disks
[i
] = disk
;
440 * Register xpram major.
442 rc
= register_blkdev(XPRAM_MAJOR
, XPRAM_NAME
);
446 devfs_mk_dir("slram");
449 * Assign the other needed values: make request function, sizes and
450 * hardsect size. All the minor devices feature the same value.
452 xpram_queue
= blk_alloc_queue(GFP_KERNEL
);
457 blk_queue_make_request(xpram_queue
, xpram_make_request
);
458 blk_queue_hardsect_size(xpram_queue
, 4096);
461 * Setup device structures.
464 for (i
= 0; i
< xpram_devs
; i
++) {
465 struct gendisk
*disk
= xpram_disks
[i
];
467 xpram_devices
[i
].size
= xpram_sizes
[i
] / 4;
468 xpram_devices
[i
].offset
= offset
;
469 offset
+= xpram_devices
[i
].size
;
470 disk
->major
= XPRAM_MAJOR
;
471 disk
->first_minor
= i
;
472 disk
->fops
= &xpram_devops
;
473 disk
->private_data
= &xpram_devices
[i
];
474 disk
->queue
= xpram_queue
;
475 sprintf(disk
->disk_name
, "slram%d", i
);
476 sprintf(disk
->devfs_name
, "slram/%d", i
);
477 set_capacity(disk
, xpram_sizes
[i
] << 1);
483 devfs_remove("slram");
484 unregister_blkdev(XPRAM_MAJOR
, XPRAM_NAME
);
487 put_disk(xpram_disks
[i
]);
492 * Finally, the init/exit functions.
494 static void __exit
xpram_exit(void)
497 for (i
= 0; i
< xpram_devs
; i
++) {
498 del_gendisk(xpram_disks
[i
]);
499 put_disk(xpram_disks
[i
]);
501 unregister_blkdev(XPRAM_MAJOR
, XPRAM_NAME
);
502 devfs_remove("slram");
503 blk_cleanup_queue(xpram_queue
);
504 sysdev_unregister(&xpram_sys_device
);
505 sysdev_class_unregister(&xpram_sysclass
);
508 static int __init
xpram_init(void)
512 /* Find out size of expanded memory. */
513 if (xpram_present() != 0) {
514 PRINT_WARN("No expanded memory available\n");
517 xpram_pages
= xpram_highest_page_index();
518 PRINT_INFO(" %u pages expanded memory found (%lu KB).\n",
519 xpram_pages
, (unsigned long) xpram_pages
*4);
520 rc
= xpram_setup_sizes(xpram_pages
);
523 rc
= sysdev_class_register(&xpram_sysclass
);
527 rc
= sysdev_register(&xpram_sys_device
);
529 sysdev_class_unregister(&xpram_sysclass
);
532 rc
= xpram_setup_blkdev();
534 sysdev_unregister(&xpram_sys_device
);
538 module_init(xpram_init
);
539 module_exit(xpram_exit
);