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 | |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/moduleparam.h> |
| 9 | #include <linux/ctype.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/blkdev.h> |
| 14 | #include <asm/extmem.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <linux/completion.h> |
| 17 | #include <linux/interrupt.h> |
Carsten Otte | cfb1b55 | 2006-01-06 00:19:14 -0800 | [diff] [blame] | 18 | #include <asm/s390_rdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | //#define DCSSBLK_DEBUG /* Debug messages on/off */ |
| 21 | #define DCSSBLK_NAME "dcssblk" |
| 22 | #define DCSSBLK_MINORS_PER_DISK 1 |
| 23 | #define DCSSBLK_PARM_LEN 400 |
| 24 | |
| 25 | #ifdef DCSSBLK_DEBUG |
| 26 | #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSSBLK_NAME " debug: " x) |
| 27 | #else |
| 28 | #define PRINT_DEBUG(x...) do {} while (0) |
| 29 | #endif |
| 30 | #define PRINT_INFO(x...) printk(KERN_INFO DCSSBLK_NAME " info: " x) |
| 31 | #define PRINT_WARN(x...) printk(KERN_WARNING DCSSBLK_NAME " warning: " x) |
| 32 | #define PRINT_ERR(x...) printk(KERN_ERR DCSSBLK_NAME " error: " x) |
| 33 | |
| 34 | |
| 35 | static int dcssblk_open(struct inode *inode, struct file *filp); |
| 36 | static int dcssblk_release(struct inode *inode, struct file *filp); |
| 37 | static int dcssblk_make_request(struct request_queue *q, struct bio *bio); |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 38 | static int dcssblk_direct_access(struct block_device *bdev, sector_t secnum, |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 39 | void **kaddr, unsigned long *pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0"; |
| 42 | |
| 43 | static int dcssblk_major; |
| 44 | static struct block_device_operations dcssblk_devops = { |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 45 | .owner = THIS_MODULE, |
| 46 | .open = dcssblk_open, |
| 47 | .release = dcssblk_release, |
| 48 | .direct_access = dcssblk_direct_access, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 51 | static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | size_t count); |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 53 | static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | size_t count); |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 55 | static ssize_t dcssblk_save_store(struct device * dev, struct device_attribute *attr, const char * buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | size_t count); |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 57 | static ssize_t dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf); |
| 58 | static ssize_t dcssblk_shared_store(struct device * dev, struct device_attribute *attr, const char * buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | size_t count); |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 60 | static ssize_t dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store); |
| 63 | static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store); |
| 64 | static DEVICE_ATTR(save, S_IWUSR | S_IRUGO, dcssblk_save_show, |
| 65 | dcssblk_save_store); |
| 66 | static DEVICE_ATTR(shared, S_IWUSR | S_IRUGO, dcssblk_shared_show, |
| 67 | dcssblk_shared_store); |
| 68 | |
| 69 | static struct device *dcssblk_root_dev; |
| 70 | |
| 71 | struct dcssblk_dev_info { |
| 72 | struct list_head lh; |
| 73 | struct device dev; |
| 74 | char segment_name[BUS_ID_SIZE]; |
| 75 | atomic_t use_count; |
| 76 | struct gendisk *gd; |
| 77 | unsigned long start; |
| 78 | unsigned long end; |
| 79 | int segment_type; |
| 80 | unsigned char save_pending; |
| 81 | unsigned char is_shared; |
| 82 | struct request_queue *dcssblk_queue; |
| 83 | }; |
| 84 | |
Denis Cheng | c11ca97 | 2008-01-26 14:11:13 +0100 | [diff] [blame] | 85 | static LIST_HEAD(dcssblk_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | static struct rw_semaphore dcssblk_devices_sem; |
| 87 | |
| 88 | /* |
| 89 | * release function for segment device. |
| 90 | */ |
| 91 | static void |
| 92 | dcssblk_release_segment(struct device *dev) |
| 93 | { |
| 94 | PRINT_DEBUG("segment release fn called for %s\n", dev->bus_id); |
| 95 | kfree(container_of(dev, struct dcssblk_dev_info, dev)); |
| 96 | module_put(THIS_MODULE); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * get a minor number. needs to be called with |
| 101 | * down_write(&dcssblk_devices_sem) and the |
| 102 | * device needs to be enqueued before the semaphore is |
| 103 | * freed. |
| 104 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 105 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info) |
| 107 | { |
| 108 | int minor, found; |
| 109 | struct dcssblk_dev_info *entry; |
| 110 | |
| 111 | if (dev_info == NULL) |
| 112 | return -EINVAL; |
| 113 | for (minor = 0; minor < (1<<MINORBITS); minor++) { |
| 114 | found = 0; |
| 115 | // test if minor available |
| 116 | list_for_each_entry(entry, &dcssblk_devices, lh) |
| 117 | if (minor == entry->gd->first_minor) |
| 118 | found++; |
| 119 | if (!found) break; // got unused minor |
| 120 | } |
| 121 | if (found) |
| 122 | return -EBUSY; |
| 123 | dev_info->gd->first_minor = minor; |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * get the struct dcssblk_dev_info from dcssblk_devices |
| 129 | * for the given name. |
| 130 | * down_read(&dcssblk_devices_sem) must be held. |
| 131 | */ |
| 132 | static struct dcssblk_dev_info * |
| 133 | dcssblk_get_device_by_name(char *name) |
| 134 | { |
| 135 | struct dcssblk_dev_info *entry; |
| 136 | |
| 137 | list_for_each_entry(entry, &dcssblk_devices, lh) { |
| 138 | if (!strcmp(name, entry->segment_name)) { |
| 139 | return entry; |
| 140 | } |
| 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
Gerald Schaefer | 931bb68 | 2007-11-05 11:10:09 +0100 | [diff] [blame] | 145 | static void dcssblk_unregister_callback(struct device *dev) |
| 146 | { |
| 147 | device_unregister(dev); |
| 148 | put_device(dev); |
| 149 | } |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* |
| 152 | * device attribute for switching shared/nonshared (exclusive) |
| 153 | * operation (show + store) |
| 154 | */ |
| 155 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 156 | dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | struct dcssblk_dev_info *dev_info; |
| 159 | |
| 160 | dev_info = container_of(dev, struct dcssblk_dev_info, dev); |
| 161 | return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n"); |
| 162 | } |
| 163 | |
| 164 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 165 | 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] | 166 | { |
| 167 | struct dcssblk_dev_info *dev_info; |
| 168 | int rc; |
| 169 | |
| 170 | if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) { |
| 171 | PRINT_WARN("Invalid value, must be 0 or 1\n"); |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | down_write(&dcssblk_devices_sem); |
| 175 | dev_info = container_of(dev, struct dcssblk_dev_info, dev); |
| 176 | if (atomic_read(&dev_info->use_count)) { |
| 177 | PRINT_ERR("share: segment %s is busy!\n", |
| 178 | dev_info->segment_name); |
| 179 | rc = -EBUSY; |
| 180 | goto out; |
| 181 | } |
| 182 | if (inbuf[0] == '1') { |
| 183 | // reload segment in shared mode |
| 184 | rc = segment_modify_shared(dev_info->segment_name, |
| 185 | SEGMENT_SHARED); |
| 186 | if (rc < 0) { |
| 187 | BUG_ON(rc == -EINVAL); |
Gerald Schaefer | 444f0e5 | 2007-02-05 21:17:11 +0100 | [diff] [blame] | 188 | if (rc != -EAGAIN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | goto removeseg; |
| 190 | } else { |
| 191 | dev_info->is_shared = 1; |
| 192 | switch (dev_info->segment_type) { |
| 193 | case SEG_TYPE_SR: |
| 194 | case SEG_TYPE_ER: |
| 195 | case SEG_TYPE_SC: |
| 196 | set_disk_ro(dev_info->gd,1); |
| 197 | } |
| 198 | } |
| 199 | } else if (inbuf[0] == '0') { |
| 200 | // reload segment in exclusive mode |
| 201 | if (dev_info->segment_type == SEG_TYPE_SC) { |
| 202 | PRINT_ERR("Segment type SC (%s) cannot be loaded in " |
| 203 | "non-shared mode\n", dev_info->segment_name); |
| 204 | rc = -EINVAL; |
| 205 | goto out; |
| 206 | } |
| 207 | rc = segment_modify_shared(dev_info->segment_name, |
| 208 | SEGMENT_EXCLUSIVE); |
| 209 | if (rc < 0) { |
| 210 | BUG_ON(rc == -EINVAL); |
Gerald Schaefer | 444f0e5 | 2007-02-05 21:17:11 +0100 | [diff] [blame] | 211 | if (rc != -EAGAIN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | goto removeseg; |
| 213 | } else { |
| 214 | dev_info->is_shared = 0; |
| 215 | set_disk_ro(dev_info->gd, 0); |
| 216 | } |
| 217 | } else { |
| 218 | PRINT_WARN("Invalid value, must be 0 or 1\n"); |
| 219 | rc = -EINVAL; |
| 220 | goto out; |
| 221 | } |
| 222 | rc = count; |
| 223 | goto out; |
| 224 | |
| 225 | removeseg: |
| 226 | PRINT_ERR("Could not reload segment %s, removing it now!\n", |
| 227 | dev_info->segment_name); |
| 228 | list_del(&dev_info->lh); |
| 229 | |
| 230 | del_gendisk(dev_info->gd); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 231 | blk_cleanup_queue(dev_info->dcssblk_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | dev_info->gd->queue = NULL; |
| 233 | put_disk(dev_info->gd); |
Gerald Schaefer | 931bb68 | 2007-11-05 11:10:09 +0100 | [diff] [blame] | 234 | rc = device_schedule_callback(dev, dcssblk_unregister_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | out: |
| 236 | up_write(&dcssblk_devices_sem); |
| 237 | return rc; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * device attribute for save operation on current copy |
| 242 | * of the segment. If the segment is busy, saving will |
| 243 | * become pending until it gets released, which can be |
| 244 | * undone by storing a non-true value to this entry. |
| 245 | * (show + store) |
| 246 | */ |
| 247 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 248 | dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | struct dcssblk_dev_info *dev_info; |
| 251 | |
| 252 | dev_info = container_of(dev, struct dcssblk_dev_info, dev); |
| 253 | return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n"); |
| 254 | } |
| 255 | |
| 256 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 257 | 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] | 258 | { |
| 259 | struct dcssblk_dev_info *dev_info; |
| 260 | |
| 261 | if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) { |
| 262 | PRINT_WARN("Invalid value, must be 0 or 1\n"); |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | dev_info = container_of(dev, struct dcssblk_dev_info, dev); |
| 266 | |
| 267 | down_write(&dcssblk_devices_sem); |
| 268 | if (inbuf[0] == '1') { |
| 269 | if (atomic_read(&dev_info->use_count) == 0) { |
| 270 | // device is idle => we save immediately |
| 271 | PRINT_INFO("Saving segment %s\n", |
| 272 | dev_info->segment_name); |
| 273 | segment_save(dev_info->segment_name); |
| 274 | } else { |
| 275 | // device is busy => we save it when it becomes |
| 276 | // idle in dcssblk_release |
| 277 | PRINT_INFO("Segment %s is currently busy, it will " |
| 278 | "be saved when it becomes idle...\n", |
| 279 | dev_info->segment_name); |
| 280 | dev_info->save_pending = 1; |
| 281 | } |
| 282 | } else if (inbuf[0] == '0') { |
| 283 | if (dev_info->save_pending) { |
| 284 | // device is busy & the user wants to undo his save |
| 285 | // request |
| 286 | dev_info->save_pending = 0; |
| 287 | PRINT_INFO("Pending save for segment %s deactivated\n", |
| 288 | dev_info->segment_name); |
| 289 | } |
| 290 | } else { |
| 291 | up_write(&dcssblk_devices_sem); |
| 292 | PRINT_WARN("Invalid value, must be 0 or 1\n"); |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | up_write(&dcssblk_devices_sem); |
| 296 | return count; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * device attribute for adding devices |
| 301 | */ |
| 302 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 303 | 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] | 304 | { |
| 305 | int rc, i; |
| 306 | struct dcssblk_dev_info *dev_info; |
| 307 | char *local_buf; |
| 308 | unsigned long seg_byte_size; |
| 309 | |
| 310 | dev_info = NULL; |
| 311 | if (dev != dcssblk_root_dev) { |
| 312 | rc = -EINVAL; |
| 313 | goto out_nobuf; |
| 314 | } |
| 315 | local_buf = kmalloc(count + 1, GFP_KERNEL); |
| 316 | if (local_buf == NULL) { |
| 317 | rc = -ENOMEM; |
| 318 | goto out_nobuf; |
| 319 | } |
| 320 | /* |
| 321 | * parse input |
| 322 | */ |
| 323 | for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) { |
| 324 | local_buf[i] = toupper(buf[i]); |
| 325 | } |
| 326 | local_buf[i] = '\0'; |
| 327 | if ((i == 0) || (i > 8)) { |
| 328 | rc = -ENAMETOOLONG; |
| 329 | goto out; |
| 330 | } |
| 331 | /* |
| 332 | * already loaded? |
| 333 | */ |
| 334 | down_read(&dcssblk_devices_sem); |
| 335 | dev_info = dcssblk_get_device_by_name(local_buf); |
| 336 | up_read(&dcssblk_devices_sem); |
| 337 | if (dev_info != NULL) { |
| 338 | PRINT_WARN("Segment %s already loaded!\n", local_buf); |
| 339 | rc = -EEXIST; |
| 340 | goto out; |
| 341 | } |
| 342 | /* |
| 343 | * get a struct dcssblk_dev_info |
| 344 | */ |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 345 | dev_info = kzalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | if (dev_info == NULL) { |
| 347 | rc = -ENOMEM; |
| 348 | goto out; |
| 349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | strcpy(dev_info->segment_name, local_buf); |
| 352 | strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE); |
| 353 | dev_info->dev.release = dcssblk_release_segment; |
| 354 | INIT_LIST_HEAD(&dev_info->lh); |
| 355 | |
| 356 | dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK); |
| 357 | if (dev_info->gd == NULL) { |
| 358 | rc = -ENOMEM; |
| 359 | goto free_dev_info; |
| 360 | } |
| 361 | dev_info->gd->major = dcssblk_major; |
| 362 | dev_info->gd->fops = &dcssblk_devops; |
| 363 | dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL); |
| 364 | dev_info->gd->queue = dev_info->dcssblk_queue; |
| 365 | dev_info->gd->private_data = dev_info; |
| 366 | dev_info->gd->driverfs_dev = &dev_info->dev; |
Heiko Carstens | c5411db | 2008-02-05 16:50:50 +0100 | [diff] [blame] | 367 | blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request); |
| 368 | blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* |
| 370 | * load the segment |
| 371 | */ |
| 372 | rc = segment_load(local_buf, SEGMENT_SHARED, |
| 373 | &dev_info->start, &dev_info->end); |
| 374 | if (rc < 0) { |
Martin Schwidefsky | ca68305 | 2008-04-17 07:46:31 +0200 | [diff] [blame] | 375 | segment_warning(rc, dev_info->segment_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | goto dealloc_gendisk; |
| 377 | } |
| 378 | seg_byte_size = (dev_info->end - dev_info->start + 1); |
| 379 | set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors |
| 380 | PRINT_INFO("Loaded segment %s, size = %lu Byte, " |
| 381 | "capacity = %lu (512 Byte) sectors\n", local_buf, |
| 382 | seg_byte_size, seg_byte_size >> 9); |
| 383 | |
| 384 | dev_info->segment_type = rc; |
| 385 | dev_info->save_pending = 0; |
| 386 | dev_info->is_shared = 1; |
| 387 | dev_info->dev.parent = dcssblk_root_dev; |
| 388 | |
| 389 | /* |
| 390 | * get minor, add to list |
| 391 | */ |
| 392 | down_write(&dcssblk_devices_sem); |
| 393 | rc = dcssblk_assign_free_minor(dev_info); |
| 394 | if (rc) { |
| 395 | up_write(&dcssblk_devices_sem); |
| 396 | PRINT_ERR("No free minor number available! " |
| 397 | "Unloading segment...\n"); |
| 398 | goto unload_seg; |
| 399 | } |
| 400 | sprintf(dev_info->gd->disk_name, "dcssblk%d", |
| 401 | dev_info->gd->first_minor); |
| 402 | list_add_tail(&dev_info->lh, &dcssblk_devices); |
| 403 | |
| 404 | if (!try_module_get(THIS_MODULE)) { |
| 405 | rc = -ENODEV; |
| 406 | goto list_del; |
| 407 | } |
| 408 | /* |
| 409 | * register the device |
| 410 | */ |
| 411 | rc = device_register(&dev_info->dev); |
| 412 | if (rc) { |
| 413 | PRINT_ERR("Segment %s could not be registered RC=%d\n", |
| 414 | local_buf, rc); |
| 415 | module_put(THIS_MODULE); |
| 416 | goto list_del; |
| 417 | } |
| 418 | get_device(&dev_info->dev); |
| 419 | rc = device_create_file(&dev_info->dev, &dev_attr_shared); |
| 420 | if (rc) |
| 421 | goto unregister_dev; |
| 422 | rc = device_create_file(&dev_info->dev, &dev_attr_save); |
| 423 | if (rc) |
| 424 | goto unregister_dev; |
| 425 | |
Christian Borntraeger | 436d1bc | 2007-12-04 16:09:03 +0100 | [diff] [blame] | 426 | add_disk(dev_info->gd); |
| 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | switch (dev_info->segment_type) { |
| 429 | case SEG_TYPE_SR: |
| 430 | case SEG_TYPE_ER: |
| 431 | case SEG_TYPE_SC: |
| 432 | set_disk_ro(dev_info->gd,1); |
| 433 | break; |
| 434 | default: |
| 435 | set_disk_ro(dev_info->gd,0); |
| 436 | break; |
| 437 | } |
| 438 | PRINT_DEBUG("Segment %s loaded successfully\n", local_buf); |
| 439 | up_write(&dcssblk_devices_sem); |
| 440 | rc = count; |
| 441 | goto out; |
| 442 | |
| 443 | unregister_dev: |
| 444 | PRINT_ERR("device_create_file() failed!\n"); |
| 445 | list_del(&dev_info->lh); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 446 | blk_cleanup_queue(dev_info->dcssblk_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | dev_info->gd->queue = NULL; |
| 448 | put_disk(dev_info->gd); |
| 449 | device_unregister(&dev_info->dev); |
| 450 | segment_unload(dev_info->segment_name); |
| 451 | put_device(&dev_info->dev); |
| 452 | up_write(&dcssblk_devices_sem); |
| 453 | goto out; |
| 454 | list_del: |
| 455 | list_del(&dev_info->lh); |
| 456 | up_write(&dcssblk_devices_sem); |
| 457 | unload_seg: |
| 458 | segment_unload(local_buf); |
| 459 | dealloc_gendisk: |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 460 | blk_cleanup_queue(dev_info->dcssblk_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | dev_info->gd->queue = NULL; |
| 462 | put_disk(dev_info->gd); |
| 463 | free_dev_info: |
| 464 | kfree(dev_info); |
| 465 | out: |
| 466 | kfree(local_buf); |
| 467 | out_nobuf: |
| 468 | return rc; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * device attribute for removing devices |
| 473 | */ |
| 474 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 475 | 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] | 476 | { |
| 477 | struct dcssblk_dev_info *dev_info; |
| 478 | int rc, i; |
| 479 | char *local_buf; |
| 480 | |
| 481 | if (dev != dcssblk_root_dev) { |
| 482 | return -EINVAL; |
| 483 | } |
| 484 | local_buf = kmalloc(count + 1, GFP_KERNEL); |
| 485 | if (local_buf == NULL) { |
| 486 | return -ENOMEM; |
| 487 | } |
| 488 | /* |
| 489 | * parse input |
| 490 | */ |
| 491 | for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) { |
| 492 | local_buf[i] = toupper(buf[i]); |
| 493 | } |
| 494 | local_buf[i] = '\0'; |
| 495 | if ((i == 0) || (i > 8)) { |
| 496 | rc = -ENAMETOOLONG; |
| 497 | goto out_buf; |
| 498 | } |
| 499 | |
| 500 | down_write(&dcssblk_devices_sem); |
| 501 | dev_info = dcssblk_get_device_by_name(local_buf); |
| 502 | if (dev_info == NULL) { |
| 503 | up_write(&dcssblk_devices_sem); |
| 504 | PRINT_WARN("Segment %s is not loaded!\n", local_buf); |
| 505 | rc = -ENODEV; |
| 506 | goto out_buf; |
| 507 | } |
| 508 | if (atomic_read(&dev_info->use_count) != 0) { |
| 509 | up_write(&dcssblk_devices_sem); |
| 510 | PRINT_WARN("Segment %s is in use!\n", local_buf); |
| 511 | rc = -EBUSY; |
| 512 | goto out_buf; |
| 513 | } |
| 514 | list_del(&dev_info->lh); |
| 515 | |
| 516 | del_gendisk(dev_info->gd); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 517 | blk_cleanup_queue(dev_info->dcssblk_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | dev_info->gd->queue = NULL; |
| 519 | put_disk(dev_info->gd); |
| 520 | device_unregister(&dev_info->dev); |
| 521 | segment_unload(dev_info->segment_name); |
| 522 | PRINT_DEBUG("Segment %s unloaded successfully\n", |
| 523 | dev_info->segment_name); |
| 524 | put_device(&dev_info->dev); |
| 525 | up_write(&dcssblk_devices_sem); |
| 526 | |
| 527 | rc = count; |
| 528 | out_buf: |
| 529 | kfree(local_buf); |
| 530 | return rc; |
| 531 | } |
| 532 | |
| 533 | static int |
| 534 | dcssblk_open(struct inode *inode, struct file *filp) |
| 535 | { |
| 536 | struct dcssblk_dev_info *dev_info; |
| 537 | int rc; |
| 538 | |
| 539 | dev_info = inode->i_bdev->bd_disk->private_data; |
| 540 | if (NULL == dev_info) { |
| 541 | rc = -ENODEV; |
| 542 | goto out; |
| 543 | } |
| 544 | atomic_inc(&dev_info->use_count); |
| 545 | inode->i_bdev->bd_block_size = 4096; |
| 546 | rc = 0; |
| 547 | out: |
| 548 | return rc; |
| 549 | } |
| 550 | |
| 551 | static int |
| 552 | dcssblk_release(struct inode *inode, struct file *filp) |
| 553 | { |
| 554 | struct dcssblk_dev_info *dev_info; |
| 555 | int rc; |
| 556 | |
| 557 | dev_info = inode->i_bdev->bd_disk->private_data; |
| 558 | if (NULL == dev_info) { |
| 559 | rc = -ENODEV; |
| 560 | goto out; |
| 561 | } |
| 562 | down_write(&dcssblk_devices_sem); |
| 563 | if (atomic_dec_and_test(&dev_info->use_count) |
| 564 | && (dev_info->save_pending)) { |
| 565 | PRINT_INFO("Segment %s became idle and is being saved now\n", |
| 566 | dev_info->segment_name); |
| 567 | segment_save(dev_info->segment_name); |
| 568 | dev_info->save_pending = 0; |
| 569 | } |
| 570 | up_write(&dcssblk_devices_sem); |
| 571 | rc = 0; |
| 572 | out: |
| 573 | return rc; |
| 574 | } |
| 575 | |
| 576 | static int |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 577 | dcssblk_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
| 579 | struct dcssblk_dev_info *dev_info; |
| 580 | struct bio_vec *bvec; |
| 581 | unsigned long index; |
| 582 | unsigned long page_addr; |
| 583 | unsigned long source_addr; |
| 584 | unsigned long bytes_done; |
| 585 | int i; |
| 586 | |
| 587 | bytes_done = 0; |
| 588 | dev_info = bio->bi_bdev->bd_disk->private_data; |
| 589 | if (dev_info == NULL) |
| 590 | goto fail; |
| 591 | if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0) |
| 592 | /* Request is not page-aligned. */ |
| 593 | goto fail; |
| 594 | if (((bio->bi_size >> 9) + bio->bi_sector) |
| 595 | > get_capacity(bio->bi_bdev->bd_disk)) { |
| 596 | /* Request beyond end of DCSS segment. */ |
| 597 | goto fail; |
| 598 | } |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 599 | /* verify data transfer direction */ |
| 600 | if (dev_info->is_shared) { |
| 601 | switch (dev_info->segment_type) { |
| 602 | case SEG_TYPE_SR: |
| 603 | case SEG_TYPE_ER: |
| 604 | case SEG_TYPE_SC: |
| 605 | /* cannot write to these segments */ |
| 606 | if (bio_data_dir(bio) == WRITE) { |
| 607 | PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id); |
| 608 | goto fail; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | index = (bio->bi_sector >> 3); |
| 614 | bio_for_each_segment(bvec, bio, i) { |
| 615 | page_addr = (unsigned long) |
| 616 | page_address(bvec->bv_page) + bvec->bv_offset; |
| 617 | source_addr = dev_info->start + (index<<12) + bytes_done; |
Roel Kluin | 39f73b2 | 2008-02-19 15:29:33 +0100 | [diff] [blame] | 618 | if (unlikely((page_addr & 4095) != 0) || (bvec->bv_len & 4095) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | // More paranoia. |
| 620 | goto fail; |
| 621 | if (bio_data_dir(bio) == READ) { |
| 622 | memcpy((void*)page_addr, (void*)source_addr, |
| 623 | bvec->bv_len); |
| 624 | } else { |
| 625 | memcpy((void*)source_addr, (void*)page_addr, |
| 626 | bvec->bv_len); |
| 627 | } |
| 628 | bytes_done += bvec->bv_len; |
| 629 | } |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 630 | bio_endio(bio, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | return 0; |
| 632 | fail: |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 633 | bio_io_error(bio); |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static int |
| 638 | dcssblk_direct_access (struct block_device *bdev, sector_t secnum, |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 639 | void **kaddr, unsigned long *pfn) |
Carsten Otte | 420edbc | 2005-06-23 22:05:23 -0700 | [diff] [blame] | 640 | { |
| 641 | struct dcssblk_dev_info *dev_info; |
| 642 | unsigned long pgoff; |
| 643 | |
| 644 | dev_info = bdev->bd_disk->private_data; |
| 645 | if (!dev_info) |
| 646 | return -ENODEV; |
| 647 | if (secnum % (PAGE_SIZE/512)) |
| 648 | return -EINVAL; |
| 649 | pgoff = secnum / (PAGE_SIZE / 512); |
| 650 | if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start) |
| 651 | return -ERANGE; |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 652 | *kaddr = (void *) (dev_info->start+pgoff*PAGE_SIZE); |
| 653 | *pfn = virt_to_phys(*kaddr) >> PAGE_SHIFT; |
| 654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static void |
| 659 | dcssblk_check_params(void) |
| 660 | { |
| 661 | int rc, i, j, k; |
| 662 | char buf[9]; |
| 663 | struct dcssblk_dev_info *dev_info; |
| 664 | |
| 665 | for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0'); |
| 666 | i++) { |
| 667 | for (j = i; (dcssblk_segments[j] != ',') && |
| 668 | (dcssblk_segments[j] != '\0') && |
| 669 | (dcssblk_segments[j] != '(') && |
| 670 | (j - i) < 8; j++) |
| 671 | { |
| 672 | buf[j-i] = dcssblk_segments[j]; |
| 673 | } |
| 674 | buf[j-i] = '\0'; |
Cornelia Huck | f901e5d | 2005-06-25 14:55:29 -0700 | [diff] [blame] | 675 | rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if ((rc >= 0) && (dcssblk_segments[j] == '(')) { |
| 677 | for (k = 0; buf[k] != '\0'; k++) |
| 678 | buf[k] = toupper(buf[k]); |
| 679 | if (!strncmp(&dcssblk_segments[j], "(local)", 7)) { |
| 680 | down_read(&dcssblk_devices_sem); |
| 681 | dev_info = dcssblk_get_device_by_name(buf); |
| 682 | up_read(&dcssblk_devices_sem); |
| 683 | if (dev_info) |
| 684 | dcssblk_shared_store(&dev_info->dev, |
Cornelia Huck | f901e5d | 2005-06-25 14:55:29 -0700 | [diff] [blame] | 685 | NULL, "0\n", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | while ((dcssblk_segments[j] != ',') && |
| 689 | (dcssblk_segments[j] != '\0')) |
| 690 | { |
| 691 | j++; |
| 692 | } |
| 693 | if (dcssblk_segments[j] == '\0') |
| 694 | break; |
| 695 | i = j; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * The init/exit functions. |
| 701 | */ |
| 702 | static void __exit |
| 703 | dcssblk_exit(void) |
| 704 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | PRINT_DEBUG("DCSSBLOCK EXIT...\n"); |
| 706 | s390_root_dev_unregister(dcssblk_root_dev); |
Akinobu Mita | 00d5940 | 2007-07-17 04:03:46 -0700 | [diff] [blame] | 707 | unregister_blkdev(dcssblk_major, DCSSBLK_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | PRINT_DEBUG("...finished!\n"); |
| 709 | } |
| 710 | |
| 711 | static int __init |
| 712 | dcssblk_init(void) |
| 713 | { |
| 714 | int rc; |
| 715 | |
| 716 | PRINT_DEBUG("DCSSBLOCK INIT...\n"); |
| 717 | dcssblk_root_dev = s390_root_dev_register("dcssblk"); |
| 718 | if (IS_ERR(dcssblk_root_dev)) { |
| 719 | PRINT_ERR("device_register() failed!\n"); |
| 720 | return PTR_ERR(dcssblk_root_dev); |
| 721 | } |
| 722 | rc = device_create_file(dcssblk_root_dev, &dev_attr_add); |
| 723 | if (rc) { |
| 724 | PRINT_ERR("device_create_file(add) failed!\n"); |
| 725 | s390_root_dev_unregister(dcssblk_root_dev); |
| 726 | return rc; |
| 727 | } |
| 728 | rc = device_create_file(dcssblk_root_dev, &dev_attr_remove); |
| 729 | if (rc) { |
| 730 | PRINT_ERR("device_create_file(remove) failed!\n"); |
| 731 | s390_root_dev_unregister(dcssblk_root_dev); |
| 732 | return rc; |
| 733 | } |
| 734 | rc = register_blkdev(0, DCSSBLK_NAME); |
| 735 | if (rc < 0) { |
| 736 | PRINT_ERR("Can't get dynamic major!\n"); |
| 737 | s390_root_dev_unregister(dcssblk_root_dev); |
| 738 | return rc; |
| 739 | } |
| 740 | dcssblk_major = rc; |
| 741 | init_rwsem(&dcssblk_devices_sem); |
| 742 | |
| 743 | dcssblk_check_params(); |
| 744 | |
| 745 | PRINT_DEBUG("...finished!\n"); |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | module_init(dcssblk_init); |
| 750 | module_exit(dcssblk_exit); |
| 751 | |
| 752 | module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444); |
| 753 | MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, " |
| 754 | "comma-separated list, each name max. 8 chars.\n" |
| 755 | "Adding \"(local)\" to segment name equals echoing 0 to " |
| 756 | "/sys/devices/dcssblk/<segment name>/shared after loading " |
| 757 | "the segment - \n" |
| 758 | "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\""); |
| 759 | |
| 760 | MODULE_LICENSE("GPL"); |