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 __must_hold(shost->host_lock) 77 { 78 enum scsi_host_state oldstate = READ_ONCE(shost->shost_state); 79 80 if (state == oldstate) 81 return 0; 82 83 switch (state) { 84 case SHOST_CREATED: 85 /* There are no legal states that come back to 86 * created. This is the manually initialised start 87 * state */ 88 goto illegal; 89 90 case SHOST_RUNNING: 91 switch (oldstate) { 92 case SHOST_CREATED: 93 case SHOST_RECOVERY: 94 break; 95 default: 96 goto illegal; 97 } 98 break; 99 100 case SHOST_RECOVERY: 101 switch (oldstate) { 102 case SHOST_RUNNING: 103 break; 104 default: 105 goto illegal; 106 } 107 break; 108 109 case SHOST_CANCEL: 110 switch (oldstate) { 111 case SHOST_CREATED: 112 case SHOST_RUNNING: 113 case SHOST_CANCEL_RECOVERY: 114 break; 115 default: 116 goto illegal; 117 } 118 break; 119 120 case SHOST_DEL: 121 switch (oldstate) { 122 case SHOST_CANCEL: 123 case SHOST_DEL_RECOVERY: 124 break; 125 default: 126 goto illegal; 127 } 128 break; 129 130 case SHOST_CANCEL_RECOVERY: 131 switch (oldstate) { 132 case SHOST_CANCEL: 133 case SHOST_RECOVERY: 134 break; 135 default: 136 goto illegal; 137 } 138 break; 139 140 case SHOST_DEL_RECOVERY: 141 switch (oldstate) { 142 case SHOST_CANCEL_RECOVERY: 143 break; 144 default: 145 goto illegal; 146 } 147 break; 148 } 149 WRITE_ONCE(shost->shost_state, state); 150 return 0; 151 152 illegal: 153 SCSI_LOG_ERROR_RECOVERY(1, 154 shost_printk(KERN_ERR, shost, 155 "Illegal host state transition" 156 "%s->%s\n", 157 scsi_host_state_name(oldstate), 158 scsi_host_state_name(state))); 159 return -EINVAL; 160 } 161 162 /** 163 * scsi_remove_host - remove a scsi host 164 * @shost: a pointer to a scsi host to remove 165 **/ 166 void scsi_remove_host(struct Scsi_Host *shost) 167 { 168 unsigned long flags; 169 170 mutex_lock(&shost->scan_mutex); 171 spin_lock_irqsave(shost->host_lock, flags); 172 if (scsi_host_set_state(shost, SHOST_CANCEL)) 173 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)) { 174 spin_unlock_irqrestore(shost->host_lock, flags); 175 mutex_unlock(&shost->scan_mutex); 176 return; 177 } 178 spin_unlock_irqrestore(shost->host_lock, flags); 179 180 scsi_autopm_get_host(shost); 181 flush_workqueue(shost->tmf_work_q); 182 scsi_forget_host(shost); 183 mutex_unlock(&shost->scan_mutex); 184 scsi_proc_host_rm(shost); 185 scsi_proc_hostdir_rm(shost->hostt); 186 187 /* 188 * New SCSI devices cannot be attached anymore because of the SCSI host 189 * state so drop the tag set refcnt. Wait until the tag set refcnt drops 190 * to zero because .exit_cmd_priv implementations may need the host 191 * pointer. 192 */ 193 kref_put(&shost->tagset_refcnt, scsi_mq_free_tags); 194 wait_for_completion(&shost->tagset_freed); 195 196 spin_lock_irqsave(shost->host_lock, flags); 197 if (scsi_host_set_state(shost, SHOST_DEL)) 198 BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY)); 199 spin_unlock_irqrestore(shost->host_lock, flags); 200 201 transport_unregister_device(&shost->shost_gendev); 202 device_unregister(&shost->shost_dev); 203 device_del(&shost->shost_gendev); 204 } 205 EXPORT_SYMBOL(scsi_remove_host); 206 207 /** 208 * scsi_add_host_with_dma - add a scsi host with dma device 209 * @shost: scsi host pointer to add 210 * @dev: a struct device of type scsi class 211 * @dma_dev: dma device for the host 212 * 213 * Note: You rarely need to worry about this unless you're in a 214 * virtualised host environments, so use the simpler scsi_add_host() 215 * function instead. 216 * 217 * Return value: 218 * 0 on success / != 0 for error 219 **/ 220 int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, 221 struct device *dma_dev) 222 { 223 const struct scsi_host_template *sht = shost->hostt; 224 int error = -EINVAL; 225 226 shost_printk(KERN_INFO, shost, "%s\n", 227 sht->info ? sht->info(shost) : sht->name); 228 229 if (!shost->can_queue) { 230 shost_printk(KERN_ERR, shost, 231 "can_queue = 0 no longer supported\n"); 232 goto fail; 233 } 234 235 if (shost->nr_reserved_cmds && !sht->queue_reserved_command) { 236 shost_printk(KERN_ERR, shost, 237 "nr_reserved_cmds set but no method to queue\n"); 238 goto fail; 239 } 240 241 /* Use min_t(int, ...) in case shost->can_queue exceeds SHRT_MAX */ 242 shost->cmd_per_lun = min_t(int, shost->cmd_per_lun, 243 shost->can_queue); 244 245 error = scsi_init_sense_cache(shost); 246 if (error) 247 goto fail; 248 249 if (!shost->shost_gendev.parent) 250 shost->shost_gendev.parent = dev ? dev : &platform_bus; 251 if (!dma_dev) 252 dma_dev = shost->shost_gendev.parent; 253 254 shost->dma_dev = dma_dev; 255 256 shost->max_sectors = min_not_zero(shost->max_sectors, 257 dma_max_mapping_size(dma_dev) >> SECTOR_SHIFT); 258 259 error = scsi_mq_setup_tags(shost); 260 if (error) 261 goto fail; 262 263 kref_init(&shost->tagset_refcnt); 264 init_completion(&shost->tagset_freed); 265 266 /* 267 * Increase usage count temporarily here so that calling 268 * scsi_autopm_put_host() will trigger runtime idle if there is 269 * nothing else preventing suspending the device. 270 */ 271 pm_runtime_get_noresume(&shost->shost_gendev); 272 pm_runtime_set_active(&shost->shost_gendev); 273 pm_runtime_enable(&shost->shost_gendev); 274 device_enable_async_suspend(&shost->shost_gendev); 275 276 error = device_add(&shost->shost_gendev); 277 if (error) 278 goto out_disable_runtime_pm; 279 280 scoped_guard(spinlock_irq, shost->host_lock) 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 enum scsi_host_state state = scsi_get_host_state(shost); 356 struct device *parent = dev->parent; 357 358 /* Wait for functions invoked through call_rcu(&scmd->rcu, ...) */ 359 rcu_barrier(); 360 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 (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 (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 scoped_guard(spinlock_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 427 index = ida_alloc(&host_index_ida, GFP_KERNEL); 428 if (index < 0) { 429 kfree(shost); 430 return NULL; 431 } 432 shost->host_no = index; 433 434 shost->dma_channel = 0xff; 435 436 /* These three are default values which can be overridden */ 437 shost->max_channel = 0; 438 shost->max_id = 8; 439 shost->max_lun = 8; 440 441 /* Give each shost a default transportt */ 442 shost->transportt = &blank_transport_template; 443 444 /* 445 * All drivers right now should be able to handle 12 byte 446 * commands. Every so often there are requests for 16 byte 447 * commands, but individual low-level drivers need to certify that 448 * they actually do something sensible with such commands. 449 */ 450 shost->max_cmd_len = 12; 451 shost->hostt = sht; 452 shost->this_id = sht->this_id; 453 shost->can_queue = sht->can_queue; 454 shost->nr_reserved_cmds = sht->nr_reserved_cmds; 455 shost->sg_tablesize = sht->sg_tablesize; 456 shost->sg_prot_tablesize = sht->sg_prot_tablesize; 457 shost->cmd_per_lun = sht->cmd_per_lun; 458 shost->no_write_same = sht->no_write_same; 459 shost->host_tagset = sht->host_tagset; 460 shost->queuecommand_may_block = sht->queuecommand_may_block; 461 462 if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler) 463 shost->eh_deadline = -1; 464 else if ((ulong) shost_eh_deadline * HZ > INT_MAX) { 465 shost_printk(KERN_WARNING, shost, 466 "eh_deadline %u too large, setting to %u\n", 467 shost_eh_deadline, INT_MAX / HZ); 468 shost->eh_deadline = INT_MAX; 469 } else 470 shost->eh_deadline = shost_eh_deadline * HZ; 471 472 if (sht->supported_mode == MODE_UNKNOWN) 473 /* means we didn't set it ... default to INITIATOR */ 474 shost->active_mode = MODE_INITIATOR; 475 else 476 shost->active_mode = sht->supported_mode; 477 478 if (sht->max_host_blocked) 479 shost->max_host_blocked = sht->max_host_blocked; 480 else 481 shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED; 482 483 /* 484 * If the driver imposes no hard sector transfer limit, start at 485 * machine infinity initially. 486 */ 487 if (sht->max_sectors) 488 shost->max_sectors = sht->max_sectors; 489 else 490 shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS; 491 492 shost->virt_boundary_mask = sht->virt_boundary_mask; 493 if (shost->virt_boundary_mask) { 494 WARN_ON_ONCE(sht->max_segment_size && 495 sht->max_segment_size != UINT_MAX); 496 shost->max_segment_size = UINT_MAX; 497 } else { 498 if (sht->max_segment_size) 499 shost->max_segment_size = sht->max_segment_size; 500 else 501 shost->max_segment_size = BLK_MAX_SEGMENT_SIZE; 502 } 503 504 /* 32-byte (dword) is a common minimum for HBAs. */ 505 if (sht->dma_alignment) 506 shost->dma_alignment = sht->dma_alignment; 507 else 508 shost->dma_alignment = 3; 509 510 /* 511 * assume a 4GB boundary, if not set 512 */ 513 if (sht->dma_boundary) 514 shost->dma_boundary = sht->dma_boundary; 515 else 516 shost->dma_boundary = 0xffffffff; 517 518 device_initialize(&shost->shost_gendev); 519 dev_set_name(&shost->shost_gendev, "host%d", shost->host_no); 520 shost->shost_gendev.bus = &scsi_bus_type; 521 shost->shost_gendev.type = &scsi_host_type; 522 scsi_enable_async_suspend(&shost->shost_gendev); 523 524 device_initialize(&shost->shost_dev); 525 shost->shost_dev.parent = &shost->shost_gendev; 526 shost->shost_dev.class = &shost_class; 527 dev_set_name(&shost->shost_dev, "host%d", shost->host_no); 528 shost->shost_dev.groups = sht->shost_groups; 529 530 shost->ehandler = kthread_run(scsi_error_handler, shost, 531 "scsi_eh_%d", shost->host_no); 532 if (IS_ERR(shost->ehandler)) { 533 shost_printk(KERN_WARNING, shost, 534 "error handler thread failed to spawn, error = %ld\n", 535 PTR_ERR(shost->ehandler)); 536 shost->ehandler = NULL; 537 goto fail; 538 } 539 540 shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d", 541 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 542 1, shost->host_no); 543 if (!shost->tmf_work_q) { 544 shost_printk(KERN_WARNING, shost, 545 "failed to create tmf workq\n"); 546 goto fail; 547 } 548 if (scsi_proc_hostdir_add(shost->hostt) < 0) 549 goto fail; 550 return shost; 551 fail: 552 /* 553 * Host state is still SHOST_CREATED and that is enough to release 554 * ->shost_gendev. scsi_host_dev_release() will free 555 * dev_name(&shost->shost_dev). 556 */ 557 put_device(&shost->shost_gendev); 558 559 return NULL; 560 } 561 EXPORT_SYMBOL(scsi_host_alloc); 562 563 static int __scsi_host_match(struct device *dev, const void *data) 564 { 565 struct Scsi_Host *p; 566 const unsigned int *hostnum = data; 567 568 p = class_to_shost(dev); 569 return p->host_no == *hostnum; 570 } 571 572 /** 573 * scsi_host_lookup - get a reference to a Scsi_Host by host no 574 * @hostnum: host number to locate 575 * 576 * Return value: 577 * A pointer to located Scsi_Host or NULL. 578 * 579 * The caller must do a scsi_host_put() to drop the reference 580 * that scsi_host_get() took. The put_device() below dropped 581 * the reference from class_find_device(). 582 **/ 583 struct Scsi_Host *scsi_host_lookup(unsigned int hostnum) 584 { 585 struct device *cdev; 586 struct Scsi_Host *shost = NULL; 587 588 cdev = class_find_device(&shost_class, NULL, &hostnum, 589 __scsi_host_match); 590 if (cdev) { 591 shost = scsi_host_get(class_to_shost(cdev)); 592 put_device(cdev); 593 } 594 return shost; 595 } 596 EXPORT_SYMBOL(scsi_host_lookup); 597 598 /** 599 * scsi_host_get - inc a Scsi_Host ref count 600 * @shost: Pointer to Scsi_Host to inc. 601 **/ 602 struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost) 603 { 604 if (scsi_get_host_state(shost) == SHOST_DEL || 605 !get_device(&shost->shost_gendev)) 606 return NULL; 607 return shost; 608 } 609 EXPORT_SYMBOL(scsi_host_get); 610 611 static bool scsi_host_check_in_flight(struct request *rq, void *data) 612 { 613 int *count = data; 614 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); 615 616 if (test_bit(SCMD_STATE_INFLIGHT, &cmd->state)) 617 (*count)++; 618 619 return true; 620 } 621 622 /** 623 * scsi_host_busy - Return the count of in-flight commands 624 * @shost: Pointer to Scsi_Host 625 **/ 626 int scsi_host_busy(struct Scsi_Host *shost) 627 { 628 int cnt = 0; 629 630 blk_mq_tagset_busy_iter(&shost->tag_set, 631 scsi_host_check_in_flight, &cnt); 632 return cnt; 633 } 634 EXPORT_SYMBOL(scsi_host_busy); 635 636 /** 637 * scsi_host_put - dec a Scsi_Host ref count 638 * @shost: Pointer to Scsi_Host to dec. 639 **/ 640 void scsi_host_put(struct Scsi_Host *shost) 641 { 642 put_device(&shost->shost_gendev); 643 } 644 EXPORT_SYMBOL(scsi_host_put); 645 646 int scsi_init_hosts(void) 647 { 648 return class_register(&shost_class); 649 } 650 651 void scsi_exit_hosts(void) 652 { 653 class_unregister(&shost_class); 654 ida_destroy(&host_index_ida); 655 } 656 657 int scsi_is_host_device(const struct device *dev) 658 { 659 return dev->type == &scsi_host_type; 660 } 661 EXPORT_SYMBOL(scsi_is_host_device); 662 663 /** 664 * scsi_queue_work - Queue work to the Scsi_Host workqueue. 665 * @shost: Pointer to Scsi_Host. 666 * @work: Work to queue for execution. 667 * 668 * Return value: 669 * 1 - work queued for execution 670 * 0 - work is already queued 671 * -EINVAL - work queue doesn't exist 672 **/ 673 int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work) 674 { 675 if (unlikely(!shost->work_q)) { 676 shost_printk(KERN_ERR, shost, 677 "ERROR: Scsi host '%s' attempted to queue scsi-work, " 678 "when no workqueue created.\n", shost->hostt->name); 679 dump_stack(); 680 681 return -EINVAL; 682 } 683 684 return queue_work(shost->work_q, work); 685 } 686 EXPORT_SYMBOL_GPL(scsi_queue_work); 687 688 /** 689 * scsi_flush_work - Flush a Scsi_Host's workqueue. 690 * @shost: Pointer to Scsi_Host. 691 **/ 692 void scsi_flush_work(struct Scsi_Host *shost) 693 { 694 if (!shost->work_q) { 695 shost_printk(KERN_ERR, shost, 696 "ERROR: Scsi host '%s' attempted to flush scsi-work, " 697 "when no workqueue created.\n", shost->hostt->name); 698 dump_stack(); 699 return; 700 } 701 702 flush_workqueue(shost->work_q); 703 } 704 EXPORT_SYMBOL_GPL(scsi_flush_work); 705 706 static bool complete_all_cmds_iter(struct request *rq, void *data) 707 { 708 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq); 709 enum scsi_host_status status = *(enum scsi_host_status *)data; 710 711 scsi_dma_unmap(scmd); 712 scmd->result = 0; 713 set_host_byte(scmd, status); 714 scsi_done(scmd); 715 return true; 716 } 717 718 /** 719 * scsi_host_complete_all_commands - Terminate all running commands 720 * @shost: Scsi Host on which commands should be terminated 721 * @status: Status to be set for the terminated commands 722 * 723 * There is no protection against modification of the number 724 * of outstanding commands. It is the responsibility of the 725 * caller to ensure that concurrent I/O submission and/or 726 * completion is stopped when calling this function. 727 */ 728 void scsi_host_complete_all_commands(struct Scsi_Host *shost, 729 enum scsi_host_status status) 730 { 731 blk_mq_tagset_busy_iter(&shost->tag_set, complete_all_cmds_iter, 732 &status); 733 } 734 EXPORT_SYMBOL_GPL(scsi_host_complete_all_commands); 735 736 struct scsi_host_busy_iter_data { 737 bool (*fn)(struct scsi_cmnd *, void *); 738 void *priv; 739 }; 740 741 static bool __scsi_host_busy_iter_fn(struct request *req, void *priv) 742 { 743 struct scsi_host_busy_iter_data *iter_data = priv; 744 struct scsi_cmnd *sc = blk_mq_rq_to_pdu(req); 745 746 return iter_data->fn(sc, iter_data->priv); 747 } 748 749 /** 750 * scsi_host_busy_iter - Iterate over all busy commands 751 * @shost: Pointer to Scsi_Host. 752 * @fn: Function to call on each busy command 753 * @priv: Data pointer passed to @fn 754 * 755 * If locking against concurrent command completions is required 756 * ithas to be provided by the caller 757 **/ 758 void scsi_host_busy_iter(struct Scsi_Host *shost, 759 bool (*fn)(struct scsi_cmnd *, void *), 760 void *priv) 761 { 762 struct scsi_host_busy_iter_data iter_data = { 763 .fn = fn, 764 .priv = priv, 765 }; 766 767 blk_mq_tagset_busy_iter(&shost->tag_set, __scsi_host_busy_iter_fn, 768 &iter_data); 769 } 770 EXPORT_SYMBOL_GPL(scsi_host_busy_iter); 771