blob: 3d6b364a8d8292897f9dbe00fad25b51cd0401a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Christoph Hellwigd2852032014-01-17 12:06:53 +01002 * Copyright (C) 1999 Eric Youngdale
3 * Copyright (C) 2014 Christoph Hellwig
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * SCSI queueing library.
6 * Initial versions: Eric Youngdale (eric@andante.org).
7 * Based upon conversations with large numbers
8 * of people at Linux Expo.
9 */
10
11#include <linux/bio.h>
James Bottomleyd3f46f32008-01-15 11:11:46 -060012#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/blkdev.h>
14#include <linux/completion.h>
15#include <linux/kernel.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
James Bottomleyfaead262006-02-14 10:42:07 -060020#include <linux/hardirq.h>
Jens Axboec6132da2007-10-16 11:08:49 +020021#include <linux/scatterlist.h>
Christoph Hellwigd2852032014-01-17 12:06:53 +010022#include <linux/blk-mq.h>
Hannes Reineckef1569ff2014-10-24 14:27:07 +020023#include <linux/ratelimit.h>
Hannes Reineckea8aa3972015-12-01 10:16:57 +010024#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020027#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <scsi/scsi_dbg.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_driver.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_host.h>
Christoph Hellwigee14c672015-08-27 14:16:59 +020033#include <scsi/scsi_dh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Christoph Hellwig3b5382c2014-05-06 12:25:40 +020035#include <trace/events/scsi.h>
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "scsi_priv.h"
38#include "scsi_logging.h"
39
40
Martin K. Petersen7027ad72008-07-17 17:08:48 -040041struct kmem_cache *scsi_sdb_cache;
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +030042static struct kmem_cache *scsi_sense_cache;
43static struct kmem_cache *scsi_sense_isadma_cache;
44static DEFINE_MUTEX(scsi_sense_cache_mutex);
45
46static inline struct kmem_cache *
47scsi_select_sense_cache(struct Scsi_Host *shost)
48{
49 return shost->unchecked_isa_dma ?
50 scsi_sense_isadma_cache : scsi_sense_cache;
51}
52
53void scsi_free_sense_buffer(struct Scsi_Host *shost,
54 unsigned char *sense_buffer)
55{
56 kmem_cache_free(scsi_select_sense_cache(shost), sense_buffer);
57}
58
59unsigned char *scsi_alloc_sense_buffer(struct Scsi_Host *shost, gfp_t gfp_mask,
60 int numa_node)
61{
62 return kmem_cache_alloc_node(scsi_select_sense_cache(shost), gfp_mask,
63 numa_node);
64}
65
66int scsi_init_sense_cache(struct Scsi_Host *shost)
67{
68 struct kmem_cache *cache;
69 int ret = 0;
70
71 cache = scsi_select_sense_cache(shost);
72 if (cache)
73 return 0;
74
75 mutex_lock(&scsi_sense_cache_mutex);
76 if (shost->unchecked_isa_dma) {
77 scsi_sense_isadma_cache =
78 kmem_cache_create("scsi_sense_cache(DMA)",
79 SCSI_SENSE_BUFFERSIZE, 0,
80 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL);
81 if (!scsi_sense_isadma_cache)
82 ret = -ENOMEM;
83 } else {
84 scsi_sense_cache =
85 kmem_cache_create("scsi_sense_cache",
86 SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN, NULL);
87 if (!scsi_sense_cache)
88 ret = -ENOMEM;
89 }
90
91 mutex_unlock(&scsi_sense_cache_mutex);
92 return ret;
93}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +020094
Jens Axboea488e742010-04-16 21:13:15 +020095/*
96 * When to reinvoke queueing after a resource shortage. It's 3 msecs to
97 * not change behaviour from the previous unplug mechanism, experimentation
98 * may prove this needs changing.
99 */
100#define SCSI_QUEUE_DELAY 3
101
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100102static void
103scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct Scsi_Host *host = cmd->device->host;
106 struct scsi_device *device = cmd->device;
Mike Christief0c0a372008-08-17 15:24:38 -0500107 struct scsi_target *starget = scsi_target(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /*
Tejun Heo d8c37e72005-05-14 00:46:08 +0900110 * Set the appropriate busy bit for the device/host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
112 * If the host/device isn't busy, assume that something actually
113 * completed, and that we should be able to queue a command now.
114 *
115 * Note that the prior mid-layer assumption that any host could
116 * always queue at least one command is now broken. The mid-layer
117 * will implement a user specifiable stall (see
118 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
119 * if a command is requeued with no other commands outstanding
120 * either for the device or for the host.
121 */
Mike Christief0c0a372008-08-17 15:24:38 -0500122 switch (reason) {
123 case SCSI_MLQUEUE_HOST_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100124 atomic_set(&host->host_blocked, host->max_host_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500125 break;
126 case SCSI_MLQUEUE_DEVICE_BUSY:
James Smart573e5912011-07-06 12:45:17 -0400127 case SCSI_MLQUEUE_EH_RETRY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100128 atomic_set(&device->device_blocked,
129 device->max_device_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500130 break;
131 case SCSI_MLQUEUE_TARGET_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100132 atomic_set(&starget->target_blocked,
133 starget->max_target_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -0500134 break;
135 }
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100136}
137
Christoph Hellwigd2852032014-01-17 12:06:53 +0100138static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd)
139{
140 struct scsi_device *sdev = cmd->device;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100141
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700142 blk_mq_requeue_request(cmd->request, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100143 put_device(&sdev->sdev_gendev);
144}
145
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +0100146/**
147 * __scsi_queue_insert - private queue insertion
148 * @cmd: The SCSI command being requeued
149 * @reason: The reason for the requeue
150 * @unbusy: Whether the queue should be unbusied
151 *
152 * This is a private queue insertion. The public interface
153 * scsi_queue_insert() always assumes the queue should be unbusied
154 * because it's always called before the completion. This function is
155 * for a requeue after completion, which should only occur in this
156 * file.
157 */
158static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy)
159{
160 struct scsi_device *device = cmd->device;
161 struct request_queue *q = device->request_queue;
162 unsigned long flags;
163
164 SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
165 "Inserting command %p into mlqueue\n", cmd));
166
167 scsi_set_blocked(cmd, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Decrement the counters, since these commands are no longer
171 * active on the host/device.
172 */
James Bottomley4f5299a2009-01-02 10:42:21 -0600173 if (unbusy)
174 scsi_device_unbusy(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /*
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500177 * Requeue this command. It will go before all other commands
Bart Van Asscheb4854622012-06-29 15:36:07 +0000178 * that are already in the queue. Schedule requeue work under
179 * lock such that the kblockd_schedule_work() call happens
180 * before blk_cleanup_queue() finishes.
Jens Axboea488e742010-04-16 21:13:15 +0200181 */
Alan Stern644373a2014-03-28 10:51:15 -0700182 cmd->result = 0;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100183 if (q->mq_ops) {
184 scsi_mq_requeue_cmd(cmd);
185 return;
186 }
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500187 spin_lock_irqsave(q->queue_lock, flags);
James Bottomley59897da2005-09-14 12:57:42 -0400188 blk_requeue_request(q, cmd->request);
Jens Axboe59c3d452014-04-08 09:15:35 -0600189 kblockd_schedule_work(&device->requeue_work);
Bart Van Asscheb4854622012-06-29 15:36:07 +0000190 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
James Bottomley4f5299a2009-01-02 10:42:21 -0600193/*
194 * Function: scsi_queue_insert()
195 *
196 * Purpose: Insert a command in the midlevel queue.
197 *
198 * Arguments: cmd - command that we are adding to queue.
199 * reason - why we are inserting command to queue.
200 *
201 * Lock status: Assumed that lock is not held upon entry.
202 *
203 * Returns: Nothing.
204 *
205 * Notes: We do this for one of two cases. Either the host is busy
206 * and it cannot accept any more commands for the time being,
207 * or the device returned QUEUE_FULL and can accept no more
208 * commands.
209 * Notes: This could be called either from an interrupt context or a
210 * normal process context.
211 */
Bart Van Assche84feb162012-06-29 15:35:05 +0000212void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
James Bottomley4f5299a2009-01-02 10:42:21 -0600213{
Bart Van Assche84feb162012-06-29 15:35:05 +0000214 __scsi_queue_insert(cmd, reason, 1);
James Bottomley4f5299a2009-01-02 10:42:21 -0600215}
Christoph Hellwige8064022016-10-20 15:12:13 +0200216
217static int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
James Bottomley33aa6872005-08-28 11:31:14 -0500218 int data_direction, void *buffer, unsigned bufflen,
Martin K. Petersen2bfad212014-04-09 22:20:48 -0400219 unsigned char *sense, int timeout, int retries, u64 flags,
Christoph Hellwige8064022016-10-20 15:12:13 +0200220 req_flags_t rq_flags, int *resid)
James Bottomley39216032005-06-15 18:48:29 -0500221{
222 struct request *req;
223 int write = (data_direction == DMA_TO_DEVICE);
224 int ret = DRIVER_ERROR << 24;
225
Mel Gorman71baba42015-11-06 16:28:28 -0800226 req = blk_get_request(sdev->request_queue, write, __GFP_RECLAIM);
Joe Lawrencea492f072014-08-28 08:15:21 -0600227 if (IS_ERR(req))
James Bottomleybfe159a2011-07-07 15:45:40 -0500228 return ret;
Jens Axboef27b0872014-06-06 07:57:37 -0600229 blk_rq_set_block_pc(req);
James Bottomley39216032005-06-15 18:48:29 -0500230
231 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
Mel Gorman71baba42015-11-06 16:28:28 -0800232 buffer, bufflen, __GFP_RECLAIM))
James Bottomley39216032005-06-15 18:48:29 -0500233 goto out;
234
235 req->cmd_len = COMMAND_SIZE(cmd[0]);
236 memcpy(req->cmd, cmd, req->cmd_len);
237 req->sense = sense;
238 req->sense_len = 0;
Mike Christie17e01f22005-11-11 05:31:37 -0600239 req->retries = retries;
James Bottomley39216032005-06-15 18:48:29 -0500240 req->timeout = timeout;
Christoph Hellwige8064022016-10-20 15:12:13 +0200241 req->cmd_flags |= flags;
242 req->rq_flags |= rq_flags | RQF_QUIET | RQF_PREEMPT;
James Bottomley39216032005-06-15 18:48:29 -0500243
244 /*
245 * head injection *required* here otherwise quiesce won't work
246 */
247 blk_execute_rq(req->q, NULL, req, 1);
248
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400249 /*
250 * Some devices (USB mass-storage in particular) may transfer
251 * garbage data together with a residue indicating that the data
252 * is invalid. Prevent the garbage from being misinterpreted
253 * and prevent security leaks by zeroing out the excess data.
254 */
Tejun Heoc3a4d782009-05-07 22:24:37 +0900255 if (unlikely(req->resid_len > 0 && req->resid_len <= bufflen))
256 memset(buffer + (bufflen - req->resid_len), 0, req->resid_len);
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400257
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900258 if (resid)
Tejun Heoc3a4d782009-05-07 22:24:37 +0900259 *resid = req->resid_len;
James Bottomley39216032005-06-15 18:48:29 -0500260 ret = req->errors;
261 out:
262 blk_put_request(req);
263
264 return ret;
265}
Christoph Hellwige8064022016-10-20 15:12:13 +0200266
267/**
268 * scsi_execute - insert request and wait for the result
269 * @sdev: scsi device
270 * @cmd: scsi command
271 * @data_direction: data direction
272 * @buffer: data buffer
273 * @bufflen: len of buffer
274 * @sense: optional sense buffer
275 * @timeout: request timeout in seconds
276 * @retries: number of times to retry request
277 * @flags: or into request flags;
278 * @resid: optional residual length
279 *
280 * returns the req->errors value which is the scsi_cmnd result
281 * field.
282 */
283int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
284 int data_direction, void *buffer, unsigned bufflen,
285 unsigned char *sense, int timeout, int retries, u64 flags,
286 int *resid)
287{
288 return __scsi_execute(sdev, cmd, data_direction, buffer, bufflen, sense,
289 timeout, retries, flags, 0, resid);
290}
James Bottomley33aa6872005-08-28 11:31:14 -0500291EXPORT_SYMBOL(scsi_execute);
James Bottomley39216032005-06-15 18:48:29 -0500292
Lin Ming9b214932013-03-23 11:42:25 +0800293int scsi_execute_req_flags(struct scsi_device *sdev, const unsigned char *cmd,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500294 int data_direction, void *buffer, unsigned bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900295 struct scsi_sense_hdr *sshdr, int timeout, int retries,
Christoph Hellwige8064022016-10-20 15:12:13 +0200296 int *resid, u64 flags, req_flags_t rq_flags)
James Bottomleyea73a9f2005-08-28 11:33:52 -0500297{
298 char *sense = NULL;
akpm@osdl.org1ccb48b2005-06-26 00:12:51 -0700299 int result;
300
James Bottomleyea73a9f2005-08-28 11:33:52 -0500301 if (sshdr) {
Jes Sorensen24669f752006-01-16 10:31:18 -0500302 sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500303 if (!sense)
304 return DRIVER_ERROR << 24;
James Bottomleyea73a9f2005-08-28 11:33:52 -0500305 }
Christoph Hellwige8064022016-10-20 15:12:13 +0200306 result = __scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
307 sense, timeout, retries, flags, rq_flags, resid);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500308 if (sshdr)
James Bottomleye5143852005-08-09 11:55:36 -0500309 scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500310
311 kfree(sense);
312 return result;
313}
Lin Ming9b214932013-03-23 11:42:25 +0800314EXPORT_SYMBOL(scsi_execute_req_flags);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
317 * Function: scsi_init_cmd_errh()
318 *
319 * Purpose: Initialize cmd fields related to error handling.
320 *
321 * Arguments: cmd - command that is ready to be queued.
322 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * Notes: This function has the job of initializing a number of
324 * fields related to error handling. Typically this will
325 * be called once for each command, as required.
326 */
Christoph Hellwig631c2282006-07-08 20:42:15 +0200327static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 cmd->serial_number = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200330 scsi_set_resid(cmd, 0);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900331 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (cmd->cmd_len == 0)
Boaz Harroshdb4742d2008-04-30 11:27:26 +0300333 cmd->cmd_len = scsi_command_size(cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336void scsi_device_unbusy(struct scsi_device *sdev)
337{
338 struct Scsi_Host *shost = sdev->host;
Mike Christief0c0a372008-08-17 15:24:38 -0500339 struct scsi_target *starget = scsi_target(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned long flags;
341
Christoph Hellwig74665012014-01-22 15:29:29 +0100342 atomic_dec(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100343 if (starget->can_queue > 0)
344 atomic_dec(&starget->target_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +0100345
James Bottomley939647e2005-09-18 15:05:20 -0500346 if (unlikely(scsi_host_in_recovery(shost) &&
Christoph Hellwig74665012014-01-22 15:29:29 +0100347 (shost->host_failed || shost->host_eh_scheduled))) {
348 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 scsi_eh_wakeup(shost);
Christoph Hellwig74665012014-01-22 15:29:29 +0100350 spin_unlock_irqrestore(shost->host_lock, flags);
351 }
352
Christoph Hellwig71e75c92014-04-11 19:07:01 +0200353 atomic_dec(&sdev->device_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Christoph Hellwigd2852032014-01-17 12:06:53 +0100356static void scsi_kick_queue(struct request_queue *q)
357{
358 if (q->mq_ops)
359 blk_mq_start_hw_queues(q);
360 else
361 blk_run_queue(q);
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/*
365 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
366 * and call blk_run_queue for all the scsi_devices on the target -
367 * including current_sdev first.
368 *
369 * Called with *no* scsi locks held.
370 */
371static void scsi_single_lun_run(struct scsi_device *current_sdev)
372{
373 struct Scsi_Host *shost = current_sdev->host;
374 struct scsi_device *sdev, *tmp;
375 struct scsi_target *starget = scsi_target(current_sdev);
376 unsigned long flags;
377
378 spin_lock_irqsave(shost->host_lock, flags);
379 starget->starget_sdev_user = NULL;
380 spin_unlock_irqrestore(shost->host_lock, flags);
381
382 /*
383 * Call blk_run_queue for all LUNs on the target, starting with
384 * current_sdev. We race with others (to set starget_sdev_user),
385 * but in most cases, we will be first. Ideally, each LU on the
386 * target would get some limited time or requests on the target.
387 */
Christoph Hellwigd2852032014-01-17 12:06:53 +0100388 scsi_kick_queue(current_sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 spin_lock_irqsave(shost->host_lock, flags);
391 if (starget->starget_sdev_user)
392 goto out;
393 list_for_each_entry_safe(sdev, tmp, &starget->devices,
394 same_target_siblings) {
395 if (sdev == current_sdev)
396 continue;
397 if (scsi_device_get(sdev))
398 continue;
399
400 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100401 scsi_kick_queue(sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 spin_lock_irqsave(shost->host_lock, flags);
403
404 scsi_device_put(sdev);
405 }
406 out:
407 spin_unlock_irqrestore(shost->host_lock, flags);
408}
409
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100410static inline bool scsi_device_is_busy(struct scsi_device *sdev)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400411{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100412 if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
413 return true;
414 if (atomic_read(&sdev->device_blocked) > 0)
415 return true;
416 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400417}
418
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100419static inline bool scsi_target_is_busy(struct scsi_target *starget)
Mike Christief0c0a372008-08-17 15:24:38 -0500420{
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100421 if (starget->can_queue > 0) {
422 if (atomic_read(&starget->target_busy) >= starget->can_queue)
423 return true;
424 if (atomic_read(&starget->target_blocked) > 0)
425 return true;
426 }
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100427 return false;
Mike Christief0c0a372008-08-17 15:24:38 -0500428}
429
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100430static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400431{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100432 if (shost->can_queue > 0 &&
433 atomic_read(&shost->host_busy) >= shost->can_queue)
434 return true;
435 if (atomic_read(&shost->host_blocked) > 0)
436 return true;
437 if (shost->host_self_blocked)
438 return true;
439 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400440}
441
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800442static void scsi_starved_list_run(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Mike Christie2a3a59e2008-11-11 13:42:35 -0600444 LIST_HEAD(starved_list);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800445 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 unsigned long flags;
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 spin_lock_irqsave(shost->host_lock, flags);
Mike Christie2a3a59e2008-11-11 13:42:35 -0600449 list_splice_init(&shost->starved_list, &starved_list);
450
451 while (!list_empty(&starved_list)) {
James Bottomleye2eb7242013-07-02 15:05:26 +0200452 struct request_queue *slq;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
455 * As long as shost is accepting commands and we have
456 * starved queues, call blk_run_queue. scsi_request_fn
457 * drops the queue_lock and can add us back to the
458 * starved_list.
459 *
460 * host_lock protects the starved_list and starved_entry.
461 * scsi_request_fn must get the host_lock before checking
462 * or modifying starved_list or starved_entry.
463 */
Mike Christie2a3a59e2008-11-11 13:42:35 -0600464 if (scsi_host_is_busy(shost))
Mike Christief0c0a372008-08-17 15:24:38 -0500465 break;
Mike Christief0c0a372008-08-17 15:24:38 -0500466
Mike Christie2a3a59e2008-11-11 13:42:35 -0600467 sdev = list_entry(starved_list.next,
468 struct scsi_device, starved_entry);
469 list_del_init(&sdev->starved_entry);
Mike Christief0c0a372008-08-17 15:24:38 -0500470 if (scsi_target_is_busy(scsi_target(sdev))) {
471 list_move_tail(&sdev->starved_entry,
472 &shost->starved_list);
473 continue;
474 }
475
James Bottomleye2eb7242013-07-02 15:05:26 +0200476 /*
477 * Once we drop the host lock, a racing scsi_remove_device()
478 * call may remove the sdev from the starved list and destroy
479 * it and the queue. Mitigate by taking a reference to the
480 * queue and never touching the sdev again after we drop the
481 * host lock. Note: if __scsi_remove_device() invokes
482 * blk_cleanup_queue() before the queue is run from this
483 * function then blk_run_queue() will return immediately since
484 * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
485 */
486 slq = sdev->request_queue;
487 if (!blk_get_queue(slq))
488 continue;
489 spin_unlock_irqrestore(shost->host_lock, flags);
490
Christoph Hellwigd2852032014-01-17 12:06:53 +0100491 scsi_kick_queue(slq);
James Bottomleye2eb7242013-07-02 15:05:26 +0200492 blk_put_queue(slq);
493
494 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Mike Christie2a3a59e2008-11-11 13:42:35 -0600496 /* put any unprocessed entries back */
497 list_splice(&starved_list, &shost->starved_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800499}
500
501/*
502 * Function: scsi_run_queue()
503 *
504 * Purpose: Select a proper request queue to serve next
505 *
506 * Arguments: q - last request's queue
507 *
508 * Returns: Nothing
509 *
510 * Notes: The previous command was completely finished, start
511 * a new one if possible.
512 */
513static void scsi_run_queue(struct request_queue *q)
514{
515 struct scsi_device *sdev = q->queuedata;
516
517 if (scsi_target(sdev)->single_lun)
518 scsi_single_lun_run(sdev);
519 if (!list_empty(&sdev->host->starved_list))
520 scsi_starved_list_run(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Christoph Hellwigd2852032014-01-17 12:06:53 +0100522 if (q->mq_ops)
523 blk_mq_start_stopped_hw_queues(q, false);
524 else
525 blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Jens Axboe9937a5e2011-05-17 11:04:44 +0200528void scsi_requeue_run_queue(struct work_struct *work)
529{
530 struct scsi_device *sdev;
531 struct request_queue *q;
532
533 sdev = container_of(work, struct scsi_device, requeue_work);
534 q = sdev->request_queue;
535 scsi_run_queue(q);
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/*
539 * Function: scsi_requeue_command()
540 *
541 * Purpose: Handle post-processing of completed commands.
542 *
543 * Arguments: q - queue to operate on
544 * cmd - command that may need to be requeued.
545 *
546 * Returns: Nothing
547 *
548 * Notes: After command completion, there may be blocks left
549 * over which weren't finished by the previous command
550 * this can be for a number of reasons - the main one is
551 * I/O errors in the middle of the request, in which case
552 * we need to request the blocks that come after the bad
553 * sector.
James Bottomleye91442b2005-09-09 10:44:16 -0500554 * Notes: Upon return, cmd is a stale pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
556static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
557{
Bart Van Assche940f5d42012-06-29 15:34:26 +0000558 struct scsi_device *sdev = cmd->device;
James Bottomleye91442b2005-09-09 10:44:16 -0500559 struct request *req = cmd->request;
Tejun Heo 283369c2005-04-24 02:06:36 -0500560 unsigned long flags;
561
Tejun Heo 283369c2005-04-24 02:06:36 -0500562 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig134997a2014-02-20 14:20:58 -0800563 blk_unprep_request(req);
564 req->special = NULL;
565 scsi_put_command(cmd);
James Bottomleye91442b2005-09-09 10:44:16 -0500566 blk_requeue_request(q, req);
Tejun Heo 283369c2005-04-24 02:06:36 -0500567 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 scsi_run_queue(q);
Bart Van Assche940f5d42012-06-29 15:34:26 +0000570
571 put_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574void scsi_run_host_queues(struct Scsi_Host *shost)
575{
576 struct scsi_device *sdev;
577
578 shost_for_each_device(sdev, shost)
579 scsi_run_queue(sdev->request_queue);
580}
581
Christoph Hellwigd2852032014-01-17 12:06:53 +0100582static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
583{
584 if (cmd->request->cmd_type == REQ_TYPE_FS) {
585 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
586
587 if (drv->uninit_command)
588 drv->uninit_command(cmd);
589 }
590}
591
592static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd)
593{
Ming Lin91dbc082016-04-04 14:48:07 -0700594 struct scsi_data_buffer *sdb;
595
Christoph Hellwigd2852032014-01-17 12:06:53 +0100596 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700597 sg_free_table_chained(&cmd->sdb.table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700598 if (cmd->request->next_rq) {
599 sdb = cmd->request->next_rq->special;
600 if (sdb)
Ming Lin001d63b2016-04-04 14:48:09 -0700601 sg_free_table_chained(&sdb->table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700602 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100603 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700604 sg_free_table_chained(&cmd->prot_sdb->table, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100605}
606
607static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
608{
609 struct scsi_device *sdev = cmd->device;
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +0530610 struct Scsi_Host *shost = sdev->host;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100611 unsigned long flags;
612
Christoph Hellwigd2852032014-01-17 12:06:53 +0100613 scsi_mq_free_sgtables(cmd);
614 scsi_uninit_cmd(cmd);
615
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +0530616 if (shost->use_cmd_list) {
617 BUG_ON(list_empty(&cmd->list));
618 spin_lock_irqsave(&sdev->list_lock, flags);
619 list_del_init(&cmd->list);
620 spin_unlock_irqrestore(&sdev->list_lock, flags);
621 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/*
625 * Function: scsi_release_buffers()
626 *
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200627 * Purpose: Free resources allocate for a scsi_command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 *
629 * Arguments: cmd - command that we are bailing.
630 *
631 * Lock status: Assumed that no lock is held upon entry.
632 *
633 * Returns: Nothing
634 *
635 * Notes: In the event that an upper level driver rejects a
636 * command, we must release resources allocated during
637 * the __init_io() function. Primarily this would involve
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200638 * the scatter-gather table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 */
Christoph Hellwigf1bea552014-04-15 12:26:54 +0200640static void scsi_release_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200642 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700643 sg_free_table_chained(&cmd->sdb.table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200644
645 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
646
647 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700648 sg_free_table_chained(&cmd->prot_sdb->table, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200651static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
652{
653 struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special;
654
Ming Lin001d63b2016-04-04 14:48:09 -0700655 sg_free_table_chained(&bidi_sdb->table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200656 kmem_cache_free(scsi_sdb_cache, bidi_sdb);
657 cmd->request->next_rq->special = NULL;
658}
659
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800660static bool scsi_end_request(struct request *req, int error,
661 unsigned int bytes, unsigned int bidi_bytes)
662{
663 struct scsi_cmnd *cmd = req->special;
664 struct scsi_device *sdev = cmd->device;
665 struct request_queue *q = sdev->request_queue;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800666
667 if (blk_update_request(req, error, bytes))
668 return true;
669
670 /* Bidi request must be completed as a whole */
671 if (unlikely(bidi_bytes) &&
672 blk_update_request(req->next_rq, error, bidi_bytes))
673 return true;
674
675 if (blk_queue_add_random(q))
676 add_disk_randomness(req->rq_disk);
677
Christoph Hellwigd2852032014-01-17 12:06:53 +0100678 if (req->mq_ctx) {
679 /*
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700680 * In the MQ case the command gets freed by __blk_mq_end_request,
Christoph Hellwigd2852032014-01-17 12:06:53 +0100681 * so we have to do all cleanup that depends on it earlier.
682 *
683 * We also can't kick the queues from irq context, so we
684 * will have to defer it to a workqueue.
685 */
686 scsi_mq_uninit_cmd(cmd);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800687
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700688 __blk_mq_end_request(req, error);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100689
690 if (scsi_target(sdev)->single_lun ||
691 !list_empty(&sdev->host->starved_list))
692 kblockd_schedule_work(&sdev->requeue_work);
693 else
694 blk_mq_start_stopped_hw_queues(q, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100695 } else {
696 unsigned long flags;
697
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400698 if (bidi_bytes)
699 scsi_release_bidi_buffers(cmd);
700
Christoph Hellwigd2852032014-01-17 12:06:53 +0100701 spin_lock_irqsave(q->queue_lock, flags);
702 blk_finish_request(req, error);
703 spin_unlock_irqrestore(q->queue_lock, flags);
704
Christoph Hellwigd2852032014-01-17 12:06:53 +0100705 scsi_release_buffers(cmd);
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700706
707 scsi_put_command(cmd);
708 scsi_run_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100709 }
710
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700711 put_device(&sdev->sdev_gendev);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800712 return false;
713}
714
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200715/**
716 * __scsi_error_from_host_byte - translate SCSI error code into errno
717 * @cmd: SCSI command (unused)
718 * @result: scsi error code
719 *
720 * Translate SCSI error code into standard UNIX errno.
721 * Return values:
722 * -ENOLINK temporary transport failure
723 * -EREMOTEIO permanent target failure, do not retry
724 * -EBADE permanent nexus failure, retry on other path
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200725 * -ENOSPC No write space available
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200726 * -ENODATA Medium error
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200727 * -EIO unspecified I/O error
728 */
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100729static int __scsi_error_from_host_byte(struct scsi_cmnd *cmd, int result)
730{
731 int error = 0;
732
733 switch(host_byte(result)) {
734 case DID_TRANSPORT_FAILFAST:
735 error = -ENOLINK;
736 break;
737 case DID_TARGET_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000738 set_host_byte(cmd, DID_OK);
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100739 error = -EREMOTEIO;
740 break;
741 case DID_NEXUS_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000742 set_host_byte(cmd, DID_OK);
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100743 error = -EBADE;
744 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200745 case DID_ALLOC_FAILURE:
746 set_host_byte(cmd, DID_OK);
747 error = -ENOSPC;
748 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200749 case DID_MEDIUM_ERROR:
750 set_host_byte(cmd, DID_OK);
751 error = -ENODATA;
752 break;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100753 default:
754 error = -EIO;
755 break;
756 }
757
758 return error;
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761/*
762 * Function: scsi_io_completion()
763 *
764 * Purpose: Completion processing for block device I/O requests.
765 *
766 * Arguments: cmd - command that is finished.
767 *
768 * Lock status: Assumed that no lock is held upon entry.
769 *
770 * Returns: Nothing
771 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200772 * Notes: We will finish off the specified number of sectors. If we
773 * are done, the command block will be released and the queue
774 * function will be goosed. If we are not done then we have to
Alan Sternb60af5b2008-11-03 15:56:47 -0500775 * figure out what to do next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *
Alan Sternb60af5b2008-11-03 15:56:47 -0500777 * a) We can call scsi_requeue_command(). The request
778 * will be unprepared and put back on the queue. Then
779 * a new command will be created for it. This should
780 * be used if we made forward progress, or if we want
781 * to switch from READ(10) to READ(6) for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200783 * b) We can call __scsi_queue_insert(). The request will
Alan Sternb60af5b2008-11-03 15:56:47 -0500784 * be put back on the queue and retried using the same
785 * command as before, possibly after a delay.
786 *
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800787 * c) We can call scsi_end_request() with -EIO to fail
Alan Sternb60af5b2008-11-03 15:56:47 -0500788 * the remainder of the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700790void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 int result = cmd->result;
Jens Axboe165125e2007-07-24 09:28:11 +0200793 struct request_queue *q = cmd->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct request *req = cmd->request;
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500795 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 struct scsi_sense_hdr sshdr;
Hannes Reinecke4753cbc2014-10-24 14:26:52 +0200797 bool sense_valid = false;
Hannes Reineckec11c0042014-10-24 14:27:01 +0200798 int sense_deferred = 0, level = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500799 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
800 ACTION_DELAYED_RETRY} action;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900801 unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (result) {
804 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
805 if (sense_valid)
806 sense_deferred = scsi_sense_is_deferred(&sshdr);
807 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200808
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200809 if (req->cmd_type == REQ_TYPE_BLOCK_PC) { /* SG_IO ioctl from block level */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (sense_valid && req->sense) {
812 /*
813 * SG_IO wants current and deferred errors
814 */
815 int len = 8 + cmd->sense_buffer[7];
816
817 if (len > SCSI_SENSE_BUFFERSIZE)
818 len = SCSI_SENSE_BUFFERSIZE;
819 memcpy(req->sense, cmd->sense_buffer, len);
820 req->sense_len = len;
821 }
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500822 if (!sense_deferred)
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100823 error = __scsi_error_from_host_byte(cmd, result);
Pete Wyckoffb22f6872007-03-13 16:53:28 -0400824 }
Mike Snitzer27c41972012-05-31 15:05:33 -0400825 /*
826 * __scsi_error_from_host_byte may have reset the host_byte
827 */
828 req->errors = cmd->result;
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900829
830 req->resid_len = scsi_get_resid(cmd);
831
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200832 if (scsi_bidi_cmnd(cmd)) {
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900833 /*
834 * Bidi commands Must be complete as a whole,
835 * both sides at once.
836 */
837 req->next_rq->resid_len = scsi_in(cmd)->resid;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800838 if (scsi_end_request(req, 0, blk_rq_bytes(req),
839 blk_rq_bytes(req->next_rq)))
840 BUG();
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200841 return;
842 }
James Bottomley89fb4cd2014-07-03 19:17:34 +0200843 } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
844 /*
845 * Certain non BLOCK_PC requests are commands that don't
846 * actually transfer anything (FLUSH), so cannot use
847 * good_bytes != blk_rq_bytes(req) as the signal for an error.
848 * This sets the error explicitly for the problem case.
849 */
850 error = __scsi_error_from_host_byte(cmd, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200853 /* no bidi support for !REQ_TYPE_BLOCK_PC yet */
854 BUG_ON(blk_bidi_rq(req));
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 * Next deal with any sectors which we were able to correctly
858 * handle.
859 */
Hannes Reinecke91921e02014-06-25 16:39:59 +0200860 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
861 "%u sectors total, %d bytes done.\n",
862 blk_rq_sectors(req), good_bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
James Bottomleya9bddd72009-03-30 16:55:51 +0000864 /*
865 * Recovered errors need reporting, but they're always treated
866 * as success, so fiddle the result code here. For BLOCK_PC
867 * we already took a copy of the original into rq->errors which
868 * is what gets returned to the user
869 */
Douglas Gilberte7efe592010-01-03 13:51:15 -0500870 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
871 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
872 * print since caller wants ATA registers. Only occurs on
873 * SCSI ATA PASS_THROUGH commands when CK_COND=1
874 */
875 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
876 ;
Christoph Hellwige8064022016-10-20 15:12:13 +0200877 else if (!(req->rq_flags & RQF_QUIET))
Hannes Reinecked811b842014-10-24 14:26:45 +0200878 scsi_print_sense(cmd);
James Bottomleya9bddd72009-03-30 16:55:51 +0000879 result = 0;
880 /* BLOCK_PC may have set error */
881 error = 0;
882 }
883
884 /*
James Bottomleya621bac2016-05-13 12:04:06 -0700885 * special case: failed zero length commands always need to
886 * drop down into the retry code. Otherwise, if we finished
887 * all bytes in the request we are done now.
James Bottomleyd6b0c532006-07-02 10:06:28 -0500888 */
James Bottomleya621bac2016-05-13 12:04:06 -0700889 if (!(blk_rq_bytes(req) == 0 && error) &&
890 !scsi_end_request(req, error, good_bytes, 0))
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800891 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200892
893 /*
894 * Kill remainder if no retrys.
895 */
896 if (error && scsi_noretry_cmd(cmd)) {
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800897 if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
898 BUG();
899 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200900 }
901
902 /*
903 * If there had been no error, but we have leftover bytes in the
904 * requeues just queue the command up again.
905 */
906 if (result == 0)
907 goto requeue;
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700908
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100909 error = __scsi_error_from_host_byte(cmd, result);
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500910
Alan Sternb60af5b2008-11-03 15:56:47 -0500911 if (host_byte(result) == DID_RESET) {
912 /* Third party bus reset or reset for error recovery
913 * reasons. Just retry the command and see what
914 * happens.
915 */
916 action = ACTION_RETRY;
917 } else if (sense_valid && !sense_deferred) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 switch (sshdr.sense_key) {
919 case UNIT_ATTENTION:
920 if (cmd->device->removable) {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700921 /* Detected disc change. Set a bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * and quietly refuse further access.
923 */
924 cmd->device->changed = 1;
Alan Sternb60af5b2008-11-03 15:56:47 -0500925 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 } else {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700927 /* Must have been a power glitch, or a
928 * bus reset. Could not have been a
929 * media change, so we just retry the
Alan Sternb60af5b2008-11-03 15:56:47 -0500930 * command and see what happens.
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700931 */
Alan Sternb60af5b2008-11-03 15:56:47 -0500932 action = ACTION_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934 break;
935 case ILLEGAL_REQUEST:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700936 /* If we had an ILLEGAL REQUEST returned, then
937 * we may have performed an unsupported
938 * command. The only thing this should be
939 * would be a ten byte read where only a six
940 * byte read was supported. Also, on a system
941 * where READ CAPACITY failed, we may have
942 * read past the end of the disk.
943 */
Jens Axboe26a68012005-11-29 21:03:34 +0100944 if ((cmd->device->use_10_for_rw &&
945 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 (cmd->cmnd[0] == READ_10 ||
947 cmd->cmnd[0] == WRITE_10)) {
Alan Sternb60af5b2008-11-03 15:56:47 -0500948 /* This will issue a new 6-byte command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 cmd->device->use_10_for_rw = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500950 action = ACTION_REPREP;
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500951 } else if (sshdr.asc == 0x10) /* DIX */ {
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500952 action = ACTION_FAIL;
953 error = -EILSEQ;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500954 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
Martin K. Petersen5db44862012-09-18 12:19:32 -0400955 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500956 action = ACTION_FAIL;
Martin K. Petersen66a651a2012-02-13 15:38:22 -0500957 error = -EREMOTEIO;
Alan Sternb60af5b2008-11-03 15:56:47 -0500958 } else
959 action = ACTION_FAIL;
960 break;
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400961 case ABORTED_COMMAND:
James Bottomley126c09822009-02-19 21:48:54 +0000962 action = ACTION_FAIL;
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200963 if (sshdr.asc == 0x10) /* DIF */
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500964 error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 break;
966 case NOT_READY:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700967 /* If the device is in the process of becoming
James Bottomleyf3e93f72006-04-25 16:48:30 -0500968 * ready, or has a temporary blockage, retry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
James Bottomleyf3e93f72006-04-25 16:48:30 -0500970 if (sshdr.asc == 0x04) {
971 switch (sshdr.ascq) {
972 case 0x01: /* becoming ready */
973 case 0x04: /* format in progress */
974 case 0x05: /* rebuild in progress */
975 case 0x06: /* recalculation in progress */
976 case 0x07: /* operation in progress */
977 case 0x08: /* Long write in progress */
978 case 0x09: /* self test in progress */
Martin K. Petersend8705f12009-11-26 12:00:41 -0500979 case 0x14: /* space allocation in progress */
Alan Sternb60af5b2008-11-03 15:56:47 -0500980 action = ACTION_DELAYED_RETRY;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500981 break;
Alan Stern3dbf6a52008-12-15 10:31:28 -0500982 default:
Alan Stern3dbf6a52008-12-15 10:31:28 -0500983 action = ACTION_FAIL;
984 break;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500985 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200986 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500987 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 case VOLUME_OVERFLOW:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700990 /* See SSC3rXX or current. */
Alan Sternb60af5b2008-11-03 15:56:47 -0500991 action = ACTION_FAIL;
992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 default:
Alan Sternb60af5b2008-11-03 15:56:47 -0500994 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
996 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200997 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500998 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500999
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +09001000 if (action != ACTION_FAIL &&
Maurizio Lombardie6c11db2014-07-10 09:41:53 +02001001 time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +09001002 action = ACTION_FAIL;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +09001003
Alan Sternb60af5b2008-11-03 15:56:47 -05001004 switch (action) {
1005 case ACTION_FAIL:
1006 /* Give up and fail the remainder of the request */
Christoph Hellwige8064022016-10-20 15:12:13 +02001007 if (!(req->rq_flags & RQF_QUIET)) {
Hannes Reineckef1569ff2014-10-24 14:27:07 +02001008 static DEFINE_RATELIMIT_STATE(_rs,
1009 DEFAULT_RATELIMIT_INTERVAL,
1010 DEFAULT_RATELIMIT_BURST);
1011
1012 if (unlikely(scsi_logging_level))
1013 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
1014 SCSI_LOG_MLCOMPLETE_BITS);
1015
1016 /*
1017 * if logging is enabled the failure will be printed
1018 * in scsi_log_completion(), so avoid duplicate messages
1019 */
1020 if (!level && __ratelimit(&_rs)) {
1021 scsi_print_result(cmd, NULL, FAILED);
1022 if (driver_byte(result) & DRIVER_SENSE)
1023 scsi_print_sense(cmd);
1024 scsi_print_command(cmd);
1025 }
James Bottomley3173d8c2005-09-04 11:32:05 -05001026 }
Christoph Hellwigf6d47e72014-02-16 06:16:13 -08001027 if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
1028 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +02001029 /*FALLTHRU*/
Alan Sternb60af5b2008-11-03 15:56:47 -05001030 case ACTION_REPREP:
Christoph Hellwigbc85dc52014-05-01 16:51:03 +02001031 requeue:
Alan Sternb60af5b2008-11-03 15:56:47 -05001032 /* Unprep the request and put it back at the head of the queue.
1033 * A new command will be prepared and issued.
1034 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01001035 if (q->mq_ops) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001036 cmd->request->rq_flags &= ~RQF_DONTPREP;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001037 scsi_mq_uninit_cmd(cmd);
1038 scsi_mq_requeue_cmd(cmd);
1039 } else {
1040 scsi_release_buffers(cmd);
1041 scsi_requeue_command(q, cmd);
1042 }
Alan Sternb60af5b2008-11-03 15:56:47 -05001043 break;
1044 case ACTION_RETRY:
1045 /* Retry the same command immediately */
James Bottomley4f5299a2009-01-02 10:42:21 -06001046 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -05001047 break;
1048 case ACTION_DELAYED_RETRY:
1049 /* Retry the same command after a delay */
James Bottomley4f5299a2009-01-02 10:42:21 -06001050 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -05001051 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001055static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001057 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001060 * If sg table allocation fails, requeue request later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001062 if (unlikely(sg_alloc_table_chained(&sdb->table,
1063 blk_rq_nr_phys_segments(req), sdb->table.sgl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return BLKPREP_DEFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /*
1067 * Next, walk the list, and fill in the addresses and sizes of
1068 * each segment.
1069 */
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001070 count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
1071 BUG_ON(count > sdb->table.nents);
1072 sdb->table.nents = count;
Christoph Hellwigfd102b12017-01-13 12:29:11 +01001073 sdb->length = blk_rq_payload_bytes(req);
Rusty Russell4a03d902007-11-19 11:28:48 +11001074 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001076
1077/*
1078 * Function: scsi_init_io()
1079 *
1080 * Purpose: SCSI I/O initialize function.
1081 *
1082 * Arguments: cmd - Command descriptor we wish to initialize
1083 *
1084 * Returns: 0 on success
1085 * BLKPREP_DEFER if the failure is retryable
1086 * BLKPREP_KILL if the failure is fatal
1087 */
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001088int scsi_init_io(struct scsi_cmnd *cmd)
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001089{
Christoph Hellwig5e012aa2014-04-15 12:24:55 +02001090 struct scsi_device *sdev = cmd->device;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001091 struct request *rq = cmd->request;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001092 bool is_mq = (rq->mq_ctx != NULL);
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001093 int error;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001094
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001095 BUG_ON(!blk_rq_nr_phys_segments(rq));
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001096
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001097 error = scsi_init_sgtable(rq, &cmd->sdb);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001098 if (error)
1099 goto err_exit;
1100
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001101 if (blk_bidi_rq(rq)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001102 if (!rq->q->mq_ops) {
1103 struct scsi_data_buffer *bidi_sdb =
1104 kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC);
1105 if (!bidi_sdb) {
1106 error = BLKPREP_DEFER;
1107 goto err_exit;
1108 }
1109
1110 rq->next_rq->special = bidi_sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001111 }
1112
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001113 error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001114 if (error)
1115 goto err_exit;
1116 }
1117
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001118 if (blk_integrity_rq(rq)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001119 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1120 int ivecs, count;
1121
Ewan D. Milne91724c22015-01-15 10:02:12 -05001122 if (prot_sdb == NULL) {
1123 /*
1124 * This can happen if someone (e.g. multipath)
1125 * queues a command to a device on an adapter
1126 * that does not support DIX.
1127 */
1128 WARN_ON_ONCE(1);
1129 error = BLKPREP_KILL;
1130 goto err_exit;
1131 }
1132
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001133 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001134
Ming Lin001d63b2016-04-04 14:48:09 -07001135 if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
Ming Lin22cc3d42016-04-04 14:48:08 -07001136 prot_sdb->table.sgl)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001137 error = BLKPREP_DEFER;
1138 goto err_exit;
1139 }
1140
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001141 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001142 prot_sdb->table.sgl);
1143 BUG_ON(unlikely(count > ivecs));
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001144 BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q)));
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001145
1146 cmd->prot_sdb = prot_sdb;
1147 cmd->prot_sdb->table.nents = count;
1148 }
1149
Christoph Hellwigd2852032014-01-17 12:06:53 +01001150 return BLKPREP_OK;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001151err_exit:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001152 if (is_mq) {
1153 scsi_mq_free_sgtables(cmd);
1154 } else {
1155 scsi_release_buffers(cmd);
1156 cmd->request->special = NULL;
1157 scsi_put_command(cmd);
1158 put_device(&sdev->sdev_gendev);
1159 }
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001160 return error;
1161}
Boaz Harroshbb52d822007-12-13 16:14:27 -08001162EXPORT_SYMBOL(scsi_init_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Christoph Hellwig3b003152006-11-04 20:10:55 +01001164static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
1165 struct request *req)
1166{
1167 struct scsi_cmnd *cmd;
1168
1169 if (!req->special) {
Christoph Hellwig04796332014-02-20 14:20:55 -08001170 /* Bail if we can't get a reference to the device */
1171 if (!get_device(&sdev->sdev_gendev))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001172 return NULL;
Christoph Hellwig04796332014-02-20 14:20:55 -08001173
1174 cmd = scsi_get_command(sdev, GFP_ATOMIC);
1175 if (unlikely(!cmd)) {
1176 put_device(&sdev->sdev_gendev);
1177 return NULL;
1178 }
Christoph Hellwig3b003152006-11-04 20:10:55 +01001179 req->special = cmd;
1180 } else {
1181 cmd = req->special;
1182 }
1183
1184 /* pull a tag out of the request if we have one */
1185 cmd->tag = req->tag;
1186 cmd->request = req;
1187
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001188 cmd->cmnd = req->cmd;
Martin K. Petersen72f7d322011-03-08 02:03:59 -05001189 cmd->prot_op = SCSI_PROT_NORMAL;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001190
Christoph Hellwig3b003152006-11-04 20:10:55 +01001191 return cmd;
1192}
1193
Christoph Hellwig4f1e5762014-06-28 12:36:28 +02001194static int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
James Bottomley7b163182005-12-15 20:17:02 -06001195{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001196 struct scsi_cmnd *cmd = req->special;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001197
1198 /*
1199 * BLOCK_PC requests may transfer data, in which case they must
1200 * a bio attached to them. Or they might contain a SCSI command
1201 * that does not transfer data, in which case they may optionally
1202 * submit a request without an attached bio.
1203 */
1204 if (req->bio) {
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001205 int ret = scsi_init_io(cmd);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001206 if (unlikely(ret))
1207 return ret;
1208 } else {
Tejun Heob0790412009-05-07 22:24:42 +09001209 BUG_ON(blk_rq_bytes(req));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001210
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001211 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001212 }
James Bottomley7b163182005-12-15 20:17:02 -06001213
James Bottomley7b163182005-12-15 20:17:02 -06001214 cmd->cmd_len = req->cmd_len;
Tejun Heob0790412009-05-07 22:24:42 +09001215 cmd->transfersize = blk_rq_bytes(req);
James Bottomley7b163182005-12-15 20:17:02 -06001216 cmd->allowed = req->retries;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001217 return BLKPREP_OK;
1218}
1219
1220/*
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001221 * Setup a REQ_TYPE_FS command. These are simple request from filesystems
1222 * that still need to be translated to SCSI CDBs from the ULD.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001223 */
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001224static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001225{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001226 struct scsi_cmnd *cmd = req->special;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001227
Christoph Hellwigee14c672015-08-27 14:16:59 +02001228 if (unlikely(sdev->handler && sdev->handler->prep_fn)) {
1229 int ret = sdev->handler->prep_fn(sdev, req);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001230 if (ret != BLKPREP_OK)
1231 return ret;
1232 }
1233
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001234 memset(cmd->cmnd, 0, BLK_MAX_CDB);
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001235 return scsi_cmd_to_driver(cmd)->init_command(cmd);
James Bottomley7b163182005-12-15 20:17:02 -06001236}
James Bottomley7b163182005-12-15 20:17:02 -06001237
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001238static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req)
1239{
1240 struct scsi_cmnd *cmd = req->special;
1241
1242 if (!blk_rq_bytes(req))
1243 cmd->sc_data_direction = DMA_NONE;
1244 else if (rq_data_dir(req) == WRITE)
1245 cmd->sc_data_direction = DMA_TO_DEVICE;
1246 else
1247 cmd->sc_data_direction = DMA_FROM_DEVICE;
1248
1249 switch (req->cmd_type) {
1250 case REQ_TYPE_FS:
1251 return scsi_setup_fs_cmnd(sdev, req);
1252 case REQ_TYPE_BLOCK_PC:
1253 return scsi_setup_blk_pc_cmnd(sdev, req);
1254 default:
1255 return BLKPREP_KILL;
1256 }
1257}
1258
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001259static int
1260scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Christoph Hellwig3b003152006-11-04 20:10:55 +01001262 int ret = BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001265 * If the device is not in running state we will reject some
1266 * or all commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
Christoph Hellwig3b003152006-11-04 20:10:55 +01001269 switch (sdev->sdev_state) {
1270 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05001271 case SDEV_TRANSPORT_OFFLINE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001272 /*
1273 * If the device is offline we refuse to process any
1274 * commands. The device must be brought online
1275 * before trying any recovery commands.
1276 */
1277 sdev_printk(KERN_ERR, sdev,
1278 "rejecting I/O to offline device\n");
1279 ret = BLKPREP_KILL;
1280 break;
1281 case SDEV_DEL:
1282 /*
1283 * If the device is fully deleted, we refuse to
1284 * process any commands as well.
1285 */
James Bottomley9ccfc752005-10-02 11:45:08 -05001286 sdev_printk(KERN_ERR, sdev,
1287 "rejecting I/O to dead device\n");
Christoph Hellwig3b003152006-11-04 20:10:55 +01001288 ret = BLKPREP_KILL;
1289 break;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001290 case SDEV_BLOCK:
James Bottomley6f4267e2008-08-22 16:53:31 -05001291 case SDEV_CREATED_BLOCK:
Bart Van Asschebba0bdd2015-03-04 10:31:47 +01001292 ret = BLKPREP_DEFER;
1293 break;
1294 case SDEV_QUIESCE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001295 /*
1296 * If the devices is blocked we defer normal commands.
1297 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001298 if (!(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001299 ret = BLKPREP_DEFER;
1300 break;
1301 default:
1302 /*
1303 * For any other not fully online state we only allow
1304 * special commands. In particular any user initiated
1305 * command is not allowed.
1306 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001307 if (!(req->rq_flags & RQF_PREEMPT))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001308 ret = BLKPREP_KILL;
1309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001312 return ret;
1313}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001315static int
1316scsi_prep_return(struct request_queue *q, struct request *req, int ret)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001317{
1318 struct scsi_device *sdev = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Christoph Hellwig3b003152006-11-04 20:10:55 +01001320 switch (ret) {
1321 case BLKPREP_KILL:
jiangyiwene1cd3912016-02-16 20:14:13 +08001322 case BLKPREP_INVALID:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001323 req->errors = DID_NO_CONNECT << 16;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001324 /* release the command and kill it */
1325 if (req->special) {
1326 struct scsi_cmnd *cmd = req->special;
1327 scsi_release_buffers(cmd);
1328 scsi_put_command(cmd);
Christoph Hellwig68c03d92014-04-15 12:24:56 +02001329 put_device(&sdev->sdev_gendev);
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001330 req->special = NULL;
1331 }
Christoph Hellwig3b003152006-11-04 20:10:55 +01001332 break;
1333 case BLKPREP_DEFER:
1334 /*
Tejun Heo9934c8c2009-05-08 11:54:16 +09001335 * If we defer, the blk_peek_request() returns NULL, but the
Jens Axboea488e742010-04-16 21:13:15 +02001336 * queue must be restarted, so we schedule a callback to happen
1337 * shortly.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001338 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001339 if (atomic_read(&sdev->device_busy) == 0)
Jens Axboea488e742010-04-16 21:13:15 +02001340 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001341 break;
1342 default:
Christoph Hellwige8064022016-10-20 15:12:13 +02001343 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Christoph Hellwig3b003152006-11-04 20:10:55 +01001346 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001348
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001349static int scsi_prep_fn(struct request_queue *q, struct request *req)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001350{
1351 struct scsi_device *sdev = q->queuedata;
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001352 struct scsi_cmnd *cmd;
1353 int ret;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001354
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001355 ret = scsi_prep_state_check(sdev, req);
1356 if (ret != BLKPREP_OK)
1357 goto out;
1358
1359 cmd = scsi_get_cmd_from_req(sdev, req);
1360 if (unlikely(!cmd)) {
1361 ret = BLKPREP_DEFER;
1362 goto out;
1363 }
1364
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001365 ret = scsi_setup_cmnd(sdev, req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001366out:
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001367 return scsi_prep_return(q, req, ret);
1368}
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001369
1370static void scsi_unprep_fn(struct request_queue *q, struct request *req)
1371{
Christoph Hellwigd2852032014-01-17 12:06:53 +01001372 scsi_uninit_cmd(req->special);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375/*
1376 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1377 * return 0.
1378 *
1379 * Called with the queue_lock held.
1380 */
1381static inline int scsi_dev_queue_ready(struct request_queue *q,
1382 struct scsi_device *sdev)
1383{
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001384 unsigned int busy;
1385
1386 busy = atomic_inc_return(&sdev->device_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001387 if (atomic_read(&sdev->device_blocked)) {
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001388 if (busy)
1389 goto out_dec;
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /*
1392 * unblock after device_blocked iterates to zero
1393 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001394 if (atomic_dec_return(&sdev->device_blocked) > 0) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001395 /*
1396 * For the MQ case we take care of this in the caller.
1397 */
1398 if (!q->mq_ops)
1399 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001400 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001402 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1403 "unblocking device at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001405
1406 if (busy >= sdev->queue_depth)
1407 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 return 1;
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001410out_dec:
1411 atomic_dec(&sdev->device_busy);
1412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Mike Christief0c0a372008-08-17 15:24:38 -05001415/*
1416 * scsi_target_queue_ready: checks if there we can send commands to target
1417 * @sdev: scsi device on starget to check.
Mike Christief0c0a372008-08-17 15:24:38 -05001418 */
1419static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1420 struct scsi_device *sdev)
1421{
1422 struct scsi_target *starget = scsi_target(sdev);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001423 unsigned int busy;
Mike Christief0c0a372008-08-17 15:24:38 -05001424
1425 if (starget->single_lun) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001426 spin_lock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001427 if (starget->starget_sdev_user &&
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001428 starget->starget_sdev_user != sdev) {
1429 spin_unlock_irq(shost->host_lock);
1430 return 0;
1431 }
Mike Christief0c0a372008-08-17 15:24:38 -05001432 starget->starget_sdev_user = sdev;
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001433 spin_unlock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001434 }
1435
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001436 if (starget->can_queue <= 0)
1437 return 1;
1438
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001439 busy = atomic_inc_return(&starget->target_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001440 if (atomic_read(&starget->target_blocked) > 0) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001441 if (busy)
1442 goto starved;
1443
Mike Christief0c0a372008-08-17 15:24:38 -05001444 /*
1445 * unblock after target_blocked iterates to zero
1446 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001447 if (atomic_dec_return(&starget->target_blocked) > 0)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001448 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001449
1450 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1451 "unblocking target at zero depth\n"));
Mike Christief0c0a372008-08-17 15:24:38 -05001452 }
1453
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001454 if (busy >= starget->can_queue)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001455 goto starved;
Mike Christief0c0a372008-08-17 15:24:38 -05001456
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001457 return 1;
1458
1459starved:
1460 spin_lock_irq(shost->host_lock);
1461 list_move_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001462 spin_unlock_irq(shost->host_lock);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001463out_dec:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001464 if (starget->can_queue > 0)
1465 atomic_dec(&starget->target_busy);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001466 return 0;
Mike Christief0c0a372008-08-17 15:24:38 -05001467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/*
1470 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1471 * return 0. We must end up running the queue again whenever 0 is
1472 * returned, else IO can hang.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
1474static inline int scsi_host_queue_ready(struct request_queue *q,
1475 struct Scsi_Host *shost,
1476 struct scsi_device *sdev)
1477{
Christoph Hellwig74665012014-01-22 15:29:29 +01001478 unsigned int busy;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001479
James Bottomley939647e2005-09-18 15:05:20 -05001480 if (scsi_host_in_recovery(shost))
Christoph Hellwig74665012014-01-22 15:29:29 +01001481 return 0;
1482
1483 busy = atomic_inc_return(&shost->host_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001484 if (atomic_read(&shost->host_blocked) > 0) {
Christoph Hellwig74665012014-01-22 15:29:29 +01001485 if (busy)
1486 goto starved;
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /*
1489 * unblock after host_blocked iterates to zero
1490 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001491 if (atomic_dec_return(&shost->host_blocked) > 0)
Christoph Hellwig74665012014-01-22 15:29:29 +01001492 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001493
1494 SCSI_LOG_MLQUEUE(3,
1495 shost_printk(KERN_INFO, shost,
1496 "unblocking host at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
Christoph Hellwig74665012014-01-22 15:29:29 +01001498
1499 if (shost->can_queue > 0 && busy >= shost->can_queue)
1500 goto starved;
1501 if (shost->host_self_blocked)
1502 goto starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 /* We're OK to process the command, so we can't be starved */
Christoph Hellwig74665012014-01-22 15:29:29 +01001505 if (!list_empty(&sdev->starved_entry)) {
1506 spin_lock_irq(shost->host_lock);
1507 if (!list_empty(&sdev->starved_entry))
1508 list_del_init(&sdev->starved_entry);
1509 spin_unlock_irq(shost->host_lock);
1510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Christoph Hellwig74665012014-01-22 15:29:29 +01001512 return 1;
1513
1514starved:
1515 spin_lock_irq(shost->host_lock);
1516 if (list_empty(&sdev->starved_entry))
1517 list_add_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001518 spin_unlock_irq(shost->host_lock);
Christoph Hellwig74665012014-01-22 15:29:29 +01001519out_dec:
1520 atomic_dec(&shost->host_busy);
1521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524/*
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001525 * Busy state exporting function for request stacking drivers.
1526 *
1527 * For efficiency, no lock is taken to check the busy state of
1528 * shost/starget/sdev, since the returned value is not guaranteed and
1529 * may be changed after request stacking drivers call the function,
1530 * regardless of taking lock or not.
1531 *
Bart Van Assche67bd9412012-06-29 15:33:22 +00001532 * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
1533 * needs to return 'not busy'. Otherwise, request stacking drivers
1534 * may hold requests forever.
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001535 */
1536static int scsi_lld_busy(struct request_queue *q)
1537{
1538 struct scsi_device *sdev = q->queuedata;
1539 struct Scsi_Host *shost;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001540
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001541 if (blk_queue_dying(q))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001542 return 0;
1543
1544 shost = sdev->host;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001545
Jun'ichi Nomurab7e94a12012-05-22 18:57:17 +09001546 /*
1547 * Ignore host/starget busy state.
1548 * Since block layer does not have a concept of fairness across
1549 * multiple queues, congestion of host/starget needs to be handled
1550 * in SCSI layer.
1551 */
1552 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001553 return 1;
1554
1555 return 0;
1556}
1557
1558/*
James Bottomleye91442b2005-09-09 10:44:16 -05001559 * Kill a request for a dead device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 */
Jens Axboe165125e2007-07-24 09:28:11 +02001561static void scsi_kill_request(struct request *req, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
James Bottomleye91442b2005-09-09 10:44:16 -05001563 struct scsi_cmnd *cmd = req->special;
Jiri Slaby03b14702009-09-23 16:15:35 +02001564 struct scsi_device *sdev;
1565 struct scsi_target *starget;
1566 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Tejun Heo9934c8c2009-05-08 11:54:16 +09001568 blk_start_request(req);
James Bottomley788ce432005-09-09 13:40:23 -05001569
Hannes Reinecke74571812011-11-09 08:39:24 +01001570 scmd_printk(KERN_INFO, cmd, "killing request\n");
1571
Jiri Slaby03b14702009-09-23 16:15:35 +02001572 sdev = cmd->device;
1573 starget = scsi_target(sdev);
1574 shost = sdev->host;
James Bottomleye91442b2005-09-09 10:44:16 -05001575 scsi_init_cmd_errh(cmd);
1576 cmd->result = DID_NO_CONNECT << 16;
1577 atomic_inc(&cmd->device->iorequest_cnt);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001578
1579 /*
1580 * SCSI request completion path will do scsi_device_unbusy(),
1581 * bump busy counts. To bump the counters, we need to dance
1582 * with the locks as normal issue path does.
1583 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001584 atomic_inc(&sdev->device_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +01001585 atomic_inc(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001586 if (starget->can_queue > 0)
1587 atomic_inc(&starget->target_busy);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001588
Jens Axboe242f9dc2008-09-14 05:55:09 -07001589 blk_complete_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Jens Axboe1aea6432006-01-09 16:03:03 +01001592static void scsi_softirq_done(struct request *rq)
1593{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001594 struct scsi_cmnd *cmd = rq->special;
1595 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
Jens Axboe1aea6432006-01-09 16:03:03 +01001596 int disposition;
1597
1598 INIT_LIST_HEAD(&cmd->eh_entry);
1599
Jens Axboe242f9dc2008-09-14 05:55:09 -07001600 atomic_inc(&cmd->device->iodone_cnt);
1601 if (cmd->result)
1602 atomic_inc(&cmd->device->ioerr_cnt);
1603
Jens Axboe1aea6432006-01-09 16:03:03 +01001604 disposition = scsi_decide_disposition(cmd);
1605 if (disposition != SUCCESS &&
1606 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
1607 sdev_printk(KERN_ERR, cmd->device,
1608 "timing out command, waited %lus\n",
1609 wait_for/HZ);
1610 disposition = SUCCESS;
1611 }
Hannes Reinecke91921e02014-06-25 16:39:59 +02001612
Jens Axboe1aea6432006-01-09 16:03:03 +01001613 scsi_log_completion(cmd, disposition);
1614
1615 switch (disposition) {
1616 case SUCCESS:
1617 scsi_finish_command(cmd);
1618 break;
1619 case NEEDS_RETRY:
Christoph Hellwig596f4822007-01-02 12:56:00 +01001620 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
Jens Axboe1aea6432006-01-09 16:03:03 +01001621 break;
1622 case ADD_TO_MLQUEUE:
1623 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1624 break;
1625 default:
1626 if (!scsi_eh_scmd_add(cmd, 0))
1627 scsi_finish_command(cmd);
1628 }
1629}
1630
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001631/**
Christoph Hellwig82042a22014-09-05 18:23:07 -07001632 * scsi_dispatch_command - Dispatch a command to the low-level driver.
1633 * @cmd: command block we are dispatching.
1634 *
1635 * Return: nonzero return request was rejected and device's queue needs to be
1636 * plugged.
1637 */
1638static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
1639{
1640 struct Scsi_Host *host = cmd->device->host;
1641 int rtn = 0;
1642
1643 atomic_inc(&cmd->device->iorequest_cnt);
1644
1645 /* check if the device is still usable */
1646 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
1647 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
1648 * returns an immediate error upwards, and signals
1649 * that the device is no longer present */
1650 cmd->result = DID_NO_CONNECT << 16;
1651 goto done;
1652 }
1653
1654 /* Check to see if the scsi lld made this device blocked. */
1655 if (unlikely(scsi_device_blocked(cmd->device))) {
1656 /*
1657 * in blocked state, the command is just put back on
1658 * the device queue. The suspend state has already
1659 * blocked the queue so future requests should not
1660 * occur until the device transitions out of the
1661 * suspend state.
1662 */
1663 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1664 "queuecommand : device blocked\n"));
1665 return SCSI_MLQUEUE_DEVICE_BUSY;
1666 }
1667
1668 /* Store the LUN value in cmnd, if needed. */
1669 if (cmd->device->lun_in_cdb)
1670 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
1671 (cmd->device->lun << 5 & 0xe0);
1672
1673 scsi_log_send(cmd);
1674
1675 /*
1676 * Before we queue this command, check if the command
1677 * length exceeds what the host adapter can handle.
1678 */
1679 if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
1680 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1681 "queuecommand : command too long. "
1682 "cdb_size=%d host->max_cmd_len=%d\n",
1683 cmd->cmd_len, cmd->device->host->max_cmd_len));
1684 cmd->result = (DID_ABORT << 16);
1685 goto done;
1686 }
1687
1688 if (unlikely(host->shost_state == SHOST_DEL)) {
1689 cmd->result = (DID_NO_CONNECT << 16);
1690 goto done;
1691
1692 }
1693
1694 trace_scsi_dispatch_cmd_start(cmd);
1695 rtn = host->hostt->queuecommand(host, cmd);
1696 if (rtn) {
1697 trace_scsi_dispatch_cmd_error(cmd, rtn);
1698 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
1699 rtn != SCSI_MLQUEUE_TARGET_BUSY)
1700 rtn = SCSI_MLQUEUE_HOST_BUSY;
1701
1702 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1703 "queuecommand : request rejected\n"));
1704 }
1705
1706 return rtn;
1707 done:
1708 cmd->scsi_done(cmd);
1709 return 0;
1710}
1711
1712/**
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001713 * scsi_done - Invoke completion on finished SCSI command.
1714 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
1715 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
1716 *
1717 * Description: This function is the mid-level's (SCSI Core) interrupt routine,
1718 * which regains ownership of the SCSI command (de facto) from a LLDD, and
1719 * calls blk_complete_request() for further processing.
1720 *
1721 * This function is interrupt context safe.
1722 */
1723static void scsi_done(struct scsi_cmnd *cmd)
1724{
1725 trace_scsi_dispatch_cmd_done(cmd);
1726 blk_complete_request(cmd->request);
1727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/*
1730 * Function: scsi_request_fn()
1731 *
1732 * Purpose: Main strategy routine for SCSI.
1733 *
1734 * Arguments: q - Pointer to actual queue.
1735 *
1736 * Returns: Nothing
1737 *
1738 * Lock status: IO request lock assumed to be held when called.
1739 */
1740static void scsi_request_fn(struct request_queue *q)
Bart Van Assche613be1f2014-02-20 14:20:56 -08001741 __releases(q->queue_lock)
1742 __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 struct scsi_device *sdev = q->queuedata;
1745 struct Scsi_Host *shost;
1746 struct scsi_cmnd *cmd;
1747 struct request *req;
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /*
1750 * To start with, we keep looping until the queue is empty, or until
1751 * the host is no longer able to accept any more requests.
1752 */
1753 shost = sdev->host;
Jens Axboea488e742010-04-16 21:13:15 +02001754 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 int rtn;
1756 /*
1757 * get next queueable request. We do this early to make sure
Hannes Reinecke91921e02014-06-25 16:39:59 +02001758 * that the request is fully prepared even if we cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 * accept it.
1760 */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001761 req = blk_peek_request(q);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001762 if (!req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 break;
1764
1765 if (unlikely(!scsi_device_online(sdev))) {
James Bottomley9ccfc752005-10-02 11:45:08 -05001766 sdev_printk(KERN_ERR, sdev,
1767 "rejecting I/O to offline device\n");
James Bottomleye91442b2005-09-09 10:44:16 -05001768 scsi_kill_request(req, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 continue;
1770 }
1771
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001772 if (!scsi_dev_queue_ready(q, sdev))
1773 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 /*
1776 * Remove the request from the request list.
1777 */
1778 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
Tejun Heo9934c8c2009-05-08 11:54:16 +09001779 blk_start_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001781 spin_unlock_irq(q->queue_lock);
James Bottomleye91442b2005-09-09 10:44:16 -05001782 cmd = req->special;
1783 if (unlikely(cmd == NULL)) {
1784 printk(KERN_CRIT "impossible request in %s.\n"
1785 "please mail a stack trace to "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001786 "linux-scsi@vger.kernel.org\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001787 __func__);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001788 blk_dump_rq_flags(req, "foo");
James Bottomleye91442b2005-09-09 10:44:16 -05001789 BUG();
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Mike Christieecefe8a2008-07-11 19:50:35 -05001792 /*
1793 * We hit this when the driver is using a host wide
1794 * tag map. For device level tag maps the queue_depth check
1795 * in the device ready fn would prevent us from trying
1796 * to allocate a tag. Since the map is a shared host resource
1797 * we add the dev to the starved list so it eventually gets
1798 * a run when a tag is freed.
1799 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001800 if (blk_queue_tagged(q) && !(req->rq_flags & RQF_QUEUED)) {
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001801 spin_lock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001802 if (list_empty(&sdev->starved_entry))
1803 list_add_tail(&sdev->starved_entry,
1804 &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001805 spin_unlock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001806 goto not_ready;
1807 }
1808
Mike Christief0c0a372008-08-17 15:24:38 -05001809 if (!scsi_target_queue_ready(shost, sdev))
1810 goto not_ready;
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (!scsi_host_queue_ready(q, shost, sdev))
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001813 goto host_not_ready;
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001814
1815 if (sdev->simple_tags)
1816 cmd->flags |= SCMD_TAGGED;
1817 else
1818 cmd->flags &= ~SCMD_TAGGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /*
1821 * Finally, initialize any error handling parameters, and set up
1822 * the timers for timeouts.
1823 */
1824 scsi_init_cmd_errh(cmd);
1825
1826 /*
1827 * Dispatch the command to the low-level driver.
1828 */
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001829 cmd->scsi_done = scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 rtn = scsi_dispatch_cmd(cmd);
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001831 if (rtn) {
1832 scsi_queue_insert(cmd, rtn);
1833 spin_lock_irq(q->queue_lock);
Jens Axboea488e742010-04-16 21:13:15 +02001834 goto out_delay;
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001835 }
1836 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838
Bart Van Assche613be1f2014-02-20 14:20:56 -08001839 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001841 host_not_ready:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001842 if (scsi_target(sdev)->can_queue > 0)
1843 atomic_dec(&scsi_target(sdev)->target_busy);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001844 not_ready:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 /*
1846 * lock q, handle tag, requeue req, and decrement device_busy. We
1847 * must return with queue_lock held.
1848 *
1849 * Decrementing device_busy without checking it is OK, as all such
1850 * cases (host limits or settings) should run the queue at some
1851 * later time.
1852 */
1853 spin_lock_irq(q->queue_lock);
1854 blk_requeue_request(q, req);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001855 atomic_dec(&sdev->device_busy);
Jens Axboea488e742010-04-16 21:13:15 +02001856out_delay:
Guenter Roeck480cadc2014-08-10 05:54:25 -07001857 if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
Jens Axboea488e742010-04-16 21:13:15 +02001858 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Christoph Hellwigd2852032014-01-17 12:06:53 +01001861static inline int prep_to_mq(int ret)
1862{
1863 switch (ret) {
1864 case BLKPREP_OK:
Omar Sandoval2868f132016-11-15 11:11:59 -08001865 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001866 case BLKPREP_DEFER:
1867 return BLK_MQ_RQ_QUEUE_BUSY;
1868 default:
1869 return BLK_MQ_RQ_QUEUE_ERROR;
1870 }
1871}
1872
1873static int scsi_mq_prep_fn(struct request *req)
1874{
1875 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1876 struct scsi_device *sdev = req->q->queuedata;
1877 struct Scsi_Host *shost = sdev->host;
1878 unsigned char *sense_buf = cmd->sense_buffer;
1879 struct scatterlist *sg;
1880
1881 memset(cmd, 0, sizeof(struct scsi_cmnd));
1882
1883 req->special = cmd;
1884
1885 cmd->request = req;
1886 cmd->device = sdev;
1887 cmd->sense_buffer = sense_buf;
1888
1889 cmd->tag = req->tag;
1890
Christoph Hellwigd2852032014-01-17 12:06:53 +01001891 cmd->cmnd = req->cmd;
1892 cmd->prot_op = SCSI_PROT_NORMAL;
1893
1894 INIT_LIST_HEAD(&cmd->list);
1895 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1896 cmd->jiffies_at_alloc = jiffies;
1897
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +05301898 if (shost->use_cmd_list) {
1899 spin_lock_irq(&sdev->list_lock);
1900 list_add_tail(&cmd->list, &sdev->cmd_list);
1901 spin_unlock_irq(&sdev->list_lock);
1902 }
Christoph Hellwigd2852032014-01-17 12:06:53 +01001903
1904 sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
1905 cmd->sdb.table.sgl = sg;
1906
1907 if (scsi_host_get_prot(shost)) {
1908 cmd->prot_sdb = (void *)sg +
Tony Battersby120bb3e2014-12-08 17:20:52 -05001909 min_t(unsigned int,
Ming Lin65e86172016-04-04 14:48:10 -07001910 shost->sg_tablesize, SG_CHUNK_SIZE) *
Tony Battersby120bb3e2014-12-08 17:20:52 -05001911 sizeof(struct scatterlist);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001912 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
1913
1914 cmd->prot_sdb->table.sgl =
1915 (struct scatterlist *)(cmd->prot_sdb + 1);
1916 }
1917
1918 if (blk_bidi_rq(req)) {
1919 struct request *next_rq = req->next_rq;
1920 struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq);
1921
1922 memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer));
1923 bidi_sdb->table.sgl =
1924 (struct scatterlist *)(bidi_sdb + 1);
1925
1926 next_rq->special = bidi_sdb;
1927 }
1928
Christoph Hellwigfe052522014-09-22 15:59:31 +02001929 blk_mq_start_request(req);
1930
Christoph Hellwigd2852032014-01-17 12:06:53 +01001931 return scsi_setup_cmnd(sdev, req);
1932}
1933
1934static void scsi_mq_done(struct scsi_cmnd *cmd)
1935{
1936 trace_scsi_dispatch_cmd_done(cmd);
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001937 blk_mq_complete_request(cmd->request, cmd->request->errors);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001938}
1939
Jens Axboe74c45052014-10-29 11:14:52 -06001940static int scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
1941 const struct blk_mq_queue_data *bd)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001942{
Jens Axboe74c45052014-10-29 11:14:52 -06001943 struct request *req = bd->rq;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001944 struct request_queue *q = req->q;
1945 struct scsi_device *sdev = q->queuedata;
1946 struct Scsi_Host *shost = sdev->host;
1947 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1948 int ret;
1949 int reason;
1950
1951 ret = prep_to_mq(scsi_prep_state_check(sdev, req));
Omar Sandoval2868f132016-11-15 11:11:59 -08001952 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001953 goto out;
1954
1955 ret = BLK_MQ_RQ_QUEUE_BUSY;
1956 if (!get_device(&sdev->sdev_gendev))
1957 goto out;
1958
1959 if (!scsi_dev_queue_ready(q, sdev))
1960 goto out_put_device;
1961 if (!scsi_target_queue_ready(shost, sdev))
1962 goto out_dec_device_busy;
1963 if (!scsi_host_queue_ready(q, shost, sdev))
1964 goto out_dec_target_busy;
1965
Christoph Hellwigfe052522014-09-22 15:59:31 +02001966
Christoph Hellwige8064022016-10-20 15:12:13 +02001967 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001968 ret = prep_to_mq(scsi_mq_prep_fn(req));
Omar Sandoval2868f132016-11-15 11:11:59 -08001969 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001970 goto out_dec_host_busy;
Christoph Hellwige8064022016-10-20 15:12:13 +02001971 req->rq_flags |= RQF_DONTPREP;
Christoph Hellwigfe052522014-09-22 15:59:31 +02001972 } else {
1973 blk_mq_start_request(req);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001974 }
1975
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001976 if (sdev->simple_tags)
1977 cmd->flags |= SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001978 else
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001979 cmd->flags &= ~SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001980
Christoph Hellwigd2852032014-01-17 12:06:53 +01001981 scsi_init_cmd_errh(cmd);
1982 cmd->scsi_done = scsi_mq_done;
1983
1984 reason = scsi_dispatch_cmd(cmd);
1985 if (reason) {
1986 scsi_set_blocked(cmd, reason);
1987 ret = BLK_MQ_RQ_QUEUE_BUSY;
1988 goto out_dec_host_busy;
1989 }
1990
1991 return BLK_MQ_RQ_QUEUE_OK;
1992
1993out_dec_host_busy:
1994 atomic_dec(&shost->host_busy);
1995out_dec_target_busy:
1996 if (scsi_target(sdev)->can_queue > 0)
1997 atomic_dec(&scsi_target(sdev)->target_busy);
1998out_dec_device_busy:
1999 atomic_dec(&sdev->device_busy);
2000out_put_device:
2001 put_device(&sdev->sdev_gendev);
2002out:
2003 switch (ret) {
2004 case BLK_MQ_RQ_QUEUE_BUSY:
Christoph Hellwigd2852032014-01-17 12:06:53 +01002005 if (atomic_read(&sdev->device_busy) == 0 &&
2006 !scsi_device_blocked(sdev))
2007 blk_mq_delay_queue(hctx, SCSI_QUEUE_DELAY);
2008 break;
2009 case BLK_MQ_RQ_QUEUE_ERROR:
2010 /*
2011 * Make sure to release all allocated ressources when
2012 * we hit an error, as we will never see this command
2013 * again.
2014 */
Christoph Hellwige8064022016-10-20 15:12:13 +02002015 if (req->rq_flags & RQF_DONTPREP)
Christoph Hellwigd2852032014-01-17 12:06:53 +01002016 scsi_mq_uninit_cmd(cmd);
2017 break;
2018 default:
2019 break;
2020 }
2021 return ret;
2022}
2023
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002024static enum blk_eh_timer_return scsi_timeout(struct request *req,
2025 bool reserved)
2026{
2027 if (reserved)
2028 return BLK_EH_RESET_TIMER;
2029 return scsi_times_out(req);
2030}
2031
Christoph Hellwigd2852032014-01-17 12:06:53 +01002032static int scsi_init_request(void *data, struct request *rq,
2033 unsigned int hctx_idx, unsigned int request_idx,
2034 unsigned int numa_node)
2035{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002036 struct Scsi_Host *shost = data;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002037 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2038
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002039 cmd->sense_buffer =
2040 scsi_alloc_sense_buffer(shost, GFP_KERNEL, numa_node);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002041 if (!cmd->sense_buffer)
2042 return -ENOMEM;
2043 return 0;
2044}
2045
2046static void scsi_exit_request(void *data, struct request *rq,
2047 unsigned int hctx_idx, unsigned int request_idx)
2048{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002049 struct Scsi_Host *shost = data;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002050 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
2051
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002052 scsi_free_sense_buffer(shost, cmd->sense_buffer);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002053}
2054
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002055static int scsi_map_queues(struct blk_mq_tag_set *set)
2056{
2057 struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
2058
2059 if (shost->hostt->map_queues)
2060 return shost->hostt->map_queues(shost);
2061 return blk_mq_map_queues(set);
2062}
2063
Christoph Hellwigf1bea552014-04-15 12:26:54 +02002064static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct device *host_dev;
2067 u64 bounce_limit = 0xffffffff;
2068
2069 if (shost->unchecked_isa_dma)
2070 return BLK_BOUNCE_ISA;
2071 /*
2072 * Platforms with virtual-DMA translation
2073 * hardware have no practical limit.
2074 */
2075 if (!PCI_DMA_BUS_IS_PHYS)
2076 return BLK_BOUNCE_ANY;
2077
2078 host_dev = scsi_get_device(shost);
2079 if (host_dev && host_dev->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +00002080 bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 return bounce_limit;
2083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Christoph Hellwigd2852032014-01-17 12:06:53 +01002085static void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Lin Ming6f381fa2012-04-12 13:50:38 +08002087 struct device *dev = shost->dma_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Jens Axboea8474ce2007-08-07 09:02:51 +02002089 /*
2090 * this limit is imposed by hardware restrictions
2091 */
Martin K. Petersen8a783622010-02-26 00:20:39 -05002092 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
Ming Lin65e86172016-04-04 14:48:10 -07002093 SG_MAX_SEGMENTS));
Jens Axboea8474ce2007-08-07 09:02:51 +02002094
Martin K. Petersen13f05c82010-09-10 20:50:10 +02002095 if (scsi_host_prot_dma(shost)) {
2096 shost->sg_prot_tablesize =
2097 min_not_zero(shost->sg_prot_tablesize,
2098 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
2099 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
2100 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
2101 }
2102
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002103 blk_queue_max_hw_sectors(q, shost->max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
2105 blk_queue_segment_boundary(q, shost->dma_boundary);
FUJITA Tomonori99c84db2008-02-04 22:28:17 -08002106 dma_set_seg_boundary(dev, shost->dma_boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
FUJITA Tomonori860ac562008-02-04 22:28:05 -08002108 blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (!shost->use_clustering)
Martin K. Petersene692cb62010-12-01 19:41:49 +01002111 q->limits.cluster = 0;
James Bottomley465ff312008-01-01 10:00:10 -06002112
2113 /*
2114 * set a reasonable default alignment on word boundaries: the
2115 * host and device may alter it using
2116 * blk_queue_update_dma_alignment() later.
2117 */
2118 blk_queue_dma_alignment(q, 0x03);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002119}
James Bottomley465ff312008-01-01 10:00:10 -06002120
Christoph Hellwigd2852032014-01-17 12:06:53 +01002121struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,
2122 request_fn_proc *request_fn)
2123{
2124 struct request_queue *q;
2125
2126 q = blk_init_queue(request_fn, NULL);
2127 if (!q)
2128 return NULL;
2129 __scsi_init_queue(shost, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return q;
2131}
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002132EXPORT_SYMBOL(__scsi_alloc_queue);
2133
2134struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
2135{
2136 struct request_queue *q;
2137
2138 q = __scsi_alloc_queue(sdev->host, scsi_request_fn);
2139 if (!q)
2140 return NULL;
2141
2142 blk_queue_prep_rq(q, scsi_prep_fn);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02002143 blk_queue_unprep_rq(q, scsi_unprep_fn);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002144 blk_queue_softirq_done(q, scsi_softirq_done);
Jens Axboe242f9dc2008-09-14 05:55:09 -07002145 blk_queue_rq_timed_out(q, scsi_times_out);
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04002146 blk_queue_lld_busy(q, scsi_lld_busy);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002147 return q;
2148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Christoph Hellwigd2852032014-01-17 12:06:53 +01002150static struct blk_mq_ops scsi_mq_ops = {
Christoph Hellwigd2852032014-01-17 12:06:53 +01002151 .queue_rq = scsi_queue_rq,
2152 .complete = scsi_softirq_done,
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002153 .timeout = scsi_timeout,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002154 .init_request = scsi_init_request,
2155 .exit_request = scsi_exit_request,
Christoph Hellwig2d9c5c22016-11-01 08:12:48 -06002156 .map_queues = scsi_map_queues,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002157};
2158
2159struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
2160{
2161 sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
2162 if (IS_ERR(sdev->request_queue))
2163 return NULL;
2164
2165 sdev->request_queue->queuedata = sdev;
2166 __scsi_init_queue(sdev->host, sdev->request_queue);
2167 return sdev->request_queue;
2168}
2169
2170int scsi_mq_setup_tags(struct Scsi_Host *shost)
2171{
2172 unsigned int cmd_size, sgl_size, tbl_size;
2173
2174 tbl_size = shost->sg_tablesize;
Ming Lin65e86172016-04-04 14:48:10 -07002175 if (tbl_size > SG_CHUNK_SIZE)
2176 tbl_size = SG_CHUNK_SIZE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002177 sgl_size = tbl_size * sizeof(struct scatterlist);
2178 cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
2179 if (scsi_host_get_prot(shost))
2180 cmd_size += sizeof(struct scsi_data_buffer) + sgl_size;
2181
2182 memset(&shost->tag_set, 0, sizeof(shost->tag_set));
2183 shost->tag_set.ops = &scsi_mq_ops;
Bart Van Asscheefec4b92014-10-30 14:45:36 +01002184 shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002185 shost->tag_set.queue_depth = shost->can_queue;
2186 shost->tag_set.cmd_size = cmd_size;
2187 shost->tag_set.numa_node = NUMA_NO_NODE;
2188 shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Shaohua Li24391c02015-01-23 14:18:00 -07002189 shost->tag_set.flags |=
2190 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002191 shost->tag_set.driver_data = shost;
2192
2193 return blk_mq_alloc_tag_set(&shost->tag_set);
2194}
2195
2196void scsi_mq_destroy_tags(struct Scsi_Host *shost)
2197{
2198 blk_mq_free_tag_set(&shost->tag_set);
2199}
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201/*
2202 * Function: scsi_block_requests()
2203 *
2204 * Purpose: Utility function used by low-level drivers to prevent further
2205 * commands from being queued to the device.
2206 *
2207 * Arguments: shost - Host in question
2208 *
2209 * Returns: Nothing
2210 *
2211 * Lock status: No locks are assumed held.
2212 *
2213 * Notes: There is no timer nor any other means by which the requests
2214 * get unblocked other than the low-level driver calling
2215 * scsi_unblock_requests().
2216 */
2217void scsi_block_requests(struct Scsi_Host *shost)
2218{
2219 shost->host_self_blocked = 1;
2220}
2221EXPORT_SYMBOL(scsi_block_requests);
2222
2223/*
2224 * Function: scsi_unblock_requests()
2225 *
2226 * Purpose: Utility function used by low-level drivers to allow further
2227 * commands from being queued to the device.
2228 *
2229 * Arguments: shost - Host in question
2230 *
2231 * Returns: Nothing
2232 *
2233 * Lock status: No locks are assumed held.
2234 *
2235 * Notes: There is no timer nor any other means by which the requests
2236 * get unblocked other than the low-level driver calling
2237 * scsi_unblock_requests().
2238 *
2239 * This is done as an API function so that changes to the
2240 * internals of the scsi mid-layer won't require wholesale
2241 * changes to drivers that use this feature.
2242 */
2243void scsi_unblock_requests(struct Scsi_Host *shost)
2244{
2245 shost->host_self_blocked = 0;
2246 scsi_run_host_queues(shost);
2247}
2248EXPORT_SYMBOL(scsi_unblock_requests);
2249
2250int __init scsi_init_queue(void)
2251{
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002252 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
2253 sizeof(struct scsi_data_buffer),
2254 0, 0, NULL);
2255 if (!scsi_sdb_cache) {
2256 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
FUJITA Tomonorif0787272008-12-14 01:23:45 +09002257 return -ENOMEM;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02002258 }
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return 0;
2261}
2262
2263void scsi_exit_queue(void)
2264{
Christoph Hellwig0a6ac4e2017-01-03 08:28:41 +03002265 kmem_cache_destroy(scsi_sense_cache);
2266 kmem_cache_destroy(scsi_sense_isadma_cache);
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002267 kmem_cache_destroy(scsi_sdb_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
James Bottomley5baba832006-03-18 14:10:35 -06002269
2270/**
2271 * scsi_mode_select - issue a mode select
2272 * @sdev: SCSI device to be queried
2273 * @pf: Page format bit (1 == standard, 0 == vendor specific)
2274 * @sp: Save page bit (0 == don't save, 1 == save)
2275 * @modepage: mode page being requested
2276 * @buffer: request buffer (may not be smaller than eight bytes)
2277 * @len: length of request buffer.
2278 * @timeout: command timeout
2279 * @retries: number of retries before failing
2280 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002281 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley5baba832006-03-18 14:10:35 -06002282 * must be SCSI_SENSE_BUFFERSIZE big.
2283 *
2284 * Returns zero if successful; negative error number or scsi
2285 * status on error
2286 *
2287 */
2288int
2289scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
2290 unsigned char *buffer, int len, int timeout, int retries,
2291 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2292{
2293 unsigned char cmd[10];
2294 unsigned char *real_buffer;
2295 int ret;
2296
2297 memset(cmd, 0, sizeof(cmd));
2298 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2299
2300 if (sdev->use_10_for_ms) {
2301 if (len > 65535)
2302 return -EINVAL;
2303 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2304 if (!real_buffer)
2305 return -ENOMEM;
2306 memcpy(real_buffer + 8, buffer, len);
2307 len += 8;
2308 real_buffer[0] = 0;
2309 real_buffer[1] = 0;
2310 real_buffer[2] = data->medium_type;
2311 real_buffer[3] = data->device_specific;
2312 real_buffer[4] = data->longlba ? 0x01 : 0;
2313 real_buffer[5] = 0;
2314 real_buffer[6] = data->block_descriptor_length >> 8;
2315 real_buffer[7] = data->block_descriptor_length;
2316
2317 cmd[0] = MODE_SELECT_10;
2318 cmd[7] = len >> 8;
2319 cmd[8] = len;
2320 } else {
2321 if (len > 255 || data->block_descriptor_length > 255 ||
2322 data->longlba)
2323 return -EINVAL;
2324
2325 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2326 if (!real_buffer)
2327 return -ENOMEM;
2328 memcpy(real_buffer + 4, buffer, len);
2329 len += 4;
2330 real_buffer[0] = 0;
2331 real_buffer[1] = data->medium_type;
2332 real_buffer[2] = data->device_specific;
2333 real_buffer[3] = data->block_descriptor_length;
2334
2335
2336 cmd[0] = MODE_SELECT;
2337 cmd[4] = len;
2338 }
2339
2340 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002341 sshdr, timeout, retries, NULL);
James Bottomley5baba832006-03-18 14:10:35 -06002342 kfree(real_buffer);
2343 return ret;
2344}
2345EXPORT_SYMBOL_GPL(scsi_mode_select);
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347/**
Rob Landleyeb448202007-11-03 13:30:39 -05002348 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
James Bottomley1cf72692005-08-28 11:27:01 -05002349 * @sdev: SCSI device to be queried
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 * @dbd: set if mode sense will allow block descriptors to be returned
2351 * @modepage: mode page being requested
2352 * @buffer: request buffer (may not be smaller than eight bytes)
2353 * @len: length of request buffer.
2354 * @timeout: command timeout
2355 * @retries: number of retries before failing
2356 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002357 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley1cf72692005-08-28 11:27:01 -05002358 * must be SCSI_SENSE_BUFFERSIZE big.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 *
2360 * Returns zero if unsuccessful, or the header offset (either 4
2361 * or 8 depending on whether a six or ten byte command was
2362 * issued) if successful.
Rob Landleyeb448202007-11-03 13:30:39 -05002363 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364int
James Bottomley1cf72692005-08-28 11:27:01 -05002365scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 unsigned char *buffer, int len, int timeout, int retries,
James Bottomley5baba832006-03-18 14:10:35 -06002367 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 unsigned char cmd[12];
2370 int use_10_for_ms;
2371 int header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002372 int result, retry_count = retries;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002373 struct scsi_sense_hdr my_sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 memset(data, 0, sizeof(*data));
2376 memset(&cmd[0], 0, 12);
2377 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
2378 cmd[2] = modepage;
2379
James Bottomleyea73a9f2005-08-28 11:33:52 -05002380 /* caller might not be interested in sense, but we need it */
2381 if (!sshdr)
2382 sshdr = &my_sshdr;
2383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 retry:
James Bottomley1cf72692005-08-28 11:27:01 -05002385 use_10_for_ms = sdev->use_10_for_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 if (use_10_for_ms) {
2388 if (len < 8)
2389 len = 8;
2390
2391 cmd[0] = MODE_SENSE_10;
2392 cmd[8] = len;
2393 header_length = 8;
2394 } else {
2395 if (len < 4)
2396 len = 4;
2397
2398 cmd[0] = MODE_SENSE;
2399 cmd[4] = len;
2400 header_length = 4;
2401 }
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 memset(buffer, 0, len);
2404
James Bottomley1cf72692005-08-28 11:27:01 -05002405 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002406 sshdr, timeout, retries, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 /* This code looks awful: what it's doing is making sure an
2409 * ILLEGAL REQUEST sense return identifies the actual command
2410 * byte as the problem. MODE_SENSE commands can return
2411 * ILLEGAL REQUEST if the code page isn't supported */
2412
James Bottomley1cf72692005-08-28 11:27:01 -05002413 if (use_10_for_ms && !scsi_status_is_good(result) &&
2414 (driver_byte(result) & DRIVER_SENSE)) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05002415 if (scsi_sense_valid(sshdr)) {
2416 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2417 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 /*
2419 * Invalid command operation code
2420 */
James Bottomley1cf72692005-08-28 11:27:01 -05002421 sdev->use_10_for_ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 goto retry;
2423 }
2424 }
2425 }
2426
James Bottomley1cf72692005-08-28 11:27:01 -05002427 if(scsi_status_is_good(result)) {
Al Viro6d73c852006-02-23 02:03:16 +01002428 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2429 (modepage == 6 || modepage == 8))) {
2430 /* Initio breakage? */
2431 header_length = 0;
2432 data->length = 13;
2433 data->medium_type = 0;
2434 data->device_specific = 0;
2435 data->longlba = 0;
2436 data->block_descriptor_length = 0;
2437 } else if(use_10_for_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 data->length = buffer[0]*256 + buffer[1] + 2;
2439 data->medium_type = buffer[2];
2440 data->device_specific = buffer[3];
2441 data->longlba = buffer[4] & 0x01;
2442 data->block_descriptor_length = buffer[6]*256
2443 + buffer[7];
2444 } else {
2445 data->length = buffer[0] + 1;
2446 data->medium_type = buffer[1];
2447 data->device_specific = buffer[2];
2448 data->block_descriptor_length = buffer[3];
2449 }
Al Viro6d73c852006-02-23 02:03:16 +01002450 data->header_length = header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002451 } else if ((status_byte(result) == CHECK_CONDITION) &&
2452 scsi_sense_valid(sshdr) &&
2453 sshdr->sense_key == UNIT_ATTENTION && retry_count) {
2454 retry_count--;
2455 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
2457
James Bottomley1cf72692005-08-28 11:27:01 -05002458 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460EXPORT_SYMBOL(scsi_mode_sense);
2461
James Bottomley001aac22007-12-02 19:10:40 +02002462/**
2463 * scsi_test_unit_ready - test if unit is ready
2464 * @sdev: scsi device to change the state of.
2465 * @timeout: command timeout
2466 * @retries: number of retries before failing
2467 * @sshdr_external: Optional pointer to struct scsi_sense_hdr for
2468 * returning sense. Make sure that this is cleared before passing
2469 * in.
2470 *
2471 * Returns zero if unsuccessful or an error if TUR failed. For
Tejun Heo9f8a2c22010-12-08 20:57:40 +01002472 * removable media, UNIT_ATTENTION sets ->changed flag.
James Bottomley001aac22007-12-02 19:10:40 +02002473 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474int
James Bottomley001aac22007-12-02 19:10:40 +02002475scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
2476 struct scsi_sense_hdr *sshdr_external)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 char cmd[] = {
2479 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2480 };
James Bottomley001aac22007-12-02 19:10:40 +02002481 struct scsi_sense_hdr *sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 int result;
James Bottomley001aac22007-12-02 19:10:40 +02002483
2484 if (!sshdr_external)
2485 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
2486 else
2487 sshdr = sshdr_external;
2488
2489 /* try to eat the UNIT_ATTENTION if there are enough retries */
2490 do {
2491 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002492 timeout, retries, NULL);
James Bottomley32c356d2008-08-20 00:58:13 +02002493 if (sdev->removable && scsi_sense_valid(sshdr) &&
2494 sshdr->sense_key == UNIT_ATTENTION)
2495 sdev->changed = 1;
2496 } while (scsi_sense_valid(sshdr) &&
2497 sshdr->sense_key == UNIT_ATTENTION && --retries);
James Bottomley001aac22007-12-02 19:10:40 +02002498
James Bottomley001aac22007-12-02 19:10:40 +02002499 if (!sshdr_external)
2500 kfree(sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return result;
2502}
2503EXPORT_SYMBOL(scsi_test_unit_ready);
2504
2505/**
Rob Landleyeb448202007-11-03 13:30:39 -05002506 * scsi_device_set_state - Take the given device through the device state model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 * @sdev: scsi device to change the state of.
2508 * @state: state to change to.
2509 *
2510 * Returns zero if unsuccessful or an error if the requested
2511 * transition is illegal.
Rob Landleyeb448202007-11-03 13:30:39 -05002512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513int
2514scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2515{
2516 enum scsi_device_state oldstate = sdev->sdev_state;
2517
2518 if (state == oldstate)
2519 return 0;
2520
2521 switch (state) {
2522 case SDEV_CREATED:
James Bottomley6f4267e2008-08-22 16:53:31 -05002523 switch (oldstate) {
2524 case SDEV_CREATED_BLOCK:
2525 break;
2526 default:
2527 goto illegal;
2528 }
2529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 case SDEV_RUNNING:
2532 switch (oldstate) {
2533 case SDEV_CREATED:
2534 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002535 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 case SDEV_QUIESCE:
2537 case SDEV_BLOCK:
2538 break;
2539 default:
2540 goto illegal;
2541 }
2542 break;
2543
2544 case SDEV_QUIESCE:
2545 switch (oldstate) {
2546 case SDEV_RUNNING:
2547 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002548 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 break;
2550 default:
2551 goto illegal;
2552 }
2553 break;
2554
2555 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002556 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 switch (oldstate) {
2558 case SDEV_CREATED:
2559 case SDEV_RUNNING:
2560 case SDEV_QUIESCE:
2561 case SDEV_BLOCK:
2562 break;
2563 default:
2564 goto illegal;
2565 }
2566 break;
2567
2568 case SDEV_BLOCK:
2569 switch (oldstate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 case SDEV_RUNNING:
James Bottomley6f4267e2008-08-22 16:53:31 -05002571 case SDEV_CREATED_BLOCK:
2572 break;
2573 default:
2574 goto illegal;
2575 }
2576 break;
2577
2578 case SDEV_CREATED_BLOCK:
2579 switch (oldstate) {
2580 case SDEV_CREATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 break;
2582 default:
2583 goto illegal;
2584 }
2585 break;
2586
2587 case SDEV_CANCEL:
2588 switch (oldstate) {
2589 case SDEV_CREATED:
2590 case SDEV_RUNNING:
Alan Stern9ea72902006-06-23 14:25:34 -04002591 case SDEV_QUIESCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002593 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 case SDEV_BLOCK:
2595 break;
2596 default:
2597 goto illegal;
2598 }
2599 break;
2600
2601 case SDEV_DEL:
2602 switch (oldstate) {
Brian King309bd272006-06-27 11:10:43 -05002603 case SDEV_CREATED:
2604 case SDEV_RUNNING:
2605 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002606 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 case SDEV_CANCEL:
Bart Van Assche0516c082013-07-02 15:06:33 +02002608 case SDEV_CREATED_BLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 break;
2610 default:
2611 goto illegal;
2612 }
2613 break;
2614
2615 }
2616 sdev->sdev_state = state;
2617 return 0;
2618
2619 illegal:
Hannes Reinecke91921e02014-06-25 16:39:59 +02002620 SCSI_LOG_ERROR_RECOVERY(1,
James Bottomley9ccfc752005-10-02 11:45:08 -05002621 sdev_printk(KERN_ERR, sdev,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002622 "Illegal state transition %s->%s",
James Bottomley9ccfc752005-10-02 11:45:08 -05002623 scsi_device_state_name(oldstate),
2624 scsi_device_state_name(state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 );
2626 return -EINVAL;
2627}
2628EXPORT_SYMBOL(scsi_device_set_state);
2629
2630/**
Jeff Garzika341cd02007-10-29 17:15:22 -04002631 * sdev_evt_emit - emit a single SCSI device uevent
2632 * @sdev: associated SCSI device
2633 * @evt: event to emit
2634 *
2635 * Send a single uevent (scsi_event) to the associated scsi_device.
2636 */
2637static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2638{
2639 int idx = 0;
2640 char *envp[3];
2641
2642 switch (evt->evt_type) {
2643 case SDEV_EVT_MEDIA_CHANGE:
2644 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2645 break;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002646 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
Hannes Reinecked3d32892016-02-19 09:17:16 +01002647 scsi_rescan_device(&sdev->sdev_gendev);
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002648 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2649 break;
2650 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2651 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2652 break;
2653 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2654 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2655 break;
2656 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2657 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2658 break;
2659 case SDEV_EVT_LUN_CHANGE_REPORTED:
2660 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2661 break;
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002662 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2663 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
2664 break;
Jeff Garzika341cd02007-10-29 17:15:22 -04002665 default:
2666 /* do nothing */
2667 break;
2668 }
2669
2670 envp[idx++] = NULL;
2671
2672 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2673}
2674
2675/**
2676 * sdev_evt_thread - send a uevent for each scsi event
2677 * @work: work struct for scsi_device
2678 *
2679 * Dispatch queued events to their associated scsi_device kobjects
2680 * as uevents.
2681 */
2682void scsi_evt_thread(struct work_struct *work)
2683{
2684 struct scsi_device *sdev;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002685 enum scsi_device_event evt_type;
Jeff Garzika341cd02007-10-29 17:15:22 -04002686 LIST_HEAD(event_list);
2687
2688 sdev = container_of(work, struct scsi_device, event_work);
2689
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002690 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2691 if (test_and_clear_bit(evt_type, sdev->pending_events))
2692 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2693
Jeff Garzika341cd02007-10-29 17:15:22 -04002694 while (1) {
2695 struct scsi_event *evt;
2696 struct list_head *this, *tmp;
2697 unsigned long flags;
2698
2699 spin_lock_irqsave(&sdev->list_lock, flags);
2700 list_splice_init(&sdev->event_list, &event_list);
2701 spin_unlock_irqrestore(&sdev->list_lock, flags);
2702
2703 if (list_empty(&event_list))
2704 break;
2705
2706 list_for_each_safe(this, tmp, &event_list) {
2707 evt = list_entry(this, struct scsi_event, node);
2708 list_del(&evt->node);
2709 scsi_evt_emit(sdev, evt);
2710 kfree(evt);
2711 }
2712 }
2713}
2714
2715/**
2716 * sdev_evt_send - send asserted event to uevent thread
2717 * @sdev: scsi_device event occurred on
2718 * @evt: event to send
2719 *
2720 * Assert scsi device event asynchronously.
2721 */
2722void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2723{
2724 unsigned long flags;
2725
Kay Sievers4d1566e2008-03-19 13:04:47 +01002726#if 0
2727 /* FIXME: currently this check eliminates all media change events
2728 * for polled devices. Need to update to discriminate between AN
2729 * and polled events */
Jeff Garzika341cd02007-10-29 17:15:22 -04002730 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2731 kfree(evt);
2732 return;
2733 }
Kay Sievers4d1566e2008-03-19 13:04:47 +01002734#endif
Jeff Garzika341cd02007-10-29 17:15:22 -04002735
2736 spin_lock_irqsave(&sdev->list_lock, flags);
2737 list_add_tail(&evt->node, &sdev->event_list);
2738 schedule_work(&sdev->event_work);
2739 spin_unlock_irqrestore(&sdev->list_lock, flags);
2740}
2741EXPORT_SYMBOL_GPL(sdev_evt_send);
2742
2743/**
2744 * sdev_evt_alloc - allocate a new scsi event
2745 * @evt_type: type of event to allocate
2746 * @gfpflags: GFP flags for allocation
2747 *
2748 * Allocates and returns a new scsi_event.
2749 */
2750struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2751 gfp_t gfpflags)
2752{
2753 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2754 if (!evt)
2755 return NULL;
2756
2757 evt->evt_type = evt_type;
2758 INIT_LIST_HEAD(&evt->node);
2759
2760 /* evt_type-specific initialization, if any */
2761 switch (evt_type) {
2762 case SDEV_EVT_MEDIA_CHANGE:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002763 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2764 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2765 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2766 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2767 case SDEV_EVT_LUN_CHANGE_REPORTED:
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002768 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
Jeff Garzika341cd02007-10-29 17:15:22 -04002769 default:
2770 /* do nothing */
2771 break;
2772 }
2773
2774 return evt;
2775}
2776EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2777
2778/**
2779 * sdev_evt_send_simple - send asserted event to uevent thread
2780 * @sdev: scsi_device event occurred on
2781 * @evt_type: type of event to send
2782 * @gfpflags: GFP flags for allocation
2783 *
2784 * Assert scsi device event asynchronously, given an event type.
2785 */
2786void sdev_evt_send_simple(struct scsi_device *sdev,
2787 enum scsi_device_event evt_type, gfp_t gfpflags)
2788{
2789 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2790 if (!evt) {
2791 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2792 evt_type);
2793 return;
2794 }
2795
2796 sdev_evt_send(sdev, evt);
2797}
2798EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2799
2800/**
Bart Van Assche669f0442016-11-22 16:17:13 -08002801 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
2802 * @sdev: SCSI device to count the number of scsi_request_fn() callers for.
2803 */
2804static int scsi_request_fn_active(struct scsi_device *sdev)
2805{
2806 struct request_queue *q = sdev->request_queue;
2807 int request_fn_active;
2808
2809 WARN_ON_ONCE(sdev->host->use_blk_mq);
2810
2811 spin_lock_irq(q->queue_lock);
2812 request_fn_active = q->request_fn_active;
2813 spin_unlock_irq(q->queue_lock);
2814
2815 return request_fn_active;
2816}
2817
2818/**
2819 * scsi_wait_for_queuecommand() - wait for ongoing queuecommand() calls
2820 * @sdev: SCSI device pointer.
2821 *
2822 * Wait until the ongoing shost->hostt->queuecommand() calls that are
2823 * invoked from scsi_request_fn() have finished.
2824 */
2825static void scsi_wait_for_queuecommand(struct scsi_device *sdev)
2826{
2827 WARN_ON_ONCE(sdev->host->use_blk_mq);
2828
2829 while (scsi_request_fn_active(sdev))
2830 msleep(20);
2831}
2832
2833/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 * scsi_device_quiesce - Block user issued commands.
2835 * @sdev: scsi device to quiesce.
2836 *
2837 * This works by trying to transition to the SDEV_QUIESCE state
2838 * (which must be a legal transition). When the device is in this
2839 * state, only special requests will be accepted, all others will
2840 * be deferred. Since special requests may also be requeued requests,
2841 * a successful return doesn't guarantee the device will be
2842 * totally quiescent.
2843 *
2844 * Must be called with user context, may sleep.
2845 *
2846 * Returns zero if unsuccessful or an error if not.
Rob Landleyeb448202007-11-03 13:30:39 -05002847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848int
2849scsi_device_quiesce(struct scsi_device *sdev)
2850{
2851 int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2852 if (err)
2853 return err;
2854
2855 scsi_run_queue(sdev->request_queue);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02002856 while (atomic_read(&sdev->device_busy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 msleep_interruptible(200);
2858 scsi_run_queue(sdev->request_queue);
2859 }
2860 return 0;
2861}
2862EXPORT_SYMBOL(scsi_device_quiesce);
2863
2864/**
2865 * scsi_device_resume - Restart user issued commands to a quiesced device.
2866 * @sdev: scsi device to resume.
2867 *
2868 * Moves the device from quiesced back to running and restarts the
2869 * queues.
2870 *
2871 * Must be called with user context, may sleep.
Rob Landleyeb448202007-11-03 13:30:39 -05002872 */
Dan Williamsa7a20d12012-03-22 17:05:11 -07002873void scsi_device_resume(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
Dan Williamsa7a20d12012-03-22 17:05:11 -07002875 /* check if the device state was mutated prior to resume, and if
2876 * so assume the state is being managed elsewhere (for example
2877 * device deleted during suspend)
2878 */
2879 if (sdev->sdev_state != SDEV_QUIESCE ||
2880 scsi_device_set_state(sdev, SDEV_RUNNING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return;
2882 scsi_run_queue(sdev->request_queue);
2883}
2884EXPORT_SYMBOL(scsi_device_resume);
2885
2886static void
2887device_quiesce_fn(struct scsi_device *sdev, void *data)
2888{
2889 scsi_device_quiesce(sdev);
2890}
2891
2892void
2893scsi_target_quiesce(struct scsi_target *starget)
2894{
2895 starget_for_each_device(starget, NULL, device_quiesce_fn);
2896}
2897EXPORT_SYMBOL(scsi_target_quiesce);
2898
2899static void
2900device_resume_fn(struct scsi_device *sdev, void *data)
2901{
2902 scsi_device_resume(sdev);
2903}
2904
2905void
2906scsi_target_resume(struct scsi_target *starget)
2907{
2908 starget_for_each_device(starget, NULL, device_resume_fn);
2909}
2910EXPORT_SYMBOL(scsi_target_resume);
2911
2912/**
Rob Landleyeb448202007-11-03 13:30:39 -05002913 * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 * @sdev: device to block
2915 *
2916 * Block request made by scsi lld's to temporarily stop all
Bart Van Assche669f0442016-11-22 16:17:13 -08002917 * scsi commands on the specified device. May sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 *
2919 * Returns zero if successful or error if not
2920 *
2921 * Notes:
2922 * This routine transitions the device to the SDEV_BLOCK state
2923 * (which must be a legal transition). When the device is in this
2924 * state, all commands are deferred until the scsi lld reenables
2925 * the device with scsi_device_unblock or device_block_tmo fires.
Bart Van Assche669f0442016-11-22 16:17:13 -08002926 *
2927 * To do: avoid that scsi_send_eh_cmnd() calls queuecommand() after
2928 * scsi_internal_device_block() has blocked a SCSI device and also
2929 * remove the rport mutex lock and unlock calls from srp_queuecommand().
Rob Landleyeb448202007-11-03 13:30:39 -05002930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931int
2932scsi_internal_device_block(struct scsi_device *sdev)
2933{
Jens Axboe165125e2007-07-24 09:28:11 +02002934 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 unsigned long flags;
2936 int err = 0;
2937
2938 err = scsi_device_set_state(sdev, SDEV_BLOCK);
James Bottomley6f4267e2008-08-22 16:53:31 -05002939 if (err) {
2940 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2941
2942 if (err)
2943 return err;
2944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 /*
2947 * The device has transitioned to SDEV_BLOCK. Stop the
2948 * block layer from calling the midlayer with this device's
2949 * request queue.
2950 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01002951 if (q->mq_ops) {
Bart Van Assche7dbbf0f2016-11-22 16:17:50 -08002952 blk_mq_quiesce_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002953 } else {
2954 spin_lock_irqsave(q->queue_lock, flags);
2955 blk_stop_queue(q);
2956 spin_unlock_irqrestore(q->queue_lock, flags);
Bart Van Assche669f0442016-11-22 16:17:13 -08002957 scsi_wait_for_queuecommand(sdev);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 return 0;
2961}
2962EXPORT_SYMBOL_GPL(scsi_internal_device_block);
2963
2964/**
2965 * scsi_internal_device_unblock - resume a device after a block request
2966 * @sdev: device to resume
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002967 * @new_state: state to set devices to after unblocking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 *
2969 * Called by scsi lld's or the midlayer to restart the device queue
2970 * for the previously suspended scsi device. Called from interrupt or
2971 * normal process context.
2972 *
2973 * Returns zero if successful or error if not.
2974 *
2975 * Notes:
2976 * This routine transitions the device to the SDEV_RUNNING state
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002977 * or to one of the offline states (which must be a legal transition)
Mike Christied0754982012-05-17 23:56:58 -05002978 * allowing the midlayer to goose the queue for this device.
Rob Landleyeb448202007-11-03 13:30:39 -05002979 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980int
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002981scsi_internal_device_unblock(struct scsi_device *sdev,
2982 enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
Jens Axboe165125e2007-07-24 09:28:11 +02002984 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 unsigned long flags;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002986
2987 /*
2988 * Try to transition the scsi device to SDEV_RUNNING or one of the
2989 * offlined states and goose the device queue if successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 */
Vikas Chaudhary0e580762012-08-09 04:51:30 -04002991 if ((sdev->sdev_state == SDEV_BLOCK) ||
2992 (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002993 sdev->sdev_state = new_state;
2994 else if (sdev->sdev_state == SDEV_CREATED_BLOCK) {
2995 if (new_state == SDEV_TRANSPORT_OFFLINE ||
2996 new_state == SDEV_OFFLINE)
2997 sdev->sdev_state = new_state;
2998 else
2999 sdev->sdev_state = SDEV_CREATED;
3000 } else if (sdev->sdev_state != SDEV_CANCEL &&
Mike Christie986fe6c2010-10-06 03:10:59 -05003001 sdev->sdev_state != SDEV_OFFLINE)
Takahiro Yasui5c10e632009-04-29 12:13:02 -04003002 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Christoph Hellwigd2852032014-01-17 12:06:53 +01003004 if (q->mq_ops) {
3005 blk_mq_start_stopped_hw_queues(q, false);
3006 } else {
3007 spin_lock_irqsave(q->queue_lock, flags);
3008 blk_start_queue(q);
3009 spin_unlock_irqrestore(q->queue_lock, flags);
3010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
3012 return 0;
3013}
3014EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
3015
3016static void
3017device_block(struct scsi_device *sdev, void *data)
3018{
3019 scsi_internal_device_block(sdev);
3020}
3021
3022static int
3023target_block(struct device *dev, void *data)
3024{
3025 if (scsi_is_target_device(dev))
3026 starget_for_each_device(to_scsi_target(dev), NULL,
3027 device_block);
3028 return 0;
3029}
3030
3031void
3032scsi_target_block(struct device *dev)
3033{
3034 if (scsi_is_target_device(dev))
3035 starget_for_each_device(to_scsi_target(dev), NULL,
3036 device_block);
3037 else
3038 device_for_each_child(dev, NULL, target_block);
3039}
3040EXPORT_SYMBOL_GPL(scsi_target_block);
3041
3042static void
3043device_unblock(struct scsi_device *sdev, void *data)
3044{
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003045 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
3048static int
3049target_unblock(struct device *dev, void *data)
3050{
3051 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003052 starget_for_each_device(to_scsi_target(dev), data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 device_unblock);
3054 return 0;
3055}
3056
3057void
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003058scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
3060 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003061 starget_for_each_device(to_scsi_target(dev), &new_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 device_unblock);
3063 else
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003064 device_for_each_child(dev, &new_state, target_unblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066EXPORT_SYMBOL_GPL(scsi_target_unblock);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003067
3068/**
3069 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
Rob Landleyeb448202007-11-03 13:30:39 -05003070 * @sgl: scatter-gather list
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003071 * @sg_count: number of segments in sg
3072 * @offset: offset in bytes into sg, on return offset into the mapped area
3073 * @len: bytes to map, on return number of bytes mapped
3074 *
3075 * Returns virtual address of the start of the mapped page
3076 */
Jens Axboec6132da2007-10-16 11:08:49 +02003077void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003078 size_t *offset, size_t *len)
3079{
3080 int i;
3081 size_t sg_len = 0, len_complete = 0;
Jens Axboec6132da2007-10-16 11:08:49 +02003082 struct scatterlist *sg;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003083 struct page *page;
3084
Andrew Morton22cfefb2007-02-05 16:39:03 -08003085 WARN_ON(!irqs_disabled());
3086
Jens Axboec6132da2007-10-16 11:08:49 +02003087 for_each_sg(sgl, sg, sg_count, i) {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003088 len_complete = sg_len; /* Complete sg-entries */
Jens Axboec6132da2007-10-16 11:08:49 +02003089 sg_len += sg->length;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003090 if (sg_len > *offset)
3091 break;
3092 }
3093
3094 if (unlikely(i == sg_count)) {
Andrew Morton169e1a22006-04-18 21:09:08 -07003095 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
3096 "elements %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003097 __func__, sg_len, *offset, sg_count);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003098 WARN_ON(1);
3099 return NULL;
3100 }
3101
3102 /* Offset starting from the beginning of first page in this sg-entry */
Jens Axboec6132da2007-10-16 11:08:49 +02003103 *offset = *offset - len_complete + sg->offset;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003104
3105 /* Assumption: contiguous pages can be accessed as "page + i" */
Jens Axboe45711f12007-10-22 21:19:53 +02003106 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003107 *offset &= ~PAGE_MASK;
3108
3109 /* Bytes in this sg-entry from *offset to the end of the page */
3110 sg_len = PAGE_SIZE - *offset;
3111 if (*len > sg_len)
3112 *len = sg_len;
3113
Cong Wang77dfce02011-11-25 23:14:23 +08003114 return kmap_atomic(page);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003115}
3116EXPORT_SYMBOL(scsi_kmap_atomic_sg);
3117
3118/**
Rob Landleyeb448202007-11-03 13:30:39 -05003119 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003120 * @virt: virtual address to be unmapped
3121 */
3122void scsi_kunmap_atomic_sg(void *virt)
3123{
Cong Wang77dfce02011-11-25 23:14:23 +08003124 kunmap_atomic(virt);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003125}
3126EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
Aaron Lu6f4c8272013-01-23 15:09:32 +08003127
3128void sdev_disable_disk_events(struct scsi_device *sdev)
3129{
3130 atomic_inc(&sdev->disk_events_disable_depth);
3131}
3132EXPORT_SYMBOL(sdev_disable_disk_events);
3133
3134void sdev_enable_disk_events(struct scsi_device *sdev)
3135{
3136 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
3137 return;
3138 atomic_dec(&sdev->disk_events_disable_depth);
3139}
3140EXPORT_SYMBOL(sdev_enable_disk_events);
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003141
3142/**
3143 * scsi_vpd_lun_id - return a unique device identification
3144 * @sdev: SCSI device
3145 * @id: buffer for the identification
3146 * @id_len: length of the buffer
3147 *
3148 * Copies a unique device identification into @id based
3149 * on the information in the VPD page 0x83 of the device.
3150 * The string will be formatted as a SCSI name string.
3151 *
3152 * Returns the length of the identification or error on failure.
3153 * If the identifier is longer than the supplied buffer the actual
3154 * identifier length is returned and the buffer is not zero-padded.
3155 */
3156int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
3157{
3158 u8 cur_id_type = 0xff;
3159 u8 cur_id_size = 0;
3160 unsigned char *d, *cur_id_str;
3161 unsigned char __rcu *vpd_pg83;
3162 int id_size = -EINVAL;
3163
3164 rcu_read_lock();
3165 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3166 if (!vpd_pg83) {
3167 rcu_read_unlock();
3168 return -ENXIO;
3169 }
3170
3171 /*
3172 * Look for the correct descriptor.
3173 * Order of preference for lun descriptor:
3174 * - SCSI name string
3175 * - NAA IEEE Registered Extended
3176 * - EUI-64 based 16-byte
3177 * - EUI-64 based 12-byte
3178 * - NAA IEEE Registered
3179 * - NAA IEEE Extended
Hannes Reinecked2308232016-05-09 09:14:29 +02003180 * - T10 Vendor ID
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003181 * as longer descriptors reduce the likelyhood
3182 * of identification clashes.
3183 */
3184
3185 /* The id string must be at least 20 bytes + terminating NULL byte */
3186 if (id_len < 21) {
3187 rcu_read_unlock();
3188 return -EINVAL;
3189 }
3190
3191 memset(id, 0, id_len);
3192 d = vpd_pg83 + 4;
3193 while (d < vpd_pg83 + sdev->vpd_pg83_len) {
3194 /* Skip designators not referring to the LUN */
3195 if ((d[1] & 0x30) != 0x00)
3196 goto next_desig;
3197
3198 switch (d[1] & 0xf) {
Hannes Reinecked2308232016-05-09 09:14:29 +02003199 case 0x1:
3200 /* T10 Vendor ID */
3201 if (cur_id_size > d[3])
3202 break;
3203 /* Prefer anything */
3204 if (cur_id_type > 0x01 && cur_id_type != 0xff)
3205 break;
3206 cur_id_size = d[3];
3207 if (cur_id_size + 4 > id_len)
3208 cur_id_size = id_len - 4;
3209 cur_id_str = d + 4;
3210 cur_id_type = d[1] & 0xf;
3211 id_size = snprintf(id, id_len, "t10.%*pE",
3212 cur_id_size, cur_id_str);
3213 break;
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003214 case 0x2:
3215 /* EUI-64 */
3216 if (cur_id_size > d[3])
3217 break;
3218 /* Prefer NAA IEEE Registered Extended */
3219 if (cur_id_type == 0x3 &&
3220 cur_id_size == d[3])
3221 break;
3222 cur_id_size = d[3];
3223 cur_id_str = d + 4;
3224 cur_id_type = d[1] & 0xf;
3225 switch (cur_id_size) {
3226 case 8:
3227 id_size = snprintf(id, id_len,
3228 "eui.%8phN",
3229 cur_id_str);
3230 break;
3231 case 12:
3232 id_size = snprintf(id, id_len,
3233 "eui.%12phN",
3234 cur_id_str);
3235 break;
3236 case 16:
3237 id_size = snprintf(id, id_len,
3238 "eui.%16phN",
3239 cur_id_str);
3240 break;
3241 default:
3242 cur_id_size = 0;
3243 break;
3244 }
3245 break;
3246 case 0x3:
3247 /* NAA */
3248 if (cur_id_size > d[3])
3249 break;
3250 cur_id_size = d[3];
3251 cur_id_str = d + 4;
3252 cur_id_type = d[1] & 0xf;
3253 switch (cur_id_size) {
3254 case 8:
3255 id_size = snprintf(id, id_len,
3256 "naa.%8phN",
3257 cur_id_str);
3258 break;
3259 case 16:
3260 id_size = snprintf(id, id_len,
3261 "naa.%16phN",
3262 cur_id_str);
3263 break;
3264 default:
3265 cur_id_size = 0;
3266 break;
3267 }
3268 break;
3269 case 0x8:
3270 /* SCSI name string */
3271 if (cur_id_size + 4 > d[3])
3272 break;
3273 /* Prefer others for truncated descriptor */
3274 if (cur_id_size && d[3] > id_len)
3275 break;
3276 cur_id_size = id_size = d[3];
3277 cur_id_str = d + 4;
3278 cur_id_type = d[1] & 0xf;
3279 if (cur_id_size >= id_len)
3280 cur_id_size = id_len - 1;
3281 memcpy(id, cur_id_str, cur_id_size);
3282 /* Decrease priority for truncated descriptor */
3283 if (cur_id_size != id_size)
3284 cur_id_size = 6;
3285 break;
3286 default:
3287 break;
3288 }
3289next_desig:
3290 d += d[3] + 4;
3291 }
3292 rcu_read_unlock();
3293
3294 return id_size;
3295}
3296EXPORT_SYMBOL(scsi_vpd_lun_id);
Hannes Reineckea8aa3972015-12-01 10:16:57 +01003297
3298/*
3299 * scsi_vpd_tpg_id - return a target port group identifier
3300 * @sdev: SCSI device
3301 *
3302 * Returns the Target Port Group identifier from the information
3303 * froom VPD page 0x83 of the device.
3304 *
3305 * Returns the identifier or error on failure.
3306 */
3307int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
3308{
3309 unsigned char *d;
3310 unsigned char __rcu *vpd_pg83;
3311 int group_id = -EAGAIN, rel_port = -1;
3312
3313 rcu_read_lock();
3314 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3315 if (!vpd_pg83) {
3316 rcu_read_unlock();
3317 return -ENXIO;
3318 }
3319
3320 d = sdev->vpd_pg83 + 4;
3321 while (d < sdev->vpd_pg83 + sdev->vpd_pg83_len) {
3322 switch (d[1] & 0xf) {
3323 case 0x4:
3324 /* Relative target port */
3325 rel_port = get_unaligned_be16(&d[6]);
3326 break;
3327 case 0x5:
3328 /* Target port group */
3329 group_id = get_unaligned_be16(&d[6]);
3330 break;
3331 default:
3332 break;
3333 }
3334 d += d[3] + 4;
3335 }
3336 rcu_read_unlock();
3337
3338 if (group_id >= 0 && rel_id && rel_port != -1)
3339 *rel_id = rel_port;
3340
3341 return group_id;
3342}
3343EXPORT_SYMBOL(scsi_vpd_tpg_id);