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