LCOV - code coverage report
Current view: top level - drivers/scsi - scsi_lib.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 3 958 0.3 %
Date: 2020-09-30 20:25:40 Functions: 1 91 1.1 %
Branches: 1 674 0.1 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-only
       2                 :            : /*
       3                 :            :  * Copyright (C) 1999 Eric Youngdale
       4                 :            :  * Copyright (C) 2014 Christoph Hellwig
       5                 :            :  *
       6                 :            :  *  SCSI queueing library.
       7                 :            :  *      Initial versions: Eric Youngdale (eric@andante.org).
       8                 :            :  *                        Based upon conversations with large numbers
       9                 :            :  *                        of people at Linux Expo.
      10                 :            :  */
      11                 :            : 
      12                 :            : #include <linux/bio.h>
      13                 :            : #include <linux/bitops.h>
      14                 :            : #include <linux/blkdev.h>
      15                 :            : #include <linux/completion.h>
      16                 :            : #include <linux/kernel.h>
      17                 :            : #include <linux/export.h>
      18                 :            : #include <linux/init.h>
      19                 :            : #include <linux/pci.h>
      20                 :            : #include <linux/delay.h>
      21                 :            : #include <linux/hardirq.h>
      22                 :            : #include <linux/scatterlist.h>
      23                 :            : #include <linux/blk-mq.h>
      24                 :            : #include <linux/ratelimit.h>
      25                 :            : #include <asm/unaligned.h>
      26                 :            : 
      27                 :            : #include <scsi/scsi.h>
      28                 :            : #include <scsi/scsi_cmnd.h>
      29                 :            : #include <scsi/scsi_dbg.h>
      30                 :            : #include <scsi/scsi_device.h>
      31                 :            : #include <scsi/scsi_driver.h>
      32                 :            : #include <scsi/scsi_eh.h>
      33                 :            : #include <scsi/scsi_host.h>
      34                 :            : #include <scsi/scsi_transport.h> /* __scsi_init_queue() */
      35                 :            : #include <scsi/scsi_dh.h>
      36                 :            : 
      37                 :            : #include <trace/events/scsi.h>
      38                 :            : 
      39                 :            : #include "scsi_debugfs.h"
      40                 :            : #include "scsi_priv.h"
      41                 :            : #include "scsi_logging.h"
      42                 :            : 
      43                 :            : /*
      44                 :            :  * Size of integrity metadata is usually small, 1 inline sg should
      45                 :            :  * cover normal cases.
      46                 :            :  */
      47                 :            : #ifdef CONFIG_ARCH_NO_SG_CHAIN
      48                 :            : #define  SCSI_INLINE_PROT_SG_CNT  0
      49                 :            : #define  SCSI_INLINE_SG_CNT  0
      50                 :            : #else
      51                 :            : #define  SCSI_INLINE_PROT_SG_CNT  1
      52                 :            : #define  SCSI_INLINE_SG_CNT  2
      53                 :            : #endif
      54                 :            : 
      55                 :            : static struct kmem_cache *scsi_sdb_cache;
      56                 :            : static struct kmem_cache *scsi_sense_cache;
      57                 :            : static struct kmem_cache *scsi_sense_isadma_cache;
      58                 :            : static DEFINE_MUTEX(scsi_sense_cache_mutex);
      59                 :            : 
      60                 :            : static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd);
      61                 :            : 
      62                 :            : static inline struct kmem_cache *
      63                 :            : scsi_select_sense_cache(bool unchecked_isa_dma)
      64                 :            : {
      65   [ #  #  #  #  :          0 :         return unchecked_isa_dma ? scsi_sense_isadma_cache : scsi_sense_cache;
                   #  # ]
      66                 :            : }
      67                 :            : 
      68                 :            : static void scsi_free_sense_buffer(bool unchecked_isa_dma,
      69                 :            :                                    unsigned char *sense_buffer)
      70                 :            : {
      71                 :          0 :         kmem_cache_free(scsi_select_sense_cache(unchecked_isa_dma),
      72                 :            :                         sense_buffer);
      73                 :            : }
      74                 :            : 
      75                 :            : static unsigned char *scsi_alloc_sense_buffer(bool unchecked_isa_dma,
      76                 :            :         gfp_t gfp_mask, int numa_node)
      77                 :            : {
      78                 :            :         return kmem_cache_alloc_node(scsi_select_sense_cache(unchecked_isa_dma),
      79                 :            :                                      gfp_mask, numa_node);
      80                 :            : }
      81                 :            : 
      82                 :          0 : int scsi_init_sense_cache(struct Scsi_Host *shost)
      83                 :            : {
      84                 :            :         struct kmem_cache *cache;
      85                 :            :         int ret = 0;
      86                 :            : 
      87                 :          0 :         mutex_lock(&scsi_sense_cache_mutex);
      88                 :          0 :         cache = scsi_select_sense_cache(shost->unchecked_isa_dma);
      89         [ #  # ]:          0 :         if (cache)
      90                 :            :                 goto exit;
      91                 :            : 
      92         [ #  # ]:          0 :         if (shost->unchecked_isa_dma) {
      93                 :          0 :                 scsi_sense_isadma_cache =
      94                 :          0 :                         kmem_cache_create("scsi_sense_cache(DMA)",
      95                 :            :                                 SCSI_SENSE_BUFFERSIZE, 0,
      96                 :            :                                 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL);
      97         [ #  # ]:          0 :                 if (!scsi_sense_isadma_cache)
      98                 :            :                         ret = -ENOMEM;
      99                 :            :         } else {
     100                 :          0 :                 scsi_sense_cache =
     101                 :          0 :                         kmem_cache_create_usercopy("scsi_sense_cache",
     102                 :            :                                 SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN,
     103                 :            :                                 0, SCSI_SENSE_BUFFERSIZE, NULL);
     104         [ #  # ]:          0 :                 if (!scsi_sense_cache)
     105                 :            :                         ret = -ENOMEM;
     106                 :            :         }
     107                 :            :  exit:
     108                 :          0 :         mutex_unlock(&scsi_sense_cache_mutex);
     109                 :          0 :         return ret;
     110                 :            : }
     111                 :            : 
     112                 :            : /*
     113                 :            :  * When to reinvoke queueing after a resource shortage. It's 3 msecs to
     114                 :            :  * not change behaviour from the previous unplug mechanism, experimentation
     115                 :            :  * may prove this needs changing.
     116                 :            :  */
     117                 :            : #define SCSI_QUEUE_DELAY        3
     118                 :            : 
     119                 :            : static void
     120                 :          0 : scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
     121                 :            : {
     122                 :          0 :         struct Scsi_Host *host = cmd->device->host;
     123                 :            :         struct scsi_device *device = cmd->device;
     124                 :            :         struct scsi_target *starget = scsi_target(device);
     125                 :            : 
     126                 :            :         /*
     127                 :            :          * Set the appropriate busy bit for the device/host.
     128                 :            :          *
     129                 :            :          * If the host/device isn't busy, assume that something actually
     130                 :            :          * completed, and that we should be able to queue a command now.
     131                 :            :          *
     132                 :            :          * Note that the prior mid-layer assumption that any host could
     133                 :            :          * always queue at least one command is now broken.  The mid-layer
     134                 :            :          * will implement a user specifiable stall (see
     135                 :            :          * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
     136                 :            :          * if a command is requeued with no other commands outstanding
     137                 :            :          * either for the device or for the host.
     138                 :            :          */
     139   [ #  #  #  # ]:          0 :         switch (reason) {
     140                 :            :         case SCSI_MLQUEUE_HOST_BUSY:
     141                 :          0 :                 atomic_set(&host->host_blocked, host->max_host_blocked);
     142                 :          0 :                 break;
     143                 :            :         case SCSI_MLQUEUE_DEVICE_BUSY:
     144                 :            :         case SCSI_MLQUEUE_EH_RETRY:
     145                 :          0 :                 atomic_set(&device->device_blocked,
     146                 :            :                            device->max_device_blocked);
     147                 :          0 :                 break;
     148                 :            :         case SCSI_MLQUEUE_TARGET_BUSY:
     149                 :          0 :                 atomic_set(&starget->target_blocked,
     150                 :            :                            starget->max_target_blocked);
     151                 :          0 :                 break;
     152                 :            :         }
     153                 :          0 : }
     154                 :            : 
     155                 :          0 : static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd)
     156                 :            : {
     157         [ #  # ]:          0 :         if (cmd->request->rq_flags & RQF_DONTPREP) {
     158                 :          0 :                 cmd->request->rq_flags &= ~RQF_DONTPREP;
     159                 :          0 :                 scsi_mq_uninit_cmd(cmd);
     160                 :            :         } else {
     161         [ #  # ]:          0 :                 WARN_ON_ONCE(true);
     162                 :            :         }
     163                 :          0 :         blk_mq_requeue_request(cmd->request, true);
     164                 :          0 : }
     165                 :            : 
     166                 :            : /**
     167                 :            :  * __scsi_queue_insert - private queue insertion
     168                 :            :  * @cmd: The SCSI command being requeued
     169                 :            :  * @reason:  The reason for the requeue
     170                 :            :  * @unbusy: Whether the queue should be unbusied
     171                 :            :  *
     172                 :            :  * This is a private queue insertion.  The public interface
     173                 :            :  * scsi_queue_insert() always assumes the queue should be unbusied
     174                 :            :  * because it's always called before the completion.  This function is
     175                 :            :  * for a requeue after completion, which should only occur in this
     176                 :            :  * file.
     177                 :            :  */
     178                 :          0 : static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, bool unbusy)
     179                 :            : {
     180                 :          0 :         struct scsi_device *device = cmd->device;
     181                 :            : 
     182                 :            :         SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
     183                 :            :                 "Inserting command %p into mlqueue\n", cmd));
     184                 :            : 
     185                 :          0 :         scsi_set_blocked(cmd, reason);
     186                 :            : 
     187                 :            :         /*
     188                 :            :          * Decrement the counters, since these commands are no longer
     189                 :            :          * active on the host/device.
     190                 :            :          */
     191         [ #  # ]:          0 :         if (unbusy)
     192                 :          0 :                 scsi_device_unbusy(device);
     193                 :            : 
     194                 :            :         /*
     195                 :            :          * Requeue this command.  It will go before all other commands
     196                 :            :          * that are already in the queue. Schedule requeue work under
     197                 :            :          * lock such that the kblockd_schedule_work() call happens
     198                 :            :          * before blk_cleanup_queue() finishes.
     199                 :            :          */
     200                 :          0 :         cmd->result = 0;
     201                 :            : 
     202                 :          0 :         blk_mq_requeue_request(cmd->request, true);
     203                 :          0 : }
     204                 :            : 
     205                 :            : /*
     206                 :            :  * Function:    scsi_queue_insert()
     207                 :            :  *
     208                 :            :  * Purpose:     Insert a command in the midlevel queue.
     209                 :            :  *
     210                 :            :  * Arguments:   cmd    - command that we are adding to queue.
     211                 :            :  *              reason - why we are inserting command to queue.
     212                 :            :  *
     213                 :            :  * Lock status: Assumed that lock is not held upon entry.
     214                 :            :  *
     215                 :            :  * Returns:     Nothing.
     216                 :            :  *
     217                 :            :  * Notes:       We do this for one of two cases.  Either the host is busy
     218                 :            :  *              and it cannot accept any more commands for the time being,
     219                 :            :  *              or the device returned QUEUE_FULL and can accept no more
     220                 :            :  *              commands.
     221                 :            :  * Notes:       This could be called either from an interrupt context or a
     222                 :            :  *              normal process context.
     223                 :            :  */
     224                 :          0 : void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
     225                 :            : {
     226                 :          0 :         __scsi_queue_insert(cmd, reason, true);
     227                 :          0 : }
     228                 :            : 
     229                 :            : 
     230                 :            : /**
     231                 :            :  * __scsi_execute - insert request and wait for the result
     232                 :            :  * @sdev:       scsi device
     233                 :            :  * @cmd:        scsi command
     234                 :            :  * @data_direction: data direction
     235                 :            :  * @buffer:     data buffer
     236                 :            :  * @bufflen:    len of buffer
     237                 :            :  * @sense:      optional sense buffer
     238                 :            :  * @sshdr:      optional decoded sense header
     239                 :            :  * @timeout:    request timeout in seconds
     240                 :            :  * @retries:    number of times to retry request
     241                 :            :  * @flags:      flags for ->cmd_flags
     242                 :            :  * @rq_flags:   flags for ->rq_flags
     243                 :            :  * @resid:      optional residual length
     244                 :            :  *
     245                 :            :  * Returns the scsi_cmnd result field if a command was executed, or a negative
     246                 :            :  * Linux error code if we didn't get that far.
     247                 :            :  */
     248                 :          0 : int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
     249                 :            :                  int data_direction, void *buffer, unsigned bufflen,
     250                 :            :                  unsigned char *sense, struct scsi_sense_hdr *sshdr,
     251                 :            :                  int timeout, int retries, u64 flags, req_flags_t rq_flags,
     252                 :            :                  int *resid)
     253                 :            : {
     254                 :            :         struct request *req;
     255                 :            :         struct scsi_request *rq;
     256                 :            :         int ret = DRIVER_ERROR << 24;
     257                 :            : 
     258         [ #  # ]:          0 :         req = blk_get_request(sdev->request_queue,
     259                 :            :                         data_direction == DMA_TO_DEVICE ?
     260                 :            :                         REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, BLK_MQ_REQ_PREEMPT);
     261         [ #  # ]:          0 :         if (IS_ERR(req))
     262                 :            :                 return ret;
     263                 :            :         rq = scsi_req(req);
     264                 :            : 
     265   [ #  #  #  # ]:          0 :         if (bufflen &&  blk_rq_map_kern(sdev->request_queue, req,
     266                 :            :                                         buffer, bufflen, GFP_NOIO))
     267                 :            :                 goto out;
     268                 :            : 
     269                 :          0 :         rq->cmd_len = COMMAND_SIZE(cmd[0]);
     270                 :          0 :         memcpy(rq->cmd, cmd, rq->cmd_len);
     271                 :          0 :         rq->retries = retries;
     272                 :          0 :         req->timeout = timeout;
     273                 :          0 :         req->cmd_flags |= flags;
     274                 :          0 :         req->rq_flags |= rq_flags | RQF_QUIET;
     275                 :            : 
     276                 :            :         /*
     277                 :            :          * head injection *required* here otherwise quiesce won't work
     278                 :            :          */
     279                 :          0 :         blk_execute_rq(req->q, NULL, req, 1);
     280                 :            : 
     281                 :            :         /*
     282                 :            :          * Some devices (USB mass-storage in particular) may transfer
     283                 :            :          * garbage data together with a residue indicating that the data
     284                 :            :          * is invalid.  Prevent the garbage from being misinterpreted
     285                 :            :          * and prevent security leaks by zeroing out the excess data.
     286                 :            :          */
     287   [ #  #  #  # ]:          0 :         if (unlikely(rq->resid_len > 0 && rq->resid_len <= bufflen))
     288                 :          0 :                 memset(buffer + (bufflen - rq->resid_len), 0, rq->resid_len);
     289                 :            : 
     290         [ #  # ]:          0 :         if (resid)
     291                 :          0 :                 *resid = rq->resid_len;
     292   [ #  #  #  # ]:          0 :         if (sense && rq->sense_len)
     293                 :          0 :                 memcpy(sense, rq->sense, SCSI_SENSE_BUFFERSIZE);
     294         [ #  # ]:          0 :         if (sshdr)
     295                 :          0 :                 scsi_normalize_sense(rq->sense, rq->sense_len, sshdr);
     296                 :          0 :         ret = rq->result;
     297                 :            :  out:
     298                 :          0 :         blk_put_request(req);
     299                 :            : 
     300                 :          0 :         return ret;
     301                 :            : }
     302                 :            : EXPORT_SYMBOL(__scsi_execute);
     303                 :            : 
     304                 :            : /*
     305                 :            :  * Function:    scsi_init_cmd_errh()
     306                 :            :  *
     307                 :            :  * Purpose:     Initialize cmd fields related to error handling.
     308                 :            :  *
     309                 :            :  * Arguments:   cmd     - command that is ready to be queued.
     310                 :            :  *
     311                 :            :  * Notes:       This function has the job of initializing a number of
     312                 :            :  *              fields related to error handling.   Typically this will
     313                 :            :  *              be called once for each command, as required.
     314                 :            :  */
     315                 :          0 : static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
     316                 :            : {
     317                 :            :         scsi_set_resid(cmd, 0);
     318                 :          0 :         memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
     319         [ #  # ]:          0 :         if (cmd->cmd_len == 0)
     320                 :          0 :                 cmd->cmd_len = scsi_command_size(cmd->cmnd);
     321                 :          0 : }
     322                 :            : 
     323                 :            : /*
     324                 :            :  * Decrement the host_busy counter and wake up the error handler if necessary.
     325                 :            :  * Avoid as follows that the error handler is not woken up if shost->host_busy
     326                 :            :  * == shost->host_failed: use call_rcu() in scsi_eh_scmd_add() in combination
     327                 :            :  * with an RCU read lock in this function to ensure that this function in its
     328                 :            :  * entirety either finishes before scsi_eh_scmd_add() increases the
     329                 :            :  * host_failed counter or that it notices the shost state change made by
     330                 :            :  * scsi_eh_scmd_add().
     331                 :            :  */
     332                 :          0 : static void scsi_dec_host_busy(struct Scsi_Host *shost)
     333                 :            : {
     334                 :            :         unsigned long flags;
     335                 :            : 
     336                 :            :         rcu_read_lock();
     337                 :          0 :         atomic_dec(&shost->host_busy);
     338         [ #  # ]:          0 :         if (unlikely(scsi_host_in_recovery(shost))) {
     339                 :          0 :                 spin_lock_irqsave(shost->host_lock, flags);
     340   [ #  #  #  # ]:          0 :                 if (shost->host_failed || shost->host_eh_scheduled)
     341                 :          0 :                         scsi_eh_wakeup(shost);
     342                 :          0 :                 spin_unlock_irqrestore(shost->host_lock, flags);
     343                 :            :         }
     344                 :            :         rcu_read_unlock();
     345                 :          0 : }
     346                 :            : 
     347                 :          0 : void scsi_device_unbusy(struct scsi_device *sdev)
     348                 :            : {
     349                 :          0 :         struct Scsi_Host *shost = sdev->host;
     350                 :            :         struct scsi_target *starget = scsi_target(sdev);
     351                 :            : 
     352                 :          0 :         scsi_dec_host_busy(shost);
     353                 :            : 
     354         [ #  # ]:          0 :         if (starget->can_queue > 0)
     355                 :          0 :                 atomic_dec(&starget->target_busy);
     356                 :            : 
     357                 :          0 :         atomic_dec(&sdev->device_busy);
     358                 :          0 : }
     359                 :            : 
     360                 :            : static void scsi_kick_queue(struct request_queue *q)
     361                 :            : {
     362                 :          0 :         blk_mq_run_hw_queues(q, false);
     363                 :            : }
     364                 :            : 
     365                 :            : /*
     366                 :            :  * Called for single_lun devices on IO completion. Clear starget_sdev_user,
     367                 :            :  * and call blk_run_queue for all the scsi_devices on the target -
     368                 :            :  * including current_sdev first.
     369                 :            :  *
     370                 :            :  * Called with *no* scsi locks held.
     371                 :            :  */
     372                 :          0 : static void scsi_single_lun_run(struct scsi_device *current_sdev)
     373                 :            : {
     374                 :          0 :         struct Scsi_Host *shost = current_sdev->host;
     375                 :            :         struct scsi_device *sdev, *tmp;
     376                 :            :         struct scsi_target *starget = scsi_target(current_sdev);
     377                 :            :         unsigned long flags;
     378                 :            : 
     379                 :          0 :         spin_lock_irqsave(shost->host_lock, flags);
     380                 :          0 :         starget->starget_sdev_user = NULL;
     381                 :          0 :         spin_unlock_irqrestore(shost->host_lock, flags);
     382                 :            : 
     383                 :            :         /*
     384                 :            :          * Call blk_run_queue for all LUNs on the target, starting with
     385                 :            :          * current_sdev. We race with others (to set starget_sdev_user),
     386                 :            :          * but in most cases, we will be first. Ideally, each LU on the
     387                 :            :          * target would get some limited time or requests on the target.
     388                 :            :          */
     389                 :          0 :         scsi_kick_queue(current_sdev->request_queue);
     390                 :            : 
     391                 :          0 :         spin_lock_irqsave(shost->host_lock, flags);
     392         [ #  # ]:          0 :         if (starget->starget_sdev_user)
     393                 :            :                 goto out;
     394         [ #  # ]:          0 :         list_for_each_entry_safe(sdev, tmp, &starget->devices,
     395                 :            :                         same_target_siblings) {
     396         [ #  # ]:          0 :                 if (sdev == current_sdev)
     397                 :          0 :                         continue;
     398         [ #  # ]:          0 :                 if (scsi_device_get(sdev))
     399                 :          0 :                         continue;
     400                 :            : 
     401                 :          0 :                 spin_unlock_irqrestore(shost->host_lock, flags);
     402                 :          0 :                 scsi_kick_queue(sdev->request_queue);
     403                 :          0 :                 spin_lock_irqsave(shost->host_lock, flags);
     404                 :            :         
     405                 :          0 :                 scsi_device_put(sdev);
     406                 :            :         }
     407                 :            :  out:
     408                 :          0 :         spin_unlock_irqrestore(shost->host_lock, flags);
     409                 :          0 : }
     410                 :            : 
     411                 :            : static inline bool scsi_device_is_busy(struct scsi_device *sdev)
     412                 :            : {
     413         [ #  # ]:          0 :         if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
     414                 :            :                 return true;
     415         [ #  # ]:          0 :         if (atomic_read(&sdev->device_blocked) > 0)
     416                 :            :                 return true;
     417                 :            :         return false;
     418                 :            : }
     419                 :            : 
     420                 :            : static inline bool scsi_target_is_busy(struct scsi_target *starget)
     421                 :            : {
     422         [ #  # ]:          0 :         if (starget->can_queue > 0) {
     423         [ #  # ]:          0 :                 if (atomic_read(&starget->target_busy) >= starget->can_queue)
     424                 :            :                         return true;
     425         [ #  # ]:          0 :                 if (atomic_read(&starget->target_blocked) > 0)
     426                 :            :                         return true;
     427                 :            :         }
     428                 :            :         return false;
     429                 :            : }
     430                 :            : 
     431                 :            : static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
     432                 :            : {
     433   [ #  #  #  # ]:          0 :         if (shost->can_queue > 0 &&
     434                 :          0 :             atomic_read(&shost->host_busy) >= shost->can_queue)
     435                 :            :                 return true;
     436         [ #  # ]:          0 :         if (atomic_read(&shost->host_blocked) > 0)
     437                 :            :                 return true;
     438         [ #  # ]:          0 :         if (shost->host_self_blocked)
     439                 :            :                 return true;
     440                 :            :         return false;
     441                 :            : }
     442                 :            : 
     443                 :          0 : static void scsi_starved_list_run(struct Scsi_Host *shost)
     444                 :            : {
     445                 :          0 :         LIST_HEAD(starved_list);
     446                 :            :         struct scsi_device *sdev;
     447                 :            :         unsigned long flags;
     448                 :            : 
     449                 :          0 :         spin_lock_irqsave(shost->host_lock, flags);
     450                 :          0 :         list_splice_init(&shost->starved_list, &starved_list);
     451                 :            : 
     452         [ #  # ]:          0 :         while (!list_empty(&starved_list)) {
     453                 :            :                 struct request_queue *slq;
     454                 :            : 
     455                 :            :                 /*
     456                 :            :                  * As long as shost is accepting commands and we have
     457                 :            :                  * starved queues, call blk_run_queue. scsi_request_fn
     458                 :            :                  * drops the queue_lock and can add us back to the
     459                 :            :                  * starved_list.
     460                 :            :                  *
     461                 :            :                  * host_lock protects the starved_list and starved_entry.
     462                 :            :                  * scsi_request_fn must get the host_lock before checking
     463                 :            :                  * or modifying starved_list or starved_entry.
     464                 :            :                  */
     465         [ #  # ]:          0 :                 if (scsi_host_is_busy(shost))
     466                 :            :                         break;
     467                 :            : 
     468                 :          0 :                 sdev = list_entry(starved_list.next,
     469                 :            :                                   struct scsi_device, starved_entry);
     470                 :          0 :                 list_del_init(&sdev->starved_entry);
     471         [ #  # ]:          0 :                 if (scsi_target_is_busy(scsi_target(sdev))) {
     472                 :            :                         list_move_tail(&sdev->starved_entry,
     473                 :            :                                        &shost->starved_list);
     474                 :          0 :                         continue;
     475                 :            :                 }
     476                 :            : 
     477                 :            :                 /*
     478                 :            :                  * Once we drop the host lock, a racing scsi_remove_device()
     479                 :            :                  * call may remove the sdev from the starved list and destroy
     480                 :            :                  * it and the queue.  Mitigate by taking a reference to the
     481                 :            :                  * queue and never touching the sdev again after we drop the
     482                 :            :                  * host lock.  Note: if __scsi_remove_device() invokes
     483                 :            :                  * blk_cleanup_queue() before the queue is run from this
     484                 :            :                  * function then blk_run_queue() will return immediately since
     485                 :            :                  * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
     486                 :            :                  */
     487                 :          0 :                 slq = sdev->request_queue;
     488         [ #  # ]:          0 :                 if (!blk_get_queue(slq))
     489                 :          0 :                         continue;
     490                 :          0 :                 spin_unlock_irqrestore(shost->host_lock, flags);
     491                 :            : 
     492                 :            :                 scsi_kick_queue(slq);
     493                 :          0 :                 blk_put_queue(slq);
     494                 :            : 
     495                 :          0 :                 spin_lock_irqsave(shost->host_lock, flags);
     496                 :            :         }
     497                 :            :         /* put any unprocessed entries back */
     498                 :            :         list_splice(&starved_list, &shost->starved_list);
     499                 :          0 :         spin_unlock_irqrestore(shost->host_lock, flags);
     500                 :          0 : }
     501                 :            : 
     502                 :            : /*
     503                 :            :  * Function:   scsi_run_queue()
     504                 :            :  *
     505                 :            :  * Purpose:    Select a proper request queue to serve next
     506                 :            :  *
     507                 :            :  * Arguments:  q       - last request's queue
     508                 :            :  *
     509                 :            :  * Returns:     Nothing
     510                 :            :  *
     511                 :            :  * Notes:      The previous command was completely finished, start
     512                 :            :  *             a new one if possible.
     513                 :            :  */
     514                 :          0 : static void scsi_run_queue(struct request_queue *q)
     515                 :            : {
     516                 :          0 :         struct scsi_device *sdev = q->queuedata;
     517                 :            : 
     518         [ #  # ]:          0 :         if (scsi_target(sdev)->single_lun)
     519                 :          0 :                 scsi_single_lun_run(sdev);
     520         [ #  # ]:          0 :         if (!list_empty(&sdev->host->starved_list))
     521                 :          0 :                 scsi_starved_list_run(sdev->host);
     522                 :            : 
     523                 :          0 :         blk_mq_run_hw_queues(q, false);
     524                 :          0 : }
     525                 :            : 
     526                 :          0 : void scsi_requeue_run_queue(struct work_struct *work)
     527                 :            : {
     528                 :            :         struct scsi_device *sdev;
     529                 :            :         struct request_queue *q;
     530                 :            : 
     531                 :            :         sdev = container_of(work, struct scsi_device, requeue_work);
     532                 :          0 :         q = sdev->request_queue;
     533                 :          0 :         scsi_run_queue(q);
     534                 :          0 : }
     535                 :            : 
     536                 :          0 : void scsi_run_host_queues(struct Scsi_Host *shost)
     537                 :            : {
     538                 :            :         struct scsi_device *sdev;
     539                 :            : 
     540         [ #  # ]:          0 :         shost_for_each_device(sdev, shost)
     541                 :          0 :                 scsi_run_queue(sdev->request_queue);
     542                 :          0 : }
     543                 :            : 
     544                 :          0 : static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
     545                 :            : {
     546         [ #  # ]:          0 :         if (!blk_rq_is_passthrough(cmd->request)) {
     547                 :            :                 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
     548                 :            : 
     549         [ #  # ]:          0 :                 if (drv->uninit_command)
     550                 :          0 :                         drv->uninit_command(cmd);
     551                 :            :         }
     552                 :          0 : }
     553                 :            : 
     554                 :          0 : static void scsi_free_sgtables(struct scsi_cmnd *cmd)
     555                 :            : {
     556         [ #  # ]:          0 :         if (cmd->sdb.table.nents)
     557                 :          0 :                 sg_free_table_chained(&cmd->sdb.table,
     558                 :            :                                 SCSI_INLINE_SG_CNT);
     559         [ #  # ]:          0 :         if (scsi_prot_sg_count(cmd))
     560                 :          0 :                 sg_free_table_chained(&cmd->prot_sdb->table,
     561                 :            :                                 SCSI_INLINE_PROT_SG_CNT);
     562                 :          0 : }
     563                 :            : 
     564                 :          0 : static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
     565                 :            : {
     566                 :          0 :         scsi_free_sgtables(cmd);
     567                 :          0 :         scsi_uninit_cmd(cmd);
     568                 :          0 :         scsi_del_cmd_from_list(cmd);
     569                 :          0 : }
     570                 :            : 
     571                 :          0 : static void scsi_run_queue_async(struct scsi_device *sdev)
     572                 :            : {
     573   [ #  #  #  # ]:          0 :         if (scsi_target(sdev)->single_lun ||
     574                 :          0 :             !list_empty(&sdev->host->starved_list))
     575                 :          0 :                 kblockd_schedule_work(&sdev->requeue_work);
     576                 :            :         else
     577                 :          0 :                 blk_mq_run_hw_queues(sdev->request_queue, true);
     578                 :          0 : }
     579                 :            : 
     580                 :            : /* Returns false when no more bytes to process, true if there are more */
     581                 :          0 : static bool scsi_end_request(struct request *req, blk_status_t error,
     582                 :            :                 unsigned int bytes)
     583                 :            : {
     584                 :            :         struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
     585                 :          0 :         struct scsi_device *sdev = cmd->device;
     586                 :          0 :         struct request_queue *q = sdev->request_queue;
     587                 :            : 
     588         [ #  # ]:          0 :         if (blk_update_request(req, error, bytes))
     589                 :            :                 return true;
     590                 :            : 
     591         [ #  # ]:          0 :         if (blk_queue_add_random(q))
     592                 :          0 :                 add_disk_randomness(req->rq_disk);
     593                 :            : 
     594         [ #  # ]:          0 :         if (!blk_rq_is_scsi(req)) {
     595   [ #  #  #  # ]:          0 :                 WARN_ON_ONCE(!(cmd->flags & SCMD_INITIALIZED));
     596                 :          0 :                 cmd->flags &= ~SCMD_INITIALIZED;
     597                 :            :         }
     598                 :            : 
     599                 :            :         /*
     600                 :            :          * Calling rcu_barrier() is not necessary here because the
     601                 :            :          * SCSI error handler guarantees that the function called by
     602                 :            :          * call_rcu() has been called before scsi_end_request() is
     603                 :            :          * called.
     604                 :            :          */
     605                 :            :         destroy_rcu_head(&cmd->rcu);
     606                 :            : 
     607                 :            :         /*
     608                 :            :          * In the MQ case the command gets freed by __blk_mq_end_request,
     609                 :            :          * so we have to do all cleanup that depends on it earlier.
     610                 :            :          *
     611                 :            :          * We also can't kick the queues from irq context, so we
     612                 :            :          * will have to defer it to a workqueue.
     613                 :            :          */
     614                 :          0 :         scsi_mq_uninit_cmd(cmd);
     615                 :            : 
     616                 :            :         /*
     617                 :            :          * queue is still alive, so grab the ref for preventing it
     618                 :            :          * from being cleaned up during running queue.
     619                 :            :          */
     620                 :          0 :         percpu_ref_get(&q->q_usage_counter);
     621                 :            : 
     622                 :          0 :         __blk_mq_end_request(req, error);
     623                 :            : 
     624                 :          0 :         scsi_run_queue_async(sdev);
     625                 :            : 
     626                 :            :         percpu_ref_put(&q->q_usage_counter);
     627                 :          0 :         return false;
     628                 :            : }
     629                 :            : 
     630                 :            : /**
     631                 :            :  * scsi_result_to_blk_status - translate a SCSI result code into blk_status_t
     632                 :            :  * @cmd:        SCSI command
     633                 :            :  * @result:     scsi error code
     634                 :            :  *
     635                 :            :  * Translate a SCSI result code into a blk_status_t value. May reset the host
     636                 :            :  * byte of @cmd->result.
     637                 :            :  */
     638                 :          0 : static blk_status_t scsi_result_to_blk_status(struct scsi_cmnd *cmd, int result)
     639                 :            : {
     640   [ #  #  #  #  :          0 :         switch (host_byte(result)) {
                #  #  # ]
     641                 :            :         case DID_OK:
     642                 :            :                 /*
     643                 :            :                  * Also check the other bytes than the status byte in result
     644                 :            :                  * to handle the case when a SCSI LLD sets result to
     645                 :            :                  * DRIVER_SENSE << 24 without setting SAM_STAT_CHECK_CONDITION.
     646                 :            :                  */
     647   [ #  #  #  # ]:          0 :                 if (scsi_status_is_good(result) && (result & ~0xff) == 0)
     648                 :            :                         return BLK_STS_OK;
     649                 :          0 :                 return BLK_STS_IOERR;
     650                 :            :         case DID_TRANSPORT_FAILFAST:
     651                 :            :                 return BLK_STS_TRANSPORT;
     652                 :            :         case DID_TARGET_FAILURE:
     653                 :            :                 set_host_byte(cmd, DID_OK);
     654                 :          0 :                 return BLK_STS_TARGET;
     655                 :            :         case DID_NEXUS_FAILURE:
     656                 :            :                 set_host_byte(cmd, DID_OK);
     657                 :          0 :                 return BLK_STS_NEXUS;
     658                 :            :         case DID_ALLOC_FAILURE:
     659                 :            :                 set_host_byte(cmd, DID_OK);
     660                 :          0 :                 return BLK_STS_NOSPC;
     661                 :            :         case DID_MEDIUM_ERROR:
     662                 :            :                 set_host_byte(cmd, DID_OK);
     663                 :          0 :                 return BLK_STS_MEDIUM;
     664                 :            :         default:
     665                 :          0 :                 return BLK_STS_IOERR;
     666                 :            :         }
     667                 :            : }
     668                 :            : 
     669                 :            : /* Helper for scsi_io_completion() when "reprep" action required. */
     670                 :            : static void scsi_io_completion_reprep(struct scsi_cmnd *cmd,
     671                 :            :                                       struct request_queue *q)
     672                 :            : {
     673                 :            :         /* A new command will be prepared and issued. */
     674                 :          0 :         scsi_mq_requeue_cmd(cmd);
     675                 :            : }
     676                 :            : 
     677                 :            : /* Helper for scsi_io_completion() when special action required. */
     678                 :          0 : static void scsi_io_completion_action(struct scsi_cmnd *cmd, int result)
     679                 :            : {
     680                 :            :         struct request_queue *q = cmd->device->request_queue;
     681                 :          0 :         struct request *req = cmd->request;
     682                 :            :         int level = 0;
     683                 :            :         enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
     684                 :            :               ACTION_DELAYED_RETRY} action;
     685                 :          0 :         unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
     686                 :            :         struct scsi_sense_hdr sshdr;
     687                 :            :         bool sense_valid;
     688                 :            :         bool sense_current = true;      /* false implies "deferred sense" */
     689                 :            :         blk_status_t blk_stat;
     690                 :            : 
     691                 :          0 :         sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
     692         [ #  # ]:          0 :         if (sense_valid)
     693                 :          0 :                 sense_current = !scsi_sense_is_deferred(&sshdr);
     694                 :            : 
     695                 :          0 :         blk_stat = scsi_result_to_blk_status(cmd, result);
     696                 :            : 
     697         [ #  # ]:          0 :         if (host_byte(result) == DID_RESET) {
     698                 :            :                 /* Third party bus reset or reset for error recovery
     699                 :            :                  * reasons.  Just retry the command and see what
     700                 :            :                  * happens.
     701                 :            :                  */
     702                 :            :                 action = ACTION_RETRY;
     703         [ #  # ]:          0 :         } else if (sense_valid && sense_current) {
     704   [ #  #  #  #  :          0 :                 switch (sshdr.sense_key) {
                      # ]
     705                 :            :                 case UNIT_ATTENTION:
     706         [ #  # ]:          0 :                         if (cmd->device->removable) {
     707                 :            :                                 /* Detected disc change.  Set a bit
     708                 :            :                                  * and quietly refuse further access.
     709                 :            :                                  */
     710                 :          0 :                                 cmd->device->changed = 1;
     711                 :            :                                 action = ACTION_FAIL;
     712                 :            :                         } else {
     713                 :            :                                 /* Must have been a power glitch, or a
     714                 :            :                                  * bus reset.  Could not have been a
     715                 :            :                                  * media change, so we just retry the
     716                 :            :                                  * command and see what happens.
     717                 :            :                                  */
     718                 :            :                                 action = ACTION_RETRY;
     719                 :            :                         }
     720                 :            :                         break;
     721                 :            :                 case ILLEGAL_REQUEST:
     722                 :            :                         /* If we had an ILLEGAL REQUEST returned, then
     723                 :            :                          * we may have performed an unsupported
     724                 :            :                          * command.  The only thing this should be
     725                 :            :                          * would be a ten byte read where only a six
     726                 :            :                          * byte read was supported.  Also, on a system
     727                 :            :                          * where READ CAPACITY failed, we may have
     728                 :            :                          * read past the end of the disk.
     729                 :            :                          */
     730   [ #  #  #  # ]:          0 :                         if ((cmd->device->use_10_for_rw &&
     731   [ #  #  #  # ]:          0 :                             sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
     732                 :          0 :                             (cmd->cmnd[0] == READ_10 ||
     733                 :            :                              cmd->cmnd[0] == WRITE_10)) {
     734                 :            :                                 /* This will issue a new 6-byte command. */
     735                 :          0 :                                 cmd->device->use_10_for_rw = 0;
     736                 :          0 :                                 action = ACTION_REPREP;
     737         [ #  # ]:          0 :                         } else if (sshdr.asc == 0x10) /* DIX */ {
     738                 :            :                                 action = ACTION_FAIL;
     739                 :            :                                 blk_stat = BLK_STS_PROTECTION;
     740                 :            :                         /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
     741         [ #  # ]:          0 :                         } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
     742                 :            :                                 action = ACTION_FAIL;
     743                 :            :                                 blk_stat = BLK_STS_TARGET;
     744                 :            :                         } else
     745                 :            :                                 action = ACTION_FAIL;
     746                 :            :                         break;
     747                 :            :                 case ABORTED_COMMAND:
     748                 :            :                         action = ACTION_FAIL;
     749         [ #  # ]:          0 :                         if (sshdr.asc == 0x10) /* DIF */
     750                 :            :                                 blk_stat = BLK_STS_PROTECTION;
     751                 :            :                         break;
     752                 :            :                 case NOT_READY:
     753                 :            :                         /* If the device is in the process of becoming
     754                 :            :                          * ready, or has a temporary blockage, retry.
     755                 :            :                          */
     756         [ #  # ]:          0 :                         if (sshdr.asc == 0x04) {
     757         [ #  # ]:          0 :                                 switch (sshdr.ascq) {
     758                 :            :                                 case 0x01: /* becoming ready */
     759                 :            :                                 case 0x04: /* format in progress */
     760                 :            :                                 case 0x05: /* rebuild in progress */
     761                 :            :                                 case 0x06: /* recalculation in progress */
     762                 :            :                                 case 0x07: /* operation in progress */
     763                 :            :                                 case 0x08: /* Long write in progress */
     764                 :            :                                 case 0x09: /* self test in progress */
     765                 :            :                                 case 0x14: /* space allocation in progress */
     766                 :            :                                 case 0x1a: /* start stop unit in progress */
     767                 :            :                                 case 0x1b: /* sanitize in progress */
     768                 :            :                                 case 0x1d: /* configuration in progress */
     769                 :            :                                 case 0x24: /* depopulation in progress */
     770                 :            :                                         action = ACTION_DELAYED_RETRY;
     771                 :            :                                         break;
     772                 :            :                                 default:
     773                 :            :                                         action = ACTION_FAIL;
     774                 :          0 :                                         break;
     775                 :            :                                 }
     776                 :            :                         } else
     777                 :            :                                 action = ACTION_FAIL;
     778                 :            :                         break;
     779                 :            :                 case VOLUME_OVERFLOW:
     780                 :            :                         /* See SSC3rXX or current. */
     781                 :            :                         action = ACTION_FAIL;
     782                 :            :                         break;
     783                 :            :                 default:
     784                 :            :                         action = ACTION_FAIL;
     785                 :            :                         break;
     786                 :            :                 }
     787                 :            :         } else
     788                 :            :                 action = ACTION_FAIL;
     789                 :            : 
     790         [ #  # ]:          0 :         if (action != ACTION_FAIL &&
     791         [ #  # ]:          0 :             time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
     792                 :            :                 action = ACTION_FAIL;
     793                 :            : 
     794   [ #  #  #  #  :          0 :         switch (action) {
                      # ]
     795                 :            :         case ACTION_FAIL:
     796                 :            :                 /* Give up and fail the remainder of the request */
     797         [ #  # ]:          0 :                 if (!(req->rq_flags & RQF_QUIET)) {
     798                 :            :                         static DEFINE_RATELIMIT_STATE(_rs,
     799                 :            :                                         DEFAULT_RATELIMIT_INTERVAL,
     800                 :            :                                         DEFAULT_RATELIMIT_BURST);
     801                 :            : 
     802                 :            :                         if (unlikely(scsi_logging_level))
     803                 :            :                                 level =
     804                 :            :                                      SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
     805                 :            :                                                     SCSI_LOG_MLCOMPLETE_BITS);
     806                 :            : 
     807                 :            :                         /*
     808                 :            :                          * if logging is enabled the failure will be printed
     809                 :            :                          * in scsi_log_completion(), so avoid duplicate messages
     810                 :            :                          */
     811         [ #  # ]:          0 :                         if (!level && __ratelimit(&_rs)) {
     812                 :          0 :                                 scsi_print_result(cmd, NULL, FAILED);
     813         [ #  # ]:          0 :                                 if (driver_byte(result) == DRIVER_SENSE)
     814                 :          0 :                                         scsi_print_sense(cmd);
     815                 :          0 :                                 scsi_print_command(cmd);
     816                 :            :                         }
     817                 :            :                 }
     818         [ #  # ]:          0 :                 if (!scsi_end_request(req, blk_stat, blk_rq_err_bytes(req)))
     819                 :          0 :                         return;
     820                 :            :                 /*FALLTHRU*/
     821                 :            :         case ACTION_REPREP:
     822                 :            :                 scsi_io_completion_reprep(cmd, q);
     823                 :            :                 break;
     824                 :            :         case ACTION_RETRY:
     825                 :            :                 /* Retry the same command immediately */
     826                 :          0 :                 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, false);
     827                 :          0 :                 break;
     828                 :            :         case ACTION_DELAYED_RETRY:
     829                 :            :                 /* Retry the same command after a delay */
     830                 :          0 :                 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, false);
     831                 :          0 :                 break;
     832                 :            :         }
     833                 :            : }
     834                 :            : 
     835                 :            : /*
     836                 :            :  * Helper for scsi_io_completion() when cmd->result is non-zero. Returns a
     837                 :            :  * new result that may suppress further error checking. Also modifies
     838                 :            :  * *blk_statp in some cases.
     839                 :            :  */
     840                 :          0 : static int scsi_io_completion_nz_result(struct scsi_cmnd *cmd, int result,
     841                 :            :                                         blk_status_t *blk_statp)
     842                 :            : {
     843                 :            :         bool sense_valid;
     844                 :            :         bool sense_current = true;      /* false implies "deferred sense" */
     845                 :          0 :         struct request *req = cmd->request;
     846                 :            :         struct scsi_sense_hdr sshdr;
     847                 :            : 
     848                 :          0 :         sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
     849         [ #  # ]:          0 :         if (sense_valid)
     850                 :          0 :                 sense_current = !scsi_sense_is_deferred(&sshdr);
     851                 :            : 
     852         [ #  # ]:          0 :         if (blk_rq_is_passthrough(req)) {
     853         [ #  # ]:          0 :                 if (sense_valid) {
     854                 :            :                         /*
     855                 :            :                          * SG_IO wants current and deferred errors
     856                 :            :                          */
     857                 :          0 :                         scsi_req(req)->sense_len =
     858                 :          0 :                                 min(8 + cmd->sense_buffer[7],
     859                 :            :                                     SCSI_SENSE_BUFFERSIZE);
     860                 :            :                 }
     861         [ #  # ]:          0 :                 if (sense_current)
     862                 :          0 :                         *blk_statp = scsi_result_to_blk_status(cmd, result);
     863   [ #  #  #  # ]:          0 :         } else if (blk_rq_bytes(req) == 0 && sense_current) {
     864                 :            :                 /*
     865                 :            :                  * Flush commands do not transfers any data, and thus cannot use
     866                 :            :                  * good_bytes != blk_rq_bytes(req) as the signal for an error.
     867                 :            :                  * This sets *blk_statp explicitly for the problem case.
     868                 :            :                  */
     869                 :          0 :                 *blk_statp = scsi_result_to_blk_status(cmd, result);
     870                 :            :         }
     871                 :            :         /*
     872                 :            :          * Recovered errors need reporting, but they're always treated as
     873                 :            :          * success, so fiddle the result code here.  For passthrough requests
     874                 :            :          * we already took a copy of the original into sreq->result which
     875                 :            :          * is what gets returned to the user
     876                 :            :          */
     877   [ #  #  #  # ]:          0 :         if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
     878                 :            :                 bool do_print = true;
     879                 :            :                 /*
     880                 :            :                  * if ATA PASS-THROUGH INFORMATION AVAILABLE [0x0, 0x1d]
     881                 :            :                  * skip print since caller wants ATA registers. Only occurs
     882                 :            :                  * on SCSI ATA PASS_THROUGH commands when CK_COND=1
     883                 :            :                  */
     884   [ #  #  #  # ]:          0 :                 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
     885                 :            :                         do_print = false;
     886         [ #  # ]:          0 :                 else if (req->rq_flags & RQF_QUIET)
     887                 :            :                         do_print = false;
     888         [ #  # ]:          0 :                 if (do_print)
     889                 :          0 :                         scsi_print_sense(cmd);
     890                 :            :                 result = 0;
     891                 :            :                 /* for passthrough, *blk_statp may be set */
     892                 :          0 :                 *blk_statp = BLK_STS_OK;
     893                 :            :         }
     894                 :            :         /*
     895                 :            :          * Another corner case: the SCSI status byte is non-zero but 'good'.
     896                 :            :          * Example: PRE-FETCH command returns SAM_STAT_CONDITION_MET when
     897                 :            :          * it is able to fit nominated LBs in its cache (and SAM_STAT_GOOD
     898                 :            :          * if it can't fit). Treat SAM_STAT_CONDITION_MET and the related
     899                 :            :          * intermediate statuses (both obsolete in SAM-4) as good.
     900                 :            :          */
     901   [ #  #  #  # ]:          0 :         if (status_byte(result) && scsi_status_is_good(result)) {
     902                 :            :                 result = 0;
     903                 :          0 :                 *blk_statp = BLK_STS_OK;
     904                 :            :         }
     905                 :          0 :         return result;
     906                 :            : }
     907                 :            : 
     908                 :            : /*
     909                 :            :  * Function:    scsi_io_completion()
     910                 :            :  *
     911                 :            :  * Purpose:     Completion processing for block device I/O requests.
     912                 :            :  *
     913                 :            :  * Arguments:   cmd   - command that is finished.
     914                 :            :  *
     915                 :            :  * Lock status: Assumed that no lock is held upon entry.
     916                 :            :  *
     917                 :            :  * Returns:     Nothing
     918                 :            :  *
     919                 :            :  * Notes:       We will finish off the specified number of sectors.  If we
     920                 :            :  *              are done, the command block will be released and the queue
     921                 :            :  *              function will be goosed.  If we are not done then we have to
     922                 :            :  *              figure out what to do next:
     923                 :            :  *
     924                 :            :  *              a) We can call scsi_requeue_command().  The request
     925                 :            :  *                 will be unprepared and put back on the queue.  Then
     926                 :            :  *                 a new command will be created for it.  This should
     927                 :            :  *                 be used if we made forward progress, or if we want
     928                 :            :  *                 to switch from READ(10) to READ(6) for example.
     929                 :            :  *
     930                 :            :  *              b) We can call __scsi_queue_insert().  The request will
     931                 :            :  *                 be put back on the queue and retried using the same
     932                 :            :  *                 command as before, possibly after a delay.
     933                 :            :  *
     934                 :            :  *              c) We can call scsi_end_request() with blk_stat other than
     935                 :            :  *                 BLK_STS_OK, to fail the remainder of the request.
     936                 :            :  */
     937                 :          0 : void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
     938                 :            : {
     939                 :          0 :         int result = cmd->result;
     940                 :            :         struct request_queue *q = cmd->device->request_queue;
     941                 :          0 :         struct request *req = cmd->request;
     942                 :          0 :         blk_status_t blk_stat = BLK_STS_OK;
     943                 :            : 
     944         [ #  # ]:          0 :         if (unlikely(result))   /* a nz result may or may not be an error */
     945                 :          0 :                 result = scsi_io_completion_nz_result(cmd, result, &blk_stat);
     946                 :            : 
     947         [ #  # ]:          0 :         if (unlikely(blk_rq_is_passthrough(req))) {
     948                 :            :                 /*
     949                 :            :                  * scsi_result_to_blk_status may have reset the host_byte
     950                 :            :                  */
     951                 :          0 :                 scsi_req(req)->result = cmd->result;
     952                 :            :         }
     953                 :            : 
     954                 :            :         /*
     955                 :            :          * Next deal with any sectors which we were able to correctly
     956                 :            :          * handle.
     957                 :            :          */
     958                 :            :         SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
     959                 :            :                 "%u sectors total, %d bytes done.\n",
     960                 :            :                 blk_rq_sectors(req), good_bytes));
     961                 :            : 
     962                 :            :         /*
     963                 :            :          * Next deal with any sectors which we were able to correctly
     964                 :            :          * handle. Failed, zero length commands always need to drop down
     965                 :            :          * to retry code. Fast path should return in this block.
     966                 :            :          */
     967   [ #  #  #  # ]:          0 :         if (likely(blk_rq_bytes(req) > 0 || blk_stat == BLK_STS_OK)) {
     968         [ #  # ]:          0 :                 if (likely(!scsi_end_request(req, blk_stat, good_bytes)))
     969                 :          0 :                         return; /* no bytes remaining */
     970                 :            :         }
     971                 :            : 
     972                 :            :         /* Kill remainder if no retries. */
     973   [ #  #  #  # ]:          0 :         if (unlikely(blk_stat && scsi_noretry_cmd(cmd))) {
     974         [ #  # ]:          0 :                 if (scsi_end_request(req, blk_stat, blk_rq_bytes(req)))
     975         [ #  # ]:          0 :                         WARN_ONCE(true,
     976                 :            :                             "Bytes remaining after failed, no-retry command");
     977                 :            :                 return;
     978                 :            :         }
     979                 :            : 
     980                 :            :         /*
     981                 :            :          * If there had been no error, but we have leftover bytes in the
     982                 :            :          * requeues just queue the command up again.
     983                 :            :          */
     984         [ #  # ]:          0 :         if (likely(result == 0))
     985                 :            :                 scsi_io_completion_reprep(cmd, q);
     986                 :            :         else
     987                 :          0 :                 scsi_io_completion_action(cmd, result);
     988                 :            : }
     989                 :            : 
     990                 :          0 : static blk_status_t scsi_init_sgtable(struct request *req,
     991                 :            :                 struct scsi_data_buffer *sdb)
     992                 :            : {
     993                 :            :         int count;
     994                 :            : 
     995                 :            :         /*
     996                 :            :          * If sg table allocation fails, requeue request later.
     997                 :            :          */
     998         [ #  # ]:          0 :         if (unlikely(sg_alloc_table_chained(&sdb->table,
     999                 :            :                         blk_rq_nr_phys_segments(req), sdb->table.sgl,
    1000                 :            :                         SCSI_INLINE_SG_CNT)))
    1001                 :            :                 return BLK_STS_RESOURCE;
    1002                 :            : 
    1003                 :            :         /* 
    1004                 :            :          * Next, walk the list, and fill in the addresses and sizes of
    1005                 :            :          * each segment.
    1006                 :            :          */
    1007                 :          0 :         count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
    1008         [ #  # ]:          0 :         BUG_ON(count > sdb->table.nents);
    1009                 :          0 :         sdb->table.nents = count;
    1010                 :          0 :         sdb->length = blk_rq_payload_bytes(req);
    1011                 :          0 :         return BLK_STS_OK;
    1012                 :            : }
    1013                 :            : 
    1014                 :            : /*
    1015                 :            :  * Function:    scsi_init_io()
    1016                 :            :  *
    1017                 :            :  * Purpose:     SCSI I/O initialize function.
    1018                 :            :  *
    1019                 :            :  * Arguments:   cmd   - Command descriptor we wish to initialize
    1020                 :            :  *
    1021                 :            :  * Returns:     BLK_STS_OK on success
    1022                 :            :  *              BLK_STS_RESOURCE if the failure is retryable
    1023                 :            :  *              BLK_STS_IOERR if the failure is fatal
    1024                 :            :  */
    1025                 :          0 : blk_status_t scsi_init_io(struct scsi_cmnd *cmd)
    1026                 :            : {
    1027                 :          0 :         struct request *rq = cmd->request;
    1028                 :            :         blk_status_t ret;
    1029                 :            : 
    1030   [ #  #  #  #  :          0 :         if (WARN_ON_ONCE(!blk_rq_nr_phys_segments(rq)))
                   #  # ]
    1031                 :            :                 return BLK_STS_IOERR;
    1032                 :            : 
    1033                 :          0 :         ret = scsi_init_sgtable(rq, &cmd->sdb);
    1034         [ #  # ]:          0 :         if (ret)
    1035                 :          0 :                 return ret;
    1036                 :            : 
    1037                 :            :         if (blk_integrity_rq(rq)) {
    1038                 :            :                 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
    1039                 :            :                 int ivecs, count;
    1040                 :            : 
    1041                 :            :                 if (WARN_ON_ONCE(!prot_sdb)) {
    1042                 :            :                         /*
    1043                 :            :                          * This can happen if someone (e.g. multipath)
    1044                 :            :                          * queues a command to a device on an adapter
    1045                 :            :                          * that does not support DIX.
    1046                 :            :                          */
    1047                 :            :                         ret = BLK_STS_IOERR;
    1048                 :            :                         goto out_free_sgtables;
    1049                 :            :                 }
    1050                 :            : 
    1051                 :            :                 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
    1052                 :            : 
    1053                 :            :                 if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
    1054                 :            :                                 prot_sdb->table.sgl,
    1055                 :            :                                 SCSI_INLINE_PROT_SG_CNT)) {
    1056                 :            :                         ret = BLK_STS_RESOURCE;
    1057                 :            :                         goto out_free_sgtables;
    1058                 :            :                 }
    1059                 :            : 
    1060                 :            :                 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
    1061                 :            :                                                 prot_sdb->table.sgl);
    1062                 :            :                 BUG_ON(count > ivecs);
    1063                 :            :                 BUG_ON(count > queue_max_integrity_segments(rq->q));
    1064                 :            : 
    1065                 :            :                 cmd->prot_sdb = prot_sdb;
    1066                 :            :                 cmd->prot_sdb->table.nents = count;
    1067                 :            :         }
    1068                 :            : 
    1069                 :            :         return BLK_STS_OK;
    1070                 :            : out_free_sgtables:
    1071                 :            :         scsi_free_sgtables(cmd);
    1072                 :            :         return ret;
    1073                 :            : }
    1074                 :            : EXPORT_SYMBOL(scsi_init_io);
    1075                 :            : 
    1076                 :            : /**
    1077                 :            :  * scsi_initialize_rq - initialize struct scsi_cmnd partially
    1078                 :            :  * @rq: Request associated with the SCSI command to be initialized.
    1079                 :            :  *
    1080                 :            :  * This function initializes the members of struct scsi_cmnd that must be
    1081                 :            :  * initialized before request processing starts and that won't be
    1082                 :            :  * reinitialized if a SCSI command is requeued.
    1083                 :            :  *
    1084                 :            :  * Called from inside blk_get_request() for pass-through requests and from
    1085                 :            :  * inside scsi_init_command() for filesystem requests.
    1086                 :            :  */
    1087                 :          0 : static void scsi_initialize_rq(struct request *rq)
    1088                 :            : {
    1089                 :            :         struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
    1090                 :            : 
    1091                 :          0 :         scsi_req_init(&cmd->req);
    1092                 :            :         init_rcu_head(&cmd->rcu);
    1093                 :          0 :         cmd->jiffies_at_alloc = jiffies;
    1094                 :          0 :         cmd->retries = 0;
    1095                 :          0 : }
    1096                 :            : 
    1097                 :            : /*
    1098                 :            :  * Only called when the request isn't completed by SCSI, and not freed by
    1099                 :            :  * SCSI
    1100                 :            :  */
    1101                 :          0 : static void scsi_cleanup_rq(struct request *rq)
    1102                 :            : {
    1103         [ #  # ]:          0 :         if (rq->rq_flags & RQF_DONTPREP) {
    1104                 :          0 :                 scsi_mq_uninit_cmd(blk_mq_rq_to_pdu(rq));
    1105                 :          0 :                 rq->rq_flags &= ~RQF_DONTPREP;
    1106                 :            :         }
    1107                 :          0 : }
    1108                 :            : 
    1109                 :            : /* Add a command to the list used by the aacraid and dpt_i2o drivers */
    1110                 :          0 : void scsi_add_cmd_to_list(struct scsi_cmnd *cmd)
    1111                 :            : {
    1112                 :          0 :         struct scsi_device *sdev = cmd->device;
    1113                 :          0 :         struct Scsi_Host *shost = sdev->host;
    1114                 :            :         unsigned long flags;
    1115                 :            : 
    1116         [ #  # ]:          0 :         if (shost->use_cmd_list) {
    1117                 :          0 :                 spin_lock_irqsave(&sdev->list_lock, flags);
    1118                 :          0 :                 list_add_tail(&cmd->list, &sdev->cmd_list);
    1119                 :            :                 spin_unlock_irqrestore(&sdev->list_lock, flags);
    1120                 :            :         }
    1121                 :          0 : }
    1122                 :            : 
    1123                 :            : /* Remove a command from the list used by the aacraid and dpt_i2o drivers */
    1124                 :          0 : void scsi_del_cmd_from_list(struct scsi_cmnd *cmd)
    1125                 :            : {
    1126                 :          0 :         struct scsi_device *sdev = cmd->device;
    1127                 :          0 :         struct Scsi_Host *shost = sdev->host;
    1128                 :            :         unsigned long flags;
    1129                 :            : 
    1130         [ #  # ]:          0 :         if (shost->use_cmd_list) {
    1131                 :          0 :                 spin_lock_irqsave(&sdev->list_lock, flags);
    1132         [ #  # ]:          0 :                 BUG_ON(list_empty(&cmd->list));
    1133                 :            :                 list_del_init(&cmd->list);
    1134                 :            :                 spin_unlock_irqrestore(&sdev->list_lock, flags);
    1135                 :            :         }
    1136                 :          0 : }
    1137                 :            : 
    1138                 :            : /* Called after a request has been started. */
    1139                 :          0 : void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd)
    1140                 :            : {
    1141                 :          0 :         void *buf = cmd->sense_buffer;
    1142                 :          0 :         void *prot = cmd->prot_sdb;
    1143                 :            :         struct request *rq = blk_mq_rq_from_pdu(cmd);
    1144                 :          0 :         unsigned int flags = cmd->flags & SCMD_PRESERVED_FLAGS;
    1145                 :            :         unsigned long jiffies_at_alloc;
    1146                 :            :         int retries;
    1147                 :            : 
    1148   [ #  #  #  # ]:          0 :         if (!blk_rq_is_scsi(rq) && !(flags & SCMD_INITIALIZED)) {
    1149                 :          0 :                 flags |= SCMD_INITIALIZED;
    1150                 :            :                 scsi_initialize_rq(rq);
    1151                 :            :         }
    1152                 :            : 
    1153                 :          0 :         jiffies_at_alloc = cmd->jiffies_at_alloc;
    1154                 :          0 :         retries = cmd->retries;
    1155                 :            :         /* zero out the cmd, except for the embedded scsi_request */
    1156                 :          0 :         memset((char *)cmd + sizeof(cmd->req), 0,
    1157                 :          0 :                 sizeof(*cmd) - sizeof(cmd->req) + dev->host->hostt->cmd_size);
    1158                 :            : 
    1159                 :          0 :         cmd->device = dev;
    1160                 :          0 :         cmd->sense_buffer = buf;
    1161                 :          0 :         cmd->prot_sdb = prot;
    1162                 :          0 :         cmd->flags = flags;
    1163                 :          0 :         INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
    1164                 :          0 :         cmd->jiffies_at_alloc = jiffies_at_alloc;
    1165                 :          0 :         cmd->retries = retries;
    1166                 :            : 
    1167                 :          0 :         scsi_add_cmd_to_list(cmd);
    1168                 :          0 : }
    1169                 :            : 
    1170                 :          0 : static blk_status_t scsi_setup_scsi_cmnd(struct scsi_device *sdev,
    1171                 :            :                 struct request *req)
    1172                 :            : {
    1173                 :            :         struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
    1174                 :            : 
    1175                 :            :         /*
    1176                 :            :          * Passthrough requests may transfer data, in which case they must
    1177                 :            :          * a bio attached to them.  Or they might contain a SCSI command
    1178                 :            :          * that does not transfer data, in which case they may optionally
    1179                 :            :          * submit a request without an attached bio.
    1180                 :            :          */
    1181         [ #  # ]:          0 :         if (req->bio) {
    1182                 :          0 :                 blk_status_t ret = scsi_init_io(cmd);
    1183         [ #  # ]:          0 :                 if (unlikely(ret != BLK_STS_OK))
    1184                 :            :                         return ret;
    1185                 :            :         } else {
    1186         [ #  # ]:          0 :                 BUG_ON(blk_rq_bytes(req));
    1187                 :            : 
    1188                 :          0 :                 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
    1189                 :            :         }
    1190                 :            : 
    1191                 :          0 :         cmd->cmd_len = scsi_req(req)->cmd_len;
    1192                 :          0 :         cmd->cmnd = scsi_req(req)->cmd;
    1193                 :          0 :         cmd->transfersize = blk_rq_bytes(req);
    1194                 :          0 :         cmd->allowed = scsi_req(req)->retries;
    1195                 :          0 :         return BLK_STS_OK;
    1196                 :            : }
    1197                 :            : 
    1198                 :            : /*
    1199                 :            :  * Setup a normal block command.  These are simple request from filesystems
    1200                 :            :  * that still need to be translated to SCSI CDBs from the ULD.
    1201                 :            :  */
    1202                 :          0 : static blk_status_t scsi_setup_fs_cmnd(struct scsi_device *sdev,
    1203                 :            :                 struct request *req)
    1204                 :            : {
    1205                 :            :         struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
    1206                 :            : 
    1207   [ #  #  #  # ]:          0 :         if (unlikely(sdev->handler && sdev->handler->prep_fn)) {
    1208                 :          0 :                 blk_status_t ret = sdev->handler->prep_fn(sdev, req);
    1209         [ #  # ]:          0 :                 if (ret != BLK_STS_OK)
    1210                 :            :                         return ret;
    1211                 :            :         }
    1212                 :            : 
    1213                 :          0 :         cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd;
    1214                 :          0 :         memset(cmd->cmnd, 0, BLK_MAX_CDB);
    1215                 :          0 :         return scsi_cmd_to_driver(cmd)->init_command(cmd);
    1216                 :            : }
    1217                 :            : 
    1218                 :          0 : static blk_status_t scsi_setup_cmnd(struct scsi_device *sdev,
    1219                 :            :                 struct request *req)
    1220                 :            : {
    1221                 :            :         struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
    1222                 :            :         blk_status_t ret;
    1223                 :            : 
    1224         [ #  # ]:          0 :         if (!blk_rq_bytes(req))
    1225                 :          0 :                 cmd->sc_data_direction = DMA_NONE;
    1226         [ #  # ]:          0 :         else if (rq_data_dir(req) == WRITE)
    1227                 :          0 :                 cmd->sc_data_direction = DMA_TO_DEVICE;
    1228                 :            :         else
    1229                 :          0 :                 cmd->sc_data_direction = DMA_FROM_DEVICE;
    1230                 :            : 
    1231         [ #  # ]:          0 :         if (blk_rq_is_scsi(req))
    1232                 :          0 :                 ret = scsi_setup_scsi_cmnd(sdev, req);
    1233                 :            :         else
    1234                 :          0 :                 ret = scsi_setup_fs_cmnd(sdev, req);
    1235                 :            : 
    1236         [ #  # ]:          0 :         if (ret != BLK_STS_OK)
    1237                 :          0 :                 scsi_free_sgtables(cmd);
    1238                 :            : 
    1239                 :          0 :         return ret;
    1240                 :            : }
    1241                 :            : 
    1242                 :            : static blk_status_t
    1243                 :          0 : scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
    1244                 :            : {
    1245   [ #  #  #  #  :          0 :         switch (sdev->sdev_state) {
                      # ]
    1246                 :            :         case SDEV_OFFLINE:
    1247                 :            :         case SDEV_TRANSPORT_OFFLINE:
    1248                 :            :                 /*
    1249                 :            :                  * If the device is offline we refuse to process any
    1250                 :            :                  * commands.  The device must be brought online
    1251                 :            :                  * before trying any recovery commands.
    1252                 :            :                  */
    1253                 :          0 :                 sdev_printk(KERN_ERR, sdev,
    1254                 :            :                             "rejecting I/O to offline device\n");
    1255                 :          0 :                 return BLK_STS_IOERR;
    1256                 :            :         case SDEV_DEL:
    1257                 :            :                 /*
    1258                 :            :                  * If the device is fully deleted, we refuse to
    1259                 :            :                  * process any commands as well.
    1260                 :            :                  */
    1261                 :          0 :                 sdev_printk(KERN_ERR, sdev,
    1262                 :            :                             "rejecting I/O to dead device\n");
    1263                 :          0 :                 return BLK_STS_IOERR;
    1264                 :            :         case SDEV_BLOCK:
    1265                 :            :         case SDEV_CREATED_BLOCK:
    1266                 :            :                 return BLK_STS_RESOURCE;
    1267                 :            :         case SDEV_QUIESCE:
    1268                 :            :                 /*
    1269                 :            :                  * If the devices is blocked we defer normal commands.
    1270                 :            :                  */
    1271   [ #  #  #  # ]:          0 :                 if (req && !(req->rq_flags & RQF_PREEMPT))
    1272                 :            :                         return BLK_STS_RESOURCE;
    1273                 :          0 :                 return BLK_STS_OK;
    1274                 :            :         default:
    1275                 :            :                 /*
    1276                 :            :                  * For any other not fully online state we only allow
    1277                 :            :                  * special commands.  In particular any user initiated
    1278                 :            :                  * command is not allowed.
    1279                 :            :                  */
    1280   [ #  #  #  # ]:          0 :                 if (req && !(req->rq_flags & RQF_PREEMPT))
    1281                 :            :                         return BLK_STS_IOERR;
    1282                 :          0 :                 return BLK_STS_OK;
    1283                 :            :         }
    1284                 :            : }
    1285                 :            : 
    1286                 :            : /*
    1287                 :            :  * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
    1288                 :            :  * return 0.
    1289                 :            :  *
    1290                 :            :  * Called with the queue_lock held.
    1291                 :            :  */
    1292                 :          0 : static inline int scsi_dev_queue_ready(struct request_queue *q,
    1293                 :            :                                   struct scsi_device *sdev)
    1294                 :            : {
    1295                 :            :         unsigned int busy;
    1296                 :            : 
    1297                 :          0 :         busy = atomic_inc_return(&sdev->device_busy) - 1;
    1298         [ #  # ]:          0 :         if (atomic_read(&sdev->device_blocked)) {
    1299         [ #  # ]:          0 :                 if (busy)
    1300                 :            :                         goto out_dec;
    1301                 :            : 
    1302                 :            :                 /*
    1303                 :            :                  * unblock after device_blocked iterates to zero
    1304                 :            :                  */
    1305         [ #  # ]:          0 :                 if (atomic_dec_return(&sdev->device_blocked) > 0)
    1306                 :            :                         goto out_dec;
    1307                 :            :                 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
    1308                 :            :                                    "unblocking device at zero depth\n"));
    1309                 :            :         }
    1310                 :            : 
    1311         [ #  # ]:          0 :         if (busy >= sdev->queue_depth)
    1312                 :            :                 goto out_dec;
    1313                 :            : 
    1314                 :            :         return 1;
    1315                 :            : out_dec:
    1316                 :            :         atomic_dec(&sdev->device_busy);
    1317                 :          0 :         return 0;
    1318                 :            : }
    1319                 :            : 
    1320                 :            : /*
    1321                 :            :  * scsi_target_queue_ready: checks if there we can send commands to target
    1322                 :            :  * @sdev: scsi device on starget to check.
    1323                 :            :  */
    1324                 :          0 : static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
    1325                 :            :                                            struct scsi_device *sdev)
    1326                 :            : {
    1327                 :            :         struct scsi_target *starget = scsi_target(sdev);
    1328                 :            :         unsigned int busy;
    1329                 :            : 
    1330         [ #  # ]:          0 :         if (starget->single_lun) {
    1331                 :          0 :                 spin_lock_irq(shost->host_lock);
    1332   [ #  #  #  # ]:          0 :                 if (starget->starget_sdev_user &&
    1333                 :            :                     starget->starget_sdev_user != sdev) {
    1334                 :          0 :                         spin_unlock_irq(shost->host_lock);
    1335                 :          0 :                         return 0;
    1336                 :            :                 }
    1337                 :          0 :                 starget->starget_sdev_user = sdev;
    1338                 :          0 :                 spin_unlock_irq(shost->host_lock);
    1339                 :            :         }
    1340                 :            : 
    1341         [ #  # ]:          0 :         if (starget->can_queue <= 0)
    1342                 :            :                 return 1;
    1343                 :            : 
    1344                 :          0 :         busy = atomic_inc_return(&starget->target_busy) - 1;
    1345         [ #  # ]:          0 :         if (atomic_read(&starget->target_blocked) > 0) {
    1346         [ #  # ]:          0 :                 if (busy)
    1347                 :            :                         goto starved;
    1348                 :            : 
    1349                 :            :                 /*
    1350                 :            :                  * unblock after target_blocked iterates to zero
    1351                 :            :                  */
    1352         [ #  # ]:          0 :                 if (atomic_dec_return(&starget->target_blocked) > 0)
    1353                 :            :                         goto out_dec;
    1354                 :            : 
    1355                 :            :                 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
    1356                 :            :                                  "unblocking target at zero depth\n"));
    1357                 :            :         }
    1358                 :            : 
    1359         [ #  # ]:          0 :         if (busy >= starget->can_queue)
    1360                 :            :                 goto starved;
    1361                 :            : 
    1362                 :            :         return 1;
    1363                 :            : 
    1364                 :            : starved:
    1365                 :          0 :         spin_lock_irq(shost->host_lock);
    1366                 :          0 :         list_move_tail(&sdev->starved_entry, &shost->starved_list);
    1367                 :          0 :         spin_unlock_irq(shost->host_lock);
    1368                 :            : out_dec:
    1369         [ #  # ]:          0 :         if (starget->can_queue > 0)
    1370                 :            :                 atomic_dec(&starget->target_busy);
    1371                 :            :         return 0;
    1372                 :            : }
    1373                 :            : 
    1374                 :            : /*
    1375                 :            :  * scsi_host_queue_ready: if we can send requests to shost, return 1 else
    1376                 :            :  * return 0. We must end up running the queue again whenever 0 is
    1377                 :            :  * returned, else IO can hang.
    1378                 :            :  */
    1379                 :          0 : static inline int scsi_host_queue_ready(struct request_queue *q,
    1380                 :            :                                    struct Scsi_Host *shost,
    1381                 :            :                                    struct scsi_device *sdev)
    1382                 :            : {
    1383                 :            :         unsigned int busy;
    1384                 :            : 
    1385         [ #  # ]:          0 :         if (scsi_host_in_recovery(shost))
    1386                 :            :                 return 0;
    1387                 :            : 
    1388                 :          0 :         busy = atomic_inc_return(&shost->host_busy) - 1;
    1389         [ #  # ]:          0 :         if (atomic_read(&shost->host_blocked) > 0) {
    1390         [ #  # ]:          0 :                 if (busy)
    1391                 :            :                         goto starved;
    1392                 :            : 
    1393                 :            :                 /*
    1394                 :            :                  * unblock after host_blocked iterates to zero
    1395                 :            :                  */
    1396         [ #  # ]:          0 :                 if (atomic_dec_return(&shost->host_blocked) > 0)
    1397                 :            :                         goto out_dec;
    1398                 :            : 
    1399                 :            :                 SCSI_LOG_MLQUEUE(3,
    1400                 :            :                         shost_printk(KERN_INFO, shost,
    1401                 :            :                                      "unblocking host at zero depth\n"));
    1402                 :            :         }
    1403                 :            : 
    1404   [ #  #  #  # ]:          0 :         if (shost->can_queue > 0 && busy >= shost->can_queue)
    1405                 :            :                 goto starved;
    1406         [ #  # ]:          0 :         if (shost->host_self_blocked)
    1407                 :            :                 goto starved;
    1408                 :            : 
    1409                 :            :         /* We're OK to process the command, so we can't be starved */
    1410         [ #  # ]:          0 :         if (!list_empty(&sdev->starved_entry)) {
    1411                 :          0 :                 spin_lock_irq(shost->host_lock);
    1412         [ #  # ]:          0 :                 if (!list_empty(&sdev->starved_entry))
    1413                 :            :                         list_del_init(&sdev->starved_entry);
    1414                 :          0 :                 spin_unlock_irq(shost->host_lock);
    1415                 :            :         }
    1416                 :            : 
    1417                 :            :         return 1;
    1418                 :            : 
    1419                 :            : starved:
    1420                 :          0 :         spin_lock_irq(shost->host_lock);
    1421         [ #  # ]:          0 :         if (list_empty(&sdev->starved_entry))
    1422                 :          0 :                 list_add_tail(&sdev->starved_entry, &shost->starved_list);
    1423                 :          0 :         spin_unlock_irq(shost->host_lock);
    1424                 :            : out_dec:
    1425                 :          0 :         scsi_dec_host_busy(shost);
    1426                 :          0 :         return 0;
    1427                 :            : }
    1428                 :            : 
    1429                 :            : /*
    1430                 :            :  * Busy state exporting function for request stacking drivers.
    1431                 :            :  *
    1432                 :            :  * For efficiency, no lock is taken to check the busy state of
    1433                 :            :  * shost/starget/sdev, since the returned value is not guaranteed and
    1434                 :            :  * may be changed after request stacking drivers call the function,
    1435                 :            :  * regardless of taking lock or not.
    1436                 :            :  *
    1437                 :            :  * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
    1438                 :            :  * needs to return 'not busy'. Otherwise, request stacking drivers
    1439                 :            :  * may hold requests forever.
    1440                 :            :  */
    1441                 :          0 : static bool scsi_mq_lld_busy(struct request_queue *q)
    1442                 :            : {
    1443                 :          0 :         struct scsi_device *sdev = q->queuedata;
    1444                 :            :         struct Scsi_Host *shost;
    1445                 :            : 
    1446         [ #  # ]:          0 :         if (blk_queue_dying(q))
    1447                 :            :                 return false;
    1448                 :            : 
    1449                 :          0 :         shost = sdev->host;
    1450                 :            : 
    1451                 :            :         /*
    1452                 :            :          * Ignore host/starget busy state.
    1453                 :            :          * Since block layer does not have a concept of fairness across
    1454                 :            :          * multiple queues, congestion of host/starget needs to be handled
    1455                 :            :          * in SCSI layer.
    1456                 :            :          */
    1457   [ #  #  #  # ]:          0 :         if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
    1458                 :            :                 return true;
    1459                 :            : 
    1460                 :            :         return false;
    1461                 :            : }
    1462                 :            : 
    1463                 :          0 : static void scsi_softirq_done(struct request *rq)
    1464                 :            : {
    1465                 :            :         struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
    1466                 :          0 :         unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
    1467                 :            :         int disposition;
    1468                 :            : 
    1469                 :          0 :         INIT_LIST_HEAD(&cmd->eh_entry);
    1470                 :            : 
    1471                 :          0 :         atomic_inc(&cmd->device->iodone_cnt);
    1472         [ #  # ]:          0 :         if (cmd->result)
    1473                 :          0 :                 atomic_inc(&cmd->device->ioerr_cnt);
    1474                 :            : 
    1475                 :          0 :         disposition = scsi_decide_disposition(cmd);
    1476         [ #  # ]:          0 :         if (disposition != SUCCESS &&
    1477         [ #  # ]:          0 :             time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
    1478                 :          0 :                 scmd_printk(KERN_ERR, cmd,
    1479                 :            :                             "timing out command, waited %lus\n",
    1480                 :            :                             wait_for/HZ);
    1481                 :            :                 disposition = SUCCESS;
    1482                 :            :         }
    1483                 :            : 
    1484                 :            :         scsi_log_completion(cmd, disposition);
    1485                 :            : 
    1486   [ #  #  #  # ]:          0 :         switch (disposition) {
    1487                 :            :                 case SUCCESS:
    1488                 :          0 :                         scsi_finish_command(cmd);
    1489                 :          0 :                         break;
    1490                 :            :                 case NEEDS_RETRY:
    1491                 :            :                         scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
    1492                 :            :                         break;
    1493                 :            :                 case ADD_TO_MLQUEUE:
    1494                 :            :                         scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
    1495                 :            :                         break;
    1496                 :            :                 default:
    1497                 :          0 :                         scsi_eh_scmd_add(cmd);
    1498                 :          0 :                         break;
    1499                 :            :         }
    1500                 :          0 : }
    1501                 :            : 
    1502                 :            : /**
    1503                 :            :  * scsi_dispatch_command - Dispatch a command to the low-level driver.
    1504                 :            :  * @cmd: command block we are dispatching.
    1505                 :            :  *
    1506                 :            :  * Return: nonzero return request was rejected and device's queue needs to be
    1507                 :            :  * plugged.
    1508                 :            :  */
    1509                 :          0 : static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
    1510                 :            : {
    1511                 :          0 :         struct Scsi_Host *host = cmd->device->host;
    1512                 :            :         int rtn = 0;
    1513                 :            : 
    1514                 :          0 :         atomic_inc(&cmd->device->iorequest_cnt);
    1515                 :            : 
    1516                 :            :         /* check if the device is still usable */
    1517         [ #  # ]:          0 :         if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
    1518                 :            :                 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
    1519                 :            :                  * returns an immediate error upwards, and signals
    1520                 :            :                  * that the device is no longer present */
    1521                 :          0 :                 cmd->result = DID_NO_CONNECT << 16;
    1522                 :          0 :                 goto done;
    1523                 :            :         }
    1524                 :            : 
    1525                 :            :         /* Check to see if the scsi lld made this device blocked. */
    1526         [ #  # ]:          0 :         if (unlikely(scsi_device_blocked(cmd->device))) {
    1527                 :            :                 /*
    1528                 :            :                  * in blocked state, the command is just put back on
    1529                 :            :                  * the device queue.  The suspend state has already
    1530                 :            :                  * blocked the queue so future requests should not
    1531                 :            :                  * occur until the device transitions out of the
    1532                 :            :                  * suspend state.
    1533                 :            :                  */
    1534                 :            :                 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
    1535                 :            :                         "queuecommand : device blocked\n"));
    1536                 :            :                 return SCSI_MLQUEUE_DEVICE_BUSY;
    1537                 :            :         }
    1538                 :            : 
    1539                 :            :         /* Store the LUN value in cmnd, if needed. */
    1540         [ #  # ]:          0 :         if (cmd->device->lun_in_cdb)
    1541                 :          0 :                 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
    1542                 :          0 :                                (cmd->device->lun << 5 & 0xe0);
    1543                 :            : 
    1544                 :            :         scsi_log_send(cmd);
    1545                 :            : 
    1546                 :            :         /*
    1547                 :            :          * Before we queue this command, check if the command
    1548                 :            :          * length exceeds what the host adapter can handle.
    1549                 :            :          */
    1550         [ #  # ]:          0 :         if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
    1551                 :            :                 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
    1552                 :            :                                "queuecommand : command too long. "
    1553                 :            :                                "cdb_size=%d host->max_cmd_len=%d\n",
    1554                 :            :                                cmd->cmd_len, cmd->device->host->max_cmd_len));
    1555                 :          0 :                 cmd->result = (DID_ABORT << 16);
    1556                 :          0 :                 goto done;
    1557                 :            :         }
    1558                 :            : 
    1559         [ #  # ]:          0 :         if (unlikely(host->shost_state == SHOST_DEL)) {
    1560                 :          0 :                 cmd->result = (DID_NO_CONNECT << 16);
    1561                 :          0 :                 goto done;
    1562                 :            : 
    1563                 :            :         }
    1564                 :            : 
    1565                 :          0 :         trace_scsi_dispatch_cmd_start(cmd);
    1566                 :          0 :         rtn = host->hostt->queuecommand(host, cmd);
    1567         [ #  # ]:          0 :         if (rtn) {
    1568                 :          0 :                 trace_scsi_dispatch_cmd_error(cmd, rtn);
    1569         [ #  # ]:          0 :                 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
    1570                 :          0 :                     rtn != SCSI_MLQUEUE_TARGET_BUSY)
    1571                 :            :                         rtn = SCSI_MLQUEUE_HOST_BUSY;
    1572                 :            : 
    1573                 :            :                 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
    1574                 :            :                         "queuecommand : request rejected\n"));
    1575                 :            :         }
    1576                 :            : 
    1577                 :          0 :         return rtn;
    1578                 :            :  done:
    1579                 :          0 :         cmd->scsi_done(cmd);
    1580                 :          0 :         return 0;
    1581                 :            : }
    1582                 :            : 
    1583                 :            : /* Size in bytes of the sg-list stored in the scsi-mq command-private data. */
    1584                 :            : static unsigned int scsi_mq_inline_sgl_size(struct Scsi_Host *shost)
    1585                 :            : {
    1586                 :          0 :         return min_t(unsigned int, shost->sg_tablesize, SCSI_INLINE_SG_CNT) *
    1587                 :            :                 sizeof(struct scatterlist);
    1588                 :            : }
    1589                 :            : 
    1590                 :          0 : static blk_status_t scsi_mq_prep_fn(struct request *req)
    1591                 :            : {
    1592                 :            :         struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
    1593                 :          0 :         struct scsi_device *sdev = req->q->queuedata;
    1594                 :          0 :         struct Scsi_Host *shost = sdev->host;
    1595                 :            :         struct scatterlist *sg;
    1596                 :            : 
    1597                 :          0 :         scsi_init_command(sdev, cmd);
    1598                 :            : 
    1599                 :          0 :         cmd->request = req;
    1600                 :          0 :         cmd->tag = req->tag;
    1601                 :          0 :         cmd->prot_op = SCSI_PROT_NORMAL;
    1602                 :            : 
    1603                 :          0 :         sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
    1604                 :          0 :         cmd->sdb.table.sgl = sg;
    1605                 :            : 
    1606         [ #  # ]:          0 :         if (scsi_host_get_prot(shost)) {
    1607                 :          0 :                 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
    1608                 :            : 
    1609                 :          0 :                 cmd->prot_sdb->table.sgl =
    1610                 :          0 :                         (struct scatterlist *)(cmd->prot_sdb + 1);
    1611                 :            :         }
    1612                 :            : 
    1613                 :          0 :         blk_mq_start_request(req);
    1614                 :            : 
    1615                 :          0 :         return scsi_setup_cmnd(sdev, req);
    1616                 :            : }
    1617                 :            : 
    1618                 :          0 : static void scsi_mq_done(struct scsi_cmnd *cmd)
    1619                 :            : {
    1620         [ #  # ]:          0 :         if (unlikely(test_and_set_bit(SCMD_STATE_COMPLETE, &cmd->state)))
    1621                 :          0 :                 return;
    1622                 :          0 :         trace_scsi_dispatch_cmd_done(cmd);
    1623                 :            : 
    1624                 :            :         /*
    1625                 :            :          * If the block layer didn't complete the request due to a timeout
    1626                 :            :          * injection, scsi must clear its internal completed state so that the
    1627                 :            :          * timeout handler will see it needs to escalate its own error
    1628                 :            :          * recovery.
    1629                 :            :          */
    1630         [ #  # ]:          0 :         if (unlikely(!blk_mq_complete_request(cmd->request)))
    1631                 :          0 :                 clear_bit(SCMD_STATE_COMPLETE, &cmd->state);
    1632                 :            : }
    1633                 :            : 
    1634                 :          0 : static void scsi_mq_put_budget(struct blk_mq_hw_ctx *hctx)
    1635                 :            : {
    1636                 :          0 :         struct request_queue *q = hctx->queue;
    1637                 :          0 :         struct scsi_device *sdev = q->queuedata;
    1638                 :            : 
    1639                 :          0 :         atomic_dec(&sdev->device_busy);
    1640                 :          0 : }
    1641                 :            : 
    1642                 :          0 : static bool scsi_mq_get_budget(struct blk_mq_hw_ctx *hctx)
    1643                 :            : {
    1644                 :          0 :         struct request_queue *q = hctx->queue;
    1645                 :          0 :         struct scsi_device *sdev = q->queuedata;
    1646                 :            : 
    1647         [ #  # ]:          0 :         if (scsi_dev_queue_ready(q, sdev))
    1648                 :            :                 return true;
    1649                 :            : 
    1650   [ #  #  #  # ]:          0 :         if (atomic_read(&sdev->device_busy) == 0 && !scsi_device_blocked(sdev))
    1651                 :          0 :                 blk_mq_delay_run_hw_queue(hctx, SCSI_QUEUE_DELAY);
    1652                 :            :         return false;
    1653                 :            : }
    1654                 :            : 
    1655                 :          0 : static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
    1656                 :            :                          const struct blk_mq_queue_data *bd)
    1657                 :            : {
    1658                 :          0 :         struct request *req = bd->rq;
    1659                 :          0 :         struct request_queue *q = req->q;
    1660                 :          0 :         struct scsi_device *sdev = q->queuedata;
    1661                 :          0 :         struct Scsi_Host *shost = sdev->host;
    1662                 :            :         struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
    1663                 :            :         blk_status_t ret;
    1664                 :            :         int reason;
    1665                 :            : 
    1666                 :            :         /*
    1667                 :            :          * If the device is not in running state we will reject some or all
    1668                 :            :          * commands.
    1669                 :            :          */
    1670         [ #  # ]:          0 :         if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
    1671                 :          0 :                 ret = scsi_prep_state_check(sdev, req);
    1672         [ #  # ]:          0 :                 if (ret != BLK_STS_OK)
    1673                 :            :                         goto out_put_budget;
    1674                 :            :         }
    1675                 :            : 
    1676                 :            :         ret = BLK_STS_RESOURCE;
    1677         [ #  # ]:          0 :         if (!scsi_target_queue_ready(shost, sdev))
    1678                 :            :                 goto out_put_budget;
    1679         [ #  # ]:          0 :         if (!scsi_host_queue_ready(q, shost, sdev))
    1680                 :            :                 goto out_dec_target_busy;
    1681                 :            : 
    1682         [ #  # ]:          0 :         if (!(req->rq_flags & RQF_DONTPREP)) {
    1683                 :          0 :                 ret = scsi_mq_prep_fn(req);
    1684         [ #  # ]:          0 :                 if (ret != BLK_STS_OK)
    1685                 :            :                         goto out_dec_host_busy;
    1686                 :          0 :                 req->rq_flags |= RQF_DONTPREP;
    1687                 :            :         } else {
    1688                 :          0 :                 clear_bit(SCMD_STATE_COMPLETE, &cmd->state);
    1689                 :          0 :                 blk_mq_start_request(req);
    1690                 :            :         }
    1691                 :            : 
    1692                 :          0 :         cmd->flags &= SCMD_PRESERVED_FLAGS;
    1693         [ #  # ]:          0 :         if (sdev->simple_tags)
    1694                 :          0 :                 cmd->flags |= SCMD_TAGGED;
    1695         [ #  # ]:          0 :         if (bd->last)
    1696                 :          0 :                 cmd->flags |= SCMD_LAST;
    1697                 :            : 
    1698                 :          0 :         scsi_init_cmd_errh(cmd);
    1699                 :          0 :         cmd->scsi_done = scsi_mq_done;
    1700                 :            : 
    1701                 :          0 :         reason = scsi_dispatch_cmd(cmd);
    1702         [ #  # ]:          0 :         if (reason) {
    1703                 :          0 :                 scsi_set_blocked(cmd, reason);
    1704                 :            :                 ret = BLK_STS_RESOURCE;
    1705                 :          0 :                 goto out_dec_host_busy;
    1706                 :            :         }
    1707                 :            : 
    1708                 :            :         return BLK_STS_OK;
    1709                 :            : 
    1710                 :            : out_dec_host_busy:
    1711                 :          0 :         scsi_dec_host_busy(shost);
    1712                 :            : out_dec_target_busy:
    1713         [ #  # ]:          0 :         if (scsi_target(sdev)->can_queue > 0)
    1714                 :          0 :                 atomic_dec(&scsi_target(sdev)->target_busy);
    1715                 :            : out_put_budget:
    1716                 :          0 :         scsi_mq_put_budget(hctx);
    1717      [ #  #  # ]:          0 :         switch (ret) {
    1718                 :            :         case BLK_STS_OK:
    1719                 :            :                 break;
    1720                 :            :         case BLK_STS_RESOURCE:
    1721   [ #  #  #  # ]:          0 :                 if (atomic_read(&sdev->device_busy) ||
    1722                 :            :                     scsi_device_blocked(sdev))
    1723                 :            :                         ret = BLK_STS_DEV_RESOURCE;
    1724                 :            :                 break;
    1725                 :            :         default:
    1726         [ #  # ]:          0 :                 if (unlikely(!scsi_device_online(sdev)))
    1727                 :          0 :                         scsi_req(req)->result = DID_NO_CONNECT << 16;
    1728                 :            :                 else
    1729                 :          0 :                         scsi_req(req)->result = DID_ERROR << 16;
    1730                 :            :                 /*
    1731                 :            :                  * Make sure to release all allocated resources when
    1732                 :            :                  * we hit an error, as we will never see this command
    1733                 :            :                  * again.
    1734                 :            :                  */
    1735         [ #  # ]:          0 :                 if (req->rq_flags & RQF_DONTPREP)
    1736                 :          0 :                         scsi_mq_uninit_cmd(cmd);
    1737                 :          0 :                 scsi_run_queue_async(sdev);
    1738                 :          0 :                 break;
    1739                 :            :         }
    1740                 :          0 :         return ret;
    1741                 :            : }
    1742                 :            : 
    1743                 :          0 : static enum blk_eh_timer_return scsi_timeout(struct request *req,
    1744                 :            :                 bool reserved)
    1745                 :            : {
    1746         [ #  # ]:          0 :         if (reserved)
    1747                 :            :                 return BLK_EH_RESET_TIMER;
    1748                 :          0 :         return scsi_times_out(req);
    1749                 :            : }
    1750                 :            : 
    1751                 :          0 : static int scsi_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
    1752                 :            :                                 unsigned int hctx_idx, unsigned int numa_node)
    1753                 :            : {
    1754                 :          0 :         struct Scsi_Host *shost = set->driver_data;
    1755                 :          0 :         const bool unchecked_isa_dma = shost->unchecked_isa_dma;
    1756                 :            :         struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
    1757                 :            :         struct scatterlist *sg;
    1758                 :            : 
    1759         [ #  # ]:          0 :         if (unchecked_isa_dma)
    1760                 :          0 :                 cmd->flags |= SCMD_UNCHECKED_ISA_DMA;
    1761                 :          0 :         cmd->sense_buffer = scsi_alloc_sense_buffer(unchecked_isa_dma,
    1762                 :            :                                                     GFP_KERNEL, numa_node);
    1763         [ #  # ]:          0 :         if (!cmd->sense_buffer)
    1764                 :            :                 return -ENOMEM;
    1765                 :          0 :         cmd->req.sense = cmd->sense_buffer;
    1766                 :            : 
    1767         [ #  # ]:          0 :         if (scsi_host_get_prot(shost)) {
    1768                 :          0 :                 sg = (void *)cmd + sizeof(struct scsi_cmnd) +
    1769                 :          0 :                         shost->hostt->cmd_size;
    1770                 :          0 :                 cmd->prot_sdb = (void *)sg + scsi_mq_inline_sgl_size(shost);
    1771                 :            :         }
    1772                 :            : 
    1773                 :            :         return 0;
    1774                 :            : }
    1775                 :            : 
    1776                 :          0 : static void scsi_mq_exit_request(struct blk_mq_tag_set *set, struct request *rq,
    1777                 :            :                                  unsigned int hctx_idx)
    1778                 :            : {
    1779                 :            :         struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
    1780                 :            : 
    1781                 :          0 :         scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA,
    1782                 :            :                                cmd->sense_buffer);
    1783                 :          0 : }
    1784                 :            : 
    1785                 :          0 : static int scsi_map_queues(struct blk_mq_tag_set *set)
    1786                 :            : {
    1787                 :          0 :         struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
    1788                 :            : 
    1789         [ #  # ]:          0 :         if (shost->hostt->map_queues)
    1790                 :          0 :                 return shost->hostt->map_queues(shost);
    1791                 :          0 :         return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
    1792                 :            : }
    1793                 :            : 
    1794                 :          0 : void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
    1795                 :            : {
    1796                 :          0 :         struct device *dev = shost->dma_dev;
    1797                 :            : 
    1798                 :            :         /*
    1799                 :            :          * this limit is imposed by hardware restrictions
    1800                 :            :          */
    1801         [ #  # ]:          0 :         blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
    1802                 :            :                                         SG_MAX_SEGMENTS));
    1803                 :            : 
    1804         [ #  # ]:          0 :         if (scsi_host_prot_dma(shost)) {
    1805                 :          0 :                 shost->sg_prot_tablesize =
    1806   [ #  #  #  # ]:          0 :                         min_not_zero(shost->sg_prot_tablesize,
    1807                 :            :                                      (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
    1808         [ #  # ]:          0 :                 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
    1809                 :            :                 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
    1810                 :            :         }
    1811                 :            : 
    1812         [ #  # ]:          0 :         if (dev->dma_mask) {
    1813                 :          0 :                 shost->max_sectors = min_t(unsigned int, shost->max_sectors,
    1814                 :            :                                 dma_max_mapping_size(dev) >> SECTOR_SHIFT);
    1815                 :            :         }
    1816                 :          0 :         blk_queue_max_hw_sectors(q, shost->max_sectors);
    1817         [ #  # ]:          0 :         if (shost->unchecked_isa_dma)
    1818                 :          0 :                 blk_queue_bounce_limit(q, BLK_BOUNCE_ISA);
    1819                 :          0 :         blk_queue_segment_boundary(q, shost->dma_boundary);
    1820                 :          0 :         dma_set_seg_boundary(dev, shost->dma_boundary);
    1821                 :            : 
    1822                 :          0 :         blk_queue_max_segment_size(q, shost->max_segment_size);
    1823                 :          0 :         blk_queue_virt_boundary(q, shost->virt_boundary_mask);
    1824                 :            :         dma_set_max_seg_size(dev, queue_max_segment_size(q));
    1825                 :            : 
    1826                 :            :         /*
    1827                 :            :          * Set a reasonable default alignment:  The larger of 32-byte (dword),
    1828                 :            :          * which is a common minimum for HBAs, and the minimum DMA alignment,
    1829                 :            :          * which is set by the platform.
    1830                 :            :          *
    1831                 :            :          * Devices that require a bigger alignment can increase it later.
    1832                 :            :          */
    1833                 :          0 :         blk_queue_dma_alignment(q, max(4, dma_get_cache_alignment()) - 1);
    1834                 :          0 : }
    1835                 :            : EXPORT_SYMBOL_GPL(__scsi_init_queue);
    1836                 :            : 
    1837                 :            : static const struct blk_mq_ops scsi_mq_ops_no_commit = {
    1838                 :            :         .get_budget     = scsi_mq_get_budget,
    1839                 :            :         .put_budget     = scsi_mq_put_budget,
    1840                 :            :         .queue_rq       = scsi_queue_rq,
    1841                 :            :         .complete       = scsi_softirq_done,
    1842                 :            :         .timeout        = scsi_timeout,
    1843                 :            : #ifdef CONFIG_BLK_DEBUG_FS
    1844                 :            :         .show_rq        = scsi_show_rq,
    1845                 :            : #endif
    1846                 :            :         .init_request   = scsi_mq_init_request,
    1847                 :            :         .exit_request   = scsi_mq_exit_request,
    1848                 :            :         .initialize_rq_fn = scsi_initialize_rq,
    1849                 :            :         .cleanup_rq     = scsi_cleanup_rq,
    1850                 :            :         .busy           = scsi_mq_lld_busy,
    1851                 :            :         .map_queues     = scsi_map_queues,
    1852                 :            : };
    1853                 :            : 
    1854                 :            : 
    1855                 :          0 : static void scsi_commit_rqs(struct blk_mq_hw_ctx *hctx)
    1856                 :            : {
    1857                 :          0 :         struct request_queue *q = hctx->queue;
    1858                 :          0 :         struct scsi_device *sdev = q->queuedata;
    1859                 :          0 :         struct Scsi_Host *shost = sdev->host;
    1860                 :            : 
    1861                 :          0 :         shost->hostt->commit_rqs(shost, hctx->queue_num);
    1862                 :          0 : }
    1863                 :            : 
    1864                 :            : static const struct blk_mq_ops scsi_mq_ops = {
    1865                 :            :         .get_budget     = scsi_mq_get_budget,
    1866                 :            :         .put_budget     = scsi_mq_put_budget,
    1867                 :            :         .queue_rq       = scsi_queue_rq,
    1868                 :            :         .commit_rqs     = scsi_commit_rqs,
    1869                 :            :         .complete       = scsi_softirq_done,
    1870                 :            :         .timeout        = scsi_timeout,
    1871                 :            : #ifdef CONFIG_BLK_DEBUG_FS
    1872                 :            :         .show_rq        = scsi_show_rq,
    1873                 :            : #endif
    1874                 :            :         .init_request   = scsi_mq_init_request,
    1875                 :            :         .exit_request   = scsi_mq_exit_request,
    1876                 :            :         .initialize_rq_fn = scsi_initialize_rq,
    1877                 :            :         .cleanup_rq     = scsi_cleanup_rq,
    1878                 :            :         .busy           = scsi_mq_lld_busy,
    1879                 :            :         .map_queues     = scsi_map_queues,
    1880                 :            : };
    1881                 :            : 
    1882                 :          0 : struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
    1883                 :            : {
    1884                 :          0 :         sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
    1885         [ #  # ]:          0 :         if (IS_ERR(sdev->request_queue))
    1886                 :            :                 return NULL;
    1887                 :            : 
    1888                 :          0 :         sdev->request_queue->queuedata = sdev;
    1889                 :          0 :         __scsi_init_queue(sdev->host, sdev->request_queue);
    1890                 :          0 :         blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, sdev->request_queue);
    1891                 :          0 :         return sdev->request_queue;
    1892                 :            : }
    1893                 :            : 
    1894                 :          0 : int scsi_mq_setup_tags(struct Scsi_Host *shost)
    1895                 :            : {
    1896                 :            :         unsigned int cmd_size, sgl_size;
    1897                 :            : 
    1898                 :          0 :         sgl_size = max_t(unsigned int, sizeof(struct scatterlist),
    1899                 :            :                                 scsi_mq_inline_sgl_size(shost));
    1900                 :          0 :         cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
    1901         [ #  # ]:          0 :         if (scsi_host_get_prot(shost))
    1902                 :          0 :                 cmd_size += sizeof(struct scsi_data_buffer) +
    1903                 :            :                         sizeof(struct scatterlist) * SCSI_INLINE_PROT_SG_CNT;
    1904                 :            : 
    1905                 :          0 :         memset(&shost->tag_set, 0, sizeof(shost->tag_set));
    1906         [ #  # ]:          0 :         if (shost->hostt->commit_rqs)
    1907                 :          0 :                 shost->tag_set.ops = &scsi_mq_ops;
    1908                 :            :         else
    1909                 :          0 :                 shost->tag_set.ops = &scsi_mq_ops_no_commit;
    1910         [ #  # ]:          0 :         shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
    1911                 :          0 :         shost->tag_set.queue_depth = shost->can_queue;
    1912                 :          0 :         shost->tag_set.cmd_size = cmd_size;
    1913                 :          0 :         shost->tag_set.numa_node = NUMA_NO_NODE;
    1914                 :          0 :         shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
    1915                 :          0 :         shost->tag_set.flags |=
    1916                 :          0 :                 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
    1917                 :          0 :         shost->tag_set.driver_data = shost;
    1918                 :            : 
    1919                 :          0 :         return blk_mq_alloc_tag_set(&shost->tag_set);
    1920                 :            : }
    1921                 :            : 
    1922                 :          0 : void scsi_mq_destroy_tags(struct Scsi_Host *shost)
    1923                 :            : {
    1924                 :          0 :         blk_mq_free_tag_set(&shost->tag_set);
    1925                 :          0 : }
    1926                 :            : 
    1927                 :            : /**
    1928                 :            :  * scsi_device_from_queue - return sdev associated with a request_queue
    1929                 :            :  * @q: The request queue to return the sdev from
    1930                 :            :  *
    1931                 :            :  * Return the sdev associated with a request queue or NULL if the
    1932                 :            :  * request_queue does not reference a SCSI device.
    1933                 :            :  */
    1934                 :          0 : struct scsi_device *scsi_device_from_queue(struct request_queue *q)
    1935                 :            : {
    1936                 :            :         struct scsi_device *sdev = NULL;
    1937                 :            : 
    1938   [ #  #  #  # ]:          0 :         if (q->mq_ops == &scsi_mq_ops_no_commit ||
    1939                 :            :             q->mq_ops == &scsi_mq_ops)
    1940                 :          0 :                 sdev = q->queuedata;
    1941   [ #  #  #  # ]:          0 :         if (!sdev || !get_device(&sdev->sdev_gendev))
    1942                 :            :                 sdev = NULL;
    1943                 :            : 
    1944                 :          0 :         return sdev;
    1945                 :            : }
    1946                 :            : EXPORT_SYMBOL_GPL(scsi_device_from_queue);
    1947                 :            : 
    1948                 :            : /*
    1949                 :            :  * Function:    scsi_block_requests()
    1950                 :            :  *
    1951                 :            :  * Purpose:     Utility function used by low-level drivers to prevent further
    1952                 :            :  *              commands from being queued to the device.
    1953                 :            :  *
    1954                 :            :  * Arguments:   shost       - Host in question
    1955                 :            :  *
    1956                 :            :  * Returns:     Nothing
    1957                 :            :  *
    1958                 :            :  * Lock status: No locks are assumed held.
    1959                 :            :  *
    1960                 :            :  * Notes:       There is no timer nor any other means by which the requests
    1961                 :            :  *              get unblocked other than the low-level driver calling
    1962                 :            :  *              scsi_unblock_requests().
    1963                 :            :  */
    1964                 :          0 : void scsi_block_requests(struct Scsi_Host *shost)
    1965                 :            : {
    1966                 :          0 :         shost->host_self_blocked = 1;
    1967                 :          0 : }
    1968                 :            : EXPORT_SYMBOL(scsi_block_requests);
    1969                 :            : 
    1970                 :            : /*
    1971                 :            :  * Function:    scsi_unblock_requests()
    1972                 :            :  *
    1973                 :            :  * Purpose:     Utility function used by low-level drivers to allow further
    1974                 :            :  *              commands from being queued to the device.
    1975                 :            :  *
    1976                 :            :  * Arguments:   shost       - Host in question
    1977                 :            :  *
    1978                 :            :  * Returns:     Nothing
    1979                 :            :  *
    1980                 :            :  * Lock status: No locks are assumed held.
    1981                 :            :  *
    1982                 :            :  * Notes:       There is no timer nor any other means by which the requests
    1983                 :            :  *              get unblocked other than the low-level driver calling
    1984                 :            :  *              scsi_unblock_requests().
    1985                 :            :  *
    1986                 :            :  *              This is done as an API function so that changes to the
    1987                 :            :  *              internals of the scsi mid-layer won't require wholesale
    1988                 :            :  *              changes to drivers that use this feature.
    1989                 :            :  */
    1990                 :          0 : void scsi_unblock_requests(struct Scsi_Host *shost)
    1991                 :            : {
    1992                 :          0 :         shost->host_self_blocked = 0;
    1993                 :          0 :         scsi_run_host_queues(shost);
    1994                 :          0 : }
    1995                 :            : EXPORT_SYMBOL(scsi_unblock_requests);
    1996                 :            : 
    1997                 :        207 : int __init scsi_init_queue(void)
    1998                 :            : {
    1999                 :        207 :         scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
    2000                 :            :                                            sizeof(struct scsi_data_buffer),
    2001                 :            :                                            0, 0, NULL);
    2002         [ -  + ]:        207 :         if (!scsi_sdb_cache) {
    2003                 :          0 :                 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
    2004                 :          0 :                 return -ENOMEM;
    2005                 :            :         }
    2006                 :            : 
    2007                 :            :         return 0;
    2008                 :            : }
    2009                 :            : 
    2010                 :          0 : void scsi_exit_queue(void)
    2011                 :            : {
    2012                 :          0 :         kmem_cache_destroy(scsi_sense_cache);
    2013                 :          0 :         kmem_cache_destroy(scsi_sense_isadma_cache);
    2014                 :          0 :         kmem_cache_destroy(scsi_sdb_cache);
    2015                 :          0 : }
    2016                 :            : 
    2017                 :            : /**
    2018                 :            :  *      scsi_mode_select - issue a mode select
    2019                 :            :  *      @sdev:  SCSI device to be queried
    2020                 :            :  *      @pf:    Page format bit (1 == standard, 0 == vendor specific)
    2021                 :            :  *      @sp:    Save page bit (0 == don't save, 1 == save)
    2022                 :            :  *      @modepage: mode page being requested
    2023                 :            :  *      @buffer: request buffer (may not be smaller than eight bytes)
    2024                 :            :  *      @len:   length of request buffer.
    2025                 :            :  *      @timeout: command timeout
    2026                 :            :  *      @retries: number of retries before failing
    2027                 :            :  *      @data: returns a structure abstracting the mode header data
    2028                 :            :  *      @sshdr: place to put sense data (or NULL if no sense to be collected).
    2029                 :            :  *              must be SCSI_SENSE_BUFFERSIZE big.
    2030                 :            :  *
    2031                 :            :  *      Returns zero if successful; negative error number or scsi
    2032                 :            :  *      status on error
    2033                 :            :  *
    2034                 :            :  */
    2035                 :            : int
    2036                 :          0 : scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
    2037                 :            :                  unsigned char *buffer, int len, int timeout, int retries,
    2038                 :            :                  struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
    2039                 :            : {
    2040                 :            :         unsigned char cmd[10];
    2041                 :            :         unsigned char *real_buffer;
    2042                 :            :         int ret;
    2043                 :            : 
    2044                 :          0 :         memset(cmd, 0, sizeof(cmd));
    2045         [ #  # ]:          0 :         cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
    2046                 :            : 
    2047         [ #  # ]:          0 :         if (sdev->use_10_for_ms) {
    2048         [ #  # ]:          0 :                 if (len > 65535)
    2049                 :            :                         return -EINVAL;
    2050                 :          0 :                 real_buffer = kmalloc(8 + len, GFP_KERNEL);
    2051         [ #  # ]:          0 :                 if (!real_buffer)
    2052                 :            :                         return -ENOMEM;
    2053                 :          0 :                 memcpy(real_buffer + 8, buffer, len);
    2054                 :            :                 len += 8;
    2055                 :          0 :                 real_buffer[0] = 0;
    2056                 :          0 :                 real_buffer[1] = 0;
    2057                 :          0 :                 real_buffer[2] = data->medium_type;
    2058                 :          0 :                 real_buffer[3] = data->device_specific;
    2059                 :          0 :                 real_buffer[4] = data->longlba ? 0x01 : 0;
    2060                 :          0 :                 real_buffer[5] = 0;
    2061                 :          0 :                 real_buffer[6] = data->block_descriptor_length >> 8;
    2062                 :          0 :                 real_buffer[7] = data->block_descriptor_length;
    2063                 :            : 
    2064                 :          0 :                 cmd[0] = MODE_SELECT_10;
    2065                 :          0 :                 cmd[7] = len >> 8;
    2066                 :          0 :                 cmd[8] = len;
    2067                 :            :         } else {
    2068   [ #  #  #  #  :          0 :                 if (len > 255 || data->block_descriptor_length > 255 ||
                   #  # ]
    2069                 :            :                     data->longlba)
    2070                 :            :                         return -EINVAL;
    2071                 :            : 
    2072                 :          0 :                 real_buffer = kmalloc(4 + len, GFP_KERNEL);
    2073         [ #  # ]:          0 :                 if (!real_buffer)
    2074                 :            :                         return -ENOMEM;
    2075                 :          0 :                 memcpy(real_buffer + 4, buffer, len);
    2076                 :            :                 len += 4;
    2077                 :          0 :                 real_buffer[0] = 0;
    2078                 :          0 :                 real_buffer[1] = data->medium_type;
    2079                 :          0 :                 real_buffer[2] = data->device_specific;
    2080                 :          0 :                 real_buffer[3] = data->block_descriptor_length;
    2081                 :            :                 
    2082                 :            : 
    2083                 :          0 :                 cmd[0] = MODE_SELECT;
    2084                 :          0 :                 cmd[4] = len;
    2085                 :            :         }
    2086                 :            : 
    2087                 :          0 :         ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
    2088                 :            :                                sshdr, timeout, retries, NULL);
    2089                 :          0 :         kfree(real_buffer);
    2090                 :          0 :         return ret;
    2091                 :            : }
    2092                 :            : EXPORT_SYMBOL_GPL(scsi_mode_select);
    2093                 :            : 
    2094                 :            : /**
    2095                 :            :  *      scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
    2096                 :            :  *      @sdev:  SCSI device to be queried
    2097                 :            :  *      @dbd:   set if mode sense will allow block descriptors to be returned
    2098                 :            :  *      @modepage: mode page being requested
    2099                 :            :  *      @buffer: request buffer (may not be smaller than eight bytes)
    2100                 :            :  *      @len:   length of request buffer.
    2101                 :            :  *      @timeout: command timeout
    2102                 :            :  *      @retries: number of retries before failing
    2103                 :            :  *      @data: returns a structure abstracting the mode header data
    2104                 :            :  *      @sshdr: place to put sense data (or NULL if no sense to be collected).
    2105                 :            :  *              must be SCSI_SENSE_BUFFERSIZE big.
    2106                 :            :  *
    2107                 :            :  *      Returns zero if unsuccessful, or the header offset (either 4
    2108                 :            :  *      or 8 depending on whether a six or ten byte command was
    2109                 :            :  *      issued) if successful.
    2110                 :            :  */
    2111                 :            : int
    2112                 :          0 : scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
    2113                 :            :                   unsigned char *buffer, int len, int timeout, int retries,
    2114                 :            :                   struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
    2115                 :            : {
    2116                 :            :         unsigned char cmd[12];
    2117                 :            :         int use_10_for_ms;
    2118                 :            :         int header_length;
    2119                 :            :         int result, retry_count = retries;
    2120                 :            :         struct scsi_sense_hdr my_sshdr;
    2121                 :            : 
    2122                 :          0 :         memset(data, 0, sizeof(*data));
    2123                 :          0 :         memset(&cmd[0], 0, 12);
    2124                 :          0 :         cmd[1] = dbd & 0x18;        /* allows DBD and LLBA bits */
    2125                 :          0 :         cmd[2] = modepage;
    2126                 :            : 
    2127                 :            :         /* caller might not be interested in sense, but we need it */
    2128         [ #  # ]:          0 :         if (!sshdr)
    2129                 :            :                 sshdr = &my_sshdr;
    2130                 :            : 
    2131                 :            :  retry:
    2132                 :          0 :         use_10_for_ms = sdev->use_10_for_ms;
    2133                 :            : 
    2134         [ #  # ]:          0 :         if (use_10_for_ms) {
    2135         [ #  # ]:          0 :                 if (len < 8)
    2136                 :            :                         len = 8;
    2137                 :            : 
    2138                 :          0 :                 cmd[0] = MODE_SENSE_10;
    2139                 :          0 :                 cmd[8] = len;
    2140                 :            :                 header_length = 8;
    2141                 :            :         } else {
    2142         [ #  # ]:          0 :                 if (len < 4)
    2143                 :            :                         len = 4;
    2144                 :            : 
    2145                 :          0 :                 cmd[0] = MODE_SENSE;
    2146                 :          0 :                 cmd[4] = len;
    2147                 :            :                 header_length = 4;
    2148                 :            :         }
    2149                 :            : 
    2150                 :          0 :         memset(buffer, 0, len);
    2151                 :            : 
    2152                 :            :         result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
    2153                 :            :                                   sshdr, timeout, retries, NULL);
    2154                 :            : 
    2155                 :            :         /* This code looks awful: what it's doing is making sure an
    2156                 :            :          * ILLEGAL REQUEST sense return identifies the actual command
    2157                 :            :          * byte as the problem.  MODE_SENSE commands can return
    2158                 :            :          * ILLEGAL REQUEST if the code page isn't supported */
    2159                 :            : 
    2160   [ #  #  #  #  :          0 :         if (use_10_for_ms && !scsi_status_is_good(result) &&
                   #  # ]
    2161                 :          0 :             driver_byte(result) == DRIVER_SENSE) {
    2162         [ #  # ]:          0 :                 if (scsi_sense_valid(sshdr)) {
    2163   [ #  #  #  # ]:          0 :                         if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
    2164         [ #  # ]:          0 :                             (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
    2165                 :            :                                 /* 
    2166                 :            :                                  * Invalid command operation code
    2167                 :            :                                  */
    2168                 :          0 :                                 sdev->use_10_for_ms = 0;
    2169                 :          0 :                                 goto retry;
    2170                 :            :                         }
    2171                 :            :                 }
    2172                 :            :         }
    2173                 :            : 
    2174         [ #  # ]:          0 :         if(scsi_status_is_good(result)) {
    2175   [ #  #  #  #  :          0 :                 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
             #  #  #  # ]
    2176                 :            :                              (modepage == 6 || modepage == 8))) {
    2177                 :            :                         /* Initio breakage? */
    2178                 :            :                         header_length = 0;
    2179                 :          0 :                         data->length = 13;
    2180                 :          0 :                         data->medium_type = 0;
    2181                 :          0 :                         data->device_specific = 0;
    2182                 :          0 :                         data->longlba = 0;
    2183                 :          0 :                         data->block_descriptor_length = 0;
    2184         [ #  # ]:          0 :                 } else if(use_10_for_ms) {
    2185                 :          0 :                         data->length = buffer[0]*256 + buffer[1] + 2;
    2186                 :          0 :                         data->medium_type = buffer[2];
    2187                 :          0 :                         data->device_specific = buffer[3];
    2188                 :          0 :                         data->longlba = buffer[4] & 0x01;
    2189                 :          0 :                         data->block_descriptor_length = buffer[6]*256
    2190                 :          0 :                                 + buffer[7];
    2191                 :            :                 } else {
    2192                 :          0 :                         data->length = buffer[0] + 1;
    2193                 :          0 :                         data->medium_type = buffer[1];
    2194                 :          0 :                         data->device_specific = buffer[2];
    2195                 :          0 :                         data->block_descriptor_length = buffer[3];
    2196                 :            :                 }
    2197                 :          0 :                 data->header_length = header_length;
    2198   [ #  #  #  # ]:          0 :         } else if ((status_byte(result) == CHECK_CONDITION) &&
    2199         [ #  # ]:          0 :                    scsi_sense_valid(sshdr) &&
    2200         [ #  # ]:          0 :                    sshdr->sense_key == UNIT_ATTENTION && retry_count) {
    2201                 :          0 :                 retry_count--;
    2202                 :          0 :                 goto retry;
    2203                 :            :         }
    2204                 :            : 
    2205                 :          0 :         return result;
    2206                 :            : }
    2207                 :            : EXPORT_SYMBOL(scsi_mode_sense);
    2208                 :            : 
    2209                 :            : /**
    2210                 :            :  *      scsi_test_unit_ready - test if unit is ready
    2211                 :            :  *      @sdev:  scsi device to change the state of.
    2212                 :            :  *      @timeout: command timeout
    2213                 :            :  *      @retries: number of retries before failing
    2214                 :            :  *      @sshdr: outpout pointer for decoded sense information.
    2215                 :            :  *
    2216                 :            :  *      Returns zero if unsuccessful or an error if TUR failed.  For
    2217                 :            :  *      removable media, UNIT_ATTENTION sets ->changed flag.
    2218                 :            :  **/
    2219                 :            : int
    2220                 :          0 : scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
    2221                 :            :                      struct scsi_sense_hdr *sshdr)
    2222                 :            : {
    2223                 :          0 :         char cmd[] = {
    2224                 :            :                 TEST_UNIT_READY, 0, 0, 0, 0, 0,
    2225                 :            :         };
    2226                 :            :         int result;
    2227                 :            : 
    2228                 :            :         /* try to eat the UNIT_ATTENTION if there are enough retries */
    2229                 :            :         do {
    2230                 :            :                 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
    2231                 :            :                                           timeout, 1, NULL);
    2232   [ #  #  #  #  :          0 :                 if (sdev->removable && scsi_sense_valid(sshdr) &&
                   #  # ]
    2233                 :          0 :                     sshdr->sense_key == UNIT_ATTENTION)
    2234                 :          0 :                         sdev->changed = 1;
    2235         [ #  # ]:          0 :         } while (scsi_sense_valid(sshdr) &&
    2236   [ #  #  #  # ]:          0 :                  sshdr->sense_key == UNIT_ATTENTION && --retries);
    2237                 :            : 
    2238                 :          0 :         return result;
    2239                 :            : }
    2240                 :            : EXPORT_SYMBOL(scsi_test_unit_ready);
    2241                 :            : 
    2242                 :            : /**
    2243                 :            :  *      scsi_device_set_state - Take the given device through the device state model.
    2244                 :            :  *      @sdev:  scsi device to change the state of.
    2245                 :            :  *      @state: state to change to.
    2246                 :            :  *
    2247                 :            :  *      Returns zero if successful or an error if the requested
    2248                 :            :  *      transition is illegal.
    2249                 :            :  */
    2250                 :            : int
    2251                 :          0 : scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
    2252                 :            : {
    2253                 :          0 :         enum scsi_device_state oldstate = sdev->sdev_state;
    2254                 :            : 
    2255         [ #  # ]:          0 :         if (state == oldstate)
    2256                 :            :                 return 0;
    2257                 :            : 
    2258   [ #  #  #  #  :          0 :         switch (state) {
             #  #  #  #  
                      # ]
    2259                 :            :         case SDEV_CREATED:
    2260         [ #  # ]:          0 :                 switch (oldstate) {
    2261                 :            :                 case SDEV_CREATED_BLOCK:
    2262                 :            :                         break;
    2263                 :            :                 default:
    2264                 :            :                         goto illegal;
    2265                 :            :                 }
    2266                 :            :                 break;
    2267                 :            :                         
    2268                 :            :         case SDEV_RUNNING:
    2269         [ #  # ]:          0 :                 switch (oldstate) {
    2270                 :            :                 case SDEV_CREATED:
    2271                 :            :                 case SDEV_OFFLINE:
    2272                 :            :                 case SDEV_TRANSPORT_OFFLINE:
    2273                 :            :                 case SDEV_QUIESCE:
    2274                 :            :                 case SDEV_BLOCK:
    2275                 :            :                         break;
    2276                 :            :                 default:
    2277                 :            :                         goto illegal;
    2278                 :            :                 }
    2279                 :            :                 break;
    2280                 :            : 
    2281                 :            :         case SDEV_QUIESCE:
    2282         [ #  # ]:          0 :                 switch (oldstate) {
    2283                 :            :                 case SDEV_RUNNING:
    2284                 :            :                 case SDEV_OFFLINE:
    2285                 :            :                 case SDEV_TRANSPORT_OFFLINE:
    2286                 :            :                         break;
    2287                 :            :                 default:
    2288                 :            :                         goto illegal;
    2289                 :            :                 }
    2290                 :            :                 break;
    2291                 :            : 
    2292                 :            :         case SDEV_OFFLINE:
    2293                 :            :         case SDEV_TRANSPORT_OFFLINE:
    2294         [ #  # ]:          0 :                 switch (oldstate) {
    2295                 :            :                 case SDEV_CREATED:
    2296                 :            :                 case SDEV_RUNNING:
    2297                 :            :                 case SDEV_QUIESCE:
    2298                 :            :                 case SDEV_BLOCK:
    2299                 :            :                         break;
    2300                 :            :                 default:
    2301                 :            :                         goto illegal;
    2302                 :            :                 }
    2303                 :            :                 break;
    2304                 :            : 
    2305                 :            :         case SDEV_BLOCK:
    2306         [ #  # ]:          0 :                 switch (oldstate) {
    2307                 :            :                 case SDEV_RUNNING:
    2308                 :            :                 case SDEV_CREATED_BLOCK:
    2309                 :            :                 case SDEV_OFFLINE:
    2310                 :            :                         break;
    2311                 :            :                 default:
    2312                 :            :                         goto illegal;
    2313                 :            :                 }
    2314                 :            :                 break;
    2315                 :            : 
    2316                 :            :         case SDEV_CREATED_BLOCK:
    2317         [ #  # ]:          0 :                 switch (oldstate) {
    2318                 :            :                 case SDEV_CREATED:
    2319                 :            :                         break;
    2320                 :            :                 default:
    2321                 :            :                         goto illegal;
    2322                 :            :                 }
    2323                 :            :                 break;
    2324                 :            : 
    2325                 :            :         case SDEV_CANCEL:
    2326         [ #  # ]:          0 :                 switch (oldstate) {
    2327                 :            :                 case SDEV_CREATED:
    2328                 :            :                 case SDEV_RUNNING:
    2329                 :            :                 case SDEV_QUIESCE:
    2330                 :            :                 case SDEV_OFFLINE:
    2331                 :            :                 case SDEV_TRANSPORT_OFFLINE:
    2332                 :            :                         break;
    2333                 :            :                 default:
    2334                 :            :                         goto illegal;
    2335                 :            :                 }
    2336                 :            :                 break;
    2337                 :            : 
    2338                 :            :         case SDEV_DEL:
    2339         [ #  # ]:          0 :                 switch (oldstate) {
    2340                 :            :                 case SDEV_CREATED:
    2341                 :            :                 case SDEV_RUNNING:
    2342                 :            :                 case SDEV_OFFLINE:
    2343                 :            :                 case SDEV_TRANSPORT_OFFLINE:
    2344                 :            :                 case SDEV_CANCEL:
    2345                 :            :                 case SDEV_BLOCK:
    2346                 :            :                 case SDEV_CREATED_BLOCK:
    2347                 :            :                         break;
    2348                 :            :                 default:
    2349                 :            :                         goto illegal;
    2350                 :            :                 }
    2351                 :            :                 break;
    2352                 :            : 
    2353                 :            :         }
    2354                 :          0 :         sdev->sdev_state = state;
    2355                 :          0 :         return 0;
    2356                 :            : 
    2357                 :            :  illegal:
    2358                 :            :         SCSI_LOG_ERROR_RECOVERY(1,
    2359                 :            :                                 sdev_printk(KERN_ERR, sdev,
    2360                 :            :                                             "Illegal state transition %s->%s",
    2361                 :            :                                             scsi_device_state_name(oldstate),
    2362                 :            :                                             scsi_device_state_name(state))
    2363                 :            :                                 );
    2364                 :            :         return -EINVAL;
    2365                 :            : }
    2366                 :            : EXPORT_SYMBOL(scsi_device_set_state);
    2367                 :            : 
    2368                 :            : /**
    2369                 :            :  *      sdev_evt_emit - emit a single SCSI device uevent
    2370                 :            :  *      @sdev: associated SCSI device
    2371                 :            :  *      @evt: event to emit
    2372                 :            :  *
    2373                 :            :  *      Send a single uevent (scsi_event) to the associated scsi_device.
    2374                 :            :  */
    2375                 :          0 : static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
    2376                 :            : {
    2377                 :            :         int idx = 0;
    2378                 :            :         char *envp[3];
    2379                 :            : 
    2380   [ #  #  #  #  :          0 :         switch (evt->evt_type) {
             #  #  #  #  
                      # ]
    2381                 :            :         case SDEV_EVT_MEDIA_CHANGE:
    2382                 :          0 :                 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
    2383                 :          0 :                 break;
    2384                 :            :         case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
    2385                 :          0 :                 scsi_rescan_device(&sdev->sdev_gendev);
    2386                 :          0 :                 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
    2387                 :          0 :                 break;
    2388                 :            :         case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
    2389                 :          0 :                 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
    2390                 :          0 :                 break;
    2391                 :            :         case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
    2392                 :          0 :                envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
    2393                 :          0 :                 break;
    2394                 :            :         case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
    2395                 :          0 :                 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
    2396                 :          0 :                 break;
    2397                 :            :         case SDEV_EVT_LUN_CHANGE_REPORTED:
    2398                 :          0 :                 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
    2399                 :          0 :                 break;
    2400                 :            :         case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
    2401                 :          0 :                 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
    2402                 :          0 :                 break;
    2403                 :            :         case SDEV_EVT_POWER_ON_RESET_OCCURRED:
    2404                 :          0 :                 envp[idx++] = "SDEV_UA=POWER_ON_RESET_OCCURRED";
    2405                 :          0 :                 break;
    2406                 :            :         default:
    2407                 :            :                 /* do nothing */
    2408                 :            :                 break;
    2409                 :            :         }
    2410                 :            : 
    2411                 :          0 :         envp[idx++] = NULL;
    2412                 :            : 
    2413                 :          0 :         kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
    2414                 :          0 : }
    2415                 :            : 
    2416                 :            : /**
    2417                 :            :  *      sdev_evt_thread - send a uevent for each scsi event
    2418                 :            :  *      @work: work struct for scsi_device
    2419                 :            :  *
    2420                 :            :  *      Dispatch queued events to their associated scsi_device kobjects
    2421                 :            :  *      as uevents.
    2422                 :            :  */
    2423                 :          0 : void scsi_evt_thread(struct work_struct *work)
    2424                 :            : {
    2425                 :            :         struct scsi_device *sdev;
    2426                 :            :         enum scsi_device_event evt_type;
    2427                 :          0 :         LIST_HEAD(event_list);
    2428                 :            : 
    2429                 :          0 :         sdev = container_of(work, struct scsi_device, event_work);
    2430                 :            : 
    2431         [ #  # ]:          0 :         for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
    2432         [ #  # ]:          0 :                 if (test_and_clear_bit(evt_type, sdev->pending_events))
    2433                 :          0 :                         sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
    2434                 :            : 
    2435                 :            :         while (1) {
    2436                 :            :                 struct scsi_event *evt;
    2437                 :            :                 struct list_head *this, *tmp;
    2438                 :            :                 unsigned long flags;
    2439                 :            : 
    2440                 :          0 :                 spin_lock_irqsave(&sdev->list_lock, flags);
    2441                 :          0 :                 list_splice_init(&sdev->event_list, &event_list);
    2442                 :            :                 spin_unlock_irqrestore(&sdev->list_lock, flags);
    2443                 :            : 
    2444         [ #  # ]:          0 :                 if (list_empty(&event_list))
    2445                 :            :                         break;
    2446                 :            : 
    2447         [ #  # ]:          0 :                 list_for_each_safe(this, tmp, &event_list) {
    2448                 :          0 :                         evt = list_entry(this, struct scsi_event, node);
    2449                 :            :                         list_del(&evt->node);
    2450                 :          0 :                         scsi_evt_emit(sdev, evt);
    2451                 :          0 :                         kfree(evt);
    2452                 :            :                 }
    2453                 :            :         }
    2454                 :          0 : }
    2455                 :            : 
    2456                 :            : /**
    2457                 :            :  *      sdev_evt_send - send asserted event to uevent thread
    2458                 :            :  *      @sdev: scsi_device event occurred on
    2459                 :            :  *      @evt: event to send
    2460                 :            :  *
    2461                 :            :  *      Assert scsi device event asynchronously.
    2462                 :            :  */
    2463                 :          0 : void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
    2464                 :            : {
    2465                 :            :         unsigned long flags;
    2466                 :            : 
    2467                 :            : #if 0
    2468                 :            :         /* FIXME: currently this check eliminates all media change events
    2469                 :            :          * for polled devices.  Need to update to discriminate between AN
    2470                 :            :          * and polled events */
    2471                 :            :         if (!test_bit(evt->evt_type, sdev->supported_events)) {
    2472                 :            :                 kfree(evt);
    2473                 :            :                 return;
    2474                 :            :         }
    2475                 :            : #endif
    2476                 :            : 
    2477                 :          0 :         spin_lock_irqsave(&sdev->list_lock, flags);
    2478                 :          0 :         list_add_tail(&evt->node, &sdev->event_list);
    2479                 :          0 :         schedule_work(&sdev->event_work);
    2480                 :            :         spin_unlock_irqrestore(&sdev->list_lock, flags);
    2481                 :          0 : }
    2482                 :            : EXPORT_SYMBOL_GPL(sdev_evt_send);
    2483                 :            : 
    2484                 :            : /**
    2485                 :            :  *      sdev_evt_alloc - allocate a new scsi event
    2486                 :            :  *      @evt_type: type of event to allocate
    2487                 :            :  *      @gfpflags: GFP flags for allocation
    2488                 :            :  *
    2489                 :            :  *      Allocates and returns a new scsi_event.
    2490                 :            :  */
    2491                 :          0 : struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
    2492                 :            :                                   gfp_t gfpflags)
    2493                 :            : {
    2494                 :          0 :         struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
    2495   [ #  #  #  # ]:          0 :         if (!evt)
    2496                 :            :                 return NULL;
    2497                 :            : 
    2498                 :          0 :         evt->evt_type = evt_type;
    2499                 :          0 :         INIT_LIST_HEAD(&evt->node);
    2500                 :            : 
    2501                 :            :         /* evt_type-specific initialization, if any */
    2502                 :            :         switch (evt_type) {
    2503                 :            :         case SDEV_EVT_MEDIA_CHANGE:
    2504                 :            :         case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
    2505                 :            :         case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
    2506                 :            :         case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
    2507                 :            :         case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
    2508                 :            :         case SDEV_EVT_LUN_CHANGE_REPORTED:
    2509                 :            :         case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
    2510                 :            :         case SDEV_EVT_POWER_ON_RESET_OCCURRED:
    2511                 :            :         default:
    2512                 :            :                 /* do nothing */
    2513                 :            :                 break;
    2514                 :            :         }
    2515                 :            : 
    2516                 :          0 :         return evt;
    2517                 :            : }
    2518                 :            : EXPORT_SYMBOL_GPL(sdev_evt_alloc);
    2519                 :            : 
    2520                 :            : /**
    2521                 :            :  *      sdev_evt_send_simple - send asserted event to uevent thread
    2522                 :            :  *      @sdev: scsi_device event occurred on
    2523                 :            :  *      @evt_type: type of event to send
    2524                 :            :  *      @gfpflags: GFP flags for allocation
    2525                 :            :  *
    2526                 :            :  *      Assert scsi device event asynchronously, given an event type.
    2527                 :            :  */
    2528                 :          0 : void sdev_evt_send_simple(struct scsi_device *sdev,
    2529                 :            :                           enum scsi_device_event evt_type, gfp_t gfpflags)
    2530                 :            : {
    2531                 :            :         struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
    2532         [ #  # ]:          0 :         if (!evt) {
    2533                 :          0 :                 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
    2534                 :            :                             evt_type);
    2535                 :          0 :                 return;
    2536                 :            :         }
    2537                 :            : 
    2538                 :          0 :         sdev_evt_send(sdev, evt);
    2539                 :            : }
    2540                 :            : EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
    2541                 :            : 
    2542                 :            : /**
    2543                 :            :  *      scsi_device_quiesce - Block user issued commands.
    2544                 :            :  *      @sdev:  scsi device to quiesce.
    2545                 :            :  *
    2546                 :            :  *      This works by trying to transition to the SDEV_QUIESCE state
    2547                 :            :  *      (which must be a legal transition).  When the device is in this
    2548                 :            :  *      state, only special requests will be accepted, all others will
    2549                 :            :  *      be deferred.  Since special requests may also be requeued requests,
    2550                 :            :  *      a successful return doesn't guarantee the device will be 
    2551                 :            :  *      totally quiescent.
    2552                 :            :  *
    2553                 :            :  *      Must be called with user context, may sleep.
    2554                 :            :  *
    2555                 :            :  *      Returns zero if unsuccessful or an error if not.
    2556                 :            :  */
    2557                 :            : int
    2558                 :          0 : scsi_device_quiesce(struct scsi_device *sdev)
    2559                 :            : {
    2560                 :          0 :         struct request_queue *q = sdev->request_queue;
    2561                 :            :         int err;
    2562                 :            : 
    2563                 :            :         /*
    2564                 :            :          * It is allowed to call scsi_device_quiesce() multiple times from
    2565                 :            :          * the same context but concurrent scsi_device_quiesce() calls are
    2566                 :            :          * not allowed.
    2567                 :            :          */
    2568   [ #  #  #  #  :          0 :         WARN_ON_ONCE(sdev->quiesced_by && sdev->quiesced_by != current);
             #  #  #  # ]
    2569                 :            : 
    2570         [ #  # ]:          0 :         if (sdev->quiesced_by == current)
    2571                 :            :                 return 0;
    2572                 :            : 
    2573                 :          0 :         blk_set_pm_only(q);
    2574                 :            : 
    2575                 :          0 :         blk_mq_freeze_queue(q);
    2576                 :            :         /*
    2577                 :            :          * Ensure that the effect of blk_set_pm_only() will be visible
    2578                 :            :          * for percpu_ref_tryget() callers that occur after the queue
    2579                 :            :          * unfreeze even if the queue was already frozen before this function
    2580                 :            :          * was called. See also https://lwn.net/Articles/573497/.
    2581                 :            :          */
    2582                 :          0 :         synchronize_rcu();
    2583                 :          0 :         blk_mq_unfreeze_queue(q);
    2584                 :            : 
    2585                 :          0 :         mutex_lock(&sdev->state_mutex);
    2586                 :          0 :         err = scsi_device_set_state(sdev, SDEV_QUIESCE);
    2587         [ #  # ]:          0 :         if (err == 0)
    2588                 :          0 :                 sdev->quiesced_by = current;
    2589                 :            :         else
    2590                 :          0 :                 blk_clear_pm_only(q);
    2591                 :          0 :         mutex_unlock(&sdev->state_mutex);
    2592                 :            : 
    2593                 :          0 :         return err;
    2594                 :            : }
    2595                 :            : EXPORT_SYMBOL(scsi_device_quiesce);
    2596                 :            : 
    2597                 :            : /**
    2598                 :            :  *      scsi_device_resume - Restart user issued commands to a quiesced device.
    2599                 :            :  *      @sdev:  scsi device to resume.
    2600                 :            :  *
    2601                 :            :  *      Moves the device from quiesced back to running and restarts the
    2602                 :            :  *      queues.
    2603                 :            :  *
    2604                 :            :  *      Must be called with user context, may sleep.
    2605                 :            :  */
    2606                 :          0 : void scsi_device_resume(struct scsi_device *sdev)
    2607                 :            : {
    2608                 :            :         /* check if the device state was mutated prior to resume, and if
    2609                 :            :          * so assume the state is being managed elsewhere (for example
    2610                 :            :          * device deleted during suspend)
    2611                 :            :          */
    2612                 :          0 :         mutex_lock(&sdev->state_mutex);
    2613         [ #  # ]:          0 :         if (sdev->quiesced_by) {
    2614                 :          0 :                 sdev->quiesced_by = NULL;
    2615                 :          0 :                 blk_clear_pm_only(sdev->request_queue);
    2616                 :            :         }
    2617         [ #  # ]:          0 :         if (sdev->sdev_state == SDEV_QUIESCE)
    2618                 :          0 :                 scsi_device_set_state(sdev, SDEV_RUNNING);
    2619                 :          0 :         mutex_unlock(&sdev->state_mutex);
    2620                 :          0 : }
    2621                 :            : EXPORT_SYMBOL(scsi_device_resume);
    2622                 :            : 
    2623                 :            : static void
    2624                 :          0 : device_quiesce_fn(struct scsi_device *sdev, void *data)
    2625                 :            : {
    2626                 :          0 :         scsi_device_quiesce(sdev);
    2627                 :          0 : }
    2628                 :            : 
    2629                 :            : void
    2630                 :          0 : scsi_target_quiesce(struct scsi_target *starget)
    2631                 :            : {
    2632                 :          0 :         starget_for_each_device(starget, NULL, device_quiesce_fn);
    2633                 :          0 : }
    2634                 :            : EXPORT_SYMBOL(scsi_target_quiesce);
    2635                 :            : 
    2636                 :            : static void
    2637                 :          0 : device_resume_fn(struct scsi_device *sdev, void *data)
    2638                 :            : {
    2639                 :          0 :         scsi_device_resume(sdev);
    2640                 :          0 : }
    2641                 :            : 
    2642                 :            : void
    2643                 :          0 : scsi_target_resume(struct scsi_target *starget)
    2644                 :            : {
    2645                 :          0 :         starget_for_each_device(starget, NULL, device_resume_fn);
    2646                 :          0 : }
    2647                 :            : EXPORT_SYMBOL(scsi_target_resume);
    2648                 :            : 
    2649                 :            : /**
    2650                 :            :  * scsi_internal_device_block_nowait - try to transition to the SDEV_BLOCK state
    2651                 :            :  * @sdev: device to block
    2652                 :            :  *
    2653                 :            :  * Pause SCSI command processing on the specified device. Does not sleep.
    2654                 :            :  *
    2655                 :            :  * Returns zero if successful or a negative error code upon failure.
    2656                 :            :  *
    2657                 :            :  * Notes:
    2658                 :            :  * This routine transitions the device to the SDEV_BLOCK state (which must be
    2659                 :            :  * a legal transition). When the device is in this state, command processing
    2660                 :            :  * is paused until the device leaves the SDEV_BLOCK state. See also
    2661                 :            :  * scsi_internal_device_unblock_nowait().
    2662                 :            :  */
    2663                 :          0 : int scsi_internal_device_block_nowait(struct scsi_device *sdev)
    2664                 :            : {
    2665                 :          0 :         struct request_queue *q = sdev->request_queue;
    2666                 :            :         int err = 0;
    2667                 :            : 
    2668                 :          0 :         err = scsi_device_set_state(sdev, SDEV_BLOCK);
    2669         [ #  # ]:          0 :         if (err) {
    2670                 :          0 :                 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
    2671                 :            : 
    2672         [ #  # ]:          0 :                 if (err)
    2673                 :            :                         return err;
    2674                 :            :         }
    2675                 :            : 
    2676                 :            :         /* 
    2677                 :            :          * The device has transitioned to SDEV_BLOCK.  Stop the
    2678                 :            :          * block layer from calling the midlayer with this device's
    2679                 :            :          * request queue. 
    2680                 :            :          */
    2681                 :          0 :         blk_mq_quiesce_queue_nowait(q);
    2682                 :          0 :         return 0;
    2683                 :            : }
    2684                 :            : EXPORT_SYMBOL_GPL(scsi_internal_device_block_nowait);
    2685                 :            : 
    2686                 :            : /**
    2687                 :            :  * scsi_internal_device_block - try to transition to the SDEV_BLOCK state
    2688                 :            :  * @sdev: device to block
    2689                 :            :  *
    2690                 :            :  * Pause SCSI command processing on the specified device and wait until all
    2691                 :            :  * ongoing scsi_request_fn() / scsi_queue_rq() calls have finished. May sleep.
    2692                 :            :  *
    2693                 :            :  * Returns zero if successful or a negative error code upon failure.
    2694                 :            :  *
    2695                 :            :  * Note:
    2696                 :            :  * This routine transitions the device to the SDEV_BLOCK state (which must be
    2697                 :            :  * a legal transition). When the device is in this state, command processing
    2698                 :            :  * is paused until the device leaves the SDEV_BLOCK state. See also
    2699                 :            :  * scsi_internal_device_unblock().
    2700                 :            :  */
    2701                 :          0 : static int scsi_internal_device_block(struct scsi_device *sdev)
    2702                 :            : {
    2703                 :          0 :         struct request_queue *q = sdev->request_queue;
    2704                 :            :         int err;
    2705                 :            : 
    2706                 :          0 :         mutex_lock(&sdev->state_mutex);
    2707                 :          0 :         err = scsi_internal_device_block_nowait(sdev);
    2708         [ #  # ]:          0 :         if (err == 0)
    2709                 :          0 :                 blk_mq_quiesce_queue(q);
    2710                 :          0 :         mutex_unlock(&sdev->state_mutex);
    2711                 :            : 
    2712                 :          0 :         return err;
    2713                 :            : }
    2714                 :            :  
    2715                 :          0 : void scsi_start_queue(struct scsi_device *sdev)
    2716                 :            : {
    2717                 :          0 :         struct request_queue *q = sdev->request_queue;
    2718                 :            : 
    2719                 :          0 :         blk_mq_unquiesce_queue(q);
    2720                 :          0 : }
    2721                 :            : 
    2722                 :            : /**
    2723                 :            :  * scsi_internal_device_unblock_nowait - resume a device after a block request
    2724                 :            :  * @sdev:       device to resume
    2725                 :            :  * @new_state:  state to set the device to after unblocking
    2726                 :            :  *
    2727                 :            :  * Restart the device queue for a previously suspended SCSI device. Does not
    2728                 :            :  * sleep.
    2729                 :            :  *
    2730                 :            :  * Returns zero if successful or a negative error code upon failure.
    2731                 :            :  *
    2732                 :            :  * Notes:
    2733                 :            :  * This routine transitions the device to the SDEV_RUNNING state or to one of
    2734                 :            :  * the offline states (which must be a legal transition) allowing the midlayer
    2735                 :            :  * to goose the queue for this device.
    2736                 :            :  */
    2737                 :          0 : int scsi_internal_device_unblock_nowait(struct scsi_device *sdev,
    2738                 :            :                                         enum scsi_device_state new_state)
    2739                 :            : {
    2740         [ #  # ]:          0 :         switch (new_state) {
    2741                 :            :         case SDEV_RUNNING:
    2742                 :            :         case SDEV_TRANSPORT_OFFLINE:
    2743                 :            :                 break;
    2744                 :            :         default:
    2745                 :            :                 return -EINVAL;
    2746                 :            :         }
    2747                 :            : 
    2748                 :            :         /*
    2749                 :            :          * Try to transition the scsi device to SDEV_RUNNING or one of the
    2750                 :            :          * offlined states and goose the device queue if successful.
    2751                 :            :          */
    2752   [ #  #  #  # ]:          0 :         switch (sdev->sdev_state) {
    2753                 :            :         case SDEV_BLOCK:
    2754                 :            :         case SDEV_TRANSPORT_OFFLINE:
    2755                 :          0 :                 sdev->sdev_state = new_state;
    2756                 :          0 :                 break;
    2757                 :            :         case SDEV_CREATED_BLOCK:
    2758         [ #  # ]:          0 :                 if (new_state == SDEV_TRANSPORT_OFFLINE ||
    2759                 :            :                     new_state == SDEV_OFFLINE)
    2760                 :          0 :                         sdev->sdev_state = new_state;
    2761                 :            :                 else
    2762                 :          0 :                         sdev->sdev_state = SDEV_CREATED;
    2763                 :            :                 break;
    2764                 :            :         case SDEV_CANCEL:
    2765                 :            :         case SDEV_OFFLINE:
    2766                 :            :                 break;
    2767                 :            :         default:
    2768                 :            :                 return -EINVAL;
    2769                 :            :         }
    2770                 :            :         scsi_start_queue(sdev);
    2771                 :            : 
    2772                 :          0 :         return 0;
    2773                 :            : }
    2774                 :            : EXPORT_SYMBOL_GPL(scsi_internal_device_unblock_nowait);
    2775                 :            : 
    2776                 :            : /**
    2777                 :            :  * scsi_internal_device_unblock - resume a device after a block request
    2778                 :            :  * @sdev:       device to resume
    2779                 :            :  * @new_state:  state to set the device to after unblocking
    2780                 :            :  *
    2781                 :            :  * Restart the device queue for a previously suspended SCSI device. May sleep.
    2782                 :            :  *
    2783                 :            :  * Returns zero if successful or a negative error code upon failure.
    2784                 :            :  *
    2785                 :            :  * Notes:
    2786                 :            :  * This routine transitions the device to the SDEV_RUNNING state or to one of
    2787                 :            :  * the offline states (which must be a legal transition) allowing the midlayer
    2788                 :            :  * to goose the queue for this device.
    2789                 :            :  */
    2790                 :          0 : static int scsi_internal_device_unblock(struct scsi_device *sdev,
    2791                 :            :                                         enum scsi_device_state new_state)
    2792                 :            : {
    2793                 :            :         int ret;
    2794                 :            : 
    2795                 :          0 :         mutex_lock(&sdev->state_mutex);
    2796                 :          0 :         ret = scsi_internal_device_unblock_nowait(sdev, new_state);
    2797                 :          0 :         mutex_unlock(&sdev->state_mutex);
    2798                 :            : 
    2799                 :          0 :         return ret;
    2800                 :            : }
    2801                 :            : 
    2802                 :            : static void
    2803                 :          0 : device_block(struct scsi_device *sdev, void *data)
    2804                 :            : {
    2805                 :            :         int ret;
    2806                 :            : 
    2807                 :          0 :         ret = scsi_internal_device_block(sdev);
    2808                 :            : 
    2809   [ #  #  #  # ]:          0 :         WARN_ONCE(ret, "scsi_internal_device_block(%s) failed: ret = %d\n",
    2810                 :            :                   dev_name(&sdev->sdev_gendev), ret);
    2811                 :          0 : }
    2812                 :            : 
    2813                 :            : static int
    2814                 :          0 : target_block(struct device *dev, void *data)
    2815                 :            : {
    2816         [ #  # ]:          0 :         if (scsi_is_target_device(dev))
    2817                 :          0 :                 starget_for_each_device(to_scsi_target(dev), NULL,
    2818                 :            :                                         device_block);
    2819                 :          0 :         return 0;
    2820                 :            : }
    2821                 :            : 
    2822                 :            : void
    2823                 :          0 : scsi_target_block(struct device *dev)
    2824                 :            : {
    2825         [ #  # ]:          0 :         if (scsi_is_target_device(dev))
    2826                 :          0 :                 starget_for_each_device(to_scsi_target(dev), NULL,
    2827                 :            :                                         device_block);
    2828                 :            :         else
    2829                 :          0 :                 device_for_each_child(dev, NULL, target_block);
    2830                 :          0 : }
    2831                 :            : EXPORT_SYMBOL_GPL(scsi_target_block);
    2832                 :            : 
    2833                 :            : static void
    2834                 :          0 : device_unblock(struct scsi_device *sdev, void *data)
    2835                 :            : {
    2836                 :          0 :         scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
    2837                 :          0 : }
    2838                 :            : 
    2839                 :            : static int
    2840                 :          0 : target_unblock(struct device *dev, void *data)
    2841                 :            : {
    2842         [ #  # ]:          0 :         if (scsi_is_target_device(dev))
    2843                 :          0 :                 starget_for_each_device(to_scsi_target(dev), data,
    2844                 :            :                                         device_unblock);
    2845                 :          0 :         return 0;
    2846                 :            : }
    2847                 :            : 
    2848                 :            : void
    2849                 :          0 : scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
    2850                 :            : {
    2851         [ #  # ]:          0 :         if (scsi_is_target_device(dev))
    2852                 :          0 :                 starget_for_each_device(to_scsi_target(dev), &new_state,
    2853                 :            :                                         device_unblock);
    2854                 :            :         else
    2855                 :          0 :                 device_for_each_child(dev, &new_state, target_unblock);
    2856                 :          0 : }
    2857                 :            : EXPORT_SYMBOL_GPL(scsi_target_unblock);
    2858                 :            : 
    2859                 :            : /**
    2860                 :            :  * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
    2861                 :            :  * @sgl:        scatter-gather list
    2862                 :            :  * @sg_count:   number of segments in sg
    2863                 :            :  * @offset:     offset in bytes into sg, on return offset into the mapped area
    2864                 :            :  * @len:        bytes to map, on return number of bytes mapped
    2865                 :            :  *
    2866                 :            :  * Returns virtual address of the start of the mapped page
    2867                 :            :  */
    2868                 :          0 : void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
    2869                 :            :                           size_t *offset, size_t *len)
    2870                 :            : {
    2871                 :            :         int i;
    2872                 :            :         size_t sg_len = 0, len_complete = 0;
    2873                 :            :         struct scatterlist *sg;
    2874                 :            :         struct page *page;
    2875                 :            : 
    2876         [ #  # ]:          0 :         WARN_ON(!irqs_disabled());
    2877                 :            : 
    2878         [ #  # ]:          0 :         for_each_sg(sgl, sg, sg_count, i) {
    2879                 :            :                 len_complete = sg_len; /* Complete sg-entries */
    2880                 :          0 :                 sg_len += sg->length;
    2881         [ #  # ]:          0 :                 if (sg_len > *offset)
    2882                 :            :                         break;
    2883                 :            :         }
    2884                 :            : 
    2885         [ #  # ]:          0 :         if (unlikely(i == sg_count)) {
    2886                 :          0 :                 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
    2887                 :            :                         "elements %d\n",
    2888                 :            :                        __func__, sg_len, *offset, sg_count);
    2889                 :          0 :                 WARN_ON(1);
    2890                 :          0 :                 return NULL;
    2891                 :            :         }
    2892                 :            : 
    2893                 :            :         /* Offset starting from the beginning of first page in this sg-entry */
    2894                 :          0 :         *offset = *offset - len_complete + sg->offset;
    2895                 :            : 
    2896                 :            :         /* Assumption: contiguous pages can be accessed as "page + i" */
    2897                 :          0 :         page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
    2898                 :          0 :         *offset &= ~PAGE_MASK;
    2899                 :            : 
    2900                 :            :         /* Bytes in this sg-entry from *offset to the end of the page */
    2901                 :          0 :         sg_len = PAGE_SIZE - *offset;
    2902         [ #  # ]:          0 :         if (*len > sg_len)
    2903                 :          0 :                 *len = sg_len;
    2904                 :            : 
    2905                 :          0 :         return kmap_atomic(page);
    2906                 :            : }
    2907                 :            : EXPORT_SYMBOL(scsi_kmap_atomic_sg);
    2908                 :            : 
    2909                 :            : /**
    2910                 :            :  * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
    2911                 :            :  * @virt:       virtual address to be unmapped
    2912                 :            :  */
    2913                 :          0 : void scsi_kunmap_atomic_sg(void *virt)
    2914                 :            : {
    2915                 :            :         kunmap_atomic(virt);
    2916                 :          0 : }
    2917                 :            : EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
    2918                 :            : 
    2919                 :          0 : void sdev_disable_disk_events(struct scsi_device *sdev)
    2920                 :            : {
    2921                 :          0 :         atomic_inc(&sdev->disk_events_disable_depth);
    2922                 :          0 : }
    2923                 :            : EXPORT_SYMBOL(sdev_disable_disk_events);
    2924                 :            : 
    2925                 :          0 : void sdev_enable_disk_events(struct scsi_device *sdev)
    2926                 :            : {
    2927   [ #  #  #  #  :          0 :         if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
                   #  # ]
    2928                 :          0 :                 return;
    2929                 :          0 :         atomic_dec(&sdev->disk_events_disable_depth);
    2930                 :            : }
    2931                 :            : EXPORT_SYMBOL(sdev_enable_disk_events);
    2932                 :            : 
    2933                 :            : /**
    2934                 :            :  * scsi_vpd_lun_id - return a unique device identification
    2935                 :            :  * @sdev: SCSI device
    2936                 :            :  * @id:   buffer for the identification
    2937                 :            :  * @id_len:  length of the buffer
    2938                 :            :  *
    2939                 :            :  * Copies a unique device identification into @id based
    2940                 :            :  * on the information in the VPD page 0x83 of the device.
    2941                 :            :  * The string will be formatted as a SCSI name string.
    2942                 :            :  *
    2943                 :            :  * Returns the length of the identification or error on failure.
    2944                 :            :  * If the identifier is longer than the supplied buffer the actual
    2945                 :            :  * identifier length is returned and the buffer is not zero-padded.
    2946                 :            :  */
    2947                 :          0 : int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
    2948                 :            : {
    2949                 :            :         u8 cur_id_type = 0xff;
    2950                 :            :         u8 cur_id_size = 0;
    2951                 :            :         const unsigned char *d, *cur_id_str;
    2952                 :            :         const struct scsi_vpd *vpd_pg83;
    2953                 :            :         int id_size = -EINVAL;
    2954                 :            : 
    2955                 :            :         rcu_read_lock();
    2956                 :          0 :         vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
    2957         [ #  # ]:          0 :         if (!vpd_pg83) {
    2958                 :            :                 rcu_read_unlock();
    2959                 :          0 :                 return -ENXIO;
    2960                 :            :         }
    2961                 :            : 
    2962                 :            :         /*
    2963                 :            :          * Look for the correct descriptor.
    2964                 :            :          * Order of preference for lun descriptor:
    2965                 :            :          * - SCSI name string
    2966                 :            :          * - NAA IEEE Registered Extended
    2967                 :            :          * - EUI-64 based 16-byte
    2968                 :            :          * - EUI-64 based 12-byte
    2969                 :            :          * - NAA IEEE Registered
    2970                 :            :          * - NAA IEEE Extended
    2971                 :            :          * - T10 Vendor ID
    2972                 :            :          * as longer descriptors reduce the likelyhood
    2973                 :            :          * of identification clashes.
    2974                 :            :          */
    2975                 :            : 
    2976                 :            :         /* The id string must be at least 20 bytes + terminating NULL byte */
    2977         [ #  # ]:          0 :         if (id_len < 21) {
    2978                 :            :                 rcu_read_unlock();
    2979                 :          0 :                 return -EINVAL;
    2980                 :            :         }
    2981                 :            : 
    2982                 :          0 :         memset(id, 0, id_len);
    2983                 :          0 :         d = vpd_pg83->data + 4;
    2984         [ #  # ]:          0 :         while (d < vpd_pg83->data + vpd_pg83->len) {
    2985                 :            :                 /* Skip designators not referring to the LUN */
    2986         [ #  # ]:          0 :                 if ((d[1] & 0x30) != 0x00)
    2987                 :            :                         goto next_desig;
    2988                 :            : 
    2989   [ #  #  #  #  :          0 :                 switch (d[1] & 0xf) {
                      # ]
    2990                 :            :                 case 0x1:
    2991                 :            :                         /* T10 Vendor ID */
    2992         [ #  # ]:          0 :                         if (cur_id_size > d[3])
    2993                 :            :                                 break;
    2994                 :            :                         /* Prefer anything */
    2995         [ #  # ]:          0 :                         if (cur_id_type > 0x01 && cur_id_type != 0xff)
    2996                 :            :                                 break;
    2997                 :            :                         cur_id_size = d[3];
    2998         [ #  # ]:          0 :                         if (cur_id_size + 4 > id_len)
    2999                 :          0 :                                 cur_id_size = id_len - 4;
    3000                 :          0 :                         cur_id_str = d + 4;
    3001                 :            :                         cur_id_type = d[1] & 0xf;
    3002                 :          0 :                         id_size = snprintf(id, id_len, "t10.%*pE",
    3003                 :            :                                            cur_id_size, cur_id_str);
    3004                 :          0 :                         break;
    3005                 :            :                 case 0x2:
    3006                 :            :                         /* EUI-64 */
    3007         [ #  # ]:          0 :                         if (cur_id_size > d[3])
    3008                 :            :                                 break;
    3009                 :            :                         /* Prefer NAA IEEE Registered Extended */
    3010   [ #  #  #  # ]:          0 :                         if (cur_id_type == 0x3 &&
    3011                 :            :                             cur_id_size == d[3])
    3012                 :            :                                 break;
    3013                 :            :                         cur_id_size = d[3];
    3014                 :          0 :                         cur_id_str = d + 4;
    3015                 :            :                         cur_id_type = d[1] & 0xf;
    3016   [ #  #  #  # ]:          0 :                         switch (cur_id_size) {
    3017                 :            :                         case 8:
    3018                 :          0 :                                 id_size = snprintf(id, id_len,
    3019                 :            :                                                    "eui.%8phN",
    3020                 :            :                                                    cur_id_str);
    3021                 :          0 :                                 break;
    3022                 :            :                         case 12:
    3023                 :          0 :                                 id_size = snprintf(id, id_len,
    3024                 :            :                                                    "eui.%12phN",
    3025                 :            :                                                    cur_id_str);
    3026                 :          0 :                                 break;
    3027                 :            :                         case 16:
    3028                 :          0 :                                 id_size = snprintf(id, id_len,
    3029                 :            :                                                    "eui.%16phN",
    3030                 :            :                                                    cur_id_str);
    3031                 :          0 :                                 break;
    3032                 :            :                         default:
    3033                 :            :                                 cur_id_size = 0;
    3034                 :            :                                 break;
    3035                 :            :                         }
    3036                 :            :                         break;
    3037                 :            :                 case 0x3:
    3038                 :            :                         /* NAA */
    3039         [ #  # ]:          0 :                         if (cur_id_size > d[3])
    3040                 :            :                                 break;
    3041                 :            :                         cur_id_size = d[3];
    3042                 :          0 :                         cur_id_str = d + 4;
    3043                 :            :                         cur_id_type = d[1] & 0xf;
    3044      [ #  #  # ]:          0 :                         switch (cur_id_size) {
    3045                 :            :                         case 8:
    3046                 :          0 :                                 id_size = snprintf(id, id_len,
    3047                 :            :                                                    "naa.%8phN",
    3048                 :            :                                                    cur_id_str);
    3049                 :          0 :                                 break;
    3050                 :            :                         case 16:
    3051                 :          0 :                                 id_size = snprintf(id, id_len,
    3052                 :            :                                                    "naa.%16phN",
    3053                 :            :                                                    cur_id_str);
    3054                 :          0 :                                 break;
    3055                 :            :                         default:
    3056                 :            :                                 cur_id_size = 0;
    3057                 :            :                                 break;
    3058                 :            :                         }
    3059                 :            :                         break;
    3060                 :            :                 case 0x8:
    3061                 :            :                         /* SCSI name string */
    3062         [ #  # ]:          0 :                         if (cur_id_size + 4 > d[3])
    3063                 :            :                                 break;
    3064                 :            :                         /* Prefer others for truncated descriptor */
    3065   [ #  #  #  # ]:          0 :                         if (cur_id_size && d[3] > id_len)
    3066                 :            :                                 break;
    3067                 :            :                         cur_id_size = id_size = d[3];
    3068                 :          0 :                         cur_id_str = d + 4;
    3069                 :            :                         cur_id_type = d[1] & 0xf;
    3070         [ #  # ]:          0 :                         if (cur_id_size >= id_len)
    3071                 :          0 :                                 cur_id_size = id_len - 1;
    3072                 :          0 :                         memcpy(id, cur_id_str, cur_id_size);
    3073                 :            :                         /* Decrease priority for truncated descriptor */
    3074         [ #  # ]:          0 :                         if (cur_id_size != id_size)
    3075                 :            :                                 cur_id_size = 6;
    3076                 :            :                         break;
    3077                 :            :                 default:
    3078                 :            :                         break;
    3079                 :            :                 }
    3080                 :            : next_desig:
    3081                 :          0 :                 d += d[3] + 4;
    3082                 :            :         }
    3083                 :            :         rcu_read_unlock();
    3084                 :            : 
    3085                 :          0 :         return id_size;
    3086                 :            : }
    3087                 :            : EXPORT_SYMBOL(scsi_vpd_lun_id);
    3088                 :            : 
    3089                 :            : /*
    3090                 :            :  * scsi_vpd_tpg_id - return a target port group identifier
    3091                 :            :  * @sdev: SCSI device
    3092                 :            :  *
    3093                 :            :  * Returns the Target Port Group identifier from the information
    3094                 :            :  * froom VPD page 0x83 of the device.
    3095                 :            :  *
    3096                 :            :  * Returns the identifier or error on failure.
    3097                 :            :  */
    3098                 :          0 : int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
    3099                 :            : {
    3100                 :            :         const unsigned char *d;
    3101                 :            :         const struct scsi_vpd *vpd_pg83;
    3102                 :            :         int group_id = -EAGAIN, rel_port = -1;
    3103                 :            : 
    3104                 :            :         rcu_read_lock();
    3105                 :          0 :         vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
    3106         [ #  # ]:          0 :         if (!vpd_pg83) {
    3107                 :            :                 rcu_read_unlock();
    3108                 :          0 :                 return -ENXIO;
    3109                 :            :         }
    3110                 :            : 
    3111                 :          0 :         d = vpd_pg83->data + 4;
    3112         [ #  # ]:          0 :         while (d < vpd_pg83->data + vpd_pg83->len) {
    3113      [ #  #  # ]:          0 :                 switch (d[1] & 0xf) {
    3114                 :            :                 case 0x4:
    3115                 :            :                         /* Relative target port */
    3116                 :          0 :                         rel_port = get_unaligned_be16(&d[6]);
    3117                 :          0 :                         break;
    3118                 :            :                 case 0x5:
    3119                 :            :                         /* Target port group */
    3120                 :          0 :                         group_id = get_unaligned_be16(&d[6]);
    3121                 :          0 :                         break;
    3122                 :            :                 default:
    3123                 :            :                         break;
    3124                 :            :                 }
    3125                 :          0 :                 d += d[3] + 4;
    3126                 :            :         }
    3127                 :            :         rcu_read_unlock();
    3128                 :            : 
    3129   [ #  #  #  # ]:          0 :         if (group_id >= 0 && rel_id && rel_port != -1)
    3130                 :          0 :                 *rel_id = rel_port;
    3131                 :            : 
    3132                 :          0 :         return group_id;
    3133                 :            : }
    3134                 :            : EXPORT_SYMBOL(scsi_vpd_tpg_id);

Generated by: LCOV version 1.14