Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
d285203c CH |
2 | * Copyright (C) 1999 Eric Youngdale |
3 | * Copyright (C) 2014 Christoph Hellwig | |
1da177e4 LT |
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> | |
d3f46f39 | 12 | #include <linux/bitops.h> |
1da177e4 LT |
13 | #include <linux/blkdev.h> |
14 | #include <linux/completion.h> | |
15 | #include <linux/kernel.h> | |
09703660 | 16 | #include <linux/export.h> |
1da177e4 LT |
17 | #include <linux/init.h> |
18 | #include <linux/pci.h> | |
19 | #include <linux/delay.h> | |
faead26d | 20 | #include <linux/hardirq.h> |
c6132da1 | 21 | #include <linux/scatterlist.h> |
d285203c | 22 | #include <linux/blk-mq.h> |
f1569ff1 | 23 | #include <linux/ratelimit.h> |
a8aa3978 | 24 | #include <asm/unaligned.h> |
1da177e4 LT |
25 | |
26 | #include <scsi/scsi.h> | |
beb40487 | 27 | #include <scsi/scsi_cmnd.h> |
1da177e4 LT |
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> | |
ee14c674 | 33 | #include <scsi/scsi_dh.h> |
1da177e4 | 34 | |
3b5382c4 CH |
35 | #include <trace/events/scsi.h> |
36 | ||
1da177e4 LT |
37 | #include "scsi_priv.h" |
38 | #include "scsi_logging.h" | |
39 | ||
e9c787e6 | 40 | static struct kmem_cache *scsi_sdb_cache; |
0a6ac4ee CH |
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); | |
1da177e4 | 44 | |
0a6ac4ee CH |
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 | ||
e9c787e6 | 52 | static void scsi_free_sense_buffer(struct Scsi_Host *shost, |
0a6ac4ee CH |
53 | unsigned char *sense_buffer) |
54 | { | |
55 | kmem_cache_free(scsi_select_sense_cache(shost), sense_buffer); | |
56 | } | |
57 | ||
e9c787e6 CH |
58 | static unsigned char *scsi_alloc_sense_buffer(struct Scsi_Host *shost, |
59 | gfp_t gfp_mask, int numa_node) | |
0a6ac4ee CH |
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 | } | |
6f9a35e2 | 93 | |
a488e749 JA |
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 | ||
de3e8bf3 CH |
101 | static void |
102 | scsi_set_blocked(struct scsi_cmnd *cmd, int reason) | |
1da177e4 LT |
103 | { |
104 | struct Scsi_Host *host = cmd->device->host; | |
105 | struct scsi_device *device = cmd->device; | |
f0c0a376 | 106 | struct scsi_target *starget = scsi_target(device); |
1da177e4 LT |
107 | |
108 | /* | |
d8c37e7b | 109 | * Set the appropriate busy bit for the device/host. |
1da177e4 LT |
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 | */ | |
f0c0a376 MC |
121 | switch (reason) { |
122 | case SCSI_MLQUEUE_HOST_BUSY: | |
cd9070c9 | 123 | atomic_set(&host->host_blocked, host->max_host_blocked); |
f0c0a376 MC |
124 | break; |
125 | case SCSI_MLQUEUE_DEVICE_BUSY: | |
573e5913 | 126 | case SCSI_MLQUEUE_EH_RETRY: |
cd9070c9 CH |
127 | atomic_set(&device->device_blocked, |
128 | device->max_device_blocked); | |
f0c0a376 MC |
129 | break; |
130 | case SCSI_MLQUEUE_TARGET_BUSY: | |
cd9070c9 CH |
131 | atomic_set(&starget->target_blocked, |
132 | starget->max_target_blocked); | |
f0c0a376 MC |
133 | break; |
134 | } | |
de3e8bf3 CH |
135 | } |
136 | ||
d285203c CH |
137 | static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd) |
138 | { | |
139 | struct scsi_device *sdev = cmd->device; | |
d285203c | 140 | |
2b053aca | 141 | blk_mq_requeue_request(cmd->request, true); |
d285203c CH |
142 | put_device(&sdev->sdev_gendev); |
143 | } | |
144 | ||
de3e8bf3 CH |
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); | |
1da177e4 | 167 | |
1da177e4 LT |
168 | /* |
169 | * Decrement the counters, since these commands are no longer | |
170 | * active on the host/device. | |
171 | */ | |
4f5299ac JB |
172 | if (unbusy) |
173 | scsi_device_unbusy(device); | |
1da177e4 LT |
174 | |
175 | /* | |
a1bf9d1d | 176 | * Requeue this command. It will go before all other commands |
b485462a BVA |
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. | |
a488e749 | 180 | */ |
644373a4 | 181 | cmd->result = 0; |
d285203c CH |
182 | if (q->mq_ops) { |
183 | scsi_mq_requeue_cmd(cmd); | |
184 | return; | |
185 | } | |
a1bf9d1d | 186 | spin_lock_irqsave(q->queue_lock, flags); |
59897dad | 187 | blk_requeue_request(q, cmd->request); |
59c3d45e | 188 | kblockd_schedule_work(&device->requeue_work); |
b485462a | 189 | spin_unlock_irqrestore(q->queue_lock, flags); |
1da177e4 LT |
190 | } |
191 | ||
4f5299ac JB |
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 | */ | |
84feb166 | 211 | void scsi_queue_insert(struct scsi_cmnd *cmd, int reason) |
4f5299ac | 212 | { |
84feb166 | 213 | __scsi_queue_insert(cmd, reason, 1); |
4f5299ac | 214 | } |
e8064021 CH |
215 | |
216 | static int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd, | |
33aa687d | 217 | int data_direction, void *buffer, unsigned bufflen, |
3949e2f0 CH |
218 | unsigned char *sense, struct scsi_sense_hdr *sshdr, |
219 | int timeout, int retries, u64 flags, req_flags_t rq_flags, | |
220 | int *resid) | |
39216033 JB |
221 | { |
222 | struct request *req; | |
82ed4db4 | 223 | struct scsi_request *rq; |
39216033 JB |
224 | int ret = DRIVER_ERROR << 24; |
225 | ||
aebf526b CH |
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); | |
a492f075 | 229 | if (IS_ERR(req)) |
bfe159a5 | 230 | return ret; |
82ed4db4 CH |
231 | rq = scsi_req(req); |
232 | scsi_req_init(req); | |
39216033 JB |
233 | |
234 | if (bufflen && blk_rq_map_kern(sdev->request_queue, req, | |
71baba4b | 235 | buffer, bufflen, __GFP_RECLAIM)) |
39216033 JB |
236 | goto out; |
237 | ||
82ed4db4 CH |
238 | rq->cmd_len = COMMAND_SIZE(cmd[0]); |
239 | memcpy(rq->cmd, cmd, rq->cmd_len); | |
17e01f21 | 240 | req->retries = retries; |
39216033 | 241 | req->timeout = timeout; |
e8064021 CH |
242 | req->cmd_flags |= flags; |
243 | req->rq_flags |= rq_flags | RQF_QUIET | RQF_PREEMPT; | |
39216033 JB |
244 | |
245 | /* | |
246 | * head injection *required* here otherwise quiesce won't work | |
247 | */ | |
248 | blk_execute_rq(req->q, NULL, req, 1); | |
249 | ||
bdb2b8ca AS |
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 | */ | |
82ed4db4 CH |
256 | if (unlikely(rq->resid_len > 0 && rq->resid_len <= bufflen)) |
257 | memset(buffer + (bufflen - rq->resid_len), 0, rq->resid_len); | |
bdb2b8ca | 258 | |
f4f4e47e | 259 | if (resid) |
82ed4db4 CH |
260 | *resid = rq->resid_len; |
261 | if (sense && rq->sense_len) | |
262 | memcpy(sense, rq->sense, SCSI_SENSE_BUFFERSIZE); | |
3949e2f0 CH |
263 | if (sshdr) |
264 | scsi_normalize_sense(rq->sense, rq->sense_len, sshdr); | |
39216033 JB |
265 | ret = req->errors; |
266 | out: | |
267 | blk_put_request(req); | |
268 | ||
269 | return ret; | |
270 | } | |
e8064021 CH |
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, | |
3949e2f0 | 294 | NULL, timeout, retries, flags, 0, resid); |
e8064021 | 295 | } |
33aa687d | 296 | EXPORT_SYMBOL(scsi_execute); |
39216033 | 297 | |
9b21493c | 298 | int scsi_execute_req_flags(struct scsi_device *sdev, const unsigned char *cmd, |
ea73a9f2 | 299 | int data_direction, void *buffer, unsigned bufflen, |
f4f4e47e | 300 | struct scsi_sense_hdr *sshdr, int timeout, int retries, |
e8064021 | 301 | int *resid, u64 flags, req_flags_t rq_flags) |
ea73a9f2 | 302 | { |
3949e2f0 CH |
303 | return __scsi_execute(sdev, cmd, data_direction, buffer, bufflen, |
304 | NULL, sshdr, timeout, retries, flags, rq_flags, | |
305 | resid); | |
ea73a9f2 | 306 | } |
9b21493c | 307 | EXPORT_SYMBOL(scsi_execute_req_flags); |
ea73a9f2 | 308 | |
1da177e4 LT |
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 | * | |
1da177e4 LT |
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 | */ | |
631c228c | 320 | static void scsi_init_cmd_errh(struct scsi_cmnd *cmd) |
1da177e4 | 321 | { |
1da177e4 | 322 | cmd->serial_number = 0; |
30b0c37b | 323 | scsi_set_resid(cmd, 0); |
b80ca4f7 | 324 | memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); |
1da177e4 | 325 | if (cmd->cmd_len == 0) |
db4742dd | 326 | cmd->cmd_len = scsi_command_size(cmd->cmnd); |
1da177e4 LT |
327 | } |
328 | ||
329 | void scsi_device_unbusy(struct scsi_device *sdev) | |
330 | { | |
331 | struct Scsi_Host *shost = sdev->host; | |
f0c0a376 | 332 | struct scsi_target *starget = scsi_target(sdev); |
1da177e4 LT |
333 | unsigned long flags; |
334 | ||
74665016 | 335 | atomic_dec(&shost->host_busy); |
2ccbb008 CH |
336 | if (starget->can_queue > 0) |
337 | atomic_dec(&starget->target_busy); | |
74665016 | 338 | |
939647ee | 339 | if (unlikely(scsi_host_in_recovery(shost) && |
74665016 CH |
340 | (shost->host_failed || shost->host_eh_scheduled))) { |
341 | spin_lock_irqsave(shost->host_lock, flags); | |
1da177e4 | 342 | scsi_eh_wakeup(shost); |
74665016 CH |
343 | spin_unlock_irqrestore(shost->host_lock, flags); |
344 | } | |
345 | ||
71e75c97 | 346 | atomic_dec(&sdev->device_busy); |
1da177e4 LT |
347 | } |
348 | ||
d285203c CH |
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 | ||
1da177e4 LT |
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 | */ | |
d285203c | 381 | scsi_kick_queue(current_sdev->request_queue); |
1da177e4 LT |
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); | |
d285203c | 394 | scsi_kick_queue(sdev->request_queue); |
1da177e4 LT |
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 | ||
cd9070c9 | 403 | static inline bool scsi_device_is_busy(struct scsi_device *sdev) |
9d112517 | 404 | { |
cd9070c9 CH |
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; | |
9d112517 KU |
410 | } |
411 | ||
cd9070c9 | 412 | static inline bool scsi_target_is_busy(struct scsi_target *starget) |
f0c0a376 | 413 | { |
2ccbb008 CH |
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 | } | |
cd9070c9 | 420 | return false; |
f0c0a376 MC |
421 | } |
422 | ||
cd9070c9 | 423 | static inline bool scsi_host_is_busy(struct Scsi_Host *shost) |
9d112517 | 424 | { |
cd9070c9 CH |
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; | |
9d112517 KU |
433 | } |
434 | ||
21a05df5 | 435 | static void scsi_starved_list_run(struct Scsi_Host *shost) |
1da177e4 | 436 | { |
2a3a59e5 | 437 | LIST_HEAD(starved_list); |
21a05df5 | 438 | struct scsi_device *sdev; |
1da177e4 LT |
439 | unsigned long flags; |
440 | ||
1da177e4 | 441 | spin_lock_irqsave(shost->host_lock, flags); |
2a3a59e5 MC |
442 | list_splice_init(&shost->starved_list, &starved_list); |
443 | ||
444 | while (!list_empty(&starved_list)) { | |
e2eb7244 JB |
445 | struct request_queue *slq; |
446 | ||
1da177e4 LT |
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 | */ | |
2a3a59e5 | 457 | if (scsi_host_is_busy(shost)) |
f0c0a376 | 458 | break; |
f0c0a376 | 459 | |
2a3a59e5 MC |
460 | sdev = list_entry(starved_list.next, |
461 | struct scsi_device, starved_entry); | |
462 | list_del_init(&sdev->starved_entry); | |
f0c0a376 MC |
463 | if (scsi_target_is_busy(scsi_target(sdev))) { |
464 | list_move_tail(&sdev->starved_entry, | |
465 | &shost->starved_list); | |
466 | continue; | |
467 | } | |
468 | ||
e2eb7244 JB |
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 | ||
d285203c | 484 | scsi_kick_queue(slq); |
e2eb7244 JB |
485 | blk_put_queue(slq); |
486 | ||
487 | spin_lock_irqsave(shost->host_lock, flags); | |
1da177e4 | 488 | } |
2a3a59e5 MC |
489 | /* put any unprocessed entries back */ |
490 | list_splice(&starved_list, &shost->starved_list); | |
1da177e4 | 491 | spin_unlock_irqrestore(shost->host_lock, flags); |
21a05df5 CH |
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); | |
1da177e4 | 514 | |
d285203c CH |
515 | if (q->mq_ops) |
516 | blk_mq_start_stopped_hw_queues(q, false); | |
517 | else | |
518 | blk_run_queue(q); | |
1da177e4 LT |
519 | } |
520 | ||
9937a5e2 JA |
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 | ||
1da177e4 LT |
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. | |
e91442b6 | 547 | * Notes: Upon return, cmd is a stale pointer. |
1da177e4 LT |
548 | */ |
549 | static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd) | |
550 | { | |
940f5d47 | 551 | struct scsi_device *sdev = cmd->device; |
e91442b6 | 552 | struct request *req = cmd->request; |
283369cc TH |
553 | unsigned long flags; |
554 | ||
283369cc | 555 | spin_lock_irqsave(q->queue_lock, flags); |
134997a0 CH |
556 | blk_unprep_request(req); |
557 | req->special = NULL; | |
558 | scsi_put_command(cmd); | |
e91442b6 | 559 | blk_requeue_request(q, req); |
283369cc | 560 | spin_unlock_irqrestore(q->queue_lock, flags); |
1da177e4 LT |
561 | |
562 | scsi_run_queue(q); | |
940f5d47 BVA |
563 | |
564 | put_device(&sdev->sdev_gendev); | |
1da177e4 LT |
565 | } |
566 | ||
1da177e4 LT |
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 | ||
d285203c CH |
575 | static void scsi_uninit_cmd(struct scsi_cmnd *cmd) |
576 | { | |
57292b58 | 577 | if (!blk_rq_is_passthrough(cmd->request)) { |
d285203c CH |
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 | { | |
91dbc08d ML |
587 | struct scsi_data_buffer *sdb; |
588 | ||
d285203c | 589 | if (cmd->sdb.table.nents) |
001d63be | 590 | sg_free_table_chained(&cmd->sdb.table, true); |
91dbc08d ML |
591 | if (cmd->request->next_rq) { |
592 | sdb = cmd->request->next_rq->special; | |
593 | if (sdb) | |
001d63be | 594 | sg_free_table_chained(&sdb->table, true); |
91dbc08d | 595 | } |
d285203c | 596 | if (scsi_prot_sg_count(cmd)) |
001d63be | 597 | sg_free_table_chained(&cmd->prot_sdb->table, true); |
d285203c CH |
598 | } |
599 | ||
600 | static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd) | |
601 | { | |
602 | struct scsi_device *sdev = cmd->device; | |
64bdcbc4 | 603 | struct Scsi_Host *shost = sdev->host; |
d285203c CH |
604 | unsigned long flags; |
605 | ||
d285203c CH |
606 | scsi_mq_free_sgtables(cmd); |
607 | scsi_uninit_cmd(cmd); | |
608 | ||
64bdcbc4 KD |
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 | } | |
d285203c CH |
615 | } |
616 | ||
1da177e4 LT |
617 | /* |
618 | * Function: scsi_release_buffers() | |
619 | * | |
c682adf3 | 620 | * Purpose: Free resources allocate for a scsi_command. |
1da177e4 LT |
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 | |
c682adf3 | 631 | * the scatter-gather table. |
1da177e4 | 632 | */ |
f1bea55d | 633 | static void scsi_release_buffers(struct scsi_cmnd *cmd) |
1da177e4 | 634 | { |
c682adf3 | 635 | if (cmd->sdb.table.nents) |
001d63be | 636 | sg_free_table_chained(&cmd->sdb.table, false); |
c682adf3 CH |
637 | |
638 | memset(&cmd->sdb, 0, sizeof(cmd->sdb)); | |
639 | ||
640 | if (scsi_prot_sg_count(cmd)) | |
001d63be | 641 | sg_free_table_chained(&cmd->prot_sdb->table, false); |
1da177e4 LT |
642 | } |
643 | ||
c682adf3 CH |
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 | ||
001d63be | 648 | sg_free_table_chained(&bidi_sdb->table, false); |
c682adf3 CH |
649 | kmem_cache_free(scsi_sdb_cache, bidi_sdb); |
650 | cmd->request->next_rq->special = NULL; | |
651 | } | |
652 | ||
f6d47e74 CH |
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; | |
f6d47e74 CH |
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 | ||
d285203c CH |
671 | if (req->mq_ctx) { |
672 | /* | |
c8a446ad | 673 | * In the MQ case the command gets freed by __blk_mq_end_request, |
d285203c CH |
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); | |
680 | ||
c8a446ad | 681 | __blk_mq_end_request(req, error); |
d285203c CH |
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); | |
d285203c CH |
688 | } else { |
689 | unsigned long flags; | |
690 | ||
f81426a8 DG |
691 | if (bidi_bytes) |
692 | scsi_release_bidi_buffers(cmd); | |
e9c787e6 CH |
693 | scsi_release_buffers(cmd); |
694 | scsi_put_command(cmd); | |
f81426a8 | 695 | |
d285203c CH |
696 | spin_lock_irqsave(q->queue_lock, flags); |
697 | blk_finish_request(req, error); | |
698 | spin_unlock_irqrestore(q->queue_lock, flags); | |
699 | ||
bb3ec62a | 700 | scsi_run_queue(q); |
d285203c | 701 | } |
f6d47e74 | 702 | |
bb3ec62a | 703 | put_device(&sdev->sdev_gendev); |
f6d47e74 CH |
704 | return false; |
705 | } | |
706 | ||
0f7f6234 HR |
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 | |
a9d6ceb8 | 717 | * -ENOSPC No write space available |
7e782af5 | 718 | * -ENODATA Medium error |
0f7f6234 HR |
719 | * -EIO unspecified I/O error |
720 | */ | |
63583cca HR |
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: | |
2082ebc4 | 730 | set_host_byte(cmd, DID_OK); |
63583cca HR |
731 | error = -EREMOTEIO; |
732 | break; | |
733 | case DID_NEXUS_FAILURE: | |
2082ebc4 | 734 | set_host_byte(cmd, DID_OK); |
63583cca HR |
735 | error = -EBADE; |
736 | break; | |
a9d6ceb8 HR |
737 | case DID_ALLOC_FAILURE: |
738 | set_host_byte(cmd, DID_OK); | |
739 | error = -ENOSPC; | |
740 | break; | |
7e782af5 HR |
741 | case DID_MEDIUM_ERROR: |
742 | set_host_byte(cmd, DID_OK); | |
743 | error = -ENODATA; | |
744 | break; | |
63583cca HR |
745 | default: |
746 | error = -EIO; | |
747 | break; | |
748 | } | |
749 | ||
750 | return error; | |
751 | } | |
752 | ||
1da177e4 LT |
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 | * | |
bc85dc50 CH |
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 | |
b60af5b0 | 767 | * figure out what to do next: |
1da177e4 | 768 | * |
b60af5b0 AS |
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. | |
1da177e4 | 774 | * |
bc85dc50 | 775 | * b) We can call __scsi_queue_insert(). The request will |
b60af5b0 AS |
776 | * be put back on the queue and retried using the same |
777 | * command as before, possibly after a delay. | |
778 | * | |
f6d47e74 | 779 | * c) We can call scsi_end_request() with -EIO to fail |
b60af5b0 | 780 | * the remainder of the request. |
1da177e4 | 781 | */ |
03aba2f7 | 782 | void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes) |
1da177e4 LT |
783 | { |
784 | int result = cmd->result; | |
165125e1 | 785 | struct request_queue *q = cmd->device->request_queue; |
1da177e4 | 786 | struct request *req = cmd->request; |
fa8e36c3 | 787 | int error = 0; |
1da177e4 | 788 | struct scsi_sense_hdr sshdr; |
4753cbc0 | 789 | bool sense_valid = false; |
c11c004b | 790 | int sense_deferred = 0, level = 0; |
b60af5b0 AS |
791 | enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY, |
792 | ACTION_DELAYED_RETRY} action; | |
ee60b2c5 | 793 | unsigned long wait_for = (cmd->allowed + 1) * req->timeout; |
1da177e4 | 794 | |
1da177e4 LT |
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 | } | |
631c228c | 800 | |
57292b58 | 801 | if (blk_rq_is_passthrough(req)) { |
1da177e4 | 802 | if (result) { |
82ed4db4 | 803 | if (sense_valid) { |
1da177e4 LT |
804 | /* |
805 | * SG_IO wants current and deferred errors | |
806 | */ | |
82ed4db4 CH |
807 | scsi_req(req)->sense_len = |
808 | min(8 + cmd->sense_buffer[7], | |
809 | SCSI_SENSE_BUFFERSIZE); | |
1da177e4 | 810 | } |
fa8e36c3 | 811 | if (!sense_deferred) |
63583cca | 812 | error = __scsi_error_from_host_byte(cmd, result); |
b22f687d | 813 | } |
27c41973 MS |
814 | /* |
815 | * __scsi_error_from_host_byte may have reset the host_byte | |
816 | */ | |
817 | req->errors = cmd->result; | |
e6bb7a96 | 818 | |
82ed4db4 | 819 | scsi_req(req)->resid_len = scsi_get_resid(cmd); |
e6bb7a96 | 820 | |
6f9a35e2 | 821 | if (scsi_bidi_cmnd(cmd)) { |
e6bb7a96 FT |
822 | /* |
823 | * Bidi commands Must be complete as a whole, | |
824 | * both sides at once. | |
825 | */ | |
82ed4db4 | 826 | scsi_req(req->next_rq)->resid_len = scsi_in(cmd)->resid; |
f6d47e74 CH |
827 | if (scsi_end_request(req, 0, blk_rq_bytes(req), |
828 | blk_rq_bytes(req->next_rq))) | |
829 | BUG(); | |
6f9a35e2 BH |
830 | return; |
831 | } | |
89fb4cd1 JB |
832 | } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) { |
833 | /* | |
aebf526b | 834 | * Flush commands do not transfers any data, and thus cannot use |
89fb4cd1 JB |
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); | |
1da177e4 LT |
839 | } |
840 | ||
57292b58 | 841 | /* no bidi support for !blk_rq_is_passthrough yet */ |
33659ebb | 842 | BUG_ON(blk_bidi_rq(req)); |
30b0c37b | 843 | |
1da177e4 LT |
844 | /* |
845 | * Next deal with any sectors which we were able to correctly | |
846 | * handle. | |
847 | */ | |
91921e01 HR |
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)); | |
d6b0c537 | 851 | |
a9bddd74 | 852 | /* |
aebf526b CH |
853 | * Recovered errors need reporting, but they're always treated as |
854 | * success, so fiddle the result code here. For passthrough requests | |
a9bddd74 JB |
855 | * we already took a copy of the original into rq->errors which |
856 | * is what gets returned to the user | |
857 | */ | |
e7efe593 DG |
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 | ; | |
e8064021 | 865 | else if (!(req->rq_flags & RQF_QUIET)) |
d811b848 | 866 | scsi_print_sense(cmd); |
a9bddd74 | 867 | result = 0; |
aebf526b | 868 | /* for passthrough error may be set */ |
a9bddd74 JB |
869 | error = 0; |
870 | } | |
871 | ||
872 | /* | |
a621bac3 JB |
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. | |
d6b0c537 | 876 | */ |
a621bac3 JB |
877 | if (!(blk_rq_bytes(req) == 0 && error) && |
878 | !scsi_end_request(req, error, good_bytes, 0)) | |
f6d47e74 | 879 | return; |
bc85dc50 CH |
880 | |
881 | /* | |
882 | * Kill remainder if no retrys. | |
883 | */ | |
884 | if (error && scsi_noretry_cmd(cmd)) { | |
f6d47e74 CH |
885 | if (scsi_end_request(req, error, blk_rq_bytes(req), 0)) |
886 | BUG(); | |
887 | return; | |
bc85dc50 CH |
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. | |
d6b0c537 | 893 | */ |
bc85dc50 CH |
894 | if (result == 0) |
895 | goto requeue; | |
03aba2f7 | 896 | |
63583cca | 897 | error = __scsi_error_from_host_byte(cmd, result); |
3e695f89 | 898 | |
b60af5b0 AS |
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) { | |
1da177e4 LT |
906 | switch (sshdr.sense_key) { |
907 | case UNIT_ATTENTION: | |
908 | if (cmd->device->removable) { | |
03aba2f7 | 909 | /* Detected disc change. Set a bit |
1da177e4 LT |
910 | * and quietly refuse further access. |
911 | */ | |
912 | cmd->device->changed = 1; | |
b60af5b0 | 913 | action = ACTION_FAIL; |
1da177e4 | 914 | } else { |
03aba2f7 LT |
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 | |
b60af5b0 | 918 | * command and see what happens. |
03aba2f7 | 919 | */ |
b60af5b0 | 920 | action = ACTION_RETRY; |
1da177e4 LT |
921 | } |
922 | break; | |
923 | case ILLEGAL_REQUEST: | |
03aba2f7 LT |
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 | */ | |
26a68019 JA |
932 | if ((cmd->device->use_10_for_rw && |
933 | sshdr.asc == 0x20 && sshdr.ascq == 0x00) && | |
1da177e4 LT |
934 | (cmd->cmnd[0] == READ_10 || |
935 | cmd->cmnd[0] == WRITE_10)) { | |
b60af5b0 | 936 | /* This will issue a new 6-byte command. */ |
1da177e4 | 937 | cmd->device->use_10_for_rw = 0; |
b60af5b0 | 938 | action = ACTION_REPREP; |
3e695f89 | 939 | } else if (sshdr.asc == 0x10) /* DIX */ { |
3e695f89 MP |
940 | action = ACTION_FAIL; |
941 | error = -EILSEQ; | |
c98a0eb0 | 942 | /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */ |
5db44863 | 943 | } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) { |
c98a0eb0 | 944 | action = ACTION_FAIL; |
66a651aa | 945 | error = -EREMOTEIO; |
b60af5b0 AS |
946 | } else |
947 | action = ACTION_FAIL; | |
948 | break; | |
511e44f4 | 949 | case ABORTED_COMMAND: |
126c0982 | 950 | action = ACTION_FAIL; |
e6c11dbb | 951 | if (sshdr.asc == 0x10) /* DIF */ |
3e695f89 | 952 | error = -EILSEQ; |
1da177e4 LT |
953 | break; |
954 | case NOT_READY: | |
03aba2f7 | 955 | /* If the device is in the process of becoming |
f3e93f73 | 956 | * ready, or has a temporary blockage, retry. |
1da177e4 | 957 | */ |
f3e93f73 JB |
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 */ | |
d8705f11 | 967 | case 0x14: /* space allocation in progress */ |
b60af5b0 | 968 | action = ACTION_DELAYED_RETRY; |
f3e93f73 | 969 | break; |
3dbf6a54 | 970 | default: |
3dbf6a54 AS |
971 | action = ACTION_FAIL; |
972 | break; | |
f3e93f73 | 973 | } |
e6c11dbb | 974 | } else |
b60af5b0 | 975 | action = ACTION_FAIL; |
b60af5b0 | 976 | break; |
1da177e4 | 977 | case VOLUME_OVERFLOW: |
03aba2f7 | 978 | /* See SSC3rXX or current. */ |
b60af5b0 AS |
979 | action = ACTION_FAIL; |
980 | break; | |
1da177e4 | 981 | default: |
b60af5b0 | 982 | action = ACTION_FAIL; |
1da177e4 LT |
983 | break; |
984 | } | |
e6c11dbb | 985 | } else |
b60af5b0 | 986 | action = ACTION_FAIL; |
b60af5b0 | 987 | |
ee60b2c5 | 988 | if (action != ACTION_FAIL && |
e6c11dbb | 989 | time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) |
ee60b2c5 | 990 | action = ACTION_FAIL; |
ee60b2c5 | 991 | |
b60af5b0 AS |
992 | switch (action) { |
993 | case ACTION_FAIL: | |
994 | /* Give up and fail the remainder of the request */ | |
e8064021 | 995 | if (!(req->rq_flags & RQF_QUIET)) { |
f1569ff1 HR |
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 | } | |
3173d8c3 | 1014 | } |
f6d47e74 CH |
1015 | if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0)) |
1016 | return; | |
bc85dc50 | 1017 | /*FALLTHRU*/ |
b60af5b0 | 1018 | case ACTION_REPREP: |
bc85dc50 | 1019 | requeue: |
b60af5b0 AS |
1020 | /* Unprep the request and put it back at the head of the queue. |
1021 | * A new command will be prepared and issued. | |
1022 | */ | |
d285203c | 1023 | if (q->mq_ops) { |
e8064021 | 1024 | cmd->request->rq_flags &= ~RQF_DONTPREP; |
d285203c CH |
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 | } | |
b60af5b0 AS |
1031 | break; |
1032 | case ACTION_RETRY: | |
1033 | /* Retry the same command immediately */ | |
4f5299ac | 1034 | __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0); |
b60af5b0 AS |
1035 | break; |
1036 | case ACTION_DELAYED_RETRY: | |
1037 | /* Retry the same command after a delay */ | |
4f5299ac | 1038 | __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0); |
b60af5b0 | 1039 | break; |
1da177e4 LT |
1040 | } |
1041 | } | |
1da177e4 | 1042 | |
3c356bde | 1043 | static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb) |
1da177e4 | 1044 | { |
6f9a35e2 | 1045 | int count; |
1da177e4 LT |
1046 | |
1047 | /* | |
3b003157 | 1048 | * If sg table allocation fails, requeue request later. |
1da177e4 | 1049 | */ |
f9d03f96 CH |
1050 | if (unlikely(sg_alloc_table_chained(&sdb->table, |
1051 | blk_rq_nr_phys_segments(req), sdb->table.sgl))) | |
1da177e4 | 1052 | return BLKPREP_DEFER; |
1da177e4 | 1053 | |
1da177e4 LT |
1054 | /* |
1055 | * Next, walk the list, and fill in the addresses and sizes of | |
1056 | * each segment. | |
1057 | */ | |
30b0c37b BH |
1058 | count = blk_rq_map_sg(req->q, req, sdb->table.sgl); |
1059 | BUG_ON(count > sdb->table.nents); | |
1060 | sdb->table.nents = count; | |
fd102b12 | 1061 | sdb->length = blk_rq_payload_bytes(req); |
4a03d90e | 1062 | return BLKPREP_OK; |
1da177e4 | 1063 | } |
6f9a35e2 BH |
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 | */ | |
3c356bde | 1076 | int scsi_init_io(struct scsi_cmnd *cmd) |
6f9a35e2 | 1077 | { |
5e012aad | 1078 | struct scsi_device *sdev = cmd->device; |
13f05c8d | 1079 | struct request *rq = cmd->request; |
d285203c | 1080 | bool is_mq = (rq->mq_ctx != NULL); |
635d98b1 | 1081 | int error; |
13f05c8d | 1082 | |
fd3fc0b4 JT |
1083 | if (WARN_ON_ONCE(!blk_rq_nr_phys_segments(rq))) |
1084 | return -EINVAL; | |
635d98b1 | 1085 | |
3c356bde | 1086 | error = scsi_init_sgtable(rq, &cmd->sdb); |
6f9a35e2 BH |
1087 | if (error) |
1088 | goto err_exit; | |
1089 | ||
13f05c8d | 1090 | if (blk_bidi_rq(rq)) { |
d285203c CH |
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; | |
6f9a35e2 BH |
1100 | } |
1101 | ||
3c356bde | 1102 | error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special); |
6f9a35e2 BH |
1103 | if (error) |
1104 | goto err_exit; | |
1105 | } | |
1106 | ||
13f05c8d | 1107 | if (blk_integrity_rq(rq)) { |
7027ad72 MP |
1108 | struct scsi_data_buffer *prot_sdb = cmd->prot_sdb; |
1109 | int ivecs, count; | |
1110 | ||
91724c20 EM |
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 | ||
13f05c8d | 1122 | ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio); |
7027ad72 | 1123 | |
001d63be | 1124 | if (sg_alloc_table_chained(&prot_sdb->table, ivecs, |
22cc3d4c | 1125 | prot_sdb->table.sgl)) { |
7027ad72 MP |
1126 | error = BLKPREP_DEFER; |
1127 | goto err_exit; | |
1128 | } | |
1129 | ||
13f05c8d | 1130 | count = blk_rq_map_integrity_sg(rq->q, rq->bio, |
7027ad72 MP |
1131 | prot_sdb->table.sgl); |
1132 | BUG_ON(unlikely(count > ivecs)); | |
13f05c8d | 1133 | BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q))); |
7027ad72 MP |
1134 | |
1135 | cmd->prot_sdb = prot_sdb; | |
1136 | cmd->prot_sdb->table.nents = count; | |
1137 | } | |
1138 | ||
d285203c | 1139 | return BLKPREP_OK; |
6f9a35e2 | 1140 | err_exit: |
d285203c CH |
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 | } | |
6f9a35e2 BH |
1149 | return error; |
1150 | } | |
bb52d82f | 1151 | EXPORT_SYMBOL(scsi_init_io); |
1da177e4 | 1152 | |
e9c787e6 | 1153 | void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd) |
3b003157 | 1154 | { |
e9c787e6 CH |
1155 | void *buf = cmd->sense_buffer; |
1156 | void *prot = cmd->prot_sdb; | |
1157 | unsigned long flags; | |
3b003157 | 1158 | |
82ed4db4 CH |
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)); | |
3b003157 | 1162 | |
e9c787e6 CH |
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; | |
64a87b24 | 1168 | |
e9c787e6 CH |
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); | |
3b003157 CH |
1172 | } |
1173 | ||
aebf526b | 1174 | static int scsi_setup_scsi_cmnd(struct scsi_device *sdev, struct request *req) |
7b16318d | 1175 | { |
a1b73fc1 | 1176 | struct scsi_cmnd *cmd = req->special; |
3b003157 CH |
1177 | |
1178 | /* | |
aebf526b | 1179 | * Passthrough requests may transfer data, in which case they must |
3b003157 CH |
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) { | |
3c356bde | 1185 | int ret = scsi_init_io(cmd); |
3b003157 CH |
1186 | if (unlikely(ret)) |
1187 | return ret; | |
1188 | } else { | |
b0790410 | 1189 | BUG_ON(blk_rq_bytes(req)); |
3b003157 | 1190 | |
30b0c37b | 1191 | memset(&cmd->sdb, 0, sizeof(cmd->sdb)); |
3b003157 | 1192 | } |
7b16318d | 1193 | |
82ed4db4 CH |
1194 | cmd->cmd_len = scsi_req(req)->cmd_len; |
1195 | cmd->cmnd = scsi_req(req)->cmd; | |
b0790410 | 1196 | cmd->transfersize = blk_rq_bytes(req); |
7b16318d | 1197 | cmd->allowed = req->retries; |
3b003157 | 1198 | return BLKPREP_OK; |
7b16318d | 1199 | } |
7b16318d | 1200 | |
3b003157 | 1201 | /* |
aebf526b | 1202 | * Setup a normal block command. These are simple request from filesystems |
3868cf8e | 1203 | * that still need to be translated to SCSI CDBs from the ULD. |
3b003157 | 1204 | */ |
3868cf8e | 1205 | static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req) |
1da177e4 | 1206 | { |
a1b73fc1 | 1207 | struct scsi_cmnd *cmd = req->special; |
a6a8d9f8 | 1208 | |
ee14c674 CH |
1209 | if (unlikely(sdev->handler && sdev->handler->prep_fn)) { |
1210 | int ret = sdev->handler->prep_fn(sdev, req); | |
a6a8d9f8 CS |
1211 | if (ret != BLKPREP_OK) |
1212 | return ret; | |
1213 | } | |
1214 | ||
82ed4db4 | 1215 | cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd; |
64a87b24 | 1216 | memset(cmd->cmnd, 0, BLK_MAX_CDB); |
3868cf8e | 1217 | return scsi_cmd_to_driver(cmd)->init_command(cmd); |
3b003157 CH |
1218 | } |
1219 | ||
6af7a4ff CH |
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 | ||
aebf526b CH |
1231 | if (blk_rq_is_scsi(req)) |
1232 | return scsi_setup_scsi_cmnd(sdev, req); | |
1233 | else | |
6af7a4ff | 1234 | return scsi_setup_fs_cmnd(sdev, req); |
6af7a4ff CH |
1235 | } |
1236 | ||
a1b73fc1 CH |
1237 | static int |
1238 | scsi_prep_state_check(struct scsi_device *sdev, struct request *req) | |
3b003157 | 1239 | { |
3b003157 CH |
1240 | int ret = BLKPREP_OK; |
1241 | ||
1da177e4 | 1242 | /* |
3b003157 CH |
1243 | * If the device is not in running state we will reject some |
1244 | * or all commands. | |
1da177e4 | 1245 | */ |
3b003157 CH |
1246 | if (unlikely(sdev->sdev_state != SDEV_RUNNING)) { |
1247 | switch (sdev->sdev_state) { | |
1248 | case SDEV_OFFLINE: | |
1b8d2620 | 1249 | case SDEV_TRANSPORT_OFFLINE: |
3b003157 CH |
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 | */ | |
9ccfc756 | 1264 | sdev_printk(KERN_ERR, sdev, |
3b003157 CH |
1265 | "rejecting I/O to dead device\n"); |
1266 | ret = BLKPREP_KILL; | |
1267 | break; | |
3b003157 | 1268 | case SDEV_BLOCK: |
6f4267e3 | 1269 | case SDEV_CREATED_BLOCK: |
bba0bdd7 BVA |
1270 | ret = BLKPREP_DEFER; |
1271 | break; | |
1272 | case SDEV_QUIESCE: | |
3b003157 CH |
1273 | /* |
1274 | * If the devices is blocked we defer normal commands. | |
1275 | */ | |
e8064021 | 1276 | if (!(req->rq_flags & RQF_PREEMPT)) |
3b003157 CH |
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 | */ | |
e8064021 | 1285 | if (!(req->rq_flags & RQF_PREEMPT)) |
3b003157 CH |
1286 | ret = BLKPREP_KILL; |
1287 | break; | |
1da177e4 | 1288 | } |
1da177e4 | 1289 | } |
7f9a6bc4 JB |
1290 | return ret; |
1291 | } | |
1da177e4 | 1292 | |
a1b73fc1 CH |
1293 | static int |
1294 | scsi_prep_return(struct request_queue *q, struct request *req, int ret) | |
7f9a6bc4 JB |
1295 | { |
1296 | struct scsi_device *sdev = q->queuedata; | |
1da177e4 | 1297 | |
3b003157 CH |
1298 | switch (ret) { |
1299 | case BLKPREP_KILL: | |
e1cd3911 | 1300 | case BLKPREP_INVALID: |
3b003157 | 1301 | req->errors = DID_NO_CONNECT << 16; |
7f9a6bc4 JB |
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); | |
68c03d91 | 1307 | put_device(&sdev->sdev_gendev); |
7f9a6bc4 JB |
1308 | req->special = NULL; |
1309 | } | |
3b003157 CH |
1310 | break; |
1311 | case BLKPREP_DEFER: | |
1da177e4 | 1312 | /* |
9934c8c0 | 1313 | * If we defer, the blk_peek_request() returns NULL, but the |
a488e749 JA |
1314 | * queue must be restarted, so we schedule a callback to happen |
1315 | * shortly. | |
1da177e4 | 1316 | */ |
71e75c97 | 1317 | if (atomic_read(&sdev->device_busy) == 0) |
a488e749 | 1318 | blk_delay_queue(q, SCSI_QUEUE_DELAY); |
3b003157 CH |
1319 | break; |
1320 | default: | |
e8064021 | 1321 | req->rq_flags |= RQF_DONTPREP; |
1da177e4 LT |
1322 | } |
1323 | ||
3b003157 | 1324 | return ret; |
1da177e4 | 1325 | } |
7f9a6bc4 | 1326 | |
a1b73fc1 | 1327 | static int scsi_prep_fn(struct request_queue *q, struct request *req) |
7f9a6bc4 JB |
1328 | { |
1329 | struct scsi_device *sdev = q->queuedata; | |
e9c787e6 | 1330 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); |
a1b73fc1 CH |
1331 | int ret; |
1332 | ||
1333 | ret = scsi_prep_state_check(sdev, req); | |
1334 | if (ret != BLKPREP_OK) | |
1335 | goto out; | |
1336 | ||
e9c787e6 CH |
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; | |
a1b73fc1 | 1346 | } |
7f9a6bc4 | 1347 | |
e9c787e6 CH |
1348 | cmd->tag = req->tag; |
1349 | cmd->request = req; | |
e9c787e6 CH |
1350 | cmd->prot_op = SCSI_PROT_NORMAL; |
1351 | ||
6af7a4ff | 1352 | ret = scsi_setup_cmnd(sdev, req); |
a1b73fc1 | 1353 | out: |
7f9a6bc4 JB |
1354 | return scsi_prep_return(q, req, ret); |
1355 | } | |
a1b73fc1 CH |
1356 | |
1357 | static void scsi_unprep_fn(struct request_queue *q, struct request *req) | |
1358 | { | |
d285203c | 1359 | scsi_uninit_cmd(req->special); |
a1b73fc1 | 1360 | } |
1da177e4 LT |
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 | { | |
71e75c97 CH |
1371 | unsigned int busy; |
1372 | ||
1373 | busy = atomic_inc_return(&sdev->device_busy) - 1; | |
cd9070c9 | 1374 | if (atomic_read(&sdev->device_blocked)) { |
71e75c97 CH |
1375 | if (busy) |
1376 | goto out_dec; | |
1377 | ||
1da177e4 LT |
1378 | /* |
1379 | * unblock after device_blocked iterates to zero | |
1380 | */ | |
cd9070c9 | 1381 | if (atomic_dec_return(&sdev->device_blocked) > 0) { |
d285203c CH |
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); | |
71e75c97 | 1387 | goto out_dec; |
1da177e4 | 1388 | } |
71e75c97 CH |
1389 | SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev, |
1390 | "unblocking device at zero depth\n")); | |
1da177e4 | 1391 | } |
71e75c97 CH |
1392 | |
1393 | if (busy >= sdev->queue_depth) | |
1394 | goto out_dec; | |
1da177e4 LT |
1395 | |
1396 | return 1; | |
71e75c97 CH |
1397 | out_dec: |
1398 | atomic_dec(&sdev->device_busy); | |
1399 | return 0; | |
1da177e4 LT |
1400 | } |
1401 | ||
f0c0a376 MC |
1402 | /* |
1403 | * scsi_target_queue_ready: checks if there we can send commands to target | |
1404 | * @sdev: scsi device on starget to check. | |
f0c0a376 MC |
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); | |
7ae65c0f | 1410 | unsigned int busy; |
f0c0a376 MC |
1411 | |
1412 | if (starget->single_lun) { | |
7ae65c0f | 1413 | spin_lock_irq(shost->host_lock); |
f0c0a376 | 1414 | if (starget->starget_sdev_user && |
7ae65c0f CH |
1415 | starget->starget_sdev_user != sdev) { |
1416 | spin_unlock_irq(shost->host_lock); | |
1417 | return 0; | |
1418 | } | |
f0c0a376 | 1419 | starget->starget_sdev_user = sdev; |
7ae65c0f | 1420 | spin_unlock_irq(shost->host_lock); |
f0c0a376 MC |
1421 | } |
1422 | ||
2ccbb008 CH |
1423 | if (starget->can_queue <= 0) |
1424 | return 1; | |
1425 | ||
7ae65c0f | 1426 | busy = atomic_inc_return(&starget->target_busy) - 1; |
cd9070c9 | 1427 | if (atomic_read(&starget->target_blocked) > 0) { |
7ae65c0f CH |
1428 | if (busy) |
1429 | goto starved; | |
1430 | ||
f0c0a376 MC |
1431 | /* |
1432 | * unblock after target_blocked iterates to zero | |
1433 | */ | |
cd9070c9 | 1434 | if (atomic_dec_return(&starget->target_blocked) > 0) |
7ae65c0f | 1435 | goto out_dec; |
cf68d334 CH |
1436 | |
1437 | SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget, | |
1438 | "unblocking target at zero depth\n")); | |
f0c0a376 MC |
1439 | } |
1440 | ||
2ccbb008 | 1441 | if (busy >= starget->can_queue) |
7ae65c0f | 1442 | goto starved; |
f0c0a376 | 1443 | |
7ae65c0f CH |
1444 | return 1; |
1445 | ||
1446 | starved: | |
1447 | spin_lock_irq(shost->host_lock); | |
1448 | list_move_tail(&sdev->starved_entry, &shost->starved_list); | |
cf68d334 | 1449 | spin_unlock_irq(shost->host_lock); |
7ae65c0f | 1450 | out_dec: |
2ccbb008 CH |
1451 | if (starget->can_queue > 0) |
1452 | atomic_dec(&starget->target_busy); | |
7ae65c0f | 1453 | return 0; |
f0c0a376 MC |
1454 | } |
1455 | ||
1da177e4 LT |
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. | |
1da177e4 LT |
1460 | */ |
1461 | static inline int scsi_host_queue_ready(struct request_queue *q, | |
1462 | struct Scsi_Host *shost, | |
1463 | struct scsi_device *sdev) | |
1464 | { | |
74665016 | 1465 | unsigned int busy; |
cf68d334 | 1466 | |
939647ee | 1467 | if (scsi_host_in_recovery(shost)) |
74665016 CH |
1468 | return 0; |
1469 | ||
1470 | busy = atomic_inc_return(&shost->host_busy) - 1; | |
cd9070c9 | 1471 | if (atomic_read(&shost->host_blocked) > 0) { |
74665016 CH |
1472 | if (busy) |
1473 | goto starved; | |
1474 | ||
1da177e4 LT |
1475 | /* |
1476 | * unblock after host_blocked iterates to zero | |
1477 | */ | |
cd9070c9 | 1478 | if (atomic_dec_return(&shost->host_blocked) > 0) |
74665016 | 1479 | goto out_dec; |
cf68d334 CH |
1480 | |
1481 | SCSI_LOG_MLQUEUE(3, | |
1482 | shost_printk(KERN_INFO, shost, | |
1483 | "unblocking host at zero depth\n")); | |
1da177e4 | 1484 | } |
74665016 CH |
1485 | |
1486 | if (shost->can_queue > 0 && busy >= shost->can_queue) | |
1487 | goto starved; | |
1488 | if (shost->host_self_blocked) | |
1489 | goto starved; | |
1da177e4 LT |
1490 | |
1491 | /* We're OK to process the command, so we can't be starved */ | |
74665016 CH |
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 | } | |
1da177e4 | 1498 | |
74665016 CH |
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); | |
cf68d334 | 1505 | spin_unlock_irq(shost->host_lock); |
74665016 CH |
1506 | out_dec: |
1507 | atomic_dec(&shost->host_busy); | |
1508 | return 0; | |
1da177e4 LT |
1509 | } |
1510 | ||
6c5121b7 KU |
1511 | /* |
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 | * | |
67bd9413 BVA |
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. | |
6c5121b7 KU |
1522 | */ |
1523 | static int scsi_lld_busy(struct request_queue *q) | |
1524 | { | |
1525 | struct scsi_device *sdev = q->queuedata; | |
1526 | struct Scsi_Host *shost; | |
6c5121b7 | 1527 | |
3f3299d5 | 1528 | if (blk_queue_dying(q)) |
6c5121b7 KU |
1529 | return 0; |
1530 | ||
1531 | shost = sdev->host; | |
6c5121b7 | 1532 | |
b7e94a16 JN |
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)) | |
6c5121b7 KU |
1540 | return 1; |
1541 | ||
1542 | return 0; | |
1543 | } | |
1544 | ||
1da177e4 | 1545 | /* |
e91442b6 | 1546 | * Kill a request for a dead device |
1da177e4 | 1547 | */ |
165125e1 | 1548 | static void scsi_kill_request(struct request *req, struct request_queue *q) |
1da177e4 | 1549 | { |
e91442b6 | 1550 | struct scsi_cmnd *cmd = req->special; |
03b14708 JS |
1551 | struct scsi_device *sdev; |
1552 | struct scsi_target *starget; | |
1553 | struct Scsi_Host *shost; | |
1da177e4 | 1554 | |
9934c8c0 | 1555 | blk_start_request(req); |
788ce43a | 1556 | |
74571813 HR |
1557 | scmd_printk(KERN_INFO, cmd, "killing request\n"); |
1558 | ||
03b14708 JS |
1559 | sdev = cmd->device; |
1560 | starget = scsi_target(sdev); | |
1561 | shost = sdev->host; | |
e91442b6 JB |
1562 | scsi_init_cmd_errh(cmd); |
1563 | cmd->result = DID_NO_CONNECT << 16; | |
1564 | atomic_inc(&cmd->device->iorequest_cnt); | |
e36e0c80 TH |
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 | */ | |
71e75c97 | 1571 | atomic_inc(&sdev->device_busy); |
74665016 | 1572 | atomic_inc(&shost->host_busy); |
2ccbb008 CH |
1573 | if (starget->can_queue > 0) |
1574 | atomic_inc(&starget->target_busy); | |
e36e0c80 | 1575 | |
242f9dcb | 1576 | blk_complete_request(req); |
1da177e4 LT |
1577 | } |
1578 | ||
1aea6434 JA |
1579 | static void scsi_softirq_done(struct request *rq) |
1580 | { | |
242f9dcb JA |
1581 | struct scsi_cmnd *cmd = rq->special; |
1582 | unsigned long wait_for = (cmd->allowed + 1) * rq->timeout; | |
1aea6434 JA |
1583 | int disposition; |
1584 | ||
1585 | INIT_LIST_HEAD(&cmd->eh_entry); | |
1586 | ||
242f9dcb JA |
1587 | atomic_inc(&cmd->device->iodone_cnt); |
1588 | if (cmd->result) | |
1589 | atomic_inc(&cmd->device->ioerr_cnt); | |
1590 | ||
1aea6434 JA |
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 | } | |
91921e01 | 1599 | |
1aea6434 JA |
1600 | scsi_log_completion(cmd, disposition); |
1601 | ||
1602 | switch (disposition) { | |
1603 | case SUCCESS: | |
1604 | scsi_finish_command(cmd); | |
1605 | break; | |
1606 | case NEEDS_RETRY: | |
596f482a | 1607 | scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY); |
1aea6434 JA |
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 | ||
82042a2c CH |
1618 | /** |
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 | ||
3b5382c4 CH |
1699 | /** |
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 | ||
1da177e4 LT |
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) | |
613be1f6 BVA |
1728 | __releases(q->queue_lock) |
1729 | __acquires(q->queue_lock) | |
1da177e4 LT |
1730 | { |
1731 | struct scsi_device *sdev = q->queuedata; | |
1732 | struct Scsi_Host *shost; | |
1733 | struct scsi_cmnd *cmd; | |
1734 | struct request *req; | |
1735 | ||
1da177e4 LT |
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; | |
a488e749 | 1741 | for (;;) { |
1da177e4 LT |
1742 | int rtn; |
1743 | /* | |
1744 | * get next queueable request. We do this early to make sure | |
91921e01 | 1745 | * that the request is fully prepared even if we cannot |
1da177e4 LT |
1746 | * accept it. |
1747 | */ | |
9934c8c0 | 1748 | req = blk_peek_request(q); |
71e75c97 | 1749 | if (!req) |
1da177e4 LT |
1750 | break; |
1751 | ||
1752 | if (unlikely(!scsi_device_online(sdev))) { | |
9ccfc756 JB |
1753 | sdev_printk(KERN_ERR, sdev, |
1754 | "rejecting I/O to offline device\n"); | |
e91442b6 | 1755 | scsi_kill_request(req, q); |
1da177e4 LT |
1756 | continue; |
1757 | } | |
1758 | ||
71e75c97 CH |
1759 | if (!scsi_dev_queue_ready(q, sdev)) |
1760 | break; | |
1da177e4 LT |
1761 | |
1762 | /* | |
1763 | * Remove the request from the request list. | |
1764 | */ | |
1765 | if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req))) | |
9934c8c0 | 1766 | blk_start_request(req); |
1da177e4 | 1767 | |
cf68d334 | 1768 | spin_unlock_irq(q->queue_lock); |
e91442b6 JB |
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 " | |
4aff5e23 | 1773 | "linux-scsi@vger.kernel.org\n", |
cadbd4a5 | 1774 | __func__); |
4aff5e23 | 1775 | blk_dump_rq_flags(req, "foo"); |
e91442b6 JB |
1776 | BUG(); |
1777 | } | |
1da177e4 | 1778 | |
ecefe8a9 MC |
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 | */ | |
e8064021 | 1787 | if (blk_queue_tagged(q) && !(req->rq_flags & RQF_QUEUED)) { |
cf68d334 | 1788 | spin_lock_irq(shost->host_lock); |
ecefe8a9 MC |
1789 | if (list_empty(&sdev->starved_entry)) |
1790 | list_add_tail(&sdev->starved_entry, | |
1791 | &shost->starved_list); | |
cf68d334 | 1792 | spin_unlock_irq(shost->host_lock); |
ecefe8a9 MC |
1793 | goto not_ready; |
1794 | } | |
1795 | ||
f0c0a376 MC |
1796 | if (!scsi_target_queue_ready(shost, sdev)) |
1797 | goto not_ready; | |
1798 | ||
1da177e4 | 1799 | if (!scsi_host_queue_ready(q, shost, sdev)) |
cf68d334 | 1800 | goto host_not_ready; |
125c99bc CH |
1801 | |
1802 | if (sdev->simple_tags) | |
1803 | cmd->flags |= SCMD_TAGGED; | |
1804 | else | |
1805 | cmd->flags &= ~SCMD_TAGGED; | |
1da177e4 | 1806 | |
1da177e4 LT |
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 | */ | |
3b5382c4 | 1816 | cmd->scsi_done = scsi_done; |
1da177e4 | 1817 | rtn = scsi_dispatch_cmd(cmd); |
d0d3bbf9 CH |
1818 | if (rtn) { |
1819 | scsi_queue_insert(cmd, rtn); | |
1820 | spin_lock_irq(q->queue_lock); | |
a488e749 | 1821 | goto out_delay; |
d0d3bbf9 CH |
1822 | } |
1823 | spin_lock_irq(q->queue_lock); | |
1da177e4 LT |
1824 | } |
1825 | ||
613be1f6 | 1826 | return; |
1da177e4 | 1827 | |
cf68d334 | 1828 | host_not_ready: |
2ccbb008 CH |
1829 | if (scsi_target(sdev)->can_queue > 0) |
1830 | atomic_dec(&scsi_target(sdev)->target_busy); | |
cf68d334 | 1831 | not_ready: |
1da177e4 LT |
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); | |
71e75c97 | 1842 | atomic_dec(&sdev->device_busy); |
a488e749 | 1843 | out_delay: |
480cadc2 | 1844 | if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev)) |
a488e749 | 1845 | blk_delay_queue(q, SCSI_QUEUE_DELAY); |
1da177e4 LT |
1846 | } |
1847 | ||
d285203c CH |
1848 | static inline int prep_to_mq(int ret) |
1849 | { | |
1850 | switch (ret) { | |
1851 | case BLKPREP_OK: | |
2868f13c | 1852 | return BLK_MQ_RQ_QUEUE_OK; |
d285203c CH |
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 | ||
82ed4db4 CH |
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)); | |
d285203c CH |
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 | ||
d285203c CH |
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 | ||
64bdcbc4 KD |
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 | } | |
d285203c CH |
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 + | |
120bb3e1 | 1897 | min_t(unsigned int, |
65e8617f | 1898 | shost->sg_tablesize, SG_CHUNK_SIZE) * |
120bb3e1 | 1899 | sizeof(struct scatterlist); |
d285203c CH |
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 | ||
fe052529 CH |
1917 | blk_mq_start_request(req); |
1918 | ||
d285203c CH |
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); | |
f4829a9b | 1925 | blk_mq_complete_request(cmd->request, cmd->request->errors); |
d285203c CH |
1926 | } |
1927 | ||
74c45052 JA |
1928 | static int scsi_queue_rq(struct blk_mq_hw_ctx *hctx, |
1929 | const struct blk_mq_queue_data *bd) | |
d285203c | 1930 | { |
74c45052 | 1931 | struct request *req = bd->rq; |
d285203c CH |
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)); | |
2868f13c | 1940 | if (ret != BLK_MQ_RQ_QUEUE_OK) |
d285203c CH |
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 | ||
e8064021 | 1954 | if (!(req->rq_flags & RQF_DONTPREP)) { |
d285203c | 1955 | ret = prep_to_mq(scsi_mq_prep_fn(req)); |
2868f13c | 1956 | if (ret != BLK_MQ_RQ_QUEUE_OK) |
d285203c | 1957 | goto out_dec_host_busy; |
e8064021 | 1958 | req->rq_flags |= RQF_DONTPREP; |
fe052529 CH |
1959 | } else { |
1960 | blk_mq_start_request(req); | |
d285203c CH |
1961 | } |
1962 | ||
125c99bc CH |
1963 | if (sdev->simple_tags) |
1964 | cmd->flags |= SCMD_TAGGED; | |
b1dd2aac | 1965 | else |
125c99bc | 1966 | cmd->flags &= ~SCMD_TAGGED; |
b1dd2aac | 1967 | |
d285203c CH |
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: | |
d285203c CH |
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 | */ | |
e8064021 | 2002 | if (req->rq_flags & RQF_DONTPREP) |
d285203c CH |
2003 | scsi_mq_uninit_cmd(cmd); |
2004 | break; | |
2005 | default: | |
2006 | break; | |
2007 | } | |
2008 | return ret; | |
2009 | } | |
2010 | ||
0152fb6b CH |
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 | ||
d285203c CH |
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 | { | |
0a6ac4ee | 2023 | struct Scsi_Host *shost = data; |
d285203c CH |
2024 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
2025 | ||
0a6ac4ee CH |
2026 | cmd->sense_buffer = |
2027 | scsi_alloc_sense_buffer(shost, GFP_KERNEL, numa_node); | |
d285203c CH |
2028 | if (!cmd->sense_buffer) |
2029 | return -ENOMEM; | |
82ed4db4 | 2030 | cmd->req.sense = cmd->sense_buffer; |
d285203c CH |
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 | { | |
0a6ac4ee | 2037 | struct Scsi_Host *shost = data; |
d285203c CH |
2038 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
2039 | ||
0a6ac4ee | 2040 | scsi_free_sense_buffer(shost, cmd->sense_buffer); |
d285203c CH |
2041 | } |
2042 | ||
2d9c5c20 CH |
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 | ||
f1bea55d | 2052 | static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost) |
1da177e4 LT |
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) | |
e83b3664 | 2068 | bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT; |
1da177e4 LT |
2069 | |
2070 | return bounce_limit; | |
2071 | } | |
1da177e4 | 2072 | |
d48777a6 | 2073 | void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q) |
1da177e4 | 2074 | { |
6f381fa3 | 2075 | struct device *dev = shost->dma_dev; |
1da177e4 | 2076 | |
a8474ce2 JA |
2077 | /* |
2078 | * this limit is imposed by hardware restrictions | |
2079 | */ | |
8a78362c | 2080 | blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize, |
65e8617f | 2081 | SG_MAX_SEGMENTS)); |
a8474ce2 | 2082 | |
13f05c8d MP |
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 | ||
086fa5ff | 2091 | blk_queue_max_hw_sectors(q, shost->max_sectors); |
1da177e4 LT |
2092 | blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost)); |
2093 | blk_queue_segment_boundary(q, shost->dma_boundary); | |
99c84dbd | 2094 | dma_set_seg_boundary(dev, shost->dma_boundary); |
1da177e4 | 2095 | |
860ac568 FT |
2096 | blk_queue_max_segment_size(q, dma_get_max_seg_size(dev)); |
2097 | ||
1da177e4 | 2098 | if (!shost->use_clustering) |
e692cb66 | 2099 | q->limits.cluster = 0; |
465ff318 JB |
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); | |
d285203c | 2107 | } |
d48777a6 | 2108 | EXPORT_SYMBOL_GPL(__scsi_init_queue); |
465ff318 | 2109 | |
e9c787e6 | 2110 | static int scsi_init_rq(struct request_queue *q, struct request *rq, gfp_t gfp) |
d285203c | 2111 | { |
e9c787e6 CH |
2112 | struct Scsi_Host *shost = q->rq_alloc_data; |
2113 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); | |
d285203c | 2114 | |
e9c787e6 CH |
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; | |
82ed4db4 | 2120 | cmd->req.sense = cmd->sense_buffer; |
e9c787e6 CH |
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; | |
2134 | } | |
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); | |
1da177e4 | 2144 | } |
b58d9154 FT |
2145 | |
2146 | struct request_queue *scsi_alloc_queue(struct scsi_device *sdev) | |
2147 | { | |
e9c787e6 | 2148 | struct Scsi_Host *shost = sdev->host; |
b58d9154 FT |
2149 | struct request_queue *q; |
2150 | ||
e9c787e6 | 2151 | q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE); |
b58d9154 FT |
2152 | if (!q) |
2153 | return NULL; | |
e9c787e6 CH |
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; | |
2159 | ||
2160 | if (blk_init_allocated_queue(q) < 0) { | |
2161 | blk_cleanup_queue(q); | |
2162 | return NULL; | |
2163 | } | |
b58d9154 | 2164 | |
e9c787e6 | 2165 | __scsi_init_queue(shost, q); |
b58d9154 | 2166 | blk_queue_prep_rq(q, scsi_prep_fn); |
a1b73fc1 | 2167 | blk_queue_unprep_rq(q, scsi_unprep_fn); |
b58d9154 | 2168 | blk_queue_softirq_done(q, scsi_softirq_done); |
242f9dcb | 2169 | blk_queue_rq_timed_out(q, scsi_times_out); |
6c5121b7 | 2170 | blk_queue_lld_busy(q, scsi_lld_busy); |
b58d9154 FT |
2171 | return q; |
2172 | } | |
1da177e4 | 2173 | |
d285203c | 2174 | static struct blk_mq_ops scsi_mq_ops = { |
d285203c CH |
2175 | .queue_rq = scsi_queue_rq, |
2176 | .complete = scsi_softirq_done, | |
0152fb6b | 2177 | .timeout = scsi_timeout, |
d285203c CH |
2178 | .init_request = scsi_init_request, |
2179 | .exit_request = scsi_exit_request, | |
2d9c5c20 | 2180 | .map_queues = scsi_map_queues, |
d285203c CH |
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; | |
65e8617f ML |
2199 | if (tbl_size > SG_CHUNK_SIZE) |
2200 | tbl_size = SG_CHUNK_SIZE; | |
d285203c CH |
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; | |
efec4b90 | 2208 | shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1; |
d285203c CH |
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; | |
24391c0d SL |
2213 | shost->tag_set.flags |= |
2214 | BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy); | |
d285203c CH |
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 | ||
857de6e0 HR |
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 | ||
1da177e4 LT |
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 | { | |
6362abd3 MP |
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"); | |
f078727b | 2304 | return -ENOMEM; |
6f9a35e2 BH |
2305 | } |
2306 | ||
1da177e4 LT |
2307 | return 0; |
2308 | } | |
2309 | ||
2310 | void scsi_exit_queue(void) | |
2311 | { | |
0a6ac4ee CH |
2312 | kmem_cache_destroy(scsi_sense_cache); |
2313 | kmem_cache_destroy(scsi_sense_isadma_cache); | |
6362abd3 | 2314 | kmem_cache_destroy(scsi_sdb_cache); |
1da177e4 | 2315 | } |
5baba830 JB |
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 | |
eb44820c | 2328 | * @sshdr: place to put sense data (or NULL if no sense to be collected). |
5baba830 JB |
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, | |
f4f4e47e | 2388 | sshdr, timeout, retries, NULL); |
5baba830 JB |
2389 | kfree(real_buffer); |
2390 | return ret; | |
2391 | } | |
2392 | EXPORT_SYMBOL_GPL(scsi_mode_select); | |
2393 | ||
1da177e4 | 2394 | /** |
eb44820c | 2395 | * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary. |
1cf72699 | 2396 | * @sdev: SCSI device to be queried |
1da177e4 LT |
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 | |
eb44820c | 2404 | * @sshdr: place to put sense data (or NULL if no sense to be collected). |
1cf72699 | 2405 | * must be SCSI_SENSE_BUFFERSIZE big. |
1da177e4 LT |
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. | |
eb44820c | 2410 | */ |
1da177e4 | 2411 | int |
1cf72699 | 2412 | scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage, |
1da177e4 | 2413 | unsigned char *buffer, int len, int timeout, int retries, |
5baba830 JB |
2414 | struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) |
2415 | { | |
1da177e4 LT |
2416 | unsigned char cmd[12]; |
2417 | int use_10_for_ms; | |
2418 | int header_length; | |
0ae80ba9 | 2419 | int result, retry_count = retries; |
ea73a9f2 | 2420 | struct scsi_sense_hdr my_sshdr; |
1da177e4 LT |
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 | ||
ea73a9f2 JB |
2427 | /* caller might not be interested in sense, but we need it */ |
2428 | if (!sshdr) | |
2429 | sshdr = &my_sshdr; | |
2430 | ||
1da177e4 | 2431 | retry: |
1cf72699 | 2432 | use_10_for_ms = sdev->use_10_for_ms; |
1da177e4 LT |
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 | ||
1da177e4 LT |
2450 | memset(buffer, 0, len); |
2451 | ||
1cf72699 | 2452 | result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len, |
f4f4e47e | 2453 | sshdr, timeout, retries, NULL); |
1da177e4 LT |
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 | ||
1cf72699 JB |
2460 | if (use_10_for_ms && !scsi_status_is_good(result) && |
2461 | (driver_byte(result) & DRIVER_SENSE)) { | |
ea73a9f2 JB |
2462 | if (scsi_sense_valid(sshdr)) { |
2463 | if ((sshdr->sense_key == ILLEGAL_REQUEST) && | |
2464 | (sshdr->asc == 0x20) && (sshdr->ascq == 0)) { | |
1da177e4 LT |
2465 | /* |
2466 | * Invalid command operation code | |
2467 | */ | |
1cf72699 | 2468 | sdev->use_10_for_ms = 0; |
1da177e4 LT |
2469 | goto retry; |
2470 | } | |
2471 | } | |
2472 | } | |
2473 | ||
1cf72699 | 2474 | if(scsi_status_is_good(result)) { |
6d73c851 AV |
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) { | |
1da177e4 LT |
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 | } | |
6d73c851 | 2497 | data->header_length = header_length; |
0ae80ba9 HR |
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; | |
1da177e4 LT |
2503 | } |
2504 | ||
1cf72699 | 2505 | return result; |
1da177e4 LT |
2506 | } |
2507 | EXPORT_SYMBOL(scsi_mode_sense); | |
2508 | ||
001aac25 JB |
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 | |
74a78ebd | 2514 | * @sshdr: outpout pointer for decoded sense information. |
001aac25 JB |
2515 | * |
2516 | * Returns zero if unsuccessful or an error if TUR failed. For | |
9f8a2c23 | 2517 | * removable media, UNIT_ATTENTION sets ->changed flag. |
001aac25 | 2518 | **/ |
1da177e4 | 2519 | int |
001aac25 | 2520 | scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries, |
74a78ebd | 2521 | struct scsi_sense_hdr *sshdr) |
1da177e4 | 2522 | { |
1da177e4 LT |
2523 | char cmd[] = { |
2524 | TEST_UNIT_READY, 0, 0, 0, 0, 0, | |
2525 | }; | |
2526 | int result; | |
001aac25 | 2527 | |
001aac25 JB |
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, | |
f4f4e47e | 2531 | timeout, retries, NULL); |
32c356d7 JB |
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); | |
001aac25 | 2537 | |
1da177e4 LT |
2538 | return result; |
2539 | } | |
2540 | EXPORT_SYMBOL(scsi_test_unit_ready); | |
2541 | ||
2542 | /** | |
eb44820c | 2543 | * scsi_device_set_state - Take the given device through the device state model. |
1da177e4 LT |
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. | |
eb44820c | 2549 | */ |
1da177e4 LT |
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: | |
6f4267e3 JB |
2560 | switch (oldstate) { |
2561 | case SDEV_CREATED_BLOCK: | |
2562 | break; | |
2563 | default: | |
2564 | goto illegal; | |
2565 | } | |
2566 | break; | |
1da177e4 LT |
2567 | |
2568 | case SDEV_RUNNING: | |
2569 | switch (oldstate) { | |
2570 | case SDEV_CREATED: | |
2571 | case SDEV_OFFLINE: | |
1b8d2620 | 2572 | case SDEV_TRANSPORT_OFFLINE: |
1da177e4 LT |
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: | |
1b8d2620 | 2585 | case SDEV_TRANSPORT_OFFLINE: |
1da177e4 LT |
2586 | break; |
2587 | default: | |
2588 | goto illegal; | |
2589 | } | |
2590 | break; | |
2591 | ||
2592 | case SDEV_OFFLINE: | |
1b8d2620 | 2593 | case SDEV_TRANSPORT_OFFLINE: |
1da177e4 LT |
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) { | |
1da177e4 | 2607 | case SDEV_RUNNING: |
6f4267e3 JB |
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: | |
1da177e4 LT |
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: | |
9ea72909 | 2628 | case SDEV_QUIESCE: |
1da177e4 | 2629 | case SDEV_OFFLINE: |
1b8d2620 | 2630 | case SDEV_TRANSPORT_OFFLINE: |
1da177e4 LT |
2631 | case SDEV_BLOCK: |
2632 | break; | |
2633 | default: | |
2634 | goto illegal; | |
2635 | } | |
2636 | break; | |
2637 | ||
2638 | case SDEV_DEL: | |
2639 | switch (oldstate) { | |
309bd271 BK |
2640 | case SDEV_CREATED: |
2641 | case SDEV_RUNNING: | |
2642 | case SDEV_OFFLINE: | |
1b8d2620 | 2643 | case SDEV_TRANSPORT_OFFLINE: |
1da177e4 | 2644 | case SDEV_CANCEL: |
0516c08d | 2645 | case SDEV_CREATED_BLOCK: |
1da177e4 LT |
2646 | break; |
2647 | default: | |
2648 | goto illegal; | |
2649 | } | |
2650 | break; | |
2651 | ||
2652 | } | |
2653 | sdev->sdev_state = state; | |
2654 | return 0; | |
2655 | ||
2656 | illegal: | |
91921e01 | 2657 | SCSI_LOG_ERROR_RECOVERY(1, |
9ccfc756 | 2658 | sdev_printk(KERN_ERR, sdev, |
91921e01 | 2659 | "Illegal state transition %s->%s", |
9ccfc756 JB |
2660 | scsi_device_state_name(oldstate), |
2661 | scsi_device_state_name(state)) | |
1da177e4 LT |
2662 | ); |
2663 | return -EINVAL; | |
2664 | } | |
2665 | EXPORT_SYMBOL(scsi_device_set_state); | |
2666 | ||
a341cd0f JG |
2667 | /** |
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; | |
279afdfe | 2683 | case SDEV_EVT_INQUIRY_CHANGE_REPORTED: |
d3d32891 | 2684 | scsi_rescan_device(&sdev->sdev_gendev); |
279afdfe EM |
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; | |
14c3e677 HR |
2699 | case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED: |
2700 | envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED"; | |
2701 | break; | |
a341cd0f JG |
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; | |
279afdfe | 2722 | enum scsi_device_event evt_type; |
a341cd0f JG |
2723 | LIST_HEAD(event_list); |
2724 | ||
2725 | sdev = container_of(work, struct scsi_device, event_work); | |
2726 | ||
279afdfe EM |
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 | ||
a341cd0f JG |
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 | ||
4d1566ed KS |
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 */ | |
a341cd0f JG |
2767 | if (!test_bit(evt->evt_type, sdev->supported_events)) { |
2768 | kfree(evt); | |
2769 | return; | |
2770 | } | |
4d1566ed | 2771 | #endif |
a341cd0f JG |
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: | |
279afdfe EM |
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: | |
14c3e677 | 2805 | case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED: |
a341cd0f JG |
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 | ||
669f0441 BVA |
2837 | /** |
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 | ||
1da177e4 LT |
2870 | /** |
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. | |
eb44820c | 2884 | */ |
1da177e4 LT |
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); | |
71e75c97 | 2893 | while (atomic_read(&sdev->device_busy)) { |
1da177e4 LT |
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. | |
eb44820c | 2909 | */ |
a7a20d10 | 2910 | void scsi_device_resume(struct scsi_device *sdev) |
1da177e4 | 2911 | { |
a7a20d10 DW |
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)) | |
1da177e4 LT |
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 | /** | |
eb44820c | 2950 | * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state |
1da177e4 LT |
2951 | * @sdev: device to block |
2952 | * | |
2953 | * Block request made by scsi lld's to temporarily stop all | |
669f0441 | 2954 | * scsi commands on the specified device. May sleep. |
1da177e4 LT |
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. | |
669f0441 BVA |
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(). | |
eb44820c | 2967 | */ |
1da177e4 LT |
2968 | int |
2969 | scsi_internal_device_block(struct scsi_device *sdev) | |
2970 | { | |
165125e1 | 2971 | struct request_queue *q = sdev->request_queue; |
1da177e4 LT |
2972 | unsigned long flags; |
2973 | int err = 0; | |
2974 | ||
2975 | err = scsi_device_set_state(sdev, SDEV_BLOCK); | |
6f4267e3 JB |
2976 | if (err) { |
2977 | err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK); | |
2978 | ||
2979 | if (err) | |
2980 | return err; | |
2981 | } | |
1da177e4 LT |
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 | */ | |
d285203c | 2988 | if (q->mq_ops) { |
7dbbf0fa | 2989 | blk_mq_quiesce_queue(q); |
d285203c CH |
2990 | } else { |
2991 | spin_lock_irqsave(q->queue_lock, flags); | |
2992 | blk_stop_queue(q); | |
2993 | spin_unlock_irqrestore(q->queue_lock, flags); | |
669f0441 | 2994 | scsi_wait_for_queuecommand(sdev); |
d285203c | 2995 | } |
1da177e4 LT |
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 | |
5d9fb5cc | 3004 | * @new_state: state to set devices to after unblocking |
1da177e4 LT |
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 | |
5d9fb5cc | 3014 | * or to one of the offline states (which must be a legal transition) |
d075498c | 3015 | * allowing the midlayer to goose the queue for this device. |
eb44820c | 3016 | */ |
1da177e4 | 3017 | int |
5d9fb5cc MC |
3018 | scsi_internal_device_unblock(struct scsi_device *sdev, |
3019 | enum scsi_device_state new_state) | |
1da177e4 | 3020 | { |
165125e1 | 3021 | struct request_queue *q = sdev->request_queue; |
1da177e4 | 3022 | unsigned long flags; |
5d9fb5cc MC |
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. | |
1da177e4 | 3027 | */ |
0e58076b VC |
3028 | if ((sdev->sdev_state == SDEV_BLOCK) || |
3029 | (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE)) | |
5d9fb5cc MC |
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 && | |
986fe6c7 | 3038 | sdev->sdev_state != SDEV_OFFLINE) |
5c10e63c | 3039 | return -EINVAL; |
1da177e4 | 3040 | |
d285203c CH |
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 | } | |
1da177e4 LT |
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 | { | |
5d9fb5cc | 3082 | scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data); |
1da177e4 LT |
3083 | } |
3084 | ||
3085 | static int | |
3086 | target_unblock(struct device *dev, void *data) | |
3087 | { | |
3088 | if (scsi_is_target_device(dev)) | |
5d9fb5cc | 3089 | starget_for_each_device(to_scsi_target(dev), data, |
1da177e4 LT |
3090 | device_unblock); |
3091 | return 0; | |
3092 | } | |
3093 | ||
3094 | void | |
5d9fb5cc | 3095 | scsi_target_unblock(struct device *dev, enum scsi_device_state new_state) |
1da177e4 LT |
3096 | { |
3097 | if (scsi_is_target_device(dev)) | |
5d9fb5cc | 3098 | starget_for_each_device(to_scsi_target(dev), &new_state, |
1da177e4 LT |
3099 | device_unblock); |
3100 | else | |
5d9fb5cc | 3101 | device_for_each_child(dev, &new_state, target_unblock); |
1da177e4 LT |
3102 | } |
3103 | EXPORT_SYMBOL_GPL(scsi_target_unblock); | |
cdb8c2a6 GL |
3104 | |
3105 | /** | |
3106 | * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt | |
eb44820c | 3107 | * @sgl: scatter-gather list |
cdb8c2a6 GL |
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 | */ | |
c6132da1 | 3114 | void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count, |
cdb8c2a6 GL |
3115 | size_t *offset, size_t *len) |
3116 | { | |
3117 | int i; | |
3118 | size_t sg_len = 0, len_complete = 0; | |
c6132da1 | 3119 | struct scatterlist *sg; |
cdb8c2a6 GL |
3120 | struct page *page; |
3121 | ||
22cfefb5 AM |
3122 | WARN_ON(!irqs_disabled()); |
3123 | ||
c6132da1 | 3124 | for_each_sg(sgl, sg, sg_count, i) { |
cdb8c2a6 | 3125 | len_complete = sg_len; /* Complete sg-entries */ |
c6132da1 | 3126 | sg_len += sg->length; |
cdb8c2a6 GL |
3127 | if (sg_len > *offset) |
3128 | break; | |
3129 | } | |
3130 | ||
3131 | if (unlikely(i == sg_count)) { | |
169e1a2a AM |
3132 | printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, " |
3133 | "elements %d\n", | |
cadbd4a5 | 3134 | __func__, sg_len, *offset, sg_count); |
cdb8c2a6 GL |
3135 | WARN_ON(1); |
3136 | return NULL; | |
3137 | } | |
3138 | ||
3139 | /* Offset starting from the beginning of first page in this sg-entry */ | |
c6132da1 | 3140 | *offset = *offset - len_complete + sg->offset; |
cdb8c2a6 GL |
3141 | |
3142 | /* Assumption: contiguous pages can be accessed as "page + i" */ | |
45711f1a | 3143 | page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT)); |
cdb8c2a6 GL |
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 | ||
77dfce07 | 3151 | return kmap_atomic(page); |
cdb8c2a6 GL |
3152 | } |
3153 | EXPORT_SYMBOL(scsi_kmap_atomic_sg); | |
3154 | ||
3155 | /** | |
eb44820c | 3156 | * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg |
cdb8c2a6 GL |
3157 | * @virt: virtual address to be unmapped |
3158 | */ | |
3159 | void scsi_kunmap_atomic_sg(void *virt) | |
3160 | { | |
77dfce07 | 3161 | kunmap_atomic(virt); |
cdb8c2a6 GL |
3162 | } |
3163 | EXPORT_SYMBOL(scsi_kunmap_atomic_sg); | |
6f4c827e AL |
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); | |
9983bed3 HR |
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 | |
d230823a | 3217 | * - T10 Vendor ID |
9983bed3 HR |
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) { | |
d230823a HR |
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; | |
9983bed3 HR |
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); | |
a8aa3978 HR |
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); |