blob: f6097b89d5d381229cc0be0a9753c908de5caa47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Christoph Hellwigd2852032014-01-17 12:06:53 +01002 * Copyright (C) 1999 Eric Youngdale
3 * Copyright (C) 2014 Christoph Hellwig
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * SCSI queueing library.
6 * Initial versions: Eric Youngdale (eric@andante.org).
7 * Based upon conversations with large numbers
8 * of people at Linux Expo.
9 */
10
11#include <linux/bio.h>
James Bottomleyd3f46f32008-01-15 11:11:46 -060012#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/blkdev.h>
14#include <linux/completion.h>
15#include <linux/kernel.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
James Bottomleyfaead262006-02-14 10:42:07 -060020#include <linux/hardirq.h>
Jens Axboec6132da2007-10-16 11:08:49 +020021#include <linux/scatterlist.h>
Christoph Hellwigd2852032014-01-17 12:06:53 +010022#include <linux/blk-mq.h>
Hannes Reineckef1569ff2014-10-24 14:27:07 +020023#include <linux/ratelimit.h>
Hannes Reineckea8aa3972015-12-01 10:16:57 +010024#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020027#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <scsi/scsi_dbg.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_driver.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_host.h>
Bart Van Assche7aa686d2017-05-02 10:45:03 -070033#include <scsi/scsi_transport.h> /* __scsi_init_queue() */
Christoph Hellwigee14c672015-08-27 14:16:59 +020034#include <scsi/scsi_dh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Christoph Hellwig3b5382c2014-05-06 12:25:40 +020036#include <trace/events/scsi.h>
37
Bart Van Assche0eebd002017-04-26 13:47:57 -070038#include "scsi_debugfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "scsi_priv.h"
40#include "scsi_logging.h"
41
Christoph Hellwige9c787e2017-01-02 21:55:26 +030042static struct kmem_cache *scsi_sdb_cache;
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030043static struct kmem_cache *scsi_sense_cache;
44static struct kmem_cache *scsi_sense_isadma_cache;
45static DEFINE_MUTEX(scsi_sense_cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030047static inline struct kmem_cache *
Bart Van Assche8e688252017-06-02 14:21:52 -070048scsi_select_sense_cache(bool unchecked_isa_dma)
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030049{
Bart Van Assche8e688252017-06-02 14:21:52 -070050 return unchecked_isa_dma ? scsi_sense_isadma_cache : scsi_sense_cache;
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030051}
52
Bart Van Assche8e688252017-06-02 14:21:52 -070053static void scsi_free_sense_buffer(bool unchecked_isa_dma,
54 unsigned char *sense_buffer)
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030055{
Bart Van Assche8e688252017-06-02 14:21:52 -070056 kmem_cache_free(scsi_select_sense_cache(unchecked_isa_dma),
57 sense_buffer);
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030058}
59
Bart Van Assche8e688252017-06-02 14:21:52 -070060static unsigned char *scsi_alloc_sense_buffer(bool unchecked_isa_dma,
Christoph Hellwige9c787e2017-01-02 21:55:26 +030061 gfp_t gfp_mask, int numa_node)
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030062{
Bart Van Assche8e688252017-06-02 14:21:52 -070063 return kmem_cache_alloc_node(scsi_select_sense_cache(unchecked_isa_dma),
64 gfp_mask, numa_node);
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030065}
66
67int scsi_init_sense_cache(struct Scsi_Host *shost)
68{
69 struct kmem_cache *cache;
70 int ret = 0;
71
Bart Van Assche8e688252017-06-02 14:21:52 -070072 cache = scsi_select_sense_cache(shost->unchecked_isa_dma);
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030073 if (cache)
74 return 0;
75
76 mutex_lock(&scsi_sense_cache_mutex);
77 if (shost->unchecked_isa_dma) {
78 scsi_sense_isadma_cache =
79 kmem_cache_create("scsi_sense_cache(DMA)",
80 SCSI_SENSE_BUFFERSIZE, 0,
81 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL);
82 if (!scsi_sense_isadma_cache)
83 ret = -ENOMEM;
84 } else {
85 scsi_sense_cache =
86 kmem_cache_create("scsi_sense_cache",
87 SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN, NULL);
88 if (!scsi_sense_cache)
89 ret = -ENOMEM;
90 }
91
92 mutex_unlock(&scsi_sense_cache_mutex);
93 return ret;
94}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +020095
Jens Axboea488e742010-04-16 21:13:15 +020096/*
97 * When to reinvoke queueing after a resource shortage. It's 3 msecs to
98 * not change behaviour from the previous unplug mechanism, experimentation
99 * may prove this needs changing.
100 */
101#define SCSI_QUEUE_DELAY 3
102
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100103static void
104scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct Scsi_Host *host = cmd->device->host;
107 struct scsi_device *device = cmd->device;
Mike Christief0c0a372008-08-17 15:24:38 -0500108 struct scsi_target *starget = scsi_target(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /*
Tejun Heo d8c37e72005-05-14 00:46:08 +0900111 * Set the appropriate busy bit for the device/host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
113 * If the host/device isn't busy, assume that something actually
114 * completed, and that we should be able to queue a command now.
115 *
116 * Note that the prior mid-layer assumption that any host could
117 * always queue at least one command is now broken. The mid-layer
118 * will implement a user specifiable stall (see
119 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
120 * if a command is requeued with no other commands outstanding
121 * either for the device or for the host.
122 */
Mike Christief0c0a372008-08-17 15:24:38 -0500123 switch (reason) {
124 case SCSI_MLQUEUE_HOST_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100125 atomic_set(&host->host_blocked, host->max_host_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500126 break;
127 case SCSI_MLQUEUE_DEVICE_BUSY:
James Smart573e5912011-07-06 12:45:17 -0400128 case SCSI_MLQUEUE_EH_RETRY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100129 atomic_set(&device->device_blocked,
130 device->max_device_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500131 break;
132 case SCSI_MLQUEUE_TARGET_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100133 atomic_set(&starget->target_blocked,
134 starget->max_target_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500135 break;
136 }
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100137}
138
Christoph Hellwigd2852032014-01-17 12:06:53 +0100139static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd)
140{
141 struct scsi_device *sdev = cmd->device;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100142
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700143 blk_mq_requeue_request(cmd->request, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100144 put_device(&sdev->sdev_gendev);
145}
146
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100147/**
148 * __scsi_queue_insert - private queue insertion
149 * @cmd: The SCSI command being requeued
150 * @reason: The reason for the requeue
151 * @unbusy: Whether the queue should be unbusied
152 *
153 * This is a private queue insertion. The public interface
154 * scsi_queue_insert() always assumes the queue should be unbusied
155 * because it's always called before the completion. This function is
156 * for a requeue after completion, which should only occur in this
157 * file.
158 */
159static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy)
160{
161 struct scsi_device *device = cmd->device;
162 struct request_queue *q = device->request_queue;
163 unsigned long flags;
164
165 SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
166 "Inserting command %p into mlqueue\n", cmd));
167
168 scsi_set_blocked(cmd, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * Decrement the counters, since these commands are no longer
172 * active on the host/device.
173 */
James Bottomley4f5299a2009-01-02 10:42:21 -0600174 if (unbusy)
175 scsi_device_unbusy(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /*
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500178 * Requeue this command. It will go before all other commands
Bart Van Asscheb4854622012-06-29 15:36:07 +0000179 * that are already in the queue. Schedule requeue work under
180 * lock such that the kblockd_schedule_work() call happens
181 * before blk_cleanup_queue() finishes.
Jens Axboea488e742010-04-16 21:13:15 +0200182 */
Alan Stern644373a2014-03-28 10:51:15 -0700183 cmd->result = 0;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100184 if (q->mq_ops) {
185 scsi_mq_requeue_cmd(cmd);
186 return;
187 }
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500188 spin_lock_irqsave(q->queue_lock, flags);
James Bottomley59897da2005-09-14 12:57:42 -0400189 blk_requeue_request(q, cmd->request);
Jens Axboe59c3d452014-04-08 09:15:35 -0600190 kblockd_schedule_work(&device->requeue_work);
Bart Van Asscheb4854622012-06-29 15:36:07 +0000191 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
James Bottomley4f5299a2009-01-02 10:42:21 -0600194/*
195 * Function: scsi_queue_insert()
196 *
197 * Purpose: Insert a command in the midlevel queue.
198 *
199 * Arguments: cmd - command that we are adding to queue.
200 * reason - why we are inserting command to queue.
201 *
202 * Lock status: Assumed that lock is not held upon entry.
203 *
204 * Returns: Nothing.
205 *
206 * Notes: We do this for one of two cases. Either the host is busy
207 * and it cannot accept any more commands for the time being,
208 * or the device returned QUEUE_FULL and can accept no more
209 * commands.
210 * Notes: This could be called either from an interrupt context or a
211 * normal process context.
212 */
Bart Van Assche84feb162012-06-29 15:35:05 +0000213void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
James Bottomley4f5299a2009-01-02 10:42:21 -0600214{
Bart Van Assche84feb162012-06-29 15:35:05 +0000215 __scsi_queue_insert(cmd, reason, 1);
James Bottomley4f5299a2009-01-02 10:42:21 -0600216}
Christoph Hellwige8064022016-10-20 15:12:13 +0200217
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100218
219/**
220 * scsi_execute - insert request and wait for the result
221 * @sdev: scsi device
222 * @cmd: scsi command
223 * @data_direction: data direction
224 * @buffer: data buffer
225 * @bufflen: len of buffer
226 * @sense: optional sense buffer
227 * @sshdr: optional decoded sense header
228 * @timeout: request timeout in seconds
229 * @retries: number of times to retry request
230 * @flags: flags for ->cmd_flags
231 * @rq_flags: flags for ->rq_flags
232 * @resid: optional residual length
233 *
Christoph Hellwig17d53632017-04-20 16:03:01 +0200234 * Returns the scsi_cmnd result field if a command was executed, or a negative
235 * Linux error code if we didn't get that far.
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100236 */
237int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
James Bottomley33aa6872005-08-28 11:31:14 -0500238 int data_direction, void *buffer, unsigned bufflen,
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100239 unsigned char *sense, struct scsi_sense_hdr *sshdr,
240 int timeout, int retries, u64 flags, req_flags_t rq_flags,
241 int *resid)
James Bottomley39216032005-06-15 18:48:29 -0500242{
243 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100244 struct scsi_request *rq;
James Bottomley39216032005-06-15 18:48:29 -0500245 int ret = DRIVER_ERROR << 24;
246
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100247 req = blk_get_request(sdev->request_queue,
248 data_direction == DMA_TO_DEVICE ?
249 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, __GFP_RECLAIM);
Joe Lawrencea492f072014-08-28 08:15:21 -0600250 if (IS_ERR(req))
James Bottomleybfe159a2011-07-07 15:45:40 -0500251 return ret;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100252 rq = scsi_req(req);
James Bottomley39216032005-06-15 18:48:29 -0500253
254 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
Mel Gorman71baba42015-11-06 16:28:28 -0800255 buffer, bufflen, __GFP_RECLAIM))
James Bottomley39216032005-06-15 18:48:29 -0500256 goto out;
257
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100258 rq->cmd_len = COMMAND_SIZE(cmd[0]);
259 memcpy(rq->cmd, cmd, rq->cmd_len);
Christoph Hellwig64c7f1d2017-04-05 19:18:12 +0200260 rq->retries = retries;
James Bottomley39216032005-06-15 18:48:29 -0500261 req->timeout = timeout;
Christoph Hellwige8064022016-10-20 15:12:13 +0200262 req->cmd_flags |= flags;
263 req->rq_flags |= rq_flags | RQF_QUIET | RQF_PREEMPT;
James Bottomley39216032005-06-15 18:48:29 -0500264
265 /*
266 * head injection *required* here otherwise quiesce won't work
267 */
268 blk_execute_rq(req->q, NULL, req, 1);
269
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400270 /*
271 * Some devices (USB mass-storage in particular) may transfer
272 * garbage data together with a residue indicating that the data
273 * is invalid. Prevent the garbage from being misinterpreted
274 * and prevent security leaks by zeroing out the excess data.
275 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100276 if (unlikely(rq->resid_len > 0 && rq->resid_len <= bufflen))
277 memset(buffer + (bufflen - rq->resid_len), 0, rq->resid_len);
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400278
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900279 if (resid)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100280 *resid = rq->resid_len;
281 if (sense && rq->sense_len)
282 memcpy(sense, rq->sense, SCSI_SENSE_BUFFERSIZE);
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100283 if (sshdr)
284 scsi_normalize_sense(rq->sense, rq->sense_len, sshdr);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200285 ret = rq->result;
James Bottomley39216032005-06-15 18:48:29 -0500286 out:
287 blk_put_request(req);
288
289 return ret;
290}
James Bottomley33aa6872005-08-28 11:31:14 -0500291EXPORT_SYMBOL(scsi_execute);
James Bottomley39216032005-06-15 18:48:29 -0500292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*
294 * Function: scsi_init_cmd_errh()
295 *
296 * Purpose: Initialize cmd fields related to error handling.
297 *
298 * Arguments: cmd - command that is ready to be queued.
299 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * Notes: This function has the job of initializing a number of
301 * fields related to error handling. Typically this will
302 * be called once for each command, as required.
303 */
Christoph Hellwig631c2282006-07-08 20:42:15 +0200304static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 cmd->serial_number = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200307 scsi_set_resid(cmd, 0);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900308 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (cmd->cmd_len == 0)
Boaz Harroshdb4742d2008-04-30 11:27:26 +0300310 cmd->cmd_len = scsi_command_size(cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313void scsi_device_unbusy(struct scsi_device *sdev)
314{
315 struct Scsi_Host *shost = sdev->host;
Mike Christief0c0a372008-08-17 15:24:38 -0500316 struct scsi_target *starget = scsi_target(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned long flags;
318
Christoph Hellwig74665012014-01-22 15:29:29 +0100319 atomic_dec(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100320 if (starget->can_queue > 0)
321 atomic_dec(&starget->target_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +0100322
James Bottomley939647e2005-09-18 15:05:20 -0500323 if (unlikely(scsi_host_in_recovery(shost) &&
Christoph Hellwig74665012014-01-22 15:29:29 +0100324 (shost->host_failed || shost->host_eh_scheduled))) {
325 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 scsi_eh_wakeup(shost);
Christoph Hellwig74665012014-01-22 15:29:29 +0100327 spin_unlock_irqrestore(shost->host_lock, flags);
328 }
329
Christoph Hellwig71e75c92014-04-11 19:07:01 +0200330 atomic_dec(&sdev->device_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Christoph Hellwigd2852032014-01-17 12:06:53 +0100333static void scsi_kick_queue(struct request_queue *q)
334{
335 if (q->mq_ops)
336 blk_mq_start_hw_queues(q);
337 else
338 blk_run_queue(q);
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/*
342 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
343 * and call blk_run_queue for all the scsi_devices on the target -
344 * including current_sdev first.
345 *
346 * Called with *no* scsi locks held.
347 */
348static void scsi_single_lun_run(struct scsi_device *current_sdev)
349{
350 struct Scsi_Host *shost = current_sdev->host;
351 struct scsi_device *sdev, *tmp;
352 struct scsi_target *starget = scsi_target(current_sdev);
353 unsigned long flags;
354
355 spin_lock_irqsave(shost->host_lock, flags);
356 starget->starget_sdev_user = NULL;
357 spin_unlock_irqrestore(shost->host_lock, flags);
358
359 /*
360 * Call blk_run_queue for all LUNs on the target, starting with
361 * current_sdev. We race with others (to set starget_sdev_user),
362 * but in most cases, we will be first. Ideally, each LU on the
363 * target would get some limited time or requests on the target.
364 */
Christoph Hellwigd2852032014-01-17 12:06:53 +0100365 scsi_kick_queue(current_sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 spin_lock_irqsave(shost->host_lock, flags);
368 if (starget->starget_sdev_user)
369 goto out;
370 list_for_each_entry_safe(sdev, tmp, &starget->devices,
371 same_target_siblings) {
372 if (sdev == current_sdev)
373 continue;
374 if (scsi_device_get(sdev))
375 continue;
376
377 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100378 scsi_kick_queue(sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_lock_irqsave(shost->host_lock, flags);
380
381 scsi_device_put(sdev);
382 }
383 out:
384 spin_unlock_irqrestore(shost->host_lock, flags);
385}
386
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100387static inline bool scsi_device_is_busy(struct scsi_device *sdev)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400388{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100389 if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
390 return true;
391 if (atomic_read(&sdev->device_blocked) > 0)
392 return true;
393 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400394}
395
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100396static inline bool scsi_target_is_busy(struct scsi_target *starget)
Mike Christief0c0a372008-08-17 15:24:38 -0500397{
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100398 if (starget->can_queue > 0) {
399 if (atomic_read(&starget->target_busy) >= starget->can_queue)
400 return true;
401 if (atomic_read(&starget->target_blocked) > 0)
402 return true;
403 }
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100404 return false;
Mike Christief0c0a372008-08-17 15:24:38 -0500405}
406
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100407static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400408{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100409 if (shost->can_queue > 0 &&
410 atomic_read(&shost->host_busy) >= shost->can_queue)
411 return true;
412 if (atomic_read(&shost->host_blocked) > 0)
413 return true;
414 if (shost->host_self_blocked)
415 return true;
416 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400417}
418
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800419static void scsi_starved_list_run(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Mike Christie2a3a59e2008-11-11 13:42:35 -0600421 LIST_HEAD(starved_list);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800422 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 unsigned long flags;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_lock_irqsave(shost->host_lock, flags);
Mike Christie2a3a59e2008-11-11 13:42:35 -0600426 list_splice_init(&shost->starved_list, &starved_list);
427
428 while (!list_empty(&starved_list)) {
James Bottomleye2eb7242013-07-02 15:05:26 +0200429 struct request_queue *slq;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
432 * As long as shost is accepting commands and we have
433 * starved queues, call blk_run_queue. scsi_request_fn
434 * drops the queue_lock and can add us back to the
435 * starved_list.
436 *
437 * host_lock protects the starved_list and starved_entry.
438 * scsi_request_fn must get the host_lock before checking
439 * or modifying starved_list or starved_entry.
440 */
Mike Christie2a3a59e2008-11-11 13:42:35 -0600441 if (scsi_host_is_busy(shost))
Mike Christief0c0a372008-08-17 15:24:38 -0500442 break;
Mike Christief0c0a372008-08-17 15:24:38 -0500443
Mike Christie2a3a59e2008-11-11 13:42:35 -0600444 sdev = list_entry(starved_list.next,
445 struct scsi_device, starved_entry);
446 list_del_init(&sdev->starved_entry);
Mike Christief0c0a372008-08-17 15:24:38 -0500447 if (scsi_target_is_busy(scsi_target(sdev))) {
448 list_move_tail(&sdev->starved_entry,
449 &shost->starved_list);
450 continue;
451 }
452
James Bottomleye2eb7242013-07-02 15:05:26 +0200453 /*
454 * Once we drop the host lock, a racing scsi_remove_device()
455 * call may remove the sdev from the starved list and destroy
456 * it and the queue. Mitigate by taking a reference to the
457 * queue and never touching the sdev again after we drop the
458 * host lock. Note: if __scsi_remove_device() invokes
459 * blk_cleanup_queue() before the queue is run from this
460 * function then blk_run_queue() will return immediately since
461 * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
462 */
463 slq = sdev->request_queue;
464 if (!blk_get_queue(slq))
465 continue;
466 spin_unlock_irqrestore(shost->host_lock, flags);
467
Christoph Hellwigd2852032014-01-17 12:06:53 +0100468 scsi_kick_queue(slq);
James Bottomleye2eb7242013-07-02 15:05:26 +0200469 blk_put_queue(slq);
470
471 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Mike Christie2a3a59e2008-11-11 13:42:35 -0600473 /* put any unprocessed entries back */
474 list_splice(&starved_list, &shost->starved_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800476}
477
478/*
479 * Function: scsi_run_queue()
480 *
481 * Purpose: Select a proper request queue to serve next
482 *
483 * Arguments: q - last request's queue
484 *
485 * Returns: Nothing
486 *
487 * Notes: The previous command was completely finished, start
488 * a new one if possible.
489 */
490static void scsi_run_queue(struct request_queue *q)
491{
492 struct scsi_device *sdev = q->queuedata;
493
494 if (scsi_target(sdev)->single_lun)
495 scsi_single_lun_run(sdev);
496 if (!list_empty(&sdev->host->starved_list))
497 scsi_starved_list_run(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Christoph Hellwigd2852032014-01-17 12:06:53 +0100499 if (q->mq_ops)
Bart Van Assche36e3cf22017-04-07 11:16:53 -0700500 blk_mq_run_hw_queues(q, false);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100501 else
502 blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Jens Axboe9937a5e2011-05-17 11:04:44 +0200505void scsi_requeue_run_queue(struct work_struct *work)
506{
507 struct scsi_device *sdev;
508 struct request_queue *q;
509
510 sdev = container_of(work, struct scsi_device, requeue_work);
511 q = sdev->request_queue;
512 scsi_run_queue(q);
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/*
516 * Function: scsi_requeue_command()
517 *
518 * Purpose: Handle post-processing of completed commands.
519 *
520 * Arguments: q - queue to operate on
521 * cmd - command that may need to be requeued.
522 *
523 * Returns: Nothing
524 *
525 * Notes: After command completion, there may be blocks left
526 * over which weren't finished by the previous command
527 * this can be for a number of reasons - the main one is
528 * I/O errors in the middle of the request, in which case
529 * we need to request the blocks that come after the bad
530 * sector.
James Bottomleye91442b2005-09-09 10:44:16 -0500531 * Notes: Upon return, cmd is a stale pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
533static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
534{
Bart Van Assche940f5d42012-06-29 15:34:26 +0000535 struct scsi_device *sdev = cmd->device;
James Bottomleye91442b2005-09-09 10:44:16 -0500536 struct request *req = cmd->request;
Tejun Heo 283369c2005-04-24 02:06:36 -0500537 unsigned long flags;
538
Tejun Heo 283369c2005-04-24 02:06:36 -0500539 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig134997a2014-02-20 14:20:58 -0800540 blk_unprep_request(req);
541 req->special = NULL;
542 scsi_put_command(cmd);
James Bottomleye91442b2005-09-09 10:44:16 -0500543 blk_requeue_request(q, req);
Tejun Heo 283369c2005-04-24 02:06:36 -0500544 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 scsi_run_queue(q);
Bart Van Assche940f5d42012-06-29 15:34:26 +0000547
548 put_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551void scsi_run_host_queues(struct Scsi_Host *shost)
552{
553 struct scsi_device *sdev;
554
555 shost_for_each_device(sdev, shost)
556 scsi_run_queue(sdev->request_queue);
557}
558
Christoph Hellwigd2852032014-01-17 12:06:53 +0100559static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
560{
Christoph Hellwig57292b52017-01-31 16:57:29 +0100561 if (!blk_rq_is_passthrough(cmd->request)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +0100562 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
563
564 if (drv->uninit_command)
565 drv->uninit_command(cmd);
566 }
567}
568
569static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd)
570{
Ming Lin91dbc082016-04-04 14:48:07 -0700571 struct scsi_data_buffer *sdb;
572
Christoph Hellwigd2852032014-01-17 12:06:53 +0100573 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700574 sg_free_table_chained(&cmd->sdb.table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700575 if (cmd->request->next_rq) {
576 sdb = cmd->request->next_rq->special;
577 if (sdb)
Ming Lin001d63b2016-04-04 14:48:09 -0700578 sg_free_table_chained(&sdb->table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700579 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100580 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700581 sg_free_table_chained(&cmd->prot_sdb->table, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100582}
583
584static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
585{
Christoph Hellwigd2852032014-01-17 12:06:53 +0100586 scsi_mq_free_sgtables(cmd);
587 scsi_uninit_cmd(cmd);
Bart Van Assche2dd6fb52017-06-02 14:21:58 -0700588 scsi_del_cmd_from_list(cmd);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/*
592 * Function: scsi_release_buffers()
593 *
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200594 * Purpose: Free resources allocate for a scsi_command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 *
596 * Arguments: cmd - command that we are bailing.
597 *
598 * Lock status: Assumed that no lock is held upon entry.
599 *
600 * Returns: Nothing
601 *
602 * Notes: In the event that an upper level driver rejects a
603 * command, we must release resources allocated during
604 * the __init_io() function. Primarily this would involve
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200605 * the scatter-gather table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Christoph Hellwigf1bea552014-04-15 12:26:54 +0200607static void scsi_release_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200609 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700610 sg_free_table_chained(&cmd->sdb.table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200611
612 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
613
614 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700615 sg_free_table_chained(&cmd->prot_sdb->table, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200618static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
619{
620 struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special;
621
Ming Lin001d63b2016-04-04 14:48:09 -0700622 sg_free_table_chained(&bidi_sdb->table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200623 kmem_cache_free(scsi_sdb_cache, bidi_sdb);
624 cmd->request->next_rq->special = NULL;
625}
626
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200627static bool scsi_end_request(struct request *req, blk_status_t error,
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800628 unsigned int bytes, unsigned int bidi_bytes)
629{
630 struct scsi_cmnd *cmd = req->special;
631 struct scsi_device *sdev = cmd->device;
632 struct request_queue *q = sdev->request_queue;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800633
634 if (blk_update_request(req, error, bytes))
635 return true;
636
637 /* Bidi request must be completed as a whole */
638 if (unlikely(bidi_bytes) &&
639 blk_update_request(req->next_rq, error, bidi_bytes))
640 return true;
641
642 if (blk_queue_add_random(q))
643 add_disk_randomness(req->rq_disk);
644
Christoph Hellwigd2852032014-01-17 12:06:53 +0100645 if (req->mq_ctx) {
646 /*
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700647 * In the MQ case the command gets freed by __blk_mq_end_request,
Christoph Hellwigd2852032014-01-17 12:06:53 +0100648 * so we have to do all cleanup that depends on it earlier.
649 *
650 * We also can't kick the queues from irq context, so we
651 * will have to defer it to a workqueue.
652 */
653 scsi_mq_uninit_cmd(cmd);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800654
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700655 __blk_mq_end_request(req, error);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100656
657 if (scsi_target(sdev)->single_lun ||
658 !list_empty(&sdev->host->starved_list))
659 kblockd_schedule_work(&sdev->requeue_work);
660 else
Bart Van Assche36e3cf22017-04-07 11:16:53 -0700661 blk_mq_run_hw_queues(q, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100662 } else {
663 unsigned long flags;
664
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400665 if (bidi_bytes)
666 scsi_release_bidi_buffers(cmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +0300667 scsi_release_buffers(cmd);
668 scsi_put_command(cmd);
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400669
Christoph Hellwigd2852032014-01-17 12:06:53 +0100670 spin_lock_irqsave(q->queue_lock, flags);
671 blk_finish_request(req, error);
672 spin_unlock_irqrestore(q->queue_lock, flags);
673
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700674 scsi_run_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100675 }
676
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700677 put_device(&sdev->sdev_gendev);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800678 return false;
679}
680
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200681/**
682 * __scsi_error_from_host_byte - translate SCSI error code into errno
683 * @cmd: SCSI command (unused)
684 * @result: scsi error code
685 *
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200686 * Translate SCSI error code into block errors.
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200687 */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200688static blk_status_t __scsi_error_from_host_byte(struct scsi_cmnd *cmd,
689 int result)
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100690{
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200691 switch (host_byte(result)) {
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100692 case DID_TRANSPORT_FAILFAST:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200693 return BLK_STS_TRANSPORT;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100694 case DID_TARGET_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000695 set_host_byte(cmd, DID_OK);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200696 return BLK_STS_TARGET;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100697 case DID_NEXUS_FAILURE:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200698 return BLK_STS_NEXUS;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200699 case DID_ALLOC_FAILURE:
700 set_host_byte(cmd, DID_OK);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200701 return BLK_STS_NOSPC;
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200702 case DID_MEDIUM_ERROR:
703 set_host_byte(cmd, DID_OK);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200704 return BLK_STS_MEDIUM;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100705 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200706 return BLK_STS_IOERR;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100707 }
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100708}
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710/*
711 * Function: scsi_io_completion()
712 *
713 * Purpose: Completion processing for block device I/O requests.
714 *
715 * Arguments: cmd - command that is finished.
716 *
717 * Lock status: Assumed that no lock is held upon entry.
718 *
719 * Returns: Nothing
720 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200721 * Notes: We will finish off the specified number of sectors. If we
722 * are done, the command block will be released and the queue
723 * function will be goosed. If we are not done then we have to
Alan Sternb60af5b2008-11-03 15:56:47 -0500724 * figure out what to do next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 *
Alan Sternb60af5b2008-11-03 15:56:47 -0500726 * a) We can call scsi_requeue_command(). The request
727 * will be unprepared and put back on the queue. Then
728 * a new command will be created for it. This should
729 * be used if we made forward progress, or if we want
730 * to switch from READ(10) to READ(6) for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200732 * b) We can call __scsi_queue_insert(). The request will
Alan Sternb60af5b2008-11-03 15:56:47 -0500733 * be put back on the queue and retried using the same
734 * command as before, possibly after a delay.
735 *
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800736 * c) We can call scsi_end_request() with -EIO to fail
Alan Sternb60af5b2008-11-03 15:56:47 -0500737 * the remainder of the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700739void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int result = cmd->result;
Jens Axboe165125e2007-07-24 09:28:11 +0200742 struct request_queue *q = cmd->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct request *req = cmd->request;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200744 blk_status_t error = BLK_STS_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct scsi_sense_hdr sshdr;
Hannes Reinecke4753cbc2014-10-24 14:26:52 +0200746 bool sense_valid = false;
Hannes Reineckec11c0042014-10-24 14:27:01 +0200747 int sense_deferred = 0, level = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500748 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
749 ACTION_DELAYED_RETRY} action;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900750 unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (result) {
753 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
754 if (sense_valid)
755 sense_deferred = scsi_sense_is_deferred(&sshdr);
756 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200757
Christoph Hellwig57292b52017-01-31 16:57:29 +0100758 if (blk_rq_is_passthrough(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (result) {
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100760 if (sense_valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /*
762 * SG_IO wants current and deferred errors
763 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100764 scsi_req(req)->sense_len =
765 min(8 + cmd->sense_buffer[7],
766 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500768 if (!sense_deferred)
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100769 error = __scsi_error_from_host_byte(cmd, result);
Pete Wyckoffb22f6872007-03-13 16:53:28 -0400770 }
Mike Snitzer27c41972012-05-31 15:05:33 -0400771 /*
772 * __scsi_error_from_host_byte may have reset the host_byte
773 */
Christoph Hellwig17d53632017-04-20 16:03:01 +0200774 scsi_req(req)->result = cmd->result;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100775 scsi_req(req)->resid_len = scsi_get_resid(cmd);
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900776
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200777 if (scsi_bidi_cmnd(cmd)) {
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900778 /*
779 * Bidi commands Must be complete as a whole,
780 * both sides at once.
781 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100782 scsi_req(req->next_rq)->resid_len = scsi_in(cmd)->resid;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200783 if (scsi_end_request(req, BLK_STS_OK, blk_rq_bytes(req),
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800784 blk_rq_bytes(req->next_rq)))
785 BUG();
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200786 return;
787 }
James Bottomley89fb4cd2014-07-03 19:17:34 +0200788 } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
789 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100790 * Flush commands do not transfers any data, and thus cannot use
James Bottomley89fb4cd2014-07-03 19:17:34 +0200791 * good_bytes != blk_rq_bytes(req) as the signal for an error.
792 * This sets the error explicitly for the problem case.
793 */
794 error = __scsi_error_from_host_byte(cmd, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
Christoph Hellwig57292b52017-01-31 16:57:29 +0100797 /* no bidi support for !blk_rq_is_passthrough yet */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200798 BUG_ON(blk_bidi_rq(req));
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 * Next deal with any sectors which we were able to correctly
802 * handle.
803 */
Hannes Reinecke91921e02014-06-25 16:39:59 +0200804 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
805 "%u sectors total, %d bytes done.\n",
806 blk_rq_sectors(req), good_bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
James Bottomleya9bddd72009-03-30 16:55:51 +0000808 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100809 * Recovered errors need reporting, but they're always treated as
810 * success, so fiddle the result code here. For passthrough requests
Christoph Hellwig17d53632017-04-20 16:03:01 +0200811 * we already took a copy of the original into sreq->result which
James Bottomleya9bddd72009-03-30 16:55:51 +0000812 * is what gets returned to the user
813 */
Douglas Gilberte7efe592010-01-03 13:51:15 -0500814 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
815 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
816 * print since caller wants ATA registers. Only occurs on
817 * SCSI ATA PASS_THROUGH commands when CK_COND=1
818 */
819 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
820 ;
Christoph Hellwige8064022016-10-20 15:12:13 +0200821 else if (!(req->rq_flags & RQF_QUIET))
Hannes Reinecked811b842014-10-24 14:26:45 +0200822 scsi_print_sense(cmd);
James Bottomleya9bddd72009-03-30 16:55:51 +0000823 result = 0;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100824 /* for passthrough error may be set */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200825 error = BLK_STS_OK;
James Bottomleya9bddd72009-03-30 16:55:51 +0000826 }
827
828 /*
James Bottomleya621bac2016-05-13 12:04:06 -0700829 * special case: failed zero length commands always need to
830 * drop down into the retry code. Otherwise, if we finished
831 * all bytes in the request we are done now.
James Bottomleyd6b0c532006-07-02 10:06:28 -0500832 */
James Bottomleya621bac2016-05-13 12:04:06 -0700833 if (!(blk_rq_bytes(req) == 0 && error) &&
834 !scsi_end_request(req, error, good_bytes, 0))
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800835 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200836
837 /*
838 * Kill remainder if no retrys.
839 */
840 if (error && scsi_noretry_cmd(cmd)) {
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800841 if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
842 BUG();
843 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200844 }
845
846 /*
847 * If there had been no error, but we have leftover bytes in the
848 * requeues just queue the command up again.
849 */
850 if (result == 0)
851 goto requeue;
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700852
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100853 error = __scsi_error_from_host_byte(cmd, result);
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500854
Alan Sternb60af5b2008-11-03 15:56:47 -0500855 if (host_byte(result) == DID_RESET) {
856 /* Third party bus reset or reset for error recovery
857 * reasons. Just retry the command and see what
858 * happens.
859 */
860 action = ACTION_RETRY;
861 } else if (sense_valid && !sense_deferred) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 switch (sshdr.sense_key) {
863 case UNIT_ATTENTION:
864 if (cmd->device->removable) {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700865 /* Detected disc change. Set a bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * and quietly refuse further access.
867 */
868 cmd->device->changed = 1;
Alan Sternb60af5b2008-11-03 15:56:47 -0500869 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 } else {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700871 /* Must have been a power glitch, or a
872 * bus reset. Could not have been a
873 * media change, so we just retry the
Alan Sternb60af5b2008-11-03 15:56:47 -0500874 * command and see what happens.
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700875 */
Alan Sternb60af5b2008-11-03 15:56:47 -0500876 action = ACTION_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 break;
879 case ILLEGAL_REQUEST:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700880 /* If we had an ILLEGAL REQUEST returned, then
881 * we may have performed an unsupported
882 * command. The only thing this should be
883 * would be a ten byte read where only a six
884 * byte read was supported. Also, on a system
885 * where READ CAPACITY failed, we may have
886 * read past the end of the disk.
887 */
Jens Axboe26a68012005-11-29 21:03:34 +0100888 if ((cmd->device->use_10_for_rw &&
889 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 (cmd->cmnd[0] == READ_10 ||
891 cmd->cmnd[0] == WRITE_10)) {
Alan Sternb60af5b2008-11-03 15:56:47 -0500892 /* This will issue a new 6-byte command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 cmd->device->use_10_for_rw = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500894 action = ACTION_REPREP;
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500895 } else if (sshdr.asc == 0x10) /* DIX */ {
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500896 action = ACTION_FAIL;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200897 error = BLK_STS_PROTECTION;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500898 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
Martin K. Petersen5db44862012-09-18 12:19:32 -0400899 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500900 action = ACTION_FAIL;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200901 error = BLK_STS_TARGET;
Alan Sternb60af5b2008-11-03 15:56:47 -0500902 } else
903 action = ACTION_FAIL;
904 break;
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400905 case ABORTED_COMMAND:
James Bottomley126c09822009-02-19 21:48:54 +0000906 action = ACTION_FAIL;
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200907 if (sshdr.asc == 0x10) /* DIF */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200908 error = BLK_STS_PROTECTION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 break;
910 case NOT_READY:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700911 /* If the device is in the process of becoming
James Bottomleyf3e93f72006-04-25 16:48:30 -0500912 * ready, or has a temporary blockage, retry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
James Bottomleyf3e93f72006-04-25 16:48:30 -0500914 if (sshdr.asc == 0x04) {
915 switch (sshdr.ascq) {
916 case 0x01: /* becoming ready */
917 case 0x04: /* format in progress */
918 case 0x05: /* rebuild in progress */
919 case 0x06: /* recalculation in progress */
920 case 0x07: /* operation in progress */
921 case 0x08: /* Long write in progress */
922 case 0x09: /* self test in progress */
Martin K. Petersend8705f12009-11-26 12:00:41 -0500923 case 0x14: /* space allocation in progress */
Alan Sternb60af5b2008-11-03 15:56:47 -0500924 action = ACTION_DELAYED_RETRY;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500925 break;
Alan Stern3dbf6a52008-12-15 10:31:28 -0500926 default:
Alan Stern3dbf6a52008-12-15 10:31:28 -0500927 action = ACTION_FAIL;
928 break;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500929 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200930 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500931 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 case VOLUME_OVERFLOW:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700934 /* See SSC3rXX or current. */
Alan Sternb60af5b2008-11-03 15:56:47 -0500935 action = ACTION_FAIL;
936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 default:
Alan Sternb60af5b2008-11-03 15:56:47 -0500938 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 break;
940 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200941 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500942 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500943
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900944 if (action != ACTION_FAIL &&
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200945 time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900946 action = ACTION_FAIL;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900947
Alan Sternb60af5b2008-11-03 15:56:47 -0500948 switch (action) {
949 case ACTION_FAIL:
950 /* Give up and fail the remainder of the request */
Christoph Hellwige8064022016-10-20 15:12:13 +0200951 if (!(req->rq_flags & RQF_QUIET)) {
Hannes Reineckef1569ff2014-10-24 14:27:07 +0200952 static DEFINE_RATELIMIT_STATE(_rs,
953 DEFAULT_RATELIMIT_INTERVAL,
954 DEFAULT_RATELIMIT_BURST);
955
956 if (unlikely(scsi_logging_level))
957 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
958 SCSI_LOG_MLCOMPLETE_BITS);
959
960 /*
961 * if logging is enabled the failure will be printed
962 * in scsi_log_completion(), so avoid duplicate messages
963 */
964 if (!level && __ratelimit(&_rs)) {
965 scsi_print_result(cmd, NULL, FAILED);
966 if (driver_byte(result) & DRIVER_SENSE)
967 scsi_print_sense(cmd);
968 scsi_print_command(cmd);
969 }
James Bottomley3173d8c2005-09-04 11:32:05 -0500970 }
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800971 if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
972 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200973 /*FALLTHRU*/
Alan Sternb60af5b2008-11-03 15:56:47 -0500974 case ACTION_REPREP:
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200975 requeue:
Alan Sternb60af5b2008-11-03 15:56:47 -0500976 /* Unprep the request and put it back at the head of the queue.
977 * A new command will be prepared and issued.
978 */
Christoph Hellwigd2852032014-01-17 12:06:53 +0100979 if (q->mq_ops) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200980 cmd->request->rq_flags &= ~RQF_DONTPREP;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100981 scsi_mq_uninit_cmd(cmd);
982 scsi_mq_requeue_cmd(cmd);
983 } else {
984 scsi_release_buffers(cmd);
985 scsi_requeue_command(q, cmd);
986 }
Alan Sternb60af5b2008-11-03 15:56:47 -0500987 break;
988 case ACTION_RETRY:
989 /* Retry the same command immediately */
James Bottomley4f5299a2009-01-02 10:42:21 -0600990 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -0500991 break;
992 case ACTION_DELAYED_RETRY:
993 /* Retry the same command after a delay */
James Bottomley4f5299a2009-01-02 10:42:21 -0600994 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -0500995 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Christoph Hellwig3c356bd2014-09-05 18:20:23 -0700999static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001001 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001004 * If sg table allocation fails, requeue request later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001006 if (unlikely(sg_alloc_table_chained(&sdb->table,
1007 blk_rq_nr_phys_segments(req), sdb->table.sgl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return BLKPREP_DEFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /*
1011 * Next, walk the list, and fill in the addresses and sizes of
1012 * each segment.
1013 */
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001014 count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
1015 BUG_ON(count > sdb->table.nents);
1016 sdb->table.nents = count;
Christoph Hellwigfd102b12017-01-13 12:29:11 +01001017 sdb->length = blk_rq_payload_bytes(req);
Rusty Russell4a03d902007-11-19 11:28:48 +11001018 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001020
1021/*
1022 * Function: scsi_init_io()
1023 *
1024 * Purpose: SCSI I/O initialize function.
1025 *
1026 * Arguments: cmd - Command descriptor we wish to initialize
1027 *
1028 * Returns: 0 on success
1029 * BLKPREP_DEFER if the failure is retryable
1030 * BLKPREP_KILL if the failure is fatal
1031 */
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001032int scsi_init_io(struct scsi_cmnd *cmd)
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001033{
Christoph Hellwig5e012aa2014-04-15 12:24:55 +02001034 struct scsi_device *sdev = cmd->device;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001035 struct request *rq = cmd->request;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001036 bool is_mq = (rq->mq_ctx != NULL);
Johannes Thumshirne7661a82017-04-12 09:21:19 +02001037 int error = BLKPREP_KILL;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001038
Johannes Thumshirnfd3fc0b2017-01-31 10:16:00 +01001039 if (WARN_ON_ONCE(!blk_rq_nr_phys_segments(rq)))
Johannes Thumshirne7661a82017-04-12 09:21:19 +02001040 goto err_exit;
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001041
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001042 error = scsi_init_sgtable(rq, &cmd->sdb);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001043 if (error)
1044 goto err_exit;
1045
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001046 if (blk_bidi_rq(rq)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001047 if (!rq->q->mq_ops) {
1048 struct scsi_data_buffer *bidi_sdb =
1049 kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC);
1050 if (!bidi_sdb) {
1051 error = BLKPREP_DEFER;
1052 goto err_exit;
1053 }
1054
1055 rq->next_rq->special = bidi_sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001056 }
1057
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001058 error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001059 if (error)
1060 goto err_exit;
1061 }
1062
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001063 if (blk_integrity_rq(rq)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001064 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1065 int ivecs, count;
1066
Ewan D. Milne91724c22015-01-15 10:02:12 -05001067 if (prot_sdb == NULL) {
1068 /*
1069 * This can happen if someone (e.g. multipath)
1070 * queues a command to a device on an adapter
1071 * that does not support DIX.
1072 */
1073 WARN_ON_ONCE(1);
1074 error = BLKPREP_KILL;
1075 goto err_exit;
1076 }
1077
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001078 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001079
Ming Lin001d63b2016-04-04 14:48:09 -07001080 if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
Ming Lin22cc3d42016-04-04 14:48:08 -07001081 prot_sdb->table.sgl)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001082 error = BLKPREP_DEFER;
1083 goto err_exit;
1084 }
1085
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001086 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001087 prot_sdb->table.sgl);
1088 BUG_ON(unlikely(count > ivecs));
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001089 BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q)));
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001090
1091 cmd->prot_sdb = prot_sdb;
1092 cmd->prot_sdb->table.nents = count;
1093 }
1094
Christoph Hellwigd2852032014-01-17 12:06:53 +01001095 return BLKPREP_OK;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001096err_exit:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001097 if (is_mq) {
1098 scsi_mq_free_sgtables(cmd);
1099 } else {
1100 scsi_release_buffers(cmd);
1101 cmd->request->special = NULL;
1102 scsi_put_command(cmd);
1103 put_device(&sdev->sdev_gendev);
1104 }
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001105 return error;
1106}
Boaz Harroshbb52d822007-12-13 16:14:27 -08001107EXPORT_SYMBOL(scsi_init_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07001109/**
1110 * scsi_initialize_rq - initialize struct scsi_cmnd.req
1111 *
1112 * Called from inside blk_get_request().
1113 */
1114void scsi_initialize_rq(struct request *rq)
1115{
Bart Van Asschec8d9cf22017-06-20 11:15:42 -07001116 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1117
1118 scsi_req_init(&cmd->req);
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07001119}
1120EXPORT_SYMBOL(scsi_initialize_rq);
1121
Bart Van Assche2dd6fb52017-06-02 14:21:58 -07001122/* Add a command to the list used by the aacraid and dpt_i2o drivers */
1123void scsi_add_cmd_to_list(struct scsi_cmnd *cmd)
1124{
1125 struct scsi_device *sdev = cmd->device;
1126 struct Scsi_Host *shost = sdev->host;
1127 unsigned long flags;
1128
1129 if (shost->use_cmd_list) {
1130 spin_lock_irqsave(&sdev->list_lock, flags);
1131 list_add_tail(&cmd->list, &sdev->cmd_list);
1132 spin_unlock_irqrestore(&sdev->list_lock, flags);
1133 }
1134}
1135
1136/* Remove a command from the list used by the aacraid and dpt_i2o drivers */
1137void scsi_del_cmd_from_list(struct scsi_cmnd *cmd)
1138{
1139 struct scsi_device *sdev = cmd->device;
1140 struct Scsi_Host *shost = sdev->host;
1141 unsigned long flags;
1142
1143 if (shost->use_cmd_list) {
1144 spin_lock_irqsave(&sdev->list_lock, flags);
1145 BUG_ON(list_empty(&cmd->list));
1146 list_del_init(&cmd->list);
1147 spin_unlock_irqrestore(&sdev->list_lock, flags);
1148 }
1149}
1150
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07001151/* Called after a request has been started. */
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001152void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001153{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001154 void *buf = cmd->sense_buffer;
1155 void *prot = cmd->prot_sdb;
Bart Van Assche8e688252017-06-02 14:21:52 -07001156 unsigned int unchecked_isa_dma = cmd->flags & SCMD_UNCHECKED_ISA_DMA;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001157
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001158 /* zero out the cmd, except for the embedded scsi_request */
1159 memset((char *)cmd + sizeof(cmd->req), 0,
Christoph Hellwigee524232017-02-21 10:04:55 +01001160 sizeof(*cmd) - sizeof(cmd->req) + dev->host->hostt->cmd_size);
Christoph Hellwig04796332014-02-20 14:20:55 -08001161
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001162 cmd->device = dev;
1163 cmd->sense_buffer = buf;
1164 cmd->prot_sdb = prot;
Bart Van Assche8e688252017-06-02 14:21:52 -07001165 cmd->flags = unchecked_isa_dma;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001166 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1167 cmd->jiffies_at_alloc = jiffies;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001168
Bart Van Assche2dd6fb52017-06-02 14:21:58 -07001169 scsi_add_cmd_to_list(cmd);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001170}
1171
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001172static int scsi_setup_scsi_cmnd(struct scsi_device *sdev, struct request *req)
James Bottomley7b163182005-12-15 20:17:02 -06001173{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001174 struct scsi_cmnd *cmd = req->special;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001175
1176 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001177 * Passthrough requests may transfer data, in which case they must
Christoph Hellwig3b003152006-11-04 20:10:55 +01001178 * a bio attached to them. Or they might contain a SCSI command
1179 * that does not transfer data, in which case they may optionally
1180 * submit a request without an attached bio.
1181 */
1182 if (req->bio) {
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001183 int ret = scsi_init_io(cmd);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001184 if (unlikely(ret))
1185 return ret;
1186 } else {
Tejun Heob0790412009-05-07 22:24:42 +09001187 BUG_ON(blk_rq_bytes(req));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001188
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001189 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001190 }
James Bottomley7b163182005-12-15 20:17:02 -06001191
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001192 cmd->cmd_len = scsi_req(req)->cmd_len;
1193 cmd->cmnd = scsi_req(req)->cmd;
Tejun Heob0790412009-05-07 22:24:42 +09001194 cmd->transfersize = blk_rq_bytes(req);
Christoph Hellwig64c7f1d2017-04-05 19:18:12 +02001195 cmd->allowed = scsi_req(req)->retries;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001196 return BLKPREP_OK;
1197}
1198
1199/*
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001200 * Setup a normal block command. These are simple request from filesystems
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001201 * that still need to be translated to SCSI CDBs from the ULD.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001202 */
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001203static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001204{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001205 struct scsi_cmnd *cmd = req->special;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001206
Christoph Hellwigee14c672015-08-27 14:16:59 +02001207 if (unlikely(sdev->handler && sdev->handler->prep_fn)) {
1208 int ret = sdev->handler->prep_fn(sdev, req);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001209 if (ret != BLKPREP_OK)
1210 return ret;
1211 }
1212
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001213 cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001214 memset(cmd->cmnd, 0, BLK_MAX_CDB);
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001215 return scsi_cmd_to_driver(cmd)->init_command(cmd);
James Bottomley7b163182005-12-15 20:17:02 -06001216}
James Bottomley7b163182005-12-15 20:17:02 -06001217
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001218static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req)
1219{
1220 struct scsi_cmnd *cmd = req->special;
1221
1222 if (!blk_rq_bytes(req))
1223 cmd->sc_data_direction = DMA_NONE;
1224 else if (rq_data_dir(req) == WRITE)
1225 cmd->sc_data_direction = DMA_TO_DEVICE;
1226 else
1227 cmd->sc_data_direction = DMA_FROM_DEVICE;
1228
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001229 if (blk_rq_is_scsi(req))
1230 return scsi_setup_scsi_cmnd(sdev, req);
1231 else
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001232 return scsi_setup_fs_cmnd(sdev, req);
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001233}
1234
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001235static int
1236scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Christoph Hellwig3b003152006-11-04 20:10:55 +01001238 int ret = BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001241 * If the device is not in running state we will reject some
1242 * or all commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
Christoph Hellwig3b003152006-11-04 20:10:55 +01001245 switch (sdev->sdev_state) {
1246 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05001247 case SDEV_TRANSPORT_OFFLINE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001248 /*
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 sdev_printk(KERN_ERR, sdev,
1254 "rejecting I/O to offline device\n");
1255 ret = BLKPREP_KILL;
1256 break;
1257 case SDEV_DEL:
1258 /*
1259 * If the device is fully deleted, we refuse to
1260 * process any commands as well.
1261 */
James Bottomley9ccfc752005-10-02 11:45:08 -05001262 sdev_printk(KERN_ERR, sdev,
1263 "rejecting I/O to dead device\n");
Christoph Hellwig3b003152006-11-04 20:10:55 +01001264 ret = BLKPREP_KILL;
1265 break;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001266 case SDEV_BLOCK:
James Bottomley6f4267e2008-08-22 16:53:31 -05001267 case SDEV_CREATED_BLOCK:
Bart Van Asschebba0bdd2015-03-04 10:31:47 +01001268 ret = BLKPREP_DEFER;
1269 break;
1270 case SDEV_QUIESCE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001271 /*
1272 * If the devices is blocked we defer normal commands.
1273 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001274 if (!(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001275 ret = BLKPREP_DEFER;
1276 break;
1277 default:
1278 /*
1279 * For any other not fully online state we only allow
1280 * special commands. In particular any user initiated
1281 * command is not allowed.
1282 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001283 if (!(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001284 ret = BLKPREP_KILL;
1285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001288 return ret;
1289}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001291static int
1292scsi_prep_return(struct request_queue *q, struct request *req, int ret)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001293{
1294 struct scsi_device *sdev = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Christoph Hellwig3b003152006-11-04 20:10:55 +01001296 switch (ret) {
1297 case BLKPREP_KILL:
jiangyiwene1cd3912016-02-16 20:14:13 +08001298 case BLKPREP_INVALID:
Christoph Hellwig17d53632017-04-20 16:03:01 +02001299 scsi_req(req)->result = DID_NO_CONNECT << 16;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001300 /* release the command and kill it */
1301 if (req->special) {
1302 struct scsi_cmnd *cmd = req->special;
1303 scsi_release_buffers(cmd);
1304 scsi_put_command(cmd);
Christoph Hellwig68c03d92014-04-15 12:24:56 +02001305 put_device(&sdev->sdev_gendev);
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001306 req->special = NULL;
1307 }
Christoph Hellwig3b003152006-11-04 20:10:55 +01001308 break;
1309 case BLKPREP_DEFER:
1310 /*
Tejun Heo9934c8c2009-05-08 11:54:16 +09001311 * If we defer, the blk_peek_request() returns NULL, but the
Jens Axboea488e742010-04-16 21:13:15 +02001312 * queue must be restarted, so we schedule a callback to happen
1313 * shortly.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001314 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001315 if (atomic_read(&sdev->device_busy) == 0)
Jens Axboea488e742010-04-16 21:13:15 +02001316 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001317 break;
1318 default:
Christoph Hellwige8064022016-10-20 15:12:13 +02001319 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Christoph Hellwig3b003152006-11-04 20:10:55 +01001322 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001324
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001325static int scsi_prep_fn(struct request_queue *q, struct request *req)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001326{
1327 struct scsi_device *sdev = q->queuedata;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001328 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001329 int ret;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001330
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001331 ret = scsi_prep_state_check(sdev, req);
1332 if (ret != BLKPREP_OK)
1333 goto out;
1334
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001335 if (!req->special) {
1336 /* Bail if we can't get a reference to the device */
1337 if (unlikely(!get_device(&sdev->sdev_gendev))) {
1338 ret = BLKPREP_DEFER;
1339 goto out;
1340 }
1341
1342 scsi_init_command(sdev, cmd);
1343 req->special = cmd;
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001344 }
1345
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001346 cmd->tag = req->tag;
1347 cmd->request = req;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001348 cmd->prot_op = SCSI_PROT_NORMAL;
1349
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001350 ret = scsi_setup_cmnd(sdev, req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001351out:
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001352 return scsi_prep_return(q, req, ret);
1353}
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001354
1355static void scsi_unprep_fn(struct request_queue *q, struct request *req)
1356{
Christoph Hellwigd2852032014-01-17 12:06:53 +01001357 scsi_uninit_cmd(req->special);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360/*
1361 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1362 * return 0.
1363 *
1364 * Called with the queue_lock held.
1365 */
1366static inline int scsi_dev_queue_ready(struct request_queue *q,
1367 struct scsi_device *sdev)
1368{
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001369 unsigned int busy;
1370
1371 busy = atomic_inc_return(&sdev->device_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001372 if (atomic_read(&sdev->device_blocked)) {
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001373 if (busy)
1374 goto out_dec;
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /*
1377 * unblock after device_blocked iterates to zero
1378 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001379 if (atomic_dec_return(&sdev->device_blocked) > 0) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001380 /*
1381 * For the MQ case we take care of this in the caller.
1382 */
1383 if (!q->mq_ops)
1384 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001385 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001387 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1388 "unblocking device at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001390
1391 if (busy >= sdev->queue_depth)
1392 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 return 1;
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001395out_dec:
1396 atomic_dec(&sdev->device_busy);
1397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Mike Christief0c0a372008-08-17 15:24:38 -05001400/*
1401 * scsi_target_queue_ready: checks if there we can send commands to target
1402 * @sdev: scsi device on starget to check.
Mike Christief0c0a372008-08-17 15:24:38 -05001403 */
1404static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1405 struct scsi_device *sdev)
1406{
1407 struct scsi_target *starget = scsi_target(sdev);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001408 unsigned int busy;
Mike Christief0c0a372008-08-17 15:24:38 -05001409
1410 if (starget->single_lun) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001411 spin_lock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001412 if (starget->starget_sdev_user &&
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001413 starget->starget_sdev_user != sdev) {
1414 spin_unlock_irq(shost->host_lock);
1415 return 0;
1416 }
Mike Christief0c0a372008-08-17 15:24:38 -05001417 starget->starget_sdev_user = sdev;
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001418 spin_unlock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001419 }
1420
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001421 if (starget->can_queue <= 0)
1422 return 1;
1423
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001424 busy = atomic_inc_return(&starget->target_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001425 if (atomic_read(&starget->target_blocked) > 0) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001426 if (busy)
1427 goto starved;
1428
Mike Christief0c0a372008-08-17 15:24:38 -05001429 /*
1430 * unblock after target_blocked iterates to zero
1431 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001432 if (atomic_dec_return(&starget->target_blocked) > 0)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001433 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001434
1435 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1436 "unblocking target at zero depth\n"));
Mike Christief0c0a372008-08-17 15:24:38 -05001437 }
1438
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001439 if (busy >= starget->can_queue)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001440 goto starved;
Mike Christief0c0a372008-08-17 15:24:38 -05001441
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001442 return 1;
1443
1444starved:
1445 spin_lock_irq(shost->host_lock);
1446 list_move_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001447 spin_unlock_irq(shost->host_lock);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001448out_dec:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001449 if (starget->can_queue > 0)
1450 atomic_dec(&starget->target_busy);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001451 return 0;
Mike Christief0c0a372008-08-17 15:24:38 -05001452}
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454/*
1455 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1456 * return 0. We must end up running the queue again whenever 0 is
1457 * returned, else IO can hang.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 */
1459static inline int scsi_host_queue_ready(struct request_queue *q,
1460 struct Scsi_Host *shost,
1461 struct scsi_device *sdev)
1462{
Christoph Hellwig74665012014-01-22 15:29:29 +01001463 unsigned int busy;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001464
James Bottomley939647e2005-09-18 15:05:20 -05001465 if (scsi_host_in_recovery(shost))
Christoph Hellwig74665012014-01-22 15:29:29 +01001466 return 0;
1467
1468 busy = atomic_inc_return(&shost->host_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001469 if (atomic_read(&shost->host_blocked) > 0) {
Christoph Hellwig74665012014-01-22 15:29:29 +01001470 if (busy)
1471 goto starved;
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /*
1474 * unblock after host_blocked iterates to zero
1475 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001476 if (atomic_dec_return(&shost->host_blocked) > 0)
Christoph Hellwig74665012014-01-22 15:29:29 +01001477 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001478
1479 SCSI_LOG_MLQUEUE(3,
1480 shost_printk(KERN_INFO, shost,
1481 "unblocking host at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Christoph Hellwig74665012014-01-22 15:29:29 +01001483
1484 if (shost->can_queue > 0 && busy >= shost->can_queue)
1485 goto starved;
1486 if (shost->host_self_blocked)
1487 goto starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 /* We're OK to process the command, so we can't be starved */
Christoph Hellwig74665012014-01-22 15:29:29 +01001490 if (!list_empty(&sdev->starved_entry)) {
1491 spin_lock_irq(shost->host_lock);
1492 if (!list_empty(&sdev->starved_entry))
1493 list_del_init(&sdev->starved_entry);
1494 spin_unlock_irq(shost->host_lock);
1495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Christoph Hellwig74665012014-01-22 15:29:29 +01001497 return 1;
1498
1499starved:
1500 spin_lock_irq(shost->host_lock);
1501 if (list_empty(&sdev->starved_entry))
1502 list_add_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001503 spin_unlock_irq(shost->host_lock);
Christoph Hellwig74665012014-01-22 15:29:29 +01001504out_dec:
1505 atomic_dec(&shost->host_busy);
1506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
1509/*
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001510 * Busy state exporting function for request stacking drivers.
1511 *
1512 * For efficiency, no lock is taken to check the busy state of
1513 * shost/starget/sdev, since the returned value is not guaranteed and
1514 * may be changed after request stacking drivers call the function,
1515 * regardless of taking lock or not.
1516 *
Bart Van Assche67bd9412012-06-29 15:33:22 +00001517 * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
1518 * needs to return 'not busy'. Otherwise, request stacking drivers
1519 * may hold requests forever.
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001520 */
1521static int scsi_lld_busy(struct request_queue *q)
1522{
1523 struct scsi_device *sdev = q->queuedata;
1524 struct Scsi_Host *shost;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001525
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001526 if (blk_queue_dying(q))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001527 return 0;
1528
1529 shost = sdev->host;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001530
Jun'ichi Nomurab7e94a12012-05-22 18:57:17 +09001531 /*
1532 * Ignore host/starget busy state.
1533 * Since block layer does not have a concept of fairness across
1534 * multiple queues, congestion of host/starget needs to be handled
1535 * in SCSI layer.
1536 */
1537 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001538 return 1;
1539
1540 return 0;
1541}
1542
1543/*
James Bottomleye91442b2005-09-09 10:44:16 -05001544 * Kill a request for a dead device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 */
Jens Axboe165125e2007-07-24 09:28:11 +02001546static void scsi_kill_request(struct request *req, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
James Bottomleye91442b2005-09-09 10:44:16 -05001548 struct scsi_cmnd *cmd = req->special;
Jiri Slaby03b14702009-09-23 16:15:35 +02001549 struct scsi_device *sdev;
1550 struct scsi_target *starget;
1551 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Tejun Heo9934c8c2009-05-08 11:54:16 +09001553 blk_start_request(req);
James Bottomley788ce432005-09-09 13:40:23 -05001554
Hannes Reinecke74571812011-11-09 08:39:24 +01001555 scmd_printk(KERN_INFO, cmd, "killing request\n");
1556
Jiri Slaby03b14702009-09-23 16:15:35 +02001557 sdev = cmd->device;
1558 starget = scsi_target(sdev);
1559 shost = sdev->host;
James Bottomleye91442b2005-09-09 10:44:16 -05001560 scsi_init_cmd_errh(cmd);
1561 cmd->result = DID_NO_CONNECT << 16;
1562 atomic_inc(&cmd->device->iorequest_cnt);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001563
1564 /*
1565 * SCSI request completion path will do scsi_device_unbusy(),
1566 * bump busy counts. To bump the counters, we need to dance
1567 * with the locks as normal issue path does.
1568 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001569 atomic_inc(&sdev->device_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +01001570 atomic_inc(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001571 if (starget->can_queue > 0)
1572 atomic_inc(&starget->target_busy);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001573
Jens Axboe242f9dc2008-09-14 05:55:09 -07001574 blk_complete_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Jens Axboe1aea6432006-01-09 16:03:03 +01001577static void scsi_softirq_done(struct request *rq)
1578{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001579 struct scsi_cmnd *cmd = rq->special;
1580 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
Jens Axboe1aea6432006-01-09 16:03:03 +01001581 int disposition;
1582
1583 INIT_LIST_HEAD(&cmd->eh_entry);
1584
Jens Axboe242f9dc2008-09-14 05:55:09 -07001585 atomic_inc(&cmd->device->iodone_cnt);
1586 if (cmd->result)
1587 atomic_inc(&cmd->device->ioerr_cnt);
1588
Jens Axboe1aea6432006-01-09 16:03:03 +01001589 disposition = scsi_decide_disposition(cmd);
1590 if (disposition != SUCCESS &&
1591 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
1592 sdev_printk(KERN_ERR, cmd->device,
1593 "timing out command, waited %lus\n",
1594 wait_for/HZ);
1595 disposition = SUCCESS;
1596 }
Hannes Reinecke91921e02014-06-25 16:39:59 +02001597
Jens Axboe1aea6432006-01-09 16:03:03 +01001598 scsi_log_completion(cmd, disposition);
1599
1600 switch (disposition) {
1601 case SUCCESS:
1602 scsi_finish_command(cmd);
1603 break;
1604 case NEEDS_RETRY:
Christoph Hellwig596f4822007-01-02 12:56:00 +01001605 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
Jens Axboe1aea6432006-01-09 16:03:03 +01001606 break;
1607 case ADD_TO_MLQUEUE:
1608 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1609 break;
1610 default:
Hannes Reineckea0658632017-04-06 15:36:35 +02001611 scsi_eh_scmd_add(cmd);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +02001612 break;
Jens Axboe1aea6432006-01-09 16:03:03 +01001613 }
1614}
1615
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001616/**
Christoph Hellwig82042a22014-09-05 18:23:07 -07001617 * scsi_dispatch_command - Dispatch a command to the low-level driver.
1618 * @cmd: command block we are dispatching.
1619 *
1620 * Return: nonzero return request was rejected and device's queue needs to be
1621 * plugged.
1622 */
1623static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
1624{
1625 struct Scsi_Host *host = cmd->device->host;
1626 int rtn = 0;
1627
1628 atomic_inc(&cmd->device->iorequest_cnt);
1629
1630 /* check if the device is still usable */
1631 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
1632 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
1633 * returns an immediate error upwards, and signals
1634 * that the device is no longer present */
1635 cmd->result = DID_NO_CONNECT << 16;
1636 goto done;
1637 }
1638
1639 /* Check to see if the scsi lld made this device blocked. */
1640 if (unlikely(scsi_device_blocked(cmd->device))) {
1641 /*
1642 * in blocked state, the command is just put back on
1643 * the device queue. The suspend state has already
1644 * blocked the queue so future requests should not
1645 * occur until the device transitions out of the
1646 * suspend state.
1647 */
1648 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1649 "queuecommand : device blocked\n"));
1650 return SCSI_MLQUEUE_DEVICE_BUSY;
1651 }
1652
1653 /* Store the LUN value in cmnd, if needed. */
1654 if (cmd->device->lun_in_cdb)
1655 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
1656 (cmd->device->lun << 5 & 0xe0);
1657
1658 scsi_log_send(cmd);
1659
1660 /*
1661 * Before we queue this command, check if the command
1662 * length exceeds what the host adapter can handle.
1663 */
1664 if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
1665 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1666 "queuecommand : command too long. "
1667 "cdb_size=%d host->max_cmd_len=%d\n",
1668 cmd->cmd_len, cmd->device->host->max_cmd_len));
1669 cmd->result = (DID_ABORT << 16);
1670 goto done;
1671 }
1672
1673 if (unlikely(host->shost_state == SHOST_DEL)) {
1674 cmd->result = (DID_NO_CONNECT << 16);
1675 goto done;
1676
1677 }
1678
1679 trace_scsi_dispatch_cmd_start(cmd);
1680 rtn = host->hostt->queuecommand(host, cmd);
1681 if (rtn) {
1682 trace_scsi_dispatch_cmd_error(cmd, rtn);
1683 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
1684 rtn != SCSI_MLQUEUE_TARGET_BUSY)
1685 rtn = SCSI_MLQUEUE_HOST_BUSY;
1686
1687 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1688 "queuecommand : request rejected\n"));
1689 }
1690
1691 return rtn;
1692 done:
1693 cmd->scsi_done(cmd);
1694 return 0;
1695}
1696
1697/**
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001698 * scsi_done - Invoke completion on finished SCSI command.
1699 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
1700 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
1701 *
1702 * Description: This function is the mid-level's (SCSI Core) interrupt routine,
1703 * which regains ownership of the SCSI command (de facto) from a LLDD, and
1704 * calls blk_complete_request() for further processing.
1705 *
1706 * This function is interrupt context safe.
1707 */
1708static void scsi_done(struct scsi_cmnd *cmd)
1709{
1710 trace_scsi_dispatch_cmd_done(cmd);
1711 blk_complete_request(cmd->request);
1712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/*
1715 * Function: scsi_request_fn()
1716 *
1717 * Purpose: Main strategy routine for SCSI.
1718 *
1719 * Arguments: q - Pointer to actual queue.
1720 *
1721 * Returns: Nothing
1722 *
1723 * Lock status: IO request lock assumed to be held when called.
1724 */
1725static void scsi_request_fn(struct request_queue *q)
Bart Van Assche613be1f2014-02-20 14:20:56 -08001726 __releases(q->queue_lock)
1727 __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct scsi_device *sdev = q->queuedata;
1730 struct Scsi_Host *shost;
1731 struct scsi_cmnd *cmd;
1732 struct request *req;
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /*
1735 * To start with, we keep looping until the queue is empty, or until
1736 * the host is no longer able to accept any more requests.
1737 */
1738 shost = sdev->host;
Jens Axboea488e742010-04-16 21:13:15 +02001739 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 int rtn;
1741 /*
1742 * get next queueable request. We do this early to make sure
Hannes Reinecke91921e02014-06-25 16:39:59 +02001743 * that the request is fully prepared even if we cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 * accept it.
1745 */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001746 req = blk_peek_request(q);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001747 if (!req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 break;
1749
1750 if (unlikely(!scsi_device_online(sdev))) {
James Bottomley9ccfc752005-10-02 11:45:08 -05001751 sdev_printk(KERN_ERR, sdev,
1752 "rejecting I/O to offline device\n");
James Bottomleye91442b2005-09-09 10:44:16 -05001753 scsi_kill_request(req, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 continue;
1755 }
1756
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001757 if (!scsi_dev_queue_ready(q, sdev))
1758 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 /*
1761 * Remove the request from the request list.
1762 */
1763 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
Tejun Heo9934c8c2009-05-08 11:54:16 +09001764 blk_start_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001766 spin_unlock_irq(q->queue_lock);
James Bottomleye91442b2005-09-09 10:44:16 -05001767 cmd = req->special;
1768 if (unlikely(cmd == NULL)) {
1769 printk(KERN_CRIT "impossible request in %s.\n"
1770 "please mail a stack trace to "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001771 "linux-scsi@vger.kernel.org\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001772 __func__);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001773 blk_dump_rq_flags(req, "foo");
James Bottomleye91442b2005-09-09 10:44:16 -05001774 BUG();
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Mike Christieecefe8a2008-07-11 19:50:35 -05001777 /*
1778 * We hit this when the driver is using a host wide
1779 * tag map. For device level tag maps the queue_depth check
1780 * in the device ready fn would prevent us from trying
1781 * to allocate a tag. Since the map is a shared host resource
1782 * we add the dev to the starved list so it eventually gets
1783 * a run when a tag is freed.
1784 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001785 if (blk_queue_tagged(q) && !(req->rq_flags & RQF_QUEUED)) {
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001786 spin_lock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001787 if (list_empty(&sdev->starved_entry))
1788 list_add_tail(&sdev->starved_entry,
1789 &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001790 spin_unlock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001791 goto not_ready;
1792 }
1793
Mike Christief0c0a372008-08-17 15:24:38 -05001794 if (!scsi_target_queue_ready(shost, sdev))
1795 goto not_ready;
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (!scsi_host_queue_ready(q, shost, sdev))
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001798 goto host_not_ready;
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001799
1800 if (sdev->simple_tags)
1801 cmd->flags |= SCMD_TAGGED;
1802 else
1803 cmd->flags &= ~SCMD_TAGGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /*
1806 * Finally, initialize any error handling parameters, and set up
1807 * the timers for timeouts.
1808 */
1809 scsi_init_cmd_errh(cmd);
1810
1811 /*
1812 * Dispatch the command to the low-level driver.
1813 */
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001814 cmd->scsi_done = scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 rtn = scsi_dispatch_cmd(cmd);
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001816 if (rtn) {
1817 scsi_queue_insert(cmd, rtn);
1818 spin_lock_irq(q->queue_lock);
Jens Axboea488e742010-04-16 21:13:15 +02001819 goto out_delay;
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001820 }
1821 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823
Bart Van Assche613be1f2014-02-20 14:20:56 -08001824 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001826 host_not_ready:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001827 if (scsi_target(sdev)->can_queue > 0)
1828 atomic_dec(&scsi_target(sdev)->target_busy);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001829 not_ready:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /*
1831 * lock q, handle tag, requeue req, and decrement device_busy. We
1832 * must return with queue_lock held.
1833 *
1834 * Decrementing device_busy without checking it is OK, as all such
1835 * cases (host limits or settings) should run the queue at some
1836 * later time.
1837 */
1838 spin_lock_irq(q->queue_lock);
1839 blk_requeue_request(q, req);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001840 atomic_dec(&sdev->device_busy);
Jens Axboea488e742010-04-16 21:13:15 +02001841out_delay:
Guenter Roeck480cadc2014-08-10 05:54:25 -07001842 if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
Jens Axboea488e742010-04-16 21:13:15 +02001843 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001846static inline blk_status_t prep_to_mq(int ret)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001847{
1848 switch (ret) {
1849 case BLKPREP_OK:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001850 return BLK_STS_OK;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001851 case BLKPREP_DEFER:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001852 return BLK_STS_RESOURCE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001853 default:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001854 return BLK_STS_IOERR;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001855 }
1856}
1857
Bart Van Asschebe4c1862017-06-02 14:21:59 -07001858/* Size in bytes of the sg-list stored in the scsi-mq command-private data. */
1859static unsigned int scsi_mq_sgl_size(struct Scsi_Host *shost)
1860{
1861 return min_t(unsigned int, shost->sg_tablesize, SG_CHUNK_SIZE) *
1862 sizeof(struct scatterlist);
1863}
1864
Christoph Hellwigd2852032014-01-17 12:06:53 +01001865static int scsi_mq_prep_fn(struct request *req)
1866{
1867 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1868 struct scsi_device *sdev = req->q->queuedata;
1869 struct Scsi_Host *shost = sdev->host;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001870 struct scatterlist *sg;
1871
Bart Van Assche08f78432017-06-02 14:22:00 -07001872 scsi_init_command(sdev, cmd);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001873
1874 req->special = cmd;
1875
1876 cmd->request = req;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001877
1878 cmd->tag = req->tag;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001879 cmd->prot_op = SCSI_PROT_NORMAL;
1880
Christoph Hellwigd2852032014-01-17 12:06:53 +01001881 sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
1882 cmd->sdb.table.sgl = sg;
1883
1884 if (scsi_host_get_prot(shost)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001885 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
1886
1887 cmd->prot_sdb->table.sgl =
1888 (struct scatterlist *)(cmd->prot_sdb + 1);
1889 }
1890
1891 if (blk_bidi_rq(req)) {
1892 struct request *next_rq = req->next_rq;
1893 struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq);
1894
1895 memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer));
1896 bidi_sdb->table.sgl =
1897 (struct scatterlist *)(bidi_sdb + 1);
1898
1899 next_rq->special = bidi_sdb;
1900 }
1901
Christoph Hellwigfe052522014-09-22 15:59:31 +02001902 blk_mq_start_request(req);
1903
Christoph Hellwigd2852032014-01-17 12:06:53 +01001904 return scsi_setup_cmnd(sdev, req);
1905}
1906
1907static void scsi_mq_done(struct scsi_cmnd *cmd)
1908{
1909 trace_scsi_dispatch_cmd_done(cmd);
Christoph Hellwig08e00292017-04-20 16:03:09 +02001910 blk_mq_complete_request(cmd->request);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001911}
1912
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001913static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -06001914 const struct blk_mq_queue_data *bd)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001915{
Jens Axboe74c45052014-10-29 11:14:52 -06001916 struct request *req = bd->rq;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001917 struct request_queue *q = req->q;
1918 struct scsi_device *sdev = q->queuedata;
1919 struct Scsi_Host *shost = sdev->host;
1920 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001921 blk_status_t ret;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001922 int reason;
1923
1924 ret = prep_to_mq(scsi_prep_state_check(sdev, req));
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001925 if (ret != BLK_STS_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001926 goto out;
1927
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001928 ret = BLK_STS_RESOURCE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001929 if (!get_device(&sdev->sdev_gendev))
1930 goto out;
1931
1932 if (!scsi_dev_queue_ready(q, sdev))
1933 goto out_put_device;
1934 if (!scsi_target_queue_ready(shost, sdev))
1935 goto out_dec_device_busy;
1936 if (!scsi_host_queue_ready(q, shost, sdev))
1937 goto out_dec_target_busy;
1938
Christoph Hellwige8064022016-10-20 15:12:13 +02001939 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001940 ret = prep_to_mq(scsi_mq_prep_fn(req));
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001941 if (ret != BLK_STS_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001942 goto out_dec_host_busy;
Christoph Hellwige8064022016-10-20 15:12:13 +02001943 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwigfe052522014-09-22 15:59:31 +02001944 } else {
1945 blk_mq_start_request(req);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001946 }
1947
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001948 if (sdev->simple_tags)
1949 cmd->flags |= SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001950 else
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001951 cmd->flags &= ~SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001952
Christoph Hellwigd2852032014-01-17 12:06:53 +01001953 scsi_init_cmd_errh(cmd);
1954 cmd->scsi_done = scsi_mq_done;
1955
1956 reason = scsi_dispatch_cmd(cmd);
1957 if (reason) {
1958 scsi_set_blocked(cmd, reason);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001959 ret = BLK_STS_RESOURCE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001960 goto out_dec_host_busy;
1961 }
1962
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001963 return BLK_STS_OK;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001964
1965out_dec_host_busy:
1966 atomic_dec(&shost->host_busy);
1967out_dec_target_busy:
1968 if (scsi_target(sdev)->can_queue > 0)
1969 atomic_dec(&scsi_target(sdev)->target_busy);
1970out_dec_device_busy:
1971 atomic_dec(&sdev->device_busy);
1972out_put_device:
1973 put_device(&sdev->sdev_gendev);
1974out:
1975 switch (ret) {
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001976 case BLK_STS_OK:
1977 break;
1978 case BLK_STS_RESOURCE:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001979 if (atomic_read(&sdev->device_busy) == 0 &&
1980 !scsi_device_blocked(sdev))
Bart Van Assche36e3cf22017-04-07 11:16:53 -07001981 blk_mq_delay_run_hw_queue(hctx, SCSI_QUEUE_DELAY);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001982 break;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001983 default:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001984 /*
1985 * Make sure to release all allocated ressources when
1986 * we hit an error, as we will never see this command
1987 * again.
1988 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001989 if (req->rq_flags & RQF_DONTPREP)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001990 scsi_mq_uninit_cmd(cmd);
1991 break;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001992 }
1993 return ret;
1994}
1995
Christoph Hellwig0152fb62014-09-13 16:40:13 -07001996static enum blk_eh_timer_return scsi_timeout(struct request *req,
1997 bool reserved)
1998{
1999 if (reserved)
2000 return BLK_EH_RESET_TIMER;
2001 return scsi_times_out(req);
2002}
2003
Christoph Hellwigd6296d32017-05-01 10:19:08 -06002004static int scsi_init_request(struct blk_mq_tag_set *set, struct request *rq,
2005 unsigned int hctx_idx, unsigned int numa_node)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002006{
Christoph Hellwigd6296d32017-05-01 10:19:08 -06002007 struct Scsi_Host *shost = set->driver_data;
Bart Van Assche8e688252017-06-02 14:21:52 -07002008 const bool unchecked_isa_dma = shost->unchecked_isa_dma;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002009 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
Bart Van Assche08f78432017-06-02 14:22:00 -07002010 struct scatterlist *sg;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002011
Bart Van Assche8e688252017-06-02 14:21:52 -07002012 if (unchecked_isa_dma)
2013 cmd->flags |= SCMD_UNCHECKED_ISA_DMA;
2014 cmd->sense_buffer = scsi_alloc_sense_buffer(unchecked_isa_dma,
2015 GFP_KERNEL, numa_node);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002016 if (!cmd->sense_buffer)
2017 return -ENOMEM;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002018 cmd->req.sense = cmd->sense_buffer;
Bart Van Assche08f78432017-06-02 14:22:00 -07002019
2020 if (scsi_host_get_prot(shost)) {
2021 sg = (void *)cmd + sizeof(struct scsi_cmnd) +
2022 shost->hostt->cmd_size;
2023 cmd->prot_sdb = (void *)sg + scsi_mq_sgl_size(shost);
2024 }
2025
Christoph Hellwigd2852032014-01-17 12:06:53 +01002026 return 0;
2027}
2028
Christoph Hellwigd6296d32017-05-01 10:19:08 -06002029static void scsi_exit_request(struct blk_mq_tag_set *set, struct request *rq,
2030 unsigned int hctx_idx)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002031{
2032 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2033
Bart Van Assche8e688252017-06-02 14:21:52 -07002034 scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA,
2035 cmd->sense_buffer);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002036}
2037
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002038static int scsi_map_queues(struct blk_mq_tag_set *set)
2039{
2040 struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
2041
2042 if (shost->hostt->map_queues)
2043 return shost->hostt->map_queues(shost);
2044 return blk_mq_map_queues(set);
2045}
2046
Christoph Hellwigf1bea552014-04-15 12:26:54 +02002047static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 struct device *host_dev;
2050 u64 bounce_limit = 0xffffffff;
2051
2052 if (shost->unchecked_isa_dma)
2053 return BLK_BOUNCE_ISA;
2054 /*
2055 * Platforms with virtual-DMA translation
2056 * hardware have no practical limit.
2057 */
2058 if (!PCI_DMA_BUS_IS_PHYS)
2059 return BLK_BOUNCE_ANY;
2060
2061 host_dev = scsi_get_device(shost);
2062 if (host_dev && host_dev->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +00002063 bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 return bounce_limit;
2066}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Christoph Hellwigd48777a62017-01-02 21:52:10 +03002068void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Lin Ming6f381fa2012-04-12 13:50:38 +08002070 struct device *dev = shost->dma_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Bart Van Assche9efc1602017-05-31 14:43:46 -07002072 queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
2073
Jens Axboea8474ce2007-08-07 09:02:51 +02002074 /*
2075 * this limit is imposed by hardware restrictions
2076 */
Martin K. Petersen8a783622010-02-26 00:20:39 -05002077 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
Ming Lin65e86172016-04-04 14:48:10 -07002078 SG_MAX_SEGMENTS));
Jens Axboea8474ce2007-08-07 09:02:51 +02002079
Martin K. Petersen13f05c82010-09-10 20:50:10 +02002080 if (scsi_host_prot_dma(shost)) {
2081 shost->sg_prot_tablesize =
2082 min_not_zero(shost->sg_prot_tablesize,
2083 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
2084 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
2085 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
2086 }
2087
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002088 blk_queue_max_hw_sectors(q, shost->max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
2090 blk_queue_segment_boundary(q, shost->dma_boundary);
FUJITA Tomonori99c84db2008-02-04 22:28:17 -08002091 dma_set_seg_boundary(dev, shost->dma_boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
FUJITA Tomonori860ac562008-02-04 22:28:05 -08002093 blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (!shost->use_clustering)
Martin K. Petersene692cb62010-12-01 19:41:49 +01002096 q->limits.cluster = 0;
James Bottomley465ff312008-01-01 10:00:10 -06002097
2098 /*
2099 * set a reasonable default alignment on word boundaries: the
2100 * host and device may alter it using
2101 * blk_queue_update_dma_alignment() later.
2102 */
2103 blk_queue_dma_alignment(q, 0x03);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002104}
Christoph Hellwigd48777a62017-01-02 21:52:10 +03002105EXPORT_SYMBOL_GPL(__scsi_init_queue);
James Bottomley465ff312008-01-01 10:00:10 -06002106
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002107static int scsi_init_rq(struct request_queue *q, struct request *rq, gfp_t gfp)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002108{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002109 struct Scsi_Host *shost = q->rq_alloc_data;
Bart Van Assche8e688252017-06-02 14:21:52 -07002110 const bool unchecked_isa_dma = shost->unchecked_isa_dma;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002111 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002112
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002113 memset(cmd, 0, sizeof(*cmd));
2114
Bart Van Assche8e688252017-06-02 14:21:52 -07002115 if (unchecked_isa_dma)
2116 cmd->flags |= SCMD_UNCHECKED_ISA_DMA;
2117 cmd->sense_buffer = scsi_alloc_sense_buffer(unchecked_isa_dma, gfp,
2118 NUMA_NO_NODE);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002119 if (!cmd->sense_buffer)
2120 goto fail;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002121 cmd->req.sense = cmd->sense_buffer;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002122
2123 if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
2124 cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp);
2125 if (!cmd->prot_sdb)
2126 goto fail_free_sense;
2127 }
2128
2129 return 0;
2130
2131fail_free_sense:
Bart Van Assche8e688252017-06-02 14:21:52 -07002132 scsi_free_sense_buffer(unchecked_isa_dma, cmd->sense_buffer);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002133fail:
2134 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002136
2137static void scsi_exit_rq(struct request_queue *q, struct request *rq)
2138{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002139 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2140
2141 if (cmd->prot_sdb)
2142 kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
Bart Van Assche8e688252017-06-02 14:21:52 -07002143 scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA,
2144 cmd->sense_buffer);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002145}
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002146
2147struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
2148{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002149 struct Scsi_Host *shost = sdev->host;
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002150 struct request_queue *q;
2151
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002152 q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002153 if (!q)
2154 return NULL;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002155 q->cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
2156 q->rq_alloc_data = shost;
2157 q->request_fn = scsi_request_fn;
2158 q->init_rq_fn = scsi_init_rq;
2159 q->exit_rq_fn = scsi_exit_rq;
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07002160 q->initialize_rq_fn = scsi_initialize_rq;
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002161
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002162 if (blk_init_allocated_queue(q) < 0) {
2163 blk_cleanup_queue(q);
2164 return NULL;
2165 }
2166
2167 __scsi_init_queue(shost, q);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002168 blk_queue_prep_rq(q, scsi_prep_fn);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02002169 blk_queue_unprep_rq(q, scsi_unprep_fn);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002170 blk_queue_softirq_done(q, scsi_softirq_done);
Jens Axboe242f9dc2008-09-14 05:55:09 -07002171 blk_queue_rq_timed_out(q, scsi_times_out);
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04002172 blk_queue_lld_busy(q, scsi_lld_busy);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002173 return q;
2174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Eric Biggersf363b082017-03-30 13:39:16 -07002176static const struct blk_mq_ops scsi_mq_ops = {
Christoph Hellwigd2852032014-01-17 12:06:53 +01002177 .queue_rq = scsi_queue_rq,
2178 .complete = scsi_softirq_done,
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002179 .timeout = scsi_timeout,
Bart Van Assche0eebd002017-04-26 13:47:57 -07002180#ifdef CONFIG_BLK_DEBUG_FS
2181 .show_rq = scsi_show_rq,
2182#endif
Christoph Hellwigd2852032014-01-17 12:06:53 +01002183 .init_request = scsi_init_request,
2184 .exit_request = scsi_exit_request,
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07002185 .initialize_rq_fn = scsi_initialize_rq,
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002186 .map_queues = scsi_map_queues,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002187};
2188
2189struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
2190{
2191 sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
2192 if (IS_ERR(sdev->request_queue))
2193 return NULL;
2194
2195 sdev->request_queue->queuedata = sdev;
2196 __scsi_init_queue(sdev->host, sdev->request_queue);
2197 return sdev->request_queue;
2198}
2199
2200int scsi_mq_setup_tags(struct Scsi_Host *shost)
2201{
Bart Van Asschebe4c1862017-06-02 14:21:59 -07002202 unsigned int cmd_size, sgl_size;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002203
Bart Van Asschebe4c1862017-06-02 14:21:59 -07002204 sgl_size = scsi_mq_sgl_size(shost);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002205 cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
2206 if (scsi_host_get_prot(shost))
2207 cmd_size += sizeof(struct scsi_data_buffer) + sgl_size;
2208
2209 memset(&shost->tag_set, 0, sizeof(shost->tag_set));
2210 shost->tag_set.ops = &scsi_mq_ops;
Bart Van Asscheefec4b92014-10-30 14:45:36 +01002211 shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002212 shost->tag_set.queue_depth = shost->can_queue;
2213 shost->tag_set.cmd_size = cmd_size;
2214 shost->tag_set.numa_node = NUMA_NO_NODE;
2215 shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Shaohua Li24391c02015-01-23 14:18:00 -07002216 shost->tag_set.flags |=
2217 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002218 shost->tag_set.driver_data = shost;
2219
2220 return blk_mq_alloc_tag_set(&shost->tag_set);
2221}
2222
2223void scsi_mq_destroy_tags(struct Scsi_Host *shost)
2224{
2225 blk_mq_free_tag_set(&shost->tag_set);
2226}
2227
Hannes Reinecke857de6e2017-02-17 09:02:45 +01002228/**
2229 * scsi_device_from_queue - return sdev associated with a request_queue
2230 * @q: The request queue to return the sdev from
2231 *
2232 * Return the sdev associated with a request queue or NULL if the
2233 * request_queue does not reference a SCSI device.
2234 */
2235struct scsi_device *scsi_device_from_queue(struct request_queue *q)
2236{
2237 struct scsi_device *sdev = NULL;
2238
2239 if (q->mq_ops) {
2240 if (q->mq_ops == &scsi_mq_ops)
2241 sdev = q->queuedata;
2242 } else if (q->request_fn == scsi_request_fn)
2243 sdev = q->queuedata;
2244 if (!sdev || !get_device(&sdev->sdev_gendev))
2245 sdev = NULL;
2246
2247 return sdev;
2248}
2249EXPORT_SYMBOL_GPL(scsi_device_from_queue);
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251/*
2252 * Function: scsi_block_requests()
2253 *
2254 * Purpose: Utility function used by low-level drivers to prevent further
2255 * commands from being queued to the device.
2256 *
2257 * Arguments: shost - Host in question
2258 *
2259 * Returns: Nothing
2260 *
2261 * Lock status: No locks are assumed held.
2262 *
2263 * Notes: There is no timer nor any other means by which the requests
2264 * get unblocked other than the low-level driver calling
2265 * scsi_unblock_requests().
2266 */
2267void scsi_block_requests(struct Scsi_Host *shost)
2268{
2269 shost->host_self_blocked = 1;
2270}
2271EXPORT_SYMBOL(scsi_block_requests);
2272
2273/*
2274 * Function: scsi_unblock_requests()
2275 *
2276 * Purpose: Utility function used by low-level drivers to allow further
2277 * commands from being queued to the device.
2278 *
2279 * Arguments: shost - Host in question
2280 *
2281 * Returns: Nothing
2282 *
2283 * Lock status: No locks are assumed held.
2284 *
2285 * Notes: There is no timer nor any other means by which the requests
2286 * get unblocked other than the low-level driver calling
2287 * scsi_unblock_requests().
2288 *
2289 * This is done as an API function so that changes to the
2290 * internals of the scsi mid-layer won't require wholesale
2291 * changes to drivers that use this feature.
2292 */
2293void scsi_unblock_requests(struct Scsi_Host *shost)
2294{
2295 shost->host_self_blocked = 0;
2296 scsi_run_host_queues(shost);
2297}
2298EXPORT_SYMBOL(scsi_unblock_requests);
2299
2300int __init scsi_init_queue(void)
2301{
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002302 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
2303 sizeof(struct scsi_data_buffer),
2304 0, 0, NULL);
2305 if (!scsi_sdb_cache) {
2306 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
FUJITA Tomonorif0787272008-12-14 01:23:45 +09002307 return -ENOMEM;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02002308 }
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return 0;
2311}
2312
2313void scsi_exit_queue(void)
2314{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002315 kmem_cache_destroy(scsi_sense_cache);
2316 kmem_cache_destroy(scsi_sense_isadma_cache);
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002317 kmem_cache_destroy(scsi_sdb_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
James Bottomley5baba832006-03-18 14:10:35 -06002319
2320/**
2321 * scsi_mode_select - issue a mode select
2322 * @sdev: SCSI device to be queried
2323 * @pf: Page format bit (1 == standard, 0 == vendor specific)
2324 * @sp: Save page bit (0 == don't save, 1 == save)
2325 * @modepage: mode page being requested
2326 * @buffer: request buffer (may not be smaller than eight bytes)
2327 * @len: length of request buffer.
2328 * @timeout: command timeout
2329 * @retries: number of retries before failing
2330 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002331 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley5baba832006-03-18 14:10:35 -06002332 * must be SCSI_SENSE_BUFFERSIZE big.
2333 *
2334 * Returns zero if successful; negative error number or scsi
2335 * status on error
2336 *
2337 */
2338int
2339scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
2340 unsigned char *buffer, int len, int timeout, int retries,
2341 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2342{
2343 unsigned char cmd[10];
2344 unsigned char *real_buffer;
2345 int ret;
2346
2347 memset(cmd, 0, sizeof(cmd));
2348 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2349
2350 if (sdev->use_10_for_ms) {
2351 if (len > 65535)
2352 return -EINVAL;
2353 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2354 if (!real_buffer)
2355 return -ENOMEM;
2356 memcpy(real_buffer + 8, buffer, len);
2357 len += 8;
2358 real_buffer[0] = 0;
2359 real_buffer[1] = 0;
2360 real_buffer[2] = data->medium_type;
2361 real_buffer[3] = data->device_specific;
2362 real_buffer[4] = data->longlba ? 0x01 : 0;
2363 real_buffer[5] = 0;
2364 real_buffer[6] = data->block_descriptor_length >> 8;
2365 real_buffer[7] = data->block_descriptor_length;
2366
2367 cmd[0] = MODE_SELECT_10;
2368 cmd[7] = len >> 8;
2369 cmd[8] = len;
2370 } else {
2371 if (len > 255 || data->block_descriptor_length > 255 ||
2372 data->longlba)
2373 return -EINVAL;
2374
2375 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2376 if (!real_buffer)
2377 return -ENOMEM;
2378 memcpy(real_buffer + 4, buffer, len);
2379 len += 4;
2380 real_buffer[0] = 0;
2381 real_buffer[1] = data->medium_type;
2382 real_buffer[2] = data->device_specific;
2383 real_buffer[3] = data->block_descriptor_length;
2384
2385
2386 cmd[0] = MODE_SELECT;
2387 cmd[4] = len;
2388 }
2389
2390 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002391 sshdr, timeout, retries, NULL);
James Bottomley5baba832006-03-18 14:10:35 -06002392 kfree(real_buffer);
2393 return ret;
2394}
2395EXPORT_SYMBOL_GPL(scsi_mode_select);
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397/**
Rob Landleyeb448202007-11-03 13:30:39 -05002398 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
James Bottomley1cf72692005-08-28 11:27:01 -05002399 * @sdev: SCSI device to be queried
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 * @dbd: set if mode sense will allow block descriptors to be returned
2401 * @modepage: mode page being requested
2402 * @buffer: request buffer (may not be smaller than eight bytes)
2403 * @len: length of request buffer.
2404 * @timeout: command timeout
2405 * @retries: number of retries before failing
2406 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002407 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley1cf72692005-08-28 11:27:01 -05002408 * must be SCSI_SENSE_BUFFERSIZE big.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 *
2410 * Returns zero if unsuccessful, or the header offset (either 4
2411 * or 8 depending on whether a six or ten byte command was
2412 * issued) if successful.
Rob Landleyeb448202007-11-03 13:30:39 -05002413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414int
James Bottomley1cf72692005-08-28 11:27:01 -05002415scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 unsigned char *buffer, int len, int timeout, int retries,
James Bottomley5baba832006-03-18 14:10:35 -06002417 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2418{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 unsigned char cmd[12];
2420 int use_10_for_ms;
2421 int header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002422 int result, retry_count = retries;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002423 struct scsi_sense_hdr my_sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 memset(data, 0, sizeof(*data));
2426 memset(&cmd[0], 0, 12);
2427 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
2428 cmd[2] = modepage;
2429
James Bottomleyea73a9f2005-08-28 11:33:52 -05002430 /* caller might not be interested in sense, but we need it */
2431 if (!sshdr)
2432 sshdr = &my_sshdr;
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 retry:
James Bottomley1cf72692005-08-28 11:27:01 -05002435 use_10_for_ms = sdev->use_10_for_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 if (use_10_for_ms) {
2438 if (len < 8)
2439 len = 8;
2440
2441 cmd[0] = MODE_SENSE_10;
2442 cmd[8] = len;
2443 header_length = 8;
2444 } else {
2445 if (len < 4)
2446 len = 4;
2447
2448 cmd[0] = MODE_SENSE;
2449 cmd[4] = len;
2450 header_length = 4;
2451 }
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 memset(buffer, 0, len);
2454
James Bottomley1cf72692005-08-28 11:27:01 -05002455 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002456 sshdr, timeout, retries, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 /* This code looks awful: what it's doing is making sure an
2459 * ILLEGAL REQUEST sense return identifies the actual command
2460 * byte as the problem. MODE_SENSE commands can return
2461 * ILLEGAL REQUEST if the code page isn't supported */
2462
James Bottomley1cf72692005-08-28 11:27:01 -05002463 if (use_10_for_ms && !scsi_status_is_good(result) &&
2464 (driver_byte(result) & DRIVER_SENSE)) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05002465 if (scsi_sense_valid(sshdr)) {
2466 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2467 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 /*
2469 * Invalid command operation code
2470 */
James Bottomley1cf72692005-08-28 11:27:01 -05002471 sdev->use_10_for_ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 goto retry;
2473 }
2474 }
2475 }
2476
James Bottomley1cf72692005-08-28 11:27:01 -05002477 if(scsi_status_is_good(result)) {
Al Viro6d73c852006-02-23 02:03:16 +01002478 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2479 (modepage == 6 || modepage == 8))) {
2480 /* Initio breakage? */
2481 header_length = 0;
2482 data->length = 13;
2483 data->medium_type = 0;
2484 data->device_specific = 0;
2485 data->longlba = 0;
2486 data->block_descriptor_length = 0;
2487 } else if(use_10_for_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 data->length = buffer[0]*256 + buffer[1] + 2;
2489 data->medium_type = buffer[2];
2490 data->device_specific = buffer[3];
2491 data->longlba = buffer[4] & 0x01;
2492 data->block_descriptor_length = buffer[6]*256
2493 + buffer[7];
2494 } else {
2495 data->length = buffer[0] + 1;
2496 data->medium_type = buffer[1];
2497 data->device_specific = buffer[2];
2498 data->block_descriptor_length = buffer[3];
2499 }
Al Viro6d73c852006-02-23 02:03:16 +01002500 data->header_length = header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002501 } else if ((status_byte(result) == CHECK_CONDITION) &&
2502 scsi_sense_valid(sshdr) &&
2503 sshdr->sense_key == UNIT_ATTENTION && retry_count) {
2504 retry_count--;
2505 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507
James Bottomley1cf72692005-08-28 11:27:01 -05002508 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510EXPORT_SYMBOL(scsi_mode_sense);
2511
James Bottomley001aac22007-12-02 19:10:40 +02002512/**
2513 * scsi_test_unit_ready - test if unit is ready
2514 * @sdev: scsi device to change the state of.
2515 * @timeout: command timeout
2516 * @retries: number of retries before failing
Christoph Hellwig74a78eb2017-02-14 20:15:57 +01002517 * @sshdr: outpout pointer for decoded sense information.
James Bottomley001aac22007-12-02 19:10:40 +02002518 *
2519 * Returns zero if unsuccessful or an error if TUR failed. For
Tejun Heo9f8a2c22010-12-08 20:57:40 +01002520 * removable media, UNIT_ATTENTION sets ->changed flag.
James Bottomley001aac22007-12-02 19:10:40 +02002521 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522int
James Bottomley001aac22007-12-02 19:10:40 +02002523scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
Christoph Hellwig74a78eb2017-02-14 20:15:57 +01002524 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 char cmd[] = {
2527 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2528 };
2529 int result;
James Bottomley001aac22007-12-02 19:10:40 +02002530
James Bottomley001aac22007-12-02 19:10:40 +02002531 /* try to eat the UNIT_ATTENTION if there are enough retries */
2532 do {
2533 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002534 timeout, retries, NULL);
James Bottomley32c356d2008-08-20 00:58:13 +02002535 if (sdev->removable && scsi_sense_valid(sshdr) &&
2536 sshdr->sense_key == UNIT_ATTENTION)
2537 sdev->changed = 1;
2538 } while (scsi_sense_valid(sshdr) &&
2539 sshdr->sense_key == UNIT_ATTENTION && --retries);
James Bottomley001aac22007-12-02 19:10:40 +02002540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 return result;
2542}
2543EXPORT_SYMBOL(scsi_test_unit_ready);
2544
2545/**
Rob Landleyeb448202007-11-03 13:30:39 -05002546 * scsi_device_set_state - Take the given device through the device state model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 * @sdev: scsi device to change the state of.
2548 * @state: state to change to.
2549 *
2550 * Returns zero if unsuccessful or an error if the requested
2551 * transition is illegal.
Rob Landleyeb448202007-11-03 13:30:39 -05002552 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553int
2554scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2555{
2556 enum scsi_device_state oldstate = sdev->sdev_state;
2557
2558 if (state == oldstate)
2559 return 0;
2560
2561 switch (state) {
2562 case SDEV_CREATED:
James Bottomley6f4267e2008-08-22 16:53:31 -05002563 switch (oldstate) {
2564 case SDEV_CREATED_BLOCK:
2565 break;
2566 default:
2567 goto illegal;
2568 }
2569 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571 case SDEV_RUNNING:
2572 switch (oldstate) {
2573 case SDEV_CREATED:
2574 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002575 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 case SDEV_QUIESCE:
2577 case SDEV_BLOCK:
2578 break;
2579 default:
2580 goto illegal;
2581 }
2582 break;
2583
2584 case SDEV_QUIESCE:
2585 switch (oldstate) {
2586 case SDEV_RUNNING:
2587 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002588 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 break;
2590 default:
2591 goto illegal;
2592 }
2593 break;
2594
2595 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002596 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 switch (oldstate) {
2598 case SDEV_CREATED:
2599 case SDEV_RUNNING:
2600 case SDEV_QUIESCE:
2601 case SDEV_BLOCK:
2602 break;
2603 default:
2604 goto illegal;
2605 }
2606 break;
2607
2608 case SDEV_BLOCK:
2609 switch (oldstate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 case SDEV_RUNNING:
James Bottomley6f4267e2008-08-22 16:53:31 -05002611 case SDEV_CREATED_BLOCK:
2612 break;
2613 default:
2614 goto illegal;
2615 }
2616 break;
2617
2618 case SDEV_CREATED_BLOCK:
2619 switch (oldstate) {
2620 case SDEV_CREATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 break;
2622 default:
2623 goto illegal;
2624 }
2625 break;
2626
2627 case SDEV_CANCEL:
2628 switch (oldstate) {
2629 case SDEV_CREATED:
2630 case SDEV_RUNNING:
Alan Stern9ea72902006-06-23 14:25:34 -04002631 case SDEV_QUIESCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002633 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 break;
2635 default:
2636 goto illegal;
2637 }
2638 break;
2639
2640 case SDEV_DEL:
2641 switch (oldstate) {
Brian King309bd272006-06-27 11:10:43 -05002642 case SDEV_CREATED:
2643 case SDEV_RUNNING:
2644 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002645 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 case SDEV_CANCEL:
Bart Van Assche255ee932017-06-02 14:21:57 -07002647 case SDEV_BLOCK:
Bart Van Assche0516c082013-07-02 15:06:33 +02002648 case SDEV_CREATED_BLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 break;
2650 default:
2651 goto illegal;
2652 }
2653 break;
2654
2655 }
2656 sdev->sdev_state = state;
2657 return 0;
2658
2659 illegal:
Hannes Reinecke91921e02014-06-25 16:39:59 +02002660 SCSI_LOG_ERROR_RECOVERY(1,
James Bottomley9ccfc752005-10-02 11:45:08 -05002661 sdev_printk(KERN_ERR, sdev,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002662 "Illegal state transition %s->%s",
James Bottomley9ccfc752005-10-02 11:45:08 -05002663 scsi_device_state_name(oldstate),
2664 scsi_device_state_name(state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 );
2666 return -EINVAL;
2667}
2668EXPORT_SYMBOL(scsi_device_set_state);
2669
2670/**
Jeff Garzika341cd02007-10-29 17:15:22 -04002671 * sdev_evt_emit - emit a single SCSI device uevent
2672 * @sdev: associated SCSI device
2673 * @evt: event to emit
2674 *
2675 * Send a single uevent (scsi_event) to the associated scsi_device.
2676 */
2677static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2678{
2679 int idx = 0;
2680 char *envp[3];
2681
2682 switch (evt->evt_type) {
2683 case SDEV_EVT_MEDIA_CHANGE:
2684 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2685 break;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002686 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
Hannes Reinecked3d32892016-02-19 09:17:16 +01002687 scsi_rescan_device(&sdev->sdev_gendev);
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002688 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2689 break;
2690 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2691 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2692 break;
2693 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2694 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2695 break;
2696 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2697 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2698 break;
2699 case SDEV_EVT_LUN_CHANGE_REPORTED:
2700 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2701 break;
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002702 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2703 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
2704 break;
Jeff Garzika341cd02007-10-29 17:15:22 -04002705 default:
2706 /* do nothing */
2707 break;
2708 }
2709
2710 envp[idx++] = NULL;
2711
2712 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2713}
2714
2715/**
2716 * sdev_evt_thread - send a uevent for each scsi event
2717 * @work: work struct for scsi_device
2718 *
2719 * Dispatch queued events to their associated scsi_device kobjects
2720 * as uevents.
2721 */
2722void scsi_evt_thread(struct work_struct *work)
2723{
2724 struct scsi_device *sdev;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002725 enum scsi_device_event evt_type;
Jeff Garzika341cd02007-10-29 17:15:22 -04002726 LIST_HEAD(event_list);
2727
2728 sdev = container_of(work, struct scsi_device, event_work);
2729
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002730 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2731 if (test_and_clear_bit(evt_type, sdev->pending_events))
2732 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2733
Jeff Garzika341cd02007-10-29 17:15:22 -04002734 while (1) {
2735 struct scsi_event *evt;
2736 struct list_head *this, *tmp;
2737 unsigned long flags;
2738
2739 spin_lock_irqsave(&sdev->list_lock, flags);
2740 list_splice_init(&sdev->event_list, &event_list);
2741 spin_unlock_irqrestore(&sdev->list_lock, flags);
2742
2743 if (list_empty(&event_list))
2744 break;
2745
2746 list_for_each_safe(this, tmp, &event_list) {
2747 evt = list_entry(this, struct scsi_event, node);
2748 list_del(&evt->node);
2749 scsi_evt_emit(sdev, evt);
2750 kfree(evt);
2751 }
2752 }
2753}
2754
2755/**
2756 * sdev_evt_send - send asserted event to uevent thread
2757 * @sdev: scsi_device event occurred on
2758 * @evt: event to send
2759 *
2760 * Assert scsi device event asynchronously.
2761 */
2762void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2763{
2764 unsigned long flags;
2765
Kay Sievers4d1566e2008-03-19 13:04:47 +01002766#if 0
2767 /* FIXME: currently this check eliminates all media change events
2768 * for polled devices. Need to update to discriminate between AN
2769 * and polled events */
Jeff Garzika341cd02007-10-29 17:15:22 -04002770 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2771 kfree(evt);
2772 return;
2773 }
Kay Sievers4d1566e2008-03-19 13:04:47 +01002774#endif
Jeff Garzika341cd02007-10-29 17:15:22 -04002775
2776 spin_lock_irqsave(&sdev->list_lock, flags);
2777 list_add_tail(&evt->node, &sdev->event_list);
2778 schedule_work(&sdev->event_work);
2779 spin_unlock_irqrestore(&sdev->list_lock, flags);
2780}
2781EXPORT_SYMBOL_GPL(sdev_evt_send);
2782
2783/**
2784 * sdev_evt_alloc - allocate a new scsi event
2785 * @evt_type: type of event to allocate
2786 * @gfpflags: GFP flags for allocation
2787 *
2788 * Allocates and returns a new scsi_event.
2789 */
2790struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2791 gfp_t gfpflags)
2792{
2793 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2794 if (!evt)
2795 return NULL;
2796
2797 evt->evt_type = evt_type;
2798 INIT_LIST_HEAD(&evt->node);
2799
2800 /* evt_type-specific initialization, if any */
2801 switch (evt_type) {
2802 case SDEV_EVT_MEDIA_CHANGE:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002803 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2804 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2805 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2806 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2807 case SDEV_EVT_LUN_CHANGE_REPORTED:
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002808 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
Jeff Garzika341cd02007-10-29 17:15:22 -04002809 default:
2810 /* do nothing */
2811 break;
2812 }
2813
2814 return evt;
2815}
2816EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2817
2818/**
2819 * sdev_evt_send_simple - send asserted event to uevent thread
2820 * @sdev: scsi_device event occurred on
2821 * @evt_type: type of event to send
2822 * @gfpflags: GFP flags for allocation
2823 *
2824 * Assert scsi device event asynchronously, given an event type.
2825 */
2826void sdev_evt_send_simple(struct scsi_device *sdev,
2827 enum scsi_device_event evt_type, gfp_t gfpflags)
2828{
2829 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2830 if (!evt) {
2831 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2832 evt_type);
2833 return;
2834 }
2835
2836 sdev_evt_send(sdev, evt);
2837}
2838EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2839
2840/**
Bart Van Assche669f0442016-11-22 16:17:13 -08002841 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
2842 * @sdev: SCSI device to count the number of scsi_request_fn() callers for.
2843 */
2844static int scsi_request_fn_active(struct scsi_device *sdev)
2845{
2846 struct request_queue *q = sdev->request_queue;
2847 int request_fn_active;
2848
2849 WARN_ON_ONCE(sdev->host->use_blk_mq);
2850
2851 spin_lock_irq(q->queue_lock);
2852 request_fn_active = q->request_fn_active;
2853 spin_unlock_irq(q->queue_lock);
2854
2855 return request_fn_active;
2856}
2857
2858/**
2859 * scsi_wait_for_queuecommand() - wait for ongoing queuecommand() calls
2860 * @sdev: SCSI device pointer.
2861 *
2862 * Wait until the ongoing shost->hostt->queuecommand() calls that are
2863 * invoked from scsi_request_fn() have finished.
2864 */
2865static void scsi_wait_for_queuecommand(struct scsi_device *sdev)
2866{
2867 WARN_ON_ONCE(sdev->host->use_blk_mq);
2868
2869 while (scsi_request_fn_active(sdev))
2870 msleep(20);
2871}
2872
2873/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 * scsi_device_quiesce - Block user issued commands.
2875 * @sdev: scsi device to quiesce.
2876 *
2877 * This works by trying to transition to the SDEV_QUIESCE state
2878 * (which must be a legal transition). When the device is in this
2879 * state, only special requests will be accepted, all others will
2880 * be deferred. Since special requests may also be requeued requests,
2881 * a successful return doesn't guarantee the device will be
2882 * totally quiescent.
2883 *
2884 * Must be called with user context, may sleep.
2885 *
2886 * Returns zero if unsuccessful or an error if not.
Rob Landleyeb448202007-11-03 13:30:39 -05002887 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888int
2889scsi_device_quiesce(struct scsi_device *sdev)
2890{
Bart Van Assche0db6ca82017-06-02 14:21:55 -07002891 int err;
2892
2893 mutex_lock(&sdev->state_mutex);
2894 err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2895 mutex_unlock(&sdev->state_mutex);
2896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (err)
2898 return err;
2899
2900 scsi_run_queue(sdev->request_queue);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02002901 while (atomic_read(&sdev->device_busy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 msleep_interruptible(200);
2903 scsi_run_queue(sdev->request_queue);
2904 }
2905 return 0;
2906}
2907EXPORT_SYMBOL(scsi_device_quiesce);
2908
2909/**
2910 * scsi_device_resume - Restart user issued commands to a quiesced device.
2911 * @sdev: scsi device to resume.
2912 *
2913 * Moves the device from quiesced back to running and restarts the
2914 * queues.
2915 *
2916 * Must be called with user context, may sleep.
Rob Landleyeb448202007-11-03 13:30:39 -05002917 */
Dan Williamsa7a20d12012-03-22 17:05:11 -07002918void scsi_device_resume(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Dan Williamsa7a20d12012-03-22 17:05:11 -07002920 /* check if the device state was mutated prior to resume, and if
2921 * so assume the state is being managed elsewhere (for example
2922 * device deleted during suspend)
2923 */
Bart Van Assche0db6ca82017-06-02 14:21:55 -07002924 mutex_lock(&sdev->state_mutex);
2925 if (sdev->sdev_state == SDEV_QUIESCE &&
2926 scsi_device_set_state(sdev, SDEV_RUNNING) == 0)
2927 scsi_run_queue(sdev->request_queue);
2928 mutex_unlock(&sdev->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929}
2930EXPORT_SYMBOL(scsi_device_resume);
2931
2932static void
2933device_quiesce_fn(struct scsi_device *sdev, void *data)
2934{
2935 scsi_device_quiesce(sdev);
2936}
2937
2938void
2939scsi_target_quiesce(struct scsi_target *starget)
2940{
2941 starget_for_each_device(starget, NULL, device_quiesce_fn);
2942}
2943EXPORT_SYMBOL(scsi_target_quiesce);
2944
2945static void
2946device_resume_fn(struct scsi_device *sdev, void *data)
2947{
2948 scsi_device_resume(sdev);
2949}
2950
2951void
2952scsi_target_resume(struct scsi_target *starget)
2953{
2954 starget_for_each_device(starget, NULL, device_resume_fn);
2955}
2956EXPORT_SYMBOL(scsi_target_resume);
2957
2958/**
Bart Van Assche551eb592017-06-02 14:21:53 -07002959 * scsi_internal_device_block_nowait - try to transition to the SDEV_BLOCK state
2960 * @sdev: device to block
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 *
Bart Van Assche551eb592017-06-02 14:21:53 -07002962 * Pause SCSI command processing on the specified device. Does not sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 *
Bart Van Assche551eb592017-06-02 14:21:53 -07002964 * Returns zero if successful or a negative error code upon failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 *
Bart Van Assche551eb592017-06-02 14:21:53 -07002966 * Notes:
2967 * This routine transitions the device to the SDEV_BLOCK state (which must be
2968 * a legal transition). When the device is in this state, command processing
2969 * is paused until the device leaves the SDEV_BLOCK state. See also
2970 * scsi_internal_device_unblock_nowait().
Rob Landleyeb448202007-11-03 13:30:39 -05002971 */
Bart Van Assche551eb592017-06-02 14:21:53 -07002972int scsi_internal_device_block_nowait(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Jens Axboe165125e2007-07-24 09:28:11 +02002974 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 unsigned long flags;
2976 int err = 0;
2977
2978 err = scsi_device_set_state(sdev, SDEV_BLOCK);
James Bottomley6f4267e2008-08-22 16:53:31 -05002979 if (err) {
2980 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2981
2982 if (err)
2983 return err;
2984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 /*
2987 * The device has transitioned to SDEV_BLOCK. Stop the
2988 * block layer from calling the midlayer with this device's
2989 * request queue.
2990 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01002991 if (q->mq_ops) {
Linus Torvalds90311142017-07-06 12:10:33 -07002992 blk_mq_quiesce_queue_nowait(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002993 } else {
2994 spin_lock_irqsave(q->queue_lock, flags);
2995 blk_stop_queue(q);
2996 spin_unlock_irqrestore(q->queue_lock, flags);
2997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 return 0;
3000}
Bart Van Assche551eb592017-06-02 14:21:53 -07003001EXPORT_SYMBOL_GPL(scsi_internal_device_block_nowait);
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003/**
Bart Van Assche551eb592017-06-02 14:21:53 -07003004 * scsi_internal_device_block - try to transition to the SDEV_BLOCK state
3005 * @sdev: device to block
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 *
Bart Van Assche551eb592017-06-02 14:21:53 -07003007 * Pause SCSI command processing on the specified device and wait until all
3008 * ongoing scsi_request_fn() / scsi_queue_rq() calls have finished. May sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 *
Bart Van Assche551eb592017-06-02 14:21:53 -07003010 * Returns zero if successful or a negative error code upon failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 *
Bart Van Assche551eb592017-06-02 14:21:53 -07003012 * Note:
3013 * This routine transitions the device to the SDEV_BLOCK state (which must be
3014 * a legal transition). When the device is in this state, command processing
3015 * is paused until the device leaves the SDEV_BLOCK state. See also
3016 * scsi_internal_device_unblock().
3017 *
3018 * To do: avoid that scsi_send_eh_cmnd() calls queuecommand() after
3019 * scsi_internal_device_block() has blocked a SCSI device and also
3020 * remove the rport mutex lock and unlock calls from srp_queuecommand().
Rob Landleyeb448202007-11-03 13:30:39 -05003021 */
Bart Van Assche551eb592017-06-02 14:21:53 -07003022static int scsi_internal_device_block(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
Bart Van Assche551eb592017-06-02 14:21:53 -07003024 struct request_queue *q = sdev->request_queue;
3025 int err;
3026
Bart Van Assche0db6ca82017-06-02 14:21:55 -07003027 mutex_lock(&sdev->state_mutex);
Bart Van Assche551eb592017-06-02 14:21:53 -07003028 err = scsi_internal_device_block_nowait(sdev);
3029 if (err == 0) {
3030 if (q->mq_ops)
3031 blk_mq_quiesce_queue(q);
3032 else
3033 scsi_wait_for_queuecommand(sdev);
3034 }
Bart Van Assche0db6ca82017-06-02 14:21:55 -07003035 mutex_unlock(&sdev->state_mutex);
3036
Bart Van Assche551eb592017-06-02 14:21:53 -07003037 return err;
3038}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Bart Van Assche66483a42017-06-02 14:21:56 -07003040void scsi_start_queue(struct scsi_device *sdev)
3041{
3042 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 unsigned long flags;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003044
Bart Van Assche66483a42017-06-02 14:21:56 -07003045 if (q->mq_ops) {
Linus Torvalds90311142017-07-06 12:10:33 -07003046 blk_mq_unquiesce_queue(q);
Bart Van Assche66483a42017-06-02 14:21:56 -07003047 } else {
3048 spin_lock_irqsave(q->queue_lock, flags);
3049 blk_start_queue(q);
3050 spin_unlock_irqrestore(q->queue_lock, flags);
3051 }
3052}
3053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054/**
Bart Van Assche43f75712017-06-02 14:21:54 -07003055 * scsi_internal_device_unblock_nowait - resume a device after a block request
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 * @sdev: device to resume
Bart Van Assche43f75712017-06-02 14:21:54 -07003057 * @new_state: state to set the device to after unblocking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 *
Bart Van Assche43f75712017-06-02 14:21:54 -07003059 * Restart the device queue for a previously suspended SCSI device. Does not
3060 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 *
Bart Van Assche43f75712017-06-02 14:21:54 -07003062 * Returns zero if successful or a negative error code upon failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 *
Bart Van Assche43f75712017-06-02 14:21:54 -07003064 * Notes:
3065 * This routine transitions the device to the SDEV_RUNNING state or to one of
3066 * the offline states (which must be a legal transition) allowing the midlayer
3067 * to goose the queue for this device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 */
Bart Van Assche43f75712017-06-02 14:21:54 -07003069int scsi_internal_device_unblock_nowait(struct scsi_device *sdev,
3070 enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003072 /*
3073 * Try to transition the scsi device to SDEV_RUNNING or one of the
3074 * offlined states and goose the device queue if successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 */
Vikas Chaudhary0e580762012-08-09 04:51:30 -04003076 if ((sdev->sdev_state == SDEV_BLOCK) ||
3077 (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003078 sdev->sdev_state = new_state;
3079 else if (sdev->sdev_state == SDEV_CREATED_BLOCK) {
3080 if (new_state == SDEV_TRANSPORT_OFFLINE ||
3081 new_state == SDEV_OFFLINE)
3082 sdev->sdev_state = new_state;
3083 else
3084 sdev->sdev_state = SDEV_CREATED;
3085 } else if (sdev->sdev_state != SDEV_CANCEL &&
Mike Christie986fe6c2010-10-06 03:10:59 -05003086 sdev->sdev_state != SDEV_OFFLINE)
Takahiro Yasui5c10e632009-04-29 12:13:02 -04003087 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Bart Van Assche66483a42017-06-02 14:21:56 -07003089 scsi_start_queue(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 return 0;
3092}
Bart Van Assche43f75712017-06-02 14:21:54 -07003093EXPORT_SYMBOL_GPL(scsi_internal_device_unblock_nowait);
3094
3095/**
3096 * scsi_internal_device_unblock - resume a device after a block request
3097 * @sdev: device to resume
3098 * @new_state: state to set the device to after unblocking
3099 *
3100 * Restart the device queue for a previously suspended SCSI device. May sleep.
3101 *
3102 * Returns zero if successful or a negative error code upon failure.
3103 *
3104 * Notes:
3105 * This routine transitions the device to the SDEV_RUNNING state or to one of
3106 * the offline states (which must be a legal transition) allowing the midlayer
3107 * to goose the queue for this device.
3108 */
3109static int scsi_internal_device_unblock(struct scsi_device *sdev,
3110 enum scsi_device_state new_state)
3111{
Bart Van Assche0db6ca82017-06-02 14:21:55 -07003112 int ret;
3113
3114 mutex_lock(&sdev->state_mutex);
3115 ret = scsi_internal_device_unblock_nowait(sdev, new_state);
3116 mutex_unlock(&sdev->state_mutex);
3117
3118 return ret;
Bart Van Assche43f75712017-06-02 14:21:54 -07003119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121static void
3122device_block(struct scsi_device *sdev, void *data)
3123{
Bart Van Assche551eb592017-06-02 14:21:53 -07003124 scsi_internal_device_block(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
3127static int
3128target_block(struct device *dev, void *data)
3129{
3130 if (scsi_is_target_device(dev))
3131 starget_for_each_device(to_scsi_target(dev), NULL,
3132 device_block);
3133 return 0;
3134}
3135
3136void
3137scsi_target_block(struct device *dev)
3138{
3139 if (scsi_is_target_device(dev))
3140 starget_for_each_device(to_scsi_target(dev), NULL,
3141 device_block);
3142 else
3143 device_for_each_child(dev, NULL, target_block);
3144}
3145EXPORT_SYMBOL_GPL(scsi_target_block);
3146
3147static void
3148device_unblock(struct scsi_device *sdev, void *data)
3149{
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003150 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
3152
3153static int
3154target_unblock(struct device *dev, void *data)
3155{
3156 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003157 starget_for_each_device(to_scsi_target(dev), data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 device_unblock);
3159 return 0;
3160}
3161
3162void
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003163scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
3165 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003166 starget_for_each_device(to_scsi_target(dev), &new_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 device_unblock);
3168 else
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003169 device_for_each_child(dev, &new_state, target_unblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171EXPORT_SYMBOL_GPL(scsi_target_unblock);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003172
3173/**
3174 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
Rob Landleyeb448202007-11-03 13:30:39 -05003175 * @sgl: scatter-gather list
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003176 * @sg_count: number of segments in sg
3177 * @offset: offset in bytes into sg, on return offset into the mapped area
3178 * @len: bytes to map, on return number of bytes mapped
3179 *
3180 * Returns virtual address of the start of the mapped page
3181 */
Jens Axboec6132da2007-10-16 11:08:49 +02003182void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003183 size_t *offset, size_t *len)
3184{
3185 int i;
3186 size_t sg_len = 0, len_complete = 0;
Jens Axboec6132da2007-10-16 11:08:49 +02003187 struct scatterlist *sg;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003188 struct page *page;
3189
Andrew Morton22cfefb2007-02-05 16:39:03 -08003190 WARN_ON(!irqs_disabled());
3191
Jens Axboec6132da2007-10-16 11:08:49 +02003192 for_each_sg(sgl, sg, sg_count, i) {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003193 len_complete = sg_len; /* Complete sg-entries */
Jens Axboec6132da2007-10-16 11:08:49 +02003194 sg_len += sg->length;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003195 if (sg_len > *offset)
3196 break;
3197 }
3198
3199 if (unlikely(i == sg_count)) {
Andrew Morton169e1a22006-04-18 21:09:08 -07003200 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
3201 "elements %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003202 __func__, sg_len, *offset, sg_count);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003203 WARN_ON(1);
3204 return NULL;
3205 }
3206
3207 /* Offset starting from the beginning of first page in this sg-entry */
Jens Axboec6132da2007-10-16 11:08:49 +02003208 *offset = *offset - len_complete + sg->offset;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003209
3210 /* Assumption: contiguous pages can be accessed as "page + i" */
Jens Axboe45711f12007-10-22 21:19:53 +02003211 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003212 *offset &= ~PAGE_MASK;
3213
3214 /* Bytes in this sg-entry from *offset to the end of the page */
3215 sg_len = PAGE_SIZE - *offset;
3216 if (*len > sg_len)
3217 *len = sg_len;
3218
Cong Wang77dfce02011-11-25 23:14:23 +08003219 return kmap_atomic(page);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003220}
3221EXPORT_SYMBOL(scsi_kmap_atomic_sg);
3222
3223/**
Rob Landleyeb448202007-11-03 13:30:39 -05003224 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003225 * @virt: virtual address to be unmapped
3226 */
3227void scsi_kunmap_atomic_sg(void *virt)
3228{
Cong Wang77dfce02011-11-25 23:14:23 +08003229 kunmap_atomic(virt);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003230}
3231EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
Aaron Lu6f4c8272013-01-23 15:09:32 +08003232
3233void sdev_disable_disk_events(struct scsi_device *sdev)
3234{
3235 atomic_inc(&sdev->disk_events_disable_depth);
3236}
3237EXPORT_SYMBOL(sdev_disable_disk_events);
3238
3239void sdev_enable_disk_events(struct scsi_device *sdev)
3240{
3241 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
3242 return;
3243 atomic_dec(&sdev->disk_events_disable_depth);
3244}
3245EXPORT_SYMBOL(sdev_enable_disk_events);
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003246
3247/**
3248 * scsi_vpd_lun_id - return a unique device identification
3249 * @sdev: SCSI device
3250 * @id: buffer for the identification
3251 * @id_len: length of the buffer
3252 *
3253 * Copies a unique device identification into @id based
3254 * on the information in the VPD page 0x83 of the device.
3255 * The string will be formatted as a SCSI name string.
3256 *
3257 * Returns the length of the identification or error on failure.
3258 * If the identifier is longer than the supplied buffer the actual
3259 * identifier length is returned and the buffer is not zero-padded.
3260 */
3261int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
3262{
3263 u8 cur_id_type = 0xff;
3264 u8 cur_id_size = 0;
3265 unsigned char *d, *cur_id_str;
3266 unsigned char __rcu *vpd_pg83;
3267 int id_size = -EINVAL;
3268
3269 rcu_read_lock();
3270 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3271 if (!vpd_pg83) {
3272 rcu_read_unlock();
3273 return -ENXIO;
3274 }
3275
3276 /*
3277 * Look for the correct descriptor.
3278 * Order of preference for lun descriptor:
3279 * - SCSI name string
3280 * - NAA IEEE Registered Extended
3281 * - EUI-64 based 16-byte
3282 * - EUI-64 based 12-byte
3283 * - NAA IEEE Registered
3284 * - NAA IEEE Extended
Hannes Reinecked2308232016-05-09 09:14:29 +02003285 * - T10 Vendor ID
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003286 * as longer descriptors reduce the likelyhood
3287 * of identification clashes.
3288 */
3289
3290 /* The id string must be at least 20 bytes + terminating NULL byte */
3291 if (id_len < 21) {
3292 rcu_read_unlock();
3293 return -EINVAL;
3294 }
3295
3296 memset(id, 0, id_len);
3297 d = vpd_pg83 + 4;
3298 while (d < vpd_pg83 + sdev->vpd_pg83_len) {
3299 /* Skip designators not referring to the LUN */
3300 if ((d[1] & 0x30) != 0x00)
3301 goto next_desig;
3302
3303 switch (d[1] & 0xf) {
Hannes Reinecked2308232016-05-09 09:14:29 +02003304 case 0x1:
3305 /* T10 Vendor ID */
3306 if (cur_id_size > d[3])
3307 break;
3308 /* Prefer anything */
3309 if (cur_id_type > 0x01 && cur_id_type != 0xff)
3310 break;
3311 cur_id_size = d[3];
3312 if (cur_id_size + 4 > id_len)
3313 cur_id_size = id_len - 4;
3314 cur_id_str = d + 4;
3315 cur_id_type = d[1] & 0xf;
3316 id_size = snprintf(id, id_len, "t10.%*pE",
3317 cur_id_size, cur_id_str);
3318 break;
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003319 case 0x2:
3320 /* EUI-64 */
3321 if (cur_id_size > d[3])
3322 break;
3323 /* Prefer NAA IEEE Registered Extended */
3324 if (cur_id_type == 0x3 &&
3325 cur_id_size == d[3])
3326 break;
3327 cur_id_size = d[3];
3328 cur_id_str = d + 4;
3329 cur_id_type = d[1] & 0xf;
3330 switch (cur_id_size) {
3331 case 8:
3332 id_size = snprintf(id, id_len,
3333 "eui.%8phN",
3334 cur_id_str);
3335 break;
3336 case 12:
3337 id_size = snprintf(id, id_len,
3338 "eui.%12phN",
3339 cur_id_str);
3340 break;
3341 case 16:
3342 id_size = snprintf(id, id_len,
3343 "eui.%16phN",
3344 cur_id_str);
3345 break;
3346 default:
3347 cur_id_size = 0;
3348 break;
3349 }
3350 break;
3351 case 0x3:
3352 /* NAA */
3353 if (cur_id_size > d[3])
3354 break;
3355 cur_id_size = d[3];
3356 cur_id_str = d + 4;
3357 cur_id_type = d[1] & 0xf;
3358 switch (cur_id_size) {
3359 case 8:
3360 id_size = snprintf(id, id_len,
3361 "naa.%8phN",
3362 cur_id_str);
3363 break;
3364 case 16:
3365 id_size = snprintf(id, id_len,
3366 "naa.%16phN",
3367 cur_id_str);
3368 break;
3369 default:
3370 cur_id_size = 0;
3371 break;
3372 }
3373 break;
3374 case 0x8:
3375 /* SCSI name string */
3376 if (cur_id_size + 4 > d[3])
3377 break;
3378 /* Prefer others for truncated descriptor */
3379 if (cur_id_size && d[3] > id_len)
3380 break;
3381 cur_id_size = id_size = d[3];
3382 cur_id_str = d + 4;
3383 cur_id_type = d[1] & 0xf;
3384 if (cur_id_size >= id_len)
3385 cur_id_size = id_len - 1;
3386 memcpy(id, cur_id_str, cur_id_size);
3387 /* Decrease priority for truncated descriptor */
3388 if (cur_id_size != id_size)
3389 cur_id_size = 6;
3390 break;
3391 default:
3392 break;
3393 }
3394next_desig:
3395 d += d[3] + 4;
3396 }
3397 rcu_read_unlock();
3398
3399 return id_size;
3400}
3401EXPORT_SYMBOL(scsi_vpd_lun_id);
Hannes Reineckea8aa3972015-12-01 10:16:57 +01003402
3403/*
3404 * scsi_vpd_tpg_id - return a target port group identifier
3405 * @sdev: SCSI device
3406 *
3407 * Returns the Target Port Group identifier from the information
3408 * froom VPD page 0x83 of the device.
3409 *
3410 * Returns the identifier or error on failure.
3411 */
3412int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
3413{
3414 unsigned char *d;
3415 unsigned char __rcu *vpd_pg83;
3416 int group_id = -EAGAIN, rel_port = -1;
3417
3418 rcu_read_lock();
3419 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3420 if (!vpd_pg83) {
3421 rcu_read_unlock();
3422 return -ENXIO;
3423 }
3424
3425 d = sdev->vpd_pg83 + 4;
3426 while (d < sdev->vpd_pg83 + sdev->vpd_pg83_len) {
3427 switch (d[1] & 0xf) {
3428 case 0x4:
3429 /* Relative target port */
3430 rel_port = get_unaligned_be16(&d[6]);
3431 break;
3432 case 0x5:
3433 /* Target port group */
3434 group_id = get_unaligned_be16(&d[6]);
3435 break;
3436 default:
3437 break;
3438 }
3439 d += d[3] + 4;
3440 }
3441 rcu_read_unlock();
3442
3443 if (group_id >= 0 && rel_id && rel_port != -1)
3444 *rel_id = rel_port;
3445
3446 return group_id;
3447}
3448EXPORT_SYMBOL(scsi_vpd_tpg_id);