1 // SPDX-License-Identifier: GPL-2.0
3 * Xpram.c -- the S/390 expanded memory RAM-disk
5 * significant parts of this code are based on
6 * the sbull device driver presented in
7 * A. Rubini: Linux Device Drivers
9 * Author of XPRAM specific coding: Reinhard Buendgen
11 * Rewrite for 2.5: Martin Schwidefsky <schwidefsky@de.ibm.com>
13 * External interfaces:
14 * Interfaces to linux kernel
15 * xpram_setup: read kernel parameters
16 * Device specific file operations
20 * "ad-hoc" partitioning:
21 * the expanded memory can be partitioned among several devices
22 * (with different minors). The partitioning set up can be
23 * set by kernel or module parameters (int devs & int sizes[])
25 * Potential future improvements:
26 * generic hard disk support to replace ad-hoc partitioning
29 #define KMSG_COMPONENT "xpram"
30 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/ctype.h> /* isdigit, isxdigit */
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/blkdev.h>
38 #include <linux/blkpg.h>
39 #include <linux/hdreg.h> /* HDIO_GETGEO */
40 #include <linux/device.h>
41 #include <linux/bio.h>
42 #include <linux/gfp.h>
43 #include <linux/uaccess.h>
45 #define XPRAM_NAME "xpram"
46 #define XPRAM_DEVS 1 /* one partition */
47 #define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
50 unsigned int size; /* size of xpram segment in pages */
51 unsigned int offset; /* start page of xpram segment */
54 static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
55 static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
56 static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
57 static struct request_queue *xpram_queues[XPRAM_MAX_DEVS];
58 static unsigned int xpram_pages;
59 static int xpram_devs;
62 * Parameter parsing functions.
64 static int devs = XPRAM_DEVS;
65 static char *sizes[XPRAM_MAX_DEVS];
67 module_param(devs, int, 0);
68 module_param_array(sizes, charp, NULL, 0);
70 MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
71 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
72 MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \
73 "the defaults are 0s \n" \
74 "All devices with size 0 equally partition the "
75 "remaining space on the expanded strorage not "
76 "claimed by explicit sizes\n");
77 MODULE_LICENSE("GPL");
80 * Copy expanded memory page (4kB) into main memory
82 * page_addr: address of target page
83 * xpage_index: index of expandeded memory page
85 * 0: if operation succeeds
86 * -EIO: if pgin failed
87 * -ENXIO: if xpram has vanished
89 static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
91 int cc = 2; /* return unused cc 2 if pgin traps */
94 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
99 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
110 * Copy a 4kB page of main memory to an expanded memory page
112 * page_addr: address of source page
113 * xpage_index: index of expandeded memory page
115 * 0: if operation succeeds
116 * -EIO: if pgout failed
117 * -ENXIO: if xpram has vanished
119 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
121 int cc = 2; /* return unused cc 2 if pgin traps */
124 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
129 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
140 * Check if xpram is available.
142 static int __init xpram_present(void)
144 unsigned long mem_page;
147 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
150 rc = xpram_page_in(mem_page, 0);
152 return rc ? -ENXIO : 0;
156 * Return index of the last available xpram page.
158 static unsigned long __init xpram_highest_page_index(void)
160 unsigned int page_index, add_bit;
161 unsigned long mem_page;
163 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
168 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
169 while (add_bit > 0) {
170 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
171 page_index |= add_bit;
175 free_page (mem_page);
181 * Block device make request function.
183 static blk_qc_t xpram_submit_bio(struct bio *bio)
185 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
187 struct bvec_iter iter;
189 unsigned long page_addr;
192 blk_queue_split(&bio);
194 if ((bio->bi_iter.bi_sector & 7) != 0 ||
195 (bio->bi_iter.bi_size & 4095) != 0)
196 /* Request is not page-aligned. */
198 if ((bio->bi_iter.bi_size >> 12) > xdev->size)
199 /* Request size is no page-aligned. */
201 if ((bio->bi_iter.bi_sector >> 3) > 0xffffffffU - xdev->offset)
203 index = (bio->bi_iter.bi_sector >> 3) + xdev->offset;
204 bio_for_each_segment(bvec, bio, iter) {
205 page_addr = (unsigned long)
206 kmap(bvec.bv_page) + bvec.bv_offset;
208 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
212 if (bio_data_dir(bio) == READ) {
213 if (xpram_page_in(page_addr, index) != 0)
216 if (xpram_page_out(page_addr, index) != 0)
225 return BLK_QC_T_NONE;
228 return BLK_QC_T_NONE;
231 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
236 * get geometry: we have to fake one... trim the size to a
237 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
238 * whatever cylinders. Tell also that data starts at sector. 4.
240 size = (xpram_pages * 8) & ~0x3f;
241 geo->cylinders = size >> 6;
248 static const struct block_device_operations xpram_devops =
250 .owner = THIS_MODULE,
251 .submit_bio = xpram_submit_bio,
252 .getgeo = xpram_getgeo,
256 * Setup xpram_sizes array.
258 static int __init xpram_setup_sizes(unsigned long pages)
260 unsigned long mem_needed;
261 unsigned long mem_auto;
262 unsigned long long size;
267 /* Check number of devices. */
268 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
269 pr_err("%d is not a valid number of XPRAM devices\n",devs);
275 * Copy sizes array to xpram_sizes and align partition
276 * sizes to page boundary.
280 for (i = 0; i < xpram_devs; i++) {
282 size = simple_strtoull(sizes[i], &sizes_end, 0);
283 switch (*sizes_end) {
292 xpram_sizes[i] = (size + 3) & -4UL;
295 mem_needed += xpram_sizes[i];
300 pr_info(" number of devices (partitions): %d \n", xpram_devs);
301 for (i = 0; i < xpram_devs; i++) {
303 pr_info(" size of partition %d: %u kB\n",
306 pr_info(" size of partition %d to be set "
307 "automatically\n",i);
309 pr_info(" memory needed (for sized partitions): %lu kB\n",
311 pr_info(" partitions to be sized automatically: %d\n",
314 if (mem_needed > pages * 4) {
315 pr_err("Not enough expanded memory available\n");
321 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
322 * else: ; all partitions with zero xpram_sizes[i]
323 * partition equally the remaining space
326 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
327 pr_info(" automatically determined "
328 "partition size: %lu kB\n", mem_auto);
329 for (i = 0; i < xpram_devs; i++)
330 if (xpram_sizes[i] == 0)
331 xpram_sizes[i] = mem_auto;
336 static int __init xpram_setup_blkdev(void)
338 unsigned long offset;
341 for (i = 0; i < xpram_devs; i++) {
342 xpram_disks[i] = alloc_disk(1);
345 xpram_queues[i] = blk_alloc_queue(NUMA_NO_NODE);
346 if (!xpram_queues[i]) {
347 put_disk(xpram_disks[i]);
350 blk_queue_flag_set(QUEUE_FLAG_NONROT, xpram_queues[i]);
351 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, xpram_queues[i]);
352 blk_queue_logical_block_size(xpram_queues[i], 4096);
356 * Register xpram major.
358 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
363 * Setup device structures.
366 for (i = 0; i < xpram_devs; i++) {
367 struct gendisk *disk = xpram_disks[i];
369 xpram_devices[i].size = xpram_sizes[i] / 4;
370 xpram_devices[i].offset = offset;
371 offset += xpram_devices[i].size;
372 disk->major = XPRAM_MAJOR;
373 disk->first_minor = i;
374 disk->fops = &xpram_devops;
375 disk->private_data = &xpram_devices[i];
376 disk->queue = xpram_queues[i];
377 sprintf(disk->disk_name, "slram%d", i);
378 set_capacity(disk, xpram_sizes[i] << 1);
385 blk_cleanup_queue(xpram_queues[i]);
386 put_disk(xpram_disks[i]);
392 * Finally, the init/exit functions.
394 static void __exit xpram_exit(void)
397 for (i = 0; i < xpram_devs; i++) {
398 del_gendisk(xpram_disks[i]);
399 blk_cleanup_queue(xpram_queues[i]);
400 put_disk(xpram_disks[i]);
402 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
405 static int __init xpram_init(void)
409 /* Find out size of expanded memory. */
410 if (xpram_present() != 0) {
411 pr_err("No expanded memory available\n");
414 xpram_pages = xpram_highest_page_index() + 1;
415 pr_info(" %u pages expanded memory found (%lu KB).\n",
416 xpram_pages, (unsigned long) xpram_pages*4);
417 rc = xpram_setup_sizes(xpram_pages);
420 return xpram_setup_blkdev();
423 module_init(xpram_init);
424 module_exit(xpram_exit);