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/slab.h>
37 #include <linux/blkdev.h>
38 #include <linux/blkpg.h>
39 #include <linux/hdreg.h> /* HDIO_GETGEO */
40 #include <linux/sysdev.h>
41 #include <linux/bio.h>
42 #include <asm/uaccess.h>
44 #define XPRAM_NAME "xpram"
45 #define XPRAM_DEVS 1 /* one partition */
46 #define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
49 unsigned int size; /* size of xpram segment in pages */
50 unsigned int offset; /* start page of xpram segment */
53 static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
54 static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
55 static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
56 static struct request_queue *xpram_queues[XPRAM_MAX_DEVS];
57 static unsigned int xpram_pages;
58 static int xpram_devs;
61 * Parameter parsing functions.
63 static int __initdata devs = XPRAM_DEVS;
64 static char __initdata *sizes[XPRAM_MAX_DEVS];
66 module_param(devs, int, 0);
67 module_param_array(sizes, charp, NULL, 0);
69 MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
70 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
71 MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \
72 "the defaults are 0s \n" \
73 "All devices with size 0 equally partition the "
74 "remaining space on the expanded strorage not "
75 "claimed by explicit sizes\n");
76 MODULE_LICENSE("GPL");
79 * Copy expanded memory page (4kB) into main memory
81 * page_addr: address of target page
82 * xpage_index: index of expandeded memory page
84 * 0: if operation succeeds
85 * -EIO: if pgin failed
86 * -ENXIO: if xpram has vanished
88 static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
90 int cc = 2; /* return unused cc 2 if pgin traps */
93 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
98 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
109 * Copy a 4kB page of main memory to an expanded memory page
111 * page_addr: address of source page
112 * xpage_index: index of expandeded memory page
114 * 0: if operation succeeds
115 * -EIO: if pgout failed
116 * -ENXIO: if xpram has vanished
118 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
120 int cc = 2; /* return unused cc 2 if pgin traps */
123 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
128 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
139 * Check if xpram is available.
141 static int __init xpram_present(void)
143 unsigned long mem_page;
146 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
149 rc = xpram_page_in(mem_page, 0);
151 return rc ? -ENXIO : 0;
155 * Return index of the last available xpram page.
157 static unsigned long __init xpram_highest_page_index(void)
159 unsigned int page_index, add_bit;
160 unsigned long mem_page;
162 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
167 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
168 while (add_bit > 0) {
169 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
170 page_index |= add_bit;
174 free_page (mem_page);
180 * Block device make request function.
182 static int xpram_make_request(struct request_queue *q, struct bio *bio)
184 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
185 struct bio_vec *bvec;
187 unsigned long page_addr;
191 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
192 /* Request is not page-aligned. */
194 if ((bio->bi_size >> 12) > xdev->size)
195 /* Request size is no page-aligned. */
197 if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
199 index = (bio->bi_sector >> 3) + xdev->offset;
200 bio_for_each_segment(bvec, bio, i) {
201 page_addr = (unsigned long)
202 kmap(bvec->bv_page) + bvec->bv_offset;
203 bytes = bvec->bv_len;
204 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
208 if (bio_data_dir(bio) == READ) {
209 if (xpram_page_in(page_addr, index) != 0)
212 if (xpram_page_out(page_addr, index) != 0)
220 set_bit(BIO_UPTODATE, &bio->bi_flags);
228 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
233 * get geometry: we have to fake one... trim the size to a
234 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
235 * whatever cylinders. Tell also that data starts at sector. 4.
237 size = (xpram_pages * 8) & ~0x3f;
238 geo->cylinders = size >> 6;
245 static struct block_device_operations xpram_devops =
247 .owner = THIS_MODULE,
248 .getgeo = xpram_getgeo,
252 * Setup xpram_sizes array.
254 static int __init xpram_setup_sizes(unsigned long pages)
256 unsigned long mem_needed;
257 unsigned long mem_auto;
258 unsigned long long size;
262 /* Check number of devices. */
263 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
264 pr_err("%d is not a valid number of XPRAM devices\n",devs);
270 * Copy sizes array to xpram_sizes and align partition
271 * sizes to page boundary.
275 for (i = 0; i < xpram_devs; i++) {
277 size = simple_strtoull(sizes[i], &sizes[i], 0);
278 switch (sizes[i][0]) {
287 xpram_sizes[i] = (size + 3) & -4UL;
290 mem_needed += xpram_sizes[i];
295 pr_info(" number of devices (partitions): %d \n", xpram_devs);
296 for (i = 0; i < xpram_devs; i++) {
298 pr_info(" size of partition %d: %u kB\n",
301 pr_info(" size of partition %d to be set "
302 "automatically\n",i);
304 pr_info(" memory needed (for sized partitions): %lu kB\n",
306 pr_info(" partitions to be sized automatically: %d\n",
309 if (mem_needed > pages * 4) {
310 pr_err("Not enough expanded memory available\n");
316 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
317 * else: ; all partitions with zero xpram_sizes[i]
318 * partition equally the remaining space
321 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
322 pr_info(" automatically determined "
323 "partition size: %lu kB\n", mem_auto);
324 for (i = 0; i < xpram_devs; i++)
325 if (xpram_sizes[i] == 0)
326 xpram_sizes[i] = mem_auto;
331 static int __init xpram_setup_blkdev(void)
333 unsigned long offset;
336 for (i = 0; i < xpram_devs; i++) {
337 xpram_disks[i] = alloc_disk(1);
340 xpram_queues[i] = blk_alloc_queue(GFP_KERNEL);
341 if (!xpram_queues[i]) {
342 put_disk(xpram_disks[i]);
345 blk_queue_make_request(xpram_queues[i], xpram_make_request);
346 blk_queue_hardsect_size(xpram_queues[i], 4096);
350 * Register xpram major.
352 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
357 * Setup device structures.
360 for (i = 0; i < xpram_devs; i++) {
361 struct gendisk *disk = xpram_disks[i];
363 xpram_devices[i].size = xpram_sizes[i] / 4;
364 xpram_devices[i].offset = offset;
365 offset += xpram_devices[i].size;
366 disk->major = XPRAM_MAJOR;
367 disk->first_minor = i;
368 disk->fops = &xpram_devops;
369 disk->private_data = &xpram_devices[i];
370 disk->queue = xpram_queues[i];
371 sprintf(disk->disk_name, "slram%d", i);
372 set_capacity(disk, xpram_sizes[i] << 1);
379 blk_cleanup_queue(xpram_queues[i]);
380 put_disk(xpram_disks[i]);
386 * Finally, the init/exit functions.
388 static void __exit xpram_exit(void)
391 for (i = 0; i < xpram_devs; i++) {
392 del_gendisk(xpram_disks[i]);
393 blk_cleanup_queue(xpram_queues[i]);
394 put_disk(xpram_disks[i]);
396 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
399 static int __init xpram_init(void)
403 /* Find out size of expanded memory. */
404 if (xpram_present() != 0) {
405 pr_err("No expanded memory available\n");
408 xpram_pages = xpram_highest_page_index() + 1;
409 pr_info(" %u pages expanded memory found (%lu KB).\n",
410 xpram_pages, (unsigned long) xpram_pages*4);
411 rc = xpram_setup_sizes(xpram_pages);
414 return xpram_setup_blkdev();
417 module_init(xpram_init);
418 module_exit(xpram_exit);