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 cancel_work_sync(&shost->eh_work); 362 if (shost->tmf_work_q) 363 destroy_workqueue(shost->tmf_work_q); 364 if (shost->ehandler) 365 kthread_stop(shost->ehandler); 366 if (shost->work_q) 367 destroy_workqueue(shost->work_q); 368 369 if (state == SHOST_CREATED) { 370 /* 371 * Free the shost_dev device name and remove the proc host dir 372 * here if scsi_host_{alloc,put}() have been called but neither 373 * scsi_host_add() nor scsi_remove_host() has been called. 374 * This avoids that the memory allocated for the shost_dev 375 * name as well as the proc dir structure are leaked. 376 */ 377 scsi_proc_hostdir_rm(shost->hostt); 378 kfree(dev_name(&shost->shost_dev)); 379 } 380 381 kfree(shost->shost_data); 382 383 ida_free(&host_index_ida, shost->host_no); 384 385 if (state != SHOST_CREATED) 386 put_device(parent); 387 kfree(shost); 388 } 389 390 static const struct device_type scsi_host_type = { 391 .name = "scsi_host", 392 .release = scsi_host_dev_release, 393 }; 394 395 /** 396 * scsi_host_alloc - register a scsi host adapter instance. 397 * @sht: pointer to scsi host template 398 * @privsize: extra bytes to allocate for driver 399 * 400 * Note: 401 * Allocate a new Scsi_Host and perform basic initialization. 402 * The host is not published to the scsi midlayer until scsi_add_host 403 * is called. 404 * 405 * Return value: 406 * Pointer to a new Scsi_Host 407 **/ 408 struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int privsize) 409 { 410 struct Scsi_Host *shost; 411 int index; 412 413 shost = kzalloc(sizeof(struct Scsi_Host) + privsize, GFP_KERNEL); 414 if (!shost) 415 return NULL; 416 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 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 (scsi_get_host_state(shost) == 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