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> |
| 39 | #include <linux/devfs_fs_kernel.h> |
| 40 | #include <asm/uaccess.h> |
| 41 | |
| 42 | #define XPRAM_NAME "xpram" |
| 43 | #define XPRAM_DEVS 1 /* one partition */ |
| 44 | #define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */ |
| 45 | |
| 46 | #define PRINT_DEBUG(x...) printk(KERN_DEBUG XPRAM_NAME " debug:" x) |
| 47 | #define PRINT_INFO(x...) printk(KERN_INFO XPRAM_NAME " info:" x) |
| 48 | #define PRINT_WARN(x...) printk(KERN_WARNING XPRAM_NAME " warning:" x) |
| 49 | #define PRINT_ERR(x...) printk(KERN_ERR XPRAM_NAME " error:" x) |
| 50 | |
| 51 | |
| 52 | static struct sysdev_class xpram_sysclass = { |
| 53 | set_kset_name("xpram"), |
| 54 | }; |
| 55 | |
| 56 | static struct sys_device xpram_sys_device = { |
| 57 | .id = 0, |
| 58 | .cls = &xpram_sysclass, |
| 59 | }; |
| 60 | |
| 61 | typedef struct { |
| 62 | unsigned int size; /* size of xpram segment in pages */ |
| 63 | unsigned int offset; /* start page of xpram segment */ |
| 64 | } xpram_device_t; |
| 65 | |
| 66 | static xpram_device_t xpram_devices[XPRAM_MAX_DEVS]; |
| 67 | static unsigned int xpram_sizes[XPRAM_MAX_DEVS]; |
| 68 | static struct gendisk *xpram_disks[XPRAM_MAX_DEVS]; |
| 69 | static unsigned int xpram_pages; |
| 70 | static int xpram_devs; |
| 71 | |
| 72 | /* |
| 73 | * Parameter parsing functions. |
| 74 | */ |
| 75 | static int devs = XPRAM_DEVS; |
| 76 | static unsigned int sizes[XPRAM_MAX_DEVS]; |
| 77 | |
| 78 | module_param(devs, int, 0); |
| 79 | module_param_array(sizes, int, NULL, 0); |
| 80 | |
| 81 | MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \ |
| 82 | "the default is " __MODULE_STRING(XPRAM_DEVS) "\n"); |
| 83 | MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \ |
| 84 | "the defaults are 0s \n" \ |
| 85 | "All devices with size 0 equally partition the " |
| 86 | "remaining space on the expanded strorage not " |
| 87 | "claimed by explicit sizes\n"); |
| 88 | MODULE_LICENSE("GPL"); |
| 89 | |
| 90 | #ifndef MODULE |
| 91 | /* |
| 92 | * Parses the kernel parameters given in the kernel parameter line. |
| 93 | * The expected format is |
| 94 | * <number_of_partitions>[","<partition_size>]* |
| 95 | * where |
| 96 | * devices is a positive integer that initializes xpram_devs |
| 97 | * each size is a non-negative integer possibly followed by a |
| 98 | * magnitude (k,K,m,M,g,G), the list of sizes initialises |
| 99 | * xpram_sizes |
| 100 | * |
| 101 | * Arguments |
| 102 | * str: substring of kernel parameter line that contains xprams |
| 103 | * kernel parameters. |
| 104 | * |
| 105 | * Result 0 on success, -EINVAL else -- only for Version > 2.3 |
| 106 | * |
| 107 | * Side effects |
| 108 | * the global variabls devs is set to the value of |
| 109 | * <number_of_partitions> and sizes[i] is set to the i-th |
| 110 | * partition size (if provided). A parsing error of a value |
| 111 | * results in this value being set to -EINVAL. |
| 112 | */ |
| 113 | static int __init xpram_setup (char *str) |
| 114 | { |
| 115 | char *cp; |
| 116 | int i; |
| 117 | |
| 118 | devs = simple_strtoul(str, &cp, 10); |
| 119 | if (cp <= str || devs > XPRAM_MAX_DEVS) |
| 120 | return 0; |
| 121 | for (i = 0; (i < devs) && (*cp++ == ','); i++) { |
| 122 | sizes[i] = simple_strtoul(cp, &cp, 10); |
| 123 | if (*cp == 'g' || *cp == 'G') { |
| 124 | sizes[i] <<= 20; |
| 125 | cp++; |
| 126 | } else if (*cp == 'm' || *cp == 'M') { |
| 127 | sizes[i] <<= 10; |
| 128 | cp++; |
| 129 | } else if (*cp == 'k' || *cp == 'K') |
| 130 | cp++; |
| 131 | while (isspace(*cp)) cp++; |
| 132 | } |
| 133 | if (*cp == ',' && i >= devs) |
| 134 | PRINT_WARN("partition sizes list has too many entries.\n"); |
| 135 | else if (*cp != 0) |
| 136 | PRINT_WARN("ignored '%s' at end of parameter string.\n", cp); |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | __setup("xpram_parts=", xpram_setup); |
| 141 | #endif |
| 142 | |
| 143 | /* |
| 144 | * Copy expanded memory page (4kB) into main memory |
| 145 | * Arguments |
| 146 | * page_addr: address of target page |
| 147 | * xpage_index: index of expandeded memory page |
| 148 | * Return value |
| 149 | * 0: if operation succeeds |
| 150 | * -EIO: if pgin failed |
| 151 | * -ENXIO: if xpram has vanished |
| 152 | */ |
| 153 | static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index) |
| 154 | { |
| 155 | int cc; |
| 156 | |
| 157 | __asm__ __volatile__ ( |
| 158 | " lhi %0,2\n" /* return unused cc 2 if pgin traps */ |
| 159 | " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */ |
| 160 | "0: ipm %0\n" |
| 161 | " srl %0,28\n" |
| 162 | "1:\n" |
| 163 | #ifndef CONFIG_ARCH_S390X |
| 164 | ".section __ex_table,\"a\"\n" |
| 165 | " .align 4\n" |
| 166 | " .long 0b,1b\n" |
| 167 | ".previous" |
| 168 | #else |
| 169 | ".section __ex_table,\"a\"\n" |
| 170 | " .align 8\n" |
| 171 | " .quad 0b,1b\n" |
| 172 | ".previous" |
| 173 | #endif |
| 174 | : "=&d" (cc) |
| 175 | : "a" (__pa(page_addr)), "a" (xpage_index) |
| 176 | : "cc" ); |
| 177 | if (cc == 3) |
| 178 | return -ENXIO; |
| 179 | if (cc == 2) { |
| 180 | PRINT_ERR("expanded storage lost!\n"); |
| 181 | return -ENXIO; |
| 182 | } |
| 183 | if (cc == 1) { |
| 184 | PRINT_ERR("page in failed for page index %u.\n", |
| 185 | xpage_index); |
| 186 | return -EIO; |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Copy a 4kB page of main memory to an expanded memory page |
| 193 | * Arguments |
| 194 | * page_addr: address of source page |
| 195 | * xpage_index: index of expandeded memory page |
| 196 | * Return value |
| 197 | * 0: if operation succeeds |
| 198 | * -EIO: if pgout failed |
| 199 | * -ENXIO: if xpram has vanished |
| 200 | */ |
| 201 | static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index) |
| 202 | { |
| 203 | int cc; |
| 204 | |
| 205 | __asm__ __volatile__ ( |
| 206 | " lhi %0,2\n" /* return unused cc 2 if pgout traps */ |
| 207 | " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */ |
| 208 | "0: ipm %0\n" |
| 209 | " srl %0,28\n" |
| 210 | "1:\n" |
| 211 | #ifndef CONFIG_ARCH_S390X |
| 212 | ".section __ex_table,\"a\"\n" |
| 213 | " .align 4\n" |
| 214 | " .long 0b,1b\n" |
| 215 | ".previous" |
| 216 | #else |
| 217 | ".section __ex_table,\"a\"\n" |
| 218 | " .align 8\n" |
| 219 | " .quad 0b,1b\n" |
| 220 | ".previous" |
| 221 | #endif |
| 222 | : "=&d" (cc) |
| 223 | : "a" (__pa(page_addr)), "a" (xpage_index) |
| 224 | : "cc" ); |
| 225 | if (cc == 3) |
| 226 | return -ENXIO; |
| 227 | if (cc == 2) { |
| 228 | PRINT_ERR("expanded storage lost!\n"); |
| 229 | return -ENXIO; |
| 230 | } |
| 231 | if (cc == 1) { |
| 232 | PRINT_ERR("page out failed for page index %u.\n", |
| 233 | xpage_index); |
| 234 | return -EIO; |
| 235 | } |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Check if xpram is available. |
| 241 | */ |
| 242 | static int __init xpram_present(void) |
| 243 | { |
| 244 | unsigned long mem_page; |
| 245 | int rc; |
| 246 | |
| 247 | mem_page = (unsigned long) __get_free_page(GFP_KERNEL); |
| 248 | if (!mem_page) |
| 249 | return -ENOMEM; |
| 250 | rc = xpram_page_in(mem_page, 0); |
| 251 | free_page(mem_page); |
| 252 | return rc ? -ENXIO : 0; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Return index of the last available xpram page. |
| 257 | */ |
| 258 | static unsigned long __init xpram_highest_page_index(void) |
| 259 | { |
| 260 | unsigned int page_index, add_bit; |
| 261 | unsigned long mem_page; |
| 262 | |
| 263 | mem_page = (unsigned long) __get_free_page(GFP_KERNEL); |
| 264 | if (!mem_page) |
| 265 | return 0; |
| 266 | |
| 267 | page_index = 0; |
| 268 | add_bit = 1ULL << (sizeof(unsigned int)*8 - 1); |
| 269 | while (add_bit > 0) { |
| 270 | if (xpram_page_in(mem_page, page_index | add_bit) == 0) |
| 271 | page_index |= add_bit; |
| 272 | add_bit >>= 1; |
| 273 | } |
| 274 | |
| 275 | free_page (mem_page); |
| 276 | |
| 277 | return page_index; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Block device make request function. |
| 282 | */ |
| 283 | static int xpram_make_request(request_queue_t *q, struct bio *bio) |
| 284 | { |
| 285 | xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data; |
| 286 | struct bio_vec *bvec; |
| 287 | unsigned int index; |
| 288 | unsigned long page_addr; |
| 289 | unsigned long bytes; |
| 290 | int i; |
| 291 | |
| 292 | if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0) |
| 293 | /* Request is not page-aligned. */ |
| 294 | goto fail; |
| 295 | if ((bio->bi_size >> 12) > xdev->size) |
| 296 | /* Request size is no page-aligned. */ |
| 297 | goto fail; |
| 298 | if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset) |
| 299 | goto fail; |
| 300 | index = (bio->bi_sector >> 3) + xdev->offset; |
| 301 | bio_for_each_segment(bvec, bio, i) { |
| 302 | page_addr = (unsigned long) |
| 303 | kmap(bvec->bv_page) + bvec->bv_offset; |
| 304 | bytes = bvec->bv_len; |
| 305 | if ((page_addr & 4095) != 0 || (bytes & 4095) != 0) |
| 306 | /* More paranoia. */ |
| 307 | goto fail; |
| 308 | while (bytes > 0) { |
| 309 | if (bio_data_dir(bio) == READ) { |
| 310 | if (xpram_page_in(page_addr, index) != 0) |
| 311 | goto fail; |
| 312 | } else { |
| 313 | if (xpram_page_out(page_addr, index) != 0) |
| 314 | goto fail; |
| 315 | } |
| 316 | page_addr += 4096; |
| 317 | bytes -= 4096; |
| 318 | index++; |
| 319 | } |
| 320 | } |
| 321 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
| 322 | bytes = bio->bi_size; |
| 323 | bio->bi_size = 0; |
| 324 | bio->bi_end_io(bio, bytes, 0); |
| 325 | return 0; |
| 326 | fail: |
| 327 | bio_io_error(bio, bio->bi_size); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int xpram_ioctl (struct inode *inode, struct file *filp, |
| 332 | unsigned int cmd, unsigned long arg) |
| 333 | { |
| 334 | struct hd_geometry __user *geo; |
| 335 | unsigned long size; |
| 336 | if (cmd != HDIO_GETGEO) |
| 337 | return -EINVAL; |
| 338 | /* |
| 339 | * get geometry: we have to fake one... trim the size to a |
| 340 | * multiple of 64 (32k): tell we have 16 sectors, 4 heads, |
| 341 | * whatever cylinders. Tell also that data starts at sector. 4. |
| 342 | */ |
| 343 | geo = (struct hd_geometry __user *) arg; |
| 344 | size = (xpram_pages * 8) & ~0x3f; |
| 345 | put_user(size >> 6, &geo->cylinders); |
| 346 | put_user(4, &geo->heads); |
| 347 | put_user(16, &geo->sectors); |
| 348 | put_user(4, &geo->start); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static struct block_device_operations xpram_devops = |
| 353 | { |
| 354 | .owner = THIS_MODULE, |
| 355 | .ioctl = xpram_ioctl, |
| 356 | }; |
| 357 | |
| 358 | /* |
| 359 | * Setup xpram_sizes array. |
| 360 | */ |
| 361 | static int __init xpram_setup_sizes(unsigned long pages) |
| 362 | { |
| 363 | unsigned long mem_needed; |
| 364 | unsigned long mem_auto; |
| 365 | int mem_auto_no; |
| 366 | int i; |
| 367 | |
| 368 | /* Check number of devices. */ |
| 369 | if (devs <= 0 || devs > XPRAM_MAX_DEVS) { |
| 370 | PRINT_ERR("invalid number %d of devices\n",devs); |
| 371 | return -EINVAL; |
| 372 | } |
| 373 | xpram_devs = devs; |
| 374 | |
| 375 | /* |
| 376 | * Copy sizes array to xpram_sizes and align partition |
| 377 | * sizes to page boundary. |
| 378 | */ |
| 379 | mem_needed = 0; |
| 380 | mem_auto_no = 0; |
| 381 | for (i = 0; i < xpram_devs; i++) { |
| 382 | xpram_sizes[i] = (sizes[i] + 3) & -4UL; |
| 383 | if (xpram_sizes[i]) |
| 384 | mem_needed += xpram_sizes[i]; |
| 385 | else |
| 386 | mem_auto_no++; |
| 387 | } |
| 388 | |
| 389 | PRINT_INFO(" number of devices (partitions): %d \n", xpram_devs); |
| 390 | for (i = 0; i < xpram_devs; i++) { |
| 391 | if (xpram_sizes[i]) |
| 392 | PRINT_INFO(" size of partition %d: %u kB\n", |
| 393 | i, xpram_sizes[i]); |
| 394 | else |
| 395 | PRINT_INFO(" size of partition %d to be set " |
| 396 | "automatically\n",i); |
| 397 | } |
| 398 | PRINT_DEBUG(" memory needed (for sized partitions): %lu kB\n", |
| 399 | mem_needed); |
| 400 | PRINT_DEBUG(" partitions to be sized automatically: %d\n", |
| 401 | mem_auto_no); |
| 402 | |
| 403 | if (mem_needed > pages * 4) { |
| 404 | PRINT_ERR("Not enough expanded memory available\n"); |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * partitioning: |
| 410 | * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB |
| 411 | * else: ; all partitions with zero xpram_sizes[i] |
| 412 | * partition equally the remaining space |
| 413 | */ |
| 414 | if (mem_auto_no) { |
| 415 | mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4; |
| 416 | PRINT_INFO(" automatically determined " |
| 417 | "partition size: %lu kB\n", mem_auto); |
| 418 | for (i = 0; i < xpram_devs; i++) |
| 419 | if (xpram_sizes[i] == 0) |
| 420 | xpram_sizes[i] = mem_auto; |
| 421 | } |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static struct request_queue *xpram_queue; |
| 426 | |
| 427 | static int __init xpram_setup_blkdev(void) |
| 428 | { |
| 429 | unsigned long offset; |
| 430 | int i, rc = -ENOMEM; |
| 431 | |
| 432 | for (i = 0; i < xpram_devs; i++) { |
| 433 | struct gendisk *disk = alloc_disk(1); |
| 434 | if (!disk) |
| 435 | goto out; |
| 436 | xpram_disks[i] = disk; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Register xpram major. |
| 441 | */ |
| 442 | rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME); |
| 443 | if (rc < 0) |
| 444 | goto out; |
| 445 | |
| 446 | devfs_mk_dir("slram"); |
| 447 | |
| 448 | /* |
| 449 | * Assign the other needed values: make request function, sizes and |
| 450 | * hardsect size. All the minor devices feature the same value. |
| 451 | */ |
| 452 | xpram_queue = blk_alloc_queue(GFP_KERNEL); |
| 453 | if (!xpram_queue) { |
| 454 | rc = -ENOMEM; |
| 455 | goto out_unreg; |
| 456 | } |
| 457 | blk_queue_make_request(xpram_queue, xpram_make_request); |
| 458 | blk_queue_hardsect_size(xpram_queue, 4096); |
| 459 | |
| 460 | /* |
| 461 | * Setup device structures. |
| 462 | */ |
| 463 | offset = 0; |
| 464 | for (i = 0; i < xpram_devs; i++) { |
| 465 | struct gendisk *disk = xpram_disks[i]; |
| 466 | |
| 467 | xpram_devices[i].size = xpram_sizes[i] / 4; |
| 468 | xpram_devices[i].offset = offset; |
| 469 | offset += xpram_devices[i].size; |
| 470 | disk->major = XPRAM_MAJOR; |
| 471 | disk->first_minor = i; |
| 472 | disk->fops = &xpram_devops; |
| 473 | disk->private_data = &xpram_devices[i]; |
| 474 | disk->queue = xpram_queue; |
| 475 | sprintf(disk->disk_name, "slram%d", i); |
| 476 | sprintf(disk->devfs_name, "slram/%d", i); |
| 477 | set_capacity(disk, xpram_sizes[i] << 1); |
| 478 | add_disk(disk); |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | out_unreg: |
| 483 | devfs_remove("slram"); |
| 484 | unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME); |
| 485 | out: |
| 486 | while (i--) |
| 487 | put_disk(xpram_disks[i]); |
| 488 | return rc; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Finally, the init/exit functions. |
| 493 | */ |
| 494 | static void __exit xpram_exit(void) |
| 495 | { |
| 496 | int i; |
| 497 | for (i = 0; i < xpram_devs; i++) { |
| 498 | del_gendisk(xpram_disks[i]); |
| 499 | put_disk(xpram_disks[i]); |
| 500 | } |
| 501 | unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME); |
| 502 | devfs_remove("slram"); |
| 503 | blk_cleanup_queue(xpram_queue); |
| 504 | sysdev_unregister(&xpram_sys_device); |
| 505 | sysdev_class_unregister(&xpram_sysclass); |
| 506 | } |
| 507 | |
| 508 | static int __init xpram_init(void) |
| 509 | { |
| 510 | int rc; |
| 511 | |
| 512 | /* Find out size of expanded memory. */ |
| 513 | if (xpram_present() != 0) { |
| 514 | PRINT_WARN("No expanded memory available\n"); |
| 515 | return -ENODEV; |
| 516 | } |
| 517 | xpram_pages = xpram_highest_page_index(); |
| 518 | PRINT_INFO(" %u pages expanded memory found (%lu KB).\n", |
| 519 | xpram_pages, (unsigned long) xpram_pages*4); |
| 520 | rc = xpram_setup_sizes(xpram_pages); |
| 521 | if (rc) |
| 522 | return rc; |
| 523 | rc = sysdev_class_register(&xpram_sysclass); |
| 524 | if (rc) |
| 525 | return rc; |
| 526 | |
| 527 | rc = sysdev_register(&xpram_sys_device); |
| 528 | if (rc) { |
| 529 | sysdev_class_unregister(&xpram_sysclass); |
| 530 | return rc; |
| 531 | } |
| 532 | rc = xpram_setup_blkdev(); |
| 533 | if (rc) |
| 534 | sysdev_unregister(&xpram_sys_device); |
| 535 | return rc; |
| 536 | } |
| 537 | |
| 538 | module_init(xpram_init); |
| 539 | module_exit(xpram_exit); |