Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2 | * Copyright (C) 1999 Eric Youngdale |
| 3 | * Copyright (C) 2014 Christoph Hellwig |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 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 Bottomley | d3f46f3 | 2008-01-15 11:11:46 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/blkdev.h> |
| 14 | #include <linux/completion.h> |
| 15 | #include <linux/kernel.h> |
Paul Gortmaker | 0970366 | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 16 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/pci.h> |
| 19 | #include <linux/delay.h> |
James Bottomley | faead26 | 2006-02-14 10:42:07 -0600 | [diff] [blame] | 20 | #include <linux/hardirq.h> |
Jens Axboe | c6132da | 2007-10-16 11:08:49 +0200 | [diff] [blame] | 21 | #include <linux/scatterlist.h> |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 22 | #include <linux/blk-mq.h> |
Hannes Reinecke | f1569ff | 2014-10-24 14:27:07 +0200 | [diff] [blame] | 23 | #include <linux/ratelimit.h> |
Hannes Reinecke | a8aa397 | 2015-12-01 10:16:57 +0100 | [diff] [blame] | 24 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <scsi/scsi.h> |
Christoph Hellwig | beb4048 | 2006-06-10 18:01:03 +0200 | [diff] [blame] | 27 | #include <scsi/scsi_cmnd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #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 Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 33 | #include <scsi/scsi_dh.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Christoph Hellwig | 3b5382c | 2014-05-06 12:25:40 +0200 | [diff] [blame] | 35 | #include <trace/events/scsi.h> |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "scsi_priv.h" |
| 38 | #include "scsi_logging.h" |
| 39 | |
| 40 | |
Martin K. Petersen | 7027ad7 | 2008-07-17 17:08:48 -0400 | [diff] [blame] | 41 | struct kmem_cache *scsi_sdb_cache; |
Christoph Hellwig | 0a6ac4e | 2017-01-03 08:28:41 +0300 | [diff] [blame^] | 42 | static struct kmem_cache *scsi_sense_cache; |
| 43 | static struct kmem_cache *scsi_sense_isadma_cache; |
| 44 | static DEFINE_MUTEX(scsi_sense_cache_mutex); |
| 45 | |
| 46 | static inline struct kmem_cache * |
| 47 | scsi_select_sense_cache(struct Scsi_Host *shost) |
| 48 | { |
| 49 | return shost->unchecked_isa_dma ? |
| 50 | scsi_sense_isadma_cache : scsi_sense_cache; |
| 51 | } |
| 52 | |
| 53 | void scsi_free_sense_buffer(struct Scsi_Host *shost, |
| 54 | unsigned char *sense_buffer) |
| 55 | { |
| 56 | kmem_cache_free(scsi_select_sense_cache(shost), sense_buffer); |
| 57 | } |
| 58 | |
| 59 | unsigned char *scsi_alloc_sense_buffer(struct Scsi_Host *shost, gfp_t gfp_mask, |
| 60 | int numa_node) |
| 61 | { |
| 62 | return kmem_cache_alloc_node(scsi_select_sense_cache(shost), gfp_mask, |
| 63 | numa_node); |
| 64 | } |
| 65 | |
| 66 | int scsi_init_sense_cache(struct Scsi_Host *shost) |
| 67 | { |
| 68 | struct kmem_cache *cache; |
| 69 | int ret = 0; |
| 70 | |
| 71 | cache = scsi_select_sense_cache(shost); |
| 72 | if (cache) |
| 73 | return 0; |
| 74 | |
| 75 | mutex_lock(&scsi_sense_cache_mutex); |
| 76 | if (shost->unchecked_isa_dma) { |
| 77 | scsi_sense_isadma_cache = |
| 78 | kmem_cache_create("scsi_sense_cache(DMA)", |
| 79 | SCSI_SENSE_BUFFERSIZE, 0, |
| 80 | SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL); |
| 81 | if (!scsi_sense_isadma_cache) |
| 82 | ret = -ENOMEM; |
| 83 | } else { |
| 84 | scsi_sense_cache = |
| 85 | kmem_cache_create("scsi_sense_cache", |
| 86 | SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN, NULL); |
| 87 | if (!scsi_sense_cache) |
| 88 | ret = -ENOMEM; |
| 89 | } |
| 90 | |
| 91 | mutex_unlock(&scsi_sense_cache_mutex); |
| 92 | return ret; |
| 93 | } |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 94 | |
Jens Axboe | a488e74 | 2010-04-16 21:13:15 +0200 | [diff] [blame] | 95 | /* |
| 96 | * When to reinvoke queueing after a resource shortage. It's 3 msecs to |
| 97 | * not change behaviour from the previous unplug mechanism, experimentation |
| 98 | * may prove this needs changing. |
| 99 | */ |
| 100 | #define SCSI_QUEUE_DELAY 3 |
| 101 | |
Christoph Hellwig | de3e8bf | 2014-01-23 13:55:38 +0100 | [diff] [blame] | 102 | static void |
| 103 | scsi_set_blocked(struct scsi_cmnd *cmd, int reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
| 105 | struct Scsi_Host *host = cmd->device->host; |
| 106 | struct scsi_device *device = cmd->device; |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 107 | struct scsi_target *starget = scsi_target(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
Tejun Heo | d8c37e7 | 2005-05-14 00:46:08 +0900 | [diff] [blame] | 110 | * Set the appropriate busy bit for the device/host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * |
| 112 | * If the host/device isn't busy, assume that something actually |
| 113 | * completed, and that we should be able to queue a command now. |
| 114 | * |
| 115 | * Note that the prior mid-layer assumption that any host could |
| 116 | * always queue at least one command is now broken. The mid-layer |
| 117 | * will implement a user specifiable stall (see |
| 118 | * scsi_host.max_host_blocked and scsi_device.max_device_blocked) |
| 119 | * if a command is requeued with no other commands outstanding |
| 120 | * either for the device or for the host. |
| 121 | */ |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 122 | switch (reason) { |
| 123 | case SCSI_MLQUEUE_HOST_BUSY: |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 124 | atomic_set(&host->host_blocked, host->max_host_blocked); |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 125 | break; |
| 126 | case SCSI_MLQUEUE_DEVICE_BUSY: |
James Smart | 573e591 | 2011-07-06 12:45:17 -0400 | [diff] [blame] | 127 | case SCSI_MLQUEUE_EH_RETRY: |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 128 | atomic_set(&device->device_blocked, |
| 129 | device->max_device_blocked); |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 130 | break; |
| 131 | case SCSI_MLQUEUE_TARGET_BUSY: |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 132 | atomic_set(&starget->target_blocked, |
| 133 | starget->max_target_blocked); |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 134 | break; |
| 135 | } |
Christoph Hellwig | de3e8bf | 2014-01-23 13:55:38 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 138 | static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd) |
| 139 | { |
| 140 | struct scsi_device *sdev = cmd->device; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 141 | |
Bart Van Assche | 2b053ac | 2016-10-28 17:21:41 -0700 | [diff] [blame] | 142 | blk_mq_requeue_request(cmd->request, true); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 143 | put_device(&sdev->sdev_gendev); |
| 144 | } |
| 145 | |
Christoph Hellwig | de3e8bf | 2014-01-23 13:55:38 +0100 | [diff] [blame] | 146 | /** |
| 147 | * __scsi_queue_insert - private queue insertion |
| 148 | * @cmd: The SCSI command being requeued |
| 149 | * @reason: The reason for the requeue |
| 150 | * @unbusy: Whether the queue should be unbusied |
| 151 | * |
| 152 | * This is a private queue insertion. The public interface |
| 153 | * scsi_queue_insert() always assumes the queue should be unbusied |
| 154 | * because it's always called before the completion. This function is |
| 155 | * for a requeue after completion, which should only occur in this |
| 156 | * file. |
| 157 | */ |
| 158 | static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy) |
| 159 | { |
| 160 | struct scsi_device *device = cmd->device; |
| 161 | struct request_queue *q = device->request_queue; |
| 162 | unsigned long flags; |
| 163 | |
| 164 | SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd, |
| 165 | "Inserting command %p into mlqueue\n", cmd)); |
| 166 | |
| 167 | scsi_set_blocked(cmd, reason); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | * Decrement the counters, since these commands are no longer |
| 171 | * active on the host/device. |
| 172 | */ |
James Bottomley | 4f5299a | 2009-01-02 10:42:21 -0600 | [diff] [blame] | 173 | if (unbusy) |
| 174 | scsi_device_unbusy(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | /* |
Tejun Heo | a1bf9d1d | 2005-04-24 02:08:52 -0500 | [diff] [blame] | 177 | * Requeue this command. It will go before all other commands |
Bart Van Assche | b485462 | 2012-06-29 15:36:07 +0000 | [diff] [blame] | 178 | * that are already in the queue. Schedule requeue work under |
| 179 | * lock such that the kblockd_schedule_work() call happens |
| 180 | * before blk_cleanup_queue() finishes. |
Jens Axboe | a488e74 | 2010-04-16 21:13:15 +0200 | [diff] [blame] | 181 | */ |
Alan Stern | 644373a | 2014-03-28 10:51:15 -0700 | [diff] [blame] | 182 | cmd->result = 0; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 183 | if (q->mq_ops) { |
| 184 | scsi_mq_requeue_cmd(cmd); |
| 185 | return; |
| 186 | } |
Tejun Heo | a1bf9d1d | 2005-04-24 02:08:52 -0500 | [diff] [blame] | 187 | spin_lock_irqsave(q->queue_lock, flags); |
James Bottomley | 59897da | 2005-09-14 12:57:42 -0400 | [diff] [blame] | 188 | blk_requeue_request(q, cmd->request); |
Jens Axboe | 59c3d45 | 2014-04-08 09:15:35 -0600 | [diff] [blame] | 189 | kblockd_schedule_work(&device->requeue_work); |
Bart Van Assche | b485462 | 2012-06-29 15:36:07 +0000 | [diff] [blame] | 190 | spin_unlock_irqrestore(q->queue_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
James Bottomley | 4f5299a | 2009-01-02 10:42:21 -0600 | [diff] [blame] | 193 | /* |
| 194 | * Function: scsi_queue_insert() |
| 195 | * |
| 196 | * Purpose: Insert a command in the midlevel queue. |
| 197 | * |
| 198 | * Arguments: cmd - command that we are adding to queue. |
| 199 | * reason - why we are inserting command to queue. |
| 200 | * |
| 201 | * Lock status: Assumed that lock is not held upon entry. |
| 202 | * |
| 203 | * Returns: Nothing. |
| 204 | * |
| 205 | * Notes: We do this for one of two cases. Either the host is busy |
| 206 | * and it cannot accept any more commands for the time being, |
| 207 | * or the device returned QUEUE_FULL and can accept no more |
| 208 | * commands. |
| 209 | * Notes: This could be called either from an interrupt context or a |
| 210 | * normal process context. |
| 211 | */ |
Bart Van Assche | 84feb16 | 2012-06-29 15:35:05 +0000 | [diff] [blame] | 212 | void scsi_queue_insert(struct scsi_cmnd *cmd, int reason) |
James Bottomley | 4f5299a | 2009-01-02 10:42:21 -0600 | [diff] [blame] | 213 | { |
Bart Van Assche | 84feb16 | 2012-06-29 15:35:05 +0000 | [diff] [blame] | 214 | __scsi_queue_insert(cmd, reason, 1); |
James Bottomley | 4f5299a | 2009-01-02 10:42:21 -0600 | [diff] [blame] | 215 | } |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 216 | |
| 217 | static int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd, |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 218 | int data_direction, void *buffer, unsigned bufflen, |
Martin K. Petersen | 2bfad21 | 2014-04-09 22:20:48 -0400 | [diff] [blame] | 219 | unsigned char *sense, int timeout, int retries, u64 flags, |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 220 | req_flags_t rq_flags, int *resid) |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 221 | { |
| 222 | struct request *req; |
| 223 | int write = (data_direction == DMA_TO_DEVICE); |
| 224 | int ret = DRIVER_ERROR << 24; |
| 225 | |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 226 | req = blk_get_request(sdev->request_queue, write, __GFP_RECLAIM); |
Joe Lawrence | a492f07 | 2014-08-28 08:15:21 -0600 | [diff] [blame] | 227 | if (IS_ERR(req)) |
James Bottomley | bfe159a | 2011-07-07 15:45:40 -0500 | [diff] [blame] | 228 | return ret; |
Jens Axboe | f27b087 | 2014-06-06 07:57:37 -0600 | [diff] [blame] | 229 | blk_rq_set_block_pc(req); |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 230 | |
| 231 | if (bufflen && blk_rq_map_kern(sdev->request_queue, req, |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 232 | buffer, bufflen, __GFP_RECLAIM)) |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 233 | goto out; |
| 234 | |
| 235 | req->cmd_len = COMMAND_SIZE(cmd[0]); |
| 236 | memcpy(req->cmd, cmd, req->cmd_len); |
| 237 | req->sense = sense; |
| 238 | req->sense_len = 0; |
Mike Christie | 17e01f2 | 2005-11-11 05:31:37 -0600 | [diff] [blame] | 239 | req->retries = retries; |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 240 | req->timeout = timeout; |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 241 | req->cmd_flags |= flags; |
| 242 | req->rq_flags |= rq_flags | RQF_QUIET | RQF_PREEMPT; |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * head injection *required* here otherwise quiesce won't work |
| 246 | */ |
| 247 | blk_execute_rq(req->q, NULL, req, 1); |
| 248 | |
Alan Stern | bdb2b8c | 2008-06-24 14:03:14 -0400 | [diff] [blame] | 249 | /* |
| 250 | * Some devices (USB mass-storage in particular) may transfer |
| 251 | * garbage data together with a residue indicating that the data |
| 252 | * is invalid. Prevent the garbage from being misinterpreted |
| 253 | * and prevent security leaks by zeroing out the excess data. |
| 254 | */ |
Tejun Heo | c3a4d78 | 2009-05-07 22:24:37 +0900 | [diff] [blame] | 255 | if (unlikely(req->resid_len > 0 && req->resid_len <= bufflen)) |
| 256 | memset(buffer + (bufflen - req->resid_len), 0, req->resid_len); |
Alan Stern | bdb2b8c | 2008-06-24 14:03:14 -0400 | [diff] [blame] | 257 | |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 258 | if (resid) |
Tejun Heo | c3a4d78 | 2009-05-07 22:24:37 +0900 | [diff] [blame] | 259 | *resid = req->resid_len; |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 260 | ret = req->errors; |
| 261 | out: |
| 262 | blk_put_request(req); |
| 263 | |
| 264 | return ret; |
| 265 | } |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 266 | |
| 267 | /** |
| 268 | * scsi_execute - insert request and wait for the result |
| 269 | * @sdev: scsi device |
| 270 | * @cmd: scsi command |
| 271 | * @data_direction: data direction |
| 272 | * @buffer: data buffer |
| 273 | * @bufflen: len of buffer |
| 274 | * @sense: optional sense buffer |
| 275 | * @timeout: request timeout in seconds |
| 276 | * @retries: number of times to retry request |
| 277 | * @flags: or into request flags; |
| 278 | * @resid: optional residual length |
| 279 | * |
| 280 | * returns the req->errors value which is the scsi_cmnd result |
| 281 | * field. |
| 282 | */ |
| 283 | int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd, |
| 284 | int data_direction, void *buffer, unsigned bufflen, |
| 285 | unsigned char *sense, int timeout, int retries, u64 flags, |
| 286 | int *resid) |
| 287 | { |
| 288 | return __scsi_execute(sdev, cmd, data_direction, buffer, bufflen, sense, |
| 289 | timeout, retries, flags, 0, resid); |
| 290 | } |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 291 | EXPORT_SYMBOL(scsi_execute); |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 292 | |
Lin Ming | 9b21493 | 2013-03-23 11:42:25 +0800 | [diff] [blame] | 293 | int scsi_execute_req_flags(struct scsi_device *sdev, const unsigned char *cmd, |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 294 | int data_direction, void *buffer, unsigned bufflen, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 295 | struct scsi_sense_hdr *sshdr, int timeout, int retries, |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 296 | int *resid, u64 flags, req_flags_t rq_flags) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 297 | { |
| 298 | char *sense = NULL; |
akpm@osdl.org | 1ccb48b | 2005-06-26 00:12:51 -0700 | [diff] [blame] | 299 | int result; |
| 300 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 301 | if (sshdr) { |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 302 | sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO); |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 303 | if (!sense) |
| 304 | return DRIVER_ERROR << 24; |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 305 | } |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 306 | result = __scsi_execute(sdev, cmd, data_direction, buffer, bufflen, |
| 307 | sense, timeout, retries, flags, rq_flags, resid); |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 308 | if (sshdr) |
James Bottomley | e514385 | 2005-08-09 11:55:36 -0500 | [diff] [blame] | 309 | scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr); |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 310 | |
| 311 | kfree(sense); |
| 312 | return result; |
| 313 | } |
Lin Ming | 9b21493 | 2013-03-23 11:42:25 +0800 | [diff] [blame] | 314 | EXPORT_SYMBOL(scsi_execute_req_flags); |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | /* |
| 317 | * Function: scsi_init_cmd_errh() |
| 318 | * |
| 319 | * Purpose: Initialize cmd fields related to error handling. |
| 320 | * |
| 321 | * Arguments: cmd - command that is ready to be queued. |
| 322 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | * Notes: This function has the job of initializing a number of |
| 324 | * fields related to error handling. Typically this will |
| 325 | * be called once for each command, as required. |
| 326 | */ |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 327 | static void scsi_init_cmd_errh(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | cmd->serial_number = 0; |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 330 | scsi_set_resid(cmd, 0); |
FUJITA Tomonori | b80ca4f | 2008-01-13 15:46:13 +0900 | [diff] [blame] | 331 | memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (cmd->cmd_len == 0) |
Boaz Harrosh | db4742d | 2008-04-30 11:27:26 +0300 | [diff] [blame] | 333 | cmd->cmd_len = scsi_command_size(cmd->cmnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void scsi_device_unbusy(struct scsi_device *sdev) |
| 337 | { |
| 338 | struct Scsi_Host *shost = sdev->host; |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 339 | struct scsi_target *starget = scsi_target(sdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | unsigned long flags; |
| 341 | |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 342 | atomic_dec(&shost->host_busy); |
Christoph Hellwig | 2ccbb00 | 2014-03-26 10:35:00 +0100 | [diff] [blame] | 343 | if (starget->can_queue > 0) |
| 344 | atomic_dec(&starget->target_busy); |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 345 | |
James Bottomley | 939647e | 2005-09-18 15:05:20 -0500 | [diff] [blame] | 346 | if (unlikely(scsi_host_in_recovery(shost) && |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 347 | (shost->host_failed || shost->host_eh_scheduled))) { |
| 348 | spin_lock_irqsave(shost->host_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | scsi_eh_wakeup(shost); |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 350 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 351 | } |
| 352 | |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 353 | atomic_dec(&sdev->device_busy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 356 | static void scsi_kick_queue(struct request_queue *q) |
| 357 | { |
| 358 | if (q->mq_ops) |
| 359 | blk_mq_start_hw_queues(q); |
| 360 | else |
| 361 | blk_run_queue(q); |
| 362 | } |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /* |
| 365 | * Called for single_lun devices on IO completion. Clear starget_sdev_user, |
| 366 | * and call blk_run_queue for all the scsi_devices on the target - |
| 367 | * including current_sdev first. |
| 368 | * |
| 369 | * Called with *no* scsi locks held. |
| 370 | */ |
| 371 | static void scsi_single_lun_run(struct scsi_device *current_sdev) |
| 372 | { |
| 373 | struct Scsi_Host *shost = current_sdev->host; |
| 374 | struct scsi_device *sdev, *tmp; |
| 375 | struct scsi_target *starget = scsi_target(current_sdev); |
| 376 | unsigned long flags; |
| 377 | |
| 378 | spin_lock_irqsave(shost->host_lock, flags); |
| 379 | starget->starget_sdev_user = NULL; |
| 380 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 381 | |
| 382 | /* |
| 383 | * Call blk_run_queue for all LUNs on the target, starting with |
| 384 | * current_sdev. We race with others (to set starget_sdev_user), |
| 385 | * but in most cases, we will be first. Ideally, each LU on the |
| 386 | * target would get some limited time or requests on the target. |
| 387 | */ |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 388 | scsi_kick_queue(current_sdev->request_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | spin_lock_irqsave(shost->host_lock, flags); |
| 391 | if (starget->starget_sdev_user) |
| 392 | goto out; |
| 393 | list_for_each_entry_safe(sdev, tmp, &starget->devices, |
| 394 | same_target_siblings) { |
| 395 | if (sdev == current_sdev) |
| 396 | continue; |
| 397 | if (scsi_device_get(sdev)) |
| 398 | continue; |
| 399 | |
| 400 | spin_unlock_irqrestore(shost->host_lock, flags); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 401 | scsi_kick_queue(sdev->request_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | spin_lock_irqsave(shost->host_lock, flags); |
| 403 | |
| 404 | scsi_device_put(sdev); |
| 405 | } |
| 406 | out: |
| 407 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 408 | } |
| 409 | |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 410 | static inline bool scsi_device_is_busy(struct scsi_device *sdev) |
Kiyoshi Ueda | 9d11251 | 2008-10-04 14:11:06 -0400 | [diff] [blame] | 411 | { |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 412 | if (atomic_read(&sdev->device_busy) >= sdev->queue_depth) |
| 413 | return true; |
| 414 | if (atomic_read(&sdev->device_blocked) > 0) |
| 415 | return true; |
| 416 | return false; |
Kiyoshi Ueda | 9d11251 | 2008-10-04 14:11:06 -0400 | [diff] [blame] | 417 | } |
| 418 | |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 419 | static inline bool scsi_target_is_busy(struct scsi_target *starget) |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 420 | { |
Christoph Hellwig | 2ccbb00 | 2014-03-26 10:35:00 +0100 | [diff] [blame] | 421 | if (starget->can_queue > 0) { |
| 422 | if (atomic_read(&starget->target_busy) >= starget->can_queue) |
| 423 | return true; |
| 424 | if (atomic_read(&starget->target_blocked) > 0) |
| 425 | return true; |
| 426 | } |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 427 | return false; |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 428 | } |
| 429 | |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 430 | static inline bool scsi_host_is_busy(struct Scsi_Host *shost) |
Kiyoshi Ueda | 9d11251 | 2008-10-04 14:11:06 -0400 | [diff] [blame] | 431 | { |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 432 | if (shost->can_queue > 0 && |
| 433 | atomic_read(&shost->host_busy) >= shost->can_queue) |
| 434 | return true; |
| 435 | if (atomic_read(&shost->host_blocked) > 0) |
| 436 | return true; |
| 437 | if (shost->host_self_blocked) |
| 438 | return true; |
| 439 | return false; |
Kiyoshi Ueda | 9d11251 | 2008-10-04 14:11:06 -0400 | [diff] [blame] | 440 | } |
| 441 | |
Christoph Hellwig | 21a05df | 2014-02-20 14:20:54 -0800 | [diff] [blame] | 442 | static void scsi_starved_list_run(struct Scsi_Host *shost) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Mike Christie | 2a3a59e | 2008-11-11 13:42:35 -0600 | [diff] [blame] | 444 | LIST_HEAD(starved_list); |
Christoph Hellwig | 21a05df | 2014-02-20 14:20:54 -0800 | [diff] [blame] | 445 | struct scsi_device *sdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | unsigned long flags; |
| 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | spin_lock_irqsave(shost->host_lock, flags); |
Mike Christie | 2a3a59e | 2008-11-11 13:42:35 -0600 | [diff] [blame] | 449 | list_splice_init(&shost->starved_list, &starved_list); |
| 450 | |
| 451 | while (!list_empty(&starved_list)) { |
James Bottomley | e2eb724 | 2013-07-02 15:05:26 +0200 | [diff] [blame] | 452 | struct request_queue *slq; |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | /* |
| 455 | * As long as shost is accepting commands and we have |
| 456 | * starved queues, call blk_run_queue. scsi_request_fn |
| 457 | * drops the queue_lock and can add us back to the |
| 458 | * starved_list. |
| 459 | * |
| 460 | * host_lock protects the starved_list and starved_entry. |
| 461 | * scsi_request_fn must get the host_lock before checking |
| 462 | * or modifying starved_list or starved_entry. |
| 463 | */ |
Mike Christie | 2a3a59e | 2008-11-11 13:42:35 -0600 | [diff] [blame] | 464 | if (scsi_host_is_busy(shost)) |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 465 | break; |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 466 | |
Mike Christie | 2a3a59e | 2008-11-11 13:42:35 -0600 | [diff] [blame] | 467 | sdev = list_entry(starved_list.next, |
| 468 | struct scsi_device, starved_entry); |
| 469 | list_del_init(&sdev->starved_entry); |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 470 | if (scsi_target_is_busy(scsi_target(sdev))) { |
| 471 | list_move_tail(&sdev->starved_entry, |
| 472 | &shost->starved_list); |
| 473 | continue; |
| 474 | } |
| 475 | |
James Bottomley | e2eb724 | 2013-07-02 15:05:26 +0200 | [diff] [blame] | 476 | /* |
| 477 | * Once we drop the host lock, a racing scsi_remove_device() |
| 478 | * call may remove the sdev from the starved list and destroy |
| 479 | * it and the queue. Mitigate by taking a reference to the |
| 480 | * queue and never touching the sdev again after we drop the |
| 481 | * host lock. Note: if __scsi_remove_device() invokes |
| 482 | * blk_cleanup_queue() before the queue is run from this |
| 483 | * function then blk_run_queue() will return immediately since |
| 484 | * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING. |
| 485 | */ |
| 486 | slq = sdev->request_queue; |
| 487 | if (!blk_get_queue(slq)) |
| 488 | continue; |
| 489 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 490 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 491 | scsi_kick_queue(slq); |
James Bottomley | e2eb724 | 2013-07-02 15:05:26 +0200 | [diff] [blame] | 492 | blk_put_queue(slq); |
| 493 | |
| 494 | spin_lock_irqsave(shost->host_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
Mike Christie | 2a3a59e | 2008-11-11 13:42:35 -0600 | [diff] [blame] | 496 | /* put any unprocessed entries back */ |
| 497 | list_splice(&starved_list, &shost->starved_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | spin_unlock_irqrestore(shost->host_lock, flags); |
Christoph Hellwig | 21a05df | 2014-02-20 14:20:54 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Function: scsi_run_queue() |
| 503 | * |
| 504 | * Purpose: Select a proper request queue to serve next |
| 505 | * |
| 506 | * Arguments: q - last request's queue |
| 507 | * |
| 508 | * Returns: Nothing |
| 509 | * |
| 510 | * Notes: The previous command was completely finished, start |
| 511 | * a new one if possible. |
| 512 | */ |
| 513 | static void scsi_run_queue(struct request_queue *q) |
| 514 | { |
| 515 | struct scsi_device *sdev = q->queuedata; |
| 516 | |
| 517 | if (scsi_target(sdev)->single_lun) |
| 518 | scsi_single_lun_run(sdev); |
| 519 | if (!list_empty(&sdev->host->starved_list)) |
| 520 | scsi_starved_list_run(sdev->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 522 | if (q->mq_ops) |
| 523 | blk_mq_start_stopped_hw_queues(q, false); |
| 524 | else |
| 525 | blk_run_queue(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Jens Axboe | 9937a5e | 2011-05-17 11:04:44 +0200 | [diff] [blame] | 528 | void scsi_requeue_run_queue(struct work_struct *work) |
| 529 | { |
| 530 | struct scsi_device *sdev; |
| 531 | struct request_queue *q; |
| 532 | |
| 533 | sdev = container_of(work, struct scsi_device, requeue_work); |
| 534 | q = sdev->request_queue; |
| 535 | scsi_run_queue(q); |
| 536 | } |
| 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | /* |
| 539 | * Function: scsi_requeue_command() |
| 540 | * |
| 541 | * Purpose: Handle post-processing of completed commands. |
| 542 | * |
| 543 | * Arguments: q - queue to operate on |
| 544 | * cmd - command that may need to be requeued. |
| 545 | * |
| 546 | * Returns: Nothing |
| 547 | * |
| 548 | * Notes: After command completion, there may be blocks left |
| 549 | * over which weren't finished by the previous command |
| 550 | * this can be for a number of reasons - the main one is |
| 551 | * I/O errors in the middle of the request, in which case |
| 552 | * we need to request the blocks that come after the bad |
| 553 | * sector. |
James Bottomley | e91442b | 2005-09-09 10:44:16 -0500 | [diff] [blame] | 554 | * Notes: Upon return, cmd is a stale pointer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | */ |
| 556 | static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd) |
| 557 | { |
Bart Van Assche | 940f5d4 | 2012-06-29 15:34:26 +0000 | [diff] [blame] | 558 | struct scsi_device *sdev = cmd->device; |
James Bottomley | e91442b | 2005-09-09 10:44:16 -0500 | [diff] [blame] | 559 | struct request *req = cmd->request; |
Tejun Heo | 283369c | 2005-04-24 02:06:36 -0500 | [diff] [blame] | 560 | unsigned long flags; |
| 561 | |
Tejun Heo | 283369c | 2005-04-24 02:06:36 -0500 | [diff] [blame] | 562 | spin_lock_irqsave(q->queue_lock, flags); |
Christoph Hellwig | 134997a | 2014-02-20 14:20:58 -0800 | [diff] [blame] | 563 | blk_unprep_request(req); |
| 564 | req->special = NULL; |
| 565 | scsi_put_command(cmd); |
James Bottomley | e91442b | 2005-09-09 10:44:16 -0500 | [diff] [blame] | 566 | blk_requeue_request(q, req); |
Tejun Heo | 283369c | 2005-04-24 02:06:36 -0500 | [diff] [blame] | 567 | spin_unlock_irqrestore(q->queue_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | scsi_run_queue(q); |
Bart Van Assche | 940f5d4 | 2012-06-29 15:34:26 +0000 | [diff] [blame] | 570 | |
| 571 | put_device(&sdev->sdev_gendev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | void scsi_run_host_queues(struct Scsi_Host *shost) |
| 575 | { |
| 576 | struct scsi_device *sdev; |
| 577 | |
| 578 | shost_for_each_device(sdev, shost) |
| 579 | scsi_run_queue(sdev->request_queue); |
| 580 | } |
| 581 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 582 | static void scsi_uninit_cmd(struct scsi_cmnd *cmd) |
| 583 | { |
| 584 | if (cmd->request->cmd_type == REQ_TYPE_FS) { |
| 585 | struct scsi_driver *drv = scsi_cmd_to_driver(cmd); |
| 586 | |
| 587 | if (drv->uninit_command) |
| 588 | drv->uninit_command(cmd); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd) |
| 593 | { |
Ming Lin | 91dbc08 | 2016-04-04 14:48:07 -0700 | [diff] [blame] | 594 | struct scsi_data_buffer *sdb; |
| 595 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 596 | if (cmd->sdb.table.nents) |
Ming Lin | 001d63b | 2016-04-04 14:48:09 -0700 | [diff] [blame] | 597 | sg_free_table_chained(&cmd->sdb.table, true); |
Ming Lin | 91dbc08 | 2016-04-04 14:48:07 -0700 | [diff] [blame] | 598 | if (cmd->request->next_rq) { |
| 599 | sdb = cmd->request->next_rq->special; |
| 600 | if (sdb) |
Ming Lin | 001d63b | 2016-04-04 14:48:09 -0700 | [diff] [blame] | 601 | sg_free_table_chained(&sdb->table, true); |
Ming Lin | 91dbc08 | 2016-04-04 14:48:07 -0700 | [diff] [blame] | 602 | } |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 603 | if (scsi_prot_sg_count(cmd)) |
Ming Lin | 001d63b | 2016-04-04 14:48:09 -0700 | [diff] [blame] | 604 | sg_free_table_chained(&cmd->prot_sdb->table, true); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd) |
| 608 | { |
| 609 | struct scsi_device *sdev = cmd->device; |
Kashyap.Desai@avagotech.com | 64bdcbc | 2014-08-20 19:24:33 +0530 | [diff] [blame] | 610 | struct Scsi_Host *shost = sdev->host; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 611 | unsigned long flags; |
| 612 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 613 | scsi_mq_free_sgtables(cmd); |
| 614 | scsi_uninit_cmd(cmd); |
| 615 | |
Kashyap.Desai@avagotech.com | 64bdcbc | 2014-08-20 19:24:33 +0530 | [diff] [blame] | 616 | if (shost->use_cmd_list) { |
| 617 | BUG_ON(list_empty(&cmd->list)); |
| 618 | spin_lock_irqsave(&sdev->list_lock, flags); |
| 619 | list_del_init(&cmd->list); |
| 620 | spin_unlock_irqrestore(&sdev->list_lock, flags); |
| 621 | } |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | /* |
| 625 | * Function: scsi_release_buffers() |
| 626 | * |
Christoph Hellwig | c682adf | 2014-05-01 16:51:02 +0200 | [diff] [blame] | 627 | * Purpose: Free resources allocate for a scsi_command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | * |
| 629 | * Arguments: cmd - command that we are bailing. |
| 630 | * |
| 631 | * Lock status: Assumed that no lock is held upon entry. |
| 632 | * |
| 633 | * Returns: Nothing |
| 634 | * |
| 635 | * Notes: In the event that an upper level driver rejects a |
| 636 | * command, we must release resources allocated during |
| 637 | * the __init_io() function. Primarily this would involve |
Christoph Hellwig | c682adf | 2014-05-01 16:51:02 +0200 | [diff] [blame] | 638 | * the scatter-gather table. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | */ |
Christoph Hellwig | f1bea55 | 2014-04-15 12:26:54 +0200 | [diff] [blame] | 640 | static void scsi_release_buffers(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
Christoph Hellwig | c682adf | 2014-05-01 16:51:02 +0200 | [diff] [blame] | 642 | if (cmd->sdb.table.nents) |
Ming Lin | 001d63b | 2016-04-04 14:48:09 -0700 | [diff] [blame] | 643 | sg_free_table_chained(&cmd->sdb.table, false); |
Christoph Hellwig | c682adf | 2014-05-01 16:51:02 +0200 | [diff] [blame] | 644 | |
| 645 | memset(&cmd->sdb, 0, sizeof(cmd->sdb)); |
| 646 | |
| 647 | if (scsi_prot_sg_count(cmd)) |
Ming Lin | 001d63b | 2016-04-04 14:48:09 -0700 | [diff] [blame] | 648 | sg_free_table_chained(&cmd->prot_sdb->table, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Christoph Hellwig | c682adf | 2014-05-01 16:51:02 +0200 | [diff] [blame] | 651 | static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd) |
| 652 | { |
| 653 | struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special; |
| 654 | |
Ming Lin | 001d63b | 2016-04-04 14:48:09 -0700 | [diff] [blame] | 655 | sg_free_table_chained(&bidi_sdb->table, false); |
Christoph Hellwig | c682adf | 2014-05-01 16:51:02 +0200 | [diff] [blame] | 656 | kmem_cache_free(scsi_sdb_cache, bidi_sdb); |
| 657 | cmd->request->next_rq->special = NULL; |
| 658 | } |
| 659 | |
Christoph Hellwig | f6d47e7 | 2014-02-16 06:16:13 -0800 | [diff] [blame] | 660 | static bool scsi_end_request(struct request *req, int error, |
| 661 | unsigned int bytes, unsigned int bidi_bytes) |
| 662 | { |
| 663 | struct scsi_cmnd *cmd = req->special; |
| 664 | struct scsi_device *sdev = cmd->device; |
| 665 | struct request_queue *q = sdev->request_queue; |
Christoph Hellwig | f6d47e7 | 2014-02-16 06:16:13 -0800 | [diff] [blame] | 666 | |
| 667 | if (blk_update_request(req, error, bytes)) |
| 668 | return true; |
| 669 | |
| 670 | /* Bidi request must be completed as a whole */ |
| 671 | if (unlikely(bidi_bytes) && |
| 672 | blk_update_request(req->next_rq, error, bidi_bytes)) |
| 673 | return true; |
| 674 | |
| 675 | if (blk_queue_add_random(q)) |
| 676 | add_disk_randomness(req->rq_disk); |
| 677 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 678 | if (req->mq_ctx) { |
| 679 | /* |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 680 | * In the MQ case the command gets freed by __blk_mq_end_request, |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 681 | * so we have to do all cleanup that depends on it earlier. |
| 682 | * |
| 683 | * We also can't kick the queues from irq context, so we |
| 684 | * will have to defer it to a workqueue. |
| 685 | */ |
| 686 | scsi_mq_uninit_cmd(cmd); |
Christoph Hellwig | f6d47e7 | 2014-02-16 06:16:13 -0800 | [diff] [blame] | 687 | |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 688 | __blk_mq_end_request(req, error); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 689 | |
| 690 | if (scsi_target(sdev)->single_lun || |
| 691 | !list_empty(&sdev->host->starved_list)) |
| 692 | kblockd_schedule_work(&sdev->requeue_work); |
| 693 | else |
| 694 | blk_mq_start_stopped_hw_queues(q, true); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 695 | } else { |
| 696 | unsigned long flags; |
| 697 | |
Daniel Gryniewicz | f81426a | 2014-09-16 10:41:13 -0400 | [diff] [blame] | 698 | if (bidi_bytes) |
| 699 | scsi_release_bidi_buffers(cmd); |
| 700 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 701 | spin_lock_irqsave(q->queue_lock, flags); |
| 702 | blk_finish_request(req, error); |
| 703 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 704 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 705 | scsi_release_buffers(cmd); |
Christoph Hellwig | bb3ec62 | 2014-09-05 18:02:09 -0700 | [diff] [blame] | 706 | |
| 707 | scsi_put_command(cmd); |
| 708 | scsi_run_queue(q); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 709 | } |
| 710 | |
Christoph Hellwig | bb3ec62 | 2014-09-05 18:02:09 -0700 | [diff] [blame] | 711 | put_device(&sdev->sdev_gendev); |
Christoph Hellwig | f6d47e7 | 2014-02-16 06:16:13 -0800 | [diff] [blame] | 712 | return false; |
| 713 | } |
| 714 | |
Hannes Reinecke | 0f7f623 | 2013-07-01 15:16:23 +0200 | [diff] [blame] | 715 | /** |
| 716 | * __scsi_error_from_host_byte - translate SCSI error code into errno |
| 717 | * @cmd: SCSI command (unused) |
| 718 | * @result: scsi error code |
| 719 | * |
| 720 | * Translate SCSI error code into standard UNIX errno. |
| 721 | * Return values: |
| 722 | * -ENOLINK temporary transport failure |
| 723 | * -EREMOTEIO permanent target failure, do not retry |
| 724 | * -EBADE permanent nexus failure, retry on other path |
Hannes Reinecke | a9d6ceb8 | 2013-07-01 15:16:25 +0200 | [diff] [blame] | 725 | * -ENOSPC No write space available |
Hannes Reinecke | 7e782af | 2013-07-01 15:16:26 +0200 | [diff] [blame] | 726 | * -ENODATA Medium error |
Hannes Reinecke | 0f7f623 | 2013-07-01 15:16:23 +0200 | [diff] [blame] | 727 | * -EIO unspecified I/O error |
| 728 | */ |
Hannes Reinecke | 63583cc | 2011-01-18 10:13:11 +0100 | [diff] [blame] | 729 | static int __scsi_error_from_host_byte(struct scsi_cmnd *cmd, int result) |
| 730 | { |
| 731 | int error = 0; |
| 732 | |
| 733 | switch(host_byte(result)) { |
| 734 | case DID_TRANSPORT_FAILFAST: |
| 735 | error = -ENOLINK; |
| 736 | break; |
| 737 | case DID_TARGET_FAILURE: |
Moger, Babu | 2082ebc | 2012-01-24 20:38:46 +0000 | [diff] [blame] | 738 | set_host_byte(cmd, DID_OK); |
Hannes Reinecke | 63583cc | 2011-01-18 10:13:11 +0100 | [diff] [blame] | 739 | error = -EREMOTEIO; |
| 740 | break; |
| 741 | case DID_NEXUS_FAILURE: |
Moger, Babu | 2082ebc | 2012-01-24 20:38:46 +0000 | [diff] [blame] | 742 | set_host_byte(cmd, DID_OK); |
Hannes Reinecke | 63583cc | 2011-01-18 10:13:11 +0100 | [diff] [blame] | 743 | error = -EBADE; |
| 744 | break; |
Hannes Reinecke | a9d6ceb8 | 2013-07-01 15:16:25 +0200 | [diff] [blame] | 745 | case DID_ALLOC_FAILURE: |
| 746 | set_host_byte(cmd, DID_OK); |
| 747 | error = -ENOSPC; |
| 748 | break; |
Hannes Reinecke | 7e782af | 2013-07-01 15:16:26 +0200 | [diff] [blame] | 749 | case DID_MEDIUM_ERROR: |
| 750 | set_host_byte(cmd, DID_OK); |
| 751 | error = -ENODATA; |
| 752 | break; |
Hannes Reinecke | 63583cc | 2011-01-18 10:13:11 +0100 | [diff] [blame] | 753 | default: |
| 754 | error = -EIO; |
| 755 | break; |
| 756 | } |
| 757 | |
| 758 | return error; |
| 759 | } |
| 760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | /* |
| 762 | * Function: scsi_io_completion() |
| 763 | * |
| 764 | * Purpose: Completion processing for block device I/O requests. |
| 765 | * |
| 766 | * Arguments: cmd - command that is finished. |
| 767 | * |
| 768 | * Lock status: Assumed that no lock is held upon entry. |
| 769 | * |
| 770 | * Returns: Nothing |
| 771 | * |
Christoph Hellwig | bc85dc5 | 2014-05-01 16:51:03 +0200 | [diff] [blame] | 772 | * Notes: We will finish off the specified number of sectors. If we |
| 773 | * are done, the command block will be released and the queue |
| 774 | * function will be goosed. If we are not done then we have to |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 775 | * figure out what to do next: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | * |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 777 | * a) We can call scsi_requeue_command(). The request |
| 778 | * will be unprepared and put back on the queue. Then |
| 779 | * a new command will be created for it. This should |
| 780 | * be used if we made forward progress, or if we want |
| 781 | * to switch from READ(10) to READ(6) for example. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | * |
Christoph Hellwig | bc85dc5 | 2014-05-01 16:51:03 +0200 | [diff] [blame] | 783 | * b) We can call __scsi_queue_insert(). The request will |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 784 | * be put back on the queue and retried using the same |
| 785 | * command as before, possibly after a delay. |
| 786 | * |
Christoph Hellwig | f6d47e7 | 2014-02-16 06:16:13 -0800 | [diff] [blame] | 787 | * c) We can call scsi_end_request() with -EIO to fail |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 788 | * the remainder of the request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | */ |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 790 | void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
| 792 | int result = cmd->result; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 793 | struct request_queue *q = cmd->device->request_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | struct request *req = cmd->request; |
James Bottomley | fa8e36c | 2008-04-02 18:11:52 -0500 | [diff] [blame] | 795 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | struct scsi_sense_hdr sshdr; |
Hannes Reinecke | 4753cbc | 2014-10-24 14:26:52 +0200 | [diff] [blame] | 797 | bool sense_valid = false; |
Hannes Reinecke | c11c004 | 2014-10-24 14:27:01 +0200 | [diff] [blame] | 798 | int sense_deferred = 0, level = 0; |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 799 | enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY, |
| 800 | ACTION_DELAYED_RETRY} action; |
Eiichi Tsukata | ee60b2c | 2014-02-11 14:29:52 +0900 | [diff] [blame] | 801 | unsigned long wait_for = (cmd->allowed + 1) * req->timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | if (result) { |
| 804 | sense_valid = scsi_command_normalize_sense(cmd, &sshdr); |
| 805 | if (sense_valid) |
| 806 | sense_deferred = scsi_sense_is_deferred(&sshdr); |
| 807 | } |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 808 | |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 809 | if (req->cmd_type == REQ_TYPE_BLOCK_PC) { /* SG_IO ioctl from block level */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | if (result) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | if (sense_valid && req->sense) { |
| 812 | /* |
| 813 | * SG_IO wants current and deferred errors |
| 814 | */ |
| 815 | int len = 8 + cmd->sense_buffer[7]; |
| 816 | |
| 817 | if (len > SCSI_SENSE_BUFFERSIZE) |
| 818 | len = SCSI_SENSE_BUFFERSIZE; |
| 819 | memcpy(req->sense, cmd->sense_buffer, len); |
| 820 | req->sense_len = len; |
| 821 | } |
James Bottomley | fa8e36c | 2008-04-02 18:11:52 -0500 | [diff] [blame] | 822 | if (!sense_deferred) |
Hannes Reinecke | 63583cc | 2011-01-18 10:13:11 +0100 | [diff] [blame] | 823 | error = __scsi_error_from_host_byte(cmd, result); |
Pete Wyckoff | b22f687 | 2007-03-13 16:53:28 -0400 | [diff] [blame] | 824 | } |
Mike Snitzer | 27c4197 | 2012-05-31 15:05:33 -0400 | [diff] [blame] | 825 | /* |
| 826 | * __scsi_error_from_host_byte may have reset the host_byte |
| 827 | */ |
| 828 | req->errors = cmd->result; |
FUJITA Tomonori | e6bb7a96 | 2009-05-11 17:56:08 +0900 | [diff] [blame] | 829 | |
| 830 | req->resid_len = scsi_get_resid(cmd); |
| 831 | |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 832 | if (scsi_bidi_cmnd(cmd)) { |
FUJITA Tomonori | e6bb7a96 | 2009-05-11 17:56:08 +0900 | [diff] [blame] | 833 | /* |
| 834 | * Bidi commands Must be complete as a whole, |
| 835 | * both sides at once. |
| 836 | */ |
| 837 | req->next_rq->resid_len = scsi_in(cmd)->resid; |
Christoph Hellwig | f6d47e7 | 2014-02-16 06:16:13 -0800 | [diff] [blame] | 838 | if (scsi_end_request(req, 0, blk_rq_bytes(req), |
| 839 | blk_rq_bytes(req->next_rq))) |
| 840 | BUG(); |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 841 | return; |
| 842 | } |
James Bottomley | 89fb4cd | 2014-07-03 19:17:34 +0200 | [diff] [blame] | 843 | } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) { |
| 844 | /* |
| 845 | * Certain non BLOCK_PC requests are commands that don't |
| 846 | * actually transfer anything (FLUSH), so cannot use |
| 847 | * good_bytes != blk_rq_bytes(req) as the signal for an error. |
| 848 | * This sets the error explicitly for the problem case. |
| 849 | */ |
| 850 | error = __scsi_error_from_host_byte(cmd, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 853 | /* no bidi support for !REQ_TYPE_BLOCK_PC yet */ |
| 854 | BUG_ON(blk_bidi_rq(req)); |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | * Next deal with any sectors which we were able to correctly |
| 858 | * handle. |
| 859 | */ |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 860 | SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd, |
| 861 | "%u sectors total, %d bytes done.\n", |
| 862 | blk_rq_sectors(req), good_bytes)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
James Bottomley | a9bddd7 | 2009-03-30 16:55:51 +0000 | [diff] [blame] | 864 | /* |
| 865 | * Recovered errors need reporting, but they're always treated |
| 866 | * as success, so fiddle the result code here. For BLOCK_PC |
| 867 | * we already took a copy of the original into rq->errors which |
| 868 | * is what gets returned to the user |
| 869 | */ |
Douglas Gilbert | e7efe59 | 2010-01-03 13:51:15 -0500 | [diff] [blame] | 870 | if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) { |
| 871 | /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip |
| 872 | * print since caller wants ATA registers. Only occurs on |
| 873 | * SCSI ATA PASS_THROUGH commands when CK_COND=1 |
| 874 | */ |
| 875 | if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d)) |
| 876 | ; |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 877 | else if (!(req->rq_flags & RQF_QUIET)) |
Hannes Reinecke | d811b84 | 2014-10-24 14:26:45 +0200 | [diff] [blame] | 878 | scsi_print_sense(cmd); |
James Bottomley | a9bddd7 | 2009-03-30 16:55:51 +0000 | [diff] [blame] | 879 | result = 0; |
| 880 | /* BLOCK_PC may have set error */ |
| 881 | error = 0; |
| 882 | } |
| 883 | |
| 884 | /* |
James Bottomley | a621bac | 2016-05-13 12:04:06 -0700 | [diff] [blame] | 885 | * special case: failed zero length commands always need to |
| 886 | * drop down into the retry code. Otherwise, if we finished |
| 887 | * all bytes in the request we are done now. |
James Bottomley | d6b0c53 | 2006-07-02 10:06:28 -0500 | [diff] [blame] | 888 | */ |
James Bottomley | a621bac | 2016-05-13 12:04:06 -0700 | [diff] [blame] | 889 | if (!(blk_rq_bytes(req) == 0 && error) && |
| 890 | !scsi_end_request(req, error, good_bytes, 0)) |
Christoph Hellwig | f6d47e7 | 2014-02-16 06:16:13 -0800 | [diff] [blame] | 891 | return; |
Christoph Hellwig | bc85dc5 | 2014-05-01 16:51:03 +0200 | [diff] [blame] | 892 | |
| 893 | /* |
| 894 | * Kill remainder if no retrys. |
| 895 | */ |
| 896 | if (error && scsi_noretry_cmd(cmd)) { |
Christoph Hellwig | f6d47e7 | 2014-02-16 06:16:13 -0800 | [diff] [blame] | 897 | if (scsi_end_request(req, error, blk_rq_bytes(req), 0)) |
| 898 | BUG(); |
| 899 | return; |
Christoph Hellwig | bc85dc5 | 2014-05-01 16:51:03 +0200 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /* |
| 903 | * If there had been no error, but we have leftover bytes in the |
| 904 | * requeues just queue the command up again. |
| 905 | */ |
| 906 | if (result == 0) |
| 907 | goto requeue; |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 908 | |
Hannes Reinecke | 63583cc | 2011-01-18 10:13:11 +0100 | [diff] [blame] | 909 | error = __scsi_error_from_host_byte(cmd, result); |
Martin K. Petersen | 3e695f8 | 2009-01-04 03:04:31 -0500 | [diff] [blame] | 910 | |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 911 | if (host_byte(result) == DID_RESET) { |
| 912 | /* Third party bus reset or reset for error recovery |
| 913 | * reasons. Just retry the command and see what |
| 914 | * happens. |
| 915 | */ |
| 916 | action = ACTION_RETRY; |
| 917 | } else if (sense_valid && !sense_deferred) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | switch (sshdr.sense_key) { |
| 919 | case UNIT_ATTENTION: |
| 920 | if (cmd->device->removable) { |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 921 | /* Detected disc change. Set a bit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | * and quietly refuse further access. |
| 923 | */ |
| 924 | cmd->device->changed = 1; |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 925 | action = ACTION_FAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } else { |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 927 | /* Must have been a power glitch, or a |
| 928 | * bus reset. Could not have been a |
| 929 | * media change, so we just retry the |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 930 | * command and see what happens. |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 931 | */ |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 932 | action = ACTION_RETRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
| 934 | break; |
| 935 | case ILLEGAL_REQUEST: |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 936 | /* If we had an ILLEGAL REQUEST returned, then |
| 937 | * we may have performed an unsupported |
| 938 | * command. The only thing this should be |
| 939 | * would be a ten byte read where only a six |
| 940 | * byte read was supported. Also, on a system |
| 941 | * where READ CAPACITY failed, we may have |
| 942 | * read past the end of the disk. |
| 943 | */ |
Jens Axboe | 26a6801 | 2005-11-29 21:03:34 +0100 | [diff] [blame] | 944 | if ((cmd->device->use_10_for_rw && |
| 945 | sshdr.asc == 0x20 && sshdr.ascq == 0x00) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | (cmd->cmnd[0] == READ_10 || |
| 947 | cmd->cmnd[0] == WRITE_10)) { |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 948 | /* This will issue a new 6-byte command. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | cmd->device->use_10_for_rw = 0; |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 950 | action = ACTION_REPREP; |
Martin K. Petersen | 3e695f8 | 2009-01-04 03:04:31 -0500 | [diff] [blame] | 951 | } else if (sshdr.asc == 0x10) /* DIX */ { |
Martin K. Petersen | 3e695f8 | 2009-01-04 03:04:31 -0500 | [diff] [blame] | 952 | action = ACTION_FAIL; |
| 953 | error = -EILSEQ; |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 954 | /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */ |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 955 | } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) { |
Martin K. Petersen | c98a0eb | 2011-03-08 02:07:15 -0500 | [diff] [blame] | 956 | action = ACTION_FAIL; |
Martin K. Petersen | 66a651a | 2012-02-13 15:38:22 -0500 | [diff] [blame] | 957 | error = -EREMOTEIO; |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 958 | } else |
| 959 | action = ACTION_FAIL; |
| 960 | break; |
Martin K. Petersen | 511e44f | 2008-07-17 04:28:33 -0400 | [diff] [blame] | 961 | case ABORTED_COMMAND: |
James Bottomley | 126c0982 | 2009-02-19 21:48:54 +0000 | [diff] [blame] | 962 | action = ACTION_FAIL; |
Maurizio Lombardi | e6c11db | 2014-07-10 09:41:53 +0200 | [diff] [blame] | 963 | if (sshdr.asc == 0x10) /* DIF */ |
Martin K. Petersen | 3e695f8 | 2009-01-04 03:04:31 -0500 | [diff] [blame] | 964 | error = -EILSEQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | break; |
| 966 | case NOT_READY: |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 967 | /* If the device is in the process of becoming |
James Bottomley | f3e93f7 | 2006-04-25 16:48:30 -0500 | [diff] [blame] | 968 | * ready, or has a temporary blockage, retry. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | */ |
James Bottomley | f3e93f7 | 2006-04-25 16:48:30 -0500 | [diff] [blame] | 970 | if (sshdr.asc == 0x04) { |
| 971 | switch (sshdr.ascq) { |
| 972 | case 0x01: /* becoming ready */ |
| 973 | case 0x04: /* format in progress */ |
| 974 | case 0x05: /* rebuild in progress */ |
| 975 | case 0x06: /* recalculation in progress */ |
| 976 | case 0x07: /* operation in progress */ |
| 977 | case 0x08: /* Long write in progress */ |
| 978 | case 0x09: /* self test in progress */ |
Martin K. Petersen | d8705f1 | 2009-11-26 12:00:41 -0500 | [diff] [blame] | 979 | case 0x14: /* space allocation in progress */ |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 980 | action = ACTION_DELAYED_RETRY; |
James Bottomley | f3e93f7 | 2006-04-25 16:48:30 -0500 | [diff] [blame] | 981 | break; |
Alan Stern | 3dbf6a5 | 2008-12-15 10:31:28 -0500 | [diff] [blame] | 982 | default: |
Alan Stern | 3dbf6a5 | 2008-12-15 10:31:28 -0500 | [diff] [blame] | 983 | action = ACTION_FAIL; |
| 984 | break; |
James Bottomley | f3e93f7 | 2006-04-25 16:48:30 -0500 | [diff] [blame] | 985 | } |
Maurizio Lombardi | e6c11db | 2014-07-10 09:41:53 +0200 | [diff] [blame] | 986 | } else |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 987 | action = ACTION_FAIL; |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 988 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | case VOLUME_OVERFLOW: |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 990 | /* See SSC3rXX or current. */ |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 991 | action = ACTION_FAIL; |
| 992 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | default: |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 994 | action = ACTION_FAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | break; |
| 996 | } |
Maurizio Lombardi | e6c11db | 2014-07-10 09:41:53 +0200 | [diff] [blame] | 997 | } else |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 998 | action = ACTION_FAIL; |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 999 | |
Eiichi Tsukata | ee60b2c | 2014-02-11 14:29:52 +0900 | [diff] [blame] | 1000 | if (action != ACTION_FAIL && |
Maurizio Lombardi | e6c11db | 2014-07-10 09:41:53 +0200 | [diff] [blame] | 1001 | time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) |
Eiichi Tsukata | ee60b2c | 2014-02-11 14:29:52 +0900 | [diff] [blame] | 1002 | action = ACTION_FAIL; |
Eiichi Tsukata | ee60b2c | 2014-02-11 14:29:52 +0900 | [diff] [blame] | 1003 | |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 1004 | switch (action) { |
| 1005 | case ACTION_FAIL: |
| 1006 | /* Give up and fail the remainder of the request */ |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 1007 | if (!(req->rq_flags & RQF_QUIET)) { |
Hannes Reinecke | f1569ff | 2014-10-24 14:27:07 +0200 | [diff] [blame] | 1008 | static DEFINE_RATELIMIT_STATE(_rs, |
| 1009 | DEFAULT_RATELIMIT_INTERVAL, |
| 1010 | DEFAULT_RATELIMIT_BURST); |
| 1011 | |
| 1012 | if (unlikely(scsi_logging_level)) |
| 1013 | level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT, |
| 1014 | SCSI_LOG_MLCOMPLETE_BITS); |
| 1015 | |
| 1016 | /* |
| 1017 | * if logging is enabled the failure will be printed |
| 1018 | * in scsi_log_completion(), so avoid duplicate messages |
| 1019 | */ |
| 1020 | if (!level && __ratelimit(&_rs)) { |
| 1021 | scsi_print_result(cmd, NULL, FAILED); |
| 1022 | if (driver_byte(result) & DRIVER_SENSE) |
| 1023 | scsi_print_sense(cmd); |
| 1024 | scsi_print_command(cmd); |
| 1025 | } |
James Bottomley | 3173d8c | 2005-09-04 11:32:05 -0500 | [diff] [blame] | 1026 | } |
Christoph Hellwig | f6d47e7 | 2014-02-16 06:16:13 -0800 | [diff] [blame] | 1027 | if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0)) |
| 1028 | return; |
Christoph Hellwig | bc85dc5 | 2014-05-01 16:51:03 +0200 | [diff] [blame] | 1029 | /*FALLTHRU*/ |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 1030 | case ACTION_REPREP: |
Christoph Hellwig | bc85dc5 | 2014-05-01 16:51:03 +0200 | [diff] [blame] | 1031 | requeue: |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 1032 | /* Unprep the request and put it back at the head of the queue. |
| 1033 | * A new command will be prepared and issued. |
| 1034 | */ |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1035 | if (q->mq_ops) { |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 1036 | cmd->request->rq_flags &= ~RQF_DONTPREP; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1037 | scsi_mq_uninit_cmd(cmd); |
| 1038 | scsi_mq_requeue_cmd(cmd); |
| 1039 | } else { |
| 1040 | scsi_release_buffers(cmd); |
| 1041 | scsi_requeue_command(q, cmd); |
| 1042 | } |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 1043 | break; |
| 1044 | case ACTION_RETRY: |
| 1045 | /* Retry the same command immediately */ |
James Bottomley | 4f5299a | 2009-01-02 10:42:21 -0600 | [diff] [blame] | 1046 | __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0); |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 1047 | break; |
| 1048 | case ACTION_DELAYED_RETRY: |
| 1049 | /* Retry the same command after a delay */ |
James Bottomley | 4f5299a | 2009-01-02 10:42:21 -0600 | [diff] [blame] | 1050 | __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0); |
Alan Stern | b60af5b | 2008-11-03 15:56:47 -0500 | [diff] [blame] | 1051 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | } |
| 1053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
Christoph Hellwig | 3c356bd | 2014-09-05 18:20:23 -0700 | [diff] [blame] | 1055 | static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | { |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 1057 | int count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
| 1059 | /* |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1060 | * If sg table allocation fails, requeue request later. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | */ |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 1062 | if (unlikely(sg_alloc_table_chained(&sdb->table, |
| 1063 | blk_rq_nr_phys_segments(req), sdb->table.sgl))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | return BLKPREP_DEFER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | /* |
| 1067 | * Next, walk the list, and fill in the addresses and sizes of |
| 1068 | * each segment. |
| 1069 | */ |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 1070 | count = blk_rq_map_sg(req->q, req, sdb->table.sgl); |
| 1071 | BUG_ON(count > sdb->table.nents); |
| 1072 | sdb->table.nents = count; |
Christoph Hellwig | fd102b1 | 2017-01-13 12:29:11 +0100 | [diff] [blame] | 1073 | sdb->length = blk_rq_payload_bytes(req); |
Rusty Russell | 4a03d90 | 2007-11-19 11:28:48 +1100 | [diff] [blame] | 1074 | return BLKPREP_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | } |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 1076 | |
| 1077 | /* |
| 1078 | * Function: scsi_init_io() |
| 1079 | * |
| 1080 | * Purpose: SCSI I/O initialize function. |
| 1081 | * |
| 1082 | * Arguments: cmd - Command descriptor we wish to initialize |
| 1083 | * |
| 1084 | * Returns: 0 on success |
| 1085 | * BLKPREP_DEFER if the failure is retryable |
| 1086 | * BLKPREP_KILL if the failure is fatal |
| 1087 | */ |
Christoph Hellwig | 3c356bd | 2014-09-05 18:20:23 -0700 | [diff] [blame] | 1088 | int scsi_init_io(struct scsi_cmnd *cmd) |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 1089 | { |
Christoph Hellwig | 5e012aa | 2014-04-15 12:24:55 +0200 | [diff] [blame] | 1090 | struct scsi_device *sdev = cmd->device; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 1091 | struct request *rq = cmd->request; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1092 | bool is_mq = (rq->mq_ctx != NULL); |
Christoph Hellwig | 635d98b | 2014-06-28 11:51:01 +0200 | [diff] [blame] | 1093 | int error; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 1094 | |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 1095 | BUG_ON(!blk_rq_nr_phys_segments(rq)); |
Christoph Hellwig | 635d98b | 2014-06-28 11:51:01 +0200 | [diff] [blame] | 1096 | |
Christoph Hellwig | 3c356bd | 2014-09-05 18:20:23 -0700 | [diff] [blame] | 1097 | error = scsi_init_sgtable(rq, &cmd->sdb); |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 1098 | if (error) |
| 1099 | goto err_exit; |
| 1100 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 1101 | if (blk_bidi_rq(rq)) { |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1102 | if (!rq->q->mq_ops) { |
| 1103 | struct scsi_data_buffer *bidi_sdb = |
| 1104 | kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC); |
| 1105 | if (!bidi_sdb) { |
| 1106 | error = BLKPREP_DEFER; |
| 1107 | goto err_exit; |
| 1108 | } |
| 1109 | |
| 1110 | rq->next_rq->special = bidi_sdb; |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
Christoph Hellwig | 3c356bd | 2014-09-05 18:20:23 -0700 | [diff] [blame] | 1113 | error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special); |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 1114 | if (error) |
| 1115 | goto err_exit; |
| 1116 | } |
| 1117 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 1118 | if (blk_integrity_rq(rq)) { |
Martin K. Petersen | 7027ad7 | 2008-07-17 17:08:48 -0400 | [diff] [blame] | 1119 | struct scsi_data_buffer *prot_sdb = cmd->prot_sdb; |
| 1120 | int ivecs, count; |
| 1121 | |
Ewan D. Milne | 91724c2 | 2015-01-15 10:02:12 -0500 | [diff] [blame] | 1122 | if (prot_sdb == NULL) { |
| 1123 | /* |
| 1124 | * This can happen if someone (e.g. multipath) |
| 1125 | * queues a command to a device on an adapter |
| 1126 | * that does not support DIX. |
| 1127 | */ |
| 1128 | WARN_ON_ONCE(1); |
| 1129 | error = BLKPREP_KILL; |
| 1130 | goto err_exit; |
| 1131 | } |
| 1132 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 1133 | ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio); |
Martin K. Petersen | 7027ad7 | 2008-07-17 17:08:48 -0400 | [diff] [blame] | 1134 | |
Ming Lin | 001d63b | 2016-04-04 14:48:09 -0700 | [diff] [blame] | 1135 | if (sg_alloc_table_chained(&prot_sdb->table, ivecs, |
Ming Lin | 22cc3d4 | 2016-04-04 14:48:08 -0700 | [diff] [blame] | 1136 | prot_sdb->table.sgl)) { |
Martin K. Petersen | 7027ad7 | 2008-07-17 17:08:48 -0400 | [diff] [blame] | 1137 | error = BLKPREP_DEFER; |
| 1138 | goto err_exit; |
| 1139 | } |
| 1140 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 1141 | count = blk_rq_map_integrity_sg(rq->q, rq->bio, |
Martin K. Petersen | 7027ad7 | 2008-07-17 17:08:48 -0400 | [diff] [blame] | 1142 | prot_sdb->table.sgl); |
| 1143 | BUG_ON(unlikely(count > ivecs)); |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 1144 | BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q))); |
Martin K. Petersen | 7027ad7 | 2008-07-17 17:08:48 -0400 | [diff] [blame] | 1145 | |
| 1146 | cmd->prot_sdb = prot_sdb; |
| 1147 | cmd->prot_sdb->table.nents = count; |
| 1148 | } |
| 1149 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1150 | return BLKPREP_OK; |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 1151 | err_exit: |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1152 | if (is_mq) { |
| 1153 | scsi_mq_free_sgtables(cmd); |
| 1154 | } else { |
| 1155 | scsi_release_buffers(cmd); |
| 1156 | cmd->request->special = NULL; |
| 1157 | scsi_put_command(cmd); |
| 1158 | put_device(&sdev->sdev_gendev); |
| 1159 | } |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 1160 | return error; |
| 1161 | } |
Boaz Harrosh | bb52d82 | 2007-12-13 16:14:27 -0800 | [diff] [blame] | 1162 | EXPORT_SYMBOL(scsi_init_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1164 | static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev, |
| 1165 | struct request *req) |
| 1166 | { |
| 1167 | struct scsi_cmnd *cmd; |
| 1168 | |
| 1169 | if (!req->special) { |
Christoph Hellwig | 0479633 | 2014-02-20 14:20:55 -0800 | [diff] [blame] | 1170 | /* Bail if we can't get a reference to the device */ |
| 1171 | if (!get_device(&sdev->sdev_gendev)) |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1172 | return NULL; |
Christoph Hellwig | 0479633 | 2014-02-20 14:20:55 -0800 | [diff] [blame] | 1173 | |
| 1174 | cmd = scsi_get_command(sdev, GFP_ATOMIC); |
| 1175 | if (unlikely(!cmd)) { |
| 1176 | put_device(&sdev->sdev_gendev); |
| 1177 | return NULL; |
| 1178 | } |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1179 | req->special = cmd; |
| 1180 | } else { |
| 1181 | cmd = req->special; |
| 1182 | } |
| 1183 | |
| 1184 | /* pull a tag out of the request if we have one */ |
| 1185 | cmd->tag = req->tag; |
| 1186 | cmd->request = req; |
| 1187 | |
Boaz Harrosh | 64a87b2 | 2008-04-30 11:19:47 +0300 | [diff] [blame] | 1188 | cmd->cmnd = req->cmd; |
Martin K. Petersen | 72f7d32 | 2011-03-08 02:03:59 -0500 | [diff] [blame] | 1189 | cmd->prot_op = SCSI_PROT_NORMAL; |
Boaz Harrosh | 64a87b2 | 2008-04-30 11:19:47 +0300 | [diff] [blame] | 1190 | |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1191 | return cmd; |
| 1192 | } |
| 1193 | |
Christoph Hellwig | 4f1e576 | 2014-06-28 12:36:28 +0200 | [diff] [blame] | 1194 | static int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req) |
James Bottomley | 7b16318 | 2005-12-15 20:17:02 -0600 | [diff] [blame] | 1195 | { |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1196 | struct scsi_cmnd *cmd = req->special; |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1197 | |
| 1198 | /* |
| 1199 | * BLOCK_PC requests may transfer data, in which case they must |
| 1200 | * a bio attached to them. Or they might contain a SCSI command |
| 1201 | * that does not transfer data, in which case they may optionally |
| 1202 | * submit a request without an attached bio. |
| 1203 | */ |
| 1204 | if (req->bio) { |
Christoph Hellwig | 3c356bd | 2014-09-05 18:20:23 -0700 | [diff] [blame] | 1205 | int ret = scsi_init_io(cmd); |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1206 | if (unlikely(ret)) |
| 1207 | return ret; |
| 1208 | } else { |
Tejun Heo | b079041 | 2009-05-07 22:24:42 +0900 | [diff] [blame] | 1209 | BUG_ON(blk_rq_bytes(req)); |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1210 | |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 1211 | memset(&cmd->sdb, 0, sizeof(cmd->sdb)); |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1212 | } |
James Bottomley | 7b16318 | 2005-12-15 20:17:02 -0600 | [diff] [blame] | 1213 | |
James Bottomley | 7b16318 | 2005-12-15 20:17:02 -0600 | [diff] [blame] | 1214 | cmd->cmd_len = req->cmd_len; |
Tejun Heo | b079041 | 2009-05-07 22:24:42 +0900 | [diff] [blame] | 1215 | cmd->transfersize = blk_rq_bytes(req); |
James Bottomley | 7b16318 | 2005-12-15 20:17:02 -0600 | [diff] [blame] | 1216 | cmd->allowed = req->retries; |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1217 | return BLKPREP_OK; |
| 1218 | } |
| 1219 | |
| 1220 | /* |
Christoph Hellwig | 3868cf8 | 2014-06-28 11:58:42 +0200 | [diff] [blame] | 1221 | * Setup a REQ_TYPE_FS command. These are simple request from filesystems |
| 1222 | * that still need to be translated to SCSI CDBs from the ULD. |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1223 | */ |
Christoph Hellwig | 3868cf8 | 2014-06-28 11:58:42 +0200 | [diff] [blame] | 1224 | static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req) |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1225 | { |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1226 | struct scsi_cmnd *cmd = req->special; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 1227 | |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 1228 | if (unlikely(sdev->handler && sdev->handler->prep_fn)) { |
| 1229 | int ret = sdev->handler->prep_fn(sdev, req); |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 1230 | if (ret != BLKPREP_OK) |
| 1231 | return ret; |
| 1232 | } |
| 1233 | |
Boaz Harrosh | 64a87b2 | 2008-04-30 11:19:47 +0300 | [diff] [blame] | 1234 | memset(cmd->cmnd, 0, BLK_MAX_CDB); |
Christoph Hellwig | 3868cf8 | 2014-06-28 11:58:42 +0200 | [diff] [blame] | 1235 | return scsi_cmd_to_driver(cmd)->init_command(cmd); |
James Bottomley | 7b16318 | 2005-12-15 20:17:02 -0600 | [diff] [blame] | 1236 | } |
James Bottomley | 7b16318 | 2005-12-15 20:17:02 -0600 | [diff] [blame] | 1237 | |
Christoph Hellwig | 6af7a4f | 2014-07-08 13:16:17 +0200 | [diff] [blame] | 1238 | static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req) |
| 1239 | { |
| 1240 | struct scsi_cmnd *cmd = req->special; |
| 1241 | |
| 1242 | if (!blk_rq_bytes(req)) |
| 1243 | cmd->sc_data_direction = DMA_NONE; |
| 1244 | else if (rq_data_dir(req) == WRITE) |
| 1245 | cmd->sc_data_direction = DMA_TO_DEVICE; |
| 1246 | else |
| 1247 | cmd->sc_data_direction = DMA_FROM_DEVICE; |
| 1248 | |
| 1249 | switch (req->cmd_type) { |
| 1250 | case REQ_TYPE_FS: |
| 1251 | return scsi_setup_fs_cmnd(sdev, req); |
| 1252 | case REQ_TYPE_BLOCK_PC: |
| 1253 | return scsi_setup_blk_pc_cmnd(sdev, req); |
| 1254 | default: |
| 1255 | return BLKPREP_KILL; |
| 1256 | } |
| 1257 | } |
| 1258 | |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1259 | static int |
| 1260 | scsi_prep_state_check(struct scsi_device *sdev, struct request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | { |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1262 | int ret = BLKPREP_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | |
| 1264 | /* |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1265 | * If the device is not in running state we will reject some |
| 1266 | * or all commands. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | if (unlikely(sdev->sdev_state != SDEV_RUNNING)) { |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1269 | switch (sdev->sdev_state) { |
| 1270 | case SDEV_OFFLINE: |
Mike Christie | 1b8d262 | 2012-05-17 23:56:56 -0500 | [diff] [blame] | 1271 | case SDEV_TRANSPORT_OFFLINE: |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1272 | /* |
| 1273 | * If the device is offline we refuse to process any |
| 1274 | * commands. The device must be brought online |
| 1275 | * before trying any recovery commands. |
| 1276 | */ |
| 1277 | sdev_printk(KERN_ERR, sdev, |
| 1278 | "rejecting I/O to offline device\n"); |
| 1279 | ret = BLKPREP_KILL; |
| 1280 | break; |
| 1281 | case SDEV_DEL: |
| 1282 | /* |
| 1283 | * If the device is fully deleted, we refuse to |
| 1284 | * process any commands as well. |
| 1285 | */ |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 1286 | sdev_printk(KERN_ERR, sdev, |
| 1287 | "rejecting I/O to dead device\n"); |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1288 | ret = BLKPREP_KILL; |
| 1289 | break; |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1290 | case SDEV_BLOCK: |
James Bottomley | 6f4267e | 2008-08-22 16:53:31 -0500 | [diff] [blame] | 1291 | case SDEV_CREATED_BLOCK: |
Bart Van Assche | bba0bdd | 2015-03-04 10:31:47 +0100 | [diff] [blame] | 1292 | ret = BLKPREP_DEFER; |
| 1293 | break; |
| 1294 | case SDEV_QUIESCE: |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1295 | /* |
| 1296 | * If the devices is blocked we defer normal commands. |
| 1297 | */ |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 1298 | if (!(req->rq_flags & RQF_PREEMPT)) |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1299 | ret = BLKPREP_DEFER; |
| 1300 | break; |
| 1301 | default: |
| 1302 | /* |
| 1303 | * For any other not fully online state we only allow |
| 1304 | * special commands. In particular any user initiated |
| 1305 | * command is not allowed. |
| 1306 | */ |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 1307 | if (!(req->rq_flags & RQF_PREEMPT)) |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1308 | ret = BLKPREP_KILL; |
| 1309 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | } |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1312 | return ret; |
| 1313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1315 | static int |
| 1316 | scsi_prep_return(struct request_queue *q, struct request *req, int ret) |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1317 | { |
| 1318 | struct scsi_device *sdev = q->queuedata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1320 | switch (ret) { |
| 1321 | case BLKPREP_KILL: |
jiangyiwen | e1cd391 | 2016-02-16 20:14:13 +0800 | [diff] [blame] | 1322 | case BLKPREP_INVALID: |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1323 | req->errors = DID_NO_CONNECT << 16; |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1324 | /* release the command and kill it */ |
| 1325 | if (req->special) { |
| 1326 | struct scsi_cmnd *cmd = req->special; |
| 1327 | scsi_release_buffers(cmd); |
| 1328 | scsi_put_command(cmd); |
Christoph Hellwig | 68c03d9 | 2014-04-15 12:24:56 +0200 | [diff] [blame] | 1329 | put_device(&sdev->sdev_gendev); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1330 | req->special = NULL; |
| 1331 | } |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1332 | break; |
| 1333 | case BLKPREP_DEFER: |
| 1334 | /* |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 1335 | * If we defer, the blk_peek_request() returns NULL, but the |
Jens Axboe | a488e74 | 2010-04-16 21:13:15 +0200 | [diff] [blame] | 1336 | * queue must be restarted, so we schedule a callback to happen |
| 1337 | * shortly. |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1338 | */ |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1339 | if (atomic_read(&sdev->device_busy) == 0) |
Jens Axboe | a488e74 | 2010-04-16 21:13:15 +0200 | [diff] [blame] | 1340 | blk_delay_queue(q, SCSI_QUEUE_DELAY); |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1341 | break; |
| 1342 | default: |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 1343 | req->rq_flags |= RQF_DONTPREP; |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1344 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
Christoph Hellwig | 3b00315 | 2006-11-04 20:10:55 +0100 | [diff] [blame] | 1346 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | } |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1348 | |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1349 | static int scsi_prep_fn(struct request_queue *q, struct request *req) |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1350 | { |
| 1351 | struct scsi_device *sdev = q->queuedata; |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1352 | struct scsi_cmnd *cmd; |
| 1353 | int ret; |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1354 | |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1355 | ret = scsi_prep_state_check(sdev, req); |
| 1356 | if (ret != BLKPREP_OK) |
| 1357 | goto out; |
| 1358 | |
| 1359 | cmd = scsi_get_cmd_from_req(sdev, req); |
| 1360 | if (unlikely(!cmd)) { |
| 1361 | ret = BLKPREP_DEFER; |
| 1362 | goto out; |
| 1363 | } |
| 1364 | |
Christoph Hellwig | 6af7a4f | 2014-07-08 13:16:17 +0200 | [diff] [blame] | 1365 | ret = scsi_setup_cmnd(sdev, req); |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1366 | out: |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 1367 | return scsi_prep_return(q, req, ret); |
| 1368 | } |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1369 | |
| 1370 | static void scsi_unprep_fn(struct request_queue *q, struct request *req) |
| 1371 | { |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1372 | scsi_uninit_cmd(req->special); |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 1373 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | |
| 1375 | /* |
| 1376 | * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else |
| 1377 | * return 0. |
| 1378 | * |
| 1379 | * Called with the queue_lock held. |
| 1380 | */ |
| 1381 | static inline int scsi_dev_queue_ready(struct request_queue *q, |
| 1382 | struct scsi_device *sdev) |
| 1383 | { |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1384 | unsigned int busy; |
| 1385 | |
| 1386 | busy = atomic_inc_return(&sdev->device_busy) - 1; |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 1387 | if (atomic_read(&sdev->device_blocked)) { |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1388 | if (busy) |
| 1389 | goto out_dec; |
| 1390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | /* |
| 1392 | * unblock after device_blocked iterates to zero |
| 1393 | */ |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 1394 | if (atomic_dec_return(&sdev->device_blocked) > 0) { |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1395 | /* |
| 1396 | * For the MQ case we take care of this in the caller. |
| 1397 | */ |
| 1398 | if (!q->mq_ops) |
| 1399 | blk_delay_queue(q, SCSI_QUEUE_DELAY); |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1400 | goto out_dec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1402 | SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev, |
| 1403 | "unblocking device at zero depth\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | } |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1405 | |
| 1406 | if (busy >= sdev->queue_depth) |
| 1407 | goto out_dec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
| 1409 | return 1; |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1410 | out_dec: |
| 1411 | atomic_dec(&sdev->device_busy); |
| 1412 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1415 | /* |
| 1416 | * scsi_target_queue_ready: checks if there we can send commands to target |
| 1417 | * @sdev: scsi device on starget to check. |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1418 | */ |
| 1419 | static inline int scsi_target_queue_ready(struct Scsi_Host *shost, |
| 1420 | struct scsi_device *sdev) |
| 1421 | { |
| 1422 | struct scsi_target *starget = scsi_target(sdev); |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1423 | unsigned int busy; |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1424 | |
| 1425 | if (starget->single_lun) { |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1426 | spin_lock_irq(shost->host_lock); |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1427 | if (starget->starget_sdev_user && |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1428 | starget->starget_sdev_user != sdev) { |
| 1429 | spin_unlock_irq(shost->host_lock); |
| 1430 | return 0; |
| 1431 | } |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1432 | starget->starget_sdev_user = sdev; |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1433 | spin_unlock_irq(shost->host_lock); |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1434 | } |
| 1435 | |
Christoph Hellwig | 2ccbb00 | 2014-03-26 10:35:00 +0100 | [diff] [blame] | 1436 | if (starget->can_queue <= 0) |
| 1437 | return 1; |
| 1438 | |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1439 | busy = atomic_inc_return(&starget->target_busy) - 1; |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 1440 | if (atomic_read(&starget->target_blocked) > 0) { |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1441 | if (busy) |
| 1442 | goto starved; |
| 1443 | |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1444 | /* |
| 1445 | * unblock after target_blocked iterates to zero |
| 1446 | */ |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 1447 | if (atomic_dec_return(&starget->target_blocked) > 0) |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1448 | goto out_dec; |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1449 | |
| 1450 | SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget, |
| 1451 | "unblocking target at zero depth\n")); |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1452 | } |
| 1453 | |
Christoph Hellwig | 2ccbb00 | 2014-03-26 10:35:00 +0100 | [diff] [blame] | 1454 | if (busy >= starget->can_queue) |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1455 | goto starved; |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1456 | |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1457 | return 1; |
| 1458 | |
| 1459 | starved: |
| 1460 | spin_lock_irq(shost->host_lock); |
| 1461 | list_move_tail(&sdev->starved_entry, &shost->starved_list); |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1462 | spin_unlock_irq(shost->host_lock); |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1463 | out_dec: |
Christoph Hellwig | 2ccbb00 | 2014-03-26 10:35:00 +0100 | [diff] [blame] | 1464 | if (starget->can_queue > 0) |
| 1465 | atomic_dec(&starget->target_busy); |
Christoph Hellwig | 7ae65c0 | 2014-01-22 14:49:41 +0100 | [diff] [blame] | 1466 | return 0; |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1467 | } |
| 1468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | /* |
| 1470 | * scsi_host_queue_ready: if we can send requests to shost, return 1 else |
| 1471 | * return 0. We must end up running the queue again whenever 0 is |
| 1472 | * returned, else IO can hang. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | */ |
| 1474 | static inline int scsi_host_queue_ready(struct request_queue *q, |
| 1475 | struct Scsi_Host *shost, |
| 1476 | struct scsi_device *sdev) |
| 1477 | { |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 1478 | unsigned int busy; |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1479 | |
James Bottomley | 939647e | 2005-09-18 15:05:20 -0500 | [diff] [blame] | 1480 | if (scsi_host_in_recovery(shost)) |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 1481 | return 0; |
| 1482 | |
| 1483 | busy = atomic_inc_return(&shost->host_busy) - 1; |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 1484 | if (atomic_read(&shost->host_blocked) > 0) { |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 1485 | if (busy) |
| 1486 | goto starved; |
| 1487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | /* |
| 1489 | * unblock after host_blocked iterates to zero |
| 1490 | */ |
Christoph Hellwig | cd9070c | 2014-01-23 12:07:41 +0100 | [diff] [blame] | 1491 | if (atomic_dec_return(&shost->host_blocked) > 0) |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 1492 | goto out_dec; |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1493 | |
| 1494 | SCSI_LOG_MLQUEUE(3, |
| 1495 | shost_printk(KERN_INFO, shost, |
| 1496 | "unblocking host at zero depth\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 1498 | |
| 1499 | if (shost->can_queue > 0 && busy >= shost->can_queue) |
| 1500 | goto starved; |
| 1501 | if (shost->host_self_blocked) |
| 1502 | goto starved; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | |
| 1504 | /* We're OK to process the command, so we can't be starved */ |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 1505 | if (!list_empty(&sdev->starved_entry)) { |
| 1506 | spin_lock_irq(shost->host_lock); |
| 1507 | if (!list_empty(&sdev->starved_entry)) |
| 1508 | list_del_init(&sdev->starved_entry); |
| 1509 | spin_unlock_irq(shost->host_lock); |
| 1510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 1512 | return 1; |
| 1513 | |
| 1514 | starved: |
| 1515 | spin_lock_irq(shost->host_lock); |
| 1516 | if (list_empty(&sdev->starved_entry)) |
| 1517 | list_add_tail(&sdev->starved_entry, &shost->starved_list); |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1518 | spin_unlock_irq(shost->host_lock); |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 1519 | out_dec: |
| 1520 | atomic_dec(&shost->host_busy); |
| 1521 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | /* |
Kiyoshi Ueda | 6c5121b | 2008-10-04 14:11:35 -0400 | [diff] [blame] | 1525 | * Busy state exporting function for request stacking drivers. |
| 1526 | * |
| 1527 | * For efficiency, no lock is taken to check the busy state of |
| 1528 | * shost/starget/sdev, since the returned value is not guaranteed and |
| 1529 | * may be changed after request stacking drivers call the function, |
| 1530 | * regardless of taking lock or not. |
| 1531 | * |
Bart Van Assche | 67bd941 | 2012-06-29 15:33:22 +0000 | [diff] [blame] | 1532 | * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi |
| 1533 | * needs to return 'not busy'. Otherwise, request stacking drivers |
| 1534 | * may hold requests forever. |
Kiyoshi Ueda | 6c5121b | 2008-10-04 14:11:35 -0400 | [diff] [blame] | 1535 | */ |
| 1536 | static int scsi_lld_busy(struct request_queue *q) |
| 1537 | { |
| 1538 | struct scsi_device *sdev = q->queuedata; |
| 1539 | struct Scsi_Host *shost; |
Kiyoshi Ueda | 6c5121b | 2008-10-04 14:11:35 -0400 | [diff] [blame] | 1540 | |
Bart Van Assche | 3f3299d | 2012-11-28 13:42:38 +0100 | [diff] [blame] | 1541 | if (blk_queue_dying(q)) |
Kiyoshi Ueda | 6c5121b | 2008-10-04 14:11:35 -0400 | [diff] [blame] | 1542 | return 0; |
| 1543 | |
| 1544 | shost = sdev->host; |
Kiyoshi Ueda | 6c5121b | 2008-10-04 14:11:35 -0400 | [diff] [blame] | 1545 | |
Jun'ichi Nomura | b7e94a1 | 2012-05-22 18:57:17 +0900 | [diff] [blame] | 1546 | /* |
| 1547 | * Ignore host/starget busy state. |
| 1548 | * Since block layer does not have a concept of fairness across |
| 1549 | * multiple queues, congestion of host/starget needs to be handled |
| 1550 | * in SCSI layer. |
| 1551 | */ |
| 1552 | if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev)) |
Kiyoshi Ueda | 6c5121b | 2008-10-04 14:11:35 -0400 | [diff] [blame] | 1553 | return 1; |
| 1554 | |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | /* |
James Bottomley | e91442b | 2005-09-09 10:44:16 -0500 | [diff] [blame] | 1559 | * Kill a request for a dead device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1561 | static void scsi_kill_request(struct request *req, struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | { |
James Bottomley | e91442b | 2005-09-09 10:44:16 -0500 | [diff] [blame] | 1563 | struct scsi_cmnd *cmd = req->special; |
Jiri Slaby | 03b1470 | 2009-09-23 16:15:35 +0200 | [diff] [blame] | 1564 | struct scsi_device *sdev; |
| 1565 | struct scsi_target *starget; |
| 1566 | struct Scsi_Host *shost; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 1568 | blk_start_request(req); |
James Bottomley | 788ce43 | 2005-09-09 13:40:23 -0500 | [diff] [blame] | 1569 | |
Hannes Reinecke | 7457181 | 2011-11-09 08:39:24 +0100 | [diff] [blame] | 1570 | scmd_printk(KERN_INFO, cmd, "killing request\n"); |
| 1571 | |
Jiri Slaby | 03b1470 | 2009-09-23 16:15:35 +0200 | [diff] [blame] | 1572 | sdev = cmd->device; |
| 1573 | starget = scsi_target(sdev); |
| 1574 | shost = sdev->host; |
James Bottomley | e91442b | 2005-09-09 10:44:16 -0500 | [diff] [blame] | 1575 | scsi_init_cmd_errh(cmd); |
| 1576 | cmd->result = DID_NO_CONNECT << 16; |
| 1577 | atomic_inc(&cmd->device->iorequest_cnt); |
Tejun Heo | e36e0c8 | 2006-04-11 17:27:53 +0900 | [diff] [blame] | 1578 | |
| 1579 | /* |
| 1580 | * SCSI request completion path will do scsi_device_unbusy(), |
| 1581 | * bump busy counts. To bump the counters, we need to dance |
| 1582 | * with the locks as normal issue path does. |
| 1583 | */ |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1584 | atomic_inc(&sdev->device_busy); |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 1585 | atomic_inc(&shost->host_busy); |
Christoph Hellwig | 2ccbb00 | 2014-03-26 10:35:00 +0100 | [diff] [blame] | 1586 | if (starget->can_queue > 0) |
| 1587 | atomic_inc(&starget->target_busy); |
Tejun Heo | e36e0c8 | 2006-04-11 17:27:53 +0900 | [diff] [blame] | 1588 | |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1589 | blk_complete_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
Jens Axboe | 1aea643 | 2006-01-09 16:03:03 +0100 | [diff] [blame] | 1592 | static void scsi_softirq_done(struct request *rq) |
| 1593 | { |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1594 | struct scsi_cmnd *cmd = rq->special; |
| 1595 | unsigned long wait_for = (cmd->allowed + 1) * rq->timeout; |
Jens Axboe | 1aea643 | 2006-01-09 16:03:03 +0100 | [diff] [blame] | 1596 | int disposition; |
| 1597 | |
| 1598 | INIT_LIST_HEAD(&cmd->eh_entry); |
| 1599 | |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1600 | atomic_inc(&cmd->device->iodone_cnt); |
| 1601 | if (cmd->result) |
| 1602 | atomic_inc(&cmd->device->ioerr_cnt); |
| 1603 | |
Jens Axboe | 1aea643 | 2006-01-09 16:03:03 +0100 | [diff] [blame] | 1604 | disposition = scsi_decide_disposition(cmd); |
| 1605 | if (disposition != SUCCESS && |
| 1606 | time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) { |
| 1607 | sdev_printk(KERN_ERR, cmd->device, |
| 1608 | "timing out command, waited %lus\n", |
| 1609 | wait_for/HZ); |
| 1610 | disposition = SUCCESS; |
| 1611 | } |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1612 | |
Jens Axboe | 1aea643 | 2006-01-09 16:03:03 +0100 | [diff] [blame] | 1613 | scsi_log_completion(cmd, disposition); |
| 1614 | |
| 1615 | switch (disposition) { |
| 1616 | case SUCCESS: |
| 1617 | scsi_finish_command(cmd); |
| 1618 | break; |
| 1619 | case NEEDS_RETRY: |
Christoph Hellwig | 596f482 | 2007-01-02 12:56:00 +0100 | [diff] [blame] | 1620 | scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY); |
Jens Axboe | 1aea643 | 2006-01-09 16:03:03 +0100 | [diff] [blame] | 1621 | break; |
| 1622 | case ADD_TO_MLQUEUE: |
| 1623 | scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY); |
| 1624 | break; |
| 1625 | default: |
| 1626 | if (!scsi_eh_scmd_add(cmd, 0)) |
| 1627 | scsi_finish_command(cmd); |
| 1628 | } |
| 1629 | } |
| 1630 | |
Christoph Hellwig | 3b5382c | 2014-05-06 12:25:40 +0200 | [diff] [blame] | 1631 | /** |
Christoph Hellwig | 82042a2 | 2014-09-05 18:23:07 -0700 | [diff] [blame] | 1632 | * scsi_dispatch_command - Dispatch a command to the low-level driver. |
| 1633 | * @cmd: command block we are dispatching. |
| 1634 | * |
| 1635 | * Return: nonzero return request was rejected and device's queue needs to be |
| 1636 | * plugged. |
| 1637 | */ |
| 1638 | static int scsi_dispatch_cmd(struct scsi_cmnd *cmd) |
| 1639 | { |
| 1640 | struct Scsi_Host *host = cmd->device->host; |
| 1641 | int rtn = 0; |
| 1642 | |
| 1643 | atomic_inc(&cmd->device->iorequest_cnt); |
| 1644 | |
| 1645 | /* check if the device is still usable */ |
| 1646 | if (unlikely(cmd->device->sdev_state == SDEV_DEL)) { |
| 1647 | /* in SDEV_DEL we error all commands. DID_NO_CONNECT |
| 1648 | * returns an immediate error upwards, and signals |
| 1649 | * that the device is no longer present */ |
| 1650 | cmd->result = DID_NO_CONNECT << 16; |
| 1651 | goto done; |
| 1652 | } |
| 1653 | |
| 1654 | /* Check to see if the scsi lld made this device blocked. */ |
| 1655 | if (unlikely(scsi_device_blocked(cmd->device))) { |
| 1656 | /* |
| 1657 | * in blocked state, the command is just put back on |
| 1658 | * the device queue. The suspend state has already |
| 1659 | * blocked the queue so future requests should not |
| 1660 | * occur until the device transitions out of the |
| 1661 | * suspend state. |
| 1662 | */ |
| 1663 | SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd, |
| 1664 | "queuecommand : device blocked\n")); |
| 1665 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 1666 | } |
| 1667 | |
| 1668 | /* Store the LUN value in cmnd, if needed. */ |
| 1669 | if (cmd->device->lun_in_cdb) |
| 1670 | cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) | |
| 1671 | (cmd->device->lun << 5 & 0xe0); |
| 1672 | |
| 1673 | scsi_log_send(cmd); |
| 1674 | |
| 1675 | /* |
| 1676 | * Before we queue this command, check if the command |
| 1677 | * length exceeds what the host adapter can handle. |
| 1678 | */ |
| 1679 | if (cmd->cmd_len > cmd->device->host->max_cmd_len) { |
| 1680 | SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd, |
| 1681 | "queuecommand : command too long. " |
| 1682 | "cdb_size=%d host->max_cmd_len=%d\n", |
| 1683 | cmd->cmd_len, cmd->device->host->max_cmd_len)); |
| 1684 | cmd->result = (DID_ABORT << 16); |
| 1685 | goto done; |
| 1686 | } |
| 1687 | |
| 1688 | if (unlikely(host->shost_state == SHOST_DEL)) { |
| 1689 | cmd->result = (DID_NO_CONNECT << 16); |
| 1690 | goto done; |
| 1691 | |
| 1692 | } |
| 1693 | |
| 1694 | trace_scsi_dispatch_cmd_start(cmd); |
| 1695 | rtn = host->hostt->queuecommand(host, cmd); |
| 1696 | if (rtn) { |
| 1697 | trace_scsi_dispatch_cmd_error(cmd, rtn); |
| 1698 | if (rtn != SCSI_MLQUEUE_DEVICE_BUSY && |
| 1699 | rtn != SCSI_MLQUEUE_TARGET_BUSY) |
| 1700 | rtn = SCSI_MLQUEUE_HOST_BUSY; |
| 1701 | |
| 1702 | SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd, |
| 1703 | "queuecommand : request rejected\n")); |
| 1704 | } |
| 1705 | |
| 1706 | return rtn; |
| 1707 | done: |
| 1708 | cmd->scsi_done(cmd); |
| 1709 | return 0; |
| 1710 | } |
| 1711 | |
| 1712 | /** |
Christoph Hellwig | 3b5382c | 2014-05-06 12:25:40 +0200 | [diff] [blame] | 1713 | * scsi_done - Invoke completion on finished SCSI command. |
| 1714 | * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives |
| 1715 | * ownership back to SCSI Core -- i.e. the LLDD has finished with it. |
| 1716 | * |
| 1717 | * Description: This function is the mid-level's (SCSI Core) interrupt routine, |
| 1718 | * which regains ownership of the SCSI command (de facto) from a LLDD, and |
| 1719 | * calls blk_complete_request() for further processing. |
| 1720 | * |
| 1721 | * This function is interrupt context safe. |
| 1722 | */ |
| 1723 | static void scsi_done(struct scsi_cmnd *cmd) |
| 1724 | { |
| 1725 | trace_scsi_dispatch_cmd_done(cmd); |
| 1726 | blk_complete_request(cmd->request); |
| 1727 | } |
| 1728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | /* |
| 1730 | * Function: scsi_request_fn() |
| 1731 | * |
| 1732 | * Purpose: Main strategy routine for SCSI. |
| 1733 | * |
| 1734 | * Arguments: q - Pointer to actual queue. |
| 1735 | * |
| 1736 | * Returns: Nothing |
| 1737 | * |
| 1738 | * Lock status: IO request lock assumed to be held when called. |
| 1739 | */ |
| 1740 | static void scsi_request_fn(struct request_queue *q) |
Bart Van Assche | 613be1f | 2014-02-20 14:20:56 -0800 | [diff] [blame] | 1741 | __releases(q->queue_lock) |
| 1742 | __acquires(q->queue_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | { |
| 1744 | struct scsi_device *sdev = q->queuedata; |
| 1745 | struct Scsi_Host *shost; |
| 1746 | struct scsi_cmnd *cmd; |
| 1747 | struct request *req; |
| 1748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | /* |
| 1750 | * To start with, we keep looping until the queue is empty, or until |
| 1751 | * the host is no longer able to accept any more requests. |
| 1752 | */ |
| 1753 | shost = sdev->host; |
Jens Axboe | a488e74 | 2010-04-16 21:13:15 +0200 | [diff] [blame] | 1754 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | int rtn; |
| 1756 | /* |
| 1757 | * get next queueable request. We do this early to make sure |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1758 | * that the request is fully prepared even if we cannot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | * accept it. |
| 1760 | */ |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 1761 | req = blk_peek_request(q); |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1762 | if (!req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | break; |
| 1764 | |
| 1765 | if (unlikely(!scsi_device_online(sdev))) { |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 1766 | sdev_printk(KERN_ERR, sdev, |
| 1767 | "rejecting I/O to offline device\n"); |
James Bottomley | e91442b | 2005-09-09 10:44:16 -0500 | [diff] [blame] | 1768 | scsi_kill_request(req, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | continue; |
| 1770 | } |
| 1771 | |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1772 | if (!scsi_dev_queue_ready(q, sdev)) |
| 1773 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | |
| 1775 | /* |
| 1776 | * Remove the request from the request list. |
| 1777 | */ |
| 1778 | if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req))) |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 1779 | blk_start_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1781 | spin_unlock_irq(q->queue_lock); |
James Bottomley | e91442b | 2005-09-09 10:44:16 -0500 | [diff] [blame] | 1782 | cmd = req->special; |
| 1783 | if (unlikely(cmd == NULL)) { |
| 1784 | printk(KERN_CRIT "impossible request in %s.\n" |
| 1785 | "please mail a stack trace to " |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 1786 | "linux-scsi@vger.kernel.org\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 1787 | __func__); |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 1788 | blk_dump_rq_flags(req, "foo"); |
James Bottomley | e91442b | 2005-09-09 10:44:16 -0500 | [diff] [blame] | 1789 | BUG(); |
| 1790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | |
Mike Christie | ecefe8a | 2008-07-11 19:50:35 -0500 | [diff] [blame] | 1792 | /* |
| 1793 | * We hit this when the driver is using a host wide |
| 1794 | * tag map. For device level tag maps the queue_depth check |
| 1795 | * in the device ready fn would prevent us from trying |
| 1796 | * to allocate a tag. Since the map is a shared host resource |
| 1797 | * we add the dev to the starved list so it eventually gets |
| 1798 | * a run when a tag is freed. |
| 1799 | */ |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 1800 | if (blk_queue_tagged(q) && !(req->rq_flags & RQF_QUEUED)) { |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1801 | spin_lock_irq(shost->host_lock); |
Mike Christie | ecefe8a | 2008-07-11 19:50:35 -0500 | [diff] [blame] | 1802 | if (list_empty(&sdev->starved_entry)) |
| 1803 | list_add_tail(&sdev->starved_entry, |
| 1804 | &shost->starved_list); |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1805 | spin_unlock_irq(shost->host_lock); |
Mike Christie | ecefe8a | 2008-07-11 19:50:35 -0500 | [diff] [blame] | 1806 | goto not_ready; |
| 1807 | } |
| 1808 | |
Mike Christie | f0c0a37 | 2008-08-17 15:24:38 -0500 | [diff] [blame] | 1809 | if (!scsi_target_queue_ready(shost, sdev)) |
| 1810 | goto not_ready; |
| 1811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | if (!scsi_host_queue_ready(q, shost, sdev)) |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1813 | goto host_not_ready; |
Christoph Hellwig | 125c99b | 2014-11-03 12:47:47 +0100 | [diff] [blame] | 1814 | |
| 1815 | if (sdev->simple_tags) |
| 1816 | cmd->flags |= SCMD_TAGGED; |
| 1817 | else |
| 1818 | cmd->flags &= ~SCMD_TAGGED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | /* |
| 1821 | * Finally, initialize any error handling parameters, and set up |
| 1822 | * the timers for timeouts. |
| 1823 | */ |
| 1824 | scsi_init_cmd_errh(cmd); |
| 1825 | |
| 1826 | /* |
| 1827 | * Dispatch the command to the low-level driver. |
| 1828 | */ |
Christoph Hellwig | 3b5382c | 2014-05-06 12:25:40 +0200 | [diff] [blame] | 1829 | cmd->scsi_done = scsi_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | rtn = scsi_dispatch_cmd(cmd); |
Christoph Hellwig | d0d3bbf | 2014-01-22 18:39:04 +0100 | [diff] [blame] | 1831 | if (rtn) { |
| 1832 | scsi_queue_insert(cmd, rtn); |
| 1833 | spin_lock_irq(q->queue_lock); |
Jens Axboe | a488e74 | 2010-04-16 21:13:15 +0200 | [diff] [blame] | 1834 | goto out_delay; |
Christoph Hellwig | d0d3bbf | 2014-01-22 18:39:04 +0100 | [diff] [blame] | 1835 | } |
| 1836 | spin_lock_irq(q->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Bart Van Assche | 613be1f | 2014-02-20 14:20:56 -0800 | [diff] [blame] | 1839 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1841 | host_not_ready: |
Christoph Hellwig | 2ccbb00 | 2014-03-26 10:35:00 +0100 | [diff] [blame] | 1842 | if (scsi_target(sdev)->can_queue > 0) |
| 1843 | atomic_dec(&scsi_target(sdev)->target_busy); |
Christoph Hellwig | cf68d33 | 2014-01-22 14:36:32 +0100 | [diff] [blame] | 1844 | not_ready: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | /* |
| 1846 | * lock q, handle tag, requeue req, and decrement device_busy. We |
| 1847 | * must return with queue_lock held. |
| 1848 | * |
| 1849 | * Decrementing device_busy without checking it is OK, as all such |
| 1850 | * cases (host limits or settings) should run the queue at some |
| 1851 | * later time. |
| 1852 | */ |
| 1853 | spin_lock_irq(q->queue_lock); |
| 1854 | blk_requeue_request(q, req); |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 1855 | atomic_dec(&sdev->device_busy); |
Jens Axboe | a488e74 | 2010-04-16 21:13:15 +0200 | [diff] [blame] | 1856 | out_delay: |
Guenter Roeck | 480cadc | 2014-08-10 05:54:25 -0700 | [diff] [blame] | 1857 | if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev)) |
Jens Axboe | a488e74 | 2010-04-16 21:13:15 +0200 | [diff] [blame] | 1858 | blk_delay_queue(q, SCSI_QUEUE_DELAY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1861 | static inline int prep_to_mq(int ret) |
| 1862 | { |
| 1863 | switch (ret) { |
| 1864 | case BLKPREP_OK: |
Omar Sandoval | 2868f13 | 2016-11-15 11:11:59 -0800 | [diff] [blame] | 1865 | return BLK_MQ_RQ_QUEUE_OK; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1866 | case BLKPREP_DEFER: |
| 1867 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 1868 | default: |
| 1869 | return BLK_MQ_RQ_QUEUE_ERROR; |
| 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | static int scsi_mq_prep_fn(struct request *req) |
| 1874 | { |
| 1875 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); |
| 1876 | struct scsi_device *sdev = req->q->queuedata; |
| 1877 | struct Scsi_Host *shost = sdev->host; |
| 1878 | unsigned char *sense_buf = cmd->sense_buffer; |
| 1879 | struct scatterlist *sg; |
| 1880 | |
| 1881 | memset(cmd, 0, sizeof(struct scsi_cmnd)); |
| 1882 | |
| 1883 | req->special = cmd; |
| 1884 | |
| 1885 | cmd->request = req; |
| 1886 | cmd->device = sdev; |
| 1887 | cmd->sense_buffer = sense_buf; |
| 1888 | |
| 1889 | cmd->tag = req->tag; |
| 1890 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1891 | cmd->cmnd = req->cmd; |
| 1892 | cmd->prot_op = SCSI_PROT_NORMAL; |
| 1893 | |
| 1894 | INIT_LIST_HEAD(&cmd->list); |
| 1895 | INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler); |
| 1896 | cmd->jiffies_at_alloc = jiffies; |
| 1897 | |
Kashyap.Desai@avagotech.com | 64bdcbc | 2014-08-20 19:24:33 +0530 | [diff] [blame] | 1898 | if (shost->use_cmd_list) { |
| 1899 | spin_lock_irq(&sdev->list_lock); |
| 1900 | list_add_tail(&cmd->list, &sdev->cmd_list); |
| 1901 | spin_unlock_irq(&sdev->list_lock); |
| 1902 | } |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1903 | |
| 1904 | sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size; |
| 1905 | cmd->sdb.table.sgl = sg; |
| 1906 | |
| 1907 | if (scsi_host_get_prot(shost)) { |
| 1908 | cmd->prot_sdb = (void *)sg + |
Tony Battersby | 120bb3e | 2014-12-08 17:20:52 -0500 | [diff] [blame] | 1909 | min_t(unsigned int, |
Ming Lin | 65e8617 | 2016-04-04 14:48:10 -0700 | [diff] [blame] | 1910 | shost->sg_tablesize, SG_CHUNK_SIZE) * |
Tony Battersby | 120bb3e | 2014-12-08 17:20:52 -0500 | [diff] [blame] | 1911 | sizeof(struct scatterlist); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1912 | memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer)); |
| 1913 | |
| 1914 | cmd->prot_sdb->table.sgl = |
| 1915 | (struct scatterlist *)(cmd->prot_sdb + 1); |
| 1916 | } |
| 1917 | |
| 1918 | if (blk_bidi_rq(req)) { |
| 1919 | struct request *next_rq = req->next_rq; |
| 1920 | struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq); |
| 1921 | |
| 1922 | memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer)); |
| 1923 | bidi_sdb->table.sgl = |
| 1924 | (struct scatterlist *)(bidi_sdb + 1); |
| 1925 | |
| 1926 | next_rq->special = bidi_sdb; |
| 1927 | } |
| 1928 | |
Christoph Hellwig | fe05252 | 2014-09-22 15:59:31 +0200 | [diff] [blame] | 1929 | blk_mq_start_request(req); |
| 1930 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1931 | return scsi_setup_cmnd(sdev, req); |
| 1932 | } |
| 1933 | |
| 1934 | static void scsi_mq_done(struct scsi_cmnd *cmd) |
| 1935 | { |
| 1936 | trace_scsi_dispatch_cmd_done(cmd); |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 1937 | blk_mq_complete_request(cmd->request, cmd->request->errors); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1938 | } |
| 1939 | |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 1940 | static int scsi_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 1941 | const struct blk_mq_queue_data *bd) |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1942 | { |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 1943 | struct request *req = bd->rq; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1944 | struct request_queue *q = req->q; |
| 1945 | struct scsi_device *sdev = q->queuedata; |
| 1946 | struct Scsi_Host *shost = sdev->host; |
| 1947 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); |
| 1948 | int ret; |
| 1949 | int reason; |
| 1950 | |
| 1951 | ret = prep_to_mq(scsi_prep_state_check(sdev, req)); |
Omar Sandoval | 2868f13 | 2016-11-15 11:11:59 -0800 | [diff] [blame] | 1952 | if (ret != BLK_MQ_RQ_QUEUE_OK) |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1953 | goto out; |
| 1954 | |
| 1955 | ret = BLK_MQ_RQ_QUEUE_BUSY; |
| 1956 | if (!get_device(&sdev->sdev_gendev)) |
| 1957 | goto out; |
| 1958 | |
| 1959 | if (!scsi_dev_queue_ready(q, sdev)) |
| 1960 | goto out_put_device; |
| 1961 | if (!scsi_target_queue_ready(shost, sdev)) |
| 1962 | goto out_dec_device_busy; |
| 1963 | if (!scsi_host_queue_ready(q, shost, sdev)) |
| 1964 | goto out_dec_target_busy; |
| 1965 | |
Christoph Hellwig | fe05252 | 2014-09-22 15:59:31 +0200 | [diff] [blame] | 1966 | |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 1967 | if (!(req->rq_flags & RQF_DONTPREP)) { |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1968 | ret = prep_to_mq(scsi_mq_prep_fn(req)); |
Omar Sandoval | 2868f13 | 2016-11-15 11:11:59 -0800 | [diff] [blame] | 1969 | if (ret != BLK_MQ_RQ_QUEUE_OK) |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1970 | goto out_dec_host_busy; |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 1971 | req->rq_flags |= RQF_DONTPREP; |
Christoph Hellwig | fe05252 | 2014-09-22 15:59:31 +0200 | [diff] [blame] | 1972 | } else { |
| 1973 | blk_mq_start_request(req); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1974 | } |
| 1975 | |
Christoph Hellwig | 125c99b | 2014-11-03 12:47:47 +0100 | [diff] [blame] | 1976 | if (sdev->simple_tags) |
| 1977 | cmd->flags |= SCMD_TAGGED; |
Christoph Hellwig | b1dd2aa | 2014-10-19 17:13:58 +0200 | [diff] [blame] | 1978 | else |
Christoph Hellwig | 125c99b | 2014-11-03 12:47:47 +0100 | [diff] [blame] | 1979 | cmd->flags &= ~SCMD_TAGGED; |
Christoph Hellwig | b1dd2aa | 2014-10-19 17:13:58 +0200 | [diff] [blame] | 1980 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 1981 | scsi_init_cmd_errh(cmd); |
| 1982 | cmd->scsi_done = scsi_mq_done; |
| 1983 | |
| 1984 | reason = scsi_dispatch_cmd(cmd); |
| 1985 | if (reason) { |
| 1986 | scsi_set_blocked(cmd, reason); |
| 1987 | ret = BLK_MQ_RQ_QUEUE_BUSY; |
| 1988 | goto out_dec_host_busy; |
| 1989 | } |
| 1990 | |
| 1991 | return BLK_MQ_RQ_QUEUE_OK; |
| 1992 | |
| 1993 | out_dec_host_busy: |
| 1994 | atomic_dec(&shost->host_busy); |
| 1995 | out_dec_target_busy: |
| 1996 | if (scsi_target(sdev)->can_queue > 0) |
| 1997 | atomic_dec(&scsi_target(sdev)->target_busy); |
| 1998 | out_dec_device_busy: |
| 1999 | atomic_dec(&sdev->device_busy); |
| 2000 | out_put_device: |
| 2001 | put_device(&sdev->sdev_gendev); |
| 2002 | out: |
| 2003 | switch (ret) { |
| 2004 | case BLK_MQ_RQ_QUEUE_BUSY: |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2005 | if (atomic_read(&sdev->device_busy) == 0 && |
| 2006 | !scsi_device_blocked(sdev)) |
| 2007 | blk_mq_delay_queue(hctx, SCSI_QUEUE_DELAY); |
| 2008 | break; |
| 2009 | case BLK_MQ_RQ_QUEUE_ERROR: |
| 2010 | /* |
| 2011 | * Make sure to release all allocated ressources when |
| 2012 | * we hit an error, as we will never see this command |
| 2013 | * again. |
| 2014 | */ |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 2015 | if (req->rq_flags & RQF_DONTPREP) |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2016 | scsi_mq_uninit_cmd(cmd); |
| 2017 | break; |
| 2018 | default: |
| 2019 | break; |
| 2020 | } |
| 2021 | return ret; |
| 2022 | } |
| 2023 | |
Christoph Hellwig | 0152fb6 | 2014-09-13 16:40:13 -0700 | [diff] [blame] | 2024 | static enum blk_eh_timer_return scsi_timeout(struct request *req, |
| 2025 | bool reserved) |
| 2026 | { |
| 2027 | if (reserved) |
| 2028 | return BLK_EH_RESET_TIMER; |
| 2029 | return scsi_times_out(req); |
| 2030 | } |
| 2031 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2032 | static int scsi_init_request(void *data, struct request *rq, |
| 2033 | unsigned int hctx_idx, unsigned int request_idx, |
| 2034 | unsigned int numa_node) |
| 2035 | { |
Christoph Hellwig | 0a6ac4e | 2017-01-03 08:28:41 +0300 | [diff] [blame^] | 2036 | struct Scsi_Host *shost = data; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2037 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
| 2038 | |
Christoph Hellwig | 0a6ac4e | 2017-01-03 08:28:41 +0300 | [diff] [blame^] | 2039 | cmd->sense_buffer = |
| 2040 | scsi_alloc_sense_buffer(shost, GFP_KERNEL, numa_node); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2041 | if (!cmd->sense_buffer) |
| 2042 | return -ENOMEM; |
| 2043 | return 0; |
| 2044 | } |
| 2045 | |
| 2046 | static void scsi_exit_request(void *data, struct request *rq, |
| 2047 | unsigned int hctx_idx, unsigned int request_idx) |
| 2048 | { |
Christoph Hellwig | 0a6ac4e | 2017-01-03 08:28:41 +0300 | [diff] [blame^] | 2049 | struct Scsi_Host *shost = data; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2050 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
| 2051 | |
Christoph Hellwig | 0a6ac4e | 2017-01-03 08:28:41 +0300 | [diff] [blame^] | 2052 | scsi_free_sense_buffer(shost, cmd->sense_buffer); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
Christoph Hellwig | 2d9c5c2 | 2016-11-01 08:12:48 -0600 | [diff] [blame] | 2055 | static int scsi_map_queues(struct blk_mq_tag_set *set) |
| 2056 | { |
| 2057 | struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set); |
| 2058 | |
| 2059 | if (shost->hostt->map_queues) |
| 2060 | return shost->hostt->map_queues(shost); |
| 2061 | return blk_mq_map_queues(set); |
| 2062 | } |
| 2063 | |
Christoph Hellwig | f1bea55 | 2014-04-15 12:26:54 +0200 | [diff] [blame] | 2064 | static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | { |
| 2066 | struct device *host_dev; |
| 2067 | u64 bounce_limit = 0xffffffff; |
| 2068 | |
| 2069 | if (shost->unchecked_isa_dma) |
| 2070 | return BLK_BOUNCE_ISA; |
| 2071 | /* |
| 2072 | * Platforms with virtual-DMA translation |
| 2073 | * hardware have no practical limit. |
| 2074 | */ |
| 2075 | if (!PCI_DMA_BUS_IS_PHYS) |
| 2076 | return BLK_BOUNCE_ANY; |
| 2077 | |
| 2078 | host_dev = scsi_get_device(shost); |
| 2079 | if (host_dev && host_dev->dma_mask) |
Russell King | e83b366 | 2014-02-11 17:11:04 +0000 | [diff] [blame] | 2080 | bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | |
| 2082 | return bounce_limit; |
| 2083 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2085 | static void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | { |
Lin Ming | 6f381fa | 2012-04-12 13:50:38 +0800 | [diff] [blame] | 2087 | struct device *dev = shost->dma_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
Jens Axboe | a8474ce | 2007-08-07 09:02:51 +0200 | [diff] [blame] | 2089 | /* |
| 2090 | * this limit is imposed by hardware restrictions |
| 2091 | */ |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 2092 | blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize, |
Ming Lin | 65e8617 | 2016-04-04 14:48:10 -0700 | [diff] [blame] | 2093 | SG_MAX_SEGMENTS)); |
Jens Axboe | a8474ce | 2007-08-07 09:02:51 +0200 | [diff] [blame] | 2094 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 2095 | if (scsi_host_prot_dma(shost)) { |
| 2096 | shost->sg_prot_tablesize = |
| 2097 | min_not_zero(shost->sg_prot_tablesize, |
| 2098 | (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS); |
| 2099 | BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize); |
| 2100 | blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize); |
| 2101 | } |
| 2102 | |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 2103 | blk_queue_max_hw_sectors(q, shost->max_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost)); |
| 2105 | blk_queue_segment_boundary(q, shost->dma_boundary); |
FUJITA Tomonori | 99c84db | 2008-02-04 22:28:17 -0800 | [diff] [blame] | 2106 | dma_set_seg_boundary(dev, shost->dma_boundary); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | |
FUJITA Tomonori | 860ac56 | 2008-02-04 22:28:05 -0800 | [diff] [blame] | 2108 | blk_queue_max_segment_size(q, dma_get_max_seg_size(dev)); |
| 2109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | if (!shost->use_clustering) |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 2111 | q->limits.cluster = 0; |
James Bottomley | 465ff31 | 2008-01-01 10:00:10 -0600 | [diff] [blame] | 2112 | |
| 2113 | /* |
| 2114 | * set a reasonable default alignment on word boundaries: the |
| 2115 | * host and device may alter it using |
| 2116 | * blk_queue_update_dma_alignment() later. |
| 2117 | */ |
| 2118 | blk_queue_dma_alignment(q, 0x03); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2119 | } |
James Bottomley | 465ff31 | 2008-01-01 10:00:10 -0600 | [diff] [blame] | 2120 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2121 | struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost, |
| 2122 | request_fn_proc *request_fn) |
| 2123 | { |
| 2124 | struct request_queue *q; |
| 2125 | |
| 2126 | q = blk_init_queue(request_fn, NULL); |
| 2127 | if (!q) |
| 2128 | return NULL; |
| 2129 | __scsi_init_queue(shost, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | return q; |
| 2131 | } |
FUJITA Tomonori | b58d915 | 2006-11-16 19:24:10 +0900 | [diff] [blame] | 2132 | EXPORT_SYMBOL(__scsi_alloc_queue); |
| 2133 | |
| 2134 | struct request_queue *scsi_alloc_queue(struct scsi_device *sdev) |
| 2135 | { |
| 2136 | struct request_queue *q; |
| 2137 | |
| 2138 | q = __scsi_alloc_queue(sdev->host, scsi_request_fn); |
| 2139 | if (!q) |
| 2140 | return NULL; |
| 2141 | |
| 2142 | blk_queue_prep_rq(q, scsi_prep_fn); |
Christoph Hellwig | a1b73fc | 2014-05-01 16:51:04 +0200 | [diff] [blame] | 2143 | blk_queue_unprep_rq(q, scsi_unprep_fn); |
FUJITA Tomonori | b58d915 | 2006-11-16 19:24:10 +0900 | [diff] [blame] | 2144 | blk_queue_softirq_done(q, scsi_softirq_done); |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 2145 | blk_queue_rq_timed_out(q, scsi_times_out); |
Kiyoshi Ueda | 6c5121b | 2008-10-04 14:11:35 -0400 | [diff] [blame] | 2146 | blk_queue_lld_busy(q, scsi_lld_busy); |
FUJITA Tomonori | b58d915 | 2006-11-16 19:24:10 +0900 | [diff] [blame] | 2147 | return q; |
| 2148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2150 | static struct blk_mq_ops scsi_mq_ops = { |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2151 | .queue_rq = scsi_queue_rq, |
| 2152 | .complete = scsi_softirq_done, |
Christoph Hellwig | 0152fb6 | 2014-09-13 16:40:13 -0700 | [diff] [blame] | 2153 | .timeout = scsi_timeout, |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2154 | .init_request = scsi_init_request, |
| 2155 | .exit_request = scsi_exit_request, |
Christoph Hellwig | 2d9c5c2 | 2016-11-01 08:12:48 -0600 | [diff] [blame] | 2156 | .map_queues = scsi_map_queues, |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2157 | }; |
| 2158 | |
| 2159 | struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev) |
| 2160 | { |
| 2161 | sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set); |
| 2162 | if (IS_ERR(sdev->request_queue)) |
| 2163 | return NULL; |
| 2164 | |
| 2165 | sdev->request_queue->queuedata = sdev; |
| 2166 | __scsi_init_queue(sdev->host, sdev->request_queue); |
| 2167 | return sdev->request_queue; |
| 2168 | } |
| 2169 | |
| 2170 | int scsi_mq_setup_tags(struct Scsi_Host *shost) |
| 2171 | { |
| 2172 | unsigned int cmd_size, sgl_size, tbl_size; |
| 2173 | |
| 2174 | tbl_size = shost->sg_tablesize; |
Ming Lin | 65e8617 | 2016-04-04 14:48:10 -0700 | [diff] [blame] | 2175 | if (tbl_size > SG_CHUNK_SIZE) |
| 2176 | tbl_size = SG_CHUNK_SIZE; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2177 | sgl_size = tbl_size * sizeof(struct scatterlist); |
| 2178 | cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size; |
| 2179 | if (scsi_host_get_prot(shost)) |
| 2180 | cmd_size += sizeof(struct scsi_data_buffer) + sgl_size; |
| 2181 | |
| 2182 | memset(&shost->tag_set, 0, sizeof(shost->tag_set)); |
| 2183 | shost->tag_set.ops = &scsi_mq_ops; |
Bart Van Assche | efec4b9 | 2014-10-30 14:45:36 +0100 | [diff] [blame] | 2184 | shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1; |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2185 | shost->tag_set.queue_depth = shost->can_queue; |
| 2186 | shost->tag_set.cmd_size = cmd_size; |
| 2187 | shost->tag_set.numa_node = NUMA_NO_NODE; |
| 2188 | shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; |
Shaohua Li | 24391c0 | 2015-01-23 14:18:00 -0700 | [diff] [blame] | 2189 | shost->tag_set.flags |= |
| 2190 | BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2191 | shost->tag_set.driver_data = shost; |
| 2192 | |
| 2193 | return blk_mq_alloc_tag_set(&shost->tag_set); |
| 2194 | } |
| 2195 | |
| 2196 | void scsi_mq_destroy_tags(struct Scsi_Host *shost) |
| 2197 | { |
| 2198 | blk_mq_free_tag_set(&shost->tag_set); |
| 2199 | } |
| 2200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | /* |
| 2202 | * Function: scsi_block_requests() |
| 2203 | * |
| 2204 | * Purpose: Utility function used by low-level drivers to prevent further |
| 2205 | * commands from being queued to the device. |
| 2206 | * |
| 2207 | * Arguments: shost - Host in question |
| 2208 | * |
| 2209 | * Returns: Nothing |
| 2210 | * |
| 2211 | * Lock status: No locks are assumed held. |
| 2212 | * |
| 2213 | * Notes: There is no timer nor any other means by which the requests |
| 2214 | * get unblocked other than the low-level driver calling |
| 2215 | * scsi_unblock_requests(). |
| 2216 | */ |
| 2217 | void scsi_block_requests(struct Scsi_Host *shost) |
| 2218 | { |
| 2219 | shost->host_self_blocked = 1; |
| 2220 | } |
| 2221 | EXPORT_SYMBOL(scsi_block_requests); |
| 2222 | |
| 2223 | /* |
| 2224 | * Function: scsi_unblock_requests() |
| 2225 | * |
| 2226 | * Purpose: Utility function used by low-level drivers to allow further |
| 2227 | * commands from being queued to the device. |
| 2228 | * |
| 2229 | * Arguments: shost - Host in question |
| 2230 | * |
| 2231 | * Returns: Nothing |
| 2232 | * |
| 2233 | * Lock status: No locks are assumed held. |
| 2234 | * |
| 2235 | * Notes: There is no timer nor any other means by which the requests |
| 2236 | * get unblocked other than the low-level driver calling |
| 2237 | * scsi_unblock_requests(). |
| 2238 | * |
| 2239 | * This is done as an API function so that changes to the |
| 2240 | * internals of the scsi mid-layer won't require wholesale |
| 2241 | * changes to drivers that use this feature. |
| 2242 | */ |
| 2243 | void scsi_unblock_requests(struct Scsi_Host *shost) |
| 2244 | { |
| 2245 | shost->host_self_blocked = 0; |
| 2246 | scsi_run_host_queues(shost); |
| 2247 | } |
| 2248 | EXPORT_SYMBOL(scsi_unblock_requests); |
| 2249 | |
| 2250 | int __init scsi_init_queue(void) |
| 2251 | { |
Martin K. Petersen | 6362abd | 2008-06-05 23:30:03 -0400 | [diff] [blame] | 2252 | scsi_sdb_cache = kmem_cache_create("scsi_data_buffer", |
| 2253 | sizeof(struct scsi_data_buffer), |
| 2254 | 0, 0, NULL); |
| 2255 | if (!scsi_sdb_cache) { |
| 2256 | printk(KERN_ERR "SCSI: can't init scsi sdb cache\n"); |
FUJITA Tomonori | f078727 | 2008-12-14 01:23:45 +0900 | [diff] [blame] | 2257 | return -ENOMEM; |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 2258 | } |
| 2259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | return 0; |
| 2261 | } |
| 2262 | |
| 2263 | void scsi_exit_queue(void) |
| 2264 | { |
Christoph Hellwig | 0a6ac4e | 2017-01-03 08:28:41 +0300 | [diff] [blame^] | 2265 | kmem_cache_destroy(scsi_sense_cache); |
| 2266 | kmem_cache_destroy(scsi_sense_isadma_cache); |
Martin K. Petersen | 6362abd | 2008-06-05 23:30:03 -0400 | [diff] [blame] | 2267 | kmem_cache_destroy(scsi_sdb_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | } |
James Bottomley | 5baba83 | 2006-03-18 14:10:35 -0600 | [diff] [blame] | 2269 | |
| 2270 | /** |
| 2271 | * scsi_mode_select - issue a mode select |
| 2272 | * @sdev: SCSI device to be queried |
| 2273 | * @pf: Page format bit (1 == standard, 0 == vendor specific) |
| 2274 | * @sp: Save page bit (0 == don't save, 1 == save) |
| 2275 | * @modepage: mode page being requested |
| 2276 | * @buffer: request buffer (may not be smaller than eight bytes) |
| 2277 | * @len: length of request buffer. |
| 2278 | * @timeout: command timeout |
| 2279 | * @retries: number of retries before failing |
| 2280 | * @data: returns a structure abstracting the mode header data |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2281 | * @sshdr: place to put sense data (or NULL if no sense to be collected). |
James Bottomley | 5baba83 | 2006-03-18 14:10:35 -0600 | [diff] [blame] | 2282 | * must be SCSI_SENSE_BUFFERSIZE big. |
| 2283 | * |
| 2284 | * Returns zero if successful; negative error number or scsi |
| 2285 | * status on error |
| 2286 | * |
| 2287 | */ |
| 2288 | int |
| 2289 | scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage, |
| 2290 | unsigned char *buffer, int len, int timeout, int retries, |
| 2291 | struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) |
| 2292 | { |
| 2293 | unsigned char cmd[10]; |
| 2294 | unsigned char *real_buffer; |
| 2295 | int ret; |
| 2296 | |
| 2297 | memset(cmd, 0, sizeof(cmd)); |
| 2298 | cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0); |
| 2299 | |
| 2300 | if (sdev->use_10_for_ms) { |
| 2301 | if (len > 65535) |
| 2302 | return -EINVAL; |
| 2303 | real_buffer = kmalloc(8 + len, GFP_KERNEL); |
| 2304 | if (!real_buffer) |
| 2305 | return -ENOMEM; |
| 2306 | memcpy(real_buffer + 8, buffer, len); |
| 2307 | len += 8; |
| 2308 | real_buffer[0] = 0; |
| 2309 | real_buffer[1] = 0; |
| 2310 | real_buffer[2] = data->medium_type; |
| 2311 | real_buffer[3] = data->device_specific; |
| 2312 | real_buffer[4] = data->longlba ? 0x01 : 0; |
| 2313 | real_buffer[5] = 0; |
| 2314 | real_buffer[6] = data->block_descriptor_length >> 8; |
| 2315 | real_buffer[7] = data->block_descriptor_length; |
| 2316 | |
| 2317 | cmd[0] = MODE_SELECT_10; |
| 2318 | cmd[7] = len >> 8; |
| 2319 | cmd[8] = len; |
| 2320 | } else { |
| 2321 | if (len > 255 || data->block_descriptor_length > 255 || |
| 2322 | data->longlba) |
| 2323 | return -EINVAL; |
| 2324 | |
| 2325 | real_buffer = kmalloc(4 + len, GFP_KERNEL); |
| 2326 | if (!real_buffer) |
| 2327 | return -ENOMEM; |
| 2328 | memcpy(real_buffer + 4, buffer, len); |
| 2329 | len += 4; |
| 2330 | real_buffer[0] = 0; |
| 2331 | real_buffer[1] = data->medium_type; |
| 2332 | real_buffer[2] = data->device_specific; |
| 2333 | real_buffer[3] = data->block_descriptor_length; |
| 2334 | |
| 2335 | |
| 2336 | cmd[0] = MODE_SELECT; |
| 2337 | cmd[4] = len; |
| 2338 | } |
| 2339 | |
| 2340 | ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 2341 | sshdr, timeout, retries, NULL); |
James Bottomley | 5baba83 | 2006-03-18 14:10:35 -0600 | [diff] [blame] | 2342 | kfree(real_buffer); |
| 2343 | return ret; |
| 2344 | } |
| 2345 | EXPORT_SYMBOL_GPL(scsi_mode_select); |
| 2346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2348 | * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary. |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 2349 | * @sdev: SCSI device to be queried |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | * @dbd: set if mode sense will allow block descriptors to be returned |
| 2351 | * @modepage: mode page being requested |
| 2352 | * @buffer: request buffer (may not be smaller than eight bytes) |
| 2353 | * @len: length of request buffer. |
| 2354 | * @timeout: command timeout |
| 2355 | * @retries: number of retries before failing |
| 2356 | * @data: returns a structure abstracting the mode header data |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2357 | * @sshdr: place to put sense data (or NULL if no sense to be collected). |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 2358 | * must be SCSI_SENSE_BUFFERSIZE big. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | * |
| 2360 | * Returns zero if unsuccessful, or the header offset (either 4 |
| 2361 | * or 8 depending on whether a six or ten byte command was |
| 2362 | * issued) if successful. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2363 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | int |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 2365 | scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | unsigned char *buffer, int len, int timeout, int retries, |
James Bottomley | 5baba83 | 2006-03-18 14:10:35 -0600 | [diff] [blame] | 2367 | struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) |
| 2368 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | unsigned char cmd[12]; |
| 2370 | int use_10_for_ms; |
| 2371 | int header_length; |
Hannes Reinecke | 0ae80ba | 2015-06-12 16:12:48 +0200 | [diff] [blame] | 2372 | int result, retry_count = retries; |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2373 | struct scsi_sense_hdr my_sshdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | |
| 2375 | memset(data, 0, sizeof(*data)); |
| 2376 | memset(&cmd[0], 0, 12); |
| 2377 | cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */ |
| 2378 | cmd[2] = modepage; |
| 2379 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2380 | /* caller might not be interested in sense, but we need it */ |
| 2381 | if (!sshdr) |
| 2382 | sshdr = &my_sshdr; |
| 2383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | retry: |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 2385 | use_10_for_ms = sdev->use_10_for_ms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
| 2387 | if (use_10_for_ms) { |
| 2388 | if (len < 8) |
| 2389 | len = 8; |
| 2390 | |
| 2391 | cmd[0] = MODE_SENSE_10; |
| 2392 | cmd[8] = len; |
| 2393 | header_length = 8; |
| 2394 | } else { |
| 2395 | if (len < 4) |
| 2396 | len = 4; |
| 2397 | |
| 2398 | cmd[0] = MODE_SENSE; |
| 2399 | cmd[4] = len; |
| 2400 | header_length = 4; |
| 2401 | } |
| 2402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | memset(buffer, 0, len); |
| 2404 | |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 2405 | result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 2406 | sshdr, timeout, retries, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | |
| 2408 | /* This code looks awful: what it's doing is making sure an |
| 2409 | * ILLEGAL REQUEST sense return identifies the actual command |
| 2410 | * byte as the problem. MODE_SENSE commands can return |
| 2411 | * ILLEGAL REQUEST if the code page isn't supported */ |
| 2412 | |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 2413 | if (use_10_for_ms && !scsi_status_is_good(result) && |
| 2414 | (driver_byte(result) & DRIVER_SENSE)) { |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2415 | if (scsi_sense_valid(sshdr)) { |
| 2416 | if ((sshdr->sense_key == ILLEGAL_REQUEST) && |
| 2417 | (sshdr->asc == 0x20) && (sshdr->ascq == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | /* |
| 2419 | * Invalid command operation code |
| 2420 | */ |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 2421 | sdev->use_10_for_ms = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | goto retry; |
| 2423 | } |
| 2424 | } |
| 2425 | } |
| 2426 | |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 2427 | if(scsi_status_is_good(result)) { |
Al Viro | 6d73c85 | 2006-02-23 02:03:16 +0100 | [diff] [blame] | 2428 | if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b && |
| 2429 | (modepage == 6 || modepage == 8))) { |
| 2430 | /* Initio breakage? */ |
| 2431 | header_length = 0; |
| 2432 | data->length = 13; |
| 2433 | data->medium_type = 0; |
| 2434 | data->device_specific = 0; |
| 2435 | data->longlba = 0; |
| 2436 | data->block_descriptor_length = 0; |
| 2437 | } else if(use_10_for_ms) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | data->length = buffer[0]*256 + buffer[1] + 2; |
| 2439 | data->medium_type = buffer[2]; |
| 2440 | data->device_specific = buffer[3]; |
| 2441 | data->longlba = buffer[4] & 0x01; |
| 2442 | data->block_descriptor_length = buffer[6]*256 |
| 2443 | + buffer[7]; |
| 2444 | } else { |
| 2445 | data->length = buffer[0] + 1; |
| 2446 | data->medium_type = buffer[1]; |
| 2447 | data->device_specific = buffer[2]; |
| 2448 | data->block_descriptor_length = buffer[3]; |
| 2449 | } |
Al Viro | 6d73c85 | 2006-02-23 02:03:16 +0100 | [diff] [blame] | 2450 | data->header_length = header_length; |
Hannes Reinecke | 0ae80ba | 2015-06-12 16:12:48 +0200 | [diff] [blame] | 2451 | } else if ((status_byte(result) == CHECK_CONDITION) && |
| 2452 | scsi_sense_valid(sshdr) && |
| 2453 | sshdr->sense_key == UNIT_ATTENTION && retry_count) { |
| 2454 | retry_count--; |
| 2455 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 2458 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | } |
| 2460 | EXPORT_SYMBOL(scsi_mode_sense); |
| 2461 | |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 2462 | /** |
| 2463 | * scsi_test_unit_ready - test if unit is ready |
| 2464 | * @sdev: scsi device to change the state of. |
| 2465 | * @timeout: command timeout |
| 2466 | * @retries: number of retries before failing |
| 2467 | * @sshdr_external: Optional pointer to struct scsi_sense_hdr for |
| 2468 | * returning sense. Make sure that this is cleared before passing |
| 2469 | * in. |
| 2470 | * |
| 2471 | * Returns zero if unsuccessful or an error if TUR failed. For |
Tejun Heo | 9f8a2c2 | 2010-12-08 20:57:40 +0100 | [diff] [blame] | 2472 | * removable media, UNIT_ATTENTION sets ->changed flag. |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 2473 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | int |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 2475 | scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries, |
| 2476 | struct scsi_sense_hdr *sshdr_external) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | char cmd[] = { |
| 2479 | TEST_UNIT_READY, 0, 0, 0, 0, 0, |
| 2480 | }; |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 2481 | struct scsi_sense_hdr *sshdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | int result; |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 2483 | |
| 2484 | if (!sshdr_external) |
| 2485 | sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL); |
| 2486 | else |
| 2487 | sshdr = sshdr_external; |
| 2488 | |
| 2489 | /* try to eat the UNIT_ATTENTION if there are enough retries */ |
| 2490 | do { |
| 2491 | result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 2492 | timeout, retries, NULL); |
James Bottomley | 32c356d | 2008-08-20 00:58:13 +0200 | [diff] [blame] | 2493 | if (sdev->removable && scsi_sense_valid(sshdr) && |
| 2494 | sshdr->sense_key == UNIT_ATTENTION) |
| 2495 | sdev->changed = 1; |
| 2496 | } while (scsi_sense_valid(sshdr) && |
| 2497 | sshdr->sense_key == UNIT_ATTENTION && --retries); |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 2498 | |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 2499 | if (!sshdr_external) |
| 2500 | kfree(sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | return result; |
| 2502 | } |
| 2503 | EXPORT_SYMBOL(scsi_test_unit_ready); |
| 2504 | |
| 2505 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2506 | * scsi_device_set_state - Take the given device through the device state model. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | * @sdev: scsi device to change the state of. |
| 2508 | * @state: state to change to. |
| 2509 | * |
| 2510 | * Returns zero if unsuccessful or an error if the requested |
| 2511 | * transition is illegal. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2512 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | int |
| 2514 | scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state) |
| 2515 | { |
| 2516 | enum scsi_device_state oldstate = sdev->sdev_state; |
| 2517 | |
| 2518 | if (state == oldstate) |
| 2519 | return 0; |
| 2520 | |
| 2521 | switch (state) { |
| 2522 | case SDEV_CREATED: |
James Bottomley | 6f4267e | 2008-08-22 16:53:31 -0500 | [diff] [blame] | 2523 | switch (oldstate) { |
| 2524 | case SDEV_CREATED_BLOCK: |
| 2525 | break; |
| 2526 | default: |
| 2527 | goto illegal; |
| 2528 | } |
| 2529 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | |
| 2531 | case SDEV_RUNNING: |
| 2532 | switch (oldstate) { |
| 2533 | case SDEV_CREATED: |
| 2534 | case SDEV_OFFLINE: |
Mike Christie | 1b8d262 | 2012-05-17 23:56:56 -0500 | [diff] [blame] | 2535 | case SDEV_TRANSPORT_OFFLINE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | case SDEV_QUIESCE: |
| 2537 | case SDEV_BLOCK: |
| 2538 | break; |
| 2539 | default: |
| 2540 | goto illegal; |
| 2541 | } |
| 2542 | break; |
| 2543 | |
| 2544 | case SDEV_QUIESCE: |
| 2545 | switch (oldstate) { |
| 2546 | case SDEV_RUNNING: |
| 2547 | case SDEV_OFFLINE: |
Mike Christie | 1b8d262 | 2012-05-17 23:56:56 -0500 | [diff] [blame] | 2548 | case SDEV_TRANSPORT_OFFLINE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | break; |
| 2550 | default: |
| 2551 | goto illegal; |
| 2552 | } |
| 2553 | break; |
| 2554 | |
| 2555 | case SDEV_OFFLINE: |
Mike Christie | 1b8d262 | 2012-05-17 23:56:56 -0500 | [diff] [blame] | 2556 | case SDEV_TRANSPORT_OFFLINE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | switch (oldstate) { |
| 2558 | case SDEV_CREATED: |
| 2559 | case SDEV_RUNNING: |
| 2560 | case SDEV_QUIESCE: |
| 2561 | case SDEV_BLOCK: |
| 2562 | break; |
| 2563 | default: |
| 2564 | goto illegal; |
| 2565 | } |
| 2566 | break; |
| 2567 | |
| 2568 | case SDEV_BLOCK: |
| 2569 | switch (oldstate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | case SDEV_RUNNING: |
James Bottomley | 6f4267e | 2008-08-22 16:53:31 -0500 | [diff] [blame] | 2571 | case SDEV_CREATED_BLOCK: |
| 2572 | break; |
| 2573 | default: |
| 2574 | goto illegal; |
| 2575 | } |
| 2576 | break; |
| 2577 | |
| 2578 | case SDEV_CREATED_BLOCK: |
| 2579 | switch (oldstate) { |
| 2580 | case SDEV_CREATED: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | break; |
| 2582 | default: |
| 2583 | goto illegal; |
| 2584 | } |
| 2585 | break; |
| 2586 | |
| 2587 | case SDEV_CANCEL: |
| 2588 | switch (oldstate) { |
| 2589 | case SDEV_CREATED: |
| 2590 | case SDEV_RUNNING: |
Alan Stern | 9ea7290 | 2006-06-23 14:25:34 -0400 | [diff] [blame] | 2591 | case SDEV_QUIESCE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | case SDEV_OFFLINE: |
Mike Christie | 1b8d262 | 2012-05-17 23:56:56 -0500 | [diff] [blame] | 2593 | case SDEV_TRANSPORT_OFFLINE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | case SDEV_BLOCK: |
| 2595 | break; |
| 2596 | default: |
| 2597 | goto illegal; |
| 2598 | } |
| 2599 | break; |
| 2600 | |
| 2601 | case SDEV_DEL: |
| 2602 | switch (oldstate) { |
Brian King | 309bd27 | 2006-06-27 11:10:43 -0500 | [diff] [blame] | 2603 | case SDEV_CREATED: |
| 2604 | case SDEV_RUNNING: |
| 2605 | case SDEV_OFFLINE: |
Mike Christie | 1b8d262 | 2012-05-17 23:56:56 -0500 | [diff] [blame] | 2606 | case SDEV_TRANSPORT_OFFLINE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | case SDEV_CANCEL: |
Bart Van Assche | 0516c08 | 2013-07-02 15:06:33 +0200 | [diff] [blame] | 2608 | case SDEV_CREATED_BLOCK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | break; |
| 2610 | default: |
| 2611 | goto illegal; |
| 2612 | } |
| 2613 | break; |
| 2614 | |
| 2615 | } |
| 2616 | sdev->sdev_state = state; |
| 2617 | return 0; |
| 2618 | |
| 2619 | illegal: |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 2620 | SCSI_LOG_ERROR_RECOVERY(1, |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 2621 | sdev_printk(KERN_ERR, sdev, |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 2622 | "Illegal state transition %s->%s", |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 2623 | scsi_device_state_name(oldstate), |
| 2624 | scsi_device_state_name(state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | ); |
| 2626 | return -EINVAL; |
| 2627 | } |
| 2628 | EXPORT_SYMBOL(scsi_device_set_state); |
| 2629 | |
| 2630 | /** |
Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 2631 | * sdev_evt_emit - emit a single SCSI device uevent |
| 2632 | * @sdev: associated SCSI device |
| 2633 | * @evt: event to emit |
| 2634 | * |
| 2635 | * Send a single uevent (scsi_event) to the associated scsi_device. |
| 2636 | */ |
| 2637 | static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt) |
| 2638 | { |
| 2639 | int idx = 0; |
| 2640 | char *envp[3]; |
| 2641 | |
| 2642 | switch (evt->evt_type) { |
| 2643 | case SDEV_EVT_MEDIA_CHANGE: |
| 2644 | envp[idx++] = "SDEV_MEDIA_CHANGE=1"; |
| 2645 | break; |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 2646 | case SDEV_EVT_INQUIRY_CHANGE_REPORTED: |
Hannes Reinecke | d3d3289 | 2016-02-19 09:17:16 +0100 | [diff] [blame] | 2647 | scsi_rescan_device(&sdev->sdev_gendev); |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 2648 | envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED"; |
| 2649 | break; |
| 2650 | case SDEV_EVT_CAPACITY_CHANGE_REPORTED: |
| 2651 | envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED"; |
| 2652 | break; |
| 2653 | case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED: |
| 2654 | envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED"; |
| 2655 | break; |
| 2656 | case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED: |
| 2657 | envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED"; |
| 2658 | break; |
| 2659 | case SDEV_EVT_LUN_CHANGE_REPORTED: |
| 2660 | envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED"; |
| 2661 | break; |
Hannes Reinecke | 14c3e67 | 2015-07-06 13:41:53 +0200 | [diff] [blame] | 2662 | case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED: |
| 2663 | envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED"; |
| 2664 | break; |
Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 2665 | default: |
| 2666 | /* do nothing */ |
| 2667 | break; |
| 2668 | } |
| 2669 | |
| 2670 | envp[idx++] = NULL; |
| 2671 | |
| 2672 | kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp); |
| 2673 | } |
| 2674 | |
| 2675 | /** |
| 2676 | * sdev_evt_thread - send a uevent for each scsi event |
| 2677 | * @work: work struct for scsi_device |
| 2678 | * |
| 2679 | * Dispatch queued events to their associated scsi_device kobjects |
| 2680 | * as uevents. |
| 2681 | */ |
| 2682 | void scsi_evt_thread(struct work_struct *work) |
| 2683 | { |
| 2684 | struct scsi_device *sdev; |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 2685 | enum scsi_device_event evt_type; |
Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 2686 | LIST_HEAD(event_list); |
| 2687 | |
| 2688 | sdev = container_of(work, struct scsi_device, event_work); |
| 2689 | |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 2690 | for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++) |
| 2691 | if (test_and_clear_bit(evt_type, sdev->pending_events)) |
| 2692 | sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL); |
| 2693 | |
Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 2694 | while (1) { |
| 2695 | struct scsi_event *evt; |
| 2696 | struct list_head *this, *tmp; |
| 2697 | unsigned long flags; |
| 2698 | |
| 2699 | spin_lock_irqsave(&sdev->list_lock, flags); |
| 2700 | list_splice_init(&sdev->event_list, &event_list); |
| 2701 | spin_unlock_irqrestore(&sdev->list_lock, flags); |
| 2702 | |
| 2703 | if (list_empty(&event_list)) |
| 2704 | break; |
| 2705 | |
| 2706 | list_for_each_safe(this, tmp, &event_list) { |
| 2707 | evt = list_entry(this, struct scsi_event, node); |
| 2708 | list_del(&evt->node); |
| 2709 | scsi_evt_emit(sdev, evt); |
| 2710 | kfree(evt); |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | /** |
| 2716 | * sdev_evt_send - send asserted event to uevent thread |
| 2717 | * @sdev: scsi_device event occurred on |
| 2718 | * @evt: event to send |
| 2719 | * |
| 2720 | * Assert scsi device event asynchronously. |
| 2721 | */ |
| 2722 | void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt) |
| 2723 | { |
| 2724 | unsigned long flags; |
| 2725 | |
Kay Sievers | 4d1566e | 2008-03-19 13:04:47 +0100 | [diff] [blame] | 2726 | #if 0 |
| 2727 | /* FIXME: currently this check eliminates all media change events |
| 2728 | * for polled devices. Need to update to discriminate between AN |
| 2729 | * and polled events */ |
Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 2730 | if (!test_bit(evt->evt_type, sdev->supported_events)) { |
| 2731 | kfree(evt); |
| 2732 | return; |
| 2733 | } |
Kay Sievers | 4d1566e | 2008-03-19 13:04:47 +0100 | [diff] [blame] | 2734 | #endif |
Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 2735 | |
| 2736 | spin_lock_irqsave(&sdev->list_lock, flags); |
| 2737 | list_add_tail(&evt->node, &sdev->event_list); |
| 2738 | schedule_work(&sdev->event_work); |
| 2739 | spin_unlock_irqrestore(&sdev->list_lock, flags); |
| 2740 | } |
| 2741 | EXPORT_SYMBOL_GPL(sdev_evt_send); |
| 2742 | |
| 2743 | /** |
| 2744 | * sdev_evt_alloc - allocate a new scsi event |
| 2745 | * @evt_type: type of event to allocate |
| 2746 | * @gfpflags: GFP flags for allocation |
| 2747 | * |
| 2748 | * Allocates and returns a new scsi_event. |
| 2749 | */ |
| 2750 | struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type, |
| 2751 | gfp_t gfpflags) |
| 2752 | { |
| 2753 | struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags); |
| 2754 | if (!evt) |
| 2755 | return NULL; |
| 2756 | |
| 2757 | evt->evt_type = evt_type; |
| 2758 | INIT_LIST_HEAD(&evt->node); |
| 2759 | |
| 2760 | /* evt_type-specific initialization, if any */ |
| 2761 | switch (evt_type) { |
| 2762 | case SDEV_EVT_MEDIA_CHANGE: |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 2763 | case SDEV_EVT_INQUIRY_CHANGE_REPORTED: |
| 2764 | case SDEV_EVT_CAPACITY_CHANGE_REPORTED: |
| 2765 | case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED: |
| 2766 | case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED: |
| 2767 | case SDEV_EVT_LUN_CHANGE_REPORTED: |
Hannes Reinecke | 14c3e67 | 2015-07-06 13:41:53 +0200 | [diff] [blame] | 2768 | case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED: |
Jeff Garzik | a341cd0 | 2007-10-29 17:15:22 -0400 | [diff] [blame] | 2769 | default: |
| 2770 | /* do nothing */ |
| 2771 | break; |
| 2772 | } |
| 2773 | |
| 2774 | return evt; |
| 2775 | } |
| 2776 | EXPORT_SYMBOL_GPL(sdev_evt_alloc); |
| 2777 | |
| 2778 | /** |
| 2779 | * sdev_evt_send_simple - send asserted event to uevent thread |
| 2780 | * @sdev: scsi_device event occurred on |
| 2781 | * @evt_type: type of event to send |
| 2782 | * @gfpflags: GFP flags for allocation |
| 2783 | * |
| 2784 | * Assert scsi device event asynchronously, given an event type. |
| 2785 | */ |
| 2786 | void sdev_evt_send_simple(struct scsi_device *sdev, |
| 2787 | enum scsi_device_event evt_type, gfp_t gfpflags) |
| 2788 | { |
| 2789 | struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags); |
| 2790 | if (!evt) { |
| 2791 | sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n", |
| 2792 | evt_type); |
| 2793 | return; |
| 2794 | } |
| 2795 | |
| 2796 | sdev_evt_send(sdev, evt); |
| 2797 | } |
| 2798 | EXPORT_SYMBOL_GPL(sdev_evt_send_simple); |
| 2799 | |
| 2800 | /** |
Bart Van Assche | 669f044 | 2016-11-22 16:17:13 -0800 | [diff] [blame] | 2801 | * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn() |
| 2802 | * @sdev: SCSI device to count the number of scsi_request_fn() callers for. |
| 2803 | */ |
| 2804 | static int scsi_request_fn_active(struct scsi_device *sdev) |
| 2805 | { |
| 2806 | struct request_queue *q = sdev->request_queue; |
| 2807 | int request_fn_active; |
| 2808 | |
| 2809 | WARN_ON_ONCE(sdev->host->use_blk_mq); |
| 2810 | |
| 2811 | spin_lock_irq(q->queue_lock); |
| 2812 | request_fn_active = q->request_fn_active; |
| 2813 | spin_unlock_irq(q->queue_lock); |
| 2814 | |
| 2815 | return request_fn_active; |
| 2816 | } |
| 2817 | |
| 2818 | /** |
| 2819 | * scsi_wait_for_queuecommand() - wait for ongoing queuecommand() calls |
| 2820 | * @sdev: SCSI device pointer. |
| 2821 | * |
| 2822 | * Wait until the ongoing shost->hostt->queuecommand() calls that are |
| 2823 | * invoked from scsi_request_fn() have finished. |
| 2824 | */ |
| 2825 | static void scsi_wait_for_queuecommand(struct scsi_device *sdev) |
| 2826 | { |
| 2827 | WARN_ON_ONCE(sdev->host->use_blk_mq); |
| 2828 | |
| 2829 | while (scsi_request_fn_active(sdev)) |
| 2830 | msleep(20); |
| 2831 | } |
| 2832 | |
| 2833 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | * scsi_device_quiesce - Block user issued commands. |
| 2835 | * @sdev: scsi device to quiesce. |
| 2836 | * |
| 2837 | * This works by trying to transition to the SDEV_QUIESCE state |
| 2838 | * (which must be a legal transition). When the device is in this |
| 2839 | * state, only special requests will be accepted, all others will |
| 2840 | * be deferred. Since special requests may also be requeued requests, |
| 2841 | * a successful return doesn't guarantee the device will be |
| 2842 | * totally quiescent. |
| 2843 | * |
| 2844 | * Must be called with user context, may sleep. |
| 2845 | * |
| 2846 | * Returns zero if unsuccessful or an error if not. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2847 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | int |
| 2849 | scsi_device_quiesce(struct scsi_device *sdev) |
| 2850 | { |
| 2851 | int err = scsi_device_set_state(sdev, SDEV_QUIESCE); |
| 2852 | if (err) |
| 2853 | return err; |
| 2854 | |
| 2855 | scsi_run_queue(sdev->request_queue); |
Christoph Hellwig | 71e75c9 | 2014-04-11 19:07:01 +0200 | [diff] [blame] | 2856 | while (atomic_read(&sdev->device_busy)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | msleep_interruptible(200); |
| 2858 | scsi_run_queue(sdev->request_queue); |
| 2859 | } |
| 2860 | return 0; |
| 2861 | } |
| 2862 | EXPORT_SYMBOL(scsi_device_quiesce); |
| 2863 | |
| 2864 | /** |
| 2865 | * scsi_device_resume - Restart user issued commands to a quiesced device. |
| 2866 | * @sdev: scsi device to resume. |
| 2867 | * |
| 2868 | * Moves the device from quiesced back to running and restarts the |
| 2869 | * queues. |
| 2870 | * |
| 2871 | * Must be called with user context, may sleep. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2872 | */ |
Dan Williams | a7a20d1 | 2012-03-22 17:05:11 -0700 | [diff] [blame] | 2873 | void scsi_device_resume(struct scsi_device *sdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | { |
Dan Williams | a7a20d1 | 2012-03-22 17:05:11 -0700 | [diff] [blame] | 2875 | /* check if the device state was mutated prior to resume, and if |
| 2876 | * so assume the state is being managed elsewhere (for example |
| 2877 | * device deleted during suspend) |
| 2878 | */ |
| 2879 | if (sdev->sdev_state != SDEV_QUIESCE || |
| 2880 | scsi_device_set_state(sdev, SDEV_RUNNING)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | return; |
| 2882 | scsi_run_queue(sdev->request_queue); |
| 2883 | } |
| 2884 | EXPORT_SYMBOL(scsi_device_resume); |
| 2885 | |
| 2886 | static void |
| 2887 | device_quiesce_fn(struct scsi_device *sdev, void *data) |
| 2888 | { |
| 2889 | scsi_device_quiesce(sdev); |
| 2890 | } |
| 2891 | |
| 2892 | void |
| 2893 | scsi_target_quiesce(struct scsi_target *starget) |
| 2894 | { |
| 2895 | starget_for_each_device(starget, NULL, device_quiesce_fn); |
| 2896 | } |
| 2897 | EXPORT_SYMBOL(scsi_target_quiesce); |
| 2898 | |
| 2899 | static void |
| 2900 | device_resume_fn(struct scsi_device *sdev, void *data) |
| 2901 | { |
| 2902 | scsi_device_resume(sdev); |
| 2903 | } |
| 2904 | |
| 2905 | void |
| 2906 | scsi_target_resume(struct scsi_target *starget) |
| 2907 | { |
| 2908 | starget_for_each_device(starget, NULL, device_resume_fn); |
| 2909 | } |
| 2910 | EXPORT_SYMBOL(scsi_target_resume); |
| 2911 | |
| 2912 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2913 | * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | * @sdev: device to block |
| 2915 | * |
| 2916 | * Block request made by scsi lld's to temporarily stop all |
Bart Van Assche | 669f044 | 2016-11-22 16:17:13 -0800 | [diff] [blame] | 2917 | * scsi commands on the specified device. May sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | * |
| 2919 | * Returns zero if successful or error if not |
| 2920 | * |
| 2921 | * Notes: |
| 2922 | * This routine transitions the device to the SDEV_BLOCK state |
| 2923 | * (which must be a legal transition). When the device is in this |
| 2924 | * state, all commands are deferred until the scsi lld reenables |
| 2925 | * the device with scsi_device_unblock or device_block_tmo fires. |
Bart Van Assche | 669f044 | 2016-11-22 16:17:13 -0800 | [diff] [blame] | 2926 | * |
| 2927 | * To do: avoid that scsi_send_eh_cmnd() calls queuecommand() after |
| 2928 | * scsi_internal_device_block() has blocked a SCSI device and also |
| 2929 | * remove the rport mutex lock and unlock calls from srp_queuecommand(). |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2930 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | int |
| 2932 | scsi_internal_device_block(struct scsi_device *sdev) |
| 2933 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2934 | struct request_queue *q = sdev->request_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | unsigned long flags; |
| 2936 | int err = 0; |
| 2937 | |
| 2938 | err = scsi_device_set_state(sdev, SDEV_BLOCK); |
James Bottomley | 6f4267e | 2008-08-22 16:53:31 -0500 | [diff] [blame] | 2939 | if (err) { |
| 2940 | err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK); |
| 2941 | |
| 2942 | if (err) |
| 2943 | return err; |
| 2944 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | |
| 2946 | /* |
| 2947 | * The device has transitioned to SDEV_BLOCK. Stop the |
| 2948 | * block layer from calling the midlayer with this device's |
| 2949 | * request queue. |
| 2950 | */ |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2951 | if (q->mq_ops) { |
Bart Van Assche | 7dbbf0f | 2016-11-22 16:17:50 -0800 | [diff] [blame] | 2952 | blk_mq_quiesce_queue(q); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2953 | } else { |
| 2954 | spin_lock_irqsave(q->queue_lock, flags); |
| 2955 | blk_stop_queue(q); |
| 2956 | spin_unlock_irqrestore(q->queue_lock, flags); |
Bart Van Assche | 669f044 | 2016-11-22 16:17:13 -0800 | [diff] [blame] | 2957 | scsi_wait_for_queuecommand(sdev); |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 2958 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | |
| 2960 | return 0; |
| 2961 | } |
| 2962 | EXPORT_SYMBOL_GPL(scsi_internal_device_block); |
| 2963 | |
| 2964 | /** |
| 2965 | * scsi_internal_device_unblock - resume a device after a block request |
| 2966 | * @sdev: device to resume |
Mike Christie | 5d9fb5c | 2012-05-17 23:56:57 -0500 | [diff] [blame] | 2967 | * @new_state: state to set devices to after unblocking |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | * |
| 2969 | * Called by scsi lld's or the midlayer to restart the device queue |
| 2970 | * for the previously suspended scsi device. Called from interrupt or |
| 2971 | * normal process context. |
| 2972 | * |
| 2973 | * Returns zero if successful or error if not. |
| 2974 | * |
| 2975 | * Notes: |
| 2976 | * This routine transitions the device to the SDEV_RUNNING state |
Mike Christie | 5d9fb5c | 2012-05-17 23:56:57 -0500 | [diff] [blame] | 2977 | * or to one of the offline states (which must be a legal transition) |
Mike Christie | d075498 | 2012-05-17 23:56:58 -0500 | [diff] [blame] | 2978 | * allowing the midlayer to goose the queue for this device. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2979 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | int |
Mike Christie | 5d9fb5c | 2012-05-17 23:56:57 -0500 | [diff] [blame] | 2981 | scsi_internal_device_unblock(struct scsi_device *sdev, |
| 2982 | enum scsi_device_state new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2984 | struct request_queue *q = sdev->request_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | unsigned long flags; |
Mike Christie | 5d9fb5c | 2012-05-17 23:56:57 -0500 | [diff] [blame] | 2986 | |
| 2987 | /* |
| 2988 | * Try to transition the scsi device to SDEV_RUNNING or one of the |
| 2989 | * offlined states and goose the device queue if successful. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | */ |
Vikas Chaudhary | 0e58076 | 2012-08-09 04:51:30 -0400 | [diff] [blame] | 2991 | if ((sdev->sdev_state == SDEV_BLOCK) || |
| 2992 | (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE)) |
Mike Christie | 5d9fb5c | 2012-05-17 23:56:57 -0500 | [diff] [blame] | 2993 | sdev->sdev_state = new_state; |
| 2994 | else if (sdev->sdev_state == SDEV_CREATED_BLOCK) { |
| 2995 | if (new_state == SDEV_TRANSPORT_OFFLINE || |
| 2996 | new_state == SDEV_OFFLINE) |
| 2997 | sdev->sdev_state = new_state; |
| 2998 | else |
| 2999 | sdev->sdev_state = SDEV_CREATED; |
| 3000 | } else if (sdev->sdev_state != SDEV_CANCEL && |
Mike Christie | 986fe6c | 2010-10-06 03:10:59 -0500 | [diff] [blame] | 3001 | sdev->sdev_state != SDEV_OFFLINE) |
Takahiro Yasui | 5c10e63 | 2009-04-29 12:13:02 -0400 | [diff] [blame] | 3002 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | |
Christoph Hellwig | d285203 | 2014-01-17 12:06:53 +0100 | [diff] [blame] | 3004 | if (q->mq_ops) { |
| 3005 | blk_mq_start_stopped_hw_queues(q, false); |
| 3006 | } else { |
| 3007 | spin_lock_irqsave(q->queue_lock, flags); |
| 3008 | blk_start_queue(q); |
| 3009 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 3010 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | |
| 3012 | return 0; |
| 3013 | } |
| 3014 | EXPORT_SYMBOL_GPL(scsi_internal_device_unblock); |
| 3015 | |
| 3016 | static void |
| 3017 | device_block(struct scsi_device *sdev, void *data) |
| 3018 | { |
| 3019 | scsi_internal_device_block(sdev); |
| 3020 | } |
| 3021 | |
| 3022 | static int |
| 3023 | target_block(struct device *dev, void *data) |
| 3024 | { |
| 3025 | if (scsi_is_target_device(dev)) |
| 3026 | starget_for_each_device(to_scsi_target(dev), NULL, |
| 3027 | device_block); |
| 3028 | return 0; |
| 3029 | } |
| 3030 | |
| 3031 | void |
| 3032 | scsi_target_block(struct device *dev) |
| 3033 | { |
| 3034 | if (scsi_is_target_device(dev)) |
| 3035 | starget_for_each_device(to_scsi_target(dev), NULL, |
| 3036 | device_block); |
| 3037 | else |
| 3038 | device_for_each_child(dev, NULL, target_block); |
| 3039 | } |
| 3040 | EXPORT_SYMBOL_GPL(scsi_target_block); |
| 3041 | |
| 3042 | static void |
| 3043 | device_unblock(struct scsi_device *sdev, void *data) |
| 3044 | { |
Mike Christie | 5d9fb5c | 2012-05-17 23:56:57 -0500 | [diff] [blame] | 3045 | scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
| 3048 | static int |
| 3049 | target_unblock(struct device *dev, void *data) |
| 3050 | { |
| 3051 | if (scsi_is_target_device(dev)) |
Mike Christie | 5d9fb5c | 2012-05-17 23:56:57 -0500 | [diff] [blame] | 3052 | starget_for_each_device(to_scsi_target(dev), data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | device_unblock); |
| 3054 | return 0; |
| 3055 | } |
| 3056 | |
| 3057 | void |
Mike Christie | 5d9fb5c | 2012-05-17 23:56:57 -0500 | [diff] [blame] | 3058 | scsi_target_unblock(struct device *dev, enum scsi_device_state new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | { |
| 3060 | if (scsi_is_target_device(dev)) |
Mike Christie | 5d9fb5c | 2012-05-17 23:56:57 -0500 | [diff] [blame] | 3061 | starget_for_each_device(to_scsi_target(dev), &new_state, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | device_unblock); |
| 3063 | else |
Mike Christie | 5d9fb5c | 2012-05-17 23:56:57 -0500 | [diff] [blame] | 3064 | device_for_each_child(dev, &new_state, target_unblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | } |
| 3066 | EXPORT_SYMBOL_GPL(scsi_target_unblock); |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3067 | |
| 3068 | /** |
| 3069 | * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 3070 | * @sgl: scatter-gather list |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3071 | * @sg_count: number of segments in sg |
| 3072 | * @offset: offset in bytes into sg, on return offset into the mapped area |
| 3073 | * @len: bytes to map, on return number of bytes mapped |
| 3074 | * |
| 3075 | * Returns virtual address of the start of the mapped page |
| 3076 | */ |
Jens Axboe | c6132da | 2007-10-16 11:08:49 +0200 | [diff] [blame] | 3077 | void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count, |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3078 | size_t *offset, size_t *len) |
| 3079 | { |
| 3080 | int i; |
| 3081 | size_t sg_len = 0, len_complete = 0; |
Jens Axboe | c6132da | 2007-10-16 11:08:49 +0200 | [diff] [blame] | 3082 | struct scatterlist *sg; |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3083 | struct page *page; |
| 3084 | |
Andrew Morton | 22cfefb | 2007-02-05 16:39:03 -0800 | [diff] [blame] | 3085 | WARN_ON(!irqs_disabled()); |
| 3086 | |
Jens Axboe | c6132da | 2007-10-16 11:08:49 +0200 | [diff] [blame] | 3087 | for_each_sg(sgl, sg, sg_count, i) { |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3088 | len_complete = sg_len; /* Complete sg-entries */ |
Jens Axboe | c6132da | 2007-10-16 11:08:49 +0200 | [diff] [blame] | 3089 | sg_len += sg->length; |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3090 | if (sg_len > *offset) |
| 3091 | break; |
| 3092 | } |
| 3093 | |
| 3094 | if (unlikely(i == sg_count)) { |
Andrew Morton | 169e1a2 | 2006-04-18 21:09:08 -0700 | [diff] [blame] | 3095 | printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, " |
| 3096 | "elements %d\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 3097 | __func__, sg_len, *offset, sg_count); |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3098 | WARN_ON(1); |
| 3099 | return NULL; |
| 3100 | } |
| 3101 | |
| 3102 | /* Offset starting from the beginning of first page in this sg-entry */ |
Jens Axboe | c6132da | 2007-10-16 11:08:49 +0200 | [diff] [blame] | 3103 | *offset = *offset - len_complete + sg->offset; |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3104 | |
| 3105 | /* Assumption: contiguous pages can be accessed as "page + i" */ |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 3106 | page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT)); |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3107 | *offset &= ~PAGE_MASK; |
| 3108 | |
| 3109 | /* Bytes in this sg-entry from *offset to the end of the page */ |
| 3110 | sg_len = PAGE_SIZE - *offset; |
| 3111 | if (*len > sg_len) |
| 3112 | *len = sg_len; |
| 3113 | |
Cong Wang | 77dfce0 | 2011-11-25 23:14:23 +0800 | [diff] [blame] | 3114 | return kmap_atomic(page); |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3115 | } |
| 3116 | EXPORT_SYMBOL(scsi_kmap_atomic_sg); |
| 3117 | |
| 3118 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 3119 | * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3120 | * @virt: virtual address to be unmapped |
| 3121 | */ |
| 3122 | void scsi_kunmap_atomic_sg(void *virt) |
| 3123 | { |
Cong Wang | 77dfce0 | 2011-11-25 23:14:23 +0800 | [diff] [blame] | 3124 | kunmap_atomic(virt); |
Guennadi Liakhovetski | cdb8c2a | 2006-04-02 21:57:43 +0200 | [diff] [blame] | 3125 | } |
| 3126 | EXPORT_SYMBOL(scsi_kunmap_atomic_sg); |
Aaron Lu | 6f4c827 | 2013-01-23 15:09:32 +0800 | [diff] [blame] | 3127 | |
| 3128 | void sdev_disable_disk_events(struct scsi_device *sdev) |
| 3129 | { |
| 3130 | atomic_inc(&sdev->disk_events_disable_depth); |
| 3131 | } |
| 3132 | EXPORT_SYMBOL(sdev_disable_disk_events); |
| 3133 | |
| 3134 | void sdev_enable_disk_events(struct scsi_device *sdev) |
| 3135 | { |
| 3136 | if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0)) |
| 3137 | return; |
| 3138 | atomic_dec(&sdev->disk_events_disable_depth); |
| 3139 | } |
| 3140 | EXPORT_SYMBOL(sdev_enable_disk_events); |
Hannes Reinecke | 9983bed | 2015-12-01 10:16:55 +0100 | [diff] [blame] | 3141 | |
| 3142 | /** |
| 3143 | * scsi_vpd_lun_id - return a unique device identification |
| 3144 | * @sdev: SCSI device |
| 3145 | * @id: buffer for the identification |
| 3146 | * @id_len: length of the buffer |
| 3147 | * |
| 3148 | * Copies a unique device identification into @id based |
| 3149 | * on the information in the VPD page 0x83 of the device. |
| 3150 | * The string will be formatted as a SCSI name string. |
| 3151 | * |
| 3152 | * Returns the length of the identification or error on failure. |
| 3153 | * If the identifier is longer than the supplied buffer the actual |
| 3154 | * identifier length is returned and the buffer is not zero-padded. |
| 3155 | */ |
| 3156 | int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len) |
| 3157 | { |
| 3158 | u8 cur_id_type = 0xff; |
| 3159 | u8 cur_id_size = 0; |
| 3160 | unsigned char *d, *cur_id_str; |
| 3161 | unsigned char __rcu *vpd_pg83; |
| 3162 | int id_size = -EINVAL; |
| 3163 | |
| 3164 | rcu_read_lock(); |
| 3165 | vpd_pg83 = rcu_dereference(sdev->vpd_pg83); |
| 3166 | if (!vpd_pg83) { |
| 3167 | rcu_read_unlock(); |
| 3168 | return -ENXIO; |
| 3169 | } |
| 3170 | |
| 3171 | /* |
| 3172 | * Look for the correct descriptor. |
| 3173 | * Order of preference for lun descriptor: |
| 3174 | * - SCSI name string |
| 3175 | * - NAA IEEE Registered Extended |
| 3176 | * - EUI-64 based 16-byte |
| 3177 | * - EUI-64 based 12-byte |
| 3178 | * - NAA IEEE Registered |
| 3179 | * - NAA IEEE Extended |
Hannes Reinecke | d230823 | 2016-05-09 09:14:29 +0200 | [diff] [blame] | 3180 | * - T10 Vendor ID |
Hannes Reinecke | 9983bed | 2015-12-01 10:16:55 +0100 | [diff] [blame] | 3181 | * as longer descriptors reduce the likelyhood |
| 3182 | * of identification clashes. |
| 3183 | */ |
| 3184 | |
| 3185 | /* The id string must be at least 20 bytes + terminating NULL byte */ |
| 3186 | if (id_len < 21) { |
| 3187 | rcu_read_unlock(); |
| 3188 | return -EINVAL; |
| 3189 | } |
| 3190 | |
| 3191 | memset(id, 0, id_len); |
| 3192 | d = vpd_pg83 + 4; |
| 3193 | while (d < vpd_pg83 + sdev->vpd_pg83_len) { |
| 3194 | /* Skip designators not referring to the LUN */ |
| 3195 | if ((d[1] & 0x30) != 0x00) |
| 3196 | goto next_desig; |
| 3197 | |
| 3198 | switch (d[1] & 0xf) { |
Hannes Reinecke | d230823 | 2016-05-09 09:14:29 +0200 | [diff] [blame] | 3199 | case 0x1: |
| 3200 | /* T10 Vendor ID */ |
| 3201 | if (cur_id_size > d[3]) |
| 3202 | break; |
| 3203 | /* Prefer anything */ |
| 3204 | if (cur_id_type > 0x01 && cur_id_type != 0xff) |
| 3205 | break; |
| 3206 | cur_id_size = d[3]; |
| 3207 | if (cur_id_size + 4 > id_len) |
| 3208 | cur_id_size = id_len - 4; |
| 3209 | cur_id_str = d + 4; |
| 3210 | cur_id_type = d[1] & 0xf; |
| 3211 | id_size = snprintf(id, id_len, "t10.%*pE", |
| 3212 | cur_id_size, cur_id_str); |
| 3213 | break; |
Hannes Reinecke | 9983bed | 2015-12-01 10:16:55 +0100 | [diff] [blame] | 3214 | case 0x2: |
| 3215 | /* EUI-64 */ |
| 3216 | if (cur_id_size > d[3]) |
| 3217 | break; |
| 3218 | /* Prefer NAA IEEE Registered Extended */ |
| 3219 | if (cur_id_type == 0x3 && |
| 3220 | cur_id_size == d[3]) |
| 3221 | break; |
| 3222 | cur_id_size = d[3]; |
| 3223 | cur_id_str = d + 4; |
| 3224 | cur_id_type = d[1] & 0xf; |
| 3225 | switch (cur_id_size) { |
| 3226 | case 8: |
| 3227 | id_size = snprintf(id, id_len, |
| 3228 | "eui.%8phN", |
| 3229 | cur_id_str); |
| 3230 | break; |
| 3231 | case 12: |
| 3232 | id_size = snprintf(id, id_len, |
| 3233 | "eui.%12phN", |
| 3234 | cur_id_str); |
| 3235 | break; |
| 3236 | case 16: |
| 3237 | id_size = snprintf(id, id_len, |
| 3238 | "eui.%16phN", |
| 3239 | cur_id_str); |
| 3240 | break; |
| 3241 | default: |
| 3242 | cur_id_size = 0; |
| 3243 | break; |
| 3244 | } |
| 3245 | break; |
| 3246 | case 0x3: |
| 3247 | /* NAA */ |
| 3248 | if (cur_id_size > d[3]) |
| 3249 | break; |
| 3250 | cur_id_size = d[3]; |
| 3251 | cur_id_str = d + 4; |
| 3252 | cur_id_type = d[1] & 0xf; |
| 3253 | switch (cur_id_size) { |
| 3254 | case 8: |
| 3255 | id_size = snprintf(id, id_len, |
| 3256 | "naa.%8phN", |
| 3257 | cur_id_str); |
| 3258 | break; |
| 3259 | case 16: |
| 3260 | id_size = snprintf(id, id_len, |
| 3261 | "naa.%16phN", |
| 3262 | cur_id_str); |
| 3263 | break; |
| 3264 | default: |
| 3265 | cur_id_size = 0; |
| 3266 | break; |
| 3267 | } |
| 3268 | break; |
| 3269 | case 0x8: |
| 3270 | /* SCSI name string */ |
| 3271 | if (cur_id_size + 4 > d[3]) |
| 3272 | break; |
| 3273 | /* Prefer others for truncated descriptor */ |
| 3274 | if (cur_id_size && d[3] > id_len) |
| 3275 | break; |
| 3276 | cur_id_size = id_size = d[3]; |
| 3277 | cur_id_str = d + 4; |
| 3278 | cur_id_type = d[1] & 0xf; |
| 3279 | if (cur_id_size >= id_len) |
| 3280 | cur_id_size = id_len - 1; |
| 3281 | memcpy(id, cur_id_str, cur_id_size); |
| 3282 | /* Decrease priority for truncated descriptor */ |
| 3283 | if (cur_id_size != id_size) |
| 3284 | cur_id_size = 6; |
| 3285 | break; |
| 3286 | default: |
| 3287 | break; |
| 3288 | } |
| 3289 | next_desig: |
| 3290 | d += d[3] + 4; |
| 3291 | } |
| 3292 | rcu_read_unlock(); |
| 3293 | |
| 3294 | return id_size; |
| 3295 | } |
| 3296 | EXPORT_SYMBOL(scsi_vpd_lun_id); |
Hannes Reinecke | a8aa397 | 2015-12-01 10:16:57 +0100 | [diff] [blame] | 3297 | |
| 3298 | /* |
| 3299 | * scsi_vpd_tpg_id - return a target port group identifier |
| 3300 | * @sdev: SCSI device |
| 3301 | * |
| 3302 | * Returns the Target Port Group identifier from the information |
| 3303 | * froom VPD page 0x83 of the device. |
| 3304 | * |
| 3305 | * Returns the identifier or error on failure. |
| 3306 | */ |
| 3307 | int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id) |
| 3308 | { |
| 3309 | unsigned char *d; |
| 3310 | unsigned char __rcu *vpd_pg83; |
| 3311 | int group_id = -EAGAIN, rel_port = -1; |
| 3312 | |
| 3313 | rcu_read_lock(); |
| 3314 | vpd_pg83 = rcu_dereference(sdev->vpd_pg83); |
| 3315 | if (!vpd_pg83) { |
| 3316 | rcu_read_unlock(); |
| 3317 | return -ENXIO; |
| 3318 | } |
| 3319 | |
| 3320 | d = sdev->vpd_pg83 + 4; |
| 3321 | while (d < sdev->vpd_pg83 + sdev->vpd_pg83_len) { |
| 3322 | switch (d[1] & 0xf) { |
| 3323 | case 0x4: |
| 3324 | /* Relative target port */ |
| 3325 | rel_port = get_unaligned_be16(&d[6]); |
| 3326 | break; |
| 3327 | case 0x5: |
| 3328 | /* Target port group */ |
| 3329 | group_id = get_unaligned_be16(&d[6]); |
| 3330 | break; |
| 3331 | default: |
| 3332 | break; |
| 3333 | } |
| 3334 | d += d[3] + 4; |
| 3335 | } |
| 3336 | rcu_read_unlock(); |
| 3337 | |
| 3338 | if (group_id >= 0 && rel_id && rel_port != -1) |
| 3339 | *rel_id = rel_port; |
| 3340 | |
| 3341 | return group_id; |
| 3342 | } |
| 3343 | EXPORT_SYMBOL(scsi_vpd_tpg_id); |