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 | |
Martin Schwidefsky | d1c2f89 | 2008-12-25 13:39:31 +0100 | [diff] [blame] | 28 | #define KMSG_COMPONENT "xpram" |
| 29 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
| 37 | #include <linux/blkpg.h> |
| 38 | #include <linux/hdreg.h> /* HDIO_GETGEO */ |
Kay Sievers | edbaa60 | 2011-12-21 16:26:03 -0800 | [diff] [blame] | 39 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/bio.h> |
Michael Holzheu | 1453209 | 2009-06-16 10:30:27 +0200 | [diff] [blame] | 41 | #include <linux/suspend.h> |
| 42 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/uaccess.h> |
| 45 | |
| 46 | #define XPRAM_NAME "xpram" |
| 47 | #define XPRAM_DEVS 1 /* one partition */ |
| 48 | #define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */ |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | typedef struct { |
| 51 | unsigned int size; /* size of xpram segment in pages */ |
| 52 | unsigned int offset; /* start page of xpram segment */ |
| 53 | } xpram_device_t; |
| 54 | |
| 55 | static xpram_device_t xpram_devices[XPRAM_MAX_DEVS]; |
| 56 | static unsigned int xpram_sizes[XPRAM_MAX_DEVS]; |
| 57 | static struct gendisk *xpram_disks[XPRAM_MAX_DEVS]; |
Martin Schwidefsky | 3ce6609 | 2008-10-10 21:33:24 +0200 | [diff] [blame] | 58 | static struct request_queue *xpram_queues[XPRAM_MAX_DEVS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | static unsigned int xpram_pages; |
| 60 | static int xpram_devs; |
| 61 | |
| 62 | /* |
| 63 | * Parameter parsing functions. |
| 64 | */ |
Martin Schwidefsky | 0c0db03 | 2011-03-03 17:56:05 +0100 | [diff] [blame] | 65 | static int devs = XPRAM_DEVS; |
| 66 | static char *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; |
Martin Schwidefsky | 8df22b4 | 2008-07-14 09:59:24 +0200 | [diff] [blame] | 103 | if (cc == 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return -ENXIO; |
Martin Schwidefsky | 8df22b4 | 2008-07-14 09:59:24 +0200 | [diff] [blame] | 105 | if (cc == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Copy a 4kB page of main memory to an expanded memory page |
| 112 | * Arguments |
| 113 | * page_addr: address of source page |
| 114 | * xpage_index: index of expandeded memory page |
| 115 | * Return value |
| 116 | * 0: if operation succeeds |
| 117 | * -EIO: if pgout failed |
| 118 | * -ENXIO: if xpram has vanished |
| 119 | */ |
| 120 | static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index) |
| 121 | { |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 122 | int cc = 2; /* return unused cc 2 if pgin traps */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 124 | asm volatile( |
| 125 | " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */ |
| 126 | "0: ipm %0\n" |
| 127 | " srl %0,28\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | "1:\n" |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 129 | EX_TABLE(0b,1b) |
| 130 | : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (cc == 3) |
| 132 | return -ENXIO; |
Martin Schwidefsky | 8df22b4 | 2008-07-14 09:59:24 +0200 | [diff] [blame] | 133 | if (cc == 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return -ENXIO; |
Martin Schwidefsky | 8df22b4 | 2008-07-14 09:59:24 +0200 | [diff] [blame] | 135 | if (cc == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Check if xpram is available. |
| 142 | */ |
Michael Holzheu | 1453209 | 2009-06-16 10:30:27 +0200 | [diff] [blame] | 143 | static int xpram_present(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | unsigned long mem_page; |
| 146 | int rc; |
| 147 | |
| 148 | mem_page = (unsigned long) __get_free_page(GFP_KERNEL); |
| 149 | if (!mem_page) |
| 150 | return -ENOMEM; |
| 151 | rc = xpram_page_in(mem_page, 0); |
| 152 | free_page(mem_page); |
| 153 | return rc ? -ENXIO : 0; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Return index of the last available xpram page. |
| 158 | */ |
Michael Holzheu | 1453209 | 2009-06-16 10:30:27 +0200 | [diff] [blame] | 159 | static unsigned long xpram_highest_page_index(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
| 161 | unsigned int page_index, add_bit; |
| 162 | unsigned long mem_page; |
| 163 | |
| 164 | mem_page = (unsigned long) __get_free_page(GFP_KERNEL); |
| 165 | if (!mem_page) |
| 166 | return 0; |
| 167 | |
| 168 | page_index = 0; |
| 169 | add_bit = 1ULL << (sizeof(unsigned int)*8 - 1); |
| 170 | while (add_bit > 0) { |
| 171 | if (xpram_page_in(mem_page, page_index | add_bit) == 0) |
| 172 | page_index |= add_bit; |
| 173 | add_bit >>= 1; |
| 174 | } |
| 175 | |
| 176 | free_page (mem_page); |
| 177 | |
| 178 | return page_index; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Block device make request function. |
| 183 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 184 | static void xpram_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
| 186 | xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 187 | struct bio_vec bvec; |
| 188 | struct bvec_iter iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | unsigned int index; |
| 190 | unsigned long page_addr; |
| 191 | unsigned long bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 193 | if ((bio->bi_iter.bi_sector & 7) != 0 || |
| 194 | (bio->bi_iter.bi_size & 4095) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* Request is not page-aligned. */ |
| 196 | goto fail; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 197 | if ((bio->bi_iter.bi_size >> 12) > xdev->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | /* Request size is no page-aligned. */ |
| 199 | goto fail; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 200 | if ((bio->bi_iter.bi_sector >> 3) > 0xffffffffU - xdev->offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | goto fail; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 202 | index = (bio->bi_iter.bi_sector >> 3) + xdev->offset; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 203 | bio_for_each_segment(bvec, bio, iter) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | page_addr = (unsigned long) |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 205 | kmap(bvec.bv_page) + bvec.bv_offset; |
| 206 | bytes = bvec.bv_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Schwidefsky | 1462222 | 2007-10-12 16:11:53 +0200 | [diff] [blame] | 224 | bio_endio(bio, 0); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 225 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | fail: |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 227 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 230 | static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | unsigned long size; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* |
| 235 | * get geometry: we have to fake one... trim the size to a |
| 236 | * multiple of 64 (32k): tell we have 16 sectors, 4 heads, |
| 237 | * whatever cylinders. Tell also that data starts at sector. 4. |
| 238 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | size = (xpram_pages * 8) & ~0x3f; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 240 | geo->cylinders = size >> 6; |
| 241 | geo->heads = 4; |
| 242 | geo->sectors = 16; |
| 243 | geo->start = 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 247 | static const struct block_device_operations xpram_devops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
| 249 | .owner = THIS_MODULE, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 250 | .getgeo = xpram_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | /* |
| 254 | * Setup xpram_sizes array. |
| 255 | */ |
| 256 | static int __init xpram_setup_sizes(unsigned long pages) |
| 257 | { |
| 258 | unsigned long mem_needed; |
| 259 | unsigned long mem_auto; |
Heiko Carstens | f257b06 | 2006-07-17 16:09:23 +0200 | [diff] [blame] | 260 | unsigned long long size; |
Sebastian Ott | 7e2e2b9 | 2014-01-20 16:43:33 +0100 | [diff] [blame] | 261 | char *sizes_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | int mem_auto_no; |
| 263 | int i; |
| 264 | |
| 265 | /* Check number of devices. */ |
| 266 | if (devs <= 0 || devs > XPRAM_MAX_DEVS) { |
Martin Schwidefsky | d1c2f89 | 2008-12-25 13:39:31 +0100 | [diff] [blame] | 267 | pr_err("%d is not a valid number of XPRAM devices\n",devs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Carstens | f257b06 | 2006-07-17 16:09:23 +0200 | [diff] [blame] | 279 | if (sizes[i]) { |
Sebastian Ott | 7e2e2b9 | 2014-01-20 16:43:33 +0100 | [diff] [blame] | 280 | size = simple_strtoull(sizes[i], &sizes_end, 0); |
| 281 | switch (*sizes_end) { |
Heiko Carstens | f257b06 | 2006-07-17 16:09:23 +0200 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if (xpram_sizes[i]) |
| 293 | mem_needed += xpram_sizes[i]; |
| 294 | else |
| 295 | mem_auto_no++; |
| 296 | } |
| 297 | |
Martin Schwidefsky | d1c2f89 | 2008-12-25 13:39:31 +0100 | [diff] [blame] | 298 | pr_info(" number of devices (partitions): %d \n", xpram_devs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | for (i = 0; i < xpram_devs; i++) { |
| 300 | if (xpram_sizes[i]) |
Martin Schwidefsky | d1c2f89 | 2008-12-25 13:39:31 +0100 | [diff] [blame] | 301 | pr_info(" size of partition %d: %u kB\n", |
| 302 | i, xpram_sizes[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | else |
Martin Schwidefsky | d1c2f89 | 2008-12-25 13:39:31 +0100 | [diff] [blame] | 304 | pr_info(" size of partition %d to be set " |
| 305 | "automatically\n",i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
Martin Schwidefsky | d1c2f89 | 2008-12-25 13:39:31 +0100 | [diff] [blame] | 307 | pr_info(" memory needed (for sized partitions): %lu kB\n", |
| 308 | mem_needed); |
| 309 | pr_info(" partitions to be sized automatically: %d\n", |
| 310 | mem_auto_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | if (mem_needed > pages * 4) { |
Martin Schwidefsky | d1c2f89 | 2008-12-25 13:39:31 +0100 | [diff] [blame] | 313 | pr_err("Not enough expanded memory available\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Martin Schwidefsky | d1c2f89 | 2008-12-25 13:39:31 +0100 | [diff] [blame] | 325 | pr_info(" automatically determined " |
| 326 | "partition size: %lu kB\n", mem_auto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | static 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 Schwidefsky | 3ce6609 | 2008-10-10 21:33:24 +0200 | [diff] [blame] | 340 | xpram_disks[i] = alloc_disk(1); |
| 341 | if (!xpram_disks[i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | goto out; |
Martin Schwidefsky | 3ce6609 | 2008-10-10 21:33:24 +0200 | [diff] [blame] | 343 | xpram_queues[i] = blk_alloc_queue(GFP_KERNEL); |
| 344 | if (!xpram_queues[i]) { |
| 345 | put_disk(xpram_disks[i]); |
| 346 | goto out; |
| 347 | } |
Christian Borntraeger | 00e5d35 | 2013-05-16 15:27:13 +0200 | [diff] [blame] | 348 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, xpram_queues[i]); |
Martin Schwidefsky | 3ce6609 | 2008-10-10 21:33:24 +0200 | [diff] [blame] | 349 | blk_queue_make_request(xpram_queues[i], xpram_make_request); |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 350 | blk_queue_logical_block_size(xpram_queues[i], 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Register xpram major. |
| 355 | */ |
| 356 | rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME); |
| 357 | if (rc < 0) |
| 358 | goto out; |
| 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | * Setup device structures. |
| 362 | */ |
| 363 | offset = 0; |
| 364 | for (i = 0; i < xpram_devs; i++) { |
| 365 | struct gendisk *disk = xpram_disks[i]; |
| 366 | |
| 367 | xpram_devices[i].size = xpram_sizes[i] / 4; |
| 368 | xpram_devices[i].offset = offset; |
| 369 | offset += xpram_devices[i].size; |
| 370 | disk->major = XPRAM_MAJOR; |
| 371 | disk->first_minor = i; |
| 372 | disk->fops = &xpram_devops; |
| 373 | disk->private_data = &xpram_devices[i]; |
Martin Schwidefsky | 3ce6609 | 2008-10-10 21:33:24 +0200 | [diff] [blame] | 374 | disk->queue = xpram_queues[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | sprintf(disk->disk_name, "slram%d", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | set_capacity(disk, xpram_sizes[i] << 1); |
| 377 | add_disk(disk); |
| 378 | } |
| 379 | |
| 380 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | out: |
Martin Schwidefsky | 3ce6609 | 2008-10-10 21:33:24 +0200 | [diff] [blame] | 382 | while (i--) { |
| 383 | blk_cleanup_queue(xpram_queues[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | put_disk(xpram_disks[i]); |
Martin Schwidefsky | 3ce6609 | 2008-10-10 21:33:24 +0200 | [diff] [blame] | 385 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return rc; |
| 387 | } |
| 388 | |
| 389 | /* |
Michael Holzheu | 1453209 | 2009-06-16 10:30:27 +0200 | [diff] [blame] | 390 | * Resume failed: Print error message and call panic. |
| 391 | */ |
| 392 | static void xpram_resume_error(const char *message) |
| 393 | { |
Christian Borntraeger | 2c48c4d | 2009-07-07 16:37:11 +0200 | [diff] [blame] | 394 | pr_err("Resuming the system failed: %s\n", message); |
Michael Holzheu | 1453209 | 2009-06-16 10:30:27 +0200 | [diff] [blame] | 395 | panic("xpram resume error\n"); |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Check if xpram setup changed between suspend and resume. |
| 400 | */ |
| 401 | static int xpram_restore(struct device *dev) |
| 402 | { |
| 403 | if (!xpram_pages) |
| 404 | return 0; |
| 405 | if (xpram_present() != 0) |
| 406 | xpram_resume_error("xpram disappeared"); |
| 407 | if (xpram_pages != xpram_highest_page_index() + 1) |
| 408 | xpram_resume_error("Size of xpram changed"); |
Michael Holzheu | 1453209 | 2009-06-16 10:30:27 +0200 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 412 | static const struct dev_pm_ops xpram_pm_ops = { |
Michael Holzheu | 1453209 | 2009-06-16 10:30:27 +0200 | [diff] [blame] | 413 | .restore = xpram_restore, |
| 414 | }; |
| 415 | |
| 416 | static struct platform_driver xpram_pdrv = { |
| 417 | .driver = { |
| 418 | .name = XPRAM_NAME, |
| 419 | .owner = THIS_MODULE, |
| 420 | .pm = &xpram_pm_ops, |
| 421 | }, |
| 422 | }; |
| 423 | |
| 424 | static struct platform_device *xpram_pdev; |
| 425 | |
| 426 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | * Finally, the init/exit functions. |
| 428 | */ |
| 429 | static void __exit xpram_exit(void) |
| 430 | { |
| 431 | int i; |
| 432 | for (i = 0; i < xpram_devs; i++) { |
| 433 | del_gendisk(xpram_disks[i]); |
Martin Schwidefsky | 3ce6609 | 2008-10-10 21:33:24 +0200 | [diff] [blame] | 434 | blk_cleanup_queue(xpram_queues[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | put_disk(xpram_disks[i]); |
| 436 | } |
| 437 | unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME); |
Michael Holzheu | 1453209 | 2009-06-16 10:30:27 +0200 | [diff] [blame] | 438 | platform_device_unregister(xpram_pdev); |
| 439 | platform_driver_unregister(&xpram_pdrv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static int __init xpram_init(void) |
| 443 | { |
| 444 | int rc; |
| 445 | |
| 446 | /* Find out size of expanded memory. */ |
| 447 | if (xpram_present() != 0) { |
Martin Schwidefsky | d1c2f89 | 2008-12-25 13:39:31 +0100 | [diff] [blame] | 448 | pr_err("No expanded memory available\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return -ENODEV; |
| 450 | } |
Christian Borntraeger | e620c49 | 2006-09-20 15:59:32 +0200 | [diff] [blame] | 451 | xpram_pages = xpram_highest_page_index() + 1; |
Martin Schwidefsky | d1c2f89 | 2008-12-25 13:39:31 +0100 | [diff] [blame] | 452 | pr_info(" %u pages expanded memory found (%lu KB).\n", |
| 453 | xpram_pages, (unsigned long) xpram_pages*4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | rc = xpram_setup_sizes(xpram_pages); |
| 455 | if (rc) |
| 456 | return rc; |
Michael Holzheu | 1453209 | 2009-06-16 10:30:27 +0200 | [diff] [blame] | 457 | rc = platform_driver_register(&xpram_pdrv); |
| 458 | if (rc) |
| 459 | return rc; |
| 460 | xpram_pdev = platform_device_register_simple(XPRAM_NAME, -1, NULL, 0); |
| 461 | if (IS_ERR(xpram_pdev)) { |
| 462 | rc = PTR_ERR(xpram_pdev); |
| 463 | goto fail_platform_driver_unregister; |
| 464 | } |
| 465 | rc = xpram_setup_blkdev(); |
| 466 | if (rc) |
| 467 | goto fail_platform_device_unregister; |
| 468 | return 0; |
| 469 | |
| 470 | fail_platform_device_unregister: |
| 471 | platform_device_unregister(xpram_pdev); |
| 472 | fail_platform_driver_unregister: |
| 473 | platform_driver_unregister(&xpram_pdrv); |
| 474 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | module_init(xpram_init); |
| 478 | module_exit(xpram_exit); |