Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  hosts.c Copyright (C) 1992 Drew Eckhardt
0004  *          Copyright (C) 1993, 1994, 1995 Eric Youngdale
0005  *          Copyright (C) 2002-2003 Christoph Hellwig
0006  *
0007  *  mid to lowlevel SCSI driver interface
0008  *      Initial versions: Drew Eckhardt
0009  *      Subsequent revisions: Eric Youngdale
0010  *
0011  *  <drew@colorado.edu>
0012  *
0013  *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
0014  *  Added QLOGIC QLA1280 SCSI controller kernel host support. 
0015  *     August 4, 1999 Fred Lewis, Intel DuPont
0016  *
0017  *  Updated to reflect the new initialization scheme for the higher 
0018  *  level of scsi drivers (sd/sr/st)
0019  *  September 17, 2000 Torben Mathiasen <tmm@image.dk>
0020  *
0021  *  Restructured scsi_host lists and associated functions.
0022  *  September 04, 2002 Mike Anderson (andmike@us.ibm.com)
0023  */
0024 
0025 #include <linux/module.h>
0026 #include <linux/blkdev.h>
0027 #include <linux/kernel.h>
0028 #include <linux/slab.h>
0029 #include <linux/kthread.h>
0030 #include <linux/string.h>
0031 #include <linux/mm.h>
0032 #include <linux/init.h>
0033 #include <linux/completion.h>
0034 #include <linux/transport_class.h>
0035 #include <linux/platform_device.h>
0036 #include <linux/pm_runtime.h>
0037 #include <linux/idr.h>
0038 #include <scsi/scsi_device.h>
0039 #include <scsi/scsi_host.h>
0040 #include <scsi/scsi_transport.h>
0041 #include <scsi/scsi_cmnd.h>
0042 
0043 #include "scsi_priv.h"
0044 #include "scsi_logging.h"
0045 
0046 
0047 static int shost_eh_deadline = -1;
0048 
0049 module_param_named(eh_deadline, shost_eh_deadline, int, S_IRUGO|S_IWUSR);
0050 MODULE_PARM_DESC(eh_deadline,
0051          "SCSI EH timeout in seconds (should be between 0 and 2^31-1)");
0052 
0053 static DEFINE_IDA(host_index_ida);
0054 
0055 
0056 static void scsi_host_cls_release(struct device *dev)
0057 {
0058     put_device(&class_to_shost(dev)->shost_gendev);
0059 }
0060 
0061 static struct class shost_class = {
0062     .name       = "scsi_host",
0063     .dev_release    = scsi_host_cls_release,
0064     .dev_groups = scsi_shost_groups,
0065 };
0066 
0067 /**
0068  *  scsi_host_set_state - Take the given host through the host state model.
0069  *  @shost: scsi host to change the state of.
0070  *  @state: state to change to.
0071  *
0072  *  Returns zero if unsuccessful or an error if the requested
0073  *  transition is illegal.
0074  **/
0075 int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
0076 {
0077     enum scsi_host_state oldstate = shost->shost_state;
0078 
0079     if (state == oldstate)
0080         return 0;
0081 
0082     switch (state) {
0083     case SHOST_CREATED:
0084         /* There are no legal states that come back to
0085          * created.  This is the manually initialised start
0086          * state */
0087         goto illegal;
0088 
0089     case SHOST_RUNNING:
0090         switch (oldstate) {
0091         case SHOST_CREATED:
0092         case SHOST_RECOVERY:
0093             break;
0094         default:
0095             goto illegal;
0096         }
0097         break;
0098 
0099     case SHOST_RECOVERY:
0100         switch (oldstate) {
0101         case SHOST_RUNNING:
0102             break;
0103         default:
0104             goto illegal;
0105         }
0106         break;
0107 
0108     case SHOST_CANCEL:
0109         switch (oldstate) {
0110         case SHOST_CREATED:
0111         case SHOST_RUNNING:
0112         case SHOST_CANCEL_RECOVERY:
0113             break;
0114         default:
0115             goto illegal;
0116         }
0117         break;
0118 
0119     case SHOST_DEL:
0120         switch (oldstate) {
0121         case SHOST_CANCEL:
0122         case SHOST_DEL_RECOVERY:
0123             break;
0124         default:
0125             goto illegal;
0126         }
0127         break;
0128 
0129     case SHOST_CANCEL_RECOVERY:
0130         switch (oldstate) {
0131         case SHOST_CANCEL:
0132         case SHOST_RECOVERY:
0133             break;
0134         default:
0135             goto illegal;
0136         }
0137         break;
0138 
0139     case SHOST_DEL_RECOVERY:
0140         switch (oldstate) {
0141         case SHOST_CANCEL_RECOVERY:
0142             break;
0143         default:
0144             goto illegal;
0145         }
0146         break;
0147     }
0148     shost->shost_state = state;
0149     return 0;
0150 
0151  illegal:
0152     SCSI_LOG_ERROR_RECOVERY(1,
0153                 shost_printk(KERN_ERR, shost,
0154                          "Illegal host state transition"
0155                          "%s->%s\n",
0156                          scsi_host_state_name(oldstate),
0157                          scsi_host_state_name(state)));
0158     return -EINVAL;
0159 }
0160 
0161 /**
0162  * scsi_remove_host - remove a scsi host
0163  * @shost:  a pointer to a scsi host to remove
0164  **/
0165 void scsi_remove_host(struct Scsi_Host *shost)
0166 {
0167     unsigned long flags;
0168 
0169     mutex_lock(&shost->scan_mutex);
0170     spin_lock_irqsave(shost->host_lock, flags);
0171     if (scsi_host_set_state(shost, SHOST_CANCEL))
0172         if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)) {
0173             spin_unlock_irqrestore(shost->host_lock, flags);
0174             mutex_unlock(&shost->scan_mutex);
0175             return;
0176         }
0177     spin_unlock_irqrestore(shost->host_lock, flags);
0178 
0179     scsi_autopm_get_host(shost);
0180     flush_workqueue(shost->tmf_work_q);
0181     scsi_forget_host(shost);
0182     mutex_unlock(&shost->scan_mutex);
0183     scsi_proc_host_rm(shost);
0184 
0185     /*
0186      * New SCSI devices cannot be attached anymore because of the SCSI host
0187      * state so drop the tag set refcnt. Wait until the tag set refcnt drops
0188      * to zero because .exit_cmd_priv implementations may need the host
0189      * pointer.
0190      */
0191     kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
0192     wait_for_completion(&shost->tagset_freed);
0193 
0194     spin_lock_irqsave(shost->host_lock, flags);
0195     if (scsi_host_set_state(shost, SHOST_DEL))
0196         BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY));
0197     spin_unlock_irqrestore(shost->host_lock, flags);
0198 
0199     transport_unregister_device(&shost->shost_gendev);
0200     device_unregister(&shost->shost_dev);
0201     device_del(&shost->shost_gendev);
0202 }
0203 EXPORT_SYMBOL(scsi_remove_host);
0204 
0205 /**
0206  * scsi_add_host_with_dma - add a scsi host with dma device
0207  * @shost:  scsi host pointer to add
0208  * @dev:    a struct device of type scsi class
0209  * @dma_dev:    dma device for the host
0210  *
0211  * Note: You rarely need to worry about this unless you're in a
0212  * virtualised host environments, so use the simpler scsi_add_host()
0213  * function instead.
0214  *
0215  * Return value: 
0216  *  0 on success / != 0 for error
0217  **/
0218 int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,
0219                struct device *dma_dev)
0220 {
0221     struct scsi_host_template *sht = shost->hostt;
0222     int error = -EINVAL;
0223 
0224     shost_printk(KERN_INFO, shost, "%s\n",
0225             sht->info ? sht->info(shost) : sht->name);
0226 
0227     if (!shost->can_queue) {
0228         shost_printk(KERN_ERR, shost,
0229                  "can_queue = 0 no longer supported\n");
0230         goto fail;
0231     }
0232 
0233     /* Use min_t(int, ...) in case shost->can_queue exceeds SHRT_MAX */
0234     shost->cmd_per_lun = min_t(int, shost->cmd_per_lun,
0235                    shost->can_queue);
0236 
0237     error = scsi_init_sense_cache(shost);
0238     if (error)
0239         goto fail;
0240 
0241     if (!shost->shost_gendev.parent)
0242         shost->shost_gendev.parent = dev ? dev : &platform_bus;
0243     if (!dma_dev)
0244         dma_dev = shost->shost_gendev.parent;
0245 
0246     shost->dma_dev = dma_dev;
0247 
0248     if (dma_dev->dma_mask) {
0249         shost->max_sectors = min_t(unsigned int, shost->max_sectors,
0250                 dma_max_mapping_size(dma_dev) >> SECTOR_SHIFT);
0251     }
0252 
0253     error = scsi_mq_setup_tags(shost);
0254     if (error)
0255         goto fail;
0256 
0257     kref_init(&shost->tagset_refcnt);
0258     init_completion(&shost->tagset_freed);
0259 
0260     /*
0261      * Increase usage count temporarily here so that calling
0262      * scsi_autopm_put_host() will trigger runtime idle if there is
0263      * nothing else preventing suspending the device.
0264      */
0265     pm_runtime_get_noresume(&shost->shost_gendev);
0266     pm_runtime_set_active(&shost->shost_gendev);
0267     pm_runtime_enable(&shost->shost_gendev);
0268     device_enable_async_suspend(&shost->shost_gendev);
0269 
0270     error = device_add(&shost->shost_gendev);
0271     if (error)
0272         goto out_disable_runtime_pm;
0273 
0274     scsi_host_set_state(shost, SHOST_RUNNING);
0275     get_device(shost->shost_gendev.parent);
0276 
0277     device_enable_async_suspend(&shost->shost_dev);
0278 
0279     get_device(&shost->shost_gendev);
0280     error = device_add(&shost->shost_dev);
0281     if (error)
0282         goto out_del_gendev;
0283 
0284     if (shost->transportt->host_size) {
0285         shost->shost_data = kzalloc(shost->transportt->host_size,
0286                      GFP_KERNEL);
0287         if (shost->shost_data == NULL) {
0288             error = -ENOMEM;
0289             goto out_del_dev;
0290         }
0291     }
0292 
0293     if (shost->transportt->create_work_queue) {
0294         snprintf(shost->work_q_name, sizeof(shost->work_q_name),
0295              "scsi_wq_%d", shost->host_no);
0296         shost->work_q = alloc_workqueue("%s",
0297             WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
0298             1, shost->work_q_name);
0299 
0300         if (!shost->work_q) {
0301             error = -EINVAL;
0302             goto out_del_dev;
0303         }
0304     }
0305 
0306     error = scsi_sysfs_add_host(shost);
0307     if (error)
0308         goto out_del_dev;
0309 
0310     scsi_proc_host_add(shost);
0311     scsi_autopm_put_host(shost);
0312     return error;
0313 
0314     /*
0315      * Any host allocation in this function will be freed in
0316      * scsi_host_dev_release().
0317      */
0318  out_del_dev:
0319     device_del(&shost->shost_dev);
0320  out_del_gendev:
0321     /*
0322      * Host state is SHOST_RUNNING so we have to explicitly release
0323      * ->shost_dev.
0324      */
0325     put_device(&shost->shost_dev);
0326     device_del(&shost->shost_gendev);
0327  out_disable_runtime_pm:
0328     device_disable_async_suspend(&shost->shost_gendev);
0329     pm_runtime_disable(&shost->shost_gendev);
0330     pm_runtime_set_suspended(&shost->shost_gendev);
0331     pm_runtime_put_noidle(&shost->shost_gendev);
0332     kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
0333  fail:
0334     return error;
0335 }
0336 EXPORT_SYMBOL(scsi_add_host_with_dma);
0337 
0338 static void scsi_host_dev_release(struct device *dev)
0339 {
0340     struct Scsi_Host *shost = dev_to_shost(dev);
0341     struct device *parent = dev->parent;
0342 
0343     scsi_proc_hostdir_rm(shost->hostt);
0344 
0345     /* Wait for functions invoked through call_rcu(&scmd->rcu, ...) */
0346     rcu_barrier();
0347 
0348     if (shost->tmf_work_q)
0349         destroy_workqueue(shost->tmf_work_q);
0350     if (shost->ehandler)
0351         kthread_stop(shost->ehandler);
0352     if (shost->work_q)
0353         destroy_workqueue(shost->work_q);
0354 
0355     if (shost->shost_state == SHOST_CREATED) {
0356         /*
0357          * Free the shost_dev device name here if scsi_host_alloc()
0358          * and scsi_host_put() have been called but neither
0359          * scsi_host_add() nor scsi_host_remove() has been called.
0360          * This avoids that the memory allocated for the shost_dev
0361          * name is leaked.
0362          */
0363         kfree(dev_name(&shost->shost_dev));
0364     }
0365 
0366     kfree(shost->shost_data);
0367 
0368     ida_free(&host_index_ida, shost->host_no);
0369 
0370     if (shost->shost_state != SHOST_CREATED)
0371         put_device(parent);
0372     kfree(shost);
0373 }
0374 
0375 static struct device_type scsi_host_type = {
0376     .name =     "scsi_host",
0377     .release =  scsi_host_dev_release,
0378 };
0379 
0380 /**
0381  * scsi_host_alloc - register a scsi host adapter instance.
0382  * @sht:    pointer to scsi host template
0383  * @privsize:   extra bytes to allocate for driver
0384  *
0385  * Note:
0386  *  Allocate a new Scsi_Host and perform basic initialization.
0387  *  The host is not published to the scsi midlayer until scsi_add_host
0388  *  is called.
0389  *
0390  * Return value:
0391  *  Pointer to a new Scsi_Host
0392  **/
0393 struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
0394 {
0395     struct Scsi_Host *shost;
0396     int index;
0397 
0398     shost = kzalloc(sizeof(struct Scsi_Host) + privsize, GFP_KERNEL);
0399     if (!shost)
0400         return NULL;
0401 
0402     shost->host_lock = &shost->default_lock;
0403     spin_lock_init(shost->host_lock);
0404     shost->shost_state = SHOST_CREATED;
0405     INIT_LIST_HEAD(&shost->__devices);
0406     INIT_LIST_HEAD(&shost->__targets);
0407     INIT_LIST_HEAD(&shost->eh_abort_list);
0408     INIT_LIST_HEAD(&shost->eh_cmd_q);
0409     INIT_LIST_HEAD(&shost->starved_list);
0410     init_waitqueue_head(&shost->host_wait);
0411     mutex_init(&shost->scan_mutex);
0412 
0413     index = ida_alloc(&host_index_ida, GFP_KERNEL);
0414     if (index < 0) {
0415         kfree(shost);
0416         return NULL;
0417     }
0418     shost->host_no = index;
0419 
0420     shost->dma_channel = 0xff;
0421 
0422     /* These three are default values which can be overridden */
0423     shost->max_channel = 0;
0424     shost->max_id = 8;
0425     shost->max_lun = 8;
0426 
0427     /* Give each shost a default transportt */
0428     shost->transportt = &blank_transport_template;
0429 
0430     /*
0431      * All drivers right now should be able to handle 12 byte
0432      * commands.  Every so often there are requests for 16 byte
0433      * commands, but individual low-level drivers need to certify that
0434      * they actually do something sensible with such commands.
0435      */
0436     shost->max_cmd_len = 12;
0437     shost->hostt = sht;
0438     shost->this_id = sht->this_id;
0439     shost->can_queue = sht->can_queue;
0440     shost->sg_tablesize = sht->sg_tablesize;
0441     shost->sg_prot_tablesize = sht->sg_prot_tablesize;
0442     shost->cmd_per_lun = sht->cmd_per_lun;
0443     shost->no_write_same = sht->no_write_same;
0444     shost->host_tagset = sht->host_tagset;
0445 
0446     if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler)
0447         shost->eh_deadline = -1;
0448     else if ((ulong) shost_eh_deadline * HZ > INT_MAX) {
0449         shost_printk(KERN_WARNING, shost,
0450                  "eh_deadline %u too large, setting to %u\n",
0451                  shost_eh_deadline, INT_MAX / HZ);
0452         shost->eh_deadline = INT_MAX;
0453     } else
0454         shost->eh_deadline = shost_eh_deadline * HZ;
0455 
0456     if (sht->supported_mode == MODE_UNKNOWN)
0457         /* means we didn't set it ... default to INITIATOR */
0458         shost->active_mode = MODE_INITIATOR;
0459     else
0460         shost->active_mode = sht->supported_mode;
0461 
0462     if (sht->max_host_blocked)
0463         shost->max_host_blocked = sht->max_host_blocked;
0464     else
0465         shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
0466 
0467     /*
0468      * If the driver imposes no hard sector transfer limit, start at
0469      * machine infinity initially.
0470      */
0471     if (sht->max_sectors)
0472         shost->max_sectors = sht->max_sectors;
0473     else
0474         shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
0475 
0476     if (sht->max_segment_size)
0477         shost->max_segment_size = sht->max_segment_size;
0478     else
0479         shost->max_segment_size = BLK_MAX_SEGMENT_SIZE;
0480 
0481     /*
0482      * assume a 4GB boundary, if not set
0483      */
0484     if (sht->dma_boundary)
0485         shost->dma_boundary = sht->dma_boundary;
0486     else
0487         shost->dma_boundary = 0xffffffff;
0488 
0489     if (sht->virt_boundary_mask)
0490         shost->virt_boundary_mask = sht->virt_boundary_mask;
0491 
0492     device_initialize(&shost->shost_gendev);
0493     dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
0494     shost->shost_gendev.bus = &scsi_bus_type;
0495     shost->shost_gendev.type = &scsi_host_type;
0496     scsi_enable_async_suspend(&shost->shost_gendev);
0497 
0498     device_initialize(&shost->shost_dev);
0499     shost->shost_dev.parent = &shost->shost_gendev;
0500     shost->shost_dev.class = &shost_class;
0501     dev_set_name(&shost->shost_dev, "host%d", shost->host_no);
0502     shost->shost_dev.groups = sht->shost_groups;
0503 
0504     shost->ehandler = kthread_run(scsi_error_handler, shost,
0505             "scsi_eh_%d", shost->host_no);
0506     if (IS_ERR(shost->ehandler)) {
0507         shost_printk(KERN_WARNING, shost,
0508             "error handler thread failed to spawn, error = %ld\n",
0509             PTR_ERR(shost->ehandler));
0510         shost->ehandler = NULL;
0511         goto fail;
0512     }
0513 
0514     shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d",
0515                     WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS,
0516                        1, shost->host_no);
0517     if (!shost->tmf_work_q) {
0518         shost_printk(KERN_WARNING, shost,
0519                  "failed to create tmf workq\n");
0520         goto fail;
0521     }
0522     scsi_proc_hostdir_add(shost->hostt);
0523     return shost;
0524  fail:
0525     /*
0526      * Host state is still SHOST_CREATED and that is enough to release
0527      * ->shost_gendev. scsi_host_dev_release() will free
0528      * dev_name(&shost->shost_dev).
0529      */
0530     put_device(&shost->shost_gendev);
0531 
0532     return NULL;
0533 }
0534 EXPORT_SYMBOL(scsi_host_alloc);
0535 
0536 static int __scsi_host_match(struct device *dev, const void *data)
0537 {
0538     struct Scsi_Host *p;
0539     const unsigned short *hostnum = data;
0540 
0541     p = class_to_shost(dev);
0542     return p->host_no == *hostnum;
0543 }
0544 
0545 /**
0546  * scsi_host_lookup - get a reference to a Scsi_Host by host no
0547  * @hostnum:    host number to locate
0548  *
0549  * Return value:
0550  *  A pointer to located Scsi_Host or NULL.
0551  *
0552  *  The caller must do a scsi_host_put() to drop the reference
0553  *  that scsi_host_get() took. The put_device() below dropped
0554  *  the reference from class_find_device().
0555  **/
0556 struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
0557 {
0558     struct device *cdev;
0559     struct Scsi_Host *shost = NULL;
0560 
0561     cdev = class_find_device(&shost_class, NULL, &hostnum,
0562                  __scsi_host_match);
0563     if (cdev) {
0564         shost = scsi_host_get(class_to_shost(cdev));
0565         put_device(cdev);
0566     }
0567     return shost;
0568 }
0569 EXPORT_SYMBOL(scsi_host_lookup);
0570 
0571 /**
0572  * scsi_host_get - inc a Scsi_Host ref count
0573  * @shost:  Pointer to Scsi_Host to inc.
0574  **/
0575 struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
0576 {
0577     if ((shost->shost_state == SHOST_DEL) ||
0578         !get_device(&shost->shost_gendev))
0579         return NULL;
0580     return shost;
0581 }
0582 EXPORT_SYMBOL(scsi_host_get);
0583 
0584 static bool scsi_host_check_in_flight(struct request *rq, void *data)
0585 {
0586     int *count = data;
0587     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
0588 
0589     if (test_bit(SCMD_STATE_INFLIGHT, &cmd->state))
0590         (*count)++;
0591 
0592     return true;
0593 }
0594 
0595 /**
0596  * scsi_host_busy - Return the host busy counter
0597  * @shost:  Pointer to Scsi_Host to inc.
0598  **/
0599 int scsi_host_busy(struct Scsi_Host *shost)
0600 {
0601     int cnt = 0;
0602 
0603     blk_mq_tagset_busy_iter(&shost->tag_set,
0604                 scsi_host_check_in_flight, &cnt);
0605     return cnt;
0606 }
0607 EXPORT_SYMBOL(scsi_host_busy);
0608 
0609 /**
0610  * scsi_host_put - dec a Scsi_Host ref count
0611  * @shost:  Pointer to Scsi_Host to dec.
0612  **/
0613 void scsi_host_put(struct Scsi_Host *shost)
0614 {
0615     put_device(&shost->shost_gendev);
0616 }
0617 EXPORT_SYMBOL(scsi_host_put);
0618 
0619 int scsi_init_hosts(void)
0620 {
0621     return class_register(&shost_class);
0622 }
0623 
0624 void scsi_exit_hosts(void)
0625 {
0626     class_unregister(&shost_class);
0627     ida_destroy(&host_index_ida);
0628 }
0629 
0630 int scsi_is_host_device(const struct device *dev)
0631 {
0632     return dev->type == &scsi_host_type;
0633 }
0634 EXPORT_SYMBOL(scsi_is_host_device);
0635 
0636 /**
0637  * scsi_queue_work - Queue work to the Scsi_Host workqueue.
0638  * @shost:  Pointer to Scsi_Host.
0639  * @work:   Work to queue for execution.
0640  *
0641  * Return value:
0642  *  1 - work queued for execution
0643  *  0 - work is already queued
0644  *  -EINVAL - work queue doesn't exist
0645  **/
0646 int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
0647 {
0648     if (unlikely(!shost->work_q)) {
0649         shost_printk(KERN_ERR, shost,
0650             "ERROR: Scsi host '%s' attempted to queue scsi-work, "
0651             "when no workqueue created.\n", shost->hostt->name);
0652         dump_stack();
0653 
0654         return -EINVAL;
0655     }
0656 
0657     return queue_work(shost->work_q, work);
0658 }
0659 EXPORT_SYMBOL_GPL(scsi_queue_work);
0660 
0661 /**
0662  * scsi_flush_work - Flush a Scsi_Host's workqueue.
0663  * @shost:  Pointer to Scsi_Host.
0664  **/
0665 void scsi_flush_work(struct Scsi_Host *shost)
0666 {
0667     if (!shost->work_q) {
0668         shost_printk(KERN_ERR, shost,
0669             "ERROR: Scsi host '%s' attempted to flush scsi-work, "
0670             "when no workqueue created.\n", shost->hostt->name);
0671         dump_stack();
0672         return;
0673     }
0674 
0675     flush_workqueue(shost->work_q);
0676 }
0677 EXPORT_SYMBOL_GPL(scsi_flush_work);
0678 
0679 static bool complete_all_cmds_iter(struct request *rq, void *data)
0680 {
0681     struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
0682     enum scsi_host_status status = *(enum scsi_host_status *)data;
0683 
0684     scsi_dma_unmap(scmd);
0685     scmd->result = 0;
0686     set_host_byte(scmd, status);
0687     scsi_done(scmd);
0688     return true;
0689 }
0690 
0691 /**
0692  * scsi_host_complete_all_commands - Terminate all running commands
0693  * @shost:  Scsi Host on which commands should be terminated
0694  * @status: Status to be set for the terminated commands
0695  *
0696  * There is no protection against modification of the number
0697  * of outstanding commands. It is the responsibility of the
0698  * caller to ensure that concurrent I/O submission and/or
0699  * completion is stopped when calling this function.
0700  */
0701 void scsi_host_complete_all_commands(struct Scsi_Host *shost,
0702                      enum scsi_host_status status)
0703 {
0704     blk_mq_tagset_busy_iter(&shost->tag_set, complete_all_cmds_iter,
0705                 &status);
0706 }
0707 EXPORT_SYMBOL_GPL(scsi_host_complete_all_commands);
0708 
0709 struct scsi_host_busy_iter_data {
0710     bool (*fn)(struct scsi_cmnd *, void *);
0711     void *priv;
0712 };
0713 
0714 static bool __scsi_host_busy_iter_fn(struct request *req, void *priv)
0715 {
0716     struct scsi_host_busy_iter_data *iter_data = priv;
0717     struct scsi_cmnd *sc = blk_mq_rq_to_pdu(req);
0718 
0719     return iter_data->fn(sc, iter_data->priv);
0720 }
0721 
0722 /**
0723  * scsi_host_busy_iter - Iterate over all busy commands
0724  * @shost:  Pointer to Scsi_Host.
0725  * @fn:     Function to call on each busy command
0726  * @priv:   Data pointer passed to @fn
0727  *
0728  * If locking against concurrent command completions is required
0729  * ithas to be provided by the caller
0730  **/
0731 void scsi_host_busy_iter(struct Scsi_Host *shost,
0732              bool (*fn)(struct scsi_cmnd *, void *),
0733              void *priv)
0734 {
0735     struct scsi_host_busy_iter_data iter_data = {
0736         .fn = fn,
0737         .priv = priv,
0738     };
0739 
0740     blk_mq_tagset_busy_iter(&shost->tag_set, __scsi_host_busy_iter_fn,
0741                 &iter_data);
0742 }
0743 EXPORT_SYMBOL_GPL(scsi_host_busy_iter);