Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 | 2 | /* |
d285203c CH |
3 | * Copyright (C) 1999 Eric Youngdale |
4 | * Copyright (C) 2014 Christoph Hellwig | |
1da177e4 LT |
5 | * |
6 | * SCSI queueing library. | |
7 | * Initial versions: Eric Youngdale (eric@andante.org). | |
8 | * Based upon conversations with large numbers | |
9 | * of people at Linux Expo. | |
10 | */ | |
11 | ||
12 | #include <linux/bio.h> | |
d3f46f39 | 13 | #include <linux/bitops.h> |
1da177e4 LT |
14 | #include <linux/blkdev.h> |
15 | #include <linux/completion.h> | |
16 | #include <linux/kernel.h> | |
09703660 | 17 | #include <linux/export.h> |
1da177e4 LT |
18 | #include <linux/init.h> |
19 | #include <linux/pci.h> | |
20 | #include <linux/delay.h> | |
faead26d | 21 | #include <linux/hardirq.h> |
c6132da1 | 22 | #include <linux/scatterlist.h> |
d285203c | 23 | #include <linux/blk-mq.h> |
fe45e630 | 24 | #include <linux/blk-integrity.h> |
f1569ff1 | 25 | #include <linux/ratelimit.h> |
5f60d5f6 | 26 | #include <linux/unaligned.h> |
1da177e4 LT |
27 | |
28 | #include <scsi/scsi.h> | |
beb40487 | 29 | #include <scsi/scsi_cmnd.h> |
1da177e4 LT |
30 | #include <scsi/scsi_dbg.h> |
31 | #include <scsi/scsi_device.h> | |
32 | #include <scsi/scsi_driver.h> | |
33 | #include <scsi/scsi_eh.h> | |
34 | #include <scsi/scsi_host.h> | |
afd53a3d | 35 | #include <scsi/scsi_transport.h> /* scsi_init_limits() */ |
ee14c674 | 36 | #include <scsi/scsi_dh.h> |
1da177e4 | 37 | |
3b5382c4 CH |
38 | #include <trace/events/scsi.h> |
39 | ||
0eebd005 | 40 | #include "scsi_debugfs.h" |
1da177e4 LT |
41 | #include "scsi_priv.h" |
42 | #include "scsi_logging.h" | |
43 | ||
92524fa1 ML |
44 | /* |
45 | * Size of integrity metadata is usually small, 1 inline sg should | |
46 | * cover normal cases. | |
47 | */ | |
3e99b3b1 ML |
48 | #ifdef CONFIG_ARCH_NO_SG_CHAIN |
49 | #define SCSI_INLINE_PROT_SG_CNT 0 | |
50 | #define SCSI_INLINE_SG_CNT 0 | |
51 | #else | |
92524fa1 | 52 | #define SCSI_INLINE_PROT_SG_CNT 1 |
3dccdf53 | 53 | #define SCSI_INLINE_SG_CNT 2 |
3e99b3b1 | 54 | #endif |
3dccdf53 | 55 | |
0a6ac4ee | 56 | static struct kmem_cache *scsi_sense_cache; |
0a6ac4ee | 57 | static DEFINE_MUTEX(scsi_sense_cache_mutex); |
1da177e4 | 58 | |
a45a1f36 BVA |
59 | static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd); |
60 | ||
0a6ac4ee CH |
61 | int scsi_init_sense_cache(struct Scsi_Host *shost) |
62 | { | |
0a6ac4ee CH |
63 | int ret = 0; |
64 | ||
f9b0530f | 65 | mutex_lock(&scsi_sense_cache_mutex); |
aaff5eba | 66 | if (!scsi_sense_cache) { |
0a6ac4ee | 67 | scsi_sense_cache = |
0afe76e8 DW |
68 | kmem_cache_create_usercopy("scsi_sense_cache", |
69 | SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN, | |
70 | 0, SCSI_SENSE_BUFFERSIZE, NULL); | |
0a6ac4ee CH |
71 | if (!scsi_sense_cache) |
72 | ret = -ENOMEM; | |
73 | } | |
0a6ac4ee CH |
74 | mutex_unlock(&scsi_sense_cache_mutex); |
75 | return ret; | |
76 | } | |
6f9a35e2 | 77 | |
de3e8bf3 CH |
78 | static void |
79 | scsi_set_blocked(struct scsi_cmnd *cmd, int reason) | |
1da177e4 LT |
80 | { |
81 | struct Scsi_Host *host = cmd->device->host; | |
82 | struct scsi_device *device = cmd->device; | |
f0c0a376 | 83 | struct scsi_target *starget = scsi_target(device); |
1da177e4 LT |
84 | |
85 | /* | |
d8c37e7b | 86 | * Set the appropriate busy bit for the device/host. |
1da177e4 LT |
87 | * |
88 | * If the host/device isn't busy, assume that something actually | |
89 | * completed, and that we should be able to queue a command now. | |
90 | * | |
91 | * Note that the prior mid-layer assumption that any host could | |
92 | * always queue at least one command is now broken. The mid-layer | |
93 | * will implement a user specifiable stall (see | |
94 | * scsi_host.max_host_blocked and scsi_device.max_device_blocked) | |
95 | * if a command is requeued with no other commands outstanding | |
96 | * either for the device or for the host. | |
97 | */ | |
f0c0a376 MC |
98 | switch (reason) { |
99 | case SCSI_MLQUEUE_HOST_BUSY: | |
cd9070c9 | 100 | atomic_set(&host->host_blocked, host->max_host_blocked); |
f0c0a376 MC |
101 | break; |
102 | case SCSI_MLQUEUE_DEVICE_BUSY: | |
573e5913 | 103 | case SCSI_MLQUEUE_EH_RETRY: |
cd9070c9 CH |
104 | atomic_set(&device->device_blocked, |
105 | device->max_device_blocked); | |
f0c0a376 MC |
106 | break; |
107 | case SCSI_MLQUEUE_TARGET_BUSY: | |
cd9070c9 CH |
108 | atomic_set(&starget->target_blocked, |
109 | starget->max_target_blocked); | |
f0c0a376 MC |
110 | break; |
111 | } | |
de3e8bf3 CH |
112 | } |
113 | ||
54249306 | 114 | static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd, unsigned long msecs) |
d285203c | 115 | { |
aa8e25e5 BVA |
116 | struct request *rq = scsi_cmd_to_rq(cmd); |
117 | ||
118 | if (rq->rq_flags & RQF_DONTPREP) { | |
119 | rq->rq_flags &= ~RQF_DONTPREP; | |
a45a1f36 BVA |
120 | scsi_mq_uninit_cmd(cmd); |
121 | } else { | |
122 | WARN_ON_ONCE(true); | |
123 | } | |
54249306 | 124 | |
8b566edb BVA |
125 | blk_mq_requeue_request(rq, false); |
126 | if (!scsi_host_in_recovery(cmd->device->host)) | |
54249306 | 127 | blk_mq_delay_kick_requeue_list(rq->q, msecs); |
d285203c CH |
128 | } |
129 | ||
de3e8bf3 CH |
130 | /** |
131 | * __scsi_queue_insert - private queue insertion | |
132 | * @cmd: The SCSI command being requeued | |
133 | * @reason: The reason for the requeue | |
134 | * @unbusy: Whether the queue should be unbusied | |
135 | * | |
136 | * This is a private queue insertion. The public interface | |
137 | * scsi_queue_insert() always assumes the queue should be unbusied | |
138 | * because it's always called before the completion. This function is | |
139 | * for a requeue after completion, which should only occur in this | |
140 | * file. | |
141 | */ | |
08640e81 | 142 | static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, bool unbusy) |
de3e8bf3 CH |
143 | { |
144 | struct scsi_device *device = cmd->device; | |
de3e8bf3 CH |
145 | |
146 | SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd, | |
147 | "Inserting command %p into mlqueue\n", cmd)); | |
148 | ||
149 | scsi_set_blocked(cmd, reason); | |
1da177e4 | 150 | |
1da177e4 LT |
151 | /* |
152 | * Decrement the counters, since these commands are no longer | |
153 | * active on the host/device. | |
154 | */ | |
4f5299ac | 155 | if (unbusy) |
6eb045e0 | 156 | scsi_device_unbusy(device, cmd); |
1da177e4 LT |
157 | |
158 | /* | |
a1bf9d1d | 159 | * Requeue this command. It will go before all other commands |
b485462a BVA |
160 | * that are already in the queue. Schedule requeue work under |
161 | * lock such that the kblockd_schedule_work() call happens | |
6f8191fd | 162 | * before blk_mq_destroy_queue() finishes. |
a488e749 | 163 | */ |
644373a4 | 164 | cmd->result = 0; |
f664a3cc | 165 | |
8b566edb BVA |
166 | blk_mq_requeue_request(scsi_cmd_to_rq(cmd), |
167 | !scsi_host_in_recovery(cmd->device->host)); | |
1da177e4 LT |
168 | } |
169 | ||
ea941016 AA |
170 | /** |
171 | * scsi_queue_insert - Reinsert a command in the queue. | |
172 | * @cmd: command that we are adding to queue. | |
173 | * @reason: why we are inserting command to queue. | |
4f5299ac | 174 | * |
ea941016 AA |
175 | * We do this for one of two cases. Either the host is busy and it cannot accept |
176 | * any more commands for the time being, or the device returned QUEUE_FULL and | |
177 | * can accept no more commands. | |
4f5299ac | 178 | * |
ea941016 AA |
179 | * Context: This could be called either from an interrupt context or a normal |
180 | * process context. | |
4f5299ac | 181 | */ |
84feb166 | 182 | void scsi_queue_insert(struct scsi_cmnd *cmd, int reason) |
4f5299ac | 183 | { |
08640e81 | 184 | __scsi_queue_insert(cmd, reason, true); |
4f5299ac | 185 | } |
e8064021 | 186 | |
39d2112a RD |
187 | /** |
188 | * scsi_failures_reset_retries - reset all failures to zero | |
189 | * @failures: &struct scsi_failures with specific failure modes set | |
190 | */ | |
994724e6 MC |
191 | void scsi_failures_reset_retries(struct scsi_failures *failures) |
192 | { | |
193 | struct scsi_failure *failure; | |
194 | ||
195 | failures->total_retries = 0; | |
196 | ||
197 | for (failure = failures->failure_definitions; failure->result; | |
198 | failure++) | |
199 | failure->retries = 0; | |
200 | } | |
201 | EXPORT_SYMBOL_GPL(scsi_failures_reset_retries); | |
202 | ||
203 | /** | |
204 | * scsi_check_passthrough - Determine if passthrough scsi_cmnd needs a retry. | |
205 | * @scmd: scsi_cmnd to check. | |
206 | * @failures: scsi_failures struct that lists failures to check for. | |
207 | * | |
208 | * Returns -EAGAIN if the caller should retry else 0. | |
209 | */ | |
210 | static int scsi_check_passthrough(struct scsi_cmnd *scmd, | |
211 | struct scsi_failures *failures) | |
212 | { | |
213 | struct scsi_failure *failure; | |
214 | struct scsi_sense_hdr sshdr; | |
215 | enum sam_status status; | |
216 | ||
8604f633 MC |
217 | if (!scmd->result) |
218 | return 0; | |
219 | ||
994724e6 MC |
220 | if (!failures) |
221 | return 0; | |
222 | ||
223 | for (failure = failures->failure_definitions; failure->result; | |
224 | failure++) { | |
225 | if (failure->result == SCMD_FAILURE_RESULT_ANY) | |
226 | goto maybe_retry; | |
227 | ||
228 | if (host_byte(scmd->result) && | |
229 | host_byte(scmd->result) == host_byte(failure->result)) | |
230 | goto maybe_retry; | |
231 | ||
232 | status = status_byte(scmd->result); | |
233 | if (!status) | |
234 | continue; | |
235 | ||
236 | if (failure->result == SCMD_FAILURE_STAT_ANY && | |
237 | !scsi_status_is_good(scmd->result)) | |
238 | goto maybe_retry; | |
239 | ||
240 | if (status != status_byte(failure->result)) | |
241 | continue; | |
242 | ||
243 | if (status_byte(failure->result) != SAM_STAT_CHECK_CONDITION || | |
244 | failure->sense == SCMD_FAILURE_SENSE_ANY) | |
245 | goto maybe_retry; | |
246 | ||
247 | if (!scsi_command_normalize_sense(scmd, &sshdr)) | |
248 | return 0; | |
249 | ||
250 | if (failure->sense != sshdr.sense_key) | |
251 | continue; | |
252 | ||
253 | if (failure->asc == SCMD_FAILURE_ASC_ANY) | |
254 | goto maybe_retry; | |
255 | ||
256 | if (failure->asc != sshdr.asc) | |
257 | continue; | |
258 | ||
259 | if (failure->ascq == SCMD_FAILURE_ASCQ_ANY || | |
260 | failure->ascq == sshdr.ascq) | |
261 | goto maybe_retry; | |
262 | } | |
263 | ||
264 | return 0; | |
265 | ||
266 | maybe_retry: | |
267 | if (failure->allowed) { | |
268 | if (failure->allowed == SCMD_FAILURE_NO_LIMIT || | |
269 | ++failure->retries <= failure->allowed) | |
270 | return -EAGAIN; | |
271 | } else { | |
272 | if (failures->total_allowed == SCMD_FAILURE_NO_LIMIT || | |
273 | ++failures->total_retries <= failures->total_allowed) | |
274 | return -EAGAIN; | |
275 | } | |
276 | ||
277 | return 0; | |
278 | } | |
279 | ||
76aaf87b | 280 | /** |
d0949565 MC |
281 | * scsi_execute_cmd - insert request and wait for the result |
282 | * @sdev: scsi_device | |
76aaf87b | 283 | * @cmd: scsi command |
d0949565 | 284 | * @opf: block layer request cmd_flags |
76aaf87b CH |
285 | * @buffer: data buffer |
286 | * @bufflen: len of buffer | |
422969bb | 287 | * @timeout: request timeout in HZ |
994724e6 | 288 | * @ml_retries: number of times SCSI midlayer will retry request |
d0949565 | 289 | * @args: Optional args. See struct definition for field descriptions |
76aaf87b | 290 | * |
17d5363b CH |
291 | * Returns the scsi_cmnd result field if a command was executed, or a negative |
292 | * Linux error code if we didn't get that far. | |
76aaf87b | 293 | */ |
d0949565 MC |
294 | int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd, |
295 | blk_opf_t opf, void *buffer, unsigned int bufflen, | |
994724e6 | 296 | int timeout, int ml_retries, |
d0949565 | 297 | const struct scsi_exec_args *args) |
39216033 | 298 | { |
d0949565 | 299 | static const struct scsi_exec_args default_args; |
39216033 | 300 | struct request *req; |
ce70fd9a | 301 | struct scsi_cmnd *scmd; |
ced202f7 | 302 | int ret; |
39216033 | 303 | |
d0949565 MC |
304 | if (!args) |
305 | args = &default_args; | |
306 | else if (WARN_ON_ONCE(args->sense && | |
307 | args->sense_len != SCSI_SENSE_BUFFERSIZE)) | |
308 | return -EINVAL; | |
309 | ||
994724e6 | 310 | retry: |
d0949565 | 311 | req = scsi_alloc_request(sdev->request_queue, opf, args->req_flags); |
a492f075 | 312 | if (IS_ERR(req)) |
ced202f7 | 313 | return PTR_ERR(req); |
39216033 | 314 | |
ced202f7 | 315 | if (bufflen) { |
af78428e | 316 | ret = blk_rq_map_kern(req, buffer, bufflen, GFP_NOIO); |
ced202f7 HR |
317 | if (ret) |
318 | goto out; | |
319 | } | |
ce70fd9a CH |
320 | scmd = blk_mq_rq_to_pdu(req); |
321 | scmd->cmd_len = COMMAND_SIZE(cmd[0]); | |
322 | memcpy(scmd->cmnd, cmd, scmd->cmd_len); | |
994724e6 | 323 | scmd->allowed = ml_retries; |
35cd2f55 | 324 | scmd->flags |= args->scmd_flags; |
39216033 | 325 | req->timeout = timeout; |
d0949565 | 326 | req->rq_flags |= RQF_QUIET; |
39216033 JB |
327 | |
328 | /* | |
329 | * head injection *required* here otherwise quiesce won't work | |
330 | */ | |
b84ba30b | 331 | blk_execute_rq(req, true); |
39216033 | 332 | |
994724e6 MC |
333 | if (scsi_check_passthrough(scmd, args->failures) == -EAGAIN) { |
334 | blk_mq_free_request(req); | |
335 | goto retry; | |
336 | } | |
337 | ||
bdb2b8ca AS |
338 | /* |
339 | * Some devices (USB mass-storage in particular) may transfer | |
340 | * garbage data together with a residue indicating that the data | |
341 | * is invalid. Prevent the garbage from being misinterpreted | |
342 | * and prevent security leaks by zeroing out the excess data. | |
343 | */ | |
a9a4ea11 CH |
344 | if (unlikely(scmd->resid_len > 0 && scmd->resid_len <= bufflen)) |
345 | memset(buffer + bufflen - scmd->resid_len, 0, scmd->resid_len); | |
bdb2b8ca | 346 | |
d0949565 MC |
347 | if (args->resid) |
348 | *args->resid = scmd->resid_len; | |
349 | if (args->sense) | |
350 | memcpy(args->sense, scmd->sense_buffer, SCSI_SENSE_BUFFERSIZE); | |
351 | if (args->sshdr) | |
5b794f98 | 352 | scsi_normalize_sense(scmd->sense_buffer, scmd->sense_len, |
d0949565 MC |
353 | args->sshdr); |
354 | ||
dbb4c84d | 355 | ret = scmd->result; |
39216033 | 356 | out: |
0bf6d96c | 357 | blk_mq_free_request(req); |
39216033 JB |
358 | |
359 | return ret; | |
360 | } | |
d0949565 | 361 | EXPORT_SYMBOL(scsi_execute_cmd); |
39216033 | 362 | |
3bd6f43f | 363 | /* |
6eb045e0 ML |
364 | * Wake up the error handler if necessary. Avoid as follows that the error |
365 | * handler is not woken up if host in-flight requests number == | |
366 | * shost->host_failed: use call_rcu() in scsi_eh_scmd_add() in combination | |
367 | * with an RCU read lock in this function to ensure that this function in | |
368 | * its entirety either finishes before scsi_eh_scmd_add() increases the | |
3bd6f43f BVA |
369 | * host_failed counter or that it notices the shost state change made by |
370 | * scsi_eh_scmd_add(). | |
371 | */ | |
6eb045e0 | 372 | static void scsi_dec_host_busy(struct Scsi_Host *shost, struct scsi_cmnd *cmd) |
1da177e4 | 373 | { |
1da177e4 LT |
374 | unsigned long flags; |
375 | ||
3bd6f43f | 376 | rcu_read_lock(); |
6eb045e0 | 377 | __clear_bit(SCMD_STATE_INFLIGHT, &cmd->state); |
3bd6f43f | 378 | if (unlikely(scsi_host_in_recovery(shost))) { |
4e6c9011 ML |
379 | unsigned int busy = scsi_host_busy(shost); |
380 | ||
74665016 | 381 | spin_lock_irqsave(shost->host_lock, flags); |
3bd6f43f | 382 | if (shost->host_failed || shost->host_eh_scheduled) |
4e6c9011 | 383 | scsi_eh_wakeup(shost, busy); |
74665016 CH |
384 | spin_unlock_irqrestore(shost->host_lock, flags); |
385 | } | |
3bd6f43f BVA |
386 | rcu_read_unlock(); |
387 | } | |
388 | ||
6eb045e0 | 389 | void scsi_device_unbusy(struct scsi_device *sdev, struct scsi_cmnd *cmd) |
3bd6f43f BVA |
390 | { |
391 | struct Scsi_Host *shost = sdev->host; | |
392 | struct scsi_target *starget = scsi_target(sdev); | |
393 | ||
6eb045e0 | 394 | scsi_dec_host_busy(shost, cmd); |
3bd6f43f BVA |
395 | |
396 | if (starget->can_queue > 0) | |
397 | atomic_dec(&starget->target_busy); | |
74665016 | 398 | |
020b0f0a | 399 | sbitmap_put(&sdev->budget_map, cmd->budget_token); |
2a5a24aa | 400 | cmd->budget_token = -1; |
1da177e4 LT |
401 | } |
402 | ||
d460f624 BVA |
403 | /* |
404 | * Kick the queue of SCSI device @sdev if @sdev != current_sdev. Called with | |
405 | * interrupts disabled. | |
406 | */ | |
407 | static void scsi_kick_sdev_queue(struct scsi_device *sdev, void *data) | |
408 | { | |
409 | struct scsi_device *current_sdev = data; | |
410 | ||
411 | if (sdev != current_sdev) | |
412 | blk_mq_run_hw_queues(sdev->request_queue, true); | |
413 | } | |
414 | ||
1da177e4 LT |
415 | /* |
416 | * Called for single_lun devices on IO completion. Clear starget_sdev_user, | |
417 | * and call blk_run_queue for all the scsi_devices on the target - | |
418 | * including current_sdev first. | |
419 | * | |
420 | * Called with *no* scsi locks held. | |
421 | */ | |
422 | static void scsi_single_lun_run(struct scsi_device *current_sdev) | |
423 | { | |
424 | struct Scsi_Host *shost = current_sdev->host; | |
1da177e4 LT |
425 | struct scsi_target *starget = scsi_target(current_sdev); |
426 | unsigned long flags; | |
427 | ||
428 | spin_lock_irqsave(shost->host_lock, flags); | |
429 | starget->starget_sdev_user = NULL; | |
430 | spin_unlock_irqrestore(shost->host_lock, flags); | |
431 | ||
432 | /* | |
433 | * Call blk_run_queue for all LUNs on the target, starting with | |
434 | * current_sdev. We race with others (to set starget_sdev_user), | |
435 | * but in most cases, we will be first. Ideally, each LU on the | |
436 | * target would get some limited time or requests on the target. | |
437 | */ | |
65a558f6 BVA |
438 | blk_mq_run_hw_queues(current_sdev->request_queue, |
439 | shost->queuecommand_may_block); | |
1da177e4 LT |
440 | |
441 | spin_lock_irqsave(shost->host_lock, flags); | |
d460f624 BVA |
442 | if (!starget->starget_sdev_user) |
443 | __starget_for_each_device(starget, current_sdev, | |
444 | scsi_kick_sdev_queue); | |
1da177e4 LT |
445 | spin_unlock_irqrestore(shost->host_lock, flags); |
446 | } | |
447 | ||
cd9070c9 | 448 | static inline bool scsi_device_is_busy(struct scsi_device *sdev) |
9d112517 | 449 | { |
8278807a | 450 | if (scsi_device_busy(sdev) >= sdev->queue_depth) |
cd9070c9 CH |
451 | return true; |
452 | if (atomic_read(&sdev->device_blocked) > 0) | |
453 | return true; | |
454 | return false; | |
9d112517 KU |
455 | } |
456 | ||
cd9070c9 | 457 | static inline bool scsi_target_is_busy(struct scsi_target *starget) |
f0c0a376 | 458 | { |
2ccbb008 CH |
459 | if (starget->can_queue > 0) { |
460 | if (atomic_read(&starget->target_busy) >= starget->can_queue) | |
461 | return true; | |
462 | if (atomic_read(&starget->target_blocked) > 0) | |
463 | return true; | |
464 | } | |
cd9070c9 | 465 | return false; |
f0c0a376 MC |
466 | } |
467 | ||
cd9070c9 | 468 | static inline bool scsi_host_is_busy(struct Scsi_Host *shost) |
9d112517 | 469 | { |
cd9070c9 CH |
470 | if (atomic_read(&shost->host_blocked) > 0) |
471 | return true; | |
472 | if (shost->host_self_blocked) | |
473 | return true; | |
474 | return false; | |
9d112517 KU |
475 | } |
476 | ||
21a05df5 | 477 | static void scsi_starved_list_run(struct Scsi_Host *shost) |
1da177e4 | 478 | { |
2a3a59e5 | 479 | LIST_HEAD(starved_list); |
21a05df5 | 480 | struct scsi_device *sdev; |
1da177e4 LT |
481 | unsigned long flags; |
482 | ||
1da177e4 | 483 | spin_lock_irqsave(shost->host_lock, flags); |
2a3a59e5 MC |
484 | list_splice_init(&shost->starved_list, &starved_list); |
485 | ||
486 | while (!list_empty(&starved_list)) { | |
e2eb7244 JB |
487 | struct request_queue *slq; |
488 | ||
1da177e4 LT |
489 | /* |
490 | * As long as shost is accepting commands and we have | |
491 | * starved queues, call blk_run_queue. scsi_request_fn | |
492 | * drops the queue_lock and can add us back to the | |
493 | * starved_list. | |
494 | * | |
495 | * host_lock protects the starved_list and starved_entry. | |
496 | * scsi_request_fn must get the host_lock before checking | |
497 | * or modifying starved_list or starved_entry. | |
498 | */ | |
2a3a59e5 | 499 | if (scsi_host_is_busy(shost)) |
f0c0a376 | 500 | break; |
f0c0a376 | 501 | |
2a3a59e5 MC |
502 | sdev = list_entry(starved_list.next, |
503 | struct scsi_device, starved_entry); | |
504 | list_del_init(&sdev->starved_entry); | |
f0c0a376 MC |
505 | if (scsi_target_is_busy(scsi_target(sdev))) { |
506 | list_move_tail(&sdev->starved_entry, | |
507 | &shost->starved_list); | |
508 | continue; | |
509 | } | |
510 | ||
e2eb7244 JB |
511 | /* |
512 | * Once we drop the host lock, a racing scsi_remove_device() | |
513 | * call may remove the sdev from the starved list and destroy | |
514 | * it and the queue. Mitigate by taking a reference to the | |
515 | * queue and never touching the sdev again after we drop the | |
516 | * host lock. Note: if __scsi_remove_device() invokes | |
6f8191fd | 517 | * blk_mq_destroy_queue() before the queue is run from this |
e2eb7244 | 518 | * function then blk_run_queue() will return immediately since |
6f8191fd | 519 | * blk_mq_destroy_queue() marks the queue with QUEUE_FLAG_DYING. |
e2eb7244 JB |
520 | */ |
521 | slq = sdev->request_queue; | |
522 | if (!blk_get_queue(slq)) | |
523 | continue; | |
524 | spin_unlock_irqrestore(shost->host_lock, flags); | |
525 | ||
b5ca9acf | 526 | blk_mq_run_hw_queues(slq, false); |
e2eb7244 JB |
527 | blk_put_queue(slq); |
528 | ||
529 | spin_lock_irqsave(shost->host_lock, flags); | |
1da177e4 | 530 | } |
2a3a59e5 MC |
531 | /* put any unprocessed entries back */ |
532 | list_splice(&starved_list, &shost->starved_list); | |
1da177e4 | 533 | spin_unlock_irqrestore(shost->host_lock, flags); |
21a05df5 CH |
534 | } |
535 | ||
ea941016 AA |
536 | /** |
537 | * scsi_run_queue - Select a proper request queue to serve next. | |
538 | * @q: last request's queue | |
21a05df5 | 539 | * |
ea941016 | 540 | * The previous command was completely finished, start a new one if possible. |
21a05df5 CH |
541 | */ |
542 | static void scsi_run_queue(struct request_queue *q) | |
543 | { | |
544 | struct scsi_device *sdev = q->queuedata; | |
545 | ||
546 | if (scsi_target(sdev)->single_lun) | |
547 | scsi_single_lun_run(sdev); | |
548 | if (!list_empty(&sdev->host->starved_list)) | |
549 | scsi_starved_list_run(sdev->host); | |
1da177e4 | 550 | |
d42e2e34 | 551 | /* Note: blk_mq_kick_requeue_list() runs the queue asynchronously. */ |
8b566edb | 552 | blk_mq_kick_requeue_list(q); |
1da177e4 LT |
553 | } |
554 | ||
9937a5e2 JA |
555 | void scsi_requeue_run_queue(struct work_struct *work) |
556 | { | |
557 | struct scsi_device *sdev; | |
558 | struct request_queue *q; | |
559 | ||
560 | sdev = container_of(work, struct scsi_device, requeue_work); | |
561 | q = sdev->request_queue; | |
562 | scsi_run_queue(q); | |
563 | } | |
564 | ||
1da177e4 LT |
565 | void scsi_run_host_queues(struct Scsi_Host *shost) |
566 | { | |
567 | struct scsi_device *sdev; | |
568 | ||
569 | shost_for_each_device(sdev, shost) | |
570 | scsi_run_queue(sdev->request_queue); | |
571 | } | |
572 | ||
d285203c CH |
573 | static void scsi_uninit_cmd(struct scsi_cmnd *cmd) |
574 | { | |
aa8e25e5 | 575 | if (!blk_rq_is_passthrough(scsi_cmd_to_rq(cmd))) { |
d285203c CH |
576 | struct scsi_driver *drv = scsi_cmd_to_driver(cmd); |
577 | ||
578 | if (drv->uninit_command) | |
579 | drv->uninit_command(cmd); | |
580 | } | |
581 | } | |
582 | ||
7007e9dd | 583 | void scsi_free_sgtables(struct scsi_cmnd *cmd) |
d285203c CH |
584 | { |
585 | if (cmd->sdb.table.nents) | |
3dccdf53 ML |
586 | sg_free_table_chained(&cmd->sdb.table, |
587 | SCSI_INLINE_SG_CNT); | |
d285203c | 588 | if (scsi_prot_sg_count(cmd)) |
92524fa1 ML |
589 | sg_free_table_chained(&cmd->prot_sdb->table, |
590 | SCSI_INLINE_PROT_SG_CNT); | |
d285203c | 591 | } |
7007e9dd | 592 | EXPORT_SYMBOL_GPL(scsi_free_sgtables); |
d285203c CH |
593 | |
594 | static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd) | |
595 | { | |
20a66f2b | 596 | scsi_free_sgtables(cmd); |
d285203c | 597 | scsi_uninit_cmd(cmd); |
d285203c CH |
598 | } |
599 | ||
3f0dcfbc ML |
600 | static void scsi_run_queue_async(struct scsi_device *sdev) |
601 | { | |
8b566edb BVA |
602 | if (scsi_host_in_recovery(sdev->host)) |
603 | return; | |
604 | ||
3f0dcfbc | 605 | if (scsi_target(sdev)->single_lun || |
ed5dd6a6 | 606 | !list_empty(&sdev->host->starved_list)) { |
3f0dcfbc | 607 | kblockd_schedule_work(&sdev->requeue_work); |
ed5dd6a6 ML |
608 | } else { |
609 | /* | |
610 | * smp_mb() present in sbitmap_queue_clear() or implied in | |
611 | * .end_io is for ordering writing .device_busy in | |
612 | * scsi_device_unbusy() and reading sdev->restarts. | |
613 | */ | |
614 | int old = atomic_read(&sdev->restarts); | |
615 | ||
616 | /* | |
617 | * ->restarts has to be kept as non-zero if new budget | |
618 | * contention occurs. | |
619 | * | |
620 | * No need to run queue when either another re-run | |
621 | * queue wins in updating ->restarts or a new budget | |
622 | * contention occurs. | |
623 | */ | |
624 | if (old && atomic_cmpxchg(&sdev->restarts, old, 0) == old) | |
625 | blk_mq_run_hw_queues(sdev->request_queue, true); | |
626 | } | |
3f0dcfbc ML |
627 | } |
628 | ||
7e63b5a4 | 629 | /* Returns false when no more bytes to process, true if there are more */ |
2a842aca | 630 | static bool scsi_end_request(struct request *req, blk_status_t error, |
ae3d56d8 | 631 | unsigned int bytes) |
f6d47e74 | 632 | { |
bed2213d | 633 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); |
f6d47e74 CH |
634 | struct scsi_device *sdev = cmd->device; |
635 | struct request_queue *q = sdev->request_queue; | |
f6d47e74 CH |
636 | |
637 | if (blk_update_request(req, error, bytes)) | |
638 | return true; | |
639 | ||
39a9f1c3 | 640 | if (q->limits.features & BLK_FEAT_ADD_RANDOM) |
f3fa33ac | 641 | add_disk_randomness(req->q->disk); |
f6d47e74 | 642 | |
ca91259b BVA |
643 | WARN_ON_ONCE(!blk_rq_is_passthrough(req) && |
644 | !(cmd->flags & SCMD_INITIALIZED)); | |
645 | cmd->flags = 0; | |
64104f70 | 646 | |
db983f6e BVA |
647 | /* |
648 | * Calling rcu_barrier() is not necessary here because the | |
649 | * SCSI error handler guarantees that the function called by | |
650 | * call_rcu() has been called before scsi_end_request() is | |
651 | * called. | |
652 | */ | |
653 | destroy_rcu_head(&cmd->rcu); | |
654 | ||
f664a3cc JA |
655 | /* |
656 | * In the MQ case the command gets freed by __blk_mq_end_request, | |
657 | * so we have to do all cleanup that depends on it earlier. | |
658 | * | |
659 | * We also can't kick the queues from irq context, so we | |
660 | * will have to defer it to a workqueue. | |
661 | */ | |
662 | scsi_mq_uninit_cmd(cmd); | |
f81426a8 | 663 | |
a78b03bc JA |
664 | /* |
665 | * queue is still alive, so grab the ref for preventing it | |
666 | * from being cleaned up during running queue. | |
667 | */ | |
668 | percpu_ref_get(&q->q_usage_counter); | |
f81426a8 | 669 | |
f664a3cc | 670 | __blk_mq_end_request(req, error); |
d285203c | 671 | |
3f0dcfbc | 672 | scsi_run_queue_async(sdev); |
f6d47e74 | 673 | |
a78b03bc | 674 | percpu_ref_put(&q->q_usage_counter); |
f6d47e74 CH |
675 | return false; |
676 | } | |
677 | ||
0f7f6234 | 678 | /** |
a77b32d8 | 679 | * scsi_result_to_blk_status - translate a SCSI result code into blk_status_t |
0f7f6234 HR |
680 | * @result: scsi error code |
681 | * | |
7dfaae6a | 682 | * Translate a SCSI result code into a blk_status_t value. |
0f7f6234 | 683 | */ |
7dfaae6a | 684 | static blk_status_t scsi_result_to_blk_status(int result) |
63583cca | 685 | { |
36ebf1e2 MC |
686 | /* |
687 | * Check the scsi-ml byte first in case we converted a host or status | |
688 | * byte. | |
689 | */ | |
73432693 | 690 | switch (scsi_ml_byte(result)) { |
36ebf1e2 MC |
691 | case SCSIML_STAT_OK: |
692 | break; | |
693 | case SCSIML_STAT_RESV_CONFLICT: | |
7ba15083 | 694 | return BLK_STS_RESV_CONFLICT; |
36ebf1e2 MC |
695 | case SCSIML_STAT_NOSPC: |
696 | return BLK_STS_NOSPC; | |
697 | case SCSIML_STAT_MED_ERROR: | |
698 | return BLK_STS_MEDIUM; | |
699 | case SCSIML_STAT_TGT_FAILURE: | |
700 | return BLK_STS_TARGET; | |
390e2d1a NC |
701 | case SCSIML_STAT_DL_TIMEOUT: |
702 | return BLK_STS_DURATION_LIMIT; | |
36ebf1e2 MC |
703 | } |
704 | ||
2a842aca | 705 | switch (host_byte(result)) { |
f4abab3f | 706 | case DID_OK: |
a7479a84 | 707 | if (scsi_status_is_good(result)) |
f4abab3f BVA |
708 | return BLK_STS_OK; |
709 | return BLK_STS_IOERR; | |
63583cca | 710 | case DID_TRANSPORT_FAILFAST: |
962c8dcd | 711 | case DID_TRANSPORT_MARGINAL: |
2a842aca | 712 | return BLK_STS_TRANSPORT; |
63583cca | 713 | default: |
2a842aca | 714 | return BLK_STS_IOERR; |
63583cca | 715 | } |
63583cca HR |
716 | } |
717 | ||
79478bf9 CH |
718 | /** |
719 | * scsi_rq_err_bytes - determine number of bytes till the next failure boundary | |
720 | * @rq: request to examine | |
721 | * | |
722 | * Description: | |
723 | * A request could be merge of IOs which require different failure | |
724 | * handling. This function determines the number of bytes which | |
725 | * can be failed from the beginning of the request without | |
726 | * crossing into area which need to be retried further. | |
727 | * | |
728 | * Return: | |
729 | * The number of bytes to fail. | |
730 | */ | |
731 | static unsigned int scsi_rq_err_bytes(const struct request *rq) | |
732 | { | |
2599cac5 | 733 | blk_opf_t ff = rq->cmd_flags & REQ_FAILFAST_MASK; |
79478bf9 CH |
734 | unsigned int bytes = 0; |
735 | struct bio *bio; | |
736 | ||
737 | if (!(rq->rq_flags & RQF_MIXED_MERGE)) | |
738 | return blk_rq_bytes(rq); | |
739 | ||
740 | /* | |
741 | * Currently the only 'mixing' which can happen is between | |
742 | * different fastfail types. We can safely fail portions | |
743 | * which have all the failfast bits that the first one has - | |
744 | * the ones which are at least as eager to fail as the first | |
745 | * one. | |
746 | */ | |
747 | for (bio = rq->bio; bio; bio = bio->bi_next) { | |
748 | if ((bio->bi_opf & ff) != ff) | |
749 | break; | |
750 | bytes += bio->bi_iter.bi_size; | |
751 | } | |
752 | ||
753 | /* this could lead to infinite loop */ | |
754 | BUG_ON(blk_rq_bytes(rq) && !bytes); | |
755 | return bytes; | |
756 | } | |
757 | ||
2a242d59 MC |
758 | static bool scsi_cmd_runtime_exceeced(struct scsi_cmnd *cmd) |
759 | { | |
aa8e25e5 | 760 | struct request *req = scsi_cmd_to_rq(cmd); |
2a242d59 MC |
761 | unsigned long wait_for; |
762 | ||
763 | if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT) | |
764 | return false; | |
765 | ||
766 | wait_for = (cmd->allowed + 1) * req->timeout; | |
767 | if (time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) { | |
768 | scmd_printk(KERN_ERR, cmd, "timing out command, waited %lus\n", | |
769 | wait_for/HZ); | |
770 | return true; | |
771 | } | |
772 | return false; | |
773 | } | |
774 | ||
54249306 BB |
775 | /* |
776 | * When ALUA transition state is returned, reprep the cmd to | |
777 | * use the ALUA handler's transition timeout. Delay the reprep | |
778 | * 1 sec to avoid aggressive retries of the target in that | |
779 | * state. | |
780 | */ | |
781 | #define ALUA_TRANSITION_REPREP_DELAY 1000 | |
782 | ||
da32baea DG |
783 | /* Helper for scsi_io_completion() when special action required. */ |
784 | static void scsi_io_completion_action(struct scsi_cmnd *cmd, int result) | |
1da177e4 | 785 | { |
aa8e25e5 | 786 | struct request *req = scsi_cmd_to_rq(cmd); |
da32baea | 787 | int level = 0; |
54249306 BB |
788 | enum {ACTION_FAIL, ACTION_REPREP, ACTION_DELAYED_REPREP, |
789 | ACTION_RETRY, ACTION_DELAYED_RETRY} action; | |
da32baea DG |
790 | struct scsi_sense_hdr sshdr; |
791 | bool sense_valid; | |
792 | bool sense_current = true; /* false implies "deferred sense" */ | |
793 | blk_status_t blk_stat; | |
1da177e4 | 794 | |
da32baea DG |
795 | sense_valid = scsi_command_normalize_sense(cmd, &sshdr); |
796 | if (sense_valid) | |
797 | sense_current = !scsi_sense_is_deferred(&sshdr); | |
03aba2f7 | 798 | |
7dfaae6a | 799 | blk_stat = scsi_result_to_blk_status(result); |
3e695f89 | 800 | |
b60af5b0 AS |
801 | if (host_byte(result) == DID_RESET) { |
802 | /* Third party bus reset or reset for error recovery | |
803 | * reasons. Just retry the command and see what | |
804 | * happens. | |
805 | */ | |
806 | action = ACTION_RETRY; | |
da32baea | 807 | } else if (sense_valid && sense_current) { |
1da177e4 LT |
808 | switch (sshdr.sense_key) { |
809 | case UNIT_ATTENTION: | |
810 | if (cmd->device->removable) { | |
03aba2f7 | 811 | /* Detected disc change. Set a bit |
1da177e4 LT |
812 | * and quietly refuse further access. |
813 | */ | |
814 | cmd->device->changed = 1; | |
b60af5b0 | 815 | action = ACTION_FAIL; |
1da177e4 | 816 | } else { |
03aba2f7 LT |
817 | /* Must have been a power glitch, or a |
818 | * bus reset. Could not have been a | |
819 | * media change, so we just retry the | |
b60af5b0 | 820 | * command and see what happens. |
03aba2f7 | 821 | */ |
b60af5b0 | 822 | action = ACTION_RETRY; |
1da177e4 LT |
823 | } |
824 | break; | |
825 | case ILLEGAL_REQUEST: | |
03aba2f7 LT |
826 | /* If we had an ILLEGAL REQUEST returned, then |
827 | * we may have performed an unsupported | |
828 | * command. The only thing this should be | |
829 | * would be a ten byte read where only a six | |
830 | * byte read was supported. Also, on a system | |
831 | * where READ CAPACITY failed, we may have | |
832 | * read past the end of the disk. | |
833 | */ | |
26a68019 JA |
834 | if ((cmd->device->use_10_for_rw && |
835 | sshdr.asc == 0x20 && sshdr.ascq == 0x00) && | |
1da177e4 LT |
836 | (cmd->cmnd[0] == READ_10 || |
837 | cmd->cmnd[0] == WRITE_10)) { | |
b60af5b0 | 838 | /* This will issue a new 6-byte command. */ |
1da177e4 | 839 | cmd->device->use_10_for_rw = 0; |
b60af5b0 | 840 | action = ACTION_REPREP; |
3e695f89 | 841 | } else if (sshdr.asc == 0x10) /* DIX */ { |
3e695f89 | 842 | action = ACTION_FAIL; |
da32baea | 843 | blk_stat = BLK_STS_PROTECTION; |
c98a0eb0 | 844 | /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */ |
5db44863 | 845 | } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) { |
c98a0eb0 | 846 | action = ACTION_FAIL; |
da32baea | 847 | blk_stat = BLK_STS_TARGET; |
b60af5b0 AS |
848 | } else |
849 | action = ACTION_FAIL; | |
850 | break; | |
511e44f4 | 851 | case ABORTED_COMMAND: |
126c0982 | 852 | action = ACTION_FAIL; |
e6c11dbb | 853 | if (sshdr.asc == 0x10) /* DIF */ |
da32baea | 854 | blk_stat = BLK_STS_PROTECTION; |
1da177e4 LT |
855 | break; |
856 | case NOT_READY: | |
03aba2f7 | 857 | /* If the device is in the process of becoming |
f3e93f73 | 858 | * ready, or has a temporary blockage, retry. |
1da177e4 | 859 | */ |
f3e93f73 JB |
860 | if (sshdr.asc == 0x04) { |
861 | switch (sshdr.ascq) { | |
862 | case 0x01: /* becoming ready */ | |
863 | case 0x04: /* format in progress */ | |
864 | case 0x05: /* rebuild in progress */ | |
865 | case 0x06: /* recalculation in progress */ | |
866 | case 0x07: /* operation in progress */ | |
867 | case 0x08: /* Long write in progress */ | |
868 | case 0x09: /* self test in progress */ | |
104739ac | 869 | case 0x11: /* notify (enable spinup) required */ |
d8705f11 | 870 | case 0x14: /* space allocation in progress */ |
e37c7d9a DG |
871 | case 0x1a: /* start stop unit in progress */ |
872 | case 0x1b: /* sanitize in progress */ | |
873 | case 0x1d: /* configuration in progress */ | |
b60af5b0 | 874 | action = ACTION_DELAYED_RETRY; |
f3e93f73 | 875 | break; |
0d882320 | 876 | case 0x0a: /* ALUA state transition */ |
54249306 BB |
877 | action = ACTION_DELAYED_REPREP; |
878 | break; | |
9ff7c383 IP |
879 | /* |
880 | * Depopulation might take many hours, | |
881 | * thus it is not worthwhile to retry. | |
882 | */ | |
883 | case 0x24: /* depopulation in progress */ | |
884 | case 0x25: /* depopulation restore in progress */ | |
885 | fallthrough; | |
3dbf6a54 | 886 | default: |
3dbf6a54 AS |
887 | action = ACTION_FAIL; |
888 | break; | |
f3e93f73 | 889 | } |
e6c11dbb | 890 | } else |
b60af5b0 | 891 | action = ACTION_FAIL; |
b60af5b0 | 892 | break; |
1da177e4 | 893 | case VOLUME_OVERFLOW: |
03aba2f7 | 894 | /* See SSC3rXX or current. */ |
b60af5b0 AS |
895 | action = ACTION_FAIL; |
896 | break; | |
d8f53b0a DLM |
897 | case DATA_PROTECT: |
898 | action = ACTION_FAIL; | |
899 | if ((sshdr.asc == 0x0C && sshdr.ascq == 0x12) || | |
900 | (sshdr.asc == 0x55 && | |
901 | (sshdr.ascq == 0x0E || sshdr.ascq == 0x0F))) { | |
902 | /* Insufficient zone resources */ | |
903 | blk_stat = BLK_STS_ZONE_OPEN_RESOURCE; | |
904 | } | |
905 | break; | |
390e2d1a NC |
906 | case COMPLETED: |
907 | fallthrough; | |
1da177e4 | 908 | default: |
b60af5b0 | 909 | action = ACTION_FAIL; |
1da177e4 LT |
910 | break; |
911 | } | |
e6c11dbb | 912 | } else |
b60af5b0 | 913 | action = ACTION_FAIL; |
b60af5b0 | 914 | |
2a242d59 | 915 | if (action != ACTION_FAIL && scsi_cmd_runtime_exceeced(cmd)) |
ee60b2c5 | 916 | action = ACTION_FAIL; |
ee60b2c5 | 917 | |
b60af5b0 AS |
918 | switch (action) { |
919 | case ACTION_FAIL: | |
920 | /* Give up and fail the remainder of the request */ | |
e8064021 | 921 | if (!(req->rq_flags & RQF_QUIET)) { |
f1569ff1 HR |
922 | static DEFINE_RATELIMIT_STATE(_rs, |
923 | DEFAULT_RATELIMIT_INTERVAL, | |
924 | DEFAULT_RATELIMIT_BURST); | |
925 | ||
926 | if (unlikely(scsi_logging_level)) | |
da32baea DG |
927 | level = |
928 | SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT, | |
929 | SCSI_LOG_MLCOMPLETE_BITS); | |
f1569ff1 HR |
930 | |
931 | /* | |
932 | * if logging is enabled the failure will be printed | |
933 | * in scsi_log_completion(), so avoid duplicate messages | |
934 | */ | |
935 | if (!level && __ratelimit(&_rs)) { | |
936 | scsi_print_result(cmd, NULL, FAILED); | |
464a00c9 | 937 | if (sense_valid) |
f1569ff1 HR |
938 | scsi_print_sense(cmd); |
939 | scsi_print_command(cmd); | |
940 | } | |
3173d8c3 | 941 | } |
79478bf9 | 942 | if (!scsi_end_request(req, blk_stat, scsi_rq_err_bytes(req))) |
f6d47e74 | 943 | return; |
df561f66 | 944 | fallthrough; |
b60af5b0 | 945 | case ACTION_REPREP: |
54249306 BB |
946 | scsi_mq_requeue_cmd(cmd, 0); |
947 | break; | |
948 | case ACTION_DELAYED_REPREP: | |
949 | scsi_mq_requeue_cmd(cmd, ALUA_TRANSITION_REPREP_DELAY); | |
b60af5b0 AS |
950 | break; |
951 | case ACTION_RETRY: | |
952 | /* Retry the same command immediately */ | |
08640e81 | 953 | __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, false); |
b60af5b0 AS |
954 | break; |
955 | case ACTION_DELAYED_RETRY: | |
956 | /* Retry the same command after a delay */ | |
08640e81 | 957 | __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, false); |
b60af5b0 | 958 | break; |
1da177e4 LT |
959 | } |
960 | } | |
1da177e4 | 961 | |
ab831084 DG |
962 | /* |
963 | * Helper for scsi_io_completion() when cmd->result is non-zero. Returns a | |
964 | * new result that may suppress further error checking. Also modifies | |
965 | * *blk_statp in some cases. | |
966 | */ | |
967 | static int scsi_io_completion_nz_result(struct scsi_cmnd *cmd, int result, | |
968 | blk_status_t *blk_statp) | |
969 | { | |
970 | bool sense_valid; | |
971 | bool sense_current = true; /* false implies "deferred sense" */ | |
aa8e25e5 | 972 | struct request *req = scsi_cmd_to_rq(cmd); |
ab831084 DG |
973 | struct scsi_sense_hdr sshdr; |
974 | ||
975 | sense_valid = scsi_command_normalize_sense(cmd, &sshdr); | |
976 | if (sense_valid) | |
977 | sense_current = !scsi_sense_is_deferred(&sshdr); | |
978 | ||
979 | if (blk_rq_is_passthrough(req)) { | |
980 | if (sense_valid) { | |
981 | /* | |
982 | * SG_IO wants current and deferred errors | |
983 | */ | |
5b794f98 CH |
984 | cmd->sense_len = min(8 + cmd->sense_buffer[7], |
985 | SCSI_SENSE_BUFFERSIZE); | |
ab831084 DG |
986 | } |
987 | if (sense_current) | |
7dfaae6a | 988 | *blk_statp = scsi_result_to_blk_status(result); |
ab831084 DG |
989 | } else if (blk_rq_bytes(req) == 0 && sense_current) { |
990 | /* | |
991 | * Flush commands do not transfers any data, and thus cannot use | |
992 | * good_bytes != blk_rq_bytes(req) as the signal for an error. | |
993 | * This sets *blk_statp explicitly for the problem case. | |
994 | */ | |
7dfaae6a | 995 | *blk_statp = scsi_result_to_blk_status(result); |
ab831084 DG |
996 | } |
997 | /* | |
998 | * Recovered errors need reporting, but they're always treated as | |
999 | * success, so fiddle the result code here. For passthrough requests | |
1000 | * we already took a copy of the original into sreq->result which | |
1001 | * is what gets returned to the user | |
1002 | */ | |
1003 | if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) { | |
1004 | bool do_print = true; | |
1005 | /* | |
1006 | * if ATA PASS-THROUGH INFORMATION AVAILABLE [0x0, 0x1d] | |
1007 | * skip print since caller wants ATA registers. Only occurs | |
1008 | * on SCSI ATA PASS_THROUGH commands when CK_COND=1 | |
1009 | */ | |
1010 | if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d)) | |
1011 | do_print = false; | |
1012 | else if (req->rq_flags & RQF_QUIET) | |
1013 | do_print = false; | |
1014 | if (do_print) | |
1015 | scsi_print_sense(cmd); | |
1016 | result = 0; | |
1017 | /* for passthrough, *blk_statp may be set */ | |
1018 | *blk_statp = BLK_STS_OK; | |
1019 | } | |
1020 | /* | |
1021 | * Another corner case: the SCSI status byte is non-zero but 'good'. | |
1022 | * Example: PRE-FETCH command returns SAM_STAT_CONDITION_MET when | |
1023 | * it is able to fit nominated LBs in its cache (and SAM_STAT_GOOD | |
1024 | * if it can't fit). Treat SAM_STAT_CONDITION_MET and the related | |
1025 | * intermediate statuses (both obsolete in SAM-4) as good. | |
1026 | */ | |
3d45cefc | 1027 | if ((result & 0xff) && scsi_status_is_good(result)) { |
ab831084 DG |
1028 | result = 0; |
1029 | *blk_statp = BLK_STS_OK; | |
1030 | } | |
1031 | return result; | |
1032 | } | |
1033 | ||
ea941016 AA |
1034 | /** |
1035 | * scsi_io_completion - Completion processing for SCSI commands. | |
1036 | * @cmd: command that is finished. | |
1037 | * @good_bytes: number of processed bytes. | |
1da177e4 | 1038 | * |
ea941016 AA |
1039 | * We will finish off the specified number of sectors. If we are done, the |
1040 | * command block will be released and the queue function will be goosed. If we | |
1041 | * are not done then we have to figure out what to do next: | |
1da177e4 | 1042 | * |
54249306 | 1043 | * a) We can call scsi_mq_requeue_cmd(). The request will be |
ea941016 AA |
1044 | * unprepared and put back on the queue. Then a new command will |
1045 | * be created for it. This should be used if we made forward | |
1046 | * progress, or if we want to switch from READ(10) to READ(6) for | |
1047 | * example. | |
1da177e4 | 1048 | * |
ea941016 AA |
1049 | * b) We can call scsi_io_completion_action(). The request will be |
1050 | * put back on the queue and retried using the same command as | |
1051 | * before, possibly after a delay. | |
b60af5b0 | 1052 | * |
ea941016 AA |
1053 | * c) We can call scsi_end_request() with blk_stat other than |
1054 | * BLK_STS_OK, to fail the remainder of the request. | |
1da177e4 | 1055 | */ |
03aba2f7 | 1056 | void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes) |
1da177e4 LT |
1057 | { |
1058 | int result = cmd->result; | |
aa8e25e5 | 1059 | struct request *req = scsi_cmd_to_rq(cmd); |
1f7cbb8e | 1060 | blk_status_t blk_stat = BLK_STS_OK; |
1da177e4 | 1061 | |
0d437906 | 1062 | if (unlikely(result)) /* a nz result may or may not be an error */ |
ab831084 | 1063 | result = scsi_io_completion_nz_result(cmd, result, &blk_stat); |
631c228c | 1064 | |
1da177e4 LT |
1065 | /* |
1066 | * Next deal with any sectors which we were able to correctly | |
1067 | * handle. | |
1068 | */ | |
91921e01 HR |
1069 | SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd, |
1070 | "%u sectors total, %d bytes done.\n", | |
1071 | blk_rq_sectors(req), good_bytes)); | |
d6b0c537 | 1072 | |
a9bddd74 | 1073 | /* |
ea941016 | 1074 | * Failed, zero length commands always need to drop down |
1f7cbb8e | 1075 | * to retry code. Fast path should return in this block. |
d6b0c537 | 1076 | */ |
0d437906 | 1077 | if (likely(blk_rq_bytes(req) > 0 || blk_stat == BLK_STS_OK)) { |
ae3d56d8 | 1078 | if (likely(!scsi_end_request(req, blk_stat, good_bytes))) |
1f7cbb8e DG |
1079 | return; /* no bytes remaining */ |
1080 | } | |
bc85dc50 | 1081 | |
0d437906 DG |
1082 | /* Kill remainder if no retries. */ |
1083 | if (unlikely(blk_stat && scsi_noretry_cmd(cmd))) { | |
ae3d56d8 | 1084 | if (scsi_end_request(req, blk_stat, blk_rq_bytes(req))) |
8e1695a0 DG |
1085 | WARN_ONCE(true, |
1086 | "Bytes remaining after failed, no-retry command"); | |
f6d47e74 | 1087 | return; |
bc85dc50 CH |
1088 | } |
1089 | ||
1090 | /* | |
1091 | * If there had been no error, but we have leftover bytes in the | |
e9076e7f | 1092 | * request just queue the command up again. |
d6b0c537 | 1093 | */ |
0d437906 | 1094 | if (likely(result == 0)) |
54249306 | 1095 | scsi_mq_requeue_cmd(cmd, 0); |
4ae61c68 | 1096 | else |
da32baea | 1097 | scsi_io_completion_action(cmd, result); |
1da177e4 | 1098 | } |
1da177e4 | 1099 | |
cc97923a CH |
1100 | static inline bool scsi_cmd_needs_dma_drain(struct scsi_device *sdev, |
1101 | struct request *rq) | |
1da177e4 | 1102 | { |
cc97923a CH |
1103 | return sdev->dma_drain_len && blk_rq_is_passthrough(rq) && |
1104 | !op_is_write(req_op(rq)) && | |
1105 | sdev->host->hostt->dma_need_drain(rq); | |
1da177e4 | 1106 | } |
6f9a35e2 | 1107 | |
ea941016 | 1108 | /** |
76fc0df9 BVA |
1109 | * scsi_alloc_sgtables - Allocate and initialize data and integrity scatterlists |
1110 | * @cmd: SCSI command data structure to initialize. | |
1111 | * | |
1112 | * Initializes @cmd->sdb and also @cmd->prot_sdb if data integrity is enabled | |
1113 | * for @cmd. | |
6f9a35e2 | 1114 | * |
ea941016 AA |
1115 | * Returns: |
1116 | * * BLK_STS_OK - on success | |
1117 | * * BLK_STS_RESOURCE - if the failure is retryable | |
1118 | * * BLK_STS_IOERR - if the failure is fatal | |
6f9a35e2 | 1119 | */ |
7007e9dd | 1120 | blk_status_t scsi_alloc_sgtables(struct scsi_cmnd *cmd) |
6f9a35e2 | 1121 | { |
cc97923a | 1122 | struct scsi_device *sdev = cmd->device; |
aa8e25e5 | 1123 | struct request *rq = scsi_cmd_to_rq(cmd); |
cc97923a CH |
1124 | unsigned short nr_segs = blk_rq_nr_phys_segments(rq); |
1125 | struct scatterlist *last_sg = NULL; | |
159b2cbf | 1126 | blk_status_t ret; |
cc97923a | 1127 | bool need_drain = scsi_cmd_needs_dma_drain(sdev, rq); |
0475bd6c | 1128 | int count; |
13f05c8d | 1129 | |
cc97923a | 1130 | if (WARN_ON_ONCE(!nr_segs)) |
159b2cbf | 1131 | return BLK_STS_IOERR; |
635d98b1 | 1132 | |
cc97923a CH |
1133 | /* |
1134 | * Make sure there is space for the drain. The driver must adjust | |
1135 | * max_hw_segments to be prepared for this. | |
1136 | */ | |
1137 | if (need_drain) | |
1138 | nr_segs++; | |
1139 | ||
0475bd6c CH |
1140 | /* |
1141 | * If sg table allocation fails, requeue request later. | |
1142 | */ | |
cc97923a CH |
1143 | if (unlikely(sg_alloc_table_chained(&cmd->sdb.table, nr_segs, |
1144 | cmd->sdb.table.sgl, SCSI_INLINE_SG_CNT))) | |
0475bd6c CH |
1145 | return BLK_STS_RESOURCE; |
1146 | ||
1147 | /* | |
1148 | * Next, walk the list, and fill in the addresses and sizes of | |
1149 | * each segment. | |
1150 | */ | |
75618ac6 | 1151 | count = __blk_rq_map_sg(rq, cmd->sdb.table.sgl, &last_sg); |
cc97923a | 1152 | |
e94b45d0 | 1153 | if (blk_rq_bytes(rq) & rq->q->limits.dma_pad_mask) { |
bdf8710d | 1154 | unsigned int pad_len = |
e94b45d0 | 1155 | (rq->q->limits.dma_pad_mask & ~blk_rq_bytes(rq)) + 1; |
bdf8710d CH |
1156 | |
1157 | last_sg->length += pad_len; | |
1158 | cmd->extra_len += pad_len; | |
1159 | } | |
1160 | ||
cc97923a CH |
1161 | if (need_drain) { |
1162 | sg_unmark_end(last_sg); | |
1163 | last_sg = sg_next(last_sg); | |
1164 | sg_set_buf(last_sg, sdev->dma_drain_buf, sdev->dma_drain_len); | |
1165 | sg_mark_end(last_sg); | |
1166 | ||
bdf8710d | 1167 | cmd->extra_len += sdev->dma_drain_len; |
cc97923a CH |
1168 | count++; |
1169 | } | |
1170 | ||
0475bd6c CH |
1171 | BUG_ON(count > cmd->sdb.table.nents); |
1172 | cmd->sdb.table.nents = count; | |
1173 | cmd->sdb.length = blk_rq_payload_bytes(rq); | |
6f9a35e2 | 1174 | |
13f05c8d | 1175 | if (blk_integrity_rq(rq)) { |
7027ad72 | 1176 | struct scsi_data_buffer *prot_sdb = cmd->prot_sdb; |
7027ad72 | 1177 | |
14784565 | 1178 | if (WARN_ON_ONCE(!prot_sdb)) { |
91724c20 EM |
1179 | /* |
1180 | * This can happen if someone (e.g. multipath) | |
1181 | * queues a command to a device on an adapter | |
1182 | * that does not support DIX. | |
1183 | */ | |
159b2cbf | 1184 | ret = BLK_STS_IOERR; |
14784565 | 1185 | goto out_free_sgtables; |
91724c20 EM |
1186 | } |
1187 | ||
76c313f6 KB |
1188 | if (sg_alloc_table_chained(&prot_sdb->table, |
1189 | rq->nr_integrity_segments, | |
4635873c | 1190 | prot_sdb->table.sgl, |
92524fa1 | 1191 | SCSI_INLINE_PROT_SG_CNT)) { |
159b2cbf | 1192 | ret = BLK_STS_RESOURCE; |
14784565 | 1193 | goto out_free_sgtables; |
7027ad72 MP |
1194 | } |
1195 | ||
76c313f6 | 1196 | count = blk_rq_map_integrity_sg(rq, prot_sdb->table.sgl); |
7027ad72 MP |
1197 | cmd->prot_sdb = prot_sdb; |
1198 | cmd->prot_sdb->table.nents = count; | |
1199 | } | |
1200 | ||
159b2cbf | 1201 | return BLK_STS_OK; |
14784565 | 1202 | out_free_sgtables: |
20a66f2b | 1203 | scsi_free_sgtables(cmd); |
159b2cbf | 1204 | return ret; |
6f9a35e2 | 1205 | } |
7007e9dd | 1206 | EXPORT_SYMBOL(scsi_alloc_sgtables); |
1da177e4 | 1207 | |
ca18d6f7 | 1208 | /** |
832889f5 | 1209 | * scsi_initialize_rq - initialize struct scsi_cmnd partially |
35c0506f | 1210 | * @rq: Request associated with the SCSI command to be initialized. |
ca18d6f7 | 1211 | * |
832889f5 BVA |
1212 | * This function initializes the members of struct scsi_cmnd that must be |
1213 | * initialized before request processing starts and that won't be | |
1214 | * reinitialized if a SCSI command is requeued. | |
ca18d6f7 | 1215 | */ |
e4c9470b | 1216 | static void scsi_initialize_rq(struct request *rq) |
ca18d6f7 | 1217 | { |
c8d9cf22 BVA |
1218 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
1219 | ||
ce70fd9a CH |
1220 | memset(cmd->cmnd, 0, sizeof(cmd->cmnd)); |
1221 | cmd->cmd_len = MAX_COMMAND_SIZE; | |
5b794f98 | 1222 | cmd->sense_len = 0; |
3be8828f | 1223 | init_rcu_head(&cmd->rcu); |
832889f5 BVA |
1224 | cmd->jiffies_at_alloc = jiffies; |
1225 | cmd->retries = 0; | |
ca18d6f7 | 1226 | } |
ca18d6f7 | 1227 | |
39d2112a RD |
1228 | /** |
1229 | * scsi_alloc_request - allocate a block request and partially | |
1230 | * initialize its &scsi_cmnd | |
1231 | * @q: the device's request queue | |
1232 | * @opf: the request operation code | |
1233 | * @flags: block layer allocation flags | |
1234 | * | |
1235 | * Return: &struct request pointer on success or %NULL on failure | |
1236 | */ | |
ea957547 BVA |
1237 | struct request *scsi_alloc_request(struct request_queue *q, blk_opf_t opf, |
1238 | blk_mq_req_flags_t flags) | |
68ec3b81 CH |
1239 | { |
1240 | struct request *rq; | |
1241 | ||
ea957547 | 1242 | rq = blk_mq_alloc_request(q, opf, flags); |
68ec3b81 CH |
1243 | if (!IS_ERR(rq)) |
1244 | scsi_initialize_rq(rq); | |
1245 | return rq; | |
1246 | } | |
1247 | EXPORT_SYMBOL_GPL(scsi_alloc_request); | |
1248 | ||
b7e9e1fb ML |
1249 | /* |
1250 | * Only called when the request isn't completed by SCSI, and not freed by | |
1251 | * SCSI | |
1252 | */ | |
1253 | static void scsi_cleanup_rq(struct request *rq) | |
1254 | { | |
54bebe46 AK |
1255 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
1256 | ||
1257 | cmd->flags = 0; | |
1258 | ||
b7e9e1fb | 1259 | if (rq->rq_flags & RQF_DONTPREP) { |
54bebe46 | 1260 | scsi_mq_uninit_cmd(cmd); |
b7e9e1fb ML |
1261 | rq->rq_flags &= ~RQF_DONTPREP; |
1262 | } | |
1263 | } | |
1264 | ||
65ca846a | 1265 | /* Called before a request is prepared. See also scsi_mq_prep_fn(). */ |
e9c787e6 | 1266 | void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd) |
3b003157 | 1267 | { |
aa8e25e5 | 1268 | struct request *rq = scsi_cmd_to_rq(cmd); |
71bada34 CH |
1269 | |
1270 | if (!blk_rq_is_passthrough(rq) && !(cmd->flags & SCMD_INITIALIZED)) { | |
1271 | cmd->flags |= SCMD_INITIALIZED; | |
64104f70 BVA |
1272 | scsi_initialize_rq(rq); |
1273 | } | |
3b003157 | 1274 | |
e9c787e6 | 1275 | cmd->device = dev; |
5ae17501 | 1276 | INIT_LIST_HEAD(&cmd->eh_entry); |
e9c787e6 | 1277 | INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler); |
3b003157 CH |
1278 | } |
1279 | ||
785ba83b CH |
1280 | static blk_status_t scsi_setup_scsi_cmnd(struct scsi_device *sdev, |
1281 | struct request *req) | |
7b16318d | 1282 | { |
bed2213d | 1283 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); |
3b003157 CH |
1284 | |
1285 | /* | |
aebf526b | 1286 | * Passthrough requests may transfer data, in which case they must |
3b003157 CH |
1287 | * a bio attached to them. Or they might contain a SCSI command |
1288 | * that does not transfer data, in which case they may optionally | |
1289 | * submit a request without an attached bio. | |
1290 | */ | |
1291 | if (req->bio) { | |
7007e9dd | 1292 | blk_status_t ret = scsi_alloc_sgtables(cmd); |
159b2cbf CH |
1293 | if (unlikely(ret != BLK_STS_OK)) |
1294 | return ret; | |
3b003157 | 1295 | } else { |
b0790410 | 1296 | BUG_ON(blk_rq_bytes(req)); |
3b003157 | 1297 | |
30b0c37b | 1298 | memset(&cmd->sdb, 0, sizeof(cmd->sdb)); |
3b003157 | 1299 | } |
7b16318d | 1300 | |
b0790410 | 1301 | cmd->transfersize = blk_rq_bytes(req); |
785ba83b | 1302 | return BLK_STS_OK; |
7b16318d | 1303 | } |
7b16318d | 1304 | |
c092d4ec | 1305 | static blk_status_t |
822bd2db | 1306 | scsi_device_state_check(struct scsi_device *sdev, struct request *req) |
3b003157 | 1307 | { |
c092d4ec | 1308 | switch (sdev->sdev_state) { |
e6044f71 BVA |
1309 | case SDEV_CREATED: |
1310 | return BLK_STS_OK; | |
c092d4ec CH |
1311 | case SDEV_OFFLINE: |
1312 | case SDEV_TRANSPORT_OFFLINE: | |
1313 | /* | |
1314 | * If the device is offline we refuse to process any | |
1315 | * commands. The device must be brought online | |
1316 | * before trying any recovery commands. | |
1317 | */ | |
b0962c53 EM |
1318 | if (!sdev->offline_already) { |
1319 | sdev->offline_already = true; | |
1320 | sdev_printk(KERN_ERR, sdev, | |
1321 | "rejecting I/O to offline device\n"); | |
1322 | } | |
c092d4ec CH |
1323 | return BLK_STS_IOERR; |
1324 | case SDEV_DEL: | |
1325 | /* | |
1326 | * If the device is fully deleted, we refuse to | |
1327 | * process any commands as well. | |
1328 | */ | |
1329 | sdev_printk(KERN_ERR, sdev, | |
1330 | "rejecting I/O to dead device\n"); | |
1331 | return BLK_STS_IOERR; | |
1332 | case SDEV_BLOCK: | |
1333 | case SDEV_CREATED_BLOCK: | |
1334 | return BLK_STS_RESOURCE; | |
1335 | case SDEV_QUIESCE: | |
1336 | /* | |
e6044f71 BVA |
1337 | * If the device is blocked we only accept power management |
1338 | * commands. | |
c092d4ec | 1339 | */ |
e6044f71 | 1340 | if (req && WARN_ON_ONCE(!(req->rq_flags & RQF_PM))) |
c092d4ec CH |
1341 | return BLK_STS_RESOURCE; |
1342 | return BLK_STS_OK; | |
1343 | default: | |
1344 | /* | |
1345 | * For any other not fully online state we only allow | |
e6044f71 | 1346 | * power management commands. |
c092d4ec | 1347 | */ |
e6044f71 | 1348 | if (req && !(req->rq_flags & RQF_PM)) |
9574d434 | 1349 | return BLK_STS_OFFLINE; |
c092d4ec | 1350 | return BLK_STS_OK; |
1da177e4 | 1351 | } |
7f9a6bc4 | 1352 | } |
1da177e4 | 1353 | |
1da177e4 | 1354 | /* |
020b0f0a ML |
1355 | * scsi_dev_queue_ready: if we can send requests to sdev, assign one token |
1356 | * and return the token else return -1. | |
1da177e4 LT |
1357 | */ |
1358 | static inline int scsi_dev_queue_ready(struct request_queue *q, | |
1359 | struct scsi_device *sdev) | |
1360 | { | |
020b0f0a | 1361 | int token; |
71e75c97 | 1362 | |
020b0f0a | 1363 | token = sbitmap_get(&sdev->budget_map); |
3dc985bf WH |
1364 | if (token < 0) |
1365 | return -1; | |
020b0f0a | 1366 | |
3dc985bf WH |
1367 | if (!atomic_read(&sdev->device_blocked)) |
1368 | return token; | |
71e75c97 | 1369 | |
3dc985bf WH |
1370 | /* |
1371 | * Only unblock if no other commands are pending and | |
1372 | * if device_blocked has decreased to zero | |
1373 | */ | |
1374 | if (scsi_device_busy(sdev) > 1 || | |
1375 | atomic_dec_return(&sdev->device_blocked) > 0) { | |
1376 | sbitmap_put(&sdev->budget_map, token); | |
1377 | return -1; | |
1da177e4 | 1378 | } |
71e75c97 | 1379 | |
3dc985bf WH |
1380 | SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev, |
1381 | "unblocking device at zero depth\n")); | |
1382 | ||
020b0f0a | 1383 | return token; |
1da177e4 LT |
1384 | } |
1385 | ||
f0c0a376 MC |
1386 | /* |
1387 | * scsi_target_queue_ready: checks if there we can send commands to target | |
1388 | * @sdev: scsi device on starget to check. | |
f0c0a376 MC |
1389 | */ |
1390 | static inline int scsi_target_queue_ready(struct Scsi_Host *shost, | |
1391 | struct scsi_device *sdev) | |
1392 | { | |
1393 | struct scsi_target *starget = scsi_target(sdev); | |
7ae65c0f | 1394 | unsigned int busy; |
f0c0a376 MC |
1395 | |
1396 | if (starget->single_lun) { | |
7ae65c0f | 1397 | spin_lock_irq(shost->host_lock); |
f0c0a376 | 1398 | if (starget->starget_sdev_user && |
7ae65c0f CH |
1399 | starget->starget_sdev_user != sdev) { |
1400 | spin_unlock_irq(shost->host_lock); | |
1401 | return 0; | |
1402 | } | |
f0c0a376 | 1403 | starget->starget_sdev_user = sdev; |
7ae65c0f | 1404 | spin_unlock_irq(shost->host_lock); |
f0c0a376 MC |
1405 | } |
1406 | ||
2ccbb008 CH |
1407 | if (starget->can_queue <= 0) |
1408 | return 1; | |
1409 | ||
7ae65c0f | 1410 | busy = atomic_inc_return(&starget->target_busy) - 1; |
cd9070c9 | 1411 | if (atomic_read(&starget->target_blocked) > 0) { |
7ae65c0f CH |
1412 | if (busy) |
1413 | goto starved; | |
1414 | ||
f0c0a376 MC |
1415 | /* |
1416 | * unblock after target_blocked iterates to zero | |
1417 | */ | |
cd9070c9 | 1418 | if (atomic_dec_return(&starget->target_blocked) > 0) |
7ae65c0f | 1419 | goto out_dec; |
cf68d334 CH |
1420 | |
1421 | SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget, | |
1422 | "unblocking target at zero depth\n")); | |
f0c0a376 MC |
1423 | } |
1424 | ||
2ccbb008 | 1425 | if (busy >= starget->can_queue) |
7ae65c0f | 1426 | goto starved; |
f0c0a376 | 1427 | |
7ae65c0f CH |
1428 | return 1; |
1429 | ||
1430 | starved: | |
1431 | spin_lock_irq(shost->host_lock); | |
1432 | list_move_tail(&sdev->starved_entry, &shost->starved_list); | |
cf68d334 | 1433 | spin_unlock_irq(shost->host_lock); |
7ae65c0f | 1434 | out_dec: |
2ccbb008 CH |
1435 | if (starget->can_queue > 0) |
1436 | atomic_dec(&starget->target_busy); | |
7ae65c0f | 1437 | return 0; |
f0c0a376 MC |
1438 | } |
1439 | ||
1da177e4 LT |
1440 | /* |
1441 | * scsi_host_queue_ready: if we can send requests to shost, return 1 else | |
1442 | * return 0. We must end up running the queue again whenever 0 is | |
1443 | * returned, else IO can hang. | |
1da177e4 LT |
1444 | */ |
1445 | static inline int scsi_host_queue_ready(struct request_queue *q, | |
1446 | struct Scsi_Host *shost, | |
6eb045e0 ML |
1447 | struct scsi_device *sdev, |
1448 | struct scsi_cmnd *cmd) | |
1da177e4 | 1449 | { |
cd9070c9 | 1450 | if (atomic_read(&shost->host_blocked) > 0) { |
6eb045e0 | 1451 | if (scsi_host_busy(shost) > 0) |
74665016 CH |
1452 | goto starved; |
1453 | ||
1da177e4 LT |
1454 | /* |
1455 | * unblock after host_blocked iterates to zero | |
1456 | */ | |
cd9070c9 | 1457 | if (atomic_dec_return(&shost->host_blocked) > 0) |
74665016 | 1458 | goto out_dec; |
cf68d334 CH |
1459 | |
1460 | SCSI_LOG_MLQUEUE(3, | |
1461 | shost_printk(KERN_INFO, shost, | |
1462 | "unblocking host at zero depth\n")); | |
1da177e4 | 1463 | } |
74665016 | 1464 | |
74665016 CH |
1465 | if (shost->host_self_blocked) |
1466 | goto starved; | |
1da177e4 LT |
1467 | |
1468 | /* We're OK to process the command, so we can't be starved */ | |
74665016 CH |
1469 | if (!list_empty(&sdev->starved_entry)) { |
1470 | spin_lock_irq(shost->host_lock); | |
1471 | if (!list_empty(&sdev->starved_entry)) | |
1472 | list_del_init(&sdev->starved_entry); | |
1473 | spin_unlock_irq(shost->host_lock); | |
1474 | } | |
1da177e4 | 1475 | |
6eb045e0 ML |
1476 | __set_bit(SCMD_STATE_INFLIGHT, &cmd->state); |
1477 | ||
74665016 CH |
1478 | return 1; |
1479 | ||
1480 | starved: | |
1481 | spin_lock_irq(shost->host_lock); | |
1482 | if (list_empty(&sdev->starved_entry)) | |
1483 | list_add_tail(&sdev->starved_entry, &shost->starved_list); | |
cf68d334 | 1484 | spin_unlock_irq(shost->host_lock); |
74665016 | 1485 | out_dec: |
6eb045e0 | 1486 | scsi_dec_host_busy(shost, cmd); |
74665016 | 1487 | return 0; |
1da177e4 LT |
1488 | } |
1489 | ||
6c5121b7 KU |
1490 | /* |
1491 | * Busy state exporting function for request stacking drivers. | |
1492 | * | |
1493 | * For efficiency, no lock is taken to check the busy state of | |
1494 | * shost/starget/sdev, since the returned value is not guaranteed and | |
1495 | * may be changed after request stacking drivers call the function, | |
1496 | * regardless of taking lock or not. | |
1497 | * | |
67bd9413 BVA |
1498 | * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi |
1499 | * needs to return 'not busy'. Otherwise, request stacking drivers | |
1500 | * may hold requests forever. | |
6c5121b7 | 1501 | */ |
f664a3cc | 1502 | static bool scsi_mq_lld_busy(struct request_queue *q) |
6c5121b7 KU |
1503 | { |
1504 | struct scsi_device *sdev = q->queuedata; | |
1505 | struct Scsi_Host *shost; | |
6c5121b7 | 1506 | |
3f3299d5 | 1507 | if (blk_queue_dying(q)) |
f664a3cc | 1508 | return false; |
6c5121b7 KU |
1509 | |
1510 | shost = sdev->host; | |
6c5121b7 | 1511 | |
b7e94a16 JN |
1512 | /* |
1513 | * Ignore host/starget busy state. | |
1514 | * Since block layer does not have a concept of fairness across | |
1515 | * multiple queues, congestion of host/starget needs to be handled | |
1516 | * in SCSI layer. | |
1517 | */ | |
1518 | if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev)) | |
f664a3cc | 1519 | return true; |
e36e0c80 | 1520 | |
f664a3cc | 1521 | return false; |
1da177e4 LT |
1522 | } |
1523 | ||
0d2810cd BVA |
1524 | /* |
1525 | * Block layer request completion callback. May be called from interrupt | |
1526 | * context. | |
1527 | */ | |
1528 | static void scsi_complete(struct request *rq) | |
1aea6434 | 1529 | { |
bed2213d | 1530 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
b8e162f9 | 1531 | enum scsi_disposition disposition; |
1aea6434 JA |
1532 | |
1533 | INIT_LIST_HEAD(&cmd->eh_entry); | |
1534 | ||
242f9dcb JA |
1535 | atomic_inc(&cmd->device->iodone_cnt); |
1536 | if (cmd->result) | |
1537 | atomic_inc(&cmd->device->ioerr_cnt); | |
1538 | ||
1aea6434 | 1539 | disposition = scsi_decide_disposition(cmd); |
2a242d59 | 1540 | if (disposition != SUCCESS && scsi_cmd_runtime_exceeced(cmd)) |
1aea6434 | 1541 | disposition = SUCCESS; |
91921e01 | 1542 | |
1aea6434 JA |
1543 | scsi_log_completion(cmd, disposition); |
1544 | ||
1545 | switch (disposition) { | |
4c7b4d63 BH |
1546 | case SUCCESS: |
1547 | scsi_finish_command(cmd); | |
1548 | break; | |
1549 | case NEEDS_RETRY: | |
1550 | scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY); | |
1551 | break; | |
1552 | case ADD_TO_MLQUEUE: | |
1553 | scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY); | |
1554 | break; | |
1555 | default: | |
1556 | scsi_eh_scmd_add(cmd); | |
1557 | break; | |
1aea6434 JA |
1558 | } |
1559 | } | |
1560 | ||
82042a2c | 1561 | /** |
ae6b4e69 | 1562 | * scsi_dispatch_cmd - Dispatch a command to the low-level driver. |
82042a2c CH |
1563 | * @cmd: command block we are dispatching. |
1564 | * | |
1565 | * Return: nonzero return request was rejected and device's queue needs to be | |
1566 | * plugged. | |
1567 | */ | |
1568 | static int scsi_dispatch_cmd(struct scsi_cmnd *cmd) | |
1569 | { | |
1570 | struct Scsi_Host *host = cmd->device->host; | |
1571 | int rtn = 0; | |
1572 | ||
6ca9818d WH |
1573 | atomic_inc(&cmd->device->iorequest_cnt); |
1574 | ||
82042a2c CH |
1575 | /* check if the device is still usable */ |
1576 | if (unlikely(cmd->device->sdev_state == SDEV_DEL)) { | |
1577 | /* in SDEV_DEL we error all commands. DID_NO_CONNECT | |
1578 | * returns an immediate error upwards, and signals | |
1579 | * that the device is no longer present */ | |
1580 | cmd->result = DID_NO_CONNECT << 16; | |
1581 | goto done; | |
1582 | } | |
1583 | ||
1584 | /* Check to see if the scsi lld made this device blocked. */ | |
1585 | if (unlikely(scsi_device_blocked(cmd->device))) { | |
1586 | /* | |
1587 | * in blocked state, the command is just put back on | |
1588 | * the device queue. The suspend state has already | |
1589 | * blocked the queue so future requests should not | |
1590 | * occur until the device transitions out of the | |
1591 | * suspend state. | |
1592 | */ | |
1593 | SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd, | |
1594 | "queuecommand : device blocked\n")); | |
09e797c8 | 1595 | atomic_dec(&cmd->device->iorequest_cnt); |
82042a2c CH |
1596 | return SCSI_MLQUEUE_DEVICE_BUSY; |
1597 | } | |
1598 | ||
1599 | /* Store the LUN value in cmnd, if needed. */ | |
1600 | if (cmd->device->lun_in_cdb) | |
1601 | cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) | | |
1602 | (cmd->device->lun << 5 & 0xe0); | |
1603 | ||
1604 | scsi_log_send(cmd); | |
1605 | ||
1606 | /* | |
1607 | * Before we queue this command, check if the command | |
1608 | * length exceeds what the host adapter can handle. | |
1609 | */ | |
1610 | if (cmd->cmd_len > cmd->device->host->max_cmd_len) { | |
1611 | SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd, | |
1612 | "queuecommand : command too long. " | |
1613 | "cdb_size=%d host->max_cmd_len=%d\n", | |
1614 | cmd->cmd_len, cmd->device->host->max_cmd_len)); | |
1615 | cmd->result = (DID_ABORT << 16); | |
1616 | goto done; | |
1617 | } | |
1618 | ||
1619 | if (unlikely(host->shost_state == SHOST_DEL)) { | |
1620 | cmd->result = (DID_NO_CONNECT << 16); | |
1621 | goto done; | |
1622 | ||
1623 | } | |
1624 | ||
1625 | trace_scsi_dispatch_cmd_start(cmd); | |
1626 | rtn = host->hostt->queuecommand(host, cmd); | |
1627 | if (rtn) { | |
09e797c8 | 1628 | atomic_dec(&cmd->device->iorequest_cnt); |
82042a2c CH |
1629 | trace_scsi_dispatch_cmd_error(cmd, rtn); |
1630 | if (rtn != SCSI_MLQUEUE_DEVICE_BUSY && | |
1631 | rtn != SCSI_MLQUEUE_TARGET_BUSY) | |
1632 | rtn = SCSI_MLQUEUE_HOST_BUSY; | |
1633 | ||
1634 | SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd, | |
1635 | "queuecommand : request rejected\n")); | |
1636 | } | |
1637 | ||
1638 | return rtn; | |
1639 | done: | |
11b68e36 | 1640 | scsi_done(cmd); |
82042a2c CH |
1641 | return 0; |
1642 | } | |
1643 | ||
be4c186c | 1644 | /* Size in bytes of the sg-list stored in the scsi-mq command-private data. */ |
3dccdf53 | 1645 | static unsigned int scsi_mq_inline_sgl_size(struct Scsi_Host *shost) |
be4c186c | 1646 | { |
3dccdf53 | 1647 | return min_t(unsigned int, shost->sg_tablesize, SCSI_INLINE_SG_CNT) * |
be4c186c BVA |
1648 | sizeof(struct scatterlist); |
1649 | } | |
1650 | ||
5843cc3d | 1651 | static blk_status_t scsi_prepare_cmd(struct request *req) |
d285203c CH |
1652 | { |
1653 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); | |
1654 | struct scsi_device *sdev = req->q->queuedata; | |
1655 | struct Scsi_Host *shost = sdev->host; | |
71bada34 | 1656 | bool in_flight = test_bit(SCMD_STATE_INFLIGHT, &cmd->state); |
d285203c CH |
1657 | struct scatterlist *sg; |
1658 | ||
08f78436 | 1659 | scsi_init_command(sdev, cmd); |
d285203c | 1660 | |
71bada34 | 1661 | cmd->eh_eflags = 0; |
71bada34 CH |
1662 | cmd->prot_type = 0; |
1663 | cmd->prot_flags = 0; | |
1664 | cmd->submitter = 0; | |
71bada34 CH |
1665 | memset(&cmd->sdb, 0, sizeof(cmd->sdb)); |
1666 | cmd->underflow = 0; | |
1667 | cmd->transfersize = 0; | |
1668 | cmd->host_scribble = NULL; | |
1669 | cmd->result = 0; | |
1670 | cmd->extra_len = 0; | |
1671 | cmd->state = 0; | |
1672 | if (in_flight) | |
1673 | __set_bit(SCMD_STATE_INFLIGHT, &cmd->state); | |
1674 | ||
d285203c | 1675 | cmd->prot_op = SCSI_PROT_NORMAL; |
b6ba9b0e CH |
1676 | if (blk_rq_bytes(req)) |
1677 | cmd->sc_data_direction = rq_dma_dir(req); | |
1678 | else | |
1679 | cmd->sc_data_direction = DMA_NONE; | |
d285203c | 1680 | |
d285203c CH |
1681 | sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size; |
1682 | cmd->sdb.table.sgl = sg; | |
1683 | ||
1684 | if (scsi_host_get_prot(shost)) { | |
d285203c CH |
1685 | memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer)); |
1686 | ||
1687 | cmd->prot_sdb->table.sgl = | |
1688 | (struct scatterlist *)(cmd->prot_sdb + 1); | |
1689 | } | |
1690 | ||
74e5e6c1 CH |
1691 | /* |
1692 | * Special handling for passthrough commands, which don't go to the ULP | |
1693 | * at all: | |
1694 | */ | |
da6269da | 1695 | if (blk_rq_is_passthrough(req)) |
74e5e6c1 CH |
1696 | return scsi_setup_scsi_cmnd(sdev, req); |
1697 | ||
1698 | if (sdev->handler && sdev->handler->prep_fn) { | |
1699 | blk_status_t ret = sdev->handler->prep_fn(sdev, req); | |
fe052529 | 1700 | |
74e5e6c1 CH |
1701 | if (ret != BLK_STS_OK) |
1702 | return ret; | |
1703 | } | |
1704 | ||
fac8e558 MC |
1705 | /* Usually overridden by the ULP */ |
1706 | cmd->allowed = 0; | |
ce70fd9a | 1707 | memset(cmd->cmnd, 0, sizeof(cmd->cmnd)); |
74e5e6c1 | 1708 | return scsi_cmd_to_driver(cmd)->init_command(cmd); |
d285203c CH |
1709 | } |
1710 | ||
b84b6ec0 | 1711 | static void scsi_done_internal(struct scsi_cmnd *cmd, bool complete_directly) |
d285203c | 1712 | { |
b84b6ec0 SAS |
1713 | struct request *req = scsi_cmd_to_rq(cmd); |
1714 | ||
bf23e619 BVA |
1715 | switch (cmd->submitter) { |
1716 | case SUBMITTED_BY_BLOCK_LAYER: | |
1717 | break; | |
1718 | case SUBMITTED_BY_SCSI_ERROR_HANDLER: | |
1719 | return scsi_eh_done(cmd); | |
1720 | case SUBMITTED_BY_SCSI_RESET_IOCTL: | |
1721 | return; | |
1722 | } | |
1723 | ||
aa8e25e5 | 1724 | if (unlikely(blk_should_fake_timeout(scsi_cmd_to_rq(cmd)->q))) |
15f73f5b | 1725 | return; |
f1342709 KB |
1726 | if (unlikely(test_and_set_bit(SCMD_STATE_COMPLETE, &cmd->state))) |
1727 | return; | |
d285203c | 1728 | trace_scsi_dispatch_cmd_done(cmd); |
b84b6ec0 SAS |
1729 | |
1730 | if (complete_directly) | |
1731 | blk_mq_complete_request_direct(req, scsi_complete); | |
1732 | else | |
1733 | blk_mq_complete_request(req); | |
1734 | } | |
1735 | ||
1736 | void scsi_done(struct scsi_cmnd *cmd) | |
1737 | { | |
1738 | scsi_done_internal(cmd, false); | |
d285203c | 1739 | } |
a710eacb | 1740 | EXPORT_SYMBOL(scsi_done); |
d285203c | 1741 | |
b84b6ec0 SAS |
1742 | void scsi_done_direct(struct scsi_cmnd *cmd) |
1743 | { | |
1744 | scsi_done_internal(cmd, true); | |
1745 | } | |
1746 | EXPORT_SYMBOL(scsi_done_direct); | |
1747 | ||
2a5a24aa | 1748 | static void scsi_mq_put_budget(struct request_queue *q, int budget_token) |
d285203c | 1749 | { |
0df21c86 | 1750 | struct scsi_device *sdev = q->queuedata; |
0df21c86 | 1751 | |
020b0f0a | 1752 | sbitmap_put(&sdev->budget_map, budget_token); |
0df21c86 ML |
1753 | } |
1754 | ||
90552cd2 BVA |
1755 | /* |
1756 | * When to reinvoke queueing after a resource shortage. It's 3 msecs to | |
1757 | * not change behaviour from the previous unplug mechanism, experimentation | |
1758 | * may prove this needs changing. | |
1759 | */ | |
1760 | #define SCSI_QUEUE_DELAY 3 | |
1761 | ||
2a5a24aa | 1762 | static int scsi_mq_get_budget(struct request_queue *q) |
0df21c86 | 1763 | { |
d285203c | 1764 | struct scsi_device *sdev = q->queuedata; |
020b0f0a | 1765 | int token = scsi_dev_queue_ready(q, sdev); |
d285203c | 1766 | |
020b0f0a ML |
1767 | if (token >= 0) |
1768 | return token; | |
ed5dd6a6 ML |
1769 | |
1770 | atomic_inc(&sdev->restarts); | |
1771 | ||
1772 | /* | |
1773 | * Orders atomic_inc(&sdev->restarts) and atomic_read(&sdev->device_busy). | |
1774 | * .restarts must be incremented before .device_busy is read because the | |
1775 | * code in scsi_run_queue_async() depends on the order of these operations. | |
1776 | */ | |
1777 | smp_mb__after_atomic(); | |
1778 | ||
1779 | /* | |
1780 | * If all in-flight requests originated from this LUN are completed | |
1781 | * before reading .device_busy, sdev->device_busy will be observed as | |
1782 | * zero, then blk_mq_delay_run_hw_queues() will dispatch this request | |
1783 | * soon. Otherwise, completion of one of these requests will observe | |
1784 | * the .restarts flag, and the request queue will be run for handling | |
1785 | * this request, see scsi_end_request(). | |
1786 | */ | |
8278807a | 1787 | if (unlikely(scsi_device_busy(sdev) == 0 && |
ed5dd6a6 ML |
1788 | !scsi_device_blocked(sdev))) |
1789 | blk_mq_delay_run_hw_queues(sdev->request_queue, SCSI_QUEUE_DELAY); | |
2a5a24aa | 1790 | return -1; |
0df21c86 ML |
1791 | } |
1792 | ||
d022d18c ML |
1793 | static void scsi_mq_set_rq_budget_token(struct request *req, int token) |
1794 | { | |
1795 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); | |
1796 | ||
1797 | cmd->budget_token = token; | |
1798 | } | |
1799 | ||
1800 | static int scsi_mq_get_rq_budget_token(struct request *req) | |
1801 | { | |
1802 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); | |
1803 | ||
1804 | return cmd->budget_token; | |
0df21c86 ML |
1805 | } |
1806 | ||
fc17b653 | 1807 | static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx, |
74c45052 | 1808 | const struct blk_mq_queue_data *bd) |
d285203c | 1809 | { |
74c45052 | 1810 | struct request *req = bd->rq; |
d285203c CH |
1811 | struct request_queue *q = req->q; |
1812 | struct scsi_device *sdev = q->queuedata; | |
1813 | struct Scsi_Host *shost = sdev->host; | |
1814 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); | |
fc17b653 | 1815 | blk_status_t ret; |
d285203c CH |
1816 | int reason; |
1817 | ||
2a5a24aa ML |
1818 | WARN_ON_ONCE(cmd->budget_token < 0); |
1819 | ||
c092d4ec CH |
1820 | /* |
1821 | * If the device is not in running state we will reject some or all | |
1822 | * commands. | |
1823 | */ | |
1824 | if (unlikely(sdev->sdev_state != SDEV_RUNNING)) { | |
822bd2db | 1825 | ret = scsi_device_state_check(sdev, req); |
c092d4ec CH |
1826 | if (ret != BLK_STS_OK) |
1827 | goto out_put_budget; | |
1828 | } | |
d285203c | 1829 | |
fc17b653 | 1830 | ret = BLK_STS_RESOURCE; |
d285203c | 1831 | if (!scsi_target_queue_ready(shost, sdev)) |
826a70a0 | 1832 | goto out_put_budget; |
310bcaef BVA |
1833 | if (unlikely(scsi_host_in_recovery(shost))) { |
1834 | if (cmd->flags & SCMD_FAIL_IF_RECOVERING) | |
1835 | ret = BLK_STS_OFFLINE; | |
1836 | goto out_dec_target_busy; | |
1837 | } | |
6eb045e0 | 1838 | if (!scsi_host_queue_ready(q, shost, sdev, cmd)) |
d285203c CH |
1839 | goto out_dec_target_busy; |
1840 | ||
dce5c4af YB |
1841 | /* |
1842 | * Only clear the driver-private command data if the LLD does not supply | |
1843 | * a function to initialize that data. | |
1844 | */ | |
1845 | if (shost->hostt->cmd_size && !shost->hostt->init_cmd_priv) | |
1846 | memset(cmd + 1, 0, shost->hostt->cmd_size); | |
1847 | ||
e8064021 | 1848 | if (!(req->rq_flags & RQF_DONTPREP)) { |
5843cc3d | 1849 | ret = scsi_prepare_cmd(req); |
fc17b653 | 1850 | if (ret != BLK_STS_OK) |
d285203c | 1851 | goto out_dec_host_busy; |
e8064021 | 1852 | req->rq_flags |= RQF_DONTPREP; |
fe052529 | 1853 | } else { |
cd464d83 | 1854 | clear_bit(SCMD_STATE_COMPLETE, &cmd->state); |
d285203c CH |
1855 | } |
1856 | ||
8930a6c2 | 1857 | cmd->flags &= SCMD_PRESERVED_FLAGS; |
125c99bc CH |
1858 | if (sdev->simple_tags) |
1859 | cmd->flags |= SCMD_TAGGED; | |
8930a6c2 PB |
1860 | if (bd->last) |
1861 | cmd->flags |= SCMD_LAST; | |
b1dd2aac | 1862 | |
3a8dc5bb CH |
1863 | scsi_set_resid(cmd, 0); |
1864 | memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); | |
bf23e619 | 1865 | cmd->submitter = SUBMITTED_BY_BLOCK_LAYER; |
d285203c | 1866 | |
ed7fb2d0 | 1867 | blk_mq_start_request(req); |
d285203c CH |
1868 | reason = scsi_dispatch_cmd(cmd); |
1869 | if (reason) { | |
1870 | scsi_set_blocked(cmd, reason); | |
fc17b653 | 1871 | ret = BLK_STS_RESOURCE; |
d285203c CH |
1872 | goto out_dec_host_busy; |
1873 | } | |
1874 | ||
fc17b653 | 1875 | return BLK_STS_OK; |
d285203c CH |
1876 | |
1877 | out_dec_host_busy: | |
6eb045e0 | 1878 | scsi_dec_host_busy(shost, cmd); |
d285203c CH |
1879 | out_dec_target_busy: |
1880 | if (scsi_target(sdev)->can_queue > 0) | |
1881 | atomic_dec(&scsi_target(sdev)->target_busy); | |
0df21c86 | 1882 | out_put_budget: |
2a5a24aa ML |
1883 | scsi_mq_put_budget(q, cmd->budget_token); |
1884 | cmd->budget_token = -1; | |
d285203c | 1885 | switch (ret) { |
fc17b653 CH |
1886 | case BLK_STS_OK: |
1887 | break; | |
1888 | case BLK_STS_RESOURCE: | |
673235f9 | 1889 | if (scsi_device_blocked(sdev)) |
86ff7c2a | 1890 | ret = BLK_STS_DEV_RESOURCE; |
d285203c | 1891 | break; |
268940b8 | 1892 | case BLK_STS_AGAIN: |
dbb4c84d | 1893 | cmd->result = DID_BUS_BUSY << 16; |
268940b8 HR |
1894 | if (req->rq_flags & RQF_DONTPREP) |
1895 | scsi_mq_uninit_cmd(cmd); | |
1896 | break; | |
fc17b653 | 1897 | default: |
be549d49 | 1898 | if (unlikely(!scsi_device_online(sdev))) |
dbb4c84d | 1899 | cmd->result = DID_NO_CONNECT << 16; |
be549d49 | 1900 | else |
dbb4c84d | 1901 | cmd->result = DID_ERROR << 16; |
d285203c | 1902 | /* |
be549d49 | 1903 | * Make sure to release all allocated resources when |
d285203c CH |
1904 | * we hit an error, as we will never see this command |
1905 | * again. | |
1906 | */ | |
e8064021 | 1907 | if (req->rq_flags & RQF_DONTPREP) |
d285203c | 1908 | scsi_mq_uninit_cmd(cmd); |
3f0dcfbc | 1909 | scsi_run_queue_async(sdev); |
d285203c | 1910 | break; |
d285203c CH |
1911 | } |
1912 | return ret; | |
1913 | } | |
1914 | ||
e7008ff5 BVA |
1915 | static int scsi_mq_init_request(struct blk_mq_tag_set *set, struct request *rq, |
1916 | unsigned int hctx_idx, unsigned int numa_node) | |
d285203c | 1917 | { |
d6296d39 | 1918 | struct Scsi_Host *shost = set->driver_data; |
d285203c | 1919 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
08f78436 | 1920 | struct scatterlist *sg; |
65ca846a | 1921 | int ret = 0; |
d285203c | 1922 | |
aaff5eba CH |
1923 | cmd->sense_buffer = |
1924 | kmem_cache_alloc_node(scsi_sense_cache, GFP_KERNEL, numa_node); | |
d285203c CH |
1925 | if (!cmd->sense_buffer) |
1926 | return -ENOMEM; | |
08f78436 BVA |
1927 | |
1928 | if (scsi_host_get_prot(shost)) { | |
1929 | sg = (void *)cmd + sizeof(struct scsi_cmnd) + | |
1930 | shost->hostt->cmd_size; | |
3dccdf53 | 1931 | cmd->prot_sdb = (void *)sg + scsi_mq_inline_sgl_size(shost); |
08f78436 BVA |
1932 | } |
1933 | ||
65ca846a BVA |
1934 | if (shost->hostt->init_cmd_priv) { |
1935 | ret = shost->hostt->init_cmd_priv(shost, cmd); | |
1936 | if (ret < 0) | |
aaff5eba | 1937 | kmem_cache_free(scsi_sense_cache, cmd->sense_buffer); |
65ca846a BVA |
1938 | } |
1939 | ||
1940 | return ret; | |
d285203c CH |
1941 | } |
1942 | ||
e7008ff5 BVA |
1943 | static void scsi_mq_exit_request(struct blk_mq_tag_set *set, struct request *rq, |
1944 | unsigned int hctx_idx) | |
d285203c | 1945 | { |
65ca846a | 1946 | struct Scsi_Host *shost = set->driver_data; |
d285203c CH |
1947 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
1948 | ||
65ca846a BVA |
1949 | if (shost->hostt->exit_cmd_priv) |
1950 | shost->hostt->exit_cmd_priv(shost, cmd); | |
aaff5eba | 1951 | kmem_cache_free(scsi_sense_cache, cmd->sense_buffer); |
d285203c CH |
1952 | } |
1953 | ||
af183095 | 1954 | |
5a72e899 | 1955 | static int scsi_mq_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob) |
af183095 | 1956 | { |
4309ea74 | 1957 | struct Scsi_Host *shost = hctx->driver_data; |
af183095 KD |
1958 | |
1959 | if (shost->hostt->mq_poll) | |
1960 | return shost->hostt->mq_poll(shost, hctx->queue_num); | |
1961 | ||
1962 | return 0; | |
1963 | } | |
1964 | ||
4309ea74 KD |
1965 | static int scsi_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
1966 | unsigned int hctx_idx) | |
1967 | { | |
1968 | struct Scsi_Host *shost = data; | |
1969 | ||
1970 | hctx->driver_data = shost; | |
1971 | return 0; | |
1972 | } | |
1973 | ||
a4e1d0b7 | 1974 | static void scsi_map_queues(struct blk_mq_tag_set *set) |
2d9c5c20 CH |
1975 | { |
1976 | struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set); | |
1977 | ||
1978 | if (shost->hostt->map_queues) | |
1979 | return shost->hostt->map_queues(shost); | |
a4e1d0b7 | 1980 | blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); |
2d9c5c20 CH |
1981 | } |
1982 | ||
afd53a3d | 1983 | void scsi_init_limits(struct Scsi_Host *shost, struct queue_limits *lim) |
1da177e4 | 1984 | { |
6f381fa3 | 1985 | struct device *dev = shost->dma_dev; |
1da177e4 | 1986 | |
afd53a3d CH |
1987 | memset(lim, 0, sizeof(*lim)); |
1988 | lim->max_segments = | |
1989 | min_t(unsigned short, shost->sg_tablesize, SG_MAX_SEGMENTS); | |
a8474ce2 | 1990 | |
13f05c8d MP |
1991 | if (scsi_host_prot_dma(shost)) { |
1992 | shost->sg_prot_tablesize = | |
1993 | min_not_zero(shost->sg_prot_tablesize, | |
1994 | (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS); | |
1995 | BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize); | |
afd53a3d | 1996 | lim->max_integrity_segments = shost->sg_prot_tablesize; |
13f05c8d MP |
1997 | } |
1998 | ||
afd53a3d CH |
1999 | lim->max_hw_sectors = shost->max_sectors; |
2000 | lim->seg_boundary_mask = shost->dma_boundary; | |
2001 | lim->max_segment_size = shost->max_segment_size; | |
2002 | lim->virt_boundary_mask = shost->virt_boundary_mask; | |
5b7dfbef CH |
2003 | lim->dma_alignment = max_t(unsigned int, |
2004 | shost->dma_alignment, dma_get_cache_alignment() - 1); | |
1da177e4 | 2005 | |
b5a73bf4 CH |
2006 | /* |
2007 | * Propagate the DMA formation properties to the dma-mapping layer as | |
2008 | * a courtesy service to the LLDDs. This needs to check that the buses | |
2009 | * actually support the DMA API first, though. | |
2010 | */ | |
2011 | if (dev->dma_parms) { | |
2012 | dma_set_seg_boundary(dev, shost->dma_boundary); | |
2013 | dma_set_max_seg_size(dev, shost->max_segment_size); | |
2014 | } | |
d285203c | 2015 | } |
afd53a3d | 2016 | EXPORT_SYMBOL_GPL(scsi_init_limits); |
465ff318 | 2017 | |
8930a6c2 PB |
2018 | static const struct blk_mq_ops scsi_mq_ops_no_commit = { |
2019 | .get_budget = scsi_mq_get_budget, | |
2020 | .put_budget = scsi_mq_put_budget, | |
2021 | .queue_rq = scsi_queue_rq, | |
0d2810cd | 2022 | .complete = scsi_complete, |
8930a6c2 PB |
2023 | .timeout = scsi_timeout, |
2024 | #ifdef CONFIG_BLK_DEBUG_FS | |
2025 | .show_rq = scsi_show_rq, | |
2026 | #endif | |
2027 | .init_request = scsi_mq_init_request, | |
2028 | .exit_request = scsi_mq_exit_request, | |
82a9ac71 | 2029 | .cleanup_rq = scsi_cleanup_rq, |
8930a6c2 PB |
2030 | .busy = scsi_mq_lld_busy, |
2031 | .map_queues = scsi_map_queues, | |
4309ea74 | 2032 | .init_hctx = scsi_init_hctx, |
af183095 | 2033 | .poll = scsi_mq_poll, |
d022d18c ML |
2034 | .set_rq_budget_token = scsi_mq_set_rq_budget_token, |
2035 | .get_rq_budget_token = scsi_mq_get_rq_budget_token, | |
8930a6c2 PB |
2036 | }; |
2037 | ||
2038 | ||
2039 | static void scsi_commit_rqs(struct blk_mq_hw_ctx *hctx) | |
2040 | { | |
4309ea74 | 2041 | struct Scsi_Host *shost = hctx->driver_data; |
8930a6c2 PB |
2042 | |
2043 | shost->hostt->commit_rqs(shost, hctx->queue_num); | |
2044 | } | |
2045 | ||
f363b089 | 2046 | static const struct blk_mq_ops scsi_mq_ops = { |
0df21c86 ML |
2047 | .get_budget = scsi_mq_get_budget, |
2048 | .put_budget = scsi_mq_put_budget, | |
d285203c | 2049 | .queue_rq = scsi_queue_rq, |
8930a6c2 | 2050 | .commit_rqs = scsi_commit_rqs, |
0d2810cd | 2051 | .complete = scsi_complete, |
0152fb6b | 2052 | .timeout = scsi_timeout, |
0eebd005 BVA |
2053 | #ifdef CONFIG_BLK_DEBUG_FS |
2054 | .show_rq = scsi_show_rq, | |
2055 | #endif | |
e7008ff5 BVA |
2056 | .init_request = scsi_mq_init_request, |
2057 | .exit_request = scsi_mq_exit_request, | |
b7e9e1fb | 2058 | .cleanup_rq = scsi_cleanup_rq, |
3a7ea2c4 | 2059 | .busy = scsi_mq_lld_busy, |
2d9c5c20 | 2060 | .map_queues = scsi_map_queues, |
4309ea74 | 2061 | .init_hctx = scsi_init_hctx, |
af183095 | 2062 | .poll = scsi_mq_poll, |
d022d18c ML |
2063 | .set_rq_budget_token = scsi_mq_set_rq_budget_token, |
2064 | .get_rq_budget_token = scsi_mq_get_rq_budget_token, | |
d285203c CH |
2065 | }; |
2066 | ||
d285203c CH |
2067 | int scsi_mq_setup_tags(struct Scsi_Host *shost) |
2068 | { | |
be4c186c | 2069 | unsigned int cmd_size, sgl_size; |
840e1b55 | 2070 | struct blk_mq_tag_set *tag_set = &shost->tag_set; |
d285203c | 2071 | |
9393c8de MS |
2072 | sgl_size = max_t(unsigned int, sizeof(struct scatterlist), |
2073 | scsi_mq_inline_sgl_size(shost)); | |
d285203c CH |
2074 | cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size; |
2075 | if (scsi_host_get_prot(shost)) | |
92524fa1 ML |
2076 | cmd_size += sizeof(struct scsi_data_buffer) + |
2077 | sizeof(struct scatterlist) * SCSI_INLINE_PROT_SG_CNT; | |
d285203c | 2078 | |
840e1b55 | 2079 | memset(tag_set, 0, sizeof(*tag_set)); |
8930a6c2 | 2080 | if (shost->hostt->commit_rqs) |
840e1b55 | 2081 | tag_set->ops = &scsi_mq_ops; |
8930a6c2 | 2082 | else |
840e1b55 YB |
2083 | tag_set->ops = &scsi_mq_ops_no_commit; |
2084 | tag_set->nr_hw_queues = shost->nr_hw_queues ? : 1; | |
af183095 | 2085 | tag_set->nr_maps = shost->nr_maps ? : 1; |
840e1b55 YB |
2086 | tag_set->queue_depth = shost->can_queue; |
2087 | tag_set->cmd_size = cmd_size; | |
973dac8a | 2088 | tag_set->numa_node = dev_to_node(shost->dma_dev); |
ce32496e CH |
2089 | if (shost->hostt->tag_alloc_policy_rr) |
2090 | tag_set->flags |= BLK_MQ_F_TAG_RR; | |
b125bb99 BVA |
2091 | if (shost->queuecommand_may_block) |
2092 | tag_set->flags |= BLK_MQ_F_BLOCKING; | |
840e1b55 | 2093 | tag_set->driver_data = shost; |
bdb01301 HR |
2094 | if (shost->host_tagset) |
2095 | tag_set->flags |= BLK_MQ_F_TAG_HCTX_SHARED; | |
d285203c | 2096 | |
840e1b55 | 2097 | return blk_mq_alloc_tag_set(tag_set); |
d285203c CH |
2098 | } |
2099 | ||
8fe4ce58 | 2100 | void scsi_mq_free_tags(struct kref *kref) |
d285203c | 2101 | { |
8fe4ce58 BVA |
2102 | struct Scsi_Host *shost = container_of(kref, typeof(*shost), |
2103 | tagset_refcnt); | |
2104 | ||
d285203c | 2105 | blk_mq_free_tag_set(&shost->tag_set); |
8fe4ce58 | 2106 | complete(&shost->tagset_freed); |
d285203c CH |
2107 | } |
2108 | ||
857de6e0 HR |
2109 | /** |
2110 | * scsi_device_from_queue - return sdev associated with a request_queue | |
2111 | * @q: The request queue to return the sdev from | |
2112 | * | |
2113 | * Return the sdev associated with a request queue or NULL if the | |
2114 | * request_queue does not reference a SCSI device. | |
2115 | */ | |
2116 | struct scsi_device *scsi_device_from_queue(struct request_queue *q) | |
2117 | { | |
2118 | struct scsi_device *sdev = NULL; | |
2119 | ||
6b6fa7a5 SM |
2120 | if (q->mq_ops == &scsi_mq_ops_no_commit || |
2121 | q->mq_ops == &scsi_mq_ops) | |
857de6e0 HR |
2122 | sdev = q->queuedata; |
2123 | if (!sdev || !get_device(&sdev->sdev_gendev)) | |
2124 | sdev = NULL; | |
2125 | ||
2126 | return sdev; | |
2127 | } | |
4845012e CH |
2128 | /* |
2129 | * pktcdvd should have been integrated into the SCSI layers, but for historical | |
2130 | * reasons like the old IDE driver it isn't. This export allows it to safely | |
2131 | * probe if a given device is a SCSI one and only attach to that. | |
2132 | */ | |
2133 | #ifdef CONFIG_CDROM_PKTCDVD_MODULE | |
2134 | EXPORT_SYMBOL_GPL(scsi_device_from_queue); | |
2135 | #endif | |
857de6e0 | 2136 | |
ea941016 AA |
2137 | /** |
2138 | * scsi_block_requests - Utility function used by low-level drivers to prevent | |
2139 | * further commands from being queued to the device. | |
2140 | * @shost: host in question | |
1da177e4 | 2141 | * |
ea941016 AA |
2142 | * There is no timer nor any other means by which the requests get unblocked |
2143 | * other than the low-level driver calling scsi_unblock_requests(). | |
1da177e4 LT |
2144 | */ |
2145 | void scsi_block_requests(struct Scsi_Host *shost) | |
2146 | { | |
2147 | shost->host_self_blocked = 1; | |
2148 | } | |
2149 | EXPORT_SYMBOL(scsi_block_requests); | |
2150 | ||
ea941016 AA |
2151 | /** |
2152 | * scsi_unblock_requests - Utility function used by low-level drivers to allow | |
2153 | * further commands to be queued to the device. | |
2154 | * @shost: host in question | |
1da177e4 | 2155 | * |
ea941016 AA |
2156 | * There is no timer nor any other means by which the requests get unblocked |
2157 | * other than the low-level driver calling scsi_unblock_requests(). This is done | |
2158 | * as an API function so that changes to the internals of the scsi mid-layer | |
2159 | * won't require wholesale changes to drivers that use this feature. | |
1da177e4 LT |
2160 | */ |
2161 | void scsi_unblock_requests(struct Scsi_Host *shost) | |
2162 | { | |
2163 | shost->host_self_blocked = 0; | |
2164 | scsi_run_host_queues(shost); | |
2165 | } | |
2166 | EXPORT_SYMBOL(scsi_unblock_requests); | |
2167 | ||
1da177e4 LT |
2168 | void scsi_exit_queue(void) |
2169 | { | |
0a6ac4ee | 2170 | kmem_cache_destroy(scsi_sense_cache); |
1da177e4 | 2171 | } |
5baba830 JB |
2172 | |
2173 | /** | |
2174 | * scsi_mode_select - issue a mode select | |
2175 | * @sdev: SCSI device to be queried | |
2176 | * @pf: Page format bit (1 == standard, 0 == vendor specific) | |
2177 | * @sp: Save page bit (0 == don't save, 1 == save) | |
5baba830 JB |
2178 | * @buffer: request buffer (may not be smaller than eight bytes) |
2179 | * @len: length of request buffer. | |
2180 | * @timeout: command timeout | |
2181 | * @retries: number of retries before failing | |
2182 | * @data: returns a structure abstracting the mode header data | |
eb44820c | 2183 | * @sshdr: place to put sense data (or NULL if no sense to be collected). |
5baba830 JB |
2184 | * must be SCSI_SENSE_BUFFERSIZE big. |
2185 | * | |
2186 | * Returns zero if successful; negative error number or scsi | |
2187 | * status on error | |
2188 | * | |
2189 | */ | |
81d3f500 DLM |
2190 | int scsi_mode_select(struct scsi_device *sdev, int pf, int sp, |
2191 | unsigned char *buffer, int len, int timeout, int retries, | |
2192 | struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) | |
5baba830 JB |
2193 | { |
2194 | unsigned char cmd[10]; | |
2195 | unsigned char *real_buffer; | |
7dfe0b5e MC |
2196 | const struct scsi_exec_args exec_args = { |
2197 | .sshdr = sshdr, | |
2198 | }; | |
5baba830 JB |
2199 | int ret; |
2200 | ||
2201 | memset(cmd, 0, sizeof(cmd)); | |
2202 | cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0); | |
2203 | ||
a7d6840b DLM |
2204 | /* |
2205 | * Use MODE SELECT(10) if the device asked for it or if the mode page | |
2206 | * and the mode select header cannot fit within the maximumm 255 bytes | |
2207 | * of the MODE SELECT(6) command. | |
2208 | */ | |
2209 | if (sdev->use_10_for_ms || | |
2210 | len + 4 > 255 || | |
2211 | data->block_descriptor_length > 255) { | |
2212 | if (len > 65535 - 8) | |
5baba830 JB |
2213 | return -EINVAL; |
2214 | real_buffer = kmalloc(8 + len, GFP_KERNEL); | |
2215 | if (!real_buffer) | |
2216 | return -ENOMEM; | |
2217 | memcpy(real_buffer + 8, buffer, len); | |
2218 | len += 8; | |
2219 | real_buffer[0] = 0; | |
2220 | real_buffer[1] = 0; | |
2221 | real_buffer[2] = data->medium_type; | |
2222 | real_buffer[3] = data->device_specific; | |
2223 | real_buffer[4] = data->longlba ? 0x01 : 0; | |
2224 | real_buffer[5] = 0; | |
a7d6840b DLM |
2225 | put_unaligned_be16(data->block_descriptor_length, |
2226 | &real_buffer[6]); | |
5baba830 JB |
2227 | |
2228 | cmd[0] = MODE_SELECT_10; | |
a7d6840b | 2229 | put_unaligned_be16(len, &cmd[7]); |
5baba830 | 2230 | } else { |
a7d6840b | 2231 | if (data->longlba) |
5baba830 JB |
2232 | return -EINVAL; |
2233 | ||
2234 | real_buffer = kmalloc(4 + len, GFP_KERNEL); | |
2235 | if (!real_buffer) | |
2236 | return -ENOMEM; | |
2237 | memcpy(real_buffer + 4, buffer, len); | |
2238 | len += 4; | |
2239 | real_buffer[0] = 0; | |
2240 | real_buffer[1] = data->medium_type; | |
2241 | real_buffer[2] = data->device_specific; | |
2242 | real_buffer[3] = data->block_descriptor_length; | |
5baba830 JB |
2243 | |
2244 | cmd[0] = MODE_SELECT; | |
2245 | cmd[4] = len; | |
2246 | } | |
2247 | ||
7dfe0b5e MC |
2248 | ret = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_OUT, real_buffer, len, |
2249 | timeout, retries, &exec_args); | |
5baba830 JB |
2250 | kfree(real_buffer); |
2251 | return ret; | |
2252 | } | |
2253 | EXPORT_SYMBOL_GPL(scsi_mode_select); | |
2254 | ||
1da177e4 | 2255 | /** |
eb44820c | 2256 | * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary. |
1cf72699 | 2257 | * @sdev: SCSI device to be queried |
17b49bcb | 2258 | * @dbd: set to prevent mode sense from returning block descriptors |
1da177e4 | 2259 | * @modepage: mode page being requested |
a6cdc35f | 2260 | * @subpage: sub-page of the mode page being requested |
1da177e4 LT |
2261 | * @buffer: request buffer (may not be smaller than eight bytes) |
2262 | * @len: length of request buffer. | |
2263 | * @timeout: command timeout | |
2264 | * @retries: number of retries before failing | |
2265 | * @data: returns a structure abstracting the mode header data | |
eb44820c | 2266 | * @sshdr: place to put sense data (or NULL if no sense to be collected). |
1cf72699 | 2267 | * must be SCSI_SENSE_BUFFERSIZE big. |
1da177e4 | 2268 | * |
8793613d | 2269 | * Returns zero if successful, or a negative error number on failure |
eb44820c | 2270 | */ |
1da177e4 | 2271 | int |
a6cdc35f | 2272 | scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage, int subpage, |
1da177e4 | 2273 | unsigned char *buffer, int len, int timeout, int retries, |
5baba830 JB |
2274 | struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) |
2275 | { | |
1da177e4 LT |
2276 | unsigned char cmd[12]; |
2277 | int use_10_for_ms; | |
2278 | int header_length; | |
21bdff48 | 2279 | int result; |
ea73a9f2 | 2280 | struct scsi_sense_hdr my_sshdr; |
21bdff48 MC |
2281 | struct scsi_failure failure_defs[] = { |
2282 | { | |
2283 | .sense = UNIT_ATTENTION, | |
2284 | .asc = SCMD_FAILURE_ASC_ANY, | |
2285 | .ascq = SCMD_FAILURE_ASCQ_ANY, | |
2286 | .allowed = retries, | |
2287 | .result = SAM_STAT_CHECK_CONDITION, | |
2288 | }, | |
2289 | {} | |
2290 | }; | |
2291 | struct scsi_failures failures = { | |
2292 | .failure_definitions = failure_defs, | |
2293 | }; | |
7dfe0b5e MC |
2294 | const struct scsi_exec_args exec_args = { |
2295 | /* caller might not be interested in sense, but we need it */ | |
2296 | .sshdr = sshdr ? : &my_sshdr, | |
21bdff48 | 2297 | .failures = &failures, |
7dfe0b5e | 2298 | }; |
1da177e4 LT |
2299 | |
2300 | memset(data, 0, sizeof(*data)); | |
2301 | memset(&cmd[0], 0, 12); | |
0ec96913 CG |
2302 | |
2303 | dbd = sdev->set_dbd_for_ms ? 8 : dbd; | |
1da177e4 LT |
2304 | cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */ |
2305 | cmd[2] = modepage; | |
a6cdc35f | 2306 | cmd[3] = subpage; |
1da177e4 | 2307 | |
7dfe0b5e | 2308 | sshdr = exec_args.sshdr; |
ea73a9f2 | 2309 | |
1da177e4 | 2310 | retry: |
17b49bcb | 2311 | use_10_for_ms = sdev->use_10_for_ms || len > 255; |
1da177e4 LT |
2312 | |
2313 | if (use_10_for_ms) { | |
17b49bcb DLM |
2314 | if (len < 8 || len > 65535) |
2315 | return -EINVAL; | |
1da177e4 LT |
2316 | |
2317 | cmd[0] = MODE_SENSE_10; | |
17b49bcb | 2318 | put_unaligned_be16(len, &cmd[7]); |
1da177e4 LT |
2319 | header_length = 8; |
2320 | } else { | |
2321 | if (len < 4) | |
17b49bcb | 2322 | return -EINVAL; |
1da177e4 LT |
2323 | |
2324 | cmd[0] = MODE_SENSE; | |
2325 | cmd[4] = len; | |
2326 | header_length = 4; | |
2327 | } | |
2328 | ||
1da177e4 LT |
2329 | memset(buffer, 0, len); |
2330 | ||
7dfe0b5e MC |
2331 | result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, buffer, len, |
2332 | timeout, retries, &exec_args); | |
8793613d HR |
2333 | if (result < 0) |
2334 | return result; | |
1da177e4 LT |
2335 | |
2336 | /* This code looks awful: what it's doing is making sure an | |
2337 | * ILLEGAL REQUEST sense return identifies the actual command | |
2338 | * byte as the problem. MODE_SENSE commands can return | |
2339 | * ILLEGAL REQUEST if the code page isn't supported */ | |
2340 | ||
64aaa3dd | 2341 | if (!scsi_status_is_good(result)) { |
464a00c9 | 2342 | if (scsi_sense_valid(sshdr)) { |
ea73a9f2 JB |
2343 | if ((sshdr->sense_key == ILLEGAL_REQUEST) && |
2344 | (sshdr->asc == 0x20) && (sshdr->ascq == 0)) { | |
4c7b4d63 | 2345 | /* |
17b49bcb DLM |
2346 | * Invalid command operation code: retry using |
2347 | * MODE SENSE(6) if this was a MODE SENSE(10) | |
2348 | * request, except if the request mode page is | |
2349 | * too large for MODE SENSE single byte | |
2350 | * allocation length field. | |
1da177e4 | 2351 | */ |
64aaa3dd | 2352 | if (use_10_for_ms) { |
17b49bcb DLM |
2353 | if (len > 255) |
2354 | return -EIO; | |
64aaa3dd HR |
2355 | sdev->use_10_for_ms = 0; |
2356 | goto retry; | |
2357 | } | |
2358 | } | |
1da177e4 | 2359 | } |
64aaa3dd | 2360 | return -EIO; |
1da177e4 | 2361 | } |
64aaa3dd HR |
2362 | if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b && |
2363 | (modepage == 6 || modepage == 8))) { | |
2364 | /* Initio breakage? */ | |
2365 | header_length = 0; | |
2366 | data->length = 13; | |
2367 | data->medium_type = 0; | |
2368 | data->device_specific = 0; | |
2369 | data->longlba = 0; | |
2370 | data->block_descriptor_length = 0; | |
2371 | } else if (use_10_for_ms) { | |
17b49bcb | 2372 | data->length = get_unaligned_be16(&buffer[0]) + 2; |
64aaa3dd HR |
2373 | data->medium_type = buffer[2]; |
2374 | data->device_specific = buffer[3]; | |
2375 | data->longlba = buffer[4] & 0x01; | |
17b49bcb | 2376 | data->block_descriptor_length = get_unaligned_be16(&buffer[6]); |
64aaa3dd HR |
2377 | } else { |
2378 | data->length = buffer[0] + 1; | |
2379 | data->medium_type = buffer[1]; | |
2380 | data->device_specific = buffer[2]; | |
2381 | data->block_descriptor_length = buffer[3]; | |
1da177e4 | 2382 | } |
64aaa3dd HR |
2383 | data->header_length = header_length; |
2384 | ||
2385 | return 0; | |
1da177e4 LT |
2386 | } |
2387 | EXPORT_SYMBOL(scsi_mode_sense); | |
2388 | ||
001aac25 JB |
2389 | /** |
2390 | * scsi_test_unit_ready - test if unit is ready | |
2391 | * @sdev: scsi device to change the state of. | |
2392 | * @timeout: command timeout | |
2393 | * @retries: number of retries before failing | |
74a78ebd | 2394 | * @sshdr: outpout pointer for decoded sense information. |
001aac25 JB |
2395 | * |
2396 | * Returns zero if unsuccessful or an error if TUR failed. For | |
9f8a2c23 | 2397 | * removable media, UNIT_ATTENTION sets ->changed flag. |
001aac25 | 2398 | **/ |
1da177e4 | 2399 | int |
001aac25 | 2400 | scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries, |
74a78ebd | 2401 | struct scsi_sense_hdr *sshdr) |
1da177e4 | 2402 | { |
1da177e4 LT |
2403 | char cmd[] = { |
2404 | TEST_UNIT_READY, 0, 0, 0, 0, 0, | |
2405 | }; | |
7dfe0b5e MC |
2406 | const struct scsi_exec_args exec_args = { |
2407 | .sshdr = sshdr, | |
2408 | }; | |
1da177e4 | 2409 | int result; |
001aac25 | 2410 | |
001aac25 JB |
2411 | /* try to eat the UNIT_ATTENTION if there are enough retries */ |
2412 | do { | |
7dfe0b5e MC |
2413 | result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, NULL, 0, |
2414 | timeout, 1, &exec_args); | |
f43158ee | 2415 | if (sdev->removable && result > 0 && scsi_sense_valid(sshdr) && |
32c356d7 JB |
2416 | sshdr->sense_key == UNIT_ATTENTION) |
2417 | sdev->changed = 1; | |
f43158ee | 2418 | } while (result > 0 && scsi_sense_valid(sshdr) && |
32c356d7 | 2419 | sshdr->sense_key == UNIT_ATTENTION && --retries); |
001aac25 | 2420 | |
1da177e4 LT |
2421 | return result; |
2422 | } | |
2423 | EXPORT_SYMBOL(scsi_test_unit_ready); | |
2424 | ||
2425 | /** | |
eb44820c | 2426 | * scsi_device_set_state - Take the given device through the device state model. |
1da177e4 LT |
2427 | * @sdev: scsi device to change the state of. |
2428 | * @state: state to change to. | |
2429 | * | |
23cb27fd | 2430 | * Returns zero if successful or an error if the requested |
1da177e4 | 2431 | * transition is illegal. |
eb44820c | 2432 | */ |
1da177e4 LT |
2433 | int |
2434 | scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state) | |
2435 | { | |
2436 | enum scsi_device_state oldstate = sdev->sdev_state; | |
2437 | ||
2438 | if (state == oldstate) | |
2439 | return 0; | |
2440 | ||
2441 | switch (state) { | |
2442 | case SDEV_CREATED: | |
6f4267e3 JB |
2443 | switch (oldstate) { |
2444 | case SDEV_CREATED_BLOCK: | |
2445 | break; | |
2446 | default: | |
2447 | goto illegal; | |
2448 | } | |
2449 | break; | |
4c7b4d63 | 2450 | |
1da177e4 LT |
2451 | case SDEV_RUNNING: |
2452 | switch (oldstate) { | |
2453 | case SDEV_CREATED: | |
2454 | case SDEV_OFFLINE: | |
1b8d2620 | 2455 | case SDEV_TRANSPORT_OFFLINE: |
1da177e4 LT |
2456 | case SDEV_QUIESCE: |
2457 | case SDEV_BLOCK: | |
2458 | break; | |
2459 | default: | |
2460 | goto illegal; | |
2461 | } | |
2462 | break; | |
2463 | ||
2464 | case SDEV_QUIESCE: | |
2465 | switch (oldstate) { | |
2466 | case SDEV_RUNNING: | |
2467 | case SDEV_OFFLINE: | |
1b8d2620 | 2468 | case SDEV_TRANSPORT_OFFLINE: |
1da177e4 LT |
2469 | break; |
2470 | default: | |
2471 | goto illegal; | |
2472 | } | |
2473 | break; | |
2474 | ||
2475 | case SDEV_OFFLINE: | |
1b8d2620 | 2476 | case SDEV_TRANSPORT_OFFLINE: |
1da177e4 LT |
2477 | switch (oldstate) { |
2478 | case SDEV_CREATED: | |
2479 | case SDEV_RUNNING: | |
2480 | case SDEV_QUIESCE: | |
2481 | case SDEV_BLOCK: | |
2482 | break; | |
2483 | default: | |
2484 | goto illegal; | |
2485 | } | |
2486 | break; | |
2487 | ||
2488 | case SDEV_BLOCK: | |
2489 | switch (oldstate) { | |
1da177e4 | 2490 | case SDEV_RUNNING: |
6f4267e3 | 2491 | case SDEV_CREATED_BLOCK: |
6cbb7aed | 2492 | case SDEV_QUIESCE: |
a33e5bfb | 2493 | case SDEV_OFFLINE: |
6f4267e3 JB |
2494 | break; |
2495 | default: | |
2496 | goto illegal; | |
2497 | } | |
2498 | break; | |
2499 | ||
2500 | case SDEV_CREATED_BLOCK: | |
2501 | switch (oldstate) { | |
2502 | case SDEV_CREATED: | |
1da177e4 LT |
2503 | break; |
2504 | default: | |
2505 | goto illegal; | |
2506 | } | |
2507 | break; | |
2508 | ||
2509 | case SDEV_CANCEL: | |
2510 | switch (oldstate) { | |
2511 | case SDEV_CREATED: | |
2512 | case SDEV_RUNNING: | |
9ea72909 | 2513 | case SDEV_QUIESCE: |
1da177e4 | 2514 | case SDEV_OFFLINE: |
1b8d2620 | 2515 | case SDEV_TRANSPORT_OFFLINE: |
1da177e4 LT |
2516 | break; |
2517 | default: | |
2518 | goto illegal; | |
2519 | } | |
2520 | break; | |
2521 | ||
2522 | case SDEV_DEL: | |
2523 | switch (oldstate) { | |
309bd271 BK |
2524 | case SDEV_CREATED: |
2525 | case SDEV_RUNNING: | |
2526 | case SDEV_OFFLINE: | |
1b8d2620 | 2527 | case SDEV_TRANSPORT_OFFLINE: |
1da177e4 | 2528 | case SDEV_CANCEL: |
255ee932 | 2529 | case SDEV_BLOCK: |
0516c08d | 2530 | case SDEV_CREATED_BLOCK: |
1da177e4 LT |
2531 | break; |
2532 | default: | |
2533 | goto illegal; | |
2534 | } | |
2535 | break; | |
2536 | ||
2537 | } | |
b0962c53 | 2538 | sdev->offline_already = false; |
1da177e4 LT |
2539 | sdev->sdev_state = state; |
2540 | return 0; | |
2541 | ||
2542 | illegal: | |
91921e01 | 2543 | SCSI_LOG_ERROR_RECOVERY(1, |
9ccfc756 | 2544 | sdev_printk(KERN_ERR, sdev, |
91921e01 | 2545 | "Illegal state transition %s->%s", |
9ccfc756 JB |
2546 | scsi_device_state_name(oldstate), |
2547 | scsi_device_state_name(state)) | |
1da177e4 LT |
2548 | ); |
2549 | return -EINVAL; | |
2550 | } | |
2551 | EXPORT_SYMBOL(scsi_device_set_state); | |
2552 | ||
a341cd0f | 2553 | /** |
ae6b4e69 | 2554 | * scsi_evt_emit - emit a single SCSI device uevent |
a341cd0f JG |
2555 | * @sdev: associated SCSI device |
2556 | * @evt: event to emit | |
2557 | * | |
2558 | * Send a single uevent (scsi_event) to the associated scsi_device. | |
2559 | */ | |
2560 | static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt) | |
2561 | { | |
2562 | int idx = 0; | |
2563 | char *envp[3]; | |
2564 | ||
2565 | switch (evt->evt_type) { | |
2566 | case SDEV_EVT_MEDIA_CHANGE: | |
2567 | envp[idx++] = "SDEV_MEDIA_CHANGE=1"; | |
2568 | break; | |
279afdfe | 2569 | case SDEV_EVT_INQUIRY_CHANGE_REPORTED: |
79519528 | 2570 | scsi_rescan_device(sdev); |
279afdfe EM |
2571 | envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED"; |
2572 | break; | |
2573 | case SDEV_EVT_CAPACITY_CHANGE_REPORTED: | |
2574 | envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED"; | |
2575 | break; | |
2576 | case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED: | |
2577 | envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED"; | |
2578 | break; | |
2579 | case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED: | |
2580 | envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED"; | |
2581 | break; | |
2582 | case SDEV_EVT_LUN_CHANGE_REPORTED: | |
2583 | envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED"; | |
2584 | break; | |
14c3e677 HR |
2585 | case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED: |
2586 | envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED"; | |
2587 | break; | |
cf3431bb HR |
2588 | case SDEV_EVT_POWER_ON_RESET_OCCURRED: |
2589 | envp[idx++] = "SDEV_UA=POWER_ON_RESET_OCCURRED"; | |
2590 | break; | |
a341cd0f JG |
2591 | default: |
2592 | /* do nothing */ | |
2593 | break; | |
2594 | } | |
2595 | ||
2596 | envp[idx++] = NULL; | |
2597 | ||
2598 | kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp); | |
2599 | } | |
2600 | ||
2601 | /** | |
ae6b4e69 | 2602 | * scsi_evt_thread - send a uevent for each scsi event |
a341cd0f JG |
2603 | * @work: work struct for scsi_device |
2604 | * | |
2605 | * Dispatch queued events to their associated scsi_device kobjects | |
2606 | * as uevents. | |
2607 | */ | |
2608 | void scsi_evt_thread(struct work_struct *work) | |
2609 | { | |
2610 | struct scsi_device *sdev; | |
279afdfe | 2611 | enum scsi_device_event evt_type; |
a341cd0f JG |
2612 | LIST_HEAD(event_list); |
2613 | ||
2614 | sdev = container_of(work, struct scsi_device, event_work); | |
2615 | ||
279afdfe EM |
2616 | for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++) |
2617 | if (test_and_clear_bit(evt_type, sdev->pending_events)) | |
2618 | sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL); | |
2619 | ||
a341cd0f JG |
2620 | while (1) { |
2621 | struct scsi_event *evt; | |
2622 | struct list_head *this, *tmp; | |
2623 | unsigned long flags; | |
2624 | ||
2625 | spin_lock_irqsave(&sdev->list_lock, flags); | |
2626 | list_splice_init(&sdev->event_list, &event_list); | |
2627 | spin_unlock_irqrestore(&sdev->list_lock, flags); | |
2628 | ||
2629 | if (list_empty(&event_list)) | |
2630 | break; | |
2631 | ||
2632 | list_for_each_safe(this, tmp, &event_list) { | |
2633 | evt = list_entry(this, struct scsi_event, node); | |
2634 | list_del(&evt->node); | |
2635 | scsi_evt_emit(sdev, evt); | |
2636 | kfree(evt); | |
2637 | } | |
2638 | } | |
2639 | } | |
2640 | ||
2641 | /** | |
2642 | * sdev_evt_send - send asserted event to uevent thread | |
2643 | * @sdev: scsi_device event occurred on | |
2644 | * @evt: event to send | |
2645 | * | |
2646 | * Assert scsi device event asynchronously. | |
2647 | */ | |
2648 | void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt) | |
2649 | { | |
2650 | unsigned long flags; | |
2651 | ||
4d1566ed KS |
2652 | #if 0 |
2653 | /* FIXME: currently this check eliminates all media change events | |
2654 | * for polled devices. Need to update to discriminate between AN | |
2655 | * and polled events */ | |
a341cd0f JG |
2656 | if (!test_bit(evt->evt_type, sdev->supported_events)) { |
2657 | kfree(evt); | |
2658 | return; | |
2659 | } | |
4d1566ed | 2660 | #endif |
a341cd0f JG |
2661 | |
2662 | spin_lock_irqsave(&sdev->list_lock, flags); | |
2663 | list_add_tail(&evt->node, &sdev->event_list); | |
2664 | schedule_work(&sdev->event_work); | |
2665 | spin_unlock_irqrestore(&sdev->list_lock, flags); | |
2666 | } | |
2667 | EXPORT_SYMBOL_GPL(sdev_evt_send); | |
2668 | ||
2669 | /** | |
2670 | * sdev_evt_alloc - allocate a new scsi event | |
2671 | * @evt_type: type of event to allocate | |
2672 | * @gfpflags: GFP flags for allocation | |
2673 | * | |
2674 | * Allocates and returns a new scsi_event. | |
2675 | */ | |
2676 | struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type, | |
2677 | gfp_t gfpflags) | |
2678 | { | |
2679 | struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags); | |
2680 | if (!evt) | |
2681 | return NULL; | |
2682 | ||
2683 | evt->evt_type = evt_type; | |
2684 | INIT_LIST_HEAD(&evt->node); | |
2685 | ||
2686 | /* evt_type-specific initialization, if any */ | |
2687 | switch (evt_type) { | |
2688 | case SDEV_EVT_MEDIA_CHANGE: | |
279afdfe EM |
2689 | case SDEV_EVT_INQUIRY_CHANGE_REPORTED: |
2690 | case SDEV_EVT_CAPACITY_CHANGE_REPORTED: | |
2691 | case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED: | |
2692 | case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED: | |
2693 | case SDEV_EVT_LUN_CHANGE_REPORTED: | |
14c3e677 | 2694 | case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED: |
cf3431bb | 2695 | case SDEV_EVT_POWER_ON_RESET_OCCURRED: |
a341cd0f JG |
2696 | default: |
2697 | /* do nothing */ | |
2698 | break; | |
2699 | } | |
2700 | ||
2701 | return evt; | |
2702 | } | |
2703 | EXPORT_SYMBOL_GPL(sdev_evt_alloc); | |
2704 | ||
2705 | /** | |
2706 | * sdev_evt_send_simple - send asserted event to uevent thread | |
2707 | * @sdev: scsi_device event occurred on | |
2708 | * @evt_type: type of event to send | |
2709 | * @gfpflags: GFP flags for allocation | |
2710 | * | |
2711 | * Assert scsi device event asynchronously, given an event type. | |
2712 | */ | |
2713 | void sdev_evt_send_simple(struct scsi_device *sdev, | |
2714 | enum scsi_device_event evt_type, gfp_t gfpflags) | |
2715 | { | |
2716 | struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags); | |
2717 | if (!evt) { | |
2718 | sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n", | |
2719 | evt_type); | |
2720 | return; | |
2721 | } | |
2722 | ||
2723 | sdev_evt_send(sdev, evt); | |
2724 | } | |
2725 | EXPORT_SYMBOL_GPL(sdev_evt_send_simple); | |
2726 | ||
1da177e4 | 2727 | /** |
e6044f71 | 2728 | * scsi_device_quiesce - Block all commands except power management. |
1da177e4 LT |
2729 | * @sdev: scsi device to quiesce. |
2730 | * | |
2731 | * This works by trying to transition to the SDEV_QUIESCE state | |
2732 | * (which must be a legal transition). When the device is in this | |
e6044f71 BVA |
2733 | * state, only power management requests will be accepted, all others will |
2734 | * be deferred. | |
1da177e4 LT |
2735 | * |
2736 | * Must be called with user context, may sleep. | |
2737 | * | |
2738 | * Returns zero if unsuccessful or an error if not. | |
eb44820c | 2739 | */ |
1da177e4 LT |
2740 | int |
2741 | scsi_device_quiesce(struct scsi_device *sdev) | |
2742 | { | |
3a0a5299 | 2743 | struct request_queue *q = sdev->request_queue; |
1e1a9cec | 2744 | unsigned int memflags; |
0db6ca8a BVA |
2745 | int err; |
2746 | ||
3a0a5299 BVA |
2747 | /* |
2748 | * It is allowed to call scsi_device_quiesce() multiple times from | |
2749 | * the same context but concurrent scsi_device_quiesce() calls are | |
2750 | * not allowed. | |
2751 | */ | |
2752 | WARN_ON_ONCE(sdev->quiesced_by && sdev->quiesced_by != current); | |
2753 | ||
cd84a62e BVA |
2754 | if (sdev->quiesced_by == current) |
2755 | return 0; | |
2756 | ||
2757 | blk_set_pm_only(q); | |
3a0a5299 | 2758 | |
1e1a9cec | 2759 | memflags = blk_mq_freeze_queue(q); |
3a0a5299 | 2760 | /* |
cd84a62e | 2761 | * Ensure that the effect of blk_set_pm_only() will be visible |
3a0a5299 BVA |
2762 | * for percpu_ref_tryget() callers that occur after the queue |
2763 | * unfreeze even if the queue was already frozen before this function | |
2764 | * was called. See also https://lwn.net/Articles/573497/. | |
2765 | */ | |
2766 | synchronize_rcu(); | |
1e1a9cec | 2767 | blk_mq_unfreeze_queue(q, memflags); |
3a0a5299 | 2768 | |
0db6ca8a BVA |
2769 | mutex_lock(&sdev->state_mutex); |
2770 | err = scsi_device_set_state(sdev, SDEV_QUIESCE); | |
3a0a5299 BVA |
2771 | if (err == 0) |
2772 | sdev->quiesced_by = current; | |
2773 | else | |
cd84a62e | 2774 | blk_clear_pm_only(q); |
0db6ca8a BVA |
2775 | mutex_unlock(&sdev->state_mutex); |
2776 | ||
3a0a5299 | 2777 | return err; |
1da177e4 LT |
2778 | } |
2779 | EXPORT_SYMBOL(scsi_device_quiesce); | |
2780 | ||
2781 | /** | |
2782 | * scsi_device_resume - Restart user issued commands to a quiesced device. | |
2783 | * @sdev: scsi device to resume. | |
2784 | * | |
2785 | * Moves the device from quiesced back to running and restarts the | |
2786 | * queues. | |
2787 | * | |
2788 | * Must be called with user context, may sleep. | |
eb44820c | 2789 | */ |
a7a20d10 | 2790 | void scsi_device_resume(struct scsi_device *sdev) |
1da177e4 | 2791 | { |
a7a20d10 DW |
2792 | /* check if the device state was mutated prior to resume, and if |
2793 | * so assume the state is being managed elsewhere (for example | |
2794 | * device deleted during suspend) | |
2795 | */ | |
0db6ca8a | 2796 | mutex_lock(&sdev->state_mutex); |
e6044f71 BVA |
2797 | if (sdev->sdev_state == SDEV_QUIESCE) |
2798 | scsi_device_set_state(sdev, SDEV_RUNNING); | |
17605afa BVA |
2799 | if (sdev->quiesced_by) { |
2800 | sdev->quiesced_by = NULL; | |
2801 | blk_clear_pm_only(sdev->request_queue); | |
2802 | } | |
0db6ca8a | 2803 | mutex_unlock(&sdev->state_mutex); |
1da177e4 LT |
2804 | } |
2805 | EXPORT_SYMBOL(scsi_device_resume); | |
2806 | ||
2807 | static void | |
2808 | device_quiesce_fn(struct scsi_device *sdev, void *data) | |
2809 | { | |
2810 | scsi_device_quiesce(sdev); | |
2811 | } | |
2812 | ||
2813 | void | |
2814 | scsi_target_quiesce(struct scsi_target *starget) | |
2815 | { | |
2816 | starget_for_each_device(starget, NULL, device_quiesce_fn); | |
2817 | } | |
2818 | EXPORT_SYMBOL(scsi_target_quiesce); | |
2819 | ||
2820 | static void | |
2821 | device_resume_fn(struct scsi_device *sdev, void *data) | |
2822 | { | |
2823 | scsi_device_resume(sdev); | |
2824 | } | |
2825 | ||
2826 | void | |
2827 | scsi_target_resume(struct scsi_target *starget) | |
2828 | { | |
2829 | starget_for_each_device(starget, NULL, device_resume_fn); | |
2830 | } | |
2831 | EXPORT_SYMBOL(scsi_target_resume); | |
2832 | ||
d2b9f12b ML |
2833 | static int __scsi_internal_device_block_nowait(struct scsi_device *sdev) |
2834 | { | |
2835 | if (scsi_device_set_state(sdev, SDEV_BLOCK)) | |
2836 | return scsi_device_set_state(sdev, SDEV_CREATED_BLOCK); | |
2837 | ||
2838 | return 0; | |
2839 | } | |
2840 | ||
93542fbf ML |
2841 | void scsi_start_queue(struct scsi_device *sdev) |
2842 | { | |
2843 | if (cmpxchg(&sdev->queue_stopped, 1, 0)) | |
2844 | blk_mq_unquiesce_queue(sdev->request_queue); | |
2845 | } | |
2846 | ||
d7035b73 | 2847 | static void scsi_stop_queue(struct scsi_device *sdev) |
93542fbf ML |
2848 | { |
2849 | /* | |
2850 | * The atomic variable of ->queue_stopped covers that | |
2851 | * blk_mq_quiesce_queue* is balanced with blk_mq_unquiesce_queue. | |
2852 | * | |
d7035b73 | 2853 | * The caller needs to wait until quiesce is done. |
93542fbf | 2854 | */ |
d7035b73 MW |
2855 | if (!cmpxchg(&sdev->queue_stopped, 0, 1)) |
2856 | blk_mq_quiesce_queue_nowait(sdev->request_queue); | |
93542fbf ML |
2857 | } |
2858 | ||
1da177e4 | 2859 | /** |
551eb598 BVA |
2860 | * scsi_internal_device_block_nowait - try to transition to the SDEV_BLOCK state |
2861 | * @sdev: device to block | |
1da177e4 | 2862 | * |
551eb598 | 2863 | * Pause SCSI command processing on the specified device. Does not sleep. |
1da177e4 | 2864 | * |
551eb598 | 2865 | * Returns zero if successful or a negative error code upon failure. |
669f0441 | 2866 | * |
551eb598 BVA |
2867 | * Notes: |
2868 | * This routine transitions the device to the SDEV_BLOCK state (which must be | |
2869 | * a legal transition). When the device is in this state, command processing | |
2870 | * is paused until the device leaves the SDEV_BLOCK state. See also | |
2871 | * scsi_internal_device_unblock_nowait(). | |
eb44820c | 2872 | */ |
551eb598 | 2873 | int scsi_internal_device_block_nowait(struct scsi_device *sdev) |
1da177e4 | 2874 | { |
d2b9f12b | 2875 | int ret = __scsi_internal_device_block_nowait(sdev); |
1da177e4 | 2876 | |
4c7b4d63 | 2877 | /* |
1da177e4 LT |
2878 | * The device has transitioned to SDEV_BLOCK. Stop the |
2879 | * block layer from calling the midlayer with this device's | |
4c7b4d63 | 2880 | * request queue. |
1da177e4 | 2881 | */ |
d2b9f12b | 2882 | if (!ret) |
d7035b73 | 2883 | scsi_stop_queue(sdev); |
d2b9f12b | 2884 | return ret; |
1da177e4 | 2885 | } |
551eb598 BVA |
2886 | EXPORT_SYMBOL_GPL(scsi_internal_device_block_nowait); |
2887 | ||
1da177e4 | 2888 | /** |
c5e46f7a | 2889 | * scsi_device_block - try to transition to the SDEV_BLOCK state |
551eb598 | 2890 | * @sdev: device to block |
c5e46f7a | 2891 | * @data: dummy argument, ignored |
551eb598 | 2892 | * |
e20fff8a MW |
2893 | * Pause SCSI command processing on the specified device. Callers must wait |
2894 | * until all ongoing scsi_queue_rq() calls have finished after this function | |
2895 | * returns. | |
1da177e4 | 2896 | * |
551eb598 BVA |
2897 | * Note: |
2898 | * This routine transitions the device to the SDEV_BLOCK state (which must be | |
2899 | * a legal transition). When the device is in this state, command processing | |
2900 | * is paused until the device leaves the SDEV_BLOCK state. See also | |
2901 | * scsi_internal_device_unblock(). | |
eb44820c | 2902 | */ |
c5e46f7a | 2903 | static void scsi_device_block(struct scsi_device *sdev, void *data) |
1da177e4 | 2904 | { |
551eb598 | 2905 | int err; |
6d7160c7 | 2906 | enum scsi_device_state state; |
551eb598 | 2907 | |
0db6ca8a | 2908 | mutex_lock(&sdev->state_mutex); |
d2b9f12b | 2909 | err = __scsi_internal_device_block_nowait(sdev); |
6d7160c7 | 2910 | state = sdev->sdev_state; |
f664a3cc | 2911 | if (err == 0) |
d7035b73 MW |
2912 | /* |
2913 | * scsi_stop_queue() must be called with the state_mutex | |
2914 | * held. Otherwise a simultaneous scsi_start_queue() call | |
2915 | * might unquiesce the queue before we quiesce it. | |
2916 | */ | |
2917 | scsi_stop_queue(sdev); | |
e20fff8a | 2918 | |
0db6ca8a BVA |
2919 | mutex_unlock(&sdev->state_mutex); |
2920 | ||
6d7160c7 MW |
2921 | WARN_ONCE(err, "%s: failed to block %s in state %d\n", |
2922 | __func__, dev_name(&sdev->sdev_gendev), state); | |
551eb598 | 2923 | } |
4c7b4d63 | 2924 | |
1da177e4 | 2925 | /** |
43f7571b | 2926 | * scsi_internal_device_unblock_nowait - resume a device after a block request |
1da177e4 | 2927 | * @sdev: device to resume |
43f7571b | 2928 | * @new_state: state to set the device to after unblocking |
1da177e4 | 2929 | * |
43f7571b BVA |
2930 | * Restart the device queue for a previously suspended SCSI device. Does not |
2931 | * sleep. | |
1da177e4 | 2932 | * |
43f7571b | 2933 | * Returns zero if successful or a negative error code upon failure. |
1da177e4 | 2934 | * |
43f7571b BVA |
2935 | * Notes: |
2936 | * This routine transitions the device to the SDEV_RUNNING state or to one of | |
2937 | * the offline states (which must be a legal transition) allowing the midlayer | |
2938 | * to goose the queue for this device. | |
eb44820c | 2939 | */ |
43f7571b BVA |
2940 | int scsi_internal_device_unblock_nowait(struct scsi_device *sdev, |
2941 | enum scsi_device_state new_state) | |
1da177e4 | 2942 | { |
09addb1d BVA |
2943 | switch (new_state) { |
2944 | case SDEV_RUNNING: | |
2945 | case SDEV_TRANSPORT_OFFLINE: | |
2946 | break; | |
2947 | default: | |
2948 | return -EINVAL; | |
2949 | } | |
2950 | ||
5d9fb5cc MC |
2951 | /* |
2952 | * Try to transition the scsi device to SDEV_RUNNING or one of the | |
2953 | * offlined states and goose the device queue if successful. | |
1da177e4 | 2954 | */ |
8cd1ec78 HR |
2955 | switch (sdev->sdev_state) { |
2956 | case SDEV_BLOCK: | |
2957 | case SDEV_TRANSPORT_OFFLINE: | |
5d9fb5cc | 2958 | sdev->sdev_state = new_state; |
8cd1ec78 HR |
2959 | break; |
2960 | case SDEV_CREATED_BLOCK: | |
5d9fb5cc MC |
2961 | if (new_state == SDEV_TRANSPORT_OFFLINE || |
2962 | new_state == SDEV_OFFLINE) | |
2963 | sdev->sdev_state = new_state; | |
2964 | else | |
2965 | sdev->sdev_state = SDEV_CREATED; | |
8cd1ec78 HR |
2966 | break; |
2967 | case SDEV_CANCEL: | |
2968 | case SDEV_OFFLINE: | |
2969 | break; | |
2970 | default: | |
5c10e63c | 2971 | return -EINVAL; |
8cd1ec78 | 2972 | } |
66483a4a | 2973 | scsi_start_queue(sdev); |
1da177e4 LT |
2974 | |
2975 | return 0; | |
2976 | } | |
43f7571b BVA |
2977 | EXPORT_SYMBOL_GPL(scsi_internal_device_unblock_nowait); |
2978 | ||
2979 | /** | |
2980 | * scsi_internal_device_unblock - resume a device after a block request | |
2981 | * @sdev: device to resume | |
2982 | * @new_state: state to set the device to after unblocking | |
2983 | * | |
2984 | * Restart the device queue for a previously suspended SCSI device. May sleep. | |
2985 | * | |
2986 | * Returns zero if successful or a negative error code upon failure. | |
2987 | * | |
2988 | * Notes: | |
2989 | * This routine transitions the device to the SDEV_RUNNING state or to one of | |
2990 | * the offline states (which must be a legal transition) allowing the midlayer | |
2991 | * to goose the queue for this device. | |
2992 | */ | |
2993 | static int scsi_internal_device_unblock(struct scsi_device *sdev, | |
2994 | enum scsi_device_state new_state) | |
2995 | { | |
0db6ca8a BVA |
2996 | int ret; |
2997 | ||
2998 | mutex_lock(&sdev->state_mutex); | |
2999 | ret = scsi_internal_device_unblock_nowait(sdev, new_state); | |
3000 | mutex_unlock(&sdev->state_mutex); | |
3001 | ||
3002 | return ret; | |
43f7571b | 3003 | } |
1da177e4 | 3004 | |
1da177e4 LT |
3005 | static int |
3006 | target_block(struct device *dev, void *data) | |
3007 | { | |
3008 | if (scsi_is_target_device(dev)) | |
3009 | starget_for_each_device(to_scsi_target(dev), NULL, | |
c5e46f7a | 3010 | scsi_device_block); |
1da177e4 LT |
3011 | return 0; |
3012 | } | |
3013 | ||
31950192 MW |
3014 | /** |
3015 | * scsi_block_targets - transition all SCSI child devices to SDEV_BLOCK state | |
3016 | * @dev: a parent device of one or more scsi_target devices | |
3017 | * @shost: the Scsi_Host to which this device belongs | |
3018 | * | |
3019 | * Iterate over all children of @dev, which should be scsi_target devices, | |
3020 | * and switch all subordinate scsi devices to SDEV_BLOCK state. Wait for | |
3021 | * ongoing scsi_queue_rq() calls to finish. May sleep. | |
3022 | * | |
3023 | * Note: | |
3024 | * @dev must not itself be a scsi_target device. | |
3025 | */ | |
1da177e4 | 3026 | void |
31950192 | 3027 | scsi_block_targets(struct Scsi_Host *shost, struct device *dev) |
1da177e4 | 3028 | { |
31950192 MW |
3029 | WARN_ON_ONCE(scsi_is_target_device(dev)); |
3030 | device_for_each_child(dev, NULL, target_block); | |
e20fff8a | 3031 | blk_mq_wait_quiesce_done(&shost->tag_set); |
1da177e4 | 3032 | } |
31950192 | 3033 | EXPORT_SYMBOL_GPL(scsi_block_targets); |
1da177e4 LT |
3034 | |
3035 | static void | |
3036 | device_unblock(struct scsi_device *sdev, void *data) | |
3037 | { | |
5d9fb5cc | 3038 | scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data); |
1da177e4 LT |
3039 | } |
3040 | ||
3041 | static int | |
3042 | target_unblock(struct device *dev, void *data) | |
3043 | { | |
3044 | if (scsi_is_target_device(dev)) | |
5d9fb5cc | 3045 | starget_for_each_device(to_scsi_target(dev), data, |
1da177e4 LT |
3046 | device_unblock); |
3047 | return 0; | |
3048 | } | |
3049 | ||
3050 | void | |
5d9fb5cc | 3051 | scsi_target_unblock(struct device *dev, enum scsi_device_state new_state) |
1da177e4 LT |
3052 | { |
3053 | if (scsi_is_target_device(dev)) | |
5d9fb5cc | 3054 | starget_for_each_device(to_scsi_target(dev), &new_state, |
1da177e4 LT |
3055 | device_unblock); |
3056 | else | |
5d9fb5cc | 3057 | device_for_each_child(dev, &new_state, target_unblock); |
1da177e4 LT |
3058 | } |
3059 | EXPORT_SYMBOL_GPL(scsi_target_unblock); | |
cdb8c2a6 | 3060 | |
c854bcdf BVA |
3061 | /** |
3062 | * scsi_host_block - Try to transition all logical units to the SDEV_BLOCK state | |
3063 | * @shost: device to block | |
3064 | * | |
3065 | * Pause SCSI command processing for all logical units associated with the SCSI | |
3066 | * host and wait until pending scsi_queue_rq() calls have finished. | |
3067 | * | |
3068 | * Returns zero if successful or a negative error code upon failure. | |
3069 | */ | |
2bb95584 HR |
3070 | int |
3071 | scsi_host_block(struct Scsi_Host *shost) | |
3072 | { | |
3073 | struct scsi_device *sdev; | |
c854bcdf | 3074 | int ret; |
2bb95584 | 3075 | |
f983622a ML |
3076 | /* |
3077 | * Call scsi_internal_device_block_nowait so we can avoid | |
3078 | * calling synchronize_rcu() for each LUN. | |
3079 | */ | |
2bb95584 | 3080 | shost_for_each_device(sdev, shost) { |
f983622a ML |
3081 | mutex_lock(&sdev->state_mutex); |
3082 | ret = scsi_internal_device_block_nowait(sdev); | |
3083 | mutex_unlock(&sdev->state_mutex); | |
f30785db YB |
3084 | if (ret) { |
3085 | scsi_device_put(sdev); | |
c854bcdf | 3086 | return ret; |
f30785db | 3087 | } |
2bb95584 | 3088 | } |
f983622a | 3089 | |
b125bb99 BVA |
3090 | /* Wait for ongoing scsi_queue_rq() calls to finish. */ |
3091 | blk_mq_wait_quiesce_done(&shost->tag_set); | |
f983622a | 3092 | |
c854bcdf | 3093 | return 0; |
2bb95584 HR |
3094 | } |
3095 | EXPORT_SYMBOL_GPL(scsi_host_block); | |
3096 | ||
3097 | int | |
3098 | scsi_host_unblock(struct Scsi_Host *shost, int new_state) | |
3099 | { | |
3100 | struct scsi_device *sdev; | |
3101 | int ret = 0; | |
3102 | ||
3103 | shost_for_each_device(sdev, shost) { | |
3104 | ret = scsi_internal_device_unblock(sdev, new_state); | |
4dea170f YB |
3105 | if (ret) { |
3106 | scsi_device_put(sdev); | |
2bb95584 | 3107 | break; |
4dea170f | 3108 | } |
2bb95584 HR |
3109 | } |
3110 | return ret; | |
3111 | } | |
3112 | EXPORT_SYMBOL_GPL(scsi_host_unblock); | |
3113 | ||
cdb8c2a6 GL |
3114 | /** |
3115 | * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt | |
eb44820c | 3116 | * @sgl: scatter-gather list |
cdb8c2a6 GL |
3117 | * @sg_count: number of segments in sg |
3118 | * @offset: offset in bytes into sg, on return offset into the mapped area | |
3119 | * @len: bytes to map, on return number of bytes mapped | |
3120 | * | |
3121 | * Returns virtual address of the start of the mapped page | |
3122 | */ | |
c6132da1 | 3123 | void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count, |
cdb8c2a6 GL |
3124 | size_t *offset, size_t *len) |
3125 | { | |
3126 | int i; | |
3127 | size_t sg_len = 0, len_complete = 0; | |
c6132da1 | 3128 | struct scatterlist *sg; |
cdb8c2a6 GL |
3129 | struct page *page; |
3130 | ||
22cfefb5 AM |
3131 | WARN_ON(!irqs_disabled()); |
3132 | ||
c6132da1 | 3133 | for_each_sg(sgl, sg, sg_count, i) { |
cdb8c2a6 | 3134 | len_complete = sg_len; /* Complete sg-entries */ |
c6132da1 | 3135 | sg_len += sg->length; |
cdb8c2a6 GL |
3136 | if (sg_len > *offset) |
3137 | break; | |
3138 | } | |
3139 | ||
3140 | if (unlikely(i == sg_count)) { | |
169e1a2a AM |
3141 | printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, " |
3142 | "elements %d\n", | |
cadbd4a5 | 3143 | __func__, sg_len, *offset, sg_count); |
cdb8c2a6 GL |
3144 | WARN_ON(1); |
3145 | return NULL; | |
3146 | } | |
3147 | ||
3148 | /* Offset starting from the beginning of first page in this sg-entry */ | |
c6132da1 | 3149 | *offset = *offset - len_complete + sg->offset; |
cdb8c2a6 GL |
3150 | |
3151 | /* Assumption: contiguous pages can be accessed as "page + i" */ | |
45711f1a | 3152 | page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT)); |
cdb8c2a6 GL |
3153 | *offset &= ~PAGE_MASK; |
3154 | ||
3155 | /* Bytes in this sg-entry from *offset to the end of the page */ | |
3156 | sg_len = PAGE_SIZE - *offset; | |
3157 | if (*len > sg_len) | |
3158 | *len = sg_len; | |
3159 | ||
77dfce07 | 3160 | return kmap_atomic(page); |
cdb8c2a6 GL |
3161 | } |
3162 | EXPORT_SYMBOL(scsi_kmap_atomic_sg); | |
3163 | ||
3164 | /** | |
eb44820c | 3165 | * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg |
cdb8c2a6 GL |
3166 | * @virt: virtual address to be unmapped |
3167 | */ | |
3168 | void scsi_kunmap_atomic_sg(void *virt) | |
3169 | { | |
77dfce07 | 3170 | kunmap_atomic(virt); |
cdb8c2a6 GL |
3171 | } |
3172 | EXPORT_SYMBOL(scsi_kunmap_atomic_sg); | |
6f4c827e AL |
3173 | |
3174 | void sdev_disable_disk_events(struct scsi_device *sdev) | |
3175 | { | |
3176 | atomic_inc(&sdev->disk_events_disable_depth); | |
3177 | } | |
3178 | EXPORT_SYMBOL(sdev_disable_disk_events); | |
3179 | ||
3180 | void sdev_enable_disk_events(struct scsi_device *sdev) | |
3181 | { | |
3182 | if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0)) | |
3183 | return; | |
3184 | atomic_dec(&sdev->disk_events_disable_depth); | |
3185 | } | |
3186 | EXPORT_SYMBOL(sdev_enable_disk_events); | |
9983bed3 | 3187 | |
2e4209b3 MW |
3188 | static unsigned char designator_prio(const unsigned char *d) |
3189 | { | |
3190 | if (d[1] & 0x30) | |
3191 | /* not associated with LUN */ | |
3192 | return 0; | |
3193 | ||
3194 | if (d[3] == 0) | |
3195 | /* invalid length */ | |
3196 | return 0; | |
3197 | ||
3198 | /* | |
3199 | * Order of preference for lun descriptor: | |
3200 | * - SCSI name string | |
3201 | * - NAA IEEE Registered Extended | |
3202 | * - EUI-64 based 16-byte | |
3203 | * - EUI-64 based 12-byte | |
3204 | * - NAA IEEE Registered | |
3205 | * - NAA IEEE Extended | |
3206 | * - EUI-64 based 8-byte | |
3207 | * - SCSI name string (truncated) | |
3208 | * - T10 Vendor ID | |
3209 | * as longer descriptors reduce the likelyhood | |
3210 | * of identification clashes. | |
3211 | */ | |
3212 | ||
3213 | switch (d[1] & 0xf) { | |
3214 | case 8: | |
3215 | /* SCSI name string, variable-length UTF-8 */ | |
3216 | return 9; | |
3217 | case 3: | |
3218 | switch (d[4] >> 4) { | |
3219 | case 6: | |
3220 | /* NAA registered extended */ | |
3221 | return 8; | |
3222 | case 5: | |
3223 | /* NAA registered */ | |
3224 | return 5; | |
3225 | case 4: | |
3226 | /* NAA extended */ | |
3227 | return 4; | |
3228 | case 3: | |
3229 | /* NAA locally assigned */ | |
3230 | return 1; | |
3231 | default: | |
3232 | break; | |
3233 | } | |
3234 | break; | |
3235 | case 2: | |
3236 | switch (d[3]) { | |
3237 | case 16: | |
3238 | /* EUI64-based, 16 byte */ | |
3239 | return 7; | |
3240 | case 12: | |
3241 | /* EUI64-based, 12 byte */ | |
3242 | return 6; | |
3243 | case 8: | |
3244 | /* EUI64-based, 8 byte */ | |
3245 | return 3; | |
3246 | default: | |
3247 | break; | |
3248 | } | |
3249 | break; | |
3250 | case 1: | |
3251 | /* T10 vendor ID */ | |
3252 | return 1; | |
3253 | default: | |
3254 | break; | |
3255 | } | |
3256 | ||
3257 | return 0; | |
3258 | } | |
3259 | ||
9983bed3 HR |
3260 | /** |
3261 | * scsi_vpd_lun_id - return a unique device identification | |
3262 | * @sdev: SCSI device | |
3263 | * @id: buffer for the identification | |
3264 | * @id_len: length of the buffer | |
3265 | * | |
3266 | * Copies a unique device identification into @id based | |
3267 | * on the information in the VPD page 0x83 of the device. | |
3268 | * The string will be formatted as a SCSI name string. | |
3269 | * | |
3270 | * Returns the length of the identification or error on failure. | |
3271 | * If the identifier is longer than the supplied buffer the actual | |
3272 | * identifier length is returned and the buffer is not zero-padded. | |
3273 | */ | |
3274 | int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len) | |
3275 | { | |
2e4209b3 | 3276 | u8 cur_id_prio = 0; |
9983bed3 | 3277 | u8 cur_id_size = 0; |
ccf1e004 BVA |
3278 | const unsigned char *d, *cur_id_str; |
3279 | const struct scsi_vpd *vpd_pg83; | |
9983bed3 HR |
3280 | int id_size = -EINVAL; |
3281 | ||
3282 | rcu_read_lock(); | |
3283 | vpd_pg83 = rcu_dereference(sdev->vpd_pg83); | |
3284 | if (!vpd_pg83) { | |
3285 | rcu_read_unlock(); | |
3286 | return -ENXIO; | |
3287 | } | |
3288 | ||
9983bed3 HR |
3289 | /* The id string must be at least 20 bytes + terminating NULL byte */ |
3290 | if (id_len < 21) { | |
3291 | rcu_read_unlock(); | |
3292 | return -EINVAL; | |
3293 | } | |
3294 | ||
3295 | memset(id, 0, id_len); | |
16d6317e MW |
3296 | for (d = vpd_pg83->data + 4; |
3297 | d < vpd_pg83->data + vpd_pg83->len; | |
3298 | d += d[3] + 4) { | |
2e4209b3 MW |
3299 | u8 prio = designator_prio(d); |
3300 | ||
3301 | if (prio == 0 || cur_id_prio > prio) | |
16d6317e | 3302 | continue; |
9983bed3 HR |
3303 | |
3304 | switch (d[1] & 0xf) { | |
d230823a HR |
3305 | case 0x1: |
3306 | /* T10 Vendor ID */ | |
3307 | if (cur_id_size > d[3]) | |
3308 | break; | |
2e4209b3 | 3309 | cur_id_prio = prio; |
d230823a HR |
3310 | cur_id_size = d[3]; |
3311 | if (cur_id_size + 4 > id_len) | |
3312 | cur_id_size = id_len - 4; | |
3313 | cur_id_str = d + 4; | |
d230823a HR |
3314 | id_size = snprintf(id, id_len, "t10.%*pE", |
3315 | cur_id_size, cur_id_str); | |
3316 | break; | |
9983bed3 HR |
3317 | case 0x2: |
3318 | /* EUI-64 */ | |
2e4209b3 | 3319 | cur_id_prio = prio; |
9983bed3 HR |
3320 | cur_id_size = d[3]; |
3321 | cur_id_str = d + 4; | |
9983bed3 HR |
3322 | switch (cur_id_size) { |
3323 | case 8: | |
3324 | id_size = snprintf(id, id_len, | |
3325 | "eui.%8phN", | |
3326 | cur_id_str); | |
3327 | break; | |
3328 | case 12: | |
3329 | id_size = snprintf(id, id_len, | |
3330 | "eui.%12phN", | |
3331 | cur_id_str); | |
3332 | break; | |
3333 | case 16: | |
3334 | id_size = snprintf(id, id_len, | |
3335 | "eui.%16phN", | |
3336 | cur_id_str); | |
3337 | break; | |
3338 | default: | |
9983bed3 HR |
3339 | break; |
3340 | } | |
3341 | break; | |
3342 | case 0x3: | |
3343 | /* NAA */ | |
2e4209b3 | 3344 | cur_id_prio = prio; |
9983bed3 HR |
3345 | cur_id_size = d[3]; |
3346 | cur_id_str = d + 4; | |
9983bed3 HR |
3347 | switch (cur_id_size) { |
3348 | case 8: | |
3349 | id_size = snprintf(id, id_len, | |
3350 | "naa.%8phN", | |
3351 | cur_id_str); | |
3352 | break; | |
3353 | case 16: | |
3354 | id_size = snprintf(id, id_len, | |
3355 | "naa.%16phN", | |
3356 | cur_id_str); | |
3357 | break; | |
3358 | default: | |
9983bed3 HR |
3359 | break; |
3360 | } | |
3361 | break; | |
3362 | case 0x8: | |
3363 | /* SCSI name string */ | |
2e4209b3 | 3364 | if (cur_id_size > d[3]) |
9983bed3 HR |
3365 | break; |
3366 | /* Prefer others for truncated descriptor */ | |
2e4209b3 MW |
3367 | if (d[3] > id_len) { |
3368 | prio = 2; | |
3369 | if (cur_id_prio > prio) | |
3370 | break; | |
3371 | } | |
3372 | cur_id_prio = prio; | |
9983bed3 HR |
3373 | cur_id_size = id_size = d[3]; |
3374 | cur_id_str = d + 4; | |
9983bed3 HR |
3375 | if (cur_id_size >= id_len) |
3376 | cur_id_size = id_len - 1; | |
3377 | memcpy(id, cur_id_str, cur_id_size); | |
9983bed3 HR |
3378 | break; |
3379 | default: | |
3380 | break; | |
3381 | } | |
9983bed3 HR |
3382 | } |
3383 | rcu_read_unlock(); | |
3384 | ||
3385 | return id_size; | |
3386 | } | |
3387 | EXPORT_SYMBOL(scsi_vpd_lun_id); | |
a8aa3978 | 3388 | |
39d2112a | 3389 | /** |
a8aa3978 HR |
3390 | * scsi_vpd_tpg_id - return a target port group identifier |
3391 | * @sdev: SCSI device | |
39d2112a | 3392 | * @rel_id: pointer to return relative target port in if not %NULL |
a8aa3978 HR |
3393 | * |
3394 | * Returns the Target Port Group identifier from the information | |
39d2112a RD |
3395 | * from VPD page 0x83 of the device. |
3396 | * Optionally sets @rel_id to the relative target port on success. | |
a8aa3978 | 3397 | * |
39d2112a | 3398 | * Return: the identifier or error on failure. |
a8aa3978 HR |
3399 | */ |
3400 | int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id) | |
3401 | { | |
ccf1e004 BVA |
3402 | const unsigned char *d; |
3403 | const struct scsi_vpd *vpd_pg83; | |
a8aa3978 HR |
3404 | int group_id = -EAGAIN, rel_port = -1; |
3405 | ||
3406 | rcu_read_lock(); | |
3407 | vpd_pg83 = rcu_dereference(sdev->vpd_pg83); | |
3408 | if (!vpd_pg83) { | |
3409 | rcu_read_unlock(); | |
3410 | return -ENXIO; | |
3411 | } | |
3412 | ||
ccf1e004 BVA |
3413 | d = vpd_pg83->data + 4; |
3414 | while (d < vpd_pg83->data + vpd_pg83->len) { | |
a8aa3978 HR |
3415 | switch (d[1] & 0xf) { |
3416 | case 0x4: | |
3417 | /* Relative target port */ | |
3418 | rel_port = get_unaligned_be16(&d[6]); | |
3419 | break; | |
3420 | case 0x5: | |
3421 | /* Target port group */ | |
3422 | group_id = get_unaligned_be16(&d[6]); | |
3423 | break; | |
3424 | default: | |
3425 | break; | |
3426 | } | |
3427 | d += d[3] + 4; | |
3428 | } | |
3429 | rcu_read_unlock(); | |
3430 | ||
3431 | if (group_id >= 0 && rel_id && rel_port != -1) | |
3432 | *rel_id = rel_port; | |
3433 | ||
3434 | return group_id; | |
3435 | } | |
3436 | EXPORT_SYMBOL(scsi_vpd_tpg_id); | |
f2b1e9c6 HR |
3437 | |
3438 | /** | |
3439 | * scsi_build_sense - build sense data for a command | |
3440 | * @scmd: scsi command for which the sense should be formatted | |
3441 | * @desc: Sense format (non-zero == descriptor format, | |
3442 | * 0 == fixed format) | |
3443 | * @key: Sense key | |
3444 | * @asc: Additional sense code | |
3445 | * @ascq: Additional sense code qualifier | |
3446 | * | |
3447 | **/ | |
3448 | void scsi_build_sense(struct scsi_cmnd *scmd, int desc, u8 key, u8 asc, u8 ascq) | |
3449 | { | |
3450 | scsi_build_sense_buffer(desc, scmd->sense_buffer, key, asc, ascq); | |
464a00c9 | 3451 | scmd->result = SAM_STAT_CHECK_CONDITION; |
f2b1e9c6 HR |
3452 | } |
3453 | EXPORT_SYMBOL_GPL(scsi_build_sense); | |
25a1f7a0 | 3454 | |
f7c7190f | 3455 | #ifdef CONFIG_SCSI_LIB_KUNIT_TEST |
25a1f7a0 MC |
3456 | #include "scsi_lib_test.c" |
3457 | #endif |