blob: 5f661486cf6ea2daa74a516ea6963627dc973da5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Christoph Hellwigd2852032014-01-17 12:06:53 +01002 * Copyright (C) 1999 Eric Youngdale
3 * Copyright (C) 2014 Christoph Hellwig
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * SCSI queueing library.
6 * Initial versions: Eric Youngdale (eric@andante.org).
7 * Based upon conversations with large numbers
8 * of people at Linux Expo.
9 */
10
11#include <linux/bio.h>
James Bottomleyd3f46f32008-01-15 11:11:46 -060012#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/blkdev.h>
14#include <linux/completion.h>
15#include <linux/kernel.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
James Bottomleyfaead262006-02-14 10:42:07 -060020#include <linux/hardirq.h>
Jens Axboec6132da2007-10-16 11:08:49 +020021#include <linux/scatterlist.h>
Christoph Hellwigd2852032014-01-17 12:06:53 +010022#include <linux/blk-mq.h>
Hannes Reineckef1569ff2014-10-24 14:27:07 +020023#include <linux/ratelimit.h>
Hannes Reineckea8aa3972015-12-01 10:16:57 +010024#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020027#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <scsi/scsi_dbg.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_driver.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_host.h>
Christoph Hellwigee14c672015-08-27 14:16:59 +020033#include <scsi/scsi_dh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Christoph Hellwig3b5382c2014-05-06 12:25:40 +020035#include <trace/events/scsi.h>
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "scsi_priv.h"
38#include "scsi_logging.h"
39
Christoph Hellwige9c787e2017-01-02 21:55:26 +030040static struct kmem_cache *scsi_sdb_cache;
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030041static struct kmem_cache *scsi_sense_cache;
42static struct kmem_cache *scsi_sense_isadma_cache;
43static DEFINE_MUTEX(scsi_sense_cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030045static inline struct kmem_cache *
46scsi_select_sense_cache(struct Scsi_Host *shost)
47{
48 return shost->unchecked_isa_dma ?
49 scsi_sense_isadma_cache : scsi_sense_cache;
50}
51
Christoph Hellwige9c787e2017-01-02 21:55:26 +030052static void scsi_free_sense_buffer(struct Scsi_Host *shost,
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030053 unsigned char *sense_buffer)
54{
55 kmem_cache_free(scsi_select_sense_cache(shost), sense_buffer);
56}
57
Christoph Hellwige9c787e2017-01-02 21:55:26 +030058static unsigned char *scsi_alloc_sense_buffer(struct Scsi_Host *shost,
59 gfp_t gfp_mask, int numa_node)
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030060{
61 return kmem_cache_alloc_node(scsi_select_sense_cache(shost), gfp_mask,
62 numa_node);
63}
64
65int scsi_init_sense_cache(struct Scsi_Host *shost)
66{
67 struct kmem_cache *cache;
68 int ret = 0;
69
70 cache = scsi_select_sense_cache(shost);
71 if (cache)
72 return 0;
73
74 mutex_lock(&scsi_sense_cache_mutex);
75 if (shost->unchecked_isa_dma) {
76 scsi_sense_isadma_cache =
77 kmem_cache_create("scsi_sense_cache(DMA)",
78 SCSI_SENSE_BUFFERSIZE, 0,
79 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL);
80 if (!scsi_sense_isadma_cache)
81 ret = -ENOMEM;
82 } else {
83 scsi_sense_cache =
84 kmem_cache_create("scsi_sense_cache",
85 SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN, NULL);
86 if (!scsi_sense_cache)
87 ret = -ENOMEM;
88 }
89
90 mutex_unlock(&scsi_sense_cache_mutex);
91 return ret;
92}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +020093
Jens Axboea488e742010-04-16 21:13:15 +020094/*
95 * When to reinvoke queueing after a resource shortage. It's 3 msecs to
96 * not change behaviour from the previous unplug mechanism, experimentation
97 * may prove this needs changing.
98 */
99#define SCSI_QUEUE_DELAY 3
100
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100101static void
102scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct Scsi_Host *host = cmd->device->host;
105 struct scsi_device *device = cmd->device;
Mike Christief0c0a372008-08-17 15:24:38 -0500106 struct scsi_target *starget = scsi_target(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /*
Tejun Heo d8c37e72005-05-14 00:46:08 +0900109 * Set the appropriate busy bit for the device/host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
111 * If the host/device isn't busy, assume that something actually
112 * completed, and that we should be able to queue a command now.
113 *
114 * Note that the prior mid-layer assumption that any host could
115 * always queue at least one command is now broken. The mid-layer
116 * will implement a user specifiable stall (see
117 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
118 * if a command is requeued with no other commands outstanding
119 * either for the device or for the host.
120 */
Mike Christief0c0a372008-08-17 15:24:38 -0500121 switch (reason) {
122 case SCSI_MLQUEUE_HOST_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100123 atomic_set(&host->host_blocked, host->max_host_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500124 break;
125 case SCSI_MLQUEUE_DEVICE_BUSY:
James Smart573e5912011-07-06 12:45:17 -0400126 case SCSI_MLQUEUE_EH_RETRY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100127 atomic_set(&device->device_blocked,
128 device->max_device_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500129 break;
130 case SCSI_MLQUEUE_TARGET_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100131 atomic_set(&starget->target_blocked,
132 starget->max_target_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500133 break;
134 }
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100135}
136
Christoph Hellwigd2852032014-01-17 12:06:53 +0100137static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd)
138{
139 struct scsi_device *sdev = cmd->device;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100140
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700141 blk_mq_requeue_request(cmd->request, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100142 put_device(&sdev->sdev_gendev);
143}
144
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100145/**
146 * __scsi_queue_insert - private queue insertion
147 * @cmd: The SCSI command being requeued
148 * @reason: The reason for the requeue
149 * @unbusy: Whether the queue should be unbusied
150 *
151 * This is a private queue insertion. The public interface
152 * scsi_queue_insert() always assumes the queue should be unbusied
153 * because it's always called before the completion. This function is
154 * for a requeue after completion, which should only occur in this
155 * file.
156 */
157static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy)
158{
159 struct scsi_device *device = cmd->device;
160 struct request_queue *q = device->request_queue;
161 unsigned long flags;
162
163 SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
164 "Inserting command %p into mlqueue\n", cmd));
165
166 scsi_set_blocked(cmd, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * Decrement the counters, since these commands are no longer
170 * active on the host/device.
171 */
James Bottomley4f5299a2009-01-02 10:42:21 -0600172 if (unbusy)
173 scsi_device_unbusy(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /*
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500176 * Requeue this command. It will go before all other commands
Bart Van Asscheb4854622012-06-29 15:36:07 +0000177 * that are already in the queue. Schedule requeue work under
178 * lock such that the kblockd_schedule_work() call happens
179 * before blk_cleanup_queue() finishes.
Jens Axboea488e742010-04-16 21:13:15 +0200180 */
Alan Stern644373a2014-03-28 10:51:15 -0700181 cmd->result = 0;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100182 if (q->mq_ops) {
183 scsi_mq_requeue_cmd(cmd);
184 return;
185 }
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500186 spin_lock_irqsave(q->queue_lock, flags);
James Bottomley59897da2005-09-14 12:57:42 -0400187 blk_requeue_request(q, cmd->request);
Jens Axboe59c3d452014-04-08 09:15:35 -0600188 kblockd_schedule_work(&device->requeue_work);
Bart Van Asscheb4854622012-06-29 15:36:07 +0000189 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
James Bottomley4f5299a2009-01-02 10:42:21 -0600192/*
193 * Function: scsi_queue_insert()
194 *
195 * Purpose: Insert a command in the midlevel queue.
196 *
197 * Arguments: cmd - command that we are adding to queue.
198 * reason - why we are inserting command to queue.
199 *
200 * Lock status: Assumed that lock is not held upon entry.
201 *
202 * Returns: Nothing.
203 *
204 * Notes: We do this for one of two cases. Either the host is busy
205 * and it cannot accept any more commands for the time being,
206 * or the device returned QUEUE_FULL and can accept no more
207 * commands.
208 * Notes: This could be called either from an interrupt context or a
209 * normal process context.
210 */
Bart Van Assche84feb162012-06-29 15:35:05 +0000211void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
James Bottomley4f5299a2009-01-02 10:42:21 -0600212{
Bart Van Assche84feb162012-06-29 15:35:05 +0000213 __scsi_queue_insert(cmd, reason, 1);
James Bottomley4f5299a2009-01-02 10:42:21 -0600214}
Christoph Hellwige8064022016-10-20 15:12:13 +0200215
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,
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100218 unsigned char *sense, struct scsi_sense_hdr *sshdr,
219 int timeout, int retries, u64 flags, req_flags_t rq_flags,
220 int *resid)
James Bottomley39216032005-06-15 18:48:29 -0500221{
222 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100223 struct scsi_request *rq;
James Bottomley39216032005-06-15 18:48:29 -0500224 int ret = DRIVER_ERROR << 24;
225
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100226 req = blk_get_request(sdev->request_queue,
227 data_direction == DMA_TO_DEVICE ?
228 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, __GFP_RECLAIM);
Joe Lawrencea492f072014-08-28 08:15:21 -0600229 if (IS_ERR(req))
James Bottomleybfe159a2011-07-07 15:45:40 -0500230 return ret;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100231 rq = scsi_req(req);
232 scsi_req_init(req);
James Bottomley39216032005-06-15 18:48:29 -0500233
234 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
Mel Gorman71baba42015-11-06 16:28:28 -0800235 buffer, bufflen, __GFP_RECLAIM))
James Bottomley39216032005-06-15 18:48:29 -0500236 goto out;
237
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100238 rq->cmd_len = COMMAND_SIZE(cmd[0]);
239 memcpy(rq->cmd, cmd, rq->cmd_len);
Mike Christie17e01f22005-11-11 05:31:37 -0600240 req->retries = retries;
James Bottomley39216032005-06-15 18:48:29 -0500241 req->timeout = timeout;
Christoph Hellwige8064022016-10-20 15:12:13 +0200242 req->cmd_flags |= flags;
243 req->rq_flags |= rq_flags | RQF_QUIET | RQF_PREEMPT;
James Bottomley39216032005-06-15 18:48:29 -0500244
245 /*
246 * head injection *required* here otherwise quiesce won't work
247 */
248 blk_execute_rq(req->q, NULL, req, 1);
249
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400250 /*
251 * Some devices (USB mass-storage in particular) may transfer
252 * garbage data together with a residue indicating that the data
253 * is invalid. Prevent the garbage from being misinterpreted
254 * and prevent security leaks by zeroing out the excess data.
255 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100256 if (unlikely(rq->resid_len > 0 && rq->resid_len <= bufflen))
257 memset(buffer + (bufflen - rq->resid_len), 0, rq->resid_len);
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400258
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900259 if (resid)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100260 *resid = rq->resid_len;
261 if (sense && rq->sense_len)
262 memcpy(sense, rq->sense, SCSI_SENSE_BUFFERSIZE);
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100263 if (sshdr)
264 scsi_normalize_sense(rq->sense, rq->sense_len, sshdr);
James Bottomley39216032005-06-15 18:48:29 -0500265 ret = req->errors;
266 out:
267 blk_put_request(req);
268
269 return ret;
270}
Christoph Hellwige8064022016-10-20 15:12:13 +0200271
272/**
273 * scsi_execute - insert request and wait for the result
274 * @sdev: scsi device
275 * @cmd: scsi command
276 * @data_direction: data direction
277 * @buffer: data buffer
278 * @bufflen: len of buffer
279 * @sense: optional sense buffer
280 * @timeout: request timeout in seconds
281 * @retries: number of times to retry request
282 * @flags: or into request flags;
283 * @resid: optional residual length
284 *
285 * returns the req->errors value which is the scsi_cmnd result
286 * field.
287 */
288int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
289 int data_direction, void *buffer, unsigned bufflen,
290 unsigned char *sense, int timeout, int retries, u64 flags,
291 int *resid)
292{
293 return __scsi_execute(sdev, cmd, data_direction, buffer, bufflen, sense,
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100294 NULL, timeout, retries, flags, 0, resid);
Christoph Hellwige8064022016-10-20 15:12:13 +0200295}
James Bottomley33aa6872005-08-28 11:31:14 -0500296EXPORT_SYMBOL(scsi_execute);
James Bottomley39216032005-06-15 18:48:29 -0500297
Lin Ming9b214932013-03-23 11:42:25 +0800298int scsi_execute_req_flags(struct scsi_device *sdev, const unsigned char *cmd,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500299 int data_direction, void *buffer, unsigned bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900300 struct scsi_sense_hdr *sshdr, int timeout, int retries,
Christoph Hellwige8064022016-10-20 15:12:13 +0200301 int *resid, u64 flags, req_flags_t rq_flags)
James Bottomleyea73a9f2005-08-28 11:33:52 -0500302{
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100303 return __scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
304 NULL, sshdr, timeout, retries, flags, rq_flags,
305 resid);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500306}
Lin Ming9b214932013-03-23 11:42:25 +0800307EXPORT_SYMBOL(scsi_execute_req_flags);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * Function: scsi_init_cmd_errh()
311 *
312 * Purpose: Initialize cmd fields related to error handling.
313 *
314 * Arguments: cmd - command that is ready to be queued.
315 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * Notes: This function has the job of initializing a number of
317 * fields related to error handling. Typically this will
318 * be called once for each command, as required.
319 */
Christoph Hellwig631c2282006-07-08 20:42:15 +0200320static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 cmd->serial_number = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200323 scsi_set_resid(cmd, 0);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900324 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (cmd->cmd_len == 0)
Boaz Harroshdb4742d2008-04-30 11:27:26 +0300326 cmd->cmd_len = scsi_command_size(cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329void scsi_device_unbusy(struct scsi_device *sdev)
330{
331 struct Scsi_Host *shost = sdev->host;
Mike Christief0c0a372008-08-17 15:24:38 -0500332 struct scsi_target *starget = scsi_target(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unsigned long flags;
334
Christoph Hellwig74665012014-01-22 15:29:29 +0100335 atomic_dec(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100336 if (starget->can_queue > 0)
337 atomic_dec(&starget->target_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +0100338
James Bottomley939647e2005-09-18 15:05:20 -0500339 if (unlikely(scsi_host_in_recovery(shost) &&
Christoph Hellwig74665012014-01-22 15:29:29 +0100340 (shost->host_failed || shost->host_eh_scheduled))) {
341 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 scsi_eh_wakeup(shost);
Christoph Hellwig74665012014-01-22 15:29:29 +0100343 spin_unlock_irqrestore(shost->host_lock, flags);
344 }
345
Christoph Hellwig71e75c92014-04-11 19:07:01 +0200346 atomic_dec(&sdev->device_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Christoph Hellwigd2852032014-01-17 12:06:53 +0100349static void scsi_kick_queue(struct request_queue *q)
350{
351 if (q->mq_ops)
352 blk_mq_start_hw_queues(q);
353 else
354 blk_run_queue(q);
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/*
358 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
359 * and call blk_run_queue for all the scsi_devices on the target -
360 * including current_sdev first.
361 *
362 * Called with *no* scsi locks held.
363 */
364static void scsi_single_lun_run(struct scsi_device *current_sdev)
365{
366 struct Scsi_Host *shost = current_sdev->host;
367 struct scsi_device *sdev, *tmp;
368 struct scsi_target *starget = scsi_target(current_sdev);
369 unsigned long flags;
370
371 spin_lock_irqsave(shost->host_lock, flags);
372 starget->starget_sdev_user = NULL;
373 spin_unlock_irqrestore(shost->host_lock, flags);
374
375 /*
376 * Call blk_run_queue for all LUNs on the target, starting with
377 * current_sdev. We race with others (to set starget_sdev_user),
378 * but in most cases, we will be first. Ideally, each LU on the
379 * target would get some limited time or requests on the target.
380 */
Christoph Hellwigd2852032014-01-17 12:06:53 +0100381 scsi_kick_queue(current_sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 spin_lock_irqsave(shost->host_lock, flags);
384 if (starget->starget_sdev_user)
385 goto out;
386 list_for_each_entry_safe(sdev, tmp, &starget->devices,
387 same_target_siblings) {
388 if (sdev == current_sdev)
389 continue;
390 if (scsi_device_get(sdev))
391 continue;
392
393 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100394 scsi_kick_queue(sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 spin_lock_irqsave(shost->host_lock, flags);
396
397 scsi_device_put(sdev);
398 }
399 out:
400 spin_unlock_irqrestore(shost->host_lock, flags);
401}
402
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100403static inline bool scsi_device_is_busy(struct scsi_device *sdev)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400404{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100405 if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
406 return true;
407 if (atomic_read(&sdev->device_blocked) > 0)
408 return true;
409 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400410}
411
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100412static inline bool scsi_target_is_busy(struct scsi_target *starget)
Mike Christief0c0a372008-08-17 15:24:38 -0500413{
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100414 if (starget->can_queue > 0) {
415 if (atomic_read(&starget->target_busy) >= starget->can_queue)
416 return true;
417 if (atomic_read(&starget->target_blocked) > 0)
418 return true;
419 }
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100420 return false;
Mike Christief0c0a372008-08-17 15:24:38 -0500421}
422
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100423static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400424{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100425 if (shost->can_queue > 0 &&
426 atomic_read(&shost->host_busy) >= shost->can_queue)
427 return true;
428 if (atomic_read(&shost->host_blocked) > 0)
429 return true;
430 if (shost->host_self_blocked)
431 return true;
432 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400433}
434
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800435static void scsi_starved_list_run(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Mike Christie2a3a59e2008-11-11 13:42:35 -0600437 LIST_HEAD(starved_list);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800438 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 unsigned long flags;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 spin_lock_irqsave(shost->host_lock, flags);
Mike Christie2a3a59e2008-11-11 13:42:35 -0600442 list_splice_init(&shost->starved_list, &starved_list);
443
444 while (!list_empty(&starved_list)) {
James Bottomleye2eb7242013-07-02 15:05:26 +0200445 struct request_queue *slq;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
448 * As long as shost is accepting commands and we have
449 * starved queues, call blk_run_queue. scsi_request_fn
450 * drops the queue_lock and can add us back to the
451 * starved_list.
452 *
453 * host_lock protects the starved_list and starved_entry.
454 * scsi_request_fn must get the host_lock before checking
455 * or modifying starved_list or starved_entry.
456 */
Mike Christie2a3a59e2008-11-11 13:42:35 -0600457 if (scsi_host_is_busy(shost))
Mike Christief0c0a372008-08-17 15:24:38 -0500458 break;
Mike Christief0c0a372008-08-17 15:24:38 -0500459
Mike Christie2a3a59e2008-11-11 13:42:35 -0600460 sdev = list_entry(starved_list.next,
461 struct scsi_device, starved_entry);
462 list_del_init(&sdev->starved_entry);
Mike Christief0c0a372008-08-17 15:24:38 -0500463 if (scsi_target_is_busy(scsi_target(sdev))) {
464 list_move_tail(&sdev->starved_entry,
465 &shost->starved_list);
466 continue;
467 }
468
James Bottomleye2eb7242013-07-02 15:05:26 +0200469 /*
470 * Once we drop the host lock, a racing scsi_remove_device()
471 * call may remove the sdev from the starved list and destroy
472 * it and the queue. Mitigate by taking a reference to the
473 * queue and never touching the sdev again after we drop the
474 * host lock. Note: if __scsi_remove_device() invokes
475 * blk_cleanup_queue() before the queue is run from this
476 * function then blk_run_queue() will return immediately since
477 * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
478 */
479 slq = sdev->request_queue;
480 if (!blk_get_queue(slq))
481 continue;
482 spin_unlock_irqrestore(shost->host_lock, flags);
483
Christoph Hellwigd2852032014-01-17 12:06:53 +0100484 scsi_kick_queue(slq);
James Bottomleye2eb7242013-07-02 15:05:26 +0200485 blk_put_queue(slq);
486
487 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Mike Christie2a3a59e2008-11-11 13:42:35 -0600489 /* put any unprocessed entries back */
490 list_splice(&starved_list, &shost->starved_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800492}
493
494/*
495 * Function: scsi_run_queue()
496 *
497 * Purpose: Select a proper request queue to serve next
498 *
499 * Arguments: q - last request's queue
500 *
501 * Returns: Nothing
502 *
503 * Notes: The previous command was completely finished, start
504 * a new one if possible.
505 */
506static void scsi_run_queue(struct request_queue *q)
507{
508 struct scsi_device *sdev = q->queuedata;
509
510 if (scsi_target(sdev)->single_lun)
511 scsi_single_lun_run(sdev);
512 if (!list_empty(&sdev->host->starved_list))
513 scsi_starved_list_run(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Christoph Hellwigd2852032014-01-17 12:06:53 +0100515 if (q->mq_ops)
516 blk_mq_start_stopped_hw_queues(q, false);
517 else
518 blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Jens Axboe9937a5e2011-05-17 11:04:44 +0200521void scsi_requeue_run_queue(struct work_struct *work)
522{
523 struct scsi_device *sdev;
524 struct request_queue *q;
525
526 sdev = container_of(work, struct scsi_device, requeue_work);
527 q = sdev->request_queue;
528 scsi_run_queue(q);
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/*
532 * Function: scsi_requeue_command()
533 *
534 * Purpose: Handle post-processing of completed commands.
535 *
536 * Arguments: q - queue to operate on
537 * cmd - command that may need to be requeued.
538 *
539 * Returns: Nothing
540 *
541 * Notes: After command completion, there may be blocks left
542 * over which weren't finished by the previous command
543 * this can be for a number of reasons - the main one is
544 * I/O errors in the middle of the request, in which case
545 * we need to request the blocks that come after the bad
546 * sector.
James Bottomleye91442b2005-09-09 10:44:16 -0500547 * Notes: Upon return, cmd is a stale pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
549static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
550{
Bart Van Assche940f5d42012-06-29 15:34:26 +0000551 struct scsi_device *sdev = cmd->device;
James Bottomleye91442b2005-09-09 10:44:16 -0500552 struct request *req = cmd->request;
Tejun Heo 283369c2005-04-24 02:06:36 -0500553 unsigned long flags;
554
Tejun Heo 283369c2005-04-24 02:06:36 -0500555 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig134997a2014-02-20 14:20:58 -0800556 blk_unprep_request(req);
557 req->special = NULL;
558 scsi_put_command(cmd);
James Bottomleye91442b2005-09-09 10:44:16 -0500559 blk_requeue_request(q, req);
Tejun Heo 283369c2005-04-24 02:06:36 -0500560 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 scsi_run_queue(q);
Bart Van Assche940f5d42012-06-29 15:34:26 +0000563
564 put_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567void scsi_run_host_queues(struct Scsi_Host *shost)
568{
569 struct scsi_device *sdev;
570
571 shost_for_each_device(sdev, shost)
572 scsi_run_queue(sdev->request_queue);
573}
574
Christoph Hellwigd2852032014-01-17 12:06:53 +0100575static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
576{
Christoph Hellwig57292b52017-01-31 16:57:29 +0100577 if (!blk_rq_is_passthrough(cmd->request)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +0100578 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
579
580 if (drv->uninit_command)
581 drv->uninit_command(cmd);
582 }
583}
584
585static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd)
586{
Ming Lin91dbc082016-04-04 14:48:07 -0700587 struct scsi_data_buffer *sdb;
588
Christoph Hellwigd2852032014-01-17 12:06:53 +0100589 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700590 sg_free_table_chained(&cmd->sdb.table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700591 if (cmd->request->next_rq) {
592 sdb = cmd->request->next_rq->special;
593 if (sdb)
Ming Lin001d63b2016-04-04 14:48:09 -0700594 sg_free_table_chained(&sdb->table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700595 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100596 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700597 sg_free_table_chained(&cmd->prot_sdb->table, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100598}
599
600static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
601{
602 struct scsi_device *sdev = cmd->device;
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +0530603 struct Scsi_Host *shost = sdev->host;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100604 unsigned long flags;
605
Christoph Hellwigd2852032014-01-17 12:06:53 +0100606 scsi_mq_free_sgtables(cmd);
607 scsi_uninit_cmd(cmd);
608
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +0530609 if (shost->use_cmd_list) {
610 BUG_ON(list_empty(&cmd->list));
611 spin_lock_irqsave(&sdev->list_lock, flags);
612 list_del_init(&cmd->list);
613 spin_unlock_irqrestore(&sdev->list_lock, flags);
614 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/*
618 * Function: scsi_release_buffers()
619 *
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200620 * Purpose: Free resources allocate for a scsi_command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 *
622 * Arguments: cmd - command that we are bailing.
623 *
624 * Lock status: Assumed that no lock is held upon entry.
625 *
626 * Returns: Nothing
627 *
628 * Notes: In the event that an upper level driver rejects a
629 * command, we must release resources allocated during
630 * the __init_io() function. Primarily this would involve
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200631 * the scatter-gather table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
Christoph Hellwigf1bea552014-04-15 12:26:54 +0200633static void scsi_release_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200635 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700636 sg_free_table_chained(&cmd->sdb.table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200637
638 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
639
640 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700641 sg_free_table_chained(&cmd->prot_sdb->table, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200644static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
645{
646 struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special;
647
Ming Lin001d63b2016-04-04 14:48:09 -0700648 sg_free_table_chained(&bidi_sdb->table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200649 kmem_cache_free(scsi_sdb_cache, bidi_sdb);
650 cmd->request->next_rq->special = NULL;
651}
652
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800653static bool scsi_end_request(struct request *req, int error,
654 unsigned int bytes, unsigned int bidi_bytes)
655{
656 struct scsi_cmnd *cmd = req->special;
657 struct scsi_device *sdev = cmd->device;
658 struct request_queue *q = sdev->request_queue;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800659
660 if (blk_update_request(req, error, bytes))
661 return true;
662
663 /* Bidi request must be completed as a whole */
664 if (unlikely(bidi_bytes) &&
665 blk_update_request(req->next_rq, error, bidi_bytes))
666 return true;
667
668 if (blk_queue_add_random(q))
669 add_disk_randomness(req->rq_disk);
670
Christoph Hellwigd2852032014-01-17 12:06:53 +0100671 if (req->mq_ctx) {
672 /*
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700673 * In the MQ case the command gets freed by __blk_mq_end_request,
Christoph Hellwigd2852032014-01-17 12:06:53 +0100674 * so we have to do all cleanup that depends on it earlier.
675 *
676 * We also can't kick the queues from irq context, so we
677 * will have to defer it to a workqueue.
678 */
679 scsi_mq_uninit_cmd(cmd);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800680
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700681 __blk_mq_end_request(req, error);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100682
683 if (scsi_target(sdev)->single_lun ||
684 !list_empty(&sdev->host->starved_list))
685 kblockd_schedule_work(&sdev->requeue_work);
686 else
687 blk_mq_start_stopped_hw_queues(q, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100688 } else {
689 unsigned long flags;
690
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400691 if (bidi_bytes)
692 scsi_release_bidi_buffers(cmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +0300693 scsi_release_buffers(cmd);
694 scsi_put_command(cmd);
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400695
Christoph Hellwigd2852032014-01-17 12:06:53 +0100696 spin_lock_irqsave(q->queue_lock, flags);
697 blk_finish_request(req, error);
698 spin_unlock_irqrestore(q->queue_lock, flags);
699
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700700 scsi_run_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100701 }
702
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700703 put_device(&sdev->sdev_gendev);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800704 return false;
705}
706
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200707/**
708 * __scsi_error_from_host_byte - translate SCSI error code into errno
709 * @cmd: SCSI command (unused)
710 * @result: scsi error code
711 *
712 * Translate SCSI error code into standard UNIX errno.
713 * Return values:
714 * -ENOLINK temporary transport failure
715 * -EREMOTEIO permanent target failure, do not retry
716 * -EBADE permanent nexus failure, retry on other path
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200717 * -ENOSPC No write space available
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200718 * -ENODATA Medium error
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200719 * -EIO unspecified I/O error
720 */
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100721static int __scsi_error_from_host_byte(struct scsi_cmnd *cmd, int result)
722{
723 int error = 0;
724
725 switch(host_byte(result)) {
726 case DID_TRANSPORT_FAILFAST:
727 error = -ENOLINK;
728 break;
729 case DID_TARGET_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000730 set_host_byte(cmd, DID_OK);
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100731 error = -EREMOTEIO;
732 break;
733 case DID_NEXUS_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000734 set_host_byte(cmd, DID_OK);
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100735 error = -EBADE;
736 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200737 case DID_ALLOC_FAILURE:
738 set_host_byte(cmd, DID_OK);
739 error = -ENOSPC;
740 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200741 case DID_MEDIUM_ERROR:
742 set_host_byte(cmd, DID_OK);
743 error = -ENODATA;
744 break;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100745 default:
746 error = -EIO;
747 break;
748 }
749
750 return error;
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/*
754 * Function: scsi_io_completion()
755 *
756 * Purpose: Completion processing for block device I/O requests.
757 *
758 * Arguments: cmd - command that is finished.
759 *
760 * Lock status: Assumed that no lock is held upon entry.
761 *
762 * Returns: Nothing
763 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200764 * Notes: We will finish off the specified number of sectors. If we
765 * are done, the command block will be released and the queue
766 * function will be goosed. If we are not done then we have to
Alan Sternb60af5b2008-11-03 15:56:47 -0500767 * figure out what to do next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *
Alan Sternb60af5b2008-11-03 15:56:47 -0500769 * a) We can call scsi_requeue_command(). The request
770 * will be unprepared and put back on the queue. Then
771 * a new command will be created for it. This should
772 * be used if we made forward progress, or if we want
773 * to switch from READ(10) to READ(6) for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200775 * b) We can call __scsi_queue_insert(). The request will
Alan Sternb60af5b2008-11-03 15:56:47 -0500776 * be put back on the queue and retried using the same
777 * command as before, possibly after a delay.
778 *
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800779 * c) We can call scsi_end_request() with -EIO to fail
Alan Sternb60af5b2008-11-03 15:56:47 -0500780 * the remainder of the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700782void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 int result = cmd->result;
Jens Axboe165125e2007-07-24 09:28:11 +0200785 struct request_queue *q = cmd->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct request *req = cmd->request;
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500787 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct scsi_sense_hdr sshdr;
Hannes Reinecke4753cbc2014-10-24 14:26:52 +0200789 bool sense_valid = false;
Hannes Reineckec11c0042014-10-24 14:27:01 +0200790 int sense_deferred = 0, level = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500791 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
792 ACTION_DELAYED_RETRY} action;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900793 unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (result) {
796 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
797 if (sense_valid)
798 sense_deferred = scsi_sense_is_deferred(&sshdr);
799 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200800
Christoph Hellwig57292b52017-01-31 16:57:29 +0100801 if (blk_rq_is_passthrough(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (result) {
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100803 if (sense_valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /*
805 * SG_IO wants current and deferred errors
806 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100807 scsi_req(req)->sense_len =
808 min(8 + cmd->sense_buffer[7],
809 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500811 if (!sense_deferred)
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100812 error = __scsi_error_from_host_byte(cmd, result);
Pete Wyckoffb22f6872007-03-13 16:53:28 -0400813 }
Mike Snitzer27c41972012-05-31 15:05:33 -0400814 /*
815 * __scsi_error_from_host_byte may have reset the host_byte
816 */
817 req->errors = cmd->result;
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900818
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100819 scsi_req(req)->resid_len = scsi_get_resid(cmd);
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900820
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200821 if (scsi_bidi_cmnd(cmd)) {
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900822 /*
823 * Bidi commands Must be complete as a whole,
824 * both sides at once.
825 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100826 scsi_req(req->next_rq)->resid_len = scsi_in(cmd)->resid;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800827 if (scsi_end_request(req, 0, blk_rq_bytes(req),
828 blk_rq_bytes(req->next_rq)))
829 BUG();
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200830 return;
831 }
James Bottomley89fb4cd2014-07-03 19:17:34 +0200832 } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
833 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100834 * Flush commands do not transfers any data, and thus cannot use
James Bottomley89fb4cd2014-07-03 19:17:34 +0200835 * good_bytes != blk_rq_bytes(req) as the signal for an error.
836 * This sets the error explicitly for the problem case.
837 */
838 error = __scsi_error_from_host_byte(cmd, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Christoph Hellwig57292b52017-01-31 16:57:29 +0100841 /* no bidi support for !blk_rq_is_passthrough yet */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200842 BUG_ON(blk_bidi_rq(req));
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 * Next deal with any sectors which we were able to correctly
846 * handle.
847 */
Hannes Reinecke91921e02014-06-25 16:39:59 +0200848 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
849 "%u sectors total, %d bytes done.\n",
850 blk_rq_sectors(req), good_bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
James Bottomleya9bddd72009-03-30 16:55:51 +0000852 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100853 * Recovered errors need reporting, but they're always treated as
854 * success, so fiddle the result code here. For passthrough requests
James Bottomleya9bddd72009-03-30 16:55:51 +0000855 * we already took a copy of the original into rq->errors which
856 * is what gets returned to the user
857 */
Douglas Gilberte7efe592010-01-03 13:51:15 -0500858 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
859 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
860 * print since caller wants ATA registers. Only occurs on
861 * SCSI ATA PASS_THROUGH commands when CK_COND=1
862 */
863 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
864 ;
Christoph Hellwige8064022016-10-20 15:12:13 +0200865 else if (!(req->rq_flags & RQF_QUIET))
Hannes Reinecked811b842014-10-24 14:26:45 +0200866 scsi_print_sense(cmd);
James Bottomleya9bddd72009-03-30 16:55:51 +0000867 result = 0;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100868 /* for passthrough error may be set */
James Bottomleya9bddd72009-03-30 16:55:51 +0000869 error = 0;
870 }
871
872 /*
James Bottomleya621bac2016-05-13 12:04:06 -0700873 * special case: failed zero length commands always need to
874 * drop down into the retry code. Otherwise, if we finished
875 * all bytes in the request we are done now.
James Bottomleyd6b0c532006-07-02 10:06:28 -0500876 */
James Bottomleya621bac2016-05-13 12:04:06 -0700877 if (!(blk_rq_bytes(req) == 0 && error) &&
878 !scsi_end_request(req, error, good_bytes, 0))
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800879 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200880
881 /*
882 * Kill remainder if no retrys.
883 */
884 if (error && scsi_noretry_cmd(cmd)) {
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800885 if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
886 BUG();
887 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200888 }
889
890 /*
891 * If there had been no error, but we have leftover bytes in the
892 * requeues just queue the command up again.
893 */
894 if (result == 0)
895 goto requeue;
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700896
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100897 error = __scsi_error_from_host_byte(cmd, result);
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500898
Alan Sternb60af5b2008-11-03 15:56:47 -0500899 if (host_byte(result) == DID_RESET) {
900 /* Third party bus reset or reset for error recovery
901 * reasons. Just retry the command and see what
902 * happens.
903 */
904 action = ACTION_RETRY;
905 } else if (sense_valid && !sense_deferred) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 switch (sshdr.sense_key) {
907 case UNIT_ATTENTION:
908 if (cmd->device->removable) {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700909 /* Detected disc change. Set a bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * and quietly refuse further access.
911 */
912 cmd->device->changed = 1;
Alan Sternb60af5b2008-11-03 15:56:47 -0500913 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 } else {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700915 /* Must have been a power glitch, or a
916 * bus reset. Could not have been a
917 * media change, so we just retry the
Alan Sternb60af5b2008-11-03 15:56:47 -0500918 * command and see what happens.
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700919 */
Alan Sternb60af5b2008-11-03 15:56:47 -0500920 action = ACTION_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922 break;
923 case ILLEGAL_REQUEST:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700924 /* If we had an ILLEGAL REQUEST returned, then
925 * we may have performed an unsupported
926 * command. The only thing this should be
927 * would be a ten byte read where only a six
928 * byte read was supported. Also, on a system
929 * where READ CAPACITY failed, we may have
930 * read past the end of the disk.
931 */
Jens Axboe26a68012005-11-29 21:03:34 +0100932 if ((cmd->device->use_10_for_rw &&
933 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 (cmd->cmnd[0] == READ_10 ||
935 cmd->cmnd[0] == WRITE_10)) {
Alan Sternb60af5b2008-11-03 15:56:47 -0500936 /* This will issue a new 6-byte command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 cmd->device->use_10_for_rw = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500938 action = ACTION_REPREP;
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500939 } else if (sshdr.asc == 0x10) /* DIX */ {
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500940 action = ACTION_FAIL;
941 error = -EILSEQ;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500942 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
Martin K. Petersen5db44862012-09-18 12:19:32 -0400943 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500944 action = ACTION_FAIL;
Martin K. Petersen66a651a2012-02-13 15:38:22 -0500945 error = -EREMOTEIO;
Alan Sternb60af5b2008-11-03 15:56:47 -0500946 } else
947 action = ACTION_FAIL;
948 break;
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400949 case ABORTED_COMMAND:
James Bottomley126c09822009-02-19 21:48:54 +0000950 action = ACTION_FAIL;
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200951 if (sshdr.asc == 0x10) /* DIF */
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500952 error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
954 case NOT_READY:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700955 /* If the device is in the process of becoming
James Bottomleyf3e93f72006-04-25 16:48:30 -0500956 * ready, or has a temporary blockage, retry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
James Bottomleyf3e93f72006-04-25 16:48:30 -0500958 if (sshdr.asc == 0x04) {
959 switch (sshdr.ascq) {
960 case 0x01: /* becoming ready */
961 case 0x04: /* format in progress */
962 case 0x05: /* rebuild in progress */
963 case 0x06: /* recalculation in progress */
964 case 0x07: /* operation in progress */
965 case 0x08: /* Long write in progress */
966 case 0x09: /* self test in progress */
Martin K. Petersend8705f12009-11-26 12:00:41 -0500967 case 0x14: /* space allocation in progress */
Alan Sternb60af5b2008-11-03 15:56:47 -0500968 action = ACTION_DELAYED_RETRY;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500969 break;
Alan Stern3dbf6a52008-12-15 10:31:28 -0500970 default:
Alan Stern3dbf6a52008-12-15 10:31:28 -0500971 action = ACTION_FAIL;
972 break;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500973 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200974 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500975 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 case VOLUME_OVERFLOW:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700978 /* See SSC3rXX or current. */
Alan Sternb60af5b2008-11-03 15:56:47 -0500979 action = ACTION_FAIL;
980 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 default:
Alan Sternb60af5b2008-11-03 15:56:47 -0500982 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
984 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200985 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500986 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500987
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900988 if (action != ACTION_FAIL &&
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200989 time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900990 action = ACTION_FAIL;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900991
Alan Sternb60af5b2008-11-03 15:56:47 -0500992 switch (action) {
993 case ACTION_FAIL:
994 /* Give up and fail the remainder of the request */
Christoph Hellwige8064022016-10-20 15:12:13 +0200995 if (!(req->rq_flags & RQF_QUIET)) {
Hannes Reineckef1569ff2014-10-24 14:27:07 +0200996 static DEFINE_RATELIMIT_STATE(_rs,
997 DEFAULT_RATELIMIT_INTERVAL,
998 DEFAULT_RATELIMIT_BURST);
999
1000 if (unlikely(scsi_logging_level))
1001 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
1002 SCSI_LOG_MLCOMPLETE_BITS);
1003
1004 /*
1005 * if logging is enabled the failure will be printed
1006 * in scsi_log_completion(), so avoid duplicate messages
1007 */
1008 if (!level && __ratelimit(&_rs)) {
1009 scsi_print_result(cmd, NULL, FAILED);
1010 if (driver_byte(result) & DRIVER_SENSE)
1011 scsi_print_sense(cmd);
1012 scsi_print_command(cmd);
1013 }
James Bottomley3173d8c2005-09-04 11:32:05 -05001014 }
Christoph Hellwigf6d47e72014-02-16 06:16:13 -08001015 if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
1016 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +02001017 /*FALLTHRU*/
Alan Sternb60af5b2008-11-03 15:56:47 -05001018 case ACTION_REPREP:
Christoph Hellwigbc85dc52014-05-01 16:51:03 +02001019 requeue:
Alan Sternb60af5b2008-11-03 15:56:47 -05001020 /* Unprep the request and put it back at the head of the queue.
1021 * A new command will be prepared and issued.
1022 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01001023 if (q->mq_ops) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001024 cmd->request->rq_flags &= ~RQF_DONTPREP;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001025 scsi_mq_uninit_cmd(cmd);
1026 scsi_mq_requeue_cmd(cmd);
1027 } else {
1028 scsi_release_buffers(cmd);
1029 scsi_requeue_command(q, cmd);
1030 }
Alan Sternb60af5b2008-11-03 15:56:47 -05001031 break;
1032 case ACTION_RETRY:
1033 /* Retry the same command immediately */
James Bottomley4f5299a2009-01-02 10:42:21 -06001034 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -05001035 break;
1036 case ACTION_DELAYED_RETRY:
1037 /* Retry the same command after a delay */
James Bottomley4f5299a2009-01-02 10:42:21 -06001038 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -05001039 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001043static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001045 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001048 * If sg table allocation fails, requeue request later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001050 if (unlikely(sg_alloc_table_chained(&sdb->table,
1051 blk_rq_nr_phys_segments(req), sdb->table.sgl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return BLKPREP_DEFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /*
1055 * Next, walk the list, and fill in the addresses and sizes of
1056 * each segment.
1057 */
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001058 count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
1059 BUG_ON(count > sdb->table.nents);
1060 sdb->table.nents = count;
Christoph Hellwigfd102b12017-01-13 12:29:11 +01001061 sdb->length = blk_rq_payload_bytes(req);
Rusty Russell4a03d902007-11-19 11:28:48 +11001062 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001064
1065/*
1066 * Function: scsi_init_io()
1067 *
1068 * Purpose: SCSI I/O initialize function.
1069 *
1070 * Arguments: cmd - Command descriptor we wish to initialize
1071 *
1072 * Returns: 0 on success
1073 * BLKPREP_DEFER if the failure is retryable
1074 * BLKPREP_KILL if the failure is fatal
1075 */
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001076int scsi_init_io(struct scsi_cmnd *cmd)
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001077{
Christoph Hellwig5e012aa2014-04-15 12:24:55 +02001078 struct scsi_device *sdev = cmd->device;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001079 struct request *rq = cmd->request;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001080 bool is_mq = (rq->mq_ctx != NULL);
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001081 int error;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001082
Johannes Thumshirnfd3fc0b2017-01-31 10:16:00 +01001083 if (WARN_ON_ONCE(!blk_rq_nr_phys_segments(rq)))
1084 return -EINVAL;
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001085
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001086 error = scsi_init_sgtable(rq, &cmd->sdb);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001087 if (error)
1088 goto err_exit;
1089
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001090 if (blk_bidi_rq(rq)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001091 if (!rq->q->mq_ops) {
1092 struct scsi_data_buffer *bidi_sdb =
1093 kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC);
1094 if (!bidi_sdb) {
1095 error = BLKPREP_DEFER;
1096 goto err_exit;
1097 }
1098
1099 rq->next_rq->special = bidi_sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001100 }
1101
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001102 error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001103 if (error)
1104 goto err_exit;
1105 }
1106
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001107 if (blk_integrity_rq(rq)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001108 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1109 int ivecs, count;
1110
Ewan D. Milne91724c22015-01-15 10:02:12 -05001111 if (prot_sdb == NULL) {
1112 /*
1113 * This can happen if someone (e.g. multipath)
1114 * queues a command to a device on an adapter
1115 * that does not support DIX.
1116 */
1117 WARN_ON_ONCE(1);
1118 error = BLKPREP_KILL;
1119 goto err_exit;
1120 }
1121
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001122 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001123
Ming Lin001d63b2016-04-04 14:48:09 -07001124 if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
Ming Lin22cc3d42016-04-04 14:48:08 -07001125 prot_sdb->table.sgl)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001126 error = BLKPREP_DEFER;
1127 goto err_exit;
1128 }
1129
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001130 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001131 prot_sdb->table.sgl);
1132 BUG_ON(unlikely(count > ivecs));
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001133 BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q)));
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001134
1135 cmd->prot_sdb = prot_sdb;
1136 cmd->prot_sdb->table.nents = count;
1137 }
1138
Christoph Hellwigd2852032014-01-17 12:06:53 +01001139 return BLKPREP_OK;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001140err_exit:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001141 if (is_mq) {
1142 scsi_mq_free_sgtables(cmd);
1143 } else {
1144 scsi_release_buffers(cmd);
1145 cmd->request->special = NULL;
1146 scsi_put_command(cmd);
1147 put_device(&sdev->sdev_gendev);
1148 }
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001149 return error;
1150}
Boaz Harroshbb52d822007-12-13 16:14:27 -08001151EXPORT_SYMBOL(scsi_init_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001153void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001154{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001155 void *buf = cmd->sense_buffer;
1156 void *prot = cmd->prot_sdb;
1157 unsigned long flags;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001158
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001159 /* zero out the cmd, except for the embedded scsi_request */
1160 memset((char *)cmd + sizeof(cmd->req), 0,
1161 sizeof(*cmd) - sizeof(cmd->req));
Christoph Hellwig04796332014-02-20 14:20:55 -08001162
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001163 cmd->device = dev;
1164 cmd->sense_buffer = buf;
1165 cmd->prot_sdb = prot;
1166 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1167 cmd->jiffies_at_alloc = jiffies;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001168
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001169 spin_lock_irqsave(&dev->list_lock, flags);
1170 list_add_tail(&cmd->list, &dev->cmd_list);
1171 spin_unlock_irqrestore(&dev->list_lock, flags);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001172}
1173
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001174static int scsi_setup_scsi_cmnd(struct scsi_device *sdev, struct request *req)
James Bottomley7b163182005-12-15 20:17:02 -06001175{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001176 struct scsi_cmnd *cmd = req->special;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001177
1178 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001179 * Passthrough requests may transfer data, in which case they must
Christoph Hellwig3b003152006-11-04 20:10:55 +01001180 * a bio attached to them. Or they might contain a SCSI command
1181 * that does not transfer data, in which case they may optionally
1182 * submit a request without an attached bio.
1183 */
1184 if (req->bio) {
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001185 int ret = scsi_init_io(cmd);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001186 if (unlikely(ret))
1187 return ret;
1188 } else {
Tejun Heob0790412009-05-07 22:24:42 +09001189 BUG_ON(blk_rq_bytes(req));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001190
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001191 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001192 }
James Bottomley7b163182005-12-15 20:17:02 -06001193
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001194 cmd->cmd_len = scsi_req(req)->cmd_len;
1195 cmd->cmnd = scsi_req(req)->cmd;
Tejun Heob0790412009-05-07 22:24:42 +09001196 cmd->transfersize = blk_rq_bytes(req);
James Bottomley7b163182005-12-15 20:17:02 -06001197 cmd->allowed = req->retries;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001198 return BLKPREP_OK;
1199}
1200
1201/*
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001202 * Setup a normal block command. These are simple request from filesystems
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001203 * that still need to be translated to SCSI CDBs from the ULD.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001204 */
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001205static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001206{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001207 struct scsi_cmnd *cmd = req->special;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001208
Christoph Hellwigee14c672015-08-27 14:16:59 +02001209 if (unlikely(sdev->handler && sdev->handler->prep_fn)) {
1210 int ret = sdev->handler->prep_fn(sdev, req);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001211 if (ret != BLKPREP_OK)
1212 return ret;
1213 }
1214
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001215 cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001216 memset(cmd->cmnd, 0, BLK_MAX_CDB);
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001217 return scsi_cmd_to_driver(cmd)->init_command(cmd);
James Bottomley7b163182005-12-15 20:17:02 -06001218}
James Bottomley7b163182005-12-15 20:17:02 -06001219
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001220static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req)
1221{
1222 struct scsi_cmnd *cmd = req->special;
1223
1224 if (!blk_rq_bytes(req))
1225 cmd->sc_data_direction = DMA_NONE;
1226 else if (rq_data_dir(req) == WRITE)
1227 cmd->sc_data_direction = DMA_TO_DEVICE;
1228 else
1229 cmd->sc_data_direction = DMA_FROM_DEVICE;
1230
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001231 if (blk_rq_is_scsi(req))
1232 return scsi_setup_scsi_cmnd(sdev, req);
1233 else
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001234 return scsi_setup_fs_cmnd(sdev, req);
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001235}
1236
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001237static int
1238scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Christoph Hellwig3b003152006-11-04 20:10:55 +01001240 int ret = BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001243 * If the device is not in running state we will reject some
1244 * or all commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
Christoph Hellwig3b003152006-11-04 20:10:55 +01001247 switch (sdev->sdev_state) {
1248 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05001249 case SDEV_TRANSPORT_OFFLINE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001250 /*
1251 * If the device is offline we refuse to process any
1252 * commands. The device must be brought online
1253 * before trying any recovery commands.
1254 */
1255 sdev_printk(KERN_ERR, sdev,
1256 "rejecting I/O to offline device\n");
1257 ret = BLKPREP_KILL;
1258 break;
1259 case SDEV_DEL:
1260 /*
1261 * If the device is fully deleted, we refuse to
1262 * process any commands as well.
1263 */
James Bottomley9ccfc752005-10-02 11:45:08 -05001264 sdev_printk(KERN_ERR, sdev,
1265 "rejecting I/O to dead device\n");
Christoph Hellwig3b003152006-11-04 20:10:55 +01001266 ret = BLKPREP_KILL;
1267 break;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001268 case SDEV_BLOCK:
James Bottomley6f4267e2008-08-22 16:53:31 -05001269 case SDEV_CREATED_BLOCK:
Bart Van Asschebba0bdd2015-03-04 10:31:47 +01001270 ret = BLKPREP_DEFER;
1271 break;
1272 case SDEV_QUIESCE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001273 /*
1274 * If the devices is blocked we defer normal commands.
1275 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001276 if (!(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001277 ret = BLKPREP_DEFER;
1278 break;
1279 default:
1280 /*
1281 * For any other not fully online state we only allow
1282 * special commands. In particular any user initiated
1283 * command is not allowed.
1284 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001285 if (!(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001286 ret = BLKPREP_KILL;
1287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001290 return ret;
1291}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001293static int
1294scsi_prep_return(struct request_queue *q, struct request *req, int ret)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001295{
1296 struct scsi_device *sdev = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Christoph Hellwig3b003152006-11-04 20:10:55 +01001298 switch (ret) {
1299 case BLKPREP_KILL:
jiangyiwene1cd3912016-02-16 20:14:13 +08001300 case BLKPREP_INVALID:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001301 req->errors = DID_NO_CONNECT << 16;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001302 /* release the command and kill it */
1303 if (req->special) {
1304 struct scsi_cmnd *cmd = req->special;
1305 scsi_release_buffers(cmd);
1306 scsi_put_command(cmd);
Christoph Hellwig68c03d92014-04-15 12:24:56 +02001307 put_device(&sdev->sdev_gendev);
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001308 req->special = NULL;
1309 }
Christoph Hellwig3b003152006-11-04 20:10:55 +01001310 break;
1311 case BLKPREP_DEFER:
1312 /*
Tejun Heo9934c8c2009-05-08 11:54:16 +09001313 * If we defer, the blk_peek_request() returns NULL, but the
Jens Axboea488e742010-04-16 21:13:15 +02001314 * queue must be restarted, so we schedule a callback to happen
1315 * shortly.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001316 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001317 if (atomic_read(&sdev->device_busy) == 0)
Jens Axboea488e742010-04-16 21:13:15 +02001318 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001319 break;
1320 default:
Christoph Hellwige8064022016-10-20 15:12:13 +02001321 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Christoph Hellwig3b003152006-11-04 20:10:55 +01001324 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001326
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001327static int scsi_prep_fn(struct request_queue *q, struct request *req)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001328{
1329 struct scsi_device *sdev = q->queuedata;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001330 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001331 int ret;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001332
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001333 ret = scsi_prep_state_check(sdev, req);
1334 if (ret != BLKPREP_OK)
1335 goto out;
1336
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001337 if (!req->special) {
1338 /* Bail if we can't get a reference to the device */
1339 if (unlikely(!get_device(&sdev->sdev_gendev))) {
1340 ret = BLKPREP_DEFER;
1341 goto out;
1342 }
1343
1344 scsi_init_command(sdev, cmd);
1345 req->special = cmd;
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001346 }
1347
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001348 cmd->tag = req->tag;
1349 cmd->request = req;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001350 cmd->prot_op = SCSI_PROT_NORMAL;
1351
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001352 ret = scsi_setup_cmnd(sdev, req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001353out:
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001354 return scsi_prep_return(q, req, ret);
1355}
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001356
1357static void scsi_unprep_fn(struct request_queue *q, struct request *req)
1358{
Christoph Hellwigd2852032014-01-17 12:06:53 +01001359 scsi_uninit_cmd(req->special);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001360}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362/*
1363 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1364 * return 0.
1365 *
1366 * Called with the queue_lock held.
1367 */
1368static inline int scsi_dev_queue_ready(struct request_queue *q,
1369 struct scsi_device *sdev)
1370{
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001371 unsigned int busy;
1372
1373 busy = atomic_inc_return(&sdev->device_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001374 if (atomic_read(&sdev->device_blocked)) {
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001375 if (busy)
1376 goto out_dec;
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /*
1379 * unblock after device_blocked iterates to zero
1380 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001381 if (atomic_dec_return(&sdev->device_blocked) > 0) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001382 /*
1383 * For the MQ case we take care of this in the caller.
1384 */
1385 if (!q->mq_ops)
1386 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001387 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001389 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1390 "unblocking device at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001392
1393 if (busy >= sdev->queue_depth)
1394 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 return 1;
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001397out_dec:
1398 atomic_dec(&sdev->device_busy);
1399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
Mike Christief0c0a372008-08-17 15:24:38 -05001402/*
1403 * scsi_target_queue_ready: checks if there we can send commands to target
1404 * @sdev: scsi device on starget to check.
Mike Christief0c0a372008-08-17 15:24:38 -05001405 */
1406static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1407 struct scsi_device *sdev)
1408{
1409 struct scsi_target *starget = scsi_target(sdev);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001410 unsigned int busy;
Mike Christief0c0a372008-08-17 15:24:38 -05001411
1412 if (starget->single_lun) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001413 spin_lock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001414 if (starget->starget_sdev_user &&
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001415 starget->starget_sdev_user != sdev) {
1416 spin_unlock_irq(shost->host_lock);
1417 return 0;
1418 }
Mike Christief0c0a372008-08-17 15:24:38 -05001419 starget->starget_sdev_user = sdev;
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001420 spin_unlock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001421 }
1422
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001423 if (starget->can_queue <= 0)
1424 return 1;
1425
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001426 busy = atomic_inc_return(&starget->target_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001427 if (atomic_read(&starget->target_blocked) > 0) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001428 if (busy)
1429 goto starved;
1430
Mike Christief0c0a372008-08-17 15:24:38 -05001431 /*
1432 * unblock after target_blocked iterates to zero
1433 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001434 if (atomic_dec_return(&starget->target_blocked) > 0)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001435 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001436
1437 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1438 "unblocking target at zero depth\n"));
Mike Christief0c0a372008-08-17 15:24:38 -05001439 }
1440
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001441 if (busy >= starget->can_queue)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001442 goto starved;
Mike Christief0c0a372008-08-17 15:24:38 -05001443
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001444 return 1;
1445
1446starved:
1447 spin_lock_irq(shost->host_lock);
1448 list_move_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001449 spin_unlock_irq(shost->host_lock);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001450out_dec:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001451 if (starget->can_queue > 0)
1452 atomic_dec(&starget->target_busy);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001453 return 0;
Mike Christief0c0a372008-08-17 15:24:38 -05001454}
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456/*
1457 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1458 * return 0. We must end up running the queue again whenever 0 is
1459 * returned, else IO can hang.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 */
1461static inline int scsi_host_queue_ready(struct request_queue *q,
1462 struct Scsi_Host *shost,
1463 struct scsi_device *sdev)
1464{
Christoph Hellwig74665012014-01-22 15:29:29 +01001465 unsigned int busy;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001466
James Bottomley939647e2005-09-18 15:05:20 -05001467 if (scsi_host_in_recovery(shost))
Christoph Hellwig74665012014-01-22 15:29:29 +01001468 return 0;
1469
1470 busy = atomic_inc_return(&shost->host_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001471 if (atomic_read(&shost->host_blocked) > 0) {
Christoph Hellwig74665012014-01-22 15:29:29 +01001472 if (busy)
1473 goto starved;
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /*
1476 * unblock after host_blocked iterates to zero
1477 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001478 if (atomic_dec_return(&shost->host_blocked) > 0)
Christoph Hellwig74665012014-01-22 15:29:29 +01001479 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001480
1481 SCSI_LOG_MLQUEUE(3,
1482 shost_printk(KERN_INFO, shost,
1483 "unblocking host at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
Christoph Hellwig74665012014-01-22 15:29:29 +01001485
1486 if (shost->can_queue > 0 && busy >= shost->can_queue)
1487 goto starved;
1488 if (shost->host_self_blocked)
1489 goto starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 /* We're OK to process the command, so we can't be starved */
Christoph Hellwig74665012014-01-22 15:29:29 +01001492 if (!list_empty(&sdev->starved_entry)) {
1493 spin_lock_irq(shost->host_lock);
1494 if (!list_empty(&sdev->starved_entry))
1495 list_del_init(&sdev->starved_entry);
1496 spin_unlock_irq(shost->host_lock);
1497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Christoph Hellwig74665012014-01-22 15:29:29 +01001499 return 1;
1500
1501starved:
1502 spin_lock_irq(shost->host_lock);
1503 if (list_empty(&sdev->starved_entry))
1504 list_add_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001505 spin_unlock_irq(shost->host_lock);
Christoph Hellwig74665012014-01-22 15:29:29 +01001506out_dec:
1507 atomic_dec(&shost->host_busy);
1508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511/*
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001512 * Busy state exporting function for request stacking drivers.
1513 *
1514 * For efficiency, no lock is taken to check the busy state of
1515 * shost/starget/sdev, since the returned value is not guaranteed and
1516 * may be changed after request stacking drivers call the function,
1517 * regardless of taking lock or not.
1518 *
Bart Van Assche67bd9412012-06-29 15:33:22 +00001519 * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
1520 * needs to return 'not busy'. Otherwise, request stacking drivers
1521 * may hold requests forever.
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001522 */
1523static int scsi_lld_busy(struct request_queue *q)
1524{
1525 struct scsi_device *sdev = q->queuedata;
1526 struct Scsi_Host *shost;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001527
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001528 if (blk_queue_dying(q))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001529 return 0;
1530
1531 shost = sdev->host;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001532
Jun'ichi Nomurab7e94a12012-05-22 18:57:17 +09001533 /*
1534 * Ignore host/starget busy state.
1535 * Since block layer does not have a concept of fairness across
1536 * multiple queues, congestion of host/starget needs to be handled
1537 * in SCSI layer.
1538 */
1539 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001540 return 1;
1541
1542 return 0;
1543}
1544
1545/*
James Bottomleye91442b2005-09-09 10:44:16 -05001546 * Kill a request for a dead device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 */
Jens Axboe165125e2007-07-24 09:28:11 +02001548static void scsi_kill_request(struct request *req, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
James Bottomleye91442b2005-09-09 10:44:16 -05001550 struct scsi_cmnd *cmd = req->special;
Jiri Slaby03b14702009-09-23 16:15:35 +02001551 struct scsi_device *sdev;
1552 struct scsi_target *starget;
1553 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Tejun Heo9934c8c2009-05-08 11:54:16 +09001555 blk_start_request(req);
James Bottomley788ce432005-09-09 13:40:23 -05001556
Hannes Reinecke74571812011-11-09 08:39:24 +01001557 scmd_printk(KERN_INFO, cmd, "killing request\n");
1558
Jiri Slaby03b14702009-09-23 16:15:35 +02001559 sdev = cmd->device;
1560 starget = scsi_target(sdev);
1561 shost = sdev->host;
James Bottomleye91442b2005-09-09 10:44:16 -05001562 scsi_init_cmd_errh(cmd);
1563 cmd->result = DID_NO_CONNECT << 16;
1564 atomic_inc(&cmd->device->iorequest_cnt);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001565
1566 /*
1567 * SCSI request completion path will do scsi_device_unbusy(),
1568 * bump busy counts. To bump the counters, we need to dance
1569 * with the locks as normal issue path does.
1570 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001571 atomic_inc(&sdev->device_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +01001572 atomic_inc(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001573 if (starget->can_queue > 0)
1574 atomic_inc(&starget->target_busy);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001575
Jens Axboe242f9dc2008-09-14 05:55:09 -07001576 blk_complete_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Jens Axboe1aea6432006-01-09 16:03:03 +01001579static void scsi_softirq_done(struct request *rq)
1580{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001581 struct scsi_cmnd *cmd = rq->special;
1582 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
Jens Axboe1aea6432006-01-09 16:03:03 +01001583 int disposition;
1584
1585 INIT_LIST_HEAD(&cmd->eh_entry);
1586
Jens Axboe242f9dc2008-09-14 05:55:09 -07001587 atomic_inc(&cmd->device->iodone_cnt);
1588 if (cmd->result)
1589 atomic_inc(&cmd->device->ioerr_cnt);
1590
Jens Axboe1aea6432006-01-09 16:03:03 +01001591 disposition = scsi_decide_disposition(cmd);
1592 if (disposition != SUCCESS &&
1593 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
1594 sdev_printk(KERN_ERR, cmd->device,
1595 "timing out command, waited %lus\n",
1596 wait_for/HZ);
1597 disposition = SUCCESS;
1598 }
Hannes Reinecke91921e02014-06-25 16:39:59 +02001599
Jens Axboe1aea6432006-01-09 16:03:03 +01001600 scsi_log_completion(cmd, disposition);
1601
1602 switch (disposition) {
1603 case SUCCESS:
1604 scsi_finish_command(cmd);
1605 break;
1606 case NEEDS_RETRY:
Christoph Hellwig596f4822007-01-02 12:56:00 +01001607 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
Jens Axboe1aea6432006-01-09 16:03:03 +01001608 break;
1609 case ADD_TO_MLQUEUE:
1610 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1611 break;
1612 default:
1613 if (!scsi_eh_scmd_add(cmd, 0))
1614 scsi_finish_command(cmd);
1615 }
1616}
1617
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001618/**
Christoph Hellwig82042a22014-09-05 18:23:07 -07001619 * scsi_dispatch_command - Dispatch a command to the low-level driver.
1620 * @cmd: command block we are dispatching.
1621 *
1622 * Return: nonzero return request was rejected and device's queue needs to be
1623 * plugged.
1624 */
1625static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
1626{
1627 struct Scsi_Host *host = cmd->device->host;
1628 int rtn = 0;
1629
1630 atomic_inc(&cmd->device->iorequest_cnt);
1631
1632 /* check if the device is still usable */
1633 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
1634 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
1635 * returns an immediate error upwards, and signals
1636 * that the device is no longer present */
1637 cmd->result = DID_NO_CONNECT << 16;
1638 goto done;
1639 }
1640
1641 /* Check to see if the scsi lld made this device blocked. */
1642 if (unlikely(scsi_device_blocked(cmd->device))) {
1643 /*
1644 * in blocked state, the command is just put back on
1645 * the device queue. The suspend state has already
1646 * blocked the queue so future requests should not
1647 * occur until the device transitions out of the
1648 * suspend state.
1649 */
1650 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1651 "queuecommand : device blocked\n"));
1652 return SCSI_MLQUEUE_DEVICE_BUSY;
1653 }
1654
1655 /* Store the LUN value in cmnd, if needed. */
1656 if (cmd->device->lun_in_cdb)
1657 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
1658 (cmd->device->lun << 5 & 0xe0);
1659
1660 scsi_log_send(cmd);
1661
1662 /*
1663 * Before we queue this command, check if the command
1664 * length exceeds what the host adapter can handle.
1665 */
1666 if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
1667 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1668 "queuecommand : command too long. "
1669 "cdb_size=%d host->max_cmd_len=%d\n",
1670 cmd->cmd_len, cmd->device->host->max_cmd_len));
1671 cmd->result = (DID_ABORT << 16);
1672 goto done;
1673 }
1674
1675 if (unlikely(host->shost_state == SHOST_DEL)) {
1676 cmd->result = (DID_NO_CONNECT << 16);
1677 goto done;
1678
1679 }
1680
1681 trace_scsi_dispatch_cmd_start(cmd);
1682 rtn = host->hostt->queuecommand(host, cmd);
1683 if (rtn) {
1684 trace_scsi_dispatch_cmd_error(cmd, rtn);
1685 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
1686 rtn != SCSI_MLQUEUE_TARGET_BUSY)
1687 rtn = SCSI_MLQUEUE_HOST_BUSY;
1688
1689 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1690 "queuecommand : request rejected\n"));
1691 }
1692
1693 return rtn;
1694 done:
1695 cmd->scsi_done(cmd);
1696 return 0;
1697}
1698
1699/**
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001700 * scsi_done - Invoke completion on finished SCSI command.
1701 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
1702 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
1703 *
1704 * Description: This function is the mid-level's (SCSI Core) interrupt routine,
1705 * which regains ownership of the SCSI command (de facto) from a LLDD, and
1706 * calls blk_complete_request() for further processing.
1707 *
1708 * This function is interrupt context safe.
1709 */
1710static void scsi_done(struct scsi_cmnd *cmd)
1711{
1712 trace_scsi_dispatch_cmd_done(cmd);
1713 blk_complete_request(cmd->request);
1714}
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716/*
1717 * Function: scsi_request_fn()
1718 *
1719 * Purpose: Main strategy routine for SCSI.
1720 *
1721 * Arguments: q - Pointer to actual queue.
1722 *
1723 * Returns: Nothing
1724 *
1725 * Lock status: IO request lock assumed to be held when called.
1726 */
1727static void scsi_request_fn(struct request_queue *q)
Bart Van Assche613be1f2014-02-20 14:20:56 -08001728 __releases(q->queue_lock)
1729 __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 struct scsi_device *sdev = q->queuedata;
1732 struct Scsi_Host *shost;
1733 struct scsi_cmnd *cmd;
1734 struct request *req;
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /*
1737 * To start with, we keep looping until the queue is empty, or until
1738 * the host is no longer able to accept any more requests.
1739 */
1740 shost = sdev->host;
Jens Axboea488e742010-04-16 21:13:15 +02001741 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int rtn;
1743 /*
1744 * get next queueable request. We do this early to make sure
Hannes Reinecke91921e02014-06-25 16:39:59 +02001745 * that the request is fully prepared even if we cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 * accept it.
1747 */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001748 req = blk_peek_request(q);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001749 if (!req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 break;
1751
1752 if (unlikely(!scsi_device_online(sdev))) {
James Bottomley9ccfc752005-10-02 11:45:08 -05001753 sdev_printk(KERN_ERR, sdev,
1754 "rejecting I/O to offline device\n");
James Bottomleye91442b2005-09-09 10:44:16 -05001755 scsi_kill_request(req, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 continue;
1757 }
1758
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001759 if (!scsi_dev_queue_ready(q, sdev))
1760 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /*
1763 * Remove the request from the request list.
1764 */
1765 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
Tejun Heo9934c8c2009-05-08 11:54:16 +09001766 blk_start_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001768 spin_unlock_irq(q->queue_lock);
James Bottomleye91442b2005-09-09 10:44:16 -05001769 cmd = req->special;
1770 if (unlikely(cmd == NULL)) {
1771 printk(KERN_CRIT "impossible request in %s.\n"
1772 "please mail a stack trace to "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001773 "linux-scsi@vger.kernel.org\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001774 __func__);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001775 blk_dump_rq_flags(req, "foo");
James Bottomleye91442b2005-09-09 10:44:16 -05001776 BUG();
1777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Mike Christieecefe8a2008-07-11 19:50:35 -05001779 /*
1780 * We hit this when the driver is using a host wide
1781 * tag map. For device level tag maps the queue_depth check
1782 * in the device ready fn would prevent us from trying
1783 * to allocate a tag. Since the map is a shared host resource
1784 * we add the dev to the starved list so it eventually gets
1785 * a run when a tag is freed.
1786 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001787 if (blk_queue_tagged(q) && !(req->rq_flags & RQF_QUEUED)) {
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001788 spin_lock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001789 if (list_empty(&sdev->starved_entry))
1790 list_add_tail(&sdev->starved_entry,
1791 &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001792 spin_unlock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001793 goto not_ready;
1794 }
1795
Mike Christief0c0a372008-08-17 15:24:38 -05001796 if (!scsi_target_queue_ready(shost, sdev))
1797 goto not_ready;
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (!scsi_host_queue_ready(q, shost, sdev))
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001800 goto host_not_ready;
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001801
1802 if (sdev->simple_tags)
1803 cmd->flags |= SCMD_TAGGED;
1804 else
1805 cmd->flags &= ~SCMD_TAGGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /*
1808 * Finally, initialize any error handling parameters, and set up
1809 * the timers for timeouts.
1810 */
1811 scsi_init_cmd_errh(cmd);
1812
1813 /*
1814 * Dispatch the command to the low-level driver.
1815 */
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001816 cmd->scsi_done = scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 rtn = scsi_dispatch_cmd(cmd);
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001818 if (rtn) {
1819 scsi_queue_insert(cmd, rtn);
1820 spin_lock_irq(q->queue_lock);
Jens Axboea488e742010-04-16 21:13:15 +02001821 goto out_delay;
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001822 }
1823 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825
Bart Van Assche613be1f2014-02-20 14:20:56 -08001826 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001828 host_not_ready:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001829 if (scsi_target(sdev)->can_queue > 0)
1830 atomic_dec(&scsi_target(sdev)->target_busy);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001831 not_ready:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /*
1833 * lock q, handle tag, requeue req, and decrement device_busy. We
1834 * must return with queue_lock held.
1835 *
1836 * Decrementing device_busy without checking it is OK, as all such
1837 * cases (host limits or settings) should run the queue at some
1838 * later time.
1839 */
1840 spin_lock_irq(q->queue_lock);
1841 blk_requeue_request(q, req);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001842 atomic_dec(&sdev->device_busy);
Jens Axboea488e742010-04-16 21:13:15 +02001843out_delay:
Guenter Roeck480cadc2014-08-10 05:54:25 -07001844 if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
Jens Axboea488e742010-04-16 21:13:15 +02001845 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Christoph Hellwigd2852032014-01-17 12:06:53 +01001848static inline int prep_to_mq(int ret)
1849{
1850 switch (ret) {
1851 case BLKPREP_OK:
Omar Sandoval2868f132016-11-15 11:11:59 -08001852 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001853 case BLKPREP_DEFER:
1854 return BLK_MQ_RQ_QUEUE_BUSY;
1855 default:
1856 return BLK_MQ_RQ_QUEUE_ERROR;
1857 }
1858}
1859
1860static int scsi_mq_prep_fn(struct request *req)
1861{
1862 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1863 struct scsi_device *sdev = req->q->queuedata;
1864 struct Scsi_Host *shost = sdev->host;
1865 unsigned char *sense_buf = cmd->sense_buffer;
1866 struct scatterlist *sg;
1867
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001868 /* zero out the cmd, except for the embedded scsi_request */
1869 memset((char *)cmd + sizeof(cmd->req), 0,
1870 sizeof(*cmd) - sizeof(cmd->req));
Christoph Hellwigd2852032014-01-17 12:06:53 +01001871
1872 req->special = cmd;
1873
1874 cmd->request = req;
1875 cmd->device = sdev;
1876 cmd->sense_buffer = sense_buf;
1877
1878 cmd->tag = req->tag;
1879
Christoph Hellwigd2852032014-01-17 12:06:53 +01001880 cmd->prot_op = SCSI_PROT_NORMAL;
1881
1882 INIT_LIST_HEAD(&cmd->list);
1883 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1884 cmd->jiffies_at_alloc = jiffies;
1885
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +05301886 if (shost->use_cmd_list) {
1887 spin_lock_irq(&sdev->list_lock);
1888 list_add_tail(&cmd->list, &sdev->cmd_list);
1889 spin_unlock_irq(&sdev->list_lock);
1890 }
Christoph Hellwigd2852032014-01-17 12:06:53 +01001891
1892 sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
1893 cmd->sdb.table.sgl = sg;
1894
1895 if (scsi_host_get_prot(shost)) {
1896 cmd->prot_sdb = (void *)sg +
Tony Battersby120bb3e2014-12-08 17:20:52 -05001897 min_t(unsigned int,
Ming Lin65e86172016-04-04 14:48:10 -07001898 shost->sg_tablesize, SG_CHUNK_SIZE) *
Tony Battersby120bb3e2014-12-08 17:20:52 -05001899 sizeof(struct scatterlist);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001900 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
1901
1902 cmd->prot_sdb->table.sgl =
1903 (struct scatterlist *)(cmd->prot_sdb + 1);
1904 }
1905
1906 if (blk_bidi_rq(req)) {
1907 struct request *next_rq = req->next_rq;
1908 struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq);
1909
1910 memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer));
1911 bidi_sdb->table.sgl =
1912 (struct scatterlist *)(bidi_sdb + 1);
1913
1914 next_rq->special = bidi_sdb;
1915 }
1916
Christoph Hellwigfe052522014-09-22 15:59:31 +02001917 blk_mq_start_request(req);
1918
Christoph Hellwigd2852032014-01-17 12:06:53 +01001919 return scsi_setup_cmnd(sdev, req);
1920}
1921
1922static void scsi_mq_done(struct scsi_cmnd *cmd)
1923{
1924 trace_scsi_dispatch_cmd_done(cmd);
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001925 blk_mq_complete_request(cmd->request, cmd->request->errors);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001926}
1927
Jens Axboe74c45052014-10-29 11:14:52 -06001928static int scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
1929 const struct blk_mq_queue_data *bd)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001930{
Jens Axboe74c45052014-10-29 11:14:52 -06001931 struct request *req = bd->rq;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001932 struct request_queue *q = req->q;
1933 struct scsi_device *sdev = q->queuedata;
1934 struct Scsi_Host *shost = sdev->host;
1935 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1936 int ret;
1937 int reason;
1938
1939 ret = prep_to_mq(scsi_prep_state_check(sdev, req));
Omar Sandoval2868f132016-11-15 11:11:59 -08001940 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001941 goto out;
1942
1943 ret = BLK_MQ_RQ_QUEUE_BUSY;
1944 if (!get_device(&sdev->sdev_gendev))
1945 goto out;
1946
1947 if (!scsi_dev_queue_ready(q, sdev))
1948 goto out_put_device;
1949 if (!scsi_target_queue_ready(shost, sdev))
1950 goto out_dec_device_busy;
1951 if (!scsi_host_queue_ready(q, shost, sdev))
1952 goto out_dec_target_busy;
1953
Christoph Hellwige8064022016-10-20 15:12:13 +02001954 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001955 ret = prep_to_mq(scsi_mq_prep_fn(req));
Omar Sandoval2868f132016-11-15 11:11:59 -08001956 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001957 goto out_dec_host_busy;
Christoph Hellwige8064022016-10-20 15:12:13 +02001958 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwigfe052522014-09-22 15:59:31 +02001959 } else {
1960 blk_mq_start_request(req);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001961 }
1962
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001963 if (sdev->simple_tags)
1964 cmd->flags |= SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001965 else
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001966 cmd->flags &= ~SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001967
Christoph Hellwigd2852032014-01-17 12:06:53 +01001968 scsi_init_cmd_errh(cmd);
1969 cmd->scsi_done = scsi_mq_done;
1970
1971 reason = scsi_dispatch_cmd(cmd);
1972 if (reason) {
1973 scsi_set_blocked(cmd, reason);
1974 ret = BLK_MQ_RQ_QUEUE_BUSY;
1975 goto out_dec_host_busy;
1976 }
1977
1978 return BLK_MQ_RQ_QUEUE_OK;
1979
1980out_dec_host_busy:
1981 atomic_dec(&shost->host_busy);
1982out_dec_target_busy:
1983 if (scsi_target(sdev)->can_queue > 0)
1984 atomic_dec(&scsi_target(sdev)->target_busy);
1985out_dec_device_busy:
1986 atomic_dec(&sdev->device_busy);
1987out_put_device:
1988 put_device(&sdev->sdev_gendev);
1989out:
1990 switch (ret) {
1991 case BLK_MQ_RQ_QUEUE_BUSY:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001992 if (atomic_read(&sdev->device_busy) == 0 &&
1993 !scsi_device_blocked(sdev))
1994 blk_mq_delay_queue(hctx, SCSI_QUEUE_DELAY);
1995 break;
1996 case BLK_MQ_RQ_QUEUE_ERROR:
1997 /*
1998 * Make sure to release all allocated ressources when
1999 * we hit an error, as we will never see this command
2000 * again.
2001 */
Christoph Hellwige8064022016-10-20 15:12:13 +02002002 if (req->rq_flags & RQF_DONTPREP)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002003 scsi_mq_uninit_cmd(cmd);
2004 break;
2005 default:
2006 break;
2007 }
2008 return ret;
2009}
2010
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002011static enum blk_eh_timer_return scsi_timeout(struct request *req,
2012 bool reserved)
2013{
2014 if (reserved)
2015 return BLK_EH_RESET_TIMER;
2016 return scsi_times_out(req);
2017}
2018
Christoph Hellwigd2852032014-01-17 12:06:53 +01002019static int scsi_init_request(void *data, struct request *rq,
2020 unsigned int hctx_idx, unsigned int request_idx,
2021 unsigned int numa_node)
2022{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002023 struct Scsi_Host *shost = data;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002024 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2025
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002026 cmd->sense_buffer =
2027 scsi_alloc_sense_buffer(shost, GFP_KERNEL, numa_node);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002028 if (!cmd->sense_buffer)
2029 return -ENOMEM;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002030 cmd->req.sense = cmd->sense_buffer;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002031 return 0;
2032}
2033
2034static void scsi_exit_request(void *data, struct request *rq,
2035 unsigned int hctx_idx, unsigned int request_idx)
2036{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002037 struct Scsi_Host *shost = data;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002038 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2039
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002040 scsi_free_sense_buffer(shost, cmd->sense_buffer);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002041}
2042
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002043static int scsi_map_queues(struct blk_mq_tag_set *set)
2044{
2045 struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
2046
2047 if (shost->hostt->map_queues)
2048 return shost->hostt->map_queues(shost);
2049 return blk_mq_map_queues(set);
2050}
2051
Christoph Hellwigf1bea552014-04-15 12:26:54 +02002052static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 struct device *host_dev;
2055 u64 bounce_limit = 0xffffffff;
2056
2057 if (shost->unchecked_isa_dma)
2058 return BLK_BOUNCE_ISA;
2059 /*
2060 * Platforms with virtual-DMA translation
2061 * hardware have no practical limit.
2062 */
2063 if (!PCI_DMA_BUS_IS_PHYS)
2064 return BLK_BOUNCE_ANY;
2065
2066 host_dev = scsi_get_device(shost);
2067 if (host_dev && host_dev->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +00002068 bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 return bounce_limit;
2071}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Christoph Hellwigd48777a62017-01-02 21:52:10 +03002073void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Lin Ming6f381fa2012-04-12 13:50:38 +08002075 struct device *dev = shost->dma_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Jens Axboea8474ce2007-08-07 09:02:51 +02002077 /*
2078 * this limit is imposed by hardware restrictions
2079 */
Martin K. Petersen8a783622010-02-26 00:20:39 -05002080 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
Ming Lin65e86172016-04-04 14:48:10 -07002081 SG_MAX_SEGMENTS));
Jens Axboea8474ce2007-08-07 09:02:51 +02002082
Martin K. Petersen13f05c82010-09-10 20:50:10 +02002083 if (scsi_host_prot_dma(shost)) {
2084 shost->sg_prot_tablesize =
2085 min_not_zero(shost->sg_prot_tablesize,
2086 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
2087 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
2088 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
2089 }
2090
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002091 blk_queue_max_hw_sectors(q, shost->max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
2093 blk_queue_segment_boundary(q, shost->dma_boundary);
FUJITA Tomonori99c84db2008-02-04 22:28:17 -08002094 dma_set_seg_boundary(dev, shost->dma_boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
FUJITA Tomonori860ac562008-02-04 22:28:05 -08002096 blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (!shost->use_clustering)
Martin K. Petersene692cb62010-12-01 19:41:49 +01002099 q->limits.cluster = 0;
James Bottomley465ff312008-01-01 10:00:10 -06002100
2101 /*
2102 * set a reasonable default alignment on word boundaries: the
2103 * host and device may alter it using
2104 * blk_queue_update_dma_alignment() later.
2105 */
2106 blk_queue_dma_alignment(q, 0x03);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002107}
Christoph Hellwigd48777a62017-01-02 21:52:10 +03002108EXPORT_SYMBOL_GPL(__scsi_init_queue);
James Bottomley465ff312008-01-01 10:00:10 -06002109
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002110static int scsi_init_rq(struct request_queue *q, struct request *rq, gfp_t gfp)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002111{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002112 struct Scsi_Host *shost = q->rq_alloc_data;
2113 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002114
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002115 memset(cmd, 0, sizeof(*cmd));
2116
2117 cmd->sense_buffer = scsi_alloc_sense_buffer(shost, gfp, NUMA_NO_NODE);
2118 if (!cmd->sense_buffer)
2119 goto fail;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002120 cmd->req.sense = cmd->sense_buffer;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002121
2122 if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
2123 cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp);
2124 if (!cmd->prot_sdb)
2125 goto fail_free_sense;
2126 }
2127
2128 return 0;
2129
2130fail_free_sense:
2131 scsi_free_sense_buffer(shost, cmd->sense_buffer);
2132fail:
2133 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002135
2136static void scsi_exit_rq(struct request_queue *q, struct request *rq)
2137{
2138 struct Scsi_Host *shost = q->rq_alloc_data;
2139 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2140
2141 if (cmd->prot_sdb)
2142 kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
2143 scsi_free_sense_buffer(shost, cmd->sense_buffer);
2144}
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002145
2146struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
2147{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002148 struct Scsi_Host *shost = sdev->host;
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002149 struct request_queue *q;
2150
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002151 q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002152 if (!q)
2153 return NULL;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002154 q->cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
2155 q->rq_alloc_data = shost;
2156 q->request_fn = scsi_request_fn;
2157 q->init_rq_fn = scsi_init_rq;
2158 q->exit_rq_fn = scsi_exit_rq;
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002159
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002160 if (blk_init_allocated_queue(q) < 0) {
2161 blk_cleanup_queue(q);
2162 return NULL;
2163 }
2164
2165 __scsi_init_queue(shost, q);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002166 blk_queue_prep_rq(q, scsi_prep_fn);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02002167 blk_queue_unprep_rq(q, scsi_unprep_fn);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002168 blk_queue_softirq_done(q, scsi_softirq_done);
Jens Axboe242f9dc2008-09-14 05:55:09 -07002169 blk_queue_rq_timed_out(q, scsi_times_out);
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04002170 blk_queue_lld_busy(q, scsi_lld_busy);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002171 return q;
2172}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Christoph Hellwigd2852032014-01-17 12:06:53 +01002174static struct blk_mq_ops scsi_mq_ops = {
Christoph Hellwigd2852032014-01-17 12:06:53 +01002175 .queue_rq = scsi_queue_rq,
2176 .complete = scsi_softirq_done,
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002177 .timeout = scsi_timeout,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002178 .init_request = scsi_init_request,
2179 .exit_request = scsi_exit_request,
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002180 .map_queues = scsi_map_queues,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002181};
2182
2183struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
2184{
2185 sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
2186 if (IS_ERR(sdev->request_queue))
2187 return NULL;
2188
2189 sdev->request_queue->queuedata = sdev;
2190 __scsi_init_queue(sdev->host, sdev->request_queue);
2191 return sdev->request_queue;
2192}
2193
2194int scsi_mq_setup_tags(struct Scsi_Host *shost)
2195{
2196 unsigned int cmd_size, sgl_size, tbl_size;
2197
2198 tbl_size = shost->sg_tablesize;
Ming Lin65e86172016-04-04 14:48:10 -07002199 if (tbl_size > SG_CHUNK_SIZE)
2200 tbl_size = SG_CHUNK_SIZE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002201 sgl_size = tbl_size * sizeof(struct scatterlist);
2202 cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
2203 if (scsi_host_get_prot(shost))
2204 cmd_size += sizeof(struct scsi_data_buffer) + sgl_size;
2205
2206 memset(&shost->tag_set, 0, sizeof(shost->tag_set));
2207 shost->tag_set.ops = &scsi_mq_ops;
Bart Van Asscheefec4b92014-10-30 14:45:36 +01002208 shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002209 shost->tag_set.queue_depth = shost->can_queue;
2210 shost->tag_set.cmd_size = cmd_size;
2211 shost->tag_set.numa_node = NUMA_NO_NODE;
2212 shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Shaohua Li24391c02015-01-23 14:18:00 -07002213 shost->tag_set.flags |=
2214 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002215 shost->tag_set.driver_data = shost;
2216
2217 return blk_mq_alloc_tag_set(&shost->tag_set);
2218}
2219
2220void scsi_mq_destroy_tags(struct Scsi_Host *shost)
2221{
2222 blk_mq_free_tag_set(&shost->tag_set);
2223}
2224
Hannes Reinecke857de6e2017-02-17 09:02:45 +01002225/**
2226 * scsi_device_from_queue - return sdev associated with a request_queue
2227 * @q: The request queue to return the sdev from
2228 *
2229 * Return the sdev associated with a request queue or NULL if the
2230 * request_queue does not reference a SCSI device.
2231 */
2232struct scsi_device *scsi_device_from_queue(struct request_queue *q)
2233{
2234 struct scsi_device *sdev = NULL;
2235
2236 if (q->mq_ops) {
2237 if (q->mq_ops == &scsi_mq_ops)
2238 sdev = q->queuedata;
2239 } else if (q->request_fn == scsi_request_fn)
2240 sdev = q->queuedata;
2241 if (!sdev || !get_device(&sdev->sdev_gendev))
2242 sdev = NULL;
2243
2244 return sdev;
2245}
2246EXPORT_SYMBOL_GPL(scsi_device_from_queue);
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248/*
2249 * Function: scsi_block_requests()
2250 *
2251 * Purpose: Utility function used by low-level drivers to prevent further
2252 * commands from being queued to the device.
2253 *
2254 * Arguments: shost - Host in question
2255 *
2256 * Returns: Nothing
2257 *
2258 * Lock status: No locks are assumed held.
2259 *
2260 * Notes: There is no timer nor any other means by which the requests
2261 * get unblocked other than the low-level driver calling
2262 * scsi_unblock_requests().
2263 */
2264void scsi_block_requests(struct Scsi_Host *shost)
2265{
2266 shost->host_self_blocked = 1;
2267}
2268EXPORT_SYMBOL(scsi_block_requests);
2269
2270/*
2271 * Function: scsi_unblock_requests()
2272 *
2273 * Purpose: Utility function used by low-level drivers to allow further
2274 * commands from being queued to the device.
2275 *
2276 * Arguments: shost - Host in question
2277 *
2278 * Returns: Nothing
2279 *
2280 * Lock status: No locks are assumed held.
2281 *
2282 * Notes: There is no timer nor any other means by which the requests
2283 * get unblocked other than the low-level driver calling
2284 * scsi_unblock_requests().
2285 *
2286 * This is done as an API function so that changes to the
2287 * internals of the scsi mid-layer won't require wholesale
2288 * changes to drivers that use this feature.
2289 */
2290void scsi_unblock_requests(struct Scsi_Host *shost)
2291{
2292 shost->host_self_blocked = 0;
2293 scsi_run_host_queues(shost);
2294}
2295EXPORT_SYMBOL(scsi_unblock_requests);
2296
2297int __init scsi_init_queue(void)
2298{
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002299 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
2300 sizeof(struct scsi_data_buffer),
2301 0, 0, NULL);
2302 if (!scsi_sdb_cache) {
2303 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
FUJITA Tomonorif0787272008-12-14 01:23:45 +09002304 return -ENOMEM;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02002305 }
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return 0;
2308}
2309
2310void scsi_exit_queue(void)
2311{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002312 kmem_cache_destroy(scsi_sense_cache);
2313 kmem_cache_destroy(scsi_sense_isadma_cache);
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002314 kmem_cache_destroy(scsi_sdb_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
James Bottomley5baba832006-03-18 14:10:35 -06002316
2317/**
2318 * scsi_mode_select - issue a mode select
2319 * @sdev: SCSI device to be queried
2320 * @pf: Page format bit (1 == standard, 0 == vendor specific)
2321 * @sp: Save page bit (0 == don't save, 1 == save)
2322 * @modepage: mode page being requested
2323 * @buffer: request buffer (may not be smaller than eight bytes)
2324 * @len: length of request buffer.
2325 * @timeout: command timeout
2326 * @retries: number of retries before failing
2327 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002328 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley5baba832006-03-18 14:10:35 -06002329 * must be SCSI_SENSE_BUFFERSIZE big.
2330 *
2331 * Returns zero if successful; negative error number or scsi
2332 * status on error
2333 *
2334 */
2335int
2336scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
2337 unsigned char *buffer, int len, int timeout, int retries,
2338 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2339{
2340 unsigned char cmd[10];
2341 unsigned char *real_buffer;
2342 int ret;
2343
2344 memset(cmd, 0, sizeof(cmd));
2345 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2346
2347 if (sdev->use_10_for_ms) {
2348 if (len > 65535)
2349 return -EINVAL;
2350 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2351 if (!real_buffer)
2352 return -ENOMEM;
2353 memcpy(real_buffer + 8, buffer, len);
2354 len += 8;
2355 real_buffer[0] = 0;
2356 real_buffer[1] = 0;
2357 real_buffer[2] = data->medium_type;
2358 real_buffer[3] = data->device_specific;
2359 real_buffer[4] = data->longlba ? 0x01 : 0;
2360 real_buffer[5] = 0;
2361 real_buffer[6] = data->block_descriptor_length >> 8;
2362 real_buffer[7] = data->block_descriptor_length;
2363
2364 cmd[0] = MODE_SELECT_10;
2365 cmd[7] = len >> 8;
2366 cmd[8] = len;
2367 } else {
2368 if (len > 255 || data->block_descriptor_length > 255 ||
2369 data->longlba)
2370 return -EINVAL;
2371
2372 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2373 if (!real_buffer)
2374 return -ENOMEM;
2375 memcpy(real_buffer + 4, buffer, len);
2376 len += 4;
2377 real_buffer[0] = 0;
2378 real_buffer[1] = data->medium_type;
2379 real_buffer[2] = data->device_specific;
2380 real_buffer[3] = data->block_descriptor_length;
2381
2382
2383 cmd[0] = MODE_SELECT;
2384 cmd[4] = len;
2385 }
2386
2387 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002388 sshdr, timeout, retries, NULL);
James Bottomley5baba832006-03-18 14:10:35 -06002389 kfree(real_buffer);
2390 return ret;
2391}
2392EXPORT_SYMBOL_GPL(scsi_mode_select);
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394/**
Rob Landleyeb448202007-11-03 13:30:39 -05002395 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
James Bottomley1cf72692005-08-28 11:27:01 -05002396 * @sdev: SCSI device to be queried
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 * @dbd: set if mode sense will allow block descriptors to be returned
2398 * @modepage: mode page being requested
2399 * @buffer: request buffer (may not be smaller than eight bytes)
2400 * @len: length of request buffer.
2401 * @timeout: command timeout
2402 * @retries: number of retries before failing
2403 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002404 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley1cf72692005-08-28 11:27:01 -05002405 * must be SCSI_SENSE_BUFFERSIZE big.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 *
2407 * Returns zero if unsuccessful, or the header offset (either 4
2408 * or 8 depending on whether a six or ten byte command was
2409 * issued) if successful.
Rob Landleyeb448202007-11-03 13:30:39 -05002410 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411int
James Bottomley1cf72692005-08-28 11:27:01 -05002412scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 unsigned char *buffer, int len, int timeout, int retries,
James Bottomley5baba832006-03-18 14:10:35 -06002414 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2415{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 unsigned char cmd[12];
2417 int use_10_for_ms;
2418 int header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002419 int result, retry_count = retries;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002420 struct scsi_sense_hdr my_sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 memset(data, 0, sizeof(*data));
2423 memset(&cmd[0], 0, 12);
2424 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
2425 cmd[2] = modepage;
2426
James Bottomleyea73a9f2005-08-28 11:33:52 -05002427 /* caller might not be interested in sense, but we need it */
2428 if (!sshdr)
2429 sshdr = &my_sshdr;
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 retry:
James Bottomley1cf72692005-08-28 11:27:01 -05002432 use_10_for_ms = sdev->use_10_for_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434 if (use_10_for_ms) {
2435 if (len < 8)
2436 len = 8;
2437
2438 cmd[0] = MODE_SENSE_10;
2439 cmd[8] = len;
2440 header_length = 8;
2441 } else {
2442 if (len < 4)
2443 len = 4;
2444
2445 cmd[0] = MODE_SENSE;
2446 cmd[4] = len;
2447 header_length = 4;
2448 }
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 memset(buffer, 0, len);
2451
James Bottomley1cf72692005-08-28 11:27:01 -05002452 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002453 sshdr, timeout, retries, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 /* This code looks awful: what it's doing is making sure an
2456 * ILLEGAL REQUEST sense return identifies the actual command
2457 * byte as the problem. MODE_SENSE commands can return
2458 * ILLEGAL REQUEST if the code page isn't supported */
2459
James Bottomley1cf72692005-08-28 11:27:01 -05002460 if (use_10_for_ms && !scsi_status_is_good(result) &&
2461 (driver_byte(result) & DRIVER_SENSE)) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05002462 if (scsi_sense_valid(sshdr)) {
2463 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2464 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 /*
2466 * Invalid command operation code
2467 */
James Bottomley1cf72692005-08-28 11:27:01 -05002468 sdev->use_10_for_ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 goto retry;
2470 }
2471 }
2472 }
2473
James Bottomley1cf72692005-08-28 11:27:01 -05002474 if(scsi_status_is_good(result)) {
Al Viro6d73c852006-02-23 02:03:16 +01002475 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2476 (modepage == 6 || modepage == 8))) {
2477 /* Initio breakage? */
2478 header_length = 0;
2479 data->length = 13;
2480 data->medium_type = 0;
2481 data->device_specific = 0;
2482 data->longlba = 0;
2483 data->block_descriptor_length = 0;
2484 } else if(use_10_for_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 data->length = buffer[0]*256 + buffer[1] + 2;
2486 data->medium_type = buffer[2];
2487 data->device_specific = buffer[3];
2488 data->longlba = buffer[4] & 0x01;
2489 data->block_descriptor_length = buffer[6]*256
2490 + buffer[7];
2491 } else {
2492 data->length = buffer[0] + 1;
2493 data->medium_type = buffer[1];
2494 data->device_specific = buffer[2];
2495 data->block_descriptor_length = buffer[3];
2496 }
Al Viro6d73c852006-02-23 02:03:16 +01002497 data->header_length = header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002498 } else if ((status_byte(result) == CHECK_CONDITION) &&
2499 scsi_sense_valid(sshdr) &&
2500 sshdr->sense_key == UNIT_ATTENTION && retry_count) {
2501 retry_count--;
2502 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504
James Bottomley1cf72692005-08-28 11:27:01 -05002505 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507EXPORT_SYMBOL(scsi_mode_sense);
2508
James Bottomley001aac22007-12-02 19:10:40 +02002509/**
2510 * scsi_test_unit_ready - test if unit is ready
2511 * @sdev: scsi device to change the state of.
2512 * @timeout: command timeout
2513 * @retries: number of retries before failing
Christoph Hellwig74a78eb2017-02-14 20:15:57 +01002514 * @sshdr: outpout pointer for decoded sense information.
James Bottomley001aac22007-12-02 19:10:40 +02002515 *
2516 * Returns zero if unsuccessful or an error if TUR failed. For
Tejun Heo9f8a2c22010-12-08 20:57:40 +01002517 * removable media, UNIT_ATTENTION sets ->changed flag.
James Bottomley001aac22007-12-02 19:10:40 +02002518 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519int
James Bottomley001aac22007-12-02 19:10:40 +02002520scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
Christoph Hellwig74a78eb2017-02-14 20:15:57 +01002521 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 char cmd[] = {
2524 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2525 };
2526 int result;
James Bottomley001aac22007-12-02 19:10:40 +02002527
James Bottomley001aac22007-12-02 19:10:40 +02002528 /* try to eat the UNIT_ATTENTION if there are enough retries */
2529 do {
2530 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002531 timeout, retries, NULL);
James Bottomley32c356d2008-08-20 00:58:13 +02002532 if (sdev->removable && scsi_sense_valid(sshdr) &&
2533 sshdr->sense_key == UNIT_ATTENTION)
2534 sdev->changed = 1;
2535 } while (scsi_sense_valid(sshdr) &&
2536 sshdr->sense_key == UNIT_ATTENTION && --retries);
James Bottomley001aac22007-12-02 19:10:40 +02002537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return result;
2539}
2540EXPORT_SYMBOL(scsi_test_unit_ready);
2541
2542/**
Rob Landleyeb448202007-11-03 13:30:39 -05002543 * scsi_device_set_state - Take the given device through the device state model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 * @sdev: scsi device to change the state of.
2545 * @state: state to change to.
2546 *
2547 * Returns zero if unsuccessful or an error if the requested
2548 * transition is illegal.
Rob Landleyeb448202007-11-03 13:30:39 -05002549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550int
2551scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2552{
2553 enum scsi_device_state oldstate = sdev->sdev_state;
2554
2555 if (state == oldstate)
2556 return 0;
2557
2558 switch (state) {
2559 case SDEV_CREATED:
James Bottomley6f4267e2008-08-22 16:53:31 -05002560 switch (oldstate) {
2561 case SDEV_CREATED_BLOCK:
2562 break;
2563 default:
2564 goto illegal;
2565 }
2566 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 case SDEV_RUNNING:
2569 switch (oldstate) {
2570 case SDEV_CREATED:
2571 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002572 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 case SDEV_QUIESCE:
2574 case SDEV_BLOCK:
2575 break;
2576 default:
2577 goto illegal;
2578 }
2579 break;
2580
2581 case SDEV_QUIESCE:
2582 switch (oldstate) {
2583 case SDEV_RUNNING:
2584 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002585 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 break;
2587 default:
2588 goto illegal;
2589 }
2590 break;
2591
2592 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002593 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 switch (oldstate) {
2595 case SDEV_CREATED:
2596 case SDEV_RUNNING:
2597 case SDEV_QUIESCE:
2598 case SDEV_BLOCK:
2599 break;
2600 default:
2601 goto illegal;
2602 }
2603 break;
2604
2605 case SDEV_BLOCK:
2606 switch (oldstate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 case SDEV_RUNNING:
James Bottomley6f4267e2008-08-22 16:53:31 -05002608 case SDEV_CREATED_BLOCK:
2609 break;
2610 default:
2611 goto illegal;
2612 }
2613 break;
2614
2615 case SDEV_CREATED_BLOCK:
2616 switch (oldstate) {
2617 case SDEV_CREATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 break;
2619 default:
2620 goto illegal;
2621 }
2622 break;
2623
2624 case SDEV_CANCEL:
2625 switch (oldstate) {
2626 case SDEV_CREATED:
2627 case SDEV_RUNNING:
Alan Stern9ea72902006-06-23 14:25:34 -04002628 case SDEV_QUIESCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002630 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 case SDEV_BLOCK:
2632 break;
2633 default:
2634 goto illegal;
2635 }
2636 break;
2637
2638 case SDEV_DEL:
2639 switch (oldstate) {
Brian King309bd272006-06-27 11:10:43 -05002640 case SDEV_CREATED:
2641 case SDEV_RUNNING:
2642 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002643 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 case SDEV_CANCEL:
Bart Van Assche0516c082013-07-02 15:06:33 +02002645 case SDEV_CREATED_BLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 break;
2647 default:
2648 goto illegal;
2649 }
2650 break;
2651
2652 }
2653 sdev->sdev_state = state;
2654 return 0;
2655
2656 illegal:
Hannes Reinecke91921e02014-06-25 16:39:59 +02002657 SCSI_LOG_ERROR_RECOVERY(1,
James Bottomley9ccfc752005-10-02 11:45:08 -05002658 sdev_printk(KERN_ERR, sdev,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002659 "Illegal state transition %s->%s",
James Bottomley9ccfc752005-10-02 11:45:08 -05002660 scsi_device_state_name(oldstate),
2661 scsi_device_state_name(state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 );
2663 return -EINVAL;
2664}
2665EXPORT_SYMBOL(scsi_device_set_state);
2666
2667/**
Jeff Garzika341cd02007-10-29 17:15:22 -04002668 * sdev_evt_emit - emit a single SCSI device uevent
2669 * @sdev: associated SCSI device
2670 * @evt: event to emit
2671 *
2672 * Send a single uevent (scsi_event) to the associated scsi_device.
2673 */
2674static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2675{
2676 int idx = 0;
2677 char *envp[3];
2678
2679 switch (evt->evt_type) {
2680 case SDEV_EVT_MEDIA_CHANGE:
2681 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2682 break;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002683 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
Hannes Reinecked3d32892016-02-19 09:17:16 +01002684 scsi_rescan_device(&sdev->sdev_gendev);
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002685 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2686 break;
2687 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2688 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2689 break;
2690 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2691 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2692 break;
2693 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2694 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2695 break;
2696 case SDEV_EVT_LUN_CHANGE_REPORTED:
2697 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2698 break;
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002699 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2700 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
2701 break;
Jeff Garzika341cd02007-10-29 17:15:22 -04002702 default:
2703 /* do nothing */
2704 break;
2705 }
2706
2707 envp[idx++] = NULL;
2708
2709 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2710}
2711
2712/**
2713 * sdev_evt_thread - send a uevent for each scsi event
2714 * @work: work struct for scsi_device
2715 *
2716 * Dispatch queued events to their associated scsi_device kobjects
2717 * as uevents.
2718 */
2719void scsi_evt_thread(struct work_struct *work)
2720{
2721 struct scsi_device *sdev;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002722 enum scsi_device_event evt_type;
Jeff Garzika341cd02007-10-29 17:15:22 -04002723 LIST_HEAD(event_list);
2724
2725 sdev = container_of(work, struct scsi_device, event_work);
2726
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002727 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2728 if (test_and_clear_bit(evt_type, sdev->pending_events))
2729 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2730
Jeff Garzika341cd02007-10-29 17:15:22 -04002731 while (1) {
2732 struct scsi_event *evt;
2733 struct list_head *this, *tmp;
2734 unsigned long flags;
2735
2736 spin_lock_irqsave(&sdev->list_lock, flags);
2737 list_splice_init(&sdev->event_list, &event_list);
2738 spin_unlock_irqrestore(&sdev->list_lock, flags);
2739
2740 if (list_empty(&event_list))
2741 break;
2742
2743 list_for_each_safe(this, tmp, &event_list) {
2744 evt = list_entry(this, struct scsi_event, node);
2745 list_del(&evt->node);
2746 scsi_evt_emit(sdev, evt);
2747 kfree(evt);
2748 }
2749 }
2750}
2751
2752/**
2753 * sdev_evt_send - send asserted event to uevent thread
2754 * @sdev: scsi_device event occurred on
2755 * @evt: event to send
2756 *
2757 * Assert scsi device event asynchronously.
2758 */
2759void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2760{
2761 unsigned long flags;
2762
Kay Sievers4d1566e2008-03-19 13:04:47 +01002763#if 0
2764 /* FIXME: currently this check eliminates all media change events
2765 * for polled devices. Need to update to discriminate between AN
2766 * and polled events */
Jeff Garzika341cd02007-10-29 17:15:22 -04002767 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2768 kfree(evt);
2769 return;
2770 }
Kay Sievers4d1566e2008-03-19 13:04:47 +01002771#endif
Jeff Garzika341cd02007-10-29 17:15:22 -04002772
2773 spin_lock_irqsave(&sdev->list_lock, flags);
2774 list_add_tail(&evt->node, &sdev->event_list);
2775 schedule_work(&sdev->event_work);
2776 spin_unlock_irqrestore(&sdev->list_lock, flags);
2777}
2778EXPORT_SYMBOL_GPL(sdev_evt_send);
2779
2780/**
2781 * sdev_evt_alloc - allocate a new scsi event
2782 * @evt_type: type of event to allocate
2783 * @gfpflags: GFP flags for allocation
2784 *
2785 * Allocates and returns a new scsi_event.
2786 */
2787struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2788 gfp_t gfpflags)
2789{
2790 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2791 if (!evt)
2792 return NULL;
2793
2794 evt->evt_type = evt_type;
2795 INIT_LIST_HEAD(&evt->node);
2796
2797 /* evt_type-specific initialization, if any */
2798 switch (evt_type) {
2799 case SDEV_EVT_MEDIA_CHANGE:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002800 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2801 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2802 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2803 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2804 case SDEV_EVT_LUN_CHANGE_REPORTED:
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002805 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
Jeff Garzika341cd02007-10-29 17:15:22 -04002806 default:
2807 /* do nothing */
2808 break;
2809 }
2810
2811 return evt;
2812}
2813EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2814
2815/**
2816 * sdev_evt_send_simple - send asserted event to uevent thread
2817 * @sdev: scsi_device event occurred on
2818 * @evt_type: type of event to send
2819 * @gfpflags: GFP flags for allocation
2820 *
2821 * Assert scsi device event asynchronously, given an event type.
2822 */
2823void sdev_evt_send_simple(struct scsi_device *sdev,
2824 enum scsi_device_event evt_type, gfp_t gfpflags)
2825{
2826 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2827 if (!evt) {
2828 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2829 evt_type);
2830 return;
2831 }
2832
2833 sdev_evt_send(sdev, evt);
2834}
2835EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2836
2837/**
Bart Van Assche669f0442016-11-22 16:17:13 -08002838 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
2839 * @sdev: SCSI device to count the number of scsi_request_fn() callers for.
2840 */
2841static int scsi_request_fn_active(struct scsi_device *sdev)
2842{
2843 struct request_queue *q = sdev->request_queue;
2844 int request_fn_active;
2845
2846 WARN_ON_ONCE(sdev->host->use_blk_mq);
2847
2848 spin_lock_irq(q->queue_lock);
2849 request_fn_active = q->request_fn_active;
2850 spin_unlock_irq(q->queue_lock);
2851
2852 return request_fn_active;
2853}
2854
2855/**
2856 * scsi_wait_for_queuecommand() - wait for ongoing queuecommand() calls
2857 * @sdev: SCSI device pointer.
2858 *
2859 * Wait until the ongoing shost->hostt->queuecommand() calls that are
2860 * invoked from scsi_request_fn() have finished.
2861 */
2862static void scsi_wait_for_queuecommand(struct scsi_device *sdev)
2863{
2864 WARN_ON_ONCE(sdev->host->use_blk_mq);
2865
2866 while (scsi_request_fn_active(sdev))
2867 msleep(20);
2868}
2869
2870/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 * scsi_device_quiesce - Block user issued commands.
2872 * @sdev: scsi device to quiesce.
2873 *
2874 * This works by trying to transition to the SDEV_QUIESCE state
2875 * (which must be a legal transition). When the device is in this
2876 * state, only special requests will be accepted, all others will
2877 * be deferred. Since special requests may also be requeued requests,
2878 * a successful return doesn't guarantee the device will be
2879 * totally quiescent.
2880 *
2881 * Must be called with user context, may sleep.
2882 *
2883 * Returns zero if unsuccessful or an error if not.
Rob Landleyeb448202007-11-03 13:30:39 -05002884 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885int
2886scsi_device_quiesce(struct scsi_device *sdev)
2887{
2888 int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2889 if (err)
2890 return err;
2891
2892 scsi_run_queue(sdev->request_queue);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02002893 while (atomic_read(&sdev->device_busy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 msleep_interruptible(200);
2895 scsi_run_queue(sdev->request_queue);
2896 }
2897 return 0;
2898}
2899EXPORT_SYMBOL(scsi_device_quiesce);
2900
2901/**
2902 * scsi_device_resume - Restart user issued commands to a quiesced device.
2903 * @sdev: scsi device to resume.
2904 *
2905 * Moves the device from quiesced back to running and restarts the
2906 * queues.
2907 *
2908 * Must be called with user context, may sleep.
Rob Landleyeb448202007-11-03 13:30:39 -05002909 */
Dan Williamsa7a20d12012-03-22 17:05:11 -07002910void scsi_device_resume(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
Dan Williamsa7a20d12012-03-22 17:05:11 -07002912 /* check if the device state was mutated prior to resume, and if
2913 * so assume the state is being managed elsewhere (for example
2914 * device deleted during suspend)
2915 */
2916 if (sdev->sdev_state != SDEV_QUIESCE ||
2917 scsi_device_set_state(sdev, SDEV_RUNNING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return;
2919 scsi_run_queue(sdev->request_queue);
2920}
2921EXPORT_SYMBOL(scsi_device_resume);
2922
2923static void
2924device_quiesce_fn(struct scsi_device *sdev, void *data)
2925{
2926 scsi_device_quiesce(sdev);
2927}
2928
2929void
2930scsi_target_quiesce(struct scsi_target *starget)
2931{
2932 starget_for_each_device(starget, NULL, device_quiesce_fn);
2933}
2934EXPORT_SYMBOL(scsi_target_quiesce);
2935
2936static void
2937device_resume_fn(struct scsi_device *sdev, void *data)
2938{
2939 scsi_device_resume(sdev);
2940}
2941
2942void
2943scsi_target_resume(struct scsi_target *starget)
2944{
2945 starget_for_each_device(starget, NULL, device_resume_fn);
2946}
2947EXPORT_SYMBOL(scsi_target_resume);
2948
2949/**
Rob Landleyeb448202007-11-03 13:30:39 -05002950 * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 * @sdev: device to block
2952 *
2953 * Block request made by scsi lld's to temporarily stop all
Bart Van Assche669f0442016-11-22 16:17:13 -08002954 * scsi commands on the specified device. May sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 *
2956 * Returns zero if successful or error if not
2957 *
2958 * Notes:
2959 * This routine transitions the device to the SDEV_BLOCK state
2960 * (which must be a legal transition). When the device is in this
2961 * state, all commands are deferred until the scsi lld reenables
2962 * the device with scsi_device_unblock or device_block_tmo fires.
Bart Van Assche669f0442016-11-22 16:17:13 -08002963 *
2964 * To do: avoid that scsi_send_eh_cmnd() calls queuecommand() after
2965 * scsi_internal_device_block() has blocked a SCSI device and also
2966 * remove the rport mutex lock and unlock calls from srp_queuecommand().
Rob Landleyeb448202007-11-03 13:30:39 -05002967 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968int
2969scsi_internal_device_block(struct scsi_device *sdev)
2970{
Jens Axboe165125e2007-07-24 09:28:11 +02002971 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 unsigned long flags;
2973 int err = 0;
2974
2975 err = scsi_device_set_state(sdev, SDEV_BLOCK);
James Bottomley6f4267e2008-08-22 16:53:31 -05002976 if (err) {
2977 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2978
2979 if (err)
2980 return err;
2981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 /*
2984 * The device has transitioned to SDEV_BLOCK. Stop the
2985 * block layer from calling the midlayer with this device's
2986 * request queue.
2987 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01002988 if (q->mq_ops) {
Bart Van Assche7dbbf0f2016-11-22 16:17:50 -08002989 blk_mq_quiesce_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002990 } else {
2991 spin_lock_irqsave(q->queue_lock, flags);
2992 blk_stop_queue(q);
2993 spin_unlock_irqrestore(q->queue_lock, flags);
Bart Van Assche669f0442016-11-22 16:17:13 -08002994 scsi_wait_for_queuecommand(sdev);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 return 0;
2998}
2999EXPORT_SYMBOL_GPL(scsi_internal_device_block);
3000
3001/**
3002 * scsi_internal_device_unblock - resume a device after a block request
3003 * @sdev: device to resume
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003004 * @new_state: state to set devices to after unblocking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 *
3006 * Called by scsi lld's or the midlayer to restart the device queue
3007 * for the previously suspended scsi device. Called from interrupt or
3008 * normal process context.
3009 *
3010 * Returns zero if successful or error if not.
3011 *
3012 * Notes:
3013 * This routine transitions the device to the SDEV_RUNNING state
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003014 * or to one of the offline states (which must be a legal transition)
Mike Christied0754982012-05-17 23:56:58 -05003015 * allowing the midlayer to goose the queue for this device.
Rob Landleyeb448202007-11-03 13:30:39 -05003016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017int
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003018scsi_internal_device_unblock(struct scsi_device *sdev,
3019 enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
Jens Axboe165125e2007-07-24 09:28:11 +02003021 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 unsigned long flags;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003023
3024 /*
3025 * Try to transition the scsi device to SDEV_RUNNING or one of the
3026 * offlined states and goose the device queue if successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 */
Vikas Chaudhary0e580762012-08-09 04:51:30 -04003028 if ((sdev->sdev_state == SDEV_BLOCK) ||
3029 (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003030 sdev->sdev_state = new_state;
3031 else if (sdev->sdev_state == SDEV_CREATED_BLOCK) {
3032 if (new_state == SDEV_TRANSPORT_OFFLINE ||
3033 new_state == SDEV_OFFLINE)
3034 sdev->sdev_state = new_state;
3035 else
3036 sdev->sdev_state = SDEV_CREATED;
3037 } else if (sdev->sdev_state != SDEV_CANCEL &&
Mike Christie986fe6c2010-10-06 03:10:59 -05003038 sdev->sdev_state != SDEV_OFFLINE)
Takahiro Yasui5c10e632009-04-29 12:13:02 -04003039 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Christoph Hellwigd2852032014-01-17 12:06:53 +01003041 if (q->mq_ops) {
3042 blk_mq_start_stopped_hw_queues(q, false);
3043 } else {
3044 spin_lock_irqsave(q->queue_lock, flags);
3045 blk_start_queue(q);
3046 spin_unlock_irqrestore(q->queue_lock, flags);
3047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 return 0;
3050}
3051EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
3052
3053static void
3054device_block(struct scsi_device *sdev, void *data)
3055{
3056 scsi_internal_device_block(sdev);
3057}
3058
3059static int
3060target_block(struct device *dev, void *data)
3061{
3062 if (scsi_is_target_device(dev))
3063 starget_for_each_device(to_scsi_target(dev), NULL,
3064 device_block);
3065 return 0;
3066}
3067
3068void
3069scsi_target_block(struct device *dev)
3070{
3071 if (scsi_is_target_device(dev))
3072 starget_for_each_device(to_scsi_target(dev), NULL,
3073 device_block);
3074 else
3075 device_for_each_child(dev, NULL, target_block);
3076}
3077EXPORT_SYMBOL_GPL(scsi_target_block);
3078
3079static void
3080device_unblock(struct scsi_device *sdev, void *data)
3081{
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003082 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
3084
3085static int
3086target_unblock(struct device *dev, void *data)
3087{
3088 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003089 starget_for_each_device(to_scsi_target(dev), data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 device_unblock);
3091 return 0;
3092}
3093
3094void
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003095scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
3097 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003098 starget_for_each_device(to_scsi_target(dev), &new_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 device_unblock);
3100 else
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003101 device_for_each_child(dev, &new_state, target_unblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103EXPORT_SYMBOL_GPL(scsi_target_unblock);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003104
3105/**
3106 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
Rob Landleyeb448202007-11-03 13:30:39 -05003107 * @sgl: scatter-gather list
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003108 * @sg_count: number of segments in sg
3109 * @offset: offset in bytes into sg, on return offset into the mapped area
3110 * @len: bytes to map, on return number of bytes mapped
3111 *
3112 * Returns virtual address of the start of the mapped page
3113 */
Jens Axboec6132da2007-10-16 11:08:49 +02003114void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003115 size_t *offset, size_t *len)
3116{
3117 int i;
3118 size_t sg_len = 0, len_complete = 0;
Jens Axboec6132da2007-10-16 11:08:49 +02003119 struct scatterlist *sg;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003120 struct page *page;
3121
Andrew Morton22cfefb2007-02-05 16:39:03 -08003122 WARN_ON(!irqs_disabled());
3123
Jens Axboec6132da2007-10-16 11:08:49 +02003124 for_each_sg(sgl, sg, sg_count, i) {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003125 len_complete = sg_len; /* Complete sg-entries */
Jens Axboec6132da2007-10-16 11:08:49 +02003126 sg_len += sg->length;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003127 if (sg_len > *offset)
3128 break;
3129 }
3130
3131 if (unlikely(i == sg_count)) {
Andrew Morton169e1a22006-04-18 21:09:08 -07003132 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
3133 "elements %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003134 __func__, sg_len, *offset, sg_count);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003135 WARN_ON(1);
3136 return NULL;
3137 }
3138
3139 /* Offset starting from the beginning of first page in this sg-entry */
Jens Axboec6132da2007-10-16 11:08:49 +02003140 *offset = *offset - len_complete + sg->offset;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003141
3142 /* Assumption: contiguous pages can be accessed as "page + i" */
Jens Axboe45711f12007-10-22 21:19:53 +02003143 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003144 *offset &= ~PAGE_MASK;
3145
3146 /* Bytes in this sg-entry from *offset to the end of the page */
3147 sg_len = PAGE_SIZE - *offset;
3148 if (*len > sg_len)
3149 *len = sg_len;
3150
Cong Wang77dfce02011-11-25 23:14:23 +08003151 return kmap_atomic(page);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003152}
3153EXPORT_SYMBOL(scsi_kmap_atomic_sg);
3154
3155/**
Rob Landleyeb448202007-11-03 13:30:39 -05003156 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003157 * @virt: virtual address to be unmapped
3158 */
3159void scsi_kunmap_atomic_sg(void *virt)
3160{
Cong Wang77dfce02011-11-25 23:14:23 +08003161 kunmap_atomic(virt);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003162}
3163EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
Aaron Lu6f4c8272013-01-23 15:09:32 +08003164
3165void sdev_disable_disk_events(struct scsi_device *sdev)
3166{
3167 atomic_inc(&sdev->disk_events_disable_depth);
3168}
3169EXPORT_SYMBOL(sdev_disable_disk_events);
3170
3171void sdev_enable_disk_events(struct scsi_device *sdev)
3172{
3173 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
3174 return;
3175 atomic_dec(&sdev->disk_events_disable_depth);
3176}
3177EXPORT_SYMBOL(sdev_enable_disk_events);
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003178
3179/**
3180 * scsi_vpd_lun_id - return a unique device identification
3181 * @sdev: SCSI device
3182 * @id: buffer for the identification
3183 * @id_len: length of the buffer
3184 *
3185 * Copies a unique device identification into @id based
3186 * on the information in the VPD page 0x83 of the device.
3187 * The string will be formatted as a SCSI name string.
3188 *
3189 * Returns the length of the identification or error on failure.
3190 * If the identifier is longer than the supplied buffer the actual
3191 * identifier length is returned and the buffer is not zero-padded.
3192 */
3193int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
3194{
3195 u8 cur_id_type = 0xff;
3196 u8 cur_id_size = 0;
3197 unsigned char *d, *cur_id_str;
3198 unsigned char __rcu *vpd_pg83;
3199 int id_size = -EINVAL;
3200
3201 rcu_read_lock();
3202 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3203 if (!vpd_pg83) {
3204 rcu_read_unlock();
3205 return -ENXIO;
3206 }
3207
3208 /*
3209 * Look for the correct descriptor.
3210 * Order of preference for lun descriptor:
3211 * - SCSI name string
3212 * - NAA IEEE Registered Extended
3213 * - EUI-64 based 16-byte
3214 * - EUI-64 based 12-byte
3215 * - NAA IEEE Registered
3216 * - NAA IEEE Extended
Hannes Reinecked2308232016-05-09 09:14:29 +02003217 * - T10 Vendor ID
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003218 * as longer descriptors reduce the likelyhood
3219 * of identification clashes.
3220 */
3221
3222 /* The id string must be at least 20 bytes + terminating NULL byte */
3223 if (id_len < 21) {
3224 rcu_read_unlock();
3225 return -EINVAL;
3226 }
3227
3228 memset(id, 0, id_len);
3229 d = vpd_pg83 + 4;
3230 while (d < vpd_pg83 + sdev->vpd_pg83_len) {
3231 /* Skip designators not referring to the LUN */
3232 if ((d[1] & 0x30) != 0x00)
3233 goto next_desig;
3234
3235 switch (d[1] & 0xf) {
Hannes Reinecked2308232016-05-09 09:14:29 +02003236 case 0x1:
3237 /* T10 Vendor ID */
3238 if (cur_id_size > d[3])
3239 break;
3240 /* Prefer anything */
3241 if (cur_id_type > 0x01 && cur_id_type != 0xff)
3242 break;
3243 cur_id_size = d[3];
3244 if (cur_id_size + 4 > id_len)
3245 cur_id_size = id_len - 4;
3246 cur_id_str = d + 4;
3247 cur_id_type = d[1] & 0xf;
3248 id_size = snprintf(id, id_len, "t10.%*pE",
3249 cur_id_size, cur_id_str);
3250 break;
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003251 case 0x2:
3252 /* EUI-64 */
3253 if (cur_id_size > d[3])
3254 break;
3255 /* Prefer NAA IEEE Registered Extended */
3256 if (cur_id_type == 0x3 &&
3257 cur_id_size == d[3])
3258 break;
3259 cur_id_size = d[3];
3260 cur_id_str = d + 4;
3261 cur_id_type = d[1] & 0xf;
3262 switch (cur_id_size) {
3263 case 8:
3264 id_size = snprintf(id, id_len,
3265 "eui.%8phN",
3266 cur_id_str);
3267 break;
3268 case 12:
3269 id_size = snprintf(id, id_len,
3270 "eui.%12phN",
3271 cur_id_str);
3272 break;
3273 case 16:
3274 id_size = snprintf(id, id_len,
3275 "eui.%16phN",
3276 cur_id_str);
3277 break;
3278 default:
3279 cur_id_size = 0;
3280 break;
3281 }
3282 break;
3283 case 0x3:
3284 /* NAA */
3285 if (cur_id_size > d[3])
3286 break;
3287 cur_id_size = d[3];
3288 cur_id_str = d + 4;
3289 cur_id_type = d[1] & 0xf;
3290 switch (cur_id_size) {
3291 case 8:
3292 id_size = snprintf(id, id_len,
3293 "naa.%8phN",
3294 cur_id_str);
3295 break;
3296 case 16:
3297 id_size = snprintf(id, id_len,
3298 "naa.%16phN",
3299 cur_id_str);
3300 break;
3301 default:
3302 cur_id_size = 0;
3303 break;
3304 }
3305 break;
3306 case 0x8:
3307 /* SCSI name string */
3308 if (cur_id_size + 4 > d[3])
3309 break;
3310 /* Prefer others for truncated descriptor */
3311 if (cur_id_size && d[3] > id_len)
3312 break;
3313 cur_id_size = id_size = d[3];
3314 cur_id_str = d + 4;
3315 cur_id_type = d[1] & 0xf;
3316 if (cur_id_size >= id_len)
3317 cur_id_size = id_len - 1;
3318 memcpy(id, cur_id_str, cur_id_size);
3319 /* Decrease priority for truncated descriptor */
3320 if (cur_id_size != id_size)
3321 cur_id_size = 6;
3322 break;
3323 default:
3324 break;
3325 }
3326next_desig:
3327 d += d[3] + 4;
3328 }
3329 rcu_read_unlock();
3330
3331 return id_size;
3332}
3333EXPORT_SYMBOL(scsi_vpd_lun_id);
Hannes Reineckea8aa3972015-12-01 10:16:57 +01003334
3335/*
3336 * scsi_vpd_tpg_id - return a target port group identifier
3337 * @sdev: SCSI device
3338 *
3339 * Returns the Target Port Group identifier from the information
3340 * froom VPD page 0x83 of the device.
3341 *
3342 * Returns the identifier or error on failure.
3343 */
3344int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
3345{
3346 unsigned char *d;
3347 unsigned char __rcu *vpd_pg83;
3348 int group_id = -EAGAIN, rel_port = -1;
3349
3350 rcu_read_lock();
3351 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3352 if (!vpd_pg83) {
3353 rcu_read_unlock();
3354 return -ENXIO;
3355 }
3356
3357 d = sdev->vpd_pg83 + 4;
3358 while (d < sdev->vpd_pg83 + sdev->vpd_pg83_len) {
3359 switch (d[1] & 0xf) {
3360 case 0x4:
3361 /* Relative target port */
3362 rel_port = get_unaligned_be16(&d[6]);
3363 break;
3364 case 0x5:
3365 /* Target port group */
3366 group_id = get_unaligned_be16(&d[6]);
3367 break;
3368 default:
3369 break;
3370 }
3371 d += d[3] + 4;
3372 }
3373 rcu_read_unlock();
3374
3375 if (group_id >= 0 && rel_id && rel_port != -1)
3376 *rel_id = rel_port;
3377
3378 return group_id;
3379}
3380EXPORT_SYMBOL(scsi_vpd_tpg_id);