blob: 116d1b3eeb157093002ea32edbf7c45133dc695b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Xpram.c -- the S/390 expanded memory RAM-disk
3 *
4 * significant parts of this code are based on
5 * the sbull device driver presented in
6 * A. Rubini: Linux Device Drivers
7 *
8 * Author of XPRAM specific coding: Reinhard Buendgen
9 * buendgen@de.ibm.com
10 * Rewrite for 2.5: Martin Schwidefsky <schwidefsky@de.ibm.com>
11 *
12 * External interfaces:
13 * Interfaces to linux kernel
14 * xpram_setup: read kernel parameters
15 * Device specific file operations
16 * xpram_iotcl
17 * xpram_open
18 *
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[])
23 *
24 * Potential future improvements:
25 * generic hard disk support to replace ad-hoc partitioning
26 */
27
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +010028#define KMSG_COMPONENT "xpram"
29#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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>
Michael Holzheu14532092009-06-16 10:30:27 +020042#include <linux/suspend.h>
43#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
46#define XPRAM_NAME "xpram"
47#define XPRAM_DEVS 1 /* one partition */
48#define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050typedef struct {
51 unsigned int size; /* size of xpram segment in pages */
52 unsigned int offset; /* start page of xpram segment */
53} xpram_device_t;
54
55static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
56static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
57static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
Martin Schwidefsky3ce66092008-10-10 21:33:24 +020058static struct request_queue *xpram_queues[XPRAM_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static unsigned int xpram_pages;
60static int xpram_devs;
61
62/*
63 * Parameter parsing functions.
64 */
Heiko Carstens5c898ba2006-07-12 16:40:14 +020065static int __initdata devs = XPRAM_DEVS;
66static char __initdata *sizes[XPRAM_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68module_param(devs, int, 0);
Heiko Carstens5c898ba2006-07-12 16:40:14 +020069module_param_array(sizes, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
72 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
73MODULE_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");
78MODULE_LICENSE("GPL");
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * Copy expanded memory page (4kB) into main memory
82 * Arguments
83 * page_addr: address of target page
84 * xpage_index: index of expandeded memory page
85 * Return value
86 * 0: if operation succeeds
87 * -EIO: if pgin failed
88 * -ENXIO: if xpram has vanished
89 */
90static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
91{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020092 int cc = 2; /* return unused cc 2 if pgin traps */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020094 asm volatile(
95 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
96 "0: ipm %0\n"
97 " srl %0,28\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020099 EX_TABLE(0b,1b)
100 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (cc == 3)
102 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200103 if (cc == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200105 if (cc == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108}
109
110/*
111 * Copy a 4kB page of main memory to an expanded memory page
112 * Arguments
113 * page_addr: address of source page
114 * xpage_index: index of expandeded memory page
115 * Return value
116 * 0: if operation succeeds
117 * -EIO: if pgout failed
118 * -ENXIO: if xpram has vanished
119 */
120static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
121{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200122 int cc = 2; /* return unused cc 2 if pgin traps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200124 asm volatile(
125 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
126 "0: ipm %0\n"
127 " srl %0,28\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200129 EX_TABLE(0b,1b)
130 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (cc == 3)
132 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200133 if (cc == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200135 if (cc == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return 0;
138}
139
140/*
141 * Check if xpram is available.
142 */
Michael Holzheu14532092009-06-16 10:30:27 +0200143static int xpram_present(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 unsigned long mem_page;
146 int rc;
147
148 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
149 if (!mem_page)
150 return -ENOMEM;
151 rc = xpram_page_in(mem_page, 0);
152 free_page(mem_page);
153 return rc ? -ENXIO : 0;
154}
155
156/*
157 * Return index of the last available xpram page.
158 */
Michael Holzheu14532092009-06-16 10:30:27 +0200159static unsigned long xpram_highest_page_index(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 unsigned int page_index, add_bit;
162 unsigned long mem_page;
163
164 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
165 if (!mem_page)
166 return 0;
167
168 page_index = 0;
169 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
170 while (add_bit > 0) {
171 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
172 page_index |= add_bit;
173 add_bit >>= 1;
174 }
175
176 free_page (mem_page);
177
178 return page_index;
179}
180
181/*
182 * Block device make request function.
183 */
Jens Axboe165125e2007-07-24 09:28:11 +0200184static int xpram_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
187 struct bio_vec *bvec;
188 unsigned int index;
189 unsigned long page_addr;
190 unsigned long bytes;
191 int i;
192
193 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
194 /* Request is not page-aligned. */
195 goto fail;
196 if ((bio->bi_size >> 12) > xdev->size)
197 /* Request size is no page-aligned. */
198 goto fail;
199 if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
200 goto fail;
201 index = (bio->bi_sector >> 3) + xdev->offset;
202 bio_for_each_segment(bvec, bio, i) {
203 page_addr = (unsigned long)
204 kmap(bvec->bv_page) + bvec->bv_offset;
205 bytes = bvec->bv_len;
206 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
207 /* More paranoia. */
208 goto fail;
209 while (bytes > 0) {
210 if (bio_data_dir(bio) == READ) {
211 if (xpram_page_in(page_addr, index) != 0)
212 goto fail;
213 } else {
214 if (xpram_page_out(page_addr, index) != 0)
215 goto fail;
216 }
217 page_addr += 4096;
218 bytes -= 4096;
219 index++;
220 }
221 }
222 set_bit(BIO_UPTODATE, &bio->bi_flags);
Martin Schwidefsky14622222007-10-12 16:11:53 +0200223 bio_endio(bio, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
225fail:
NeilBrown6712ecf2007-09-27 12:47:43 +0200226 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
228}
229
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800230static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 unsigned long size;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /*
235 * get geometry: we have to fake one... trim the size to a
236 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
237 * whatever cylinders. Tell also that data starts at sector. 4.
238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 size = (xpram_pages * 8) & ~0x3f;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800240 geo->cylinders = size >> 6;
241 geo->heads = 4;
242 geo->sectors = 16;
243 geo->start = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245}
246
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700247static const struct block_device_operations xpram_devops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 .owner = THIS_MODULE,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800250 .getgeo = xpram_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
253/*
254 * Setup xpram_sizes array.
255 */
256static int __init xpram_setup_sizes(unsigned long pages)
257{
258 unsigned long mem_needed;
259 unsigned long mem_auto;
Heiko Carstensf257b062006-07-17 16:09:23 +0200260 unsigned long long size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int mem_auto_no;
262 int i;
263
264 /* Check number of devices. */
265 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100266 pr_err("%d is not a valid number of XPRAM devices\n",devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return -EINVAL;
268 }
269 xpram_devs = devs;
270
271 /*
272 * Copy sizes array to xpram_sizes and align partition
273 * sizes to page boundary.
274 */
275 mem_needed = 0;
276 mem_auto_no = 0;
277 for (i = 0; i < xpram_devs; i++) {
Heiko Carstensf257b062006-07-17 16:09:23 +0200278 if (sizes[i]) {
279 size = simple_strtoull(sizes[i], &sizes[i], 0);
280 switch (sizes[i][0]) {
281 case 'g':
282 case 'G':
283 size <<= 20;
284 break;
285 case 'm':
286 case 'M':
287 size <<= 10;
288 }
289 xpram_sizes[i] = (size + 3) & -4UL;
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (xpram_sizes[i])
292 mem_needed += xpram_sizes[i];
293 else
294 mem_auto_no++;
295 }
296
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100297 pr_info(" number of devices (partitions): %d \n", xpram_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 for (i = 0; i < xpram_devs; i++) {
299 if (xpram_sizes[i])
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100300 pr_info(" size of partition %d: %u kB\n",
301 i, xpram_sizes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 else
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100303 pr_info(" size of partition %d to be set "
304 "automatically\n",i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100306 pr_info(" memory needed (for sized partitions): %lu kB\n",
307 mem_needed);
308 pr_info(" partitions to be sized automatically: %d\n",
309 mem_auto_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 if (mem_needed > pages * 4) {
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100312 pr_err("Not enough expanded memory available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -EINVAL;
314 }
315
316 /*
317 * partitioning:
318 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
319 * else: ; all partitions with zero xpram_sizes[i]
320 * partition equally the remaining space
321 */
322 if (mem_auto_no) {
323 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100324 pr_info(" automatically determined "
325 "partition size: %lu kB\n", mem_auto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 for (i = 0; i < xpram_devs; i++)
327 if (xpram_sizes[i] == 0)
328 xpram_sizes[i] = mem_auto;
329 }
330 return 0;
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static int __init xpram_setup_blkdev(void)
334{
335 unsigned long offset;
336 int i, rc = -ENOMEM;
337
338 for (i = 0; i < xpram_devs; i++) {
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200339 xpram_disks[i] = alloc_disk(1);
340 if (!xpram_disks[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto out;
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200342 xpram_queues[i] = blk_alloc_queue(GFP_KERNEL);
343 if (!xpram_queues[i]) {
344 put_disk(xpram_disks[i]);
345 goto out;
346 }
347 blk_queue_make_request(xpram_queues[i], xpram_make_request);
Martin K. Petersene1defc42009-05-22 17:17:49 -0400348 blk_queue_logical_block_size(xpram_queues[i], 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 /*
352 * Register xpram major.
353 */
354 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
355 if (rc < 0)
356 goto out;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * Setup device structures.
360 */
361 offset = 0;
362 for (i = 0; i < xpram_devs; i++) {
363 struct gendisk *disk = xpram_disks[i];
364
365 xpram_devices[i].size = xpram_sizes[i] / 4;
366 xpram_devices[i].offset = offset;
367 offset += xpram_devices[i].size;
368 disk->major = XPRAM_MAJOR;
369 disk->first_minor = i;
370 disk->fops = &xpram_devops;
371 disk->private_data = &xpram_devices[i];
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200372 disk->queue = xpram_queues[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 sprintf(disk->disk_name, "slram%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 set_capacity(disk, xpram_sizes[i] << 1);
375 add_disk(disk);
376 }
377
378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379out:
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200380 while (i--) {
381 blk_cleanup_queue(xpram_queues[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 put_disk(xpram_disks[i]);
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return rc;
385}
386
387/*
Michael Holzheu14532092009-06-16 10:30:27 +0200388 * Resume failed: Print error message and call panic.
389 */
390static void xpram_resume_error(const char *message)
391{
Christian Borntraeger2c48c4d2009-07-07 16:37:11 +0200392 pr_err("Resuming the system failed: %s\n", message);
Michael Holzheu14532092009-06-16 10:30:27 +0200393 panic("xpram resume error\n");
394}
395
396/*
397 * Check if xpram setup changed between suspend and resume.
398 */
399static int xpram_restore(struct device *dev)
400{
401 if (!xpram_pages)
402 return 0;
403 if (xpram_present() != 0)
404 xpram_resume_error("xpram disappeared");
405 if (xpram_pages != xpram_highest_page_index() + 1)
406 xpram_resume_error("Size of xpram changed");
Michael Holzheu14532092009-06-16 10:30:27 +0200407 return 0;
408}
409
Michael Holzheu14532092009-06-16 10:30:27 +0200410static struct dev_pm_ops xpram_pm_ops = {
Michael Holzheu14532092009-06-16 10:30:27 +0200411 .restore = xpram_restore,
412};
413
414static struct platform_driver xpram_pdrv = {
415 .driver = {
416 .name = XPRAM_NAME,
417 .owner = THIS_MODULE,
418 .pm = &xpram_pm_ops,
419 },
420};
421
422static struct platform_device *xpram_pdev;
423
424/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * Finally, the init/exit functions.
426 */
427static void __exit xpram_exit(void)
428{
429 int i;
430 for (i = 0; i < xpram_devs; i++) {
431 del_gendisk(xpram_disks[i]);
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200432 blk_cleanup_queue(xpram_queues[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 put_disk(xpram_disks[i]);
434 }
435 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
Michael Holzheu14532092009-06-16 10:30:27 +0200436 platform_device_unregister(xpram_pdev);
437 platform_driver_unregister(&xpram_pdrv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440static int __init xpram_init(void)
441{
442 int rc;
443
444 /* Find out size of expanded memory. */
445 if (xpram_present() != 0) {
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100446 pr_err("No expanded memory available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -ENODEV;
448 }
Christian Borntraegere620c492006-09-20 15:59:32 +0200449 xpram_pages = xpram_highest_page_index() + 1;
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100450 pr_info(" %u pages expanded memory found (%lu KB).\n",
451 xpram_pages, (unsigned long) xpram_pages*4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 rc = xpram_setup_sizes(xpram_pages);
453 if (rc)
454 return rc;
Michael Holzheu14532092009-06-16 10:30:27 +0200455 rc = platform_driver_register(&xpram_pdrv);
456 if (rc)
457 return rc;
458 xpram_pdev = platform_device_register_simple(XPRAM_NAME, -1, NULL, 0);
459 if (IS_ERR(xpram_pdev)) {
460 rc = PTR_ERR(xpram_pdev);
461 goto fail_platform_driver_unregister;
462 }
463 rc = xpram_setup_blkdev();
464 if (rc)
465 goto fail_platform_device_unregister;
466 return 0;
467
468fail_platform_device_unregister:
469 platform_device_unregister(xpram_pdev);
470fail_platform_driver_unregister:
471 platform_driver_unregister(&xpram_pdrv);
472 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475module_init(xpram_init);
476module_exit(xpram_exit);