blob: 043ab9e9f5a37d66f87d30fd26350b353e30242f [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;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +020042
Jens Axboea488e742010-04-16 21:13:15 +020043/*
44 * When to reinvoke queueing after a resource shortage. It's 3 msecs to
45 * not change behaviour from the previous unplug mechanism, experimentation
46 * may prove this needs changing.
47 */
48#define SCSI_QUEUE_DELAY 3
49
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +010050static void
51scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 struct Scsi_Host *host = cmd->device->host;
54 struct scsi_device *device = cmd->device;
Mike Christief0c0a372008-08-17 15:24:38 -050055 struct scsi_target *starget = scsi_target(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 /*
Tejun Heo d8c37e72005-05-14 00:46:08 +090058 * Set the appropriate busy bit for the device/host.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 *
60 * If the host/device isn't busy, assume that something actually
61 * completed, and that we should be able to queue a command now.
62 *
63 * Note that the prior mid-layer assumption that any host could
64 * always queue at least one command is now broken. The mid-layer
65 * will implement a user specifiable stall (see
66 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
67 * if a command is requeued with no other commands outstanding
68 * either for the device or for the host.
69 */
Mike Christief0c0a372008-08-17 15:24:38 -050070 switch (reason) {
71 case SCSI_MLQUEUE_HOST_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +010072 atomic_set(&host->host_blocked, host->max_host_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -050073 break;
74 case SCSI_MLQUEUE_DEVICE_BUSY:
James Smart573e5912011-07-06 12:45:17 -040075 case SCSI_MLQUEUE_EH_RETRY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +010076 atomic_set(&device->device_blocked,
77 device->max_device_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -050078 break;
79 case SCSI_MLQUEUE_TARGET_BUSY:
Christoph Hellwigcd9070c2014-01-23 12:07:41 +010080 atomic_set(&starget->target_blocked,
81 starget->max_target_blocked);
Mike Christief0c0a372008-08-17 15:24:38 -050082 break;
83 }
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +010084}
85
Christoph Hellwigd2852032014-01-17 12:06:53 +010086static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd)
87{
88 struct scsi_device *sdev = cmd->device;
89 struct request_queue *q = cmd->request->q;
90
91 blk_mq_requeue_request(cmd->request);
92 blk_mq_kick_requeue_list(q);
93 put_device(&sdev->sdev_gendev);
94}
95
Christoph Hellwigde3e8bf2014-01-23 13:55:38 +010096/**
97 * __scsi_queue_insert - private queue insertion
98 * @cmd: The SCSI command being requeued
99 * @reason: The reason for the requeue
100 * @unbusy: Whether the queue should be unbusied
101 *
102 * This is a private queue insertion. The public interface
103 * scsi_queue_insert() always assumes the queue should be unbusied
104 * because it's always called before the completion. This function is
105 * for a requeue after completion, which should only occur in this
106 * file.
107 */
108static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy)
109{
110 struct scsi_device *device = cmd->device;
111 struct request_queue *q = device->request_queue;
112 unsigned long flags;
113
114 SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
115 "Inserting command %p into mlqueue\n", cmd));
116
117 scsi_set_blocked(cmd, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * Decrement the counters, since these commands are no longer
121 * active on the host/device.
122 */
James Bottomley4f5299a2009-01-02 10:42:21 -0600123 if (unbusy)
124 scsi_device_unbusy(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /*
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500127 * Requeue this command. It will go before all other commands
Bart Van Asscheb4854622012-06-29 15:36:07 +0000128 * that are already in the queue. Schedule requeue work under
129 * lock such that the kblockd_schedule_work() call happens
130 * before blk_cleanup_queue() finishes.
Jens Axboea488e742010-04-16 21:13:15 +0200131 */
Alan Stern644373a2014-03-28 10:51:15 -0700132 cmd->result = 0;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100133 if (q->mq_ops) {
134 scsi_mq_requeue_cmd(cmd);
135 return;
136 }
Tejun Heo a1bf9d1d2005-04-24 02:08:52 -0500137 spin_lock_irqsave(q->queue_lock, flags);
James Bottomley59897da2005-09-14 12:57:42 -0400138 blk_requeue_request(q, cmd->request);
Jens Axboe59c3d452014-04-08 09:15:35 -0600139 kblockd_schedule_work(&device->requeue_work);
Bart Van Asscheb4854622012-06-29 15:36:07 +0000140 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
James Bottomley4f5299a2009-01-02 10:42:21 -0600143/*
144 * Function: scsi_queue_insert()
145 *
146 * Purpose: Insert a command in the midlevel queue.
147 *
148 * Arguments: cmd - command that we are adding to queue.
149 * reason - why we are inserting command to queue.
150 *
151 * Lock status: Assumed that lock is not held upon entry.
152 *
153 * Returns: Nothing.
154 *
155 * Notes: We do this for one of two cases. Either the host is busy
156 * and it cannot accept any more commands for the time being,
157 * or the device returned QUEUE_FULL and can accept no more
158 * commands.
159 * Notes: This could be called either from an interrupt context or a
160 * normal process context.
161 */
Bart Van Assche84feb162012-06-29 15:35:05 +0000162void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
James Bottomley4f5299a2009-01-02 10:42:21 -0600163{
Bart Van Assche84feb162012-06-29 15:35:05 +0000164 __scsi_queue_insert(cmd, reason, 1);
James Bottomley4f5299a2009-01-02 10:42:21 -0600165}
James Bottomley39216032005-06-15 18:48:29 -0500166/**
James Bottomley33aa6872005-08-28 11:31:14 -0500167 * scsi_execute - insert request and wait for the result
James Bottomley39216032005-06-15 18:48:29 -0500168 * @sdev: scsi device
169 * @cmd: scsi command
170 * @data_direction: data direction
171 * @buffer: data buffer
172 * @bufflen: len of buffer
173 * @sense: optional sense buffer
174 * @timeout: request timeout in seconds
175 * @retries: number of times to retry request
James Bottomley33aa6872005-08-28 11:31:14 -0500176 * @flags: or into request flags;
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900177 * @resid: optional residual length
James Bottomley39216032005-06-15 18:48:29 -0500178 *
Michael Opdenacker59c51592007-05-09 08:57:56 +0200179 * returns the req->errors value which is the scsi_cmnd result
James Bottomleyea73a9f2005-08-28 11:33:52 -0500180 * field.
Rob Landleyeb448202007-11-03 13:30:39 -0500181 */
James Bottomley33aa6872005-08-28 11:31:14 -0500182int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
183 int data_direction, void *buffer, unsigned bufflen,
Martin K. Petersen2bfad212014-04-09 22:20:48 -0400184 unsigned char *sense, int timeout, int retries, u64 flags,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900185 int *resid)
James Bottomley39216032005-06-15 18:48:29 -0500186{
187 struct request *req;
188 int write = (data_direction == DMA_TO_DEVICE);
189 int ret = DRIVER_ERROR << 24;
190
Mel Gorman71baba42015-11-06 16:28:28 -0800191 req = blk_get_request(sdev->request_queue, write, __GFP_RECLAIM);
Joe Lawrencea492f072014-08-28 08:15:21 -0600192 if (IS_ERR(req))
James Bottomleybfe159a2011-07-07 15:45:40 -0500193 return ret;
Jens Axboef27b0872014-06-06 07:57:37 -0600194 blk_rq_set_block_pc(req);
James Bottomley39216032005-06-15 18:48:29 -0500195
196 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
Mel Gorman71baba42015-11-06 16:28:28 -0800197 buffer, bufflen, __GFP_RECLAIM))
James Bottomley39216032005-06-15 18:48:29 -0500198 goto out;
199
200 req->cmd_len = COMMAND_SIZE(cmd[0]);
201 memcpy(req->cmd, cmd, req->cmd_len);
202 req->sense = sense;
203 req->sense_len = 0;
Mike Christie17e01f22005-11-11 05:31:37 -0600204 req->retries = retries;
Subhash Jadavani70e75882016-08-12 16:44:37 -0700205 if (likely(!sdev->timeout_override))
206 req->timeout = timeout;
207 else
208 req->timeout = sdev->timeout_override;
209
Jens Axboe4aff5e22006-08-10 08:44:47 +0200210 req->cmd_flags |= flags | REQ_QUIET | REQ_PREEMPT;
James Bottomley39216032005-06-15 18:48:29 -0500211
212 /*
213 * head injection *required* here otherwise quiesce won't work
214 */
215 blk_execute_rq(req->q, NULL, req, 1);
216
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400217 /*
218 * Some devices (USB mass-storage in particular) may transfer
219 * garbage data together with a residue indicating that the data
220 * is invalid. Prevent the garbage from being misinterpreted
221 * and prevent security leaks by zeroing out the excess data.
222 */
Tejun Heoc3a4d782009-05-07 22:24:37 +0900223 if (unlikely(req->resid_len > 0 && req->resid_len <= bufflen))
224 memset(buffer + (bufflen - req->resid_len), 0, req->resid_len);
Alan Sternbdb2b8c2008-06-24 14:03:14 -0400225
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900226 if (resid)
Tejun Heoc3a4d782009-05-07 22:24:37 +0900227 *resid = req->resid_len;
James Bottomley39216032005-06-15 18:48:29 -0500228 ret = req->errors;
229 out:
230 blk_put_request(req);
231
232 return ret;
233}
James Bottomley33aa6872005-08-28 11:31:14 -0500234EXPORT_SYMBOL(scsi_execute);
James Bottomley39216032005-06-15 18:48:29 -0500235
Lin Ming9b214932013-03-23 11:42:25 +0800236int scsi_execute_req_flags(struct scsi_device *sdev, const unsigned char *cmd,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500237 int data_direction, void *buffer, unsigned bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900238 struct scsi_sense_hdr *sshdr, int timeout, int retries,
Martin K. Petersen2bfad212014-04-09 22:20:48 -0400239 int *resid, u64 flags)
James Bottomleyea73a9f2005-08-28 11:33:52 -0500240{
241 char *sense = NULL;
akpm@osdl.org1ccb48b2005-06-26 00:12:51 -0700242 int result;
243
James Bottomleyea73a9f2005-08-28 11:33:52 -0500244 if (sshdr) {
Jes Sorensen24669f752006-01-16 10:31:18 -0500245 sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500246 if (!sense)
247 return DRIVER_ERROR << 24;
James Bottomleyea73a9f2005-08-28 11:33:52 -0500248 }
akpm@osdl.org1ccb48b2005-06-26 00:12:51 -0700249 result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
Lin Ming9b214932013-03-23 11:42:25 +0800250 sense, timeout, retries, flags, resid);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500251 if (sshdr)
James Bottomleye5143852005-08-09 11:55:36 -0500252 scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500253
254 kfree(sense);
255 return result;
256}
Lin Ming9b214932013-03-23 11:42:25 +0800257EXPORT_SYMBOL(scsi_execute_req_flags);
James Bottomleyea73a9f2005-08-28 11:33:52 -0500258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * Function: scsi_init_cmd_errh()
261 *
262 * Purpose: Initialize cmd fields related to error handling.
263 *
264 * Arguments: cmd - command that is ready to be queued.
265 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * Notes: This function has the job of initializing a number of
267 * fields related to error handling. Typically this will
268 * be called once for each command, as required.
269 */
Christoph Hellwig631c2282006-07-08 20:42:15 +0200270static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 cmd->serial_number = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200273 scsi_set_resid(cmd, 0);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900274 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (cmd->cmd_len == 0)
Boaz Harroshdb4742d2008-04-30 11:27:26 +0300276 cmd->cmd_len = scsi_command_size(cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279void scsi_device_unbusy(struct scsi_device *sdev)
280{
281 struct Scsi_Host *shost = sdev->host;
Mike Christief0c0a372008-08-17 15:24:38 -0500282 struct scsi_target *starget = scsi_target(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 unsigned long flags;
284
Christoph Hellwig74665012014-01-22 15:29:29 +0100285 atomic_dec(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100286 if (starget->can_queue > 0)
287 atomic_dec(&starget->target_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +0100288
James Bottomley939647e2005-09-18 15:05:20 -0500289 if (unlikely(scsi_host_in_recovery(shost) &&
Christoph Hellwig74665012014-01-22 15:29:29 +0100290 (shost->host_failed || shost->host_eh_scheduled))) {
291 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 scsi_eh_wakeup(shost);
Christoph Hellwig74665012014-01-22 15:29:29 +0100293 spin_unlock_irqrestore(shost->host_lock, flags);
294 }
295
Christoph Hellwig71e75c92014-04-11 19:07:01 +0200296 atomic_dec(&sdev->device_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Christoph Hellwigd2852032014-01-17 12:06:53 +0100299static void scsi_kick_queue(struct request_queue *q)
300{
301 if (q->mq_ops)
302 blk_mq_start_hw_queues(q);
303 else
304 blk_run_queue(q);
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*
308 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
309 * and call blk_run_queue for all the scsi_devices on the target -
310 * including current_sdev first.
311 *
312 * Called with *no* scsi locks held.
313 */
314static void scsi_single_lun_run(struct scsi_device *current_sdev)
315{
316 struct Scsi_Host *shost = current_sdev->host;
317 struct scsi_device *sdev, *tmp;
318 struct scsi_target *starget = scsi_target(current_sdev);
319 unsigned long flags;
320
321 spin_lock_irqsave(shost->host_lock, flags);
322 starget->starget_sdev_user = NULL;
323 spin_unlock_irqrestore(shost->host_lock, flags);
324
325 /*
326 * Call blk_run_queue for all LUNs on the target, starting with
327 * current_sdev. We race with others (to set starget_sdev_user),
328 * but in most cases, we will be first. Ideally, each LU on the
329 * target would get some limited time or requests on the target.
330 */
Christoph Hellwigd2852032014-01-17 12:06:53 +0100331 scsi_kick_queue(current_sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 spin_lock_irqsave(shost->host_lock, flags);
334 if (starget->starget_sdev_user)
335 goto out;
336 list_for_each_entry_safe(sdev, tmp, &starget->devices,
337 same_target_siblings) {
338 if (sdev == current_sdev)
339 continue;
340 if (scsi_device_get(sdev))
341 continue;
342
343 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100344 scsi_kick_queue(sdev->request_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 spin_lock_irqsave(shost->host_lock, flags);
346
347 scsi_device_put(sdev);
348 }
349 out:
350 spin_unlock_irqrestore(shost->host_lock, flags);
351}
352
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100353static inline bool scsi_device_is_busy(struct scsi_device *sdev)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400354{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100355 if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
356 return true;
357 if (atomic_read(&sdev->device_blocked) > 0)
358 return true;
359 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400360}
361
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100362static inline bool scsi_target_is_busy(struct scsi_target *starget)
Mike Christief0c0a372008-08-17 15:24:38 -0500363{
Christoph Hellwig2ccbb002014-03-26 10:35:00 +0100364 if (starget->can_queue > 0) {
365 if (atomic_read(&starget->target_busy) >= starget->can_queue)
366 return true;
367 if (atomic_read(&starget->target_blocked) > 0)
368 return true;
369 }
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100370 return false;
Mike Christief0c0a372008-08-17 15:24:38 -0500371}
372
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100373static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400374{
Christoph Hellwigcd9070c2014-01-23 12:07:41 +0100375 if (shost->can_queue > 0 &&
376 atomic_read(&shost->host_busy) >= shost->can_queue)
377 return true;
378 if (atomic_read(&shost->host_blocked) > 0)
379 return true;
380 if (shost->host_self_blocked)
381 return true;
382 return false;
Kiyoshi Ueda9d112512008-10-04 14:11:06 -0400383}
384
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800385static void scsi_starved_list_run(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Mike Christie2a3a59e2008-11-11 13:42:35 -0600387 LIST_HEAD(starved_list);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800388 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unsigned long flags;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 spin_lock_irqsave(shost->host_lock, flags);
Mike Christie2a3a59e2008-11-11 13:42:35 -0600392 list_splice_init(&shost->starved_list, &starved_list);
393
394 while (!list_empty(&starved_list)) {
James Bottomleye2eb7242013-07-02 15:05:26 +0200395 struct request_queue *slq;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * As long as shost is accepting commands and we have
399 * starved queues, call blk_run_queue. scsi_request_fn
400 * drops the queue_lock and can add us back to the
401 * starved_list.
402 *
403 * host_lock protects the starved_list and starved_entry.
404 * scsi_request_fn must get the host_lock before checking
405 * or modifying starved_list or starved_entry.
406 */
Mike Christie2a3a59e2008-11-11 13:42:35 -0600407 if (scsi_host_is_busy(shost))
Mike Christief0c0a372008-08-17 15:24:38 -0500408 break;
Mike Christief0c0a372008-08-17 15:24:38 -0500409
Mike Christie2a3a59e2008-11-11 13:42:35 -0600410 sdev = list_entry(starved_list.next,
411 struct scsi_device, starved_entry);
412 list_del_init(&sdev->starved_entry);
Mike Christief0c0a372008-08-17 15:24:38 -0500413 if (scsi_target_is_busy(scsi_target(sdev))) {
414 list_move_tail(&sdev->starved_entry,
415 &shost->starved_list);
416 continue;
417 }
418
James Bottomleye2eb7242013-07-02 15:05:26 +0200419 /*
420 * Once we drop the host lock, a racing scsi_remove_device()
421 * call may remove the sdev from the starved list and destroy
422 * it and the queue. Mitigate by taking a reference to the
423 * queue and never touching the sdev again after we drop the
424 * host lock. Note: if __scsi_remove_device() invokes
425 * blk_cleanup_queue() before the queue is run from this
426 * function then blk_run_queue() will return immediately since
427 * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
428 */
429 slq = sdev->request_queue;
430 if (!blk_get_queue(slq))
431 continue;
432 spin_unlock_irqrestore(shost->host_lock, flags);
433
Christoph Hellwigd2852032014-01-17 12:06:53 +0100434 scsi_kick_queue(slq);
James Bottomleye2eb7242013-07-02 15:05:26 +0200435 blk_put_queue(slq);
436
437 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Mike Christie2a3a59e2008-11-11 13:42:35 -0600439 /* put any unprocessed entries back */
440 list_splice(&starved_list, &shost->starved_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 spin_unlock_irqrestore(shost->host_lock, flags);
Christoph Hellwig21a05df2014-02-20 14:20:54 -0800442}
443
444/*
445 * Function: scsi_run_queue()
446 *
447 * Purpose: Select a proper request queue to serve next
448 *
449 * Arguments: q - last request's queue
450 *
451 * Returns: Nothing
452 *
453 * Notes: The previous command was completely finished, start
454 * a new one if possible.
455 */
456static void scsi_run_queue(struct request_queue *q)
457{
458 struct scsi_device *sdev = q->queuedata;
459
460 if (scsi_target(sdev)->single_lun)
461 scsi_single_lun_run(sdev);
462 if (!list_empty(&sdev->host->starved_list))
463 scsi_starved_list_run(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Christoph Hellwigd2852032014-01-17 12:06:53 +0100465 if (q->mq_ops)
466 blk_mq_start_stopped_hw_queues(q, false);
467 else
468 blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Jens Axboe9937a5e2011-05-17 11:04:44 +0200471void scsi_requeue_run_queue(struct work_struct *work)
472{
473 struct scsi_device *sdev;
474 struct request_queue *q;
475
476 sdev = container_of(work, struct scsi_device, requeue_work);
477 q = sdev->request_queue;
478 scsi_run_queue(q);
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
482 * Function: scsi_requeue_command()
483 *
484 * Purpose: Handle post-processing of completed commands.
485 *
486 * Arguments: q - queue to operate on
487 * cmd - command that may need to be requeued.
488 *
489 * Returns: Nothing
490 *
491 * Notes: After command completion, there may be blocks left
492 * over which weren't finished by the previous command
493 * this can be for a number of reasons - the main one is
494 * I/O errors in the middle of the request, in which case
495 * we need to request the blocks that come after the bad
496 * sector.
James Bottomleye91442b2005-09-09 10:44:16 -0500497 * Notes: Upon return, cmd is a stale pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
499static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
500{
Bart Van Assche940f5d42012-06-29 15:34:26 +0000501 struct scsi_device *sdev = cmd->device;
James Bottomleye91442b2005-09-09 10:44:16 -0500502 struct request *req = cmd->request;
Tejun Heo 283369c2005-04-24 02:06:36 -0500503 unsigned long flags;
504
Tejun Heo 283369c2005-04-24 02:06:36 -0500505 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig134997a2014-02-20 14:20:58 -0800506 blk_unprep_request(req);
507 req->special = NULL;
508 scsi_put_command(cmd);
James Bottomleye91442b2005-09-09 10:44:16 -0500509 blk_requeue_request(q, req);
Tejun Heo 283369c2005-04-24 02:06:36 -0500510 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 scsi_run_queue(q);
Bart Van Assche940f5d42012-06-29 15:34:26 +0000513
514 put_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517void scsi_run_host_queues(struct Scsi_Host *shost)
518{
519 struct scsi_device *sdev;
520
521 shost_for_each_device(sdev, shost)
522 scsi_run_queue(sdev->request_queue);
523}
524
Christoph Hellwigd2852032014-01-17 12:06:53 +0100525static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
526{
527 if (cmd->request->cmd_type == REQ_TYPE_FS) {
528 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
529
530 if (drv->uninit_command)
531 drv->uninit_command(cmd);
532 }
533}
534
535static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd)
536{
Ming Lin91dbc082016-04-04 14:48:07 -0700537 struct scsi_data_buffer *sdb;
538
Christoph Hellwigd2852032014-01-17 12:06:53 +0100539 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700540 sg_free_table_chained(&cmd->sdb.table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700541 if (cmd->request->next_rq) {
542 sdb = cmd->request->next_rq->special;
543 if (sdb)
Ming Lin001d63b2016-04-04 14:48:09 -0700544 sg_free_table_chained(&sdb->table, true);
Ming Lin91dbc082016-04-04 14:48:07 -0700545 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100546 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700547 sg_free_table_chained(&cmd->prot_sdb->table, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100548}
549
550static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
551{
552 struct scsi_device *sdev = cmd->device;
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +0530553 struct Scsi_Host *shost = sdev->host;
Christoph Hellwigd2852032014-01-17 12:06:53 +0100554 unsigned long flags;
555
Christoph Hellwigd2852032014-01-17 12:06:53 +0100556 scsi_mq_free_sgtables(cmd);
557 scsi_uninit_cmd(cmd);
558
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +0530559 if (shost->use_cmd_list) {
560 BUG_ON(list_empty(&cmd->list));
561 spin_lock_irqsave(&sdev->list_lock, flags);
562 list_del_init(&cmd->list);
563 spin_unlock_irqrestore(&sdev->list_lock, flags);
564 }
Christoph Hellwigd2852032014-01-17 12:06:53 +0100565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/*
568 * Function: scsi_release_buffers()
569 *
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200570 * Purpose: Free resources allocate for a scsi_command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 *
572 * Arguments: cmd - command that we are bailing.
573 *
574 * Lock status: Assumed that no lock is held upon entry.
575 *
576 * Returns: Nothing
577 *
578 * Notes: In the event that an upper level driver rejects a
579 * command, we must release resources allocated during
580 * the __init_io() function. Primarily this would involve
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200581 * the scatter-gather table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Christoph Hellwigf1bea552014-04-15 12:26:54 +0200583static void scsi_release_buffers(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200585 if (cmd->sdb.table.nents)
Ming Lin001d63b2016-04-04 14:48:09 -0700586 sg_free_table_chained(&cmd->sdb.table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200587
588 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
589
590 if (scsi_prot_sg_count(cmd))
Ming Lin001d63b2016-04-04 14:48:09 -0700591 sg_free_table_chained(&cmd->prot_sdb->table, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200594static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
595{
596 struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special;
597
Ming Lin001d63b2016-04-04 14:48:09 -0700598 sg_free_table_chained(&bidi_sdb->table, false);
Christoph Hellwigc682adf2014-05-01 16:51:02 +0200599 kmem_cache_free(scsi_sdb_cache, bidi_sdb);
600 cmd->request->next_rq->special = NULL;
601}
602
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800603static bool scsi_end_request(struct request *req, int error,
604 unsigned int bytes, unsigned int bidi_bytes)
605{
606 struct scsi_cmnd *cmd = req->special;
607 struct scsi_device *sdev = cmd->device;
608 struct request_queue *q = sdev->request_queue;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800609
610 if (blk_update_request(req, error, bytes))
611 return true;
612
613 /* Bidi request must be completed as a whole */
614 if (unlikely(bidi_bytes) &&
615 blk_update_request(req->next_rq, error, bidi_bytes))
616 return true;
617
618 if (blk_queue_add_random(q))
619 add_disk_randomness(req->rq_disk);
620
Christoph Hellwigd2852032014-01-17 12:06:53 +0100621 if (req->mq_ctx) {
622 /*
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700623 * In the MQ case the command gets freed by __blk_mq_end_request,
Christoph Hellwigd2852032014-01-17 12:06:53 +0100624 * so we have to do all cleanup that depends on it earlier.
625 *
626 * We also can't kick the queues from irq context, so we
627 * will have to defer it to a workqueue.
628 */
629 scsi_mq_uninit_cmd(cmd);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800630
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700631 __blk_mq_end_request(req, error);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100632
633 if (scsi_target(sdev)->single_lun ||
634 !list_empty(&sdev->host->starved_list))
635 kblockd_schedule_work(&sdev->requeue_work);
636 else
637 blk_mq_start_stopped_hw_queues(q, true);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100638 } else {
639 unsigned long flags;
640
Daniel Gryniewiczf81426a2014-09-16 10:41:13 -0400641 if (bidi_bytes)
642 scsi_release_bidi_buffers(cmd);
643
Christoph Hellwigd2852032014-01-17 12:06:53 +0100644 spin_lock_irqsave(q->queue_lock, flags);
645 blk_finish_request(req, error);
646 spin_unlock_irqrestore(q->queue_lock, flags);
647
Christoph Hellwigd2852032014-01-17 12:06:53 +0100648 scsi_release_buffers(cmd);
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700649
650 scsi_put_command(cmd);
651 scsi_run_queue(q);
Christoph Hellwigd2852032014-01-17 12:06:53 +0100652 }
653
Christoph Hellwigbb3ec622014-09-05 18:02:09 -0700654 put_device(&sdev->sdev_gendev);
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800655 return false;
656}
657
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200658/**
659 * __scsi_error_from_host_byte - translate SCSI error code into errno
660 * @cmd: SCSI command (unused)
661 * @result: scsi error code
662 *
663 * Translate SCSI error code into standard UNIX errno.
664 * Return values:
665 * -ENOLINK temporary transport failure
666 * -EREMOTEIO permanent target failure, do not retry
667 * -EBADE permanent nexus failure, retry on other path
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200668 * -ENOSPC No write space available
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200669 * -ENODATA Medium error
Hannes Reinecke0f7f6232013-07-01 15:16:23 +0200670 * -EIO unspecified I/O error
671 */
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100672static int __scsi_error_from_host_byte(struct scsi_cmnd *cmd, int result)
673{
674 int error = 0;
675
676 switch(host_byte(result)) {
677 case DID_TRANSPORT_FAILFAST:
678 error = -ENOLINK;
679 break;
680 case DID_TARGET_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000681 set_host_byte(cmd, DID_OK);
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100682 error = -EREMOTEIO;
683 break;
684 case DID_NEXUS_FAILURE:
Moger, Babu2082ebc2012-01-24 20:38:46 +0000685 set_host_byte(cmd, DID_OK);
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100686 error = -EBADE;
687 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200688 case DID_ALLOC_FAILURE:
689 set_host_byte(cmd, DID_OK);
690 error = -ENOSPC;
691 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200692 case DID_MEDIUM_ERROR:
693 set_host_byte(cmd, DID_OK);
694 error = -ENODATA;
695 break;
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100696 default:
697 error = -EIO;
698 break;
699 }
700
701 return error;
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*
705 * Function: scsi_io_completion()
706 *
707 * Purpose: Completion processing for block device I/O requests.
708 *
709 * Arguments: cmd - command that is finished.
710 *
711 * Lock status: Assumed that no lock is held upon entry.
712 *
713 * Returns: Nothing
714 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200715 * Notes: We will finish off the specified number of sectors. If we
716 * are done, the command block will be released and the queue
717 * function will be goosed. If we are not done then we have to
Alan Sternb60af5b2008-11-03 15:56:47 -0500718 * figure out what to do next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 *
Alan Sternb60af5b2008-11-03 15:56:47 -0500720 * a) We can call scsi_requeue_command(). The request
721 * will be unprepared and put back on the queue. Then
722 * a new command will be created for it. This should
723 * be used if we made forward progress, or if we want
724 * to switch from READ(10) to READ(6) for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 *
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200726 * b) We can call __scsi_queue_insert(). The request will
Alan Sternb60af5b2008-11-03 15:56:47 -0500727 * be put back on the queue and retried using the same
728 * command as before, possibly after a delay.
729 *
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800730 * c) We can call scsi_end_request() with -EIO to fail
Alan Sternb60af5b2008-11-03 15:56:47 -0500731 * the remainder of the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700733void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 int result = cmd->result;
Jens Axboe165125e2007-07-24 09:28:11 +0200736 struct request_queue *q = cmd->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct request *req = cmd->request;
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500738 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct scsi_sense_hdr sshdr;
Hannes Reinecke4753cbc2014-10-24 14:26:52 +0200740 bool sense_valid = false;
Hannes Reineckec11c0042014-10-24 14:27:01 +0200741 int sense_deferred = 0, level = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500742 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
743 ACTION_DELAYED_RETRY} action;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900744 unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (result) {
747 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
748 if (sense_valid)
749 sense_deferred = scsi_sense_is_deferred(&sshdr);
750 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200751
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200752 if (req->cmd_type == REQ_TYPE_BLOCK_PC) { /* SG_IO ioctl from block level */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (sense_valid && req->sense) {
755 /*
756 * SG_IO wants current and deferred errors
757 */
758 int len = 8 + cmd->sense_buffer[7];
759
760 if (len > SCSI_SENSE_BUFFERSIZE)
761 len = SCSI_SENSE_BUFFERSIZE;
762 memcpy(req->sense, cmd->sense_buffer, len);
763 req->sense_len = len;
764 }
James Bottomleyfa8e36c2008-04-02 18:11:52 -0500765 if (!sense_deferred)
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100766 error = __scsi_error_from_host_byte(cmd, result);
Pete Wyckoffb22f6872007-03-13 16:53:28 -0400767 }
Mike Snitzer27c41972012-05-31 15:05:33 -0400768 /*
769 * __scsi_error_from_host_byte may have reset the host_byte
770 */
771 req->errors = cmd->result;
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900772
773 req->resid_len = scsi_get_resid(cmd);
774
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200775 if (scsi_bidi_cmnd(cmd)) {
FUJITA Tomonorie6bb7a962009-05-11 17:56:08 +0900776 /*
777 * Bidi commands Must be complete as a whole,
778 * both sides at once.
779 */
780 req->next_rq->resid_len = scsi_in(cmd)->resid;
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800781 if (scsi_end_request(req, 0, blk_rq_bytes(req),
782 blk_rq_bytes(req->next_rq)))
783 BUG();
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200784 return;
785 }
James Bottomley89fb4cd2014-07-03 19:17:34 +0200786 } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
787 /*
788 * Certain non BLOCK_PC requests are commands that don't
789 * actually transfer anything (FLUSH), so cannot use
790 * good_bytes != blk_rq_bytes(req) as the signal for an error.
791 * This sets the error explicitly for the problem case.
792 */
793 error = __scsi_error_from_host_byte(cmd, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200796 /* no bidi support for !REQ_TYPE_BLOCK_PC yet */
797 BUG_ON(blk_bidi_rq(req));
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * Next deal with any sectors which we were able to correctly
801 * handle.
802 */
Hannes Reinecke91921e02014-06-25 16:39:59 +0200803 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
804 "%u sectors total, %d bytes done.\n",
805 blk_rq_sectors(req), good_bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
James Bottomleya9bddd72009-03-30 16:55:51 +0000807 /*
808 * Recovered errors need reporting, but they're always treated
809 * as success, so fiddle the result code here. For BLOCK_PC
810 * we already took a copy of the original into rq->errors which
811 * is what gets returned to the user
812 */
Douglas Gilberte7efe592010-01-03 13:51:15 -0500813 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
814 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
815 * print since caller wants ATA registers. Only occurs on
816 * SCSI ATA PASS_THROUGH commands when CK_COND=1
817 */
818 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
819 ;
820 else if (!(req->cmd_flags & REQ_QUIET))
Hannes Reinecked811b842014-10-24 14:26:45 +0200821 scsi_print_sense(cmd);
James Bottomleya9bddd72009-03-30 16:55:51 +0000822 result = 0;
823 /* BLOCK_PC may have set error */
824 error = 0;
825 }
826
827 /*
James Bottomleya621bac2016-05-13 12:04:06 -0700828 * special case: failed zero length commands always need to
829 * drop down into the retry code. Otherwise, if we finished
830 * all bytes in the request we are done now.
James Bottomleyd6b0c532006-07-02 10:06:28 -0500831 */
James Bottomleya621bac2016-05-13 12:04:06 -0700832 if (!(blk_rq_bytes(req) == 0 && error) &&
833 !scsi_end_request(req, error, good_bytes, 0))
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800834 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200835
836 /*
837 * Kill remainder if no retrys.
838 */
839 if (error && scsi_noretry_cmd(cmd)) {
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800840 if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
841 BUG();
842 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200843 }
844
845 /*
846 * If there had been no error, but we have leftover bytes in the
847 * requeues just queue the command up again.
848 */
849 if (result == 0)
850 goto requeue;
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700851
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100852 error = __scsi_error_from_host_byte(cmd, result);
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500853
Alan Sternb60af5b2008-11-03 15:56:47 -0500854 if (host_byte(result) == DID_RESET) {
855 /* Third party bus reset or reset for error recovery
856 * reasons. Just retry the command and see what
857 * happens.
858 */
859 action = ACTION_RETRY;
860 } else if (sense_valid && !sense_deferred) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 switch (sshdr.sense_key) {
862 case UNIT_ATTENTION:
863 if (cmd->device->removable) {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700864 /* Detected disc change. Set a bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 * and quietly refuse further access.
866 */
867 cmd->device->changed = 1;
Alan Sternb60af5b2008-11-03 15:56:47 -0500868 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 } else {
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700870 /* Must have been a power glitch, or a
871 * bus reset. Could not have been a
872 * media change, so we just retry the
Alan Sternb60af5b2008-11-03 15:56:47 -0500873 * command and see what happens.
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700874 */
Alan Sternb60af5b2008-11-03 15:56:47 -0500875 action = ACTION_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877 break;
878 case ILLEGAL_REQUEST:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700879 /* If we had an ILLEGAL REQUEST returned, then
880 * we may have performed an unsupported
881 * command. The only thing this should be
882 * would be a ten byte read where only a six
883 * byte read was supported. Also, on a system
884 * where READ CAPACITY failed, we may have
885 * read past the end of the disk.
886 */
Jens Axboe26a68012005-11-29 21:03:34 +0100887 if ((cmd->device->use_10_for_rw &&
888 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 (cmd->cmnd[0] == READ_10 ||
890 cmd->cmnd[0] == WRITE_10)) {
Alan Sternb60af5b2008-11-03 15:56:47 -0500891 /* This will issue a new 6-byte command. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 cmd->device->use_10_for_rw = 0;
Alan Sternb60af5b2008-11-03 15:56:47 -0500893 action = ACTION_REPREP;
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500894 } else if (sshdr.asc == 0x10) /* DIX */ {
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500895 action = ACTION_FAIL;
896 error = -EILSEQ;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500897 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
Martin K. Petersen5db44862012-09-18 12:19:32 -0400898 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500899 action = ACTION_FAIL;
Martin K. Petersen66a651a2012-02-13 15:38:22 -0500900 error = -EREMOTEIO;
Alan Sternb60af5b2008-11-03 15:56:47 -0500901 } else
902 action = ACTION_FAIL;
903 break;
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400904 case ABORTED_COMMAND:
James Bottomley126c09822009-02-19 21:48:54 +0000905 action = ACTION_FAIL;
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200906 if (sshdr.asc == 0x10) /* DIF */
Martin K. Petersen3e695f82009-01-04 03:04:31 -0500907 error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 break;
909 case NOT_READY:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700910 /* If the device is in the process of becoming
James Bottomleyf3e93f72006-04-25 16:48:30 -0500911 * ready, or has a temporary blockage, retry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
James Bottomleyf3e93f72006-04-25 16:48:30 -0500913 if (sshdr.asc == 0x04) {
914 switch (sshdr.ascq) {
915 case 0x01: /* becoming ready */
916 case 0x04: /* format in progress */
917 case 0x05: /* rebuild in progress */
918 case 0x06: /* recalculation in progress */
919 case 0x07: /* operation in progress */
920 case 0x08: /* Long write in progress */
921 case 0x09: /* self test in progress */
Martin K. Petersend8705f12009-11-26 12:00:41 -0500922 case 0x14: /* space allocation in progress */
Alan Sternb60af5b2008-11-03 15:56:47 -0500923 action = ACTION_DELAYED_RETRY;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500924 break;
Alan Stern3dbf6a52008-12-15 10:31:28 -0500925 default:
Alan Stern3dbf6a52008-12-15 10:31:28 -0500926 action = ACTION_FAIL;
927 break;
James Bottomleyf3e93f72006-04-25 16:48:30 -0500928 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200929 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500930 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500931 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 case VOLUME_OVERFLOW:
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700933 /* See SSC3rXX or current. */
Alan Sternb60af5b2008-11-03 15:56:47 -0500934 action = ACTION_FAIL;
935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 default:
Alan Sternb60af5b2008-11-03 15:56:47 -0500937 action = ACTION_FAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 break;
939 }
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200940 } else
Alan Sternb60af5b2008-11-03 15:56:47 -0500941 action = ACTION_FAIL;
Alan Sternb60af5b2008-11-03 15:56:47 -0500942
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900943 if (action != ACTION_FAIL &&
Maurizio Lombardie6c11db2014-07-10 09:41:53 +0200944 time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900945 action = ACTION_FAIL;
Eiichi Tsukataee60b2c2014-02-11 14:29:52 +0900946
Alan Sternb60af5b2008-11-03 15:56:47 -0500947 switch (action) {
948 case ACTION_FAIL:
949 /* Give up and fail the remainder of the request */
Hannes Reineckef1569ff2014-10-24 14:27:07 +0200950 if (!(req->cmd_flags & REQ_QUIET)) {
951 static DEFINE_RATELIMIT_STATE(_rs,
952 DEFAULT_RATELIMIT_INTERVAL,
953 DEFAULT_RATELIMIT_BURST);
954
955 if (unlikely(scsi_logging_level))
956 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
957 SCSI_LOG_MLCOMPLETE_BITS);
958
959 /*
960 * if logging is enabled the failure will be printed
961 * in scsi_log_completion(), so avoid duplicate messages
962 */
963 if (!level && __ratelimit(&_rs)) {
964 scsi_print_result(cmd, NULL, FAILED);
965 if (driver_byte(result) & DRIVER_SENSE)
966 scsi_print_sense(cmd);
967 scsi_print_command(cmd);
968 }
James Bottomley3173d8c2005-09-04 11:32:05 -0500969 }
Christoph Hellwigf6d47e72014-02-16 06:16:13 -0800970 if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
971 return;
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200972 /*FALLTHRU*/
Alan Sternb60af5b2008-11-03 15:56:47 -0500973 case ACTION_REPREP:
Christoph Hellwigbc85dc52014-05-01 16:51:03 +0200974 requeue:
Alan Sternb60af5b2008-11-03 15:56:47 -0500975 /* Unprep the request and put it back at the head of the queue.
976 * A new command will be prepared and issued.
977 */
Christoph Hellwigd2852032014-01-17 12:06:53 +0100978 if (q->mq_ops) {
979 cmd->request->cmd_flags &= ~REQ_DONTPREP;
980 scsi_mq_uninit_cmd(cmd);
981 scsi_mq_requeue_cmd(cmd);
982 } else {
983 scsi_release_buffers(cmd);
984 scsi_requeue_command(q, cmd);
985 }
Alan Sternb60af5b2008-11-03 15:56:47 -0500986 break;
987 case ACTION_RETRY:
988 /* Retry the same command immediately */
James Bottomley4f5299a2009-01-02 10:42:21 -0600989 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -0500990 break;
991 case ACTION_DELAYED_RETRY:
992 /* Retry the same command after a delay */
James Bottomley4f5299a2009-01-02 10:42:21 -0600993 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
Alan Sternb60af5b2008-11-03 15:56:47 -0500994 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Christoph Hellwig3c356bd2014-09-05 18:20:23 -0700998static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001000 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001003 * If sg table allocation fails, requeue request later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 */
Ming Lin001d63b2016-04-04 14:48:09 -07001005 if (unlikely(sg_alloc_table_chained(&sdb->table, req->nr_phys_segments,
Ming Lin22cc3d42016-04-04 14:48:08 -07001006 sdb->table.sgl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return BLKPREP_DEFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /*
1010 * Next, walk the list, and fill in the addresses and sizes of
1011 * each segment.
1012 */
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001013 count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
1014 BUG_ON(count > sdb->table.nents);
1015 sdb->table.nents = count;
Tejun Heo1011c1b2009-05-07 22:24:45 +09001016 sdb->length = blk_rq_bytes(req);
Rusty Russell4a03d902007-11-19 11:28:48 +11001017 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001019
1020/*
1021 * Function: scsi_init_io()
1022 *
1023 * Purpose: SCSI I/O initialize function.
1024 *
1025 * Arguments: cmd - Command descriptor we wish to initialize
1026 *
1027 * Returns: 0 on success
1028 * BLKPREP_DEFER if the failure is retryable
1029 * BLKPREP_KILL if the failure is fatal
1030 */
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001031int scsi_init_io(struct scsi_cmnd *cmd)
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001032{
Christoph Hellwig5e012aa2014-04-15 12:24:55 +02001033 struct scsi_device *sdev = cmd->device;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001034 struct request *rq = cmd->request;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001035 bool is_mq = (rq->mq_ctx != NULL);
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001036 int error;
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001037
Johannes Thumshirna026c972017-01-31 10:16:00 +01001038 if (WARN_ON_ONCE(!rq->nr_phys_segments))
1039 return -EINVAL;
Christoph Hellwig635d98b2014-06-28 11:51:01 +02001040
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001041 error = scsi_init_sgtable(rq, &cmd->sdb);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001042 if (error)
1043 goto err_exit;
1044
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001045 if (blk_bidi_rq(rq)) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001046 if (!rq->q->mq_ops) {
1047 struct scsi_data_buffer *bidi_sdb =
1048 kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC);
1049 if (!bidi_sdb) {
1050 error = BLKPREP_DEFER;
1051 goto err_exit;
1052 }
1053
1054 rq->next_rq->special = bidi_sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001055 }
1056
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001057 error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special);
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001058 if (error)
1059 goto err_exit;
1060 }
1061
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001062 if (blk_integrity_rq(rq)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001063 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1064 int ivecs, count;
1065
Ewan D. Milne91724c22015-01-15 10:02:12 -05001066 if (prot_sdb == NULL) {
1067 /*
1068 * This can happen if someone (e.g. multipath)
1069 * queues a command to a device on an adapter
1070 * that does not support DIX.
1071 */
1072 WARN_ON_ONCE(1);
1073 error = BLKPREP_KILL;
1074 goto err_exit;
1075 }
1076
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001077 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001078
Ming Lin001d63b2016-04-04 14:48:09 -07001079 if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
Ming Lin22cc3d42016-04-04 14:48:08 -07001080 prot_sdb->table.sgl)) {
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001081 error = BLKPREP_DEFER;
1082 goto err_exit;
1083 }
1084
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001085 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001086 prot_sdb->table.sgl);
1087 BUG_ON(unlikely(count > ivecs));
Martin K. Petersen13f05c82010-09-10 20:50:10 +02001088 BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q)));
Martin K. Petersen7027ad72008-07-17 17:08:48 -04001089
1090 cmd->prot_sdb = prot_sdb;
1091 cmd->prot_sdb->table.nents = count;
1092 }
1093
Christoph Hellwigd2852032014-01-17 12:06:53 +01001094 return BLKPREP_OK;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001095err_exit:
Christoph Hellwigd2852032014-01-17 12:06:53 +01001096 if (is_mq) {
1097 scsi_mq_free_sgtables(cmd);
1098 } else {
1099 scsi_release_buffers(cmd);
1100 cmd->request->special = NULL;
1101 scsi_put_command(cmd);
1102 put_device(&sdev->sdev_gendev);
1103 }
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001104 return error;
1105}
Boaz Harroshbb52d822007-12-13 16:14:27 -08001106EXPORT_SYMBOL(scsi_init_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Christoph Hellwig3b003152006-11-04 20:10:55 +01001108static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
1109 struct request *req)
1110{
1111 struct scsi_cmnd *cmd;
1112
1113 if (!req->special) {
Christoph Hellwig04796332014-02-20 14:20:55 -08001114 /* Bail if we can't get a reference to the device */
1115 if (!get_device(&sdev->sdev_gendev))
Christoph Hellwig3b003152006-11-04 20:10:55 +01001116 return NULL;
Christoph Hellwig04796332014-02-20 14:20:55 -08001117
1118 cmd = scsi_get_command(sdev, GFP_ATOMIC);
1119 if (unlikely(!cmd)) {
1120 put_device(&sdev->sdev_gendev);
1121 return NULL;
1122 }
Christoph Hellwig3b003152006-11-04 20:10:55 +01001123 req->special = cmd;
1124 } else {
1125 cmd = req->special;
1126 }
1127
1128 /* pull a tag out of the request if we have one */
1129 cmd->tag = req->tag;
1130 cmd->request = req;
1131
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001132 cmd->cmnd = req->cmd;
Martin K. Petersen72f7d322011-03-08 02:03:59 -05001133 cmd->prot_op = SCSI_PROT_NORMAL;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001134
Christoph Hellwig3b003152006-11-04 20:10:55 +01001135 return cmd;
1136}
1137
Christoph Hellwig4f1e5762014-06-28 12:36:28 +02001138static int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
James Bottomley7b163182005-12-15 20:17:02 -06001139{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001140 struct scsi_cmnd *cmd = req->special;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001141
1142 /*
1143 * BLOCK_PC requests may transfer data, in which case they must
1144 * a bio attached to them. Or they might contain a SCSI command
1145 * that does not transfer data, in which case they may optionally
1146 * submit a request without an attached bio.
1147 */
1148 if (req->bio) {
Christoph Hellwig3c356bd2014-09-05 18:20:23 -07001149 int ret = scsi_init_io(cmd);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001150 if (unlikely(ret))
1151 return ret;
1152 } else {
Tejun Heob0790412009-05-07 22:24:42 +09001153 BUG_ON(blk_rq_bytes(req));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001154
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001155 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
Christoph Hellwig3b003152006-11-04 20:10:55 +01001156 }
James Bottomley7b163182005-12-15 20:17:02 -06001157
James Bottomley7b163182005-12-15 20:17:02 -06001158 cmd->cmd_len = req->cmd_len;
Tejun Heob0790412009-05-07 22:24:42 +09001159 cmd->transfersize = blk_rq_bytes(req);
James Bottomley7b163182005-12-15 20:17:02 -06001160 cmd->allowed = req->retries;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001161 return BLKPREP_OK;
1162}
1163
1164/*
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001165 * Setup a REQ_TYPE_FS command. These are simple request from filesystems
1166 * that still need to be translated to SCSI CDBs from the ULD.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001167 */
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001168static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
Christoph Hellwig3b003152006-11-04 20:10:55 +01001169{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001170 struct scsi_cmnd *cmd = req->special;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001171
Christoph Hellwigee14c672015-08-27 14:16:59 +02001172 if (unlikely(sdev->handler && sdev->handler->prep_fn)) {
1173 int ret = sdev->handler->prep_fn(sdev, req);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001174 if (ret != BLKPREP_OK)
1175 return ret;
1176 }
1177
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001178 memset(cmd->cmnd, 0, BLK_MAX_CDB);
Christoph Hellwig3868cf82014-06-28 11:58:42 +02001179 return scsi_cmd_to_driver(cmd)->init_command(cmd);
James Bottomley7b163182005-12-15 20:17:02 -06001180}
James Bottomley7b163182005-12-15 20:17:02 -06001181
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001182static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req)
1183{
1184 struct scsi_cmnd *cmd = req->special;
1185
1186 if (!blk_rq_bytes(req))
1187 cmd->sc_data_direction = DMA_NONE;
1188 else if (rq_data_dir(req) == WRITE)
1189 cmd->sc_data_direction = DMA_TO_DEVICE;
1190 else
1191 cmd->sc_data_direction = DMA_FROM_DEVICE;
1192
1193 switch (req->cmd_type) {
1194 case REQ_TYPE_FS:
1195 return scsi_setup_fs_cmnd(sdev, req);
1196 case REQ_TYPE_BLOCK_PC:
1197 return scsi_setup_blk_pc_cmnd(sdev, req);
1198 default:
1199 return BLKPREP_KILL;
1200 }
1201}
1202
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001203static int
1204scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Christoph Hellwig3b003152006-11-04 20:10:55 +01001206 int ret = BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 /*
Christoph Hellwig3b003152006-11-04 20:10:55 +01001209 * If the device is not in running state we will reject some
1210 * or all commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
Christoph Hellwig3b003152006-11-04 20:10:55 +01001213 switch (sdev->sdev_state) {
1214 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05001215 case SDEV_TRANSPORT_OFFLINE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001216 /*
1217 * If the device is offline we refuse to process any
1218 * commands. The device must be brought online
1219 * before trying any recovery commands.
1220 */
1221 sdev_printk(KERN_ERR, sdev,
1222 "rejecting I/O to offline device\n");
1223 ret = BLKPREP_KILL;
1224 break;
1225 case SDEV_DEL:
1226 /*
1227 * If the device is fully deleted, we refuse to
1228 * process any commands as well.
1229 */
James Bottomley9ccfc752005-10-02 11:45:08 -05001230 sdev_printk(KERN_ERR, sdev,
1231 "rejecting I/O to dead device\n");
Christoph Hellwig3b003152006-11-04 20:10:55 +01001232 ret = BLKPREP_KILL;
1233 break;
Christoph Hellwig3b003152006-11-04 20:10:55 +01001234 case SDEV_BLOCK:
James Bottomley6f4267e2008-08-22 16:53:31 -05001235 case SDEV_CREATED_BLOCK:
Bart Van Asschebba0bdd2015-03-04 10:31:47 +01001236 ret = BLKPREP_DEFER;
1237 break;
1238 case SDEV_QUIESCE:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001239 /*
1240 * If the devices is blocked we defer normal commands.
1241 */
1242 if (!(req->cmd_flags & REQ_PREEMPT))
1243 ret = BLKPREP_DEFER;
1244 break;
1245 default:
1246 /*
1247 * For any other not fully online state we only allow
1248 * special commands. In particular any user initiated
1249 * command is not allowed.
1250 */
1251 if (!(req->cmd_flags & REQ_PREEMPT))
1252 ret = BLKPREP_KILL;
1253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001256 return ret;
1257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001259static int
1260scsi_prep_return(struct request_queue *q, struct request *req, int ret)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001261{
1262 struct scsi_device *sdev = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Christoph Hellwig3b003152006-11-04 20:10:55 +01001264 switch (ret) {
1265 case BLKPREP_KILL:
jiangyiwene1cd3912016-02-16 20:14:13 +08001266 case BLKPREP_INVALID:
Christoph Hellwig3b003152006-11-04 20:10:55 +01001267 req->errors = DID_NO_CONNECT << 16;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001268 /* release the command and kill it */
1269 if (req->special) {
1270 struct scsi_cmnd *cmd = req->special;
1271 scsi_release_buffers(cmd);
1272 scsi_put_command(cmd);
Christoph Hellwig68c03d92014-04-15 12:24:56 +02001273 put_device(&sdev->sdev_gendev);
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001274 req->special = NULL;
1275 }
Christoph Hellwig3b003152006-11-04 20:10:55 +01001276 break;
1277 case BLKPREP_DEFER:
1278 /*
Tejun Heo9934c8c2009-05-08 11:54:16 +09001279 * If we defer, the blk_peek_request() returns NULL, but the
Jens Axboea488e742010-04-16 21:13:15 +02001280 * queue must be restarted, so we schedule a callback to happen
1281 * shortly.
Christoph Hellwig3b003152006-11-04 20:10:55 +01001282 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001283 if (atomic_read(&sdev->device_busy) == 0)
Jens Axboea488e742010-04-16 21:13:15 +02001284 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig3b003152006-11-04 20:10:55 +01001285 break;
1286 default:
1287 req->cmd_flags |= REQ_DONTPREP;
1288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Christoph Hellwig3b003152006-11-04 20:10:55 +01001290 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001292
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001293static int scsi_prep_fn(struct request_queue *q, struct request *req)
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001294{
1295 struct scsi_device *sdev = q->queuedata;
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001296 struct scsi_cmnd *cmd;
1297 int ret;
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001298
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001299 ret = scsi_prep_state_check(sdev, req);
1300 if (ret != BLKPREP_OK)
1301 goto out;
1302
1303 cmd = scsi_get_cmd_from_req(sdev, req);
1304 if (unlikely(!cmd)) {
1305 ret = BLKPREP_DEFER;
1306 goto out;
1307 }
1308
Christoph Hellwig6af7a4f2014-07-08 13:16:17 +02001309 ret = scsi_setup_cmnd(sdev, req);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001310out:
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001311 return scsi_prep_return(q, req, ret);
1312}
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001313
1314static void scsi_unprep_fn(struct request_queue *q, struct request *req)
1315{
Christoph Hellwigd2852032014-01-17 12:06:53 +01001316 scsi_uninit_cmd(req->special);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001317}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319/*
1320 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1321 * return 0.
1322 *
1323 * Called with the queue_lock held.
1324 */
1325static inline int scsi_dev_queue_ready(struct request_queue *q,
1326 struct scsi_device *sdev)
1327{
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001328 unsigned int busy;
1329
1330 busy = atomic_inc_return(&sdev->device_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001331 if (atomic_read(&sdev->device_blocked)) {
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001332 if (busy)
1333 goto out_dec;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /*
1336 * unblock after device_blocked iterates to zero
1337 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001338 if (atomic_dec_return(&sdev->device_blocked) > 0) {
Christoph Hellwigd2852032014-01-17 12:06:53 +01001339 /*
1340 * For the MQ case we take care of this in the caller.
1341 */
1342 if (!q->mq_ops)
1343 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001344 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001346 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1347 "unblocking device at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001349
1350 if (busy >= sdev->queue_depth)
1351 goto out_dec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 return 1;
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001354out_dec:
1355 atomic_dec(&sdev->device_busy);
1356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Mike Christief0c0a372008-08-17 15:24:38 -05001359/*
1360 * scsi_target_queue_ready: checks if there we can send commands to target
1361 * @sdev: scsi device on starget to check.
Mike Christief0c0a372008-08-17 15:24:38 -05001362 */
1363static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1364 struct scsi_device *sdev)
1365{
1366 struct scsi_target *starget = scsi_target(sdev);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001367 unsigned int busy;
Mike Christief0c0a372008-08-17 15:24:38 -05001368
1369 if (starget->single_lun) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001370 spin_lock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001371 if (starget->starget_sdev_user &&
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001372 starget->starget_sdev_user != sdev) {
1373 spin_unlock_irq(shost->host_lock);
1374 return 0;
1375 }
Mike Christief0c0a372008-08-17 15:24:38 -05001376 starget->starget_sdev_user = sdev;
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001377 spin_unlock_irq(shost->host_lock);
Mike Christief0c0a372008-08-17 15:24:38 -05001378 }
1379
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001380 if (starget->can_queue <= 0)
1381 return 1;
1382
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001383 busy = atomic_inc_return(&starget->target_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001384 if (atomic_read(&starget->target_blocked) > 0) {
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001385 if (busy)
1386 goto starved;
1387
Mike Christief0c0a372008-08-17 15:24:38 -05001388 /*
1389 * unblock after target_blocked iterates to zero
1390 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001391 if (atomic_dec_return(&starget->target_blocked) > 0)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001392 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001393
1394 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1395 "unblocking target at zero depth\n"));
Mike Christief0c0a372008-08-17 15:24:38 -05001396 }
1397
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001398 if (busy >= starget->can_queue)
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001399 goto starved;
Mike Christief0c0a372008-08-17 15:24:38 -05001400
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001401 return 1;
1402
1403starved:
1404 spin_lock_irq(shost->host_lock);
1405 list_move_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001406 spin_unlock_irq(shost->host_lock);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001407out_dec:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001408 if (starget->can_queue > 0)
1409 atomic_dec(&starget->target_busy);
Christoph Hellwig7ae65c02014-01-22 14:49:41 +01001410 return 0;
Mike Christief0c0a372008-08-17 15:24:38 -05001411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/*
1414 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1415 * return 0. We must end up running the queue again whenever 0 is
1416 * returned, else IO can hang.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
1418static inline int scsi_host_queue_ready(struct request_queue *q,
1419 struct Scsi_Host *shost,
1420 struct scsi_device *sdev)
1421{
Christoph Hellwig74665012014-01-22 15:29:29 +01001422 unsigned int busy;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001423
James Bottomley939647e2005-09-18 15:05:20 -05001424 if (scsi_host_in_recovery(shost))
Christoph Hellwig74665012014-01-22 15:29:29 +01001425 return 0;
1426
1427 busy = atomic_inc_return(&shost->host_busy) - 1;
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001428 if (atomic_read(&shost->host_blocked) > 0) {
Christoph Hellwig74665012014-01-22 15:29:29 +01001429 if (busy)
1430 goto starved;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /*
1433 * unblock after host_blocked iterates to zero
1434 */
Christoph Hellwigcd9070c2014-01-23 12:07:41 +01001435 if (atomic_dec_return(&shost->host_blocked) > 0)
Christoph Hellwig74665012014-01-22 15:29:29 +01001436 goto out_dec;
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001437
1438 SCSI_LOG_MLQUEUE(3,
1439 shost_printk(KERN_INFO, shost,
1440 "unblocking host at zero depth\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
Christoph Hellwig74665012014-01-22 15:29:29 +01001442
1443 if (shost->can_queue > 0 && busy >= shost->can_queue)
1444 goto starved;
1445 if (shost->host_self_blocked)
1446 goto starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 /* We're OK to process the command, so we can't be starved */
Christoph Hellwig74665012014-01-22 15:29:29 +01001449 if (!list_empty(&sdev->starved_entry)) {
1450 spin_lock_irq(shost->host_lock);
1451 if (!list_empty(&sdev->starved_entry))
1452 list_del_init(&sdev->starved_entry);
1453 spin_unlock_irq(shost->host_lock);
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Christoph Hellwig74665012014-01-22 15:29:29 +01001456 return 1;
1457
1458starved:
1459 spin_lock_irq(shost->host_lock);
1460 if (list_empty(&sdev->starved_entry))
1461 list_add_tail(&sdev->starved_entry, &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001462 spin_unlock_irq(shost->host_lock);
Christoph Hellwig74665012014-01-22 15:29:29 +01001463out_dec:
1464 atomic_dec(&shost->host_busy);
1465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
1468/*
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001469 * Busy state exporting function for request stacking drivers.
1470 *
1471 * For efficiency, no lock is taken to check the busy state of
1472 * shost/starget/sdev, since the returned value is not guaranteed and
1473 * may be changed after request stacking drivers call the function,
1474 * regardless of taking lock or not.
1475 *
Bart Van Assche67bd9412012-06-29 15:33:22 +00001476 * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
1477 * needs to return 'not busy'. Otherwise, request stacking drivers
1478 * may hold requests forever.
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001479 */
1480static int scsi_lld_busy(struct request_queue *q)
1481{
1482 struct scsi_device *sdev = q->queuedata;
1483 struct Scsi_Host *shost;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001484
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001485 if (blk_queue_dying(q))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001486 return 0;
1487
1488 shost = sdev->host;
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001489
Jun'ichi Nomurab7e94a12012-05-22 18:57:17 +09001490 /*
1491 * Ignore host/starget busy state.
1492 * Since block layer does not have a concept of fairness across
1493 * multiple queues, congestion of host/starget needs to be handled
1494 * in SCSI layer.
1495 */
1496 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04001497 return 1;
1498
1499 return 0;
1500}
1501
1502/*
James Bottomleye91442b2005-09-09 10:44:16 -05001503 * Kill a request for a dead device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
Jens Axboe165125e2007-07-24 09:28:11 +02001505static void scsi_kill_request(struct request *req, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
James Bottomleye91442b2005-09-09 10:44:16 -05001507 struct scsi_cmnd *cmd = req->special;
Jiri Slaby03b14702009-09-23 16:15:35 +02001508 struct scsi_device *sdev;
1509 struct scsi_target *starget;
1510 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Tejun Heo9934c8c2009-05-08 11:54:16 +09001512 blk_start_request(req);
James Bottomley788ce432005-09-09 13:40:23 -05001513
Hannes Reinecke74571812011-11-09 08:39:24 +01001514 scmd_printk(KERN_INFO, cmd, "killing request\n");
1515
Jiri Slaby03b14702009-09-23 16:15:35 +02001516 sdev = cmd->device;
1517 starget = scsi_target(sdev);
1518 shost = sdev->host;
James Bottomleye91442b2005-09-09 10:44:16 -05001519 scsi_init_cmd_errh(cmd);
1520 cmd->result = DID_NO_CONNECT << 16;
1521 atomic_inc(&cmd->device->iorequest_cnt);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001522
1523 /*
1524 * SCSI request completion path will do scsi_device_unbusy(),
1525 * bump busy counts. To bump the counters, we need to dance
1526 * with the locks as normal issue path does.
1527 */
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001528 atomic_inc(&sdev->device_busy);
Christoph Hellwig74665012014-01-22 15:29:29 +01001529 atomic_inc(&shost->host_busy);
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001530 if (starget->can_queue > 0)
1531 atomic_inc(&starget->target_busy);
Tejun Heoe36e0c82006-04-11 17:27:53 +09001532
Jens Axboe242f9dc2008-09-14 05:55:09 -07001533 blk_complete_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Jens Axboe1aea6432006-01-09 16:03:03 +01001536static void scsi_softirq_done(struct request *rq)
1537{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001538 struct scsi_cmnd *cmd = rq->special;
1539 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
Jens Axboe1aea6432006-01-09 16:03:03 +01001540 int disposition;
1541
1542 INIT_LIST_HEAD(&cmd->eh_entry);
1543
Jens Axboe242f9dc2008-09-14 05:55:09 -07001544 atomic_inc(&cmd->device->iodone_cnt);
1545 if (cmd->result)
1546 atomic_inc(&cmd->device->ioerr_cnt);
1547
Jens Axboe1aea6432006-01-09 16:03:03 +01001548 disposition = scsi_decide_disposition(cmd);
1549 if (disposition != SUCCESS &&
1550 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
1551 sdev_printk(KERN_ERR, cmd->device,
1552 "timing out command, waited %lus\n",
1553 wait_for/HZ);
1554 disposition = SUCCESS;
1555 }
Hannes Reinecke91921e02014-06-25 16:39:59 +02001556
Jens Axboe1aea6432006-01-09 16:03:03 +01001557 scsi_log_completion(cmd, disposition);
1558
1559 switch (disposition) {
1560 case SUCCESS:
1561 scsi_finish_command(cmd);
1562 break;
1563 case NEEDS_RETRY:
Christoph Hellwig596f4822007-01-02 12:56:00 +01001564 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
Jens Axboe1aea6432006-01-09 16:03:03 +01001565 break;
1566 case ADD_TO_MLQUEUE:
1567 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1568 break;
1569 default:
1570 if (!scsi_eh_scmd_add(cmd, 0))
1571 scsi_finish_command(cmd);
1572 }
1573}
1574
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001575/**
Christoph Hellwig82042a22014-09-05 18:23:07 -07001576 * scsi_dispatch_command - Dispatch a command to the low-level driver.
1577 * @cmd: command block we are dispatching.
1578 *
1579 * Return: nonzero return request was rejected and device's queue needs to be
1580 * plugged.
1581 */
1582static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
1583{
1584 struct Scsi_Host *host = cmd->device->host;
1585 int rtn = 0;
1586
1587 atomic_inc(&cmd->device->iorequest_cnt);
1588
1589 /* check if the device is still usable */
1590 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
1591 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
1592 * returns an immediate error upwards, and signals
1593 * that the device is no longer present */
1594 cmd->result = DID_NO_CONNECT << 16;
1595 goto done;
1596 }
1597
1598 /* Check to see if the scsi lld made this device blocked. */
1599 if (unlikely(scsi_device_blocked(cmd->device))) {
1600 /*
1601 * in blocked state, the command is just put back on
1602 * the device queue. The suspend state has already
1603 * blocked the queue so future requests should not
1604 * occur until the device transitions out of the
1605 * suspend state.
1606 */
1607 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1608 "queuecommand : device blocked\n"));
1609 return SCSI_MLQUEUE_DEVICE_BUSY;
1610 }
1611
1612 /* Store the LUN value in cmnd, if needed. */
1613 if (cmd->device->lun_in_cdb)
1614 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
1615 (cmd->device->lun << 5 & 0xe0);
1616
1617 scsi_log_send(cmd);
1618
1619 /*
1620 * Before we queue this command, check if the command
1621 * length exceeds what the host adapter can handle.
1622 */
1623 if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
1624 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1625 "queuecommand : command too long. "
1626 "cdb_size=%d host->max_cmd_len=%d\n",
1627 cmd->cmd_len, cmd->device->host->max_cmd_len));
1628 cmd->result = (DID_ABORT << 16);
1629 goto done;
1630 }
1631
1632 if (unlikely(host->shost_state == SHOST_DEL)) {
1633 cmd->result = (DID_NO_CONNECT << 16);
1634 goto done;
1635
1636 }
1637
1638 trace_scsi_dispatch_cmd_start(cmd);
1639 rtn = host->hostt->queuecommand(host, cmd);
1640 if (rtn) {
1641 trace_scsi_dispatch_cmd_error(cmd, rtn);
1642 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
1643 rtn != SCSI_MLQUEUE_TARGET_BUSY)
1644 rtn = SCSI_MLQUEUE_HOST_BUSY;
1645
1646 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1647 "queuecommand : request rejected\n"));
1648 }
1649
1650 return rtn;
1651 done:
1652 cmd->scsi_done(cmd);
1653 return 0;
1654}
1655
1656/**
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001657 * scsi_done - Invoke completion on finished SCSI command.
1658 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
1659 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
1660 *
1661 * Description: This function is the mid-level's (SCSI Core) interrupt routine,
1662 * which regains ownership of the SCSI command (de facto) from a LLDD, and
1663 * calls blk_complete_request() for further processing.
1664 *
1665 * This function is interrupt context safe.
1666 */
1667static void scsi_done(struct scsi_cmnd *cmd)
1668{
1669 trace_scsi_dispatch_cmd_done(cmd);
1670 blk_complete_request(cmd->request);
1671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673/*
1674 * Function: scsi_request_fn()
1675 *
1676 * Purpose: Main strategy routine for SCSI.
1677 *
1678 * Arguments: q - Pointer to actual queue.
1679 *
1680 * Returns: Nothing
1681 *
1682 * Lock status: IO request lock assumed to be held when called.
1683 */
1684static void scsi_request_fn(struct request_queue *q)
Bart Van Assche613be1f2014-02-20 14:20:56 -08001685 __releases(q->queue_lock)
1686 __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 struct scsi_device *sdev = q->queuedata;
1689 struct Scsi_Host *shost;
1690 struct scsi_cmnd *cmd;
1691 struct request *req;
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 /*
1694 * To start with, we keep looping until the queue is empty, or until
1695 * the host is no longer able to accept any more requests.
1696 */
1697 shost = sdev->host;
Jens Axboea488e742010-04-16 21:13:15 +02001698 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int rtn;
1700 /*
1701 * get next queueable request. We do this early to make sure
Hannes Reinecke91921e02014-06-25 16:39:59 +02001702 * that the request is fully prepared even if we cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 * accept it.
1704 */
Tejun Heo9934c8c2009-05-08 11:54:16 +09001705 req = blk_peek_request(q);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001706 if (!req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 break;
1708
1709 if (unlikely(!scsi_device_online(sdev))) {
James Bottomley9ccfc752005-10-02 11:45:08 -05001710 sdev_printk(KERN_ERR, sdev,
1711 "rejecting I/O to offline device\n");
James Bottomleye91442b2005-09-09 10:44:16 -05001712 scsi_kill_request(req, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 continue;
1714 }
1715
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001716 if (!scsi_dev_queue_ready(q, sdev))
1717 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 /*
1720 * Remove the request from the request list.
1721 */
1722 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
Tejun Heo9934c8c2009-05-08 11:54:16 +09001723 blk_start_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001725 spin_unlock_irq(q->queue_lock);
James Bottomleye91442b2005-09-09 10:44:16 -05001726 cmd = req->special;
1727 if (unlikely(cmd == NULL)) {
1728 printk(KERN_CRIT "impossible request in %s.\n"
1729 "please mail a stack trace to "
Jens Axboe4aff5e22006-08-10 08:44:47 +02001730 "linux-scsi@vger.kernel.org\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001731 __func__);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001732 blk_dump_rq_flags(req, "foo");
James Bottomleye91442b2005-09-09 10:44:16 -05001733 BUG();
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Mike Christieecefe8a2008-07-11 19:50:35 -05001736 /*
1737 * We hit this when the driver is using a host wide
1738 * tag map. For device level tag maps the queue_depth check
1739 * in the device ready fn would prevent us from trying
1740 * to allocate a tag. Since the map is a shared host resource
1741 * we add the dev to the starved list so it eventually gets
1742 * a run when a tag is freed.
1743 */
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001744 if (blk_queue_tagged(q) && !(req->cmd_flags & REQ_QUEUED)) {
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001745 spin_lock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001746 if (list_empty(&sdev->starved_entry))
1747 list_add_tail(&sdev->starved_entry,
1748 &shost->starved_list);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001749 spin_unlock_irq(shost->host_lock);
Mike Christieecefe8a2008-07-11 19:50:35 -05001750 goto not_ready;
1751 }
1752
Mike Christief0c0a372008-08-17 15:24:38 -05001753 if (!scsi_target_queue_ready(shost, sdev))
1754 goto not_ready;
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (!scsi_host_queue_ready(q, shost, sdev))
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001757 goto host_not_ready;
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001758
1759 if (sdev->simple_tags)
1760 cmd->flags |= SCMD_TAGGED;
1761 else
1762 cmd->flags &= ~SCMD_TAGGED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * Finally, initialize any error handling parameters, and set up
1766 * the timers for timeouts.
1767 */
1768 scsi_init_cmd_errh(cmd);
1769
1770 /*
1771 * Dispatch the command to the low-level driver.
1772 */
Christoph Hellwig3b5382c2014-05-06 12:25:40 +02001773 cmd->scsi_done = scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 rtn = scsi_dispatch_cmd(cmd);
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001775 if (rtn) {
1776 scsi_queue_insert(cmd, rtn);
1777 spin_lock_irq(q->queue_lock);
Jens Axboea488e742010-04-16 21:13:15 +02001778 goto out_delay;
Christoph Hellwigd0d3bbf2014-01-22 18:39:04 +01001779 }
1780 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782
Bart Van Assche613be1f2014-02-20 14:20:56 -08001783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001785 host_not_ready:
Christoph Hellwig2ccbb002014-03-26 10:35:00 +01001786 if (scsi_target(sdev)->can_queue > 0)
1787 atomic_dec(&scsi_target(sdev)->target_busy);
Christoph Hellwigcf68d332014-01-22 14:36:32 +01001788 not_ready:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 /*
1790 * lock q, handle tag, requeue req, and decrement device_busy. We
1791 * must return with queue_lock held.
1792 *
1793 * Decrementing device_busy without checking it is OK, as all such
1794 * cases (host limits or settings) should run the queue at some
1795 * later time.
1796 */
1797 spin_lock_irq(q->queue_lock);
1798 blk_requeue_request(q, req);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02001799 atomic_dec(&sdev->device_busy);
Jens Axboea488e742010-04-16 21:13:15 +02001800out_delay:
Guenter Roeck480cadc2014-08-10 05:54:25 -07001801 if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
Jens Axboea488e742010-04-16 21:13:15 +02001802 blk_delay_queue(q, SCSI_QUEUE_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Christoph Hellwigd2852032014-01-17 12:06:53 +01001805static inline int prep_to_mq(int ret)
1806{
1807 switch (ret) {
1808 case BLKPREP_OK:
1809 return 0;
1810 case BLKPREP_DEFER:
1811 return BLK_MQ_RQ_QUEUE_BUSY;
1812 default:
1813 return BLK_MQ_RQ_QUEUE_ERROR;
1814 }
1815}
1816
1817static int scsi_mq_prep_fn(struct request *req)
1818{
1819 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1820 struct scsi_device *sdev = req->q->queuedata;
1821 struct Scsi_Host *shost = sdev->host;
1822 unsigned char *sense_buf = cmd->sense_buffer;
1823 struct scatterlist *sg;
1824
1825 memset(cmd, 0, sizeof(struct scsi_cmnd));
1826
1827 req->special = cmd;
1828
1829 cmd->request = req;
1830 cmd->device = sdev;
1831 cmd->sense_buffer = sense_buf;
1832
1833 cmd->tag = req->tag;
1834
Christoph Hellwigd2852032014-01-17 12:06:53 +01001835 cmd->cmnd = req->cmd;
1836 cmd->prot_op = SCSI_PROT_NORMAL;
1837
1838 INIT_LIST_HEAD(&cmd->list);
1839 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1840 cmd->jiffies_at_alloc = jiffies;
1841
Kashyap.Desai@avagotech.com64bdcbc2014-08-20 19:24:33 +05301842 if (shost->use_cmd_list) {
1843 spin_lock_irq(&sdev->list_lock);
1844 list_add_tail(&cmd->list, &sdev->cmd_list);
1845 spin_unlock_irq(&sdev->list_lock);
1846 }
Christoph Hellwigd2852032014-01-17 12:06:53 +01001847
1848 sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
1849 cmd->sdb.table.sgl = sg;
1850
1851 if (scsi_host_get_prot(shost)) {
1852 cmd->prot_sdb = (void *)sg +
Tony Battersby120bb3e2014-12-08 17:20:52 -05001853 min_t(unsigned int,
Ming Lin65e86172016-04-04 14:48:10 -07001854 shost->sg_tablesize, SG_CHUNK_SIZE) *
Tony Battersby120bb3e2014-12-08 17:20:52 -05001855 sizeof(struct scatterlist);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001856 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
1857
1858 cmd->prot_sdb->table.sgl =
1859 (struct scatterlist *)(cmd->prot_sdb + 1);
1860 }
1861
1862 if (blk_bidi_rq(req)) {
1863 struct request *next_rq = req->next_rq;
1864 struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq);
1865
1866 memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer));
1867 bidi_sdb->table.sgl =
1868 (struct scatterlist *)(bidi_sdb + 1);
1869
1870 next_rq->special = bidi_sdb;
1871 }
1872
Christoph Hellwigfe052522014-09-22 15:59:31 +02001873 blk_mq_start_request(req);
1874
Christoph Hellwigd2852032014-01-17 12:06:53 +01001875 return scsi_setup_cmnd(sdev, req);
1876}
1877
1878static void scsi_mq_done(struct scsi_cmnd *cmd)
1879{
1880 trace_scsi_dispatch_cmd_done(cmd);
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001881 blk_mq_complete_request(cmd->request, cmd->request->errors);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001882}
1883
Jens Axboe74c45052014-10-29 11:14:52 -06001884static int scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
1885 const struct blk_mq_queue_data *bd)
Christoph Hellwigd2852032014-01-17 12:06:53 +01001886{
Jens Axboe74c45052014-10-29 11:14:52 -06001887 struct request *req = bd->rq;
Christoph Hellwigd2852032014-01-17 12:06:53 +01001888 struct request_queue *q = req->q;
1889 struct scsi_device *sdev = q->queuedata;
1890 struct Scsi_Host *shost = sdev->host;
1891 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1892 int ret;
1893 int reason;
1894
1895 ret = prep_to_mq(scsi_prep_state_check(sdev, req));
1896 if (ret)
1897 goto out;
1898
1899 ret = BLK_MQ_RQ_QUEUE_BUSY;
1900 if (!get_device(&sdev->sdev_gendev))
1901 goto out;
1902
1903 if (!scsi_dev_queue_ready(q, sdev))
1904 goto out_put_device;
1905 if (!scsi_target_queue_ready(shost, sdev))
1906 goto out_dec_device_busy;
1907 if (!scsi_host_queue_ready(q, shost, sdev))
1908 goto out_dec_target_busy;
1909
Christoph Hellwigfe052522014-09-22 15:59:31 +02001910
Christoph Hellwigd2852032014-01-17 12:06:53 +01001911 if (!(req->cmd_flags & REQ_DONTPREP)) {
1912 ret = prep_to_mq(scsi_mq_prep_fn(req));
1913 if (ret)
1914 goto out_dec_host_busy;
1915 req->cmd_flags |= REQ_DONTPREP;
Christoph Hellwigfe052522014-09-22 15:59:31 +02001916 } else {
1917 blk_mq_start_request(req);
Christoph Hellwigd2852032014-01-17 12:06:53 +01001918 }
1919
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001920 if (sdev->simple_tags)
1921 cmd->flags |= SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001922 else
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001923 cmd->flags &= ~SCMD_TAGGED;
Christoph Hellwigb1dd2aa2014-10-19 17:13:58 +02001924
Christoph Hellwigd2852032014-01-17 12:06:53 +01001925 scsi_init_cmd_errh(cmd);
1926 cmd->scsi_done = scsi_mq_done;
1927
1928 reason = scsi_dispatch_cmd(cmd);
1929 if (reason) {
1930 scsi_set_blocked(cmd, reason);
1931 ret = BLK_MQ_RQ_QUEUE_BUSY;
1932 goto out_dec_host_busy;
1933 }
1934
1935 return BLK_MQ_RQ_QUEUE_OK;
1936
1937out_dec_host_busy:
1938 atomic_dec(&shost->host_busy);
1939out_dec_target_busy:
1940 if (scsi_target(sdev)->can_queue > 0)
1941 atomic_dec(&scsi_target(sdev)->target_busy);
1942out_dec_device_busy:
1943 atomic_dec(&sdev->device_busy);
1944out_put_device:
1945 put_device(&sdev->sdev_gendev);
1946out:
1947 switch (ret) {
1948 case BLK_MQ_RQ_QUEUE_BUSY:
1949 blk_mq_stop_hw_queue(hctx);
1950 if (atomic_read(&sdev->device_busy) == 0 &&
1951 !scsi_device_blocked(sdev))
1952 blk_mq_delay_queue(hctx, SCSI_QUEUE_DELAY);
1953 break;
1954 case BLK_MQ_RQ_QUEUE_ERROR:
1955 /*
1956 * Make sure to release all allocated ressources when
1957 * we hit an error, as we will never see this command
1958 * again.
1959 */
1960 if (req->cmd_flags & REQ_DONTPREP)
1961 scsi_mq_uninit_cmd(cmd);
1962 break;
1963 default:
1964 break;
1965 }
1966 return ret;
1967}
1968
Christoph Hellwig0152fb62014-09-13 16:40:13 -07001969static enum blk_eh_timer_return scsi_timeout(struct request *req,
1970 bool reserved)
1971{
1972 if (reserved)
1973 return BLK_EH_RESET_TIMER;
1974 return scsi_times_out(req);
1975}
1976
Christoph Hellwigd2852032014-01-17 12:06:53 +01001977static int scsi_init_request(void *data, struct request *rq,
1978 unsigned int hctx_idx, unsigned int request_idx,
1979 unsigned int numa_node)
1980{
1981 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1982
1983 cmd->sense_buffer = kzalloc_node(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL,
1984 numa_node);
1985 if (!cmd->sense_buffer)
1986 return -ENOMEM;
1987 return 0;
1988}
1989
1990static void scsi_exit_request(void *data, struct request *rq,
1991 unsigned int hctx_idx, unsigned int request_idx)
1992{
1993 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1994
1995 kfree(cmd->sense_buffer);
1996}
1997
Christoph Hellwigf1bea552014-04-15 12:26:54 +02001998static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 struct device *host_dev;
2001 u64 bounce_limit = 0xffffffff;
2002
2003 if (shost->unchecked_isa_dma)
2004 return BLK_BOUNCE_ISA;
2005 /*
2006 * Platforms with virtual-DMA translation
2007 * hardware have no practical limit.
2008 */
2009 if (!PCI_DMA_BUS_IS_PHYS)
2010 return BLK_BOUNCE_ANY;
2011
2012 host_dev = scsi_get_device(shost);
2013 if (host_dev && host_dev->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +00002014 bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 return bounce_limit;
2017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Christoph Hellwigd2852032014-01-17 12:06:53 +01002019static void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Lin Ming6f381fa2012-04-12 13:50:38 +08002021 struct device *dev = shost->dma_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Jens Axboea8474ce2007-08-07 09:02:51 +02002023 /*
2024 * this limit is imposed by hardware restrictions
2025 */
Martin K. Petersen8a783622010-02-26 00:20:39 -05002026 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
Ming Lin65e86172016-04-04 14:48:10 -07002027 SG_MAX_SEGMENTS));
Jens Axboea8474ce2007-08-07 09:02:51 +02002028
Martin K. Petersen13f05c82010-09-10 20:50:10 +02002029 if (scsi_host_prot_dma(shost)) {
2030 shost->sg_prot_tablesize =
2031 min_not_zero(shost->sg_prot_tablesize,
2032 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
2033 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
2034 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
2035 }
2036
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002037 blk_queue_max_hw_sectors(q, shost->max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
2039 blk_queue_segment_boundary(q, shost->dma_boundary);
FUJITA Tomonori99c84db2008-02-04 22:28:17 -08002040 dma_set_seg_boundary(dev, shost->dma_boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
FUJITA Tomonori860ac562008-02-04 22:28:05 -08002042 blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (!shost->use_clustering)
Martin K. Petersene692cb62010-12-01 19:41:49 +01002045 q->limits.cluster = 0;
James Bottomley465ff312008-01-01 10:00:10 -06002046
2047 /*
2048 * set a reasonable default alignment on word boundaries: the
2049 * host and device may alter it using
2050 * blk_queue_update_dma_alignment() later.
2051 */
2052 blk_queue_dma_alignment(q, 0x03);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002053}
James Bottomley465ff312008-01-01 10:00:10 -06002054
Christoph Hellwigd2852032014-01-17 12:06:53 +01002055struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,
2056 request_fn_proc *request_fn)
2057{
2058 struct request_queue *q;
2059
2060 q = blk_init_queue(request_fn, NULL);
2061 if (!q)
2062 return NULL;
2063 __scsi_init_queue(shost, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return q;
2065}
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002066EXPORT_SYMBOL(__scsi_alloc_queue);
2067
2068struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
2069{
2070 struct request_queue *q;
2071
2072 q = __scsi_alloc_queue(sdev->host, scsi_request_fn);
2073 if (!q)
2074 return NULL;
2075
2076 blk_queue_prep_rq(q, scsi_prep_fn);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02002077 blk_queue_unprep_rq(q, scsi_unprep_fn);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002078 blk_queue_softirq_done(q, scsi_softirq_done);
Jens Axboe242f9dc2008-09-14 05:55:09 -07002079 blk_queue_rq_timed_out(q, scsi_times_out);
Kiyoshi Ueda6c5121b2008-10-04 14:11:35 -04002080 blk_queue_lld_busy(q, scsi_lld_busy);
FUJITA Tomonorib58d9152006-11-16 19:24:10 +09002081 return q;
2082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Christoph Hellwigd2852032014-01-17 12:06:53 +01002084static struct blk_mq_ops scsi_mq_ops = {
Christoph Hellwigd2852032014-01-17 12:06:53 +01002085 .queue_rq = scsi_queue_rq,
2086 .complete = scsi_softirq_done,
Christoph Hellwig0152fb62014-09-13 16:40:13 -07002087 .timeout = scsi_timeout,
Christoph Hellwigd2852032014-01-17 12:06:53 +01002088 .init_request = scsi_init_request,
2089 .exit_request = scsi_exit_request,
2090};
2091
2092struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
2093{
2094 sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
2095 if (IS_ERR(sdev->request_queue))
2096 return NULL;
2097
2098 sdev->request_queue->queuedata = sdev;
2099 __scsi_init_queue(sdev->host, sdev->request_queue);
2100 return sdev->request_queue;
2101}
2102
2103int scsi_mq_setup_tags(struct Scsi_Host *shost)
2104{
2105 unsigned int cmd_size, sgl_size, tbl_size;
2106
2107 tbl_size = shost->sg_tablesize;
Ming Lin65e86172016-04-04 14:48:10 -07002108 if (tbl_size > SG_CHUNK_SIZE)
2109 tbl_size = SG_CHUNK_SIZE;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002110 sgl_size = tbl_size * sizeof(struct scatterlist);
2111 cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
2112 if (scsi_host_get_prot(shost))
2113 cmd_size += sizeof(struct scsi_data_buffer) + sgl_size;
2114
2115 memset(&shost->tag_set, 0, sizeof(shost->tag_set));
2116 shost->tag_set.ops = &scsi_mq_ops;
Bart Van Asscheefec4b92014-10-30 14:45:36 +01002117 shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
Christoph Hellwigd2852032014-01-17 12:06:53 +01002118 shost->tag_set.queue_depth = shost->can_queue;
2119 shost->tag_set.cmd_size = cmd_size;
2120 shost->tag_set.numa_node = NUMA_NO_NODE;
2121 shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Shaohua Li24391c02015-01-23 14:18:00 -07002122 shost->tag_set.flags |=
2123 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
Christoph Hellwigd2852032014-01-17 12:06:53 +01002124 shost->tag_set.driver_data = shost;
2125
2126 return blk_mq_alloc_tag_set(&shost->tag_set);
2127}
2128
2129void scsi_mq_destroy_tags(struct Scsi_Host *shost)
2130{
2131 blk_mq_free_tag_set(&shost->tag_set);
2132}
2133
Hannes Reineckee9dc8332017-02-17 09:02:45 +01002134/**
2135 * scsi_device_from_queue - return sdev associated with a request_queue
2136 * @q: The request queue to return the sdev from
2137 *
2138 * Return the sdev associated with a request queue or NULL if the
2139 * request_queue does not reference a SCSI device.
2140 */
2141struct scsi_device *scsi_device_from_queue(struct request_queue *q)
2142{
2143 struct scsi_device *sdev = NULL;
2144
2145 if (q->mq_ops) {
2146 if (q->mq_ops == &scsi_mq_ops)
2147 sdev = q->queuedata;
2148 } else if (q->request_fn == scsi_request_fn)
2149 sdev = q->queuedata;
2150 if (!sdev || !get_device(&sdev->sdev_gendev))
2151 sdev = NULL;
2152
2153 return sdev;
2154}
2155EXPORT_SYMBOL_GPL(scsi_device_from_queue);
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157/*
2158 * Function: scsi_block_requests()
2159 *
2160 * Purpose: Utility function used by low-level drivers to prevent further
2161 * commands from being queued to the device.
2162 *
2163 * Arguments: shost - Host in question
2164 *
2165 * Returns: Nothing
2166 *
2167 * Lock status: No locks are assumed held.
2168 *
2169 * Notes: There is no timer nor any other means by which the requests
2170 * get unblocked other than the low-level driver calling
2171 * scsi_unblock_requests().
2172 */
2173void scsi_block_requests(struct Scsi_Host *shost)
2174{
2175 shost->host_self_blocked = 1;
2176}
2177EXPORT_SYMBOL(scsi_block_requests);
2178
2179/*
2180 * Function: scsi_unblock_requests()
2181 *
2182 * Purpose: Utility function used by low-level drivers to allow further
2183 * commands from being queued to the device.
2184 *
2185 * Arguments: shost - Host in question
2186 *
2187 * Returns: Nothing
2188 *
2189 * Lock status: No locks are assumed held.
2190 *
2191 * Notes: There is no timer nor any other means by which the requests
2192 * get unblocked other than the low-level driver calling
2193 * scsi_unblock_requests().
2194 *
2195 * This is done as an API function so that changes to the
2196 * internals of the scsi mid-layer won't require wholesale
2197 * changes to drivers that use this feature.
2198 */
2199void scsi_unblock_requests(struct Scsi_Host *shost)
2200{
2201 shost->host_self_blocked = 0;
2202 scsi_run_host_queues(shost);
2203}
2204EXPORT_SYMBOL(scsi_unblock_requests);
2205
Subhash Jadavani70e75882016-08-12 16:44:37 -07002206/*
2207 * Function: scsi_set_cmd_timeout_override()
2208 *
2209 * Purpose: Utility function used by low-level drivers to override
2210 timeout for the scsi commands.
2211 *
2212 * Arguments: sdev - scsi device in question
2213 * timeout - timeout in jiffies
2214 *
2215 * Returns: Nothing
2216 *
2217 * Lock status: No locks are assumed held.
2218 *
2219 * Notes: Some platforms might be very slow and command completion may
2220 * take much longer than default scsi command timeouts.
2221 * SCSI Read/Write command timeout can be changed by
2222 * blk_queue_rq_timeout() but there is no option to override
2223 * timeout for rest of the scsi commands. This function would
2224 * would allow this.
2225 */
2226void scsi_set_cmd_timeout_override(struct scsi_device *sdev,
2227 unsigned int timeout)
2228{
2229 sdev->timeout_override = timeout;
2230}
2231EXPORT_SYMBOL(scsi_set_cmd_timeout_override);
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233int __init scsi_init_queue(void)
2234{
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002235 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
2236 sizeof(struct scsi_data_buffer),
2237 0, 0, NULL);
2238 if (!scsi_sdb_cache) {
2239 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
FUJITA Tomonorif0787272008-12-14 01:23:45 +09002240 return -ENOMEM;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02002241 }
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return 0;
2244}
2245
2246void scsi_exit_queue(void)
2247{
Martin K. Petersen6362abd2008-06-05 23:30:03 -04002248 kmem_cache_destroy(scsi_sdb_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
James Bottomley5baba832006-03-18 14:10:35 -06002250
2251/**
2252 * scsi_mode_select - issue a mode select
2253 * @sdev: SCSI device to be queried
2254 * @pf: Page format bit (1 == standard, 0 == vendor specific)
2255 * @sp: Save page bit (0 == don't save, 1 == save)
2256 * @modepage: mode page being requested
2257 * @buffer: request buffer (may not be smaller than eight bytes)
2258 * @len: length of request buffer.
2259 * @timeout: command timeout
2260 * @retries: number of retries before failing
2261 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002262 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley5baba832006-03-18 14:10:35 -06002263 * must be SCSI_SENSE_BUFFERSIZE big.
2264 *
2265 * Returns zero if successful; negative error number or scsi
2266 * status on error
2267 *
2268 */
2269int
2270scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
2271 unsigned char *buffer, int len, int timeout, int retries,
2272 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2273{
2274 unsigned char cmd[10];
2275 unsigned char *real_buffer;
2276 int ret;
2277
2278 memset(cmd, 0, sizeof(cmd));
2279 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2280
2281 if (sdev->use_10_for_ms) {
2282 if (len > 65535)
2283 return -EINVAL;
2284 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2285 if (!real_buffer)
2286 return -ENOMEM;
2287 memcpy(real_buffer + 8, buffer, len);
2288 len += 8;
2289 real_buffer[0] = 0;
2290 real_buffer[1] = 0;
2291 real_buffer[2] = data->medium_type;
2292 real_buffer[3] = data->device_specific;
2293 real_buffer[4] = data->longlba ? 0x01 : 0;
2294 real_buffer[5] = 0;
2295 real_buffer[6] = data->block_descriptor_length >> 8;
2296 real_buffer[7] = data->block_descriptor_length;
2297
2298 cmd[0] = MODE_SELECT_10;
2299 cmd[7] = len >> 8;
2300 cmd[8] = len;
2301 } else {
2302 if (len > 255 || data->block_descriptor_length > 255 ||
2303 data->longlba)
2304 return -EINVAL;
2305
2306 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2307 if (!real_buffer)
2308 return -ENOMEM;
2309 memcpy(real_buffer + 4, buffer, len);
2310 len += 4;
2311 real_buffer[0] = 0;
2312 real_buffer[1] = data->medium_type;
2313 real_buffer[2] = data->device_specific;
2314 real_buffer[3] = data->block_descriptor_length;
2315
2316
2317 cmd[0] = MODE_SELECT;
2318 cmd[4] = len;
2319 }
2320
2321 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002322 sshdr, timeout, retries, NULL);
James Bottomley5baba832006-03-18 14:10:35 -06002323 kfree(real_buffer);
2324 return ret;
2325}
2326EXPORT_SYMBOL_GPL(scsi_mode_select);
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328/**
Rob Landleyeb448202007-11-03 13:30:39 -05002329 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
James Bottomley1cf72692005-08-28 11:27:01 -05002330 * @sdev: SCSI device to be queried
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 * @dbd: set if mode sense will allow block descriptors to be returned
2332 * @modepage: mode page being requested
2333 * @buffer: request buffer (may not be smaller than eight bytes)
2334 * @len: length of request buffer.
2335 * @timeout: command timeout
2336 * @retries: number of retries before failing
2337 * @data: returns a structure abstracting the mode header data
Rob Landleyeb448202007-11-03 13:30:39 -05002338 * @sshdr: place to put sense data (or NULL if no sense to be collected).
James Bottomley1cf72692005-08-28 11:27:01 -05002339 * must be SCSI_SENSE_BUFFERSIZE big.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 *
2341 * Returns zero if unsuccessful, or the header offset (either 4
2342 * or 8 depending on whether a six or ten byte command was
2343 * issued) if successful.
Rob Landleyeb448202007-11-03 13:30:39 -05002344 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345int
James Bottomley1cf72692005-08-28 11:27:01 -05002346scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 unsigned char *buffer, int len, int timeout, int retries,
James Bottomley5baba832006-03-18 14:10:35 -06002348 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 unsigned char cmd[12];
2351 int use_10_for_ms;
2352 int header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002353 int result, retry_count = retries;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002354 struct scsi_sense_hdr my_sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 memset(data, 0, sizeof(*data));
2357 memset(&cmd[0], 0, 12);
2358 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
2359 cmd[2] = modepage;
2360
James Bottomleyea73a9f2005-08-28 11:33:52 -05002361 /* caller might not be interested in sense, but we need it */
2362 if (!sshdr)
2363 sshdr = &my_sshdr;
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 retry:
James Bottomley1cf72692005-08-28 11:27:01 -05002366 use_10_for_ms = sdev->use_10_for_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 if (use_10_for_ms) {
2369 if (len < 8)
2370 len = 8;
2371
2372 cmd[0] = MODE_SENSE_10;
2373 cmd[8] = len;
2374 header_length = 8;
2375 } else {
2376 if (len < 4)
2377 len = 4;
2378
2379 cmd[0] = MODE_SENSE;
2380 cmd[4] = len;
2381 header_length = 4;
2382 }
2383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 memset(buffer, 0, len);
2385
James Bottomley1cf72692005-08-28 11:27:01 -05002386 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002387 sshdr, timeout, retries, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 /* This code looks awful: what it's doing is making sure an
2390 * ILLEGAL REQUEST sense return identifies the actual command
2391 * byte as the problem. MODE_SENSE commands can return
2392 * ILLEGAL REQUEST if the code page isn't supported */
2393
James Bottomley1cf72692005-08-28 11:27:01 -05002394 if (use_10_for_ms && !scsi_status_is_good(result) &&
2395 (driver_byte(result) & DRIVER_SENSE)) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05002396 if (scsi_sense_valid(sshdr)) {
2397 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2398 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 /*
2400 * Invalid command operation code
2401 */
James Bottomley1cf72692005-08-28 11:27:01 -05002402 sdev->use_10_for_ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 goto retry;
2404 }
2405 }
2406 }
2407
James Bottomley1cf72692005-08-28 11:27:01 -05002408 if(scsi_status_is_good(result)) {
Al Viro6d73c852006-02-23 02:03:16 +01002409 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2410 (modepage == 6 || modepage == 8))) {
2411 /* Initio breakage? */
2412 header_length = 0;
2413 data->length = 13;
2414 data->medium_type = 0;
2415 data->device_specific = 0;
2416 data->longlba = 0;
2417 data->block_descriptor_length = 0;
2418 } else if(use_10_for_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 data->length = buffer[0]*256 + buffer[1] + 2;
2420 data->medium_type = buffer[2];
2421 data->device_specific = buffer[3];
2422 data->longlba = buffer[4] & 0x01;
2423 data->block_descriptor_length = buffer[6]*256
2424 + buffer[7];
2425 } else {
2426 data->length = buffer[0] + 1;
2427 data->medium_type = buffer[1];
2428 data->device_specific = buffer[2];
2429 data->block_descriptor_length = buffer[3];
2430 }
Al Viro6d73c852006-02-23 02:03:16 +01002431 data->header_length = header_length;
Hannes Reinecke0ae80ba2015-06-12 16:12:48 +02002432 } else if ((status_byte(result) == CHECK_CONDITION) &&
2433 scsi_sense_valid(sshdr) &&
2434 sshdr->sense_key == UNIT_ATTENTION && retry_count) {
2435 retry_count--;
2436 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
2438
James Bottomley1cf72692005-08-28 11:27:01 -05002439 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441EXPORT_SYMBOL(scsi_mode_sense);
2442
James Bottomley001aac22007-12-02 19:10:40 +02002443/**
2444 * scsi_test_unit_ready - test if unit is ready
2445 * @sdev: scsi device to change the state of.
2446 * @timeout: command timeout
2447 * @retries: number of retries before failing
2448 * @sshdr_external: Optional pointer to struct scsi_sense_hdr for
2449 * returning sense. Make sure that this is cleared before passing
2450 * in.
2451 *
2452 * Returns zero if unsuccessful or an error if TUR failed. For
Tejun Heo9f8a2c22010-12-08 20:57:40 +01002453 * removable media, UNIT_ATTENTION sets ->changed flag.
James Bottomley001aac22007-12-02 19:10:40 +02002454 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455int
James Bottomley001aac22007-12-02 19:10:40 +02002456scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
2457 struct scsi_sense_hdr *sshdr_external)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 char cmd[] = {
2460 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2461 };
James Bottomley001aac22007-12-02 19:10:40 +02002462 struct scsi_sense_hdr *sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 int result;
James Bottomley001aac22007-12-02 19:10:40 +02002464
2465 if (!sshdr_external)
2466 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
2467 else
2468 sshdr = sshdr_external;
2469
2470 /* try to eat the UNIT_ATTENTION if there are enough retries */
2471 do {
2472 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002473 timeout, retries, NULL);
James Bottomley32c356d2008-08-20 00:58:13 +02002474 if (sdev->removable && scsi_sense_valid(sshdr) &&
2475 sshdr->sense_key == UNIT_ATTENTION)
2476 sdev->changed = 1;
2477 } while (scsi_sense_valid(sshdr) &&
2478 sshdr->sense_key == UNIT_ATTENTION && --retries);
James Bottomley001aac22007-12-02 19:10:40 +02002479
James Bottomley001aac22007-12-02 19:10:40 +02002480 if (!sshdr_external)
2481 kfree(sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return result;
2483}
2484EXPORT_SYMBOL(scsi_test_unit_ready);
2485
2486/**
Rob Landleyeb448202007-11-03 13:30:39 -05002487 * scsi_device_set_state - Take the given device through the device state model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 * @sdev: scsi device to change the state of.
2489 * @state: state to change to.
2490 *
2491 * Returns zero if unsuccessful or an error if the requested
2492 * transition is illegal.
Rob Landleyeb448202007-11-03 13:30:39 -05002493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494int
2495scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2496{
2497 enum scsi_device_state oldstate = sdev->sdev_state;
2498
2499 if (state == oldstate)
2500 return 0;
2501
2502 switch (state) {
2503 case SDEV_CREATED:
James Bottomley6f4267e2008-08-22 16:53:31 -05002504 switch (oldstate) {
2505 case SDEV_CREATED_BLOCK:
2506 break;
2507 default:
2508 goto illegal;
2509 }
2510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 case SDEV_RUNNING:
2513 switch (oldstate) {
2514 case SDEV_CREATED:
2515 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002516 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 case SDEV_QUIESCE:
2518 case SDEV_BLOCK:
2519 break;
2520 default:
2521 goto illegal;
2522 }
2523 break;
2524
2525 case SDEV_QUIESCE:
2526 switch (oldstate) {
2527 case SDEV_RUNNING:
2528 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002529 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 break;
2531 default:
2532 goto illegal;
2533 }
2534 break;
2535
2536 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002537 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 switch (oldstate) {
2539 case SDEV_CREATED:
2540 case SDEV_RUNNING:
2541 case SDEV_QUIESCE:
2542 case SDEV_BLOCK:
2543 break;
2544 default:
2545 goto illegal;
2546 }
2547 break;
2548
2549 case SDEV_BLOCK:
2550 switch (oldstate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 case SDEV_RUNNING:
James Bottomley6f4267e2008-08-22 16:53:31 -05002552 case SDEV_CREATED_BLOCK:
2553 break;
2554 default:
2555 goto illegal;
2556 }
2557 break;
2558
2559 case SDEV_CREATED_BLOCK:
2560 switch (oldstate) {
2561 case SDEV_CREATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 break;
2563 default:
2564 goto illegal;
2565 }
2566 break;
2567
2568 case SDEV_CANCEL:
2569 switch (oldstate) {
2570 case SDEV_CREATED:
2571 case SDEV_RUNNING:
Alan Stern9ea72902006-06-23 14:25:34 -04002572 case SDEV_QUIESCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002574 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 case SDEV_BLOCK:
2576 break;
2577 default:
2578 goto illegal;
2579 }
2580 break;
2581
2582 case SDEV_DEL:
2583 switch (oldstate) {
Brian King309bd272006-06-27 11:10:43 -05002584 case SDEV_CREATED:
2585 case SDEV_RUNNING:
2586 case SDEV_OFFLINE:
Mike Christie1b8d2622012-05-17 23:56:56 -05002587 case SDEV_TRANSPORT_OFFLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 case SDEV_CANCEL:
Bart Van Assche0516c082013-07-02 15:06:33 +02002589 case SDEV_CREATED_BLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 break;
2591 default:
2592 goto illegal;
2593 }
2594 break;
2595
2596 }
2597 sdev->sdev_state = state;
2598 return 0;
2599
2600 illegal:
Hannes Reinecke91921e02014-06-25 16:39:59 +02002601 SCSI_LOG_ERROR_RECOVERY(1,
James Bottomley9ccfc752005-10-02 11:45:08 -05002602 sdev_printk(KERN_ERR, sdev,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002603 "Illegal state transition %s->%s",
James Bottomley9ccfc752005-10-02 11:45:08 -05002604 scsi_device_state_name(oldstate),
2605 scsi_device_state_name(state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 );
2607 return -EINVAL;
2608}
2609EXPORT_SYMBOL(scsi_device_set_state);
2610
2611/**
Jeff Garzika341cd02007-10-29 17:15:22 -04002612 * sdev_evt_emit - emit a single SCSI device uevent
2613 * @sdev: associated SCSI device
2614 * @evt: event to emit
2615 *
2616 * Send a single uevent (scsi_event) to the associated scsi_device.
2617 */
2618static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2619{
2620 int idx = 0;
2621 char *envp[3];
2622
2623 switch (evt->evt_type) {
2624 case SDEV_EVT_MEDIA_CHANGE:
2625 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2626 break;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002627 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
Hannes Reinecked3d32892016-02-19 09:17:16 +01002628 scsi_rescan_device(&sdev->sdev_gendev);
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002629 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2630 break;
2631 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2632 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2633 break;
2634 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2635 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2636 break;
2637 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2638 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2639 break;
2640 case SDEV_EVT_LUN_CHANGE_REPORTED:
2641 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2642 break;
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002643 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2644 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
2645 break;
Jeff Garzika341cd02007-10-29 17:15:22 -04002646 default:
2647 /* do nothing */
2648 break;
2649 }
2650
2651 envp[idx++] = NULL;
2652
2653 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2654}
2655
2656/**
2657 * sdev_evt_thread - send a uevent for each scsi event
2658 * @work: work struct for scsi_device
2659 *
2660 * Dispatch queued events to their associated scsi_device kobjects
2661 * as uevents.
2662 */
2663void scsi_evt_thread(struct work_struct *work)
2664{
2665 struct scsi_device *sdev;
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002666 enum scsi_device_event evt_type;
Jeff Garzika341cd02007-10-29 17:15:22 -04002667 LIST_HEAD(event_list);
2668
2669 sdev = container_of(work, struct scsi_device, event_work);
2670
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002671 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2672 if (test_and_clear_bit(evt_type, sdev->pending_events))
2673 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2674
Jeff Garzika341cd02007-10-29 17:15:22 -04002675 while (1) {
2676 struct scsi_event *evt;
2677 struct list_head *this, *tmp;
2678 unsigned long flags;
2679
2680 spin_lock_irqsave(&sdev->list_lock, flags);
2681 list_splice_init(&sdev->event_list, &event_list);
2682 spin_unlock_irqrestore(&sdev->list_lock, flags);
2683
2684 if (list_empty(&event_list))
2685 break;
2686
2687 list_for_each_safe(this, tmp, &event_list) {
2688 evt = list_entry(this, struct scsi_event, node);
2689 list_del(&evt->node);
2690 scsi_evt_emit(sdev, evt);
2691 kfree(evt);
2692 }
2693 }
2694}
2695
2696/**
2697 * sdev_evt_send - send asserted event to uevent thread
2698 * @sdev: scsi_device event occurred on
2699 * @evt: event to send
2700 *
2701 * Assert scsi device event asynchronously.
2702 */
2703void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2704{
2705 unsigned long flags;
2706
Kay Sievers4d1566e2008-03-19 13:04:47 +01002707#if 0
2708 /* FIXME: currently this check eliminates all media change events
2709 * for polled devices. Need to update to discriminate between AN
2710 * and polled events */
Jeff Garzika341cd02007-10-29 17:15:22 -04002711 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2712 kfree(evt);
2713 return;
2714 }
Kay Sievers4d1566e2008-03-19 13:04:47 +01002715#endif
Jeff Garzika341cd02007-10-29 17:15:22 -04002716
2717 spin_lock_irqsave(&sdev->list_lock, flags);
2718 list_add_tail(&evt->node, &sdev->event_list);
2719 schedule_work(&sdev->event_work);
2720 spin_unlock_irqrestore(&sdev->list_lock, flags);
2721}
2722EXPORT_SYMBOL_GPL(sdev_evt_send);
2723
2724/**
2725 * sdev_evt_alloc - allocate a new scsi event
2726 * @evt_type: type of event to allocate
2727 * @gfpflags: GFP flags for allocation
2728 *
2729 * Allocates and returns a new scsi_event.
2730 */
2731struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2732 gfp_t gfpflags)
2733{
2734 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2735 if (!evt)
2736 return NULL;
2737
2738 evt->evt_type = evt_type;
2739 INIT_LIST_HEAD(&evt->node);
2740
2741 /* evt_type-specific initialization, if any */
2742 switch (evt_type) {
2743 case SDEV_EVT_MEDIA_CHANGE:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04002744 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2745 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2746 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2747 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2748 case SDEV_EVT_LUN_CHANGE_REPORTED:
Hannes Reinecke14c3e672015-07-06 13:41:53 +02002749 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
Jeff Garzika341cd02007-10-29 17:15:22 -04002750 default:
2751 /* do nothing */
2752 break;
2753 }
2754
2755 return evt;
2756}
2757EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2758
2759/**
2760 * sdev_evt_send_simple - send asserted event to uevent thread
2761 * @sdev: scsi_device event occurred on
2762 * @evt_type: type of event to send
2763 * @gfpflags: GFP flags for allocation
2764 *
2765 * Assert scsi device event asynchronously, given an event type.
2766 */
2767void sdev_evt_send_simple(struct scsi_device *sdev,
2768 enum scsi_device_event evt_type, gfp_t gfpflags)
2769{
2770 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2771 if (!evt) {
2772 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2773 evt_type);
2774 return;
2775 }
2776
2777 sdev_evt_send(sdev, evt);
2778}
2779EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2780
2781/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 * scsi_device_quiesce - Block user issued commands.
2783 * @sdev: scsi device to quiesce.
2784 *
2785 * This works by trying to transition to the SDEV_QUIESCE state
2786 * (which must be a legal transition). When the device is in this
2787 * state, only special requests will be accepted, all others will
2788 * be deferred. Since special requests may also be requeued requests,
2789 * a successful return doesn't guarantee the device will be
2790 * totally quiescent.
2791 *
2792 * Must be called with user context, may sleep.
2793 *
2794 * Returns zero if unsuccessful or an error if not.
Rob Landleyeb448202007-11-03 13:30:39 -05002795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796int
2797scsi_device_quiesce(struct scsi_device *sdev)
2798{
2799 int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2800 if (err)
2801 return err;
2802
2803 scsi_run_queue(sdev->request_queue);
Christoph Hellwig71e75c92014-04-11 19:07:01 +02002804 while (atomic_read(&sdev->device_busy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 msleep_interruptible(200);
2806 scsi_run_queue(sdev->request_queue);
2807 }
2808 return 0;
2809}
2810EXPORT_SYMBOL(scsi_device_quiesce);
2811
2812/**
2813 * scsi_device_resume - Restart user issued commands to a quiesced device.
2814 * @sdev: scsi device to resume.
2815 *
2816 * Moves the device from quiesced back to running and restarts the
2817 * queues.
2818 *
2819 * Must be called with user context, may sleep.
Rob Landleyeb448202007-11-03 13:30:39 -05002820 */
Dan Williamsa7a20d12012-03-22 17:05:11 -07002821void scsi_device_resume(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Dan Williamsa7a20d12012-03-22 17:05:11 -07002823 /* check if the device state was mutated prior to resume, and if
2824 * so assume the state is being managed elsewhere (for example
2825 * device deleted during suspend)
2826 */
2827 if (sdev->sdev_state != SDEV_QUIESCE ||
2828 scsi_device_set_state(sdev, SDEV_RUNNING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return;
2830 scsi_run_queue(sdev->request_queue);
2831}
2832EXPORT_SYMBOL(scsi_device_resume);
2833
2834static void
2835device_quiesce_fn(struct scsi_device *sdev, void *data)
2836{
2837 scsi_device_quiesce(sdev);
2838}
2839
2840void
2841scsi_target_quiesce(struct scsi_target *starget)
2842{
2843 starget_for_each_device(starget, NULL, device_quiesce_fn);
2844}
2845EXPORT_SYMBOL(scsi_target_quiesce);
2846
2847static void
2848device_resume_fn(struct scsi_device *sdev, void *data)
2849{
2850 scsi_device_resume(sdev);
2851}
2852
2853void
2854scsi_target_resume(struct scsi_target *starget)
2855{
2856 starget_for_each_device(starget, NULL, device_resume_fn);
2857}
2858EXPORT_SYMBOL(scsi_target_resume);
2859
2860/**
Rob Landleyeb448202007-11-03 13:30:39 -05002861 * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 * @sdev: device to block
2863 *
2864 * Block request made by scsi lld's to temporarily stop all
2865 * scsi commands on the specified device. Called from interrupt
2866 * or normal process context.
2867 *
2868 * Returns zero if successful or error if not
2869 *
2870 * Notes:
2871 * This routine transitions the device to the SDEV_BLOCK state
2872 * (which must be a legal transition). When the device is in this
2873 * state, all commands are deferred until the scsi lld reenables
2874 * the device with scsi_device_unblock or device_block_tmo fires.
Rob Landleyeb448202007-11-03 13:30:39 -05002875 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876int
2877scsi_internal_device_block(struct scsi_device *sdev)
2878{
Jens Axboe165125e2007-07-24 09:28:11 +02002879 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 unsigned long flags;
2881 int err = 0;
2882
2883 err = scsi_device_set_state(sdev, SDEV_BLOCK);
James Bottomley6f4267e2008-08-22 16:53:31 -05002884 if (err) {
2885 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2886
2887 if (err)
2888 return err;
2889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 /*
2892 * The device has transitioned to SDEV_BLOCK. Stop the
2893 * block layer from calling the midlayer with this device's
2894 * request queue.
2895 */
Christoph Hellwigd2852032014-01-17 12:06:53 +01002896 if (q->mq_ops) {
2897 blk_mq_stop_hw_queues(q);
2898 } else {
2899 spin_lock_irqsave(q->queue_lock, flags);
2900 blk_stop_queue(q);
2901 spin_unlock_irqrestore(q->queue_lock, flags);
2902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 return 0;
2905}
2906EXPORT_SYMBOL_GPL(scsi_internal_device_block);
2907
2908/**
2909 * scsi_internal_device_unblock - resume a device after a block request
2910 * @sdev: device to resume
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002911 * @new_state: state to set devices to after unblocking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 *
2913 * Called by scsi lld's or the midlayer to restart the device queue
2914 * for the previously suspended scsi device. Called from interrupt or
2915 * normal process context.
2916 *
2917 * Returns zero if successful or error if not.
2918 *
2919 * Notes:
2920 * This routine transitions the device to the SDEV_RUNNING state
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002921 * or to one of the offline states (which must be a legal transition)
Mike Christied0754982012-05-17 23:56:58 -05002922 * allowing the midlayer to goose the queue for this device.
Rob Landleyeb448202007-11-03 13:30:39 -05002923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924int
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002925scsi_internal_device_unblock(struct scsi_device *sdev,
2926 enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
Jens Axboe165125e2007-07-24 09:28:11 +02002928 struct request_queue *q = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 unsigned long flags;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002930
2931 /*
2932 * Try to transition the scsi device to SDEV_RUNNING or one of the
2933 * offlined states and goose the device queue if successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 */
Vikas Chaudhary0e580762012-08-09 04:51:30 -04002935 if ((sdev->sdev_state == SDEV_BLOCK) ||
2936 (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002937 sdev->sdev_state = new_state;
2938 else if (sdev->sdev_state == SDEV_CREATED_BLOCK) {
2939 if (new_state == SDEV_TRANSPORT_OFFLINE ||
2940 new_state == SDEV_OFFLINE)
2941 sdev->sdev_state = new_state;
2942 else
2943 sdev->sdev_state = SDEV_CREATED;
2944 } else if (sdev->sdev_state != SDEV_CANCEL &&
Mike Christie986fe6c2010-10-06 03:10:59 -05002945 sdev->sdev_state != SDEV_OFFLINE)
Takahiro Yasui5c10e632009-04-29 12:13:02 -04002946 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Christoph Hellwigd2852032014-01-17 12:06:53 +01002948 if (q->mq_ops) {
2949 blk_mq_start_stopped_hw_queues(q, false);
2950 } else {
2951 spin_lock_irqsave(q->queue_lock, flags);
2952 blk_start_queue(q);
2953 spin_unlock_irqrestore(q->queue_lock, flags);
2954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 return 0;
2957}
2958EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
2959
2960static void
2961device_block(struct scsi_device *sdev, void *data)
2962{
2963 scsi_internal_device_block(sdev);
2964}
2965
2966static int
2967target_block(struct device *dev, void *data)
2968{
2969 if (scsi_is_target_device(dev))
2970 starget_for_each_device(to_scsi_target(dev), NULL,
2971 device_block);
2972 return 0;
2973}
2974
2975void
2976scsi_target_block(struct device *dev)
2977{
2978 if (scsi_is_target_device(dev))
2979 starget_for_each_device(to_scsi_target(dev), NULL,
2980 device_block);
2981 else
2982 device_for_each_child(dev, NULL, target_block);
2983}
2984EXPORT_SYMBOL_GPL(scsi_target_block);
2985
2986static void
2987device_unblock(struct scsi_device *sdev, void *data)
2988{
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002989 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
2992static int
2993target_unblock(struct device *dev, void *data)
2994{
2995 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002996 starget_for_each_device(to_scsi_target(dev), data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 device_unblock);
2998 return 0;
2999}
3000
3001void
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003002scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
3004 if (scsi_is_target_device(dev))
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003005 starget_for_each_device(to_scsi_target(dev), &new_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 device_unblock);
3007 else
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003008 device_for_each_child(dev, &new_state, target_unblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010EXPORT_SYMBOL_GPL(scsi_target_unblock);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003011
3012/**
3013 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
Rob Landleyeb448202007-11-03 13:30:39 -05003014 * @sgl: scatter-gather list
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003015 * @sg_count: number of segments in sg
3016 * @offset: offset in bytes into sg, on return offset into the mapped area
3017 * @len: bytes to map, on return number of bytes mapped
3018 *
3019 * Returns virtual address of the start of the mapped page
3020 */
Jens Axboec6132da2007-10-16 11:08:49 +02003021void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003022 size_t *offset, size_t *len)
3023{
3024 int i;
3025 size_t sg_len = 0, len_complete = 0;
Jens Axboec6132da2007-10-16 11:08:49 +02003026 struct scatterlist *sg;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003027 struct page *page;
3028
Andrew Morton22cfefb2007-02-05 16:39:03 -08003029 WARN_ON(!irqs_disabled());
3030
Jens Axboec6132da2007-10-16 11:08:49 +02003031 for_each_sg(sgl, sg, sg_count, i) {
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003032 len_complete = sg_len; /* Complete sg-entries */
Jens Axboec6132da2007-10-16 11:08:49 +02003033 sg_len += sg->length;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003034 if (sg_len > *offset)
3035 break;
3036 }
3037
3038 if (unlikely(i == sg_count)) {
Andrew Morton169e1a22006-04-18 21:09:08 -07003039 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
3040 "elements %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003041 __func__, sg_len, *offset, sg_count);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003042 WARN_ON(1);
3043 return NULL;
3044 }
3045
3046 /* Offset starting from the beginning of first page in this sg-entry */
Jens Axboec6132da2007-10-16 11:08:49 +02003047 *offset = *offset - len_complete + sg->offset;
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003048
3049 /* Assumption: contiguous pages can be accessed as "page + i" */
Jens Axboe45711f12007-10-22 21:19:53 +02003050 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003051 *offset &= ~PAGE_MASK;
3052
3053 /* Bytes in this sg-entry from *offset to the end of the page */
3054 sg_len = PAGE_SIZE - *offset;
3055 if (*len > sg_len)
3056 *len = sg_len;
3057
Cong Wang77dfce02011-11-25 23:14:23 +08003058 return kmap_atomic(page);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003059}
3060EXPORT_SYMBOL(scsi_kmap_atomic_sg);
3061
3062/**
Rob Landleyeb448202007-11-03 13:30:39 -05003063 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003064 * @virt: virtual address to be unmapped
3065 */
3066void scsi_kunmap_atomic_sg(void *virt)
3067{
Cong Wang77dfce02011-11-25 23:14:23 +08003068 kunmap_atomic(virt);
Guennadi Liakhovetskicdb8c2a2006-04-02 21:57:43 +02003069}
3070EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
Aaron Lu6f4c8272013-01-23 15:09:32 +08003071
3072void sdev_disable_disk_events(struct scsi_device *sdev)
3073{
3074 atomic_inc(&sdev->disk_events_disable_depth);
3075}
3076EXPORT_SYMBOL(sdev_disable_disk_events);
3077
3078void sdev_enable_disk_events(struct scsi_device *sdev)
3079{
3080 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
3081 return;
3082 atomic_dec(&sdev->disk_events_disable_depth);
3083}
3084EXPORT_SYMBOL(sdev_enable_disk_events);
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003085
3086/**
3087 * scsi_vpd_lun_id - return a unique device identification
3088 * @sdev: SCSI device
3089 * @id: buffer for the identification
3090 * @id_len: length of the buffer
3091 *
3092 * Copies a unique device identification into @id based
3093 * on the information in the VPD page 0x83 of the device.
3094 * The string will be formatted as a SCSI name string.
3095 *
3096 * Returns the length of the identification or error on failure.
3097 * If the identifier is longer than the supplied buffer the actual
3098 * identifier length is returned and the buffer is not zero-padded.
3099 */
3100int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
3101{
3102 u8 cur_id_type = 0xff;
3103 u8 cur_id_size = 0;
3104 unsigned char *d, *cur_id_str;
3105 unsigned char __rcu *vpd_pg83;
3106 int id_size = -EINVAL;
3107
3108 rcu_read_lock();
3109 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3110 if (!vpd_pg83) {
3111 rcu_read_unlock();
3112 return -ENXIO;
3113 }
3114
3115 /*
3116 * Look for the correct descriptor.
3117 * Order of preference for lun descriptor:
3118 * - SCSI name string
3119 * - NAA IEEE Registered Extended
3120 * - EUI-64 based 16-byte
3121 * - EUI-64 based 12-byte
3122 * - NAA IEEE Registered
3123 * - NAA IEEE Extended
Hannes Reinecked2308232016-05-09 09:14:29 +02003124 * - T10 Vendor ID
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003125 * as longer descriptors reduce the likelyhood
3126 * of identification clashes.
3127 */
3128
3129 /* The id string must be at least 20 bytes + terminating NULL byte */
3130 if (id_len < 21) {
3131 rcu_read_unlock();
3132 return -EINVAL;
3133 }
3134
3135 memset(id, 0, id_len);
3136 d = vpd_pg83 + 4;
3137 while (d < vpd_pg83 + sdev->vpd_pg83_len) {
3138 /* Skip designators not referring to the LUN */
3139 if ((d[1] & 0x30) != 0x00)
3140 goto next_desig;
3141
3142 switch (d[1] & 0xf) {
Hannes Reinecked2308232016-05-09 09:14:29 +02003143 case 0x1:
3144 /* T10 Vendor ID */
3145 if (cur_id_size > d[3])
3146 break;
3147 /* Prefer anything */
3148 if (cur_id_type > 0x01 && cur_id_type != 0xff)
3149 break;
3150 cur_id_size = d[3];
3151 if (cur_id_size + 4 > id_len)
3152 cur_id_size = id_len - 4;
3153 cur_id_str = d + 4;
3154 cur_id_type = d[1] & 0xf;
3155 id_size = snprintf(id, id_len, "t10.%*pE",
3156 cur_id_size, cur_id_str);
3157 break;
Hannes Reinecke9983bed2015-12-01 10:16:55 +01003158 case 0x2:
3159 /* EUI-64 */
3160 if (cur_id_size > d[3])
3161 break;
3162 /* Prefer NAA IEEE Registered Extended */
3163 if (cur_id_type == 0x3 &&
3164 cur_id_size == d[3])
3165 break;
3166 cur_id_size = d[3];
3167 cur_id_str = d + 4;
3168 cur_id_type = d[1] & 0xf;
3169 switch (cur_id_size) {
3170 case 8:
3171 id_size = snprintf(id, id_len,
3172 "eui.%8phN",
3173 cur_id_str);
3174 break;
3175 case 12:
3176 id_size = snprintf(id, id_len,
3177 "eui.%12phN",
3178 cur_id_str);
3179 break;
3180 case 16:
3181 id_size = snprintf(id, id_len,
3182 "eui.%16phN",
3183 cur_id_str);
3184 break;
3185 default:
3186 cur_id_size = 0;
3187 break;
3188 }
3189 break;
3190 case 0x3:
3191 /* NAA */
3192 if (cur_id_size > d[3])
3193 break;
3194 cur_id_size = d[3];
3195 cur_id_str = d + 4;
3196 cur_id_type = d[1] & 0xf;
3197 switch (cur_id_size) {
3198 case 8:
3199 id_size = snprintf(id, id_len,
3200 "naa.%8phN",
3201 cur_id_str);
3202 break;
3203 case 16:
3204 id_size = snprintf(id, id_len,
3205 "naa.%16phN",
3206 cur_id_str);
3207 break;
3208 default:
3209 cur_id_size = 0;
3210 break;
3211 }
3212 break;
3213 case 0x8:
3214 /* SCSI name string */
3215 if (cur_id_size + 4 > d[3])
3216 break;
3217 /* Prefer others for truncated descriptor */
3218 if (cur_id_size && d[3] > id_len)
3219 break;
3220 cur_id_size = id_size = d[3];
3221 cur_id_str = d + 4;
3222 cur_id_type = d[1] & 0xf;
3223 if (cur_id_size >= id_len)
3224 cur_id_size = id_len - 1;
3225 memcpy(id, cur_id_str, cur_id_size);
3226 /* Decrease priority for truncated descriptor */
3227 if (cur_id_size != id_size)
3228 cur_id_size = 6;
3229 break;
3230 default:
3231 break;
3232 }
3233next_desig:
3234 d += d[3] + 4;
3235 }
3236 rcu_read_unlock();
3237
3238 return id_size;
3239}
3240EXPORT_SYMBOL(scsi_vpd_lun_id);
Hannes Reineckea8aa3972015-12-01 10:16:57 +01003241
3242/*
3243 * scsi_vpd_tpg_id - return a target port group identifier
3244 * @sdev: SCSI device
3245 *
3246 * Returns the Target Port Group identifier from the information
3247 * froom VPD page 0x83 of the device.
3248 *
3249 * Returns the identifier or error on failure.
3250 */
3251int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
3252{
3253 unsigned char *d;
3254 unsigned char __rcu *vpd_pg83;
3255 int group_id = -EAGAIN, rel_port = -1;
3256
3257 rcu_read_lock();
3258 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3259 if (!vpd_pg83) {
3260 rcu_read_unlock();
3261 return -ENXIO;
3262 }
3263
3264 d = sdev->vpd_pg83 + 4;
3265 while (d < sdev->vpd_pg83 + sdev->vpd_pg83_len) {
3266 switch (d[1] & 0xf) {
3267 case 0x4:
3268 /* Relative target port */
3269 rel_port = get_unaligned_be16(&d[6]);
3270 break;
3271 case 0x5:
3272 /* Target port group */
3273 group_id = get_unaligned_be16(&d[6]);
3274 break;
3275 default:
3276 break;
3277 }
3278 d += d[3] + 4;
3279 }
3280 rcu_read_unlock();
3281
3282 if (group_id >= 0 && rel_id && rel_port != -1)
3283 *rel_id = rel_port;
3284
3285 return group_id;
3286}
3287EXPORT_SYMBOL(scsi_vpd_tpg_id);