Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | typedef 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 | |
| 56 | static xpram_device_t xpram_devices[XPRAM_MAX_DEVS]; |
| 57 | static unsigned int xpram_sizes[XPRAM_MAX_DEVS]; |
| 58 | static struct gendisk *xpram_disks[XPRAM_MAX_DEVS]; |
| 59 | static unsigned int xpram_pages; |
| 60 | static int xpram_devs; |
| 61 | |
| 62 | /* |
| 63 | * Parameter parsing functions. |
| 64 | */ |
Heiko Carstens | 5c898ba | 2006-07-12 16:40:14 +0200 | [diff] [blame] | 65 | static int __initdata devs = XPRAM_DEVS; |
| 66 | static char __initdata *sizes[XPRAM_MAX_DEVS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | module_param(devs, int, 0); |
Heiko Carstens | 5c898ba | 2006-07-12 16:40:14 +0200 | [diff] [blame] | 69 | module_param_array(sizes, charp, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \ |
| 72 | "the default is " __MODULE_STRING(XPRAM_DEVS) "\n"); |
| 73 | MODULE_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"); |
| 78 | MODULE_LICENSE("GPL"); |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* |
| 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 | */ |
| 90 | static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index) |
| 91 | { |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 92 | int cc = 2; /* return unused cc 2 if pgin traps */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 94 | asm volatile( |
| 95 | " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */ |
| 96 | "0: ipm %0\n" |
| 97 | " srl %0,28\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | "1:\n" |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 99 | EX_TABLE(0b,1b) |
| 100 | : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (cc == 3) |
| 102 | return -ENXIO; |
| 103 | if (cc == 2) { |
| 104 | PRINT_ERR("expanded storage lost!\n"); |
| 105 | return -ENXIO; |
| 106 | } |
| 107 | if (cc == 1) { |
| 108 | PRINT_ERR("page in failed for page index %u.\n", |
| 109 | xpage_index); |
| 110 | return -EIO; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Copy a 4kB page of main memory to an expanded memory page |
| 117 | * Arguments |
| 118 | * page_addr: address of source page |
| 119 | * xpage_index: index of expandeded memory page |
| 120 | * Return value |
| 121 | * 0: if operation succeeds |
| 122 | * -EIO: if pgout failed |
| 123 | * -ENXIO: if xpram has vanished |
| 124 | */ |
| 125 | static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index) |
| 126 | { |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 127 | int cc = 2; /* return unused cc 2 if pgin traps */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 129 | asm volatile( |
| 130 | " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */ |
| 131 | "0: ipm %0\n" |
| 132 | " srl %0,28\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | "1:\n" |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 134 | EX_TABLE(0b,1b) |
| 135 | : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (cc == 3) |
| 137 | return -ENXIO; |
| 138 | if (cc == 2) { |
| 139 | PRINT_ERR("expanded storage lost!\n"); |
| 140 | return -ENXIO; |
| 141 | } |
| 142 | if (cc == 1) { |
| 143 | PRINT_ERR("page out failed for page index %u.\n", |
| 144 | xpage_index); |
| 145 | return -EIO; |
| 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Check if xpram is available. |
| 152 | */ |
| 153 | static int __init xpram_present(void) |
| 154 | { |
| 155 | unsigned long mem_page; |
| 156 | int rc; |
| 157 | |
| 158 | mem_page = (unsigned long) __get_free_page(GFP_KERNEL); |
| 159 | if (!mem_page) |
| 160 | return -ENOMEM; |
| 161 | rc = xpram_page_in(mem_page, 0); |
| 162 | free_page(mem_page); |
| 163 | return rc ? -ENXIO : 0; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Return index of the last available xpram page. |
| 168 | */ |
| 169 | static unsigned long __init xpram_highest_page_index(void) |
| 170 | { |
| 171 | unsigned int page_index, add_bit; |
| 172 | unsigned long mem_page; |
| 173 | |
| 174 | mem_page = (unsigned long) __get_free_page(GFP_KERNEL); |
| 175 | if (!mem_page) |
| 176 | return 0; |
| 177 | |
| 178 | page_index = 0; |
| 179 | add_bit = 1ULL << (sizeof(unsigned int)*8 - 1); |
| 180 | while (add_bit > 0) { |
| 181 | if (xpram_page_in(mem_page, page_index | add_bit) == 0) |
| 182 | page_index |= add_bit; |
| 183 | add_bit >>= 1; |
| 184 | } |
| 185 | |
| 186 | free_page (mem_page); |
| 187 | |
| 188 | return page_index; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Block device make request function. |
| 193 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 194 | static int xpram_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
| 196 | xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data; |
| 197 | struct bio_vec *bvec; |
| 198 | unsigned int index; |
| 199 | unsigned long page_addr; |
| 200 | unsigned long bytes; |
| 201 | int i; |
| 202 | |
| 203 | if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0) |
| 204 | /* Request is not page-aligned. */ |
| 205 | goto fail; |
| 206 | if ((bio->bi_size >> 12) > xdev->size) |
| 207 | /* Request size is no page-aligned. */ |
| 208 | goto fail; |
| 209 | if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset) |
| 210 | goto fail; |
| 211 | index = (bio->bi_sector >> 3) + xdev->offset; |
| 212 | bio_for_each_segment(bvec, bio, i) { |
| 213 | page_addr = (unsigned long) |
| 214 | kmap(bvec->bv_page) + bvec->bv_offset; |
| 215 | bytes = bvec->bv_len; |
| 216 | if ((page_addr & 4095) != 0 || (bytes & 4095) != 0) |
| 217 | /* More paranoia. */ |
| 218 | goto fail; |
| 219 | while (bytes > 0) { |
| 220 | if (bio_data_dir(bio) == READ) { |
| 221 | if (xpram_page_in(page_addr, index) != 0) |
| 222 | goto fail; |
| 223 | } else { |
| 224 | if (xpram_page_out(page_addr, index) != 0) |
| 225 | goto fail; |
| 226 | } |
| 227 | page_addr += 4096; |
| 228 | bytes -= 4096; |
| 229 | index++; |
| 230 | } |
| 231 | } |
| 232 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Martin Schwidefsky | 1462222 | 2007-10-12 16:11:53 +0200 | [diff] [blame] | 233 | bio_endio(bio, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | fail: |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 236 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 240 | static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | unsigned long size; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | /* |
| 245 | * get geometry: we have to fake one... trim the size to a |
| 246 | * multiple of 64 (32k): tell we have 16 sectors, 4 heads, |
| 247 | * whatever cylinders. Tell also that data starts at sector. 4. |
| 248 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | size = (xpram_pages * 8) & ~0x3f; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 250 | geo->cylinders = size >> 6; |
| 251 | geo->heads = 4; |
| 252 | geo->sectors = 16; |
| 253 | geo->start = 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static struct block_device_operations xpram_devops = |
| 258 | { |
| 259 | .owner = THIS_MODULE, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 260 | .getgeo = xpram_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | /* |
| 264 | * Setup xpram_sizes array. |
| 265 | */ |
| 266 | static int __init xpram_setup_sizes(unsigned long pages) |
| 267 | { |
| 268 | unsigned long mem_needed; |
| 269 | unsigned long mem_auto; |
Heiko Carstens | f257b06 | 2006-07-17 16:09:23 +0200 | [diff] [blame] | 270 | unsigned long long size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | int mem_auto_no; |
| 272 | int i; |
| 273 | |
| 274 | /* Check number of devices. */ |
| 275 | if (devs <= 0 || devs > XPRAM_MAX_DEVS) { |
| 276 | PRINT_ERR("invalid number %d of devices\n",devs); |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | xpram_devs = devs; |
| 280 | |
| 281 | /* |
| 282 | * Copy sizes array to xpram_sizes and align partition |
| 283 | * sizes to page boundary. |
| 284 | */ |
| 285 | mem_needed = 0; |
| 286 | mem_auto_no = 0; |
| 287 | for (i = 0; i < xpram_devs; i++) { |
Heiko Carstens | f257b06 | 2006-07-17 16:09:23 +0200 | [diff] [blame] | 288 | if (sizes[i]) { |
| 289 | size = simple_strtoull(sizes[i], &sizes[i], 0); |
| 290 | switch (sizes[i][0]) { |
| 291 | case 'g': |
| 292 | case 'G': |
| 293 | size <<= 20; |
| 294 | break; |
| 295 | case 'm': |
| 296 | case 'M': |
| 297 | size <<= 10; |
| 298 | } |
| 299 | xpram_sizes[i] = (size + 3) & -4UL; |
| 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | if (xpram_sizes[i]) |
| 302 | mem_needed += xpram_sizes[i]; |
| 303 | else |
| 304 | mem_auto_no++; |
| 305 | } |
| 306 | |
| 307 | PRINT_INFO(" number of devices (partitions): %d \n", xpram_devs); |
| 308 | for (i = 0; i < xpram_devs; i++) { |
| 309 | if (xpram_sizes[i]) |
| 310 | PRINT_INFO(" size of partition %d: %u kB\n", |
| 311 | i, xpram_sizes[i]); |
| 312 | else |
| 313 | PRINT_INFO(" size of partition %d to be set " |
| 314 | "automatically\n",i); |
| 315 | } |
| 316 | PRINT_DEBUG(" memory needed (for sized partitions): %lu kB\n", |
| 317 | mem_needed); |
| 318 | PRINT_DEBUG(" partitions to be sized automatically: %d\n", |
| 319 | mem_auto_no); |
| 320 | |
| 321 | if (mem_needed > pages * 4) { |
| 322 | PRINT_ERR("Not enough expanded memory available\n"); |
| 323 | return -EINVAL; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * partitioning: |
| 328 | * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB |
| 329 | * else: ; all partitions with zero xpram_sizes[i] |
| 330 | * partition equally the remaining space |
| 331 | */ |
| 332 | if (mem_auto_no) { |
| 333 | mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4; |
| 334 | PRINT_INFO(" automatically determined " |
| 335 | "partition size: %lu kB\n", mem_auto); |
| 336 | for (i = 0; i < xpram_devs; i++) |
| 337 | if (xpram_sizes[i] == 0) |
| 338 | xpram_sizes[i] = mem_auto; |
| 339 | } |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static struct request_queue *xpram_queue; |
| 344 | |
| 345 | static int __init xpram_setup_blkdev(void) |
| 346 | { |
| 347 | unsigned long offset; |
| 348 | int i, rc = -ENOMEM; |
| 349 | |
| 350 | for (i = 0; i < xpram_devs; i++) { |
| 351 | struct gendisk *disk = alloc_disk(1); |
| 352 | if (!disk) |
| 353 | goto out; |
| 354 | xpram_disks[i] = disk; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Register xpram major. |
| 359 | */ |
| 360 | rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME); |
| 361 | if (rc < 0) |
| 362 | goto out; |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /* |
| 365 | * Assign the other needed values: make request function, sizes and |
| 366 | * hardsect size. All the minor devices feature the same value. |
| 367 | */ |
| 368 | xpram_queue = blk_alloc_queue(GFP_KERNEL); |
| 369 | if (!xpram_queue) { |
| 370 | rc = -ENOMEM; |
| 371 | goto out_unreg; |
| 372 | } |
| 373 | blk_queue_make_request(xpram_queue, xpram_make_request); |
| 374 | blk_queue_hardsect_size(xpram_queue, 4096); |
| 375 | |
| 376 | /* |
| 377 | * Setup device structures. |
| 378 | */ |
| 379 | offset = 0; |
| 380 | for (i = 0; i < xpram_devs; i++) { |
| 381 | struct gendisk *disk = xpram_disks[i]; |
| 382 | |
| 383 | xpram_devices[i].size = xpram_sizes[i] / 4; |
| 384 | xpram_devices[i].offset = offset; |
| 385 | offset += xpram_devices[i].size; |
| 386 | disk->major = XPRAM_MAJOR; |
| 387 | disk->first_minor = i; |
| 388 | disk->fops = &xpram_devops; |
| 389 | disk->private_data = &xpram_devices[i]; |
| 390 | disk->queue = xpram_queue; |
| 391 | sprintf(disk->disk_name, "slram%d", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | set_capacity(disk, xpram_sizes[i] << 1); |
| 393 | add_disk(disk); |
| 394 | } |
| 395 | |
| 396 | return 0; |
| 397 | out_unreg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME); |
| 399 | out: |
| 400 | while (i--) |
| 401 | put_disk(xpram_disks[i]); |
| 402 | return rc; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Finally, the init/exit functions. |
| 407 | */ |
| 408 | static void __exit xpram_exit(void) |
| 409 | { |
| 410 | int i; |
| 411 | for (i = 0; i < xpram_devs; i++) { |
| 412 | del_gendisk(xpram_disks[i]); |
| 413 | put_disk(xpram_disks[i]); |
| 414 | } |
| 415 | unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | blk_cleanup_queue(xpram_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static int __init xpram_init(void) |
| 420 | { |
| 421 | int rc; |
| 422 | |
| 423 | /* Find out size of expanded memory. */ |
| 424 | if (xpram_present() != 0) { |
| 425 | PRINT_WARN("No expanded memory available\n"); |
| 426 | return -ENODEV; |
| 427 | } |
Christian Borntraeger | e620c49 | 2006-09-20 15:59:32 +0200 | [diff] [blame] | 428 | xpram_pages = xpram_highest_page_index() + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | PRINT_INFO(" %u pages expanded memory found (%lu KB).\n", |
| 430 | xpram_pages, (unsigned long) xpram_pages*4); |
| 431 | rc = xpram_setup_sizes(xpram_pages); |
| 432 | if (rc) |
| 433 | return rc; |
Martin Schwidefsky | 37ab46a | 2006-08-07 18:13:06 +0200 | [diff] [blame] | 434 | return xpram_setup_blkdev(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | module_init(xpram_init); |
| 438 | module_exit(xpram_exit); |