blob: 90f65c8f487aa3594655e4e43e06723b1344754b [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);
44
45static 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
216static int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
James Bottomley33aa6872005-08-28 11:31:14 -0500217 int data_direction, void *buffer, unsigned bufflen,
Martin K. Petersen2bfad212014-04-09 22:20:48 -0400218 unsigned char *sense, int timeout, int retries, u64 flags,
Christoph Hellwige8064022016-10-20 15:12:13 +0200219 req_flags_t rq_flags, int *resid)
James Bottomley39216032005-06-15 18:48:29 -0500220{
221 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100222 struct scsi_request *rq;
James Bottomley39216032005-06-15 18:48:29 -0500223 int ret = DRIVER_ERROR << 24;
224
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100225 req = blk_get_request(sdev->request_queue,
226 data_direction == DMA_TO_DEVICE ?
227 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, __GFP_RECLAIM);
Joe Lawrencea492f072014-08-28 08:15:21 -0600228 if (IS_ERR(req))
James Bottomleybfe159a2011-07-07 15:45:40 -0500229 return ret;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100230 rq = scsi_req(req);
231 scsi_req_init(req);
James Bottomley39216032005-06-15 18:48:29 -0500232
233 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
Mel Gorman71baba42015-11-06 16:28:28 -0800234 buffer, bufflen, __GFP_RECLAIM))
James Bottomley39216032005-06-15 18:48:29 -0500235 goto out;
236
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100237 rq->cmd_len = COMMAND_SIZE(cmd[0]);
238 memcpy(rq->cmd, cmd, rq->cmd_len);
Mike Christie17e01f22005-11-11 05:31:37 -0600239 req->retries = retries;
James Bottomley39216032005-06-15 18:48:29 -0500240 req->timeout = timeout;
Christoph Hellwige8064022016-10-20 15:12:13 +0200241 req->cmd_flags |= flags;
242 req->rq_flags |= rq_flags | RQF_QUIET | RQF_PREEMPT;
James Bottomley39216032005-06-15 18:48:29 -0500243
244 /*
245 * head injection *required* here otherwise quiesce won't work
246 */
247 blk_execute_rq(req->q, NULL, req, 1);
248
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400249 /*
250 * Some devices (USB mass-storage in particular) may transfer
251 * garbage data together with a residue indicating that the data
252 * is invalid. Prevent the garbage from being misinterpreted
253 * and prevent security leaks by zeroing out the excess data.
254 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100255 if (unlikely(rq->resid_len > 0 && rq->resid_len <= bufflen))
256 memset(buffer + (bufflen - rq->resid_len), 0, rq->resid_len);
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400257
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900258 if (resid)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100259 *resid = rq->resid_len;
260 if (sense && rq->sense_len)
261 memcpy(sense, rq->sense, SCSI_SENSE_BUFFERSIZE);
James Bottomley39216032005-06-15 18:48:29 -0500262 ret = req->errors;
263 out:
264 blk_put_request(req);
265
266 return ret;
267}
Christoph Hellwige8064022016-10-20 15:12:13 +0200268
269/**
270 * scsi_execute - insert request and wait for the result
271 * @sdev: scsi device
272 * @cmd: scsi command
273 * @data_direction: data direction
274 * @buffer: data buffer
275 * @bufflen: len of buffer
276 * @sense: optional sense buffer
277 * @timeout: request timeout in seconds
278 * @retries: number of times to retry request
279 * @flags: or into request flags;
280 * @resid: optional residual length
281 *
282 * returns the req->errors value which is the scsi_cmnd result
283 * field.
284 */
285int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
286 int data_direction, void *buffer, unsigned bufflen,
287 unsigned char *sense, int timeout, int retries, u64 flags,
288 int *resid)
289{
290 return __scsi_execute(sdev, cmd, data_direction, buffer, bufflen, sense,
291 timeout, retries, flags, 0, resid);
292}
James Bottomley33aa6872005-08-28 11:31:14 -0500293EXPORT_SYMBOL(scsi_execute);
James Bottomley39216032005-06-15 18:48:29 -0500294
Lin Ming9b214932013-03-23 11:42:25 +0800295int scsi_execute_req_flags(struct scsi_device *sdev, const unsigned char *cmd,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500296 int data_direction, void *buffer, unsigned bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900297 struct scsi_sense_hdr *sshdr, int timeout, int retries,
Christoph Hellwige8064022016-10-20 15:12:13 +0200298 int *resid, u64 flags, req_flags_t rq_flags)
James Bottomleyea73a9f2005-08-28 11:33:52 -0500299{
300 char *sense = NULL;
akpm@osdl.org1ccb48b2005-06-26 00:12:51 -0700301 int result;
302
James Bottomleyea73a9f2005-08-28 11:33:52 -0500303 if (sshdr) {
Jes Sorensen24669f752006-01-16 10:31:18 -0500304 sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500305 if (!sense)
306 return DRIVER_ERROR << 24;
James Bottomleyea73a9f2005-08-28 11:33:52 -0500307 }
Christoph Hellwige8064022016-10-20 15:12:13 +0200308 result = __scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
309 sense, timeout, retries, flags, rq_flags, resid);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500310 if (sshdr)
James Bottomleye5143852005-08-09 11:55:36 -0500311 scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500312
313 kfree(sense);
314 return result;
315}
Lin Ming9b214932013-03-23 11:42:25 +0800316EXPORT_SYMBOL(scsi_execute_req_flags);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * Function: scsi_init_cmd_errh()
320 *
321 * Purpose: Initialize cmd fields related to error handling.
322 *
323 * Arguments: cmd - command that is ready to be queued.
324 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * Notes: This function has the job of initializing a number of
326 * fields related to error handling. Typically this will
327 * be called once for each command, as required.
328 */
Christoph Hellwig631c2282006-07-08 20:42:15 +0200329static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 cmd->serial_number = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200332 scsi_set_resid(cmd, 0);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900333 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (cmd->cmd_len == 0)
Boaz Harroshdb4742d2008-04-30 11:27:26 +0300335 cmd->cmd_len = scsi_command_size(cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338void scsi_device_unbusy(struct scsi_device *sdev)
339{
340 struct Scsi_Host *shost = sdev->host;
Mike Christief0c0a372008-08-17 15:24:38 -0500341 struct scsi_target *starget = scsi_target(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unsigned long flags;
343
Christoph Hellwig74665012014-01-22 15:29:29 +0100344 atomic_dec(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100345 if (starget->can_queue > 0)
346 atomic_dec(&starget->target_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +0100347
James Bottomley939647e2005-09-18 15:05:20 -0500348 if (unlikely(scsi_host_in_recovery(shost) &&
Christoph Hellwig74665012014-01-22 15:29:29 +0100349 (shost->host_failed || shost->host_eh_scheduled))) {
350 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 scsi_eh_wakeup(shost);
Christoph Hellwig74665012014-01-22 15:29:29 +0100352 spin_unlock_irqrestore(shost->host_lock, flags);
353 }
354
Christoph Hellwig71e75c92014-04-11 19:07:01 +0200355 atomic_dec(&sdev->device_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Christoph Hellwigd2852032014-01-17 12:06:53 +0100358static void scsi_kick_queue(struct request_queue *q)
359{
360 if (q->mq_ops)
361 blk_mq_start_hw_queues(q);
362 else
363 blk_run_queue(q);
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/*
367 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
368 * and call blk_run_queue for all the scsi_devices on the target -
369 * including current_sdev first.
370 *
371 * Called with *no* scsi locks held.
372 */
373static void scsi_single_lun_run(struct scsi_device *current_sdev)
374{
375 struct Scsi_Host *shost = current_sdev->host;
376 struct scsi_device *sdev, *tmp;
377 struct scsi_target *starget = scsi_target(current_sdev);
378 unsigned long flags;
379
380 spin_lock_irqsave(shost->host_lock, flags);
381 starget->starget_sdev_user = NULL;
382 spin_unlock_irqrestore(shost->host_lock, flags);
383
384 /*
385 * Call blk_run_queue for all LUNs on the target, starting with
386 * current_sdev. We race with others (to set starget_sdev_user),
387 * but in most cases, we will be first. Ideally, each LU on the
388 * target would get some limited time or requests on the target.
389 */
Christoph Hellwigd2852032014-01-17 12:06:53 +0100390 scsi_kick_queue(current_sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 spin_lock_irqsave(shost->host_lock, flags);
393 if (starget->starget_sdev_user)
394 goto out;
395 list_for_each_entry_safe(sdev, tmp, &starget->devices,
396 same_target_siblings) {
397 if (sdev == current_sdev)
398 continue;
399 if (scsi_device_get(sdev))
400 continue;
401
402 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100403 scsi_kick_queue(sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 spin_lock_irqsave(shost->host_lock, flags);
405
406 scsi_device_put(sdev);
407 }
408 out:
409 spin_unlock_irqrestore(shost->host_lock, flags);
410}
411
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100412static inline bool scsi_device_is_busy(struct scsi_device *sdev)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400413{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100414 if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
415 return true;
416 if (atomic_read(&sdev->device_blocked) > 0)
417 return true;
418 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400419}
420
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100421static inline bool scsi_target_is_busy(struct scsi_target *starget)
Mike Christief0c0a372008-08-17 15:24:38 -0500422{
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100423 if (starget->can_queue > 0) {
424 if (atomic_read(&starget->target_busy) >= starget->can_queue)
425 return true;
426 if (atomic_read(&starget->target_blocked) > 0)
427 return true;
428 }
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100429 return false;
Mike Christief0c0a372008-08-17 15:24:38 -0500430}
431
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100432static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400433{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100434 if (shost->can_queue > 0 &&
435 atomic_read(&shost->host_busy) >= shost->can_queue)
436 return true;
437 if (atomic_read(&shost->host_blocked) > 0)
438 return true;
439 if (shost->host_self_blocked)
440 return true;
441 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400442}
443
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800444static void scsi_starved_list_run(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Mike Christie2a3a59e2008-11-11 13:42:35 -0600446 LIST_HEAD(starved_list);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800447 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 unsigned long flags;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 spin_lock_irqsave(shost->host_lock, flags);
Mike Christie2a3a59e2008-11-11 13:42:35 -0600451 list_splice_init(&shost->starved_list, &starved_list);
452
453 while (!list_empty(&starved_list)) {
James Bottomleye2eb7242013-07-02 15:05:26 +0200454 struct request_queue *slq;
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
457 * As long as shost is accepting commands and we have
458 * starved queues, call blk_run_queue. scsi_request_fn
459 * drops the queue_lock and can add us back to the
460 * starved_list.
461 *
462 * host_lock protects the starved_list and starved_entry.
463 * scsi_request_fn must get the host_lock before checking
464 * or modifying starved_list or starved_entry.
465 */
Mike Christie2a3a59e2008-11-11 13:42:35 -0600466 if (scsi_host_is_busy(shost))
Mike Christief0c0a372008-08-17 15:24:38 -0500467 break;
Mike Christief0c0a372008-08-17 15:24:38 -0500468
Mike Christie2a3a59e2008-11-11 13:42:35 -0600469 sdev = list_entry(starved_list.next,
470 struct scsi_device, starved_entry);
471 list_del_init(&sdev->starved_entry);
Mike Christief0c0a372008-08-17 15:24:38 -0500472 if (scsi_target_is_busy(scsi_target(sdev))) {
473 list_move_tail(&sdev->starved_entry,
474 &shost->starved_list);
475 continue;
476 }
477
James Bottomleye2eb7242013-07-02 15:05:26 +0200478 /*
479 * Once we drop the host lock, a racing scsi_remove_device()
480 * call may remove the sdev from the starved list and destroy
481 * it and the queue. Mitigate by taking a reference to the
482 * queue and never touching the sdev again after we drop the
483 * host lock. Note: if __scsi_remove_device() invokes
484 * blk_cleanup_queue() before the queue is run from this
485 * function then blk_run_queue() will return immediately since
486 * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
487 */
488 slq = sdev->request_queue;
489 if (!blk_get_queue(slq))
490 continue;
491 spin_unlock_irqrestore(shost->host_lock, flags);
492
Christoph Hellwigd2852032014-01-17 12:06:53 +0100493 scsi_kick_queue(slq);
James Bottomleye2eb7242013-07-02 15:05:26 +0200494 blk_put_queue(slq);
495
496 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Mike Christie2a3a59e2008-11-11 13:42:35 -0600498 /* put any unprocessed entries back */
499 list_splice(&starved_list, &shost->starved_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800501}
502
503/*
504 * Function: scsi_run_queue()
505 *
506 * Purpose: Select a proper request queue to serve next
507 *
508 * Arguments: q - last request's queue
509 *
510 * Returns: Nothing
511 *
512 * Notes: The previous command was completely finished, start
513 * a new one if possible.
514 */
515static void scsi_run_queue(struct request_queue *q)
516{
517 struct scsi_device *sdev = q->queuedata;
518
519 if (scsi_target(sdev)->single_lun)
520 scsi_single_lun_run(sdev);
521 if (!list_empty(&sdev->host->starved_list))
522 scsi_starved_list_run(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Christoph Hellwigd2852032014-01-17 12:06:53 +0100524 if (q->mq_ops)
525 blk_mq_start_stopped_hw_queues(q, false);
526 else
527 blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Jens Axboe9937a5e2011-05-17 11:04:44 +0200530void scsi_requeue_run_queue(struct work_struct *work)
531{
532 struct scsi_device *sdev;
533 struct request_queue *q;
534
535 sdev = container_of(work, struct scsi_device, requeue_work);
536 q = sdev->request_queue;
537 scsi_run_queue(q);
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/*
541 * Function: scsi_requeue_command()
542 *
543 * Purpose: Handle post-processing of completed commands.
544 *
545 * Arguments: q - queue to operate on
546 * cmd - command that may need to be requeued.
547 *
548 * Returns: Nothing
549 *
550 * Notes: After command completion, there may be blocks left
551 * over which weren't finished by the previous command
552 * this can be for a number of reasons - the main one is
553 * I/O errors in the middle of the request, in which case
554 * we need to request the blocks that come after the bad
555 * sector.
James Bottomleye91442b2005-09-09 10:44:16 -0500556 * Notes: Upon return, cmd is a stale pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
558static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
559{
Bart Van Assche940f5d42012-06-29 15:34:26 +0000560 struct scsi_device *sdev = cmd->device;
James Bottomleye91442b2005-09-09 10:44:16 -0500561 struct request *req = cmd->request;
Tejun Heo 283369c2005-04-24 02:06:36 -0500562 unsigned long flags;
563
Tejun Heo 283369c2005-04-24 02:06:36 -0500564 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig134997a2014-02-20 14:20:58 -0800565 blk_unprep_request(req);
566 req->special = NULL;
567 scsi_put_command(cmd);
James Bottomleye91442b2005-09-09 10:44:16 -0500568 blk_requeue_request(q, req);
Tejun Heo 283369c2005-04-24 02:06:36 -0500569 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 scsi_run_queue(q);
Bart Van Assche940f5d42012-06-29 15:34:26 +0000572
573 put_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576void scsi_run_host_queues(struct Scsi_Host *shost)
577{
578 struct scsi_device *sdev;
579
580 shost_for_each_device(sdev, shost)
581 scsi_run_queue(sdev->request_queue);
582}
583
Christoph Hellwigd2852032014-01-17 12:06:53 +0100584static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
585{
Christoph Hellwig57292b52017-01-31 16:57:29 +0100586 if (!blk_rq_is_passthrough(cmd->request)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +0100587 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
588
589 if (drv->uninit_command)
590 drv->uninit_command(cmd);
591 }
592}
593
594static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd)
595{
Ming Lin91dbc082016-04-04 14:48:07 -0700596 struct scsi_data_buffer *sdb;
597
Christoph Hellwigd2852032014-01-17 12:06:53 +0100598 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700599 sg_free_table_chained(&cmd->sdb.table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700600 if (cmd->request->next_rq) {
601 sdb = cmd->request->next_rq->special;
602 if (sdb)
Ming Lin001d63b2016-04-04 14:48:09 -0700603 sg_free_table_chained(&sdb->table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700604 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100605 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700606 sg_free_table_chained(&cmd->prot_sdb->table, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100607}
608
609static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
610{
611 struct scsi_device *sdev = cmd->device;
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +0530612 struct Scsi_Host *shost = sdev->host;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100613 unsigned long flags;
614
Christoph Hellwigd2852032014-01-17 12:06:53 +0100615 scsi_mq_free_sgtables(cmd);
616 scsi_uninit_cmd(cmd);
617
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +0530618 if (shost->use_cmd_list) {
619 BUG_ON(list_empty(&cmd->list));
620 spin_lock_irqsave(&sdev->list_lock, flags);
621 list_del_init(&cmd->list);
622 spin_unlock_irqrestore(&sdev->list_lock, flags);
623 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/*
627 * Function: scsi_release_buffers()
628 *
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200629 * Purpose: Free resources allocate for a scsi_command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
631 * Arguments: cmd - command that we are bailing.
632 *
633 * Lock status: Assumed that no lock is held upon entry.
634 *
635 * Returns: Nothing
636 *
637 * Notes: In the event that an upper level driver rejects a
638 * command, we must release resources allocated during
639 * the __init_io() function. Primarily this would involve
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200640 * the scatter-gather table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
Christoph Hellwigf1bea552014-04-15 12:26:54 +0200642static void scsi_release_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200644 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700645 sg_free_table_chained(&cmd->sdb.table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200646
647 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
648
649 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700650 sg_free_table_chained(&cmd->prot_sdb->table, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200653static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
654{
655 struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special;
656
Ming Lin001d63b2016-04-04 14:48:09 -0700657 sg_free_table_chained(&bidi_sdb->table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200658 kmem_cache_free(scsi_sdb_cache, bidi_sdb);
659 cmd->request->next_rq->special = NULL;
660}
661
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800662static bool scsi_end_request(struct request *req, int error,
663 unsigned int bytes, unsigned int bidi_bytes)
664{
665 struct scsi_cmnd *cmd = req->special;
666 struct scsi_device *sdev = cmd->device;
667 struct request_queue *q = sdev->request_queue;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800668
669 if (blk_update_request(req, error, bytes))
670 return true;
671
672 /* Bidi request must be completed as a whole */
673 if (unlikely(bidi_bytes) &&
674 blk_update_request(req->next_rq, error, bidi_bytes))
675 return true;
676
677 if (blk_queue_add_random(q))
678 add_disk_randomness(req->rq_disk);
679
Christoph Hellwigd2852032014-01-17 12:06:53 +0100680 if (req->mq_ctx) {
681 /*
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700682 * In the MQ case the command gets freed by __blk_mq_end_request,
Christoph Hellwigd2852032014-01-17 12:06:53 +0100683 * so we have to do all cleanup that depends on it earlier.
684 *
685 * We also can't kick the queues from irq context, so we
686 * will have to defer it to a workqueue.
687 */
688 scsi_mq_uninit_cmd(cmd);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800689
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700690 __blk_mq_end_request(req, error);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100691
692 if (scsi_target(sdev)->single_lun ||
693 !list_empty(&sdev->host->starved_list))
694 kblockd_schedule_work(&sdev->requeue_work);
695 else
696 blk_mq_start_stopped_hw_queues(q, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100697 } else {
698 unsigned long flags;
699
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400700 if (bidi_bytes)
701 scsi_release_bidi_buffers(cmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +0300702 scsi_release_buffers(cmd);
703 scsi_put_command(cmd);
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400704
Christoph Hellwigd2852032014-01-17 12:06:53 +0100705 spin_lock_irqsave(q->queue_lock, flags);
706 blk_finish_request(req, error);
707 spin_unlock_irqrestore(q->queue_lock, flags);
708
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700709 scsi_run_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100710 }
711
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700712 put_device(&sdev->sdev_gendev);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800713 return false;
714}
715
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200716/**
717 * __scsi_error_from_host_byte - translate SCSI error code into errno
718 * @cmd: SCSI command (unused)
719 * @result: scsi error code
720 *
721 * Translate SCSI error code into standard UNIX errno.
722 * Return values:
723 * -ENOLINK temporary transport failure
724 * -EREMOTEIO permanent target failure, do not retry
725 * -EBADE permanent nexus failure, retry on other path
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200726 * -ENOSPC No write space available
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200727 * -ENODATA Medium error
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200728 * -EIO unspecified I/O error
729 */
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100730static int __scsi_error_from_host_byte(struct scsi_cmnd *cmd, int result)
731{
732 int error = 0;
733
734 switch(host_byte(result)) {
735 case DID_TRANSPORT_FAILFAST:
736 error = -ENOLINK;
737 break;
738 case DID_TARGET_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000739 set_host_byte(cmd, DID_OK);
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100740 error = -EREMOTEIO;
741 break;
742 case DID_NEXUS_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000743 set_host_byte(cmd, DID_OK);
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100744 error = -EBADE;
745 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200746 case DID_ALLOC_FAILURE:
747 set_host_byte(cmd, DID_OK);
748 error = -ENOSPC;
749 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200750 case DID_MEDIUM_ERROR:
751 set_host_byte(cmd, DID_OK);
752 error = -ENODATA;
753 break;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100754 default:
755 error = -EIO;
756 break;
757 }
758
759 return error;
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
763 * Function: scsi_io_completion()
764 *
765 * Purpose: Completion processing for block device I/O requests.
766 *
767 * Arguments: cmd - command that is finished.
768 *
769 * Lock status: Assumed that no lock is held upon entry.
770 *
771 * Returns: Nothing
772 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200773 * Notes: We will finish off the specified number of sectors. If we
774 * are done, the command block will be released and the queue
775 * function will be goosed. If we are not done then we have to
Alan Sternb60af5b2008-11-03 15:56:47 -0500776 * figure out what to do next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 *
Alan Sternb60af5b2008-11-03 15:56:47 -0500778 * a) We can call scsi_requeue_command(). The request
779 * will be unprepared and put back on the queue. Then
780 * a new command will be created for it. This should
781 * be used if we made forward progress, or if we want
782 * to switch from READ(10) to READ(6) for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200784 * b) We can call __scsi_queue_insert(). The request will
Alan Sternb60af5b2008-11-03 15:56:47 -0500785 * be put back on the queue and retried using the same
786 * command as before, possibly after a delay.
787 *
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800788 * c) We can call scsi_end_request() with -EIO to fail
Alan Sternb60af5b2008-11-03 15:56:47 -0500789 * the remainder of the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700791void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 int result = cmd->result;
Jens Axboe165125e2007-07-24 09:28:11 +0200794 struct request_queue *q = cmd->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 struct request *req = cmd->request;
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500796 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct scsi_sense_hdr sshdr;
Hannes Reinecke4753cbc2014-10-24 14:26:52 +0200798 bool sense_valid = false;
Hannes Reineckec11c0042014-10-24 14:27:01 +0200799 int sense_deferred = 0, level = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500800 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
801 ACTION_DELAYED_RETRY} action;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900802 unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (result) {
805 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
806 if (sense_valid)
807 sense_deferred = scsi_sense_is_deferred(&sshdr);
808 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200809
Christoph Hellwig57292b52017-01-31 16:57:29 +0100810 if (blk_rq_is_passthrough(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (result) {
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100812 if (sense_valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * SG_IO wants current and deferred errors
815 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100816 scsi_req(req)->sense_len =
817 min(8 + cmd->sense_buffer[7],
818 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500820 if (!sense_deferred)
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100821 error = __scsi_error_from_host_byte(cmd, result);
Pete Wyckoffb22f6872007-03-13 16:53:28 -0400822 }
Mike Snitzer27c41972012-05-31 15:05:33 -0400823 /*
824 * __scsi_error_from_host_byte may have reset the host_byte
825 */
826 req->errors = cmd->result;
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900827
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100828 scsi_req(req)->resid_len = scsi_get_resid(cmd);
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900829
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200830 if (scsi_bidi_cmnd(cmd)) {
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900831 /*
832 * Bidi commands Must be complete as a whole,
833 * both sides at once.
834 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100835 scsi_req(req->next_rq)->resid_len = scsi_in(cmd)->resid;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800836 if (scsi_end_request(req, 0, blk_rq_bytes(req),
837 blk_rq_bytes(req->next_rq)))
838 BUG();
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200839 return;
840 }
James Bottomley89fb4cd2014-07-03 19:17:34 +0200841 } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
842 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100843 * Flush commands do not transfers any data, and thus cannot use
James Bottomley89fb4cd2014-07-03 19:17:34 +0200844 * good_bytes != blk_rq_bytes(req) as the signal for an error.
845 * This sets the error explicitly for the problem case.
846 */
847 error = __scsi_error_from_host_byte(cmd, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Christoph Hellwig57292b52017-01-31 16:57:29 +0100850 /* no bidi support for !blk_rq_is_passthrough yet */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200851 BUG_ON(blk_bidi_rq(req));
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * Next deal with any sectors which we were able to correctly
855 * handle.
856 */
Hannes Reinecke91921e02014-06-25 16:39:59 +0200857 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
858 "%u sectors total, %d bytes done.\n",
859 blk_rq_sectors(req), good_bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
James Bottomleya9bddd72009-03-30 16:55:51 +0000861 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100862 * Recovered errors need reporting, but they're always treated as
863 * success, so fiddle the result code here. For passthrough requests
James Bottomleya9bddd72009-03-30 16:55:51 +0000864 * we already took a copy of the original into rq->errors which
865 * is what gets returned to the user
866 */
Douglas Gilberte7efe592010-01-03 13:51:15 -0500867 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
868 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
869 * print since caller wants ATA registers. Only occurs on
870 * SCSI ATA PASS_THROUGH commands when CK_COND=1
871 */
872 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
873 ;
Christoph Hellwige8064022016-10-20 15:12:13 +0200874 else if (!(req->rq_flags & RQF_QUIET))
Hannes Reinecked811b842014-10-24 14:26:45 +0200875 scsi_print_sense(cmd);
James Bottomleya9bddd72009-03-30 16:55:51 +0000876 result = 0;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100877 /* for passthrough error may be set */
James Bottomleya9bddd72009-03-30 16:55:51 +0000878 error = 0;
879 }
880
881 /*
James Bottomleya621bac2016-05-13 12:04:06 -0700882 * special case: failed zero length commands always need to
883 * drop down into the retry code. Otherwise, if we finished
884 * all bytes in the request we are done now.
James Bottomleyd6b0c532006-07-02 10:06:28 -0500885 */
James Bottomleya621bac2016-05-13 12:04:06 -0700886 if (!(blk_rq_bytes(req) == 0 && error) &&
887 !scsi_end_request(req, error, good_bytes, 0))
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800888 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200889
890 /*
891 * Kill remainder if no retrys.
892 */
893 if (error && scsi_noretry_cmd(cmd)) {
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800894 if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
895 BUG();
896 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200897 }
898
899 /*
900 * If there had been no error, but we have leftover bytes in the
901 * requeues just queue the command up again.
902 */
903 if (result == 0)
904 goto requeue;
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700905
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100906 error = __scsi_error_from_host_byte(cmd, result);
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500907
Alan Sternb60af5b2008-11-03 15:56:47 -0500908 if (host_byte(result) == DID_RESET) {
909 /* Third party bus reset or reset for error recovery
910 * reasons. Just retry the command and see what
911 * happens.
912 */
913 action = ACTION_RETRY;
914 } else if (sense_valid && !sense_deferred) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 switch (sshdr.sense_key) {
916 case UNIT_ATTENTION:
917 if (cmd->device->removable) {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700918 /* Detected disc change. Set a bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * and quietly refuse further access.
920 */
921 cmd->device->changed = 1;
Alan Sternb60af5b2008-11-03 15:56:47 -0500922 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 } else {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700924 /* Must have been a power glitch, or a
925 * bus reset. Could not have been a
926 * media change, so we just retry the
Alan Sternb60af5b2008-11-03 15:56:47 -0500927 * command and see what happens.
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700928 */
Alan Sternb60af5b2008-11-03 15:56:47 -0500929 action = ACTION_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931 break;
932 case ILLEGAL_REQUEST:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700933 /* If we had an ILLEGAL REQUEST returned, then
934 * we may have performed an unsupported
935 * command. The only thing this should be
936 * would be a ten byte read where only a six
937 * byte read was supported. Also, on a system
938 * where READ CAPACITY failed, we may have
939 * read past the end of the disk.
940 */
Jens Axboe26a68012005-11-29 21:03:34 +0100941 if ((cmd->device->use_10_for_rw &&
942 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 (cmd->cmnd[0] == READ_10 ||
944 cmd->cmnd[0] == WRITE_10)) {
Alan Sternb60af5b2008-11-03 15:56:47 -0500945 /* This will issue a new 6-byte command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 cmd->device->use_10_for_rw = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500947 action = ACTION_REPREP;
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500948 } else if (sshdr.asc == 0x10) /* DIX */ {
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500949 action = ACTION_FAIL;
950 error = -EILSEQ;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500951 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
Martin K. Petersen5db44862012-09-18 12:19:32 -0400952 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500953 action = ACTION_FAIL;
Martin K. Petersen66a651a2012-02-13 15:38:22 -0500954 error = -EREMOTEIO;
Alan Sternb60af5b2008-11-03 15:56:47 -0500955 } else
956 action = ACTION_FAIL;
957 break;
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400958 case ABORTED_COMMAND:
James Bottomley126c09822009-02-19 21:48:54 +0000959 action = ACTION_FAIL;
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200960 if (sshdr.asc == 0x10) /* DIF */
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500961 error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
963 case NOT_READY:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700964 /* If the device is in the process of becoming
James Bottomleyf3e93f72006-04-25 16:48:30 -0500965 * ready, or has a temporary blockage, retry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
James Bottomleyf3e93f72006-04-25 16:48:30 -0500967 if (sshdr.asc == 0x04) {
968 switch (sshdr.ascq) {
969 case 0x01: /* becoming ready */
970 case 0x04: /* format in progress */
971 case 0x05: /* rebuild in progress */
972 case 0x06: /* recalculation in progress */
973 case 0x07: /* operation in progress */
974 case 0x08: /* Long write in progress */
975 case 0x09: /* self test in progress */
Martin K. Petersend8705f12009-11-26 12:00:41 -0500976 case 0x14: /* space allocation in progress */
Alan Sternb60af5b2008-11-03 15:56:47 -0500977 action = ACTION_DELAYED_RETRY;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500978 break;
Alan Stern3dbf6a52008-12-15 10:31:28 -0500979 default:
Alan Stern3dbf6a52008-12-15 10:31:28 -0500980 action = ACTION_FAIL;
981 break;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500982 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200983 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500984 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 case VOLUME_OVERFLOW:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700987 /* See SSC3rXX or current. */
Alan Sternb60af5b2008-11-03 15:56:47 -0500988 action = ACTION_FAIL;
989 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 default:
Alan Sternb60af5b2008-11-03 15:56:47 -0500991 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 break;
993 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200994 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500995 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500996
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900997 if (action != ACTION_FAIL &&
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200998 time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900999 action = ACTION_FAIL;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +09001000
Alan Sternb60af5b2008-11-03 15:56:47 -05001001 switch (action) {
1002 case ACTION_FAIL:
1003 /* Give up and fail the remainder of the request */
Christoph Hellwige8064022016-10-20 15:12:13 +02001004 if (!(req->rq_flags & RQF_QUIET)) {
Hannes Reineckef1569ff2014-10-24 14:27:07 +02001005 static DEFINE_RATELIMIT_STATE(_rs,
1006 DEFAULT_RATELIMIT_INTERVAL,
1007 DEFAULT_RATELIMIT_BURST);
1008
1009 if (unlikely(scsi_logging_level))
1010 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
1011 SCSI_LOG_MLCOMPLETE_BITS);
1012
1013 /*
1014 * if logging is enabled the failure will be printed
1015 * in scsi_log_completion(), so avoid duplicate messages
1016 */
1017 if (!level && __ratelimit(&_rs)) {
1018 scsi_print_result(cmd, NULL, FAILED);
1019 if (driver_byte(result) & DRIVER_SENSE)
1020 scsi_print_sense(cmd);
1021 scsi_print_command(cmd);
1022 }
James Bottomley3173d8c2005-09-04 11:32:05 -05001023 }
Christoph Hellwigf6d47e72014-02-16 06:16:13 -08001024 if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
1025 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +02001026 /*FALLTHRU*/
Alan Sternb60af5b2008-11-03 15:56:47 -05001027 case ACTION_REPREP:
Christoph Hellwigbc85dc52014-05-01 16:51:03 +02001028 requeue:
Alan Sternb60af5b2008-11-03 15:56:47 -05001029 /* Unprep the request and put it back at the head of the queue.
1030 * A new command will be prepared and issued.
1031 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01001032 if (q->mq_ops) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001033 cmd->request->rq_flags &= ~RQF_DONTPREP;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001034 scsi_mq_uninit_cmd(cmd);
1035 scsi_mq_requeue_cmd(cmd);
1036 } else {
1037 scsi_release_buffers(cmd);
1038 scsi_requeue_command(q, cmd);
1039 }
Alan Sternb60af5b2008-11-03 15:56:47 -05001040 break;
1041 case ACTION_RETRY:
1042 /* Retry the same command immediately */
James Bottomley4f5299a2009-01-02 10:42:21 -06001043 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -05001044 break;
1045 case ACTION_DELAYED_RETRY:
1046 /* Retry the same command after a delay */
James Bottomley4f5299a2009-01-02 10:42:21 -06001047 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -05001048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001052static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001054 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001057 * If sg table allocation fails, requeue request later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001059 if (unlikely(sg_alloc_table_chained(&sdb->table,
1060 blk_rq_nr_phys_segments(req), sdb->table.sgl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return BLKPREP_DEFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /*
1064 * Next, walk the list, and fill in the addresses and sizes of
1065 * each segment.
1066 */
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001067 count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
1068 BUG_ON(count > sdb->table.nents);
1069 sdb->table.nents = count;
Christoph Hellwigfd102b12017-01-13 12:29:11 +01001070 sdb->length = blk_rq_payload_bytes(req);
Rusty Russell4a03d902007-11-19 11:28:48 +11001071 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001073
1074/*
1075 * Function: scsi_init_io()
1076 *
1077 * Purpose: SCSI I/O initialize function.
1078 *
1079 * Arguments: cmd - Command descriptor we wish to initialize
1080 *
1081 * Returns: 0 on success
1082 * BLKPREP_DEFER if the failure is retryable
1083 * BLKPREP_KILL if the failure is fatal
1084 */
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001085int scsi_init_io(struct scsi_cmnd *cmd)
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001086{
Christoph Hellwig5e012aa2014-04-15 12:24:55 +02001087 struct scsi_device *sdev = cmd->device;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001088 struct request *rq = cmd->request;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001089 bool is_mq = (rq->mq_ctx != NULL);
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001090 int error;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001091
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001092 BUG_ON(!blk_rq_nr_phys_segments(rq));
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001093
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001094 error = scsi_init_sgtable(rq, &cmd->sdb);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001095 if (error)
1096 goto err_exit;
1097
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001098 if (blk_bidi_rq(rq)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001099 if (!rq->q->mq_ops) {
1100 struct scsi_data_buffer *bidi_sdb =
1101 kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC);
1102 if (!bidi_sdb) {
1103 error = BLKPREP_DEFER;
1104 goto err_exit;
1105 }
1106
1107 rq->next_rq->special = bidi_sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001108 }
1109
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001110 error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001111 if (error)
1112 goto err_exit;
1113 }
1114
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001115 if (blk_integrity_rq(rq)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001116 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1117 int ivecs, count;
1118
Ewan D. Milne91724c22015-01-15 10:02:12 -05001119 if (prot_sdb == NULL) {
1120 /*
1121 * This can happen if someone (e.g. multipath)
1122 * queues a command to a device on an adapter
1123 * that does not support DIX.
1124 */
1125 WARN_ON_ONCE(1);
1126 error = BLKPREP_KILL;
1127 goto err_exit;
1128 }
1129
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001130 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001131
Ming Lin001d63b2016-04-04 14:48:09 -07001132 if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
Ming Lin22cc3d42016-04-04 14:48:08 -07001133 prot_sdb->table.sgl)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001134 error = BLKPREP_DEFER;
1135 goto err_exit;
1136 }
1137
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001138 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001139 prot_sdb->table.sgl);
1140 BUG_ON(unlikely(count > ivecs));
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001141 BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q)));
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001142
1143 cmd->prot_sdb = prot_sdb;
1144 cmd->prot_sdb->table.nents = count;
1145 }
1146
Christoph Hellwigd2852032014-01-17 12:06:53 +01001147 return BLKPREP_OK;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001148err_exit:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001149 if (is_mq) {
1150 scsi_mq_free_sgtables(cmd);
1151 } else {
1152 scsi_release_buffers(cmd);
1153 cmd->request->special = NULL;
1154 scsi_put_command(cmd);
1155 put_device(&sdev->sdev_gendev);
1156 }
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001157 return error;
1158}
Boaz Harroshbb52d822007-12-13 16:14:27 -08001159EXPORT_SYMBOL(scsi_init_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001161void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001162{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001163 void *buf = cmd->sense_buffer;
1164 void *prot = cmd->prot_sdb;
1165 unsigned long flags;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001166
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001167 /* zero out the cmd, except for the embedded scsi_request */
1168 memset((char *)cmd + sizeof(cmd->req), 0,
1169 sizeof(*cmd) - sizeof(cmd->req));
1170
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001171 cmd->device = dev;
1172 cmd->sense_buffer = buf;
1173 cmd->prot_sdb = prot;
1174 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1175 cmd->jiffies_at_alloc = jiffies;
Christoph Hellwig04796332014-02-20 14:20:55 -08001176
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001177 spin_lock_irqsave(&dev->list_lock, flags);
1178 list_add_tail(&cmd->list, &dev->cmd_list);
1179 spin_unlock_irqrestore(&dev->list_lock, flags);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001180}
1181
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001182static int scsi_setup_scsi_cmnd(struct scsi_device *sdev, struct request *req)
James Bottomley7b163182005-12-15 20:17:02 -06001183{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001184 struct scsi_cmnd *cmd = req->special;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001185
1186 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001187 * Passthrough requests may transfer data, in which case they must
Christoph Hellwig3b003152006-11-04 20:10:55 +01001188 * a bio attached to them. Or they might contain a SCSI command
1189 * that does not transfer data, in which case they may optionally
1190 * submit a request without an attached bio.
1191 */
1192 if (req->bio) {
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001193 int ret = scsi_init_io(cmd);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001194 if (unlikely(ret))
1195 return ret;
1196 } else {
Tejun Heob0790412009-05-07 22:24:42 +09001197 BUG_ON(blk_rq_bytes(req));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001198
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001199 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001200 }
James Bottomley7b163182005-12-15 20:17:02 -06001201
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001202 cmd->cmd_len = scsi_req(req)->cmd_len;
1203 cmd->cmnd = scsi_req(req)->cmd;
Tejun Heob0790412009-05-07 22:24:42 +09001204 cmd->transfersize = blk_rq_bytes(req);
James Bottomley7b163182005-12-15 20:17:02 -06001205 cmd->allowed = req->retries;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001206 return BLKPREP_OK;
1207}
1208
1209/*
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001210 * Setup a normal block command. These are simple request from filesystems
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001211 * that still need to be translated to SCSI CDBs from the ULD.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001212 */
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001213static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001214{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001215 struct scsi_cmnd *cmd = req->special;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001216
Christoph Hellwigee14c672015-08-27 14:16:59 +02001217 if (unlikely(sdev->handler && sdev->handler->prep_fn)) {
1218 int ret = sdev->handler->prep_fn(sdev, req);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001219 if (ret != BLKPREP_OK)
1220 return ret;
1221 }
1222
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001223 cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001224 memset(cmd->cmnd, 0, BLK_MAX_CDB);
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001225 return scsi_cmd_to_driver(cmd)->init_command(cmd);
James Bottomley7b163182005-12-15 20:17:02 -06001226}
James Bottomley7b163182005-12-15 20:17:02 -06001227
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001228static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req)
1229{
1230 struct scsi_cmnd *cmd = req->special;
1231
1232 if (!blk_rq_bytes(req))
1233 cmd->sc_data_direction = DMA_NONE;
1234 else if (rq_data_dir(req) == WRITE)
1235 cmd->sc_data_direction = DMA_TO_DEVICE;
1236 else
1237 cmd->sc_data_direction = DMA_FROM_DEVICE;
1238
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001239 if (blk_rq_is_scsi(req))
1240 return scsi_setup_scsi_cmnd(sdev, req);
1241 else
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001242 return scsi_setup_fs_cmnd(sdev, req);
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001243}
1244
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001245static int
1246scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Christoph Hellwig3b003152006-11-04 20:10:55 +01001248 int ret = BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001251 * If the device is not in running state we will reject some
1252 * or all commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
Christoph Hellwig3b003152006-11-04 20:10:55 +01001255 switch (sdev->sdev_state) {
1256 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05001257 case SDEV_TRANSPORT_OFFLINE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001258 /*
1259 * If the device is offline we refuse to process any
1260 * commands. The device must be brought online
1261 * before trying any recovery commands.
1262 */
1263 sdev_printk(KERN_ERR, sdev,
1264 "rejecting I/O to offline device\n");
1265 ret = BLKPREP_KILL;
1266 break;
1267 case SDEV_DEL:
1268 /*
1269 * If the device is fully deleted, we refuse to
1270 * process any commands as well.
1271 */
James Bottomley9ccfc752005-10-02 11:45:08 -05001272 sdev_printk(KERN_ERR, sdev,
1273 "rejecting I/O to dead device\n");
Christoph Hellwig3b003152006-11-04 20:10:55 +01001274 ret = BLKPREP_KILL;
1275 break;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001276 case SDEV_BLOCK:
James Bottomley6f4267e2008-08-22 16:53:31 -05001277 case SDEV_CREATED_BLOCK:
Bart Van Asschebba0bdd2015-03-04 10:31:47 +01001278 ret = BLKPREP_DEFER;
1279 break;
1280 case SDEV_QUIESCE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001281 /*
1282 * If the devices is blocked we defer normal commands.
1283 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001284 if (!(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001285 ret = BLKPREP_DEFER;
1286 break;
1287 default:
1288 /*
1289 * For any other not fully online state we only allow
1290 * special commands. In particular any user initiated
1291 * command is not allowed.
1292 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001293 if (!(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001294 ret = BLKPREP_KILL;
1295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001298 return ret;
1299}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001301static int
1302scsi_prep_return(struct request_queue *q, struct request *req, int ret)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001303{
1304 struct scsi_device *sdev = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Christoph Hellwig3b003152006-11-04 20:10:55 +01001306 switch (ret) {
1307 case BLKPREP_KILL:
jiangyiwene1cd3912016-02-16 20:14:13 +08001308 case BLKPREP_INVALID:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001309 req->errors = DID_NO_CONNECT << 16;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001310 /* release the command and kill it */
1311 if (req->special) {
1312 struct scsi_cmnd *cmd = req->special;
1313 scsi_release_buffers(cmd);
1314 scsi_put_command(cmd);
Christoph Hellwig68c03d92014-04-15 12:24:56 +02001315 put_device(&sdev->sdev_gendev);
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001316 req->special = NULL;
1317 }
Christoph Hellwig3b003152006-11-04 20:10:55 +01001318 break;
1319 case BLKPREP_DEFER:
1320 /*
Tejun Heo9934c8c2009-05-08 11:54:16 +09001321 * If we defer, the blk_peek_request() returns NULL, but the
Jens Axboea488e742010-04-16 21:13:15 +02001322 * queue must be restarted, so we schedule a callback to happen
1323 * shortly.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001324 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001325 if (atomic_read(&sdev->device_busy) == 0)
Jens Axboea488e742010-04-16 21:13:15 +02001326 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001327 break;
1328 default:
Christoph Hellwige8064022016-10-20 15:12:13 +02001329 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Christoph Hellwig3b003152006-11-04 20:10:55 +01001332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001334
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001335static int scsi_prep_fn(struct request_queue *q, struct request *req)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001336{
1337 struct scsi_device *sdev = q->queuedata;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001338 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001339 int ret;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001340
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001341 ret = scsi_prep_state_check(sdev, req);
1342 if (ret != BLKPREP_OK)
1343 goto out;
1344
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001345 if (!req->special) {
1346 /* Bail if we can't get a reference to the device */
1347 if (unlikely(!get_device(&sdev->sdev_gendev))) {
1348 ret = BLKPREP_DEFER;
1349 goto out;
1350 }
1351
1352 scsi_init_command(sdev, cmd);
1353 req->special = cmd;
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001354 }
1355
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001356 cmd->tag = req->tag;
1357 cmd->request = req;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001358 cmd->prot_op = SCSI_PROT_NORMAL;
1359
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001360 ret = scsi_setup_cmnd(sdev, req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001361out:
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001362 return scsi_prep_return(q, req, ret);
1363}
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001364
1365static void scsi_unprep_fn(struct request_queue *q, struct request *req)
1366{
Christoph Hellwigd2852032014-01-17 12:06:53 +01001367 scsi_uninit_cmd(req->special);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370/*
1371 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1372 * return 0.
1373 *
1374 * Called with the queue_lock held.
1375 */
1376static inline int scsi_dev_queue_ready(struct request_queue *q,
1377 struct scsi_device *sdev)
1378{
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001379 unsigned int busy;
1380
1381 busy = atomic_inc_return(&sdev->device_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001382 if (atomic_read(&sdev->device_blocked)) {
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001383 if (busy)
1384 goto out_dec;
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
1387 * unblock after device_blocked iterates to zero
1388 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001389 if (atomic_dec_return(&sdev->device_blocked) > 0) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001390 /*
1391 * For the MQ case we take care of this in the caller.
1392 */
1393 if (!q->mq_ops)
1394 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001395 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001397 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1398 "unblocking device at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001400
1401 if (busy >= sdev->queue_depth)
1402 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 return 1;
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001405out_dec:
1406 atomic_dec(&sdev->device_busy);
1407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Mike Christief0c0a372008-08-17 15:24:38 -05001410/*
1411 * scsi_target_queue_ready: checks if there we can send commands to target
1412 * @sdev: scsi device on starget to check.
Mike Christief0c0a372008-08-17 15:24:38 -05001413 */
1414static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1415 struct scsi_device *sdev)
1416{
1417 struct scsi_target *starget = scsi_target(sdev);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001418 unsigned int busy;
Mike Christief0c0a372008-08-17 15:24:38 -05001419
1420 if (starget->single_lun) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001421 spin_lock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001422 if (starget->starget_sdev_user &&
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001423 starget->starget_sdev_user != sdev) {
1424 spin_unlock_irq(shost->host_lock);
1425 return 0;
1426 }
Mike Christief0c0a372008-08-17 15:24:38 -05001427 starget->starget_sdev_user = sdev;
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001428 spin_unlock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001429 }
1430
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001431 if (starget->can_queue <= 0)
1432 return 1;
1433
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001434 busy = atomic_inc_return(&starget->target_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001435 if (atomic_read(&starget->target_blocked) > 0) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001436 if (busy)
1437 goto starved;
1438
Mike Christief0c0a372008-08-17 15:24:38 -05001439 /*
1440 * unblock after target_blocked iterates to zero
1441 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001442 if (atomic_dec_return(&starget->target_blocked) > 0)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001443 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001444
1445 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1446 "unblocking target at zero depth\n"));
Mike Christief0c0a372008-08-17 15:24:38 -05001447 }
1448
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001449 if (busy >= starget->can_queue)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001450 goto starved;
Mike Christief0c0a372008-08-17 15:24:38 -05001451
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001452 return 1;
1453
1454starved:
1455 spin_lock_irq(shost->host_lock);
1456 list_move_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001457 spin_unlock_irq(shost->host_lock);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001458out_dec:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001459 if (starget->can_queue > 0)
1460 atomic_dec(&starget->target_busy);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001461 return 0;
Mike Christief0c0a372008-08-17 15:24:38 -05001462}
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464/*
1465 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1466 * return 0. We must end up running the queue again whenever 0 is
1467 * returned, else IO can hang.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 */
1469static inline int scsi_host_queue_ready(struct request_queue *q,
1470 struct Scsi_Host *shost,
1471 struct scsi_device *sdev)
1472{
Christoph Hellwig74665012014-01-22 15:29:29 +01001473 unsigned int busy;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001474
James Bottomley939647e2005-09-18 15:05:20 -05001475 if (scsi_host_in_recovery(shost))
Christoph Hellwig74665012014-01-22 15:29:29 +01001476 return 0;
1477
1478 busy = atomic_inc_return(&shost->host_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001479 if (atomic_read(&shost->host_blocked) > 0) {
Christoph Hellwig74665012014-01-22 15:29:29 +01001480 if (busy)
1481 goto starved;
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /*
1484 * unblock after host_blocked iterates to zero
1485 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001486 if (atomic_dec_return(&shost->host_blocked) > 0)
Christoph Hellwig74665012014-01-22 15:29:29 +01001487 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001488
1489 SCSI_LOG_MLQUEUE(3,
1490 shost_printk(KERN_INFO, shost,
1491 "unblocking host at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
Christoph Hellwig74665012014-01-22 15:29:29 +01001493
1494 if (shost->can_queue > 0 && busy >= shost->can_queue)
1495 goto starved;
1496 if (shost->host_self_blocked)
1497 goto starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 /* We're OK to process the command, so we can't be starved */
Christoph Hellwig74665012014-01-22 15:29:29 +01001500 if (!list_empty(&sdev->starved_entry)) {
1501 spin_lock_irq(shost->host_lock);
1502 if (!list_empty(&sdev->starved_entry))
1503 list_del_init(&sdev->starved_entry);
1504 spin_unlock_irq(shost->host_lock);
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Christoph Hellwig74665012014-01-22 15:29:29 +01001507 return 1;
1508
1509starved:
1510 spin_lock_irq(shost->host_lock);
1511 if (list_empty(&sdev->starved_entry))
1512 list_add_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001513 spin_unlock_irq(shost->host_lock);
Christoph Hellwig74665012014-01-22 15:29:29 +01001514out_dec:
1515 atomic_dec(&shost->host_busy);
1516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519/*
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001520 * Busy state exporting function for request stacking drivers.
1521 *
1522 * For efficiency, no lock is taken to check the busy state of
1523 * shost/starget/sdev, since the returned value is not guaranteed and
1524 * may be changed after request stacking drivers call the function,
1525 * regardless of taking lock or not.
1526 *
Bart Van Assche67bd9412012-06-29 15:33:22 +00001527 * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
1528 * needs to return 'not busy'. Otherwise, request stacking drivers
1529 * may hold requests forever.
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001530 */
1531static int scsi_lld_busy(struct request_queue *q)
1532{
1533 struct scsi_device *sdev = q->queuedata;
1534 struct Scsi_Host *shost;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001535
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001536 if (blk_queue_dying(q))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001537 return 0;
1538
1539 shost = sdev->host;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001540
Jun'ichi Nomurab7e94a12012-05-22 18:57:17 +09001541 /*
1542 * Ignore host/starget busy state.
1543 * Since block layer does not have a concept of fairness across
1544 * multiple queues, congestion of host/starget needs to be handled
1545 * in SCSI layer.
1546 */
1547 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001548 return 1;
1549
1550 return 0;
1551}
1552
1553/*
James Bottomleye91442b2005-09-09 10:44:16 -05001554 * Kill a request for a dead device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
Jens Axboe165125e2007-07-24 09:28:11 +02001556static void scsi_kill_request(struct request *req, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
James Bottomleye91442b2005-09-09 10:44:16 -05001558 struct scsi_cmnd *cmd = req->special;
Jiri Slaby03b14702009-09-23 16:15:35 +02001559 struct scsi_device *sdev;
1560 struct scsi_target *starget;
1561 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Tejun Heo9934c8c2009-05-08 11:54:16 +09001563 blk_start_request(req);
James Bottomley788ce432005-09-09 13:40:23 -05001564
Hannes Reinecke74571812011-11-09 08:39:24 +01001565 scmd_printk(KERN_INFO, cmd, "killing request\n");
1566
Jiri Slaby03b14702009-09-23 16:15:35 +02001567 sdev = cmd->device;
1568 starget = scsi_target(sdev);
1569 shost = sdev->host;
James Bottomleye91442b2005-09-09 10:44:16 -05001570 scsi_init_cmd_errh(cmd);
1571 cmd->result = DID_NO_CONNECT << 16;
1572 atomic_inc(&cmd->device->iorequest_cnt);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001573
1574 /*
1575 * SCSI request completion path will do scsi_device_unbusy(),
1576 * bump busy counts. To bump the counters, we need to dance
1577 * with the locks as normal issue path does.
1578 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001579 atomic_inc(&sdev->device_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +01001580 atomic_inc(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001581 if (starget->can_queue > 0)
1582 atomic_inc(&starget->target_busy);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001583
Jens Axboe242f9dc2008-09-14 05:55:09 -07001584 blk_complete_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
Jens Axboe1aea6432006-01-09 16:03:03 +01001587static void scsi_softirq_done(struct request *rq)
1588{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001589 struct scsi_cmnd *cmd = rq->special;
1590 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
Jens Axboe1aea6432006-01-09 16:03:03 +01001591 int disposition;
1592
1593 INIT_LIST_HEAD(&cmd->eh_entry);
1594
Jens Axboe242f9dc2008-09-14 05:55:09 -07001595 atomic_inc(&cmd->device->iodone_cnt);
1596 if (cmd->result)
1597 atomic_inc(&cmd->device->ioerr_cnt);
1598
Jens Axboe1aea6432006-01-09 16:03:03 +01001599 disposition = scsi_decide_disposition(cmd);
1600 if (disposition != SUCCESS &&
1601 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
1602 sdev_printk(KERN_ERR, cmd->device,
1603 "timing out command, waited %lus\n",
1604 wait_for/HZ);
1605 disposition = SUCCESS;
1606 }
Hannes Reinecke91921e02014-06-25 16:39:59 +02001607
Jens Axboe1aea6432006-01-09 16:03:03 +01001608 scsi_log_completion(cmd, disposition);
1609
1610 switch (disposition) {
1611 case SUCCESS:
1612 scsi_finish_command(cmd);
1613 break;
1614 case NEEDS_RETRY:
Christoph Hellwig596f4822007-01-02 12:56:00 +01001615 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
Jens Axboe1aea6432006-01-09 16:03:03 +01001616 break;
1617 case ADD_TO_MLQUEUE:
1618 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1619 break;
1620 default:
1621 if (!scsi_eh_scmd_add(cmd, 0))
1622 scsi_finish_command(cmd);
1623 }
1624}
1625
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001626/**
Christoph Hellwig82042a22014-09-05 18:23:07 -07001627 * scsi_dispatch_command - Dispatch a command to the low-level driver.
1628 * @cmd: command block we are dispatching.
1629 *
1630 * Return: nonzero return request was rejected and device's queue needs to be
1631 * plugged.
1632 */
1633static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
1634{
1635 struct Scsi_Host *host = cmd->device->host;
1636 int rtn = 0;
1637
1638 atomic_inc(&cmd->device->iorequest_cnt);
1639
1640 /* check if the device is still usable */
1641 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
1642 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
1643 * returns an immediate error upwards, and signals
1644 * that the device is no longer present */
1645 cmd->result = DID_NO_CONNECT << 16;
1646 goto done;
1647 }
1648
1649 /* Check to see if the scsi lld made this device blocked. */
1650 if (unlikely(scsi_device_blocked(cmd->device))) {
1651 /*
1652 * in blocked state, the command is just put back on
1653 * the device queue. The suspend state has already
1654 * blocked the queue so future requests should not
1655 * occur until the device transitions out of the
1656 * suspend state.
1657 */
1658 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1659 "queuecommand : device blocked\n"));
1660 return SCSI_MLQUEUE_DEVICE_BUSY;
1661 }
1662
1663 /* Store the LUN value in cmnd, if needed. */
1664 if (cmd->device->lun_in_cdb)
1665 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
1666 (cmd->device->lun << 5 & 0xe0);
1667
1668 scsi_log_send(cmd);
1669
1670 /*
1671 * Before we queue this command, check if the command
1672 * length exceeds what the host adapter can handle.
1673 */
1674 if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
1675 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1676 "queuecommand : command too long. "
1677 "cdb_size=%d host->max_cmd_len=%d\n",
1678 cmd->cmd_len, cmd->device->host->max_cmd_len));
1679 cmd->result = (DID_ABORT << 16);
1680 goto done;
1681 }
1682
1683 if (unlikely(host->shost_state == SHOST_DEL)) {
1684 cmd->result = (DID_NO_CONNECT << 16);
1685 goto done;
1686
1687 }
1688
1689 trace_scsi_dispatch_cmd_start(cmd);
1690 rtn = host->hostt->queuecommand(host, cmd);
1691 if (rtn) {
1692 trace_scsi_dispatch_cmd_error(cmd, rtn);
1693 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
1694 rtn != SCSI_MLQUEUE_TARGET_BUSY)
1695 rtn = SCSI_MLQUEUE_HOST_BUSY;
1696
1697 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1698 "queuecommand : request rejected\n"));
1699 }
1700
1701 return rtn;
1702 done:
1703 cmd->scsi_done(cmd);
1704 return 0;
1705}
1706
1707/**
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001708 * scsi_done - Invoke completion on finished SCSI command.
1709 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
1710 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
1711 *
1712 * Description: This function is the mid-level's (SCSI Core) interrupt routine,
1713 * which regains ownership of the SCSI command (de facto) from a LLDD, and
1714 * calls blk_complete_request() for further processing.
1715 *
1716 * This function is interrupt context safe.
1717 */
1718static void scsi_done(struct scsi_cmnd *cmd)
1719{
1720 trace_scsi_dispatch_cmd_done(cmd);
1721 blk_complete_request(cmd->request);
1722}
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724/*
1725 * Function: scsi_request_fn()
1726 *
1727 * Purpose: Main strategy routine for SCSI.
1728 *
1729 * Arguments: q - Pointer to actual queue.
1730 *
1731 * Returns: Nothing
1732 *
1733 * Lock status: IO request lock assumed to be held when called.
1734 */
1735static void scsi_request_fn(struct request_queue *q)
Bart Van Assche613be1f2014-02-20 14:20:56 -08001736 __releases(q->queue_lock)
1737 __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 struct scsi_device *sdev = q->queuedata;
1740 struct Scsi_Host *shost;
1741 struct scsi_cmnd *cmd;
1742 struct request *req;
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /*
1745 * To start with, we keep looping until the queue is empty, or until
1746 * the host is no longer able to accept any more requests.
1747 */
1748 shost = sdev->host;
Jens Axboea488e742010-04-16 21:13:15 +02001749 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 int rtn;
1751 /*
1752 * get next queueable request. We do this early to make sure
Hannes Reinecke91921e02014-06-25 16:39:59 +02001753 * that the request is fully prepared even if we cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 * accept it.
1755 */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001756 req = blk_peek_request(q);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001757 if (!req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 break;
1759
1760 if (unlikely(!scsi_device_online(sdev))) {
James Bottomley9ccfc752005-10-02 11:45:08 -05001761 sdev_printk(KERN_ERR, sdev,
1762 "rejecting I/O to offline device\n");
James Bottomleye91442b2005-09-09 10:44:16 -05001763 scsi_kill_request(req, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 continue;
1765 }
1766
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001767 if (!scsi_dev_queue_ready(q, sdev))
1768 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 /*
1771 * Remove the request from the request list.
1772 */
1773 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
Tejun Heo9934c8c2009-05-08 11:54:16 +09001774 blk_start_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001776 spin_unlock_irq(q->queue_lock);
James Bottomleye91442b2005-09-09 10:44:16 -05001777 cmd = req->special;
1778 if (unlikely(cmd == NULL)) {
1779 printk(KERN_CRIT "impossible request in %s.\n"
1780 "please mail a stack trace to "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001781 "linux-scsi@vger.kernel.org\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001782 __func__);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001783 blk_dump_rq_flags(req, "foo");
James Bottomleye91442b2005-09-09 10:44:16 -05001784 BUG();
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Mike Christieecefe8a2008-07-11 19:50:35 -05001787 /*
1788 * We hit this when the driver is using a host wide
1789 * tag map. For device level tag maps the queue_depth check
1790 * in the device ready fn would prevent us from trying
1791 * to allocate a tag. Since the map is a shared host resource
1792 * we add the dev to the starved list so it eventually gets
1793 * a run when a tag is freed.
1794 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001795 if (blk_queue_tagged(q) && !(req->rq_flags & RQF_QUEUED)) {
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001796 spin_lock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001797 if (list_empty(&sdev->starved_entry))
1798 list_add_tail(&sdev->starved_entry,
1799 &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001800 spin_unlock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001801 goto not_ready;
1802 }
1803
Mike Christief0c0a372008-08-17 15:24:38 -05001804 if (!scsi_target_queue_ready(shost, sdev))
1805 goto not_ready;
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (!scsi_host_queue_ready(q, shost, sdev))
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001808 goto host_not_ready;
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001809
1810 if (sdev->simple_tags)
1811 cmd->flags |= SCMD_TAGGED;
1812 else
1813 cmd->flags &= ~SCMD_TAGGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /*
1816 * Finally, initialize any error handling parameters, and set up
1817 * the timers for timeouts.
1818 */
1819 scsi_init_cmd_errh(cmd);
1820
1821 /*
1822 * Dispatch the command to the low-level driver.
1823 */
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001824 cmd->scsi_done = scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 rtn = scsi_dispatch_cmd(cmd);
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001826 if (rtn) {
1827 scsi_queue_insert(cmd, rtn);
1828 spin_lock_irq(q->queue_lock);
Jens Axboea488e742010-04-16 21:13:15 +02001829 goto out_delay;
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001830 }
1831 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
Bart Van Assche613be1f2014-02-20 14:20:56 -08001834 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001836 host_not_ready:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001837 if (scsi_target(sdev)->can_queue > 0)
1838 atomic_dec(&scsi_target(sdev)->target_busy);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001839 not_ready:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /*
1841 * lock q, handle tag, requeue req, and decrement device_busy. We
1842 * must return with queue_lock held.
1843 *
1844 * Decrementing device_busy without checking it is OK, as all such
1845 * cases (host limits or settings) should run the queue at some
1846 * later time.
1847 */
1848 spin_lock_irq(q->queue_lock);
1849 blk_requeue_request(q, req);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001850 atomic_dec(&sdev->device_busy);
Jens Axboea488e742010-04-16 21:13:15 +02001851out_delay:
Guenter Roeck480cadc2014-08-10 05:54:25 -07001852 if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
Jens Axboea488e742010-04-16 21:13:15 +02001853 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Christoph Hellwigd2852032014-01-17 12:06:53 +01001856static inline int prep_to_mq(int ret)
1857{
1858 switch (ret) {
1859 case BLKPREP_OK:
Omar Sandoval2868f132016-11-15 11:11:59 -08001860 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001861 case BLKPREP_DEFER:
1862 return BLK_MQ_RQ_QUEUE_BUSY;
1863 default:
1864 return BLK_MQ_RQ_QUEUE_ERROR;
1865 }
1866}
1867
1868static int scsi_mq_prep_fn(struct request *req)
1869{
1870 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1871 struct scsi_device *sdev = req->q->queuedata;
1872 struct Scsi_Host *shost = sdev->host;
1873 unsigned char *sense_buf = cmd->sense_buffer;
1874 struct scatterlist *sg;
1875
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001876 /* zero out the cmd, except for the embedded scsi_request */
1877 memset((char *)cmd + sizeof(cmd->req), 0,
1878 sizeof(*cmd) - sizeof(cmd->req));
Christoph Hellwigd2852032014-01-17 12:06:53 +01001879
1880 req->special = cmd;
1881
1882 cmd->request = req;
1883 cmd->device = sdev;
1884 cmd->sense_buffer = sense_buf;
1885
1886 cmd->tag = req->tag;
1887
Christoph Hellwigd2852032014-01-17 12:06:53 +01001888 cmd->prot_op = SCSI_PROT_NORMAL;
1889
1890 INIT_LIST_HEAD(&cmd->list);
1891 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1892 cmd->jiffies_at_alloc = jiffies;
1893
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +05301894 if (shost->use_cmd_list) {
1895 spin_lock_irq(&sdev->list_lock);
1896 list_add_tail(&cmd->list, &sdev->cmd_list);
1897 spin_unlock_irq(&sdev->list_lock);
1898 }
Christoph Hellwigd2852032014-01-17 12:06:53 +01001899
1900 sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
1901 cmd->sdb.table.sgl = sg;
1902
1903 if (scsi_host_get_prot(shost)) {
1904 cmd->prot_sdb = (void *)sg +
Tony Battersby120bb3e2014-12-08 17:20:52 -05001905 min_t(unsigned int,
Ming Lin65e86172016-04-04 14:48:10 -07001906 shost->sg_tablesize, SG_CHUNK_SIZE) *
Tony Battersby120bb3e2014-12-08 17:20:52 -05001907 sizeof(struct scatterlist);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001908 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
1909
1910 cmd->prot_sdb->table.sgl =
1911 (struct scatterlist *)(cmd->prot_sdb + 1);
1912 }
1913
1914 if (blk_bidi_rq(req)) {
1915 struct request *next_rq = req->next_rq;
1916 struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq);
1917
1918 memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer));
1919 bidi_sdb->table.sgl =
1920 (struct scatterlist *)(bidi_sdb + 1);
1921
1922 next_rq->special = bidi_sdb;
1923 }
1924
Christoph Hellwigfe052522014-09-22 15:59:31 +02001925 blk_mq_start_request(req);
1926
Christoph Hellwigd2852032014-01-17 12:06:53 +01001927 return scsi_setup_cmnd(sdev, req);
1928}
1929
1930static void scsi_mq_done(struct scsi_cmnd *cmd)
1931{
1932 trace_scsi_dispatch_cmd_done(cmd);
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001933 blk_mq_complete_request(cmd->request, cmd->request->errors);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001934}
1935
Jens Axboe74c45052014-10-29 11:14:52 -06001936static int scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
1937 const struct blk_mq_queue_data *bd)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001938{
Jens Axboe74c45052014-10-29 11:14:52 -06001939 struct request *req = bd->rq;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001940 struct request_queue *q = req->q;
1941 struct scsi_device *sdev = q->queuedata;
1942 struct Scsi_Host *shost = sdev->host;
1943 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1944 int ret;
1945 int reason;
1946
1947 ret = prep_to_mq(scsi_prep_state_check(sdev, req));
Omar Sandoval2868f132016-11-15 11:11:59 -08001948 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001949 goto out;
1950
1951 ret = BLK_MQ_RQ_QUEUE_BUSY;
1952 if (!get_device(&sdev->sdev_gendev))
1953 goto out;
1954
1955 if (!scsi_dev_queue_ready(q, sdev))
1956 goto out_put_device;
1957 if (!scsi_target_queue_ready(shost, sdev))
1958 goto out_dec_device_busy;
1959 if (!scsi_host_queue_ready(q, shost, sdev))
1960 goto out_dec_target_busy;
1961
Christoph Hellwige8064022016-10-20 15:12:13 +02001962 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001963 ret = prep_to_mq(scsi_mq_prep_fn(req));
Omar Sandoval2868f132016-11-15 11:11:59 -08001964 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001965 goto out_dec_host_busy;
Christoph Hellwige8064022016-10-20 15:12:13 +02001966 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwigfe052522014-09-22 15:59:31 +02001967 } else {
1968 blk_mq_start_request(req);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001969 }
1970
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001971 if (sdev->simple_tags)
1972 cmd->flags |= SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001973 else
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001974 cmd->flags &= ~SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001975
Christoph Hellwigd2852032014-01-17 12:06:53 +01001976 scsi_init_cmd_errh(cmd);
1977 cmd->scsi_done = scsi_mq_done;
1978
1979 reason = scsi_dispatch_cmd(cmd);
1980 if (reason) {
1981 scsi_set_blocked(cmd, reason);
1982 ret = BLK_MQ_RQ_QUEUE_BUSY;
1983 goto out_dec_host_busy;
1984 }
1985
1986 return BLK_MQ_RQ_QUEUE_OK;
1987
1988out_dec_host_busy:
1989 atomic_dec(&shost->host_busy);
1990out_dec_target_busy:
1991 if (scsi_target(sdev)->can_queue > 0)
1992 atomic_dec(&scsi_target(sdev)->target_busy);
1993out_dec_device_busy:
1994 atomic_dec(&sdev->device_busy);
1995out_put_device:
1996 put_device(&sdev->sdev_gendev);
1997out:
1998 switch (ret) {
1999 case BLK_MQ_RQ_QUEUE_BUSY:
Christoph Hellwigd2852032014-01-17 12:06:53 +01002000 if (atomic_read(&sdev->device_busy) == 0 &&
2001 !scsi_device_blocked(sdev))
2002 blk_mq_delay_queue(hctx, SCSI_QUEUE_DELAY);
2003 break;
2004 case BLK_MQ_RQ_QUEUE_ERROR:
2005 /*
2006 * Make sure to release all allocated ressources when
2007 * we hit an error, as we will never see this command
2008 * again.
2009 */
Christoph Hellwige8064022016-10-20 15:12:13 +02002010 if (req->rq_flags & RQF_DONTPREP)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002011 scsi_mq_uninit_cmd(cmd);
2012 break;
2013 default:
2014 break;
2015 }
2016 return ret;
2017}
2018
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002019static enum blk_eh_timer_return scsi_timeout(struct request *req,
2020 bool reserved)
2021{
2022 if (reserved)
2023 return BLK_EH_RESET_TIMER;
2024 return scsi_times_out(req);
2025}
2026
Christoph Hellwigd2852032014-01-17 12:06:53 +01002027static int scsi_init_request(void *data, struct request *rq,
2028 unsigned int hctx_idx, unsigned int request_idx,
2029 unsigned int numa_node)
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 cmd->sense_buffer =
2035 scsi_alloc_sense_buffer(shost, GFP_KERNEL, numa_node);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002036 if (!cmd->sense_buffer)
2037 return -ENOMEM;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002038 cmd->req.sense = cmd->sense_buffer;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002039 return 0;
2040}
2041
2042static void scsi_exit_request(void *data, struct request *rq,
2043 unsigned int hctx_idx, unsigned int request_idx)
2044{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002045 struct Scsi_Host *shost = data;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002046 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2047
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002048 scsi_free_sense_buffer(shost, cmd->sense_buffer);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002049}
2050
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002051static int scsi_map_queues(struct blk_mq_tag_set *set)
2052{
2053 struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
2054
2055 if (shost->hostt->map_queues)
2056 return shost->hostt->map_queues(shost);
2057 return blk_mq_map_queues(set);
2058}
2059
Christoph Hellwigf1bea552014-04-15 12:26:54 +02002060static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 struct device *host_dev;
2063 u64 bounce_limit = 0xffffffff;
2064
2065 if (shost->unchecked_isa_dma)
2066 return BLK_BOUNCE_ISA;
2067 /*
2068 * Platforms with virtual-DMA translation
2069 * hardware have no practical limit.
2070 */
2071 if (!PCI_DMA_BUS_IS_PHYS)
2072 return BLK_BOUNCE_ANY;
2073
2074 host_dev = scsi_get_device(shost);
2075 if (host_dev && host_dev->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +00002076 bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 return bounce_limit;
2079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Christoph Hellwigd48777a62017-01-02 21:52:10 +03002081void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Lin Ming6f381fa2012-04-12 13:50:38 +08002083 struct device *dev = shost->dma_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Jens Axboea8474ce2007-08-07 09:02:51 +02002085 /*
2086 * this limit is imposed by hardware restrictions
2087 */
Martin K. Petersen8a783622010-02-26 00:20:39 -05002088 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
Ming Lin65e86172016-04-04 14:48:10 -07002089 SG_MAX_SEGMENTS));
Jens Axboea8474ce2007-08-07 09:02:51 +02002090
Martin K. Petersen13f05c82010-09-10 20:50:10 +02002091 if (scsi_host_prot_dma(shost)) {
2092 shost->sg_prot_tablesize =
2093 min_not_zero(shost->sg_prot_tablesize,
2094 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
2095 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
2096 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
2097 }
2098
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002099 blk_queue_max_hw_sectors(q, shost->max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
2101 blk_queue_segment_boundary(q, shost->dma_boundary);
FUJITA Tomonori99c84db2008-02-04 22:28:17 -08002102 dma_set_seg_boundary(dev, shost->dma_boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
FUJITA Tomonori860ac562008-02-04 22:28:05 -08002104 blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (!shost->use_clustering)
Martin K. Petersene692cb62010-12-01 19:41:49 +01002107 q->limits.cluster = 0;
James Bottomley465ff312008-01-01 10:00:10 -06002108
2109 /*
2110 * set a reasonable default alignment on word boundaries: the
2111 * host and device may alter it using
2112 * blk_queue_update_dma_alignment() later.
2113 */
2114 blk_queue_dma_alignment(q, 0x03);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002115}
Christoph Hellwigd48777a62017-01-02 21:52:10 +03002116EXPORT_SYMBOL_GPL(__scsi_init_queue);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002117
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002118static int scsi_init_rq(struct request_queue *q, struct request *rq, gfp_t gfp)
2119{
2120 struct Scsi_Host *shost = q->rq_alloc_data;
2121 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2122
2123 memset(cmd, 0, sizeof(*cmd));
2124
2125 cmd->sense_buffer = scsi_alloc_sense_buffer(shost, gfp, NUMA_NO_NODE);
2126 if (!cmd->sense_buffer)
2127 goto fail;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002128 cmd->req.sense = cmd->sense_buffer;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002129
2130 if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
2131 cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp);
2132 if (!cmd->prot_sdb)
2133 goto fail_free_sense;
2134 }
2135
2136 return 0;
2137
2138fail_free_sense:
2139 scsi_free_sense_buffer(shost, cmd->sense_buffer);
2140fail:
2141 return -ENOMEM;
2142}
2143
2144static void scsi_exit_rq(struct request_queue *q, struct request *rq)
2145{
2146 struct Scsi_Host *shost = q->rq_alloc_data;
2147 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2148
2149 if (cmd->prot_sdb)
2150 kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
2151 scsi_free_sense_buffer(shost, cmd->sense_buffer);
2152}
2153
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002154struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
2155{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002156 struct Scsi_Host *shost = sdev->host;
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002157 struct request_queue *q;
2158
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002159 q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002160 if (!q)
2161 return NULL;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002162 q->cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
2163 q->rq_alloc_data = shost;
2164 q->request_fn = scsi_request_fn;
2165 q->init_rq_fn = scsi_init_rq;
2166 q->exit_rq_fn = scsi_exit_rq;
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002167
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002168 if (blk_init_allocated_queue(q) < 0) {
2169 blk_cleanup_queue(q);
2170 return NULL;
2171 }
2172
2173 __scsi_init_queue(shost, q);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002174 blk_queue_prep_rq(q, scsi_prep_fn);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02002175 blk_queue_unprep_rq(q, scsi_unprep_fn);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002176 blk_queue_softirq_done(q, scsi_softirq_done);
Jens Axboe242f9dc2008-09-14 05:55:09 -07002177 blk_queue_rq_timed_out(q, scsi_times_out);
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04002178 blk_queue_lld_busy(q, scsi_lld_busy);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002179 return q;
2180}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Christoph Hellwigd2852032014-01-17 12:06:53 +01002182static struct blk_mq_ops scsi_mq_ops = {
Christoph Hellwigd2852032014-01-17 12:06:53 +01002183 .queue_rq = scsi_queue_rq,
2184 .complete = scsi_softirq_done,
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002185 .timeout = scsi_timeout,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002186 .init_request = scsi_init_request,
2187 .exit_request = scsi_exit_request,
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002188 .map_queues = scsi_map_queues,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002189};
2190
2191struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
2192{
2193 sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
2194 if (IS_ERR(sdev->request_queue))
2195 return NULL;
2196
2197 sdev->request_queue->queuedata = sdev;
2198 __scsi_init_queue(sdev->host, sdev->request_queue);
2199 return sdev->request_queue;
2200}
2201
2202int scsi_mq_setup_tags(struct Scsi_Host *shost)
2203{
2204 unsigned int cmd_size, sgl_size, tbl_size;
2205
2206 tbl_size = shost->sg_tablesize;
Ming Lin65e86172016-04-04 14:48:10 -07002207 if (tbl_size > SG_CHUNK_SIZE)
2208 tbl_size = SG_CHUNK_SIZE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002209 sgl_size = tbl_size * sizeof(struct scatterlist);
2210 cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
2211 if (scsi_host_get_prot(shost))
2212 cmd_size += sizeof(struct scsi_data_buffer) + sgl_size;
2213
2214 memset(&shost->tag_set, 0, sizeof(shost->tag_set));
2215 shost->tag_set.ops = &scsi_mq_ops;
Bart Van Asscheefec4b92014-10-30 14:45:36 +01002216 shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002217 shost->tag_set.queue_depth = shost->can_queue;
2218 shost->tag_set.cmd_size = cmd_size;
2219 shost->tag_set.numa_node = NUMA_NO_NODE;
2220 shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Shaohua Li24391c02015-01-23 14:18:00 -07002221 shost->tag_set.flags |=
2222 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002223 shost->tag_set.driver_data = shost;
2224
2225 return blk_mq_alloc_tag_set(&shost->tag_set);
2226}
2227
2228void scsi_mq_destroy_tags(struct Scsi_Host *shost)
2229{
2230 blk_mq_free_tag_set(&shost->tag_set);
2231}
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233/*
2234 * Function: scsi_block_requests()
2235 *
2236 * Purpose: Utility function used by low-level drivers to prevent further
2237 * commands from being queued to the device.
2238 *
2239 * Arguments: shost - Host in question
2240 *
2241 * Returns: Nothing
2242 *
2243 * Lock status: No locks are assumed held.
2244 *
2245 * Notes: There is no timer nor any other means by which the requests
2246 * get unblocked other than the low-level driver calling
2247 * scsi_unblock_requests().
2248 */
2249void scsi_block_requests(struct Scsi_Host *shost)
2250{
2251 shost->host_self_blocked = 1;
2252}
2253EXPORT_SYMBOL(scsi_block_requests);
2254
2255/*
2256 * Function: scsi_unblock_requests()
2257 *
2258 * Purpose: Utility function used by low-level drivers to allow further
2259 * commands from being queued to the device.
2260 *
2261 * Arguments: shost - Host in question
2262 *
2263 * Returns: Nothing
2264 *
2265 * Lock status: No locks are assumed held.
2266 *
2267 * Notes: There is no timer nor any other means by which the requests
2268 * get unblocked other than the low-level driver calling
2269 * scsi_unblock_requests().
2270 *
2271 * This is done as an API function so that changes to the
2272 * internals of the scsi mid-layer won't require wholesale
2273 * changes to drivers that use this feature.
2274 */
2275void scsi_unblock_requests(struct Scsi_Host *shost)
2276{
2277 shost->host_self_blocked = 0;
2278 scsi_run_host_queues(shost);
2279}
2280EXPORT_SYMBOL(scsi_unblock_requests);
2281
2282int __init scsi_init_queue(void)
2283{
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002284 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
2285 sizeof(struct scsi_data_buffer),
2286 0, 0, NULL);
2287 if (!scsi_sdb_cache) {
2288 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
FUJITA Tomonorif0787272008-12-14 01:23:45 +09002289 return -ENOMEM;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02002290 }
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return 0;
2293}
2294
2295void scsi_exit_queue(void)
2296{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002297 kmem_cache_destroy(scsi_sense_cache);
2298 kmem_cache_destroy(scsi_sense_isadma_cache);
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002299 kmem_cache_destroy(scsi_sdb_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
James Bottomley5baba832006-03-18 14:10:35 -06002301
2302/**
2303 * scsi_mode_select - issue a mode select
2304 * @sdev: SCSI device to be queried
2305 * @pf: Page format bit (1 == standard, 0 == vendor specific)
2306 * @sp: Save page bit (0 == don't save, 1 == save)
2307 * @modepage: mode page being requested
2308 * @buffer: request buffer (may not be smaller than eight bytes)
2309 * @len: length of request buffer.
2310 * @timeout: command timeout
2311 * @retries: number of retries before failing
2312 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002313 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley5baba832006-03-18 14:10:35 -06002314 * must be SCSI_SENSE_BUFFERSIZE big.
2315 *
2316 * Returns zero if successful; negative error number or scsi
2317 * status on error
2318 *
2319 */
2320int
2321scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
2322 unsigned char *buffer, int len, int timeout, int retries,
2323 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2324{
2325 unsigned char cmd[10];
2326 unsigned char *real_buffer;
2327 int ret;
2328
2329 memset(cmd, 0, sizeof(cmd));
2330 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2331
2332 if (sdev->use_10_for_ms) {
2333 if (len > 65535)
2334 return -EINVAL;
2335 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2336 if (!real_buffer)
2337 return -ENOMEM;
2338 memcpy(real_buffer + 8, buffer, len);
2339 len += 8;
2340 real_buffer[0] = 0;
2341 real_buffer[1] = 0;
2342 real_buffer[2] = data->medium_type;
2343 real_buffer[3] = data->device_specific;
2344 real_buffer[4] = data->longlba ? 0x01 : 0;
2345 real_buffer[5] = 0;
2346 real_buffer[6] = data->block_descriptor_length >> 8;
2347 real_buffer[7] = data->block_descriptor_length;
2348
2349 cmd[0] = MODE_SELECT_10;
2350 cmd[7] = len >> 8;
2351 cmd[8] = len;
2352 } else {
2353 if (len > 255 || data->block_descriptor_length > 255 ||
2354 data->longlba)
2355 return -EINVAL;
2356
2357 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2358 if (!real_buffer)
2359 return -ENOMEM;
2360 memcpy(real_buffer + 4, buffer, len);
2361 len += 4;
2362 real_buffer[0] = 0;
2363 real_buffer[1] = data->medium_type;
2364 real_buffer[2] = data->device_specific;
2365 real_buffer[3] = data->block_descriptor_length;
2366
2367
2368 cmd[0] = MODE_SELECT;
2369 cmd[4] = len;
2370 }
2371
2372 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002373 sshdr, timeout, retries, NULL);
James Bottomley5baba832006-03-18 14:10:35 -06002374 kfree(real_buffer);
2375 return ret;
2376}
2377EXPORT_SYMBOL_GPL(scsi_mode_select);
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379/**
Rob Landleyeb448202007-11-03 13:30:39 -05002380 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
James Bottomley1cf72692005-08-28 11:27:01 -05002381 * @sdev: SCSI device to be queried
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 * @dbd: set if mode sense will allow block descriptors to be returned
2383 * @modepage: mode page being requested
2384 * @buffer: request buffer (may not be smaller than eight bytes)
2385 * @len: length of request buffer.
2386 * @timeout: command timeout
2387 * @retries: number of retries before failing
2388 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002389 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley1cf72692005-08-28 11:27:01 -05002390 * must be SCSI_SENSE_BUFFERSIZE big.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 *
2392 * Returns zero if unsuccessful, or the header offset (either 4
2393 * or 8 depending on whether a six or ten byte command was
2394 * issued) if successful.
Rob Landleyeb448202007-11-03 13:30:39 -05002395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396int
James Bottomley1cf72692005-08-28 11:27:01 -05002397scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 unsigned char *buffer, int len, int timeout, int retries,
James Bottomley5baba832006-03-18 14:10:35 -06002399 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 unsigned char cmd[12];
2402 int use_10_for_ms;
2403 int header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002404 int result, retry_count = retries;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002405 struct scsi_sense_hdr my_sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 memset(data, 0, sizeof(*data));
2408 memset(&cmd[0], 0, 12);
2409 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
2410 cmd[2] = modepage;
2411
James Bottomleyea73a9f2005-08-28 11:33:52 -05002412 /* caller might not be interested in sense, but we need it */
2413 if (!sshdr)
2414 sshdr = &my_sshdr;
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 retry:
James Bottomley1cf72692005-08-28 11:27:01 -05002417 use_10_for_ms = sdev->use_10_for_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 if (use_10_for_ms) {
2420 if (len < 8)
2421 len = 8;
2422
2423 cmd[0] = MODE_SENSE_10;
2424 cmd[8] = len;
2425 header_length = 8;
2426 } else {
2427 if (len < 4)
2428 len = 4;
2429
2430 cmd[0] = MODE_SENSE;
2431 cmd[4] = len;
2432 header_length = 4;
2433 }
2434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 memset(buffer, 0, len);
2436
James Bottomley1cf72692005-08-28 11:27:01 -05002437 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002438 sshdr, timeout, retries, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 /* This code looks awful: what it's doing is making sure an
2441 * ILLEGAL REQUEST sense return identifies the actual command
2442 * byte as the problem. MODE_SENSE commands can return
2443 * ILLEGAL REQUEST if the code page isn't supported */
2444
James Bottomley1cf72692005-08-28 11:27:01 -05002445 if (use_10_for_ms && !scsi_status_is_good(result) &&
2446 (driver_byte(result) & DRIVER_SENSE)) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05002447 if (scsi_sense_valid(sshdr)) {
2448 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2449 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /*
2451 * Invalid command operation code
2452 */
James Bottomley1cf72692005-08-28 11:27:01 -05002453 sdev->use_10_for_ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto retry;
2455 }
2456 }
2457 }
2458
James Bottomley1cf72692005-08-28 11:27:01 -05002459 if(scsi_status_is_good(result)) {
Al Viro6d73c852006-02-23 02:03:16 +01002460 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2461 (modepage == 6 || modepage == 8))) {
2462 /* Initio breakage? */
2463 header_length = 0;
2464 data->length = 13;
2465 data->medium_type = 0;
2466 data->device_specific = 0;
2467 data->longlba = 0;
2468 data->block_descriptor_length = 0;
2469 } else if(use_10_for_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 data->length = buffer[0]*256 + buffer[1] + 2;
2471 data->medium_type = buffer[2];
2472 data->device_specific = buffer[3];
2473 data->longlba = buffer[4] & 0x01;
2474 data->block_descriptor_length = buffer[6]*256
2475 + buffer[7];
2476 } else {
2477 data->length = buffer[0] + 1;
2478 data->medium_type = buffer[1];
2479 data->device_specific = buffer[2];
2480 data->block_descriptor_length = buffer[3];
2481 }
Al Viro6d73c852006-02-23 02:03:16 +01002482 data->header_length = header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002483 } else if ((status_byte(result) == CHECK_CONDITION) &&
2484 scsi_sense_valid(sshdr) &&
2485 sshdr->sense_key == UNIT_ATTENTION && retry_count) {
2486 retry_count--;
2487 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489
James Bottomley1cf72692005-08-28 11:27:01 -05002490 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492EXPORT_SYMBOL(scsi_mode_sense);
2493
James Bottomley001aac22007-12-02 19:10:40 +02002494/**
2495 * scsi_test_unit_ready - test if unit is ready
2496 * @sdev: scsi device to change the state of.
2497 * @timeout: command timeout
2498 * @retries: number of retries before failing
2499 * @sshdr_external: Optional pointer to struct scsi_sense_hdr for
2500 * returning sense. Make sure that this is cleared before passing
2501 * in.
2502 *
2503 * Returns zero if unsuccessful or an error if TUR failed. For
Tejun Heo9f8a2c22010-12-08 20:57:40 +01002504 * removable media, UNIT_ATTENTION sets ->changed flag.
James Bottomley001aac22007-12-02 19:10:40 +02002505 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506int
James Bottomley001aac22007-12-02 19:10:40 +02002507scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
2508 struct scsi_sense_hdr *sshdr_external)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 char cmd[] = {
2511 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2512 };
James Bottomley001aac22007-12-02 19:10:40 +02002513 struct scsi_sense_hdr *sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 int result;
James Bottomley001aac22007-12-02 19:10:40 +02002515
2516 if (!sshdr_external)
2517 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
2518 else
2519 sshdr = sshdr_external;
2520
2521 /* try to eat the UNIT_ATTENTION if there are enough retries */
2522 do {
2523 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002524 timeout, retries, NULL);
James Bottomley32c356d2008-08-20 00:58:13 +02002525 if (sdev->removable && scsi_sense_valid(sshdr) &&
2526 sshdr->sense_key == UNIT_ATTENTION)
2527 sdev->changed = 1;
2528 } while (scsi_sense_valid(sshdr) &&
2529 sshdr->sense_key == UNIT_ATTENTION && --retries);
James Bottomley001aac22007-12-02 19:10:40 +02002530
James Bottomley001aac22007-12-02 19:10:40 +02002531 if (!sshdr_external)
2532 kfree(sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return result;
2534}
2535EXPORT_SYMBOL(scsi_test_unit_ready);
2536
2537/**
Rob Landleyeb448202007-11-03 13:30:39 -05002538 * scsi_device_set_state - Take the given device through the device state model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 * @sdev: scsi device to change the state of.
2540 * @state: state to change to.
2541 *
2542 * Returns zero if unsuccessful or an error if the requested
2543 * transition is illegal.
Rob Landleyeb448202007-11-03 13:30:39 -05002544 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545int
2546scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2547{
2548 enum scsi_device_state oldstate = sdev->sdev_state;
2549
2550 if (state == oldstate)
2551 return 0;
2552
2553 switch (state) {
2554 case SDEV_CREATED:
James Bottomley6f4267e2008-08-22 16:53:31 -05002555 switch (oldstate) {
2556 case SDEV_CREATED_BLOCK:
2557 break;
2558 default:
2559 goto illegal;
2560 }
2561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 case SDEV_RUNNING:
2564 switch (oldstate) {
2565 case SDEV_CREATED:
2566 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002567 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 case SDEV_QUIESCE:
2569 case SDEV_BLOCK:
2570 break;
2571 default:
2572 goto illegal;
2573 }
2574 break;
2575
2576 case SDEV_QUIESCE:
2577 switch (oldstate) {
2578 case SDEV_RUNNING:
2579 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002580 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 break;
2582 default:
2583 goto illegal;
2584 }
2585 break;
2586
2587 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002588 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 switch (oldstate) {
2590 case SDEV_CREATED:
2591 case SDEV_RUNNING:
2592 case SDEV_QUIESCE:
2593 case SDEV_BLOCK:
2594 break;
2595 default:
2596 goto illegal;
2597 }
2598 break;
2599
2600 case SDEV_BLOCK:
2601 switch (oldstate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 case SDEV_RUNNING:
James Bottomley6f4267e2008-08-22 16:53:31 -05002603 case SDEV_CREATED_BLOCK:
2604 break;
2605 default:
2606 goto illegal;
2607 }
2608 break;
2609
2610 case SDEV_CREATED_BLOCK:
2611 switch (oldstate) {
2612 case SDEV_CREATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 break;
2614 default:
2615 goto illegal;
2616 }
2617 break;
2618
2619 case SDEV_CANCEL:
2620 switch (oldstate) {
2621 case SDEV_CREATED:
2622 case SDEV_RUNNING:
Alan Stern9ea72902006-06-23 14:25:34 -04002623 case SDEV_QUIESCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002625 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 case SDEV_BLOCK:
2627 break;
2628 default:
2629 goto illegal;
2630 }
2631 break;
2632
2633 case SDEV_DEL:
2634 switch (oldstate) {
Brian King309bd272006-06-27 11:10:43 -05002635 case SDEV_CREATED:
2636 case SDEV_RUNNING:
2637 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002638 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 case SDEV_CANCEL:
Bart Van Assche0516c082013-07-02 15:06:33 +02002640 case SDEV_CREATED_BLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 break;
2642 default:
2643 goto illegal;
2644 }
2645 break;
2646
2647 }
2648 sdev->sdev_state = state;
2649 return 0;
2650
2651 illegal:
Hannes Reinecke91921e02014-06-25 16:39:59 +02002652 SCSI_LOG_ERROR_RECOVERY(1,
James Bottomley9ccfc752005-10-02 11:45:08 -05002653 sdev_printk(KERN_ERR, sdev,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002654 "Illegal state transition %s->%s",
James Bottomley9ccfc752005-10-02 11:45:08 -05002655 scsi_device_state_name(oldstate),
2656 scsi_device_state_name(state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 );
2658 return -EINVAL;
2659}
2660EXPORT_SYMBOL(scsi_device_set_state);
2661
2662/**
Jeff Garzika341cd02007-10-29 17:15:22 -04002663 * sdev_evt_emit - emit a single SCSI device uevent
2664 * @sdev: associated SCSI device
2665 * @evt: event to emit
2666 *
2667 * Send a single uevent (scsi_event) to the associated scsi_device.
2668 */
2669static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2670{
2671 int idx = 0;
2672 char *envp[3];
2673
2674 switch (evt->evt_type) {
2675 case SDEV_EVT_MEDIA_CHANGE:
2676 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2677 break;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002678 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
Hannes Reinecked3d32892016-02-19 09:17:16 +01002679 scsi_rescan_device(&sdev->sdev_gendev);
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002680 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2681 break;
2682 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2683 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2684 break;
2685 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2686 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2687 break;
2688 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2689 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2690 break;
2691 case SDEV_EVT_LUN_CHANGE_REPORTED:
2692 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2693 break;
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002694 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2695 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
2696 break;
Jeff Garzika341cd02007-10-29 17:15:22 -04002697 default:
2698 /* do nothing */
2699 break;
2700 }
2701
2702 envp[idx++] = NULL;
2703
2704 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2705}
2706
2707/**
2708 * sdev_evt_thread - send a uevent for each scsi event
2709 * @work: work struct for scsi_device
2710 *
2711 * Dispatch queued events to their associated scsi_device kobjects
2712 * as uevents.
2713 */
2714void scsi_evt_thread(struct work_struct *work)
2715{
2716 struct scsi_device *sdev;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002717 enum scsi_device_event evt_type;
Jeff Garzika341cd02007-10-29 17:15:22 -04002718 LIST_HEAD(event_list);
2719
2720 sdev = container_of(work, struct scsi_device, event_work);
2721
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002722 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2723 if (test_and_clear_bit(evt_type, sdev->pending_events))
2724 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2725
Jeff Garzika341cd02007-10-29 17:15:22 -04002726 while (1) {
2727 struct scsi_event *evt;
2728 struct list_head *this, *tmp;
2729 unsigned long flags;
2730
2731 spin_lock_irqsave(&sdev->list_lock, flags);
2732 list_splice_init(&sdev->event_list, &event_list);
2733 spin_unlock_irqrestore(&sdev->list_lock, flags);
2734
2735 if (list_empty(&event_list))
2736 break;
2737
2738 list_for_each_safe(this, tmp, &event_list) {
2739 evt = list_entry(this, struct scsi_event, node);
2740 list_del(&evt->node);
2741 scsi_evt_emit(sdev, evt);
2742 kfree(evt);
2743 }
2744 }
2745}
2746
2747/**
2748 * sdev_evt_send - send asserted event to uevent thread
2749 * @sdev: scsi_device event occurred on
2750 * @evt: event to send
2751 *
2752 * Assert scsi device event asynchronously.
2753 */
2754void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2755{
2756 unsigned long flags;
2757
Kay Sievers4d1566e2008-03-19 13:04:47 +01002758#if 0
2759 /* FIXME: currently this check eliminates all media change events
2760 * for polled devices. Need to update to discriminate between AN
2761 * and polled events */
Jeff Garzika341cd02007-10-29 17:15:22 -04002762 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2763 kfree(evt);
2764 return;
2765 }
Kay Sievers4d1566e2008-03-19 13:04:47 +01002766#endif
Jeff Garzika341cd02007-10-29 17:15:22 -04002767
2768 spin_lock_irqsave(&sdev->list_lock, flags);
2769 list_add_tail(&evt->node, &sdev->event_list);
2770 schedule_work(&sdev->event_work);
2771 spin_unlock_irqrestore(&sdev->list_lock, flags);
2772}
2773EXPORT_SYMBOL_GPL(sdev_evt_send);
2774
2775/**
2776 * sdev_evt_alloc - allocate a new scsi event
2777 * @evt_type: type of event to allocate
2778 * @gfpflags: GFP flags for allocation
2779 *
2780 * Allocates and returns a new scsi_event.
2781 */
2782struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2783 gfp_t gfpflags)
2784{
2785 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2786 if (!evt)
2787 return NULL;
2788
2789 evt->evt_type = evt_type;
2790 INIT_LIST_HEAD(&evt->node);
2791
2792 /* evt_type-specific initialization, if any */
2793 switch (evt_type) {
2794 case SDEV_EVT_MEDIA_CHANGE:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002795 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2796 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2797 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2798 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2799 case SDEV_EVT_LUN_CHANGE_REPORTED:
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002800 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
Jeff Garzika341cd02007-10-29 17:15:22 -04002801 default:
2802 /* do nothing */
2803 break;
2804 }
2805
2806 return evt;
2807}
2808EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2809
2810/**
2811 * sdev_evt_send_simple - send asserted event to uevent thread
2812 * @sdev: scsi_device event occurred on
2813 * @evt_type: type of event to send
2814 * @gfpflags: GFP flags for allocation
2815 *
2816 * Assert scsi device event asynchronously, given an event type.
2817 */
2818void sdev_evt_send_simple(struct scsi_device *sdev,
2819 enum scsi_device_event evt_type, gfp_t gfpflags)
2820{
2821 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2822 if (!evt) {
2823 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2824 evt_type);
2825 return;
2826 }
2827
2828 sdev_evt_send(sdev, evt);
2829}
2830EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2831
2832/**
Bart Van Assche669f0442016-11-22 16:17:13 -08002833 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
2834 * @sdev: SCSI device to count the number of scsi_request_fn() callers for.
2835 */
2836static int scsi_request_fn_active(struct scsi_device *sdev)
2837{
2838 struct request_queue *q = sdev->request_queue;
2839 int request_fn_active;
2840
2841 WARN_ON_ONCE(sdev->host->use_blk_mq);
2842
2843 spin_lock_irq(q->queue_lock);
2844 request_fn_active = q->request_fn_active;
2845 spin_unlock_irq(q->queue_lock);
2846
2847 return request_fn_active;
2848}
2849
2850/**
2851 * scsi_wait_for_queuecommand() - wait for ongoing queuecommand() calls
2852 * @sdev: SCSI device pointer.
2853 *
2854 * Wait until the ongoing shost->hostt->queuecommand() calls that are
2855 * invoked from scsi_request_fn() have finished.
2856 */
2857static void scsi_wait_for_queuecommand(struct scsi_device *sdev)
2858{
2859 WARN_ON_ONCE(sdev->host->use_blk_mq);
2860
2861 while (scsi_request_fn_active(sdev))
2862 msleep(20);
2863}
2864
2865/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 * scsi_device_quiesce - Block user issued commands.
2867 * @sdev: scsi device to quiesce.
2868 *
2869 * This works by trying to transition to the SDEV_QUIESCE state
2870 * (which must be a legal transition). When the device is in this
2871 * state, only special requests will be accepted, all others will
2872 * be deferred. Since special requests may also be requeued requests,
2873 * a successful return doesn't guarantee the device will be
2874 * totally quiescent.
2875 *
2876 * Must be called with user context, may sleep.
2877 *
2878 * Returns zero if unsuccessful or an error if not.
Rob Landleyeb448202007-11-03 13:30:39 -05002879 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880int
2881scsi_device_quiesce(struct scsi_device *sdev)
2882{
2883 int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2884 if (err)
2885 return err;
2886
2887 scsi_run_queue(sdev->request_queue);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02002888 while (atomic_read(&sdev->device_busy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 msleep_interruptible(200);
2890 scsi_run_queue(sdev->request_queue);
2891 }
2892 return 0;
2893}
2894EXPORT_SYMBOL(scsi_device_quiesce);
2895
2896/**
2897 * scsi_device_resume - Restart user issued commands to a quiesced device.
2898 * @sdev: scsi device to resume.
2899 *
2900 * Moves the device from quiesced back to running and restarts the
2901 * queues.
2902 *
2903 * Must be called with user context, may sleep.
Rob Landleyeb448202007-11-03 13:30:39 -05002904 */
Dan Williamsa7a20d12012-03-22 17:05:11 -07002905void scsi_device_resume(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Dan Williamsa7a20d12012-03-22 17:05:11 -07002907 /* check if the device state was mutated prior to resume, and if
2908 * so assume the state is being managed elsewhere (for example
2909 * device deleted during suspend)
2910 */
2911 if (sdev->sdev_state != SDEV_QUIESCE ||
2912 scsi_device_set_state(sdev, SDEV_RUNNING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return;
2914 scsi_run_queue(sdev->request_queue);
2915}
2916EXPORT_SYMBOL(scsi_device_resume);
2917
2918static void
2919device_quiesce_fn(struct scsi_device *sdev, void *data)
2920{
2921 scsi_device_quiesce(sdev);
2922}
2923
2924void
2925scsi_target_quiesce(struct scsi_target *starget)
2926{
2927 starget_for_each_device(starget, NULL, device_quiesce_fn);
2928}
2929EXPORT_SYMBOL(scsi_target_quiesce);
2930
2931static void
2932device_resume_fn(struct scsi_device *sdev, void *data)
2933{
2934 scsi_device_resume(sdev);
2935}
2936
2937void
2938scsi_target_resume(struct scsi_target *starget)
2939{
2940 starget_for_each_device(starget, NULL, device_resume_fn);
2941}
2942EXPORT_SYMBOL(scsi_target_resume);
2943
2944/**
Rob Landleyeb448202007-11-03 13:30:39 -05002945 * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 * @sdev: device to block
2947 *
2948 * Block request made by scsi lld's to temporarily stop all
Bart Van Assche669f0442016-11-22 16:17:13 -08002949 * scsi commands on the specified device. May sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 *
2951 * Returns zero if successful or error if not
2952 *
2953 * Notes:
2954 * This routine transitions the device to the SDEV_BLOCK state
2955 * (which must be a legal transition). When the device is in this
2956 * state, all commands are deferred until the scsi lld reenables
2957 * the device with scsi_device_unblock or device_block_tmo fires.
Bart Van Assche669f0442016-11-22 16:17:13 -08002958 *
2959 * To do: avoid that scsi_send_eh_cmnd() calls queuecommand() after
2960 * scsi_internal_device_block() has blocked a SCSI device and also
2961 * remove the rport mutex lock and unlock calls from srp_queuecommand().
Rob Landleyeb448202007-11-03 13:30:39 -05002962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963int
2964scsi_internal_device_block(struct scsi_device *sdev)
2965{
Jens Axboe165125e2007-07-24 09:28:11 +02002966 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 unsigned long flags;
2968 int err = 0;
2969
2970 err = scsi_device_set_state(sdev, SDEV_BLOCK);
James Bottomley6f4267e2008-08-22 16:53:31 -05002971 if (err) {
2972 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2973
2974 if (err)
2975 return err;
2976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 /*
2979 * The device has transitioned to SDEV_BLOCK. Stop the
2980 * block layer from calling the midlayer with this device's
2981 * request queue.
2982 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01002983 if (q->mq_ops) {
Bart Van Assche7dbbf0f2016-11-22 16:17:50 -08002984 blk_mq_quiesce_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002985 } else {
2986 spin_lock_irqsave(q->queue_lock, flags);
2987 blk_stop_queue(q);
2988 spin_unlock_irqrestore(q->queue_lock, flags);
Bart Van Assche669f0442016-11-22 16:17:13 -08002989 scsi_wait_for_queuecommand(sdev);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 return 0;
2993}
2994EXPORT_SYMBOL_GPL(scsi_internal_device_block);
2995
2996/**
2997 * scsi_internal_device_unblock - resume a device after a block request
2998 * @sdev: device to resume
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002999 * @new_state: state to set devices to after unblocking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 *
3001 * Called by scsi lld's or the midlayer to restart the device queue
3002 * for the previously suspended scsi device. Called from interrupt or
3003 * normal process context.
3004 *
3005 * Returns zero if successful or error if not.
3006 *
3007 * Notes:
3008 * This routine transitions the device to the SDEV_RUNNING state
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003009 * or to one of the offline states (which must be a legal transition)
Mike Christied0754982012-05-17 23:56:58 -05003010 * allowing the midlayer to goose the queue for this device.
Rob Landleyeb448202007-11-03 13:30:39 -05003011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012int
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003013scsi_internal_device_unblock(struct scsi_device *sdev,
3014 enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Jens Axboe165125e2007-07-24 09:28:11 +02003016 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 unsigned long flags;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003018
3019 /*
3020 * Try to transition the scsi device to SDEV_RUNNING or one of the
3021 * offlined states and goose the device queue if successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 */
Vikas Chaudhary0e580762012-08-09 04:51:30 -04003023 if ((sdev->sdev_state == SDEV_BLOCK) ||
3024 (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003025 sdev->sdev_state = new_state;
3026 else if (sdev->sdev_state == SDEV_CREATED_BLOCK) {
3027 if (new_state == SDEV_TRANSPORT_OFFLINE ||
3028 new_state == SDEV_OFFLINE)
3029 sdev->sdev_state = new_state;
3030 else
3031 sdev->sdev_state = SDEV_CREATED;
3032 } else if (sdev->sdev_state != SDEV_CANCEL &&
Mike Christie986fe6c2010-10-06 03:10:59 -05003033 sdev->sdev_state != SDEV_OFFLINE)
Takahiro Yasui5c10e632009-04-29 12:13:02 -04003034 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Christoph Hellwigd2852032014-01-17 12:06:53 +01003036 if (q->mq_ops) {
3037 blk_mq_start_stopped_hw_queues(q, false);
3038 } else {
3039 spin_lock_irqsave(q->queue_lock, flags);
3040 blk_start_queue(q);
3041 spin_unlock_irqrestore(q->queue_lock, flags);
3042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 return 0;
3045}
3046EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
3047
3048static void
3049device_block(struct scsi_device *sdev, void *data)
3050{
3051 scsi_internal_device_block(sdev);
3052}
3053
3054static int
3055target_block(struct device *dev, void *data)
3056{
3057 if (scsi_is_target_device(dev))
3058 starget_for_each_device(to_scsi_target(dev), NULL,
3059 device_block);
3060 return 0;
3061}
3062
3063void
3064scsi_target_block(struct device *dev)
3065{
3066 if (scsi_is_target_device(dev))
3067 starget_for_each_device(to_scsi_target(dev), NULL,
3068 device_block);
3069 else
3070 device_for_each_child(dev, NULL, target_block);
3071}
3072EXPORT_SYMBOL_GPL(scsi_target_block);
3073
3074static void
3075device_unblock(struct scsi_device *sdev, void *data)
3076{
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003077 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
3080static int
3081target_unblock(struct device *dev, void *data)
3082{
3083 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003084 starget_for_each_device(to_scsi_target(dev), data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 device_unblock);
3086 return 0;
3087}
3088
3089void
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003090scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
3092 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003093 starget_for_each_device(to_scsi_target(dev), &new_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 device_unblock);
3095 else
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003096 device_for_each_child(dev, &new_state, target_unblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098EXPORT_SYMBOL_GPL(scsi_target_unblock);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003099
3100/**
3101 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
Rob Landleyeb448202007-11-03 13:30:39 -05003102 * @sgl: scatter-gather list
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003103 * @sg_count: number of segments in sg
3104 * @offset: offset in bytes into sg, on return offset into the mapped area
3105 * @len: bytes to map, on return number of bytes mapped
3106 *
3107 * Returns virtual address of the start of the mapped page
3108 */
Jens Axboec6132da2007-10-16 11:08:49 +02003109void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003110 size_t *offset, size_t *len)
3111{
3112 int i;
3113 size_t sg_len = 0, len_complete = 0;
Jens Axboec6132da2007-10-16 11:08:49 +02003114 struct scatterlist *sg;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003115 struct page *page;
3116
Andrew Morton22cfefb2007-02-05 16:39:03 -08003117 WARN_ON(!irqs_disabled());
3118
Jens Axboec6132da2007-10-16 11:08:49 +02003119 for_each_sg(sgl, sg, sg_count, i) {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003120 len_complete = sg_len; /* Complete sg-entries */
Jens Axboec6132da2007-10-16 11:08:49 +02003121 sg_len += sg->length;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003122 if (sg_len > *offset)
3123 break;
3124 }
3125
3126 if (unlikely(i == sg_count)) {
Andrew Morton169e1a22006-04-18 21:09:08 -07003127 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
3128 "elements %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003129 __func__, sg_len, *offset, sg_count);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003130 WARN_ON(1);
3131 return NULL;
3132 }
3133
3134 /* Offset starting from the beginning of first page in this sg-entry */
Jens Axboec6132da2007-10-16 11:08:49 +02003135 *offset = *offset - len_complete + sg->offset;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003136
3137 /* Assumption: contiguous pages can be accessed as "page + i" */
Jens Axboe45711f12007-10-22 21:19:53 +02003138 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003139 *offset &= ~PAGE_MASK;
3140
3141 /* Bytes in this sg-entry from *offset to the end of the page */
3142 sg_len = PAGE_SIZE - *offset;
3143 if (*len > sg_len)
3144 *len = sg_len;
3145
Cong Wang77dfce02011-11-25 23:14:23 +08003146 return kmap_atomic(page);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003147}
3148EXPORT_SYMBOL(scsi_kmap_atomic_sg);
3149
3150/**
Rob Landleyeb448202007-11-03 13:30:39 -05003151 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003152 * @virt: virtual address to be unmapped
3153 */
3154void scsi_kunmap_atomic_sg(void *virt)
3155{
Cong Wang77dfce02011-11-25 23:14:23 +08003156 kunmap_atomic(virt);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003157}
3158EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
Aaron Lu6f4c8272013-01-23 15:09:32 +08003159
3160void sdev_disable_disk_events(struct scsi_device *sdev)
3161{
3162 atomic_inc(&sdev->disk_events_disable_depth);
3163}
3164EXPORT_SYMBOL(sdev_disable_disk_events);
3165
3166void sdev_enable_disk_events(struct scsi_device *sdev)
3167{
3168 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
3169 return;
3170 atomic_dec(&sdev->disk_events_disable_depth);
3171}
3172EXPORT_SYMBOL(sdev_enable_disk_events);
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003173
3174/**
3175 * scsi_vpd_lun_id - return a unique device identification
3176 * @sdev: SCSI device
3177 * @id: buffer for the identification
3178 * @id_len: length of the buffer
3179 *
3180 * Copies a unique device identification into @id based
3181 * on the information in the VPD page 0x83 of the device.
3182 * The string will be formatted as a SCSI name string.
3183 *
3184 * Returns the length of the identification or error on failure.
3185 * If the identifier is longer than the supplied buffer the actual
3186 * identifier length is returned and the buffer is not zero-padded.
3187 */
3188int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
3189{
3190 u8 cur_id_type = 0xff;
3191 u8 cur_id_size = 0;
3192 unsigned char *d, *cur_id_str;
3193 unsigned char __rcu *vpd_pg83;
3194 int id_size = -EINVAL;
3195
3196 rcu_read_lock();
3197 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3198 if (!vpd_pg83) {
3199 rcu_read_unlock();
3200 return -ENXIO;
3201 }
3202
3203 /*
3204 * Look for the correct descriptor.
3205 * Order of preference for lun descriptor:
3206 * - SCSI name string
3207 * - NAA IEEE Registered Extended
3208 * - EUI-64 based 16-byte
3209 * - EUI-64 based 12-byte
3210 * - NAA IEEE Registered
3211 * - NAA IEEE Extended
Hannes Reinecked2308232016-05-09 09:14:29 +02003212 * - T10 Vendor ID
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003213 * as longer descriptors reduce the likelyhood
3214 * of identification clashes.
3215 */
3216
3217 /* The id string must be at least 20 bytes + terminating NULL byte */
3218 if (id_len < 21) {
3219 rcu_read_unlock();
3220 return -EINVAL;
3221 }
3222
3223 memset(id, 0, id_len);
3224 d = vpd_pg83 + 4;
3225 while (d < vpd_pg83 + sdev->vpd_pg83_len) {
3226 /* Skip designators not referring to the LUN */
3227 if ((d[1] & 0x30) != 0x00)
3228 goto next_desig;
3229
3230 switch (d[1] & 0xf) {
Hannes Reinecked2308232016-05-09 09:14:29 +02003231 case 0x1:
3232 /* T10 Vendor ID */
3233 if (cur_id_size > d[3])
3234 break;
3235 /* Prefer anything */
3236 if (cur_id_type > 0x01 && cur_id_type != 0xff)
3237 break;
3238 cur_id_size = d[3];
3239 if (cur_id_size + 4 > id_len)
3240 cur_id_size = id_len - 4;
3241 cur_id_str = d + 4;
3242 cur_id_type = d[1] & 0xf;
3243 id_size = snprintf(id, id_len, "t10.%*pE",
3244 cur_id_size, cur_id_str);
3245 break;
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003246 case 0x2:
3247 /* EUI-64 */
3248 if (cur_id_size > d[3])
3249 break;
3250 /* Prefer NAA IEEE Registered Extended */
3251 if (cur_id_type == 0x3 &&
3252 cur_id_size == d[3])
3253 break;
3254 cur_id_size = d[3];
3255 cur_id_str = d + 4;
3256 cur_id_type = d[1] & 0xf;
3257 switch (cur_id_size) {
3258 case 8:
3259 id_size = snprintf(id, id_len,
3260 "eui.%8phN",
3261 cur_id_str);
3262 break;
3263 case 12:
3264 id_size = snprintf(id, id_len,
3265 "eui.%12phN",
3266 cur_id_str);
3267 break;
3268 case 16:
3269 id_size = snprintf(id, id_len,
3270 "eui.%16phN",
3271 cur_id_str);
3272 break;
3273 default:
3274 cur_id_size = 0;
3275 break;
3276 }
3277 break;
3278 case 0x3:
3279 /* NAA */
3280 if (cur_id_size > d[3])
3281 break;
3282 cur_id_size = d[3];
3283 cur_id_str = d + 4;
3284 cur_id_type = d[1] & 0xf;
3285 switch (cur_id_size) {
3286 case 8:
3287 id_size = snprintf(id, id_len,
3288 "naa.%8phN",
3289 cur_id_str);
3290 break;
3291 case 16:
3292 id_size = snprintf(id, id_len,
3293 "naa.%16phN",
3294 cur_id_str);
3295 break;
3296 default:
3297 cur_id_size = 0;
3298 break;
3299 }
3300 break;
3301 case 0x8:
3302 /* SCSI name string */
3303 if (cur_id_size + 4 > d[3])
3304 break;
3305 /* Prefer others for truncated descriptor */
3306 if (cur_id_size && d[3] > id_len)
3307 break;
3308 cur_id_size = id_size = d[3];
3309 cur_id_str = d + 4;
3310 cur_id_type = d[1] & 0xf;
3311 if (cur_id_size >= id_len)
3312 cur_id_size = id_len - 1;
3313 memcpy(id, cur_id_str, cur_id_size);
3314 /* Decrease priority for truncated descriptor */
3315 if (cur_id_size != id_size)
3316 cur_id_size = 6;
3317 break;
3318 default:
3319 break;
3320 }
3321next_desig:
3322 d += d[3] + 4;
3323 }
3324 rcu_read_unlock();
3325
3326 return id_size;
3327}
3328EXPORT_SYMBOL(scsi_vpd_lun_id);
Hannes Reineckea8aa3972015-12-01 10:16:57 +01003329
3330/*
3331 * scsi_vpd_tpg_id - return a target port group identifier
3332 * @sdev: SCSI device
3333 *
3334 * Returns the Target Port Group identifier from the information
3335 * froom VPD page 0x83 of the device.
3336 *
3337 * Returns the identifier or error on failure.
3338 */
3339int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
3340{
3341 unsigned char *d;
3342 unsigned char __rcu *vpd_pg83;
3343 int group_id = -EAGAIN, rel_port = -1;
3344
3345 rcu_read_lock();
3346 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3347 if (!vpd_pg83) {
3348 rcu_read_unlock();
3349 return -ENXIO;
3350 }
3351
3352 d = sdev->vpd_pg83 + 4;
3353 while (d < sdev->vpd_pg83 + sdev->vpd_pg83_len) {
3354 switch (d[1] & 0xf) {
3355 case 0x4:
3356 /* Relative target port */
3357 rel_port = get_unaligned_be16(&d[6]);
3358 break;
3359 case 0x5:
3360 /* Target port group */
3361 group_id = get_unaligned_be16(&d[6]);
3362 break;
3363 default:
3364 break;
3365 }
3366 d += d[3] + 4;
3367 }
3368 rcu_read_unlock();
3369
3370 if (group_id >= 0 && rel_id && rel_port != -1)
3371 *rel_id = rel_port;
3372
3373 return group_id;
3374}
3375EXPORT_SYMBOL(scsi_vpd_tpg_id);