1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * hosts.c Copyright (C) 1992 Drew Eckhardt 4 * Copyright (C) 1993, 1994, 1995 Eric Youngdale 5 * Copyright (C) 2002-2003 Christoph Hellwig 6 * 7 * mid to lowlevel SCSI driver interface 8 * Initial versions: Drew Eckhardt 9 * Subsequent revisions: Eric Youngdale 10 * 11 * <drew@colorado.edu> 12 * 13 * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli 14 * Added QLOGIC QLA1280 SCSI controller kernel host support. 15 * August 4, 1999 Fred Lewis, Intel DuPont 16 * 17 * Updated to reflect the new initialization scheme for the higher 18 * level of scsi drivers (sd/sr/st) 19 * September 17, 2000 Torben Mathiasen <tmm@image.dk> 20 * 21 * Restructured scsi_host lists and associated functions. 22 * September 04, 2002 Mike Anderson (andmike@us.ibm.com) 23 */ 24 25 #include <linux/module.h> 26 #include <linux/blkdev.h> 27 #include <linux/kernel.h> 28 #include <linux/slab.h> 29 #include <linux/kthread.h> 30 #include <linux/string.h> 31 #include <linux/mm.h> 32 #include <linux/init.h> 33 #include <linux/completion.h> 34 #include <linux/transport_class.h> 35 #include <linux/platform_device.h> 36 #include <linux/pm_runtime.h> 37 #include <linux/idr.h> 38 #include <scsi/scsi_device.h> 39 #include <scsi/scsi_host.h> 40 #include <scsi/scsi_transport.h> 41 #include <scsi/scsi_cmnd.h> 42 43 #include "scsi_priv.h" 44 #include "scsi_logging.h" 45 46 47 static int shost_eh_deadline = -1; 48 49 module_param_named(eh_deadline, shost_eh_deadline, int, S_IRUGO|S_IWUSR); 50 MODULE_PARM_DESC(eh_deadline, 51 "SCSI EH timeout in seconds (should be between 0 and 2^31-1)"); 52 53 static DEFINE_IDA(host_index_ida); 54 55 56 static void scsi_host_cls_release(struct device *dev) 57 { 58 put_device(&class_to_shost(dev)->shost_gendev); 59 } 60 61 static struct class shost_class = { 62 .name = "scsi_host", 63 .dev_release = scsi_host_cls_release, 64 .dev_groups = scsi_shost_groups, 65 }; 66 67 /** 68 * scsi_host_set_state - Take the given host through the host state model. 69 * @shost: scsi host to change the state of. 70 * @state: state to change to. 71 * 72 * Returns zero if unsuccessful or an error if the requested 73 * transition is illegal. 74 **/ 75 int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state) 76 { 77 enum scsi_host_state oldstate = shost->shost_state; 78 79 if (state == oldstate) 80 return 0; 81 82 switch (state) { 83 case SHOST_CREATED: 84 /* There are no legal states that come back to 85 * created. This is the manually initialised start 86 * state */ 87 goto illegal; 88 89 case SHOST_RUNNING: 90 switch (oldstate) { 91 case SHOST_CREATED: 92 case SHOST_RECOVERY: 93 break; 94 default: 95 goto illegal; 96 } 97 break; 98 99 case SHOST_RECOVERY: 100 switch (oldstate) { 101 case SHOST_RUNNING: 102 break; 103 default: 104 goto illegal; 105 } 106 break; 107 108 case SHOST_CANCEL: 109 switch (oldstate) { 110 case SHOST_CREATED: 111 case SHOST_RUNNING: 112 case SHOST_CANCEL_RECOVERY: 113 break; 114 default: 115 goto illegal; 116 } 117 break; 118 119 case SHOST_DEL: 120 switch (oldstate) { 121 case SHOST_CANCEL: 122 case SHOST_DEL_RECOVERY: 123 break; 124 default: 125 goto illegal; 126 } 127 break; 128 129 case SHOST_CANCEL_RECOVERY: 130 switch (oldstate) { 131 case SHOST_CANCEL: 132 case SHOST_RECOVERY: 133 break; 134 default: 135 goto illegal; 136 } 137 break; 138 139 case SHOST_DEL_RECOVERY: 140 switch (oldstate) { 141 case SHOST_CANCEL_RECOVERY: 142 break; 143 default: 144 goto illegal; 145 } 146 break; 147 } 148 shost->shost_state = state; 149 return 0; 150 151 illegal: 152 SCSI_LOG_ERROR_RECOVERY(1, 153 shost_printk(KERN_ERR, shost, 154 "Illegal host state transition" 155 "%s->%s\n", 156 scsi_host_state_name(oldstate), 157 scsi_host_state_name(state))); 158 return -EINVAL; 159 } 160 161 /** 162 * scsi_remove_host - remove a scsi host 163 * @shost: a pointer to a scsi host to remove 164 **/ 165 void scsi_remove_host(struct Scsi_Host *shost) 166 { 167 unsigned long flags; 168 169 mutex_lock(&shost->scan_mutex); 170 spin_lock_irqsave(shost->host_lock, flags); 171 if (scsi_host_set_state(shost, SHOST_CANCEL)) 172 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)) { 173 spin_unlock_irqrestore(shost->host_lock, flags); 174 mutex_unlock(&shost->scan_mutex); 175 return; 176 } 177 spin_unlock_irqrestore(shost->host_lock, flags); 178 179 scsi_autopm_get_host(shost); 180 flush_workqueue(shost->tmf_work_q); 181 scsi_forget_host(shost); 182 mutex_unlock(&shost->scan_mutex); 183 scsi_proc_host_rm(shost); 184 scsi_proc_hostdir_rm(shost->hostt); 185 186 /* 187 * New SCSI devices cannot be attached anymore because of the SCSI host 188 * state so drop the tag set refcnt. Wait until the tag set refcnt drops 189 * to zero because .exit_cmd_priv implementations may need the host 190 * pointer. 191 */ 192 kref_put(&shost->tagset_refcnt, scsi_mq_free_tags); 193 wait_for_completion(&shost->tagset_freed); 194 195 spin_lock_irqsave(shost->host_lock, flags); 196 if (scsi_host_set_state(shost, SHOST_DEL)) 197 BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY)); 198 spin_unlock_irqrestore(shost->host_lock, flags); 199 200 transport_unregister_device(&shost->shost_gendev); 201 device_unregister(&shost->shost_dev); 202 device_del(&shost->shost_gendev); 203 } 204 EXPORT_SYMBOL(scsi_remove_host); 205 206 /** 207 * scsi_add_host_with_dma - add a scsi host with dma device 208 * @shost: scsi host pointer to add 209 * @dev: a struct device of type scsi class 210 * @dma_dev: dma device for the host 211 * 212 * Note: You rarely need to worry about this unless you're in a 213 * virtualised host environments, so use the simpler scsi_add_host() 214 * function instead. 215 * 216 * Return value: 217 * 0 on success / != 0 for error 218 **/ 219 int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, 220 struct device *dma_dev) 221 { 222 const struct scsi_host_template *sht = shost->hostt; 223 int error = -EINVAL; 224 225 shost_printk(KERN_INFO, shost, "%s\n", 226 sht->info ? sht->info(shost) : sht->name); 227 228 if (!shost->can_queue) { 229 shost_printk(KERN_ERR, shost, 230 "can_queue = 0 no longer supported\n"); 231 goto fail; 232 } 233 234 if (shost->nr_reserved_cmds && !sht->queue_reserved_command) { 235 shost_printk(KERN_ERR, shost, 236 "nr_reserved_cmds set but no method to queue\n"); 237 goto fail; 238 } 239 240 /* Use min_t(int, ...) in case shost->can_queue exceeds SHRT_MAX */ 241 shost->cmd_per_lun = min_t(int, shost->cmd_per_lun, 242 shost->can_queue); 243 244 error = scsi_init_sense_cache(shost); 245 if (error) 246 goto fail; 247 248 if (!shost->shost_gendev.parent) 249 shost->shost_gendev.parent = dev ? dev : &platform_bus; 250 if (!dma_dev) 251 dma_dev = shost->shost_gendev.parent; 252 253 shost->dma_dev = dma_dev; 254 255 if (dma_dev->dma_mask) { 256 shost->max_sectors = min_t(unsigned int, shost->max_sectors, 257 dma_max_mapping_size(dma_dev) >> SECTOR_SHIFT); 258 } 259 260 error = scsi_mq_setup_tags(shost); 261 if (error) 262 goto fail; 263 264 kref_init(&shost->tagset_refcnt); 265 init_completion(&shost->tagset_freed); 266 267 /* 268 * Increase usage count temporarily here so that calling 269 * scsi_autopm_put_host() will trigger runtime idle if there is 270 * nothing else preventing suspending the device. 271 */ 272 pm_runtime_get_noresume(&shost->shost_gendev); 273 pm_runtime_set_active(&shost->shost_gendev); 274 pm_runtime_enable(&shost->shost_gendev); 275 device_enable_async_suspend(&shost->shost_gendev); 276 277 error = device_add(&shost->shost_gendev); 278 if (error) 279 goto out_disable_runtime_pm; 280 281 scsi_host_set_state(shost, SHOST_RUNNING); 282 get_device(shost->shost_gendev.parent); 283 284 device_enable_async_suspend(&shost->shost_dev); 285 286 get_device(&shost->shost_gendev); 287 error = device_add(&shost->shost_dev); 288 if (error) 289 goto out_del_gendev; 290 291 if (shost->transportt->host_size) { 292 shost->shost_data = kzalloc(shost->transportt->host_size, 293 GFP_KERNEL); 294 if (shost->shost_data == NULL) { 295 error = -ENOMEM; 296 goto out_del_dev; 297 } 298 } 299 300 if (shost->transportt->create_work_queue) { 301 shost->work_q = alloc_workqueue( 302 "scsi_wq_%d", 303 WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND, 1, 304 shost->host_no); 305 306 if (!shost->work_q) { 307 error = -EINVAL; 308 goto out_del_dev; 309 } 310 } 311 312 error = scsi_sysfs_add_host(shost); 313 if (error) 314 goto out_del_dev; 315 316 if (shost->nr_reserved_cmds) { 317 shost->pseudo_sdev = scsi_get_pseudo_sdev(shost); 318 if (!shost->pseudo_sdev) { 319 error = -ENOMEM; 320 goto out_del_dev; 321 } 322 } 323 324 scsi_proc_host_add(shost); 325 scsi_autopm_put_host(shost); 326 return error; 327 328 /* 329 * Any host allocation in this function will be freed in 330 * scsi_host_dev_release(). 331 */ 332 out_del_dev: 333 device_del(&shost->shost_dev); 334 out_del_gendev: 335 /* 336 * Host state is SHOST_RUNNING so we have to explicitly release 337 * ->shost_dev. 338 */ 339 put_device(&shost->shost_dev); 340 device_del(&shost->shost_gendev); 341 out_disable_runtime_pm: 342 device_disable_async_suspend(&shost->shost_gendev); 343 pm_runtime_disable(&shost->shost_gendev); 344 pm_runtime_set_suspended(&shost->shost_gendev); 345 pm_runtime_put_noidle(&shost->shost_gendev); 346 kref_put(&shost->tagset_refcnt, scsi_mq_free_tags); 347 fail: 348 return error; 349 } 350 EXPORT_SYMBOL(scsi_add_host_with_dma); 351 352 static void scsi_host_dev_release(struct device *dev) 353 { 354 struct Scsi_Host *shost = dev_to_shost(dev); 355 struct device *parent = dev->parent; 356 357 /* Wait for functions invoked through call_rcu(&scmd->rcu, ...) */ 358 rcu_barrier(); 359 360 cancel_work_sync(&shost->eh_work); 361 if (shost->tmf_work_q) 362 destroy_workqueue(shost->tmf_work_q); 363 if (shost->ehandler) 364 kthread_stop(shost->ehandler); 365 if (shost->work_q) 366 destroy_workqueue(shost->work_q); 367 368 if (shost->shost_state == SHOST_CREATED) { 369 /* 370 * Free the shost_dev device name and remove the proc host dir 371 * here if scsi_host_{alloc,put}() have been called but neither 372 * scsi_host_add() nor scsi_remove_host() has been called. 373 * This avoids that the memory allocated for the shost_dev 374 * name as well as the proc dir structure are leaked. 375 */ 376 scsi_proc_hostdir_rm(shost->hostt); 377 kfree(dev_name(&shost->shost_dev)); 378 } 379 380 kfree(shost->shost_data); 381 382 ida_free(&host_index_ida, shost->host_no); 383 384 if (shost->shost_state != SHOST_CREATED) 385 put_device(parent); 386 kfree(shost); 387 } 388 389 static const struct device_type scsi_host_type = { 390 .name = "scsi_host", 391 .release = scsi_host_dev_release, 392 }; 393 394 /** 395 * scsi_host_alloc - register a scsi host adapter instance. 396 * @sht: pointer to scsi host template 397 * @privsize: extra bytes to allocate for driver 398 * 399 * Note: 400 * Allocate a new Scsi_Host and perform basic initialization. 401 * The host is not published to the scsi midlayer until scsi_add_host 402 * is called. 403 * 404 * Return value: 405 * Pointer to a new Scsi_Host 406 **/ 407 struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int privsize) 408 { 409 struct Scsi_Host *shost; 410 int index; 411 412 shost = kzalloc(sizeof(struct Scsi_Host) + privsize, GFP_KERNEL); 413 if (!shost) 414 return NULL; 415 416 shost->host_lock = &shost->default_lock; 417 spin_lock_init(shost->host_lock); 418 shost->shost_state = SHOST_CREATED; 419 INIT_LIST_HEAD(&shost->__devices); 420 INIT_LIST_HEAD(&shost->__targets); 421 INIT_LIST_HEAD(&shost->eh_abort_list); 422 INIT_LIST_HEAD(&shost->eh_cmd_q); 423 INIT_LIST_HEAD(&shost->starved_list); 424 init_waitqueue_head(&shost->host_wait); 425 mutex_init(&shost->scan_mutex); 426 INIT_WORK(&shost->eh_work, scsi_rcu_eh_wakeup); 427 428 index = ida_alloc(&host_index_ida, GFP_KERNEL); 429 if (index < 0) { 430 kfree(shost); 431 return NULL; 432 } 433 shost->host_no = index; 434 435 shost->dma_channel = 0xff; 436 437 /* These three are default values which can be overridden */ 438 shost->max_channel = 0; 439 shost->max_id = 8; 440 shost->max_lun = 8; 441 442 /* Give each shost a default transportt */ 443 shost->transportt = &blank_transport_template; 444 445 /* 446 * All drivers right now should be able to handle 12 byte 447 * commands. Every so often there are requests for 16 byte 448 * commands, but individual low-level drivers need to certify that 449 * they actually do something sensible with such commands. 450 */ 451 shost->max_cmd_len = 12; 452 shost->hostt = sht; 453 shost->this_id = sht->this_id; 454 shost->can_queue = sht->can_queue; 455 shost->nr_reserved_cmds = sht->nr_reserved_cmds; 456 shost->sg_tablesize = sht->sg_tablesize; 457 shost->sg_prot_tablesize = sht->sg_prot_tablesize; 458 shost->cmd_per_lun = sht->cmd_per_lun; 459 shost->no_write_same = sht->no_write_same; 460 shost->host_tagset = sht->host_tagset; 461 shost->queuecommand_may_block = sht->queuecommand_may_block; 462 463 if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler) 464 shost->eh_deadline = -1; 465 else if ((ulong) shost_eh_deadline * HZ > INT_MAX) { 466 shost_printk(KERN_WARNING, shost, 467 "eh_deadline %u too large, setting to %u\n", 468 shost_eh_deadline, INT_MAX / HZ); 469 shost->eh_deadline = INT_MAX; 470 } else 471 shost->eh_deadline = shost_eh_deadline * HZ; 472 473 if (sht->supported_mode == MODE_UNKNOWN) 474 /* means we didn't set it ... default to INITIATOR */ 475 shost->active_mode = MODE_INITIATOR; 476 else 477 shost->active_mode = sht->supported_mode; 478 479 if (sht->max_host_blocked) 480 shost->max_host_blocked = sht->max_host_blocked; 481 else 482 shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED; 483 484 /* 485 * If the driver imposes no hard sector transfer limit, start at 486 * machine infinity initially. 487 */ 488 if (sht->max_sectors) 489 shost->max_sectors = sht->max_sectors; 490 else 491 shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS; 492 493 shost->virt_boundary_mask = sht->virt_boundary_mask; 494 if (shost->virt_boundary_mask) { 495 WARN_ON_ONCE(sht->max_segment_size && 496 sht->max_segment_size != UINT_MAX); 497 shost->max_segment_size = UINT_MAX; 498 } else { 499 if (sht->max_segment_size) 500 shost->max_segment_size = sht->max_segment_size; 501 else 502 shost->max_segment_size = BLK_MAX_SEGMENT_SIZE; 503 } 504 505 /* 32-byte (dword) is a common minimum for HBAs. */ 506 if (sht->dma_alignment) 507 shost->dma_alignment = sht->dma_alignment; 508 else 509 shost->dma_alignment = 3; 510 511 /* 512 * assume a 4GB boundary, if not set 513 */ 514 if (sht->dma_boundary) 515 shost->dma_boundary = sht->dma_boundary; 516 else 517 shost->dma_boundary = 0xffffffff; 518 519 device_initialize(&shost->shost_gendev); 520 dev_set_name(&shost->shost_gendev, "host%d", shost->host_no); 521 shost->shost_gendev.bus = &scsi_bus_type; 522 shost->shost_gendev.type = &scsi_host_type; 523 scsi_enable_async_suspend(&shost->shost_gendev); 524 525 device_initialize(&shost->shost_dev); 526 shost->shost_dev.parent = &shost->shost_gendev; 527 shost->shost_dev.class = &shost_class; 528 dev_set_name(&shost->shost_dev, "host%d", shost->host_no); 529 shost->shost_dev.groups = sht->shost_groups; 530 531 shost->ehandler = kthread_run(scsi_error_handler, shost, 532 "scsi_eh_%d", shost->host_no); 533 if (IS_ERR(shost->ehandler)) { 534 shost_printk(KERN_WARNING, shost, 535 "error handler thread failed to spawn, error = %ld\n", 536 PTR_ERR(shost->ehandler)); 537 shost->ehandler = NULL; 538 goto fail; 539 } 540 541 shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d", 542 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 543 1, shost->host_no); 544 if (!shost->tmf_work_q) { 545 shost_printk(KERN_WARNING, shost, 546 "failed to create tmf workq\n"); 547 goto fail; 548 } 549 if (scsi_proc_hostdir_add(shost->hostt) < 0) 550 goto fail; 551 return shost; 552 fail: 553 /* 554 * Host state is still SHOST_CREATED and that is enough to release 555 * ->shost_gendev. scsi_host_dev_release() will free 556 * dev_name(&shost->shost_dev). 557 */ 558 put_device(&shost->shost_gendev); 559 560 return NULL; 561 } 562 EXPORT_SYMBOL(scsi_host_alloc); 563 564 static int __scsi_host_match(struct device *dev, const void *data) 565 { 566 struct Scsi_Host *p; 567 const unsigned int *hostnum = data; 568 569 p = class_to_shost(dev); 570 return p->host_no == *hostnum; 571 } 572 573 /** 574 * scsi_host_lookup - get a reference to a Scsi_Host by host no 575 * @hostnum: host number to locate 576 * 577 * Return value: 578 * A pointer to located Scsi_Host or NULL. 579 * 580 * The caller must do a scsi_host_put() to drop the reference 581 * that scsi_host_get() took. The put_device() below dropped 582 * the reference from class_find_device(). 583 **/ 584 struct Scsi_Host *scsi_host_lookup(unsigned int hostnum) 585 { 586 struct device *cdev; 587 struct Scsi_Host *shost = NULL; 588 589 cdev = class_find_device(&shost_class, NULL, &hostnum, 590 __scsi_host_match); 591 if (cdev) { 592 shost = scsi_host_get(class_to_shost(cdev)); 593 put_device(cdev); 594 } 595 return shost; 596 } 597 EXPORT_SYMBOL(scsi_host_lookup); 598 599 /** 600 * scsi_host_get - inc a Scsi_Host ref count 601 * @shost: Pointer to Scsi_Host to inc. 602 **/ 603 struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost) 604 { 605 if ((shost->shost_state == SHOST_DEL) || 606 !get_device(&shost->shost_gendev)) 607 return NULL; 608 return shost; 609 } 610 EXPORT_SYMBOL(scsi_host_get); 611 612 static bool scsi_host_check_in_flight(struct request *rq, void *data) 613 { 614 int *count = data; 615 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); 616 617 if (test_bit(SCMD_STATE_INFLIGHT, &cmd->state)) 618 (*count)++; 619 620 return true; 621 } 622 623 /** 624 * scsi_host_busy - Return the count of in-flight commands 625 * @shost: Pointer to Scsi_Host 626 **/ 627 int scsi_host_busy(struct Scsi_Host *shost) 628 { 629 int cnt = 0; 630 631 blk_mq_tagset_busy_iter(&shost->tag_set, 632 scsi_host_check_in_flight, &cnt); 633 return cnt; 634 } 635 EXPORT_SYMBOL(scsi_host_busy); 636 637 /** 638 * scsi_host_put - dec a Scsi_Host ref count 639 * @shost: Pointer to Scsi_Host to dec. 640 **/ 641 void scsi_host_put(struct Scsi_Host *shost) 642 { 643 put_device(&shost->shost_gendev); 644 } 645 EXPORT_SYMBOL(scsi_host_put); 646 647 int scsi_init_hosts(void) 648 { 649 return class_register(&shost_class); 650 } 651 652 void scsi_exit_hosts(void) 653 { 654 class_unregister(&shost_class); 655 ida_destroy(&host_index_ida); 656 } 657 658 int scsi_is_host_device(const struct device *dev) 659 { 660 return dev->type == &scsi_host_type; 661 } 662 EXPORT_SYMBOL(scsi_is_host_device); 663 664 /** 665 * scsi_queue_work - Queue work to the Scsi_Host workqueue. 666 * @shost: Pointer to Scsi_Host. 667 * @work: Work to queue for execution. 668 * 669 * Return value: 670 * 1 - work queued for execution 671 * 0 - work is already queued 672 * -EINVAL - work queue doesn't exist 673 **/ 674 int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work) 675 { 676 if (unlikely(!shost->work_q)) { 677 shost_printk(KERN_ERR, shost, 678 "ERROR: Scsi host '%s' attempted to queue scsi-work, " 679 "when no workqueue created.\n", shost->hostt->name); 680 dump_stack(); 681 682 return -EINVAL; 683 } 684 685 return queue_work(shost->work_q, work); 686 } 687 EXPORT_SYMBOL_GPL(scsi_queue_work); 688 689 /** 690 * scsi_flush_work - Flush a Scsi_Host's workqueue. 691 * @shost: Pointer to Scsi_Host. 692 **/ 693 void scsi_flush_work(struct Scsi_Host *shost) 694 { 695 if (!shost->work_q) { 696 shost_printk(KERN_ERR, shost, 697 "ERROR: Scsi host '%s' attempted to flush scsi-work, " 698 "when no workqueue created.\n", shost->hostt->name); 699 dump_stack(); 700 return; 701 } 702 703 flush_workqueue(shost->work_q); 704 } 705 EXPORT_SYMBOL_GPL(scsi_flush_work); 706 707 static bool complete_all_cmds_iter(struct request *rq, void *data) 708 { 709 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq); 710 enum scsi_host_status status = *(enum scsi_host_status *)data; 711 712 scsi_dma_unmap(scmd); 713 scmd->result = 0; 714 set_host_byte(scmd, status); 715 scsi_done(scmd); 716 return true; 717 } 718 719 /** 720 * scsi_host_complete_all_commands - Terminate all running commands 721 * @shost: Scsi Host on which commands should be terminated 722 * @status: Status to be set for the terminated commands 723 * 724 * There is no protection against modification of the number 725 * of outstanding commands. It is the responsibility of the 726 * caller to ensure that concurrent I/O submission and/or 727 * completion is stopped when calling this function. 728 */ 729 void scsi_host_complete_all_commands(struct Scsi_Host *shost, 730 enum scsi_host_status status) 731 { 732 blk_mq_tagset_busy_iter(&shost->tag_set, complete_all_cmds_iter, 733 &status); 734 } 735 EXPORT_SYMBOL_GPL(scsi_host_complete_all_commands); 736 737 struct scsi_host_busy_iter_data { 738 bool (*fn)(struct scsi_cmnd *, void *); 739 void *priv; 740 }; 741 742 static bool __scsi_host_busy_iter_fn(struct request *req, void *priv) 743 { 744 struct scsi_host_busy_iter_data *iter_data = priv; 745 struct scsi_cmnd *sc = blk_mq_rq_to_pdu(req); 746 747 return iter_data->fn(sc, iter_data->priv); 748 } 749 750 /** 751 * scsi_host_busy_iter - Iterate over all busy commands 752 * @shost: Pointer to Scsi_Host. 753 * @fn: Function to call on each busy command 754 * @priv: Data pointer passed to @fn 755 * 756 * If locking against concurrent command completions is required 757 * ithas to be provided by the caller 758 **/ 759 void scsi_host_busy_iter(struct Scsi_Host *shost, 760 bool (*fn)(struct scsi_cmnd *, void *), 761 void *priv) 762 { 763 struct scsi_host_busy_iter_data iter_data = { 764 .fn = fn, 765 .priv = priv, 766 }; 767 768 blk_mq_tagset_busy_iter(&shost->tag_set, __scsi_host_busy_iter_fn, 769 &iter_data); 770 } 771 EXPORT_SYMBOL_GPL(scsi_host_busy_iter); 772