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