blob: 03916989ed2d63ec7a45966353c8350e260640ad [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
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40
41#define XPRAM_NAME "xpram"
42#define XPRAM_DEVS 1 /* one partition */
43#define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
44
45#define PRINT_DEBUG(x...) printk(KERN_DEBUG XPRAM_NAME " debug:" x)
46#define PRINT_INFO(x...) printk(KERN_INFO XPRAM_NAME " info:" x)
47#define PRINT_WARN(x...) printk(KERN_WARNING XPRAM_NAME " warning:" x)
48#define PRINT_ERR(x...) printk(KERN_ERR XPRAM_NAME " error:" x)
49
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051typedef struct {
52 unsigned int size; /* size of xpram segment in pages */
53 unsigned int offset; /* start page of xpram segment */
54} xpram_device_t;
55
56static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
57static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
58static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
Martin Schwidefsky3ce66092008-10-10 21:33:24 +020059static struct request_queue *xpram_queues[XPRAM_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static unsigned int xpram_pages;
61static int xpram_devs;
62
63/*
64 * Parameter parsing functions.
65 */
Heiko Carstens5c898ba2006-07-12 16:40:14 +020066static int __initdata devs = XPRAM_DEVS;
67static char __initdata *sizes[XPRAM_MAX_DEVS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69module_param(devs, int, 0);
Heiko Carstens5c898ba2006-07-12 16:40:14 +020070module_param_array(sizes, charp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
73 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
74MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \
75 "the defaults are 0s \n" \
76 "All devices with size 0 equally partition the "
77 "remaining space on the expanded strorage not "
78 "claimed by explicit sizes\n");
79MODULE_LICENSE("GPL");
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * Copy expanded memory page (4kB) into main memory
83 * Arguments
84 * page_addr: address of target page
85 * xpage_index: index of expandeded memory page
86 * Return value
87 * 0: if operation succeeds
88 * -EIO: if pgin failed
89 * -ENXIO: if xpram has vanished
90 */
91static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
92{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020093 int cc = 2; /* return unused cc 2 if pgin traps */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020095 asm volatile(
96 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
97 "0: ipm %0\n"
98 " srl %0,28\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200100 EX_TABLE(0b,1b)
101 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (cc == 3)
103 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200104 if (cc == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200106 if (cc == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109}
110
111/*
112 * Copy a 4kB page of main memory to an expanded memory page
113 * Arguments
114 * page_addr: address of source page
115 * xpage_index: index of expandeded memory page
116 * Return value
117 * 0: if operation succeeds
118 * -EIO: if pgout failed
119 * -ENXIO: if xpram has vanished
120 */
121static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
122{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200123 int cc = 2; /* return unused cc 2 if pgin traps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200125 asm volatile(
126 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
127 "0: ipm %0\n"
128 " srl %0,28\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 "1:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200130 EX_TABLE(0b,1b)
131 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (cc == 3)
133 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200134 if (cc == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -ENXIO;
Martin Schwidefsky8df22b42008-07-14 09:59:24 +0200136 if (cc == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139}
140
141/*
142 * Check if xpram is available.
143 */
144static int __init xpram_present(void)
145{
146 unsigned long mem_page;
147 int rc;
148
149 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
150 if (!mem_page)
151 return -ENOMEM;
152 rc = xpram_page_in(mem_page, 0);
153 free_page(mem_page);
154 return rc ? -ENXIO : 0;
155}
156
157/*
158 * Return index of the last available xpram page.
159 */
160static unsigned long __init xpram_highest_page_index(void)
161{
162 unsigned int page_index, add_bit;
163 unsigned long mem_page;
164
165 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
166 if (!mem_page)
167 return 0;
168
169 page_index = 0;
170 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
171 while (add_bit > 0) {
172 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
173 page_index |= add_bit;
174 add_bit >>= 1;
175 }
176
177 free_page (mem_page);
178
179 return page_index;
180}
181
182/*
183 * Block device make request function.
184 */
Jens Axboe165125e2007-07-24 09:28:11 +0200185static int xpram_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
188 struct bio_vec *bvec;
189 unsigned int index;
190 unsigned long page_addr;
191 unsigned long bytes;
192 int i;
193
194 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
195 /* Request is not page-aligned. */
196 goto fail;
197 if ((bio->bi_size >> 12) > xdev->size)
198 /* Request size is no page-aligned. */
199 goto fail;
200 if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
201 goto fail;
202 index = (bio->bi_sector >> 3) + xdev->offset;
203 bio_for_each_segment(bvec, bio, i) {
204 page_addr = (unsigned long)
205 kmap(bvec->bv_page) + bvec->bv_offset;
206 bytes = bvec->bv_len;
207 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
208 /* More paranoia. */
209 goto fail;
210 while (bytes > 0) {
211 if (bio_data_dir(bio) == READ) {
212 if (xpram_page_in(page_addr, index) != 0)
213 goto fail;
214 } else {
215 if (xpram_page_out(page_addr, index) != 0)
216 goto fail;
217 }
218 page_addr += 4096;
219 bytes -= 4096;
220 index++;
221 }
222 }
223 set_bit(BIO_UPTODATE, &bio->bi_flags);
Martin Schwidefsky14622222007-10-12 16:11:53 +0200224 bio_endio(bio, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return 0;
226fail:
NeilBrown6712ecf2007-09-27 12:47:43 +0200227 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 0;
229}
230
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800231static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 unsigned long size;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
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.
239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 size = (xpram_pages * 8) & ~0x3f;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800241 geo->cylinders = size >> 6;
242 geo->heads = 4;
243 geo->sectors = 16;
244 geo->start = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246}
247
248static struct block_device_operations xpram_devops =
249{
250 .owner = THIS_MODULE,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800251 .getgeo = xpram_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
254/*
255 * Setup xpram_sizes array.
256 */
257static int __init xpram_setup_sizes(unsigned long pages)
258{
259 unsigned long mem_needed;
260 unsigned long mem_auto;
Heiko Carstensf257b062006-07-17 16:09:23 +0200261 unsigned long long size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int mem_auto_no;
263 int i;
264
265 /* Check number of devices. */
266 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
267 PRINT_ERR("invalid number %d of devices\n",devs);
268 return -EINVAL;
269 }
270 xpram_devs = devs;
271
272 /*
273 * Copy sizes array to xpram_sizes and align partition
274 * sizes to page boundary.
275 */
276 mem_needed = 0;
277 mem_auto_no = 0;
278 for (i = 0; i < xpram_devs; i++) {
Heiko Carstensf257b062006-07-17 16:09:23 +0200279 if (sizes[i]) {
280 size = simple_strtoull(sizes[i], &sizes[i], 0);
281 switch (sizes[i][0]) {
282 case 'g':
283 case 'G':
284 size <<= 20;
285 break;
286 case 'm':
287 case 'M':
288 size <<= 10;
289 }
290 xpram_sizes[i] = (size + 3) & -4UL;
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (xpram_sizes[i])
293 mem_needed += xpram_sizes[i];
294 else
295 mem_auto_no++;
296 }
297
298 PRINT_INFO(" number of devices (partitions): %d \n", xpram_devs);
299 for (i = 0; i < xpram_devs; i++) {
300 if (xpram_sizes[i])
301 PRINT_INFO(" size of partition %d: %u kB\n",
302 i, xpram_sizes[i]);
303 else
304 PRINT_INFO(" size of partition %d to be set "
305 "automatically\n",i);
306 }
307 PRINT_DEBUG(" memory needed (for sized partitions): %lu kB\n",
308 mem_needed);
309 PRINT_DEBUG(" partitions to be sized automatically: %d\n",
310 mem_auto_no);
311
312 if (mem_needed > pages * 4) {
313 PRINT_ERR("Not enough expanded memory available\n");
314 return -EINVAL;
315 }
316
317 /*
318 * partitioning:
319 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
320 * else: ; all partitions with zero xpram_sizes[i]
321 * partition equally the remaining space
322 */
323 if (mem_auto_no) {
324 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
325 PRINT_INFO(" automatically determined "
326 "partition size: %lu kB\n", mem_auto);
327 for (i = 0; i < xpram_devs; i++)
328 if (xpram_sizes[i] == 0)
329 xpram_sizes[i] = mem_auto;
330 }
331 return 0;
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static int __init xpram_setup_blkdev(void)
335{
336 unsigned long offset;
337 int i, rc = -ENOMEM;
338
339 for (i = 0; i < xpram_devs; i++) {
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200340 xpram_disks[i] = alloc_disk(1);
341 if (!xpram_disks[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto out;
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200343 xpram_queues[i] = blk_alloc_queue(GFP_KERNEL);
344 if (!xpram_queues[i]) {
345 put_disk(xpram_disks[i]);
346 goto out;
347 }
348 blk_queue_make_request(xpram_queues[i], xpram_make_request);
349 blk_queue_hardsect_size(xpram_queues[i], 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 /*
353 * Register xpram major.
354 */
355 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
356 if (rc < 0)
357 goto out;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * Setup device structures.
361 */
362 offset = 0;
363 for (i = 0; i < xpram_devs; i++) {
364 struct gendisk *disk = xpram_disks[i];
365
366 xpram_devices[i].size = xpram_sizes[i] / 4;
367 xpram_devices[i].offset = offset;
368 offset += xpram_devices[i].size;
369 disk->major = XPRAM_MAJOR;
370 disk->first_minor = i;
371 disk->fops = &xpram_devops;
372 disk->private_data = &xpram_devices[i];
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200373 disk->queue = xpram_queues[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 sprintf(disk->disk_name, "slram%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 set_capacity(disk, xpram_sizes[i] << 1);
376 add_disk(disk);
377 }
378
379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380out:
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200381 while (i--) {
382 blk_cleanup_queue(xpram_queues[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 put_disk(xpram_disks[i]);
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return rc;
386}
387
388/*
389 * Finally, the init/exit functions.
390 */
391static void __exit xpram_exit(void)
392{
393 int i;
394 for (i = 0; i < xpram_devs; i++) {
395 del_gendisk(xpram_disks[i]);
Martin Schwidefsky3ce66092008-10-10 21:33:24 +0200396 blk_cleanup_queue(xpram_queues[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 put_disk(xpram_disks[i]);
398 }
399 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static int __init xpram_init(void)
403{
404 int rc;
405
406 /* Find out size of expanded memory. */
407 if (xpram_present() != 0) {
408 PRINT_WARN("No expanded memory available\n");
409 return -ENODEV;
410 }
Christian Borntraegere620c492006-09-20 15:59:32 +0200411 xpram_pages = xpram_highest_page_index() + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 PRINT_INFO(" %u pages expanded memory found (%lu KB).\n",
413 xpram_pages, (unsigned long) xpram_pages*4);
414 rc = xpram_setup_sizes(xpram_pages);
415 if (rc)
416 return rc;
Martin Schwidefsky37ab46a2006-08-07 18:13:06 +0200417 return xpram_setup_blkdev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420module_init(xpram_init);
421module_exit(xpram_exit);