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