blob: b5867e1566f42bc0dd3e6a54bc02754c2adf4c90 [file] [log] [blame]
Googler9398cc32022-12-02 17:21:52 +08001// SPDX-License-Identifier: GPL-2.0-only
Googleraf606d22022-10-26 21:40:12 -07002/*
3 * Copyright (C) 1999 Eric Youngdale
4 * Copyright (C) 2014 Christoph Hellwig
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>
13#include <linux/bitops.h>
14#include <linux/blkdev.h>
15#include <linux/completion.h>
16#include <linux/kernel.h>
17#include <linux/export.h>
18#include <linux/init.h>
19#include <linux/pci.h>
20#include <linux/delay.h>
21#include <linux/hardirq.h>
22#include <linux/scatterlist.h>
23#include <linux/blk-mq.h>
24#include <linux/ratelimit.h>
25#include <asm/unaligned.h>
26
27#include <scsi/scsi.h>
28#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_dbg.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_driver.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_host.h>
Googler9398cc32022-12-02 17:21:52 +080034#include <scsi/scsi_transport.h> /* __scsi_init_queue() */
Googleraf606d22022-10-26 21:40:12 -070035#include <scsi/scsi_dh.h>
36
37#include <trace/events/scsi.h>
38
Googler9398cc32022-12-02 17:21:52 +080039#include "scsi_debugfs.h"
Googleraf606d22022-10-26 21:40:12 -070040#include "scsi_priv.h"
41#include "scsi_logging.h"
42
Googler9398cc32022-12-02 17:21:52 +080043/*
44 * Size of integrity metadata is usually small, 1 inline sg should
45 * cover normal cases.
46 */
47#ifdef CONFIG_ARCH_NO_SG_CHAIN
48#define SCSI_INLINE_PROT_SG_CNT 0
49#define SCSI_INLINE_SG_CNT 0
50#else
51#define SCSI_INLINE_PROT_SG_CNT 1
52#define SCSI_INLINE_SG_CNT 2
53#endif
Googleraf606d22022-10-26 21:40:12 -070054
Googler9398cc32022-12-02 17:21:52 +080055static struct kmem_cache *scsi_sdb_cache;
56static struct kmem_cache *scsi_sense_cache;
57static struct kmem_cache *scsi_sense_isadma_cache;
58static DEFINE_MUTEX(scsi_sense_cache_mutex);
59
60static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd);
61
62static inline struct kmem_cache *
63scsi_select_sense_cache(bool unchecked_isa_dma)
64{
65 return unchecked_isa_dma ? scsi_sense_isadma_cache : scsi_sense_cache;
66}
67
68static void scsi_free_sense_buffer(bool unchecked_isa_dma,
69 unsigned char *sense_buffer)
70{
71 kmem_cache_free(scsi_select_sense_cache(unchecked_isa_dma),
72 sense_buffer);
73}
74
75static unsigned char *scsi_alloc_sense_buffer(bool unchecked_isa_dma,
76 gfp_t gfp_mask, int numa_node)
77{
78 return kmem_cache_alloc_node(scsi_select_sense_cache(unchecked_isa_dma),
79 gfp_mask, numa_node);
80}
81
82int scsi_init_sense_cache(struct Scsi_Host *shost)
83{
84 struct kmem_cache *cache;
85 int ret = 0;
86
87 mutex_lock(&scsi_sense_cache_mutex);
88 cache = scsi_select_sense_cache(shost->unchecked_isa_dma);
89 if (cache)
90 goto exit;
91
92 if (shost->unchecked_isa_dma) {
93 scsi_sense_isadma_cache =
94 kmem_cache_create("scsi_sense_cache(DMA)",
95 SCSI_SENSE_BUFFERSIZE, 0,
96 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL);
97 if (!scsi_sense_isadma_cache)
98 ret = -ENOMEM;
99 } else {
100 scsi_sense_cache =
101 kmem_cache_create_usercopy("scsi_sense_cache",
102 SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN,
103 0, SCSI_SENSE_BUFFERSIZE, NULL);
104 if (!scsi_sense_cache)
105 ret = -ENOMEM;
106 }
107 exit:
108 mutex_unlock(&scsi_sense_cache_mutex);
109 return ret;
110}
Googleraf606d22022-10-26 21:40:12 -0700111
112/*
113 * When to reinvoke queueing after a resource shortage. It's 3 msecs to
114 * not change behaviour from the previous unplug mechanism, experimentation
115 * may prove this needs changing.
116 */
117#define SCSI_QUEUE_DELAY 3
118
119static void
120scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
121{
122 struct Scsi_Host *host = cmd->device->host;
123 struct scsi_device *device = cmd->device;
124 struct scsi_target *starget = scsi_target(device);
125
126 /*
127 * Set the appropriate busy bit for the device/host.
128 *
129 * If the host/device isn't busy, assume that something actually
130 * completed, and that we should be able to queue a command now.
131 *
132 * Note that the prior mid-layer assumption that any host could
133 * always queue at least one command is now broken. The mid-layer
134 * will implement a user specifiable stall (see
135 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
136 * if a command is requeued with no other commands outstanding
137 * either for the device or for the host.
138 */
139 switch (reason) {
140 case SCSI_MLQUEUE_HOST_BUSY:
141 atomic_set(&host->host_blocked, host->max_host_blocked);
142 break;
143 case SCSI_MLQUEUE_DEVICE_BUSY:
144 case SCSI_MLQUEUE_EH_RETRY:
145 atomic_set(&device->device_blocked,
146 device->max_device_blocked);
147 break;
148 case SCSI_MLQUEUE_TARGET_BUSY:
149 atomic_set(&starget->target_blocked,
150 starget->max_target_blocked);
151 break;
152 }
153}
154
155static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd)
156{
Googler9398cc32022-12-02 17:21:52 +0800157 if (cmd->request->rq_flags & RQF_DONTPREP) {
158 cmd->request->rq_flags &= ~RQF_DONTPREP;
159 scsi_mq_uninit_cmd(cmd);
160 } else {
161 WARN_ON_ONCE(true);
162 }
163 blk_mq_requeue_request(cmd->request, true);
Googleraf606d22022-10-26 21:40:12 -0700164}
165
166/**
167 * __scsi_queue_insert - private queue insertion
168 * @cmd: The SCSI command being requeued
169 * @reason: The reason for the requeue
170 * @unbusy: Whether the queue should be unbusied
171 *
172 * This is a private queue insertion. The public interface
173 * scsi_queue_insert() always assumes the queue should be unbusied
174 * because it's always called before the completion. This function is
175 * for a requeue after completion, which should only occur in this
176 * file.
177 */
Googler9398cc32022-12-02 17:21:52 +0800178static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, bool unbusy)
Googleraf606d22022-10-26 21:40:12 -0700179{
180 struct scsi_device *device = cmd->device;
Googleraf606d22022-10-26 21:40:12 -0700181
182 SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
183 "Inserting command %p into mlqueue\n", cmd));
184
185 scsi_set_blocked(cmd, reason);
186
187 /*
188 * Decrement the counters, since these commands are no longer
189 * active on the host/device.
190 */
191 if (unbusy)
192 scsi_device_unbusy(device);
193
194 /*
195 * Requeue this command. It will go before all other commands
196 * that are already in the queue. Schedule requeue work under
197 * lock such that the kblockd_schedule_work() call happens
198 * before blk_cleanup_queue() finishes.
199 */
200 cmd->result = 0;
Googler9398cc32022-12-02 17:21:52 +0800201
202 blk_mq_requeue_request(cmd->request, true);
Googleraf606d22022-10-26 21:40:12 -0700203}
204
205/*
206 * Function: scsi_queue_insert()
207 *
208 * Purpose: Insert a command in the midlevel queue.
209 *
210 * Arguments: cmd - command that we are adding to queue.
211 * reason - why we are inserting command to queue.
212 *
213 * Lock status: Assumed that lock is not held upon entry.
214 *
215 * Returns: Nothing.
216 *
217 * Notes: We do this for one of two cases. Either the host is busy
218 * and it cannot accept any more commands for the time being,
219 * or the device returned QUEUE_FULL and can accept no more
220 * commands.
221 * Notes: This could be called either from an interrupt context or a
222 * normal process context.
223 */
224void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
225{
Googler9398cc32022-12-02 17:21:52 +0800226 __scsi_queue_insert(cmd, reason, true);
Googleraf606d22022-10-26 21:40:12 -0700227}
Googler9398cc32022-12-02 17:21:52 +0800228
229
Googleraf606d22022-10-26 21:40:12 -0700230/**
Googler9398cc32022-12-02 17:21:52 +0800231 * __scsi_execute - insert request and wait for the result
Googleraf606d22022-10-26 21:40:12 -0700232 * @sdev: scsi device
233 * @cmd: scsi command
234 * @data_direction: data direction
235 * @buffer: data buffer
236 * @bufflen: len of buffer
237 * @sense: optional sense buffer
Googler9398cc32022-12-02 17:21:52 +0800238 * @sshdr: optional decoded sense header
Googleraf606d22022-10-26 21:40:12 -0700239 * @timeout: request timeout in seconds
240 * @retries: number of times to retry request
Googler9398cc32022-12-02 17:21:52 +0800241 * @flags: flags for ->cmd_flags
242 * @rq_flags: flags for ->rq_flags
Googleraf606d22022-10-26 21:40:12 -0700243 * @resid: optional residual length
244 *
Googler9398cc32022-12-02 17:21:52 +0800245 * Returns the scsi_cmnd result field if a command was executed, or a negative
246 * Linux error code if we didn't get that far.
Googleraf606d22022-10-26 21:40:12 -0700247 */
Googler9398cc32022-12-02 17:21:52 +0800248int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
Googleraf606d22022-10-26 21:40:12 -0700249 int data_direction, void *buffer, unsigned bufflen,
Googler9398cc32022-12-02 17:21:52 +0800250 unsigned char *sense, struct scsi_sense_hdr *sshdr,
251 int timeout, int retries, u64 flags, req_flags_t rq_flags,
Googleraf606d22022-10-26 21:40:12 -0700252 int *resid)
253{
254 struct request *req;
Googler9398cc32022-12-02 17:21:52 +0800255 struct scsi_request *rq;
Googleraf606d22022-10-26 21:40:12 -0700256 int ret = DRIVER_ERROR << 24;
257
Googler9398cc32022-12-02 17:21:52 +0800258 req = blk_get_request(sdev->request_queue,
259 data_direction == DMA_TO_DEVICE ?
260 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, BLK_MQ_REQ_PREEMPT);
Googleraf606d22022-10-26 21:40:12 -0700261 if (IS_ERR(req))
262 return ret;
Googler9398cc32022-12-02 17:21:52 +0800263 rq = scsi_req(req);
Googleraf606d22022-10-26 21:40:12 -0700264
265 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
Googler9398cc32022-12-02 17:21:52 +0800266 buffer, bufflen, GFP_NOIO))
Googleraf606d22022-10-26 21:40:12 -0700267 goto out;
268
Googler9398cc32022-12-02 17:21:52 +0800269 rq->cmd_len = COMMAND_SIZE(cmd[0]);
270 memcpy(rq->cmd, cmd, rq->cmd_len);
271 rq->retries = retries;
Googleraf606d22022-10-26 21:40:12 -0700272 req->timeout = timeout;
Googler9398cc32022-12-02 17:21:52 +0800273 req->cmd_flags |= flags;
274 req->rq_flags |= rq_flags | RQF_QUIET;
Googleraf606d22022-10-26 21:40:12 -0700275
276 /*
277 * head injection *required* here otherwise quiesce won't work
278 */
279 blk_execute_rq(req->q, NULL, req, 1);
280
281 /*
282 * Some devices (USB mass-storage in particular) may transfer
283 * garbage data together with a residue indicating that the data
284 * is invalid. Prevent the garbage from being misinterpreted
285 * and prevent security leaks by zeroing out the excess data.
286 */
Googler9398cc32022-12-02 17:21:52 +0800287 if (unlikely(rq->resid_len > 0 && rq->resid_len <= bufflen))
288 memset(buffer + (bufflen - rq->resid_len), 0, rq->resid_len);
Googleraf606d22022-10-26 21:40:12 -0700289
290 if (resid)
Googler9398cc32022-12-02 17:21:52 +0800291 *resid = rq->resid_len;
292 if (sense && rq->sense_len)
293 memcpy(sense, rq->sense, SCSI_SENSE_BUFFERSIZE);
294 if (sshdr)
295 scsi_normalize_sense(rq->sense, rq->sense_len, sshdr);
296 ret = rq->result;
Googleraf606d22022-10-26 21:40:12 -0700297 out:
298 blk_put_request(req);
299
300 return ret;
301}
Googler9398cc32022-12-02 17:21:52 +0800302EXPORT_SYMBOL(__scsi_execute);
Googleraf606d22022-10-26 21:40:12 -0700303
304/*
305 * Function: scsi_init_cmd_errh()
306 *
307 * Purpose: Initialize cmd fields related to error handling.
308 *
309 * Arguments: cmd - command that is ready to be queued.
310 *
311 * Notes: This function has the job of initializing a number of
312 * fields related to error handling. Typically this will
313 * be called once for each command, as required.
314 */
315static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
316{
Googleraf606d22022-10-26 21:40:12 -0700317 scsi_set_resid(cmd, 0);
318 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
319 if (cmd->cmd_len == 0)
320 cmd->cmd_len = scsi_command_size(cmd->cmnd);
321}
322
Googler9398cc32022-12-02 17:21:52 +0800323/*
324 * Decrement the host_busy counter and wake up the error handler if necessary.
325 * Avoid as follows that the error handler is not woken up if shost->host_busy
326 * == shost->host_failed: use call_rcu() in scsi_eh_scmd_add() in combination
327 * with an RCU read lock in this function to ensure that this function in its
328 * entirety either finishes before scsi_eh_scmd_add() increases the
329 * host_failed counter or that it notices the shost state change made by
330 * scsi_eh_scmd_add().
331 */
332static void scsi_dec_host_busy(struct Scsi_Host *shost)
333{
334 unsigned long flags;
335
336 rcu_read_lock();
337 atomic_dec(&shost->host_busy);
338 if (unlikely(scsi_host_in_recovery(shost))) {
339 spin_lock_irqsave(shost->host_lock, flags);
340 if (shost->host_failed || shost->host_eh_scheduled)
341 scsi_eh_wakeup(shost);
342 spin_unlock_irqrestore(shost->host_lock, flags);
343 }
344 rcu_read_unlock();
345}
346
Googleraf606d22022-10-26 21:40:12 -0700347void scsi_device_unbusy(struct scsi_device *sdev)
348{
349 struct Scsi_Host *shost = sdev->host;
350 struct scsi_target *starget = scsi_target(sdev);
Googleraf606d22022-10-26 21:40:12 -0700351
Googler9398cc32022-12-02 17:21:52 +0800352 scsi_dec_host_busy(shost);
353
Googleraf606d22022-10-26 21:40:12 -0700354 if (starget->can_queue > 0)
355 atomic_dec(&starget->target_busy);
356
Googleraf606d22022-10-26 21:40:12 -0700357 atomic_dec(&sdev->device_busy);
358}
359
360static void scsi_kick_queue(struct request_queue *q)
361{
Googler9398cc32022-12-02 17:21:52 +0800362 blk_mq_run_hw_queues(q, false);
Googleraf606d22022-10-26 21:40:12 -0700363}
364
365/*
366 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
367 * and call blk_run_queue for all the scsi_devices on the target -
368 * including current_sdev first.
369 *
370 * Called with *no* scsi locks held.
371 */
372static void scsi_single_lun_run(struct scsi_device *current_sdev)
373{
374 struct Scsi_Host *shost = current_sdev->host;
375 struct scsi_device *sdev, *tmp;
376 struct scsi_target *starget = scsi_target(current_sdev);
377 unsigned long flags;
378
379 spin_lock_irqsave(shost->host_lock, flags);
380 starget->starget_sdev_user = NULL;
381 spin_unlock_irqrestore(shost->host_lock, flags);
382
383 /*
384 * Call blk_run_queue for all LUNs on the target, starting with
385 * current_sdev. We race with others (to set starget_sdev_user),
386 * but in most cases, we will be first. Ideally, each LU on the
387 * target would get some limited time or requests on the target.
388 */
389 scsi_kick_queue(current_sdev->request_queue);
390
391 spin_lock_irqsave(shost->host_lock, flags);
392 if (starget->starget_sdev_user)
393 goto out;
394 list_for_each_entry_safe(sdev, tmp, &starget->devices,
395 same_target_siblings) {
396 if (sdev == current_sdev)
397 continue;
398 if (scsi_device_get(sdev))
399 continue;
400
401 spin_unlock_irqrestore(shost->host_lock, flags);
402 scsi_kick_queue(sdev->request_queue);
403 spin_lock_irqsave(shost->host_lock, flags);
404
405 scsi_device_put(sdev);
406 }
407 out:
408 spin_unlock_irqrestore(shost->host_lock, flags);
409}
410
411static inline bool scsi_device_is_busy(struct scsi_device *sdev)
412{
413 if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
414 return true;
415 if (atomic_read(&sdev->device_blocked) > 0)
416 return true;
417 return false;
418}
419
420static inline bool scsi_target_is_busy(struct scsi_target *starget)
421{
422 if (starget->can_queue > 0) {
423 if (atomic_read(&starget->target_busy) >= starget->can_queue)
424 return true;
425 if (atomic_read(&starget->target_blocked) > 0)
426 return true;
427 }
428 return false;
429}
430
431static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
432{
433 if (shost->can_queue > 0 &&
434 atomic_read(&shost->host_busy) >= shost->can_queue)
435 return true;
436 if (atomic_read(&shost->host_blocked) > 0)
437 return true;
438 if (shost->host_self_blocked)
439 return true;
440 return false;
441}
442
443static void scsi_starved_list_run(struct Scsi_Host *shost)
444{
445 LIST_HEAD(starved_list);
446 struct scsi_device *sdev;
447 unsigned long flags;
448
449 spin_lock_irqsave(shost->host_lock, flags);
450 list_splice_init(&shost->starved_list, &starved_list);
451
452 while (!list_empty(&starved_list)) {
453 struct request_queue *slq;
454
455 /*
456 * As long as shost is accepting commands and we have
457 * starved queues, call blk_run_queue. scsi_request_fn
458 * drops the queue_lock and can add us back to the
459 * starved_list.
460 *
461 * host_lock protects the starved_list and starved_entry.
462 * scsi_request_fn must get the host_lock before checking
463 * or modifying starved_list or starved_entry.
464 */
465 if (scsi_host_is_busy(shost))
466 break;
467
468 sdev = list_entry(starved_list.next,
469 struct scsi_device, starved_entry);
470 list_del_init(&sdev->starved_entry);
471 if (scsi_target_is_busy(scsi_target(sdev))) {
472 list_move_tail(&sdev->starved_entry,
473 &shost->starved_list);
474 continue;
475 }
476
477 /*
478 * Once we drop the host lock, a racing scsi_remove_device()
479 * call may remove the sdev from the starved list and destroy
480 * it and the queue. Mitigate by taking a reference to the
481 * queue and never touching the sdev again after we drop the
482 * host lock. Note: if __scsi_remove_device() invokes
483 * blk_cleanup_queue() before the queue is run from this
484 * function then blk_run_queue() will return immediately since
485 * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
486 */
487 slq = sdev->request_queue;
488 if (!blk_get_queue(slq))
489 continue;
490 spin_unlock_irqrestore(shost->host_lock, flags);
491
492 scsi_kick_queue(slq);
493 blk_put_queue(slq);
494
495 spin_lock_irqsave(shost->host_lock, flags);
496 }
497 /* put any unprocessed entries back */
498 list_splice(&starved_list, &shost->starved_list);
499 spin_unlock_irqrestore(shost->host_lock, flags);
500}
501
502/*
503 * Function: scsi_run_queue()
504 *
505 * Purpose: Select a proper request queue to serve next
506 *
507 * Arguments: q - last request's queue
508 *
509 * Returns: Nothing
510 *
511 * Notes: The previous command was completely finished, start
512 * a new one if possible.
513 */
514static void scsi_run_queue(struct request_queue *q)
515{
516 struct scsi_device *sdev = q->queuedata;
517
518 if (scsi_target(sdev)->single_lun)
519 scsi_single_lun_run(sdev);
520 if (!list_empty(&sdev->host->starved_list))
521 scsi_starved_list_run(sdev->host);
522
Googler9398cc32022-12-02 17:21:52 +0800523 blk_mq_run_hw_queues(q, false);
Googleraf606d22022-10-26 21:40:12 -0700524}
525
526void scsi_requeue_run_queue(struct work_struct *work)
527{
528 struct scsi_device *sdev;
529 struct request_queue *q;
530
531 sdev = container_of(work, struct scsi_device, requeue_work);
532 q = sdev->request_queue;
533 scsi_run_queue(q);
534}
535
Googleraf606d22022-10-26 21:40:12 -0700536void scsi_run_host_queues(struct Scsi_Host *shost)
537{
538 struct scsi_device *sdev;
539
540 shost_for_each_device(sdev, shost)
541 scsi_run_queue(sdev->request_queue);
542}
543
544static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
545{
Googler9398cc32022-12-02 17:21:52 +0800546 if (!blk_rq_is_passthrough(cmd->request)) {
Googleraf606d22022-10-26 21:40:12 -0700547 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
548
549 if (drv->uninit_command)
550 drv->uninit_command(cmd);
551 }
552}
553
Googler9398cc32022-12-02 17:21:52 +0800554static void scsi_free_sgtables(struct scsi_cmnd *cmd)
Googleraf606d22022-10-26 21:40:12 -0700555{
Googleraf606d22022-10-26 21:40:12 -0700556 if (cmd->sdb.table.nents)
Googler9398cc32022-12-02 17:21:52 +0800557 sg_free_table_chained(&cmd->sdb.table,
558 SCSI_INLINE_SG_CNT);
Googleraf606d22022-10-26 21:40:12 -0700559 if (scsi_prot_sg_count(cmd))
Googler9398cc32022-12-02 17:21:52 +0800560 sg_free_table_chained(&cmd->prot_sdb->table,
561 SCSI_INLINE_PROT_SG_CNT);
Googleraf606d22022-10-26 21:40:12 -0700562}
563
564static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
565{
Googler9398cc32022-12-02 17:21:52 +0800566 scsi_free_sgtables(cmd);
Googleraf606d22022-10-26 21:40:12 -0700567 scsi_uninit_cmd(cmd);
Googler9398cc32022-12-02 17:21:52 +0800568 scsi_del_cmd_from_list(cmd);
Googleraf606d22022-10-26 21:40:12 -0700569}
570
Googler9398cc32022-12-02 17:21:52 +0800571static void scsi_run_queue_async(struct scsi_device *sdev)
Googleraf606d22022-10-26 21:40:12 -0700572{
Googler9398cc32022-12-02 17:21:52 +0800573 if (scsi_target(sdev)->single_lun ||
574 !list_empty(&sdev->host->starved_list))
575 kblockd_schedule_work(&sdev->requeue_work);
576 else
577 blk_mq_run_hw_queues(sdev->request_queue, true);
Googleraf606d22022-10-26 21:40:12 -0700578}
579
Googler9398cc32022-12-02 17:21:52 +0800580/* Returns false when no more bytes to process, true if there are more */
581static bool scsi_end_request(struct request *req, blk_status_t error,
582 unsigned int bytes)
Googleraf606d22022-10-26 21:40:12 -0700583{
Googler9398cc32022-12-02 17:21:52 +0800584 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Googleraf606d22022-10-26 21:40:12 -0700585 struct scsi_device *sdev = cmd->device;
586 struct request_queue *q = sdev->request_queue;
587
588 if (blk_update_request(req, error, bytes))
589 return true;
590
Googleraf606d22022-10-26 21:40:12 -0700591 if (blk_queue_add_random(q))
592 add_disk_randomness(req->rq_disk);
593
Googler9398cc32022-12-02 17:21:52 +0800594 if (!blk_rq_is_scsi(req)) {
595 WARN_ON_ONCE(!(cmd->flags & SCMD_INITIALIZED));
596 cmd->flags &= ~SCMD_INITIALIZED;
Googleraf606d22022-10-26 21:40:12 -0700597 }
598
Googler9398cc32022-12-02 17:21:52 +0800599 /*
600 * Calling rcu_barrier() is not necessary here because the
601 * SCSI error handler guarantees that the function called by
602 * call_rcu() has been called before scsi_end_request() is
603 * called.
604 */
605 destroy_rcu_head(&cmd->rcu);
606
607 /*
608 * In the MQ case the command gets freed by __blk_mq_end_request,
609 * so we have to do all cleanup that depends on it earlier.
610 *
611 * We also can't kick the queues from irq context, so we
612 * will have to defer it to a workqueue.
613 */
614 scsi_mq_uninit_cmd(cmd);
615
616 /*
617 * queue is still alive, so grab the ref for preventing it
618 * from being cleaned up during running queue.
619 */
620 percpu_ref_get(&q->q_usage_counter);
621
622 __blk_mq_end_request(req, error);
623
624 scsi_run_queue_async(sdev);
625
626 percpu_ref_put(&q->q_usage_counter);
Googleraf606d22022-10-26 21:40:12 -0700627 return false;
628}
629
630/**
Googler9398cc32022-12-02 17:21:52 +0800631 * scsi_result_to_blk_status - translate a SCSI result code into blk_status_t
632 * @cmd: SCSI command
Googleraf606d22022-10-26 21:40:12 -0700633 * @result: scsi error code
634 *
Googler9398cc32022-12-02 17:21:52 +0800635 * Translate a SCSI result code into a blk_status_t value. May reset the host
636 * byte of @cmd->result.
Googleraf606d22022-10-26 21:40:12 -0700637 */
Googler9398cc32022-12-02 17:21:52 +0800638static blk_status_t scsi_result_to_blk_status(struct scsi_cmnd *cmd, int result)
Googleraf606d22022-10-26 21:40:12 -0700639{
Googler9398cc32022-12-02 17:21:52 +0800640 switch (host_byte(result)) {
641 case DID_OK:
642 /*
643 * Also check the other bytes than the status byte in result
644 * to handle the case when a SCSI LLD sets result to
645 * DRIVER_SENSE << 24 without setting SAM_STAT_CHECK_CONDITION.
646 */
647 if (scsi_status_is_good(result) && (result & ~0xff) == 0)
648 return BLK_STS_OK;
649 return BLK_STS_IOERR;
Googleraf606d22022-10-26 21:40:12 -0700650 case DID_TRANSPORT_FAILFAST:
Googler9398cc32022-12-02 17:21:52 +0800651 return BLK_STS_TRANSPORT;
Googleraf606d22022-10-26 21:40:12 -0700652 case DID_TARGET_FAILURE:
653 set_host_byte(cmd, DID_OK);
Googler9398cc32022-12-02 17:21:52 +0800654 return BLK_STS_TARGET;
Googleraf606d22022-10-26 21:40:12 -0700655 case DID_NEXUS_FAILURE:
656 set_host_byte(cmd, DID_OK);
Googler9398cc32022-12-02 17:21:52 +0800657 return BLK_STS_NEXUS;
Googleraf606d22022-10-26 21:40:12 -0700658 case DID_ALLOC_FAILURE:
659 set_host_byte(cmd, DID_OK);
Googler9398cc32022-12-02 17:21:52 +0800660 return BLK_STS_NOSPC;
Googleraf606d22022-10-26 21:40:12 -0700661 case DID_MEDIUM_ERROR:
662 set_host_byte(cmd, DID_OK);
Googler9398cc32022-12-02 17:21:52 +0800663 return BLK_STS_MEDIUM;
Googleraf606d22022-10-26 21:40:12 -0700664 default:
Googler9398cc32022-12-02 17:21:52 +0800665 return BLK_STS_IOERR;
Googleraf606d22022-10-26 21:40:12 -0700666 }
Googleraf606d22022-10-26 21:40:12 -0700667}
668
Googler9398cc32022-12-02 17:21:52 +0800669/* Helper for scsi_io_completion() when "reprep" action required. */
670static void scsi_io_completion_reprep(struct scsi_cmnd *cmd,
671 struct request_queue *q)
Googleraf606d22022-10-26 21:40:12 -0700672{
Googler9398cc32022-12-02 17:21:52 +0800673 /* A new command will be prepared and issued. */
674 scsi_mq_requeue_cmd(cmd);
675}
676
677/* Helper for scsi_io_completion() when special action required. */
678static void scsi_io_completion_action(struct scsi_cmnd *cmd, int result)
679{
Googleraf606d22022-10-26 21:40:12 -0700680 struct request_queue *q = cmd->device->request_queue;
681 struct request *req = cmd->request;
Googler9398cc32022-12-02 17:21:52 +0800682 int level = 0;
Googleraf606d22022-10-26 21:40:12 -0700683 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
684 ACTION_DELAYED_RETRY} action;
685 unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
Googler9398cc32022-12-02 17:21:52 +0800686 struct scsi_sense_hdr sshdr;
687 bool sense_valid;
688 bool sense_current = true; /* false implies "deferred sense" */
689 blk_status_t blk_stat;
Googleraf606d22022-10-26 21:40:12 -0700690
Googler9398cc32022-12-02 17:21:52 +0800691 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
692 if (sense_valid)
693 sense_current = !scsi_sense_is_deferred(&sshdr);
Googleraf606d22022-10-26 21:40:12 -0700694
Googler9398cc32022-12-02 17:21:52 +0800695 blk_stat = scsi_result_to_blk_status(cmd, result);
Googleraf606d22022-10-26 21:40:12 -0700696
697 if (host_byte(result) == DID_RESET) {
698 /* Third party bus reset or reset for error recovery
699 * reasons. Just retry the command and see what
700 * happens.
701 */
702 action = ACTION_RETRY;
Googler9398cc32022-12-02 17:21:52 +0800703 } else if (sense_valid && sense_current) {
Googleraf606d22022-10-26 21:40:12 -0700704 switch (sshdr.sense_key) {
705 case UNIT_ATTENTION:
706 if (cmd->device->removable) {
707 /* Detected disc change. Set a bit
708 * and quietly refuse further access.
709 */
710 cmd->device->changed = 1;
711 action = ACTION_FAIL;
712 } else {
713 /* Must have been a power glitch, or a
714 * bus reset. Could not have been a
715 * media change, so we just retry the
716 * command and see what happens.
717 */
718 action = ACTION_RETRY;
719 }
720 break;
721 case ILLEGAL_REQUEST:
722 /* If we had an ILLEGAL REQUEST returned, then
723 * we may have performed an unsupported
724 * command. The only thing this should be
725 * would be a ten byte read where only a six
726 * byte read was supported. Also, on a system
727 * where READ CAPACITY failed, we may have
728 * read past the end of the disk.
729 */
730 if ((cmd->device->use_10_for_rw &&
731 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
732 (cmd->cmnd[0] == READ_10 ||
733 cmd->cmnd[0] == WRITE_10)) {
734 /* This will issue a new 6-byte command. */
735 cmd->device->use_10_for_rw = 0;
736 action = ACTION_REPREP;
737 } else if (sshdr.asc == 0x10) /* DIX */ {
738 action = ACTION_FAIL;
Googler9398cc32022-12-02 17:21:52 +0800739 blk_stat = BLK_STS_PROTECTION;
Googleraf606d22022-10-26 21:40:12 -0700740 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
741 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
742 action = ACTION_FAIL;
Googler9398cc32022-12-02 17:21:52 +0800743 blk_stat = BLK_STS_TARGET;
Googleraf606d22022-10-26 21:40:12 -0700744 } else
745 action = ACTION_FAIL;
746 break;
747 case ABORTED_COMMAND:
748 action = ACTION_FAIL;
749 if (sshdr.asc == 0x10) /* DIF */
Googler9398cc32022-12-02 17:21:52 +0800750 blk_stat = BLK_STS_PROTECTION;
Googleraf606d22022-10-26 21:40:12 -0700751 break;
752 case NOT_READY:
753 /* If the device is in the process of becoming
754 * ready, or has a temporary blockage, retry.
755 */
756 if (sshdr.asc == 0x04) {
757 switch (sshdr.ascq) {
758 case 0x01: /* becoming ready */
759 case 0x04: /* format in progress */
760 case 0x05: /* rebuild in progress */
761 case 0x06: /* recalculation in progress */
762 case 0x07: /* operation in progress */
763 case 0x08: /* Long write in progress */
764 case 0x09: /* self test in progress */
Googleraf606d22022-10-26 21:40:12 -0700765 case 0x14: /* space allocation in progress */
Googler9398cc32022-12-02 17:21:52 +0800766 case 0x1a: /* start stop unit in progress */
767 case 0x1b: /* sanitize in progress */
768 case 0x1d: /* configuration in progress */
769 case 0x24: /* depopulation in progress */
Googleraf606d22022-10-26 21:40:12 -0700770 action = ACTION_DELAYED_RETRY;
771 break;
772 default:
773 action = ACTION_FAIL;
774 break;
775 }
776 } else
777 action = ACTION_FAIL;
778 break;
779 case VOLUME_OVERFLOW:
780 /* See SSC3rXX or current. */
781 action = ACTION_FAIL;
782 break;
783 default:
784 action = ACTION_FAIL;
785 break;
786 }
787 } else
788 action = ACTION_FAIL;
789
790 if (action != ACTION_FAIL &&
791 time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
792 action = ACTION_FAIL;
793
794 switch (action) {
795 case ACTION_FAIL:
796 /* Give up and fail the remainder of the request */
Googler9398cc32022-12-02 17:21:52 +0800797 if (!(req->rq_flags & RQF_QUIET)) {
Googleraf606d22022-10-26 21:40:12 -0700798 static DEFINE_RATELIMIT_STATE(_rs,
799 DEFAULT_RATELIMIT_INTERVAL,
800 DEFAULT_RATELIMIT_BURST);
801
802 if (unlikely(scsi_logging_level))
Googler9398cc32022-12-02 17:21:52 +0800803 level =
804 SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
805 SCSI_LOG_MLCOMPLETE_BITS);
Googleraf606d22022-10-26 21:40:12 -0700806
807 /*
808 * if logging is enabled the failure will be printed
809 * in scsi_log_completion(), so avoid duplicate messages
810 */
811 if (!level && __ratelimit(&_rs)) {
812 scsi_print_result(cmd, NULL, FAILED);
Googler9398cc32022-12-02 17:21:52 +0800813 if (driver_byte(result) == DRIVER_SENSE)
Googleraf606d22022-10-26 21:40:12 -0700814 scsi_print_sense(cmd);
815 scsi_print_command(cmd);
816 }
817 }
Googler9398cc32022-12-02 17:21:52 +0800818 if (!scsi_end_request(req, blk_stat, blk_rq_err_bytes(req)))
Googleraf606d22022-10-26 21:40:12 -0700819 return;
820 /*FALLTHRU*/
821 case ACTION_REPREP:
Googler9398cc32022-12-02 17:21:52 +0800822 scsi_io_completion_reprep(cmd, q);
Googleraf606d22022-10-26 21:40:12 -0700823 break;
824 case ACTION_RETRY:
825 /* Retry the same command immediately */
Googler9398cc32022-12-02 17:21:52 +0800826 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, false);
Googleraf606d22022-10-26 21:40:12 -0700827 break;
828 case ACTION_DELAYED_RETRY:
829 /* Retry the same command after a delay */
Googler9398cc32022-12-02 17:21:52 +0800830 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, false);
Googleraf606d22022-10-26 21:40:12 -0700831 break;
832 }
833}
834
Googler9398cc32022-12-02 17:21:52 +0800835/*
836 * Helper for scsi_io_completion() when cmd->result is non-zero. Returns a
837 * new result that may suppress further error checking. Also modifies
838 * *blk_statp in some cases.
839 */
840static int scsi_io_completion_nz_result(struct scsi_cmnd *cmd, int result,
841 blk_status_t *blk_statp)
842{
843 bool sense_valid;
844 bool sense_current = true; /* false implies "deferred sense" */
845 struct request *req = cmd->request;
846 struct scsi_sense_hdr sshdr;
847
848 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
849 if (sense_valid)
850 sense_current = !scsi_sense_is_deferred(&sshdr);
851
852 if (blk_rq_is_passthrough(req)) {
853 if (sense_valid) {
854 /*
855 * SG_IO wants current and deferred errors
856 */
857 scsi_req(req)->sense_len =
858 min(8 + cmd->sense_buffer[7],
859 SCSI_SENSE_BUFFERSIZE);
860 }
861 if (sense_current)
862 *blk_statp = scsi_result_to_blk_status(cmd, result);
863 } else if (blk_rq_bytes(req) == 0 && sense_current) {
864 /*
865 * Flush commands do not transfers any data, and thus cannot use
866 * good_bytes != blk_rq_bytes(req) as the signal for an error.
867 * This sets *blk_statp explicitly for the problem case.
868 */
869 *blk_statp = scsi_result_to_blk_status(cmd, result);
870 }
871 /*
872 * Recovered errors need reporting, but they're always treated as
873 * success, so fiddle the result code here. For passthrough requests
874 * we already took a copy of the original into sreq->result which
875 * is what gets returned to the user
876 */
877 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
878 bool do_print = true;
879 /*
880 * if ATA PASS-THROUGH INFORMATION AVAILABLE [0x0, 0x1d]
881 * skip print since caller wants ATA registers. Only occurs
882 * on SCSI ATA PASS_THROUGH commands when CK_COND=1
883 */
884 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
885 do_print = false;
886 else if (req->rq_flags & RQF_QUIET)
887 do_print = false;
888 if (do_print)
889 scsi_print_sense(cmd);
890 result = 0;
891 /* for passthrough, *blk_statp may be set */
892 *blk_statp = BLK_STS_OK;
893 }
894 /*
895 * Another corner case: the SCSI status byte is non-zero but 'good'.
896 * Example: PRE-FETCH command returns SAM_STAT_CONDITION_MET when
897 * it is able to fit nominated LBs in its cache (and SAM_STAT_GOOD
898 * if it can't fit). Treat SAM_STAT_CONDITION_MET and the related
899 * intermediate statuses (both obsolete in SAM-4) as good.
900 */
901 if (status_byte(result) && scsi_status_is_good(result)) {
902 result = 0;
903 *blk_statp = BLK_STS_OK;
904 }
905 return result;
906}
907
908/*
909 * Function: scsi_io_completion()
910 *
911 * Purpose: Completion processing for block device I/O requests.
912 *
913 * Arguments: cmd - command that is finished.
914 *
915 * Lock status: Assumed that no lock is held upon entry.
916 *
917 * Returns: Nothing
918 *
919 * Notes: We will finish off the specified number of sectors. If we
920 * are done, the command block will be released and the queue
921 * function will be goosed. If we are not done then we have to
922 * figure out what to do next:
923 *
924 * a) We can call scsi_requeue_command(). The request
925 * will be unprepared and put back on the queue. Then
926 * a new command will be created for it. This should
927 * be used if we made forward progress, or if we want
928 * to switch from READ(10) to READ(6) for example.
929 *
930 * b) We can call __scsi_queue_insert(). The request will
931 * be put back on the queue and retried using the same
932 * command as before, possibly after a delay.
933 *
934 * c) We can call scsi_end_request() with blk_stat other than
935 * BLK_STS_OK, to fail the remainder of the request.
936 */
937void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
938{
939 int result = cmd->result;
940 struct request_queue *q = cmd->device->request_queue;
941 struct request *req = cmd->request;
942 blk_status_t blk_stat = BLK_STS_OK;
943
944 if (unlikely(result)) /* a nz result may or may not be an error */
945 result = scsi_io_completion_nz_result(cmd, result, &blk_stat);
946
947 if (unlikely(blk_rq_is_passthrough(req))) {
948 /*
949 * scsi_result_to_blk_status may have reset the host_byte
950 */
951 scsi_req(req)->result = cmd->result;
952 }
953
954 /*
955 * Next deal with any sectors which we were able to correctly
956 * handle.
957 */
958 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
959 "%u sectors total, %d bytes done.\n",
960 blk_rq_sectors(req), good_bytes));
961
962 /*
963 * Next deal with any sectors which we were able to correctly
964 * handle. Failed, zero length commands always need to drop down
965 * to retry code. Fast path should return in this block.
966 */
967 if (likely(blk_rq_bytes(req) > 0 || blk_stat == BLK_STS_OK)) {
968 if (likely(!scsi_end_request(req, blk_stat, good_bytes)))
969 return; /* no bytes remaining */
970 }
971
972 /* Kill remainder if no retries. */
973 if (unlikely(blk_stat && scsi_noretry_cmd(cmd))) {
974 if (scsi_end_request(req, blk_stat, blk_rq_bytes(req)))
975 WARN_ONCE(true,
976 "Bytes remaining after failed, no-retry command");
977 return;
978 }
979
980 /*
981 * If there had been no error, but we have leftover bytes in the
982 * requeues just queue the command up again.
983 */
984 if (likely(result == 0))
985 scsi_io_completion_reprep(cmd, q);
986 else
987 scsi_io_completion_action(cmd, result);
988}
989
990static blk_status_t scsi_init_sgtable(struct request *req,
991 struct scsi_data_buffer *sdb)
Googleraf606d22022-10-26 21:40:12 -0700992{
993 int count;
994
995 /*
996 * If sg table allocation fails, requeue request later.
997 */
Googler9398cc32022-12-02 17:21:52 +0800998 if (unlikely(sg_alloc_table_chained(&sdb->table,
999 blk_rq_nr_phys_segments(req), sdb->table.sgl,
1000 SCSI_INLINE_SG_CNT)))
1001 return BLK_STS_RESOURCE;
Googleraf606d22022-10-26 21:40:12 -07001002
1003 /*
1004 * Next, walk the list, and fill in the addresses and sizes of
1005 * each segment.
1006 */
1007 count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
1008 BUG_ON(count > sdb->table.nents);
1009 sdb->table.nents = count;
Googler9398cc32022-12-02 17:21:52 +08001010 sdb->length = blk_rq_payload_bytes(req);
1011 return BLK_STS_OK;
Googleraf606d22022-10-26 21:40:12 -07001012}
1013
1014/*
1015 * Function: scsi_init_io()
1016 *
1017 * Purpose: SCSI I/O initialize function.
1018 *
1019 * Arguments: cmd - Command descriptor we wish to initialize
1020 *
Googler9398cc32022-12-02 17:21:52 +08001021 * Returns: BLK_STS_OK on success
1022 * BLK_STS_RESOURCE if the failure is retryable
1023 * BLK_STS_IOERR if the failure is fatal
Googleraf606d22022-10-26 21:40:12 -07001024 */
Googler9398cc32022-12-02 17:21:52 +08001025blk_status_t scsi_init_io(struct scsi_cmnd *cmd)
Googleraf606d22022-10-26 21:40:12 -07001026{
Googleraf606d22022-10-26 21:40:12 -07001027 struct request *rq = cmd->request;
Googler9398cc32022-12-02 17:21:52 +08001028 blk_status_t ret;
Googleraf606d22022-10-26 21:40:12 -07001029
Googler9398cc32022-12-02 17:21:52 +08001030 if (WARN_ON_ONCE(!blk_rq_nr_phys_segments(rq)))
1031 return BLK_STS_IOERR;
Googleraf606d22022-10-26 21:40:12 -07001032
Googler9398cc32022-12-02 17:21:52 +08001033 ret = scsi_init_sgtable(rq, &cmd->sdb);
1034 if (ret)
1035 return ret;
Googleraf606d22022-10-26 21:40:12 -07001036
1037 if (blk_integrity_rq(rq)) {
1038 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1039 int ivecs, count;
1040
Googler9398cc32022-12-02 17:21:52 +08001041 if (WARN_ON_ONCE(!prot_sdb)) {
Googleraf606d22022-10-26 21:40:12 -07001042 /*
1043 * This can happen if someone (e.g. multipath)
1044 * queues a command to a device on an adapter
1045 * that does not support DIX.
1046 */
Googler9398cc32022-12-02 17:21:52 +08001047 ret = BLK_STS_IOERR;
1048 goto out_free_sgtables;
Googleraf606d22022-10-26 21:40:12 -07001049 }
1050
1051 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
1052
1053 if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
Googler9398cc32022-12-02 17:21:52 +08001054 prot_sdb->table.sgl,
1055 SCSI_INLINE_PROT_SG_CNT)) {
1056 ret = BLK_STS_RESOURCE;
1057 goto out_free_sgtables;
Googleraf606d22022-10-26 21:40:12 -07001058 }
1059
1060 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
1061 prot_sdb->table.sgl);
Googler9398cc32022-12-02 17:21:52 +08001062 BUG_ON(count > ivecs);
1063 BUG_ON(count > queue_max_integrity_segments(rq->q));
Googleraf606d22022-10-26 21:40:12 -07001064
1065 cmd->prot_sdb = prot_sdb;
1066 cmd->prot_sdb->table.nents = count;
1067 }
1068
Googler9398cc32022-12-02 17:21:52 +08001069 return BLK_STS_OK;
1070out_free_sgtables:
1071 scsi_free_sgtables(cmd);
1072 return ret;
Googleraf606d22022-10-26 21:40:12 -07001073}
1074EXPORT_SYMBOL(scsi_init_io);
1075
Googler9398cc32022-12-02 17:21:52 +08001076/**
1077 * scsi_initialize_rq - initialize struct scsi_cmnd partially
1078 * @rq: Request associated with the SCSI command to be initialized.
1079 *
1080 * This function initializes the members of struct scsi_cmnd that must be
1081 * initialized before request processing starts and that won't be
1082 * reinitialized if a SCSI command is requeued.
1083 *
1084 * Called from inside blk_get_request() for pass-through requests and from
1085 * inside scsi_init_command() for filesystem requests.
1086 */
1087static void scsi_initialize_rq(struct request *rq)
Googler38bda472022-08-19 10:07:08 -07001088{
Googler9398cc32022-12-02 17:21:52 +08001089 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
Googler012a81c2022-09-15 14:55:24 +08001090
Googler9398cc32022-12-02 17:21:52 +08001091 scsi_req_init(&cmd->req);
1092 init_rcu_head(&cmd->rcu);
1093 cmd->jiffies_at_alloc = jiffies;
1094 cmd->retries = 0;
Googler0109c452022-10-13 17:50:39 +08001095}
1096
Googler9398cc32022-12-02 17:21:52 +08001097/*
1098 * Only called when the request isn't completed by SCSI, and not freed by
1099 * SCSI
1100 */
1101static void scsi_cleanup_rq(struct request *rq)
Googler0109c452022-10-13 17:50:39 +08001102{
Googler9398cc32022-12-02 17:21:52 +08001103 if (rq->rq_flags & RQF_DONTPREP) {
1104 scsi_mq_uninit_cmd(blk_mq_rq_to_pdu(rq));
1105 rq->rq_flags &= ~RQF_DONTPREP;
1106 }
1107}
1108
1109/* Add a command to the list used by the aacraid and dpt_i2o drivers */
1110void scsi_add_cmd_to_list(struct scsi_cmnd *cmd)
1111{
1112 struct scsi_device *sdev = cmd->device;
1113 struct Scsi_Host *shost = sdev->host;
1114 unsigned long flags;
1115
1116 if (shost->use_cmd_list) {
1117 spin_lock_irqsave(&sdev->list_lock, flags);
1118 list_add_tail(&cmd->list, &sdev->cmd_list);
1119 spin_unlock_irqrestore(&sdev->list_lock, flags);
1120 }
1121}
1122
1123/* Remove a command from the list used by the aacraid and dpt_i2o drivers */
1124void scsi_del_cmd_from_list(struct scsi_cmnd *cmd)
1125{
1126 struct scsi_device *sdev = cmd->device;
1127 struct Scsi_Host *shost = sdev->host;
1128 unsigned long flags;
1129
1130 if (shost->use_cmd_list) {
1131 spin_lock_irqsave(&sdev->list_lock, flags);
1132 BUG_ON(list_empty(&cmd->list));
1133 list_del_init(&cmd->list);
1134 spin_unlock_irqrestore(&sdev->list_lock, flags);
1135 }
1136}
1137
1138/* Called after a request has been started. */
1139void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd)
1140{
1141 void *buf = cmd->sense_buffer;
1142 void *prot = cmd->prot_sdb;
1143 struct request *rq = blk_mq_rq_from_pdu(cmd);
1144 unsigned int flags = cmd->flags & SCMD_PRESERVED_FLAGS;
1145 unsigned long jiffies_at_alloc;
1146 int retries;
1147
1148 if (!blk_rq_is_scsi(rq) && !(flags & SCMD_INITIALIZED)) {
1149 flags |= SCMD_INITIALIZED;
1150 scsi_initialize_rq(rq);
1151 }
1152
1153 jiffies_at_alloc = cmd->jiffies_at_alloc;
1154 retries = cmd->retries;
1155 /* zero out the cmd, except for the embedded scsi_request */
1156 memset((char *)cmd + sizeof(cmd->req), 0,
1157 sizeof(*cmd) - sizeof(cmd->req) + dev->host->hostt->cmd_size);
1158
1159 cmd->device = dev;
1160 cmd->sense_buffer = buf;
1161 cmd->prot_sdb = prot;
1162 cmd->flags = flags;
1163 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1164 cmd->jiffies_at_alloc = jiffies_at_alloc;
1165 cmd->retries = retries;
1166
1167 scsi_add_cmd_to_list(cmd);
1168}
1169
1170static blk_status_t scsi_setup_scsi_cmnd(struct scsi_device *sdev,
1171 struct request *req)
1172{
1173 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Googleraf606d22022-10-26 21:40:12 -07001174
1175 /*
Googler9398cc32022-12-02 17:21:52 +08001176 * Passthrough requests may transfer data, in which case they must
Googleraf606d22022-10-26 21:40:12 -07001177 * a bio attached to them. Or they might contain a SCSI command
1178 * that does not transfer data, in which case they may optionally
1179 * submit a request without an attached bio.
1180 */
1181 if (req->bio) {
Googler9398cc32022-12-02 17:21:52 +08001182 blk_status_t ret = scsi_init_io(cmd);
1183 if (unlikely(ret != BLK_STS_OK))
Googleraf606d22022-10-26 21:40:12 -07001184 return ret;
1185 } else {
1186 BUG_ON(blk_rq_bytes(req));
1187
1188 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
1189 }
1190
Googler9398cc32022-12-02 17:21:52 +08001191 cmd->cmd_len = scsi_req(req)->cmd_len;
1192 cmd->cmnd = scsi_req(req)->cmd;
Googleraf606d22022-10-26 21:40:12 -07001193 cmd->transfersize = blk_rq_bytes(req);
Googler9398cc32022-12-02 17:21:52 +08001194 cmd->allowed = scsi_req(req)->retries;
1195 return BLK_STS_OK;
Googleraf606d22022-10-26 21:40:12 -07001196}
1197
1198/*
Googler9398cc32022-12-02 17:21:52 +08001199 * Setup a normal block command. These are simple request from filesystems
Googleraf606d22022-10-26 21:40:12 -07001200 * that still need to be translated to SCSI CDBs from the ULD.
1201 */
Googler9398cc32022-12-02 17:21:52 +08001202static blk_status_t scsi_setup_fs_cmnd(struct scsi_device *sdev,
1203 struct request *req)
Googleraf606d22022-10-26 21:40:12 -07001204{
Googler9398cc32022-12-02 17:21:52 +08001205 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Googleraf606d22022-10-26 21:40:12 -07001206
1207 if (unlikely(sdev->handler && sdev->handler->prep_fn)) {
Googler9398cc32022-12-02 17:21:52 +08001208 blk_status_t ret = sdev->handler->prep_fn(sdev, req);
1209 if (ret != BLK_STS_OK)
Googleraf606d22022-10-26 21:40:12 -07001210 return ret;
1211 }
1212
Googler9398cc32022-12-02 17:21:52 +08001213 cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd;
Googleraf606d22022-10-26 21:40:12 -07001214 memset(cmd->cmnd, 0, BLK_MAX_CDB);
1215 return scsi_cmd_to_driver(cmd)->init_command(cmd);
1216}
1217
Googler9398cc32022-12-02 17:21:52 +08001218static blk_status_t scsi_setup_cmnd(struct scsi_device *sdev,
1219 struct request *req)
Googleraf606d22022-10-26 21:40:12 -07001220{
Googler9398cc32022-12-02 17:21:52 +08001221 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1222 blk_status_t ret;
Googleraf606d22022-10-26 21:40:12 -07001223
1224 if (!blk_rq_bytes(req))
1225 cmd->sc_data_direction = DMA_NONE;
1226 else if (rq_data_dir(req) == WRITE)
1227 cmd->sc_data_direction = DMA_TO_DEVICE;
1228 else
1229 cmd->sc_data_direction = DMA_FROM_DEVICE;
1230
Googler9398cc32022-12-02 17:21:52 +08001231 if (blk_rq_is_scsi(req))
1232 ret = scsi_setup_scsi_cmnd(sdev, req);
1233 else
1234 ret = scsi_setup_fs_cmnd(sdev, req);
1235
1236 if (ret != BLK_STS_OK)
1237 scsi_free_sgtables(cmd);
1238
1239 return ret;
Googler012a81c2022-09-15 14:55:24 +08001240}
Googler38bda472022-08-19 10:07:08 -07001241
Googler9398cc32022-12-02 17:21:52 +08001242static blk_status_t
Googler012a81c2022-09-15 14:55:24 +08001243scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
1244{
Googler9398cc32022-12-02 17:21:52 +08001245 switch (sdev->sdev_state) {
1246 case SDEV_OFFLINE:
1247 case SDEV_TRANSPORT_OFFLINE:
Googler012a81c2022-09-15 14:55:24 +08001248 /*
Googler9398cc32022-12-02 17:21:52 +08001249 * If the device is offline we refuse to process any
1250 * commands. The device must be brought online
1251 * before trying any recovery commands.
Googler012a81c2022-09-15 14:55:24 +08001252 */
Googler9398cc32022-12-02 17:21:52 +08001253 sdev_printk(KERN_ERR, sdev,
1254 "rejecting I/O to offline device\n");
1255 return BLK_STS_IOERR;
1256 case SDEV_DEL:
1257 /*
1258 * If the device is fully deleted, we refuse to
1259 * process any commands as well.
1260 */
1261 sdev_printk(KERN_ERR, sdev,
1262 "rejecting I/O to dead device\n");
1263 return BLK_STS_IOERR;
1264 case SDEV_BLOCK:
1265 case SDEV_CREATED_BLOCK:
1266 return BLK_STS_RESOURCE;
1267 case SDEV_QUIESCE:
1268 /*
1269 * If the devices is blocked we defer normal commands.
1270 */
1271 if (req && !(req->rq_flags & RQF_PREEMPT))
1272 return BLK_STS_RESOURCE;
1273 return BLK_STS_OK;
Googler012a81c2022-09-15 14:55:24 +08001274 default:
Googler9398cc32022-12-02 17:21:52 +08001275 /*
1276 * For any other not fully online state we only allow
1277 * special commands. In particular any user initiated
1278 * command is not allowed.
1279 */
1280 if (req && !(req->rq_flags & RQF_PREEMPT))
1281 return BLK_STS_IOERR;
1282 return BLK_STS_OK;
Googler012a81c2022-09-15 14:55:24 +08001283 }
Googleraf606d22022-10-26 21:40:12 -07001284}
1285
1286/*
1287 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1288 * return 0.
1289 *
1290 * Called with the queue_lock held.
1291 */
1292static inline int scsi_dev_queue_ready(struct request_queue *q,
1293 struct scsi_device *sdev)
1294{
1295 unsigned int busy;
1296
1297 busy = atomic_inc_return(&sdev->device_busy) - 1;
1298 if (atomic_read(&sdev->device_blocked)) {
1299 if (busy)
1300 goto out_dec;
1301
1302 /*
1303 * unblock after device_blocked iterates to zero
1304 */
Googler9398cc32022-12-02 17:21:52 +08001305 if (atomic_dec_return(&sdev->device_blocked) > 0)
Googleraf606d22022-10-26 21:40:12 -07001306 goto out_dec;
Googleraf606d22022-10-26 21:40:12 -07001307 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1308 "unblocking device at zero depth\n"));
1309 }
1310
1311 if (busy >= sdev->queue_depth)
1312 goto out_dec;
1313
1314 return 1;
1315out_dec:
1316 atomic_dec(&sdev->device_busy);
1317 return 0;
1318}
1319
1320/*
1321 * scsi_target_queue_ready: checks if there we can send commands to target
1322 * @sdev: scsi device on starget to check.
1323 */
1324static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1325 struct scsi_device *sdev)
1326{
1327 struct scsi_target *starget = scsi_target(sdev);
1328 unsigned int busy;
1329
1330 if (starget->single_lun) {
1331 spin_lock_irq(shost->host_lock);
1332 if (starget->starget_sdev_user &&
1333 starget->starget_sdev_user != sdev) {
1334 spin_unlock_irq(shost->host_lock);
1335 return 0;
1336 }
1337 starget->starget_sdev_user = sdev;
1338 spin_unlock_irq(shost->host_lock);
1339 }
1340
1341 if (starget->can_queue <= 0)
1342 return 1;
1343
1344 busy = atomic_inc_return(&starget->target_busy) - 1;
1345 if (atomic_read(&starget->target_blocked) > 0) {
1346 if (busy)
1347 goto starved;
1348
1349 /*
1350 * unblock after target_blocked iterates to zero
1351 */
1352 if (atomic_dec_return(&starget->target_blocked) > 0)
1353 goto out_dec;
1354
1355 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1356 "unblocking target at zero depth\n"));
1357 }
1358
1359 if (busy >= starget->can_queue)
1360 goto starved;
1361
1362 return 1;
1363
1364starved:
1365 spin_lock_irq(shost->host_lock);
1366 list_move_tail(&sdev->starved_entry, &shost->starved_list);
1367 spin_unlock_irq(shost->host_lock);
1368out_dec:
1369 if (starget->can_queue > 0)
1370 atomic_dec(&starget->target_busy);
1371 return 0;
1372}
1373
1374/*
1375 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1376 * return 0. We must end up running the queue again whenever 0 is
1377 * returned, else IO can hang.
1378 */
1379static inline int scsi_host_queue_ready(struct request_queue *q,
1380 struct Scsi_Host *shost,
1381 struct scsi_device *sdev)
1382{
1383 unsigned int busy;
1384
1385 if (scsi_host_in_recovery(shost))
1386 return 0;
1387
1388 busy = atomic_inc_return(&shost->host_busy) - 1;
1389 if (atomic_read(&shost->host_blocked) > 0) {
1390 if (busy)
1391 goto starved;
1392
1393 /*
1394 * unblock after host_blocked iterates to zero
1395 */
1396 if (atomic_dec_return(&shost->host_blocked) > 0)
1397 goto out_dec;
1398
1399 SCSI_LOG_MLQUEUE(3,
1400 shost_printk(KERN_INFO, shost,
1401 "unblocking host at zero depth\n"));
1402 }
1403
1404 if (shost->can_queue > 0 && busy >= shost->can_queue)
1405 goto starved;
1406 if (shost->host_self_blocked)
1407 goto starved;
1408
1409 /* We're OK to process the command, so we can't be starved */
1410 if (!list_empty(&sdev->starved_entry)) {
1411 spin_lock_irq(shost->host_lock);
1412 if (!list_empty(&sdev->starved_entry))
1413 list_del_init(&sdev->starved_entry);
1414 spin_unlock_irq(shost->host_lock);
1415 }
1416
1417 return 1;
1418
1419starved:
1420 spin_lock_irq(shost->host_lock);
1421 if (list_empty(&sdev->starved_entry))
1422 list_add_tail(&sdev->starved_entry, &shost->starved_list);
1423 spin_unlock_irq(shost->host_lock);
1424out_dec:
Googler9398cc32022-12-02 17:21:52 +08001425 scsi_dec_host_busy(shost);
Googleraf606d22022-10-26 21:40:12 -07001426 return 0;
1427}
1428
1429/*
1430 * Busy state exporting function for request stacking drivers.
1431 *
1432 * For efficiency, no lock is taken to check the busy state of
1433 * shost/starget/sdev, since the returned value is not guaranteed and
1434 * may be changed after request stacking drivers call the function,
1435 * regardless of taking lock or not.
1436 *
1437 * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
1438 * needs to return 'not busy'. Otherwise, request stacking drivers
1439 * may hold requests forever.
1440 */
Googler9398cc32022-12-02 17:21:52 +08001441static bool scsi_mq_lld_busy(struct request_queue *q)
Googleraf606d22022-10-26 21:40:12 -07001442{
1443 struct scsi_device *sdev = q->queuedata;
1444 struct Scsi_Host *shost;
1445
1446 if (blk_queue_dying(q))
Googler9398cc32022-12-02 17:21:52 +08001447 return false;
Googleraf606d22022-10-26 21:40:12 -07001448
1449 shost = sdev->host;
1450
1451 /*
1452 * Ignore host/starget busy state.
1453 * Since block layer does not have a concept of fairness across
1454 * multiple queues, congestion of host/starget needs to be handled
1455 * in SCSI layer.
1456 */
1457 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
Googler9398cc32022-12-02 17:21:52 +08001458 return true;
Googleraf606d22022-10-26 21:40:12 -07001459
Googler9398cc32022-12-02 17:21:52 +08001460 return false;
Googleraf606d22022-10-26 21:40:12 -07001461}
1462
1463static void scsi_softirq_done(struct request *rq)
1464{
Googler9398cc32022-12-02 17:21:52 +08001465 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
Googleraf606d22022-10-26 21:40:12 -07001466 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
1467 int disposition;
1468
1469 INIT_LIST_HEAD(&cmd->eh_entry);
1470
1471 atomic_inc(&cmd->device->iodone_cnt);
1472 if (cmd->result)
1473 atomic_inc(&cmd->device->ioerr_cnt);
1474
1475 disposition = scsi_decide_disposition(cmd);
1476 if (disposition != SUCCESS &&
1477 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
Googler9398cc32022-12-02 17:21:52 +08001478 scmd_printk(KERN_ERR, cmd,
Googleraf606d22022-10-26 21:40:12 -07001479 "timing out command, waited %lus\n",
1480 wait_for/HZ);
1481 disposition = SUCCESS;
1482 }
1483
1484 scsi_log_completion(cmd, disposition);
1485
1486 switch (disposition) {
1487 case SUCCESS:
1488 scsi_finish_command(cmd);
1489 break;
1490 case NEEDS_RETRY:
1491 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
1492 break;
1493 case ADD_TO_MLQUEUE:
1494 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1495 break;
1496 default:
Googler9398cc32022-12-02 17:21:52 +08001497 scsi_eh_scmd_add(cmd);
1498 break;
Googleraf606d22022-10-26 21:40:12 -07001499 }
1500}
1501
1502/**
1503 * scsi_dispatch_command - Dispatch a command to the low-level driver.
1504 * @cmd: command block we are dispatching.
1505 *
1506 * Return: nonzero return request was rejected and device's queue needs to be
1507 * plugged.
1508 */
1509static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
1510{
1511 struct Scsi_Host *host = cmd->device->host;
1512 int rtn = 0;
1513
1514 atomic_inc(&cmd->device->iorequest_cnt);
1515
1516 /* check if the device is still usable */
1517 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
1518 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
1519 * returns an immediate error upwards, and signals
1520 * that the device is no longer present */
1521 cmd->result = DID_NO_CONNECT << 16;
1522 goto done;
1523 }
1524
1525 /* Check to see if the scsi lld made this device blocked. */
1526 if (unlikely(scsi_device_blocked(cmd->device))) {
1527 /*
1528 * in blocked state, the command is just put back on
1529 * the device queue. The suspend state has already
1530 * blocked the queue so future requests should not
1531 * occur until the device transitions out of the
1532 * suspend state.
1533 */
1534 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1535 "queuecommand : device blocked\n"));
1536 return SCSI_MLQUEUE_DEVICE_BUSY;
1537 }
1538
1539 /* Store the LUN value in cmnd, if needed. */
1540 if (cmd->device->lun_in_cdb)
1541 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
1542 (cmd->device->lun << 5 & 0xe0);
1543
1544 scsi_log_send(cmd);
1545
1546 /*
1547 * Before we queue this command, check if the command
1548 * length exceeds what the host adapter can handle.
1549 */
1550 if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
1551 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1552 "queuecommand : command too long. "
1553 "cdb_size=%d host->max_cmd_len=%d\n",
1554 cmd->cmd_len, cmd->device->host->max_cmd_len));
1555 cmd->result = (DID_ABORT << 16);
1556 goto done;
1557 }
1558
1559 if (unlikely(host->shost_state == SHOST_DEL)) {
1560 cmd->result = (DID_NO_CONNECT << 16);
1561 goto done;
1562
1563 }
1564
1565 trace_scsi_dispatch_cmd_start(cmd);
1566 rtn = host->hostt->queuecommand(host, cmd);
1567 if (rtn) {
1568 trace_scsi_dispatch_cmd_error(cmd, rtn);
1569 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
1570 rtn != SCSI_MLQUEUE_TARGET_BUSY)
1571 rtn = SCSI_MLQUEUE_HOST_BUSY;
1572
1573 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1574 "queuecommand : request rejected\n"));
1575 }
1576
1577 return rtn;
1578 done:
1579 cmd->scsi_done(cmd);
1580 return 0;
1581}
1582
Googler9398cc32022-12-02 17:21:52 +08001583/* Size in bytes of the sg-list stored in the scsi-mq command-private data. */
1584static unsigned int scsi_mq_inline_sgl_size(struct Scsi_Host *shost)
Googleraf606d22022-10-26 21:40:12 -07001585{
Googler9398cc32022-12-02 17:21:52 +08001586 return min_t(unsigned int, shost->sg_tablesize, SCSI_INLINE_SG_CNT) *
1587 sizeof(struct scatterlist);
Googleraf606d22022-10-26 21:40:12 -07001588}
1589
Googler9398cc32022-12-02 17:21:52 +08001590static blk_status_t scsi_mq_prep_fn(struct request *req)
Googleraf606d22022-10-26 21:40:12 -07001591{
1592 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1593 struct scsi_device *sdev = req->q->queuedata;
1594 struct Scsi_Host *shost = sdev->host;
Googleraf606d22022-10-26 21:40:12 -07001595 struct scatterlist *sg;
1596
Googler9398cc32022-12-02 17:21:52 +08001597 scsi_init_command(sdev, cmd);
Googleraf606d22022-10-26 21:40:12 -07001598
1599 cmd->request = req;
Googleraf606d22022-10-26 21:40:12 -07001600 cmd->tag = req->tag;
Googleraf606d22022-10-26 21:40:12 -07001601 cmd->prot_op = SCSI_PROT_NORMAL;
1602
Googleraf606d22022-10-26 21:40:12 -07001603 sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
1604 cmd->sdb.table.sgl = sg;
1605
1606 if (scsi_host_get_prot(shost)) {
Googleraf606d22022-10-26 21:40:12 -07001607 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
1608
1609 cmd->prot_sdb->table.sgl =
1610 (struct scatterlist *)(cmd->prot_sdb + 1);
1611 }
1612
Googleraf606d22022-10-26 21:40:12 -07001613 blk_mq_start_request(req);
1614
1615 return scsi_setup_cmnd(sdev, req);
1616}
1617
1618static void scsi_mq_done(struct scsi_cmnd *cmd)
1619{
Googler9398cc32022-12-02 17:21:52 +08001620 if (unlikely(test_and_set_bit(SCMD_STATE_COMPLETE, &cmd->state)))
1621 return;
Googleraf606d22022-10-26 21:40:12 -07001622 trace_scsi_dispatch_cmd_done(cmd);
Googler9398cc32022-12-02 17:21:52 +08001623
1624 /*
1625 * If the block layer didn't complete the request due to a timeout
1626 * injection, scsi must clear its internal completed state so that the
1627 * timeout handler will see it needs to escalate its own error
1628 * recovery.
1629 */
1630 if (unlikely(!blk_mq_complete_request(cmd->request)))
1631 clear_bit(SCMD_STATE_COMPLETE, &cmd->state);
Googleraf606d22022-10-26 21:40:12 -07001632}
1633
Googler9398cc32022-12-02 17:21:52 +08001634static void scsi_mq_put_budget(struct blk_mq_hw_ctx *hctx)
1635{
1636 struct request_queue *q = hctx->queue;
1637 struct scsi_device *sdev = q->queuedata;
1638
1639 atomic_dec(&sdev->device_busy);
1640}
1641
1642static bool scsi_mq_get_budget(struct blk_mq_hw_ctx *hctx)
1643{
1644 struct request_queue *q = hctx->queue;
1645 struct scsi_device *sdev = q->queuedata;
1646
1647 if (scsi_dev_queue_ready(q, sdev))
1648 return true;
1649
1650 if (atomic_read(&sdev->device_busy) == 0 && !scsi_device_blocked(sdev))
1651 blk_mq_delay_run_hw_queue(hctx, SCSI_QUEUE_DELAY);
1652 return false;
1653}
1654
1655static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
Googleraf606d22022-10-26 21:40:12 -07001656 const struct blk_mq_queue_data *bd)
1657{
1658 struct request *req = bd->rq;
1659 struct request_queue *q = req->q;
1660 struct scsi_device *sdev = q->queuedata;
1661 struct Scsi_Host *shost = sdev->host;
1662 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
Googler9398cc32022-12-02 17:21:52 +08001663 blk_status_t ret;
Googleraf606d22022-10-26 21:40:12 -07001664 int reason;
1665
Googler9398cc32022-12-02 17:21:52 +08001666 /*
1667 * If the device is not in running state we will reject some or all
1668 * commands.
1669 */
1670 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
1671 ret = scsi_prep_state_check(sdev, req);
1672 if (ret != BLK_STS_OK)
1673 goto out_put_budget;
1674 }
Googleraf606d22022-10-26 21:40:12 -07001675
Googler9398cc32022-12-02 17:21:52 +08001676 ret = BLK_STS_RESOURCE;
Googleraf606d22022-10-26 21:40:12 -07001677 if (!scsi_target_queue_ready(shost, sdev))
Googler9398cc32022-12-02 17:21:52 +08001678 goto out_put_budget;
Googleraf606d22022-10-26 21:40:12 -07001679 if (!scsi_host_queue_ready(q, shost, sdev))
1680 goto out_dec_target_busy;
1681
Googler9398cc32022-12-02 17:21:52 +08001682 if (!(req->rq_flags & RQF_DONTPREP)) {
1683 ret = scsi_mq_prep_fn(req);
1684 if (ret != BLK_STS_OK)
Googleraf606d22022-10-26 21:40:12 -07001685 goto out_dec_host_busy;
Googler9398cc32022-12-02 17:21:52 +08001686 req->rq_flags |= RQF_DONTPREP;
Googleraf606d22022-10-26 21:40:12 -07001687 } else {
Googler9398cc32022-12-02 17:21:52 +08001688 clear_bit(SCMD_STATE_COMPLETE, &cmd->state);
Googleraf606d22022-10-26 21:40:12 -07001689 blk_mq_start_request(req);
1690 }
1691
Googler9398cc32022-12-02 17:21:52 +08001692 cmd->flags &= SCMD_PRESERVED_FLAGS;
Googleraf606d22022-10-26 21:40:12 -07001693 if (sdev->simple_tags)
1694 cmd->flags |= SCMD_TAGGED;
Googler9398cc32022-12-02 17:21:52 +08001695 if (bd->last)
1696 cmd->flags |= SCMD_LAST;
Googleraf606d22022-10-26 21:40:12 -07001697
1698 scsi_init_cmd_errh(cmd);
1699 cmd->scsi_done = scsi_mq_done;
1700
1701 reason = scsi_dispatch_cmd(cmd);
1702 if (reason) {
1703 scsi_set_blocked(cmd, reason);
Googler9398cc32022-12-02 17:21:52 +08001704 ret = BLK_STS_RESOURCE;
Googleraf606d22022-10-26 21:40:12 -07001705 goto out_dec_host_busy;
1706 }
1707
Googler9398cc32022-12-02 17:21:52 +08001708 return BLK_STS_OK;
Googleraf606d22022-10-26 21:40:12 -07001709
1710out_dec_host_busy:
Googler9398cc32022-12-02 17:21:52 +08001711 scsi_dec_host_busy(shost);
Googleraf606d22022-10-26 21:40:12 -07001712out_dec_target_busy:
1713 if (scsi_target(sdev)->can_queue > 0)
1714 atomic_dec(&scsi_target(sdev)->target_busy);
Googler9398cc32022-12-02 17:21:52 +08001715out_put_budget:
1716 scsi_mq_put_budget(hctx);
Googleraf606d22022-10-26 21:40:12 -07001717 switch (ret) {
Googler9398cc32022-12-02 17:21:52 +08001718 case BLK_STS_OK:
Googleraf606d22022-10-26 21:40:12 -07001719 break;
Googler9398cc32022-12-02 17:21:52 +08001720 case BLK_STS_RESOURCE:
Googlerb48fa912023-03-17 12:40:29 +05301721 if (atomic_read(&sdev->device_busy) ||
1722 scsi_device_blocked(sdev))
Googler9398cc32022-12-02 17:21:52 +08001723 ret = BLK_STS_DEV_RESOURCE;
1724 break;
1725 default:
1726 if (unlikely(!scsi_device_online(sdev)))
1727 scsi_req(req)->result = DID_NO_CONNECT << 16;
1728 else
1729 scsi_req(req)->result = DID_ERROR << 16;
Googleraf606d22022-10-26 21:40:12 -07001730 /*
Googler9398cc32022-12-02 17:21:52 +08001731 * Make sure to release all allocated resources when
Googleraf606d22022-10-26 21:40:12 -07001732 * we hit an error, as we will never see this command
1733 * again.
1734 */
Googler9398cc32022-12-02 17:21:52 +08001735 if (req->rq_flags & RQF_DONTPREP)
Googleraf606d22022-10-26 21:40:12 -07001736 scsi_mq_uninit_cmd(cmd);
Googler9398cc32022-12-02 17:21:52 +08001737 scsi_run_queue_async(sdev);
Googler9726be62022-12-14 05:53:31 +00001738 break;
Googleraf606d22022-10-26 21:40:12 -07001739 }
1740 return ret;
1741}
1742
1743static enum blk_eh_timer_return scsi_timeout(struct request *req,
1744 bool reserved)
1745{
1746 if (reserved)
1747 return BLK_EH_RESET_TIMER;
1748 return scsi_times_out(req);
1749}
1750
Googler9398cc32022-12-02 17:21:52 +08001751static int scsi_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
1752 unsigned int hctx_idx, unsigned int numa_node)
Googleraf606d22022-10-26 21:40:12 -07001753{
Googler9398cc32022-12-02 17:21:52 +08001754 struct Scsi_Host *shost = set->driver_data;
1755 const bool unchecked_isa_dma = shost->unchecked_isa_dma;
Googleraf606d22022-10-26 21:40:12 -07001756 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
Googler9398cc32022-12-02 17:21:52 +08001757 struct scatterlist *sg;
Googleraf606d22022-10-26 21:40:12 -07001758
Googler9398cc32022-12-02 17:21:52 +08001759 if (unchecked_isa_dma)
1760 cmd->flags |= SCMD_UNCHECKED_ISA_DMA;
1761 cmd->sense_buffer = scsi_alloc_sense_buffer(unchecked_isa_dma,
1762 GFP_KERNEL, numa_node);
Googleraf606d22022-10-26 21:40:12 -07001763 if (!cmd->sense_buffer)
1764 return -ENOMEM;
Googler9398cc32022-12-02 17:21:52 +08001765 cmd->req.sense = cmd->sense_buffer;
1766
1767 if (scsi_host_get_prot(shost)) {
1768 sg = (void *)cmd + sizeof(struct scsi_cmnd) +
1769 shost->hostt->cmd_size;
1770 cmd->prot_sdb = (void *)sg + scsi_mq_inline_sgl_size(shost);
1771 }
1772
Googleraf606d22022-10-26 21:40:12 -07001773 return 0;
1774}
1775
Googler9398cc32022-12-02 17:21:52 +08001776static void scsi_mq_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1777 unsigned int hctx_idx)
Googleraf606d22022-10-26 21:40:12 -07001778{
1779 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1780
Googler9398cc32022-12-02 17:21:52 +08001781 scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA,
1782 cmd->sense_buffer);
Googleraf606d22022-10-26 21:40:12 -07001783}
1784
Googler9398cc32022-12-02 17:21:52 +08001785static int scsi_map_queues(struct blk_mq_tag_set *set)
Googleraf606d22022-10-26 21:40:12 -07001786{
Googler9398cc32022-12-02 17:21:52 +08001787 struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
Googleraf606d22022-10-26 21:40:12 -07001788
Googler9398cc32022-12-02 17:21:52 +08001789 if (shost->hostt->map_queues)
1790 return shost->hostt->map_queues(shost);
1791 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Googleraf606d22022-10-26 21:40:12 -07001792}
1793
Googler9398cc32022-12-02 17:21:52 +08001794void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
Googleraf606d22022-10-26 21:40:12 -07001795{
1796 struct device *dev = shost->dma_dev;
1797
1798 /*
1799 * this limit is imposed by hardware restrictions
1800 */
1801 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
1802 SG_MAX_SEGMENTS));
1803
1804 if (scsi_host_prot_dma(shost)) {
1805 shost->sg_prot_tablesize =
1806 min_not_zero(shost->sg_prot_tablesize,
1807 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
1808 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
1809 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
1810 }
1811
Googler9398cc32022-12-02 17:21:52 +08001812 if (dev->dma_mask) {
1813 shost->max_sectors = min_t(unsigned int, shost->max_sectors,
1814 dma_max_mapping_size(dev) >> SECTOR_SHIFT);
1815 }
Googleraf606d22022-10-26 21:40:12 -07001816 blk_queue_max_hw_sectors(q, shost->max_sectors);
Googler9398cc32022-12-02 17:21:52 +08001817 if (shost->unchecked_isa_dma)
1818 blk_queue_bounce_limit(q, BLK_BOUNCE_ISA);
Googleraf606d22022-10-26 21:40:12 -07001819 blk_queue_segment_boundary(q, shost->dma_boundary);
1820 dma_set_seg_boundary(dev, shost->dma_boundary);
1821
Googler9398cc32022-12-02 17:21:52 +08001822 blk_queue_max_segment_size(q, shost->max_segment_size);
1823 blk_queue_virt_boundary(q, shost->virt_boundary_mask);
1824 dma_set_max_seg_size(dev, queue_max_segment_size(q));
Googleraf606d22022-10-26 21:40:12 -07001825
1826 /*
1827 * Set a reasonable default alignment: The larger of 32-byte (dword),
1828 * which is a common minimum for HBAs, and the minimum DMA alignment,
1829 * which is set by the platform.
1830 *
1831 * Devices that require a bigger alignment can increase it later.
1832 */
1833 blk_queue_dma_alignment(q, max(4, dma_get_cache_alignment()) - 1);
1834}
Googler9398cc32022-12-02 17:21:52 +08001835EXPORT_SYMBOL_GPL(__scsi_init_queue);
Googleraf606d22022-10-26 21:40:12 -07001836
Googler9398cc32022-12-02 17:21:52 +08001837static const struct blk_mq_ops scsi_mq_ops_no_commit = {
1838 .get_budget = scsi_mq_get_budget,
1839 .put_budget = scsi_mq_put_budget,
Googler38bda472022-08-19 10:07:08 -07001840 .queue_rq = scsi_queue_rq,
Googler38bda472022-08-19 10:07:08 -07001841 .complete = scsi_softirq_done,
1842 .timeout = scsi_timeout,
Googler9398cc32022-12-02 17:21:52 +08001843#ifdef CONFIG_BLK_DEBUG_FS
1844 .show_rq = scsi_show_rq,
1845#endif
1846 .init_request = scsi_mq_init_request,
1847 .exit_request = scsi_mq_exit_request,
1848 .initialize_rq_fn = scsi_initialize_rq,
1849 .cleanup_rq = scsi_cleanup_rq,
1850 .busy = scsi_mq_lld_busy,
1851 .map_queues = scsi_map_queues,
1852};
1853
1854
1855static void scsi_commit_rqs(struct blk_mq_hw_ctx *hctx)
1856{
1857 struct request_queue *q = hctx->queue;
1858 struct scsi_device *sdev = q->queuedata;
1859 struct Scsi_Host *shost = sdev->host;
1860
1861 shost->hostt->commit_rqs(shost, hctx->queue_num);
1862}
1863
1864static const struct blk_mq_ops scsi_mq_ops = {
1865 .get_budget = scsi_mq_get_budget,
1866 .put_budget = scsi_mq_put_budget,
1867 .queue_rq = scsi_queue_rq,
1868 .commit_rqs = scsi_commit_rqs,
1869 .complete = scsi_softirq_done,
1870 .timeout = scsi_timeout,
1871#ifdef CONFIG_BLK_DEBUG_FS
1872 .show_rq = scsi_show_rq,
1873#endif
1874 .init_request = scsi_mq_init_request,
1875 .exit_request = scsi_mq_exit_request,
1876 .initialize_rq_fn = scsi_initialize_rq,
1877 .cleanup_rq = scsi_cleanup_rq,
1878 .busy = scsi_mq_lld_busy,
1879 .map_queues = scsi_map_queues,
Googleraf606d22022-10-26 21:40:12 -07001880};
1881
1882struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
1883{
1884 sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
1885 if (IS_ERR(sdev->request_queue))
1886 return NULL;
1887
1888 sdev->request_queue->queuedata = sdev;
1889 __scsi_init_queue(sdev->host, sdev->request_queue);
Googler9398cc32022-12-02 17:21:52 +08001890 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, sdev->request_queue);
Googleraf606d22022-10-26 21:40:12 -07001891 return sdev->request_queue;
1892}
1893
1894int scsi_mq_setup_tags(struct Scsi_Host *shost)
1895{
Googler9398cc32022-12-02 17:21:52 +08001896 unsigned int cmd_size, sgl_size;
Googleraf606d22022-10-26 21:40:12 -07001897
Googler9398cc32022-12-02 17:21:52 +08001898 sgl_size = max_t(unsigned int, sizeof(struct scatterlist),
1899 scsi_mq_inline_sgl_size(shost));
Googleraf606d22022-10-26 21:40:12 -07001900 cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
1901 if (scsi_host_get_prot(shost))
Googler9398cc32022-12-02 17:21:52 +08001902 cmd_size += sizeof(struct scsi_data_buffer) +
1903 sizeof(struct scatterlist) * SCSI_INLINE_PROT_SG_CNT;
Googleraf606d22022-10-26 21:40:12 -07001904
1905 memset(&shost->tag_set, 0, sizeof(shost->tag_set));
Googler9398cc32022-12-02 17:21:52 +08001906 if (shost->hostt->commit_rqs)
1907 shost->tag_set.ops = &scsi_mq_ops;
1908 else
1909 shost->tag_set.ops = &scsi_mq_ops_no_commit;
Googleraf606d22022-10-26 21:40:12 -07001910 shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
1911 shost->tag_set.queue_depth = shost->can_queue;
1912 shost->tag_set.cmd_size = cmd_size;
1913 shost->tag_set.numa_node = NUMA_NO_NODE;
Googler9398cc32022-12-02 17:21:52 +08001914 shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
Googleraf606d22022-10-26 21:40:12 -07001915 shost->tag_set.flags |=
1916 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
1917 shost->tag_set.driver_data = shost;
1918
1919 return blk_mq_alloc_tag_set(&shost->tag_set);
1920}
1921
1922void scsi_mq_destroy_tags(struct Scsi_Host *shost)
1923{
1924 blk_mq_free_tag_set(&shost->tag_set);
1925}
1926
1927/**
1928 * scsi_device_from_queue - return sdev associated with a request_queue
1929 * @q: The request queue to return the sdev from
1930 *
1931 * Return the sdev associated with a request queue or NULL if the
1932 * request_queue does not reference a SCSI device.
1933 */
1934struct scsi_device *scsi_device_from_queue(struct request_queue *q)
1935{
1936 struct scsi_device *sdev = NULL;
1937
Googler9398cc32022-12-02 17:21:52 +08001938 if (q->mq_ops == &scsi_mq_ops_no_commit ||
1939 q->mq_ops == &scsi_mq_ops)
Googleraf606d22022-10-26 21:40:12 -07001940 sdev = q->queuedata;
1941 if (!sdev || !get_device(&sdev->sdev_gendev))
1942 sdev = NULL;
1943
1944 return sdev;
1945}
1946EXPORT_SYMBOL_GPL(scsi_device_from_queue);
1947
1948/*
1949 * Function: scsi_block_requests()
1950 *
1951 * Purpose: Utility function used by low-level drivers to prevent further
1952 * commands from being queued to the device.
1953 *
1954 * Arguments: shost - Host in question
1955 *
1956 * Returns: Nothing
1957 *
1958 * Lock status: No locks are assumed held.
1959 *
1960 * Notes: There is no timer nor any other means by which the requests
1961 * get unblocked other than the low-level driver calling
1962 * scsi_unblock_requests().
1963 */
1964void scsi_block_requests(struct Scsi_Host *shost)
1965{
1966 shost->host_self_blocked = 1;
1967}
1968EXPORT_SYMBOL(scsi_block_requests);
1969
1970/*
1971 * Function: scsi_unblock_requests()
1972 *
1973 * Purpose: Utility function used by low-level drivers to allow further
1974 * commands from being queued to the device.
1975 *
1976 * Arguments: shost - Host in question
1977 *
1978 * Returns: Nothing
1979 *
1980 * Lock status: No locks are assumed held.
1981 *
1982 * Notes: There is no timer nor any other means by which the requests
1983 * get unblocked other than the low-level driver calling
1984 * scsi_unblock_requests().
1985 *
1986 * This is done as an API function so that changes to the
1987 * internals of the scsi mid-layer won't require wholesale
1988 * changes to drivers that use this feature.
1989 */
1990void scsi_unblock_requests(struct Scsi_Host *shost)
1991{
1992 shost->host_self_blocked = 0;
1993 scsi_run_host_queues(shost);
1994}
1995EXPORT_SYMBOL(scsi_unblock_requests);
1996
1997int __init scsi_init_queue(void)
1998{
1999 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
2000 sizeof(struct scsi_data_buffer),
2001 0, 0, NULL);
2002 if (!scsi_sdb_cache) {
2003 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
2004 return -ENOMEM;
2005 }
2006
2007 return 0;
2008}
2009
2010void scsi_exit_queue(void)
2011{
Googler9398cc32022-12-02 17:21:52 +08002012 kmem_cache_destroy(scsi_sense_cache);
2013 kmem_cache_destroy(scsi_sense_isadma_cache);
Googleraf606d22022-10-26 21:40:12 -07002014 kmem_cache_destroy(scsi_sdb_cache);
2015}
2016
2017/**
2018 * scsi_mode_select - issue a mode select
2019 * @sdev: SCSI device to be queried
2020 * @pf: Page format bit (1 == standard, 0 == vendor specific)
2021 * @sp: Save page bit (0 == don't save, 1 == save)
2022 * @modepage: mode page being requested
2023 * @buffer: request buffer (may not be smaller than eight bytes)
2024 * @len: length of request buffer.
2025 * @timeout: command timeout
2026 * @retries: number of retries before failing
2027 * @data: returns a structure abstracting the mode header data
2028 * @sshdr: place to put sense data (or NULL if no sense to be collected).
2029 * must be SCSI_SENSE_BUFFERSIZE big.
2030 *
2031 * Returns zero if successful; negative error number or scsi
2032 * status on error
2033 *
2034 */
2035int
2036scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
2037 unsigned char *buffer, int len, int timeout, int retries,
2038 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2039{
2040 unsigned char cmd[10];
2041 unsigned char *real_buffer;
2042 int ret;
2043
2044 memset(cmd, 0, sizeof(cmd));
2045 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2046
2047 if (sdev->use_10_for_ms) {
2048 if (len > 65535)
2049 return -EINVAL;
2050 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2051 if (!real_buffer)
2052 return -ENOMEM;
2053 memcpy(real_buffer + 8, buffer, len);
2054 len += 8;
2055 real_buffer[0] = 0;
2056 real_buffer[1] = 0;
2057 real_buffer[2] = data->medium_type;
2058 real_buffer[3] = data->device_specific;
2059 real_buffer[4] = data->longlba ? 0x01 : 0;
2060 real_buffer[5] = 0;
2061 real_buffer[6] = data->block_descriptor_length >> 8;
2062 real_buffer[7] = data->block_descriptor_length;
2063
2064 cmd[0] = MODE_SELECT_10;
2065 cmd[7] = len >> 8;
2066 cmd[8] = len;
2067 } else {
2068 if (len > 255 || data->block_descriptor_length > 255 ||
2069 data->longlba)
2070 return -EINVAL;
2071
2072 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2073 if (!real_buffer)
2074 return -ENOMEM;
2075 memcpy(real_buffer + 4, buffer, len);
2076 len += 4;
2077 real_buffer[0] = 0;
2078 real_buffer[1] = data->medium_type;
2079 real_buffer[2] = data->device_specific;
2080 real_buffer[3] = data->block_descriptor_length;
2081
2082
2083 cmd[0] = MODE_SELECT;
2084 cmd[4] = len;
2085 }
2086
2087 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
2088 sshdr, timeout, retries, NULL);
2089 kfree(real_buffer);
2090 return ret;
2091}
2092EXPORT_SYMBOL_GPL(scsi_mode_select);
2093
2094/**
2095 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
2096 * @sdev: SCSI device to be queried
2097 * @dbd: set if mode sense will allow block descriptors to be returned
2098 * @modepage: mode page being requested
2099 * @buffer: request buffer (may not be smaller than eight bytes)
2100 * @len: length of request buffer.
2101 * @timeout: command timeout
2102 * @retries: number of retries before failing
2103 * @data: returns a structure abstracting the mode header data
2104 * @sshdr: place to put sense data (or NULL if no sense to be collected).
2105 * must be SCSI_SENSE_BUFFERSIZE big.
2106 *
2107 * Returns zero if unsuccessful, or the header offset (either 4
2108 * or 8 depending on whether a six or ten byte command was
2109 * issued) if successful.
2110 */
2111int
2112scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
2113 unsigned char *buffer, int len, int timeout, int retries,
2114 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2115{
2116 unsigned char cmd[12];
2117 int use_10_for_ms;
2118 int header_length;
2119 int result, retry_count = retries;
2120 struct scsi_sense_hdr my_sshdr;
2121
2122 memset(data, 0, sizeof(*data));
2123 memset(&cmd[0], 0, 12);
Googleraf606d22022-10-26 21:40:12 -07002124 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
2125 cmd[2] = modepage;
2126
2127 /* caller might not be interested in sense, but we need it */
2128 if (!sshdr)
2129 sshdr = &my_sshdr;
2130
2131 retry:
2132 use_10_for_ms = sdev->use_10_for_ms;
2133
2134 if (use_10_for_ms) {
2135 if (len < 8)
2136 len = 8;
2137
2138 cmd[0] = MODE_SENSE_10;
2139 cmd[8] = len;
2140 header_length = 8;
2141 } else {
2142 if (len < 4)
2143 len = 4;
2144
2145 cmd[0] = MODE_SENSE;
2146 cmd[4] = len;
2147 header_length = 4;
2148 }
2149
2150 memset(buffer, 0, len);
2151
2152 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
2153 sshdr, timeout, retries, NULL);
2154
2155 /* This code looks awful: what it's doing is making sure an
2156 * ILLEGAL REQUEST sense return identifies the actual command
2157 * byte as the problem. MODE_SENSE commands can return
2158 * ILLEGAL REQUEST if the code page isn't supported */
2159
2160 if (use_10_for_ms && !scsi_status_is_good(result) &&
Googler9398cc32022-12-02 17:21:52 +08002161 driver_byte(result) == DRIVER_SENSE) {
Googleraf606d22022-10-26 21:40:12 -07002162 if (scsi_sense_valid(sshdr)) {
2163 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2164 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
2165 /*
2166 * Invalid command operation code
2167 */
2168 sdev->use_10_for_ms = 0;
2169 goto retry;
2170 }
2171 }
2172 }
2173
2174 if(scsi_status_is_good(result)) {
2175 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2176 (modepage == 6 || modepage == 8))) {
2177 /* Initio breakage? */
2178 header_length = 0;
2179 data->length = 13;
2180 data->medium_type = 0;
2181 data->device_specific = 0;
2182 data->longlba = 0;
2183 data->block_descriptor_length = 0;
2184 } else if(use_10_for_ms) {
2185 data->length = buffer[0]*256 + buffer[1] + 2;
2186 data->medium_type = buffer[2];
2187 data->device_specific = buffer[3];
2188 data->longlba = buffer[4] & 0x01;
2189 data->block_descriptor_length = buffer[6]*256
2190 + buffer[7];
2191 } else {
2192 data->length = buffer[0] + 1;
2193 data->medium_type = buffer[1];
2194 data->device_specific = buffer[2];
2195 data->block_descriptor_length = buffer[3];
2196 }
2197 data->header_length = header_length;
2198 } else if ((status_byte(result) == CHECK_CONDITION) &&
2199 scsi_sense_valid(sshdr) &&
2200 sshdr->sense_key == UNIT_ATTENTION && retry_count) {
2201 retry_count--;
2202 goto retry;
2203 }
2204
2205 return result;
2206}
2207EXPORT_SYMBOL(scsi_mode_sense);
2208
2209/**
2210 * scsi_test_unit_ready - test if unit is ready
2211 * @sdev: scsi device to change the state of.
2212 * @timeout: command timeout
2213 * @retries: number of retries before failing
Googler9398cc32022-12-02 17:21:52 +08002214 * @sshdr: outpout pointer for decoded sense information.
Googleraf606d22022-10-26 21:40:12 -07002215 *
2216 * Returns zero if unsuccessful or an error if TUR failed. For
2217 * removable media, UNIT_ATTENTION sets ->changed flag.
2218 **/
2219int
2220scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
Googler9398cc32022-12-02 17:21:52 +08002221 struct scsi_sense_hdr *sshdr)
Googleraf606d22022-10-26 21:40:12 -07002222{
2223 char cmd[] = {
2224 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2225 };
Googleraf606d22022-10-26 21:40:12 -07002226 int result;
2227
Googleraf606d22022-10-26 21:40:12 -07002228 /* try to eat the UNIT_ATTENTION if there are enough retries */
2229 do {
2230 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
Googler9398cc32022-12-02 17:21:52 +08002231 timeout, 1, NULL);
Googleraf606d22022-10-26 21:40:12 -07002232 if (sdev->removable && scsi_sense_valid(sshdr) &&
2233 sshdr->sense_key == UNIT_ATTENTION)
2234 sdev->changed = 1;
2235 } while (scsi_sense_valid(sshdr) &&
2236 sshdr->sense_key == UNIT_ATTENTION && --retries);
2237
Googleraf606d22022-10-26 21:40:12 -07002238 return result;
2239}
2240EXPORT_SYMBOL(scsi_test_unit_ready);
2241
2242/**
2243 * scsi_device_set_state - Take the given device through the device state model.
2244 * @sdev: scsi device to change the state of.
2245 * @state: state to change to.
2246 *
Googler9398cc32022-12-02 17:21:52 +08002247 * Returns zero if successful or an error if the requested
Googleraf606d22022-10-26 21:40:12 -07002248 * transition is illegal.
2249 */
2250int
2251scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2252{
2253 enum scsi_device_state oldstate = sdev->sdev_state;
2254
2255 if (state == oldstate)
2256 return 0;
2257
2258 switch (state) {
2259 case SDEV_CREATED:
2260 switch (oldstate) {
2261 case SDEV_CREATED_BLOCK:
2262 break;
2263 default:
2264 goto illegal;
2265 }
2266 break;
2267
2268 case SDEV_RUNNING:
2269 switch (oldstate) {
2270 case SDEV_CREATED:
2271 case SDEV_OFFLINE:
2272 case SDEV_TRANSPORT_OFFLINE:
2273 case SDEV_QUIESCE:
2274 case SDEV_BLOCK:
2275 break;
2276 default:
2277 goto illegal;
2278 }
2279 break;
2280
2281 case SDEV_QUIESCE:
2282 switch (oldstate) {
2283 case SDEV_RUNNING:
2284 case SDEV_OFFLINE:
2285 case SDEV_TRANSPORT_OFFLINE:
2286 break;
2287 default:
2288 goto illegal;
2289 }
2290 break;
2291
2292 case SDEV_OFFLINE:
2293 case SDEV_TRANSPORT_OFFLINE:
2294 switch (oldstate) {
2295 case SDEV_CREATED:
2296 case SDEV_RUNNING:
2297 case SDEV_QUIESCE:
2298 case SDEV_BLOCK:
2299 break;
2300 default:
2301 goto illegal;
2302 }
2303 break;
2304
2305 case SDEV_BLOCK:
2306 switch (oldstate) {
2307 case SDEV_RUNNING:
2308 case SDEV_CREATED_BLOCK:
Googler9398cc32022-12-02 17:21:52 +08002309 case SDEV_OFFLINE:
Googleraf606d22022-10-26 21:40:12 -07002310 break;
2311 default:
2312 goto illegal;
2313 }
2314 break;
2315
2316 case SDEV_CREATED_BLOCK:
2317 switch (oldstate) {
2318 case SDEV_CREATED:
2319 break;
2320 default:
2321 goto illegal;
2322 }
2323 break;
2324
2325 case SDEV_CANCEL:
2326 switch (oldstate) {
2327 case SDEV_CREATED:
2328 case SDEV_RUNNING:
2329 case SDEV_QUIESCE:
2330 case SDEV_OFFLINE:
2331 case SDEV_TRANSPORT_OFFLINE:
Googleraf606d22022-10-26 21:40:12 -07002332 break;
2333 default:
2334 goto illegal;
2335 }
2336 break;
2337
2338 case SDEV_DEL:
2339 switch (oldstate) {
2340 case SDEV_CREATED:
2341 case SDEV_RUNNING:
2342 case SDEV_OFFLINE:
2343 case SDEV_TRANSPORT_OFFLINE:
2344 case SDEV_CANCEL:
Googler9398cc32022-12-02 17:21:52 +08002345 case SDEV_BLOCK:
Googleraf606d22022-10-26 21:40:12 -07002346 case SDEV_CREATED_BLOCK:
2347 break;
2348 default:
2349 goto illegal;
2350 }
2351 break;
2352
2353 }
2354 sdev->sdev_state = state;
2355 return 0;
2356
2357 illegal:
2358 SCSI_LOG_ERROR_RECOVERY(1,
2359 sdev_printk(KERN_ERR, sdev,
2360 "Illegal state transition %s->%s",
2361 scsi_device_state_name(oldstate),
2362 scsi_device_state_name(state))
2363 );
2364 return -EINVAL;
2365}
2366EXPORT_SYMBOL(scsi_device_set_state);
2367
2368/**
2369 * sdev_evt_emit - emit a single SCSI device uevent
2370 * @sdev: associated SCSI device
2371 * @evt: event to emit
2372 *
2373 * Send a single uevent (scsi_event) to the associated scsi_device.
2374 */
2375static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2376{
2377 int idx = 0;
2378 char *envp[3];
2379
2380 switch (evt->evt_type) {
2381 case SDEV_EVT_MEDIA_CHANGE:
2382 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2383 break;
2384 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2385 scsi_rescan_device(&sdev->sdev_gendev);
2386 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2387 break;
2388 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2389 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2390 break;
2391 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2392 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2393 break;
2394 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2395 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2396 break;
2397 case SDEV_EVT_LUN_CHANGE_REPORTED:
2398 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2399 break;
2400 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2401 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
2402 break;
Googler9398cc32022-12-02 17:21:52 +08002403 case SDEV_EVT_POWER_ON_RESET_OCCURRED:
2404 envp[idx++] = "SDEV_UA=POWER_ON_RESET_OCCURRED";
2405 break;
Googleraf606d22022-10-26 21:40:12 -07002406 default:
2407 /* do nothing */
2408 break;
2409 }
2410
2411 envp[idx++] = NULL;
2412
2413 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2414}
2415
2416/**
2417 * sdev_evt_thread - send a uevent for each scsi event
2418 * @work: work struct for scsi_device
2419 *
2420 * Dispatch queued events to their associated scsi_device kobjects
2421 * as uevents.
2422 */
2423void scsi_evt_thread(struct work_struct *work)
2424{
2425 struct scsi_device *sdev;
2426 enum scsi_device_event evt_type;
2427 LIST_HEAD(event_list);
2428
2429 sdev = container_of(work, struct scsi_device, event_work);
2430
2431 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2432 if (test_and_clear_bit(evt_type, sdev->pending_events))
2433 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2434
2435 while (1) {
2436 struct scsi_event *evt;
2437 struct list_head *this, *tmp;
2438 unsigned long flags;
2439
2440 spin_lock_irqsave(&sdev->list_lock, flags);
2441 list_splice_init(&sdev->event_list, &event_list);
2442 spin_unlock_irqrestore(&sdev->list_lock, flags);
2443
2444 if (list_empty(&event_list))
2445 break;
2446
2447 list_for_each_safe(this, tmp, &event_list) {
2448 evt = list_entry(this, struct scsi_event, node);
2449 list_del(&evt->node);
2450 scsi_evt_emit(sdev, evt);
2451 kfree(evt);
2452 }
2453 }
2454}
2455
2456/**
2457 * sdev_evt_send - send asserted event to uevent thread
2458 * @sdev: scsi_device event occurred on
2459 * @evt: event to send
2460 *
2461 * Assert scsi device event asynchronously.
2462 */
2463void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2464{
2465 unsigned long flags;
2466
2467#if 0
2468 /* FIXME: currently this check eliminates all media change events
2469 * for polled devices. Need to update to discriminate between AN
2470 * and polled events */
2471 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2472 kfree(evt);
2473 return;
2474 }
2475#endif
2476
2477 spin_lock_irqsave(&sdev->list_lock, flags);
2478 list_add_tail(&evt->node, &sdev->event_list);
2479 schedule_work(&sdev->event_work);
2480 spin_unlock_irqrestore(&sdev->list_lock, flags);
2481}
2482EXPORT_SYMBOL_GPL(sdev_evt_send);
2483
2484/**
2485 * sdev_evt_alloc - allocate a new scsi event
2486 * @evt_type: type of event to allocate
2487 * @gfpflags: GFP flags for allocation
2488 *
2489 * Allocates and returns a new scsi_event.
2490 */
2491struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2492 gfp_t gfpflags)
2493{
2494 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2495 if (!evt)
2496 return NULL;
2497
2498 evt->evt_type = evt_type;
2499 INIT_LIST_HEAD(&evt->node);
2500
2501 /* evt_type-specific initialization, if any */
2502 switch (evt_type) {
2503 case SDEV_EVT_MEDIA_CHANGE:
2504 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2505 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2506 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2507 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2508 case SDEV_EVT_LUN_CHANGE_REPORTED:
2509 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
Googler9398cc32022-12-02 17:21:52 +08002510 case SDEV_EVT_POWER_ON_RESET_OCCURRED:
Googleraf606d22022-10-26 21:40:12 -07002511 default:
2512 /* do nothing */
2513 break;
2514 }
2515
2516 return evt;
2517}
2518EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2519
2520/**
2521 * sdev_evt_send_simple - send asserted event to uevent thread
2522 * @sdev: scsi_device event occurred on
2523 * @evt_type: type of event to send
2524 * @gfpflags: GFP flags for allocation
2525 *
2526 * Assert scsi device event asynchronously, given an event type.
2527 */
2528void sdev_evt_send_simple(struct scsi_device *sdev,
2529 enum scsi_device_event evt_type, gfp_t gfpflags)
2530{
2531 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2532 if (!evt) {
2533 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2534 evt_type);
2535 return;
2536 }
2537
2538 sdev_evt_send(sdev, evt);
2539}
2540EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2541
2542/**
2543 * scsi_device_quiesce - Block user issued commands.
2544 * @sdev: scsi device to quiesce.
2545 *
2546 * This works by trying to transition to the SDEV_QUIESCE state
2547 * (which must be a legal transition). When the device is in this
2548 * state, only special requests will be accepted, all others will
2549 * be deferred. Since special requests may also be requeued requests,
2550 * a successful return doesn't guarantee the device will be
2551 * totally quiescent.
2552 *
2553 * Must be called with user context, may sleep.
2554 *
2555 * Returns zero if unsuccessful or an error if not.
2556 */
2557int
2558scsi_device_quiesce(struct scsi_device *sdev)
2559{
Googler9398cc32022-12-02 17:21:52 +08002560 struct request_queue *q = sdev->request_queue;
2561 int err;
Googleraf606d22022-10-26 21:40:12 -07002562
Googler9398cc32022-12-02 17:21:52 +08002563 /*
2564 * It is allowed to call scsi_device_quiesce() multiple times from
2565 * the same context but concurrent scsi_device_quiesce() calls are
2566 * not allowed.
2567 */
2568 WARN_ON_ONCE(sdev->quiesced_by && sdev->quiesced_by != current);
2569
2570 if (sdev->quiesced_by == current)
2571 return 0;
2572
2573 blk_set_pm_only(q);
2574
2575 blk_mq_freeze_queue(q);
2576 /*
2577 * Ensure that the effect of blk_set_pm_only() will be visible
2578 * for percpu_ref_tryget() callers that occur after the queue
2579 * unfreeze even if the queue was already frozen before this function
2580 * was called. See also https://lwn.net/Articles/573497/.
2581 */
2582 synchronize_rcu();
2583 blk_mq_unfreeze_queue(q);
2584
2585 mutex_lock(&sdev->state_mutex);
2586 err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2587 if (err == 0)
2588 sdev->quiesced_by = current;
2589 else
2590 blk_clear_pm_only(q);
2591 mutex_unlock(&sdev->state_mutex);
2592
2593 return err;
Googleraf606d22022-10-26 21:40:12 -07002594}
2595EXPORT_SYMBOL(scsi_device_quiesce);
2596
2597/**
2598 * scsi_device_resume - Restart user issued commands to a quiesced device.
2599 * @sdev: scsi device to resume.
2600 *
2601 * Moves the device from quiesced back to running and restarts the
2602 * queues.
2603 *
2604 * Must be called with user context, may sleep.
2605 */
2606void scsi_device_resume(struct scsi_device *sdev)
2607{
2608 /* check if the device state was mutated prior to resume, and if
2609 * so assume the state is being managed elsewhere (for example
2610 * device deleted during suspend)
2611 */
Googler9398cc32022-12-02 17:21:52 +08002612 mutex_lock(&sdev->state_mutex);
2613 if (sdev->quiesced_by) {
2614 sdev->quiesced_by = NULL;
2615 blk_clear_pm_only(sdev->request_queue);
2616 }
2617 if (sdev->sdev_state == SDEV_QUIESCE)
2618 scsi_device_set_state(sdev, SDEV_RUNNING);
2619 mutex_unlock(&sdev->state_mutex);
Googleraf606d22022-10-26 21:40:12 -07002620}
2621EXPORT_SYMBOL(scsi_device_resume);
2622
2623static void
2624device_quiesce_fn(struct scsi_device *sdev, void *data)
2625{
2626 scsi_device_quiesce(sdev);
2627}
2628
2629void
2630scsi_target_quiesce(struct scsi_target *starget)
2631{
2632 starget_for_each_device(starget, NULL, device_quiesce_fn);
2633}
2634EXPORT_SYMBOL(scsi_target_quiesce);
2635
2636static void
2637device_resume_fn(struct scsi_device *sdev, void *data)
2638{
2639 scsi_device_resume(sdev);
2640}
2641
2642void
2643scsi_target_resume(struct scsi_target *starget)
2644{
2645 starget_for_each_device(starget, NULL, device_resume_fn);
2646}
2647EXPORT_SYMBOL(scsi_target_resume);
2648
2649/**
Googler9398cc32022-12-02 17:21:52 +08002650 * scsi_internal_device_block_nowait - try to transition to the SDEV_BLOCK state
2651 * @sdev: device to block
Googleraf606d22022-10-26 21:40:12 -07002652 *
Googler9398cc32022-12-02 17:21:52 +08002653 * Pause SCSI command processing on the specified device. Does not sleep.
Googleraf606d22022-10-26 21:40:12 -07002654 *
Googler9398cc32022-12-02 17:21:52 +08002655 * Returns zero if successful or a negative error code upon failure.
Googleraf606d22022-10-26 21:40:12 -07002656 *
Googler9398cc32022-12-02 17:21:52 +08002657 * Notes:
2658 * This routine transitions the device to the SDEV_BLOCK state (which must be
2659 * a legal transition). When the device is in this state, command processing
2660 * is paused until the device leaves the SDEV_BLOCK state. See also
2661 * scsi_internal_device_unblock_nowait().
Googleraf606d22022-10-26 21:40:12 -07002662 */
Googler9398cc32022-12-02 17:21:52 +08002663int scsi_internal_device_block_nowait(struct scsi_device *sdev)
Googleraf606d22022-10-26 21:40:12 -07002664{
2665 struct request_queue *q = sdev->request_queue;
Googleraf606d22022-10-26 21:40:12 -07002666 int err = 0;
2667
2668 err = scsi_device_set_state(sdev, SDEV_BLOCK);
2669 if (err) {
2670 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2671
2672 if (err)
2673 return err;
2674 }
2675
2676 /*
2677 * The device has transitioned to SDEV_BLOCK. Stop the
2678 * block layer from calling the midlayer with this device's
2679 * request queue.
2680 */
Googler9398cc32022-12-02 17:21:52 +08002681 blk_mq_quiesce_queue_nowait(q);
Googleraf606d22022-10-26 21:40:12 -07002682 return 0;
2683}
Googler9398cc32022-12-02 17:21:52 +08002684EXPORT_SYMBOL_GPL(scsi_internal_device_block_nowait);
2685
Googler9726be62022-12-14 05:53:31 +00002686/**
Googler9398cc32022-12-02 17:21:52 +08002687 * scsi_internal_device_block - try to transition to the SDEV_BLOCK state
2688 * @sdev: device to block
Googler9726be62022-12-14 05:53:31 +00002689 *
Googler9398cc32022-12-02 17:21:52 +08002690 * Pause SCSI command processing on the specified device and wait until all
2691 * ongoing scsi_request_fn() / scsi_queue_rq() calls have finished. May sleep.
Googler9726be62022-12-14 05:53:31 +00002692 *
Googler9398cc32022-12-02 17:21:52 +08002693 * Returns zero if successful or a negative error code upon failure.
Googler9726be62022-12-14 05:53:31 +00002694 *
Googler9398cc32022-12-02 17:21:52 +08002695 * Note:
2696 * This routine transitions the device to the SDEV_BLOCK state (which must be
2697 * a legal transition). When the device is in this state, command processing
2698 * is paused until the device leaves the SDEV_BLOCK state. See also
2699 * scsi_internal_device_unblock().
Googler9726be62022-12-14 05:53:31 +00002700 */
Googler9398cc32022-12-02 17:21:52 +08002701static int scsi_internal_device_block(struct scsi_device *sdev)
Googler0109c452022-10-13 17:50:39 +08002702{
Googler9398cc32022-12-02 17:21:52 +08002703 struct request_queue *q = sdev->request_queue;
2704 int err;
2705
2706 mutex_lock(&sdev->state_mutex);
2707 err = scsi_internal_device_block_nowait(sdev);
2708 if (err == 0)
2709 blk_mq_quiesce_queue(q);
2710 mutex_unlock(&sdev->state_mutex);
2711
2712 return err;
2713}
2714
2715void scsi_start_queue(struct scsi_device *sdev)
2716{
2717 struct request_queue *q = sdev->request_queue;
2718
2719 blk_mq_unquiesce_queue(q);
2720}
2721
2722/**
2723 * scsi_internal_device_unblock_nowait - resume a device after a block request
2724 * @sdev: device to resume
2725 * @new_state: state to set the device to after unblocking
2726 *
2727 * Restart the device queue for a previously suspended SCSI device. Does not
2728 * sleep.
2729 *
2730 * Returns zero if successful or a negative error code upon failure.
2731 *
2732 * Notes:
2733 * This routine transitions the device to the SDEV_RUNNING state or to one of
2734 * the offline states (which must be a legal transition) allowing the midlayer
2735 * to goose the queue for this device.
2736 */
2737int scsi_internal_device_unblock_nowait(struct scsi_device *sdev,
2738 enum scsi_device_state new_state)
2739{
2740 switch (new_state) {
2741 case SDEV_RUNNING:
2742 case SDEV_TRANSPORT_OFFLINE:
2743 break;
2744 default:
2745 return -EINVAL;
2746 }
Googleraf606d22022-10-26 21:40:12 -07002747
Googler0109c452022-10-13 17:50:39 +08002748 /*
2749 * Try to transition the scsi device to SDEV_RUNNING or one of the
2750 * offlined states and goose the device queue if successful.
2751 */
Googler9398cc32022-12-02 17:21:52 +08002752 switch (sdev->sdev_state) {
2753 case SDEV_BLOCK:
2754 case SDEV_TRANSPORT_OFFLINE:
Googler0109c452022-10-13 17:50:39 +08002755 sdev->sdev_state = new_state;
Googler9398cc32022-12-02 17:21:52 +08002756 break;
2757 case SDEV_CREATED_BLOCK:
Googler0109c452022-10-13 17:50:39 +08002758 if (new_state == SDEV_TRANSPORT_OFFLINE ||
2759 new_state == SDEV_OFFLINE)
2760 sdev->sdev_state = new_state;
2761 else
2762 sdev->sdev_state = SDEV_CREATED;
Googler9398cc32022-12-02 17:21:52 +08002763 break;
2764 case SDEV_CANCEL:
2765 case SDEV_OFFLINE:
2766 break;
2767 default:
Googler0109c452022-10-13 17:50:39 +08002768 return -EINVAL;
Googler0109c452022-10-13 17:50:39 +08002769 }
Googler9398cc32022-12-02 17:21:52 +08002770 scsi_start_queue(sdev);
Googleraf606d22022-10-26 21:40:12 -07002771
2772 return 0;
2773}
Googler9398cc32022-12-02 17:21:52 +08002774EXPORT_SYMBOL_GPL(scsi_internal_device_unblock_nowait);
2775
2776/**
2777 * scsi_internal_device_unblock - resume a device after a block request
2778 * @sdev: device to resume
2779 * @new_state: state to set the device to after unblocking
2780 *
2781 * Restart the device queue for a previously suspended SCSI device. May sleep.
2782 *
2783 * Returns zero if successful or a negative error code upon failure.
2784 *
2785 * Notes:
2786 * This routine transitions the device to the SDEV_RUNNING state or to one of
2787 * the offline states (which must be a legal transition) allowing the midlayer
2788 * to goose the queue for this device.
2789 */
2790static int scsi_internal_device_unblock(struct scsi_device *sdev,
2791 enum scsi_device_state new_state)
2792{
2793 int ret;
2794
2795 mutex_lock(&sdev->state_mutex);
2796 ret = scsi_internal_device_unblock_nowait(sdev, new_state);
2797 mutex_unlock(&sdev->state_mutex);
2798
2799 return ret;
2800}
Googleraf606d22022-10-26 21:40:12 -07002801
2802static void
2803device_block(struct scsi_device *sdev, void *data)
2804{
Googler9398cc32022-12-02 17:21:52 +08002805 int ret;
2806
2807 ret = scsi_internal_device_block(sdev);
2808
2809 WARN_ONCE(ret, "scsi_internal_device_block(%s) failed: ret = %d\n",
2810 dev_name(&sdev->sdev_gendev), ret);
Googleraf606d22022-10-26 21:40:12 -07002811}
2812
2813static int
2814target_block(struct device *dev, void *data)
2815{
2816 if (scsi_is_target_device(dev))
2817 starget_for_each_device(to_scsi_target(dev), NULL,
2818 device_block);
2819 return 0;
2820}
2821
2822void
2823scsi_target_block(struct device *dev)
2824{
2825 if (scsi_is_target_device(dev))
2826 starget_for_each_device(to_scsi_target(dev), NULL,
2827 device_block);
2828 else
2829 device_for_each_child(dev, NULL, target_block);
2830}
2831EXPORT_SYMBOL_GPL(scsi_target_block);
2832
2833static void
2834device_unblock(struct scsi_device *sdev, void *data)
2835{
2836 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
2837}
2838
2839static int
2840target_unblock(struct device *dev, void *data)
2841{
2842 if (scsi_is_target_device(dev))
2843 starget_for_each_device(to_scsi_target(dev), data,
2844 device_unblock);
2845 return 0;
2846}
2847
2848void
2849scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
2850{
2851 if (scsi_is_target_device(dev))
2852 starget_for_each_device(to_scsi_target(dev), &new_state,
2853 device_unblock);
2854 else
2855 device_for_each_child(dev, &new_state, target_unblock);
2856}
2857EXPORT_SYMBOL_GPL(scsi_target_unblock);
2858
2859/**
2860 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
2861 * @sgl: scatter-gather list
2862 * @sg_count: number of segments in sg
2863 * @offset: offset in bytes into sg, on return offset into the mapped area
2864 * @len: bytes to map, on return number of bytes mapped
2865 *
2866 * Returns virtual address of the start of the mapped page
2867 */
2868void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
2869 size_t *offset, size_t *len)
2870{
2871 int i;
2872 size_t sg_len = 0, len_complete = 0;
2873 struct scatterlist *sg;
2874 struct page *page;
2875
2876 WARN_ON(!irqs_disabled());
2877
2878 for_each_sg(sgl, sg, sg_count, i) {
2879 len_complete = sg_len; /* Complete sg-entries */
2880 sg_len += sg->length;
2881 if (sg_len > *offset)
2882 break;
2883 }
2884
2885 if (unlikely(i == sg_count)) {
2886 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
2887 "elements %d\n",
2888 __func__, sg_len, *offset, sg_count);
2889 WARN_ON(1);
2890 return NULL;
2891 }
2892
2893 /* Offset starting from the beginning of first page in this sg-entry */
2894 *offset = *offset - len_complete + sg->offset;
2895
2896 /* Assumption: contiguous pages can be accessed as "page + i" */
2897 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
2898 *offset &= ~PAGE_MASK;
2899
2900 /* Bytes in this sg-entry from *offset to the end of the page */
2901 sg_len = PAGE_SIZE - *offset;
2902 if (*len > sg_len)
2903 *len = sg_len;
2904
2905 return kmap_atomic(page);
2906}
2907EXPORT_SYMBOL(scsi_kmap_atomic_sg);
2908
2909/**
2910 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
2911 * @virt: virtual address to be unmapped
2912 */
2913void scsi_kunmap_atomic_sg(void *virt)
2914{
2915 kunmap_atomic(virt);
2916}
2917EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
2918
2919void sdev_disable_disk_events(struct scsi_device *sdev)
2920{
2921 atomic_inc(&sdev->disk_events_disable_depth);
2922}
2923EXPORT_SYMBOL(sdev_disable_disk_events);
2924
2925void sdev_enable_disk_events(struct scsi_device *sdev)
2926{
2927 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
2928 return;
2929 atomic_dec(&sdev->disk_events_disable_depth);
2930}
2931EXPORT_SYMBOL(sdev_enable_disk_events);
2932
Googler9398cc32022-12-02 17:21:52 +08002933static unsigned char designator_prio(const unsigned char *d)
2934{
2935 if (d[1] & 0x30)
2936 /* not associated with LUN */
2937 return 0;
2938
2939 if (d[3] == 0)
2940 /* invalid length */
2941 return 0;
2942
2943 /*
2944 * Order of preference for lun descriptor:
2945 * - SCSI name string
2946 * - NAA IEEE Registered Extended
2947 * - EUI-64 based 16-byte
2948 * - EUI-64 based 12-byte
2949 * - NAA IEEE Registered
2950 * - NAA IEEE Extended
2951 * - EUI-64 based 8-byte
2952 * - SCSI name string (truncated)
2953 * - T10 Vendor ID
2954 * as longer descriptors reduce the likelyhood
2955 * of identification clashes.
2956 */
2957
2958 switch (d[1] & 0xf) {
2959 case 8:
2960 /* SCSI name string, variable-length UTF-8 */
2961 return 9;
2962 case 3:
2963 switch (d[4] >> 4) {
2964 case 6:
2965 /* NAA registered extended */
2966 return 8;
2967 case 5:
2968 /* NAA registered */
2969 return 5;
2970 case 4:
2971 /* NAA extended */
2972 return 4;
2973 case 3:
2974 /* NAA locally assigned */
2975 return 1;
2976 default:
2977 break;
2978 }
2979 break;
2980 case 2:
2981 switch (d[3]) {
2982 case 16:
2983 /* EUI64-based, 16 byte */
2984 return 7;
2985 case 12:
2986 /* EUI64-based, 12 byte */
2987 return 6;
2988 case 8:
2989 /* EUI64-based, 8 byte */
2990 return 3;
2991 default:
2992 break;
2993 }
2994 break;
2995 case 1:
2996 /* T10 vendor ID */
2997 return 1;
2998 default:
2999 break;
3000 }
3001
3002 return 0;
3003}
3004
Googleraf606d22022-10-26 21:40:12 -07003005/**
3006 * scsi_vpd_lun_id - return a unique device identification
3007 * @sdev: SCSI device
3008 * @id: buffer for the identification
3009 * @id_len: length of the buffer
3010 *
3011 * Copies a unique device identification into @id based
3012 * on the information in the VPD page 0x83 of the device.
3013 * The string will be formatted as a SCSI name string.
3014 *
3015 * Returns the length of the identification or error on failure.
3016 * If the identifier is longer than the supplied buffer the actual
3017 * identifier length is returned and the buffer is not zero-padded.
3018 */
3019int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
3020{
Googler9398cc32022-12-02 17:21:52 +08003021 u8 cur_id_prio = 0;
Googleraf606d22022-10-26 21:40:12 -07003022 u8 cur_id_size = 0;
Googler9398cc32022-12-02 17:21:52 +08003023 const unsigned char *d, *cur_id_str;
3024 const struct scsi_vpd *vpd_pg83;
Googleraf606d22022-10-26 21:40:12 -07003025 int id_size = -EINVAL;
3026
3027 rcu_read_lock();
3028 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3029 if (!vpd_pg83) {
3030 rcu_read_unlock();
3031 return -ENXIO;
3032 }
3033
Googleraf606d22022-10-26 21:40:12 -07003034 /* The id string must be at least 20 bytes + terminating NULL byte */
3035 if (id_len < 21) {
3036 rcu_read_unlock();
3037 return -EINVAL;
3038 }
3039
3040 memset(id, 0, id_len);
Googler9398cc32022-12-02 17:21:52 +08003041 d = vpd_pg83->data + 4;
3042 while (d < vpd_pg83->data + vpd_pg83->len) {
3043 u8 prio = designator_prio(d);
3044
3045 if (prio == 0 || cur_id_prio > prio)
Googleraf606d22022-10-26 21:40:12 -07003046 goto next_desig;
3047
3048 switch (d[1] & 0xf) {
3049 case 0x1:
3050 /* T10 Vendor ID */
3051 if (cur_id_size > d[3])
3052 break;
Googler9398cc32022-12-02 17:21:52 +08003053 cur_id_prio = prio;
Googleraf606d22022-10-26 21:40:12 -07003054 cur_id_size = d[3];
3055 if (cur_id_size + 4 > id_len)
3056 cur_id_size = id_len - 4;
3057 cur_id_str = d + 4;
Googleraf606d22022-10-26 21:40:12 -07003058 id_size = snprintf(id, id_len, "t10.%*pE",
3059 cur_id_size, cur_id_str);
3060 break;
3061 case 0x2:
3062 /* EUI-64 */
Googler9398cc32022-12-02 17:21:52 +08003063 cur_id_prio = prio;
Googleraf606d22022-10-26 21:40:12 -07003064 cur_id_size = d[3];
3065 cur_id_str = d + 4;
Googleraf606d22022-10-26 21:40:12 -07003066 switch (cur_id_size) {
3067 case 8:
3068 id_size = snprintf(id, id_len,
3069 "eui.%8phN",
3070 cur_id_str);
3071 break;
3072 case 12:
3073 id_size = snprintf(id, id_len,
3074 "eui.%12phN",
3075 cur_id_str);
3076 break;
3077 case 16:
3078 id_size = snprintf(id, id_len,
3079 "eui.%16phN",
3080 cur_id_str);
3081 break;
3082 default:
Googleraf606d22022-10-26 21:40:12 -07003083 break;
3084 }
3085 break;
3086 case 0x3:
3087 /* NAA */
Googler9398cc32022-12-02 17:21:52 +08003088 cur_id_prio = prio;
Googleraf606d22022-10-26 21:40:12 -07003089 cur_id_size = d[3];
3090 cur_id_str = d + 4;
Googleraf606d22022-10-26 21:40:12 -07003091 switch (cur_id_size) {
3092 case 8:
3093 id_size = snprintf(id, id_len,
3094 "naa.%8phN",
3095 cur_id_str);
3096 break;
3097 case 16:
3098 id_size = snprintf(id, id_len,
3099 "naa.%16phN",
3100 cur_id_str);
3101 break;
3102 default:
Googleraf606d22022-10-26 21:40:12 -07003103 break;
3104 }
3105 break;
3106 case 0x8:
3107 /* SCSI name string */
Googler9398cc32022-12-02 17:21:52 +08003108 if (cur_id_size > d[3])
Googleraf606d22022-10-26 21:40:12 -07003109 break;
3110 /* Prefer others for truncated descriptor */
Googler9398cc32022-12-02 17:21:52 +08003111 if (d[3] > id_len) {
3112 prio = 2;
3113 if (cur_id_prio > prio)
3114 break;
3115 }
3116 cur_id_prio = prio;
Googleraf606d22022-10-26 21:40:12 -07003117 cur_id_size = id_size = d[3];
3118 cur_id_str = d + 4;
Googleraf606d22022-10-26 21:40:12 -07003119 if (cur_id_size >= id_len)
3120 cur_id_size = id_len - 1;
3121 memcpy(id, cur_id_str, cur_id_size);
Googleraf606d22022-10-26 21:40:12 -07003122 break;
3123 default:
3124 break;
3125 }
3126next_desig:
3127 d += d[3] + 4;
3128 }
3129 rcu_read_unlock();
3130
3131 return id_size;
3132}
3133EXPORT_SYMBOL(scsi_vpd_lun_id);
3134
3135/*
3136 * scsi_vpd_tpg_id - return a target port group identifier
3137 * @sdev: SCSI device
3138 *
3139 * Returns the Target Port Group identifier from the information
3140 * froom VPD page 0x83 of the device.
3141 *
3142 * Returns the identifier or error on failure.
3143 */
3144int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
3145{
Googler9398cc32022-12-02 17:21:52 +08003146 const unsigned char *d;
3147 const struct scsi_vpd *vpd_pg83;
Googleraf606d22022-10-26 21:40:12 -07003148 int group_id = -EAGAIN, rel_port = -1;
3149
3150 rcu_read_lock();
3151 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3152 if (!vpd_pg83) {
3153 rcu_read_unlock();
3154 return -ENXIO;
3155 }
3156
Googler9398cc32022-12-02 17:21:52 +08003157 d = vpd_pg83->data + 4;
3158 while (d < vpd_pg83->data + vpd_pg83->len) {
Googleraf606d22022-10-26 21:40:12 -07003159 switch (d[1] & 0xf) {
3160 case 0x4:
3161 /* Relative target port */
3162 rel_port = get_unaligned_be16(&d[6]);
3163 break;
3164 case 0x5:
3165 /* Target port group */
3166 group_id = get_unaligned_be16(&d[6]);
3167 break;
3168 default:
3169 break;
3170 }
3171 d += d[3] + 4;
3172 }
3173 rcu_read_unlock();
3174
3175 if (group_id >= 0 && rel_id && rel_port != -1)
3176 *rel_id = rel_port;
3177
3178 return group_id;
3179}
3180EXPORT_SYMBOL(scsi_vpd_tpg_id);