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