blob: 164d062c4d9417bc6c059c1d67893ffec70a92b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Christoph Hellwigd2852032014-01-17 12:06:53 +01002 * Copyright (C) 1999 Eric Youngdale
3 * Copyright (C) 2014 Christoph Hellwig
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * SCSI queueing library.
6 * Initial versions: Eric Youngdale (eric@andante.org).
7 * Based upon conversations with large numbers
8 * of people at Linux Expo.
9 */
10
11#include <linux/bio.h>
James Bottomleyd3f46f32008-01-15 11:11:46 -060012#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/blkdev.h>
14#include <linux/completion.h>
15#include <linux/kernel.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
James Bottomleyfaead262006-02-14 10:42:07 -060020#include <linux/hardirq.h>
Jens Axboec6132da2007-10-16 11:08:49 +020021#include <linux/scatterlist.h>
Christoph Hellwigd2852032014-01-17 12:06:53 +010022#include <linux/blk-mq.h>
Hannes Reineckef1569ff2014-10-24 14:27:07 +020023#include <linux/ratelimit.h>
Hannes Reineckea8aa3972015-12-01 10:16:57 +010024#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020027#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <scsi/scsi_dbg.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_driver.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_host.h>
Bart Van Assche7aa686d2017-05-02 10:45:03 -070033#include <scsi/scsi_transport.h> /* __scsi_init_queue() */
Christoph Hellwigee14c672015-08-27 14:16:59 +020034#include <scsi/scsi_dh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Christoph Hellwig3b5382c2014-05-06 12:25:40 +020036#include <trace/events/scsi.h>
37
Bart Van Assche0eebd002017-04-26 13:47:57 -070038#include "scsi_debugfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "scsi_priv.h"
40#include "scsi_logging.h"
41
Christoph Hellwige9c787e2017-01-02 21:55:26 +030042static struct kmem_cache *scsi_sdb_cache;
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030043static struct kmem_cache *scsi_sense_cache;
44static struct kmem_cache *scsi_sense_isadma_cache;
45static DEFINE_MUTEX(scsi_sense_cache_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Bart Van Asschea45a1f362017-08-30 16:58:42 -070047static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd);
48
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030049static inline struct kmem_cache *
Bart Van Assche8e688252017-06-02 14:21:52 -070050scsi_select_sense_cache(bool unchecked_isa_dma)
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030051{
Bart Van Assche8e688252017-06-02 14:21:52 -070052 return unchecked_isa_dma ? scsi_sense_isadma_cache : scsi_sense_cache;
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030053}
54
Bart Van Assche8e688252017-06-02 14:21:52 -070055static void scsi_free_sense_buffer(bool unchecked_isa_dma,
56 unsigned char *sense_buffer)
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030057{
Bart Van Assche8e688252017-06-02 14:21:52 -070058 kmem_cache_free(scsi_select_sense_cache(unchecked_isa_dma),
59 sense_buffer);
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030060}
61
Bart Van Assche8e688252017-06-02 14:21:52 -070062static unsigned char *scsi_alloc_sense_buffer(bool unchecked_isa_dma,
Christoph Hellwige9c787e2017-01-02 21:55:26 +030063 gfp_t gfp_mask, int numa_node)
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030064{
Bart Van Assche8e688252017-06-02 14:21:52 -070065 return kmem_cache_alloc_node(scsi_select_sense_cache(unchecked_isa_dma),
66 gfp_mask, numa_node);
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030067}
68
69int scsi_init_sense_cache(struct Scsi_Host *shost)
70{
71 struct kmem_cache *cache;
72 int ret = 0;
73
Bart Van Assche8e688252017-06-02 14:21:52 -070074 cache = scsi_select_sense_cache(shost->unchecked_isa_dma);
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030075 if (cache)
76 return 0;
77
78 mutex_lock(&scsi_sense_cache_mutex);
79 if (shost->unchecked_isa_dma) {
80 scsi_sense_isadma_cache =
81 kmem_cache_create("scsi_sense_cache(DMA)",
David Windsor0afe76e2017-06-10 22:50:45 -040082 SCSI_SENSE_BUFFERSIZE, 0,
83 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL);
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030084 if (!scsi_sense_isadma_cache)
85 ret = -ENOMEM;
86 } else {
87 scsi_sense_cache =
David Windsor0afe76e2017-06-10 22:50:45 -040088 kmem_cache_create_usercopy("scsi_sense_cache",
89 SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN,
90 0, SCSI_SENSE_BUFFERSIZE, NULL);
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030091 if (!scsi_sense_cache)
92 ret = -ENOMEM;
93 }
94
95 mutex_unlock(&scsi_sense_cache_mutex);
96 return ret;
97}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +020098
Jens Axboea488e742010-04-16 21:13:15 +020099/*
100 * When to reinvoke queueing after a resource shortage. It's 3 msecs to
101 * not change behaviour from the previous unplug mechanism, experimentation
102 * may prove this needs changing.
103 */
104#define SCSI_QUEUE_DELAY 3
105
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100106static void
107scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 struct Scsi_Host *host = cmd->device->host;
110 struct scsi_device *device = cmd->device;
Mike Christief0c0a372008-08-17 15:24:38 -0500111 struct scsi_target *starget = scsi_target(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /*
Tejun Heo d8c37e72005-05-14 00:46:08 +0900114 * Set the appropriate busy bit for the device/host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
116 * If the host/device isn't busy, assume that something actually
117 * completed, and that we should be able to queue a command now.
118 *
119 * Note that the prior mid-layer assumption that any host could
120 * always queue at least one command is now broken. The mid-layer
121 * will implement a user specifiable stall (see
122 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
123 * if a command is requeued with no other commands outstanding
124 * either for the device or for the host.
125 */
Mike Christief0c0a372008-08-17 15:24:38 -0500126 switch (reason) {
127 case SCSI_MLQUEUE_HOST_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100128 atomic_set(&host->host_blocked, host->max_host_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500129 break;
130 case SCSI_MLQUEUE_DEVICE_BUSY:
James Smart573e5912011-07-06 12:45:17 -0400131 case SCSI_MLQUEUE_EH_RETRY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100132 atomic_set(&device->device_blocked,
133 device->max_device_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500134 break;
135 case SCSI_MLQUEUE_TARGET_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100136 atomic_set(&starget->target_blocked,
137 starget->max_target_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500138 break;
139 }
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100140}
141
Christoph Hellwigd2852032014-01-17 12:06:53 +0100142static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd)
143{
144 struct scsi_device *sdev = cmd->device;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100145
Bart Van Asschea45a1f362017-08-30 16:58:42 -0700146 if (cmd->request->rq_flags & RQF_DONTPREP) {
147 cmd->request->rq_flags &= ~RQF_DONTPREP;
148 scsi_mq_uninit_cmd(cmd);
149 } else {
150 WARN_ON_ONCE(true);
151 }
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700152 blk_mq_requeue_request(cmd->request, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100153 put_device(&sdev->sdev_gendev);
154}
155
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100156/**
157 * __scsi_queue_insert - private queue insertion
158 * @cmd: The SCSI command being requeued
159 * @reason: The reason for the requeue
160 * @unbusy: Whether the queue should be unbusied
161 *
162 * This is a private queue insertion. The public interface
163 * scsi_queue_insert() always assumes the queue should be unbusied
164 * because it's always called before the completion. This function is
165 * for a requeue after completion, which should only occur in this
166 * file.
167 */
168static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy)
169{
170 struct scsi_device *device = cmd->device;
171 struct request_queue *q = device->request_queue;
172 unsigned long flags;
173
174 SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
175 "Inserting command %p into mlqueue\n", cmd));
176
177 scsi_set_blocked(cmd, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Decrement the counters, since these commands are no longer
181 * active on the host/device.
182 */
James Bottomley4f5299a2009-01-02 10:42:21 -0600183 if (unbusy)
184 scsi_device_unbusy(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /*
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500187 * Requeue this command. It will go before all other commands
Bart Van Asscheb4854622012-06-29 15:36:07 +0000188 * that are already in the queue. Schedule requeue work under
189 * lock such that the kblockd_schedule_work() call happens
190 * before blk_cleanup_queue() finishes.
Jens Axboea488e742010-04-16 21:13:15 +0200191 */
Alan Stern644373a2014-03-28 10:51:15 -0700192 cmd->result = 0;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100193 if (q->mq_ops) {
194 scsi_mq_requeue_cmd(cmd);
195 return;
196 }
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500197 spin_lock_irqsave(q->queue_lock, flags);
James Bottomley59897da2005-09-14 12:57:42 -0400198 blk_requeue_request(q, cmd->request);
Jens Axboe59c3d452014-04-08 09:15:35 -0600199 kblockd_schedule_work(&device->requeue_work);
Bart Van Asscheb4854622012-06-29 15:36:07 +0000200 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
James Bottomley4f5299a2009-01-02 10:42:21 -0600203/*
204 * Function: scsi_queue_insert()
205 *
206 * Purpose: Insert a command in the midlevel queue.
207 *
208 * Arguments: cmd - command that we are adding to queue.
209 * reason - why we are inserting command to queue.
210 *
211 * Lock status: Assumed that lock is not held upon entry.
212 *
213 * Returns: Nothing.
214 *
215 * Notes: We do this for one of two cases. Either the host is busy
216 * and it cannot accept any more commands for the time being,
217 * or the device returned QUEUE_FULL and can accept no more
218 * commands.
219 * Notes: This could be called either from an interrupt context or a
220 * normal process context.
221 */
Bart Van Assche84feb162012-06-29 15:35:05 +0000222void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
James Bottomley4f5299a2009-01-02 10:42:21 -0600223{
Bart Van Assche84feb162012-06-29 15:35:05 +0000224 __scsi_queue_insert(cmd, reason, 1);
James Bottomley4f5299a2009-01-02 10:42:21 -0600225}
Christoph Hellwige8064022016-10-20 15:12:13 +0200226
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100227
228/**
229 * scsi_execute - insert request and wait for the result
230 * @sdev: scsi device
231 * @cmd: scsi command
232 * @data_direction: data direction
233 * @buffer: data buffer
234 * @bufflen: len of buffer
235 * @sense: optional sense buffer
236 * @sshdr: optional decoded sense header
237 * @timeout: request timeout in seconds
238 * @retries: number of times to retry request
239 * @flags: flags for ->cmd_flags
240 * @rq_flags: flags for ->rq_flags
241 * @resid: optional residual length
242 *
Christoph Hellwig17d53632017-04-20 16:03:01 +0200243 * Returns the scsi_cmnd result field if a command was executed, or a negative
244 * Linux error code if we didn't get that far.
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100245 */
246int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
James Bottomley33aa6872005-08-28 11:31:14 -0500247 int data_direction, void *buffer, unsigned bufflen,
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100248 unsigned char *sense, struct scsi_sense_hdr *sshdr,
249 int timeout, int retries, u64 flags, req_flags_t rq_flags,
250 int *resid)
James Bottomley39216032005-06-15 18:48:29 -0500251{
252 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100253 struct scsi_request *rq;
James Bottomley39216032005-06-15 18:48:29 -0500254 int ret = DRIVER_ERROR << 24;
255
Bart Van Assche039c6352017-11-09 10:49:56 -0800256 req = blk_get_request_flags(sdev->request_queue,
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100257 data_direction == DMA_TO_DEVICE ?
Bart Van Assche039c6352017-11-09 10:49:56 -0800258 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, BLK_MQ_REQ_PREEMPT);
Joe Lawrencea492f072014-08-28 08:15:21 -0600259 if (IS_ERR(req))
James Bottomleybfe159a2011-07-07 15:45:40 -0500260 return ret;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100261 rq = scsi_req(req);
James Bottomley39216032005-06-15 18:48:29 -0500262
263 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
Mel Gorman71baba42015-11-06 16:28:28 -0800264 buffer, bufflen, __GFP_RECLAIM))
James Bottomley39216032005-06-15 18:48:29 -0500265 goto out;
266
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100267 rq->cmd_len = COMMAND_SIZE(cmd[0]);
268 memcpy(rq->cmd, cmd, rq->cmd_len);
Christoph Hellwig64c7f1d2017-04-05 19:18:12 +0200269 rq->retries = retries;
James Bottomley39216032005-06-15 18:48:29 -0500270 req->timeout = timeout;
Christoph Hellwige8064022016-10-20 15:12:13 +0200271 req->cmd_flags |= flags;
Bart Van Assche039c6352017-11-09 10:49:56 -0800272 req->rq_flags |= rq_flags | RQF_QUIET;
James Bottomley39216032005-06-15 18:48:29 -0500273
274 /*
275 * head injection *required* here otherwise quiesce won't work
276 */
277 blk_execute_rq(req->q, NULL, req, 1);
278
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400279 /*
280 * Some devices (USB mass-storage in particular) may transfer
281 * garbage data together with a residue indicating that the data
282 * is invalid. Prevent the garbage from being misinterpreted
283 * and prevent security leaks by zeroing out the excess data.
284 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100285 if (unlikely(rq->resid_len > 0 && rq->resid_len <= bufflen))
286 memset(buffer + (bufflen - rq->resid_len), 0, rq->resid_len);
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400287
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900288 if (resid)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100289 *resid = rq->resid_len;
290 if (sense && rq->sense_len)
291 memcpy(sense, rq->sense, SCSI_SENSE_BUFFERSIZE);
Christoph Hellwig3949e2f2017-02-14 20:15:58 +0100292 if (sshdr)
293 scsi_normalize_sense(rq->sense, rq->sense_len, sshdr);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200294 ret = rq->result;
James Bottomley39216032005-06-15 18:48:29 -0500295 out:
296 blk_put_request(req);
297
298 return ret;
299}
James Bottomley33aa6872005-08-28 11:31:14 -0500300EXPORT_SYMBOL(scsi_execute);
James Bottomley39216032005-06-15 18:48:29 -0500301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
303 * Function: scsi_init_cmd_errh()
304 *
305 * Purpose: Initialize cmd fields related to error handling.
306 *
307 * Arguments: cmd - command that is ready to be queued.
308 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * Notes: This function has the job of initializing a number of
310 * fields related to error handling. Typically this will
311 * be called once for each command, as required.
312 */
Christoph Hellwig631c2282006-07-08 20:42:15 +0200313static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 cmd->serial_number = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200316 scsi_set_resid(cmd, 0);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900317 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (cmd->cmd_len == 0)
Boaz Harroshdb4742d2008-04-30 11:27:26 +0300319 cmd->cmd_len = scsi_command_size(cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322void scsi_device_unbusy(struct scsi_device *sdev)
323{
324 struct Scsi_Host *shost = sdev->host;
Mike Christief0c0a372008-08-17 15:24:38 -0500325 struct scsi_target *starget = scsi_target(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned long flags;
327
Christoph Hellwig74665012014-01-22 15:29:29 +0100328 atomic_dec(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100329 if (starget->can_queue > 0)
330 atomic_dec(&starget->target_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +0100331
James Bottomley939647e2005-09-18 15:05:20 -0500332 if (unlikely(scsi_host_in_recovery(shost) &&
Christoph Hellwig74665012014-01-22 15:29:29 +0100333 (shost->host_failed || shost->host_eh_scheduled))) {
334 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 scsi_eh_wakeup(shost);
Christoph Hellwig74665012014-01-22 15:29:29 +0100336 spin_unlock_irqrestore(shost->host_lock, flags);
337 }
338
Christoph Hellwig71e75c92014-04-11 19:07:01 +0200339 atomic_dec(&sdev->device_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Christoph Hellwigd2852032014-01-17 12:06:53 +0100342static void scsi_kick_queue(struct request_queue *q)
343{
344 if (q->mq_ops)
345 blk_mq_start_hw_queues(q);
346 else
347 blk_run_queue(q);
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/*
351 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
352 * and call blk_run_queue for all the scsi_devices on the target -
353 * including current_sdev first.
354 *
355 * Called with *no* scsi locks held.
356 */
357static void scsi_single_lun_run(struct scsi_device *current_sdev)
358{
359 struct Scsi_Host *shost = current_sdev->host;
360 struct scsi_device *sdev, *tmp;
361 struct scsi_target *starget = scsi_target(current_sdev);
362 unsigned long flags;
363
364 spin_lock_irqsave(shost->host_lock, flags);
365 starget->starget_sdev_user = NULL;
366 spin_unlock_irqrestore(shost->host_lock, flags);
367
368 /*
369 * Call blk_run_queue for all LUNs on the target, starting with
370 * current_sdev. We race with others (to set starget_sdev_user),
371 * but in most cases, we will be first. Ideally, each LU on the
372 * target would get some limited time or requests on the target.
373 */
Christoph Hellwigd2852032014-01-17 12:06:53 +0100374 scsi_kick_queue(current_sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 spin_lock_irqsave(shost->host_lock, flags);
377 if (starget->starget_sdev_user)
378 goto out;
379 list_for_each_entry_safe(sdev, tmp, &starget->devices,
380 same_target_siblings) {
381 if (sdev == current_sdev)
382 continue;
383 if (scsi_device_get(sdev))
384 continue;
385
386 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100387 scsi_kick_queue(sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 spin_lock_irqsave(shost->host_lock, flags);
389
390 scsi_device_put(sdev);
391 }
392 out:
393 spin_unlock_irqrestore(shost->host_lock, flags);
394}
395
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100396static inline bool scsi_device_is_busy(struct scsi_device *sdev)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400397{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100398 if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
399 return true;
400 if (atomic_read(&sdev->device_blocked) > 0)
401 return true;
402 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400403}
404
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100405static inline bool scsi_target_is_busy(struct scsi_target *starget)
Mike Christief0c0a372008-08-17 15:24:38 -0500406{
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100407 if (starget->can_queue > 0) {
408 if (atomic_read(&starget->target_busy) >= starget->can_queue)
409 return true;
410 if (atomic_read(&starget->target_blocked) > 0)
411 return true;
412 }
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100413 return false;
Mike Christief0c0a372008-08-17 15:24:38 -0500414}
415
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100416static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400417{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100418 if (shost->can_queue > 0 &&
419 atomic_read(&shost->host_busy) >= shost->can_queue)
420 return true;
421 if (atomic_read(&shost->host_blocked) > 0)
422 return true;
423 if (shost->host_self_blocked)
424 return true;
425 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400426}
427
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800428static void scsi_starved_list_run(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Mike Christie2a3a59e2008-11-11 13:42:35 -0600430 LIST_HEAD(starved_list);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800431 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 unsigned long flags;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_lock_irqsave(shost->host_lock, flags);
Mike Christie2a3a59e2008-11-11 13:42:35 -0600435 list_splice_init(&shost->starved_list, &starved_list);
436
437 while (!list_empty(&starved_list)) {
James Bottomleye2eb7242013-07-02 15:05:26 +0200438 struct request_queue *slq;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
441 * As long as shost is accepting commands and we have
442 * starved queues, call blk_run_queue. scsi_request_fn
443 * drops the queue_lock and can add us back to the
444 * starved_list.
445 *
446 * host_lock protects the starved_list and starved_entry.
447 * scsi_request_fn must get the host_lock before checking
448 * or modifying starved_list or starved_entry.
449 */
Mike Christie2a3a59e2008-11-11 13:42:35 -0600450 if (scsi_host_is_busy(shost))
Mike Christief0c0a372008-08-17 15:24:38 -0500451 break;
Mike Christief0c0a372008-08-17 15:24:38 -0500452
Mike Christie2a3a59e2008-11-11 13:42:35 -0600453 sdev = list_entry(starved_list.next,
454 struct scsi_device, starved_entry);
455 list_del_init(&sdev->starved_entry);
Mike Christief0c0a372008-08-17 15:24:38 -0500456 if (scsi_target_is_busy(scsi_target(sdev))) {
457 list_move_tail(&sdev->starved_entry,
458 &shost->starved_list);
459 continue;
460 }
461
James Bottomleye2eb7242013-07-02 15:05:26 +0200462 /*
463 * Once we drop the host lock, a racing scsi_remove_device()
464 * call may remove the sdev from the starved list and destroy
465 * it and the queue. Mitigate by taking a reference to the
466 * queue and never touching the sdev again after we drop the
467 * host lock. Note: if __scsi_remove_device() invokes
468 * blk_cleanup_queue() before the queue is run from this
469 * function then blk_run_queue() will return immediately since
470 * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
471 */
472 slq = sdev->request_queue;
473 if (!blk_get_queue(slq))
474 continue;
475 spin_unlock_irqrestore(shost->host_lock, flags);
476
Christoph Hellwigd2852032014-01-17 12:06:53 +0100477 scsi_kick_queue(slq);
James Bottomleye2eb7242013-07-02 15:05:26 +0200478 blk_put_queue(slq);
479
480 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Mike Christie2a3a59e2008-11-11 13:42:35 -0600482 /* put any unprocessed entries back */
483 list_splice(&starved_list, &shost->starved_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800485}
486
487/*
488 * Function: scsi_run_queue()
489 *
490 * Purpose: Select a proper request queue to serve next
491 *
492 * Arguments: q - last request's queue
493 *
494 * Returns: Nothing
495 *
496 * Notes: The previous command was completely finished, start
497 * a new one if possible.
498 */
499static void scsi_run_queue(struct request_queue *q)
500{
501 struct scsi_device *sdev = q->queuedata;
502
503 if (scsi_target(sdev)->single_lun)
504 scsi_single_lun_run(sdev);
505 if (!list_empty(&sdev->host->starved_list))
506 scsi_starved_list_run(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Christoph Hellwigd2852032014-01-17 12:06:53 +0100508 if (q->mq_ops)
Bart Van Assche36e3cf22017-04-07 11:16:53 -0700509 blk_mq_run_hw_queues(q, false);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100510 else
511 blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Jens Axboe9937a5e2011-05-17 11:04:44 +0200514void scsi_requeue_run_queue(struct work_struct *work)
515{
516 struct scsi_device *sdev;
517 struct request_queue *q;
518
519 sdev = container_of(work, struct scsi_device, requeue_work);
520 q = sdev->request_queue;
521 scsi_run_queue(q);
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*
525 * Function: scsi_requeue_command()
526 *
527 * Purpose: Handle post-processing of completed commands.
528 *
529 * Arguments: q - queue to operate on
530 * cmd - command that may need to be requeued.
531 *
532 * Returns: Nothing
533 *
534 * Notes: After command completion, there may be blocks left
535 * over which weren't finished by the previous command
536 * this can be for a number of reasons - the main one is
537 * I/O errors in the middle of the request, in which case
538 * we need to request the blocks that come after the bad
539 * sector.
James Bottomleye91442b2005-09-09 10:44:16 -0500540 * Notes: Upon return, cmd is a stale pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
542static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
543{
Bart Van Assche940f5d42012-06-29 15:34:26 +0000544 struct scsi_device *sdev = cmd->device;
James Bottomleye91442b2005-09-09 10:44:16 -0500545 struct request *req = cmd->request;
Tejun Heo 283369c2005-04-24 02:06:36 -0500546 unsigned long flags;
547
Tejun Heo 283369c2005-04-24 02:06:36 -0500548 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig134997a2014-02-20 14:20:58 -0800549 blk_unprep_request(req);
550 req->special = NULL;
551 scsi_put_command(cmd);
James Bottomleye91442b2005-09-09 10:44:16 -0500552 blk_requeue_request(q, req);
Tejun Heo 283369c2005-04-24 02:06:36 -0500553 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 scsi_run_queue(q);
Bart Van Assche940f5d42012-06-29 15:34:26 +0000556
557 put_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560void scsi_run_host_queues(struct Scsi_Host *shost)
561{
562 struct scsi_device *sdev;
563
564 shost_for_each_device(sdev, shost)
565 scsi_run_queue(sdev->request_queue);
566}
567
Christoph Hellwigd2852032014-01-17 12:06:53 +0100568static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
569{
Christoph Hellwig57292b52017-01-31 16:57:29 +0100570 if (!blk_rq_is_passthrough(cmd->request)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +0100571 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
572
573 if (drv->uninit_command)
574 drv->uninit_command(cmd);
575 }
576}
577
578static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd)
579{
Ming Lin91dbc082016-04-04 14:48:07 -0700580 struct scsi_data_buffer *sdb;
581
Christoph Hellwigd2852032014-01-17 12:06:53 +0100582 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700583 sg_free_table_chained(&cmd->sdb.table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700584 if (cmd->request->next_rq) {
585 sdb = cmd->request->next_rq->special;
586 if (sdb)
Ming Lin001d63b2016-04-04 14:48:09 -0700587 sg_free_table_chained(&sdb->table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700588 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100589 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700590 sg_free_table_chained(&cmd->prot_sdb->table, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100591}
592
593static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
594{
Christoph Hellwigd2852032014-01-17 12:06:53 +0100595 scsi_mq_free_sgtables(cmd);
596 scsi_uninit_cmd(cmd);
Bart Van Assche2dd6fb52017-06-02 14:21:58 -0700597 scsi_del_cmd_from_list(cmd);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/*
601 * Function: scsi_release_buffers()
602 *
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200603 * Purpose: Free resources allocate for a scsi_command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 *
605 * Arguments: cmd - command that we are bailing.
606 *
607 * Lock status: Assumed that no lock is held upon entry.
608 *
609 * Returns: Nothing
610 *
611 * Notes: In the event that an upper level driver rejects a
612 * command, we must release resources allocated during
613 * the __init_io() function. Primarily this would involve
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200614 * the scatter-gather table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
Christoph Hellwigf1bea552014-04-15 12:26:54 +0200616static void scsi_release_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200618 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700619 sg_free_table_chained(&cmd->sdb.table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200620
621 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
622
623 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700624 sg_free_table_chained(&cmd->prot_sdb->table, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200627static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
628{
629 struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special;
630
Ming Lin001d63b2016-04-04 14:48:09 -0700631 sg_free_table_chained(&bidi_sdb->table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200632 kmem_cache_free(scsi_sdb_cache, bidi_sdb);
633 cmd->request->next_rq->special = NULL;
634}
635
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200636static bool scsi_end_request(struct request *req, blk_status_t error,
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800637 unsigned int bytes, unsigned int bidi_bytes)
638{
Bart Van Asschebed22132017-08-25 13:46:32 -0700639 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800640 struct scsi_device *sdev = cmd->device;
641 struct request_queue *q = sdev->request_queue;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800642
643 if (blk_update_request(req, error, bytes))
644 return true;
645
646 /* Bidi request must be completed as a whole */
647 if (unlikely(bidi_bytes) &&
648 blk_update_request(req->next_rq, error, bidi_bytes))
649 return true;
650
651 if (blk_queue_add_random(q))
652 add_disk_randomness(req->rq_disk);
653
Bart Van Assche64104f72017-08-30 16:58:39 -0700654 if (!blk_rq_is_scsi(req)) {
655 WARN_ON_ONCE(!(cmd->flags & SCMD_INITIALIZED));
656 cmd->flags &= ~SCMD_INITIALIZED;
657 }
658
Christoph Hellwigd2852032014-01-17 12:06:53 +0100659 if (req->mq_ctx) {
660 /*
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700661 * In the MQ case the command gets freed by __blk_mq_end_request,
Christoph Hellwigd2852032014-01-17 12:06:53 +0100662 * so we have to do all cleanup that depends on it earlier.
663 *
664 * We also can't kick the queues from irq context, so we
665 * will have to defer it to a workqueue.
666 */
667 scsi_mq_uninit_cmd(cmd);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800668
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700669 __blk_mq_end_request(req, error);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100670
671 if (scsi_target(sdev)->single_lun ||
672 !list_empty(&sdev->host->starved_list))
673 kblockd_schedule_work(&sdev->requeue_work);
674 else
Bart Van Assche36e3cf22017-04-07 11:16:53 -0700675 blk_mq_run_hw_queues(q, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100676 } else {
677 unsigned long flags;
678
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400679 if (bidi_bytes)
680 scsi_release_bidi_buffers(cmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +0300681 scsi_release_buffers(cmd);
682 scsi_put_command(cmd);
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400683
Christoph Hellwigd2852032014-01-17 12:06:53 +0100684 spin_lock_irqsave(q->queue_lock, flags);
685 blk_finish_request(req, error);
686 spin_unlock_irqrestore(q->queue_lock, flags);
687
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700688 scsi_run_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100689 }
690
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700691 put_device(&sdev->sdev_gendev);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800692 return false;
693}
694
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200695/**
696 * __scsi_error_from_host_byte - translate SCSI error code into errno
697 * @cmd: SCSI command (unused)
698 * @result: scsi error code
699 *
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200700 * Translate SCSI error code into block errors.
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200701 */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200702static blk_status_t __scsi_error_from_host_byte(struct scsi_cmnd *cmd,
703 int result)
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100704{
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200705 switch (host_byte(result)) {
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100706 case DID_TRANSPORT_FAILFAST:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200707 return BLK_STS_TRANSPORT;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100708 case DID_TARGET_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000709 set_host_byte(cmd, DID_OK);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200710 return BLK_STS_TARGET;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100711 case DID_NEXUS_FAILURE:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200712 return BLK_STS_NEXUS;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200713 case DID_ALLOC_FAILURE:
714 set_host_byte(cmd, DID_OK);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200715 return BLK_STS_NOSPC;
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200716 case DID_MEDIUM_ERROR:
717 set_host_byte(cmd, DID_OK);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200718 return BLK_STS_MEDIUM;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100719 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200720 return BLK_STS_IOERR;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100721 }
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/*
725 * Function: scsi_io_completion()
726 *
727 * Purpose: Completion processing for block device I/O requests.
728 *
729 * Arguments: cmd - command that is finished.
730 *
731 * Lock status: Assumed that no lock is held upon entry.
732 *
733 * Returns: Nothing
734 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200735 * Notes: We will finish off the specified number of sectors. If we
736 * are done, the command block will be released and the queue
737 * function will be goosed. If we are not done then we have to
Alan Sternb60af5b2008-11-03 15:56:47 -0500738 * figure out what to do next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 *
Alan Sternb60af5b2008-11-03 15:56:47 -0500740 * a) We can call scsi_requeue_command(). The request
741 * will be unprepared and put back on the queue. Then
742 * a new command will be created for it. This should
743 * be used if we made forward progress, or if we want
744 * to switch from READ(10) to READ(6) for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200746 * b) We can call __scsi_queue_insert(). The request will
Alan Sternb60af5b2008-11-03 15:56:47 -0500747 * be put back on the queue and retried using the same
748 * command as before, possibly after a delay.
749 *
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800750 * c) We can call scsi_end_request() with -EIO to fail
Alan Sternb60af5b2008-11-03 15:56:47 -0500751 * the remainder of the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700753void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 int result = cmd->result;
Jens Axboe165125e2007-07-24 09:28:11 +0200756 struct request_queue *q = cmd->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct request *req = cmd->request;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200758 blk_status_t error = BLK_STS_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct scsi_sense_hdr sshdr;
Hannes Reinecke4753cbc2014-10-24 14:26:52 +0200760 bool sense_valid = false;
Hannes Reineckec11c0042014-10-24 14:27:01 +0200761 int sense_deferred = 0, level = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500762 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
763 ACTION_DELAYED_RETRY} action;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900764 unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (result) {
767 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
768 if (sense_valid)
769 sense_deferred = scsi_sense_is_deferred(&sshdr);
770 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200771
Christoph Hellwig57292b52017-01-31 16:57:29 +0100772 if (blk_rq_is_passthrough(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (result) {
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100774 if (sense_valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /*
776 * SG_IO wants current and deferred errors
777 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100778 scsi_req(req)->sense_len =
779 min(8 + cmd->sense_buffer[7],
780 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500782 if (!sense_deferred)
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100783 error = __scsi_error_from_host_byte(cmd, result);
Pete Wyckoffb22f6872007-03-13 16:53:28 -0400784 }
Mike Snitzer27c41972012-05-31 15:05:33 -0400785 /*
786 * __scsi_error_from_host_byte may have reset the host_byte
787 */
Christoph Hellwig17d53632017-04-20 16:03:01 +0200788 scsi_req(req)->result = cmd->result;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100789 scsi_req(req)->resid_len = scsi_get_resid(cmd);
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900790
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200791 if (scsi_bidi_cmnd(cmd)) {
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900792 /*
793 * Bidi commands Must be complete as a whole,
794 * both sides at once.
795 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100796 scsi_req(req->next_rq)->resid_len = scsi_in(cmd)->resid;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200797 if (scsi_end_request(req, BLK_STS_OK, blk_rq_bytes(req),
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800798 blk_rq_bytes(req->next_rq)))
799 BUG();
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200800 return;
801 }
James Bottomley89fb4cd2014-07-03 19:17:34 +0200802 } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
803 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100804 * Flush commands do not transfers any data, and thus cannot use
James Bottomley89fb4cd2014-07-03 19:17:34 +0200805 * good_bytes != blk_rq_bytes(req) as the signal for an error.
806 * This sets the error explicitly for the problem case.
807 */
808 error = __scsi_error_from_host_byte(cmd, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
Christoph Hellwig57292b52017-01-31 16:57:29 +0100811 /* no bidi support for !blk_rq_is_passthrough yet */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200812 BUG_ON(blk_bidi_rq(req));
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * Next deal with any sectors which we were able to correctly
816 * handle.
817 */
Hannes Reinecke91921e02014-06-25 16:39:59 +0200818 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
819 "%u sectors total, %d bytes done.\n",
820 blk_rq_sectors(req), good_bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
James Bottomleya9bddd72009-03-30 16:55:51 +0000822 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100823 * Recovered errors need reporting, but they're always treated as
824 * success, so fiddle the result code here. For passthrough requests
Christoph Hellwig17d53632017-04-20 16:03:01 +0200825 * we already took a copy of the original into sreq->result which
James Bottomleya9bddd72009-03-30 16:55:51 +0000826 * is what gets returned to the user
827 */
Douglas Gilberte7efe592010-01-03 13:51:15 -0500828 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
829 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
830 * print since caller wants ATA registers. Only occurs on
831 * SCSI ATA PASS_THROUGH commands when CK_COND=1
832 */
833 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
834 ;
Christoph Hellwige8064022016-10-20 15:12:13 +0200835 else if (!(req->rq_flags & RQF_QUIET))
Hannes Reinecked811b842014-10-24 14:26:45 +0200836 scsi_print_sense(cmd);
James Bottomleya9bddd72009-03-30 16:55:51 +0000837 result = 0;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100838 /* for passthrough error may be set */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200839 error = BLK_STS_OK;
James Bottomleya9bddd72009-03-30 16:55:51 +0000840 }
841
842 /*
James Bottomleya621bac2016-05-13 12:04:06 -0700843 * special case: failed zero length commands always need to
844 * drop down into the retry code. Otherwise, if we finished
845 * all bytes in the request we are done now.
James Bottomleyd6b0c532006-07-02 10:06:28 -0500846 */
James Bottomleya621bac2016-05-13 12:04:06 -0700847 if (!(blk_rq_bytes(req) == 0 && error) &&
848 !scsi_end_request(req, error, good_bytes, 0))
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800849 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200850
851 /*
852 * Kill remainder if no retrys.
853 */
854 if (error && scsi_noretry_cmd(cmd)) {
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800855 if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
856 BUG();
857 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200858 }
859
860 /*
861 * If there had been no error, but we have leftover bytes in the
862 * requeues just queue the command up again.
863 */
864 if (result == 0)
865 goto requeue;
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700866
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100867 error = __scsi_error_from_host_byte(cmd, result);
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500868
Alan Sternb60af5b2008-11-03 15:56:47 -0500869 if (host_byte(result) == DID_RESET) {
870 /* Third party bus reset or reset for error recovery
871 * reasons. Just retry the command and see what
872 * happens.
873 */
874 action = ACTION_RETRY;
875 } else if (sense_valid && !sense_deferred) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 switch (sshdr.sense_key) {
877 case UNIT_ATTENTION:
878 if (cmd->device->removable) {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700879 /* Detected disc change. Set a bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * and quietly refuse further access.
881 */
882 cmd->device->changed = 1;
Alan Sternb60af5b2008-11-03 15:56:47 -0500883 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } else {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700885 /* Must have been a power glitch, or a
886 * bus reset. Could not have been a
887 * media change, so we just retry the
Alan Sternb60af5b2008-11-03 15:56:47 -0500888 * command and see what happens.
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700889 */
Alan Sternb60af5b2008-11-03 15:56:47 -0500890 action = ACTION_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892 break;
893 case ILLEGAL_REQUEST:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700894 /* If we had an ILLEGAL REQUEST returned, then
895 * we may have performed an unsupported
896 * command. The only thing this should be
897 * would be a ten byte read where only a six
898 * byte read was supported. Also, on a system
899 * where READ CAPACITY failed, we may have
900 * read past the end of the disk.
901 */
Jens Axboe26a68012005-11-29 21:03:34 +0100902 if ((cmd->device->use_10_for_rw &&
903 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 (cmd->cmnd[0] == READ_10 ||
905 cmd->cmnd[0] == WRITE_10)) {
Alan Sternb60af5b2008-11-03 15:56:47 -0500906 /* This will issue a new 6-byte command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 cmd->device->use_10_for_rw = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500908 action = ACTION_REPREP;
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500909 } else if (sshdr.asc == 0x10) /* DIX */ {
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500910 action = ACTION_FAIL;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200911 error = BLK_STS_PROTECTION;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500912 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
Martin K. Petersen5db44862012-09-18 12:19:32 -0400913 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500914 action = ACTION_FAIL;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200915 error = BLK_STS_TARGET;
Alan Sternb60af5b2008-11-03 15:56:47 -0500916 } else
917 action = ACTION_FAIL;
918 break;
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400919 case ABORTED_COMMAND:
James Bottomley126c09822009-02-19 21:48:54 +0000920 action = ACTION_FAIL;
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200921 if (sshdr.asc == 0x10) /* DIF */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200922 error = BLK_STS_PROTECTION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 break;
924 case NOT_READY:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700925 /* If the device is in the process of becoming
James Bottomleyf3e93f72006-04-25 16:48:30 -0500926 * ready, or has a temporary blockage, retry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 */
James Bottomleyf3e93f72006-04-25 16:48:30 -0500928 if (sshdr.asc == 0x04) {
929 switch (sshdr.ascq) {
930 case 0x01: /* becoming ready */
931 case 0x04: /* format in progress */
932 case 0x05: /* rebuild in progress */
933 case 0x06: /* recalculation in progress */
934 case 0x07: /* operation in progress */
935 case 0x08: /* Long write in progress */
936 case 0x09: /* self test in progress */
Martin K. Petersend8705f12009-11-26 12:00:41 -0500937 case 0x14: /* space allocation in progress */
Alan Sternb60af5b2008-11-03 15:56:47 -0500938 action = ACTION_DELAYED_RETRY;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500939 break;
Alan Stern3dbf6a52008-12-15 10:31:28 -0500940 default:
Alan Stern3dbf6a52008-12-15 10:31:28 -0500941 action = ACTION_FAIL;
942 break;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500943 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200944 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500945 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500946 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 case VOLUME_OVERFLOW:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700948 /* See SSC3rXX or current. */
Alan Sternb60af5b2008-11-03 15:56:47 -0500949 action = ACTION_FAIL;
950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 default:
Alan Sternb60af5b2008-11-03 15:56:47 -0500952 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
954 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200955 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500956 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500957
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900958 if (action != ACTION_FAIL &&
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200959 time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900960 action = ACTION_FAIL;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900961
Alan Sternb60af5b2008-11-03 15:56:47 -0500962 switch (action) {
963 case ACTION_FAIL:
964 /* Give up and fail the remainder of the request */
Christoph Hellwige8064022016-10-20 15:12:13 +0200965 if (!(req->rq_flags & RQF_QUIET)) {
Hannes Reineckef1569ff2014-10-24 14:27:07 +0200966 static DEFINE_RATELIMIT_STATE(_rs,
967 DEFAULT_RATELIMIT_INTERVAL,
968 DEFAULT_RATELIMIT_BURST);
969
970 if (unlikely(scsi_logging_level))
971 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
972 SCSI_LOG_MLCOMPLETE_BITS);
973
974 /*
975 * if logging is enabled the failure will be printed
976 * in scsi_log_completion(), so avoid duplicate messages
977 */
978 if (!level && __ratelimit(&_rs)) {
979 scsi_print_result(cmd, NULL, FAILED);
980 if (driver_byte(result) & DRIVER_SENSE)
981 scsi_print_sense(cmd);
982 scsi_print_command(cmd);
983 }
James Bottomley3173d8c2005-09-04 11:32:05 -0500984 }
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800985 if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
986 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200987 /*FALLTHRU*/
Alan Sternb60af5b2008-11-03 15:56:47 -0500988 case ACTION_REPREP:
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200989 requeue:
Alan Sternb60af5b2008-11-03 15:56:47 -0500990 /* Unprep the request and put it back at the head of the queue.
991 * A new command will be prepared and issued.
992 */
Christoph Hellwigd2852032014-01-17 12:06:53 +0100993 if (q->mq_ops) {
Christoph Hellwigd2852032014-01-17 12:06:53 +0100994 scsi_mq_requeue_cmd(cmd);
995 } else {
996 scsi_release_buffers(cmd);
997 scsi_requeue_command(q, cmd);
998 }
Alan Sternb60af5b2008-11-03 15:56:47 -0500999 break;
1000 case ACTION_RETRY:
1001 /* Retry the same command immediately */
James Bottomley4f5299a2009-01-02 10:42:21 -06001002 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -05001003 break;
1004 case ACTION_DELAYED_RETRY:
1005 /* Retry the same command after a delay */
James Bottomley4f5299a2009-01-02 10:42:21 -06001006 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -05001007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001011static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001013 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001016 * If sg table allocation fails, requeue request later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001018 if (unlikely(sg_alloc_table_chained(&sdb->table,
1019 blk_rq_nr_phys_segments(req), sdb->table.sgl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return BLKPREP_DEFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /*
1023 * Next, walk the list, and fill in the addresses and sizes of
1024 * each segment.
1025 */
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001026 count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
1027 BUG_ON(count > sdb->table.nents);
1028 sdb->table.nents = count;
Christoph Hellwigfd102b12017-01-13 12:29:11 +01001029 sdb->length = blk_rq_payload_bytes(req);
Rusty Russell4a03d902007-11-19 11:28:48 +11001030 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001032
1033/*
1034 * Function: scsi_init_io()
1035 *
1036 * Purpose: SCSI I/O initialize function.
1037 *
1038 * Arguments: cmd - Command descriptor we wish to initialize
1039 *
1040 * Returns: 0 on success
1041 * BLKPREP_DEFER if the failure is retryable
1042 * BLKPREP_KILL if the failure is fatal
1043 */
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001044int scsi_init_io(struct scsi_cmnd *cmd)
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001045{
Christoph Hellwig5e012aa2014-04-15 12:24:55 +02001046 struct scsi_device *sdev = cmd->device;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001047 struct request *rq = cmd->request;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001048 bool is_mq = (rq->mq_ctx != NULL);
Johannes Thumshirne7661a82017-04-12 09:21:19 +02001049 int error = BLKPREP_KILL;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001050
Johannes Thumshirnfd3fc0b2017-01-31 10:16:00 +01001051 if (WARN_ON_ONCE(!blk_rq_nr_phys_segments(rq)))
Johannes Thumshirne7661a82017-04-12 09:21:19 +02001052 goto err_exit;
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001053
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001054 error = scsi_init_sgtable(rq, &cmd->sdb);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001055 if (error)
1056 goto err_exit;
1057
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001058 if (blk_bidi_rq(rq)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001059 if (!rq->q->mq_ops) {
1060 struct scsi_data_buffer *bidi_sdb =
1061 kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC);
1062 if (!bidi_sdb) {
1063 error = BLKPREP_DEFER;
1064 goto err_exit;
1065 }
1066
1067 rq->next_rq->special = bidi_sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001068 }
1069
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001070 error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001071 if (error)
1072 goto err_exit;
1073 }
1074
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001075 if (blk_integrity_rq(rq)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001076 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1077 int ivecs, count;
1078
Ewan D. Milne91724c22015-01-15 10:02:12 -05001079 if (prot_sdb == NULL) {
1080 /*
1081 * This can happen if someone (e.g. multipath)
1082 * queues a command to a device on an adapter
1083 * that does not support DIX.
1084 */
1085 WARN_ON_ONCE(1);
1086 error = BLKPREP_KILL;
1087 goto err_exit;
1088 }
1089
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001090 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001091
Ming Lin001d63b2016-04-04 14:48:09 -07001092 if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
Ming Lin22cc3d42016-04-04 14:48:08 -07001093 prot_sdb->table.sgl)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001094 error = BLKPREP_DEFER;
1095 goto err_exit;
1096 }
1097
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001098 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001099 prot_sdb->table.sgl);
1100 BUG_ON(unlikely(count > ivecs));
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001101 BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q)));
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001102
1103 cmd->prot_sdb = prot_sdb;
1104 cmd->prot_sdb->table.nents = count;
1105 }
1106
Christoph Hellwigd2852032014-01-17 12:06:53 +01001107 return BLKPREP_OK;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001108err_exit:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001109 if (is_mq) {
1110 scsi_mq_free_sgtables(cmd);
1111 } else {
1112 scsi_release_buffers(cmd);
1113 cmd->request->special = NULL;
1114 scsi_put_command(cmd);
1115 put_device(&sdev->sdev_gendev);
1116 }
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001117 return error;
1118}
Boaz Harroshbb52d822007-12-13 16:14:27 -08001119EXPORT_SYMBOL(scsi_init_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07001121/**
Bart Van Assche832889f2017-08-30 16:58:40 -07001122 * scsi_initialize_rq - initialize struct scsi_cmnd partially
Jonathan Corbet35c05062017-08-24 16:11:09 -06001123 * @rq: Request associated with the SCSI command to be initialized.
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07001124 *
Bart Van Assche832889f2017-08-30 16:58:40 -07001125 * This function initializes the members of struct scsi_cmnd that must be
1126 * initialized before request processing starts and that won't be
1127 * reinitialized if a SCSI command is requeued.
1128 *
Bart Van Assche64104f72017-08-30 16:58:39 -07001129 * Called from inside blk_get_request() for pass-through requests and from
1130 * inside scsi_init_command() for filesystem requests.
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07001131 */
1132void scsi_initialize_rq(struct request *rq)
1133{
Bart Van Asschec8d9cf22017-06-20 11:15:42 -07001134 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1135
1136 scsi_req_init(&cmd->req);
Bart Van Assche832889f2017-08-30 16:58:40 -07001137 cmd->jiffies_at_alloc = jiffies;
1138 cmd->retries = 0;
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07001139}
1140EXPORT_SYMBOL(scsi_initialize_rq);
1141
Bart Van Assche2dd6fb52017-06-02 14:21:58 -07001142/* Add a command to the list used by the aacraid and dpt_i2o drivers */
1143void scsi_add_cmd_to_list(struct scsi_cmnd *cmd)
1144{
1145 struct scsi_device *sdev = cmd->device;
1146 struct Scsi_Host *shost = sdev->host;
1147 unsigned long flags;
1148
1149 if (shost->use_cmd_list) {
1150 spin_lock_irqsave(&sdev->list_lock, flags);
1151 list_add_tail(&cmd->list, &sdev->cmd_list);
1152 spin_unlock_irqrestore(&sdev->list_lock, flags);
1153 }
1154}
1155
1156/* Remove a command from the list used by the aacraid and dpt_i2o drivers */
1157void scsi_del_cmd_from_list(struct scsi_cmnd *cmd)
1158{
1159 struct scsi_device *sdev = cmd->device;
1160 struct Scsi_Host *shost = sdev->host;
1161 unsigned long flags;
1162
1163 if (shost->use_cmd_list) {
1164 spin_lock_irqsave(&sdev->list_lock, flags);
1165 BUG_ON(list_empty(&cmd->list));
1166 list_del_init(&cmd->list);
1167 spin_unlock_irqrestore(&sdev->list_lock, flags);
1168 }
1169}
1170
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07001171/* Called after a request has been started. */
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001172void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001173{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001174 void *buf = cmd->sense_buffer;
1175 void *prot = cmd->prot_sdb;
Bart Van Assche64104f72017-08-30 16:58:39 -07001176 struct request *rq = blk_mq_rq_from_pdu(cmd);
1177 unsigned int flags = cmd->flags & SCMD_PRESERVED_FLAGS;
Bart Van Assche832889f2017-08-30 16:58:40 -07001178 unsigned long jiffies_at_alloc;
1179 int retries;
Bart Van Assche64104f72017-08-30 16:58:39 -07001180
1181 if (!blk_rq_is_scsi(rq) && !(flags & SCMD_INITIALIZED)) {
1182 flags |= SCMD_INITIALIZED;
1183 scsi_initialize_rq(rq);
1184 }
Christoph Hellwig3b003152006-11-04 20:10:55 +01001185
Bart Van Assche832889f2017-08-30 16:58:40 -07001186 jiffies_at_alloc = cmd->jiffies_at_alloc;
1187 retries = cmd->retries;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001188 /* zero out the cmd, except for the embedded scsi_request */
1189 memset((char *)cmd + sizeof(cmd->req), 0,
Christoph Hellwigee524232017-02-21 10:04:55 +01001190 sizeof(*cmd) - sizeof(cmd->req) + dev->host->hostt->cmd_size);
Christoph Hellwig04796332014-02-20 14:20:55 -08001191
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001192 cmd->device = dev;
1193 cmd->sense_buffer = buf;
1194 cmd->prot_sdb = prot;
Bart Van Assche64104f72017-08-30 16:58:39 -07001195 cmd->flags = flags;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001196 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
Bart Van Assche832889f2017-08-30 16:58:40 -07001197 cmd->jiffies_at_alloc = jiffies_at_alloc;
1198 cmd->retries = retries;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001199
Bart Van Assche2dd6fb52017-06-02 14:21:58 -07001200 scsi_add_cmd_to_list(cmd);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001201}
1202
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001203static int scsi_setup_scsi_cmnd(struct scsi_device *sdev, struct request *req)
James Bottomley7b163182005-12-15 20:17:02 -06001204{
Bart Van Asschebed22132017-08-25 13:46:32 -07001205 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001206
1207 /*
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001208 * Passthrough requests may transfer data, in which case they must
Christoph Hellwig3b003152006-11-04 20:10:55 +01001209 * a bio attached to them. Or they might contain a SCSI command
1210 * that does not transfer data, in which case they may optionally
1211 * submit a request without an attached bio.
1212 */
1213 if (req->bio) {
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001214 int ret = scsi_init_io(cmd);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001215 if (unlikely(ret))
1216 return ret;
1217 } else {
Tejun Heob0790412009-05-07 22:24:42 +09001218 BUG_ON(blk_rq_bytes(req));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001219
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001220 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001221 }
James Bottomley7b163182005-12-15 20:17:02 -06001222
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001223 cmd->cmd_len = scsi_req(req)->cmd_len;
1224 cmd->cmnd = scsi_req(req)->cmd;
Tejun Heob0790412009-05-07 22:24:42 +09001225 cmd->transfersize = blk_rq_bytes(req);
Christoph Hellwig64c7f1d2017-04-05 19:18:12 +02001226 cmd->allowed = scsi_req(req)->retries;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001227 return BLKPREP_OK;
1228}
1229
1230/*
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001231 * Setup a normal block command. These are simple request from filesystems
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001232 * that still need to be translated to SCSI CDBs from the ULD.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001233 */
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001234static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001235{
Bart Van Asschebed22132017-08-25 13:46:32 -07001236 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001237
Christoph Hellwigee14c672015-08-27 14:16:59 +02001238 if (unlikely(sdev->handler && sdev->handler->prep_fn)) {
1239 int ret = sdev->handler->prep_fn(sdev, req);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001240 if (ret != BLKPREP_OK)
1241 return ret;
1242 }
1243
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001244 cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001245 memset(cmd->cmnd, 0, BLK_MAX_CDB);
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001246 return scsi_cmd_to_driver(cmd)->init_command(cmd);
James Bottomley7b163182005-12-15 20:17:02 -06001247}
James Bottomley7b163182005-12-15 20:17:02 -06001248
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001249static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req)
1250{
Bart Van Asschebed22132017-08-25 13:46:32 -07001251 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001252
1253 if (!blk_rq_bytes(req))
1254 cmd->sc_data_direction = DMA_NONE;
1255 else if (rq_data_dir(req) == WRITE)
1256 cmd->sc_data_direction = DMA_TO_DEVICE;
1257 else
1258 cmd->sc_data_direction = DMA_FROM_DEVICE;
1259
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001260 if (blk_rq_is_scsi(req))
1261 return scsi_setup_scsi_cmnd(sdev, req);
1262 else
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001263 return scsi_setup_fs_cmnd(sdev, req);
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001264}
1265
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001266static int
1267scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Christoph Hellwig3b003152006-11-04 20:10:55 +01001269 int ret = BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001272 * If the device is not in running state we will reject some
1273 * or all commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
Christoph Hellwig3b003152006-11-04 20:10:55 +01001276 switch (sdev->sdev_state) {
1277 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05001278 case SDEV_TRANSPORT_OFFLINE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001279 /*
1280 * If the device is offline we refuse to process any
1281 * commands. The device must be brought online
1282 * before trying any recovery commands.
1283 */
1284 sdev_printk(KERN_ERR, sdev,
1285 "rejecting I/O to offline device\n");
1286 ret = BLKPREP_KILL;
1287 break;
1288 case SDEV_DEL:
1289 /*
1290 * If the device is fully deleted, we refuse to
1291 * process any commands as well.
1292 */
James Bottomley9ccfc752005-10-02 11:45:08 -05001293 sdev_printk(KERN_ERR, sdev,
1294 "rejecting I/O to dead device\n");
Christoph Hellwig3b003152006-11-04 20:10:55 +01001295 ret = BLKPREP_KILL;
1296 break;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001297 case SDEV_BLOCK:
James Bottomley6f4267e2008-08-22 16:53:31 -05001298 case SDEV_CREATED_BLOCK:
Bart Van Asschebba0bdd2015-03-04 10:31:47 +01001299 ret = BLKPREP_DEFER;
1300 break;
1301 case SDEV_QUIESCE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001302 /*
1303 * If the devices is blocked we defer normal commands.
1304 */
Ming Leiaeec7762017-10-14 17:22:31 +08001305 if (req && !(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001306 ret = BLKPREP_DEFER;
1307 break;
1308 default:
1309 /*
1310 * For any other not fully online state we only allow
1311 * special commands. In particular any user initiated
1312 * command is not allowed.
1313 */
Ming Leiaeec7762017-10-14 17:22:31 +08001314 if (req && !(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001315 ret = BLKPREP_KILL;
1316 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001319 return ret;
1320}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001322static int
1323scsi_prep_return(struct request_queue *q, struct request *req, int ret)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001324{
1325 struct scsi_device *sdev = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Christoph Hellwig3b003152006-11-04 20:10:55 +01001327 switch (ret) {
1328 case BLKPREP_KILL:
jiangyiwene1cd3912016-02-16 20:14:13 +08001329 case BLKPREP_INVALID:
Christoph Hellwig17d53632017-04-20 16:03:01 +02001330 scsi_req(req)->result = DID_NO_CONNECT << 16;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001331 /* release the command and kill it */
1332 if (req->special) {
1333 struct scsi_cmnd *cmd = req->special;
1334 scsi_release_buffers(cmd);
1335 scsi_put_command(cmd);
Christoph Hellwig68c03d92014-04-15 12:24:56 +02001336 put_device(&sdev->sdev_gendev);
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001337 req->special = NULL;
1338 }
Christoph Hellwig3b003152006-11-04 20:10:55 +01001339 break;
1340 case BLKPREP_DEFER:
1341 /*
Tejun Heo9934c8c2009-05-08 11:54:16 +09001342 * If we defer, the blk_peek_request() returns NULL, but the
Jens Axboea488e742010-04-16 21:13:15 +02001343 * queue must be restarted, so we schedule a callback to happen
1344 * shortly.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001345 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001346 if (atomic_read(&sdev->device_busy) == 0)
Jens Axboea488e742010-04-16 21:13:15 +02001347 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001348 break;
1349 default:
Christoph Hellwige8064022016-10-20 15:12:13 +02001350 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Christoph Hellwig3b003152006-11-04 20:10:55 +01001353 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001355
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001356static int scsi_prep_fn(struct request_queue *q, struct request *req)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001357{
1358 struct scsi_device *sdev = q->queuedata;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001359 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001360 int ret;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001361
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001362 ret = scsi_prep_state_check(sdev, req);
1363 if (ret != BLKPREP_OK)
1364 goto out;
1365
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001366 if (!req->special) {
1367 /* Bail if we can't get a reference to the device */
1368 if (unlikely(!get_device(&sdev->sdev_gendev))) {
1369 ret = BLKPREP_DEFER;
1370 goto out;
1371 }
1372
1373 scsi_init_command(sdev, cmd);
1374 req->special = cmd;
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001375 }
1376
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001377 cmd->tag = req->tag;
1378 cmd->request = req;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03001379 cmd->prot_op = SCSI_PROT_NORMAL;
1380
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001381 ret = scsi_setup_cmnd(sdev, req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001382out:
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001383 return scsi_prep_return(q, req, ret);
1384}
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001385
1386static void scsi_unprep_fn(struct request_queue *q, struct request *req)
1387{
Bart Van Asschebed22132017-08-25 13:46:32 -07001388 scsi_uninit_cmd(blk_mq_rq_to_pdu(req));
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391/*
1392 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1393 * return 0.
1394 *
1395 * Called with the queue_lock held.
1396 */
1397static inline int scsi_dev_queue_ready(struct request_queue *q,
1398 struct scsi_device *sdev)
1399{
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001400 unsigned int busy;
1401
1402 busy = atomic_inc_return(&sdev->device_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001403 if (atomic_read(&sdev->device_blocked)) {
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001404 if (busy)
1405 goto out_dec;
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /*
1408 * unblock after device_blocked iterates to zero
1409 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001410 if (atomic_dec_return(&sdev->device_blocked) > 0) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001411 /*
1412 * For the MQ case we take care of this in the caller.
1413 */
1414 if (!q->mq_ops)
1415 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001416 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001418 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1419 "unblocking device at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001421
1422 if (busy >= sdev->queue_depth)
1423 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 return 1;
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001426out_dec:
1427 atomic_dec(&sdev->device_busy);
1428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Mike Christief0c0a372008-08-17 15:24:38 -05001431/*
1432 * scsi_target_queue_ready: checks if there we can send commands to target
1433 * @sdev: scsi device on starget to check.
Mike Christief0c0a372008-08-17 15:24:38 -05001434 */
1435static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1436 struct scsi_device *sdev)
1437{
1438 struct scsi_target *starget = scsi_target(sdev);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001439 unsigned int busy;
Mike Christief0c0a372008-08-17 15:24:38 -05001440
1441 if (starget->single_lun) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001442 spin_lock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001443 if (starget->starget_sdev_user &&
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001444 starget->starget_sdev_user != sdev) {
1445 spin_unlock_irq(shost->host_lock);
1446 return 0;
1447 }
Mike Christief0c0a372008-08-17 15:24:38 -05001448 starget->starget_sdev_user = sdev;
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001449 spin_unlock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001450 }
1451
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001452 if (starget->can_queue <= 0)
1453 return 1;
1454
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001455 busy = atomic_inc_return(&starget->target_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001456 if (atomic_read(&starget->target_blocked) > 0) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001457 if (busy)
1458 goto starved;
1459
Mike Christief0c0a372008-08-17 15:24:38 -05001460 /*
1461 * unblock after target_blocked iterates to zero
1462 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001463 if (atomic_dec_return(&starget->target_blocked) > 0)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001464 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001465
1466 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1467 "unblocking target at zero depth\n"));
Mike Christief0c0a372008-08-17 15:24:38 -05001468 }
1469
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001470 if (busy >= starget->can_queue)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001471 goto starved;
Mike Christief0c0a372008-08-17 15:24:38 -05001472
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001473 return 1;
1474
1475starved:
1476 spin_lock_irq(shost->host_lock);
1477 list_move_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001478 spin_unlock_irq(shost->host_lock);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001479out_dec:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001480 if (starget->can_queue > 0)
1481 atomic_dec(&starget->target_busy);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001482 return 0;
Mike Christief0c0a372008-08-17 15:24:38 -05001483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/*
1486 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1487 * return 0. We must end up running the queue again whenever 0 is
1488 * returned, else IO can hang.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 */
1490static inline int scsi_host_queue_ready(struct request_queue *q,
1491 struct Scsi_Host *shost,
1492 struct scsi_device *sdev)
1493{
Christoph Hellwig74665012014-01-22 15:29:29 +01001494 unsigned int busy;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001495
James Bottomley939647e2005-09-18 15:05:20 -05001496 if (scsi_host_in_recovery(shost))
Christoph Hellwig74665012014-01-22 15:29:29 +01001497 return 0;
1498
1499 busy = atomic_inc_return(&shost->host_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001500 if (atomic_read(&shost->host_blocked) > 0) {
Christoph Hellwig74665012014-01-22 15:29:29 +01001501 if (busy)
1502 goto starved;
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 /*
1505 * unblock after host_blocked iterates to zero
1506 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001507 if (atomic_dec_return(&shost->host_blocked) > 0)
Christoph Hellwig74665012014-01-22 15:29:29 +01001508 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001509
1510 SCSI_LOG_MLQUEUE(3,
1511 shost_printk(KERN_INFO, shost,
1512 "unblocking host at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
Christoph Hellwig74665012014-01-22 15:29:29 +01001514
1515 if (shost->can_queue > 0 && busy >= shost->can_queue)
1516 goto starved;
1517 if (shost->host_self_blocked)
1518 goto starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 /* We're OK to process the command, so we can't be starved */
Christoph Hellwig74665012014-01-22 15:29:29 +01001521 if (!list_empty(&sdev->starved_entry)) {
1522 spin_lock_irq(shost->host_lock);
1523 if (!list_empty(&sdev->starved_entry))
1524 list_del_init(&sdev->starved_entry);
1525 spin_unlock_irq(shost->host_lock);
1526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Christoph Hellwig74665012014-01-22 15:29:29 +01001528 return 1;
1529
1530starved:
1531 spin_lock_irq(shost->host_lock);
1532 if (list_empty(&sdev->starved_entry))
1533 list_add_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001534 spin_unlock_irq(shost->host_lock);
Christoph Hellwig74665012014-01-22 15:29:29 +01001535out_dec:
1536 atomic_dec(&shost->host_busy);
1537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540/*
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001541 * Busy state exporting function for request stacking drivers.
1542 *
1543 * For efficiency, no lock is taken to check the busy state of
1544 * shost/starget/sdev, since the returned value is not guaranteed and
1545 * may be changed after request stacking drivers call the function,
1546 * regardless of taking lock or not.
1547 *
Bart Van Assche67bd9412012-06-29 15:33:22 +00001548 * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
1549 * needs to return 'not busy'. Otherwise, request stacking drivers
1550 * may hold requests forever.
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001551 */
1552static int scsi_lld_busy(struct request_queue *q)
1553{
1554 struct scsi_device *sdev = q->queuedata;
1555 struct Scsi_Host *shost;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001556
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001557 if (blk_queue_dying(q))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001558 return 0;
1559
1560 shost = sdev->host;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001561
Jun'ichi Nomurab7e94a12012-05-22 18:57:17 +09001562 /*
1563 * Ignore host/starget busy state.
1564 * Since block layer does not have a concept of fairness across
1565 * multiple queues, congestion of host/starget needs to be handled
1566 * in SCSI layer.
1567 */
1568 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001569 return 1;
1570
1571 return 0;
1572}
1573
1574/*
James Bottomleye91442b2005-09-09 10:44:16 -05001575 * Kill a request for a dead device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 */
Jens Axboe165125e2007-07-24 09:28:11 +02001577static void scsi_kill_request(struct request *req, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Bart Van Asschebed22132017-08-25 13:46:32 -07001579 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Jiri Slaby03b14702009-09-23 16:15:35 +02001580 struct scsi_device *sdev;
1581 struct scsi_target *starget;
1582 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Tejun Heo9934c8c2009-05-08 11:54:16 +09001584 blk_start_request(req);
James Bottomley788ce432005-09-09 13:40:23 -05001585
Hannes Reinecke74571812011-11-09 08:39:24 +01001586 scmd_printk(KERN_INFO, cmd, "killing request\n");
1587
Jiri Slaby03b14702009-09-23 16:15:35 +02001588 sdev = cmd->device;
1589 starget = scsi_target(sdev);
1590 shost = sdev->host;
James Bottomleye91442b2005-09-09 10:44:16 -05001591 scsi_init_cmd_errh(cmd);
1592 cmd->result = DID_NO_CONNECT << 16;
1593 atomic_inc(&cmd->device->iorequest_cnt);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001594
1595 /*
1596 * SCSI request completion path will do scsi_device_unbusy(),
1597 * bump busy counts. To bump the counters, we need to dance
1598 * with the locks as normal issue path does.
1599 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001600 atomic_inc(&sdev->device_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +01001601 atomic_inc(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001602 if (starget->can_queue > 0)
1603 atomic_inc(&starget->target_busy);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001604
Jens Axboe242f9dc2008-09-14 05:55:09 -07001605 blk_complete_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
Jens Axboe1aea6432006-01-09 16:03:03 +01001608static void scsi_softirq_done(struct request *rq)
1609{
Bart Van Asschebed22132017-08-25 13:46:32 -07001610 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
Jens Axboe242f9dc2008-09-14 05:55:09 -07001611 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
Jens Axboe1aea6432006-01-09 16:03:03 +01001612 int disposition;
1613
1614 INIT_LIST_HEAD(&cmd->eh_entry);
1615
Jens Axboe242f9dc2008-09-14 05:55:09 -07001616 atomic_inc(&cmd->device->iodone_cnt);
1617 if (cmd->result)
1618 atomic_inc(&cmd->device->ioerr_cnt);
1619
Jens Axboe1aea6432006-01-09 16:03:03 +01001620 disposition = scsi_decide_disposition(cmd);
1621 if (disposition != SUCCESS &&
1622 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
1623 sdev_printk(KERN_ERR, cmd->device,
1624 "timing out command, waited %lus\n",
1625 wait_for/HZ);
1626 disposition = SUCCESS;
1627 }
Hannes Reinecke91921e02014-06-25 16:39:59 +02001628
Jens Axboe1aea6432006-01-09 16:03:03 +01001629 scsi_log_completion(cmd, disposition);
1630
1631 switch (disposition) {
1632 case SUCCESS:
1633 scsi_finish_command(cmd);
1634 break;
1635 case NEEDS_RETRY:
Christoph Hellwig596f4822007-01-02 12:56:00 +01001636 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
Jens Axboe1aea6432006-01-09 16:03:03 +01001637 break;
1638 case ADD_TO_MLQUEUE:
1639 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1640 break;
1641 default:
Hannes Reineckea0658632017-04-06 15:36:35 +02001642 scsi_eh_scmd_add(cmd);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +02001643 break;
Jens Axboe1aea6432006-01-09 16:03:03 +01001644 }
1645}
1646
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001647/**
Christoph Hellwig82042a22014-09-05 18:23:07 -07001648 * scsi_dispatch_command - Dispatch a command to the low-level driver.
1649 * @cmd: command block we are dispatching.
1650 *
1651 * Return: nonzero return request was rejected and device's queue needs to be
1652 * plugged.
1653 */
1654static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
1655{
1656 struct Scsi_Host *host = cmd->device->host;
1657 int rtn = 0;
1658
1659 atomic_inc(&cmd->device->iorequest_cnt);
1660
1661 /* check if the device is still usable */
1662 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
1663 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
1664 * returns an immediate error upwards, and signals
1665 * that the device is no longer present */
1666 cmd->result = DID_NO_CONNECT << 16;
1667 goto done;
1668 }
1669
1670 /* Check to see if the scsi lld made this device blocked. */
1671 if (unlikely(scsi_device_blocked(cmd->device))) {
1672 /*
1673 * in blocked state, the command is just put back on
1674 * the device queue. The suspend state has already
1675 * blocked the queue so future requests should not
1676 * occur until the device transitions out of the
1677 * suspend state.
1678 */
1679 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1680 "queuecommand : device blocked\n"));
1681 return SCSI_MLQUEUE_DEVICE_BUSY;
1682 }
1683
1684 /* Store the LUN value in cmnd, if needed. */
1685 if (cmd->device->lun_in_cdb)
1686 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
1687 (cmd->device->lun << 5 & 0xe0);
1688
1689 scsi_log_send(cmd);
1690
1691 /*
1692 * Before we queue this command, check if the command
1693 * length exceeds what the host adapter can handle.
1694 */
1695 if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
1696 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1697 "queuecommand : command too long. "
1698 "cdb_size=%d host->max_cmd_len=%d\n",
1699 cmd->cmd_len, cmd->device->host->max_cmd_len));
1700 cmd->result = (DID_ABORT << 16);
1701 goto done;
1702 }
1703
1704 if (unlikely(host->shost_state == SHOST_DEL)) {
1705 cmd->result = (DID_NO_CONNECT << 16);
1706 goto done;
1707
1708 }
1709
1710 trace_scsi_dispatch_cmd_start(cmd);
1711 rtn = host->hostt->queuecommand(host, cmd);
1712 if (rtn) {
1713 trace_scsi_dispatch_cmd_error(cmd, rtn);
1714 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
1715 rtn != SCSI_MLQUEUE_TARGET_BUSY)
1716 rtn = SCSI_MLQUEUE_HOST_BUSY;
1717
1718 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1719 "queuecommand : request rejected\n"));
1720 }
1721
1722 return rtn;
1723 done:
1724 cmd->scsi_done(cmd);
1725 return 0;
1726}
1727
1728/**
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001729 * scsi_done - Invoke completion on finished SCSI command.
1730 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
1731 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
1732 *
1733 * Description: This function is the mid-level's (SCSI Core) interrupt routine,
1734 * which regains ownership of the SCSI command (de facto) from a LLDD, and
1735 * calls blk_complete_request() for further processing.
1736 *
1737 * This function is interrupt context safe.
1738 */
1739static void scsi_done(struct scsi_cmnd *cmd)
1740{
1741 trace_scsi_dispatch_cmd_done(cmd);
1742 blk_complete_request(cmd->request);
1743}
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745/*
1746 * Function: scsi_request_fn()
1747 *
1748 * Purpose: Main strategy routine for SCSI.
1749 *
1750 * Arguments: q - Pointer to actual queue.
1751 *
1752 * Returns: Nothing
1753 *
Damien Le Moale98f42b2017-10-11 05:54:22 +09001754 * Lock status: request queue lock assumed to be held when called.
1755 *
1756 * Note: See sd_zbc.c sd_zbc_write_lock_zone() for write order
1757 * protection for ZBC disks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 */
1759static void scsi_request_fn(struct request_queue *q)
Bart Van Assche613be1f2014-02-20 14:20:56 -08001760 __releases(q->queue_lock)
1761 __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
1763 struct scsi_device *sdev = q->queuedata;
1764 struct Scsi_Host *shost;
1765 struct scsi_cmnd *cmd;
1766 struct request *req;
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /*
1769 * To start with, we keep looping until the queue is empty, or until
1770 * the host is no longer able to accept any more requests.
1771 */
1772 shost = sdev->host;
Jens Axboea488e742010-04-16 21:13:15 +02001773 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 int rtn;
1775 /*
1776 * get next queueable request. We do this early to make sure
Hannes Reinecke91921e02014-06-25 16:39:59 +02001777 * that the request is fully prepared even if we cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 * accept it.
1779 */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001780 req = blk_peek_request(q);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001781 if (!req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 break;
1783
1784 if (unlikely(!scsi_device_online(sdev))) {
James Bottomley9ccfc752005-10-02 11:45:08 -05001785 sdev_printk(KERN_ERR, sdev,
1786 "rejecting I/O to offline device\n");
James Bottomleye91442b2005-09-09 10:44:16 -05001787 scsi_kill_request(req, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 continue;
1789 }
1790
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001791 if (!scsi_dev_queue_ready(q, sdev))
1792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 /*
1795 * Remove the request from the request list.
1796 */
1797 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
Tejun Heo9934c8c2009-05-08 11:54:16 +09001798 blk_start_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001800 spin_unlock_irq(q->queue_lock);
Bart Van Asschebed22132017-08-25 13:46:32 -07001801 cmd = blk_mq_rq_to_pdu(req);
1802 if (cmd != req->special) {
James Bottomleye91442b2005-09-09 10:44:16 -05001803 printk(KERN_CRIT "impossible request in %s.\n"
1804 "please mail a stack trace to "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001805 "linux-scsi@vger.kernel.org\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001806 __func__);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001807 blk_dump_rq_flags(req, "foo");
James Bottomleye91442b2005-09-09 10:44:16 -05001808 BUG();
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Mike Christieecefe8a2008-07-11 19:50:35 -05001811 /*
1812 * We hit this when the driver is using a host wide
1813 * tag map. For device level tag maps the queue_depth check
1814 * in the device ready fn would prevent us from trying
1815 * to allocate a tag. Since the map is a shared host resource
1816 * we add the dev to the starved list so it eventually gets
1817 * a run when a tag is freed.
1818 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001819 if (blk_queue_tagged(q) && !(req->rq_flags & RQF_QUEUED)) {
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001820 spin_lock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001821 if (list_empty(&sdev->starved_entry))
1822 list_add_tail(&sdev->starved_entry,
1823 &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001824 spin_unlock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001825 goto not_ready;
1826 }
1827
Mike Christief0c0a372008-08-17 15:24:38 -05001828 if (!scsi_target_queue_ready(shost, sdev))
1829 goto not_ready;
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (!scsi_host_queue_ready(q, shost, sdev))
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001832 goto host_not_ready;
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001833
1834 if (sdev->simple_tags)
1835 cmd->flags |= SCMD_TAGGED;
1836 else
1837 cmd->flags &= ~SCMD_TAGGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /*
1840 * Finally, initialize any error handling parameters, and set up
1841 * the timers for timeouts.
1842 */
1843 scsi_init_cmd_errh(cmd);
1844
1845 /*
1846 * Dispatch the command to the low-level driver.
1847 */
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001848 cmd->scsi_done = scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 rtn = scsi_dispatch_cmd(cmd);
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001850 if (rtn) {
1851 scsi_queue_insert(cmd, rtn);
1852 spin_lock_irq(q->queue_lock);
Jens Axboea488e742010-04-16 21:13:15 +02001853 goto out_delay;
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001854 }
1855 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857
Bart Van Assche613be1f2014-02-20 14:20:56 -08001858 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001860 host_not_ready:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001861 if (scsi_target(sdev)->can_queue > 0)
1862 atomic_dec(&scsi_target(sdev)->target_busy);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001863 not_ready:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /*
1865 * lock q, handle tag, requeue req, and decrement device_busy. We
1866 * must return with queue_lock held.
1867 *
1868 * Decrementing device_busy without checking it is OK, as all such
1869 * cases (host limits or settings) should run the queue at some
1870 * later time.
1871 */
1872 spin_lock_irq(q->queue_lock);
1873 blk_requeue_request(q, req);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001874 atomic_dec(&sdev->device_busy);
Jens Axboea488e742010-04-16 21:13:15 +02001875out_delay:
Guenter Roeck480cadc2014-08-10 05:54:25 -07001876 if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
Jens Axboea488e742010-04-16 21:13:15 +02001877 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001880static inline blk_status_t prep_to_mq(int ret)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001881{
1882 switch (ret) {
1883 case BLKPREP_OK:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001884 return BLK_STS_OK;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001885 case BLKPREP_DEFER:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001886 return BLK_STS_RESOURCE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001887 default:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001888 return BLK_STS_IOERR;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001889 }
1890}
1891
Bart Van Asschebe4c1862017-06-02 14:21:59 -07001892/* Size in bytes of the sg-list stored in the scsi-mq command-private data. */
1893static unsigned int scsi_mq_sgl_size(struct Scsi_Host *shost)
1894{
1895 return min_t(unsigned int, shost->sg_tablesize, SG_CHUNK_SIZE) *
1896 sizeof(struct scatterlist);
1897}
1898
Christoph Hellwigd2852032014-01-17 12:06:53 +01001899static int scsi_mq_prep_fn(struct request *req)
1900{
1901 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1902 struct scsi_device *sdev = req->q->queuedata;
1903 struct Scsi_Host *shost = sdev->host;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001904 struct scatterlist *sg;
1905
Bart Van Assche08f78432017-06-02 14:22:00 -07001906 scsi_init_command(sdev, cmd);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001907
1908 req->special = cmd;
1909
1910 cmd->request = req;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001911
1912 cmd->tag = req->tag;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001913 cmd->prot_op = SCSI_PROT_NORMAL;
1914
Christoph Hellwigd2852032014-01-17 12:06:53 +01001915 sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
1916 cmd->sdb.table.sgl = sg;
1917
1918 if (scsi_host_get_prot(shost)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001919 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
1920
1921 cmd->prot_sdb->table.sgl =
1922 (struct scatterlist *)(cmd->prot_sdb + 1);
1923 }
1924
1925 if (blk_bidi_rq(req)) {
1926 struct request *next_rq = req->next_rq;
1927 struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq);
1928
1929 memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer));
1930 bidi_sdb->table.sgl =
1931 (struct scatterlist *)(bidi_sdb + 1);
1932
1933 next_rq->special = bidi_sdb;
1934 }
1935
Christoph Hellwigfe052522014-09-22 15:59:31 +02001936 blk_mq_start_request(req);
1937
Bart Van Assche8fe8ffb2017-10-20 11:46:45 -07001938 return scsi_setup_cmnd(sdev, req);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001939}
1940
1941static void scsi_mq_done(struct scsi_cmnd *cmd)
1942{
1943 trace_scsi_dispatch_cmd_done(cmd);
Christoph Hellwig08e00292017-04-20 16:03:09 +02001944 blk_mq_complete_request(cmd->request);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001945}
1946
Ming Lei0df21c82017-10-14 17:22:32 +08001947static void scsi_mq_put_budget(struct blk_mq_hw_ctx *hctx)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001948{
Ming Lei0df21c82017-10-14 17:22:32 +08001949 struct request_queue *q = hctx->queue;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001950 struct scsi_device *sdev = q->queuedata;
Ming Lei0df21c82017-10-14 17:22:32 +08001951
Ming Lei0df21c82017-10-14 17:22:32 +08001952 atomic_dec(&sdev->device_busy);
1953 put_device(&sdev->sdev_gendev);
1954}
1955
Ming Lei88022d72017-11-05 02:21:12 +08001956static bool scsi_mq_get_budget(struct blk_mq_hw_ctx *hctx)
Ming Lei0df21c82017-10-14 17:22:32 +08001957{
1958 struct request_queue *q = hctx->queue;
1959 struct scsi_device *sdev = q->queuedata;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001960
Christoph Hellwigd2852032014-01-17 12:06:53 +01001961 if (!get_device(&sdev->sdev_gendev))
1962 goto out;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001963 if (!scsi_dev_queue_ready(q, sdev))
1964 goto out_put_device;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001965
Ming Lei88022d72017-11-05 02:21:12 +08001966 return true;
Ming Lei0df21c82017-10-14 17:22:32 +08001967
Ming Lei0df21c82017-10-14 17:22:32 +08001968out_put_device:
1969 put_device(&sdev->sdev_gendev);
1970out:
Ming Lei88022d72017-11-05 02:21:12 +08001971 return false;
Ming Lei0df21c82017-10-14 17:22:32 +08001972}
1973
Christoph Hellwigd2852032014-01-17 12:06:53 +01001974static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
1975 const struct blk_mq_queue_data *bd)
1976{
1977 struct request *req = bd->rq;
1978 struct request_queue *q = req->q;
1979 struct scsi_device *sdev = q->queuedata;
1980 struct Scsi_Host *shost = sdev->host;
1981 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1982 blk_status_t ret;
1983 int reason;
1984
1985 ret = prep_to_mq(scsi_prep_state_check(sdev, req));
1986 if (ret != BLK_STS_OK)
Ming Lei0df21c82017-10-14 17:22:32 +08001987 goto out_put_budget;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001988
1989 ret = BLK_STS_RESOURCE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001990 if (!scsi_target_queue_ready(shost, sdev))
Ming Lei826a70a2017-11-04 09:55:34 +08001991 goto out_put_budget;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001992 if (!scsi_host_queue_ready(q, shost, sdev))
1993 goto out_dec_target_busy;
1994
Christoph Hellwige8064022016-10-20 15:12:13 +02001995 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001996 ret = prep_to_mq(scsi_mq_prep_fn(req));
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001997 if (ret != BLK_STS_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001998 goto out_dec_host_busy;
Christoph Hellwige8064022016-10-20 15:12:13 +02001999 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwigfe052522014-09-22 15:59:31 +02002000 } else {
2001 blk_mq_start_request(req);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002002 }
2003
Christoph Hellwig125c99b2014-11-03 12:47:47 +01002004 if (sdev->simple_tags)
2005 cmd->flags |= SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02002006 else
Christoph Hellwig125c99b2014-11-03 12:47:47 +01002007 cmd->flags &= ~SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02002008
Christoph Hellwigd2852032014-01-17 12:06:53 +01002009 scsi_init_cmd_errh(cmd);
2010 cmd->scsi_done = scsi_mq_done;
2011
2012 reason = scsi_dispatch_cmd(cmd);
2013 if (reason) {
2014 scsi_set_blocked(cmd, reason);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002015 ret = BLK_STS_RESOURCE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002016 goto out_dec_host_busy;
2017 }
2018
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002019 return BLK_STS_OK;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002020
2021out_dec_host_busy:
Ming Lei826a70a2017-11-04 09:55:34 +08002022 atomic_dec(&shost->host_busy);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002023out_dec_target_busy:
2024 if (scsi_target(sdev)->can_queue > 0)
2025 atomic_dec(&scsi_target(sdev)->target_busy);
Ming Lei0df21c82017-10-14 17:22:32 +08002026out_put_budget:
2027 scsi_mq_put_budget(hctx);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002028 switch (ret) {
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002029 case BLK_STS_OK:
2030 break;
2031 case BLK_STS_RESOURCE:
Christoph Hellwigd2852032014-01-17 12:06:53 +01002032 if (atomic_read(&sdev->device_busy) == 0 &&
2033 !scsi_device_blocked(sdev))
Bart Van Assche36e3cf22017-04-07 11:16:53 -07002034 blk_mq_delay_run_hw_queue(hctx, SCSI_QUEUE_DELAY);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002035 break;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002036 default:
Christoph Hellwigd2852032014-01-17 12:06:53 +01002037 /*
2038 * Make sure to release all allocated ressources when
2039 * we hit an error, as we will never see this command
2040 * again.
2041 */
Christoph Hellwige8064022016-10-20 15:12:13 +02002042 if (req->rq_flags & RQF_DONTPREP)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002043 scsi_mq_uninit_cmd(cmd);
2044 break;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002045 }
2046 return ret;
2047}
2048
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002049static enum blk_eh_timer_return scsi_timeout(struct request *req,
2050 bool reserved)
2051{
2052 if (reserved)
2053 return BLK_EH_RESET_TIMER;
2054 return scsi_times_out(req);
2055}
2056
Bart Van Asschee7008ff2017-08-25 13:46:31 -07002057static int scsi_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2058 unsigned int hctx_idx, unsigned int numa_node)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002059{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002060 struct Scsi_Host *shost = set->driver_data;
Bart Van Assche8e688252017-06-02 14:21:52 -07002061 const bool unchecked_isa_dma = shost->unchecked_isa_dma;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002062 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
Bart Van Assche08f78432017-06-02 14:22:00 -07002063 struct scatterlist *sg;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002064
Bart Van Assche8e688252017-06-02 14:21:52 -07002065 if (unchecked_isa_dma)
2066 cmd->flags |= SCMD_UNCHECKED_ISA_DMA;
2067 cmd->sense_buffer = scsi_alloc_sense_buffer(unchecked_isa_dma,
2068 GFP_KERNEL, numa_node);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002069 if (!cmd->sense_buffer)
2070 return -ENOMEM;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002071 cmd->req.sense = cmd->sense_buffer;
Bart Van Assche08f78432017-06-02 14:22:00 -07002072
2073 if (scsi_host_get_prot(shost)) {
2074 sg = (void *)cmd + sizeof(struct scsi_cmnd) +
2075 shost->hostt->cmd_size;
2076 cmd->prot_sdb = (void *)sg + scsi_mq_sgl_size(shost);
2077 }
2078
Christoph Hellwigd2852032014-01-17 12:06:53 +01002079 return 0;
2080}
2081
Bart Van Asschee7008ff2017-08-25 13:46:31 -07002082static void scsi_mq_exit_request(struct blk_mq_tag_set *set, struct request *rq,
2083 unsigned int hctx_idx)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002084{
2085 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2086
Bart Van Assche8e688252017-06-02 14:21:52 -07002087 scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA,
2088 cmd->sense_buffer);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002089}
2090
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002091static int scsi_map_queues(struct blk_mq_tag_set *set)
2092{
2093 struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
2094
2095 if (shost->hostt->map_queues)
2096 return shost->hostt->map_queues(shost);
2097 return blk_mq_map_queues(set);
2098}
2099
Christoph Hellwigf1bea552014-04-15 12:26:54 +02002100static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 struct device *host_dev;
2103 u64 bounce_limit = 0xffffffff;
2104
2105 if (shost->unchecked_isa_dma)
2106 return BLK_BOUNCE_ISA;
2107 /*
2108 * Platforms with virtual-DMA translation
2109 * hardware have no practical limit.
2110 */
2111 if (!PCI_DMA_BUS_IS_PHYS)
2112 return BLK_BOUNCE_ANY;
2113
2114 host_dev = scsi_get_device(shost);
2115 if (host_dev && host_dev->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +00002116 bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 return bounce_limit;
2119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Christoph Hellwigd48777a62017-01-02 21:52:10 +03002121void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Lin Ming6f381fa2012-04-12 13:50:38 +08002123 struct device *dev = shost->dma_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Bart Van Assche9efc1602017-05-31 14:43:46 -07002125 queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
2126
Jens Axboea8474ce2007-08-07 09:02:51 +02002127 /*
2128 * this limit is imposed by hardware restrictions
2129 */
Martin K. Petersen8a783622010-02-26 00:20:39 -05002130 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
Ming Lin65e86172016-04-04 14:48:10 -07002131 SG_MAX_SEGMENTS));
Jens Axboea8474ce2007-08-07 09:02:51 +02002132
Martin K. Petersen13f05c82010-09-10 20:50:10 +02002133 if (scsi_host_prot_dma(shost)) {
2134 shost->sg_prot_tablesize =
2135 min_not_zero(shost->sg_prot_tablesize,
2136 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
2137 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
2138 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
2139 }
2140
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002141 blk_queue_max_hw_sectors(q, shost->max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
2143 blk_queue_segment_boundary(q, shost->dma_boundary);
FUJITA Tomonori99c84db2008-02-04 22:28:17 -08002144 dma_set_seg_boundary(dev, shost->dma_boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
FUJITA Tomonori860ac562008-02-04 22:28:05 -08002146 blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (!shost->use_clustering)
Martin K. Petersene692cb62010-12-01 19:41:49 +01002149 q->limits.cluster = 0;
James Bottomley465ff312008-01-01 10:00:10 -06002150
2151 /*
2152 * set a reasonable default alignment on word boundaries: the
2153 * host and device may alter it using
2154 * blk_queue_update_dma_alignment() later.
2155 */
2156 blk_queue_dma_alignment(q, 0x03);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002157}
Christoph Hellwigd48777a62017-01-02 21:52:10 +03002158EXPORT_SYMBOL_GPL(__scsi_init_queue);
James Bottomley465ff312008-01-01 10:00:10 -06002159
Bart Van Asschee7008ff2017-08-25 13:46:31 -07002160static int scsi_old_init_rq(struct request_queue *q, struct request *rq,
2161 gfp_t gfp)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002162{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002163 struct Scsi_Host *shost = q->rq_alloc_data;
Bart Van Assche8e688252017-06-02 14:21:52 -07002164 const bool unchecked_isa_dma = shost->unchecked_isa_dma;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002165 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002166
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002167 memset(cmd, 0, sizeof(*cmd));
2168
Bart Van Assche8e688252017-06-02 14:21:52 -07002169 if (unchecked_isa_dma)
2170 cmd->flags |= SCMD_UNCHECKED_ISA_DMA;
2171 cmd->sense_buffer = scsi_alloc_sense_buffer(unchecked_isa_dma, gfp,
2172 NUMA_NO_NODE);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002173 if (!cmd->sense_buffer)
2174 goto fail;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002175 cmd->req.sense = cmd->sense_buffer;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002176
2177 if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
2178 cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp);
2179 if (!cmd->prot_sdb)
2180 goto fail_free_sense;
2181 }
2182
2183 return 0;
2184
2185fail_free_sense:
Bart Van Assche8e688252017-06-02 14:21:52 -07002186 scsi_free_sense_buffer(unchecked_isa_dma, cmd->sense_buffer);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002187fail:
2188 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002190
Bart Van Asschee7008ff2017-08-25 13:46:31 -07002191static void scsi_old_exit_rq(struct request_queue *q, struct request *rq)
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002192{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002193 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2194
2195 if (cmd->prot_sdb)
2196 kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
Bart Van Assche8e688252017-06-02 14:21:52 -07002197 scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA,
2198 cmd->sense_buffer);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002199}
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002200
Bart Van Asschee7008ff2017-08-25 13:46:31 -07002201struct request_queue *scsi_old_alloc_queue(struct scsi_device *sdev)
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002202{
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002203 struct Scsi_Host *shost = sdev->host;
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002204 struct request_queue *q;
2205
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002206 q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002207 if (!q)
2208 return NULL;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002209 q->cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
2210 q->rq_alloc_data = shost;
2211 q->request_fn = scsi_request_fn;
Bart Van Asschee7008ff2017-08-25 13:46:31 -07002212 q->init_rq_fn = scsi_old_init_rq;
2213 q->exit_rq_fn = scsi_old_exit_rq;
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07002214 q->initialize_rq_fn = scsi_initialize_rq;
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002215
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002216 if (blk_init_allocated_queue(q) < 0) {
2217 blk_cleanup_queue(q);
2218 return NULL;
2219 }
2220
2221 __scsi_init_queue(shost, q);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002222 blk_queue_prep_rq(q, scsi_prep_fn);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02002223 blk_queue_unprep_rq(q, scsi_unprep_fn);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002224 blk_queue_softirq_done(q, scsi_softirq_done);
Jens Axboe242f9dc2008-09-14 05:55:09 -07002225 blk_queue_rq_timed_out(q, scsi_times_out);
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04002226 blk_queue_lld_busy(q, scsi_lld_busy);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002227 return q;
2228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Eric Biggersf363b082017-03-30 13:39:16 -07002230static const struct blk_mq_ops scsi_mq_ops = {
Ming Lei0df21c82017-10-14 17:22:32 +08002231 .get_budget = scsi_mq_get_budget,
2232 .put_budget = scsi_mq_put_budget,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002233 .queue_rq = scsi_queue_rq,
2234 .complete = scsi_softirq_done,
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002235 .timeout = scsi_timeout,
Bart Van Assche0eebd002017-04-26 13:47:57 -07002236#ifdef CONFIG_BLK_DEBUG_FS
2237 .show_rq = scsi_show_rq,
2238#endif
Bart Van Asschee7008ff2017-08-25 13:46:31 -07002239 .init_request = scsi_mq_init_request,
2240 .exit_request = scsi_mq_exit_request,
Bart Van Asscheca18d6f2017-06-20 11:15:41 -07002241 .initialize_rq_fn = scsi_initialize_rq,
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002242 .map_queues = scsi_map_queues,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002243};
2244
2245struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
2246{
2247 sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
2248 if (IS_ERR(sdev->request_queue))
2249 return NULL;
2250
2251 sdev->request_queue->queuedata = sdev;
2252 __scsi_init_queue(sdev->host, sdev->request_queue);
2253 return sdev->request_queue;
2254}
2255
2256int scsi_mq_setup_tags(struct Scsi_Host *shost)
2257{
Bart Van Asschebe4c1862017-06-02 14:21:59 -07002258 unsigned int cmd_size, sgl_size;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002259
Bart Van Asschebe4c1862017-06-02 14:21:59 -07002260 sgl_size = scsi_mq_sgl_size(shost);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002261 cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
2262 if (scsi_host_get_prot(shost))
2263 cmd_size += sizeof(struct scsi_data_buffer) + sgl_size;
2264
2265 memset(&shost->tag_set, 0, sizeof(shost->tag_set));
2266 shost->tag_set.ops = &scsi_mq_ops;
Bart Van Asscheefec4b92014-10-30 14:45:36 +01002267 shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002268 shost->tag_set.queue_depth = shost->can_queue;
2269 shost->tag_set.cmd_size = cmd_size;
2270 shost->tag_set.numa_node = NUMA_NO_NODE;
2271 shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Shaohua Li24391c02015-01-23 14:18:00 -07002272 shost->tag_set.flags |=
2273 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002274 shost->tag_set.driver_data = shost;
2275
2276 return blk_mq_alloc_tag_set(&shost->tag_set);
2277}
2278
2279void scsi_mq_destroy_tags(struct Scsi_Host *shost)
2280{
2281 blk_mq_free_tag_set(&shost->tag_set);
2282}
2283
Hannes Reinecke857de6e2017-02-17 09:02:45 +01002284/**
2285 * scsi_device_from_queue - return sdev associated with a request_queue
2286 * @q: The request queue to return the sdev from
2287 *
2288 * Return the sdev associated with a request queue or NULL if the
2289 * request_queue does not reference a SCSI device.
2290 */
2291struct scsi_device *scsi_device_from_queue(struct request_queue *q)
2292{
2293 struct scsi_device *sdev = NULL;
2294
2295 if (q->mq_ops) {
2296 if (q->mq_ops == &scsi_mq_ops)
2297 sdev = q->queuedata;
2298 } else if (q->request_fn == scsi_request_fn)
2299 sdev = q->queuedata;
2300 if (!sdev || !get_device(&sdev->sdev_gendev))
2301 sdev = NULL;
2302
2303 return sdev;
2304}
2305EXPORT_SYMBOL_GPL(scsi_device_from_queue);
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307/*
2308 * Function: scsi_block_requests()
2309 *
2310 * Purpose: Utility function used by low-level drivers to prevent further
2311 * commands from being queued to the device.
2312 *
2313 * Arguments: shost - Host in question
2314 *
2315 * Returns: Nothing
2316 *
2317 * Lock status: No locks are assumed held.
2318 *
2319 * Notes: There is no timer nor any other means by which the requests
2320 * get unblocked other than the low-level driver calling
2321 * scsi_unblock_requests().
2322 */
2323void scsi_block_requests(struct Scsi_Host *shost)
2324{
2325 shost->host_self_blocked = 1;
2326}
2327EXPORT_SYMBOL(scsi_block_requests);
2328
2329/*
2330 * Function: scsi_unblock_requests()
2331 *
2332 * Purpose: Utility function used by low-level drivers to allow further
2333 * commands from being queued to the device.
2334 *
2335 * Arguments: shost - Host in question
2336 *
2337 * Returns: Nothing
2338 *
2339 * Lock status: No locks are assumed held.
2340 *
2341 * Notes: There is no timer nor any other means by which the requests
2342 * get unblocked other than the low-level driver calling
2343 * scsi_unblock_requests().
2344 *
2345 * This is done as an API function so that changes to the
2346 * internals of the scsi mid-layer won't require wholesale
2347 * changes to drivers that use this feature.
2348 */
2349void scsi_unblock_requests(struct Scsi_Host *shost)
2350{
2351 shost->host_self_blocked = 0;
2352 scsi_run_host_queues(shost);
2353}
2354EXPORT_SYMBOL(scsi_unblock_requests);
2355
2356int __init scsi_init_queue(void)
2357{
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002358 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
2359 sizeof(struct scsi_data_buffer),
2360 0, 0, NULL);
2361 if (!scsi_sdb_cache) {
2362 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
FUJITA Tomonorif0787272008-12-14 01:23:45 +09002363 return -ENOMEM;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02002364 }
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return 0;
2367}
2368
2369void scsi_exit_queue(void)
2370{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002371 kmem_cache_destroy(scsi_sense_cache);
2372 kmem_cache_destroy(scsi_sense_isadma_cache);
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002373 kmem_cache_destroy(scsi_sdb_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
James Bottomley5baba832006-03-18 14:10:35 -06002375
2376/**
2377 * scsi_mode_select - issue a mode select
2378 * @sdev: SCSI device to be queried
2379 * @pf: Page format bit (1 == standard, 0 == vendor specific)
2380 * @sp: Save page bit (0 == don't save, 1 == save)
2381 * @modepage: mode page being requested
2382 * @buffer: request buffer (may not be smaller than eight bytes)
2383 * @len: length of request buffer.
2384 * @timeout: command timeout
2385 * @retries: number of retries before failing
2386 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002387 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley5baba832006-03-18 14:10:35 -06002388 * must be SCSI_SENSE_BUFFERSIZE big.
2389 *
2390 * Returns zero if successful; negative error number or scsi
2391 * status on error
2392 *
2393 */
2394int
2395scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
2396 unsigned char *buffer, int len, int timeout, int retries,
2397 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2398{
2399 unsigned char cmd[10];
2400 unsigned char *real_buffer;
2401 int ret;
2402
2403 memset(cmd, 0, sizeof(cmd));
2404 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2405
2406 if (sdev->use_10_for_ms) {
2407 if (len > 65535)
2408 return -EINVAL;
2409 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2410 if (!real_buffer)
2411 return -ENOMEM;
2412 memcpy(real_buffer + 8, buffer, len);
2413 len += 8;
2414 real_buffer[0] = 0;
2415 real_buffer[1] = 0;
2416 real_buffer[2] = data->medium_type;
2417 real_buffer[3] = data->device_specific;
2418 real_buffer[4] = data->longlba ? 0x01 : 0;
2419 real_buffer[5] = 0;
2420 real_buffer[6] = data->block_descriptor_length >> 8;
2421 real_buffer[7] = data->block_descriptor_length;
2422
2423 cmd[0] = MODE_SELECT_10;
2424 cmd[7] = len >> 8;
2425 cmd[8] = len;
2426 } else {
2427 if (len > 255 || data->block_descriptor_length > 255 ||
2428 data->longlba)
2429 return -EINVAL;
2430
2431 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2432 if (!real_buffer)
2433 return -ENOMEM;
2434 memcpy(real_buffer + 4, buffer, len);
2435 len += 4;
2436 real_buffer[0] = 0;
2437 real_buffer[1] = data->medium_type;
2438 real_buffer[2] = data->device_specific;
2439 real_buffer[3] = data->block_descriptor_length;
2440
2441
2442 cmd[0] = MODE_SELECT;
2443 cmd[4] = len;
2444 }
2445
2446 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002447 sshdr, timeout, retries, NULL);
James Bottomley5baba832006-03-18 14:10:35 -06002448 kfree(real_buffer);
2449 return ret;
2450}
2451EXPORT_SYMBOL_GPL(scsi_mode_select);
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453/**
Rob Landleyeb448202007-11-03 13:30:39 -05002454 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
James Bottomley1cf72692005-08-28 11:27:01 -05002455 * @sdev: SCSI device to be queried
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 * @dbd: set if mode sense will allow block descriptors to be returned
2457 * @modepage: mode page being requested
2458 * @buffer: request buffer (may not be smaller than eight bytes)
2459 * @len: length of request buffer.
2460 * @timeout: command timeout
2461 * @retries: number of retries before failing
2462 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002463 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley1cf72692005-08-28 11:27:01 -05002464 * must be SCSI_SENSE_BUFFERSIZE big.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 *
2466 * Returns zero if unsuccessful, or the header offset (either 4
2467 * or 8 depending on whether a six or ten byte command was
2468 * issued) if successful.
Rob Landleyeb448202007-11-03 13:30:39 -05002469 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470int
James Bottomley1cf72692005-08-28 11:27:01 -05002471scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 unsigned char *buffer, int len, int timeout, int retries,
James Bottomley5baba832006-03-18 14:10:35 -06002473 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 unsigned char cmd[12];
2476 int use_10_for_ms;
2477 int header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002478 int result, retry_count = retries;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002479 struct scsi_sense_hdr my_sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 memset(data, 0, sizeof(*data));
2482 memset(&cmd[0], 0, 12);
2483 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
2484 cmd[2] = modepage;
2485
James Bottomleyea73a9f2005-08-28 11:33:52 -05002486 /* caller might not be interested in sense, but we need it */
2487 if (!sshdr)
2488 sshdr = &my_sshdr;
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 retry:
James Bottomley1cf72692005-08-28 11:27:01 -05002491 use_10_for_ms = sdev->use_10_for_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 if (use_10_for_ms) {
2494 if (len < 8)
2495 len = 8;
2496
2497 cmd[0] = MODE_SENSE_10;
2498 cmd[8] = len;
2499 header_length = 8;
2500 } else {
2501 if (len < 4)
2502 len = 4;
2503
2504 cmd[0] = MODE_SENSE;
2505 cmd[4] = len;
2506 header_length = 4;
2507 }
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 memset(buffer, 0, len);
2510
James Bottomley1cf72692005-08-28 11:27:01 -05002511 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002512 sshdr, timeout, retries, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 /* This code looks awful: what it's doing is making sure an
2515 * ILLEGAL REQUEST sense return identifies the actual command
2516 * byte as the problem. MODE_SENSE commands can return
2517 * ILLEGAL REQUEST if the code page isn't supported */
2518
James Bottomley1cf72692005-08-28 11:27:01 -05002519 if (use_10_for_ms && !scsi_status_is_good(result) &&
2520 (driver_byte(result) & DRIVER_SENSE)) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05002521 if (scsi_sense_valid(sshdr)) {
2522 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2523 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 /*
2525 * Invalid command operation code
2526 */
James Bottomley1cf72692005-08-28 11:27:01 -05002527 sdev->use_10_for_ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 goto retry;
2529 }
2530 }
2531 }
2532
James Bottomley1cf72692005-08-28 11:27:01 -05002533 if(scsi_status_is_good(result)) {
Al Viro6d73c852006-02-23 02:03:16 +01002534 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2535 (modepage == 6 || modepage == 8))) {
2536 /* Initio breakage? */
2537 header_length = 0;
2538 data->length = 13;
2539 data->medium_type = 0;
2540 data->device_specific = 0;
2541 data->longlba = 0;
2542 data->block_descriptor_length = 0;
2543 } else if(use_10_for_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 data->length = buffer[0]*256 + buffer[1] + 2;
2545 data->medium_type = buffer[2];
2546 data->device_specific = buffer[3];
2547 data->longlba = buffer[4] & 0x01;
2548 data->block_descriptor_length = buffer[6]*256
2549 + buffer[7];
2550 } else {
2551 data->length = buffer[0] + 1;
2552 data->medium_type = buffer[1];
2553 data->device_specific = buffer[2];
2554 data->block_descriptor_length = buffer[3];
2555 }
Al Viro6d73c852006-02-23 02:03:16 +01002556 data->header_length = header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002557 } else if ((status_byte(result) == CHECK_CONDITION) &&
2558 scsi_sense_valid(sshdr) &&
2559 sshdr->sense_key == UNIT_ATTENTION && retry_count) {
2560 retry_count--;
2561 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
2563
James Bottomley1cf72692005-08-28 11:27:01 -05002564 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566EXPORT_SYMBOL(scsi_mode_sense);
2567
James Bottomley001aac22007-12-02 19:10:40 +02002568/**
2569 * scsi_test_unit_ready - test if unit is ready
2570 * @sdev: scsi device to change the state of.
2571 * @timeout: command timeout
2572 * @retries: number of retries before failing
Christoph Hellwig74a78eb2017-02-14 20:15:57 +01002573 * @sshdr: outpout pointer for decoded sense information.
James Bottomley001aac22007-12-02 19:10:40 +02002574 *
2575 * Returns zero if unsuccessful or an error if TUR failed. For
Tejun Heo9f8a2c22010-12-08 20:57:40 +01002576 * removable media, UNIT_ATTENTION sets ->changed flag.
James Bottomley001aac22007-12-02 19:10:40 +02002577 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578int
James Bottomley001aac22007-12-02 19:10:40 +02002579scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
Christoph Hellwig74a78eb2017-02-14 20:15:57 +01002580 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 char cmd[] = {
2583 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2584 };
2585 int result;
James Bottomley001aac22007-12-02 19:10:40 +02002586
James Bottomley001aac22007-12-02 19:10:40 +02002587 /* try to eat the UNIT_ATTENTION if there are enough retries */
2588 do {
2589 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002590 timeout, retries, NULL);
James Bottomley32c356d2008-08-20 00:58:13 +02002591 if (sdev->removable && scsi_sense_valid(sshdr) &&
2592 sshdr->sense_key == UNIT_ATTENTION)
2593 sdev->changed = 1;
2594 } while (scsi_sense_valid(sshdr) &&
2595 sshdr->sense_key == UNIT_ATTENTION && --retries);
James Bottomley001aac22007-12-02 19:10:40 +02002596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return result;
2598}
2599EXPORT_SYMBOL(scsi_test_unit_ready);
2600
2601/**
Rob Landleyeb448202007-11-03 13:30:39 -05002602 * scsi_device_set_state - Take the given device through the device state model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 * @sdev: scsi device to change the state of.
2604 * @state: state to change to.
2605 *
Hannes Reinecke23cb27f2017-08-25 13:56:56 +02002606 * Returns zero if successful or an error if the requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 * transition is illegal.
Rob Landleyeb448202007-11-03 13:30:39 -05002608 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609int
2610scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2611{
2612 enum scsi_device_state oldstate = sdev->sdev_state;
2613
2614 if (state == oldstate)
2615 return 0;
2616
2617 switch (state) {
2618 case SDEV_CREATED:
James Bottomley6f4267e2008-08-22 16:53:31 -05002619 switch (oldstate) {
2620 case SDEV_CREATED_BLOCK:
2621 break;
2622 default:
2623 goto illegal;
2624 }
2625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 case SDEV_RUNNING:
2628 switch (oldstate) {
2629 case SDEV_CREATED:
2630 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002631 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 case SDEV_QUIESCE:
2633 case SDEV_BLOCK:
2634 break;
2635 default:
2636 goto illegal;
2637 }
2638 break;
2639
2640 case SDEV_QUIESCE:
2641 switch (oldstate) {
2642 case SDEV_RUNNING:
2643 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002644 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 break;
2646 default:
2647 goto illegal;
2648 }
2649 break;
2650
2651 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002652 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 switch (oldstate) {
2654 case SDEV_CREATED:
2655 case SDEV_RUNNING:
2656 case SDEV_QUIESCE:
2657 case SDEV_BLOCK:
2658 break;
2659 default:
2660 goto illegal;
2661 }
2662 break;
2663
2664 case SDEV_BLOCK:
2665 switch (oldstate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 case SDEV_RUNNING:
James Bottomley6f4267e2008-08-22 16:53:31 -05002667 case SDEV_CREATED_BLOCK:
2668 break;
2669 default:
2670 goto illegal;
2671 }
2672 break;
2673
2674 case SDEV_CREATED_BLOCK:
2675 switch (oldstate) {
2676 case SDEV_CREATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 break;
2678 default:
2679 goto illegal;
2680 }
2681 break;
2682
2683 case SDEV_CANCEL:
2684 switch (oldstate) {
2685 case SDEV_CREATED:
2686 case SDEV_RUNNING:
Alan Stern9ea72902006-06-23 14:25:34 -04002687 case SDEV_QUIESCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002689 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 break;
2691 default:
2692 goto illegal;
2693 }
2694 break;
2695
2696 case SDEV_DEL:
2697 switch (oldstate) {
Brian King309bd272006-06-27 11:10:43 -05002698 case SDEV_CREATED:
2699 case SDEV_RUNNING:
2700 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002701 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 case SDEV_CANCEL:
Bart Van Assche255ee932017-06-02 14:21:57 -07002703 case SDEV_BLOCK:
Bart Van Assche0516c082013-07-02 15:06:33 +02002704 case SDEV_CREATED_BLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 break;
2706 default:
2707 goto illegal;
2708 }
2709 break;
2710
2711 }
2712 sdev->sdev_state = state;
2713 return 0;
2714
2715 illegal:
Hannes Reinecke91921e02014-06-25 16:39:59 +02002716 SCSI_LOG_ERROR_RECOVERY(1,
James Bottomley9ccfc752005-10-02 11:45:08 -05002717 sdev_printk(KERN_ERR, sdev,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002718 "Illegal state transition %s->%s",
James Bottomley9ccfc752005-10-02 11:45:08 -05002719 scsi_device_state_name(oldstate),
2720 scsi_device_state_name(state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 );
2722 return -EINVAL;
2723}
2724EXPORT_SYMBOL(scsi_device_set_state);
2725
2726/**
Jeff Garzika341cd02007-10-29 17:15:22 -04002727 * sdev_evt_emit - emit a single SCSI device uevent
2728 * @sdev: associated SCSI device
2729 * @evt: event to emit
2730 *
2731 * Send a single uevent (scsi_event) to the associated scsi_device.
2732 */
2733static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2734{
2735 int idx = 0;
2736 char *envp[3];
2737
2738 switch (evt->evt_type) {
2739 case SDEV_EVT_MEDIA_CHANGE:
2740 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2741 break;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002742 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
Hannes Reinecked3d32892016-02-19 09:17:16 +01002743 scsi_rescan_device(&sdev->sdev_gendev);
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002744 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2745 break;
2746 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2747 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2748 break;
2749 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2750 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2751 break;
2752 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2753 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2754 break;
2755 case SDEV_EVT_LUN_CHANGE_REPORTED:
2756 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2757 break;
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002758 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2759 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
2760 break;
Hannes Reineckecf3431b2017-10-17 09:11:24 +02002761 case SDEV_EVT_POWER_ON_RESET_OCCURRED:
2762 envp[idx++] = "SDEV_UA=POWER_ON_RESET_OCCURRED";
2763 break;
Jeff Garzika341cd02007-10-29 17:15:22 -04002764 default:
2765 /* do nothing */
2766 break;
2767 }
2768
2769 envp[idx++] = NULL;
2770
2771 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2772}
2773
2774/**
2775 * sdev_evt_thread - send a uevent for each scsi event
2776 * @work: work struct for scsi_device
2777 *
2778 * Dispatch queued events to their associated scsi_device kobjects
2779 * as uevents.
2780 */
2781void scsi_evt_thread(struct work_struct *work)
2782{
2783 struct scsi_device *sdev;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002784 enum scsi_device_event evt_type;
Jeff Garzika341cd02007-10-29 17:15:22 -04002785 LIST_HEAD(event_list);
2786
2787 sdev = container_of(work, struct scsi_device, event_work);
2788
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002789 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2790 if (test_and_clear_bit(evt_type, sdev->pending_events))
2791 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2792
Jeff Garzika341cd02007-10-29 17:15:22 -04002793 while (1) {
2794 struct scsi_event *evt;
2795 struct list_head *this, *tmp;
2796 unsigned long flags;
2797
2798 spin_lock_irqsave(&sdev->list_lock, flags);
2799 list_splice_init(&sdev->event_list, &event_list);
2800 spin_unlock_irqrestore(&sdev->list_lock, flags);
2801
2802 if (list_empty(&event_list))
2803 break;
2804
2805 list_for_each_safe(this, tmp, &event_list) {
2806 evt = list_entry(this, struct scsi_event, node);
2807 list_del(&evt->node);
2808 scsi_evt_emit(sdev, evt);
2809 kfree(evt);
2810 }
2811 }
2812}
2813
2814/**
2815 * sdev_evt_send - send asserted event to uevent thread
2816 * @sdev: scsi_device event occurred on
2817 * @evt: event to send
2818 *
2819 * Assert scsi device event asynchronously.
2820 */
2821void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2822{
2823 unsigned long flags;
2824
Kay Sievers4d1566e2008-03-19 13:04:47 +01002825#if 0
2826 /* FIXME: currently this check eliminates all media change events
2827 * for polled devices. Need to update to discriminate between AN
2828 * and polled events */
Jeff Garzika341cd02007-10-29 17:15:22 -04002829 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2830 kfree(evt);
2831 return;
2832 }
Kay Sievers4d1566e2008-03-19 13:04:47 +01002833#endif
Jeff Garzika341cd02007-10-29 17:15:22 -04002834
2835 spin_lock_irqsave(&sdev->list_lock, flags);
2836 list_add_tail(&evt->node, &sdev->event_list);
2837 schedule_work(&sdev->event_work);
2838 spin_unlock_irqrestore(&sdev->list_lock, flags);
2839}
2840EXPORT_SYMBOL_GPL(sdev_evt_send);
2841
2842/**
2843 * sdev_evt_alloc - allocate a new scsi event
2844 * @evt_type: type of event to allocate
2845 * @gfpflags: GFP flags for allocation
2846 *
2847 * Allocates and returns a new scsi_event.
2848 */
2849struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2850 gfp_t gfpflags)
2851{
2852 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2853 if (!evt)
2854 return NULL;
2855
2856 evt->evt_type = evt_type;
2857 INIT_LIST_HEAD(&evt->node);
2858
2859 /* evt_type-specific initialization, if any */
2860 switch (evt_type) {
2861 case SDEV_EVT_MEDIA_CHANGE:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002862 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2863 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2864 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2865 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2866 case SDEV_EVT_LUN_CHANGE_REPORTED:
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002867 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
Hannes Reineckecf3431b2017-10-17 09:11:24 +02002868 case SDEV_EVT_POWER_ON_RESET_OCCURRED:
Jeff Garzika341cd02007-10-29 17:15:22 -04002869 default:
2870 /* do nothing */
2871 break;
2872 }
2873
2874 return evt;
2875}
2876EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2877
2878/**
2879 * sdev_evt_send_simple - send asserted event to uevent thread
2880 * @sdev: scsi_device event occurred on
2881 * @evt_type: type of event to send
2882 * @gfpflags: GFP flags for allocation
2883 *
2884 * Assert scsi device event asynchronously, given an event type.
2885 */
2886void sdev_evt_send_simple(struct scsi_device *sdev,
2887 enum scsi_device_event evt_type, gfp_t gfpflags)
2888{
2889 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2890 if (!evt) {
2891 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2892 evt_type);
2893 return;
2894 }
2895
2896 sdev_evt_send(sdev, evt);
2897}
2898EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2899
2900/**
Bart Van Assche669f0442016-11-22 16:17:13 -08002901 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
2902 * @sdev: SCSI device to count the number of scsi_request_fn() callers for.
2903 */
2904static int scsi_request_fn_active(struct scsi_device *sdev)
2905{
2906 struct request_queue *q = sdev->request_queue;
2907 int request_fn_active;
2908
2909 WARN_ON_ONCE(sdev->host->use_blk_mq);
2910
2911 spin_lock_irq(q->queue_lock);
2912 request_fn_active = q->request_fn_active;
2913 spin_unlock_irq(q->queue_lock);
2914
2915 return request_fn_active;
2916}
2917
2918/**
2919 * scsi_wait_for_queuecommand() - wait for ongoing queuecommand() calls
2920 * @sdev: SCSI device pointer.
2921 *
2922 * Wait until the ongoing shost->hostt->queuecommand() calls that are
2923 * invoked from scsi_request_fn() have finished.
2924 */
2925static void scsi_wait_for_queuecommand(struct scsi_device *sdev)
2926{
2927 WARN_ON_ONCE(sdev->host->use_blk_mq);
2928
2929 while (scsi_request_fn_active(sdev))
2930 msleep(20);
2931}
2932
2933/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 * scsi_device_quiesce - Block user issued commands.
2935 * @sdev: scsi device to quiesce.
2936 *
2937 * This works by trying to transition to the SDEV_QUIESCE state
2938 * (which must be a legal transition). When the device is in this
2939 * state, only special requests will be accepted, all others will
2940 * be deferred. Since special requests may also be requeued requests,
2941 * a successful return doesn't guarantee the device will be
2942 * totally quiescent.
2943 *
2944 * Must be called with user context, may sleep.
2945 *
2946 * Returns zero if unsuccessful or an error if not.
Rob Landleyeb448202007-11-03 13:30:39 -05002947 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948int
2949scsi_device_quiesce(struct scsi_device *sdev)
2950{
Bart Van Assche3a0a5292017-11-09 10:49:58 -08002951 struct request_queue *q = sdev->request_queue;
Bart Van Assche0db6ca82017-06-02 14:21:55 -07002952 int err;
2953
Bart Van Assche3a0a5292017-11-09 10:49:58 -08002954 /*
2955 * It is allowed to call scsi_device_quiesce() multiple times from
2956 * the same context but concurrent scsi_device_quiesce() calls are
2957 * not allowed.
2958 */
2959 WARN_ON_ONCE(sdev->quiesced_by && sdev->quiesced_by != current);
2960
2961 blk_set_preempt_only(q);
2962
2963 blk_mq_freeze_queue(q);
2964 /*
2965 * Ensure that the effect of blk_set_preempt_only() will be visible
2966 * for percpu_ref_tryget() callers that occur after the queue
2967 * unfreeze even if the queue was already frozen before this function
2968 * was called. See also https://lwn.net/Articles/573497/.
2969 */
2970 synchronize_rcu();
2971 blk_mq_unfreeze_queue(q);
2972
Bart Van Assche0db6ca82017-06-02 14:21:55 -07002973 mutex_lock(&sdev->state_mutex);
2974 err = scsi_device_set_state(sdev, SDEV_QUIESCE);
Bart Van Assche3a0a5292017-11-09 10:49:58 -08002975 if (err == 0)
2976 sdev->quiesced_by = current;
2977 else
2978 blk_clear_preempt_only(q);
Bart Van Assche0db6ca82017-06-02 14:21:55 -07002979 mutex_unlock(&sdev->state_mutex);
2980
Bart Van Assche3a0a5292017-11-09 10:49:58 -08002981 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983EXPORT_SYMBOL(scsi_device_quiesce);
2984
2985/**
2986 * scsi_device_resume - Restart user issued commands to a quiesced device.
2987 * @sdev: scsi device to resume.
2988 *
2989 * Moves the device from quiesced back to running and restarts the
2990 * queues.
2991 *
2992 * Must be called with user context, may sleep.
Rob Landleyeb448202007-11-03 13:30:39 -05002993 */
Dan Williamsa7a20d12012-03-22 17:05:11 -07002994void scsi_device_resume(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Dan Williamsa7a20d12012-03-22 17:05:11 -07002996 /* check if the device state was mutated prior to resume, and if
2997 * so assume the state is being managed elsewhere (for example
2998 * device deleted during suspend)
2999 */
Bart Van Assche0db6ca82017-06-02 14:21:55 -07003000 mutex_lock(&sdev->state_mutex);
Bart Van Assche3a0a5292017-11-09 10:49:58 -08003001 WARN_ON_ONCE(!sdev->quiesced_by);
3002 sdev->quiesced_by = NULL;
3003 blk_clear_preempt_only(sdev->request_queue);
3004 if (sdev->sdev_state == SDEV_QUIESCE)
3005 scsi_device_set_state(sdev, SDEV_RUNNING);
Bart Van Assche0db6ca82017-06-02 14:21:55 -07003006 mutex_unlock(&sdev->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008EXPORT_SYMBOL(scsi_device_resume);
3009
3010static void
3011device_quiesce_fn(struct scsi_device *sdev, void *data)
3012{
3013 scsi_device_quiesce(sdev);
3014}
3015
3016void
3017scsi_target_quiesce(struct scsi_target *starget)
3018{
3019 starget_for_each_device(starget, NULL, device_quiesce_fn);
3020}
3021EXPORT_SYMBOL(scsi_target_quiesce);
3022
3023static void
3024device_resume_fn(struct scsi_device *sdev, void *data)
3025{
3026 scsi_device_resume(sdev);
3027}
3028
3029void
3030scsi_target_resume(struct scsi_target *starget)
3031{
3032 starget_for_each_device(starget, NULL, device_resume_fn);
3033}
3034EXPORT_SYMBOL(scsi_target_resume);
3035
3036/**
Bart Van Assche551eb592017-06-02 14:21:53 -07003037 * scsi_internal_device_block_nowait - try to transition to the SDEV_BLOCK state
3038 * @sdev: device to block
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 *
Bart Van Assche551eb592017-06-02 14:21:53 -07003040 * Pause SCSI command processing on the specified device. Does not sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 *
Bart Van Assche551eb592017-06-02 14:21:53 -07003042 * Returns zero if successful or a negative error code upon failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 *
Bart Van Assche551eb592017-06-02 14:21:53 -07003044 * Notes:
3045 * This routine transitions the device to the SDEV_BLOCK state (which must be
3046 * a legal transition). When the device is in this state, command processing
3047 * is paused until the device leaves the SDEV_BLOCK state. See also
3048 * scsi_internal_device_unblock_nowait().
Rob Landleyeb448202007-11-03 13:30:39 -05003049 */
Bart Van Assche551eb592017-06-02 14:21:53 -07003050int scsi_internal_device_block_nowait(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Jens Axboe165125e2007-07-24 09:28:11 +02003052 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 unsigned long flags;
3054 int err = 0;
3055
3056 err = scsi_device_set_state(sdev, SDEV_BLOCK);
James Bottomley6f4267e2008-08-22 16:53:31 -05003057 if (err) {
3058 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
3059
3060 if (err)
3061 return err;
3062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 /*
3065 * The device has transitioned to SDEV_BLOCK. Stop the
3066 * block layer from calling the midlayer with this device's
3067 * request queue.
3068 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01003069 if (q->mq_ops) {
Linus Torvalds90311142017-07-06 12:10:33 -07003070 blk_mq_quiesce_queue_nowait(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +01003071 } else {
3072 spin_lock_irqsave(q->queue_lock, flags);
3073 blk_stop_queue(q);
3074 spin_unlock_irqrestore(q->queue_lock, flags);
3075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 return 0;
3078}
Bart Van Assche551eb592017-06-02 14:21:53 -07003079EXPORT_SYMBOL_GPL(scsi_internal_device_block_nowait);
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081/**
Bart Van Assche551eb592017-06-02 14:21:53 -07003082 * scsi_internal_device_block - try to transition to the SDEV_BLOCK state
3083 * @sdev: device to block
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 *
Bart Van Assche551eb592017-06-02 14:21:53 -07003085 * Pause SCSI command processing on the specified device and wait until all
3086 * ongoing scsi_request_fn() / scsi_queue_rq() calls have finished. May sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 *
Bart Van Assche551eb592017-06-02 14:21:53 -07003088 * Returns zero if successful or a negative error code upon failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 *
Bart Van Assche551eb592017-06-02 14:21:53 -07003090 * Note:
3091 * This routine transitions the device to the SDEV_BLOCK state (which must be
3092 * a legal transition). When the device is in this state, command processing
3093 * is paused until the device leaves the SDEV_BLOCK state. See also
3094 * scsi_internal_device_unblock().
3095 *
3096 * To do: avoid that scsi_send_eh_cmnd() calls queuecommand() after
3097 * scsi_internal_device_block() has blocked a SCSI device and also
3098 * remove the rport mutex lock and unlock calls from srp_queuecommand().
Rob Landleyeb448202007-11-03 13:30:39 -05003099 */
Bart Van Assche551eb592017-06-02 14:21:53 -07003100static int scsi_internal_device_block(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
Bart Van Assche551eb592017-06-02 14:21:53 -07003102 struct request_queue *q = sdev->request_queue;
3103 int err;
3104
Bart Van Assche0db6ca82017-06-02 14:21:55 -07003105 mutex_lock(&sdev->state_mutex);
Bart Van Assche551eb592017-06-02 14:21:53 -07003106 err = scsi_internal_device_block_nowait(sdev);
3107 if (err == 0) {
3108 if (q->mq_ops)
3109 blk_mq_quiesce_queue(q);
3110 else
3111 scsi_wait_for_queuecommand(sdev);
3112 }
Bart Van Assche0db6ca82017-06-02 14:21:55 -07003113 mutex_unlock(&sdev->state_mutex);
3114
Bart Van Assche551eb592017-06-02 14:21:53 -07003115 return err;
3116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Bart Van Assche66483a42017-06-02 14:21:56 -07003118void scsi_start_queue(struct scsi_device *sdev)
3119{
3120 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 unsigned long flags;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003122
Bart Van Assche66483a42017-06-02 14:21:56 -07003123 if (q->mq_ops) {
Linus Torvalds90311142017-07-06 12:10:33 -07003124 blk_mq_unquiesce_queue(q);
Bart Van Assche66483a42017-06-02 14:21:56 -07003125 } else {
3126 spin_lock_irqsave(q->queue_lock, flags);
3127 blk_start_queue(q);
3128 spin_unlock_irqrestore(q->queue_lock, flags);
3129 }
3130}
3131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132/**
Bart Van Assche43f75712017-06-02 14:21:54 -07003133 * scsi_internal_device_unblock_nowait - resume a device after a block request
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 * @sdev: device to resume
Bart Van Assche43f75712017-06-02 14:21:54 -07003135 * @new_state: state to set the device to after unblocking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 *
Bart Van Assche43f75712017-06-02 14:21:54 -07003137 * Restart the device queue for a previously suspended SCSI device. Does not
3138 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 *
Bart Van Assche43f75712017-06-02 14:21:54 -07003140 * Returns zero if successful or a negative error code upon failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 *
Bart Van Assche43f75712017-06-02 14:21:54 -07003142 * Notes:
3143 * This routine transitions the device to the SDEV_RUNNING state or to one of
3144 * the offline states (which must be a legal transition) allowing the midlayer
3145 * to goose the queue for this device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 */
Bart Van Assche43f75712017-06-02 14:21:54 -07003147int scsi_internal_device_unblock_nowait(struct scsi_device *sdev,
3148 enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003150 /*
3151 * Try to transition the scsi device to SDEV_RUNNING or one of the
3152 * offlined states and goose the device queue if successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 */
Hannes Reinecke8cd1ec72017-08-11 08:53:46 +02003154 switch (sdev->sdev_state) {
3155 case SDEV_BLOCK:
3156 case SDEV_TRANSPORT_OFFLINE:
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003157 sdev->sdev_state = new_state;
Hannes Reinecke8cd1ec72017-08-11 08:53:46 +02003158 break;
3159 case SDEV_CREATED_BLOCK:
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003160 if (new_state == SDEV_TRANSPORT_OFFLINE ||
3161 new_state == SDEV_OFFLINE)
3162 sdev->sdev_state = new_state;
3163 else
3164 sdev->sdev_state = SDEV_CREATED;
Hannes Reinecke8cd1ec72017-08-11 08:53:46 +02003165 break;
3166 case SDEV_CANCEL:
3167 case SDEV_OFFLINE:
3168 break;
3169 default:
Takahiro Yasui5c10e632009-04-29 12:13:02 -04003170 return -EINVAL;
Hannes Reinecke8cd1ec72017-08-11 08:53:46 +02003171 }
Bart Van Assche66483a42017-06-02 14:21:56 -07003172 scsi_start_queue(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
3174 return 0;
3175}
Bart Van Assche43f75712017-06-02 14:21:54 -07003176EXPORT_SYMBOL_GPL(scsi_internal_device_unblock_nowait);
3177
3178/**
3179 * scsi_internal_device_unblock - resume a device after a block request
3180 * @sdev: device to resume
3181 * @new_state: state to set the device to after unblocking
3182 *
3183 * Restart the device queue for a previously suspended SCSI device. May sleep.
3184 *
3185 * Returns zero if successful or a negative error code upon failure.
3186 *
3187 * Notes:
3188 * This routine transitions the device to the SDEV_RUNNING state or to one of
3189 * the offline states (which must be a legal transition) allowing the midlayer
3190 * to goose the queue for this device.
3191 */
3192static int scsi_internal_device_unblock(struct scsi_device *sdev,
3193 enum scsi_device_state new_state)
3194{
Bart Van Assche0db6ca82017-06-02 14:21:55 -07003195 int ret;
3196
3197 mutex_lock(&sdev->state_mutex);
3198 ret = scsi_internal_device_unblock_nowait(sdev, new_state);
3199 mutex_unlock(&sdev->state_mutex);
3200
3201 return ret;
Bart Van Assche43f75712017-06-02 14:21:54 -07003202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
3204static void
3205device_block(struct scsi_device *sdev, void *data)
3206{
Bart Van Assche551eb592017-06-02 14:21:53 -07003207 scsi_internal_device_block(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
3209
3210static int
3211target_block(struct device *dev, void *data)
3212{
3213 if (scsi_is_target_device(dev))
3214 starget_for_each_device(to_scsi_target(dev), NULL,
3215 device_block);
3216 return 0;
3217}
3218
3219void
3220scsi_target_block(struct device *dev)
3221{
3222 if (scsi_is_target_device(dev))
3223 starget_for_each_device(to_scsi_target(dev), NULL,
3224 device_block);
3225 else
3226 device_for_each_child(dev, NULL, target_block);
3227}
3228EXPORT_SYMBOL_GPL(scsi_target_block);
3229
3230static void
3231device_unblock(struct scsi_device *sdev, void *data)
3232{
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003233 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
3236static int
3237target_unblock(struct device *dev, void *data)
3238{
3239 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003240 starget_for_each_device(to_scsi_target(dev), data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 device_unblock);
3242 return 0;
3243}
3244
3245void
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003246scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
3248 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003249 starget_for_each_device(to_scsi_target(dev), &new_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 device_unblock);
3251 else
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003252 device_for_each_child(dev, &new_state, target_unblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254EXPORT_SYMBOL_GPL(scsi_target_unblock);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003255
3256/**
3257 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
Rob Landleyeb448202007-11-03 13:30:39 -05003258 * @sgl: scatter-gather list
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003259 * @sg_count: number of segments in sg
3260 * @offset: offset in bytes into sg, on return offset into the mapped area
3261 * @len: bytes to map, on return number of bytes mapped
3262 *
3263 * Returns virtual address of the start of the mapped page
3264 */
Jens Axboec6132da2007-10-16 11:08:49 +02003265void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003266 size_t *offset, size_t *len)
3267{
3268 int i;
3269 size_t sg_len = 0, len_complete = 0;
Jens Axboec6132da2007-10-16 11:08:49 +02003270 struct scatterlist *sg;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003271 struct page *page;
3272
Andrew Morton22cfefb2007-02-05 16:39:03 -08003273 WARN_ON(!irqs_disabled());
3274
Jens Axboec6132da2007-10-16 11:08:49 +02003275 for_each_sg(sgl, sg, sg_count, i) {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003276 len_complete = sg_len; /* Complete sg-entries */
Jens Axboec6132da2007-10-16 11:08:49 +02003277 sg_len += sg->length;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003278 if (sg_len > *offset)
3279 break;
3280 }
3281
3282 if (unlikely(i == sg_count)) {
Andrew Morton169e1a22006-04-18 21:09:08 -07003283 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
3284 "elements %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003285 __func__, sg_len, *offset, sg_count);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003286 WARN_ON(1);
3287 return NULL;
3288 }
3289
3290 /* Offset starting from the beginning of first page in this sg-entry */
Jens Axboec6132da2007-10-16 11:08:49 +02003291 *offset = *offset - len_complete + sg->offset;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003292
3293 /* Assumption: contiguous pages can be accessed as "page + i" */
Jens Axboe45711f12007-10-22 21:19:53 +02003294 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003295 *offset &= ~PAGE_MASK;
3296
3297 /* Bytes in this sg-entry from *offset to the end of the page */
3298 sg_len = PAGE_SIZE - *offset;
3299 if (*len > sg_len)
3300 *len = sg_len;
3301
Cong Wang77dfce02011-11-25 23:14:23 +08003302 return kmap_atomic(page);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003303}
3304EXPORT_SYMBOL(scsi_kmap_atomic_sg);
3305
3306/**
Rob Landleyeb448202007-11-03 13:30:39 -05003307 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003308 * @virt: virtual address to be unmapped
3309 */
3310void scsi_kunmap_atomic_sg(void *virt)
3311{
Cong Wang77dfce02011-11-25 23:14:23 +08003312 kunmap_atomic(virt);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003313}
3314EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
Aaron Lu6f4c8272013-01-23 15:09:32 +08003315
3316void sdev_disable_disk_events(struct scsi_device *sdev)
3317{
3318 atomic_inc(&sdev->disk_events_disable_depth);
3319}
3320EXPORT_SYMBOL(sdev_disable_disk_events);
3321
3322void sdev_enable_disk_events(struct scsi_device *sdev)
3323{
3324 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
3325 return;
3326 atomic_dec(&sdev->disk_events_disable_depth);
3327}
3328EXPORT_SYMBOL(sdev_enable_disk_events);
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003329
3330/**
3331 * scsi_vpd_lun_id - return a unique device identification
3332 * @sdev: SCSI device
3333 * @id: buffer for the identification
3334 * @id_len: length of the buffer
3335 *
3336 * Copies a unique device identification into @id based
3337 * on the information in the VPD page 0x83 of the device.
3338 * The string will be formatted as a SCSI name string.
3339 *
3340 * Returns the length of the identification or error on failure.
3341 * If the identifier is longer than the supplied buffer the actual
3342 * identifier length is returned and the buffer is not zero-padded.
3343 */
3344int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
3345{
3346 u8 cur_id_type = 0xff;
3347 u8 cur_id_size = 0;
Bart Van Asscheccf1e002017-08-29 08:50:13 -07003348 const unsigned char *d, *cur_id_str;
3349 const struct scsi_vpd *vpd_pg83;
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003350 int id_size = -EINVAL;
3351
3352 rcu_read_lock();
3353 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3354 if (!vpd_pg83) {
3355 rcu_read_unlock();
3356 return -ENXIO;
3357 }
3358
3359 /*
3360 * Look for the correct descriptor.
3361 * Order of preference for lun descriptor:
3362 * - SCSI name string
3363 * - NAA IEEE Registered Extended
3364 * - EUI-64 based 16-byte
3365 * - EUI-64 based 12-byte
3366 * - NAA IEEE Registered
3367 * - NAA IEEE Extended
Hannes Reinecked2308232016-05-09 09:14:29 +02003368 * - T10 Vendor ID
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003369 * as longer descriptors reduce the likelyhood
3370 * of identification clashes.
3371 */
3372
3373 /* The id string must be at least 20 bytes + terminating NULL byte */
3374 if (id_len < 21) {
3375 rcu_read_unlock();
3376 return -EINVAL;
3377 }
3378
3379 memset(id, 0, id_len);
Bart Van Asscheccf1e002017-08-29 08:50:13 -07003380 d = vpd_pg83->data + 4;
3381 while (d < vpd_pg83->data + vpd_pg83->len) {
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003382 /* Skip designators not referring to the LUN */
3383 if ((d[1] & 0x30) != 0x00)
3384 goto next_desig;
3385
3386 switch (d[1] & 0xf) {
Hannes Reinecked2308232016-05-09 09:14:29 +02003387 case 0x1:
3388 /* T10 Vendor ID */
3389 if (cur_id_size > d[3])
3390 break;
3391 /* Prefer anything */
3392 if (cur_id_type > 0x01 && cur_id_type != 0xff)
3393 break;
3394 cur_id_size = d[3];
3395 if (cur_id_size + 4 > id_len)
3396 cur_id_size = id_len - 4;
3397 cur_id_str = d + 4;
3398 cur_id_type = d[1] & 0xf;
3399 id_size = snprintf(id, id_len, "t10.%*pE",
3400 cur_id_size, cur_id_str);
3401 break;
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003402 case 0x2:
3403 /* EUI-64 */
3404 if (cur_id_size > d[3])
3405 break;
3406 /* Prefer NAA IEEE Registered Extended */
3407 if (cur_id_type == 0x3 &&
3408 cur_id_size == d[3])
3409 break;
3410 cur_id_size = d[3];
3411 cur_id_str = d + 4;
3412 cur_id_type = d[1] & 0xf;
3413 switch (cur_id_size) {
3414 case 8:
3415 id_size = snprintf(id, id_len,
3416 "eui.%8phN",
3417 cur_id_str);
3418 break;
3419 case 12:
3420 id_size = snprintf(id, id_len,
3421 "eui.%12phN",
3422 cur_id_str);
3423 break;
3424 case 16:
3425 id_size = snprintf(id, id_len,
3426 "eui.%16phN",
3427 cur_id_str);
3428 break;
3429 default:
3430 cur_id_size = 0;
3431 break;
3432 }
3433 break;
3434 case 0x3:
3435 /* NAA */
3436 if (cur_id_size > d[3])
3437 break;
3438 cur_id_size = d[3];
3439 cur_id_str = d + 4;
3440 cur_id_type = d[1] & 0xf;
3441 switch (cur_id_size) {
3442 case 8:
3443 id_size = snprintf(id, id_len,
3444 "naa.%8phN",
3445 cur_id_str);
3446 break;
3447 case 16:
3448 id_size = snprintf(id, id_len,
3449 "naa.%16phN",
3450 cur_id_str);
3451 break;
3452 default:
3453 cur_id_size = 0;
3454 break;
3455 }
3456 break;
3457 case 0x8:
3458 /* SCSI name string */
3459 if (cur_id_size + 4 > d[3])
3460 break;
3461 /* Prefer others for truncated descriptor */
3462 if (cur_id_size && d[3] > id_len)
3463 break;
3464 cur_id_size = id_size = d[3];
3465 cur_id_str = d + 4;
3466 cur_id_type = d[1] & 0xf;
3467 if (cur_id_size >= id_len)
3468 cur_id_size = id_len - 1;
3469 memcpy(id, cur_id_str, cur_id_size);
3470 /* Decrease priority for truncated descriptor */
3471 if (cur_id_size != id_size)
3472 cur_id_size = 6;
3473 break;
3474 default:
3475 break;
3476 }
3477next_desig:
3478 d += d[3] + 4;
3479 }
3480 rcu_read_unlock();
3481
3482 return id_size;
3483}
3484EXPORT_SYMBOL(scsi_vpd_lun_id);
Hannes Reineckea8aa3972015-12-01 10:16:57 +01003485
3486/*
3487 * scsi_vpd_tpg_id - return a target port group identifier
3488 * @sdev: SCSI device
3489 *
3490 * Returns the Target Port Group identifier from the information
3491 * froom VPD page 0x83 of the device.
3492 *
3493 * Returns the identifier or error on failure.
3494 */
3495int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
3496{
Bart Van Asscheccf1e002017-08-29 08:50:13 -07003497 const unsigned char *d;
3498 const struct scsi_vpd *vpd_pg83;
Hannes Reineckea8aa3972015-12-01 10:16:57 +01003499 int group_id = -EAGAIN, rel_port = -1;
3500
3501 rcu_read_lock();
3502 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3503 if (!vpd_pg83) {
3504 rcu_read_unlock();
3505 return -ENXIO;
3506 }
3507
Bart Van Asscheccf1e002017-08-29 08:50:13 -07003508 d = vpd_pg83->data + 4;
3509 while (d < vpd_pg83->data + vpd_pg83->len) {
Hannes Reineckea8aa3972015-12-01 10:16:57 +01003510 switch (d[1] & 0xf) {
3511 case 0x4:
3512 /* Relative target port */
3513 rel_port = get_unaligned_be16(&d[6]);
3514 break;
3515 case 0x5:
3516 /* Target port group */
3517 group_id = get_unaligned_be16(&d[6]);
3518 break;
3519 default:
3520 break;
3521 }
3522 d += d[3] + 4;
3523 }
3524 rcu_read_unlock();
3525
3526 if (group_id >= 0 && rel_id && rel_port != -1)
3527 *rel_id = rel_port;
3528
3529 return group_id;
3530}
3531EXPORT_SYMBOL(scsi_vpd_tpg_id);