Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dcssblk.c -- the S/390 block driver for dcss memory |
| 3 | * |
| 4 | * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer |
| 5 | */ |
| 6 | |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 7 | #define KMSG_COMPONENT "dcssblk" |
| 8 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/moduleparam.h> |
| 12 | #include <linux/ctype.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/completion.h> |
| 18 | #include <linux/interrupt.h> |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
| 20 | #include <asm/extmem.h> |
| 21 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #define DCSSBLK_NAME "dcssblk" |
| 24 | #define DCSSBLK_MINORS_PER_DISK 1 |
| 25 | #define DCSSBLK_PARM_LEN 400 |
Kay Sievers | 98df67b | 2008-12-25 13:38:55 +0100 | [diff] [blame] | 26 | #define DCSS_BUS_ID_SIZE 20 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Al Viro | 46d7432 | 2008-03-02 10:37:41 -0500 | [diff] [blame] | 28 | static int dcssblk_open(struct block_device *bdev, fmode_t mode); |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 29 | static void dcssblk_release(struct gendisk *disk, fmode_t mode); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 30 | static blk_qc_t dcssblk_make_request(struct request_queue *q, |
| 31 | struct bio *bio); |
Matthew Wilcox | dd22f55 | 2015-01-07 18:05:34 +0200 | [diff] [blame] | 32 | static long dcssblk_direct_access(struct block_device *bdev, sector_t secnum, |
Dan Williams | cb389b9 | 2015-08-07 17:41:00 -0400 | [diff] [blame] | 33 | void __pmem **kaddr, unsigned long *pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0"; |
| 36 | |
| 37 | static int dcssblk_major; |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 38 | static const struct block_device_operations dcssblk_devops = { |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 39 | .owner = THIS_MODULE, |
Al Viro | 46d7432 | 2008-03-02 10:37:41 -0500 | [diff] [blame] | 40 | .open = dcssblk_open, |
| 41 | .release = dcssblk_release, |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 42 | .direct_access = dcssblk_direct_access, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | struct dcssblk_dev_info { |
| 46 | struct list_head lh; |
| 47 | struct device dev; |
Kay Sievers | 98df67b | 2008-12-25 13:38:55 +0100 | [diff] [blame] | 48 | char segment_name[DCSS_BUS_ID_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | atomic_t use_count; |
| 50 | struct gendisk *gd; |
| 51 | unsigned long start; |
| 52 | unsigned long end; |
| 53 | int segment_type; |
| 54 | unsigned char save_pending; |
| 55 | unsigned char is_shared; |
| 56 | struct request_queue *dcssblk_queue; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 57 | int num_of_segments; |
| 58 | struct list_head seg_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 61 | struct segment_info { |
| 62 | struct list_head lh; |
Kay Sievers | 98df67b | 2008-12-25 13:38:55 +0100 | [diff] [blame] | 63 | char segment_name[DCSS_BUS_ID_SIZE]; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 64 | unsigned long start; |
| 65 | unsigned long end; |
| 66 | int segment_type; |
| 67 | }; |
| 68 | |
| 69 | static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf, |
| 70 | size_t count); |
| 71 | static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf, |
| 72 | size_t count); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 73 | |
| 74 | static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store); |
| 75 | static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 76 | |
| 77 | static struct device *dcssblk_root_dev; |
| 78 | |
Denis Cheng | c11ca97 | 2008-01-26 14:11:13 +0100 | [diff] [blame] | 79 | static LIST_HEAD(dcssblk_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | static struct rw_semaphore dcssblk_devices_sem; |
| 81 | |
| 82 | /* |
| 83 | * release function for segment device. |
| 84 | */ |
| 85 | static void |
| 86 | dcssblk_release_segment(struct device *dev) |
| 87 | { |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 88 | struct dcssblk_dev_info *dev_info; |
| 89 | struct segment_info *entry, *temp; |
| 90 | |
| 91 | dev_info = container_of(dev, struct dcssblk_dev_info, dev); |
| 92 | list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) { |
| 93 | list_del(&entry->lh); |
| 94 | kfree(entry); |
| 95 | } |
| 96 | kfree(dev_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | module_put(THIS_MODULE); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * get a minor number. needs to be called with |
| 102 | * down_write(&dcssblk_devices_sem) and the |
| 103 | * device needs to be enqueued before the semaphore is |
| 104 | * freed. |
| 105 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 106 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info) |
| 108 | { |
| 109 | int minor, found; |
| 110 | struct dcssblk_dev_info *entry; |
| 111 | |
| 112 | if (dev_info == NULL) |
| 113 | return -EINVAL; |
| 114 | for (minor = 0; minor < (1<<MINORBITS); minor++) { |
| 115 | found = 0; |
| 116 | // test if minor available |
| 117 | list_for_each_entry(entry, &dcssblk_devices, lh) |
Gerald Schaefer | d059148 | 2009-06-12 10:26:35 +0200 | [diff] [blame] | 118 | if (minor == entry->gd->first_minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | found++; |
| 120 | if (!found) break; // got unused minor |
| 121 | } |
| 122 | if (found) |
| 123 | return -EBUSY; |
| 124 | dev_info->gd->first_minor = minor; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * get the struct dcssblk_dev_info from dcssblk_devices |
| 130 | * for the given name. |
| 131 | * down_read(&dcssblk_devices_sem) must be held. |
| 132 | */ |
| 133 | static struct dcssblk_dev_info * |
| 134 | dcssblk_get_device_by_name(char *name) |
| 135 | { |
| 136 | struct dcssblk_dev_info *entry; |
| 137 | |
| 138 | list_for_each_entry(entry, &dcssblk_devices, lh) { |
| 139 | if (!strcmp(name, entry->segment_name)) { |
| 140 | return entry; |
| 141 | } |
| 142 | } |
| 143 | return NULL; |
| 144 | } |
| 145 | |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 146 | /* |
| 147 | * get the struct segment_info from seg_list |
| 148 | * for the given name. |
| 149 | * down_read(&dcssblk_devices_sem) must be held. |
| 150 | */ |
| 151 | static struct segment_info * |
| 152 | dcssblk_get_segment_by_name(char *name) |
| 153 | { |
| 154 | struct dcssblk_dev_info *dev_info; |
| 155 | struct segment_info *entry; |
| 156 | |
| 157 | list_for_each_entry(dev_info, &dcssblk_devices, lh) { |
| 158 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
| 159 | if (!strcmp(name, entry->segment_name)) |
| 160 | return entry; |
| 161 | } |
| 162 | } |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * get the highest address of the multi-segment block. |
| 168 | */ |
| 169 | static unsigned long |
| 170 | dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info) |
| 171 | { |
| 172 | unsigned long highest_addr; |
| 173 | struct segment_info *entry; |
| 174 | |
| 175 | highest_addr = 0; |
| 176 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
| 177 | if (highest_addr < entry->end) |
| 178 | highest_addr = entry->end; |
| 179 | } |
| 180 | return highest_addr; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * get the lowest address of the multi-segment block. |
| 185 | */ |
| 186 | static unsigned long |
| 187 | dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info) |
| 188 | { |
| 189 | int set_first; |
| 190 | unsigned long lowest_addr; |
| 191 | struct segment_info *entry; |
| 192 | |
| 193 | set_first = 0; |
| 194 | lowest_addr = 0; |
| 195 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
| 196 | if (set_first == 0) { |
| 197 | lowest_addr = entry->start; |
| 198 | set_first = 1; |
| 199 | } else { |
| 200 | if (lowest_addr > entry->start) |
| 201 | lowest_addr = entry->start; |
| 202 | } |
| 203 | } |
| 204 | return lowest_addr; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Check continuity of segments. |
| 209 | */ |
| 210 | static int |
| 211 | dcssblk_is_continuous(struct dcssblk_dev_info *dev_info) |
| 212 | { |
| 213 | int i, j, rc; |
| 214 | struct segment_info *sort_list, *entry, temp; |
| 215 | |
| 216 | if (dev_info->num_of_segments <= 1) |
| 217 | return 0; |
| 218 | |
| 219 | sort_list = kzalloc( |
| 220 | sizeof(struct segment_info) * dev_info->num_of_segments, |
| 221 | GFP_KERNEL); |
| 222 | if (sort_list == NULL) |
| 223 | return -ENOMEM; |
| 224 | i = 0; |
| 225 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
| 226 | memcpy(&sort_list[i], entry, sizeof(struct segment_info)); |
| 227 | i++; |
| 228 | } |
| 229 | |
| 230 | /* sort segments */ |
| 231 | for (i = 0; i < dev_info->num_of_segments; i++) |
| 232 | for (j = 0; j < dev_info->num_of_segments; j++) |
| 233 | if (sort_list[j].start > sort_list[i].start) { |
| 234 | memcpy(&temp, &sort_list[i], |
| 235 | sizeof(struct segment_info)); |
| 236 | memcpy(&sort_list[i], &sort_list[j], |
| 237 | sizeof(struct segment_info)); |
| 238 | memcpy(&sort_list[j], &temp, |
| 239 | sizeof(struct segment_info)); |
| 240 | } |
| 241 | |
| 242 | /* check continuity */ |
| 243 | for (i = 0; i < dev_info->num_of_segments - 1; i++) { |
| 244 | if ((sort_list[i].end + 1) != sort_list[i+1].start) { |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 245 | pr_err("Adjacent DCSSs %s and %s are not " |
| 246 | "contiguous\n", sort_list[i].segment_name, |
| 247 | sort_list[i+1].segment_name); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 248 | rc = -EINVAL; |
| 249 | goto out; |
| 250 | } |
| 251 | /* EN and EW are allowed in a block device */ |
| 252 | if (sort_list[i].segment_type != sort_list[i+1].segment_type) { |
| 253 | if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) || |
| 254 | (sort_list[i].segment_type == SEG_TYPE_ER) || |
| 255 | !(sort_list[i+1].segment_type & |
| 256 | SEGMENT_EXCLUSIVE) || |
| 257 | (sort_list[i+1].segment_type == SEG_TYPE_ER)) { |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 258 | pr_err("DCSS %s and DCSS %s have " |
| 259 | "incompatible types\n", |
| 260 | sort_list[i].segment_name, |
| 261 | sort_list[i+1].segment_name); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 262 | rc = -EINVAL; |
| 263 | goto out; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | rc = 0; |
| 268 | out: |
| 269 | kfree(sort_list); |
| 270 | return rc; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Load a segment |
| 275 | */ |
| 276 | static int |
| 277 | dcssblk_load_segment(char *name, struct segment_info **seg_info) |
| 278 | { |
| 279 | int rc; |
| 280 | |
| 281 | /* already loaded? */ |
| 282 | down_read(&dcssblk_devices_sem); |
| 283 | *seg_info = dcssblk_get_segment_by_name(name); |
| 284 | up_read(&dcssblk_devices_sem); |
| 285 | if (*seg_info != NULL) |
| 286 | return -EEXIST; |
| 287 | |
| 288 | /* get a struct segment_info */ |
| 289 | *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL); |
| 290 | if (*seg_info == NULL) |
| 291 | return -ENOMEM; |
| 292 | |
| 293 | strcpy((*seg_info)->segment_name, name); |
| 294 | |
| 295 | /* load the segment */ |
| 296 | rc = segment_load(name, SEGMENT_SHARED, |
| 297 | &(*seg_info)->start, &(*seg_info)->end); |
| 298 | if (rc < 0) { |
| 299 | segment_warning(rc, (*seg_info)->segment_name); |
| 300 | kfree(*seg_info); |
| 301 | } else { |
| 302 | INIT_LIST_HEAD(&(*seg_info)->lh); |
| 303 | (*seg_info)->segment_type = rc; |
| 304 | } |
| 305 | return rc; |
| 306 | } |
| 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | /* |
| 309 | * device attribute for switching shared/nonshared (exclusive) |
| 310 | * operation (show + store) |
| 311 | */ |
| 312 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 313 | dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
| 315 | struct dcssblk_dev_info *dev_info; |
| 316 | |
| 317 | dev_info = container_of(dev, struct dcssblk_dev_info, dev); |
| 318 | return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n"); |
| 319 | } |
| 320 | |
| 321 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 322 | dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | struct dcssblk_dev_info *dev_info; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 325 | struct segment_info *entry, *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | int rc; |
| 327 | |
Hongjie Yang | ded77fb | 2008-07-14 09:59:39 +0200 | [diff] [blame] | 328 | if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | down_write(&dcssblk_devices_sem); |
| 331 | dev_info = container_of(dev, struct dcssblk_dev_info, dev); |
| 332 | if (atomic_read(&dev_info->use_count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | rc = -EBUSY; |
| 334 | goto out; |
| 335 | } |
| 336 | if (inbuf[0] == '1') { |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 337 | /* reload segments in shared mode */ |
| 338 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
| 339 | rc = segment_modify_shared(entry->segment_name, |
| 340 | SEGMENT_SHARED); |
| 341 | if (rc < 0) { |
| 342 | BUG_ON(rc == -EINVAL); |
| 343 | if (rc != -EAGAIN) |
| 344 | goto removeseg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 347 | dev_info->is_shared = 1; |
| 348 | switch (dev_info->segment_type) { |
| 349 | case SEG_TYPE_SR: |
| 350 | case SEG_TYPE_ER: |
| 351 | case SEG_TYPE_SC: |
| 352 | set_disk_ro(dev_info->gd, 1); |
| 353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } else if (inbuf[0] == '0') { |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 355 | /* reload segments in exclusive mode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | if (dev_info->segment_type == SEG_TYPE_SC) { |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 357 | pr_err("DCSS %s is of type SC and cannot be " |
| 358 | "loaded as exclusive-writable\n", |
| 359 | dev_info->segment_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | rc = -EINVAL; |
| 361 | goto out; |
| 362 | } |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 363 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
| 364 | rc = segment_modify_shared(entry->segment_name, |
| 365 | SEGMENT_EXCLUSIVE); |
| 366 | if (rc < 0) { |
| 367 | BUG_ON(rc == -EINVAL); |
| 368 | if (rc != -EAGAIN) |
| 369 | goto removeseg; |
| 370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 372 | dev_info->is_shared = 0; |
| 373 | set_disk_ro(dev_info->gd, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | rc = -EINVAL; |
| 376 | goto out; |
| 377 | } |
| 378 | rc = count; |
| 379 | goto out; |
| 380 | |
| 381 | removeseg: |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 382 | pr_err("DCSS device %s is removed after a failed access mode " |
| 383 | "change\n", dev_info->segment_name); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 384 | temp = entry; |
| 385 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
| 386 | if (entry != temp) |
| 387 | segment_unload(entry->segment_name); |
| 388 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | list_del(&dev_info->lh); |
| 390 | |
| 391 | del_gendisk(dev_info->gd); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 392 | blk_cleanup_queue(dev_info->dcssblk_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | dev_info->gd->queue = NULL; |
| 394 | put_disk(dev_info->gd); |
Tejun Heo | 0b60f9e | 2014-02-03 14:03:04 -0500 | [diff] [blame] | 395 | up_write(&dcssblk_devices_sem); |
| 396 | |
| 397 | if (device_remove_file_self(dev, attr)) { |
| 398 | device_unregister(dev); |
| 399 | put_device(dev); |
| 400 | } |
| 401 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | out: |
| 403 | up_write(&dcssblk_devices_sem); |
| 404 | return rc; |
| 405 | } |
Sebastian Ott | 521b3d7 | 2012-10-01 09:12:01 +0200 | [diff] [blame] | 406 | static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show, |
| 407 | dcssblk_shared_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * device attribute for save operation on current copy |
| 411 | * of the segment. If the segment is busy, saving will |
| 412 | * become pending until it gets released, which can be |
| 413 | * undone by storing a non-true value to this entry. |
| 414 | * (show + store) |
| 415 | */ |
| 416 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 417 | dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
| 419 | struct dcssblk_dev_info *dev_info; |
| 420 | |
| 421 | dev_info = container_of(dev, struct dcssblk_dev_info, dev); |
| 422 | return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n"); |
| 423 | } |
| 424 | |
| 425 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 426 | dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
| 428 | struct dcssblk_dev_info *dev_info; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 429 | struct segment_info *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Hongjie Yang | ded77fb | 2008-07-14 09:59:39 +0200 | [diff] [blame] | 431 | if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | dev_info = container_of(dev, struct dcssblk_dev_info, dev); |
| 434 | |
| 435 | down_write(&dcssblk_devices_sem); |
| 436 | if (inbuf[0] == '1') { |
| 437 | if (atomic_read(&dev_info->use_count) == 0) { |
| 438 | // device is idle => we save immediately |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 439 | pr_info("All DCSSs that map to device %s are " |
| 440 | "saved\n", dev_info->segment_name); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 441 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
Gerald Schaefer | 5be6fdc | 2015-01-16 18:29:04 +0100 | [diff] [blame] | 442 | if (entry->segment_type == SEG_TYPE_EN || |
| 443 | entry->segment_type == SEG_TYPE_SN) |
| 444 | pr_warn("DCSS %s is of type SN or EN" |
| 445 | " and cannot be saved\n", |
| 446 | entry->segment_name); |
| 447 | else |
| 448 | segment_save(entry->segment_name); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } else { |
| 451 | // device is busy => we save it when it becomes |
| 452 | // idle in dcssblk_release |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 453 | pr_info("Device %s is in use, its DCSSs will be " |
| 454 | "saved when it becomes idle\n", |
| 455 | dev_info->segment_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | dev_info->save_pending = 1; |
| 457 | } |
| 458 | } else if (inbuf[0] == '0') { |
| 459 | if (dev_info->save_pending) { |
| 460 | // device is busy & the user wants to undo his save |
| 461 | // request |
| 462 | dev_info->save_pending = 0; |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 463 | pr_info("A pending save request for device %s " |
| 464 | "has been canceled\n", |
| 465 | dev_info->segment_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | } else { |
| 468 | up_write(&dcssblk_devices_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | return -EINVAL; |
| 470 | } |
| 471 | up_write(&dcssblk_devices_sem); |
| 472 | return count; |
| 473 | } |
Sebastian Ott | 521b3d7 | 2012-10-01 09:12:01 +0200 | [diff] [blame] | 474 | static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show, |
| 475 | dcssblk_save_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | /* |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 478 | * device attribute for showing all segments in a device |
| 479 | */ |
| 480 | static ssize_t |
| 481 | dcssblk_seglist_show(struct device *dev, struct device_attribute *attr, |
| 482 | char *buf) |
| 483 | { |
| 484 | int i; |
| 485 | |
| 486 | struct dcssblk_dev_info *dev_info; |
| 487 | struct segment_info *entry; |
| 488 | |
| 489 | down_read(&dcssblk_devices_sem); |
| 490 | dev_info = container_of(dev, struct dcssblk_dev_info, dev); |
| 491 | i = 0; |
| 492 | buf[0] = '\0'; |
| 493 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
| 494 | strcpy(&buf[i], entry->segment_name); |
| 495 | i += strlen(entry->segment_name); |
| 496 | buf[i] = '\n'; |
| 497 | i++; |
| 498 | } |
| 499 | up_read(&dcssblk_devices_sem); |
| 500 | return i; |
| 501 | } |
Sebastian Ott | 521b3d7 | 2012-10-01 09:12:01 +0200 | [diff] [blame] | 502 | static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL); |
| 503 | |
| 504 | static struct attribute *dcssblk_dev_attrs[] = { |
| 505 | &dev_attr_shared.attr, |
| 506 | &dev_attr_save.attr, |
| 507 | &dev_attr_seglist.attr, |
| 508 | NULL, |
| 509 | }; |
| 510 | static struct attribute_group dcssblk_dev_attr_group = { |
| 511 | .attrs = dcssblk_dev_attrs, |
| 512 | }; |
| 513 | static const struct attribute_group *dcssblk_dev_attr_groups[] = { |
| 514 | &dcssblk_dev_attr_group, |
| 515 | NULL, |
| 516 | }; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 517 | |
| 518 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | * device attribute for adding devices |
| 520 | */ |
| 521 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 522 | dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 524 | int rc, i, j, num_of_segments; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | struct dcssblk_dev_info *dev_info; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 526 | struct segment_info *seg_info, *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | char *local_buf; |
| 528 | unsigned long seg_byte_size; |
| 529 | |
| 530 | dev_info = NULL; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 531 | seg_info = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (dev != dcssblk_root_dev) { |
| 533 | rc = -EINVAL; |
| 534 | goto out_nobuf; |
| 535 | } |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 536 | if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) { |
| 537 | rc = -ENAMETOOLONG; |
| 538 | goto out_nobuf; |
| 539 | } |
| 540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | local_buf = kmalloc(count + 1, GFP_KERNEL); |
| 542 | if (local_buf == NULL) { |
| 543 | rc = -ENOMEM; |
| 544 | goto out_nobuf; |
| 545 | } |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | /* |
| 548 | * parse input |
| 549 | */ |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 550 | num_of_segments = 0; |
Ameen Ali | 3a9f918 | 2015-02-24 18:41:50 +0200 | [diff] [blame] | 551 | for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) { |
Martin Schwidefsky | 42cfc6b | 2015-08-19 10:42:23 +0200 | [diff] [blame] | 552 | for (j = i; j < count && |
| 553 | (buf[j] != ':') && |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 554 | (buf[j] != '\0') && |
Martin Schwidefsky | 42cfc6b | 2015-08-19 10:42:23 +0200 | [diff] [blame] | 555 | (buf[j] != '\n'); j++) { |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 556 | local_buf[j-i] = toupper(buf[j]); |
| 557 | } |
| 558 | local_buf[j-i] = '\0'; |
| 559 | if (((j - i) == 0) || ((j - i) > 8)) { |
| 560 | rc = -ENAMETOOLONG; |
| 561 | goto seg_list_del; |
| 562 | } |
| 563 | |
| 564 | rc = dcssblk_load_segment(local_buf, &seg_info); |
| 565 | if (rc < 0) |
| 566 | goto seg_list_del; |
| 567 | /* |
| 568 | * get a struct dcssblk_dev_info |
| 569 | */ |
| 570 | if (num_of_segments == 0) { |
| 571 | dev_info = kzalloc(sizeof(struct dcssblk_dev_info), |
| 572 | GFP_KERNEL); |
| 573 | if (dev_info == NULL) { |
| 574 | rc = -ENOMEM; |
| 575 | goto out; |
| 576 | } |
| 577 | strcpy(dev_info->segment_name, local_buf); |
| 578 | dev_info->segment_type = seg_info->segment_type; |
| 579 | INIT_LIST_HEAD(&dev_info->seg_list); |
| 580 | } |
| 581 | list_add_tail(&seg_info->lh, &dev_info->seg_list); |
| 582 | num_of_segments++; |
| 583 | i = j; |
| 584 | |
| 585 | if ((buf[j] == '\0') || (buf[j] == '\n')) |
| 586 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 589 | /* no trailing colon at the end of the input */ |
| 590 | if ((i > 0) && (buf[i-1] == ':')) { |
| 591 | rc = -ENAMETOOLONG; |
| 592 | goto seg_list_del; |
| 593 | } |
| 594 | strlcpy(local_buf, buf, i + 1); |
| 595 | dev_info->num_of_segments = num_of_segments; |
| 596 | rc = dcssblk_is_continuous(dev_info); |
| 597 | if (rc < 0) |
| 598 | goto seg_list_del; |
| 599 | |
| 600 | dev_info->start = dcssblk_find_lowest_addr(dev_info); |
| 601 | dev_info->end = dcssblk_find_highest_addr(dev_info); |
| 602 | |
Kees Cook | ef283688f | 2014-06-10 10:46:20 -0700 | [diff] [blame] | 603 | dev_set_name(&dev_info->dev, "%s", dev_info->segment_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | dev_info->dev.release = dcssblk_release_segment; |
Sebastian Ott | 521b3d7 | 2012-10-01 09:12:01 +0200 | [diff] [blame] | 605 | dev_info->dev.groups = dcssblk_dev_attr_groups; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | INIT_LIST_HEAD(&dev_info->lh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK); |
| 608 | if (dev_info->gd == NULL) { |
| 609 | rc = -ENOMEM; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 610 | goto seg_list_del; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | dev_info->gd->major = dcssblk_major; |
| 613 | dev_info->gd->fops = &dcssblk_devops; |
| 614 | dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL); |
| 615 | dev_info->gd->queue = dev_info->dcssblk_queue; |
| 616 | dev_info->gd->private_data = dev_info; |
| 617 | dev_info->gd->driverfs_dev = &dev_info->dev; |
Heiko Carstens | c5411db | 2008-02-05 16:50:50 +0100 | [diff] [blame] | 618 | blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request); |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 619 | blk_queue_logical_block_size(dev_info->dcssblk_queue, 4096); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | seg_byte_size = (dev_info->end - dev_info->start + 1); |
| 622 | set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 623 | pr_info("Loaded %s with total size %lu bytes and capacity %lu " |
| 624 | "sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | dev_info->save_pending = 0; |
| 627 | dev_info->is_shared = 1; |
| 628 | dev_info->dev.parent = dcssblk_root_dev; |
| 629 | |
| 630 | /* |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 631 | *get minor, add to list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | */ |
| 633 | down_write(&dcssblk_devices_sem); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 634 | if (dcssblk_get_segment_by_name(local_buf)) { |
Gerald Schaefer | 04f64b5 | 2008-08-21 19:46:40 +0200 | [diff] [blame] | 635 | rc = -EEXIST; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 636 | goto release_gd; |
Gerald Schaefer | 04f64b5 | 2008-08-21 19:46:40 +0200 | [diff] [blame] | 637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | rc = dcssblk_assign_free_minor(dev_info); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 639 | if (rc) |
| 640 | goto release_gd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | sprintf(dev_info->gd->disk_name, "dcssblk%d", |
Gerald Schaefer | d059148 | 2009-06-12 10:26:35 +0200 | [diff] [blame] | 642 | dev_info->gd->first_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | list_add_tail(&dev_info->lh, &dcssblk_devices); |
| 644 | |
| 645 | if (!try_module_get(THIS_MODULE)) { |
| 646 | rc = -ENODEV; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 647 | goto dev_list_del; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | /* |
| 650 | * register the device |
| 651 | */ |
| 652 | rc = device_register(&dev_info->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | if (rc) |
Sebastian Ott | 521b3d7 | 2012-10-01 09:12:01 +0200 | [diff] [blame] | 654 | goto put_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
Sebastian Ott | 521b3d7 | 2012-10-01 09:12:01 +0200 | [diff] [blame] | 656 | get_device(&dev_info->dev); |
Christian Borntraeger | 436d1bc | 2007-12-04 16:09:03 +0100 | [diff] [blame] | 657 | add_disk(dev_info->gd); |
| 658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | switch (dev_info->segment_type) { |
| 660 | case SEG_TYPE_SR: |
| 661 | case SEG_TYPE_ER: |
| 662 | case SEG_TYPE_SC: |
| 663 | set_disk_ro(dev_info->gd,1); |
| 664 | break; |
| 665 | default: |
| 666 | set_disk_ro(dev_info->gd,0); |
| 667 | break; |
| 668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | up_write(&dcssblk_devices_sem); |
| 670 | rc = count; |
| 671 | goto out; |
| 672 | |
Sebastian Ott | 521b3d7 | 2012-10-01 09:12:01 +0200 | [diff] [blame] | 673 | put_dev: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | list_del(&dev_info->lh); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 675 | blk_cleanup_queue(dev_info->dcssblk_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | dev_info->gd->queue = NULL; |
| 677 | put_disk(dev_info->gd); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 678 | list_for_each_entry(seg_info, &dev_info->seg_list, lh) { |
| 679 | segment_unload(seg_info->segment_name); |
| 680 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | put_device(&dev_info->dev); |
| 682 | up_write(&dcssblk_devices_sem); |
| 683 | goto out; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 684 | dev_list_del: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | list_del(&dev_info->lh); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 686 | release_gd: |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 687 | blk_cleanup_queue(dev_info->dcssblk_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | dev_info->gd->queue = NULL; |
| 689 | put_disk(dev_info->gd); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 690 | up_write(&dcssblk_devices_sem); |
| 691 | seg_list_del: |
| 692 | if (dev_info == NULL) |
| 693 | goto out; |
| 694 | list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) { |
| 695 | list_del(&seg_info->lh); |
| 696 | segment_unload(seg_info->segment_name); |
| 697 | kfree(seg_info); |
| 698 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | kfree(dev_info); |
| 700 | out: |
| 701 | kfree(local_buf); |
| 702 | out_nobuf: |
| 703 | return rc; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * device attribute for removing devices |
| 708 | */ |
| 709 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 710 | dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
| 712 | struct dcssblk_dev_info *dev_info; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 713 | struct segment_info *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | int rc, i; |
| 715 | char *local_buf; |
| 716 | |
| 717 | if (dev != dcssblk_root_dev) { |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | local_buf = kmalloc(count + 1, GFP_KERNEL); |
| 721 | if (local_buf == NULL) { |
| 722 | return -ENOMEM; |
| 723 | } |
| 724 | /* |
| 725 | * parse input |
| 726 | */ |
Martin Schwidefsky | 42cfc6b | 2015-08-19 10:42:23 +0200 | [diff] [blame] | 727 | for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | local_buf[i] = toupper(buf[i]); |
| 729 | } |
| 730 | local_buf[i] = '\0'; |
| 731 | if ((i == 0) || (i > 8)) { |
| 732 | rc = -ENAMETOOLONG; |
| 733 | goto out_buf; |
| 734 | } |
| 735 | |
| 736 | down_write(&dcssblk_devices_sem); |
| 737 | dev_info = dcssblk_get_device_by_name(local_buf); |
| 738 | if (dev_info == NULL) { |
| 739 | up_write(&dcssblk_devices_sem); |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 740 | pr_warning("Device %s cannot be removed because it is not a " |
| 741 | "known device\n", local_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | rc = -ENODEV; |
| 743 | goto out_buf; |
| 744 | } |
| 745 | if (atomic_read(&dev_info->use_count) != 0) { |
| 746 | up_write(&dcssblk_devices_sem); |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 747 | pr_warning("Device %s cannot be removed while it is in " |
| 748 | "use\n", local_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | rc = -EBUSY; |
| 750 | goto out_buf; |
| 751 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 753 | list_del(&dev_info->lh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | del_gendisk(dev_info->gd); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 755 | blk_cleanup_queue(dev_info->dcssblk_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | dev_info->gd->queue = NULL; |
| 757 | put_disk(dev_info->gd); |
| 758 | device_unregister(&dev_info->dev); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 759 | |
| 760 | /* unload all related segments */ |
| 761 | list_for_each_entry(entry, &dev_info->seg_list, lh) |
| 762 | segment_unload(entry->segment_name); |
| 763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | put_device(&dev_info->dev); |
| 765 | up_write(&dcssblk_devices_sem); |
| 766 | |
| 767 | rc = count; |
| 768 | out_buf: |
| 769 | kfree(local_buf); |
| 770 | return rc; |
| 771 | } |
| 772 | |
| 773 | static int |
Al Viro | 46d7432 | 2008-03-02 10:37:41 -0500 | [diff] [blame] | 774 | dcssblk_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
| 776 | struct dcssblk_dev_info *dev_info; |
| 777 | int rc; |
| 778 | |
Al Viro | 46d7432 | 2008-03-02 10:37:41 -0500 | [diff] [blame] | 779 | dev_info = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | if (NULL == dev_info) { |
| 781 | rc = -ENODEV; |
| 782 | goto out; |
| 783 | } |
| 784 | atomic_inc(&dev_info->use_count); |
Al Viro | 46d7432 | 2008-03-02 10:37:41 -0500 | [diff] [blame] | 785 | bdev->bd_block_size = 4096; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | rc = 0; |
| 787 | out: |
| 788 | return rc; |
| 789 | } |
| 790 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 791 | static void |
Al Viro | 46d7432 | 2008-03-02 10:37:41 -0500 | [diff] [blame] | 792 | dcssblk_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | { |
Al Viro | 46d7432 | 2008-03-02 10:37:41 -0500 | [diff] [blame] | 794 | struct dcssblk_dev_info *dev_info = disk->private_data; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 795 | struct segment_info *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Al Viro | 46d7432 | 2008-03-02 10:37:41 -0500 | [diff] [blame] | 797 | if (!dev_info) { |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 798 | WARN_ON(1); |
| 799 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
| 801 | down_write(&dcssblk_devices_sem); |
| 802 | if (atomic_dec_and_test(&dev_info->use_count) |
| 803 | && (dev_info->save_pending)) { |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 804 | pr_info("Device %s has become idle and is being saved " |
| 805 | "now\n", dev_info->segment_name); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 806 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
Gerald Schaefer | 5be6fdc | 2015-01-16 18:29:04 +0100 | [diff] [blame] | 807 | if (entry->segment_type == SEG_TYPE_EN || |
| 808 | entry->segment_type == SEG_TYPE_SN) |
| 809 | pr_warn("DCSS %s is of type SN or EN and cannot" |
| 810 | " be saved\n", entry->segment_name); |
| 811 | else |
| 812 | segment_save(entry->segment_name); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | dev_info->save_pending = 0; |
| 815 | } |
| 816 | up_write(&dcssblk_devices_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 819 | static blk_qc_t |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 820 | dcssblk_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
| 822 | struct dcssblk_dev_info *dev_info; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 823 | struct bio_vec bvec; |
| 824 | struct bvec_iter iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | unsigned long index; |
| 826 | unsigned long page_addr; |
| 827 | unsigned long source_addr; |
| 828 | unsigned long bytes_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 830 | blk_queue_split(q, &bio, q->bio_split); |
| 831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | bytes_done = 0; |
| 833 | dev_info = bio->bi_bdev->bd_disk->private_data; |
| 834 | if (dev_info == NULL) |
| 835 | goto fail; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 836 | if ((bio->bi_iter.bi_sector & 7) != 0 || |
| 837 | (bio->bi_iter.bi_size & 4095) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | /* Request is not page-aligned. */ |
| 839 | goto fail; |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 840 | if (bio_end_sector(bio) > get_capacity(bio->bi_bdev->bd_disk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | /* Request beyond end of DCSS segment. */ |
| 842 | goto fail; |
| 843 | } |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 844 | /* verify data transfer direction */ |
| 845 | if (dev_info->is_shared) { |
| 846 | switch (dev_info->segment_type) { |
| 847 | case SEG_TYPE_SR: |
| 848 | case SEG_TYPE_ER: |
| 849 | case SEG_TYPE_SC: |
| 850 | /* cannot write to these segments */ |
| 851 | if (bio_data_dir(bio) == WRITE) { |
Hongjie Yang | 93098bf | 2008-12-25 13:39:45 +0100 | [diff] [blame] | 852 | pr_warning("Writing to %s failed because it " |
| 853 | "is a read-only device\n", |
Kay Sievers | 2a0217d | 2008-10-10 21:33:09 +0200 | [diff] [blame] | 854 | dev_name(&dev_info->dev)); |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 855 | goto fail; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 860 | index = (bio->bi_iter.bi_sector >> 3); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 861 | bio_for_each_segment(bvec, bio, iter) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | page_addr = (unsigned long) |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 863 | page_address(bvec.bv_page) + bvec.bv_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | source_addr = dev_info->start + (index<<12) + bytes_done; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 865 | if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | // More paranoia. |
| 867 | goto fail; |
| 868 | if (bio_data_dir(bio) == READ) { |
| 869 | memcpy((void*)page_addr, (void*)source_addr, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 870 | bvec.bv_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | } else { |
| 872 | memcpy((void*)source_addr, (void*)page_addr, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 873 | bvec.bv_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 875 | bytes_done += bvec.bv_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | } |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 877 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 878 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | fail: |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 880 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 881 | return BLK_QC_T_NONE; |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Matthew Wilcox | dd22f55 | 2015-01-07 18:05:34 +0200 | [diff] [blame] | 884 | static long |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 885 | dcssblk_direct_access (struct block_device *bdev, sector_t secnum, |
Dan Williams | cb389b9 | 2015-08-07 17:41:00 -0400 | [diff] [blame] | 886 | void __pmem **kaddr, unsigned long *pfn) |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 887 | { |
| 888 | struct dcssblk_dev_info *dev_info; |
Matthew Wilcox | dd22f55 | 2015-01-07 18:05:34 +0200 | [diff] [blame] | 889 | unsigned long offset, dev_sz; |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 890 | void *addr; |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 891 | |
| 892 | dev_info = bdev->bd_disk->private_data; |
| 893 | if (!dev_info) |
| 894 | return -ENODEV; |
Matthew Wilcox | dd22f55 | 2015-01-07 18:05:34 +0200 | [diff] [blame] | 895 | dev_sz = dev_info->end - dev_info->start; |
| 896 | offset = secnum * 512; |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 897 | addr = (void *) (dev_info->start + offset); |
| 898 | *pfn = virt_to_phys(addr) >> PAGE_SHIFT; |
| 899 | *kaddr = (void __pmem *) addr; |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame] | 900 | |
Matthew Wilcox | dd22f55 | 2015-01-07 18:05:34 +0200 | [diff] [blame] | 901 | return dev_sz - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | static void |
| 905 | dcssblk_check_params(void) |
| 906 | { |
| 907 | int rc, i, j, k; |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 908 | char buf[DCSSBLK_PARM_LEN + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | struct dcssblk_dev_info *dev_info; |
| 910 | |
| 911 | for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0'); |
| 912 | i++) { |
Martin Schwidefsky | 42cfc6b | 2015-08-19 10:42:23 +0200 | [diff] [blame] | 913 | for (j = i; (j < DCSSBLK_PARM_LEN) && |
| 914 | (dcssblk_segments[j] != ',') && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | (dcssblk_segments[j] != '\0') && |
Martin Schwidefsky | 42cfc6b | 2015-08-19 10:42:23 +0200 | [diff] [blame] | 916 | (dcssblk_segments[j] != '('); j++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | { |
| 918 | buf[j-i] = dcssblk_segments[j]; |
| 919 | } |
| 920 | buf[j-i] = '\0'; |
Cornelia Huck | f901e5d1 | 2005-06-25 14:55:29 -0700 | [diff] [blame] | 921 | rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | if ((rc >= 0) && (dcssblk_segments[j] == '(')) { |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 923 | for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | buf[k] = toupper(buf[k]); |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 925 | buf[k] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | if (!strncmp(&dcssblk_segments[j], "(local)", 7)) { |
| 927 | down_read(&dcssblk_devices_sem); |
| 928 | dev_info = dcssblk_get_device_by_name(buf); |
| 929 | up_read(&dcssblk_devices_sem); |
| 930 | if (dev_info) |
| 931 | dcssblk_shared_store(&dev_info->dev, |
Cornelia Huck | f901e5d1 | 2005-06-25 14:55:29 -0700 | [diff] [blame] | 932 | NULL, "0\n", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | while ((dcssblk_segments[j] != ',') && |
| 936 | (dcssblk_segments[j] != '\0')) |
| 937 | { |
| 938 | j++; |
| 939 | } |
| 940 | if (dcssblk_segments[j] == '\0') |
| 941 | break; |
| 942 | i = j; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | /* |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 947 | * Suspend / Resume |
| 948 | */ |
| 949 | static int dcssblk_freeze(struct device *dev) |
| 950 | { |
| 951 | struct dcssblk_dev_info *dev_info; |
| 952 | int rc = 0; |
| 953 | |
| 954 | list_for_each_entry(dev_info, &dcssblk_devices, lh) { |
| 955 | switch (dev_info->segment_type) { |
| 956 | case SEG_TYPE_SR: |
| 957 | case SEG_TYPE_ER: |
| 958 | case SEG_TYPE_SC: |
| 959 | if (!dev_info->is_shared) |
| 960 | rc = -EINVAL; |
| 961 | break; |
| 962 | default: |
| 963 | rc = -EINVAL; |
| 964 | break; |
| 965 | } |
| 966 | if (rc) |
| 967 | break; |
| 968 | } |
| 969 | if (rc) |
Christian Borntraeger | 2c48c4d | 2009-07-07 16:37:11 +0200 | [diff] [blame] | 970 | pr_err("Suspending the system failed because DCSS device %s " |
| 971 | "is writable\n", |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 972 | dev_info->segment_name); |
| 973 | return rc; |
| 974 | } |
| 975 | |
| 976 | static int dcssblk_restore(struct device *dev) |
| 977 | { |
| 978 | struct dcssblk_dev_info *dev_info; |
| 979 | struct segment_info *entry; |
| 980 | unsigned long start, end; |
| 981 | int rc = 0; |
| 982 | |
| 983 | list_for_each_entry(dev_info, &dcssblk_devices, lh) { |
| 984 | list_for_each_entry(entry, &dev_info->seg_list, lh) { |
| 985 | segment_unload(entry->segment_name); |
| 986 | rc = segment_load(entry->segment_name, SEGMENT_SHARED, |
| 987 | &start, &end); |
| 988 | if (rc < 0) { |
| 989 | // TODO in_use check ? |
| 990 | segment_warning(rc, entry->segment_name); |
| 991 | goto out_panic; |
| 992 | } |
| 993 | if (start != entry->start || end != entry->end) { |
Christian Borntraeger | 2c48c4d | 2009-07-07 16:37:11 +0200 | [diff] [blame] | 994 | pr_err("The address range of DCSS %s changed " |
| 995 | "while the system was suspended\n", |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 996 | entry->segment_name); |
| 997 | goto out_panic; |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | return 0; |
| 1002 | out_panic: |
| 1003 | panic("fatal dcssblk resume error\n"); |
| 1004 | } |
| 1005 | |
| 1006 | static int dcssblk_thaw(struct device *dev) |
| 1007 | { |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1011 | static const struct dev_pm_ops dcssblk_pm_ops = { |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 1012 | .freeze = dcssblk_freeze, |
| 1013 | .thaw = dcssblk_thaw, |
| 1014 | .restore = dcssblk_restore, |
| 1015 | }; |
| 1016 | |
| 1017 | static struct platform_driver dcssblk_pdrv = { |
| 1018 | .driver = { |
| 1019 | .name = "dcssblk", |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 1020 | .pm = &dcssblk_pm_ops, |
| 1021 | }, |
| 1022 | }; |
| 1023 | |
| 1024 | static struct platform_device *dcssblk_pdev; |
| 1025 | |
| 1026 | |
| 1027 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | * The init/exit functions. |
| 1029 | */ |
| 1030 | static void __exit |
| 1031 | dcssblk_exit(void) |
| 1032 | { |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 1033 | platform_device_unregister(dcssblk_pdev); |
| 1034 | platform_driver_unregister(&dcssblk_pdrv); |
Mark McLoughlin | 035da16 | 2008-12-15 12:58:29 +0000 | [diff] [blame] | 1035 | root_device_unregister(dcssblk_root_dev); |
Akinobu Mita | 00d5940 | 2007-07-17 04:03:46 -0700 | [diff] [blame] | 1036 | unregister_blkdev(dcssblk_major, DCSSBLK_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | static int __init |
| 1040 | dcssblk_init(void) |
| 1041 | { |
| 1042 | int rc; |
| 1043 | |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 1044 | rc = platform_driver_register(&dcssblk_pdrv); |
| 1045 | if (rc) |
| 1046 | return rc; |
| 1047 | |
| 1048 | dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL, |
| 1049 | 0); |
| 1050 | if (IS_ERR(dcssblk_pdev)) { |
| 1051 | rc = PTR_ERR(dcssblk_pdev); |
| 1052 | goto out_pdrv; |
| 1053 | } |
| 1054 | |
Mark McLoughlin | 035da16 | 2008-12-15 12:58:29 +0000 | [diff] [blame] | 1055 | dcssblk_root_dev = root_device_register("dcssblk"); |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 1056 | if (IS_ERR(dcssblk_root_dev)) { |
| 1057 | rc = PTR_ERR(dcssblk_root_dev); |
| 1058 | goto out_pdev; |
| 1059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | rc = device_create_file(dcssblk_root_dev, &dev_attr_add); |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 1061 | if (rc) |
| 1062 | goto out_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | rc = device_create_file(dcssblk_root_dev, &dev_attr_remove); |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 1064 | if (rc) |
| 1065 | goto out_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | rc = register_blkdev(0, DCSSBLK_NAME); |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 1067 | if (rc < 0) |
| 1068 | goto out_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | dcssblk_major = rc; |
| 1070 | init_rwsem(&dcssblk_devices_sem); |
| 1071 | |
| 1072 | dcssblk_check_params(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | return 0; |
Gerald Schaefer | c369527 | 2009-06-16 10:30:51 +0200 | [diff] [blame] | 1074 | |
| 1075 | out_root: |
| 1076 | root_device_unregister(dcssblk_root_dev); |
| 1077 | out_pdev: |
| 1078 | platform_device_unregister(dcssblk_pdev); |
| 1079 | out_pdrv: |
| 1080 | platform_driver_unregister(&dcssblk_pdrv); |
| 1081 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | module_init(dcssblk_init); |
| 1085 | module_exit(dcssblk_exit); |
| 1086 | |
| 1087 | module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444); |
| 1088 | MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, " |
Hongjie Yang | b2300b9 | 2008-10-10 21:33:21 +0200 | [diff] [blame] | 1089 | "comma-separated list, names in each set separated " |
| 1090 | "by commas are separated by colons, each set contains " |
| 1091 | "names of contiguous segments and each name max. 8 chars.\n" |
| 1092 | "Adding \"(local)\" to the end of each set equals echoing 0 " |
| 1093 | "to /sys/devices/dcssblk/<device name>/shared after loading " |
| 1094 | "the contiguous segments - \n" |
| 1095 | "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
| 1097 | MODULE_LICENSE("GPL"); |