Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 1999 Eric Youngdale
0004  * Copyright (C) 2014 Christoph Hellwig
0005  *
0006  *  SCSI queueing library.
0007  *      Initial versions: Eric Youngdale (eric@andante.org).
0008  *                        Based upon conversations with large numbers
0009  *                        of people at Linux Expo.
0010  */
0011 
0012 #include <linux/bio.h>
0013 #include <linux/bitops.h>
0014 #include <linux/blkdev.h>
0015 #include <linux/completion.h>
0016 #include <linux/kernel.h>
0017 #include <linux/export.h>
0018 #include <linux/init.h>
0019 #include <linux/pci.h>
0020 #include <linux/delay.h>
0021 #include <linux/hardirq.h>
0022 #include <linux/scatterlist.h>
0023 #include <linux/blk-mq.h>
0024 #include <linux/blk-integrity.h>
0025 #include <linux/ratelimit.h>
0026 #include <asm/unaligned.h>
0027 
0028 #include <scsi/scsi.h>
0029 #include <scsi/scsi_cmnd.h>
0030 #include <scsi/scsi_dbg.h>
0031 #include <scsi/scsi_device.h>
0032 #include <scsi/scsi_driver.h>
0033 #include <scsi/scsi_eh.h>
0034 #include <scsi/scsi_host.h>
0035 #include <scsi/scsi_transport.h> /* __scsi_init_queue() */
0036 #include <scsi/scsi_dh.h>
0037 
0038 #include <trace/events/scsi.h>
0039 
0040 #include "scsi_debugfs.h"
0041 #include "scsi_priv.h"
0042 #include "scsi_logging.h"
0043 
0044 /*
0045  * Size of integrity metadata is usually small, 1 inline sg should
0046  * cover normal cases.
0047  */
0048 #ifdef CONFIG_ARCH_NO_SG_CHAIN
0049 #define  SCSI_INLINE_PROT_SG_CNT  0
0050 #define  SCSI_INLINE_SG_CNT  0
0051 #else
0052 #define  SCSI_INLINE_PROT_SG_CNT  1
0053 #define  SCSI_INLINE_SG_CNT  2
0054 #endif
0055 
0056 static struct kmem_cache *scsi_sense_cache;
0057 static DEFINE_MUTEX(scsi_sense_cache_mutex);
0058 
0059 static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd);
0060 
0061 int scsi_init_sense_cache(struct Scsi_Host *shost)
0062 {
0063     int ret = 0;
0064 
0065     mutex_lock(&scsi_sense_cache_mutex);
0066     if (!scsi_sense_cache) {
0067         scsi_sense_cache =
0068             kmem_cache_create_usercopy("scsi_sense_cache",
0069                 SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN,
0070                 0, SCSI_SENSE_BUFFERSIZE, NULL);
0071         if (!scsi_sense_cache)
0072             ret = -ENOMEM;
0073     }
0074     mutex_unlock(&scsi_sense_cache_mutex);
0075     return ret;
0076 }
0077 
0078 static void
0079 scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
0080 {
0081     struct Scsi_Host *host = cmd->device->host;
0082     struct scsi_device *device = cmd->device;
0083     struct scsi_target *starget = scsi_target(device);
0084 
0085     /*
0086      * Set the appropriate busy bit for the device/host.
0087      *
0088      * If the host/device isn't busy, assume that something actually
0089      * completed, and that we should be able to queue a command now.
0090      *
0091      * Note that the prior mid-layer assumption that any host could
0092      * always queue at least one command is now broken.  The mid-layer
0093      * will implement a user specifiable stall (see
0094      * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
0095      * if a command is requeued with no other commands outstanding
0096      * either for the device or for the host.
0097      */
0098     switch (reason) {
0099     case SCSI_MLQUEUE_HOST_BUSY:
0100         atomic_set(&host->host_blocked, host->max_host_blocked);
0101         break;
0102     case SCSI_MLQUEUE_DEVICE_BUSY:
0103     case SCSI_MLQUEUE_EH_RETRY:
0104         atomic_set(&device->device_blocked,
0105                device->max_device_blocked);
0106         break;
0107     case SCSI_MLQUEUE_TARGET_BUSY:
0108         atomic_set(&starget->target_blocked,
0109                starget->max_target_blocked);
0110         break;
0111     }
0112 }
0113 
0114 static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd, unsigned long msecs)
0115 {
0116     struct request *rq = scsi_cmd_to_rq(cmd);
0117 
0118     if (rq->rq_flags & RQF_DONTPREP) {
0119         rq->rq_flags &= ~RQF_DONTPREP;
0120         scsi_mq_uninit_cmd(cmd);
0121     } else {
0122         WARN_ON_ONCE(true);
0123     }
0124 
0125     if (msecs) {
0126         blk_mq_requeue_request(rq, false);
0127         blk_mq_delay_kick_requeue_list(rq->q, msecs);
0128     } else
0129         blk_mq_requeue_request(rq, true);
0130 }
0131 
0132 /**
0133  * __scsi_queue_insert - private queue insertion
0134  * @cmd: The SCSI command being requeued
0135  * @reason:  The reason for the requeue
0136  * @unbusy: Whether the queue should be unbusied
0137  *
0138  * This is a private queue insertion.  The public interface
0139  * scsi_queue_insert() always assumes the queue should be unbusied
0140  * because it's always called before the completion.  This function is
0141  * for a requeue after completion, which should only occur in this
0142  * file.
0143  */
0144 static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, bool unbusy)
0145 {
0146     struct scsi_device *device = cmd->device;
0147 
0148     SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
0149         "Inserting command %p into mlqueue\n", cmd));
0150 
0151     scsi_set_blocked(cmd, reason);
0152 
0153     /*
0154      * Decrement the counters, since these commands are no longer
0155      * active on the host/device.
0156      */
0157     if (unbusy)
0158         scsi_device_unbusy(device, cmd);
0159 
0160     /*
0161      * Requeue this command.  It will go before all other commands
0162      * that are already in the queue. Schedule requeue work under
0163      * lock such that the kblockd_schedule_work() call happens
0164      * before blk_mq_destroy_queue() finishes.
0165      */
0166     cmd->result = 0;
0167 
0168     blk_mq_requeue_request(scsi_cmd_to_rq(cmd), true);
0169 }
0170 
0171 /**
0172  * scsi_queue_insert - Reinsert a command in the queue.
0173  * @cmd:    command that we are adding to queue.
0174  * @reason: why we are inserting command to queue.
0175  *
0176  * We do this for one of two cases. Either the host is busy and it cannot accept
0177  * any more commands for the time being, or the device returned QUEUE_FULL and
0178  * can accept no more commands.
0179  *
0180  * Context: This could be called either from an interrupt context or a normal
0181  * process context.
0182  */
0183 void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
0184 {
0185     __scsi_queue_insert(cmd, reason, true);
0186 }
0187 
0188 
0189 /**
0190  * __scsi_execute - insert request and wait for the result
0191  * @sdev:   scsi device
0192  * @cmd:    scsi command
0193  * @data_direction: data direction
0194  * @buffer: data buffer
0195  * @bufflen:    len of buffer
0196  * @sense:  optional sense buffer
0197  * @sshdr:  optional decoded sense header
0198  * @timeout:    request timeout in HZ
0199  * @retries:    number of times to retry request
0200  * @flags:  flags for ->cmd_flags
0201  * @rq_flags:   flags for ->rq_flags
0202  * @resid:  optional residual length
0203  *
0204  * Returns the scsi_cmnd result field if a command was executed, or a negative
0205  * Linux error code if we didn't get that far.
0206  */
0207 int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
0208          int data_direction, void *buffer, unsigned bufflen,
0209          unsigned char *sense, struct scsi_sense_hdr *sshdr,
0210          int timeout, int retries, blk_opf_t flags,
0211          req_flags_t rq_flags, int *resid)
0212 {
0213     struct request *req;
0214     struct scsi_cmnd *scmd;
0215     int ret;
0216 
0217     req = scsi_alloc_request(sdev->request_queue,
0218             data_direction == DMA_TO_DEVICE ?
0219             REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
0220             rq_flags & RQF_PM ? BLK_MQ_REQ_PM : 0);
0221     if (IS_ERR(req))
0222         return PTR_ERR(req);
0223 
0224     if (bufflen) {
0225         ret = blk_rq_map_kern(sdev->request_queue, req,
0226                       buffer, bufflen, GFP_NOIO);
0227         if (ret)
0228             goto out;
0229     }
0230     scmd = blk_mq_rq_to_pdu(req);
0231     scmd->cmd_len = COMMAND_SIZE(cmd[0]);
0232     memcpy(scmd->cmnd, cmd, scmd->cmd_len);
0233     scmd->allowed = retries;
0234     req->timeout = timeout;
0235     req->cmd_flags |= flags;
0236     req->rq_flags |= rq_flags | RQF_QUIET;
0237 
0238     /*
0239      * head injection *required* here otherwise quiesce won't work
0240      */
0241     blk_execute_rq(req, true);
0242 
0243     /*
0244      * Some devices (USB mass-storage in particular) may transfer
0245      * garbage data together with a residue indicating that the data
0246      * is invalid.  Prevent the garbage from being misinterpreted
0247      * and prevent security leaks by zeroing out the excess data.
0248      */
0249     if (unlikely(scmd->resid_len > 0 && scmd->resid_len <= bufflen))
0250         memset(buffer + bufflen - scmd->resid_len, 0, scmd->resid_len);
0251 
0252     if (resid)
0253         *resid = scmd->resid_len;
0254     if (sense && scmd->sense_len)
0255         memcpy(sense, scmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
0256     if (sshdr)
0257         scsi_normalize_sense(scmd->sense_buffer, scmd->sense_len,
0258                      sshdr);
0259     ret = scmd->result;
0260  out:
0261     blk_mq_free_request(req);
0262 
0263     return ret;
0264 }
0265 EXPORT_SYMBOL(__scsi_execute);
0266 
0267 /*
0268  * Wake up the error handler if necessary. Avoid as follows that the error
0269  * handler is not woken up if host in-flight requests number ==
0270  * shost->host_failed: use call_rcu() in scsi_eh_scmd_add() in combination
0271  * with an RCU read lock in this function to ensure that this function in
0272  * its entirety either finishes before scsi_eh_scmd_add() increases the
0273  * host_failed counter or that it notices the shost state change made by
0274  * scsi_eh_scmd_add().
0275  */
0276 static void scsi_dec_host_busy(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
0277 {
0278     unsigned long flags;
0279 
0280     rcu_read_lock();
0281     __clear_bit(SCMD_STATE_INFLIGHT, &cmd->state);
0282     if (unlikely(scsi_host_in_recovery(shost))) {
0283         spin_lock_irqsave(shost->host_lock, flags);
0284         if (shost->host_failed || shost->host_eh_scheduled)
0285             scsi_eh_wakeup(shost);
0286         spin_unlock_irqrestore(shost->host_lock, flags);
0287     }
0288     rcu_read_unlock();
0289 }
0290 
0291 void scsi_device_unbusy(struct scsi_device *sdev, struct scsi_cmnd *cmd)
0292 {
0293     struct Scsi_Host *shost = sdev->host;
0294     struct scsi_target *starget = scsi_target(sdev);
0295 
0296     scsi_dec_host_busy(shost, cmd);
0297 
0298     if (starget->can_queue > 0)
0299         atomic_dec(&starget->target_busy);
0300 
0301     sbitmap_put(&sdev->budget_map, cmd->budget_token);
0302     cmd->budget_token = -1;
0303 }
0304 
0305 static void scsi_kick_queue(struct request_queue *q)
0306 {
0307     blk_mq_run_hw_queues(q, false);
0308 }
0309 
0310 /*
0311  * Called for single_lun devices on IO completion. Clear starget_sdev_user,
0312  * and call blk_run_queue for all the scsi_devices on the target -
0313  * including current_sdev first.
0314  *
0315  * Called with *no* scsi locks held.
0316  */
0317 static void scsi_single_lun_run(struct scsi_device *current_sdev)
0318 {
0319     struct Scsi_Host *shost = current_sdev->host;
0320     struct scsi_device *sdev, *tmp;
0321     struct scsi_target *starget = scsi_target(current_sdev);
0322     unsigned long flags;
0323 
0324     spin_lock_irqsave(shost->host_lock, flags);
0325     starget->starget_sdev_user = NULL;
0326     spin_unlock_irqrestore(shost->host_lock, flags);
0327 
0328     /*
0329      * Call blk_run_queue for all LUNs on the target, starting with
0330      * current_sdev. We race with others (to set starget_sdev_user),
0331      * but in most cases, we will be first. Ideally, each LU on the
0332      * target would get some limited time or requests on the target.
0333      */
0334     scsi_kick_queue(current_sdev->request_queue);
0335 
0336     spin_lock_irqsave(shost->host_lock, flags);
0337     if (starget->starget_sdev_user)
0338         goto out;
0339     list_for_each_entry_safe(sdev, tmp, &starget->devices,
0340             same_target_siblings) {
0341         if (sdev == current_sdev)
0342             continue;
0343         if (scsi_device_get(sdev))
0344             continue;
0345 
0346         spin_unlock_irqrestore(shost->host_lock, flags);
0347         scsi_kick_queue(sdev->request_queue);
0348         spin_lock_irqsave(shost->host_lock, flags);
0349 
0350         scsi_device_put(sdev);
0351     }
0352  out:
0353     spin_unlock_irqrestore(shost->host_lock, flags);
0354 }
0355 
0356 static inline bool scsi_device_is_busy(struct scsi_device *sdev)
0357 {
0358     if (scsi_device_busy(sdev) >= sdev->queue_depth)
0359         return true;
0360     if (atomic_read(&sdev->device_blocked) > 0)
0361         return true;
0362     return false;
0363 }
0364 
0365 static inline bool scsi_target_is_busy(struct scsi_target *starget)
0366 {
0367     if (starget->can_queue > 0) {
0368         if (atomic_read(&starget->target_busy) >= starget->can_queue)
0369             return true;
0370         if (atomic_read(&starget->target_blocked) > 0)
0371             return true;
0372     }
0373     return false;
0374 }
0375 
0376 static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
0377 {
0378     if (atomic_read(&shost->host_blocked) > 0)
0379         return true;
0380     if (shost->host_self_blocked)
0381         return true;
0382     return false;
0383 }
0384 
0385 static void scsi_starved_list_run(struct Scsi_Host *shost)
0386 {
0387     LIST_HEAD(starved_list);
0388     struct scsi_device *sdev;
0389     unsigned long flags;
0390 
0391     spin_lock_irqsave(shost->host_lock, flags);
0392     list_splice_init(&shost->starved_list, &starved_list);
0393 
0394     while (!list_empty(&starved_list)) {
0395         struct request_queue *slq;
0396 
0397         /*
0398          * As long as shost is accepting commands and we have
0399          * starved queues, call blk_run_queue. scsi_request_fn
0400          * drops the queue_lock and can add us back to the
0401          * starved_list.
0402          *
0403          * host_lock protects the starved_list and starved_entry.
0404          * scsi_request_fn must get the host_lock before checking
0405          * or modifying starved_list or starved_entry.
0406          */
0407         if (scsi_host_is_busy(shost))
0408             break;
0409 
0410         sdev = list_entry(starved_list.next,
0411                   struct scsi_device, starved_entry);
0412         list_del_init(&sdev->starved_entry);
0413         if (scsi_target_is_busy(scsi_target(sdev))) {
0414             list_move_tail(&sdev->starved_entry,
0415                        &shost->starved_list);
0416             continue;
0417         }
0418 
0419         /*
0420          * Once we drop the host lock, a racing scsi_remove_device()
0421          * call may remove the sdev from the starved list and destroy
0422          * it and the queue.  Mitigate by taking a reference to the
0423          * queue and never touching the sdev again after we drop the
0424          * host lock.  Note: if __scsi_remove_device() invokes
0425          * blk_mq_destroy_queue() before the queue is run from this
0426          * function then blk_run_queue() will return immediately since
0427          * blk_mq_destroy_queue() marks the queue with QUEUE_FLAG_DYING.
0428          */
0429         slq = sdev->request_queue;
0430         if (!blk_get_queue(slq))
0431             continue;
0432         spin_unlock_irqrestore(shost->host_lock, flags);
0433 
0434         scsi_kick_queue(slq);
0435         blk_put_queue(slq);
0436 
0437         spin_lock_irqsave(shost->host_lock, flags);
0438     }
0439     /* put any unprocessed entries back */
0440     list_splice(&starved_list, &shost->starved_list);
0441     spin_unlock_irqrestore(shost->host_lock, flags);
0442 }
0443 
0444 /**
0445  * scsi_run_queue - Select a proper request queue to serve next.
0446  * @q:  last request's queue
0447  *
0448  * The previous command was completely finished, start a new one if possible.
0449  */
0450 static void scsi_run_queue(struct request_queue *q)
0451 {
0452     struct scsi_device *sdev = q->queuedata;
0453 
0454     if (scsi_target(sdev)->single_lun)
0455         scsi_single_lun_run(sdev);
0456     if (!list_empty(&sdev->host->starved_list))
0457         scsi_starved_list_run(sdev->host);
0458 
0459     blk_mq_run_hw_queues(q, false);
0460 }
0461 
0462 void scsi_requeue_run_queue(struct work_struct *work)
0463 {
0464     struct scsi_device *sdev;
0465     struct request_queue *q;
0466 
0467     sdev = container_of(work, struct scsi_device, requeue_work);
0468     q = sdev->request_queue;
0469     scsi_run_queue(q);
0470 }
0471 
0472 void scsi_run_host_queues(struct Scsi_Host *shost)
0473 {
0474     struct scsi_device *sdev;
0475 
0476     shost_for_each_device(sdev, shost)
0477         scsi_run_queue(sdev->request_queue);
0478 }
0479 
0480 static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
0481 {
0482     if (!blk_rq_is_passthrough(scsi_cmd_to_rq(cmd))) {
0483         struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
0484 
0485         if (drv->uninit_command)
0486             drv->uninit_command(cmd);
0487     }
0488 }
0489 
0490 void scsi_free_sgtables(struct scsi_cmnd *cmd)
0491 {
0492     if (cmd->sdb.table.nents)
0493         sg_free_table_chained(&cmd->sdb.table,
0494                 SCSI_INLINE_SG_CNT);
0495     if (scsi_prot_sg_count(cmd))
0496         sg_free_table_chained(&cmd->prot_sdb->table,
0497                 SCSI_INLINE_PROT_SG_CNT);
0498 }
0499 EXPORT_SYMBOL_GPL(scsi_free_sgtables);
0500 
0501 static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
0502 {
0503     scsi_free_sgtables(cmd);
0504     scsi_uninit_cmd(cmd);
0505 }
0506 
0507 static void scsi_run_queue_async(struct scsi_device *sdev)
0508 {
0509     if (scsi_target(sdev)->single_lun ||
0510         !list_empty(&sdev->host->starved_list)) {
0511         kblockd_schedule_work(&sdev->requeue_work);
0512     } else {
0513         /*
0514          * smp_mb() present in sbitmap_queue_clear() or implied in
0515          * .end_io is for ordering writing .device_busy in
0516          * scsi_device_unbusy() and reading sdev->restarts.
0517          */
0518         int old = atomic_read(&sdev->restarts);
0519 
0520         /*
0521          * ->restarts has to be kept as non-zero if new budget
0522          *  contention occurs.
0523          *
0524          *  No need to run queue when either another re-run
0525          *  queue wins in updating ->restarts or a new budget
0526          *  contention occurs.
0527          */
0528         if (old && atomic_cmpxchg(&sdev->restarts, old, 0) == old)
0529             blk_mq_run_hw_queues(sdev->request_queue, true);
0530     }
0531 }
0532 
0533 /* Returns false when no more bytes to process, true if there are more */
0534 static bool scsi_end_request(struct request *req, blk_status_t error,
0535         unsigned int bytes)
0536 {
0537     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
0538     struct scsi_device *sdev = cmd->device;
0539     struct request_queue *q = sdev->request_queue;
0540 
0541     if (blk_update_request(req, error, bytes))
0542         return true;
0543 
0544     // XXX:
0545     if (blk_queue_add_random(q))
0546         add_disk_randomness(req->q->disk);
0547 
0548     if (!blk_rq_is_passthrough(req)) {
0549         WARN_ON_ONCE(!(cmd->flags & SCMD_INITIALIZED));
0550         cmd->flags &= ~SCMD_INITIALIZED;
0551     }
0552 
0553     /*
0554      * Calling rcu_barrier() is not necessary here because the
0555      * SCSI error handler guarantees that the function called by
0556      * call_rcu() has been called before scsi_end_request() is
0557      * called.
0558      */
0559     destroy_rcu_head(&cmd->rcu);
0560 
0561     /*
0562      * In the MQ case the command gets freed by __blk_mq_end_request,
0563      * so we have to do all cleanup that depends on it earlier.
0564      *
0565      * We also can't kick the queues from irq context, so we
0566      * will have to defer it to a workqueue.
0567      */
0568     scsi_mq_uninit_cmd(cmd);
0569 
0570     /*
0571      * queue is still alive, so grab the ref for preventing it
0572      * from being cleaned up during running queue.
0573      */
0574     percpu_ref_get(&q->q_usage_counter);
0575 
0576     __blk_mq_end_request(req, error);
0577 
0578     scsi_run_queue_async(sdev);
0579 
0580     percpu_ref_put(&q->q_usage_counter);
0581     return false;
0582 }
0583 
0584 /**
0585  * scsi_result_to_blk_status - translate a SCSI result code into blk_status_t
0586  * @cmd:    SCSI command
0587  * @result: scsi error code
0588  *
0589  * Translate a SCSI result code into a blk_status_t value. May reset the host
0590  * byte of @cmd->result.
0591  */
0592 static blk_status_t scsi_result_to_blk_status(struct scsi_cmnd *cmd, int result)
0593 {
0594     switch (host_byte(result)) {
0595     case DID_OK:
0596         if (scsi_status_is_good(result))
0597             return BLK_STS_OK;
0598         return BLK_STS_IOERR;
0599     case DID_TRANSPORT_FAILFAST:
0600     case DID_TRANSPORT_MARGINAL:
0601         return BLK_STS_TRANSPORT;
0602     case DID_TARGET_FAILURE:
0603         set_host_byte(cmd, DID_OK);
0604         return BLK_STS_TARGET;
0605     case DID_NEXUS_FAILURE:
0606         set_host_byte(cmd, DID_OK);
0607         return BLK_STS_NEXUS;
0608     case DID_ALLOC_FAILURE:
0609         set_host_byte(cmd, DID_OK);
0610         return BLK_STS_NOSPC;
0611     case DID_MEDIUM_ERROR:
0612         set_host_byte(cmd, DID_OK);
0613         return BLK_STS_MEDIUM;
0614     default:
0615         return BLK_STS_IOERR;
0616     }
0617 }
0618 
0619 /**
0620  * scsi_rq_err_bytes - determine number of bytes till the next failure boundary
0621  * @rq: request to examine
0622  *
0623  * Description:
0624  *     A request could be merge of IOs which require different failure
0625  *     handling.  This function determines the number of bytes which
0626  *     can be failed from the beginning of the request without
0627  *     crossing into area which need to be retried further.
0628  *
0629  * Return:
0630  *     The number of bytes to fail.
0631  */
0632 static unsigned int scsi_rq_err_bytes(const struct request *rq)
0633 {
0634     blk_opf_t ff = rq->cmd_flags & REQ_FAILFAST_MASK;
0635     unsigned int bytes = 0;
0636     struct bio *bio;
0637 
0638     if (!(rq->rq_flags & RQF_MIXED_MERGE))
0639         return blk_rq_bytes(rq);
0640 
0641     /*
0642      * Currently the only 'mixing' which can happen is between
0643      * different fastfail types.  We can safely fail portions
0644      * which have all the failfast bits that the first one has -
0645      * the ones which are at least as eager to fail as the first
0646      * one.
0647      */
0648     for (bio = rq->bio; bio; bio = bio->bi_next) {
0649         if ((bio->bi_opf & ff) != ff)
0650             break;
0651         bytes += bio->bi_iter.bi_size;
0652     }
0653 
0654     /* this could lead to infinite loop */
0655     BUG_ON(blk_rq_bytes(rq) && !bytes);
0656     return bytes;
0657 }
0658 
0659 static bool scsi_cmd_runtime_exceeced(struct scsi_cmnd *cmd)
0660 {
0661     struct request *req = scsi_cmd_to_rq(cmd);
0662     unsigned long wait_for;
0663 
0664     if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
0665         return false;
0666 
0667     wait_for = (cmd->allowed + 1) * req->timeout;
0668     if (time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
0669         scmd_printk(KERN_ERR, cmd, "timing out command, waited %lus\n",
0670                 wait_for/HZ);
0671         return true;
0672     }
0673     return false;
0674 }
0675 
0676 /*
0677  * When ALUA transition state is returned, reprep the cmd to
0678  * use the ALUA handler's transition timeout. Delay the reprep
0679  * 1 sec to avoid aggressive retries of the target in that
0680  * state.
0681  */
0682 #define ALUA_TRANSITION_REPREP_DELAY    1000
0683 
0684 /* Helper for scsi_io_completion() when special action required. */
0685 static void scsi_io_completion_action(struct scsi_cmnd *cmd, int result)
0686 {
0687     struct request *req = scsi_cmd_to_rq(cmd);
0688     int level = 0;
0689     enum {ACTION_FAIL, ACTION_REPREP, ACTION_DELAYED_REPREP,
0690           ACTION_RETRY, ACTION_DELAYED_RETRY} action;
0691     struct scsi_sense_hdr sshdr;
0692     bool sense_valid;
0693     bool sense_current = true;      /* false implies "deferred sense" */
0694     blk_status_t blk_stat;
0695 
0696     sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
0697     if (sense_valid)
0698         sense_current = !scsi_sense_is_deferred(&sshdr);
0699 
0700     blk_stat = scsi_result_to_blk_status(cmd, result);
0701 
0702     if (host_byte(result) == DID_RESET) {
0703         /* Third party bus reset or reset for error recovery
0704          * reasons.  Just retry the command and see what
0705          * happens.
0706          */
0707         action = ACTION_RETRY;
0708     } else if (sense_valid && sense_current) {
0709         switch (sshdr.sense_key) {
0710         case UNIT_ATTENTION:
0711             if (cmd->device->removable) {
0712                 /* Detected disc change.  Set a bit
0713                  * and quietly refuse further access.
0714                  */
0715                 cmd->device->changed = 1;
0716                 action = ACTION_FAIL;
0717             } else {
0718                 /* Must have been a power glitch, or a
0719                  * bus reset.  Could not have been a
0720                  * media change, so we just retry the
0721                  * command and see what happens.
0722                  */
0723                 action = ACTION_RETRY;
0724             }
0725             break;
0726         case ILLEGAL_REQUEST:
0727             /* If we had an ILLEGAL REQUEST returned, then
0728              * we may have performed an unsupported
0729              * command.  The only thing this should be
0730              * would be a ten byte read where only a six
0731              * byte read was supported.  Also, on a system
0732              * where READ CAPACITY failed, we may have
0733              * read past the end of the disk.
0734              */
0735             if ((cmd->device->use_10_for_rw &&
0736                 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
0737                 (cmd->cmnd[0] == READ_10 ||
0738                  cmd->cmnd[0] == WRITE_10)) {
0739                 /* This will issue a new 6-byte command. */
0740                 cmd->device->use_10_for_rw = 0;
0741                 action = ACTION_REPREP;
0742             } else if (sshdr.asc == 0x10) /* DIX */ {
0743                 action = ACTION_FAIL;
0744                 blk_stat = BLK_STS_PROTECTION;
0745             /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
0746             } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
0747                 action = ACTION_FAIL;
0748                 blk_stat = BLK_STS_TARGET;
0749             } else
0750                 action = ACTION_FAIL;
0751             break;
0752         case ABORTED_COMMAND:
0753             action = ACTION_FAIL;
0754             if (sshdr.asc == 0x10) /* DIF */
0755                 blk_stat = BLK_STS_PROTECTION;
0756             break;
0757         case NOT_READY:
0758             /* If the device is in the process of becoming
0759              * ready, or has a temporary blockage, retry.
0760              */
0761             if (sshdr.asc == 0x04) {
0762                 switch (sshdr.ascq) {
0763                 case 0x01: /* becoming ready */
0764                 case 0x04: /* format in progress */
0765                 case 0x05: /* rebuild in progress */
0766                 case 0x06: /* recalculation in progress */
0767                 case 0x07: /* operation in progress */
0768                 case 0x08: /* Long write in progress */
0769                 case 0x09: /* self test in progress */
0770                 case 0x11: /* notify (enable spinup) required */
0771                 case 0x14: /* space allocation in progress */
0772                 case 0x1a: /* start stop unit in progress */
0773                 case 0x1b: /* sanitize in progress */
0774                 case 0x1d: /* configuration in progress */
0775                 case 0x24: /* depopulation in progress */
0776                     action = ACTION_DELAYED_RETRY;
0777                     break;
0778                 case 0x0a: /* ALUA state transition */
0779                     action = ACTION_DELAYED_REPREP;
0780                     break;
0781                 default:
0782                     action = ACTION_FAIL;
0783                     break;
0784                 }
0785             } else
0786                 action = ACTION_FAIL;
0787             break;
0788         case VOLUME_OVERFLOW:
0789             /* See SSC3rXX or current. */
0790             action = ACTION_FAIL;
0791             break;
0792         case DATA_PROTECT:
0793             action = ACTION_FAIL;
0794             if ((sshdr.asc == 0x0C && sshdr.ascq == 0x12) ||
0795                 (sshdr.asc == 0x55 &&
0796                  (sshdr.ascq == 0x0E || sshdr.ascq == 0x0F))) {
0797                 /* Insufficient zone resources */
0798                 blk_stat = BLK_STS_ZONE_OPEN_RESOURCE;
0799             }
0800             break;
0801         default:
0802             action = ACTION_FAIL;
0803             break;
0804         }
0805     } else
0806         action = ACTION_FAIL;
0807 
0808     if (action != ACTION_FAIL && scsi_cmd_runtime_exceeced(cmd))
0809         action = ACTION_FAIL;
0810 
0811     switch (action) {
0812     case ACTION_FAIL:
0813         /* Give up and fail the remainder of the request */
0814         if (!(req->rq_flags & RQF_QUIET)) {
0815             static DEFINE_RATELIMIT_STATE(_rs,
0816                     DEFAULT_RATELIMIT_INTERVAL,
0817                     DEFAULT_RATELIMIT_BURST);
0818 
0819             if (unlikely(scsi_logging_level))
0820                 level =
0821                      SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
0822                             SCSI_LOG_MLCOMPLETE_BITS);
0823 
0824             /*
0825              * if logging is enabled the failure will be printed
0826              * in scsi_log_completion(), so avoid duplicate messages
0827              */
0828             if (!level && __ratelimit(&_rs)) {
0829                 scsi_print_result(cmd, NULL, FAILED);
0830                 if (sense_valid)
0831                     scsi_print_sense(cmd);
0832                 scsi_print_command(cmd);
0833             }
0834         }
0835         if (!scsi_end_request(req, blk_stat, scsi_rq_err_bytes(req)))
0836             return;
0837         fallthrough;
0838     case ACTION_REPREP:
0839         scsi_mq_requeue_cmd(cmd, 0);
0840         break;
0841     case ACTION_DELAYED_REPREP:
0842         scsi_mq_requeue_cmd(cmd, ALUA_TRANSITION_REPREP_DELAY);
0843         break;
0844     case ACTION_RETRY:
0845         /* Retry the same command immediately */
0846         __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, false);
0847         break;
0848     case ACTION_DELAYED_RETRY:
0849         /* Retry the same command after a delay */
0850         __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, false);
0851         break;
0852     }
0853 }
0854 
0855 /*
0856  * Helper for scsi_io_completion() when cmd->result is non-zero. Returns a
0857  * new result that may suppress further error checking. Also modifies
0858  * *blk_statp in some cases.
0859  */
0860 static int scsi_io_completion_nz_result(struct scsi_cmnd *cmd, int result,
0861                     blk_status_t *blk_statp)
0862 {
0863     bool sense_valid;
0864     bool sense_current = true;  /* false implies "deferred sense" */
0865     struct request *req = scsi_cmd_to_rq(cmd);
0866     struct scsi_sense_hdr sshdr;
0867 
0868     sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
0869     if (sense_valid)
0870         sense_current = !scsi_sense_is_deferred(&sshdr);
0871 
0872     if (blk_rq_is_passthrough(req)) {
0873         if (sense_valid) {
0874             /*
0875              * SG_IO wants current and deferred errors
0876              */
0877             cmd->sense_len = min(8 + cmd->sense_buffer[7],
0878                          SCSI_SENSE_BUFFERSIZE);
0879         }
0880         if (sense_current)
0881             *blk_statp = scsi_result_to_blk_status(cmd, result);
0882     } else if (blk_rq_bytes(req) == 0 && sense_current) {
0883         /*
0884          * Flush commands do not transfers any data, and thus cannot use
0885          * good_bytes != blk_rq_bytes(req) as the signal for an error.
0886          * This sets *blk_statp explicitly for the problem case.
0887          */
0888         *blk_statp = scsi_result_to_blk_status(cmd, result);
0889     }
0890     /*
0891      * Recovered errors need reporting, but they're always treated as
0892      * success, so fiddle the result code here.  For passthrough requests
0893      * we already took a copy of the original into sreq->result which
0894      * is what gets returned to the user
0895      */
0896     if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
0897         bool do_print = true;
0898         /*
0899          * if ATA PASS-THROUGH INFORMATION AVAILABLE [0x0, 0x1d]
0900          * skip print since caller wants ATA registers. Only occurs
0901          * on SCSI ATA PASS_THROUGH commands when CK_COND=1
0902          */
0903         if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
0904             do_print = false;
0905         else if (req->rq_flags & RQF_QUIET)
0906             do_print = false;
0907         if (do_print)
0908             scsi_print_sense(cmd);
0909         result = 0;
0910         /* for passthrough, *blk_statp may be set */
0911         *blk_statp = BLK_STS_OK;
0912     }
0913     /*
0914      * Another corner case: the SCSI status byte is non-zero but 'good'.
0915      * Example: PRE-FETCH command returns SAM_STAT_CONDITION_MET when
0916      * it is able to fit nominated LBs in its cache (and SAM_STAT_GOOD
0917      * if it can't fit). Treat SAM_STAT_CONDITION_MET and the related
0918      * intermediate statuses (both obsolete in SAM-4) as good.
0919      */
0920     if ((result & 0xff) && scsi_status_is_good(result)) {
0921         result = 0;
0922         *blk_statp = BLK_STS_OK;
0923     }
0924     return result;
0925 }
0926 
0927 /**
0928  * scsi_io_completion - Completion processing for SCSI commands.
0929  * @cmd:    command that is finished.
0930  * @good_bytes: number of processed bytes.
0931  *
0932  * We will finish off the specified number of sectors. If we are done, the
0933  * command block will be released and the queue function will be goosed. If we
0934  * are not done then we have to figure out what to do next:
0935  *
0936  *   a) We can call scsi_mq_requeue_cmd().  The request will be
0937  *  unprepared and put back on the queue.  Then a new command will
0938  *  be created for it.  This should be used if we made forward
0939  *  progress, or if we want to switch from READ(10) to READ(6) for
0940  *  example.
0941  *
0942  *   b) We can call scsi_io_completion_action().  The request will be
0943  *  put back on the queue and retried using the same command as
0944  *  before, possibly after a delay.
0945  *
0946  *   c) We can call scsi_end_request() with blk_stat other than
0947  *  BLK_STS_OK, to fail the remainder of the request.
0948  */
0949 void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
0950 {
0951     int result = cmd->result;
0952     struct request *req = scsi_cmd_to_rq(cmd);
0953     blk_status_t blk_stat = BLK_STS_OK;
0954 
0955     if (unlikely(result))   /* a nz result may or may not be an error */
0956         result = scsi_io_completion_nz_result(cmd, result, &blk_stat);
0957 
0958     /*
0959      * Next deal with any sectors which we were able to correctly
0960      * handle.
0961      */
0962     SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
0963         "%u sectors total, %d bytes done.\n",
0964         blk_rq_sectors(req), good_bytes));
0965 
0966     /*
0967      * Failed, zero length commands always need to drop down
0968      * to retry code. Fast path should return in this block.
0969      */
0970     if (likely(blk_rq_bytes(req) > 0 || blk_stat == BLK_STS_OK)) {
0971         if (likely(!scsi_end_request(req, blk_stat, good_bytes)))
0972             return; /* no bytes remaining */
0973     }
0974 
0975     /* Kill remainder if no retries. */
0976     if (unlikely(blk_stat && scsi_noretry_cmd(cmd))) {
0977         if (scsi_end_request(req, blk_stat, blk_rq_bytes(req)))
0978             WARN_ONCE(true,
0979                 "Bytes remaining after failed, no-retry command");
0980         return;
0981     }
0982 
0983     /*
0984      * If there had been no error, but we have leftover bytes in the
0985      * request just queue the command up again.
0986      */
0987     if (likely(result == 0))
0988         scsi_mq_requeue_cmd(cmd, 0);
0989     else
0990         scsi_io_completion_action(cmd, result);
0991 }
0992 
0993 static inline bool scsi_cmd_needs_dma_drain(struct scsi_device *sdev,
0994         struct request *rq)
0995 {
0996     return sdev->dma_drain_len && blk_rq_is_passthrough(rq) &&
0997            !op_is_write(req_op(rq)) &&
0998            sdev->host->hostt->dma_need_drain(rq);
0999 }
1000 
1001 /**
1002  * scsi_alloc_sgtables - Allocate and initialize data and integrity scatterlists
1003  * @cmd: SCSI command data structure to initialize.
1004  *
1005  * Initializes @cmd->sdb and also @cmd->prot_sdb if data integrity is enabled
1006  * for @cmd.
1007  *
1008  * Returns:
1009  * * BLK_STS_OK       - on success
1010  * * BLK_STS_RESOURCE - if the failure is retryable
1011  * * BLK_STS_IOERR    - if the failure is fatal
1012  */
1013 blk_status_t scsi_alloc_sgtables(struct scsi_cmnd *cmd)
1014 {
1015     struct scsi_device *sdev = cmd->device;
1016     struct request *rq = scsi_cmd_to_rq(cmd);
1017     unsigned short nr_segs = blk_rq_nr_phys_segments(rq);
1018     struct scatterlist *last_sg = NULL;
1019     blk_status_t ret;
1020     bool need_drain = scsi_cmd_needs_dma_drain(sdev, rq);
1021     int count;
1022 
1023     if (WARN_ON_ONCE(!nr_segs))
1024         return BLK_STS_IOERR;
1025 
1026     /*
1027      * Make sure there is space for the drain.  The driver must adjust
1028      * max_hw_segments to be prepared for this.
1029      */
1030     if (need_drain)
1031         nr_segs++;
1032 
1033     /*
1034      * If sg table allocation fails, requeue request later.
1035      */
1036     if (unlikely(sg_alloc_table_chained(&cmd->sdb.table, nr_segs,
1037             cmd->sdb.table.sgl, SCSI_INLINE_SG_CNT)))
1038         return BLK_STS_RESOURCE;
1039 
1040     /*
1041      * Next, walk the list, and fill in the addresses and sizes of
1042      * each segment.
1043      */
1044     count = __blk_rq_map_sg(rq->q, rq, cmd->sdb.table.sgl, &last_sg);
1045 
1046     if (blk_rq_bytes(rq) & rq->q->dma_pad_mask) {
1047         unsigned int pad_len =
1048             (rq->q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
1049 
1050         last_sg->length += pad_len;
1051         cmd->extra_len += pad_len;
1052     }
1053 
1054     if (need_drain) {
1055         sg_unmark_end(last_sg);
1056         last_sg = sg_next(last_sg);
1057         sg_set_buf(last_sg, sdev->dma_drain_buf, sdev->dma_drain_len);
1058         sg_mark_end(last_sg);
1059 
1060         cmd->extra_len += sdev->dma_drain_len;
1061         count++;
1062     }
1063 
1064     BUG_ON(count > cmd->sdb.table.nents);
1065     cmd->sdb.table.nents = count;
1066     cmd->sdb.length = blk_rq_payload_bytes(rq);
1067 
1068     if (blk_integrity_rq(rq)) {
1069         struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1070         int ivecs;
1071 
1072         if (WARN_ON_ONCE(!prot_sdb)) {
1073             /*
1074              * This can happen if someone (e.g. multipath)
1075              * queues a command to a device on an adapter
1076              * that does not support DIX.
1077              */
1078             ret = BLK_STS_IOERR;
1079             goto out_free_sgtables;
1080         }
1081 
1082         ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
1083 
1084         if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
1085                 prot_sdb->table.sgl,
1086                 SCSI_INLINE_PROT_SG_CNT)) {
1087             ret = BLK_STS_RESOURCE;
1088             goto out_free_sgtables;
1089         }
1090 
1091         count = blk_rq_map_integrity_sg(rq->q, rq->bio,
1092                         prot_sdb->table.sgl);
1093         BUG_ON(count > ivecs);
1094         BUG_ON(count > queue_max_integrity_segments(rq->q));
1095 
1096         cmd->prot_sdb = prot_sdb;
1097         cmd->prot_sdb->table.nents = count;
1098     }
1099 
1100     return BLK_STS_OK;
1101 out_free_sgtables:
1102     scsi_free_sgtables(cmd);
1103     return ret;
1104 }
1105 EXPORT_SYMBOL(scsi_alloc_sgtables);
1106 
1107 /**
1108  * scsi_initialize_rq - initialize struct scsi_cmnd partially
1109  * @rq: Request associated with the SCSI command to be initialized.
1110  *
1111  * This function initializes the members of struct scsi_cmnd that must be
1112  * initialized before request processing starts and that won't be
1113  * reinitialized if a SCSI command is requeued.
1114  */
1115 static void scsi_initialize_rq(struct request *rq)
1116 {
1117     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1118 
1119     memset(cmd->cmnd, 0, sizeof(cmd->cmnd));
1120     cmd->cmd_len = MAX_COMMAND_SIZE;
1121     cmd->sense_len = 0;
1122     init_rcu_head(&cmd->rcu);
1123     cmd->jiffies_at_alloc = jiffies;
1124     cmd->retries = 0;
1125 }
1126 
1127 struct request *scsi_alloc_request(struct request_queue *q, blk_opf_t opf,
1128                    blk_mq_req_flags_t flags)
1129 {
1130     struct request *rq;
1131 
1132     rq = blk_mq_alloc_request(q, opf, flags);
1133     if (!IS_ERR(rq))
1134         scsi_initialize_rq(rq);
1135     return rq;
1136 }
1137 EXPORT_SYMBOL_GPL(scsi_alloc_request);
1138 
1139 /*
1140  * Only called when the request isn't completed by SCSI, and not freed by
1141  * SCSI
1142  */
1143 static void scsi_cleanup_rq(struct request *rq)
1144 {
1145     if (rq->rq_flags & RQF_DONTPREP) {
1146         scsi_mq_uninit_cmd(blk_mq_rq_to_pdu(rq));
1147         rq->rq_flags &= ~RQF_DONTPREP;
1148     }
1149 }
1150 
1151 /* Called before a request is prepared. See also scsi_mq_prep_fn(). */
1152 void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd)
1153 {
1154     struct request *rq = scsi_cmd_to_rq(cmd);
1155 
1156     if (!blk_rq_is_passthrough(rq) && !(cmd->flags & SCMD_INITIALIZED)) {
1157         cmd->flags |= SCMD_INITIALIZED;
1158         scsi_initialize_rq(rq);
1159     }
1160 
1161     cmd->device = dev;
1162     INIT_LIST_HEAD(&cmd->eh_entry);
1163     INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1164 }
1165 
1166 static blk_status_t scsi_setup_scsi_cmnd(struct scsi_device *sdev,
1167         struct request *req)
1168 {
1169     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1170 
1171     /*
1172      * Passthrough requests may transfer data, in which case they must
1173      * a bio attached to them.  Or they might contain a SCSI command
1174      * that does not transfer data, in which case they may optionally
1175      * submit a request without an attached bio.
1176      */
1177     if (req->bio) {
1178         blk_status_t ret = scsi_alloc_sgtables(cmd);
1179         if (unlikely(ret != BLK_STS_OK))
1180             return ret;
1181     } else {
1182         BUG_ON(blk_rq_bytes(req));
1183 
1184         memset(&cmd->sdb, 0, sizeof(cmd->sdb));
1185     }
1186 
1187     cmd->transfersize = blk_rq_bytes(req);
1188     return BLK_STS_OK;
1189 }
1190 
1191 static blk_status_t
1192 scsi_device_state_check(struct scsi_device *sdev, struct request *req)
1193 {
1194     switch (sdev->sdev_state) {
1195     case SDEV_CREATED:
1196         return BLK_STS_OK;
1197     case SDEV_OFFLINE:
1198     case SDEV_TRANSPORT_OFFLINE:
1199         /*
1200          * If the device is offline we refuse to process any
1201          * commands.  The device must be brought online
1202          * before trying any recovery commands.
1203          */
1204         if (!sdev->offline_already) {
1205             sdev->offline_already = true;
1206             sdev_printk(KERN_ERR, sdev,
1207                     "rejecting I/O to offline device\n");
1208         }
1209         return BLK_STS_IOERR;
1210     case SDEV_DEL:
1211         /*
1212          * If the device is fully deleted, we refuse to
1213          * process any commands as well.
1214          */
1215         sdev_printk(KERN_ERR, sdev,
1216                 "rejecting I/O to dead device\n");
1217         return BLK_STS_IOERR;
1218     case SDEV_BLOCK:
1219     case SDEV_CREATED_BLOCK:
1220         return BLK_STS_RESOURCE;
1221     case SDEV_QUIESCE:
1222         /*
1223          * If the device is blocked we only accept power management
1224          * commands.
1225          */
1226         if (req && WARN_ON_ONCE(!(req->rq_flags & RQF_PM)))
1227             return BLK_STS_RESOURCE;
1228         return BLK_STS_OK;
1229     default:
1230         /*
1231          * For any other not fully online state we only allow
1232          * power management commands.
1233          */
1234         if (req && !(req->rq_flags & RQF_PM))
1235             return BLK_STS_OFFLINE;
1236         return BLK_STS_OK;
1237     }
1238 }
1239 
1240 /*
1241  * scsi_dev_queue_ready: if we can send requests to sdev, assign one token
1242  * and return the token else return -1.
1243  */
1244 static inline int scsi_dev_queue_ready(struct request_queue *q,
1245                   struct scsi_device *sdev)
1246 {
1247     int token;
1248 
1249     token = sbitmap_get(&sdev->budget_map);
1250     if (atomic_read(&sdev->device_blocked)) {
1251         if (token < 0)
1252             goto out;
1253 
1254         if (scsi_device_busy(sdev) > 1)
1255             goto out_dec;
1256 
1257         /*
1258          * unblock after device_blocked iterates to zero
1259          */
1260         if (atomic_dec_return(&sdev->device_blocked) > 0)
1261             goto out_dec;
1262         SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1263                    "unblocking device at zero depth\n"));
1264     }
1265 
1266     return token;
1267 out_dec:
1268     if (token >= 0)
1269         sbitmap_put(&sdev->budget_map, token);
1270 out:
1271     return -1;
1272 }
1273 
1274 /*
1275  * scsi_target_queue_ready: checks if there we can send commands to target
1276  * @sdev: scsi device on starget to check.
1277  */
1278 static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1279                        struct scsi_device *sdev)
1280 {
1281     struct scsi_target *starget = scsi_target(sdev);
1282     unsigned int busy;
1283 
1284     if (starget->single_lun) {
1285         spin_lock_irq(shost->host_lock);
1286         if (starget->starget_sdev_user &&
1287             starget->starget_sdev_user != sdev) {
1288             spin_unlock_irq(shost->host_lock);
1289             return 0;
1290         }
1291         starget->starget_sdev_user = sdev;
1292         spin_unlock_irq(shost->host_lock);
1293     }
1294 
1295     if (starget->can_queue <= 0)
1296         return 1;
1297 
1298     busy = atomic_inc_return(&starget->target_busy) - 1;
1299     if (atomic_read(&starget->target_blocked) > 0) {
1300         if (busy)
1301             goto starved;
1302 
1303         /*
1304          * unblock after target_blocked iterates to zero
1305          */
1306         if (atomic_dec_return(&starget->target_blocked) > 0)
1307             goto out_dec;
1308 
1309         SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1310                  "unblocking target at zero depth\n"));
1311     }
1312 
1313     if (busy >= starget->can_queue)
1314         goto starved;
1315 
1316     return 1;
1317 
1318 starved:
1319     spin_lock_irq(shost->host_lock);
1320     list_move_tail(&sdev->starved_entry, &shost->starved_list);
1321     spin_unlock_irq(shost->host_lock);
1322 out_dec:
1323     if (starget->can_queue > 0)
1324         atomic_dec(&starget->target_busy);
1325     return 0;
1326 }
1327 
1328 /*
1329  * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1330  * return 0. We must end up running the queue again whenever 0 is
1331  * returned, else IO can hang.
1332  */
1333 static inline int scsi_host_queue_ready(struct request_queue *q,
1334                    struct Scsi_Host *shost,
1335                    struct scsi_device *sdev,
1336                    struct scsi_cmnd *cmd)
1337 {
1338     if (scsi_host_in_recovery(shost))
1339         return 0;
1340 
1341     if (atomic_read(&shost->host_blocked) > 0) {
1342         if (scsi_host_busy(shost) > 0)
1343             goto starved;
1344 
1345         /*
1346          * unblock after host_blocked iterates to zero
1347          */
1348         if (atomic_dec_return(&shost->host_blocked) > 0)
1349             goto out_dec;
1350 
1351         SCSI_LOG_MLQUEUE(3,
1352             shost_printk(KERN_INFO, shost,
1353                      "unblocking host at zero depth\n"));
1354     }
1355 
1356     if (shost->host_self_blocked)
1357         goto starved;
1358 
1359     /* We're OK to process the command, so we can't be starved */
1360     if (!list_empty(&sdev->starved_entry)) {
1361         spin_lock_irq(shost->host_lock);
1362         if (!list_empty(&sdev->starved_entry))
1363             list_del_init(&sdev->starved_entry);
1364         spin_unlock_irq(shost->host_lock);
1365     }
1366 
1367     __set_bit(SCMD_STATE_INFLIGHT, &cmd->state);
1368 
1369     return 1;
1370 
1371 starved:
1372     spin_lock_irq(shost->host_lock);
1373     if (list_empty(&sdev->starved_entry))
1374         list_add_tail(&sdev->starved_entry, &shost->starved_list);
1375     spin_unlock_irq(shost->host_lock);
1376 out_dec:
1377     scsi_dec_host_busy(shost, cmd);
1378     return 0;
1379 }
1380 
1381 /*
1382  * Busy state exporting function for request stacking drivers.
1383  *
1384  * For efficiency, no lock is taken to check the busy state of
1385  * shost/starget/sdev, since the returned value is not guaranteed and
1386  * may be changed after request stacking drivers call the function,
1387  * regardless of taking lock or not.
1388  *
1389  * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
1390  * needs to return 'not busy'. Otherwise, request stacking drivers
1391  * may hold requests forever.
1392  */
1393 static bool scsi_mq_lld_busy(struct request_queue *q)
1394 {
1395     struct scsi_device *sdev = q->queuedata;
1396     struct Scsi_Host *shost;
1397 
1398     if (blk_queue_dying(q))
1399         return false;
1400 
1401     shost = sdev->host;
1402 
1403     /*
1404      * Ignore host/starget busy state.
1405      * Since block layer does not have a concept of fairness across
1406      * multiple queues, congestion of host/starget needs to be handled
1407      * in SCSI layer.
1408      */
1409     if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
1410         return true;
1411 
1412     return false;
1413 }
1414 
1415 /*
1416  * Block layer request completion callback. May be called from interrupt
1417  * context.
1418  */
1419 static void scsi_complete(struct request *rq)
1420 {
1421     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1422     enum scsi_disposition disposition;
1423 
1424     INIT_LIST_HEAD(&cmd->eh_entry);
1425 
1426     atomic_inc(&cmd->device->iodone_cnt);
1427     if (cmd->result)
1428         atomic_inc(&cmd->device->ioerr_cnt);
1429 
1430     disposition = scsi_decide_disposition(cmd);
1431     if (disposition != SUCCESS && scsi_cmd_runtime_exceeced(cmd))
1432         disposition = SUCCESS;
1433 
1434     scsi_log_completion(cmd, disposition);
1435 
1436     switch (disposition) {
1437     case SUCCESS:
1438         scsi_finish_command(cmd);
1439         break;
1440     case NEEDS_RETRY:
1441         scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
1442         break;
1443     case ADD_TO_MLQUEUE:
1444         scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1445         break;
1446     default:
1447         scsi_eh_scmd_add(cmd);
1448         break;
1449     }
1450 }
1451 
1452 /**
1453  * scsi_dispatch_cmd - Dispatch a command to the low-level driver.
1454  * @cmd: command block we are dispatching.
1455  *
1456  * Return: nonzero return request was rejected and device's queue needs to be
1457  * plugged.
1458  */
1459 static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
1460 {
1461     struct Scsi_Host *host = cmd->device->host;
1462     int rtn = 0;
1463 
1464     atomic_inc(&cmd->device->iorequest_cnt);
1465 
1466     /* check if the device is still usable */
1467     if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
1468         /* in SDEV_DEL we error all commands. DID_NO_CONNECT
1469          * returns an immediate error upwards, and signals
1470          * that the device is no longer present */
1471         cmd->result = DID_NO_CONNECT << 16;
1472         goto done;
1473     }
1474 
1475     /* Check to see if the scsi lld made this device blocked. */
1476     if (unlikely(scsi_device_blocked(cmd->device))) {
1477         /*
1478          * in blocked state, the command is just put back on
1479          * the device queue.  The suspend state has already
1480          * blocked the queue so future requests should not
1481          * occur until the device transitions out of the
1482          * suspend state.
1483          */
1484         SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1485             "queuecommand : device blocked\n"));
1486         return SCSI_MLQUEUE_DEVICE_BUSY;
1487     }
1488 
1489     /* Store the LUN value in cmnd, if needed. */
1490     if (cmd->device->lun_in_cdb)
1491         cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
1492                    (cmd->device->lun << 5 & 0xe0);
1493 
1494     scsi_log_send(cmd);
1495 
1496     /*
1497      * Before we queue this command, check if the command
1498      * length exceeds what the host adapter can handle.
1499      */
1500     if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
1501         SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1502                    "queuecommand : command too long. "
1503                    "cdb_size=%d host->max_cmd_len=%d\n",
1504                    cmd->cmd_len, cmd->device->host->max_cmd_len));
1505         cmd->result = (DID_ABORT << 16);
1506         goto done;
1507     }
1508 
1509     if (unlikely(host->shost_state == SHOST_DEL)) {
1510         cmd->result = (DID_NO_CONNECT << 16);
1511         goto done;
1512 
1513     }
1514 
1515     trace_scsi_dispatch_cmd_start(cmd);
1516     rtn = host->hostt->queuecommand(host, cmd);
1517     if (rtn) {
1518         trace_scsi_dispatch_cmd_error(cmd, rtn);
1519         if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
1520             rtn != SCSI_MLQUEUE_TARGET_BUSY)
1521             rtn = SCSI_MLQUEUE_HOST_BUSY;
1522 
1523         SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1524             "queuecommand : request rejected\n"));
1525     }
1526 
1527     return rtn;
1528  done:
1529     scsi_done(cmd);
1530     return 0;
1531 }
1532 
1533 /* Size in bytes of the sg-list stored in the scsi-mq command-private data. */
1534 static unsigned int scsi_mq_inline_sgl_size(struct Scsi_Host *shost)
1535 {
1536     return min_t(unsigned int, shost->sg_tablesize, SCSI_INLINE_SG_CNT) *
1537         sizeof(struct scatterlist);
1538 }
1539 
1540 static blk_status_t scsi_prepare_cmd(struct request *req)
1541 {
1542     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1543     struct scsi_device *sdev = req->q->queuedata;
1544     struct Scsi_Host *shost = sdev->host;
1545     bool in_flight = test_bit(SCMD_STATE_INFLIGHT, &cmd->state);
1546     struct scatterlist *sg;
1547 
1548     scsi_init_command(sdev, cmd);
1549 
1550     cmd->eh_eflags = 0;
1551     cmd->prot_type = 0;
1552     cmd->prot_flags = 0;
1553     cmd->submitter = 0;
1554     memset(&cmd->sdb, 0, sizeof(cmd->sdb));
1555     cmd->underflow = 0;
1556     cmd->transfersize = 0;
1557     cmd->host_scribble = NULL;
1558     cmd->result = 0;
1559     cmd->extra_len = 0;
1560     cmd->state = 0;
1561     if (in_flight)
1562         __set_bit(SCMD_STATE_INFLIGHT, &cmd->state);
1563 
1564     /*
1565      * Only clear the driver-private command data if the LLD does not supply
1566      * a function to initialize that data.
1567      */
1568     if (!shost->hostt->init_cmd_priv)
1569         memset(cmd + 1, 0, shost->hostt->cmd_size);
1570 
1571     cmd->prot_op = SCSI_PROT_NORMAL;
1572     if (blk_rq_bytes(req))
1573         cmd->sc_data_direction = rq_dma_dir(req);
1574     else
1575         cmd->sc_data_direction = DMA_NONE;
1576 
1577     sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
1578     cmd->sdb.table.sgl = sg;
1579 
1580     if (scsi_host_get_prot(shost)) {
1581         memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
1582 
1583         cmd->prot_sdb->table.sgl =
1584             (struct scatterlist *)(cmd->prot_sdb + 1);
1585     }
1586 
1587     /*
1588      * Special handling for passthrough commands, which don't go to the ULP
1589      * at all:
1590      */
1591     if (blk_rq_is_passthrough(req))
1592         return scsi_setup_scsi_cmnd(sdev, req);
1593 
1594     if (sdev->handler && sdev->handler->prep_fn) {
1595         blk_status_t ret = sdev->handler->prep_fn(sdev, req);
1596 
1597         if (ret != BLK_STS_OK)
1598             return ret;
1599     }
1600 
1601     /* Usually overridden by the ULP */
1602     cmd->allowed = 0;
1603     memset(cmd->cmnd, 0, sizeof(cmd->cmnd));
1604     return scsi_cmd_to_driver(cmd)->init_command(cmd);
1605 }
1606 
1607 static void scsi_done_internal(struct scsi_cmnd *cmd, bool complete_directly)
1608 {
1609     struct request *req = scsi_cmd_to_rq(cmd);
1610 
1611     switch (cmd->submitter) {
1612     case SUBMITTED_BY_BLOCK_LAYER:
1613         break;
1614     case SUBMITTED_BY_SCSI_ERROR_HANDLER:
1615         return scsi_eh_done(cmd);
1616     case SUBMITTED_BY_SCSI_RESET_IOCTL:
1617         return;
1618     }
1619 
1620     if (unlikely(blk_should_fake_timeout(scsi_cmd_to_rq(cmd)->q)))
1621         return;
1622     if (unlikely(test_and_set_bit(SCMD_STATE_COMPLETE, &cmd->state)))
1623         return;
1624     trace_scsi_dispatch_cmd_done(cmd);
1625 
1626     if (complete_directly)
1627         blk_mq_complete_request_direct(req, scsi_complete);
1628     else
1629         blk_mq_complete_request(req);
1630 }
1631 
1632 void scsi_done(struct scsi_cmnd *cmd)
1633 {
1634     scsi_done_internal(cmd, false);
1635 }
1636 EXPORT_SYMBOL(scsi_done);
1637 
1638 void scsi_done_direct(struct scsi_cmnd *cmd)
1639 {
1640     scsi_done_internal(cmd, true);
1641 }
1642 EXPORT_SYMBOL(scsi_done_direct);
1643 
1644 static void scsi_mq_put_budget(struct request_queue *q, int budget_token)
1645 {
1646     struct scsi_device *sdev = q->queuedata;
1647 
1648     sbitmap_put(&sdev->budget_map, budget_token);
1649 }
1650 
1651 /*
1652  * When to reinvoke queueing after a resource shortage. It's 3 msecs to
1653  * not change behaviour from the previous unplug mechanism, experimentation
1654  * may prove this needs changing.
1655  */
1656 #define SCSI_QUEUE_DELAY 3
1657 
1658 static int scsi_mq_get_budget(struct request_queue *q)
1659 {
1660     struct scsi_device *sdev = q->queuedata;
1661     int token = scsi_dev_queue_ready(q, sdev);
1662 
1663     if (token >= 0)
1664         return token;
1665 
1666     atomic_inc(&sdev->restarts);
1667 
1668     /*
1669      * Orders atomic_inc(&sdev->restarts) and atomic_read(&sdev->device_busy).
1670      * .restarts must be incremented before .device_busy is read because the
1671      * code in scsi_run_queue_async() depends on the order of these operations.
1672      */
1673     smp_mb__after_atomic();
1674 
1675     /*
1676      * If all in-flight requests originated from this LUN are completed
1677      * before reading .device_busy, sdev->device_busy will be observed as
1678      * zero, then blk_mq_delay_run_hw_queues() will dispatch this request
1679      * soon. Otherwise, completion of one of these requests will observe
1680      * the .restarts flag, and the request queue will be run for handling
1681      * this request, see scsi_end_request().
1682      */
1683     if (unlikely(scsi_device_busy(sdev) == 0 &&
1684                 !scsi_device_blocked(sdev)))
1685         blk_mq_delay_run_hw_queues(sdev->request_queue, SCSI_QUEUE_DELAY);
1686     return -1;
1687 }
1688 
1689 static void scsi_mq_set_rq_budget_token(struct request *req, int token)
1690 {
1691     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1692 
1693     cmd->budget_token = token;
1694 }
1695 
1696 static int scsi_mq_get_rq_budget_token(struct request *req)
1697 {
1698     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1699 
1700     return cmd->budget_token;
1701 }
1702 
1703 static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
1704              const struct blk_mq_queue_data *bd)
1705 {
1706     struct request *req = bd->rq;
1707     struct request_queue *q = req->q;
1708     struct scsi_device *sdev = q->queuedata;
1709     struct Scsi_Host *shost = sdev->host;
1710     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1711     blk_status_t ret;
1712     int reason;
1713 
1714     WARN_ON_ONCE(cmd->budget_token < 0);
1715 
1716     /*
1717      * If the device is not in running state we will reject some or all
1718      * commands.
1719      */
1720     if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
1721         ret = scsi_device_state_check(sdev, req);
1722         if (ret != BLK_STS_OK)
1723             goto out_put_budget;
1724     }
1725 
1726     ret = BLK_STS_RESOURCE;
1727     if (!scsi_target_queue_ready(shost, sdev))
1728         goto out_put_budget;
1729     if (!scsi_host_queue_ready(q, shost, sdev, cmd))
1730         goto out_dec_target_busy;
1731 
1732     if (!(req->rq_flags & RQF_DONTPREP)) {
1733         ret = scsi_prepare_cmd(req);
1734         if (ret != BLK_STS_OK)
1735             goto out_dec_host_busy;
1736         req->rq_flags |= RQF_DONTPREP;
1737     } else {
1738         clear_bit(SCMD_STATE_COMPLETE, &cmd->state);
1739     }
1740 
1741     cmd->flags &= SCMD_PRESERVED_FLAGS;
1742     if (sdev->simple_tags)
1743         cmd->flags |= SCMD_TAGGED;
1744     if (bd->last)
1745         cmd->flags |= SCMD_LAST;
1746 
1747     scsi_set_resid(cmd, 0);
1748     memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1749     cmd->submitter = SUBMITTED_BY_BLOCK_LAYER;
1750 
1751     blk_mq_start_request(req);
1752     reason = scsi_dispatch_cmd(cmd);
1753     if (reason) {
1754         scsi_set_blocked(cmd, reason);
1755         ret = BLK_STS_RESOURCE;
1756         goto out_dec_host_busy;
1757     }
1758 
1759     return BLK_STS_OK;
1760 
1761 out_dec_host_busy:
1762     scsi_dec_host_busy(shost, cmd);
1763 out_dec_target_busy:
1764     if (scsi_target(sdev)->can_queue > 0)
1765         atomic_dec(&scsi_target(sdev)->target_busy);
1766 out_put_budget:
1767     scsi_mq_put_budget(q, cmd->budget_token);
1768     cmd->budget_token = -1;
1769     switch (ret) {
1770     case BLK_STS_OK:
1771         break;
1772     case BLK_STS_RESOURCE:
1773     case BLK_STS_ZONE_RESOURCE:
1774         if (scsi_device_blocked(sdev))
1775             ret = BLK_STS_DEV_RESOURCE;
1776         break;
1777     case BLK_STS_AGAIN:
1778         cmd->result = DID_BUS_BUSY << 16;
1779         if (req->rq_flags & RQF_DONTPREP)
1780             scsi_mq_uninit_cmd(cmd);
1781         break;
1782     default:
1783         if (unlikely(!scsi_device_online(sdev)))
1784             cmd->result = DID_NO_CONNECT << 16;
1785         else
1786             cmd->result = DID_ERROR << 16;
1787         /*
1788          * Make sure to release all allocated resources when
1789          * we hit an error, as we will never see this command
1790          * again.
1791          */
1792         if (req->rq_flags & RQF_DONTPREP)
1793             scsi_mq_uninit_cmd(cmd);
1794         scsi_run_queue_async(sdev);
1795         break;
1796     }
1797     return ret;
1798 }
1799 
1800 static int scsi_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
1801                 unsigned int hctx_idx, unsigned int numa_node)
1802 {
1803     struct Scsi_Host *shost = set->driver_data;
1804     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1805     struct scatterlist *sg;
1806     int ret = 0;
1807 
1808     cmd->sense_buffer =
1809         kmem_cache_alloc_node(scsi_sense_cache, GFP_KERNEL, numa_node);
1810     if (!cmd->sense_buffer)
1811         return -ENOMEM;
1812 
1813     if (scsi_host_get_prot(shost)) {
1814         sg = (void *)cmd + sizeof(struct scsi_cmnd) +
1815             shost->hostt->cmd_size;
1816         cmd->prot_sdb = (void *)sg + scsi_mq_inline_sgl_size(shost);
1817     }
1818 
1819     if (shost->hostt->init_cmd_priv) {
1820         ret = shost->hostt->init_cmd_priv(shost, cmd);
1821         if (ret < 0)
1822             kmem_cache_free(scsi_sense_cache, cmd->sense_buffer);
1823     }
1824 
1825     return ret;
1826 }
1827 
1828 static void scsi_mq_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1829                  unsigned int hctx_idx)
1830 {
1831     struct Scsi_Host *shost = set->driver_data;
1832     struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1833 
1834     if (shost->hostt->exit_cmd_priv)
1835         shost->hostt->exit_cmd_priv(shost, cmd);
1836     kmem_cache_free(scsi_sense_cache, cmd->sense_buffer);
1837 }
1838 
1839 
1840 static int scsi_mq_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
1841 {
1842     struct Scsi_Host *shost = hctx->driver_data;
1843 
1844     if (shost->hostt->mq_poll)
1845         return shost->hostt->mq_poll(shost, hctx->queue_num);
1846 
1847     return 0;
1848 }
1849 
1850 static int scsi_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1851               unsigned int hctx_idx)
1852 {
1853     struct Scsi_Host *shost = data;
1854 
1855     hctx->driver_data = shost;
1856     return 0;
1857 }
1858 
1859 static int scsi_map_queues(struct blk_mq_tag_set *set)
1860 {
1861     struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
1862 
1863     if (shost->hostt->map_queues)
1864         return shost->hostt->map_queues(shost);
1865     return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
1866 }
1867 
1868 void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
1869 {
1870     struct device *dev = shost->dma_dev;
1871 
1872     /*
1873      * this limit is imposed by hardware restrictions
1874      */
1875     blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
1876                     SG_MAX_SEGMENTS));
1877 
1878     if (scsi_host_prot_dma(shost)) {
1879         shost->sg_prot_tablesize =
1880             min_not_zero(shost->sg_prot_tablesize,
1881                      (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
1882         BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
1883         blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
1884     }
1885 
1886     blk_queue_max_hw_sectors(q, shost->max_sectors);
1887     blk_queue_segment_boundary(q, shost->dma_boundary);
1888     dma_set_seg_boundary(dev, shost->dma_boundary);
1889 
1890     blk_queue_max_segment_size(q, shost->max_segment_size);
1891     blk_queue_virt_boundary(q, shost->virt_boundary_mask);
1892     dma_set_max_seg_size(dev, queue_max_segment_size(q));
1893 
1894     /*
1895      * Set a reasonable default alignment:  The larger of 32-byte (dword),
1896      * which is a common minimum for HBAs, and the minimum DMA alignment,
1897      * which is set by the platform.
1898      *
1899      * Devices that require a bigger alignment can increase it later.
1900      */
1901     blk_queue_dma_alignment(q, max(4, dma_get_cache_alignment()) - 1);
1902 }
1903 EXPORT_SYMBOL_GPL(__scsi_init_queue);
1904 
1905 static const struct blk_mq_ops scsi_mq_ops_no_commit = {
1906     .get_budget = scsi_mq_get_budget,
1907     .put_budget = scsi_mq_put_budget,
1908     .queue_rq   = scsi_queue_rq,
1909     .complete   = scsi_complete,
1910     .timeout    = scsi_timeout,
1911 #ifdef CONFIG_BLK_DEBUG_FS
1912     .show_rq    = scsi_show_rq,
1913 #endif
1914     .init_request   = scsi_mq_init_request,
1915     .exit_request   = scsi_mq_exit_request,
1916     .cleanup_rq = scsi_cleanup_rq,
1917     .busy       = scsi_mq_lld_busy,
1918     .map_queues = scsi_map_queues,
1919     .init_hctx  = scsi_init_hctx,
1920     .poll       = scsi_mq_poll,
1921     .set_rq_budget_token = scsi_mq_set_rq_budget_token,
1922     .get_rq_budget_token = scsi_mq_get_rq_budget_token,
1923 };
1924 
1925 
1926 static void scsi_commit_rqs(struct blk_mq_hw_ctx *hctx)
1927 {
1928     struct Scsi_Host *shost = hctx->driver_data;
1929 
1930     shost->hostt->commit_rqs(shost, hctx->queue_num);
1931 }
1932 
1933 static const struct blk_mq_ops scsi_mq_ops = {
1934     .get_budget = scsi_mq_get_budget,
1935     .put_budget = scsi_mq_put_budget,
1936     .queue_rq   = scsi_queue_rq,
1937     .commit_rqs = scsi_commit_rqs,
1938     .complete   = scsi_complete,
1939     .timeout    = scsi_timeout,
1940 #ifdef CONFIG_BLK_DEBUG_FS
1941     .show_rq    = scsi_show_rq,
1942 #endif
1943     .init_request   = scsi_mq_init_request,
1944     .exit_request   = scsi_mq_exit_request,
1945     .cleanup_rq = scsi_cleanup_rq,
1946     .busy       = scsi_mq_lld_busy,
1947     .map_queues = scsi_map_queues,
1948     .init_hctx  = scsi_init_hctx,
1949     .poll       = scsi_mq_poll,
1950     .set_rq_budget_token = scsi_mq_set_rq_budget_token,
1951     .get_rq_budget_token = scsi_mq_get_rq_budget_token,
1952 };
1953 
1954 int scsi_mq_setup_tags(struct Scsi_Host *shost)
1955 {
1956     unsigned int cmd_size, sgl_size;
1957     struct blk_mq_tag_set *tag_set = &shost->tag_set;
1958 
1959     sgl_size = max_t(unsigned int, sizeof(struct scatterlist),
1960                 scsi_mq_inline_sgl_size(shost));
1961     cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
1962     if (scsi_host_get_prot(shost))
1963         cmd_size += sizeof(struct scsi_data_buffer) +
1964             sizeof(struct scatterlist) * SCSI_INLINE_PROT_SG_CNT;
1965 
1966     memset(tag_set, 0, sizeof(*tag_set));
1967     if (shost->hostt->commit_rqs)
1968         tag_set->ops = &scsi_mq_ops;
1969     else
1970         tag_set->ops = &scsi_mq_ops_no_commit;
1971     tag_set->nr_hw_queues = shost->nr_hw_queues ? : 1;
1972     tag_set->nr_maps = shost->nr_maps ? : 1;
1973     tag_set->queue_depth = shost->can_queue;
1974     tag_set->cmd_size = cmd_size;
1975     tag_set->numa_node = dev_to_node(shost->dma_dev);
1976     tag_set->flags = BLK_MQ_F_SHOULD_MERGE;
1977     tag_set->flags |=
1978         BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
1979     tag_set->driver_data = shost;
1980     if (shost->host_tagset)
1981         tag_set->flags |= BLK_MQ_F_TAG_HCTX_SHARED;
1982 
1983     return blk_mq_alloc_tag_set(tag_set);
1984 }
1985 
1986 void scsi_mq_free_tags(struct kref *kref)
1987 {
1988     struct Scsi_Host *shost = container_of(kref, typeof(*shost),
1989                            tagset_refcnt);
1990 
1991     blk_mq_free_tag_set(&shost->tag_set);
1992     complete(&shost->tagset_freed);
1993 }
1994 
1995 /**
1996  * scsi_device_from_queue - return sdev associated with a request_queue
1997  * @q: The request queue to return the sdev from
1998  *
1999  * Return the sdev associated with a request queue or NULL if the
2000  * request_queue does not reference a SCSI device.
2001  */
2002 struct scsi_device *scsi_device_from_queue(struct request_queue *q)
2003 {
2004     struct scsi_device *sdev = NULL;
2005 
2006     if (q->mq_ops == &scsi_mq_ops_no_commit ||
2007         q->mq_ops == &scsi_mq_ops)
2008         sdev = q->queuedata;
2009     if (!sdev || !get_device(&sdev->sdev_gendev))
2010         sdev = NULL;
2011 
2012     return sdev;
2013 }
2014 /*
2015  * pktcdvd should have been integrated into the SCSI layers, but for historical
2016  * reasons like the old IDE driver it isn't.  This export allows it to safely
2017  * probe if a given device is a SCSI one and only attach to that.
2018  */
2019 #ifdef CONFIG_CDROM_PKTCDVD_MODULE
2020 EXPORT_SYMBOL_GPL(scsi_device_from_queue);
2021 #endif
2022 
2023 /**
2024  * scsi_block_requests - Utility function used by low-level drivers to prevent
2025  * further commands from being queued to the device.
2026  * @shost:  host in question
2027  *
2028  * There is no timer nor any other means by which the requests get unblocked
2029  * other than the low-level driver calling scsi_unblock_requests().
2030  */
2031 void scsi_block_requests(struct Scsi_Host *shost)
2032 {
2033     shost->host_self_blocked = 1;
2034 }
2035 EXPORT_SYMBOL(scsi_block_requests);
2036 
2037 /**
2038  * scsi_unblock_requests - Utility function used by low-level drivers to allow
2039  * further commands to be queued to the device.
2040  * @shost:  host in question
2041  *
2042  * There is no timer nor any other means by which the requests get unblocked
2043  * other than the low-level driver calling scsi_unblock_requests(). This is done
2044  * as an API function so that changes to the internals of the scsi mid-layer
2045  * won't require wholesale changes to drivers that use this feature.
2046  */
2047 void scsi_unblock_requests(struct Scsi_Host *shost)
2048 {
2049     shost->host_self_blocked = 0;
2050     scsi_run_host_queues(shost);
2051 }
2052 EXPORT_SYMBOL(scsi_unblock_requests);
2053 
2054 void scsi_exit_queue(void)
2055 {
2056     kmem_cache_destroy(scsi_sense_cache);
2057 }
2058 
2059 /**
2060  *  scsi_mode_select - issue a mode select
2061  *  @sdev:  SCSI device to be queried
2062  *  @pf:    Page format bit (1 == standard, 0 == vendor specific)
2063  *  @sp:    Save page bit (0 == don't save, 1 == save)
2064  *  @buffer: request buffer (may not be smaller than eight bytes)
2065  *  @len:   length of request buffer.
2066  *  @timeout: command timeout
2067  *  @retries: number of retries before failing
2068  *  @data: returns a structure abstracting the mode header data
2069  *  @sshdr: place to put sense data (or NULL if no sense to be collected).
2070  *      must be SCSI_SENSE_BUFFERSIZE big.
2071  *
2072  *  Returns zero if successful; negative error number or scsi
2073  *  status on error
2074  *
2075  */
2076 int scsi_mode_select(struct scsi_device *sdev, int pf, int sp,
2077              unsigned char *buffer, int len, int timeout, int retries,
2078              struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2079 {
2080     unsigned char cmd[10];
2081     unsigned char *real_buffer;
2082     int ret;
2083 
2084     memset(cmd, 0, sizeof(cmd));
2085     cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2086 
2087     /*
2088      * Use MODE SELECT(10) if the device asked for it or if the mode page
2089      * and the mode select header cannot fit within the maximumm 255 bytes
2090      * of the MODE SELECT(6) command.
2091      */
2092     if (sdev->use_10_for_ms ||
2093         len + 4 > 255 ||
2094         data->block_descriptor_length > 255) {
2095         if (len > 65535 - 8)
2096             return -EINVAL;
2097         real_buffer = kmalloc(8 + len, GFP_KERNEL);
2098         if (!real_buffer)
2099             return -ENOMEM;
2100         memcpy(real_buffer + 8, buffer, len);
2101         len += 8;
2102         real_buffer[0] = 0;
2103         real_buffer[1] = 0;
2104         real_buffer[2] = data->medium_type;
2105         real_buffer[3] = data->device_specific;
2106         real_buffer[4] = data->longlba ? 0x01 : 0;
2107         real_buffer[5] = 0;
2108         put_unaligned_be16(data->block_descriptor_length,
2109                    &real_buffer[6]);
2110 
2111         cmd[0] = MODE_SELECT_10;
2112         put_unaligned_be16(len, &cmd[7]);
2113     } else {
2114         if (data->longlba)
2115             return -EINVAL;
2116 
2117         real_buffer = kmalloc(4 + len, GFP_KERNEL);
2118         if (!real_buffer)
2119             return -ENOMEM;
2120         memcpy(real_buffer + 4, buffer, len);
2121         len += 4;
2122         real_buffer[0] = 0;
2123         real_buffer[1] = data->medium_type;
2124         real_buffer[2] = data->device_specific;
2125         real_buffer[3] = data->block_descriptor_length;
2126 
2127         cmd[0] = MODE_SELECT;
2128         cmd[4] = len;
2129     }
2130 
2131     ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
2132                    sshdr, timeout, retries, NULL);
2133     kfree(real_buffer);
2134     return ret;
2135 }
2136 EXPORT_SYMBOL_GPL(scsi_mode_select);
2137 
2138 /**
2139  *  scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
2140  *  @sdev:  SCSI device to be queried
2141  *  @dbd:   set to prevent mode sense from returning block descriptors
2142  *  @modepage: mode page being requested
2143  *  @buffer: request buffer (may not be smaller than eight bytes)
2144  *  @len:   length of request buffer.
2145  *  @timeout: command timeout
2146  *  @retries: number of retries before failing
2147  *  @data: returns a structure abstracting the mode header data
2148  *  @sshdr: place to put sense data (or NULL if no sense to be collected).
2149  *      must be SCSI_SENSE_BUFFERSIZE big.
2150  *
2151  *  Returns zero if successful, or a negative error number on failure
2152  */
2153 int
2154 scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
2155           unsigned char *buffer, int len, int timeout, int retries,
2156           struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2157 {
2158     unsigned char cmd[12];
2159     int use_10_for_ms;
2160     int header_length;
2161     int result, retry_count = retries;
2162     struct scsi_sense_hdr my_sshdr;
2163 
2164     memset(data, 0, sizeof(*data));
2165     memset(&cmd[0], 0, 12);
2166 
2167     dbd = sdev->set_dbd_for_ms ? 8 : dbd;
2168     cmd[1] = dbd & 0x18;    /* allows DBD and LLBA bits */
2169     cmd[2] = modepage;
2170 
2171     /* caller might not be interested in sense, but we need it */
2172     if (!sshdr)
2173         sshdr = &my_sshdr;
2174 
2175  retry:
2176     use_10_for_ms = sdev->use_10_for_ms || len > 255;
2177 
2178     if (use_10_for_ms) {
2179         if (len < 8 || len > 65535)
2180             return -EINVAL;
2181 
2182         cmd[0] = MODE_SENSE_10;
2183         put_unaligned_be16(len, &cmd[7]);
2184         header_length = 8;
2185     } else {
2186         if (len < 4)
2187             return -EINVAL;
2188 
2189         cmd[0] = MODE_SENSE;
2190         cmd[4] = len;
2191         header_length = 4;
2192     }
2193 
2194     memset(buffer, 0, len);
2195 
2196     result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
2197                   sshdr, timeout, retries, NULL);
2198     if (result < 0)
2199         return result;
2200 
2201     /* This code looks awful: what it's doing is making sure an
2202      * ILLEGAL REQUEST sense return identifies the actual command
2203      * byte as the problem.  MODE_SENSE commands can return
2204      * ILLEGAL REQUEST if the code page isn't supported */
2205 
2206     if (!scsi_status_is_good(result)) {
2207         if (scsi_sense_valid(sshdr)) {
2208             if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2209                 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
2210                 /*
2211                  * Invalid command operation code: retry using
2212                  * MODE SENSE(6) if this was a MODE SENSE(10)
2213                  * request, except if the request mode page is
2214                  * too large for MODE SENSE single byte
2215                  * allocation length field.
2216                  */
2217                 if (use_10_for_ms) {
2218                     if (len > 255)
2219                         return -EIO;
2220                     sdev->use_10_for_ms = 0;
2221                     goto retry;
2222                 }
2223             }
2224             if (scsi_status_is_check_condition(result) &&
2225                 sshdr->sense_key == UNIT_ATTENTION &&
2226                 retry_count) {
2227                 retry_count--;
2228                 goto retry;
2229             }
2230         }
2231         return -EIO;
2232     }
2233     if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2234              (modepage == 6 || modepage == 8))) {
2235         /* Initio breakage? */
2236         header_length = 0;
2237         data->length = 13;
2238         data->medium_type = 0;
2239         data->device_specific = 0;
2240         data->longlba = 0;
2241         data->block_descriptor_length = 0;
2242     } else if (use_10_for_ms) {
2243         data->length = get_unaligned_be16(&buffer[0]) + 2;
2244         data->medium_type = buffer[2];
2245         data->device_specific = buffer[3];
2246         data->longlba = buffer[4] & 0x01;
2247         data->block_descriptor_length = get_unaligned_be16(&buffer[6]);
2248     } else {
2249         data->length = buffer[0] + 1;
2250         data->medium_type = buffer[1];
2251         data->device_specific = buffer[2];
2252         data->block_descriptor_length = buffer[3];
2253     }
2254     data->header_length = header_length;
2255 
2256     return 0;
2257 }
2258 EXPORT_SYMBOL(scsi_mode_sense);
2259 
2260 /**
2261  *  scsi_test_unit_ready - test if unit is ready
2262  *  @sdev:  scsi device to change the state of.
2263  *  @timeout: command timeout
2264  *  @retries: number of retries before failing
2265  *  @sshdr: outpout pointer for decoded sense information.
2266  *
2267  *  Returns zero if unsuccessful or an error if TUR failed.  For
2268  *  removable media, UNIT_ATTENTION sets ->changed flag.
2269  **/
2270 int
2271 scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
2272              struct scsi_sense_hdr *sshdr)
2273 {
2274     char cmd[] = {
2275         TEST_UNIT_READY, 0, 0, 0, 0, 0,
2276     };
2277     int result;
2278 
2279     /* try to eat the UNIT_ATTENTION if there are enough retries */
2280     do {
2281         result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
2282                       timeout, 1, NULL);
2283         if (sdev->removable && scsi_sense_valid(sshdr) &&
2284             sshdr->sense_key == UNIT_ATTENTION)
2285             sdev->changed = 1;
2286     } while (scsi_sense_valid(sshdr) &&
2287          sshdr->sense_key == UNIT_ATTENTION && --retries);
2288 
2289     return result;
2290 }
2291 EXPORT_SYMBOL(scsi_test_unit_ready);
2292 
2293 /**
2294  *  scsi_device_set_state - Take the given device through the device state model.
2295  *  @sdev:  scsi device to change the state of.
2296  *  @state: state to change to.
2297  *
2298  *  Returns zero if successful or an error if the requested
2299  *  transition is illegal.
2300  */
2301 int
2302 scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2303 {
2304     enum scsi_device_state oldstate = sdev->sdev_state;
2305 
2306     if (state == oldstate)
2307         return 0;
2308 
2309     switch (state) {
2310     case SDEV_CREATED:
2311         switch (oldstate) {
2312         case SDEV_CREATED_BLOCK:
2313             break;
2314         default:
2315             goto illegal;
2316         }
2317         break;
2318 
2319     case SDEV_RUNNING:
2320         switch (oldstate) {
2321         case SDEV_CREATED:
2322         case SDEV_OFFLINE:
2323         case SDEV_TRANSPORT_OFFLINE:
2324         case SDEV_QUIESCE:
2325         case SDEV_BLOCK:
2326             break;
2327         default:
2328             goto illegal;
2329         }
2330         break;
2331 
2332     case SDEV_QUIESCE:
2333         switch (oldstate) {
2334         case SDEV_RUNNING:
2335         case SDEV_OFFLINE:
2336         case SDEV_TRANSPORT_OFFLINE:
2337             break;
2338         default:
2339             goto illegal;
2340         }
2341         break;
2342 
2343     case SDEV_OFFLINE:
2344     case SDEV_TRANSPORT_OFFLINE:
2345         switch (oldstate) {
2346         case SDEV_CREATED:
2347         case SDEV_RUNNING:
2348         case SDEV_QUIESCE:
2349         case SDEV_BLOCK:
2350             break;
2351         default:
2352             goto illegal;
2353         }
2354         break;
2355 
2356     case SDEV_BLOCK:
2357         switch (oldstate) {
2358         case SDEV_RUNNING:
2359         case SDEV_CREATED_BLOCK:
2360         case SDEV_QUIESCE:
2361         case SDEV_OFFLINE:
2362             break;
2363         default:
2364             goto illegal;
2365         }
2366         break;
2367 
2368     case SDEV_CREATED_BLOCK:
2369         switch (oldstate) {
2370         case SDEV_CREATED:
2371             break;
2372         default:
2373             goto illegal;
2374         }
2375         break;
2376 
2377     case SDEV_CANCEL:
2378         switch (oldstate) {
2379         case SDEV_CREATED:
2380         case SDEV_RUNNING:
2381         case SDEV_QUIESCE:
2382         case SDEV_OFFLINE:
2383         case SDEV_TRANSPORT_OFFLINE:
2384             break;
2385         default:
2386             goto illegal;
2387         }
2388         break;
2389 
2390     case SDEV_DEL:
2391         switch (oldstate) {
2392         case SDEV_CREATED:
2393         case SDEV_RUNNING:
2394         case SDEV_OFFLINE:
2395         case SDEV_TRANSPORT_OFFLINE:
2396         case SDEV_CANCEL:
2397         case SDEV_BLOCK:
2398         case SDEV_CREATED_BLOCK:
2399             break;
2400         default:
2401             goto illegal;
2402         }
2403         break;
2404 
2405     }
2406     sdev->offline_already = false;
2407     sdev->sdev_state = state;
2408     return 0;
2409 
2410  illegal:
2411     SCSI_LOG_ERROR_RECOVERY(1,
2412                 sdev_printk(KERN_ERR, sdev,
2413                         "Illegal state transition %s->%s",
2414                         scsi_device_state_name(oldstate),
2415                         scsi_device_state_name(state))
2416                 );
2417     return -EINVAL;
2418 }
2419 EXPORT_SYMBOL(scsi_device_set_state);
2420 
2421 /**
2422  *  scsi_evt_emit - emit a single SCSI device uevent
2423  *  @sdev: associated SCSI device
2424  *  @evt: event to emit
2425  *
2426  *  Send a single uevent (scsi_event) to the associated scsi_device.
2427  */
2428 static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2429 {
2430     int idx = 0;
2431     char *envp[3];
2432 
2433     switch (evt->evt_type) {
2434     case SDEV_EVT_MEDIA_CHANGE:
2435         envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2436         break;
2437     case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2438         scsi_rescan_device(&sdev->sdev_gendev);
2439         envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2440         break;
2441     case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2442         envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2443         break;
2444     case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2445            envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2446         break;
2447     case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2448         envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2449         break;
2450     case SDEV_EVT_LUN_CHANGE_REPORTED:
2451         envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2452         break;
2453     case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2454         envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
2455         break;
2456     case SDEV_EVT_POWER_ON_RESET_OCCURRED:
2457         envp[idx++] = "SDEV_UA=POWER_ON_RESET_OCCURRED";
2458         break;
2459     default:
2460         /* do nothing */
2461         break;
2462     }
2463 
2464     envp[idx++] = NULL;
2465 
2466     kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2467 }
2468 
2469 /**
2470  *  scsi_evt_thread - send a uevent for each scsi event
2471  *  @work: work struct for scsi_device
2472  *
2473  *  Dispatch queued events to their associated scsi_device kobjects
2474  *  as uevents.
2475  */
2476 void scsi_evt_thread(struct work_struct *work)
2477 {
2478     struct scsi_device *sdev;
2479     enum scsi_device_event evt_type;
2480     LIST_HEAD(event_list);
2481 
2482     sdev = container_of(work, struct scsi_device, event_work);
2483 
2484     for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2485         if (test_and_clear_bit(evt_type, sdev->pending_events))
2486             sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2487 
2488     while (1) {
2489         struct scsi_event *evt;
2490         struct list_head *this, *tmp;
2491         unsigned long flags;
2492 
2493         spin_lock_irqsave(&sdev->list_lock, flags);
2494         list_splice_init(&sdev->event_list, &event_list);
2495         spin_unlock_irqrestore(&sdev->list_lock, flags);
2496 
2497         if (list_empty(&event_list))
2498             break;
2499 
2500         list_for_each_safe(this, tmp, &event_list) {
2501             evt = list_entry(this, struct scsi_event, node);
2502             list_del(&evt->node);
2503             scsi_evt_emit(sdev, evt);
2504             kfree(evt);
2505         }
2506     }
2507 }
2508 
2509 /**
2510  *  sdev_evt_send - send asserted event to uevent thread
2511  *  @sdev: scsi_device event occurred on
2512  *  @evt: event to send
2513  *
2514  *  Assert scsi device event asynchronously.
2515  */
2516 void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2517 {
2518     unsigned long flags;
2519 
2520 #if 0
2521     /* FIXME: currently this check eliminates all media change events
2522      * for polled devices.  Need to update to discriminate between AN
2523      * and polled events */
2524     if (!test_bit(evt->evt_type, sdev->supported_events)) {
2525         kfree(evt);
2526         return;
2527     }
2528 #endif
2529 
2530     spin_lock_irqsave(&sdev->list_lock, flags);
2531     list_add_tail(&evt->node, &sdev->event_list);
2532     schedule_work(&sdev->event_work);
2533     spin_unlock_irqrestore(&sdev->list_lock, flags);
2534 }
2535 EXPORT_SYMBOL_GPL(sdev_evt_send);
2536 
2537 /**
2538  *  sdev_evt_alloc - allocate a new scsi event
2539  *  @evt_type: type of event to allocate
2540  *  @gfpflags: GFP flags for allocation
2541  *
2542  *  Allocates and returns a new scsi_event.
2543  */
2544 struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2545                   gfp_t gfpflags)
2546 {
2547     struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2548     if (!evt)
2549         return NULL;
2550 
2551     evt->evt_type = evt_type;
2552     INIT_LIST_HEAD(&evt->node);
2553 
2554     /* evt_type-specific initialization, if any */
2555     switch (evt_type) {
2556     case SDEV_EVT_MEDIA_CHANGE:
2557     case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2558     case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2559     case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2560     case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2561     case SDEV_EVT_LUN_CHANGE_REPORTED:
2562     case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2563     case SDEV_EVT_POWER_ON_RESET_OCCURRED:
2564     default:
2565         /* do nothing */
2566         break;
2567     }
2568 
2569     return evt;
2570 }
2571 EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2572 
2573 /**
2574  *  sdev_evt_send_simple - send asserted event to uevent thread
2575  *  @sdev: scsi_device event occurred on
2576  *  @evt_type: type of event to send
2577  *  @gfpflags: GFP flags for allocation
2578  *
2579  *  Assert scsi device event asynchronously, given an event type.
2580  */
2581 void sdev_evt_send_simple(struct scsi_device *sdev,
2582               enum scsi_device_event evt_type, gfp_t gfpflags)
2583 {
2584     struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2585     if (!evt) {
2586         sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2587                 evt_type);
2588         return;
2589     }
2590 
2591     sdev_evt_send(sdev, evt);
2592 }
2593 EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2594 
2595 /**
2596  *  scsi_device_quiesce - Block all commands except power management.
2597  *  @sdev:  scsi device to quiesce.
2598  *
2599  *  This works by trying to transition to the SDEV_QUIESCE state
2600  *  (which must be a legal transition).  When the device is in this
2601  *  state, only power management requests will be accepted, all others will
2602  *  be deferred.
2603  *
2604  *  Must be called with user context, may sleep.
2605  *
2606  *  Returns zero if unsuccessful or an error if not.
2607  */
2608 int
2609 scsi_device_quiesce(struct scsi_device *sdev)
2610 {
2611     struct request_queue *q = sdev->request_queue;
2612     int err;
2613 
2614     /*
2615      * It is allowed to call scsi_device_quiesce() multiple times from
2616      * the same context but concurrent scsi_device_quiesce() calls are
2617      * not allowed.
2618      */
2619     WARN_ON_ONCE(sdev->quiesced_by && sdev->quiesced_by != current);
2620 
2621     if (sdev->quiesced_by == current)
2622         return 0;
2623 
2624     blk_set_pm_only(q);
2625 
2626     blk_mq_freeze_queue(q);
2627     /*
2628      * Ensure that the effect of blk_set_pm_only() will be visible
2629      * for percpu_ref_tryget() callers that occur after the queue
2630      * unfreeze even if the queue was already frozen before this function
2631      * was called. See also https://lwn.net/Articles/573497/.
2632      */
2633     synchronize_rcu();
2634     blk_mq_unfreeze_queue(q);
2635 
2636     mutex_lock(&sdev->state_mutex);
2637     err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2638     if (err == 0)
2639         sdev->quiesced_by = current;
2640     else
2641         blk_clear_pm_only(q);
2642     mutex_unlock(&sdev->state_mutex);
2643 
2644     return err;
2645 }
2646 EXPORT_SYMBOL(scsi_device_quiesce);
2647 
2648 /**
2649  *  scsi_device_resume - Restart user issued commands to a quiesced device.
2650  *  @sdev:  scsi device to resume.
2651  *
2652  *  Moves the device from quiesced back to running and restarts the
2653  *  queues.
2654  *
2655  *  Must be called with user context, may sleep.
2656  */
2657 void scsi_device_resume(struct scsi_device *sdev)
2658 {
2659     /* check if the device state was mutated prior to resume, and if
2660      * so assume the state is being managed elsewhere (for example
2661      * device deleted during suspend)
2662      */
2663     mutex_lock(&sdev->state_mutex);
2664     if (sdev->sdev_state == SDEV_QUIESCE)
2665         scsi_device_set_state(sdev, SDEV_RUNNING);
2666     if (sdev->quiesced_by) {
2667         sdev->quiesced_by = NULL;
2668         blk_clear_pm_only(sdev->request_queue);
2669     }
2670     mutex_unlock(&sdev->state_mutex);
2671 }
2672 EXPORT_SYMBOL(scsi_device_resume);
2673 
2674 static void
2675 device_quiesce_fn(struct scsi_device *sdev, void *data)
2676 {
2677     scsi_device_quiesce(sdev);
2678 }
2679 
2680 void
2681 scsi_target_quiesce(struct scsi_target *starget)
2682 {
2683     starget_for_each_device(starget, NULL, device_quiesce_fn);
2684 }
2685 EXPORT_SYMBOL(scsi_target_quiesce);
2686 
2687 static void
2688 device_resume_fn(struct scsi_device *sdev, void *data)
2689 {
2690     scsi_device_resume(sdev);
2691 }
2692 
2693 void
2694 scsi_target_resume(struct scsi_target *starget)
2695 {
2696     starget_for_each_device(starget, NULL, device_resume_fn);
2697 }
2698 EXPORT_SYMBOL(scsi_target_resume);
2699 
2700 static int __scsi_internal_device_block_nowait(struct scsi_device *sdev)
2701 {
2702     if (scsi_device_set_state(sdev, SDEV_BLOCK))
2703         return scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2704 
2705     return 0;
2706 }
2707 
2708 void scsi_start_queue(struct scsi_device *sdev)
2709 {
2710     if (cmpxchg(&sdev->queue_stopped, 1, 0))
2711         blk_mq_unquiesce_queue(sdev->request_queue);
2712 }
2713 
2714 static void scsi_stop_queue(struct scsi_device *sdev, bool nowait)
2715 {
2716     /*
2717      * The atomic variable of ->queue_stopped covers that
2718      * blk_mq_quiesce_queue* is balanced with blk_mq_unquiesce_queue.
2719      *
2720      * However, we still need to wait until quiesce is done
2721      * in case that queue has been stopped.
2722      */
2723     if (!cmpxchg(&sdev->queue_stopped, 0, 1)) {
2724         if (nowait)
2725             blk_mq_quiesce_queue_nowait(sdev->request_queue);
2726         else
2727             blk_mq_quiesce_queue(sdev->request_queue);
2728     } else {
2729         if (!nowait)
2730             blk_mq_wait_quiesce_done(sdev->request_queue);
2731     }
2732 }
2733 
2734 /**
2735  * scsi_internal_device_block_nowait - try to transition to the SDEV_BLOCK state
2736  * @sdev: device to block
2737  *
2738  * Pause SCSI command processing on the specified device. Does not sleep.
2739  *
2740  * Returns zero if successful or a negative error code upon failure.
2741  *
2742  * Notes:
2743  * This routine transitions the device to the SDEV_BLOCK state (which must be
2744  * a legal transition). When the device is in this state, command processing
2745  * is paused until the device leaves the SDEV_BLOCK state. See also
2746  * scsi_internal_device_unblock_nowait().
2747  */
2748 int scsi_internal_device_block_nowait(struct scsi_device *sdev)
2749 {
2750     int ret = __scsi_internal_device_block_nowait(sdev);
2751 
2752     /*
2753      * The device has transitioned to SDEV_BLOCK.  Stop the
2754      * block layer from calling the midlayer with this device's
2755      * request queue.
2756      */
2757     if (!ret)
2758         scsi_stop_queue(sdev, true);
2759     return ret;
2760 }
2761 EXPORT_SYMBOL_GPL(scsi_internal_device_block_nowait);
2762 
2763 /**
2764  * scsi_internal_device_block - try to transition to the SDEV_BLOCK state
2765  * @sdev: device to block
2766  *
2767  * Pause SCSI command processing on the specified device and wait until all
2768  * ongoing scsi_request_fn() / scsi_queue_rq() calls have finished. May sleep.
2769  *
2770  * Returns zero if successful or a negative error code upon failure.
2771  *
2772  * Note:
2773  * This routine transitions the device to the SDEV_BLOCK state (which must be
2774  * a legal transition). When the device is in this state, command processing
2775  * is paused until the device leaves the SDEV_BLOCK state. See also
2776  * scsi_internal_device_unblock().
2777  */
2778 static int scsi_internal_device_block(struct scsi_device *sdev)
2779 {
2780     int err;
2781 
2782     mutex_lock(&sdev->state_mutex);
2783     err = __scsi_internal_device_block_nowait(sdev);
2784     if (err == 0)
2785         scsi_stop_queue(sdev, false);
2786     mutex_unlock(&sdev->state_mutex);
2787 
2788     return err;
2789 }
2790 
2791 /**
2792  * scsi_internal_device_unblock_nowait - resume a device after a block request
2793  * @sdev:   device to resume
2794  * @new_state:  state to set the device to after unblocking
2795  *
2796  * Restart the device queue for a previously suspended SCSI device. Does not
2797  * sleep.
2798  *
2799  * Returns zero if successful or a negative error code upon failure.
2800  *
2801  * Notes:
2802  * This routine transitions the device to the SDEV_RUNNING state or to one of
2803  * the offline states (which must be a legal transition) allowing the midlayer
2804  * to goose the queue for this device.
2805  */
2806 int scsi_internal_device_unblock_nowait(struct scsi_device *sdev,
2807                     enum scsi_device_state new_state)
2808 {
2809     switch (new_state) {
2810     case SDEV_RUNNING:
2811     case SDEV_TRANSPORT_OFFLINE:
2812         break;
2813     default:
2814         return -EINVAL;
2815     }
2816 
2817     /*
2818      * Try to transition the scsi device to SDEV_RUNNING or one of the
2819      * offlined states and goose the device queue if successful.
2820      */
2821     switch (sdev->sdev_state) {
2822     case SDEV_BLOCK:
2823     case SDEV_TRANSPORT_OFFLINE:
2824         sdev->sdev_state = new_state;
2825         break;
2826     case SDEV_CREATED_BLOCK:
2827         if (new_state == SDEV_TRANSPORT_OFFLINE ||
2828             new_state == SDEV_OFFLINE)
2829             sdev->sdev_state = new_state;
2830         else
2831             sdev->sdev_state = SDEV_CREATED;
2832         break;
2833     case SDEV_CANCEL:
2834     case SDEV_OFFLINE:
2835         break;
2836     default:
2837         return -EINVAL;
2838     }
2839     scsi_start_queue(sdev);
2840 
2841     return 0;
2842 }
2843 EXPORT_SYMBOL_GPL(scsi_internal_device_unblock_nowait);
2844 
2845 /**
2846  * scsi_internal_device_unblock - resume a device after a block request
2847  * @sdev:   device to resume
2848  * @new_state:  state to set the device to after unblocking
2849  *
2850  * Restart the device queue for a previously suspended SCSI device. May sleep.
2851  *
2852  * Returns zero if successful or a negative error code upon failure.
2853  *
2854  * Notes:
2855  * This routine transitions the device to the SDEV_RUNNING state or to one of
2856  * the offline states (which must be a legal transition) allowing the midlayer
2857  * to goose the queue for this device.
2858  */
2859 static int scsi_internal_device_unblock(struct scsi_device *sdev,
2860                     enum scsi_device_state new_state)
2861 {
2862     int ret;
2863 
2864     mutex_lock(&sdev->state_mutex);
2865     ret = scsi_internal_device_unblock_nowait(sdev, new_state);
2866     mutex_unlock(&sdev->state_mutex);
2867 
2868     return ret;
2869 }
2870 
2871 static void
2872 device_block(struct scsi_device *sdev, void *data)
2873 {
2874     int ret;
2875 
2876     ret = scsi_internal_device_block(sdev);
2877 
2878     WARN_ONCE(ret, "scsi_internal_device_block(%s) failed: ret = %d\n",
2879           dev_name(&sdev->sdev_gendev), ret);
2880 }
2881 
2882 static int
2883 target_block(struct device *dev, void *data)
2884 {
2885     if (scsi_is_target_device(dev))
2886         starget_for_each_device(to_scsi_target(dev), NULL,
2887                     device_block);
2888     return 0;
2889 }
2890 
2891 void
2892 scsi_target_block(struct device *dev)
2893 {
2894     if (scsi_is_target_device(dev))
2895         starget_for_each_device(to_scsi_target(dev), NULL,
2896                     device_block);
2897     else
2898         device_for_each_child(dev, NULL, target_block);
2899 }
2900 EXPORT_SYMBOL_GPL(scsi_target_block);
2901 
2902 static void
2903 device_unblock(struct scsi_device *sdev, void *data)
2904 {
2905     scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
2906 }
2907 
2908 static int
2909 target_unblock(struct device *dev, void *data)
2910 {
2911     if (scsi_is_target_device(dev))
2912         starget_for_each_device(to_scsi_target(dev), data,
2913                     device_unblock);
2914     return 0;
2915 }
2916 
2917 void
2918 scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
2919 {
2920     if (scsi_is_target_device(dev))
2921         starget_for_each_device(to_scsi_target(dev), &new_state,
2922                     device_unblock);
2923     else
2924         device_for_each_child(dev, &new_state, target_unblock);
2925 }
2926 EXPORT_SYMBOL_GPL(scsi_target_unblock);
2927 
2928 int
2929 scsi_host_block(struct Scsi_Host *shost)
2930 {
2931     struct scsi_device *sdev;
2932     int ret = 0;
2933 
2934     /*
2935      * Call scsi_internal_device_block_nowait so we can avoid
2936      * calling synchronize_rcu() for each LUN.
2937      */
2938     shost_for_each_device(sdev, shost) {
2939         mutex_lock(&sdev->state_mutex);
2940         ret = scsi_internal_device_block_nowait(sdev);
2941         mutex_unlock(&sdev->state_mutex);
2942         if (ret) {
2943             scsi_device_put(sdev);
2944             break;
2945         }
2946     }
2947 
2948     /*
2949      * SCSI never enables blk-mq's BLK_MQ_F_BLOCKING flag so
2950      * calling synchronize_rcu() once is enough.
2951      */
2952     WARN_ON_ONCE(shost->tag_set.flags & BLK_MQ_F_BLOCKING);
2953 
2954     if (!ret)
2955         synchronize_rcu();
2956 
2957     return ret;
2958 }
2959 EXPORT_SYMBOL_GPL(scsi_host_block);
2960 
2961 int
2962 scsi_host_unblock(struct Scsi_Host *shost, int new_state)
2963 {
2964     struct scsi_device *sdev;
2965     int ret = 0;
2966 
2967     shost_for_each_device(sdev, shost) {
2968         ret = scsi_internal_device_unblock(sdev, new_state);
2969         if (ret) {
2970             scsi_device_put(sdev);
2971             break;
2972         }
2973     }
2974     return ret;
2975 }
2976 EXPORT_SYMBOL_GPL(scsi_host_unblock);
2977 
2978 /**
2979  * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
2980  * @sgl:    scatter-gather list
2981  * @sg_count:   number of segments in sg
2982  * @offset: offset in bytes into sg, on return offset into the mapped area
2983  * @len:    bytes to map, on return number of bytes mapped
2984  *
2985  * Returns virtual address of the start of the mapped page
2986  */
2987 void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
2988               size_t *offset, size_t *len)
2989 {
2990     int i;
2991     size_t sg_len = 0, len_complete = 0;
2992     struct scatterlist *sg;
2993     struct page *page;
2994 
2995     WARN_ON(!irqs_disabled());
2996 
2997     for_each_sg(sgl, sg, sg_count, i) {
2998         len_complete = sg_len; /* Complete sg-entries */
2999         sg_len += sg->length;
3000         if (sg_len > *offset)
3001             break;
3002     }
3003 
3004     if (unlikely(i == sg_count)) {
3005         printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
3006             "elements %d\n",
3007                __func__, sg_len, *offset, sg_count);
3008         WARN_ON(1);
3009         return NULL;
3010     }
3011 
3012     /* Offset starting from the beginning of first page in this sg-entry */
3013     *offset = *offset - len_complete + sg->offset;
3014 
3015     /* Assumption: contiguous pages can be accessed as "page + i" */
3016     page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
3017     *offset &= ~PAGE_MASK;
3018 
3019     /* Bytes in this sg-entry from *offset to the end of the page */
3020     sg_len = PAGE_SIZE - *offset;
3021     if (*len > sg_len)
3022         *len = sg_len;
3023 
3024     return kmap_atomic(page);
3025 }
3026 EXPORT_SYMBOL(scsi_kmap_atomic_sg);
3027 
3028 /**
3029  * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
3030  * @virt:   virtual address to be unmapped
3031  */
3032 void scsi_kunmap_atomic_sg(void *virt)
3033 {
3034     kunmap_atomic(virt);
3035 }
3036 EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
3037 
3038 void sdev_disable_disk_events(struct scsi_device *sdev)
3039 {
3040     atomic_inc(&sdev->disk_events_disable_depth);
3041 }
3042 EXPORT_SYMBOL(sdev_disable_disk_events);
3043 
3044 void sdev_enable_disk_events(struct scsi_device *sdev)
3045 {
3046     if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
3047         return;
3048     atomic_dec(&sdev->disk_events_disable_depth);
3049 }
3050 EXPORT_SYMBOL(sdev_enable_disk_events);
3051 
3052 static unsigned char designator_prio(const unsigned char *d)
3053 {
3054     if (d[1] & 0x30)
3055         /* not associated with LUN */
3056         return 0;
3057 
3058     if (d[3] == 0)
3059         /* invalid length */
3060         return 0;
3061 
3062     /*
3063      * Order of preference for lun descriptor:
3064      * - SCSI name string
3065      * - NAA IEEE Registered Extended
3066      * - EUI-64 based 16-byte
3067      * - EUI-64 based 12-byte
3068      * - NAA IEEE Registered
3069      * - NAA IEEE Extended
3070      * - EUI-64 based 8-byte
3071      * - SCSI name string (truncated)
3072      * - T10 Vendor ID
3073      * as longer descriptors reduce the likelyhood
3074      * of identification clashes.
3075      */
3076 
3077     switch (d[1] & 0xf) {
3078     case 8:
3079         /* SCSI name string, variable-length UTF-8 */
3080         return 9;
3081     case 3:
3082         switch (d[4] >> 4) {
3083         case 6:
3084             /* NAA registered extended */
3085             return 8;
3086         case 5:
3087             /* NAA registered */
3088             return 5;
3089         case 4:
3090             /* NAA extended */
3091             return 4;
3092         case 3:
3093             /* NAA locally assigned */
3094             return 1;
3095         default:
3096             break;
3097         }
3098         break;
3099     case 2:
3100         switch (d[3]) {
3101         case 16:
3102             /* EUI64-based, 16 byte */
3103             return 7;
3104         case 12:
3105             /* EUI64-based, 12 byte */
3106             return 6;
3107         case 8:
3108             /* EUI64-based, 8 byte */
3109             return 3;
3110         default:
3111             break;
3112         }
3113         break;
3114     case 1:
3115         /* T10 vendor ID */
3116         return 1;
3117     default:
3118         break;
3119     }
3120 
3121     return 0;
3122 }
3123 
3124 /**
3125  * scsi_vpd_lun_id - return a unique device identification
3126  * @sdev: SCSI device
3127  * @id:   buffer for the identification
3128  * @id_len:  length of the buffer
3129  *
3130  * Copies a unique device identification into @id based
3131  * on the information in the VPD page 0x83 of the device.
3132  * The string will be formatted as a SCSI name string.
3133  *
3134  * Returns the length of the identification or error on failure.
3135  * If the identifier is longer than the supplied buffer the actual
3136  * identifier length is returned and the buffer is not zero-padded.
3137  */
3138 int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
3139 {
3140     u8 cur_id_prio = 0;
3141     u8 cur_id_size = 0;
3142     const unsigned char *d, *cur_id_str;
3143     const struct scsi_vpd *vpd_pg83;
3144     int id_size = -EINVAL;
3145 
3146     rcu_read_lock();
3147     vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3148     if (!vpd_pg83) {
3149         rcu_read_unlock();
3150         return -ENXIO;
3151     }
3152 
3153     /* The id string must be at least 20 bytes + terminating NULL byte */
3154     if (id_len < 21) {
3155         rcu_read_unlock();
3156         return -EINVAL;
3157     }
3158 
3159     memset(id, 0, id_len);
3160     for (d = vpd_pg83->data + 4;
3161          d < vpd_pg83->data + vpd_pg83->len;
3162          d += d[3] + 4) {
3163         u8 prio = designator_prio(d);
3164 
3165         if (prio == 0 || cur_id_prio > prio)
3166             continue;
3167 
3168         switch (d[1] & 0xf) {
3169         case 0x1:
3170             /* T10 Vendor ID */
3171             if (cur_id_size > d[3])
3172                 break;
3173             cur_id_prio = prio;
3174             cur_id_size = d[3];
3175             if (cur_id_size + 4 > id_len)
3176                 cur_id_size = id_len - 4;
3177             cur_id_str = d + 4;
3178             id_size = snprintf(id, id_len, "t10.%*pE",
3179                        cur_id_size, cur_id_str);
3180             break;
3181         case 0x2:
3182             /* EUI-64 */
3183             cur_id_prio = prio;
3184             cur_id_size = d[3];
3185             cur_id_str = d + 4;
3186             switch (cur_id_size) {
3187             case 8:
3188                 id_size = snprintf(id, id_len,
3189                            "eui.%8phN",
3190                            cur_id_str);
3191                 break;
3192             case 12:
3193                 id_size = snprintf(id, id_len,
3194                            "eui.%12phN",
3195                            cur_id_str);
3196                 break;
3197             case 16:
3198                 id_size = snprintf(id, id_len,
3199                            "eui.%16phN",
3200                            cur_id_str);
3201                 break;
3202             default:
3203                 break;
3204             }
3205             break;
3206         case 0x3:
3207             /* NAA */
3208             cur_id_prio = prio;
3209             cur_id_size = d[3];
3210             cur_id_str = d + 4;
3211             switch (cur_id_size) {
3212             case 8:
3213                 id_size = snprintf(id, id_len,
3214                            "naa.%8phN",
3215                            cur_id_str);
3216                 break;
3217             case 16:
3218                 id_size = snprintf(id, id_len,
3219                            "naa.%16phN",
3220                            cur_id_str);
3221                 break;
3222             default:
3223                 break;
3224             }
3225             break;
3226         case 0x8:
3227             /* SCSI name string */
3228             if (cur_id_size > d[3])
3229                 break;
3230             /* Prefer others for truncated descriptor */
3231             if (d[3] > id_len) {
3232                 prio = 2;
3233                 if (cur_id_prio > prio)
3234                     break;
3235             }
3236             cur_id_prio = prio;
3237             cur_id_size = id_size = d[3];
3238             cur_id_str = d + 4;
3239             if (cur_id_size >= id_len)
3240                 cur_id_size = id_len - 1;
3241             memcpy(id, cur_id_str, cur_id_size);
3242             break;
3243         default:
3244             break;
3245         }
3246     }
3247     rcu_read_unlock();
3248 
3249     return id_size;
3250 }
3251 EXPORT_SYMBOL(scsi_vpd_lun_id);
3252 
3253 /*
3254  * scsi_vpd_tpg_id - return a target port group identifier
3255  * @sdev: SCSI device
3256  *
3257  * Returns the Target Port Group identifier from the information
3258  * froom VPD page 0x83 of the device.
3259  *
3260  * Returns the identifier or error on failure.
3261  */
3262 int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
3263 {
3264     const unsigned char *d;
3265     const struct scsi_vpd *vpd_pg83;
3266     int group_id = -EAGAIN, rel_port = -1;
3267 
3268     rcu_read_lock();
3269     vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3270     if (!vpd_pg83) {
3271         rcu_read_unlock();
3272         return -ENXIO;
3273     }
3274 
3275     d = vpd_pg83->data + 4;
3276     while (d < vpd_pg83->data + vpd_pg83->len) {
3277         switch (d[1] & 0xf) {
3278         case 0x4:
3279             /* Relative target port */
3280             rel_port = get_unaligned_be16(&d[6]);
3281             break;
3282         case 0x5:
3283             /* Target port group */
3284             group_id = get_unaligned_be16(&d[6]);
3285             break;
3286         default:
3287             break;
3288         }
3289         d += d[3] + 4;
3290     }
3291     rcu_read_unlock();
3292 
3293     if (group_id >= 0 && rel_id && rel_port != -1)
3294         *rel_id = rel_port;
3295 
3296     return group_id;
3297 }
3298 EXPORT_SYMBOL(scsi_vpd_tpg_id);
3299 
3300 /**
3301  * scsi_build_sense - build sense data for a command
3302  * @scmd:   scsi command for which the sense should be formatted
3303  * @desc:   Sense format (non-zero == descriptor format,
3304  *              0 == fixed format)
3305  * @key:    Sense key
3306  * @asc:    Additional sense code
3307  * @ascq:   Additional sense code qualifier
3308  *
3309  **/
3310 void scsi_build_sense(struct scsi_cmnd *scmd, int desc, u8 key, u8 asc, u8 ascq)
3311 {
3312     scsi_build_sense_buffer(desc, scmd->sense_buffer, key, asc, ascq);
3313     scmd->result = SAM_STAT_CHECK_CONDITION;
3314 }
3315 EXPORT_SYMBOL_GPL(scsi_build_sense);