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