1 /* 2 * Copyright (C) 2000 Jens Axboe <axboe@suse.de> 3 * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com> 4 * Copyright (C) 2006 Thomas Maier <balagi@justmail.de> 5 * 6 * May be copied or modified under the terms of the GNU General Public 7 * License. See linux/COPYING for more information. 8 * 9 * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and 10 * DVD-RAM devices. 11 * 12 * Theory of operation: 13 * 14 * At the lowest level, there is the standard driver for the CD/DVD device, 15 * typically ide-cd.c or sr.c. This driver can handle read and write requests, 16 * but it doesn't know anything about the special restrictions that apply to 17 * packet writing. One restriction is that write requests must be aligned to 18 * packet boundaries on the physical media, and the size of a write request 19 * must be equal to the packet size. Another restriction is that a 20 * GPCMD_FLUSH_CACHE command has to be issued to the drive before a read 21 * command, if the previous command was a write. 22 * 23 * The purpose of the packet writing driver is to hide these restrictions from 24 * higher layers, such as file systems, and present a block device that can be 25 * randomly read and written using 2kB-sized blocks. 26 * 27 * The lowest layer in the packet writing driver is the packet I/O scheduler. 28 * Its data is defined by the struct packet_iosched and includes two bio 29 * queues with pending read and write requests. These queues are processed 30 * by the pkt_iosched_process_queue() function. The write requests in this 31 * queue are already properly aligned and sized. This layer is responsible for 32 * issuing the flush cache commands and scheduling the I/O in a good order. 33 * 34 * The next layer transforms unaligned write requests to aligned writes. This 35 * transformation requires reading missing pieces of data from the underlying 36 * block device, assembling the pieces to full packets and queuing them to the 37 * packet I/O scheduler. 38 * 39 * At the top layer there is a custom ->submit_bio function that forwards 40 * read requests directly to the iosched queue and puts write requests in the 41 * unaligned write queue. A kernel thread performs the necessary read 42 * gathering to convert the unaligned writes to aligned writes and then feeds 43 * them to the packet I/O scheduler. 44 * 45 *************************************************************************/ 46 47 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 48 49 #include <linux/pktcdvd.h> 50 #include <linux/module.h> 51 #include <linux/types.h> 52 #include <linux/kernel.h> 53 #include <linux/compat.h> 54 #include <linux/kthread.h> 55 #include <linux/errno.h> 56 #include <linux/spinlock.h> 57 #include <linux/file.h> 58 #include <linux/proc_fs.h> 59 #include <linux/seq_file.h> 60 #include <linux/miscdevice.h> 61 #include <linux/freezer.h> 62 #include <linux/mutex.h> 63 #include <linux/slab.h> 64 #include <linux/backing-dev.h> 65 #include <scsi/scsi_cmnd.h> 66 #include <scsi/scsi_ioctl.h> 67 #include <scsi/scsi.h> 68 #include <linux/debugfs.h> 69 #include <linux/device.h> 70 #include <linux/nospec.h> 71 #include <linux/uaccess.h> 72 73 #define DRIVER_NAME "pktcdvd" 74 75 #define pkt_err(pd, fmt, ...) \ 76 pr_err("%s: " fmt, pd->name, ##__VA_ARGS__) 77 #define pkt_notice(pd, fmt, ...) \ 78 pr_notice("%s: " fmt, pd->name, ##__VA_ARGS__) 79 #define pkt_info(pd, fmt, ...) \ 80 pr_info("%s: " fmt, pd->name, ##__VA_ARGS__) 81 82 #define pkt_dbg(level, pd, fmt, ...) \ 83 do { \ 84 if (level == 2 && PACKET_DEBUG >= 2) \ 85 pr_notice("%s: %s():" fmt, \ 86 pd->name, __func__, ##__VA_ARGS__); \ 87 else if (level == 1 && PACKET_DEBUG >= 1) \ 88 pr_notice("%s: " fmt, pd->name, ##__VA_ARGS__); \ 89 } while (0) 90 91 #define MAX_SPEED 0xffff 92 93 static DEFINE_MUTEX(pktcdvd_mutex); 94 static struct pktcdvd_device *pkt_devs[MAX_WRITERS]; 95 static struct proc_dir_entry *pkt_proc; 96 static int pktdev_major; 97 static int write_congestion_on = PKT_WRITE_CONGESTION_ON; 98 static int write_congestion_off = PKT_WRITE_CONGESTION_OFF; 99 static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */ 100 static mempool_t psd_pool; 101 static struct bio_set pkt_bio_set; 102 103 static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */ 104 static struct dentry *pkt_debugfs_root = NULL; /* /sys/kernel/debug/pktcdvd */ 105 106 /* forward declaration */ 107 static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev); 108 static int pkt_remove_dev(dev_t pkt_dev); 109 static int pkt_seq_show(struct seq_file *m, void *p); 110 111 static sector_t get_zone(sector_t sector, struct pktcdvd_device *pd) 112 { 113 return (sector + pd->offset) & ~(sector_t)(pd->settings.size - 1); 114 } 115 116 /********************************************************** 117 * sysfs interface for pktcdvd 118 * by (C) 2006 Thomas Maier <balagi@justmail.de> 119 120 /sys/class/pktcdvd/pktcdvd[0-7]/ 121 stat/reset 122 stat/packets_started 123 stat/packets_finished 124 stat/kb_written 125 stat/kb_read 126 stat/kb_read_gather 127 write_queue/size 128 write_queue/congestion_off 129 write_queue/congestion_on 130 **********************************************************/ 131 132 static ssize_t packets_started_show(struct device *dev, 133 struct device_attribute *attr, char *buf) 134 { 135 struct pktcdvd_device *pd = dev_get_drvdata(dev); 136 137 return sysfs_emit(buf, "%lu\n", pd->stats.pkt_started); 138 } 139 static DEVICE_ATTR_RO(packets_started); 140 141 static ssize_t packets_finished_show(struct device *dev, 142 struct device_attribute *attr, char *buf) 143 { 144 struct pktcdvd_device *pd = dev_get_drvdata(dev); 145 146 return sysfs_emit(buf, "%lu\n", pd->stats.pkt_ended); 147 } 148 static DEVICE_ATTR_RO(packets_finished); 149 150 static ssize_t kb_written_show(struct device *dev, 151 struct device_attribute *attr, char *buf) 152 { 153 struct pktcdvd_device *pd = dev_get_drvdata(dev); 154 155 return sysfs_emit(buf, "%lu\n", pd->stats.secs_w >> 1); 156 } 157 static DEVICE_ATTR_RO(kb_written); 158 159 static ssize_t kb_read_show(struct device *dev, 160 struct device_attribute *attr, char *buf) 161 { 162 struct pktcdvd_device *pd = dev_get_drvdata(dev); 163 164 return sysfs_emit(buf, "%lu\n", pd->stats.secs_r >> 1); 165 } 166 static DEVICE_ATTR_RO(kb_read); 167 168 static ssize_t kb_read_gather_show(struct device *dev, 169 struct device_attribute *attr, char *buf) 170 { 171 struct pktcdvd_device *pd = dev_get_drvdata(dev); 172 173 return sysfs_emit(buf, "%lu\n", pd->stats.secs_rg >> 1); 174 } 175 static DEVICE_ATTR_RO(kb_read_gather); 176 177 static ssize_t reset_store(struct device *dev, struct device_attribute *attr, 178 const char *buf, size_t len) 179 { 180 struct pktcdvd_device *pd = dev_get_drvdata(dev); 181 182 if (len > 0) { 183 pd->stats.pkt_started = 0; 184 pd->stats.pkt_ended = 0; 185 pd->stats.secs_w = 0; 186 pd->stats.secs_rg = 0; 187 pd->stats.secs_r = 0; 188 } 189 return len; 190 } 191 static DEVICE_ATTR_WO(reset); 192 193 static struct attribute *pkt_stat_attrs[] = { 194 &dev_attr_packets_finished.attr, 195 &dev_attr_packets_started.attr, 196 &dev_attr_kb_read.attr, 197 &dev_attr_kb_written.attr, 198 &dev_attr_kb_read_gather.attr, 199 &dev_attr_reset.attr, 200 NULL, 201 }; 202 203 static const struct attribute_group pkt_stat_group = { 204 .name = "stat", 205 .attrs = pkt_stat_attrs, 206 }; 207 208 static ssize_t size_show(struct device *dev, 209 struct device_attribute *attr, char *buf) 210 { 211 struct pktcdvd_device *pd = dev_get_drvdata(dev); 212 int n; 213 214 spin_lock(&pd->lock); 215 n = sysfs_emit(buf, "%d\n", pd->bio_queue_size); 216 spin_unlock(&pd->lock); 217 return n; 218 } 219 static DEVICE_ATTR_RO(size); 220 221 static void init_write_congestion_marks(int* lo, int* hi) 222 { 223 if (*hi > 0) { 224 *hi = max(*hi, 500); 225 *hi = min(*hi, 1000000); 226 if (*lo <= 0) 227 *lo = *hi - 100; 228 else { 229 *lo = min(*lo, *hi - 100); 230 *lo = max(*lo, 100); 231 } 232 } else { 233 *hi = -1; 234 *lo = -1; 235 } 236 } 237 238 static ssize_t congestion_off_show(struct device *dev, 239 struct device_attribute *attr, char *buf) 240 { 241 struct pktcdvd_device *pd = dev_get_drvdata(dev); 242 int n; 243 244 spin_lock(&pd->lock); 245 n = sysfs_emit(buf, "%d\n", pd->write_congestion_off); 246 spin_unlock(&pd->lock); 247 return n; 248 } 249 250 static ssize_t congestion_off_store(struct device *dev, 251 struct device_attribute *attr, 252 const char *buf, size_t len) 253 { 254 struct pktcdvd_device *pd = dev_get_drvdata(dev); 255 int val; 256 257 if (sscanf(buf, "%d", &val) == 1) { 258 spin_lock(&pd->lock); 259 pd->write_congestion_off = val; 260 init_write_congestion_marks(&pd->write_congestion_off, 261 &pd->write_congestion_on); 262 spin_unlock(&pd->lock); 263 } 264 return len; 265 } 266 static DEVICE_ATTR_RW(congestion_off); 267 268 static ssize_t congestion_on_show(struct device *dev, 269 struct device_attribute *attr, char *buf) 270 { 271 struct pktcdvd_device *pd = dev_get_drvdata(dev); 272 int n; 273 274 spin_lock(&pd->lock); 275 n = sysfs_emit(buf, "%d\n", pd->write_congestion_on); 276 spin_unlock(&pd->lock); 277 return n; 278 } 279 280 static ssize_t congestion_on_store(struct device *dev, 281 struct device_attribute *attr, 282 const char *buf, size_t len) 283 { 284 struct pktcdvd_device *pd = dev_get_drvdata(dev); 285 int val; 286 287 if (sscanf(buf, "%d", &val) == 1) { 288 spin_lock(&pd->lock); 289 pd->write_congestion_on = val; 290 init_write_congestion_marks(&pd->write_congestion_off, 291 &pd->write_congestion_on); 292 spin_unlock(&pd->lock); 293 } 294 return len; 295 } 296 static DEVICE_ATTR_RW(congestion_on); 297 298 static struct attribute *pkt_wq_attrs[] = { 299 &dev_attr_congestion_on.attr, 300 &dev_attr_congestion_off.attr, 301 &dev_attr_size.attr, 302 NULL, 303 }; 304 305 static const struct attribute_group pkt_wq_group = { 306 .name = "write_queue", 307 .attrs = pkt_wq_attrs, 308 }; 309 310 static const struct attribute_group *pkt_groups[] = { 311 &pkt_stat_group, 312 &pkt_wq_group, 313 NULL, 314 }; 315 316 static void pkt_sysfs_dev_new(struct pktcdvd_device *pd) 317 { 318 if (class_pktcdvd) { 319 pd->dev = device_create_with_groups(class_pktcdvd, NULL, 320 MKDEV(0, 0), pd, pkt_groups, 321 "%s", pd->name); 322 if (IS_ERR(pd->dev)) 323 pd->dev = NULL; 324 } 325 } 326 327 static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd) 328 { 329 if (class_pktcdvd) 330 device_unregister(pd->dev); 331 } 332 333 334 /******************************************************************** 335 /sys/class/pktcdvd/ 336 add map block device 337 remove unmap packet dev 338 device_map show mappings 339 *******************************************************************/ 340 341 static void class_pktcdvd_release(struct class *cls) 342 { 343 kfree(cls); 344 } 345 346 static ssize_t device_map_show(struct class *c, struct class_attribute *attr, 347 char *data) 348 { 349 int n = 0; 350 int idx; 351 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); 352 for (idx = 0; idx < MAX_WRITERS; idx++) { 353 struct pktcdvd_device *pd = pkt_devs[idx]; 354 if (!pd) 355 continue; 356 n += sprintf(data+n, "%s %u:%u %u:%u\n", 357 pd->name, 358 MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev), 359 MAJOR(pd->bdev->bd_dev), 360 MINOR(pd->bdev->bd_dev)); 361 } 362 mutex_unlock(&ctl_mutex); 363 return n; 364 } 365 static CLASS_ATTR_RO(device_map); 366 367 static ssize_t add_store(struct class *c, struct class_attribute *attr, 368 const char *buf, size_t count) 369 { 370 unsigned int major, minor; 371 372 if (sscanf(buf, "%u:%u", &major, &minor) == 2) { 373 /* pkt_setup_dev() expects caller to hold reference to self */ 374 if (!try_module_get(THIS_MODULE)) 375 return -ENODEV; 376 377 pkt_setup_dev(MKDEV(major, minor), NULL); 378 379 module_put(THIS_MODULE); 380 381 return count; 382 } 383 384 return -EINVAL; 385 } 386 static CLASS_ATTR_WO(add); 387 388 static ssize_t remove_store(struct class *c, struct class_attribute *attr, 389 const char *buf, size_t count) 390 { 391 unsigned int major, minor; 392 if (sscanf(buf, "%u:%u", &major, &minor) == 2) { 393 pkt_remove_dev(MKDEV(major, minor)); 394 return count; 395 } 396 return -EINVAL; 397 } 398 static CLASS_ATTR_WO(remove); 399 400 static struct attribute *class_pktcdvd_attrs[] = { 401 &class_attr_add.attr, 402 &class_attr_remove.attr, 403 &class_attr_device_map.attr, 404 NULL, 405 }; 406 ATTRIBUTE_GROUPS(class_pktcdvd); 407 408 static int pkt_sysfs_init(void) 409 { 410 int ret = 0; 411 412 /* 413 * create control files in sysfs 414 * /sys/class/pktcdvd/... 415 */ 416 class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL); 417 if (!class_pktcdvd) 418 return -ENOMEM; 419 class_pktcdvd->name = DRIVER_NAME; 420 class_pktcdvd->owner = THIS_MODULE; 421 class_pktcdvd->class_release = class_pktcdvd_release; 422 class_pktcdvd->class_groups = class_pktcdvd_groups; 423 ret = class_register(class_pktcdvd); 424 if (ret) { 425 kfree(class_pktcdvd); 426 class_pktcdvd = NULL; 427 pr_err("failed to create class pktcdvd\n"); 428 return ret; 429 } 430 return 0; 431 } 432 433 static void pkt_sysfs_cleanup(void) 434 { 435 if (class_pktcdvd) 436 class_destroy(class_pktcdvd); 437 class_pktcdvd = NULL; 438 } 439 440 /******************************************************************** 441 entries in debugfs 442 443 /sys/kernel/debug/pktcdvd[0-7]/ 444 info 445 446 *******************************************************************/ 447 448 static int pkt_debugfs_seq_show(struct seq_file *m, void *p) 449 { 450 return pkt_seq_show(m, p); 451 } 452 453 static int pkt_debugfs_fops_open(struct inode *inode, struct file *file) 454 { 455 return single_open(file, pkt_debugfs_seq_show, inode->i_private); 456 } 457 458 static const struct file_operations debug_fops = { 459 .open = pkt_debugfs_fops_open, 460 .read = seq_read, 461 .llseek = seq_lseek, 462 .release = single_release, 463 .owner = THIS_MODULE, 464 }; 465 466 static void pkt_debugfs_dev_new(struct pktcdvd_device *pd) 467 { 468 if (!pkt_debugfs_root) 469 return; 470 pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root); 471 if (!pd->dfs_d_root) 472 return; 473 474 pd->dfs_f_info = debugfs_create_file("info", 0444, 475 pd->dfs_d_root, pd, &debug_fops); 476 } 477 478 static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd) 479 { 480 if (!pkt_debugfs_root) 481 return; 482 debugfs_remove(pd->dfs_f_info); 483 debugfs_remove(pd->dfs_d_root); 484 pd->dfs_f_info = NULL; 485 pd->dfs_d_root = NULL; 486 } 487 488 static void pkt_debugfs_init(void) 489 { 490 pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL); 491 } 492 493 static void pkt_debugfs_cleanup(void) 494 { 495 debugfs_remove(pkt_debugfs_root); 496 pkt_debugfs_root = NULL; 497 } 498 499 /* ----------------------------------------------------------*/ 500 501 502 static void pkt_bio_finished(struct pktcdvd_device *pd) 503 { 504 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0); 505 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) { 506 pkt_dbg(2, pd, "queue empty\n"); 507 atomic_set(&pd->iosched.attention, 1); 508 wake_up(&pd->wqueue); 509 } 510 } 511 512 /* 513 * Allocate a packet_data struct 514 */ 515 static struct packet_data *pkt_alloc_packet_data(int frames) 516 { 517 int i; 518 struct packet_data *pkt; 519 520 pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL); 521 if (!pkt) 522 goto no_pkt; 523 524 pkt->frames = frames; 525 pkt->w_bio = bio_kmalloc(GFP_KERNEL, frames); 526 if (!pkt->w_bio) 527 goto no_bio; 528 529 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) { 530 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO); 531 if (!pkt->pages[i]) 532 goto no_page; 533 } 534 535 spin_lock_init(&pkt->lock); 536 bio_list_init(&pkt->orig_bios); 537 538 for (i = 0; i < frames; i++) { 539 struct bio *bio = bio_kmalloc(GFP_KERNEL, 1); 540 if (!bio) 541 goto no_rd_bio; 542 543 pkt->r_bios[i] = bio; 544 } 545 546 return pkt; 547 548 no_rd_bio: 549 for (i = 0; i < frames; i++) { 550 struct bio *bio = pkt->r_bios[i]; 551 if (bio) 552 bio_put(bio); 553 } 554 555 no_page: 556 for (i = 0; i < frames / FRAMES_PER_PAGE; i++) 557 if (pkt->pages[i]) 558 __free_page(pkt->pages[i]); 559 bio_put(pkt->w_bio); 560 no_bio: 561 kfree(pkt); 562 no_pkt: 563 return NULL; 564 } 565 566 /* 567 * Free a packet_data struct 568 */ 569 static void pkt_free_packet_data(struct packet_data *pkt) 570 { 571 int i; 572 573 for (i = 0; i < pkt->frames; i++) { 574 struct bio *bio = pkt->r_bios[i]; 575 if (bio) 576 bio_put(bio); 577 } 578 for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++) 579 __free_page(pkt->pages[i]); 580 bio_put(pkt->w_bio); 581 kfree(pkt); 582 } 583 584 static void pkt_shrink_pktlist(struct pktcdvd_device *pd) 585 { 586 struct packet_data *pkt, *next; 587 588 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list)); 589 590 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) { 591 pkt_free_packet_data(pkt); 592 } 593 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list); 594 } 595 596 static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets) 597 { 598 struct packet_data *pkt; 599 600 BUG_ON(!list_empty(&pd->cdrw.pkt_free_list)); 601 602 while (nr_packets > 0) { 603 pkt = pkt_alloc_packet_data(pd->settings.size >> 2); 604 if (!pkt) { 605 pkt_shrink_pktlist(pd); 606 return 0; 607 } 608 pkt->id = nr_packets; 609 pkt->pd = pd; 610 list_add(&pkt->list, &pd->cdrw.pkt_free_list); 611 nr_packets--; 612 } 613 return 1; 614 } 615 616 static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node) 617 { 618 struct rb_node *n = rb_next(&node->rb_node); 619 if (!n) 620 return NULL; 621 return rb_entry(n, struct pkt_rb_node, rb_node); 622 } 623 624 static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node) 625 { 626 rb_erase(&node->rb_node, &pd->bio_queue); 627 mempool_free(node, &pd->rb_pool); 628 pd->bio_queue_size--; 629 BUG_ON(pd->bio_queue_size < 0); 630 } 631 632 /* 633 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s. 634 */ 635 static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s) 636 { 637 struct rb_node *n = pd->bio_queue.rb_node; 638 struct rb_node *next; 639 struct pkt_rb_node *tmp; 640 641 if (!n) { 642 BUG_ON(pd->bio_queue_size > 0); 643 return NULL; 644 } 645 646 for (;;) { 647 tmp = rb_entry(n, struct pkt_rb_node, rb_node); 648 if (s <= tmp->bio->bi_iter.bi_sector) 649 next = n->rb_left; 650 else 651 next = n->rb_right; 652 if (!next) 653 break; 654 n = next; 655 } 656 657 if (s > tmp->bio->bi_iter.bi_sector) { 658 tmp = pkt_rbtree_next(tmp); 659 if (!tmp) 660 return NULL; 661 } 662 BUG_ON(s > tmp->bio->bi_iter.bi_sector); 663 return tmp; 664 } 665 666 /* 667 * Insert a node into the pd->bio_queue rb tree. 668 */ 669 static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node) 670 { 671 struct rb_node **p = &pd->bio_queue.rb_node; 672 struct rb_node *parent = NULL; 673 sector_t s = node->bio->bi_iter.bi_sector; 674 struct pkt_rb_node *tmp; 675 676 while (*p) { 677 parent = *p; 678 tmp = rb_entry(parent, struct pkt_rb_node, rb_node); 679 if (s < tmp->bio->bi_iter.bi_sector) 680 p = &(*p)->rb_left; 681 else 682 p = &(*p)->rb_right; 683 } 684 rb_link_node(&node->rb_node, parent, p); 685 rb_insert_color(&node->rb_node, &pd->bio_queue); 686 pd->bio_queue_size++; 687 } 688 689 /* 690 * Send a packet_command to the underlying block device and 691 * wait for completion. 692 */ 693 static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc) 694 { 695 struct request_queue *q = bdev_get_queue(pd->bdev); 696 struct request *rq; 697 int ret = 0; 698 699 rq = scsi_alloc_request(q, (cgc->data_direction == CGC_DATA_WRITE) ? 700 REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); 701 if (IS_ERR(rq)) 702 return PTR_ERR(rq); 703 704 if (cgc->buflen) { 705 ret = blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen, 706 GFP_NOIO); 707 if (ret) 708 goto out; 709 } 710 711 scsi_req(rq)->cmd_len = COMMAND_SIZE(cgc->cmd[0]); 712 memcpy(scsi_req(rq)->cmd, cgc->cmd, CDROM_PACKET_SIZE); 713 714 rq->timeout = 60*HZ; 715 if (cgc->quiet) 716 rq->rq_flags |= RQF_QUIET; 717 718 blk_execute_rq(rq, false); 719 if (scsi_req(rq)->result) 720 ret = -EIO; 721 out: 722 blk_mq_free_request(rq); 723 return ret; 724 } 725 726 static const char *sense_key_string(__u8 index) 727 { 728 static const char * const info[] = { 729 "No sense", "Recovered error", "Not ready", 730 "Medium error", "Hardware error", "Illegal request", 731 "Unit attention", "Data protect", "Blank check", 732 }; 733 734 return index < ARRAY_SIZE(info) ? info[index] : "INVALID"; 735 } 736 737 /* 738 * A generic sense dump / resolve mechanism should be implemented across 739 * all ATAPI + SCSI devices. 740 */ 741 static void pkt_dump_sense(struct pktcdvd_device *pd, 742 struct packet_command *cgc) 743 { 744 struct scsi_sense_hdr *sshdr = cgc->sshdr; 745 746 if (sshdr) 747 pkt_err(pd, "%*ph - sense %02x.%02x.%02x (%s)\n", 748 CDROM_PACKET_SIZE, cgc->cmd, 749 sshdr->sense_key, sshdr->asc, sshdr->ascq, 750 sense_key_string(sshdr->sense_key)); 751 else 752 pkt_err(pd, "%*ph - no sense\n", CDROM_PACKET_SIZE, cgc->cmd); 753 } 754 755 /* 756 * flush the drive cache to media 757 */ 758 static int pkt_flush_cache(struct pktcdvd_device *pd) 759 { 760 struct packet_command cgc; 761 762 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE); 763 cgc.cmd[0] = GPCMD_FLUSH_CACHE; 764 cgc.quiet = 1; 765 766 /* 767 * the IMMED bit -- we default to not setting it, although that 768 * would allow a much faster close, this is safer 769 */ 770 #if 0 771 cgc.cmd[1] = 1 << 1; 772 #endif 773 return pkt_generic_packet(pd, &cgc); 774 } 775 776 /* 777 * speed is given as the normal factor, e.g. 4 for 4x 778 */ 779 static noinline_for_stack int pkt_set_speed(struct pktcdvd_device *pd, 780 unsigned write_speed, unsigned read_speed) 781 { 782 struct packet_command cgc; 783 struct scsi_sense_hdr sshdr; 784 int ret; 785 786 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE); 787 cgc.sshdr = &sshdr; 788 cgc.cmd[0] = GPCMD_SET_SPEED; 789 cgc.cmd[2] = (read_speed >> 8) & 0xff; 790 cgc.cmd[3] = read_speed & 0xff; 791 cgc.cmd[4] = (write_speed >> 8) & 0xff; 792 cgc.cmd[5] = write_speed & 0xff; 793 794 ret = pkt_generic_packet(pd, &cgc); 795 if (ret) 796 pkt_dump_sense(pd, &cgc); 797 798 return ret; 799 } 800 801 /* 802 * Queue a bio for processing by the low-level CD device. Must be called 803 * from process context. 804 */ 805 static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio) 806 { 807 spin_lock(&pd->iosched.lock); 808 if (bio_data_dir(bio) == READ) 809 bio_list_add(&pd->iosched.read_queue, bio); 810 else 811 bio_list_add(&pd->iosched.write_queue, bio); 812 spin_unlock(&pd->iosched.lock); 813 814 atomic_set(&pd->iosched.attention, 1); 815 wake_up(&pd->wqueue); 816 } 817 818 /* 819 * Process the queued read/write requests. This function handles special 820 * requirements for CDRW drives: 821 * - A cache flush command must be inserted before a read request if the 822 * previous request was a write. 823 * - Switching between reading and writing is slow, so don't do it more often 824 * than necessary. 825 * - Optimize for throughput at the expense of latency. This means that streaming 826 * writes will never be interrupted by a read, but if the drive has to seek 827 * before the next write, switch to reading instead if there are any pending 828 * read requests. 829 * - Set the read speed according to current usage pattern. When only reading 830 * from the device, it's best to use the highest possible read speed, but 831 * when switching often between reading and writing, it's better to have the 832 * same read and write speeds. 833 */ 834 static void pkt_iosched_process_queue(struct pktcdvd_device *pd) 835 { 836 837 if (atomic_read(&pd->iosched.attention) == 0) 838 return; 839 atomic_set(&pd->iosched.attention, 0); 840 841 for (;;) { 842 struct bio *bio; 843 int reads_queued, writes_queued; 844 845 spin_lock(&pd->iosched.lock); 846 reads_queued = !bio_list_empty(&pd->iosched.read_queue); 847 writes_queued = !bio_list_empty(&pd->iosched.write_queue); 848 spin_unlock(&pd->iosched.lock); 849 850 if (!reads_queued && !writes_queued) 851 break; 852 853 if (pd->iosched.writing) { 854 int need_write_seek = 1; 855 spin_lock(&pd->iosched.lock); 856 bio = bio_list_peek(&pd->iosched.write_queue); 857 spin_unlock(&pd->iosched.lock); 858 if (bio && (bio->bi_iter.bi_sector == 859 pd->iosched.last_write)) 860 need_write_seek = 0; 861 if (need_write_seek && reads_queued) { 862 if (atomic_read(&pd->cdrw.pending_bios) > 0) { 863 pkt_dbg(2, pd, "write, waiting\n"); 864 break; 865 } 866 pkt_flush_cache(pd); 867 pd->iosched.writing = 0; 868 } 869 } else { 870 if (!reads_queued && writes_queued) { 871 if (atomic_read(&pd->cdrw.pending_bios) > 0) { 872 pkt_dbg(2, pd, "read, waiting\n"); 873 break; 874 } 875 pd->iosched.writing = 1; 876 } 877 } 878 879 spin_lock(&pd->iosched.lock); 880 if (pd->iosched.writing) 881 bio = bio_list_pop(&pd->iosched.write_queue); 882 else 883 bio = bio_list_pop(&pd->iosched.read_queue); 884 spin_unlock(&pd->iosched.lock); 885 886 if (!bio) 887 continue; 888 889 if (bio_data_dir(bio) == READ) 890 pd->iosched.successive_reads += 891 bio->bi_iter.bi_size >> 10; 892 else { 893 pd->iosched.successive_reads = 0; 894 pd->iosched.last_write = bio_end_sector(bio); 895 } 896 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) { 897 if (pd->read_speed == pd->write_speed) { 898 pd->read_speed = MAX_SPEED; 899 pkt_set_speed(pd, pd->write_speed, pd->read_speed); 900 } 901 } else { 902 if (pd->read_speed != pd->write_speed) { 903 pd->read_speed = pd->write_speed; 904 pkt_set_speed(pd, pd->write_speed, pd->read_speed); 905 } 906 } 907 908 atomic_inc(&pd->cdrw.pending_bios); 909 submit_bio_noacct(bio); 910 } 911 } 912 913 /* 914 * Special care is needed if the underlying block device has a small 915 * max_phys_segments value. 916 */ 917 static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q) 918 { 919 if ((pd->settings.size << 9) / CD_FRAMESIZE 920 <= queue_max_segments(q)) { 921 /* 922 * The cdrom device can handle one segment/frame 923 */ 924 clear_bit(PACKET_MERGE_SEGS, &pd->flags); 925 return 0; 926 } else if ((pd->settings.size << 9) / PAGE_SIZE 927 <= queue_max_segments(q)) { 928 /* 929 * We can handle this case at the expense of some extra memory 930 * copies during write operations 931 */ 932 set_bit(PACKET_MERGE_SEGS, &pd->flags); 933 return 0; 934 } else { 935 pkt_err(pd, "cdrom max_phys_segments too small\n"); 936 return -EIO; 937 } 938 } 939 940 static void pkt_end_io_read(struct bio *bio) 941 { 942 struct packet_data *pkt = bio->bi_private; 943 struct pktcdvd_device *pd = pkt->pd; 944 BUG_ON(!pd); 945 946 pkt_dbg(2, pd, "bio=%p sec0=%llx sec=%llx err=%d\n", 947 bio, (unsigned long long)pkt->sector, 948 (unsigned long long)bio->bi_iter.bi_sector, bio->bi_status); 949 950 if (bio->bi_status) 951 atomic_inc(&pkt->io_errors); 952 if (atomic_dec_and_test(&pkt->io_wait)) { 953 atomic_inc(&pkt->run_sm); 954 wake_up(&pd->wqueue); 955 } 956 pkt_bio_finished(pd); 957 } 958 959 static void pkt_end_io_packet_write(struct bio *bio) 960 { 961 struct packet_data *pkt = bio->bi_private; 962 struct pktcdvd_device *pd = pkt->pd; 963 BUG_ON(!pd); 964 965 pkt_dbg(2, pd, "id=%d, err=%d\n", pkt->id, bio->bi_status); 966 967 pd->stats.pkt_ended++; 968 969 pkt_bio_finished(pd); 970 atomic_dec(&pkt->io_wait); 971 atomic_inc(&pkt->run_sm); 972 wake_up(&pd->wqueue); 973 } 974 975 /* 976 * Schedule reads for the holes in a packet 977 */ 978 static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt) 979 { 980 int frames_read = 0; 981 struct bio *bio; 982 int f; 983 char written[PACKET_MAX_SIZE]; 984 985 BUG_ON(bio_list_empty(&pkt->orig_bios)); 986 987 atomic_set(&pkt->io_wait, 0); 988 atomic_set(&pkt->io_errors, 0); 989 990 /* 991 * Figure out which frames we need to read before we can write. 992 */ 993 memset(written, 0, sizeof(written)); 994 spin_lock(&pkt->lock); 995 bio_list_for_each(bio, &pkt->orig_bios) { 996 int first_frame = (bio->bi_iter.bi_sector - pkt->sector) / 997 (CD_FRAMESIZE >> 9); 998 int num_frames = bio->bi_iter.bi_size / CD_FRAMESIZE; 999 pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9); 1000 BUG_ON(first_frame < 0); 1001 BUG_ON(first_frame + num_frames > pkt->frames); 1002 for (f = first_frame; f < first_frame + num_frames; f++) 1003 written[f] = 1; 1004 } 1005 spin_unlock(&pkt->lock); 1006 1007 if (pkt->cache_valid) { 1008 pkt_dbg(2, pd, "zone %llx cached\n", 1009 (unsigned long long)pkt->sector); 1010 goto out_account; 1011 } 1012 1013 /* 1014 * Schedule reads for missing parts of the packet. 1015 */ 1016 for (f = 0; f < pkt->frames; f++) { 1017 int p, offset; 1018 1019 if (written[f]) 1020 continue; 1021 1022 bio = pkt->r_bios[f]; 1023 bio_reset(bio, pd->bdev, REQ_OP_READ); 1024 bio->bi_iter.bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9); 1025 bio->bi_end_io = pkt_end_io_read; 1026 bio->bi_private = pkt; 1027 1028 p = (f * CD_FRAMESIZE) / PAGE_SIZE; 1029 offset = (f * CD_FRAMESIZE) % PAGE_SIZE; 1030 pkt_dbg(2, pd, "Adding frame %d, page:%p offs:%d\n", 1031 f, pkt->pages[p], offset); 1032 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset)) 1033 BUG(); 1034 1035 atomic_inc(&pkt->io_wait); 1036 pkt_queue_bio(pd, bio); 1037 frames_read++; 1038 } 1039 1040 out_account: 1041 pkt_dbg(2, pd, "need %d frames for zone %llx\n", 1042 frames_read, (unsigned long long)pkt->sector); 1043 pd->stats.pkt_started++; 1044 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9); 1045 } 1046 1047 /* 1048 * Find a packet matching zone, or the least recently used packet if 1049 * there is no match. 1050 */ 1051 static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone) 1052 { 1053 struct packet_data *pkt; 1054 1055 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) { 1056 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) { 1057 list_del_init(&pkt->list); 1058 if (pkt->sector != zone) 1059 pkt->cache_valid = 0; 1060 return pkt; 1061 } 1062 } 1063 BUG(); 1064 return NULL; 1065 } 1066 1067 static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt) 1068 { 1069 if (pkt->cache_valid) { 1070 list_add(&pkt->list, &pd->cdrw.pkt_free_list); 1071 } else { 1072 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list); 1073 } 1074 } 1075 1076 static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state) 1077 { 1078 #if PACKET_DEBUG > 1 1079 static const char *state_name[] = { 1080 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED" 1081 }; 1082 enum packet_data_state old_state = pkt->state; 1083 pkt_dbg(2, pd, "pkt %2d : s=%6llx %s -> %s\n", 1084 pkt->id, (unsigned long long)pkt->sector, 1085 state_name[old_state], state_name[state]); 1086 #endif 1087 pkt->state = state; 1088 } 1089 1090 /* 1091 * Scan the work queue to see if we can start a new packet. 1092 * returns non-zero if any work was done. 1093 */ 1094 static int pkt_handle_queue(struct pktcdvd_device *pd) 1095 { 1096 struct packet_data *pkt, *p; 1097 struct bio *bio = NULL; 1098 sector_t zone = 0; /* Suppress gcc warning */ 1099 struct pkt_rb_node *node, *first_node; 1100 struct rb_node *n; 1101 1102 atomic_set(&pd->scan_queue, 0); 1103 1104 if (list_empty(&pd->cdrw.pkt_free_list)) { 1105 pkt_dbg(2, pd, "no pkt\n"); 1106 return 0; 1107 } 1108 1109 /* 1110 * Try to find a zone we are not already working on. 1111 */ 1112 spin_lock(&pd->lock); 1113 first_node = pkt_rbtree_find(pd, pd->current_sector); 1114 if (!first_node) { 1115 n = rb_first(&pd->bio_queue); 1116 if (n) 1117 first_node = rb_entry(n, struct pkt_rb_node, rb_node); 1118 } 1119 node = first_node; 1120 while (node) { 1121 bio = node->bio; 1122 zone = get_zone(bio->bi_iter.bi_sector, pd); 1123 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) { 1124 if (p->sector == zone) { 1125 bio = NULL; 1126 goto try_next_bio; 1127 } 1128 } 1129 break; 1130 try_next_bio: 1131 node = pkt_rbtree_next(node); 1132 if (!node) { 1133 n = rb_first(&pd->bio_queue); 1134 if (n) 1135 node = rb_entry(n, struct pkt_rb_node, rb_node); 1136 } 1137 if (node == first_node) 1138 node = NULL; 1139 } 1140 spin_unlock(&pd->lock); 1141 if (!bio) { 1142 pkt_dbg(2, pd, "no bio\n"); 1143 return 0; 1144 } 1145 1146 pkt = pkt_get_packet_data(pd, zone); 1147 1148 pd->current_sector = zone + pd->settings.size; 1149 pkt->sector = zone; 1150 BUG_ON(pkt->frames != pd->settings.size >> 2); 1151 pkt->write_size = 0; 1152 1153 /* 1154 * Scan work queue for bios in the same zone and link them 1155 * to this packet. 1156 */ 1157 spin_lock(&pd->lock); 1158 pkt_dbg(2, pd, "looking for zone %llx\n", (unsigned long long)zone); 1159 while ((node = pkt_rbtree_find(pd, zone)) != NULL) { 1160 bio = node->bio; 1161 pkt_dbg(2, pd, "found zone=%llx\n", (unsigned long long) 1162 get_zone(bio->bi_iter.bi_sector, pd)); 1163 if (get_zone(bio->bi_iter.bi_sector, pd) != zone) 1164 break; 1165 pkt_rbtree_erase(pd, node); 1166 spin_lock(&pkt->lock); 1167 bio_list_add(&pkt->orig_bios, bio); 1168 pkt->write_size += bio->bi_iter.bi_size / CD_FRAMESIZE; 1169 spin_unlock(&pkt->lock); 1170 } 1171 /* check write congestion marks, and if bio_queue_size is 1172 * below, wake up any waiters 1173 */ 1174 if (pd->congested && 1175 pd->bio_queue_size <= pd->write_congestion_off) { 1176 pd->congested = false; 1177 wake_up_var(&pd->congested); 1178 } 1179 spin_unlock(&pd->lock); 1180 1181 pkt->sleep_time = max(PACKET_WAIT_TIME, 1); 1182 pkt_set_state(pkt, PACKET_WAITING_STATE); 1183 atomic_set(&pkt->run_sm, 1); 1184 1185 spin_lock(&pd->cdrw.active_list_lock); 1186 list_add(&pkt->list, &pd->cdrw.pkt_active_list); 1187 spin_unlock(&pd->cdrw.active_list_lock); 1188 1189 return 1; 1190 } 1191 1192 /** 1193 * bio_list_copy_data - copy contents of data buffers from one chain of bios to 1194 * another 1195 * @src: source bio list 1196 * @dst: destination bio list 1197 * 1198 * Stops when it reaches the end of either the @src list or @dst list - that is, 1199 * copies min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of 1200 * bios). 1201 */ 1202 static void bio_list_copy_data(struct bio *dst, struct bio *src) 1203 { 1204 struct bvec_iter src_iter = src->bi_iter; 1205 struct bvec_iter dst_iter = dst->bi_iter; 1206 1207 while (1) { 1208 if (!src_iter.bi_size) { 1209 src = src->bi_next; 1210 if (!src) 1211 break; 1212 1213 src_iter = src->bi_iter; 1214 } 1215 1216 if (!dst_iter.bi_size) { 1217 dst = dst->bi_next; 1218 if (!dst) 1219 break; 1220 1221 dst_iter = dst->bi_iter; 1222 } 1223 1224 bio_copy_data_iter(dst, &dst_iter, src, &src_iter); 1225 } 1226 } 1227 1228 /* 1229 * Assemble a bio to write one packet and queue the bio for processing 1230 * by the underlying block device. 1231 */ 1232 static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt) 1233 { 1234 int f; 1235 1236 bio_reset(pkt->w_bio, pd->bdev, REQ_OP_WRITE); 1237 pkt->w_bio->bi_iter.bi_sector = pkt->sector; 1238 pkt->w_bio->bi_end_io = pkt_end_io_packet_write; 1239 pkt->w_bio->bi_private = pkt; 1240 1241 /* XXX: locking? */ 1242 for (f = 0; f < pkt->frames; f++) { 1243 struct page *page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE]; 1244 unsigned offset = (f * CD_FRAMESIZE) % PAGE_SIZE; 1245 1246 if (!bio_add_page(pkt->w_bio, page, CD_FRAMESIZE, offset)) 1247 BUG(); 1248 } 1249 pkt_dbg(2, pd, "vcnt=%d\n", pkt->w_bio->bi_vcnt); 1250 1251 /* 1252 * Fill-in bvec with data from orig_bios. 1253 */ 1254 spin_lock(&pkt->lock); 1255 bio_list_copy_data(pkt->w_bio, pkt->orig_bios.head); 1256 1257 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE); 1258 spin_unlock(&pkt->lock); 1259 1260 pkt_dbg(2, pd, "Writing %d frames for zone %llx\n", 1261 pkt->write_size, (unsigned long long)pkt->sector); 1262 1263 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) 1264 pkt->cache_valid = 1; 1265 else 1266 pkt->cache_valid = 0; 1267 1268 /* Start the write request */ 1269 atomic_set(&pkt->io_wait, 1); 1270 pkt_queue_bio(pd, pkt->w_bio); 1271 } 1272 1273 static void pkt_finish_packet(struct packet_data *pkt, blk_status_t status) 1274 { 1275 struct bio *bio; 1276 1277 if (status) 1278 pkt->cache_valid = 0; 1279 1280 /* Finish all bios corresponding to this packet */ 1281 while ((bio = bio_list_pop(&pkt->orig_bios))) { 1282 bio->bi_status = status; 1283 bio_endio(bio); 1284 } 1285 } 1286 1287 static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt) 1288 { 1289 pkt_dbg(2, pd, "pkt %d\n", pkt->id); 1290 1291 for (;;) { 1292 switch (pkt->state) { 1293 case PACKET_WAITING_STATE: 1294 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0)) 1295 return; 1296 1297 pkt->sleep_time = 0; 1298 pkt_gather_data(pd, pkt); 1299 pkt_set_state(pkt, PACKET_READ_WAIT_STATE); 1300 break; 1301 1302 case PACKET_READ_WAIT_STATE: 1303 if (atomic_read(&pkt->io_wait) > 0) 1304 return; 1305 1306 if (atomic_read(&pkt->io_errors) > 0) { 1307 pkt_set_state(pkt, PACKET_RECOVERY_STATE); 1308 } else { 1309 pkt_start_write(pd, pkt); 1310 } 1311 break; 1312 1313 case PACKET_WRITE_WAIT_STATE: 1314 if (atomic_read(&pkt->io_wait) > 0) 1315 return; 1316 1317 if (!pkt->w_bio->bi_status) { 1318 pkt_set_state(pkt, PACKET_FINISHED_STATE); 1319 } else { 1320 pkt_set_state(pkt, PACKET_RECOVERY_STATE); 1321 } 1322 break; 1323 1324 case PACKET_RECOVERY_STATE: 1325 pkt_dbg(2, pd, "No recovery possible\n"); 1326 pkt_set_state(pkt, PACKET_FINISHED_STATE); 1327 break; 1328 1329 case PACKET_FINISHED_STATE: 1330 pkt_finish_packet(pkt, pkt->w_bio->bi_status); 1331 return; 1332 1333 default: 1334 BUG(); 1335 break; 1336 } 1337 } 1338 } 1339 1340 static void pkt_handle_packets(struct pktcdvd_device *pd) 1341 { 1342 struct packet_data *pkt, *next; 1343 1344 /* 1345 * Run state machine for active packets 1346 */ 1347 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { 1348 if (atomic_read(&pkt->run_sm) > 0) { 1349 atomic_set(&pkt->run_sm, 0); 1350 pkt_run_state_machine(pd, pkt); 1351 } 1352 } 1353 1354 /* 1355 * Move no longer active packets to the free list 1356 */ 1357 spin_lock(&pd->cdrw.active_list_lock); 1358 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) { 1359 if (pkt->state == PACKET_FINISHED_STATE) { 1360 list_del(&pkt->list); 1361 pkt_put_packet_data(pd, pkt); 1362 pkt_set_state(pkt, PACKET_IDLE_STATE); 1363 atomic_set(&pd->scan_queue, 1); 1364 } 1365 } 1366 spin_unlock(&pd->cdrw.active_list_lock); 1367 } 1368 1369 static void pkt_count_states(struct pktcdvd_device *pd, int *states) 1370 { 1371 struct packet_data *pkt; 1372 int i; 1373 1374 for (i = 0; i < PACKET_NUM_STATES; i++) 1375 states[i] = 0; 1376 1377 spin_lock(&pd->cdrw.active_list_lock); 1378 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { 1379 states[pkt->state]++; 1380 } 1381 spin_unlock(&pd->cdrw.active_list_lock); 1382 } 1383 1384 /* 1385 * kcdrwd is woken up when writes have been queued for one of our 1386 * registered devices 1387 */ 1388 static int kcdrwd(void *foobar) 1389 { 1390 struct pktcdvd_device *pd = foobar; 1391 struct packet_data *pkt; 1392 long min_sleep_time, residue; 1393 1394 set_user_nice(current, MIN_NICE); 1395 set_freezable(); 1396 1397 for (;;) { 1398 DECLARE_WAITQUEUE(wait, current); 1399 1400 /* 1401 * Wait until there is something to do 1402 */ 1403 add_wait_queue(&pd->wqueue, &wait); 1404 for (;;) { 1405 set_current_state(TASK_INTERRUPTIBLE); 1406 1407 /* Check if we need to run pkt_handle_queue */ 1408 if (atomic_read(&pd->scan_queue) > 0) 1409 goto work_to_do; 1410 1411 /* Check if we need to run the state machine for some packet */ 1412 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { 1413 if (atomic_read(&pkt->run_sm) > 0) 1414 goto work_to_do; 1415 } 1416 1417 /* Check if we need to process the iosched queues */ 1418 if (atomic_read(&pd->iosched.attention) != 0) 1419 goto work_to_do; 1420 1421 /* Otherwise, go to sleep */ 1422 if (PACKET_DEBUG > 1) { 1423 int states[PACKET_NUM_STATES]; 1424 pkt_count_states(pd, states); 1425 pkt_dbg(2, pd, "i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n", 1426 states[0], states[1], states[2], 1427 states[3], states[4], states[5]); 1428 } 1429 1430 min_sleep_time = MAX_SCHEDULE_TIMEOUT; 1431 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { 1432 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time) 1433 min_sleep_time = pkt->sleep_time; 1434 } 1435 1436 pkt_dbg(2, pd, "sleeping\n"); 1437 residue = schedule_timeout(min_sleep_time); 1438 pkt_dbg(2, pd, "wake up\n"); 1439 1440 /* make swsusp happy with our thread */ 1441 try_to_freeze(); 1442 1443 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { 1444 if (!pkt->sleep_time) 1445 continue; 1446 pkt->sleep_time -= min_sleep_time - residue; 1447 if (pkt->sleep_time <= 0) { 1448 pkt->sleep_time = 0; 1449 atomic_inc(&pkt->run_sm); 1450 } 1451 } 1452 1453 if (kthread_should_stop()) 1454 break; 1455 } 1456 work_to_do: 1457 set_current_state(TASK_RUNNING); 1458 remove_wait_queue(&pd->wqueue, &wait); 1459 1460 if (kthread_should_stop()) 1461 break; 1462 1463 /* 1464 * if pkt_handle_queue returns true, we can queue 1465 * another request. 1466 */ 1467 while (pkt_handle_queue(pd)) 1468 ; 1469 1470 /* 1471 * Handle packet state machine 1472 */ 1473 pkt_handle_packets(pd); 1474 1475 /* 1476 * Handle iosched queues 1477 */ 1478 pkt_iosched_process_queue(pd); 1479 } 1480 1481 return 0; 1482 } 1483 1484 static void pkt_print_settings(struct pktcdvd_device *pd) 1485 { 1486 pkt_info(pd, "%s packets, %u blocks, Mode-%c disc\n", 1487 pd->settings.fp ? "Fixed" : "Variable", 1488 pd->settings.size >> 2, 1489 pd->settings.block_mode == 8 ? '1' : '2'); 1490 } 1491 1492 static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control) 1493 { 1494 memset(cgc->cmd, 0, sizeof(cgc->cmd)); 1495 1496 cgc->cmd[0] = GPCMD_MODE_SENSE_10; 1497 cgc->cmd[2] = page_code | (page_control << 6); 1498 cgc->cmd[7] = cgc->buflen >> 8; 1499 cgc->cmd[8] = cgc->buflen & 0xff; 1500 cgc->data_direction = CGC_DATA_READ; 1501 return pkt_generic_packet(pd, cgc); 1502 } 1503 1504 static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc) 1505 { 1506 memset(cgc->cmd, 0, sizeof(cgc->cmd)); 1507 memset(cgc->buffer, 0, 2); 1508 cgc->cmd[0] = GPCMD_MODE_SELECT_10; 1509 cgc->cmd[1] = 0x10; /* PF */ 1510 cgc->cmd[7] = cgc->buflen >> 8; 1511 cgc->cmd[8] = cgc->buflen & 0xff; 1512 cgc->data_direction = CGC_DATA_WRITE; 1513 return pkt_generic_packet(pd, cgc); 1514 } 1515 1516 static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di) 1517 { 1518 struct packet_command cgc; 1519 int ret; 1520 1521 /* set up command and get the disc info */ 1522 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ); 1523 cgc.cmd[0] = GPCMD_READ_DISC_INFO; 1524 cgc.cmd[8] = cgc.buflen = 2; 1525 cgc.quiet = 1; 1526 1527 ret = pkt_generic_packet(pd, &cgc); 1528 if (ret) 1529 return ret; 1530 1531 /* not all drives have the same disc_info length, so requeue 1532 * packet with the length the drive tells us it can supply 1533 */ 1534 cgc.buflen = be16_to_cpu(di->disc_information_length) + 1535 sizeof(di->disc_information_length); 1536 1537 if (cgc.buflen > sizeof(disc_information)) 1538 cgc.buflen = sizeof(disc_information); 1539 1540 cgc.cmd[8] = cgc.buflen; 1541 return pkt_generic_packet(pd, &cgc); 1542 } 1543 1544 static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti) 1545 { 1546 struct packet_command cgc; 1547 int ret; 1548 1549 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ); 1550 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO; 1551 cgc.cmd[1] = type & 3; 1552 cgc.cmd[4] = (track & 0xff00) >> 8; 1553 cgc.cmd[5] = track & 0xff; 1554 cgc.cmd[8] = 8; 1555 cgc.quiet = 1; 1556 1557 ret = pkt_generic_packet(pd, &cgc); 1558 if (ret) 1559 return ret; 1560 1561 cgc.buflen = be16_to_cpu(ti->track_information_length) + 1562 sizeof(ti->track_information_length); 1563 1564 if (cgc.buflen > sizeof(track_information)) 1565 cgc.buflen = sizeof(track_information); 1566 1567 cgc.cmd[8] = cgc.buflen; 1568 return pkt_generic_packet(pd, &cgc); 1569 } 1570 1571 static noinline_for_stack int pkt_get_last_written(struct pktcdvd_device *pd, 1572 long *last_written) 1573 { 1574 disc_information di; 1575 track_information ti; 1576 __u32 last_track; 1577 int ret; 1578 1579 ret = pkt_get_disc_info(pd, &di); 1580 if (ret) 1581 return ret; 1582 1583 last_track = (di.last_track_msb << 8) | di.last_track_lsb; 1584 ret = pkt_get_track_info(pd, last_track, 1, &ti); 1585 if (ret) 1586 return ret; 1587 1588 /* if this track is blank, try the previous. */ 1589 if (ti.blank) { 1590 last_track--; 1591 ret = pkt_get_track_info(pd, last_track, 1, &ti); 1592 if (ret) 1593 return ret; 1594 } 1595 1596 /* if last recorded field is valid, return it. */ 1597 if (ti.lra_v) { 1598 *last_written = be32_to_cpu(ti.last_rec_address); 1599 } else { 1600 /* make it up instead */ 1601 *last_written = be32_to_cpu(ti.track_start) + 1602 be32_to_cpu(ti.track_size); 1603 if (ti.free_blocks) 1604 *last_written -= (be32_to_cpu(ti.free_blocks) + 7); 1605 } 1606 return 0; 1607 } 1608 1609 /* 1610 * write mode select package based on pd->settings 1611 */ 1612 static noinline_for_stack int pkt_set_write_settings(struct pktcdvd_device *pd) 1613 { 1614 struct packet_command cgc; 1615 struct scsi_sense_hdr sshdr; 1616 write_param_page *wp; 1617 char buffer[128]; 1618 int ret, size; 1619 1620 /* doesn't apply to DVD+RW or DVD-RAM */ 1621 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12)) 1622 return 0; 1623 1624 memset(buffer, 0, sizeof(buffer)); 1625 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ); 1626 cgc.sshdr = &sshdr; 1627 ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0); 1628 if (ret) { 1629 pkt_dump_sense(pd, &cgc); 1630 return ret; 1631 } 1632 1633 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff)); 1634 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff); 1635 if (size > sizeof(buffer)) 1636 size = sizeof(buffer); 1637 1638 /* 1639 * now get it all 1640 */ 1641 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ); 1642 cgc.sshdr = &sshdr; 1643 ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0); 1644 if (ret) { 1645 pkt_dump_sense(pd, &cgc); 1646 return ret; 1647 } 1648 1649 /* 1650 * write page is offset header + block descriptor length 1651 */ 1652 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset]; 1653 1654 wp->fp = pd->settings.fp; 1655 wp->track_mode = pd->settings.track_mode; 1656 wp->write_type = pd->settings.write_type; 1657 wp->data_block_type = pd->settings.block_mode; 1658 1659 wp->multi_session = 0; 1660 1661 #ifdef PACKET_USE_LS 1662 wp->link_size = 7; 1663 wp->ls_v = 1; 1664 #endif 1665 1666 if (wp->data_block_type == PACKET_BLOCK_MODE1) { 1667 wp->session_format = 0; 1668 wp->subhdr2 = 0x20; 1669 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) { 1670 wp->session_format = 0x20; 1671 wp->subhdr2 = 8; 1672 #if 0 1673 wp->mcn[0] = 0x80; 1674 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1); 1675 #endif 1676 } else { 1677 /* 1678 * paranoia 1679 */ 1680 pkt_err(pd, "write mode wrong %d\n", wp->data_block_type); 1681 return 1; 1682 } 1683 wp->packet_size = cpu_to_be32(pd->settings.size >> 2); 1684 1685 cgc.buflen = cgc.cmd[8] = size; 1686 ret = pkt_mode_select(pd, &cgc); 1687 if (ret) { 1688 pkt_dump_sense(pd, &cgc); 1689 return ret; 1690 } 1691 1692 pkt_print_settings(pd); 1693 return 0; 1694 } 1695 1696 /* 1697 * 1 -- we can write to this track, 0 -- we can't 1698 */ 1699 static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti) 1700 { 1701 switch (pd->mmc3_profile) { 1702 case 0x1a: /* DVD+RW */ 1703 case 0x12: /* DVD-RAM */ 1704 /* The track is always writable on DVD+RW/DVD-RAM */ 1705 return 1; 1706 default: 1707 break; 1708 } 1709 1710 if (!ti->packet || !ti->fp) 1711 return 0; 1712 1713 /* 1714 * "good" settings as per Mt Fuji. 1715 */ 1716 if (ti->rt == 0 && ti->blank == 0) 1717 return 1; 1718 1719 if (ti->rt == 0 && ti->blank == 1) 1720 return 1; 1721 1722 if (ti->rt == 1 && ti->blank == 0) 1723 return 1; 1724 1725 pkt_err(pd, "bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet); 1726 return 0; 1727 } 1728 1729 /* 1730 * 1 -- we can write to this disc, 0 -- we can't 1731 */ 1732 static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di) 1733 { 1734 switch (pd->mmc3_profile) { 1735 case 0x0a: /* CD-RW */ 1736 case 0xffff: /* MMC3 not supported */ 1737 break; 1738 case 0x1a: /* DVD+RW */ 1739 case 0x13: /* DVD-RW */ 1740 case 0x12: /* DVD-RAM */ 1741 return 1; 1742 default: 1743 pkt_dbg(2, pd, "Wrong disc profile (%x)\n", 1744 pd->mmc3_profile); 1745 return 0; 1746 } 1747 1748 /* 1749 * for disc type 0xff we should probably reserve a new track. 1750 * but i'm not sure, should we leave this to user apps? probably. 1751 */ 1752 if (di->disc_type == 0xff) { 1753 pkt_notice(pd, "unknown disc - no track?\n"); 1754 return 0; 1755 } 1756 1757 if (di->disc_type != 0x20 && di->disc_type != 0) { 1758 pkt_err(pd, "wrong disc type (%x)\n", di->disc_type); 1759 return 0; 1760 } 1761 1762 if (di->erasable == 0) { 1763 pkt_notice(pd, "disc not erasable\n"); 1764 return 0; 1765 } 1766 1767 if (di->border_status == PACKET_SESSION_RESERVED) { 1768 pkt_err(pd, "can't write to last track (reserved)\n"); 1769 return 0; 1770 } 1771 1772 return 1; 1773 } 1774 1775 static noinline_for_stack int pkt_probe_settings(struct pktcdvd_device *pd) 1776 { 1777 struct packet_command cgc; 1778 unsigned char buf[12]; 1779 disc_information di; 1780 track_information ti; 1781 int ret, track; 1782 1783 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ); 1784 cgc.cmd[0] = GPCMD_GET_CONFIGURATION; 1785 cgc.cmd[8] = 8; 1786 ret = pkt_generic_packet(pd, &cgc); 1787 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7]; 1788 1789 memset(&di, 0, sizeof(disc_information)); 1790 memset(&ti, 0, sizeof(track_information)); 1791 1792 ret = pkt_get_disc_info(pd, &di); 1793 if (ret) { 1794 pkt_err(pd, "failed get_disc\n"); 1795 return ret; 1796 } 1797 1798 if (!pkt_writable_disc(pd, &di)) 1799 return -EROFS; 1800 1801 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR; 1802 1803 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */ 1804 ret = pkt_get_track_info(pd, track, 1, &ti); 1805 if (ret) { 1806 pkt_err(pd, "failed get_track\n"); 1807 return ret; 1808 } 1809 1810 if (!pkt_writable_track(pd, &ti)) { 1811 pkt_err(pd, "can't write to this track\n"); 1812 return -EROFS; 1813 } 1814 1815 /* 1816 * we keep packet size in 512 byte units, makes it easier to 1817 * deal with request calculations. 1818 */ 1819 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2; 1820 if (pd->settings.size == 0) { 1821 pkt_notice(pd, "detected zero packet size!\n"); 1822 return -ENXIO; 1823 } 1824 if (pd->settings.size > PACKET_MAX_SECTORS) { 1825 pkt_err(pd, "packet size is too big\n"); 1826 return -EROFS; 1827 } 1828 pd->settings.fp = ti.fp; 1829 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1); 1830 1831 if (ti.nwa_v) { 1832 pd->nwa = be32_to_cpu(ti.next_writable); 1833 set_bit(PACKET_NWA_VALID, &pd->flags); 1834 } 1835 1836 /* 1837 * in theory we could use lra on -RW media as well and just zero 1838 * blocks that haven't been written yet, but in practice that 1839 * is just a no-go. we'll use that for -R, naturally. 1840 */ 1841 if (ti.lra_v) { 1842 pd->lra = be32_to_cpu(ti.last_rec_address); 1843 set_bit(PACKET_LRA_VALID, &pd->flags); 1844 } else { 1845 pd->lra = 0xffffffff; 1846 set_bit(PACKET_LRA_VALID, &pd->flags); 1847 } 1848 1849 /* 1850 * fine for now 1851 */ 1852 pd->settings.link_loss = 7; 1853 pd->settings.write_type = 0; /* packet */ 1854 pd->settings.track_mode = ti.track_mode; 1855 1856 /* 1857 * mode1 or mode2 disc 1858 */ 1859 switch (ti.data_mode) { 1860 case PACKET_MODE1: 1861 pd->settings.block_mode = PACKET_BLOCK_MODE1; 1862 break; 1863 case PACKET_MODE2: 1864 pd->settings.block_mode = PACKET_BLOCK_MODE2; 1865 break; 1866 default: 1867 pkt_err(pd, "unknown data mode\n"); 1868 return -EROFS; 1869 } 1870 return 0; 1871 } 1872 1873 /* 1874 * enable/disable write caching on drive 1875 */ 1876 static noinline_for_stack int pkt_write_caching(struct pktcdvd_device *pd, 1877 int set) 1878 { 1879 struct packet_command cgc; 1880 struct scsi_sense_hdr sshdr; 1881 unsigned char buf[64]; 1882 int ret; 1883 1884 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ); 1885 cgc.sshdr = &sshdr; 1886 cgc.buflen = pd->mode_offset + 12; 1887 1888 /* 1889 * caching mode page might not be there, so quiet this command 1890 */ 1891 cgc.quiet = 1; 1892 1893 ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0); 1894 if (ret) 1895 return ret; 1896 1897 buf[pd->mode_offset + 10] |= (!!set << 2); 1898 1899 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff)); 1900 ret = pkt_mode_select(pd, &cgc); 1901 if (ret) { 1902 pkt_err(pd, "write caching control failed\n"); 1903 pkt_dump_sense(pd, &cgc); 1904 } else if (!ret && set) 1905 pkt_notice(pd, "enabled write caching\n"); 1906 return ret; 1907 } 1908 1909 static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag) 1910 { 1911 struct packet_command cgc; 1912 1913 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE); 1914 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL; 1915 cgc.cmd[4] = lockflag ? 1 : 0; 1916 return pkt_generic_packet(pd, &cgc); 1917 } 1918 1919 /* 1920 * Returns drive maximum write speed 1921 */ 1922 static noinline_for_stack int pkt_get_max_speed(struct pktcdvd_device *pd, 1923 unsigned *write_speed) 1924 { 1925 struct packet_command cgc; 1926 struct scsi_sense_hdr sshdr; 1927 unsigned char buf[256+18]; 1928 unsigned char *cap_buf; 1929 int ret, offset; 1930 1931 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset]; 1932 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN); 1933 cgc.sshdr = &sshdr; 1934 1935 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0); 1936 if (ret) { 1937 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 + 1938 sizeof(struct mode_page_header); 1939 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0); 1940 if (ret) { 1941 pkt_dump_sense(pd, &cgc); 1942 return ret; 1943 } 1944 } 1945 1946 offset = 20; /* Obsoleted field, used by older drives */ 1947 if (cap_buf[1] >= 28) 1948 offset = 28; /* Current write speed selected */ 1949 if (cap_buf[1] >= 30) { 1950 /* If the drive reports at least one "Logical Unit Write 1951 * Speed Performance Descriptor Block", use the information 1952 * in the first block. (contains the highest speed) 1953 */ 1954 int num_spdb = (cap_buf[30] << 8) + cap_buf[31]; 1955 if (num_spdb > 0) 1956 offset = 34; 1957 } 1958 1959 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1]; 1960 return 0; 1961 } 1962 1963 /* These tables from cdrecord - I don't have orange book */ 1964 /* standard speed CD-RW (1-4x) */ 1965 static char clv_to_speed[16] = { 1966 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 1967 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 1968 }; 1969 /* high speed CD-RW (-10x) */ 1970 static char hs_clv_to_speed[16] = { 1971 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 1972 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 1973 }; 1974 /* ultra high speed CD-RW */ 1975 static char us_clv_to_speed[16] = { 1976 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 1977 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0 1978 }; 1979 1980 /* 1981 * reads the maximum media speed from ATIP 1982 */ 1983 static noinline_for_stack int pkt_media_speed(struct pktcdvd_device *pd, 1984 unsigned *speed) 1985 { 1986 struct packet_command cgc; 1987 struct scsi_sense_hdr sshdr; 1988 unsigned char buf[64]; 1989 unsigned int size, st, sp; 1990 int ret; 1991 1992 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ); 1993 cgc.sshdr = &sshdr; 1994 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP; 1995 cgc.cmd[1] = 2; 1996 cgc.cmd[2] = 4; /* READ ATIP */ 1997 cgc.cmd[8] = 2; 1998 ret = pkt_generic_packet(pd, &cgc); 1999 if (ret) { 2000 pkt_dump_sense(pd, &cgc); 2001 return ret; 2002 } 2003 size = ((unsigned int) buf[0]<<8) + buf[1] + 2; 2004 if (size > sizeof(buf)) 2005 size = sizeof(buf); 2006 2007 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ); 2008 cgc.sshdr = &sshdr; 2009 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP; 2010 cgc.cmd[1] = 2; 2011 cgc.cmd[2] = 4; 2012 cgc.cmd[8] = size; 2013 ret = pkt_generic_packet(pd, &cgc); 2014 if (ret) { 2015 pkt_dump_sense(pd, &cgc); 2016 return ret; 2017 } 2018 2019 if (!(buf[6] & 0x40)) { 2020 pkt_notice(pd, "disc type is not CD-RW\n"); 2021 return 1; 2022 } 2023 if (!(buf[6] & 0x4)) { 2024 pkt_notice(pd, "A1 values on media are not valid, maybe not CDRW?\n"); 2025 return 1; 2026 } 2027 2028 st = (buf[6] >> 3) & 0x7; /* disc sub-type */ 2029 2030 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */ 2031 2032 /* Info from cdrecord */ 2033 switch (st) { 2034 case 0: /* standard speed */ 2035 *speed = clv_to_speed[sp]; 2036 break; 2037 case 1: /* high speed */ 2038 *speed = hs_clv_to_speed[sp]; 2039 break; 2040 case 2: /* ultra high speed */ 2041 *speed = us_clv_to_speed[sp]; 2042 break; 2043 default: 2044 pkt_notice(pd, "unknown disc sub-type %d\n", st); 2045 return 1; 2046 } 2047 if (*speed) { 2048 pkt_info(pd, "maximum media speed: %d\n", *speed); 2049 return 0; 2050 } else { 2051 pkt_notice(pd, "unknown speed %d for sub-type %d\n", sp, st); 2052 return 1; 2053 } 2054 } 2055 2056 static noinline_for_stack int pkt_perform_opc(struct pktcdvd_device *pd) 2057 { 2058 struct packet_command cgc; 2059 struct scsi_sense_hdr sshdr; 2060 int ret; 2061 2062 pkt_dbg(2, pd, "Performing OPC\n"); 2063 2064 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE); 2065 cgc.sshdr = &sshdr; 2066 cgc.timeout = 60*HZ; 2067 cgc.cmd[0] = GPCMD_SEND_OPC; 2068 cgc.cmd[1] = 1; 2069 ret = pkt_generic_packet(pd, &cgc); 2070 if (ret) 2071 pkt_dump_sense(pd, &cgc); 2072 return ret; 2073 } 2074 2075 static int pkt_open_write(struct pktcdvd_device *pd) 2076 { 2077 int ret; 2078 unsigned int write_speed, media_write_speed, read_speed; 2079 2080 ret = pkt_probe_settings(pd); 2081 if (ret) { 2082 pkt_dbg(2, pd, "failed probe\n"); 2083 return ret; 2084 } 2085 2086 ret = pkt_set_write_settings(pd); 2087 if (ret) { 2088 pkt_dbg(1, pd, "failed saving write settings\n"); 2089 return -EIO; 2090 } 2091 2092 pkt_write_caching(pd, USE_WCACHING); 2093 2094 ret = pkt_get_max_speed(pd, &write_speed); 2095 if (ret) 2096 write_speed = 16 * 177; 2097 switch (pd->mmc3_profile) { 2098 case 0x13: /* DVD-RW */ 2099 case 0x1a: /* DVD+RW */ 2100 case 0x12: /* DVD-RAM */ 2101 pkt_dbg(1, pd, "write speed %ukB/s\n", write_speed); 2102 break; 2103 default: 2104 ret = pkt_media_speed(pd, &media_write_speed); 2105 if (ret) 2106 media_write_speed = 16; 2107 write_speed = min(write_speed, media_write_speed * 177); 2108 pkt_dbg(1, pd, "write speed %ux\n", write_speed / 176); 2109 break; 2110 } 2111 read_speed = write_speed; 2112 2113 ret = pkt_set_speed(pd, write_speed, read_speed); 2114 if (ret) { 2115 pkt_dbg(1, pd, "couldn't set write speed\n"); 2116 return -EIO; 2117 } 2118 pd->write_speed = write_speed; 2119 pd->read_speed = read_speed; 2120 2121 ret = pkt_perform_opc(pd); 2122 if (ret) { 2123 pkt_dbg(1, pd, "Optimum Power Calibration failed\n"); 2124 } 2125 2126 return 0; 2127 } 2128 2129 /* 2130 * called at open time. 2131 */ 2132 static int pkt_open_dev(struct pktcdvd_device *pd, fmode_t write) 2133 { 2134 int ret; 2135 long lba; 2136 struct request_queue *q; 2137 struct block_device *bdev; 2138 2139 /* 2140 * We need to re-open the cdrom device without O_NONBLOCK to be able 2141 * to read/write from/to it. It is already opened in O_NONBLOCK mode 2142 * so open should not fail. 2143 */ 2144 bdev = blkdev_get_by_dev(pd->bdev->bd_dev, FMODE_READ | FMODE_EXCL, pd); 2145 if (IS_ERR(bdev)) { 2146 ret = PTR_ERR(bdev); 2147 goto out; 2148 } 2149 2150 ret = pkt_get_last_written(pd, &lba); 2151 if (ret) { 2152 pkt_err(pd, "pkt_get_last_written failed\n"); 2153 goto out_putdev; 2154 } 2155 2156 set_capacity(pd->disk, lba << 2); 2157 set_capacity_and_notify(pd->bdev->bd_disk, lba << 2); 2158 2159 q = bdev_get_queue(pd->bdev); 2160 if (write) { 2161 ret = pkt_open_write(pd); 2162 if (ret) 2163 goto out_putdev; 2164 /* 2165 * Some CDRW drives can not handle writes larger than one packet, 2166 * even if the size is a multiple of the packet size. 2167 */ 2168 blk_queue_max_hw_sectors(q, pd->settings.size); 2169 set_bit(PACKET_WRITABLE, &pd->flags); 2170 } else { 2171 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED); 2172 clear_bit(PACKET_WRITABLE, &pd->flags); 2173 } 2174 2175 ret = pkt_set_segment_merging(pd, q); 2176 if (ret) 2177 goto out_putdev; 2178 2179 if (write) { 2180 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) { 2181 pkt_err(pd, "not enough memory for buffers\n"); 2182 ret = -ENOMEM; 2183 goto out_putdev; 2184 } 2185 pkt_info(pd, "%lukB available on disc\n", lba << 1); 2186 } 2187 2188 return 0; 2189 2190 out_putdev: 2191 blkdev_put(bdev, FMODE_READ | FMODE_EXCL); 2192 out: 2193 return ret; 2194 } 2195 2196 /* 2197 * called when the device is closed. makes sure that the device flushes 2198 * the internal cache before we close. 2199 */ 2200 static void pkt_release_dev(struct pktcdvd_device *pd, int flush) 2201 { 2202 if (flush && pkt_flush_cache(pd)) 2203 pkt_dbg(1, pd, "not flushing cache\n"); 2204 2205 pkt_lock_door(pd, 0); 2206 2207 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED); 2208 blkdev_put(pd->bdev, FMODE_READ | FMODE_EXCL); 2209 2210 pkt_shrink_pktlist(pd); 2211 } 2212 2213 static struct pktcdvd_device *pkt_find_dev_from_minor(unsigned int dev_minor) 2214 { 2215 if (dev_minor >= MAX_WRITERS) 2216 return NULL; 2217 2218 dev_minor = array_index_nospec(dev_minor, MAX_WRITERS); 2219 return pkt_devs[dev_minor]; 2220 } 2221 2222 static int pkt_open(struct block_device *bdev, fmode_t mode) 2223 { 2224 struct pktcdvd_device *pd = NULL; 2225 int ret; 2226 2227 mutex_lock(&pktcdvd_mutex); 2228 mutex_lock(&ctl_mutex); 2229 pd = pkt_find_dev_from_minor(MINOR(bdev->bd_dev)); 2230 if (!pd) { 2231 ret = -ENODEV; 2232 goto out; 2233 } 2234 BUG_ON(pd->refcnt < 0); 2235 2236 pd->refcnt++; 2237 if (pd->refcnt > 1) { 2238 if ((mode & FMODE_WRITE) && 2239 !test_bit(PACKET_WRITABLE, &pd->flags)) { 2240 ret = -EBUSY; 2241 goto out_dec; 2242 } 2243 } else { 2244 ret = pkt_open_dev(pd, mode & FMODE_WRITE); 2245 if (ret) 2246 goto out_dec; 2247 /* 2248 * needed here as well, since ext2 (among others) may change 2249 * the blocksize at mount time 2250 */ 2251 set_blocksize(bdev, CD_FRAMESIZE); 2252 } 2253 2254 mutex_unlock(&ctl_mutex); 2255 mutex_unlock(&pktcdvd_mutex); 2256 return 0; 2257 2258 out_dec: 2259 pd->refcnt--; 2260 out: 2261 mutex_unlock(&ctl_mutex); 2262 mutex_unlock(&pktcdvd_mutex); 2263 return ret; 2264 } 2265 2266 static void pkt_close(struct gendisk *disk, fmode_t mode) 2267 { 2268 struct pktcdvd_device *pd = disk->private_data; 2269 2270 mutex_lock(&pktcdvd_mutex); 2271 mutex_lock(&ctl_mutex); 2272 pd->refcnt--; 2273 BUG_ON(pd->refcnt < 0); 2274 if (pd->refcnt == 0) { 2275 int flush = test_bit(PACKET_WRITABLE, &pd->flags); 2276 pkt_release_dev(pd, flush); 2277 } 2278 mutex_unlock(&ctl_mutex); 2279 mutex_unlock(&pktcdvd_mutex); 2280 } 2281 2282 2283 static void pkt_end_io_read_cloned(struct bio *bio) 2284 { 2285 struct packet_stacked_data *psd = bio->bi_private; 2286 struct pktcdvd_device *pd = psd->pd; 2287 2288 psd->bio->bi_status = bio->bi_status; 2289 bio_put(bio); 2290 bio_endio(psd->bio); 2291 mempool_free(psd, &psd_pool); 2292 pkt_bio_finished(pd); 2293 } 2294 2295 static void pkt_make_request_read(struct pktcdvd_device *pd, struct bio *bio) 2296 { 2297 struct bio *cloned_bio = 2298 bio_alloc_clone(pd->bdev, bio, GFP_NOIO, &pkt_bio_set); 2299 struct packet_stacked_data *psd = mempool_alloc(&psd_pool, GFP_NOIO); 2300 2301 psd->pd = pd; 2302 psd->bio = bio; 2303 cloned_bio->bi_private = psd; 2304 cloned_bio->bi_end_io = pkt_end_io_read_cloned; 2305 pd->stats.secs_r += bio_sectors(bio); 2306 pkt_queue_bio(pd, cloned_bio); 2307 } 2308 2309 static void pkt_make_request_write(struct request_queue *q, struct bio *bio) 2310 { 2311 struct pktcdvd_device *pd = q->queuedata; 2312 sector_t zone; 2313 struct packet_data *pkt; 2314 int was_empty, blocked_bio; 2315 struct pkt_rb_node *node; 2316 2317 zone = get_zone(bio->bi_iter.bi_sector, pd); 2318 2319 /* 2320 * If we find a matching packet in state WAITING or READ_WAIT, we can 2321 * just append this bio to that packet. 2322 */ 2323 spin_lock(&pd->cdrw.active_list_lock); 2324 blocked_bio = 0; 2325 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) { 2326 if (pkt->sector == zone) { 2327 spin_lock(&pkt->lock); 2328 if ((pkt->state == PACKET_WAITING_STATE) || 2329 (pkt->state == PACKET_READ_WAIT_STATE)) { 2330 bio_list_add(&pkt->orig_bios, bio); 2331 pkt->write_size += 2332 bio->bi_iter.bi_size / CD_FRAMESIZE; 2333 if ((pkt->write_size >= pkt->frames) && 2334 (pkt->state == PACKET_WAITING_STATE)) { 2335 atomic_inc(&pkt->run_sm); 2336 wake_up(&pd->wqueue); 2337 } 2338 spin_unlock(&pkt->lock); 2339 spin_unlock(&pd->cdrw.active_list_lock); 2340 return; 2341 } else { 2342 blocked_bio = 1; 2343 } 2344 spin_unlock(&pkt->lock); 2345 } 2346 } 2347 spin_unlock(&pd->cdrw.active_list_lock); 2348 2349 /* 2350 * Test if there is enough room left in the bio work queue 2351 * (queue size >= congestion on mark). 2352 * If not, wait till the work queue size is below the congestion off mark. 2353 */ 2354 spin_lock(&pd->lock); 2355 if (pd->write_congestion_on > 0 2356 && pd->bio_queue_size >= pd->write_congestion_on) { 2357 struct wait_bit_queue_entry wqe; 2358 2359 init_wait_var_entry(&wqe, &pd->congested, 0); 2360 for (;;) { 2361 prepare_to_wait_event(__var_waitqueue(&pd->congested), 2362 &wqe.wq_entry, 2363 TASK_UNINTERRUPTIBLE); 2364 if (pd->bio_queue_size <= pd->write_congestion_off) 2365 break; 2366 pd->congested = true; 2367 spin_unlock(&pd->lock); 2368 schedule(); 2369 spin_lock(&pd->lock); 2370 } 2371 } 2372 spin_unlock(&pd->lock); 2373 2374 /* 2375 * No matching packet found. Store the bio in the work queue. 2376 */ 2377 node = mempool_alloc(&pd->rb_pool, GFP_NOIO); 2378 node->bio = bio; 2379 spin_lock(&pd->lock); 2380 BUG_ON(pd->bio_queue_size < 0); 2381 was_empty = (pd->bio_queue_size == 0); 2382 pkt_rbtree_insert(pd, node); 2383 spin_unlock(&pd->lock); 2384 2385 /* 2386 * Wake up the worker thread. 2387 */ 2388 atomic_set(&pd->scan_queue, 1); 2389 if (was_empty) { 2390 /* This wake_up is required for correct operation */ 2391 wake_up(&pd->wqueue); 2392 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) { 2393 /* 2394 * This wake up is not required for correct operation, 2395 * but improves performance in some cases. 2396 */ 2397 wake_up(&pd->wqueue); 2398 } 2399 } 2400 2401 static void pkt_submit_bio(struct bio *bio) 2402 { 2403 struct pktcdvd_device *pd = bio->bi_bdev->bd_disk->queue->queuedata; 2404 struct bio *split; 2405 2406 blk_queue_split(&bio); 2407 2408 pkt_dbg(2, pd, "start = %6llx stop = %6llx\n", 2409 (unsigned long long)bio->bi_iter.bi_sector, 2410 (unsigned long long)bio_end_sector(bio)); 2411 2412 /* 2413 * Clone READ bios so we can have our own bi_end_io callback. 2414 */ 2415 if (bio_data_dir(bio) == READ) { 2416 pkt_make_request_read(pd, bio); 2417 return; 2418 } 2419 2420 if (!test_bit(PACKET_WRITABLE, &pd->flags)) { 2421 pkt_notice(pd, "WRITE for ro device (%llu)\n", 2422 (unsigned long long)bio->bi_iter.bi_sector); 2423 goto end_io; 2424 } 2425 2426 if (!bio->bi_iter.bi_size || (bio->bi_iter.bi_size % CD_FRAMESIZE)) { 2427 pkt_err(pd, "wrong bio size\n"); 2428 goto end_io; 2429 } 2430 2431 do { 2432 sector_t zone = get_zone(bio->bi_iter.bi_sector, pd); 2433 sector_t last_zone = get_zone(bio_end_sector(bio) - 1, pd); 2434 2435 if (last_zone != zone) { 2436 BUG_ON(last_zone != zone + pd->settings.size); 2437 2438 split = bio_split(bio, last_zone - 2439 bio->bi_iter.bi_sector, 2440 GFP_NOIO, &pkt_bio_set); 2441 bio_chain(split, bio); 2442 } else { 2443 split = bio; 2444 } 2445 2446 pkt_make_request_write(bio->bi_bdev->bd_disk->queue, split); 2447 } while (split != bio); 2448 2449 return; 2450 end_io: 2451 bio_io_error(bio); 2452 } 2453 2454 static void pkt_init_queue(struct pktcdvd_device *pd) 2455 { 2456 struct request_queue *q = pd->disk->queue; 2457 2458 blk_queue_logical_block_size(q, CD_FRAMESIZE); 2459 blk_queue_max_hw_sectors(q, PACKET_MAX_SECTORS); 2460 q->queuedata = pd; 2461 } 2462 2463 static int pkt_seq_show(struct seq_file *m, void *p) 2464 { 2465 struct pktcdvd_device *pd = m->private; 2466 char *msg; 2467 char bdev_buf[BDEVNAME_SIZE]; 2468 int states[PACKET_NUM_STATES]; 2469 2470 seq_printf(m, "Writer %s mapped to %s:\n", pd->name, 2471 bdevname(pd->bdev, bdev_buf)); 2472 2473 seq_printf(m, "\nSettings:\n"); 2474 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2); 2475 2476 if (pd->settings.write_type == 0) 2477 msg = "Packet"; 2478 else 2479 msg = "Unknown"; 2480 seq_printf(m, "\twrite type:\t\t%s\n", msg); 2481 2482 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable"); 2483 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss); 2484 2485 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode); 2486 2487 if (pd->settings.block_mode == PACKET_BLOCK_MODE1) 2488 msg = "Mode 1"; 2489 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2) 2490 msg = "Mode 2"; 2491 else 2492 msg = "Unknown"; 2493 seq_printf(m, "\tblock mode:\t\t%s\n", msg); 2494 2495 seq_printf(m, "\nStatistics:\n"); 2496 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started); 2497 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended); 2498 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1); 2499 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1); 2500 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1); 2501 2502 seq_printf(m, "\nMisc:\n"); 2503 seq_printf(m, "\treference count:\t%d\n", pd->refcnt); 2504 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags); 2505 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed); 2506 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed); 2507 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset); 2508 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset); 2509 2510 seq_printf(m, "\nQueue state:\n"); 2511 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size); 2512 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios)); 2513 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector); 2514 2515 pkt_count_states(pd, states); 2516 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n", 2517 states[0], states[1], states[2], states[3], states[4], states[5]); 2518 2519 seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n", 2520 pd->write_congestion_off, 2521 pd->write_congestion_on); 2522 return 0; 2523 } 2524 2525 static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev) 2526 { 2527 int i; 2528 char b[BDEVNAME_SIZE]; 2529 struct block_device *bdev; 2530 struct scsi_device *sdev; 2531 2532 if (pd->pkt_dev == dev) { 2533 pkt_err(pd, "recursive setup not allowed\n"); 2534 return -EBUSY; 2535 } 2536 for (i = 0; i < MAX_WRITERS; i++) { 2537 struct pktcdvd_device *pd2 = pkt_devs[i]; 2538 if (!pd2) 2539 continue; 2540 if (pd2->bdev->bd_dev == dev) { 2541 pkt_err(pd, "%s already setup\n", 2542 bdevname(pd2->bdev, b)); 2543 return -EBUSY; 2544 } 2545 if (pd2->pkt_dev == dev) { 2546 pkt_err(pd, "can't chain pktcdvd devices\n"); 2547 return -EBUSY; 2548 } 2549 } 2550 2551 bdev = blkdev_get_by_dev(dev, FMODE_READ | FMODE_NDELAY, NULL); 2552 if (IS_ERR(bdev)) 2553 return PTR_ERR(bdev); 2554 sdev = scsi_device_from_queue(bdev->bd_disk->queue); 2555 if (!sdev) { 2556 blkdev_put(bdev, FMODE_READ | FMODE_NDELAY); 2557 return -EINVAL; 2558 } 2559 put_device(&sdev->sdev_gendev); 2560 2561 /* This is safe, since we have a reference from open(). */ 2562 __module_get(THIS_MODULE); 2563 2564 pd->bdev = bdev; 2565 set_blocksize(bdev, CD_FRAMESIZE); 2566 2567 pkt_init_queue(pd); 2568 2569 atomic_set(&pd->cdrw.pending_bios, 0); 2570 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name); 2571 if (IS_ERR(pd->cdrw.thread)) { 2572 pkt_err(pd, "can't start kernel thread\n"); 2573 goto out_mem; 2574 } 2575 2576 proc_create_single_data(pd->name, 0, pkt_proc, pkt_seq_show, pd); 2577 pkt_dbg(1, pd, "writer mapped to %s\n", bdevname(bdev, b)); 2578 return 0; 2579 2580 out_mem: 2581 blkdev_put(bdev, FMODE_READ | FMODE_NDELAY); 2582 /* This is safe: open() is still holding a reference. */ 2583 module_put(THIS_MODULE); 2584 return -ENOMEM; 2585 } 2586 2587 static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg) 2588 { 2589 struct pktcdvd_device *pd = bdev->bd_disk->private_data; 2590 int ret; 2591 2592 pkt_dbg(2, pd, "cmd %x, dev %d:%d\n", 2593 cmd, MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev)); 2594 2595 mutex_lock(&pktcdvd_mutex); 2596 switch (cmd) { 2597 case CDROMEJECT: 2598 /* 2599 * The door gets locked when the device is opened, so we 2600 * have to unlock it or else the eject command fails. 2601 */ 2602 if (pd->refcnt == 1) 2603 pkt_lock_door(pd, 0); 2604 fallthrough; 2605 /* 2606 * forward selected CDROM ioctls to CD-ROM, for UDF 2607 */ 2608 case CDROMMULTISESSION: 2609 case CDROMREADTOCENTRY: 2610 case CDROM_LAST_WRITTEN: 2611 case CDROM_SEND_PACKET: 2612 case SCSI_IOCTL_SEND_COMMAND: 2613 if (!bdev->bd_disk->fops->ioctl) 2614 ret = -ENOTTY; 2615 else 2616 ret = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); 2617 break; 2618 default: 2619 pkt_dbg(2, pd, "Unknown ioctl (%x)\n", cmd); 2620 ret = -ENOTTY; 2621 } 2622 mutex_unlock(&pktcdvd_mutex); 2623 2624 return ret; 2625 } 2626 2627 static unsigned int pkt_check_events(struct gendisk *disk, 2628 unsigned int clearing) 2629 { 2630 struct pktcdvd_device *pd = disk->private_data; 2631 struct gendisk *attached_disk; 2632 2633 if (!pd) 2634 return 0; 2635 if (!pd->bdev) 2636 return 0; 2637 attached_disk = pd->bdev->bd_disk; 2638 if (!attached_disk || !attached_disk->fops->check_events) 2639 return 0; 2640 return attached_disk->fops->check_events(attached_disk, clearing); 2641 } 2642 2643 static char *pkt_devnode(struct gendisk *disk, umode_t *mode) 2644 { 2645 return kasprintf(GFP_KERNEL, "pktcdvd/%s", disk->disk_name); 2646 } 2647 2648 static const struct block_device_operations pktcdvd_ops = { 2649 .owner = THIS_MODULE, 2650 .submit_bio = pkt_submit_bio, 2651 .open = pkt_open, 2652 .release = pkt_close, 2653 .ioctl = pkt_ioctl, 2654 .compat_ioctl = blkdev_compat_ptr_ioctl, 2655 .check_events = pkt_check_events, 2656 .devnode = pkt_devnode, 2657 }; 2658 2659 /* 2660 * Set up mapping from pktcdvd device to CD-ROM device. 2661 */ 2662 static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev) 2663 { 2664 int idx; 2665 int ret = -ENOMEM; 2666 struct pktcdvd_device *pd; 2667 struct gendisk *disk; 2668 2669 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); 2670 2671 for (idx = 0; idx < MAX_WRITERS; idx++) 2672 if (!pkt_devs[idx]) 2673 break; 2674 if (idx == MAX_WRITERS) { 2675 pr_err("max %d writers supported\n", MAX_WRITERS); 2676 ret = -EBUSY; 2677 goto out_mutex; 2678 } 2679 2680 pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL); 2681 if (!pd) 2682 goto out_mutex; 2683 2684 ret = mempool_init_kmalloc_pool(&pd->rb_pool, PKT_RB_POOL_SIZE, 2685 sizeof(struct pkt_rb_node)); 2686 if (ret) 2687 goto out_mem; 2688 2689 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list); 2690 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list); 2691 spin_lock_init(&pd->cdrw.active_list_lock); 2692 2693 spin_lock_init(&pd->lock); 2694 spin_lock_init(&pd->iosched.lock); 2695 bio_list_init(&pd->iosched.read_queue); 2696 bio_list_init(&pd->iosched.write_queue); 2697 sprintf(pd->name, DRIVER_NAME"%d", idx); 2698 init_waitqueue_head(&pd->wqueue); 2699 pd->bio_queue = RB_ROOT; 2700 2701 pd->write_congestion_on = write_congestion_on; 2702 pd->write_congestion_off = write_congestion_off; 2703 2704 ret = -ENOMEM; 2705 disk = blk_alloc_disk(NUMA_NO_NODE); 2706 if (!disk) 2707 goto out_mem; 2708 pd->disk = disk; 2709 disk->major = pktdev_major; 2710 disk->first_minor = idx; 2711 disk->minors = 1; 2712 disk->fops = &pktcdvd_ops; 2713 disk->flags = GENHD_FL_REMOVABLE | GENHD_FL_NO_PART; 2714 strcpy(disk->disk_name, pd->name); 2715 disk->private_data = pd; 2716 2717 pd->pkt_dev = MKDEV(pktdev_major, idx); 2718 ret = pkt_new_dev(pd, dev); 2719 if (ret) 2720 goto out_mem2; 2721 2722 /* inherit events of the host device */ 2723 disk->events = pd->bdev->bd_disk->events; 2724 2725 ret = add_disk(disk); 2726 if (ret) 2727 goto out_mem2; 2728 2729 pkt_sysfs_dev_new(pd); 2730 pkt_debugfs_dev_new(pd); 2731 2732 pkt_devs[idx] = pd; 2733 if (pkt_dev) 2734 *pkt_dev = pd->pkt_dev; 2735 2736 mutex_unlock(&ctl_mutex); 2737 return 0; 2738 2739 out_mem2: 2740 blk_cleanup_disk(disk); 2741 out_mem: 2742 mempool_exit(&pd->rb_pool); 2743 kfree(pd); 2744 out_mutex: 2745 mutex_unlock(&ctl_mutex); 2746 pr_err("setup of pktcdvd device failed\n"); 2747 return ret; 2748 } 2749 2750 /* 2751 * Tear down mapping from pktcdvd device to CD-ROM device. 2752 */ 2753 static int pkt_remove_dev(dev_t pkt_dev) 2754 { 2755 struct pktcdvd_device *pd; 2756 int idx; 2757 int ret = 0; 2758 2759 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); 2760 2761 for (idx = 0; idx < MAX_WRITERS; idx++) { 2762 pd = pkt_devs[idx]; 2763 if (pd && (pd->pkt_dev == pkt_dev)) 2764 break; 2765 } 2766 if (idx == MAX_WRITERS) { 2767 pr_debug("dev not setup\n"); 2768 ret = -ENXIO; 2769 goto out; 2770 } 2771 2772 if (pd->refcnt > 0) { 2773 ret = -EBUSY; 2774 goto out; 2775 } 2776 if (!IS_ERR(pd->cdrw.thread)) 2777 kthread_stop(pd->cdrw.thread); 2778 2779 pkt_devs[idx] = NULL; 2780 2781 pkt_debugfs_dev_remove(pd); 2782 pkt_sysfs_dev_remove(pd); 2783 2784 blkdev_put(pd->bdev, FMODE_READ | FMODE_NDELAY); 2785 2786 remove_proc_entry(pd->name, pkt_proc); 2787 pkt_dbg(1, pd, "writer unmapped\n"); 2788 2789 del_gendisk(pd->disk); 2790 blk_cleanup_disk(pd->disk); 2791 2792 mempool_exit(&pd->rb_pool); 2793 kfree(pd); 2794 2795 /* This is safe: open() is still holding a reference. */ 2796 module_put(THIS_MODULE); 2797 2798 out: 2799 mutex_unlock(&ctl_mutex); 2800 return ret; 2801 } 2802 2803 static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd) 2804 { 2805 struct pktcdvd_device *pd; 2806 2807 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); 2808 2809 pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index); 2810 if (pd) { 2811 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev); 2812 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev); 2813 } else { 2814 ctrl_cmd->dev = 0; 2815 ctrl_cmd->pkt_dev = 0; 2816 } 2817 ctrl_cmd->num_devices = MAX_WRITERS; 2818 2819 mutex_unlock(&ctl_mutex); 2820 } 2821 2822 static long pkt_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2823 { 2824 void __user *argp = (void __user *)arg; 2825 struct pkt_ctrl_command ctrl_cmd; 2826 int ret = 0; 2827 dev_t pkt_dev = 0; 2828 2829 if (cmd != PACKET_CTRL_CMD) 2830 return -ENOTTY; 2831 2832 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command))) 2833 return -EFAULT; 2834 2835 switch (ctrl_cmd.command) { 2836 case PKT_CTRL_CMD_SETUP: 2837 if (!capable(CAP_SYS_ADMIN)) 2838 return -EPERM; 2839 ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev); 2840 ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev); 2841 break; 2842 case PKT_CTRL_CMD_TEARDOWN: 2843 if (!capable(CAP_SYS_ADMIN)) 2844 return -EPERM; 2845 ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev)); 2846 break; 2847 case PKT_CTRL_CMD_STATUS: 2848 pkt_get_status(&ctrl_cmd); 2849 break; 2850 default: 2851 return -ENOTTY; 2852 } 2853 2854 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command))) 2855 return -EFAULT; 2856 return ret; 2857 } 2858 2859 #ifdef CONFIG_COMPAT 2860 static long pkt_ctl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2861 { 2862 return pkt_ctl_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2863 } 2864 #endif 2865 2866 static const struct file_operations pkt_ctl_fops = { 2867 .open = nonseekable_open, 2868 .unlocked_ioctl = pkt_ctl_ioctl, 2869 #ifdef CONFIG_COMPAT 2870 .compat_ioctl = pkt_ctl_compat_ioctl, 2871 #endif 2872 .owner = THIS_MODULE, 2873 .llseek = no_llseek, 2874 }; 2875 2876 static struct miscdevice pkt_misc = { 2877 .minor = MISC_DYNAMIC_MINOR, 2878 .name = DRIVER_NAME, 2879 .nodename = "pktcdvd/control", 2880 .fops = &pkt_ctl_fops 2881 }; 2882 2883 static int __init pkt_init(void) 2884 { 2885 int ret; 2886 2887 mutex_init(&ctl_mutex); 2888 2889 ret = mempool_init_kmalloc_pool(&psd_pool, PSD_POOL_SIZE, 2890 sizeof(struct packet_stacked_data)); 2891 if (ret) 2892 return ret; 2893 ret = bioset_init(&pkt_bio_set, BIO_POOL_SIZE, 0, 0); 2894 if (ret) { 2895 mempool_exit(&psd_pool); 2896 return ret; 2897 } 2898 2899 ret = register_blkdev(pktdev_major, DRIVER_NAME); 2900 if (ret < 0) { 2901 pr_err("unable to register block device\n"); 2902 goto out2; 2903 } 2904 if (!pktdev_major) 2905 pktdev_major = ret; 2906 2907 ret = pkt_sysfs_init(); 2908 if (ret) 2909 goto out; 2910 2911 pkt_debugfs_init(); 2912 2913 ret = misc_register(&pkt_misc); 2914 if (ret) { 2915 pr_err("unable to register misc device\n"); 2916 goto out_misc; 2917 } 2918 2919 pkt_proc = proc_mkdir("driver/"DRIVER_NAME, NULL); 2920 2921 return 0; 2922 2923 out_misc: 2924 pkt_debugfs_cleanup(); 2925 pkt_sysfs_cleanup(); 2926 out: 2927 unregister_blkdev(pktdev_major, DRIVER_NAME); 2928 out2: 2929 mempool_exit(&psd_pool); 2930 bioset_exit(&pkt_bio_set); 2931 return ret; 2932 } 2933 2934 static void __exit pkt_exit(void) 2935 { 2936 remove_proc_entry("driver/"DRIVER_NAME, NULL); 2937 misc_deregister(&pkt_misc); 2938 2939 pkt_debugfs_cleanup(); 2940 pkt_sysfs_cleanup(); 2941 2942 unregister_blkdev(pktdev_major, DRIVER_NAME); 2943 mempool_exit(&psd_pool); 2944 bioset_exit(&pkt_bio_set); 2945 } 2946 2947 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives"); 2948 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>"); 2949 MODULE_LICENSE("GPL"); 2950 2951 module_init(pkt_init); 2952 module_exit(pkt_exit); 2953