blob: 76814f3e898a9c444dca70b4ddfc0cd7d885847c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43
44#define XPRAM_NAME "xpram"
45#define XPRAM_DEVS 1 /* one partition */
46#define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048typedef struct {
49 unsigned int size; /* size of xpram segment in pages */
50 unsigned int offset; /* start page of xpram segment */
51} xpram_device_t;
52
53static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
54static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
55static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
Martin Schwidefsky3ce66092008-10-10 21:33:24 +020056static struct request_queue *xpram_queues[XPRAM_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static unsigned int xpram_pages;
58static int xpram_devs;
59
60/*
61 * Parameter parsing functions.
62 */
Heiko Carstens5c898ba2006-07-12 16:40:14 +020063static int __initdata devs = XPRAM_DEVS;
64static char __initdata *sizes[XPRAM_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66module_param(devs, int, 0);
Heiko Carstens5c898ba2006-07-12 16:40:14 +020067module_param_array(sizes, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
70 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
71MODULE_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");
76MODULE_LICENSE("GPL");
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
79 * Copy expanded memory page (4kB) into main memory
80 * Arguments
81 * page_addr: address of target page
82 * xpage_index: index of expandeded memory page
83 * Return value
84 * 0: if operation succeeds
85 * -EIO: if pgin failed
86 * -ENXIO: if xpram has vanished
87 */
88static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
89{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020090 int cc = 2; /* return unused cc 2 if pgin traps */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020092 asm volatile(
93 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
94 "0: ipm %0\n"
95 " srl %0,28\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020097 EX_TABLE(0b,1b)
98 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (cc == 3)
100 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200101 if (cc == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200103 if (cc == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0;
106}
107
108/*
109 * Copy a 4kB page of main memory to an expanded memory page
110 * Arguments
111 * page_addr: address of source page
112 * xpage_index: index of expandeded memory page
113 * Return value
114 * 0: if operation succeeds
115 * -EIO: if pgout failed
116 * -ENXIO: if xpram has vanished
117 */
118static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
119{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200120 int cc = 2; /* return unused cc 2 if pgin traps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200122 asm volatile(
123 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
124 "0: ipm %0\n"
125 " srl %0,28\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200127 EX_TABLE(0b,1b)
128 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (cc == 3)
130 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200131 if (cc == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200133 if (cc == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136}
137
138/*
139 * Check if xpram is available.
140 */
141static int __init xpram_present(void)
142{
143 unsigned long mem_page;
144 int rc;
145
146 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
147 if (!mem_page)
148 return -ENOMEM;
149 rc = xpram_page_in(mem_page, 0);
150 free_page(mem_page);
151 return rc ? -ENXIO : 0;
152}
153
154/*
155 * Return index of the last available xpram page.
156 */
157static unsigned long __init xpram_highest_page_index(void)
158{
159 unsigned int page_index, add_bit;
160 unsigned long mem_page;
161
162 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
163 if (!mem_page)
164 return 0;
165
166 page_index = 0;
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;
171 add_bit >>= 1;
172 }
173
174 free_page (mem_page);
175
176 return page_index;
177}
178
179/*
180 * Block device make request function.
181 */
Jens Axboe165125e2007-07-24 09:28:11 +0200182static int xpram_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
185 struct bio_vec *bvec;
186 unsigned int index;
187 unsigned long page_addr;
188 unsigned long bytes;
189 int i;
190
191 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
192 /* Request is not page-aligned. */
193 goto fail;
194 if ((bio->bi_size >> 12) > xdev->size)
195 /* Request size is no page-aligned. */
196 goto fail;
197 if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
198 goto fail;
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)
205 /* More paranoia. */
206 goto fail;
207 while (bytes > 0) {
208 if (bio_data_dir(bio) == READ) {
209 if (xpram_page_in(page_addr, index) != 0)
210 goto fail;
211 } else {
212 if (xpram_page_out(page_addr, index) != 0)
213 goto fail;
214 }
215 page_addr += 4096;
216 bytes -= 4096;
217 index++;
218 }
219 }
220 set_bit(BIO_UPTODATE, &bio->bi_flags);
Martin Schwidefsky14622222007-10-12 16:11:53 +0200221 bio_endio(bio, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
223fail:
NeilBrown6712ecf2007-09-27 12:47:43 +0200224 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return 0;
226}
227
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800228static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 unsigned long size;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /*
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.
236 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 size = (xpram_pages * 8) & ~0x3f;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800238 geo->cylinders = size >> 6;
239 geo->heads = 4;
240 geo->sectors = 16;
241 geo->start = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243}
244
245static struct block_device_operations xpram_devops =
246{
247 .owner = THIS_MODULE,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800248 .getgeo = xpram_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
251/*
252 * Setup xpram_sizes array.
253 */
254static int __init xpram_setup_sizes(unsigned long pages)
255{
256 unsigned long mem_needed;
257 unsigned long mem_auto;
Heiko Carstensf257b062006-07-17 16:09:23 +0200258 unsigned long long size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 int mem_auto_no;
260 int i;
261
262 /* Check number of devices. */
263 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100264 pr_err("%d is not a valid number of XPRAM devices\n",devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EINVAL;
266 }
267 xpram_devs = devs;
268
269 /*
270 * Copy sizes array to xpram_sizes and align partition
271 * sizes to page boundary.
272 */
273 mem_needed = 0;
274 mem_auto_no = 0;
275 for (i = 0; i < xpram_devs; i++) {
Heiko Carstensf257b062006-07-17 16:09:23 +0200276 if (sizes[i]) {
277 size = simple_strtoull(sizes[i], &sizes[i], 0);
278 switch (sizes[i][0]) {
279 case 'g':
280 case 'G':
281 size <<= 20;
282 break;
283 case 'm':
284 case 'M':
285 size <<= 10;
286 }
287 xpram_sizes[i] = (size + 3) & -4UL;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (xpram_sizes[i])
290 mem_needed += xpram_sizes[i];
291 else
292 mem_auto_no++;
293 }
294
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100295 pr_info(" number of devices (partitions): %d \n", xpram_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 for (i = 0; i < xpram_devs; i++) {
297 if (xpram_sizes[i])
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100298 pr_info(" size of partition %d: %u kB\n",
299 i, xpram_sizes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 else
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100301 pr_info(" size of partition %d to be set "
302 "automatically\n",i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100304 pr_info(" memory needed (for sized partitions): %lu kB\n",
305 mem_needed);
306 pr_info(" partitions to be sized automatically: %d\n",
307 mem_auto_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (mem_needed > pages * 4) {
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100310 pr_err("Not enough expanded memory available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return -EINVAL;
312 }
313
314 /*
315 * partitioning:
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
319 */
320 if (mem_auto_no) {
321 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100322 pr_info(" automatically determined "
323 "partition size: %lu kB\n", mem_auto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 for (i = 0; i < xpram_devs; i++)
325 if (xpram_sizes[i] == 0)
326 xpram_sizes[i] = mem_auto;
327 }
328 return 0;
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static int __init xpram_setup_blkdev(void)
332{
333 unsigned long offset;
334 int i, rc = -ENOMEM;
335
336 for (i = 0; i < xpram_devs; i++) {
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200337 xpram_disks[i] = alloc_disk(1);
338 if (!xpram_disks[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 goto out;
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200340 xpram_queues[i] = blk_alloc_queue(GFP_KERNEL);
341 if (!xpram_queues[i]) {
342 put_disk(xpram_disks[i]);
343 goto out;
344 }
345 blk_queue_make_request(xpram_queues[i], xpram_make_request);
346 blk_queue_hardsect_size(xpram_queues[i], 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 /*
350 * Register xpram major.
351 */
352 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
353 if (rc < 0)
354 goto out;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * Setup device structures.
358 */
359 offset = 0;
360 for (i = 0; i < xpram_devs; i++) {
361 struct gendisk *disk = xpram_disks[i];
362
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];
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200370 disk->queue = xpram_queues[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 sprintf(disk->disk_name, "slram%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 set_capacity(disk, xpram_sizes[i] << 1);
373 add_disk(disk);
374 }
375
376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377out:
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200378 while (i--) {
379 blk_cleanup_queue(xpram_queues[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 put_disk(xpram_disks[i]);
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return rc;
383}
384
385/*
386 * Finally, the init/exit functions.
387 */
388static void __exit xpram_exit(void)
389{
390 int i;
391 for (i = 0; i < xpram_devs; i++) {
392 del_gendisk(xpram_disks[i]);
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200393 blk_cleanup_queue(xpram_queues[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 put_disk(xpram_disks[i]);
395 }
396 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399static int __init xpram_init(void)
400{
401 int rc;
402
403 /* Find out size of expanded memory. */
404 if (xpram_present() != 0) {
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100405 pr_err("No expanded memory available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -ENODEV;
407 }
Christian Borntraegere620c492006-09-20 15:59:32 +0200408 xpram_pages = xpram_highest_page_index() + 1;
Martin Schwidefskyd1c2f892008-12-25 13:39:31 +0100409 pr_info(" %u pages expanded memory found (%lu KB).\n",
410 xpram_pages, (unsigned long) xpram_pages*4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 rc = xpram_setup_sizes(xpram_pages);
412 if (rc)
413 return rc;
Martin Schwidefsky37ab46a2006-08-07 18:13:06 +0200414 return xpram_setup_blkdev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417module_init(xpram_init);
418module_exit(xpram_exit);