blob: b0cb3e0713f30875eeb613a9b5a137f2a3bd5e50 [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>
Christoph Hellwigee14c672015-08-27 14:16:59 +020033#include <scsi/scsi_dh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Christoph Hellwig3b5382c2014-05-06 12:25:40 +020035#include <trace/events/scsi.h>
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "scsi_priv.h"
38#include "scsi_logging.h"
39
Christoph Hellwige9c787e2017-01-02 21:55:26 +030040static struct kmem_cache *scsi_sdb_cache;
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030041static struct kmem_cache *scsi_sense_cache;
42static struct kmem_cache *scsi_sense_isadma_cache;
43static DEFINE_MUTEX(scsi_sense_cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030045static inline struct kmem_cache *
46scsi_select_sense_cache(struct Scsi_Host *shost)
47{
48 return shost->unchecked_isa_dma ?
49 scsi_sense_isadma_cache : scsi_sense_cache;
50}
51
Christoph Hellwige9c787e2017-01-02 21:55:26 +030052static void scsi_free_sense_buffer(struct Scsi_Host *shost,
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030053 unsigned char *sense_buffer)
54{
55 kmem_cache_free(scsi_select_sense_cache(shost), sense_buffer);
56}
57
Christoph Hellwige9c787e2017-01-02 21:55:26 +030058static unsigned char *scsi_alloc_sense_buffer(struct Scsi_Host *shost,
59 gfp_t gfp_mask, int numa_node)
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030060{
61 return kmem_cache_alloc_node(scsi_select_sense_cache(shost), gfp_mask,
62 numa_node);
63}
64
65int scsi_init_sense_cache(struct Scsi_Host *shost)
66{
67 struct kmem_cache *cache;
68 int ret = 0;
69
70 cache = scsi_select_sense_cache(shost);
71 if (cache)
72 return 0;
73
74 mutex_lock(&scsi_sense_cache_mutex);
75 if (shost->unchecked_isa_dma) {
76 scsi_sense_isadma_cache =
77 kmem_cache_create("scsi_sense_cache(DMA)",
78 SCSI_SENSE_BUFFERSIZE, 0,
79 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL);
80 if (!scsi_sense_isadma_cache)
81 ret = -ENOMEM;
82 } else {
83 scsi_sense_cache =
84 kmem_cache_create("scsi_sense_cache",
85 SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN, NULL);
86 if (!scsi_sense_cache)
87 ret = -ENOMEM;
88 }
89
90 mutex_unlock(&scsi_sense_cache_mutex);
91 return ret;
92}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +020093
Jens Axboea488e742010-04-16 21:13:15 +020094/*
95 * When to reinvoke queueing after a resource shortage. It's 3 msecs to
96 * not change behaviour from the previous unplug mechanism, experimentation
97 * may prove this needs changing.
98 */
99#define SCSI_QUEUE_DELAY 3
100
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100101static void
102scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct Scsi_Host *host = cmd->device->host;
105 struct scsi_device *device = cmd->device;
Mike Christief0c0a372008-08-17 15:24:38 -0500106 struct scsi_target *starget = scsi_target(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /*
Tejun Heo d8c37e72005-05-14 00:46:08 +0900109 * Set the appropriate busy bit for the device/host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
111 * If the host/device isn't busy, assume that something actually
112 * completed, and that we should be able to queue a command now.
113 *
114 * Note that the prior mid-layer assumption that any host could
115 * always queue at least one command is now broken. The mid-layer
116 * will implement a user specifiable stall (see
117 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
118 * if a command is requeued with no other commands outstanding
119 * either for the device or for the host.
120 */
Mike Christief0c0a372008-08-17 15:24:38 -0500121 switch (reason) {
122 case SCSI_MLQUEUE_HOST_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100123 atomic_set(&host->host_blocked, host->max_host_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500124 break;
125 case SCSI_MLQUEUE_DEVICE_BUSY:
James Smart573e5912011-07-06 12:45:17 -0400126 case SCSI_MLQUEUE_EH_RETRY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100127 atomic_set(&device->device_blocked,
128 device->max_device_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500129 break;
130 case SCSI_MLQUEUE_TARGET_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100131 atomic_set(&starget->target_blocked,
132 starget->max_target_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500133 break;
134 }
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100135}
136
Christoph Hellwigd2852032014-01-17 12:06:53 +0100137static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd)
138{
139 struct scsi_device *sdev = cmd->device;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100140
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700141 blk_mq_requeue_request(cmd->request, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100142 put_device(&sdev->sdev_gendev);
143}
144
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100145/**
146 * __scsi_queue_insert - private queue insertion
147 * @cmd: The SCSI command being requeued
148 * @reason: The reason for the requeue
149 * @unbusy: Whether the queue should be unbusied
150 *
151 * This is a private queue insertion. The public interface
152 * scsi_queue_insert() always assumes the queue should be unbusied
153 * because it's always called before the completion. This function is
154 * for a requeue after completion, which should only occur in this
155 * file.
156 */
157static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy)
158{
159 struct scsi_device *device = cmd->device;
160 struct request_queue *q = device->request_queue;
161 unsigned long flags;
162
163 SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
164 "Inserting command %p into mlqueue\n", cmd));
165
166 scsi_set_blocked(cmd, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * Decrement the counters, since these commands are no longer
170 * active on the host/device.
171 */
James Bottomley4f5299a2009-01-02 10:42:21 -0600172 if (unbusy)
173 scsi_device_unbusy(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /*
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500176 * Requeue this command. It will go before all other commands
Bart Van Asscheb4854622012-06-29 15:36:07 +0000177 * that are already in the queue. Schedule requeue work under
178 * lock such that the kblockd_schedule_work() call happens
179 * before blk_cleanup_queue() finishes.
Jens Axboea488e742010-04-16 21:13:15 +0200180 */
Alan Stern644373a2014-03-28 10:51:15 -0700181 cmd->result = 0;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100182 if (q->mq_ops) {
183 scsi_mq_requeue_cmd(cmd);
184 return;
185 }
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500186 spin_lock_irqsave(q->queue_lock, flags);
James Bottomley59897da2005-09-14 12:57:42 -0400187 blk_requeue_request(q, cmd->request);
Jens Axboe59c3d452014-04-08 09:15:35 -0600188 kblockd_schedule_work(&device->requeue_work);
Bart Van Asscheb4854622012-06-29 15:36:07 +0000189 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
James Bottomley4f5299a2009-01-02 10:42:21 -0600192/*
193 * Function: scsi_queue_insert()
194 *
195 * Purpose: Insert a command in the midlevel queue.
196 *
197 * Arguments: cmd - command that we are adding to queue.
198 * reason - why we are inserting command to queue.
199 *
200 * Lock status: Assumed that lock is not held upon entry.
201 *
202 * Returns: Nothing.
203 *
204 * Notes: We do this for one of two cases. Either the host is busy
205 * and it cannot accept any more commands for the time being,
206 * or the device returned QUEUE_FULL and can accept no more
207 * commands.
208 * Notes: This could be called either from an interrupt context or a
209 * normal process context.
210 */
Bart Van Assche84feb162012-06-29 15:35:05 +0000211void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
James Bottomley4f5299a2009-01-02 10:42:21 -0600212{
Bart Van Assche84feb162012-06-29 15:35:05 +0000213 __scsi_queue_insert(cmd, reason, 1);
James Bottomley4f5299a2009-01-02 10:42:21 -0600214}
Christoph Hellwige8064022016-10-20 15:12:13 +0200215
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100216
217/**
218 * scsi_execute - insert request and wait for the result
219 * @sdev: scsi device
220 * @cmd: scsi command
221 * @data_direction: data direction
222 * @buffer: data buffer
223 * @bufflen: len of buffer
224 * @sense: optional sense buffer
225 * @sshdr: optional decoded sense header
226 * @timeout: request timeout in seconds
227 * @retries: number of times to retry request
228 * @flags: flags for ->cmd_flags
229 * @rq_flags: flags for ->rq_flags
230 * @resid: optional residual length
231 *
232 * returns the req->errors value which is the scsi_cmnd result
233 * field.
234 */
235int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
James Bottomley33aa6872005-08-28 11:31:14 -0500236 int data_direction, void *buffer, unsigned bufflen,
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100237 unsigned char *sense, struct scsi_sense_hdr *sshdr,
238 int timeout, int retries, u64 flags, req_flags_t rq_flags,
239 int *resid)
James Bottomley39216032005-06-15 18:48:29 -0500240{
241 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100242 struct scsi_request *rq;
James Bottomley39216032005-06-15 18:48:29 -0500243 int ret = DRIVER_ERROR << 24;
244
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100245 req = blk_get_request(sdev->request_queue,
246 data_direction == DMA_TO_DEVICE ?
247 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, __GFP_RECLAIM);
Joe Lawrencea492f072014-08-28 08:15:21 -0600248 if (IS_ERR(req))
James Bottomleybfe159a2011-07-07 15:45:40 -0500249 return ret;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100250 rq = scsi_req(req);
251 scsi_req_init(req);
James Bottomley39216032005-06-15 18:48:29 -0500252
253 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
Mel Gorman71baba42015-11-06 16:28:28 -0800254 buffer, bufflen, __GFP_RECLAIM))
James Bottomley39216032005-06-15 18:48:29 -0500255 goto out;
256
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100257 rq->cmd_len = COMMAND_SIZE(cmd[0]);
258 memcpy(rq->cmd, cmd, rq->cmd_len);
Mike Christie17e01f22005-11-11 05:31:37 -0600259 req->retries = retries;
James Bottomley39216032005-06-15 18:48:29 -0500260 req->timeout = timeout;
Christoph Hellwige8064022016-10-20 15:12:13 +0200261 req->cmd_flags |= flags;
262 req->rq_flags |= rq_flags | RQF_QUIET | RQF_PREEMPT;
James Bottomley39216032005-06-15 18:48:29 -0500263
264 /*
265 * head injection *required* here otherwise quiesce won't work
266 */
267 blk_execute_rq(req->q, NULL, req, 1);
268
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400269 /*
270 * Some devices (USB mass-storage in particular) may transfer
271 * garbage data together with a residue indicating that the data
272 * is invalid. Prevent the garbage from being misinterpreted
273 * and prevent security leaks by zeroing out the excess data.
274 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100275 if (unlikely(rq->resid_len > 0 && rq->resid_len <= bufflen))
276 memset(buffer + (bufflen - rq->resid_len), 0, rq->resid_len);
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400277
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900278 if (resid)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100279 *resid = rq->resid_len;
280 if (sense && rq->sense_len)
281 memcpy(sense, rq->sense, SCSI_SENSE_BUFFERSIZE);
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100282 if (sshdr)
283 scsi_normalize_sense(rq->sense, rq->sense_len, sshdr);
James Bottomley39216032005-06-15 18:48:29 -0500284 ret = req->errors;
285 out:
286 blk_put_request(req);
287
288 return ret;
289}
James Bottomley33aa6872005-08-28 11:31:14 -0500290EXPORT_SYMBOL(scsi_execute);
James Bottomley39216032005-06-15 18:48:29 -0500291
Lin Ming9b214932013-03-23 11:42:25 +0800292int scsi_execute_req_flags(struct scsi_device *sdev, const unsigned char *cmd,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500293 int data_direction, void *buffer, unsigned bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900294 struct scsi_sense_hdr *sshdr, int timeout, int retries,
Christoph Hellwige8064022016-10-20 15:12:13 +0200295 int *resid, u64 flags, req_flags_t rq_flags)
James Bottomleyea73a9f2005-08-28 11:33:52 -0500296{
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100297 return scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100298 NULL, sshdr, timeout, retries, flags, rq_flags,
299 resid);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500300}
Lin Ming9b214932013-03-23 11:42:25 +0800301EXPORT_SYMBOL(scsi_execute_req_flags);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
304 * Function: scsi_init_cmd_errh()
305 *
306 * Purpose: Initialize cmd fields related to error handling.
307 *
308 * Arguments: cmd - command that is ready to be queued.
309 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * Notes: This function has the job of initializing a number of
311 * fields related to error handling. Typically this will
312 * be called once for each command, as required.
313 */
Christoph Hellwig631c2282006-07-08 20:42:15 +0200314static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 cmd->serial_number = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200317 scsi_set_resid(cmd, 0);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900318 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (cmd->cmd_len == 0)
Boaz Harroshdb4742d2008-04-30 11:27:26 +0300320 cmd->cmd_len = scsi_command_size(cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323void scsi_device_unbusy(struct scsi_device *sdev)
324{
325 struct Scsi_Host *shost = sdev->host;
Mike Christief0c0a372008-08-17 15:24:38 -0500326 struct scsi_target *starget = scsi_target(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned long flags;
328
Christoph Hellwig74665012014-01-22 15:29:29 +0100329 atomic_dec(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100330 if (starget->can_queue > 0)
331 atomic_dec(&starget->target_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +0100332
James Bottomley939647e2005-09-18 15:05:20 -0500333 if (unlikely(scsi_host_in_recovery(shost) &&
Christoph Hellwig74665012014-01-22 15:29:29 +0100334 (shost->host_failed || shost->host_eh_scheduled))) {
335 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 scsi_eh_wakeup(shost);
Christoph Hellwig74665012014-01-22 15:29:29 +0100337 spin_unlock_irqrestore(shost->host_lock, flags);
338 }
339
Christoph Hellwig71e75c92014-04-11 19:07:01 +0200340 atomic_dec(&sdev->device_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Christoph Hellwigd2852032014-01-17 12:06:53 +0100343static void scsi_kick_queue(struct request_queue *q)
344{
345 if (q->mq_ops)
346 blk_mq_start_hw_queues(q);
347 else
348 blk_run_queue(q);
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/*
352 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
353 * and call blk_run_queue for all the scsi_devices on the target -
354 * including current_sdev first.
355 *
356 * Called with *no* scsi locks held.
357 */
358static void scsi_single_lun_run(struct scsi_device *current_sdev)
359{
360 struct Scsi_Host *shost = current_sdev->host;
361 struct scsi_device *sdev, *tmp;
362 struct scsi_target *starget = scsi_target(current_sdev);
363 unsigned long flags;
364
365 spin_lock_irqsave(shost->host_lock, flags);
366 starget->starget_sdev_user = NULL;
367 spin_unlock_irqrestore(shost->host_lock, flags);
368
369 /*
370 * Call blk_run_queue for all LUNs on the target, starting with
371 * current_sdev. We race with others (to set starget_sdev_user),
372 * but in most cases, we will be first. Ideally, each LU on the
373 * target would get some limited time or requests on the target.
374 */
Christoph Hellwigd2852032014-01-17 12:06:53 +0100375 scsi_kick_queue(current_sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 spin_lock_irqsave(shost->host_lock, flags);
378 if (starget->starget_sdev_user)
379 goto out;
380 list_for_each_entry_safe(sdev, tmp, &starget->devices,
381 same_target_siblings) {
382 if (sdev == current_sdev)
383 continue;
384 if (scsi_device_get(sdev))
385 continue;
386
387 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100388 scsi_kick_queue(sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 spin_lock_irqsave(shost->host_lock, flags);
390
391 scsi_device_put(sdev);
392 }
393 out:
394 spin_unlock_irqrestore(shost->host_lock, flags);
395}
396
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100397static inline bool scsi_device_is_busy(struct scsi_device *sdev)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400398{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100399 if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
400 return true;
401 if (atomic_read(&sdev->device_blocked) > 0)
402 return true;
403 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400404}
405
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100406static inline bool scsi_target_is_busy(struct scsi_target *starget)
Mike Christief0c0a372008-08-17 15:24:38 -0500407{
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100408 if (starget->can_queue > 0) {
409 if (atomic_read(&starget->target_busy) >= starget->can_queue)
410 return true;
411 if (atomic_read(&starget->target_blocked) > 0)
412 return true;
413 }
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100414 return false;
Mike Christief0c0a372008-08-17 15:24:38 -0500415}
416
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100417static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400418{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100419 if (shost->can_queue > 0 &&
420 atomic_read(&shost->host_busy) >= shost->can_queue)
421 return true;
422 if (atomic_read(&shost->host_blocked) > 0)
423 return true;
424 if (shost->host_self_blocked)
425 return true;
426 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400427}
428
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800429static void scsi_starved_list_run(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Mike Christie2a3a59e2008-11-11 13:42:35 -0600431 LIST_HEAD(starved_list);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800432 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 unsigned long flags;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 spin_lock_irqsave(shost->host_lock, flags);
Mike Christie2a3a59e2008-11-11 13:42:35 -0600436 list_splice_init(&shost->starved_list, &starved_list);
437
438 while (!list_empty(&starved_list)) {
James Bottomleye2eb7242013-07-02 15:05:26 +0200439 struct request_queue *slq;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
442 * As long as shost is accepting commands and we have
443 * starved queues, call blk_run_queue. scsi_request_fn
444 * drops the queue_lock and can add us back to the
445 * starved_list.
446 *
447 * host_lock protects the starved_list and starved_entry.
448 * scsi_request_fn must get the host_lock before checking
449 * or modifying starved_list or starved_entry.
450 */
Mike Christie2a3a59e2008-11-11 13:42:35 -0600451 if (scsi_host_is_busy(shost))
Mike Christief0c0a372008-08-17 15:24:38 -0500452 break;
Mike Christief0c0a372008-08-17 15:24:38 -0500453
Mike Christie2a3a59e2008-11-11 13:42:35 -0600454 sdev = list_entry(starved_list.next,
455 struct scsi_device, starved_entry);
456 list_del_init(&sdev->starved_entry);
Mike Christief0c0a372008-08-17 15:24:38 -0500457 if (scsi_target_is_busy(scsi_target(sdev))) {
458 list_move_tail(&sdev->starved_entry,
459 &shost->starved_list);
460 continue;
461 }
462
James Bottomleye2eb7242013-07-02 15:05:26 +0200463 /*
464 * Once we drop the host lock, a racing scsi_remove_device()
465 * call may remove the sdev from the starved list and destroy
466 * it and the queue. Mitigate by taking a reference to the
467 * queue and never touching the sdev again after we drop the
468 * host lock. Note: if __scsi_remove_device() invokes
469 * blk_cleanup_queue() before the queue is run from this
470 * function then blk_run_queue() will return immediately since
471 * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
472 */
473 slq = sdev->request_queue;
474 if (!blk_get_queue(slq))
475 continue;
476 spin_unlock_irqrestore(shost->host_lock, flags);
477
Christoph Hellwigd2852032014-01-17 12:06:53 +0100478 scsi_kick_queue(slq);
James Bottomleye2eb7242013-07-02 15:05:26 +0200479 blk_put_queue(slq);
480
481 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Mike Christie2a3a59e2008-11-11 13:42:35 -0600483 /* put any unprocessed entries back */
484 list_splice(&starved_list, &shost->starved_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800486}
487
488/*
489 * Function: scsi_run_queue()
490 *
491 * Purpose: Select a proper request queue to serve next
492 *
493 * Arguments: q - last request's queue
494 *
495 * Returns: Nothing
496 *
497 * Notes: The previous command was completely finished, start
498 * a new one if possible.
499 */
500static void scsi_run_queue(struct request_queue *q)
501{
502 struct scsi_device *sdev = q->queuedata;
503
504 if (scsi_target(sdev)->single_lun)
505 scsi_single_lun_run(sdev);
506 if (!list_empty(&sdev->host->starved_list))
507 scsi_starved_list_run(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Christoph Hellwigd2852032014-01-17 12:06:53 +0100509 if (q->mq_ops)
510 blk_mq_start_stopped_hw_queues(q, false);
511 else
512 blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Jens Axboe9937a5e2011-05-17 11:04:44 +0200515void scsi_requeue_run_queue(struct work_struct *work)
516{
517 struct scsi_device *sdev;
518 struct request_queue *q;
519
520 sdev = container_of(work, struct scsi_device, requeue_work);
521 q = sdev->request_queue;
522 scsi_run_queue(q);
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/*
526 * Function: scsi_requeue_command()
527 *
528 * Purpose: Handle post-processing of completed commands.
529 *
530 * Arguments: q - queue to operate on
531 * cmd - command that may need to be requeued.
532 *
533 * Returns: Nothing
534 *
535 * Notes: After command completion, there may be blocks left
536 * over which weren't finished by the previous command
537 * this can be for a number of reasons - the main one is
538 * I/O errors in the middle of the request, in which case
539 * we need to request the blocks that come after the bad
540 * sector.
James Bottomleye91442b2005-09-09 10:44:16 -0500541 * Notes: Upon return, cmd is a stale pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
543static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
544{
Bart Van Assche940f5d42012-06-29 15:34:26 +0000545 struct scsi_device *sdev = cmd->device;
James Bottomleye91442b2005-09-09 10:44:16 -0500546 struct request *req = cmd->request;
Tejun Heo 283369c2005-04-24 02:06:36 -0500547 unsigned long flags;
548
Tejun Heo 283369c2005-04-24 02:06:36 -0500549 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig134997a2014-02-20 14:20:58 -0800550 blk_unprep_request(req);
551 req->special = NULL;
552 scsi_put_command(cmd);
James Bottomleye91442b2005-09-09 10:44:16 -0500553 blk_requeue_request(q, req);
Tejun Heo 283369c2005-04-24 02:06:36 -0500554 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 scsi_run_queue(q);
Bart Van Assche940f5d42012-06-29 15:34:26 +0000557
558 put_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561void scsi_run_host_queues(struct Scsi_Host *shost)
562{
563 struct scsi_device *sdev;
564
565 shost_for_each_device(sdev, shost)
566 scsi_run_queue(sdev->request_queue);
567}
568
Christoph Hellwigd2852032014-01-17 12:06:53 +0100569static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
570{
Christoph Hellwig57292b52017-01-31 16:57:29 +0100571 if (!blk_rq_is_passthrough(cmd->request)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +0100572 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
573
574 if (drv->uninit_command)
575 drv->uninit_command(cmd);
576 }
577}
578
579static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd)
580{
Ming Lin91dbc082016-04-04 14:48:07 -0700581 struct scsi_data_buffer *sdb;
582
Christoph Hellwigd2852032014-01-17 12:06:53 +0100583 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700584 sg_free_table_chained(&cmd->sdb.table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700585 if (cmd->request->next_rq) {
586 sdb = cmd->request->next_rq->special;
587 if (sdb)
Ming Lin001d63b2016-04-04 14:48:09 -0700588 sg_free_table_chained(&sdb->table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700589 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100590 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700591 sg_free_table_chained(&cmd->prot_sdb->table, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100592}
593
594static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
595{
596 struct scsi_device *sdev = cmd->device;
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +0530597 struct Scsi_Host *shost = sdev->host;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100598 unsigned long flags;
599
Christoph Hellwigd2852032014-01-17 12:06:53 +0100600 scsi_mq_free_sgtables(cmd);
601 scsi_uninit_cmd(cmd);
602
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +0530603 if (shost->use_cmd_list) {
604 BUG_ON(list_empty(&cmd->list));
605 spin_lock_irqsave(&sdev->list_lock, flags);
606 list_del_init(&cmd->list);
607 spin_unlock_irqrestore(&sdev->list_lock, flags);
608 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/*
612 * Function: scsi_release_buffers()
613 *
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200614 * Purpose: Free resources allocate for a scsi_command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 *
616 * Arguments: cmd - command that we are bailing.
617 *
618 * Lock status: Assumed that no lock is held upon entry.
619 *
620 * Returns: Nothing
621 *
622 * Notes: In the event that an upper level driver rejects a
623 * command, we must release resources allocated during
624 * the __init_io() function. Primarily this would involve
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200625 * the scatter-gather table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
Christoph Hellwigf1bea552014-04-15 12:26:54 +0200627static void scsi_release_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200629 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700630 sg_free_table_chained(&cmd->sdb.table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200631
632 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
633
634 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700635 sg_free_table_chained(&cmd->prot_sdb->table, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200638static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
639{
640 struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special;
641
Ming Lin001d63b2016-04-04 14:48:09 -0700642 sg_free_table_chained(&bidi_sdb->table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200643 kmem_cache_free(scsi_sdb_cache, bidi_sdb);
644 cmd->request->next_rq->special = NULL;
645}
646
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800647static bool scsi_end_request(struct request *req, int error,
648 unsigned int bytes, unsigned int bidi_bytes)
649{
650 struct scsi_cmnd *cmd = req->special;
651 struct scsi_device *sdev = cmd->device;
652 struct request_queue *q = sdev->request_queue;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800653
654 if (blk_update_request(req, error, bytes))
655 return true;
656
657 /* Bidi request must be completed as a whole */
658 if (unlikely(bidi_bytes) &&
659 blk_update_request(req->next_rq, error, bidi_bytes))
660 return true;
661
662 if (blk_queue_add_random(q))
663 add_disk_randomness(req->rq_disk);
664
Christoph Hellwigd2852032014-01-17 12:06:53 +0100665 if (req->mq_ctx) {
666 /*
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700667 * In the MQ case the command gets freed by __blk_mq_end_request,
Christoph Hellwigd2852032014-01-17 12:06:53 +0100668 * so we have to do all cleanup that depends on it earlier.
669 *
670 * We also can't kick the queues from irq context, so we
671 * will have to defer it to a workqueue.
672 */
673 scsi_mq_uninit_cmd(cmd);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800674
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700675 __blk_mq_end_request(req, error);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100676
677 if (scsi_target(sdev)->single_lun ||
678 !list_empty(&sdev->host->starved_list))
679 kblockd_schedule_work(&sdev->requeue_work);
680 else
681 blk_mq_start_stopped_hw_queues(q, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100682 } else {
683 unsigned long flags;
684
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400685 if (bidi_bytes)
686 scsi_release_bidi_buffers(cmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +0300687 scsi_release_buffers(cmd);
688 scsi_put_command(cmd);
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400689
Christoph Hellwigd2852032014-01-17 12:06:53 +0100690 spin_lock_irqsave(q->queue_lock, flags);
691 blk_finish_request(req, error);
692 spin_unlock_irqrestore(q->queue_lock, flags);
693
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700694 scsi_run_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100695 }
696
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700697 put_device(&sdev->sdev_gendev);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800698 return false;
699}
700
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200701/**
702 * __scsi_error_from_host_byte - translate SCSI error code into errno
703 * @cmd: SCSI command (unused)
704 * @result: scsi error code
705 *
706 * Translate SCSI error code into standard UNIX errno.
707 * Return values:
708 * -ENOLINK temporary transport failure
709 * -EREMOTEIO permanent target failure, do not retry
710 * -EBADE permanent nexus failure, retry on other path
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200711 * -ENOSPC No write space available
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200712 * -ENODATA Medium error
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200713 * -EIO unspecified I/O error
714 */
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100715static int __scsi_error_from_host_byte(struct scsi_cmnd *cmd, int result)
716{
717 int error = 0;
718
719 switch(host_byte(result)) {
720 case DID_TRANSPORT_FAILFAST:
721 error = -ENOLINK;
722 break;
723 case DID_TARGET_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000724 set_host_byte(cmd, DID_OK);
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100725 error = -EREMOTEIO;
726 break;
727 case DID_NEXUS_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000728 set_host_byte(cmd, DID_OK);
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100729 error = -EBADE;
730 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200731 case DID_ALLOC_FAILURE:
732 set_host_byte(cmd, DID_OK);
733 error = -ENOSPC;
734 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200735 case DID_MEDIUM_ERROR:
736 set_host_byte(cmd, DID_OK);
737 error = -ENODATA;
738 break;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100739 default:
740 error = -EIO;
741 break;
742 }
743
744 return error;
745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747/*
748 * Function: scsi_io_completion()
749 *
750 * Purpose: Completion processing for block device I/O requests.
751 *
752 * Arguments: cmd - command that is finished.
753 *
754 * Lock status: Assumed that no lock is held upon entry.
755 *
756 * Returns: Nothing
757 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200758 * Notes: We will finish off the specified number of sectors. If we
759 * are done, the command block will be released and the queue
760 * function will be goosed. If we are not done then we have to
Alan Sternb60af5b2008-11-03 15:56:47 -0500761 * figure out what to do next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
Alan Sternb60af5b2008-11-03 15:56:47 -0500763 * a) We can call scsi_requeue_command(). The request
764 * will be unprepared and put back on the queue. Then
765 * a new command will be created for it. This should
766 * be used if we made forward progress, or if we want
767 * to switch from READ(10) to READ(6) for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200769 * b) We can call __scsi_queue_insert(). The request will
Alan Sternb60af5b2008-11-03 15:56:47 -0500770 * be put back on the queue and retried using the same
771 * command as before, possibly after a delay.
772 *
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800773 * c) We can call scsi_end_request() with -EIO to fail
Alan Sternb60af5b2008-11-03 15:56:47 -0500774 * the remainder of the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700776void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 int result = cmd->result;
Jens Axboe165125e2007-07-24 09:28:11 +0200779 struct request_queue *q = cmd->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct request *req = cmd->request;
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500781 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct scsi_sense_hdr sshdr;
Hannes Reinecke4753cbc2014-10-24 14:26:52 +0200783 bool sense_valid = false;
Hannes Reineckec11c0042014-10-24 14:27:01 +0200784 int sense_deferred = 0, level = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500785 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
786 ACTION_DELAYED_RETRY} action;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900787 unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (result) {
790 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
791 if (sense_valid)
792 sense_deferred = scsi_sense_is_deferred(&sshdr);
793 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200794
Christoph Hellwig57292b52017-01-31 16:57:29 +0100795 if (blk_rq_is_passthrough(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (result) {
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100797 if (sense_valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /*
799 * SG_IO wants current and deferred errors
800 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100801 scsi_req(req)->sense_len =
802 min(8 + cmd->sense_buffer[7],
803 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500805 if (!sense_deferred)
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100806 error = __scsi_error_from_host_byte(cmd, result);
Pete Wyckoffb22f6872007-03-13 16:53:28 -0400807 }
Mike Snitzer27c41972012-05-31 15:05:33 -0400808 /*
809 * __scsi_error_from_host_byte may have reset the host_byte
810 */
811 req->errors = cmd->result;
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900812
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100813 scsi_req(req)->resid_len = scsi_get_resid(cmd);
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900814
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200815 if (scsi_bidi_cmnd(cmd)) {
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900816 /*
817 * Bidi commands Must be complete as a whole,
818 * both sides at once.
819 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100820 scsi_req(req->next_rq)->resid_len = scsi_in(cmd)->resid;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800821 if (scsi_end_request(req, 0, blk_rq_bytes(req),
822 blk_rq_bytes(req->next_rq)))
823 BUG();
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200824 return;
825 }
James Bottomley89fb4cd2014-07-03 19:17:34 +0200826 } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
827 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100828 * Flush commands do not transfers any data, and thus cannot use
James Bottomley89fb4cd2014-07-03 19:17:34 +0200829 * good_bytes != blk_rq_bytes(req) as the signal for an error.
830 * This sets the error explicitly for the problem case.
831 */
832 error = __scsi_error_from_host_byte(cmd, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
Christoph Hellwig57292b52017-01-31 16:57:29 +0100835 /* no bidi support for !blk_rq_is_passthrough yet */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200836 BUG_ON(blk_bidi_rq(req));
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 * Next deal with any sectors which we were able to correctly
840 * handle.
841 */
Hannes Reinecke91921e02014-06-25 16:39:59 +0200842 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
843 "%u sectors total, %d bytes done.\n",
844 blk_rq_sectors(req), good_bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
James Bottomleya9bddd72009-03-30 16:55:51 +0000846 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100847 * Recovered errors need reporting, but they're always treated as
848 * success, so fiddle the result code here. For passthrough requests
James Bottomleya9bddd72009-03-30 16:55:51 +0000849 * we already took a copy of the original into rq->errors which
850 * is what gets returned to the user
851 */
Douglas Gilberte7efe592010-01-03 13:51:15 -0500852 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
853 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
854 * print since caller wants ATA registers. Only occurs on
855 * SCSI ATA PASS_THROUGH commands when CK_COND=1
856 */
857 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
858 ;
Christoph Hellwige8064022016-10-20 15:12:13 +0200859 else if (!(req->rq_flags & RQF_QUIET))
Hannes Reinecked811b842014-10-24 14:26:45 +0200860 scsi_print_sense(cmd);
James Bottomleya9bddd72009-03-30 16:55:51 +0000861 result = 0;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100862 /* for passthrough error may be set */
James Bottomleya9bddd72009-03-30 16:55:51 +0000863 error = 0;
864 }
865
866 /*
James Bottomleya621bac2016-05-13 12:04:06 -0700867 * special case: failed zero length commands always need to
868 * drop down into the retry code. Otherwise, if we finished
869 * all bytes in the request we are done now.
James Bottomleyd6b0c532006-07-02 10:06:28 -0500870 */
James Bottomleya621bac2016-05-13 12:04:06 -0700871 if (!(blk_rq_bytes(req) == 0 && error) &&
872 !scsi_end_request(req, error, good_bytes, 0))
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800873 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200874
875 /*
876 * Kill remainder if no retrys.
877 */
878 if (error && scsi_noretry_cmd(cmd)) {
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800879 if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
880 BUG();
881 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200882 }
883
884 /*
885 * If there had been no error, but we have leftover bytes in the
886 * requeues just queue the command up again.
887 */
888 if (result == 0)
889 goto requeue;
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700890
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100891 error = __scsi_error_from_host_byte(cmd, result);
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500892
Alan Sternb60af5b2008-11-03 15:56:47 -0500893 if (host_byte(result) == DID_RESET) {
894 /* Third party bus reset or reset for error recovery
895 * reasons. Just retry the command and see what
896 * happens.
897 */
898 action = ACTION_RETRY;
899 } else if (sense_valid && !sense_deferred) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 switch (sshdr.sense_key) {
901 case UNIT_ATTENTION:
902 if (cmd->device->removable) {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700903 /* Detected disc change. Set a bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * and quietly refuse further access.
905 */
906 cmd->device->changed = 1;
Alan Sternb60af5b2008-11-03 15:56:47 -0500907 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 } else {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700909 /* Must have been a power glitch, or a
910 * bus reset. Could not have been a
911 * media change, so we just retry the
Alan Sternb60af5b2008-11-03 15:56:47 -0500912 * command and see what happens.
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700913 */
Alan Sternb60af5b2008-11-03 15:56:47 -0500914 action = ACTION_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916 break;
917 case ILLEGAL_REQUEST:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700918 /* If we had an ILLEGAL REQUEST returned, then
919 * we may have performed an unsupported
920 * command. The only thing this should be
921 * would be a ten byte read where only a six
922 * byte read was supported. Also, on a system
923 * where READ CAPACITY failed, we may have
924 * read past the end of the disk.
925 */
Jens Axboe26a68012005-11-29 21:03:34 +0100926 if ((cmd->device->use_10_for_rw &&
927 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 (cmd->cmnd[0] == READ_10 ||
929 cmd->cmnd[0] == WRITE_10)) {
Alan Sternb60af5b2008-11-03 15:56:47 -0500930 /* This will issue a new 6-byte command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 cmd->device->use_10_for_rw = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500932 action = ACTION_REPREP;
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500933 } else if (sshdr.asc == 0x10) /* DIX */ {
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500934 action = ACTION_FAIL;
935 error = -EILSEQ;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500936 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
Martin K. Petersen5db44862012-09-18 12:19:32 -0400937 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500938 action = ACTION_FAIL;
Martin K. Petersen66a651a2012-02-13 15:38:22 -0500939 error = -EREMOTEIO;
Alan Sternb60af5b2008-11-03 15:56:47 -0500940 } else
941 action = ACTION_FAIL;
942 break;
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400943 case ABORTED_COMMAND:
James Bottomley126c09822009-02-19 21:48:54 +0000944 action = ACTION_FAIL;
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200945 if (sshdr.asc == 0x10) /* DIF */
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500946 error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
948 case NOT_READY:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700949 /* If the device is in the process of becoming
James Bottomleyf3e93f72006-04-25 16:48:30 -0500950 * ready, or has a temporary blockage, retry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
James Bottomleyf3e93f72006-04-25 16:48:30 -0500952 if (sshdr.asc == 0x04) {
953 switch (sshdr.ascq) {
954 case 0x01: /* becoming ready */
955 case 0x04: /* format in progress */
956 case 0x05: /* rebuild in progress */
957 case 0x06: /* recalculation in progress */
958 case 0x07: /* operation in progress */
959 case 0x08: /* Long write in progress */
960 case 0x09: /* self test in progress */
Martin K. Petersend8705f12009-11-26 12:00:41 -0500961 case 0x14: /* space allocation in progress */
Alan Sternb60af5b2008-11-03 15:56:47 -0500962 action = ACTION_DELAYED_RETRY;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500963 break;
Alan Stern3dbf6a52008-12-15 10:31:28 -0500964 default:
Alan Stern3dbf6a52008-12-15 10:31:28 -0500965 action = ACTION_FAIL;
966 break;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500967 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200968 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500969 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500970 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 case VOLUME_OVERFLOW:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700972 /* See SSC3rXX or current. */
Alan Sternb60af5b2008-11-03 15:56:47 -0500973 action = ACTION_FAIL;
974 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 default:
Alan Sternb60af5b2008-11-03 15:56:47 -0500976 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 break;
978 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200979 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500980 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500981
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900982 if (action != ACTION_FAIL &&
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200983 time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900984 action = ACTION_FAIL;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900985
Alan Sternb60af5b2008-11-03 15:56:47 -0500986 switch (action) {
987 case ACTION_FAIL:
988 /* Give up and fail the remainder of the request */
Christoph Hellwige8064022016-10-20 15:12:13 +0200989 if (!(req->rq_flags & RQF_QUIET)) {
Hannes Reineckef1569ff2014-10-24 14:27:07 +0200990 static DEFINE_RATELIMIT_STATE(_rs,
991 DEFAULT_RATELIMIT_INTERVAL,
992 DEFAULT_RATELIMIT_BURST);
993
994 if (unlikely(scsi_logging_level))
995 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
996 SCSI_LOG_MLCOMPLETE_BITS);
997
998 /*
999 * if logging is enabled the failure will be printed
1000 * in scsi_log_completion(), so avoid duplicate messages
1001 */
1002 if (!level && __ratelimit(&_rs)) {
1003 scsi_print_result(cmd, NULL, FAILED);
1004 if (driver_byte(result) & DRIVER_SENSE)
1005 scsi_print_sense(cmd);
1006 scsi_print_command(cmd);
1007 }
James Bottomley3173d8c2005-09-04 11:32:05 -05001008 }
Christoph Hellwigf6d47e72014-02-16 06:16:13 -08001009 if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
1010 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +02001011 /*FALLTHRU*/
Alan Sternb60af5b2008-11-03 15:56:47 -05001012 case ACTION_REPREP:
Christoph Hellwigbc85dc52014-05-01 16:51:03 +02001013 requeue:
Alan Sternb60af5b2008-11-03 15:56:47 -05001014 /* Unprep the request and put it back at the head of the queue.
1015 * A new command will be prepared and issued.
1016 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01001017 if (q->mq_ops) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001018 cmd->request->rq_flags &= ~RQF_DONTPREP;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001019 scsi_mq_uninit_cmd(cmd);
1020 scsi_mq_requeue_cmd(cmd);
1021 } else {
1022 scsi_release_buffers(cmd);
1023 scsi_requeue_command(q, cmd);
1024 }
Alan Sternb60af5b2008-11-03 15:56:47 -05001025 break;
1026 case ACTION_RETRY:
1027 /* Retry the same command immediately */
James Bottomley4f5299a2009-01-02 10:42:21 -06001028 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -05001029 break;
1030 case ACTION_DELAYED_RETRY:
1031 /* Retry the same command after a delay */
James Bottomley4f5299a2009-01-02 10:42:21 -06001032 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -05001033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001037static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001039 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001042 * If sg table allocation fails, requeue request later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001044 if (unlikely(sg_alloc_table_chained(&sdb->table,
1045 blk_rq_nr_phys_segments(req), sdb->table.sgl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return BLKPREP_DEFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
1049 * Next, walk the list, and fill in the addresses and sizes of
1050 * each segment.
1051 */
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001052 count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
1053 BUG_ON(count > sdb->table.nents);
1054 sdb->table.nents = count;
Christoph Hellwigfd102b12017-01-13 12:29:11 +01001055 sdb->length = blk_rq_payload_bytes(req);
Rusty Russell4a03d902007-11-19 11:28:48 +11001056 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001058
1059/*
1060 * Function: scsi_init_io()
1061 *
1062 * Purpose: SCSI I/O initialize function.
1063 *
1064 * Arguments: cmd - Command descriptor we wish to initialize
1065 *
1066 * Returns: 0 on success
1067 * BLKPREP_DEFER if the failure is retryable
1068 * BLKPREP_KILL if the failure is fatal
1069 */
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001070int scsi_init_io(struct scsi_cmnd *cmd)
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001071{
Christoph Hellwig5e012aa2014-04-15 12:24:55 +02001072 struct scsi_device *sdev = cmd->device;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001073 struct request *rq = cmd->request;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001074 bool is_mq = (rq->mq_ctx != NULL);
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001075 int error;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001076
Johannes Thumshirnfd3fc0b2017-01-31 10:16:00 +01001077 if (WARN_ON_ONCE(!blk_rq_nr_phys_segments(rq)))
1078 return -EINVAL;
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001079
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001080 error = scsi_init_sgtable(rq, &cmd->sdb);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001081 if (error)
1082 goto err_exit;
1083
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001084 if (blk_bidi_rq(rq)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001085 if (!rq->q->mq_ops) {
1086 struct scsi_data_buffer *bidi_sdb =
1087 kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC);
1088 if (!bidi_sdb) {
1089 error = BLKPREP_DEFER;
1090 goto err_exit;
1091 }
1092
1093 rq->next_rq->special = bidi_sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001094 }
1095
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001096 error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001097 if (error)
1098 goto err_exit;
1099 }
1100
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001101 if (blk_integrity_rq(rq)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001102 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1103 int ivecs, count;
1104
Ewan D. Milne91724c22015-01-15 10:02:12 -05001105 if (prot_sdb == NULL) {
1106 /*
1107 * This can happen if someone (e.g. multipath)
1108 * queues a command to a device on an adapter
1109 * that does not support DIX.
1110 */
1111 WARN_ON_ONCE(1);
1112 error = BLKPREP_KILL;
1113 goto err_exit;
1114 }
1115
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001116 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001117
Ming Lin001d63b2016-04-04 14:48:09 -07001118 if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
Ming Lin22cc3d42016-04-04 14:48:08 -07001119 prot_sdb->table.sgl)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001120 error = BLKPREP_DEFER;
1121 goto err_exit;
1122 }
1123
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001124 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001125 prot_sdb->table.sgl);
1126 BUG_ON(unlikely(count > ivecs));
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001127 BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q)));
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001128
1129 cmd->prot_sdb = prot_sdb;
1130 cmd->prot_sdb->table.nents = count;
1131 }
1132
Christoph Hellwigd2852032014-01-17 12:06:53 +01001133 return BLKPREP_OK;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001134err_exit:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001135 if (is_mq) {
1136 scsi_mq_free_sgtables(cmd);
1137 } else {
1138 scsi_release_buffers(cmd);
1139 cmd->request->special = NULL;
1140 scsi_put_command(cmd);
1141 put_device(&sdev->sdev_gendev);
1142 }
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001143 return error;
1144}
Boaz Harroshbb52d822007-12-13 16:14:27 -08001145EXPORT_SYMBOL(scsi_init_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001147void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001148{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001149 void *buf = cmd->sense_buffer;
1150 void *prot = cmd->prot_sdb;
1151 unsigned long flags;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001152
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001153 /* zero out the cmd, except for the embedded scsi_request */
1154 memset((char *)cmd + sizeof(cmd->req), 0,
1155 sizeof(*cmd) - sizeof(cmd->req));
Christoph Hellwig04796332014-02-20 14:20:55 -08001156
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001157 cmd->device = dev;
1158 cmd->sense_buffer = buf;
1159 cmd->prot_sdb = prot;
1160 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1161 cmd->jiffies_at_alloc = jiffies;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001162
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001163 spin_lock_irqsave(&dev->list_lock, flags);
1164 list_add_tail(&cmd->list, &dev->cmd_list);
1165 spin_unlock_irqrestore(&dev->list_lock, flags);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001166}
1167
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001168static int scsi_setup_scsi_cmnd(struct scsi_device *sdev, struct request *req)
James Bottomley7b163182005-12-15 20:17:02 -06001169{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001170 struct scsi_cmnd *cmd = req->special;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001171
1172 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001173 * Passthrough requests may transfer data, in which case they must
Christoph Hellwig3b003152006-11-04 20:10:55 +01001174 * a bio attached to them. Or they might contain a SCSI command
1175 * that does not transfer data, in which case they may optionally
1176 * submit a request without an attached bio.
1177 */
1178 if (req->bio) {
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001179 int ret = scsi_init_io(cmd);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001180 if (unlikely(ret))
1181 return ret;
1182 } else {
Tejun Heob0790412009-05-07 22:24:42 +09001183 BUG_ON(blk_rq_bytes(req));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001184
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001185 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001186 }
James Bottomley7b163182005-12-15 20:17:02 -06001187
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001188 cmd->cmd_len = scsi_req(req)->cmd_len;
1189 cmd->cmnd = scsi_req(req)->cmd;
Tejun Heob0790412009-05-07 22:24:42 +09001190 cmd->transfersize = blk_rq_bytes(req);
James Bottomley7b163182005-12-15 20:17:02 -06001191 cmd->allowed = req->retries;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001192 return BLKPREP_OK;
1193}
1194
1195/*
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001196 * Setup a normal block command. These are simple request from filesystems
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001197 * that still need to be translated to SCSI CDBs from the ULD.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001198 */
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001199static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001200{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001201 struct scsi_cmnd *cmd = req->special;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001202
Christoph Hellwigee14c672015-08-27 14:16:59 +02001203 if (unlikely(sdev->handler && sdev->handler->prep_fn)) {
1204 int ret = sdev->handler->prep_fn(sdev, req);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001205 if (ret != BLKPREP_OK)
1206 return ret;
1207 }
1208
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001209 cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001210 memset(cmd->cmnd, 0, BLK_MAX_CDB);
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001211 return scsi_cmd_to_driver(cmd)->init_command(cmd);
James Bottomley7b163182005-12-15 20:17:02 -06001212}
James Bottomley7b163182005-12-15 20:17:02 -06001213
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001214static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req)
1215{
1216 struct scsi_cmnd *cmd = req->special;
1217
1218 if (!blk_rq_bytes(req))
1219 cmd->sc_data_direction = DMA_NONE;
1220 else if (rq_data_dir(req) == WRITE)
1221 cmd->sc_data_direction = DMA_TO_DEVICE;
1222 else
1223 cmd->sc_data_direction = DMA_FROM_DEVICE;
1224
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001225 if (blk_rq_is_scsi(req))
1226 return scsi_setup_scsi_cmnd(sdev, req);
1227 else
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001228 return scsi_setup_fs_cmnd(sdev, req);
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001229}
1230
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001231static int
1232scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Christoph Hellwig3b003152006-11-04 20:10:55 +01001234 int ret = BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001237 * If the device is not in running state we will reject some
1238 * or all commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
Christoph Hellwig3b003152006-11-04 20:10:55 +01001241 switch (sdev->sdev_state) {
1242 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05001243 case SDEV_TRANSPORT_OFFLINE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001244 /*
1245 * If the device is offline we refuse to process any
1246 * commands. The device must be brought online
1247 * before trying any recovery commands.
1248 */
1249 sdev_printk(KERN_ERR, sdev,
1250 "rejecting I/O to offline device\n");
1251 ret = BLKPREP_KILL;
1252 break;
1253 case SDEV_DEL:
1254 /*
1255 * If the device is fully deleted, we refuse to
1256 * process any commands as well.
1257 */
James Bottomley9ccfc752005-10-02 11:45:08 -05001258 sdev_printk(KERN_ERR, sdev,
1259 "rejecting I/O to dead device\n");
Christoph Hellwig3b003152006-11-04 20:10:55 +01001260 ret = BLKPREP_KILL;
1261 break;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001262 case SDEV_BLOCK:
James Bottomley6f4267e2008-08-22 16:53:31 -05001263 case SDEV_CREATED_BLOCK:
Bart Van Asschebba0bdd2015-03-04 10:31:47 +01001264 ret = BLKPREP_DEFER;
1265 break;
1266 case SDEV_QUIESCE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001267 /*
1268 * If the devices is blocked we defer normal commands.
1269 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001270 if (!(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001271 ret = BLKPREP_DEFER;
1272 break;
1273 default:
1274 /*
1275 * For any other not fully online state we only allow
1276 * special commands. In particular any user initiated
1277 * command is not allowed.
1278 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001279 if (!(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001280 ret = BLKPREP_KILL;
1281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001284 return ret;
1285}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001287static int
1288scsi_prep_return(struct request_queue *q, struct request *req, int ret)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001289{
1290 struct scsi_device *sdev = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Christoph Hellwig3b003152006-11-04 20:10:55 +01001292 switch (ret) {
1293 case BLKPREP_KILL:
jiangyiwene1cd3912016-02-16 20:14:13 +08001294 case BLKPREP_INVALID:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001295 req->errors = DID_NO_CONNECT << 16;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001296 /* release the command and kill it */
1297 if (req->special) {
1298 struct scsi_cmnd *cmd = req->special;
1299 scsi_release_buffers(cmd);
1300 scsi_put_command(cmd);
Christoph Hellwig68c03d92014-04-15 12:24:56 +02001301 put_device(&sdev->sdev_gendev);
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001302 req->special = NULL;
1303 }
Christoph Hellwig3b003152006-11-04 20:10:55 +01001304 break;
1305 case BLKPREP_DEFER:
1306 /*
Tejun Heo9934c8c2009-05-08 11:54:16 +09001307 * If we defer, the blk_peek_request() returns NULL, but the
Jens Axboea488e742010-04-16 21:13:15 +02001308 * queue must be restarted, so we schedule a callback to happen
1309 * shortly.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001310 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001311 if (atomic_read(&sdev->device_busy) == 0)
Jens Axboea488e742010-04-16 21:13:15 +02001312 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001313 break;
1314 default:
Christoph Hellwige8064022016-10-20 15:12:13 +02001315 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Christoph Hellwig3b003152006-11-04 20:10:55 +01001318 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001320
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001321static int scsi_prep_fn(struct request_queue *q, struct request *req)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001322{
1323 struct scsi_device *sdev = q->queuedata;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001324 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001325 int ret;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001326
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001327 ret = scsi_prep_state_check(sdev, req);
1328 if (ret != BLKPREP_OK)
1329 goto out;
1330
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001331 if (!req->special) {
1332 /* Bail if we can't get a reference to the device */
1333 if (unlikely(!get_device(&sdev->sdev_gendev))) {
1334 ret = BLKPREP_DEFER;
1335 goto out;
1336 }
1337
1338 scsi_init_command(sdev, cmd);
1339 req->special = cmd;
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001340 }
1341
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001342 cmd->tag = req->tag;
1343 cmd->request = req;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001344 cmd->prot_op = SCSI_PROT_NORMAL;
1345
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001346 ret = scsi_setup_cmnd(sdev, req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001347out:
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001348 return scsi_prep_return(q, req, ret);
1349}
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001350
1351static void scsi_unprep_fn(struct request_queue *q, struct request *req)
1352{
Christoph Hellwigd2852032014-01-17 12:06:53 +01001353 scsi_uninit_cmd(req->special);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001354}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356/*
1357 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1358 * return 0.
1359 *
1360 * Called with the queue_lock held.
1361 */
1362static inline int scsi_dev_queue_ready(struct request_queue *q,
1363 struct scsi_device *sdev)
1364{
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001365 unsigned int busy;
1366
1367 busy = atomic_inc_return(&sdev->device_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001368 if (atomic_read(&sdev->device_blocked)) {
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001369 if (busy)
1370 goto out_dec;
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 /*
1373 * unblock after device_blocked iterates to zero
1374 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001375 if (atomic_dec_return(&sdev->device_blocked) > 0) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001376 /*
1377 * For the MQ case we take care of this in the caller.
1378 */
1379 if (!q->mq_ops)
1380 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001381 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001383 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1384 "unblocking device at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001386
1387 if (busy >= sdev->queue_depth)
1388 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 return 1;
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001391out_dec:
1392 atomic_dec(&sdev->device_busy);
1393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Mike Christief0c0a372008-08-17 15:24:38 -05001396/*
1397 * scsi_target_queue_ready: checks if there we can send commands to target
1398 * @sdev: scsi device on starget to check.
Mike Christief0c0a372008-08-17 15:24:38 -05001399 */
1400static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1401 struct scsi_device *sdev)
1402{
1403 struct scsi_target *starget = scsi_target(sdev);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001404 unsigned int busy;
Mike Christief0c0a372008-08-17 15:24:38 -05001405
1406 if (starget->single_lun) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001407 spin_lock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001408 if (starget->starget_sdev_user &&
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001409 starget->starget_sdev_user != sdev) {
1410 spin_unlock_irq(shost->host_lock);
1411 return 0;
1412 }
Mike Christief0c0a372008-08-17 15:24:38 -05001413 starget->starget_sdev_user = sdev;
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001414 spin_unlock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001415 }
1416
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001417 if (starget->can_queue <= 0)
1418 return 1;
1419
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001420 busy = atomic_inc_return(&starget->target_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001421 if (atomic_read(&starget->target_blocked) > 0) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001422 if (busy)
1423 goto starved;
1424
Mike Christief0c0a372008-08-17 15:24:38 -05001425 /*
1426 * unblock after target_blocked iterates to zero
1427 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001428 if (atomic_dec_return(&starget->target_blocked) > 0)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001429 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001430
1431 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1432 "unblocking target at zero depth\n"));
Mike Christief0c0a372008-08-17 15:24:38 -05001433 }
1434
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001435 if (busy >= starget->can_queue)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001436 goto starved;
Mike Christief0c0a372008-08-17 15:24:38 -05001437
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001438 return 1;
1439
1440starved:
1441 spin_lock_irq(shost->host_lock);
1442 list_move_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001443 spin_unlock_irq(shost->host_lock);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001444out_dec:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001445 if (starget->can_queue > 0)
1446 atomic_dec(&starget->target_busy);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001447 return 0;
Mike Christief0c0a372008-08-17 15:24:38 -05001448}
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450/*
1451 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1452 * return 0. We must end up running the queue again whenever 0 is
1453 * returned, else IO can hang.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 */
1455static inline int scsi_host_queue_ready(struct request_queue *q,
1456 struct Scsi_Host *shost,
1457 struct scsi_device *sdev)
1458{
Christoph Hellwig74665012014-01-22 15:29:29 +01001459 unsigned int busy;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001460
James Bottomley939647e2005-09-18 15:05:20 -05001461 if (scsi_host_in_recovery(shost))
Christoph Hellwig74665012014-01-22 15:29:29 +01001462 return 0;
1463
1464 busy = atomic_inc_return(&shost->host_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001465 if (atomic_read(&shost->host_blocked) > 0) {
Christoph Hellwig74665012014-01-22 15:29:29 +01001466 if (busy)
1467 goto starved;
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /*
1470 * unblock after host_blocked iterates to zero
1471 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001472 if (atomic_dec_return(&shost->host_blocked) > 0)
Christoph Hellwig74665012014-01-22 15:29:29 +01001473 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001474
1475 SCSI_LOG_MLQUEUE(3,
1476 shost_printk(KERN_INFO, shost,
1477 "unblocking host at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Christoph Hellwig74665012014-01-22 15:29:29 +01001479
1480 if (shost->can_queue > 0 && busy >= shost->can_queue)
1481 goto starved;
1482 if (shost->host_self_blocked)
1483 goto starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 /* We're OK to process the command, so we can't be starved */
Christoph Hellwig74665012014-01-22 15:29:29 +01001486 if (!list_empty(&sdev->starved_entry)) {
1487 spin_lock_irq(shost->host_lock);
1488 if (!list_empty(&sdev->starved_entry))
1489 list_del_init(&sdev->starved_entry);
1490 spin_unlock_irq(shost->host_lock);
1491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Christoph Hellwig74665012014-01-22 15:29:29 +01001493 return 1;
1494
1495starved:
1496 spin_lock_irq(shost->host_lock);
1497 if (list_empty(&sdev->starved_entry))
1498 list_add_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001499 spin_unlock_irq(shost->host_lock);
Christoph Hellwig74665012014-01-22 15:29:29 +01001500out_dec:
1501 atomic_dec(&shost->host_busy);
1502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
1505/*
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001506 * Busy state exporting function for request stacking drivers.
1507 *
1508 * For efficiency, no lock is taken to check the busy state of
1509 * shost/starget/sdev, since the returned value is not guaranteed and
1510 * may be changed after request stacking drivers call the function,
1511 * regardless of taking lock or not.
1512 *
Bart Van Assche67bd9412012-06-29 15:33:22 +00001513 * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
1514 * needs to return 'not busy'. Otherwise, request stacking drivers
1515 * may hold requests forever.
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001516 */
1517static int scsi_lld_busy(struct request_queue *q)
1518{
1519 struct scsi_device *sdev = q->queuedata;
1520 struct Scsi_Host *shost;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001521
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001522 if (blk_queue_dying(q))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001523 return 0;
1524
1525 shost = sdev->host;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001526
Jun'ichi Nomurab7e94a12012-05-22 18:57:17 +09001527 /*
1528 * Ignore host/starget busy state.
1529 * Since block layer does not have a concept of fairness across
1530 * multiple queues, congestion of host/starget needs to be handled
1531 * in SCSI layer.
1532 */
1533 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001534 return 1;
1535
1536 return 0;
1537}
1538
1539/*
James Bottomleye91442b2005-09-09 10:44:16 -05001540 * Kill a request for a dead device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
Jens Axboe165125e2007-07-24 09:28:11 +02001542static void scsi_kill_request(struct request *req, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
James Bottomleye91442b2005-09-09 10:44:16 -05001544 struct scsi_cmnd *cmd = req->special;
Jiri Slaby03b14702009-09-23 16:15:35 +02001545 struct scsi_device *sdev;
1546 struct scsi_target *starget;
1547 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Tejun Heo9934c8c2009-05-08 11:54:16 +09001549 blk_start_request(req);
James Bottomley788ce432005-09-09 13:40:23 -05001550
Hannes Reinecke74571812011-11-09 08:39:24 +01001551 scmd_printk(KERN_INFO, cmd, "killing request\n");
1552
Jiri Slaby03b14702009-09-23 16:15:35 +02001553 sdev = cmd->device;
1554 starget = scsi_target(sdev);
1555 shost = sdev->host;
James Bottomleye91442b2005-09-09 10:44:16 -05001556 scsi_init_cmd_errh(cmd);
1557 cmd->result = DID_NO_CONNECT << 16;
1558 atomic_inc(&cmd->device->iorequest_cnt);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001559
1560 /*
1561 * SCSI request completion path will do scsi_device_unbusy(),
1562 * bump busy counts. To bump the counters, we need to dance
1563 * with the locks as normal issue path does.
1564 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001565 atomic_inc(&sdev->device_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +01001566 atomic_inc(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001567 if (starget->can_queue > 0)
1568 atomic_inc(&starget->target_busy);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001569
Jens Axboe242f9dc2008-09-14 05:55:09 -07001570 blk_complete_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Jens Axboe1aea6432006-01-09 16:03:03 +01001573static void scsi_softirq_done(struct request *rq)
1574{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001575 struct scsi_cmnd *cmd = rq->special;
1576 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
Jens Axboe1aea6432006-01-09 16:03:03 +01001577 int disposition;
1578
1579 INIT_LIST_HEAD(&cmd->eh_entry);
1580
Jens Axboe242f9dc2008-09-14 05:55:09 -07001581 atomic_inc(&cmd->device->iodone_cnt);
1582 if (cmd->result)
1583 atomic_inc(&cmd->device->ioerr_cnt);
1584
Jens Axboe1aea6432006-01-09 16:03:03 +01001585 disposition = scsi_decide_disposition(cmd);
1586 if (disposition != SUCCESS &&
1587 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
1588 sdev_printk(KERN_ERR, cmd->device,
1589 "timing out command, waited %lus\n",
1590 wait_for/HZ);
1591 disposition = SUCCESS;
1592 }
Hannes Reinecke91921e02014-06-25 16:39:59 +02001593
Jens Axboe1aea6432006-01-09 16:03:03 +01001594 scsi_log_completion(cmd, disposition);
1595
1596 switch (disposition) {
1597 case SUCCESS:
1598 scsi_finish_command(cmd);
1599 break;
1600 case NEEDS_RETRY:
Christoph Hellwig596f4822007-01-02 12:56:00 +01001601 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
Jens Axboe1aea6432006-01-09 16:03:03 +01001602 break;
1603 case ADD_TO_MLQUEUE:
1604 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1605 break;
1606 default:
1607 if (!scsi_eh_scmd_add(cmd, 0))
1608 scsi_finish_command(cmd);
1609 }
1610}
1611
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001612/**
Christoph Hellwig82042a22014-09-05 18:23:07 -07001613 * scsi_dispatch_command - Dispatch a command to the low-level driver.
1614 * @cmd: command block we are dispatching.
1615 *
1616 * Return: nonzero return request was rejected and device's queue needs to be
1617 * plugged.
1618 */
1619static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
1620{
1621 struct Scsi_Host *host = cmd->device->host;
1622 int rtn = 0;
1623
1624 atomic_inc(&cmd->device->iorequest_cnt);
1625
1626 /* check if the device is still usable */
1627 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
1628 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
1629 * returns an immediate error upwards, and signals
1630 * that the device is no longer present */
1631 cmd->result = DID_NO_CONNECT << 16;
1632 goto done;
1633 }
1634
1635 /* Check to see if the scsi lld made this device blocked. */
1636 if (unlikely(scsi_device_blocked(cmd->device))) {
1637 /*
1638 * in blocked state, the command is just put back on
1639 * the device queue. The suspend state has already
1640 * blocked the queue so future requests should not
1641 * occur until the device transitions out of the
1642 * suspend state.
1643 */
1644 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1645 "queuecommand : device blocked\n"));
1646 return SCSI_MLQUEUE_DEVICE_BUSY;
1647 }
1648
1649 /* Store the LUN value in cmnd, if needed. */
1650 if (cmd->device->lun_in_cdb)
1651 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
1652 (cmd->device->lun << 5 & 0xe0);
1653
1654 scsi_log_send(cmd);
1655
1656 /*
1657 * Before we queue this command, check if the command
1658 * length exceeds what the host adapter can handle.
1659 */
1660 if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
1661 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1662 "queuecommand : command too long. "
1663 "cdb_size=%d host->max_cmd_len=%d\n",
1664 cmd->cmd_len, cmd->device->host->max_cmd_len));
1665 cmd->result = (DID_ABORT << 16);
1666 goto done;
1667 }
1668
1669 if (unlikely(host->shost_state == SHOST_DEL)) {
1670 cmd->result = (DID_NO_CONNECT << 16);
1671 goto done;
1672
1673 }
1674
1675 trace_scsi_dispatch_cmd_start(cmd);
1676 rtn = host->hostt->queuecommand(host, cmd);
1677 if (rtn) {
1678 trace_scsi_dispatch_cmd_error(cmd, rtn);
1679 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
1680 rtn != SCSI_MLQUEUE_TARGET_BUSY)
1681 rtn = SCSI_MLQUEUE_HOST_BUSY;
1682
1683 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1684 "queuecommand : request rejected\n"));
1685 }
1686
1687 return rtn;
1688 done:
1689 cmd->scsi_done(cmd);
1690 return 0;
1691}
1692
1693/**
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001694 * scsi_done - Invoke completion on finished SCSI command.
1695 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
1696 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
1697 *
1698 * Description: This function is the mid-level's (SCSI Core) interrupt routine,
1699 * which regains ownership of the SCSI command (de facto) from a LLDD, and
1700 * calls blk_complete_request() for further processing.
1701 *
1702 * This function is interrupt context safe.
1703 */
1704static void scsi_done(struct scsi_cmnd *cmd)
1705{
1706 trace_scsi_dispatch_cmd_done(cmd);
1707 blk_complete_request(cmd->request);
1708}
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710/*
1711 * Function: scsi_request_fn()
1712 *
1713 * Purpose: Main strategy routine for SCSI.
1714 *
1715 * Arguments: q - Pointer to actual queue.
1716 *
1717 * Returns: Nothing
1718 *
1719 * Lock status: IO request lock assumed to be held when called.
1720 */
1721static void scsi_request_fn(struct request_queue *q)
Bart Van Assche613be1f2014-02-20 14:20:56 -08001722 __releases(q->queue_lock)
1723 __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 struct scsi_device *sdev = q->queuedata;
1726 struct Scsi_Host *shost;
1727 struct scsi_cmnd *cmd;
1728 struct request *req;
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /*
1731 * To start with, we keep looping until the queue is empty, or until
1732 * the host is no longer able to accept any more requests.
1733 */
1734 shost = sdev->host;
Jens Axboea488e742010-04-16 21:13:15 +02001735 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 int rtn;
1737 /*
1738 * get next queueable request. We do this early to make sure
Hannes Reinecke91921e02014-06-25 16:39:59 +02001739 * that the request is fully prepared even if we cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * accept it.
1741 */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001742 req = blk_peek_request(q);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001743 if (!req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
1745
1746 if (unlikely(!scsi_device_online(sdev))) {
James Bottomley9ccfc752005-10-02 11:45:08 -05001747 sdev_printk(KERN_ERR, sdev,
1748 "rejecting I/O to offline device\n");
James Bottomleye91442b2005-09-09 10:44:16 -05001749 scsi_kill_request(req, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 continue;
1751 }
1752
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001753 if (!scsi_dev_queue_ready(q, sdev))
1754 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 /*
1757 * Remove the request from the request list.
1758 */
1759 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
Tejun Heo9934c8c2009-05-08 11:54:16 +09001760 blk_start_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001762 spin_unlock_irq(q->queue_lock);
James Bottomleye91442b2005-09-09 10:44:16 -05001763 cmd = req->special;
1764 if (unlikely(cmd == NULL)) {
1765 printk(KERN_CRIT "impossible request in %s.\n"
1766 "please mail a stack trace to "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001767 "linux-scsi@vger.kernel.org\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001768 __func__);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001769 blk_dump_rq_flags(req, "foo");
James Bottomleye91442b2005-09-09 10:44:16 -05001770 BUG();
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Mike Christieecefe8a2008-07-11 19:50:35 -05001773 /*
1774 * We hit this when the driver is using a host wide
1775 * tag map. For device level tag maps the queue_depth check
1776 * in the device ready fn would prevent us from trying
1777 * to allocate a tag. Since the map is a shared host resource
1778 * we add the dev to the starved list so it eventually gets
1779 * a run when a tag is freed.
1780 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001781 if (blk_queue_tagged(q) && !(req->rq_flags & RQF_QUEUED)) {
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001782 spin_lock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001783 if (list_empty(&sdev->starved_entry))
1784 list_add_tail(&sdev->starved_entry,
1785 &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001786 spin_unlock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001787 goto not_ready;
1788 }
1789
Mike Christief0c0a372008-08-17 15:24:38 -05001790 if (!scsi_target_queue_ready(shost, sdev))
1791 goto not_ready;
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (!scsi_host_queue_ready(q, shost, sdev))
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001794 goto host_not_ready;
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001795
1796 if (sdev->simple_tags)
1797 cmd->flags |= SCMD_TAGGED;
1798 else
1799 cmd->flags &= ~SCMD_TAGGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /*
1802 * Finally, initialize any error handling parameters, and set up
1803 * the timers for timeouts.
1804 */
1805 scsi_init_cmd_errh(cmd);
1806
1807 /*
1808 * Dispatch the command to the low-level driver.
1809 */
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001810 cmd->scsi_done = scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 rtn = scsi_dispatch_cmd(cmd);
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001812 if (rtn) {
1813 scsi_queue_insert(cmd, rtn);
1814 spin_lock_irq(q->queue_lock);
Jens Axboea488e742010-04-16 21:13:15 +02001815 goto out_delay;
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001816 }
1817 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819
Bart Van Assche613be1f2014-02-20 14:20:56 -08001820 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001822 host_not_ready:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001823 if (scsi_target(sdev)->can_queue > 0)
1824 atomic_dec(&scsi_target(sdev)->target_busy);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001825 not_ready:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /*
1827 * lock q, handle tag, requeue req, and decrement device_busy. We
1828 * must return with queue_lock held.
1829 *
1830 * Decrementing device_busy without checking it is OK, as all such
1831 * cases (host limits or settings) should run the queue at some
1832 * later time.
1833 */
1834 spin_lock_irq(q->queue_lock);
1835 blk_requeue_request(q, req);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001836 atomic_dec(&sdev->device_busy);
Jens Axboea488e742010-04-16 21:13:15 +02001837out_delay:
Guenter Roeck480cadc2014-08-10 05:54:25 -07001838 if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
Jens Axboea488e742010-04-16 21:13:15 +02001839 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
Christoph Hellwigd2852032014-01-17 12:06:53 +01001842static inline int prep_to_mq(int ret)
1843{
1844 switch (ret) {
1845 case BLKPREP_OK:
Omar Sandoval2868f132016-11-15 11:11:59 -08001846 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001847 case BLKPREP_DEFER:
1848 return BLK_MQ_RQ_QUEUE_BUSY;
1849 default:
1850 return BLK_MQ_RQ_QUEUE_ERROR;
1851 }
1852}
1853
1854static int scsi_mq_prep_fn(struct request *req)
1855{
1856 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1857 struct scsi_device *sdev = req->q->queuedata;
1858 struct Scsi_Host *shost = sdev->host;
1859 unsigned char *sense_buf = cmd->sense_buffer;
1860 struct scatterlist *sg;
1861
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001862 /* zero out the cmd, except for the embedded scsi_request */
1863 memset((char *)cmd + sizeof(cmd->req), 0,
1864 sizeof(*cmd) - sizeof(cmd->req));
Christoph Hellwigd2852032014-01-17 12:06:53 +01001865
1866 req->special = cmd;
1867
1868 cmd->request = req;
1869 cmd->device = sdev;
1870 cmd->sense_buffer = sense_buf;
1871
1872 cmd->tag = req->tag;
1873
Christoph Hellwigd2852032014-01-17 12:06:53 +01001874 cmd->prot_op = SCSI_PROT_NORMAL;
1875
1876 INIT_LIST_HEAD(&cmd->list);
1877 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1878 cmd->jiffies_at_alloc = jiffies;
1879
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +05301880 if (shost->use_cmd_list) {
1881 spin_lock_irq(&sdev->list_lock);
1882 list_add_tail(&cmd->list, &sdev->cmd_list);
1883 spin_unlock_irq(&sdev->list_lock);
1884 }
Christoph Hellwigd2852032014-01-17 12:06:53 +01001885
1886 sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
1887 cmd->sdb.table.sgl = sg;
1888
1889 if (scsi_host_get_prot(shost)) {
1890 cmd->prot_sdb = (void *)sg +
Tony Battersby120bb3e2014-12-08 17:20:52 -05001891 min_t(unsigned int,
Ming Lin65e86172016-04-04 14:48:10 -07001892 shost->sg_tablesize, SG_CHUNK_SIZE) *
Tony Battersby120bb3e2014-12-08 17:20:52 -05001893 sizeof(struct scatterlist);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001894 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
1895
1896 cmd->prot_sdb->table.sgl =
1897 (struct scatterlist *)(cmd->prot_sdb + 1);
1898 }
1899
1900 if (blk_bidi_rq(req)) {
1901 struct request *next_rq = req->next_rq;
1902 struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq);
1903
1904 memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer));
1905 bidi_sdb->table.sgl =
1906 (struct scatterlist *)(bidi_sdb + 1);
1907
1908 next_rq->special = bidi_sdb;
1909 }
1910
Christoph Hellwigfe052522014-09-22 15:59:31 +02001911 blk_mq_start_request(req);
1912
Christoph Hellwigd2852032014-01-17 12:06:53 +01001913 return scsi_setup_cmnd(sdev, req);
1914}
1915
1916static void scsi_mq_done(struct scsi_cmnd *cmd)
1917{
1918 trace_scsi_dispatch_cmd_done(cmd);
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001919 blk_mq_complete_request(cmd->request, cmd->request->errors);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001920}
1921
Jens Axboe74c45052014-10-29 11:14:52 -06001922static int scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
1923 const struct blk_mq_queue_data *bd)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001924{
Jens Axboe74c45052014-10-29 11:14:52 -06001925 struct request *req = bd->rq;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001926 struct request_queue *q = req->q;
1927 struct scsi_device *sdev = q->queuedata;
1928 struct Scsi_Host *shost = sdev->host;
1929 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1930 int ret;
1931 int reason;
1932
1933 ret = prep_to_mq(scsi_prep_state_check(sdev, req));
Omar Sandoval2868f132016-11-15 11:11:59 -08001934 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001935 goto out;
1936
1937 ret = BLK_MQ_RQ_QUEUE_BUSY;
1938 if (!get_device(&sdev->sdev_gendev))
1939 goto out;
1940
1941 if (!scsi_dev_queue_ready(q, sdev))
1942 goto out_put_device;
1943 if (!scsi_target_queue_ready(shost, sdev))
1944 goto out_dec_device_busy;
1945 if (!scsi_host_queue_ready(q, shost, sdev))
1946 goto out_dec_target_busy;
1947
Christoph Hellwige8064022016-10-20 15:12:13 +02001948 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001949 ret = prep_to_mq(scsi_mq_prep_fn(req));
Omar Sandoval2868f132016-11-15 11:11:59 -08001950 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001951 goto out_dec_host_busy;
Christoph Hellwige8064022016-10-20 15:12:13 +02001952 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwigfe052522014-09-22 15:59:31 +02001953 } else {
1954 blk_mq_start_request(req);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001955 }
1956
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001957 if (sdev->simple_tags)
1958 cmd->flags |= SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001959 else
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001960 cmd->flags &= ~SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001961
Christoph Hellwigd2852032014-01-17 12:06:53 +01001962 scsi_init_cmd_errh(cmd);
1963 cmd->scsi_done = scsi_mq_done;
1964
1965 reason = scsi_dispatch_cmd(cmd);
1966 if (reason) {
1967 scsi_set_blocked(cmd, reason);
1968 ret = BLK_MQ_RQ_QUEUE_BUSY;
1969 goto out_dec_host_busy;
1970 }
1971
1972 return BLK_MQ_RQ_QUEUE_OK;
1973
1974out_dec_host_busy:
1975 atomic_dec(&shost->host_busy);
1976out_dec_target_busy:
1977 if (scsi_target(sdev)->can_queue > 0)
1978 atomic_dec(&scsi_target(sdev)->target_busy);
1979out_dec_device_busy:
1980 atomic_dec(&sdev->device_busy);
1981out_put_device:
1982 put_device(&sdev->sdev_gendev);
1983out:
1984 switch (ret) {
1985 case BLK_MQ_RQ_QUEUE_BUSY:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001986 if (atomic_read(&sdev->device_busy) == 0 &&
1987 !scsi_device_blocked(sdev))
1988 blk_mq_delay_queue(hctx, SCSI_QUEUE_DELAY);
1989 break;
1990 case BLK_MQ_RQ_QUEUE_ERROR:
1991 /*
1992 * Make sure to release all allocated ressources when
1993 * we hit an error, as we will never see this command
1994 * again.
1995 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001996 if (req->rq_flags & RQF_DONTPREP)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001997 scsi_mq_uninit_cmd(cmd);
1998 break;
1999 default:
2000 break;
2001 }
2002 return ret;
2003}
2004
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002005static enum blk_eh_timer_return scsi_timeout(struct request *req,
2006 bool reserved)
2007{
2008 if (reserved)
2009 return BLK_EH_RESET_TIMER;
2010 return scsi_times_out(req);
2011}
2012
Christoph Hellwigd2852032014-01-17 12:06:53 +01002013static int scsi_init_request(void *data, struct request *rq,
2014 unsigned int hctx_idx, unsigned int request_idx,
2015 unsigned int numa_node)
2016{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002017 struct Scsi_Host *shost = data;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002018 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2019
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002020 cmd->sense_buffer =
2021 scsi_alloc_sense_buffer(shost, GFP_KERNEL, numa_node);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002022 if (!cmd->sense_buffer)
2023 return -ENOMEM;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002024 cmd->req.sense = cmd->sense_buffer;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002025 return 0;
2026}
2027
2028static void scsi_exit_request(void *data, struct request *rq,
2029 unsigned int hctx_idx, unsigned int request_idx)
2030{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002031 struct Scsi_Host *shost = data;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002032 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2033
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002034 scsi_free_sense_buffer(shost, cmd->sense_buffer);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002035}
2036
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002037static int scsi_map_queues(struct blk_mq_tag_set *set)
2038{
2039 struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
2040
2041 if (shost->hostt->map_queues)
2042 return shost->hostt->map_queues(shost);
2043 return blk_mq_map_queues(set);
2044}
2045
Christoph Hellwigf1bea552014-04-15 12:26:54 +02002046static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 struct device *host_dev;
2049 u64 bounce_limit = 0xffffffff;
2050
2051 if (shost->unchecked_isa_dma)
2052 return BLK_BOUNCE_ISA;
2053 /*
2054 * Platforms with virtual-DMA translation
2055 * hardware have no practical limit.
2056 */
2057 if (!PCI_DMA_BUS_IS_PHYS)
2058 return BLK_BOUNCE_ANY;
2059
2060 host_dev = scsi_get_device(shost);
2061 if (host_dev && host_dev->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +00002062 bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 return bounce_limit;
2065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Christoph Hellwigd48777a62017-01-02 21:52:10 +03002067void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Lin Ming6f381fa2012-04-12 13:50:38 +08002069 struct device *dev = shost->dma_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Jens Axboea8474ce2007-08-07 09:02:51 +02002071 /*
2072 * this limit is imposed by hardware restrictions
2073 */
Martin K. Petersen8a783622010-02-26 00:20:39 -05002074 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
Ming Lin65e86172016-04-04 14:48:10 -07002075 SG_MAX_SEGMENTS));
Jens Axboea8474ce2007-08-07 09:02:51 +02002076
Martin K. Petersen13f05c82010-09-10 20:50:10 +02002077 if (scsi_host_prot_dma(shost)) {
2078 shost->sg_prot_tablesize =
2079 min_not_zero(shost->sg_prot_tablesize,
2080 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
2081 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
2082 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
2083 }
2084
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002085 blk_queue_max_hw_sectors(q, shost->max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
2087 blk_queue_segment_boundary(q, shost->dma_boundary);
FUJITA Tomonori99c84db2008-02-04 22:28:17 -08002088 dma_set_seg_boundary(dev, shost->dma_boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
FUJITA Tomonori860ac562008-02-04 22:28:05 -08002090 blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (!shost->use_clustering)
Martin K. Petersene692cb62010-12-01 19:41:49 +01002093 q->limits.cluster = 0;
James Bottomley465ff312008-01-01 10:00:10 -06002094
2095 /*
2096 * set a reasonable default alignment on word boundaries: the
2097 * host and device may alter it using
2098 * blk_queue_update_dma_alignment() later.
2099 */
2100 blk_queue_dma_alignment(q, 0x03);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002101}
Christoph Hellwigd48777a62017-01-02 21:52:10 +03002102EXPORT_SYMBOL_GPL(__scsi_init_queue);
James Bottomley465ff312008-01-01 10:00:10 -06002103
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002104static int scsi_init_rq(struct request_queue *q, struct request *rq, gfp_t gfp)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002105{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002106 struct Scsi_Host *shost = q->rq_alloc_data;
2107 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002108
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002109 memset(cmd, 0, sizeof(*cmd));
2110
2111 cmd->sense_buffer = scsi_alloc_sense_buffer(shost, gfp, NUMA_NO_NODE);
2112 if (!cmd->sense_buffer)
2113 goto fail;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002114 cmd->req.sense = cmd->sense_buffer;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002115
2116 if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
2117 cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp);
2118 if (!cmd->prot_sdb)
2119 goto fail_free_sense;
2120 }
2121
2122 return 0;
2123
2124fail_free_sense:
2125 scsi_free_sense_buffer(shost, cmd->sense_buffer);
2126fail:
2127 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002129
2130static void scsi_exit_rq(struct request_queue *q, struct request *rq)
2131{
2132 struct Scsi_Host *shost = q->rq_alloc_data;
2133 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2134
2135 if (cmd->prot_sdb)
2136 kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
2137 scsi_free_sense_buffer(shost, cmd->sense_buffer);
2138}
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002139
2140struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
2141{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002142 struct Scsi_Host *shost = sdev->host;
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002143 struct request_queue *q;
2144
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002145 q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002146 if (!q)
2147 return NULL;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002148 q->cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
2149 q->rq_alloc_data = shost;
2150 q->request_fn = scsi_request_fn;
2151 q->init_rq_fn = scsi_init_rq;
2152 q->exit_rq_fn = scsi_exit_rq;
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002153
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002154 if (blk_init_allocated_queue(q) < 0) {
2155 blk_cleanup_queue(q);
2156 return NULL;
2157 }
2158
2159 __scsi_init_queue(shost, q);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002160 blk_queue_prep_rq(q, scsi_prep_fn);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02002161 blk_queue_unprep_rq(q, scsi_unprep_fn);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002162 blk_queue_softirq_done(q, scsi_softirq_done);
Jens Axboe242f9dc2008-09-14 05:55:09 -07002163 blk_queue_rq_timed_out(q, scsi_times_out);
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04002164 blk_queue_lld_busy(q, scsi_lld_busy);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002165 return q;
2166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Christoph Hellwigd2852032014-01-17 12:06:53 +01002168static struct blk_mq_ops scsi_mq_ops = {
Christoph Hellwigd2852032014-01-17 12:06:53 +01002169 .queue_rq = scsi_queue_rq,
2170 .complete = scsi_softirq_done,
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002171 .timeout = scsi_timeout,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002172 .init_request = scsi_init_request,
2173 .exit_request = scsi_exit_request,
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002174 .map_queues = scsi_map_queues,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002175};
2176
2177struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
2178{
2179 sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
2180 if (IS_ERR(sdev->request_queue))
2181 return NULL;
2182
2183 sdev->request_queue->queuedata = sdev;
2184 __scsi_init_queue(sdev->host, sdev->request_queue);
2185 return sdev->request_queue;
2186}
2187
2188int scsi_mq_setup_tags(struct Scsi_Host *shost)
2189{
2190 unsigned int cmd_size, sgl_size, tbl_size;
2191
2192 tbl_size = shost->sg_tablesize;
Ming Lin65e86172016-04-04 14:48:10 -07002193 if (tbl_size > SG_CHUNK_SIZE)
2194 tbl_size = SG_CHUNK_SIZE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002195 sgl_size = tbl_size * sizeof(struct scatterlist);
2196 cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
2197 if (scsi_host_get_prot(shost))
2198 cmd_size += sizeof(struct scsi_data_buffer) + sgl_size;
2199
2200 memset(&shost->tag_set, 0, sizeof(shost->tag_set));
2201 shost->tag_set.ops = &scsi_mq_ops;
Bart Van Asscheefec4b92014-10-30 14:45:36 +01002202 shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002203 shost->tag_set.queue_depth = shost->can_queue;
2204 shost->tag_set.cmd_size = cmd_size;
2205 shost->tag_set.numa_node = NUMA_NO_NODE;
2206 shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Shaohua Li24391c02015-01-23 14:18:00 -07002207 shost->tag_set.flags |=
2208 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002209 shost->tag_set.driver_data = shost;
2210
2211 return blk_mq_alloc_tag_set(&shost->tag_set);
2212}
2213
2214void scsi_mq_destroy_tags(struct Scsi_Host *shost)
2215{
2216 blk_mq_free_tag_set(&shost->tag_set);
2217}
2218
Hannes Reinecke857de6e2017-02-17 09:02:45 +01002219/**
2220 * scsi_device_from_queue - return sdev associated with a request_queue
2221 * @q: The request queue to return the sdev from
2222 *
2223 * Return the sdev associated with a request queue or NULL if the
2224 * request_queue does not reference a SCSI device.
2225 */
2226struct scsi_device *scsi_device_from_queue(struct request_queue *q)
2227{
2228 struct scsi_device *sdev = NULL;
2229
2230 if (q->mq_ops) {
2231 if (q->mq_ops == &scsi_mq_ops)
2232 sdev = q->queuedata;
2233 } else if (q->request_fn == scsi_request_fn)
2234 sdev = q->queuedata;
2235 if (!sdev || !get_device(&sdev->sdev_gendev))
2236 sdev = NULL;
2237
2238 return sdev;
2239}
2240EXPORT_SYMBOL_GPL(scsi_device_from_queue);
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242/*
2243 * Function: scsi_block_requests()
2244 *
2245 * Purpose: Utility function used by low-level drivers to prevent further
2246 * commands from being queued to the device.
2247 *
2248 * Arguments: shost - Host in question
2249 *
2250 * Returns: Nothing
2251 *
2252 * Lock status: No locks are assumed held.
2253 *
2254 * Notes: There is no timer nor any other means by which the requests
2255 * get unblocked other than the low-level driver calling
2256 * scsi_unblock_requests().
2257 */
2258void scsi_block_requests(struct Scsi_Host *shost)
2259{
2260 shost->host_self_blocked = 1;
2261}
2262EXPORT_SYMBOL(scsi_block_requests);
2263
2264/*
2265 * Function: scsi_unblock_requests()
2266 *
2267 * Purpose: Utility function used by low-level drivers to allow further
2268 * commands from being queued to the device.
2269 *
2270 * Arguments: shost - Host in question
2271 *
2272 * Returns: Nothing
2273 *
2274 * Lock status: No locks are assumed held.
2275 *
2276 * Notes: There is no timer nor any other means by which the requests
2277 * get unblocked other than the low-level driver calling
2278 * scsi_unblock_requests().
2279 *
2280 * This is done as an API function so that changes to the
2281 * internals of the scsi mid-layer won't require wholesale
2282 * changes to drivers that use this feature.
2283 */
2284void scsi_unblock_requests(struct Scsi_Host *shost)
2285{
2286 shost->host_self_blocked = 0;
2287 scsi_run_host_queues(shost);
2288}
2289EXPORT_SYMBOL(scsi_unblock_requests);
2290
2291int __init scsi_init_queue(void)
2292{
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002293 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
2294 sizeof(struct scsi_data_buffer),
2295 0, 0, NULL);
2296 if (!scsi_sdb_cache) {
2297 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
FUJITA Tomonorif0787272008-12-14 01:23:45 +09002298 return -ENOMEM;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02002299 }
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return 0;
2302}
2303
2304void scsi_exit_queue(void)
2305{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002306 kmem_cache_destroy(scsi_sense_cache);
2307 kmem_cache_destroy(scsi_sense_isadma_cache);
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002308 kmem_cache_destroy(scsi_sdb_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
James Bottomley5baba832006-03-18 14:10:35 -06002310
2311/**
2312 * scsi_mode_select - issue a mode select
2313 * @sdev: SCSI device to be queried
2314 * @pf: Page format bit (1 == standard, 0 == vendor specific)
2315 * @sp: Save page bit (0 == don't save, 1 == save)
2316 * @modepage: mode page being requested
2317 * @buffer: request buffer (may not be smaller than eight bytes)
2318 * @len: length of request buffer.
2319 * @timeout: command timeout
2320 * @retries: number of retries before failing
2321 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002322 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley5baba832006-03-18 14:10:35 -06002323 * must be SCSI_SENSE_BUFFERSIZE big.
2324 *
2325 * Returns zero if successful; negative error number or scsi
2326 * status on error
2327 *
2328 */
2329int
2330scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
2331 unsigned char *buffer, int len, int timeout, int retries,
2332 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2333{
2334 unsigned char cmd[10];
2335 unsigned char *real_buffer;
2336 int ret;
2337
2338 memset(cmd, 0, sizeof(cmd));
2339 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2340
2341 if (sdev->use_10_for_ms) {
2342 if (len > 65535)
2343 return -EINVAL;
2344 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2345 if (!real_buffer)
2346 return -ENOMEM;
2347 memcpy(real_buffer + 8, buffer, len);
2348 len += 8;
2349 real_buffer[0] = 0;
2350 real_buffer[1] = 0;
2351 real_buffer[2] = data->medium_type;
2352 real_buffer[3] = data->device_specific;
2353 real_buffer[4] = data->longlba ? 0x01 : 0;
2354 real_buffer[5] = 0;
2355 real_buffer[6] = data->block_descriptor_length >> 8;
2356 real_buffer[7] = data->block_descriptor_length;
2357
2358 cmd[0] = MODE_SELECT_10;
2359 cmd[7] = len >> 8;
2360 cmd[8] = len;
2361 } else {
2362 if (len > 255 || data->block_descriptor_length > 255 ||
2363 data->longlba)
2364 return -EINVAL;
2365
2366 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2367 if (!real_buffer)
2368 return -ENOMEM;
2369 memcpy(real_buffer + 4, buffer, len);
2370 len += 4;
2371 real_buffer[0] = 0;
2372 real_buffer[1] = data->medium_type;
2373 real_buffer[2] = data->device_specific;
2374 real_buffer[3] = data->block_descriptor_length;
2375
2376
2377 cmd[0] = MODE_SELECT;
2378 cmd[4] = len;
2379 }
2380
2381 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002382 sshdr, timeout, retries, NULL);
James Bottomley5baba832006-03-18 14:10:35 -06002383 kfree(real_buffer);
2384 return ret;
2385}
2386EXPORT_SYMBOL_GPL(scsi_mode_select);
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388/**
Rob Landleyeb448202007-11-03 13:30:39 -05002389 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
James Bottomley1cf72692005-08-28 11:27:01 -05002390 * @sdev: SCSI device to be queried
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 * @dbd: set if mode sense will allow block descriptors to be returned
2392 * @modepage: mode page being requested
2393 * @buffer: request buffer (may not be smaller than eight bytes)
2394 * @len: length of request buffer.
2395 * @timeout: command timeout
2396 * @retries: number of retries before failing
2397 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002398 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley1cf72692005-08-28 11:27:01 -05002399 * must be SCSI_SENSE_BUFFERSIZE big.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 *
2401 * Returns zero if unsuccessful, or the header offset (either 4
2402 * or 8 depending on whether a six or ten byte command was
2403 * issued) if successful.
Rob Landleyeb448202007-11-03 13:30:39 -05002404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405int
James Bottomley1cf72692005-08-28 11:27:01 -05002406scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 unsigned char *buffer, int len, int timeout, int retries,
James Bottomley5baba832006-03-18 14:10:35 -06002408 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 unsigned char cmd[12];
2411 int use_10_for_ms;
2412 int header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002413 int result, retry_count = retries;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002414 struct scsi_sense_hdr my_sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 memset(data, 0, sizeof(*data));
2417 memset(&cmd[0], 0, 12);
2418 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
2419 cmd[2] = modepage;
2420
James Bottomleyea73a9f2005-08-28 11:33:52 -05002421 /* caller might not be interested in sense, but we need it */
2422 if (!sshdr)
2423 sshdr = &my_sshdr;
2424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 retry:
James Bottomley1cf72692005-08-28 11:27:01 -05002426 use_10_for_ms = sdev->use_10_for_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 if (use_10_for_ms) {
2429 if (len < 8)
2430 len = 8;
2431
2432 cmd[0] = MODE_SENSE_10;
2433 cmd[8] = len;
2434 header_length = 8;
2435 } else {
2436 if (len < 4)
2437 len = 4;
2438
2439 cmd[0] = MODE_SENSE;
2440 cmd[4] = len;
2441 header_length = 4;
2442 }
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 memset(buffer, 0, len);
2445
James Bottomley1cf72692005-08-28 11:27:01 -05002446 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002447 sshdr, timeout, retries, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 /* This code looks awful: what it's doing is making sure an
2450 * ILLEGAL REQUEST sense return identifies the actual command
2451 * byte as the problem. MODE_SENSE commands can return
2452 * ILLEGAL REQUEST if the code page isn't supported */
2453
James Bottomley1cf72692005-08-28 11:27:01 -05002454 if (use_10_for_ms && !scsi_status_is_good(result) &&
2455 (driver_byte(result) & DRIVER_SENSE)) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05002456 if (scsi_sense_valid(sshdr)) {
2457 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2458 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /*
2460 * Invalid command operation code
2461 */
James Bottomley1cf72692005-08-28 11:27:01 -05002462 sdev->use_10_for_ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 goto retry;
2464 }
2465 }
2466 }
2467
James Bottomley1cf72692005-08-28 11:27:01 -05002468 if(scsi_status_is_good(result)) {
Al Viro6d73c852006-02-23 02:03:16 +01002469 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2470 (modepage == 6 || modepage == 8))) {
2471 /* Initio breakage? */
2472 header_length = 0;
2473 data->length = 13;
2474 data->medium_type = 0;
2475 data->device_specific = 0;
2476 data->longlba = 0;
2477 data->block_descriptor_length = 0;
2478 } else if(use_10_for_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 data->length = buffer[0]*256 + buffer[1] + 2;
2480 data->medium_type = buffer[2];
2481 data->device_specific = buffer[3];
2482 data->longlba = buffer[4] & 0x01;
2483 data->block_descriptor_length = buffer[6]*256
2484 + buffer[7];
2485 } else {
2486 data->length = buffer[0] + 1;
2487 data->medium_type = buffer[1];
2488 data->device_specific = buffer[2];
2489 data->block_descriptor_length = buffer[3];
2490 }
Al Viro6d73c852006-02-23 02:03:16 +01002491 data->header_length = header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002492 } else if ((status_byte(result) == CHECK_CONDITION) &&
2493 scsi_sense_valid(sshdr) &&
2494 sshdr->sense_key == UNIT_ATTENTION && retry_count) {
2495 retry_count--;
2496 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498
James Bottomley1cf72692005-08-28 11:27:01 -05002499 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501EXPORT_SYMBOL(scsi_mode_sense);
2502
James Bottomley001aac22007-12-02 19:10:40 +02002503/**
2504 * scsi_test_unit_ready - test if unit is ready
2505 * @sdev: scsi device to change the state of.
2506 * @timeout: command timeout
2507 * @retries: number of retries before failing
Christoph Hellwig74a78eb2017-02-14 20:15:57 +01002508 * @sshdr: outpout pointer for decoded sense information.
James Bottomley001aac22007-12-02 19:10:40 +02002509 *
2510 * Returns zero if unsuccessful or an error if TUR failed. For
Tejun Heo9f8a2c22010-12-08 20:57:40 +01002511 * removable media, UNIT_ATTENTION sets ->changed flag.
James Bottomley001aac22007-12-02 19:10:40 +02002512 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513int
James Bottomley001aac22007-12-02 19:10:40 +02002514scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
Christoph Hellwig74a78eb2017-02-14 20:15:57 +01002515 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 char cmd[] = {
2518 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2519 };
2520 int result;
James Bottomley001aac22007-12-02 19:10:40 +02002521
James Bottomley001aac22007-12-02 19:10:40 +02002522 /* try to eat the UNIT_ATTENTION if there are enough retries */
2523 do {
2524 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002525 timeout, retries, NULL);
James Bottomley32c356d2008-08-20 00:58:13 +02002526 if (sdev->removable && scsi_sense_valid(sshdr) &&
2527 sshdr->sense_key == UNIT_ATTENTION)
2528 sdev->changed = 1;
2529 } while (scsi_sense_valid(sshdr) &&
2530 sshdr->sense_key == UNIT_ATTENTION && --retries);
James Bottomley001aac22007-12-02 19:10:40 +02002531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return result;
2533}
2534EXPORT_SYMBOL(scsi_test_unit_ready);
2535
2536/**
Rob Landleyeb448202007-11-03 13:30:39 -05002537 * scsi_device_set_state - Take the given device through the device state model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 * @sdev: scsi device to change the state of.
2539 * @state: state to change to.
2540 *
2541 * Returns zero if unsuccessful or an error if the requested
2542 * transition is illegal.
Rob Landleyeb448202007-11-03 13:30:39 -05002543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544int
2545scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2546{
2547 enum scsi_device_state oldstate = sdev->sdev_state;
2548
2549 if (state == oldstate)
2550 return 0;
2551
2552 switch (state) {
2553 case SDEV_CREATED:
James Bottomley6f4267e2008-08-22 16:53:31 -05002554 switch (oldstate) {
2555 case SDEV_CREATED_BLOCK:
2556 break;
2557 default:
2558 goto illegal;
2559 }
2560 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 case SDEV_RUNNING:
2563 switch (oldstate) {
2564 case SDEV_CREATED:
2565 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002566 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 case SDEV_QUIESCE:
2568 case SDEV_BLOCK:
2569 break;
2570 default:
2571 goto illegal;
2572 }
2573 break;
2574
2575 case SDEV_QUIESCE:
2576 switch (oldstate) {
2577 case SDEV_RUNNING:
2578 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002579 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 break;
2581 default:
2582 goto illegal;
2583 }
2584 break;
2585
2586 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002587 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 switch (oldstate) {
2589 case SDEV_CREATED:
2590 case SDEV_RUNNING:
2591 case SDEV_QUIESCE:
2592 case SDEV_BLOCK:
2593 break;
2594 default:
2595 goto illegal;
2596 }
2597 break;
2598
2599 case SDEV_BLOCK:
2600 switch (oldstate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 case SDEV_RUNNING:
James Bottomley6f4267e2008-08-22 16:53:31 -05002602 case SDEV_CREATED_BLOCK:
2603 break;
2604 default:
2605 goto illegal;
2606 }
2607 break;
2608
2609 case SDEV_CREATED_BLOCK:
2610 switch (oldstate) {
2611 case SDEV_CREATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 break;
2613 default:
2614 goto illegal;
2615 }
2616 break;
2617
2618 case SDEV_CANCEL:
2619 switch (oldstate) {
2620 case SDEV_CREATED:
2621 case SDEV_RUNNING:
Alan Stern9ea72902006-06-23 14:25:34 -04002622 case SDEV_QUIESCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002624 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 case SDEV_BLOCK:
2626 break;
2627 default:
2628 goto illegal;
2629 }
2630 break;
2631
2632 case SDEV_DEL:
2633 switch (oldstate) {
Brian King309bd272006-06-27 11:10:43 -05002634 case SDEV_CREATED:
2635 case SDEV_RUNNING:
2636 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002637 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 case SDEV_CANCEL:
Bart Van Assche0516c082013-07-02 15:06:33 +02002639 case SDEV_CREATED_BLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 break;
2641 default:
2642 goto illegal;
2643 }
2644 break;
2645
2646 }
2647 sdev->sdev_state = state;
2648 return 0;
2649
2650 illegal:
Hannes Reinecke91921e02014-06-25 16:39:59 +02002651 SCSI_LOG_ERROR_RECOVERY(1,
James Bottomley9ccfc752005-10-02 11:45:08 -05002652 sdev_printk(KERN_ERR, sdev,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002653 "Illegal state transition %s->%s",
James Bottomley9ccfc752005-10-02 11:45:08 -05002654 scsi_device_state_name(oldstate),
2655 scsi_device_state_name(state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 );
2657 return -EINVAL;
2658}
2659EXPORT_SYMBOL(scsi_device_set_state);
2660
2661/**
Jeff Garzika341cd02007-10-29 17:15:22 -04002662 * sdev_evt_emit - emit a single SCSI device uevent
2663 * @sdev: associated SCSI device
2664 * @evt: event to emit
2665 *
2666 * Send a single uevent (scsi_event) to the associated scsi_device.
2667 */
2668static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2669{
2670 int idx = 0;
2671 char *envp[3];
2672
2673 switch (evt->evt_type) {
2674 case SDEV_EVT_MEDIA_CHANGE:
2675 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2676 break;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002677 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
Hannes Reinecked3d32892016-02-19 09:17:16 +01002678 scsi_rescan_device(&sdev->sdev_gendev);
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002679 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2680 break;
2681 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2682 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2683 break;
2684 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2685 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2686 break;
2687 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2688 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2689 break;
2690 case SDEV_EVT_LUN_CHANGE_REPORTED:
2691 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2692 break;
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002693 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2694 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
2695 break;
Jeff Garzika341cd02007-10-29 17:15:22 -04002696 default:
2697 /* do nothing */
2698 break;
2699 }
2700
2701 envp[idx++] = NULL;
2702
2703 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2704}
2705
2706/**
2707 * sdev_evt_thread - send a uevent for each scsi event
2708 * @work: work struct for scsi_device
2709 *
2710 * Dispatch queued events to their associated scsi_device kobjects
2711 * as uevents.
2712 */
2713void scsi_evt_thread(struct work_struct *work)
2714{
2715 struct scsi_device *sdev;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002716 enum scsi_device_event evt_type;
Jeff Garzika341cd02007-10-29 17:15:22 -04002717 LIST_HEAD(event_list);
2718
2719 sdev = container_of(work, struct scsi_device, event_work);
2720
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002721 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2722 if (test_and_clear_bit(evt_type, sdev->pending_events))
2723 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2724
Jeff Garzika341cd02007-10-29 17:15:22 -04002725 while (1) {
2726 struct scsi_event *evt;
2727 struct list_head *this, *tmp;
2728 unsigned long flags;
2729
2730 spin_lock_irqsave(&sdev->list_lock, flags);
2731 list_splice_init(&sdev->event_list, &event_list);
2732 spin_unlock_irqrestore(&sdev->list_lock, flags);
2733
2734 if (list_empty(&event_list))
2735 break;
2736
2737 list_for_each_safe(this, tmp, &event_list) {
2738 evt = list_entry(this, struct scsi_event, node);
2739 list_del(&evt->node);
2740 scsi_evt_emit(sdev, evt);
2741 kfree(evt);
2742 }
2743 }
2744}
2745
2746/**
2747 * sdev_evt_send - send asserted event to uevent thread
2748 * @sdev: scsi_device event occurred on
2749 * @evt: event to send
2750 *
2751 * Assert scsi device event asynchronously.
2752 */
2753void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2754{
2755 unsigned long flags;
2756
Kay Sievers4d1566e2008-03-19 13:04:47 +01002757#if 0
2758 /* FIXME: currently this check eliminates all media change events
2759 * for polled devices. Need to update to discriminate between AN
2760 * and polled events */
Jeff Garzika341cd02007-10-29 17:15:22 -04002761 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2762 kfree(evt);
2763 return;
2764 }
Kay Sievers4d1566e2008-03-19 13:04:47 +01002765#endif
Jeff Garzika341cd02007-10-29 17:15:22 -04002766
2767 spin_lock_irqsave(&sdev->list_lock, flags);
2768 list_add_tail(&evt->node, &sdev->event_list);
2769 schedule_work(&sdev->event_work);
2770 spin_unlock_irqrestore(&sdev->list_lock, flags);
2771}
2772EXPORT_SYMBOL_GPL(sdev_evt_send);
2773
2774/**
2775 * sdev_evt_alloc - allocate a new scsi event
2776 * @evt_type: type of event to allocate
2777 * @gfpflags: GFP flags for allocation
2778 *
2779 * Allocates and returns a new scsi_event.
2780 */
2781struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2782 gfp_t gfpflags)
2783{
2784 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2785 if (!evt)
2786 return NULL;
2787
2788 evt->evt_type = evt_type;
2789 INIT_LIST_HEAD(&evt->node);
2790
2791 /* evt_type-specific initialization, if any */
2792 switch (evt_type) {
2793 case SDEV_EVT_MEDIA_CHANGE:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002794 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2795 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2796 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2797 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2798 case SDEV_EVT_LUN_CHANGE_REPORTED:
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002799 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
Jeff Garzika341cd02007-10-29 17:15:22 -04002800 default:
2801 /* do nothing */
2802 break;
2803 }
2804
2805 return evt;
2806}
2807EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2808
2809/**
2810 * sdev_evt_send_simple - send asserted event to uevent thread
2811 * @sdev: scsi_device event occurred on
2812 * @evt_type: type of event to send
2813 * @gfpflags: GFP flags for allocation
2814 *
2815 * Assert scsi device event asynchronously, given an event type.
2816 */
2817void sdev_evt_send_simple(struct scsi_device *sdev,
2818 enum scsi_device_event evt_type, gfp_t gfpflags)
2819{
2820 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2821 if (!evt) {
2822 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2823 evt_type);
2824 return;
2825 }
2826
2827 sdev_evt_send(sdev, evt);
2828}
2829EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2830
2831/**
Bart Van Assche669f0442016-11-22 16:17:13 -08002832 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
2833 * @sdev: SCSI device to count the number of scsi_request_fn() callers for.
2834 */
2835static int scsi_request_fn_active(struct scsi_device *sdev)
2836{
2837 struct request_queue *q = sdev->request_queue;
2838 int request_fn_active;
2839
2840 WARN_ON_ONCE(sdev->host->use_blk_mq);
2841
2842 spin_lock_irq(q->queue_lock);
2843 request_fn_active = q->request_fn_active;
2844 spin_unlock_irq(q->queue_lock);
2845
2846 return request_fn_active;
2847}
2848
2849/**
2850 * scsi_wait_for_queuecommand() - wait for ongoing queuecommand() calls
2851 * @sdev: SCSI device pointer.
2852 *
2853 * Wait until the ongoing shost->hostt->queuecommand() calls that are
2854 * invoked from scsi_request_fn() have finished.
2855 */
2856static void scsi_wait_for_queuecommand(struct scsi_device *sdev)
2857{
2858 WARN_ON_ONCE(sdev->host->use_blk_mq);
2859
2860 while (scsi_request_fn_active(sdev))
2861 msleep(20);
2862}
2863
2864/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 * scsi_device_quiesce - Block user issued commands.
2866 * @sdev: scsi device to quiesce.
2867 *
2868 * This works by trying to transition to the SDEV_QUIESCE state
2869 * (which must be a legal transition). When the device is in this
2870 * state, only special requests will be accepted, all others will
2871 * be deferred. Since special requests may also be requeued requests,
2872 * a successful return doesn't guarantee the device will be
2873 * totally quiescent.
2874 *
2875 * Must be called with user context, may sleep.
2876 *
2877 * Returns zero if unsuccessful or an error if not.
Rob Landleyeb448202007-11-03 13:30:39 -05002878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879int
2880scsi_device_quiesce(struct scsi_device *sdev)
2881{
2882 int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2883 if (err)
2884 return err;
2885
2886 scsi_run_queue(sdev->request_queue);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02002887 while (atomic_read(&sdev->device_busy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 msleep_interruptible(200);
2889 scsi_run_queue(sdev->request_queue);
2890 }
2891 return 0;
2892}
2893EXPORT_SYMBOL(scsi_device_quiesce);
2894
2895/**
2896 * scsi_device_resume - Restart user issued commands to a quiesced device.
2897 * @sdev: scsi device to resume.
2898 *
2899 * Moves the device from quiesced back to running and restarts the
2900 * queues.
2901 *
2902 * Must be called with user context, may sleep.
Rob Landleyeb448202007-11-03 13:30:39 -05002903 */
Dan Williamsa7a20d12012-03-22 17:05:11 -07002904void scsi_device_resume(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
Dan Williamsa7a20d12012-03-22 17:05:11 -07002906 /* check if the device state was mutated prior to resume, and if
2907 * so assume the state is being managed elsewhere (for example
2908 * device deleted during suspend)
2909 */
2910 if (sdev->sdev_state != SDEV_QUIESCE ||
2911 scsi_device_set_state(sdev, SDEV_RUNNING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return;
2913 scsi_run_queue(sdev->request_queue);
2914}
2915EXPORT_SYMBOL(scsi_device_resume);
2916
2917static void
2918device_quiesce_fn(struct scsi_device *sdev, void *data)
2919{
2920 scsi_device_quiesce(sdev);
2921}
2922
2923void
2924scsi_target_quiesce(struct scsi_target *starget)
2925{
2926 starget_for_each_device(starget, NULL, device_quiesce_fn);
2927}
2928EXPORT_SYMBOL(scsi_target_quiesce);
2929
2930static void
2931device_resume_fn(struct scsi_device *sdev, void *data)
2932{
2933 scsi_device_resume(sdev);
2934}
2935
2936void
2937scsi_target_resume(struct scsi_target *starget)
2938{
2939 starget_for_each_device(starget, NULL, device_resume_fn);
2940}
2941EXPORT_SYMBOL(scsi_target_resume);
2942
2943/**
Rob Landleyeb448202007-11-03 13:30:39 -05002944 * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 * @sdev: device to block
2946 *
2947 * Block request made by scsi lld's to temporarily stop all
Bart Van Assche669f0442016-11-22 16:17:13 -08002948 * scsi commands on the specified device. May sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 *
2950 * Returns zero if successful or error if not
2951 *
2952 * Notes:
2953 * This routine transitions the device to the SDEV_BLOCK state
2954 * (which must be a legal transition). When the device is in this
2955 * state, all commands are deferred until the scsi lld reenables
2956 * the device with scsi_device_unblock or device_block_tmo fires.
Bart Van Assche669f0442016-11-22 16:17:13 -08002957 *
2958 * To do: avoid that scsi_send_eh_cmnd() calls queuecommand() after
2959 * scsi_internal_device_block() has blocked a SCSI device and also
2960 * remove the rport mutex lock and unlock calls from srp_queuecommand().
Rob Landleyeb448202007-11-03 13:30:39 -05002961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962int
2963scsi_internal_device_block(struct scsi_device *sdev)
2964{
Jens Axboe165125e2007-07-24 09:28:11 +02002965 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 unsigned long flags;
2967 int err = 0;
2968
2969 err = scsi_device_set_state(sdev, SDEV_BLOCK);
James Bottomley6f4267e2008-08-22 16:53:31 -05002970 if (err) {
2971 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2972
2973 if (err)
2974 return err;
2975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 /*
2978 * The device has transitioned to SDEV_BLOCK. Stop the
2979 * block layer from calling the midlayer with this device's
2980 * request queue.
2981 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01002982 if (q->mq_ops) {
Bart Van Assche7dbbf0f2016-11-22 16:17:50 -08002983 blk_mq_quiesce_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002984 } else {
2985 spin_lock_irqsave(q->queue_lock, flags);
2986 blk_stop_queue(q);
2987 spin_unlock_irqrestore(q->queue_lock, flags);
Bart Van Assche669f0442016-11-22 16:17:13 -08002988 scsi_wait_for_queuecommand(sdev);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991 return 0;
2992}
2993EXPORT_SYMBOL_GPL(scsi_internal_device_block);
2994
2995/**
2996 * scsi_internal_device_unblock - resume a device after a block request
2997 * @sdev: device to resume
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002998 * @new_state: state to set devices to after unblocking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 *
3000 * Called by scsi lld's or the midlayer to restart the device queue
3001 * for the previously suspended scsi device. Called from interrupt or
3002 * normal process context.
3003 *
3004 * Returns zero if successful or error if not.
3005 *
3006 * Notes:
3007 * This routine transitions the device to the SDEV_RUNNING state
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003008 * or to one of the offline states (which must be a legal transition)
Mike Christied0754982012-05-17 23:56:58 -05003009 * allowing the midlayer to goose the queue for this device.
Rob Landleyeb448202007-11-03 13:30:39 -05003010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011int
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003012scsi_internal_device_unblock(struct scsi_device *sdev,
3013 enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
Jens Axboe165125e2007-07-24 09:28:11 +02003015 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 unsigned long flags;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003017
3018 /*
3019 * Try to transition the scsi device to SDEV_RUNNING or one of the
3020 * offlined states and goose the device queue if successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 */
Vikas Chaudhary0e580762012-08-09 04:51:30 -04003022 if ((sdev->sdev_state == SDEV_BLOCK) ||
3023 (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003024 sdev->sdev_state = new_state;
3025 else if (sdev->sdev_state == SDEV_CREATED_BLOCK) {
3026 if (new_state == SDEV_TRANSPORT_OFFLINE ||
3027 new_state == SDEV_OFFLINE)
3028 sdev->sdev_state = new_state;
3029 else
3030 sdev->sdev_state = SDEV_CREATED;
3031 } else if (sdev->sdev_state != SDEV_CANCEL &&
Mike Christie986fe6c2010-10-06 03:10:59 -05003032 sdev->sdev_state != SDEV_OFFLINE)
Takahiro Yasui5c10e632009-04-29 12:13:02 -04003033 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Christoph Hellwigd2852032014-01-17 12:06:53 +01003035 if (q->mq_ops) {
3036 blk_mq_start_stopped_hw_queues(q, false);
3037 } else {
3038 spin_lock_irqsave(q->queue_lock, flags);
3039 blk_start_queue(q);
3040 spin_unlock_irqrestore(q->queue_lock, flags);
3041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
3043 return 0;
3044}
3045EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
3046
3047static void
3048device_block(struct scsi_device *sdev, void *data)
3049{
3050 scsi_internal_device_block(sdev);
3051}
3052
3053static int
3054target_block(struct device *dev, void *data)
3055{
3056 if (scsi_is_target_device(dev))
3057 starget_for_each_device(to_scsi_target(dev), NULL,
3058 device_block);
3059 return 0;
3060}
3061
3062void
3063scsi_target_block(struct device *dev)
3064{
3065 if (scsi_is_target_device(dev))
3066 starget_for_each_device(to_scsi_target(dev), NULL,
3067 device_block);
3068 else
3069 device_for_each_child(dev, NULL, target_block);
3070}
3071EXPORT_SYMBOL_GPL(scsi_target_block);
3072
3073static void
3074device_unblock(struct scsi_device *sdev, void *data)
3075{
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003076 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
3079static int
3080target_unblock(struct device *dev, void *data)
3081{
3082 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003083 starget_for_each_device(to_scsi_target(dev), data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 device_unblock);
3085 return 0;
3086}
3087
3088void
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003089scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
3091 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003092 starget_for_each_device(to_scsi_target(dev), &new_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 device_unblock);
3094 else
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003095 device_for_each_child(dev, &new_state, target_unblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097EXPORT_SYMBOL_GPL(scsi_target_unblock);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003098
3099/**
3100 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
Rob Landleyeb448202007-11-03 13:30:39 -05003101 * @sgl: scatter-gather list
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003102 * @sg_count: number of segments in sg
3103 * @offset: offset in bytes into sg, on return offset into the mapped area
3104 * @len: bytes to map, on return number of bytes mapped
3105 *
3106 * Returns virtual address of the start of the mapped page
3107 */
Jens Axboec6132da2007-10-16 11:08:49 +02003108void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003109 size_t *offset, size_t *len)
3110{
3111 int i;
3112 size_t sg_len = 0, len_complete = 0;
Jens Axboec6132da2007-10-16 11:08:49 +02003113 struct scatterlist *sg;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003114 struct page *page;
3115
Andrew Morton22cfefb2007-02-05 16:39:03 -08003116 WARN_ON(!irqs_disabled());
3117
Jens Axboec6132da2007-10-16 11:08:49 +02003118 for_each_sg(sgl, sg, sg_count, i) {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003119 len_complete = sg_len; /* Complete sg-entries */
Jens Axboec6132da2007-10-16 11:08:49 +02003120 sg_len += sg->length;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003121 if (sg_len > *offset)
3122 break;
3123 }
3124
3125 if (unlikely(i == sg_count)) {
Andrew Morton169e1a22006-04-18 21:09:08 -07003126 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
3127 "elements %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003128 __func__, sg_len, *offset, sg_count);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003129 WARN_ON(1);
3130 return NULL;
3131 }
3132
3133 /* Offset starting from the beginning of first page in this sg-entry */
Jens Axboec6132da2007-10-16 11:08:49 +02003134 *offset = *offset - len_complete + sg->offset;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003135
3136 /* Assumption: contiguous pages can be accessed as "page + i" */
Jens Axboe45711f12007-10-22 21:19:53 +02003137 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003138 *offset &= ~PAGE_MASK;
3139
3140 /* Bytes in this sg-entry from *offset to the end of the page */
3141 sg_len = PAGE_SIZE - *offset;
3142 if (*len > sg_len)
3143 *len = sg_len;
3144
Cong Wang77dfce02011-11-25 23:14:23 +08003145 return kmap_atomic(page);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003146}
3147EXPORT_SYMBOL(scsi_kmap_atomic_sg);
3148
3149/**
Rob Landleyeb448202007-11-03 13:30:39 -05003150 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003151 * @virt: virtual address to be unmapped
3152 */
3153void scsi_kunmap_atomic_sg(void *virt)
3154{
Cong Wang77dfce02011-11-25 23:14:23 +08003155 kunmap_atomic(virt);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003156}
3157EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
Aaron Lu6f4c8272013-01-23 15:09:32 +08003158
3159void sdev_disable_disk_events(struct scsi_device *sdev)
3160{
3161 atomic_inc(&sdev->disk_events_disable_depth);
3162}
3163EXPORT_SYMBOL(sdev_disable_disk_events);
3164
3165void sdev_enable_disk_events(struct scsi_device *sdev)
3166{
3167 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
3168 return;
3169 atomic_dec(&sdev->disk_events_disable_depth);
3170}
3171EXPORT_SYMBOL(sdev_enable_disk_events);
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003172
3173/**
3174 * scsi_vpd_lun_id - return a unique device identification
3175 * @sdev: SCSI device
3176 * @id: buffer for the identification
3177 * @id_len: length of the buffer
3178 *
3179 * Copies a unique device identification into @id based
3180 * on the information in the VPD page 0x83 of the device.
3181 * The string will be formatted as a SCSI name string.
3182 *
3183 * Returns the length of the identification or error on failure.
3184 * If the identifier is longer than the supplied buffer the actual
3185 * identifier length is returned and the buffer is not zero-padded.
3186 */
3187int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
3188{
3189 u8 cur_id_type = 0xff;
3190 u8 cur_id_size = 0;
3191 unsigned char *d, *cur_id_str;
3192 unsigned char __rcu *vpd_pg83;
3193 int id_size = -EINVAL;
3194
3195 rcu_read_lock();
3196 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3197 if (!vpd_pg83) {
3198 rcu_read_unlock();
3199 return -ENXIO;
3200 }
3201
3202 /*
3203 * Look for the correct descriptor.
3204 * Order of preference for lun descriptor:
3205 * - SCSI name string
3206 * - NAA IEEE Registered Extended
3207 * - EUI-64 based 16-byte
3208 * - EUI-64 based 12-byte
3209 * - NAA IEEE Registered
3210 * - NAA IEEE Extended
Hannes Reinecked2308232016-05-09 09:14:29 +02003211 * - T10 Vendor ID
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003212 * as longer descriptors reduce the likelyhood
3213 * of identification clashes.
3214 */
3215
3216 /* The id string must be at least 20 bytes + terminating NULL byte */
3217 if (id_len < 21) {
3218 rcu_read_unlock();
3219 return -EINVAL;
3220 }
3221
3222 memset(id, 0, id_len);
3223 d = vpd_pg83 + 4;
3224 while (d < vpd_pg83 + sdev->vpd_pg83_len) {
3225 /* Skip designators not referring to the LUN */
3226 if ((d[1] & 0x30) != 0x00)
3227 goto next_desig;
3228
3229 switch (d[1] & 0xf) {
Hannes Reinecked2308232016-05-09 09:14:29 +02003230 case 0x1:
3231 /* T10 Vendor ID */
3232 if (cur_id_size > d[3])
3233 break;
3234 /* Prefer anything */
3235 if (cur_id_type > 0x01 && cur_id_type != 0xff)
3236 break;
3237 cur_id_size = d[3];
3238 if (cur_id_size + 4 > id_len)
3239 cur_id_size = id_len - 4;
3240 cur_id_str = d + 4;
3241 cur_id_type = d[1] & 0xf;
3242 id_size = snprintf(id, id_len, "t10.%*pE",
3243 cur_id_size, cur_id_str);
3244 break;
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003245 case 0x2:
3246 /* EUI-64 */
3247 if (cur_id_size > d[3])
3248 break;
3249 /* Prefer NAA IEEE Registered Extended */
3250 if (cur_id_type == 0x3 &&
3251 cur_id_size == d[3])
3252 break;
3253 cur_id_size = d[3];
3254 cur_id_str = d + 4;
3255 cur_id_type = d[1] & 0xf;
3256 switch (cur_id_size) {
3257 case 8:
3258 id_size = snprintf(id, id_len,
3259 "eui.%8phN",
3260 cur_id_str);
3261 break;
3262 case 12:
3263 id_size = snprintf(id, id_len,
3264 "eui.%12phN",
3265 cur_id_str);
3266 break;
3267 case 16:
3268 id_size = snprintf(id, id_len,
3269 "eui.%16phN",
3270 cur_id_str);
3271 break;
3272 default:
3273 cur_id_size = 0;
3274 break;
3275 }
3276 break;
3277 case 0x3:
3278 /* NAA */
3279 if (cur_id_size > d[3])
3280 break;
3281 cur_id_size = d[3];
3282 cur_id_str = d + 4;
3283 cur_id_type = d[1] & 0xf;
3284 switch (cur_id_size) {
3285 case 8:
3286 id_size = snprintf(id, id_len,
3287 "naa.%8phN",
3288 cur_id_str);
3289 break;
3290 case 16:
3291 id_size = snprintf(id, id_len,
3292 "naa.%16phN",
3293 cur_id_str);
3294 break;
3295 default:
3296 cur_id_size = 0;
3297 break;
3298 }
3299 break;
3300 case 0x8:
3301 /* SCSI name string */
3302 if (cur_id_size + 4 > d[3])
3303 break;
3304 /* Prefer others for truncated descriptor */
3305 if (cur_id_size && d[3] > id_len)
3306 break;
3307 cur_id_size = id_size = d[3];
3308 cur_id_str = d + 4;
3309 cur_id_type = d[1] & 0xf;
3310 if (cur_id_size >= id_len)
3311 cur_id_size = id_len - 1;
3312 memcpy(id, cur_id_str, cur_id_size);
3313 /* Decrease priority for truncated descriptor */
3314 if (cur_id_size != id_size)
3315 cur_id_size = 6;
3316 break;
3317 default:
3318 break;
3319 }
3320next_desig:
3321 d += d[3] + 4;
3322 }
3323 rcu_read_unlock();
3324
3325 return id_size;
3326}
3327EXPORT_SYMBOL(scsi_vpd_lun_id);
Hannes Reineckea8aa3972015-12-01 10:16:57 +01003328
3329/*
3330 * scsi_vpd_tpg_id - return a target port group identifier
3331 * @sdev: SCSI device
3332 *
3333 * Returns the Target Port Group identifier from the information
3334 * froom VPD page 0x83 of the device.
3335 *
3336 * Returns the identifier or error on failure.
3337 */
3338int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
3339{
3340 unsigned char *d;
3341 unsigned char __rcu *vpd_pg83;
3342 int group_id = -EAGAIN, rel_port = -1;
3343
3344 rcu_read_lock();
3345 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3346 if (!vpd_pg83) {
3347 rcu_read_unlock();
3348 return -ENXIO;
3349 }
3350
3351 d = sdev->vpd_pg83 + 4;
3352 while (d < sdev->vpd_pg83 + sdev->vpd_pg83_len) {
3353 switch (d[1] & 0xf) {
3354 case 0x4:
3355 /* Relative target port */
3356 rel_port = get_unaligned_be16(&d[6]);
3357 break;
3358 case 0x5:
3359 /* Target port group */
3360 group_id = get_unaligned_be16(&d[6]);
3361 break;
3362 default:
3363 break;
3364 }
3365 d += d[3] + 4;
3366 }
3367 rcu_read_unlock();
3368
3369 if (group_id >= 0 && rel_id && rel_port != -1)
3370 *rel_id = rel_port;
3371
3372 return group_id;
3373}
3374EXPORT_SYMBOL(scsi_vpd_tpg_id);