Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2 | /* |
| 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> |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 34 | #include <scsi/scsi_transport.h> /* __scsi_init_queue() */ |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 35 | #include <scsi/scsi_dh.h> |
| 36 | |
| 37 | #include <trace/events/scsi.h> |
| 38 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 39 | #include "scsi_debugfs.h" |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 40 | #include "scsi_priv.h" |
| 41 | #include "scsi_logging.h" |
| 42 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 43 | /* |
| 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 |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 54 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 55 | static struct kmem_cache *scsi_sdb_cache; |
| 56 | static struct kmem_cache *scsi_sense_cache; |
| 57 | static struct kmem_cache *scsi_sense_isadma_cache; |
| 58 | static DEFINE_MUTEX(scsi_sense_cache_mutex); |
| 59 | |
| 60 | static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd); |
| 61 | |
| 62 | static inline struct kmem_cache * |
| 63 | scsi_select_sense_cache(bool unchecked_isa_dma) |
| 64 | { |
| 65 | return unchecked_isa_dma ? scsi_sense_isadma_cache : scsi_sense_cache; |
| 66 | } |
| 67 | |
| 68 | static 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 | |
| 75 | static 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 | |
| 82 | int 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 | } |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 111 | |
| 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 | |
| 119 | static void |
| 120 | scsi_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 | |
| 155 | static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd) |
| 156 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 157 | 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); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 164 | } |
| 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 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 178 | static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, bool unbusy) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 179 | { |
| 180 | struct scsi_device *device = cmd->device; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 181 | |
| 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; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 201 | |
| 202 | blk_mq_requeue_request(cmd->request, true); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 203 | } |
| 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 | */ |
| 224 | void scsi_queue_insert(struct scsi_cmnd *cmd, int reason) |
| 225 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 226 | __scsi_queue_insert(cmd, reason, true); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 227 | } |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 228 | |
| 229 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 230 | /** |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 231 | * __scsi_execute - insert request and wait for the result |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 232 | * @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 |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 238 | * @sshdr: optional decoded sense header |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 239 | * @timeout: request timeout in seconds |
| 240 | * @retries: number of times to retry request |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 241 | * @flags: flags for ->cmd_flags |
| 242 | * @rq_flags: flags for ->rq_flags |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 243 | * @resid: optional residual length |
| 244 | * |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 245 | * 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. |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 247 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 248 | int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd, |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 249 | int data_direction, void *buffer, unsigned bufflen, |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 250 | unsigned char *sense, struct scsi_sense_hdr *sshdr, |
| 251 | int timeout, int retries, u64 flags, req_flags_t rq_flags, |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 252 | int *resid) |
| 253 | { |
| 254 | struct request *req; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 255 | struct scsi_request *rq; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 256 | int ret = DRIVER_ERROR << 24; |
| 257 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 258 | 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); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 261 | if (IS_ERR(req)) |
| 262 | return ret; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 263 | rq = scsi_req(req); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 264 | |
| 265 | if (bufflen && blk_rq_map_kern(sdev->request_queue, req, |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 266 | buffer, bufflen, GFP_NOIO)) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 267 | goto out; |
| 268 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 269 | rq->cmd_len = COMMAND_SIZE(cmd[0]); |
| 270 | memcpy(rq->cmd, cmd, rq->cmd_len); |
| 271 | rq->retries = retries; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 272 | req->timeout = timeout; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 273 | req->cmd_flags |= flags; |
| 274 | req->rq_flags |= rq_flags | RQF_QUIET; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 275 | |
| 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 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 287 | if (unlikely(rq->resid_len > 0 && rq->resid_len <= bufflen)) |
| 288 | memset(buffer + (bufflen - rq->resid_len), 0, rq->resid_len); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 289 | |
| 290 | if (resid) |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 291 | *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; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 297 | out: |
| 298 | blk_put_request(req); |
| 299 | |
| 300 | return ret; |
| 301 | } |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 302 | EXPORT_SYMBOL(__scsi_execute); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 303 | |
| 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 | */ |
| 315 | static void scsi_init_cmd_errh(struct scsi_cmnd *cmd) |
| 316 | { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 317 | 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 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 323 | /* |
| 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 | */ |
| 332 | static 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 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 347 | void scsi_device_unbusy(struct scsi_device *sdev) |
| 348 | { |
| 349 | struct Scsi_Host *shost = sdev->host; |
| 350 | struct scsi_target *starget = scsi_target(sdev); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 351 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 352 | scsi_dec_host_busy(shost); |
| 353 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 354 | if (starget->can_queue > 0) |
| 355 | atomic_dec(&starget->target_busy); |
| 356 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 357 | atomic_dec(&sdev->device_busy); |
| 358 | } |
| 359 | |
| 360 | static void scsi_kick_queue(struct request_queue *q) |
| 361 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 362 | blk_mq_run_hw_queues(q, false); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 363 | } |
| 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 | */ |
| 372 | static 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 | |
| 411 | static 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 | |
| 420 | static 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 | |
| 431 | static 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 | |
| 443 | static 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 | */ |
| 514 | static 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 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 523 | blk_mq_run_hw_queues(q, false); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | void 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 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 536 | void 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 | |
| 544 | static void scsi_uninit_cmd(struct scsi_cmnd *cmd) |
| 545 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 546 | if (!blk_rq_is_passthrough(cmd->request)) { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 547 | struct scsi_driver *drv = scsi_cmd_to_driver(cmd); |
| 548 | |
| 549 | if (drv->uninit_command) |
| 550 | drv->uninit_command(cmd); |
| 551 | } |
| 552 | } |
| 553 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 554 | static void scsi_free_sgtables(struct scsi_cmnd *cmd) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 555 | { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 556 | if (cmd->sdb.table.nents) |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 557 | sg_free_table_chained(&cmd->sdb.table, |
| 558 | SCSI_INLINE_SG_CNT); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 559 | if (scsi_prot_sg_count(cmd)) |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 560 | sg_free_table_chained(&cmd->prot_sdb->table, |
| 561 | SCSI_INLINE_PROT_SG_CNT); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd) |
| 565 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 566 | scsi_free_sgtables(cmd); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 567 | scsi_uninit_cmd(cmd); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 568 | scsi_del_cmd_from_list(cmd); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 571 | static void scsi_run_queue_async(struct scsi_device *sdev) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 572 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 573 | 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); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 580 | /* Returns false when no more bytes to process, true if there are more */ |
| 581 | static bool scsi_end_request(struct request *req, blk_status_t error, |
| 582 | unsigned int bytes) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 583 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 584 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 585 | 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 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 591 | if (blk_queue_add_random(q)) |
| 592 | add_disk_randomness(req->rq_disk); |
| 593 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 594 | if (!blk_rq_is_scsi(req)) { |
| 595 | WARN_ON_ONCE(!(cmd->flags & SCMD_INITIALIZED)); |
| 596 | cmd->flags &= ~SCMD_INITIALIZED; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 599 | /* |
| 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); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 627 | return false; |
| 628 | } |
| 629 | |
| 630 | /** |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 631 | * scsi_result_to_blk_status - translate a SCSI result code into blk_status_t |
| 632 | * @cmd: SCSI command |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 633 | * @result: scsi error code |
| 634 | * |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 635 | * Translate a SCSI result code into a blk_status_t value. May reset the host |
| 636 | * byte of @cmd->result. |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 637 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 638 | static blk_status_t scsi_result_to_blk_status(struct scsi_cmnd *cmd, int result) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 639 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 640 | 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; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 650 | case DID_TRANSPORT_FAILFAST: |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 651 | return BLK_STS_TRANSPORT; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 652 | case DID_TARGET_FAILURE: |
| 653 | set_host_byte(cmd, DID_OK); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 654 | return BLK_STS_TARGET; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 655 | case DID_NEXUS_FAILURE: |
| 656 | set_host_byte(cmd, DID_OK); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 657 | return BLK_STS_NEXUS; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 658 | case DID_ALLOC_FAILURE: |
| 659 | set_host_byte(cmd, DID_OK); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 660 | return BLK_STS_NOSPC; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 661 | case DID_MEDIUM_ERROR: |
| 662 | set_host_byte(cmd, DID_OK); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 663 | return BLK_STS_MEDIUM; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 664 | default: |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 665 | return BLK_STS_IOERR; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 666 | } |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 669 | /* Helper for scsi_io_completion() when "reprep" action required. */ |
| 670 | static void scsi_io_completion_reprep(struct scsi_cmnd *cmd, |
| 671 | struct request_queue *q) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 672 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 673 | /* 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. */ |
| 678 | static void scsi_io_completion_action(struct scsi_cmnd *cmd, int result) |
| 679 | { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 680 | struct request_queue *q = cmd->device->request_queue; |
| 681 | struct request *req = cmd->request; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 682 | int level = 0; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 683 | enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY, |
| 684 | ACTION_DELAYED_RETRY} action; |
| 685 | unsigned long wait_for = (cmd->allowed + 1) * req->timeout; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 686 | struct scsi_sense_hdr sshdr; |
| 687 | bool sense_valid; |
| 688 | bool sense_current = true; /* false implies "deferred sense" */ |
| 689 | blk_status_t blk_stat; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 690 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 691 | sense_valid = scsi_command_normalize_sense(cmd, &sshdr); |
| 692 | if (sense_valid) |
| 693 | sense_current = !scsi_sense_is_deferred(&sshdr); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 694 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 695 | blk_stat = scsi_result_to_blk_status(cmd, result); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 696 | |
| 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; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 703 | } else if (sense_valid && sense_current) { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 704 | 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; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 739 | blk_stat = BLK_STS_PROTECTION; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 740 | /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */ |
| 741 | } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) { |
| 742 | action = ACTION_FAIL; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 743 | blk_stat = BLK_STS_TARGET; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 744 | } else |
| 745 | action = ACTION_FAIL; |
| 746 | break; |
| 747 | case ABORTED_COMMAND: |
| 748 | action = ACTION_FAIL; |
| 749 | if (sshdr.asc == 0x10) /* DIF */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 750 | blk_stat = BLK_STS_PROTECTION; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 751 | 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 */ |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 765 | case 0x14: /* space allocation in progress */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 766 | 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 */ |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 770 | 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 */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 797 | if (!(req->rq_flags & RQF_QUIET)) { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 798 | static DEFINE_RATELIMIT_STATE(_rs, |
| 799 | DEFAULT_RATELIMIT_INTERVAL, |
| 800 | DEFAULT_RATELIMIT_BURST); |
| 801 | |
| 802 | if (unlikely(scsi_logging_level)) |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 803 | level = |
| 804 | SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT, |
| 805 | SCSI_LOG_MLCOMPLETE_BITS); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 806 | |
| 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); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 813 | if (driver_byte(result) == DRIVER_SENSE) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 814 | scsi_print_sense(cmd); |
| 815 | scsi_print_command(cmd); |
| 816 | } |
| 817 | } |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 818 | if (!scsi_end_request(req, blk_stat, blk_rq_err_bytes(req))) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 819 | return; |
| 820 | /*FALLTHRU*/ |
| 821 | case ACTION_REPREP: |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 822 | scsi_io_completion_reprep(cmd, q); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 823 | break; |
| 824 | case ACTION_RETRY: |
| 825 | /* Retry the same command immediately */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 826 | __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, false); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 827 | break; |
| 828 | case ACTION_DELAYED_RETRY: |
| 829 | /* Retry the same command after a delay */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 830 | __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, false); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 831 | break; |
| 832 | } |
| 833 | } |
| 834 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 835 | /* |
| 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 | */ |
| 840 | static 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 | */ |
| 937 | void 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 | |
| 990 | static blk_status_t scsi_init_sgtable(struct request *req, |
| 991 | struct scsi_data_buffer *sdb) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 992 | { |
| 993 | int count; |
| 994 | |
| 995 | /* |
| 996 | * If sg table allocation fails, requeue request later. |
| 997 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 998 | 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; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1002 | |
| 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; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1010 | sdb->length = blk_rq_payload_bytes(req); |
| 1011 | return BLK_STS_OK; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1012 | } |
| 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 | * |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1021 | * Returns: BLK_STS_OK on success |
| 1022 | * BLK_STS_RESOURCE if the failure is retryable |
| 1023 | * BLK_STS_IOERR if the failure is fatal |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1024 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1025 | blk_status_t scsi_init_io(struct scsi_cmnd *cmd) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1026 | { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1027 | struct request *rq = cmd->request; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1028 | blk_status_t ret; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1029 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1030 | if (WARN_ON_ONCE(!blk_rq_nr_phys_segments(rq))) |
| 1031 | return BLK_STS_IOERR; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1032 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1033 | ret = scsi_init_sgtable(rq, &cmd->sdb); |
| 1034 | if (ret) |
| 1035 | return ret; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1036 | |
| 1037 | if (blk_integrity_rq(rq)) { |
| 1038 | struct scsi_data_buffer *prot_sdb = cmd->prot_sdb; |
| 1039 | int ivecs, count; |
| 1040 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1041 | if (WARN_ON_ONCE(!prot_sdb)) { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1042 | /* |
| 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 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1047 | ret = BLK_STS_IOERR; |
| 1048 | goto out_free_sgtables; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio); |
| 1052 | |
| 1053 | if (sg_alloc_table_chained(&prot_sdb->table, ivecs, |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1054 | prot_sdb->table.sgl, |
| 1055 | SCSI_INLINE_PROT_SG_CNT)) { |
| 1056 | ret = BLK_STS_RESOURCE; |
| 1057 | goto out_free_sgtables; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | count = blk_rq_map_integrity_sg(rq->q, rq->bio, |
| 1061 | prot_sdb->table.sgl); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1062 | BUG_ON(count > ivecs); |
| 1063 | BUG_ON(count > queue_max_integrity_segments(rq->q)); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1064 | |
| 1065 | cmd->prot_sdb = prot_sdb; |
| 1066 | cmd->prot_sdb->table.nents = count; |
| 1067 | } |
| 1068 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1069 | return BLK_STS_OK; |
| 1070 | out_free_sgtables: |
| 1071 | scsi_free_sgtables(cmd); |
| 1072 | return ret; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1073 | } |
| 1074 | EXPORT_SYMBOL(scsi_init_io); |
| 1075 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1076 | /** |
| 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 | */ |
| 1087 | static void scsi_initialize_rq(struct request *rq) |
Googler | 38bda47 | 2022-08-19 10:07:08 -0700 | [diff] [blame] | 1088 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1089 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
Googler | 012a81c | 2022-09-15 14:55:24 +0800 | [diff] [blame] | 1090 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1091 | scsi_req_init(&cmd->req); |
| 1092 | init_rcu_head(&cmd->rcu); |
| 1093 | cmd->jiffies_at_alloc = jiffies; |
| 1094 | cmd->retries = 0; |
Googler | 0109c45 | 2022-10-13 17:50:39 +0800 | [diff] [blame] | 1095 | } |
| 1096 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1097 | /* |
| 1098 | * Only called when the request isn't completed by SCSI, and not freed by |
| 1099 | * SCSI |
| 1100 | */ |
| 1101 | static void scsi_cleanup_rq(struct request *rq) |
Googler | 0109c45 | 2022-10-13 17:50:39 +0800 | [diff] [blame] | 1102 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1103 | 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 */ |
| 1110 | void 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 */ |
| 1124 | void 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. */ |
| 1139 | void 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 | |
| 1170 | static 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); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1174 | |
| 1175 | /* |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1176 | * Passthrough requests may transfer data, in which case they must |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1177 | * 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) { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1182 | blk_status_t ret = scsi_init_io(cmd); |
| 1183 | if (unlikely(ret != BLK_STS_OK)) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1184 | return ret; |
| 1185 | } else { |
| 1186 | BUG_ON(blk_rq_bytes(req)); |
| 1187 | |
| 1188 | memset(&cmd->sdb, 0, sizeof(cmd->sdb)); |
| 1189 | } |
| 1190 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1191 | cmd->cmd_len = scsi_req(req)->cmd_len; |
| 1192 | cmd->cmnd = scsi_req(req)->cmd; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1193 | cmd->transfersize = blk_rq_bytes(req); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1194 | cmd->allowed = scsi_req(req)->retries; |
| 1195 | return BLK_STS_OK; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /* |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1199 | * Setup a normal block command. These are simple request from filesystems |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1200 | * that still need to be translated to SCSI CDBs from the ULD. |
| 1201 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1202 | static blk_status_t scsi_setup_fs_cmnd(struct scsi_device *sdev, |
| 1203 | struct request *req) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1204 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1205 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1206 | |
| 1207 | if (unlikely(sdev->handler && sdev->handler->prep_fn)) { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1208 | blk_status_t ret = sdev->handler->prep_fn(sdev, req); |
| 1209 | if (ret != BLK_STS_OK) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1210 | return ret; |
| 1211 | } |
| 1212 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1213 | cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1214 | memset(cmd->cmnd, 0, BLK_MAX_CDB); |
| 1215 | return scsi_cmd_to_driver(cmd)->init_command(cmd); |
| 1216 | } |
| 1217 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1218 | static blk_status_t scsi_setup_cmnd(struct scsi_device *sdev, |
| 1219 | struct request *req) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1220 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1221 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); |
| 1222 | blk_status_t ret; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1223 | |
| 1224 | if (!blk_rq_bytes(req)) |
| 1225 | cmd->sc_data_direction = DMA_NONE; |
| 1226 | else if (rq_data_dir(req) == WRITE) |
| 1227 | cmd->sc_data_direction = DMA_TO_DEVICE; |
| 1228 | else |
| 1229 | cmd->sc_data_direction = DMA_FROM_DEVICE; |
| 1230 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1231 | 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; |
Googler | 012a81c | 2022-09-15 14:55:24 +0800 | [diff] [blame] | 1240 | } |
Googler | 38bda47 | 2022-08-19 10:07:08 -0700 | [diff] [blame] | 1241 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1242 | static blk_status_t |
Googler | 012a81c | 2022-09-15 14:55:24 +0800 | [diff] [blame] | 1243 | scsi_prep_state_check(struct scsi_device *sdev, struct request *req) |
| 1244 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1245 | switch (sdev->sdev_state) { |
| 1246 | case SDEV_OFFLINE: |
| 1247 | case SDEV_TRANSPORT_OFFLINE: |
Googler | 012a81c | 2022-09-15 14:55:24 +0800 | [diff] [blame] | 1248 | /* |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1249 | * If the device is offline we refuse to process any |
| 1250 | * commands. The device must be brought online |
| 1251 | * before trying any recovery commands. |
Googler | 012a81c | 2022-09-15 14:55:24 +0800 | [diff] [blame] | 1252 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1253 | 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; |
Googler | 012a81c | 2022-09-15 14:55:24 +0800 | [diff] [blame] | 1274 | default: |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1275 | /* |
| 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; |
Googler | 012a81c | 2022-09-15 14:55:24 +0800 | [diff] [blame] | 1283 | } |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1284 | } |
| 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 | */ |
| 1292 | static 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 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1305 | if (atomic_dec_return(&sdev->device_blocked) > 0) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1306 | goto out_dec; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1307 | 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; |
| 1315 | out_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 | */ |
| 1324 | static 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 | |
| 1364 | starved: |
| 1365 | spin_lock_irq(shost->host_lock); |
| 1366 | list_move_tail(&sdev->starved_entry, &shost->starved_list); |
| 1367 | spin_unlock_irq(shost->host_lock); |
| 1368 | out_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 | */ |
| 1379 | static 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 | |
| 1419 | starved: |
| 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); |
| 1424 | out_dec: |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1425 | scsi_dec_host_busy(shost); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1426 | 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 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1441 | static bool scsi_mq_lld_busy(struct request_queue *q) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1442 | { |
| 1443 | struct scsi_device *sdev = q->queuedata; |
| 1444 | struct Scsi_Host *shost; |
| 1445 | |
| 1446 | if (blk_queue_dying(q)) |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1447 | return false; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1448 | |
| 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)) |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1458 | return true; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1459 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1460 | return false; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | static void scsi_softirq_done(struct request *rq) |
| 1464 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1465 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1466 | 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)) { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1478 | scmd_printk(KERN_ERR, cmd, |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1479 | "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: |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1497 | scsi_eh_scmd_add(cmd); |
| 1498 | break; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1499 | } |
| 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 | */ |
| 1509 | static 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 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1583 | /* Size in bytes of the sg-list stored in the scsi-mq command-private data. */ |
| 1584 | static unsigned int scsi_mq_inline_sgl_size(struct Scsi_Host *shost) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1585 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1586 | return min_t(unsigned int, shost->sg_tablesize, SCSI_INLINE_SG_CNT) * |
| 1587 | sizeof(struct scatterlist); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1590 | static blk_status_t scsi_mq_prep_fn(struct request *req) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1591 | { |
| 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; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1595 | struct scatterlist *sg; |
| 1596 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1597 | scsi_init_command(sdev, cmd); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1598 | |
| 1599 | cmd->request = req; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1600 | cmd->tag = req->tag; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1601 | cmd->prot_op = SCSI_PROT_NORMAL; |
| 1602 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1603 | 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)) { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1607 | 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 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1613 | blk_mq_start_request(req); |
| 1614 | |
| 1615 | return scsi_setup_cmnd(sdev, req); |
| 1616 | } |
| 1617 | |
| 1618 | static void scsi_mq_done(struct scsi_cmnd *cmd) |
| 1619 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1620 | if (unlikely(test_and_set_bit(SCMD_STATE_COMPLETE, &cmd->state))) |
| 1621 | return; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1622 | trace_scsi_dispatch_cmd_done(cmd); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1623 | |
| 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); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1634 | static 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 | |
| 1642 | static 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 | |
| 1655 | static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx, |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1656 | 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); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1663 | blk_status_t ret; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1664 | int reason; |
| 1665 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1666 | /* |
| 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 | } |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1675 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1676 | ret = BLK_STS_RESOURCE; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1677 | if (!scsi_target_queue_ready(shost, sdev)) |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1678 | goto out_put_budget; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1679 | if (!scsi_host_queue_ready(q, shost, sdev)) |
| 1680 | goto out_dec_target_busy; |
| 1681 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1682 | if (!(req->rq_flags & RQF_DONTPREP)) { |
| 1683 | ret = scsi_mq_prep_fn(req); |
| 1684 | if (ret != BLK_STS_OK) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1685 | goto out_dec_host_busy; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1686 | req->rq_flags |= RQF_DONTPREP; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1687 | } else { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1688 | clear_bit(SCMD_STATE_COMPLETE, &cmd->state); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1689 | blk_mq_start_request(req); |
| 1690 | } |
| 1691 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1692 | cmd->flags &= SCMD_PRESERVED_FLAGS; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1693 | if (sdev->simple_tags) |
| 1694 | cmd->flags |= SCMD_TAGGED; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1695 | if (bd->last) |
| 1696 | cmd->flags |= SCMD_LAST; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1697 | |
| 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); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1704 | ret = BLK_STS_RESOURCE; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1705 | goto out_dec_host_busy; |
| 1706 | } |
| 1707 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1708 | return BLK_STS_OK; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1709 | |
| 1710 | out_dec_host_busy: |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1711 | scsi_dec_host_busy(shost); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1712 | out_dec_target_busy: |
| 1713 | if (scsi_target(sdev)->can_queue > 0) |
| 1714 | atomic_dec(&scsi_target(sdev)->target_busy); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1715 | out_put_budget: |
| 1716 | scsi_mq_put_budget(hctx); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1717 | switch (ret) { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1718 | case BLK_STS_OK: |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1719 | break; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1720 | case BLK_STS_RESOURCE: |
Googler | b48fa91 | 2023-03-17 12:40:29 +0530 | [diff] [blame^] | 1721 | if (atomic_read(&sdev->device_busy) || |
| 1722 | scsi_device_blocked(sdev)) |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1723 | 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; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1730 | /* |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1731 | * Make sure to release all allocated resources when |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1732 | * we hit an error, as we will never see this command |
| 1733 | * again. |
| 1734 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1735 | if (req->rq_flags & RQF_DONTPREP) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1736 | scsi_mq_uninit_cmd(cmd); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1737 | scsi_run_queue_async(sdev); |
Googler | 9726be6 | 2022-12-14 05:53:31 +0000 | [diff] [blame] | 1738 | break; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1739 | } |
| 1740 | return ret; |
| 1741 | } |
| 1742 | |
| 1743 | static 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 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1751 | static int scsi_mq_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 1752 | unsigned int hctx_idx, unsigned int numa_node) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1753 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1754 | struct Scsi_Host *shost = set->driver_data; |
| 1755 | const bool unchecked_isa_dma = shost->unchecked_isa_dma; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1756 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1757 | struct scatterlist *sg; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1758 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1759 | 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); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1763 | if (!cmd->sense_buffer) |
| 1764 | return -ENOMEM; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1765 | 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 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1773 | return 0; |
| 1774 | } |
| 1775 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1776 | static void scsi_mq_exit_request(struct blk_mq_tag_set *set, struct request *rq, |
| 1777 | unsigned int hctx_idx) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1778 | { |
| 1779 | struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); |
| 1780 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1781 | scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA, |
| 1782 | cmd->sense_buffer); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1785 | static int scsi_map_queues(struct blk_mq_tag_set *set) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1786 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1787 | struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1788 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1789 | if (shost->hostt->map_queues) |
| 1790 | return shost->hostt->map_queues(shost); |
| 1791 | return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1792 | } |
| 1793 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1794 | void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1795 | { |
| 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 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1812 | if (dev->dma_mask) { |
| 1813 | shost->max_sectors = min_t(unsigned int, shost->max_sectors, |
| 1814 | dma_max_mapping_size(dev) >> SECTOR_SHIFT); |
| 1815 | } |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1816 | blk_queue_max_hw_sectors(q, shost->max_sectors); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1817 | if (shost->unchecked_isa_dma) |
| 1818 | blk_queue_bounce_limit(q, BLK_BOUNCE_ISA); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1819 | blk_queue_segment_boundary(q, shost->dma_boundary); |
| 1820 | dma_set_seg_boundary(dev, shost->dma_boundary); |
| 1821 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1822 | 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)); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1825 | |
| 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 | } |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1835 | EXPORT_SYMBOL_GPL(__scsi_init_queue); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1836 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1837 | static const struct blk_mq_ops scsi_mq_ops_no_commit = { |
| 1838 | .get_budget = scsi_mq_get_budget, |
| 1839 | .put_budget = scsi_mq_put_budget, |
Googler | 38bda47 | 2022-08-19 10:07:08 -0700 | [diff] [blame] | 1840 | .queue_rq = scsi_queue_rq, |
Googler | 38bda47 | 2022-08-19 10:07:08 -0700 | [diff] [blame] | 1841 | .complete = scsi_softirq_done, |
| 1842 | .timeout = scsi_timeout, |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1843 | #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 | |
| 1855 | static 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 | |
| 1864 | static 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, |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1880 | }; |
| 1881 | |
| 1882 | struct 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); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1890 | blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, sdev->request_queue); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1891 | return sdev->request_queue; |
| 1892 | } |
| 1893 | |
| 1894 | int scsi_mq_setup_tags(struct Scsi_Host *shost) |
| 1895 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1896 | unsigned int cmd_size, sgl_size; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1897 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1898 | sgl_size = max_t(unsigned int, sizeof(struct scatterlist), |
| 1899 | scsi_mq_inline_sgl_size(shost)); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1900 | cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size; |
| 1901 | if (scsi_host_get_prot(shost)) |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1902 | cmd_size += sizeof(struct scsi_data_buffer) + |
| 1903 | sizeof(struct scatterlist) * SCSI_INLINE_PROT_SG_CNT; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1904 | |
| 1905 | memset(&shost->tag_set, 0, sizeof(shost->tag_set)); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1906 | 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; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1910 | 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; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1914 | shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1915 | 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 | |
| 1922 | void 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 | */ |
| 1934 | struct scsi_device *scsi_device_from_queue(struct request_queue *q) |
| 1935 | { |
| 1936 | struct scsi_device *sdev = NULL; |
| 1937 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 1938 | if (q->mq_ops == &scsi_mq_ops_no_commit || |
| 1939 | q->mq_ops == &scsi_mq_ops) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 1940 | sdev = q->queuedata; |
| 1941 | if (!sdev || !get_device(&sdev->sdev_gendev)) |
| 1942 | sdev = NULL; |
| 1943 | |
| 1944 | return sdev; |
| 1945 | } |
| 1946 | EXPORT_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 | */ |
| 1964 | void scsi_block_requests(struct Scsi_Host *shost) |
| 1965 | { |
| 1966 | shost->host_self_blocked = 1; |
| 1967 | } |
| 1968 | EXPORT_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 | */ |
| 1990 | void scsi_unblock_requests(struct Scsi_Host *shost) |
| 1991 | { |
| 1992 | shost->host_self_blocked = 0; |
| 1993 | scsi_run_host_queues(shost); |
| 1994 | } |
| 1995 | EXPORT_SYMBOL(scsi_unblock_requests); |
| 1996 | |
| 1997 | int __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 | |
| 2010 | void scsi_exit_queue(void) |
| 2011 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2012 | kmem_cache_destroy(scsi_sense_cache); |
| 2013 | kmem_cache_destroy(scsi_sense_isadma_cache); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2014 | 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 | */ |
| 2035 | int |
| 2036 | scsi_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 | } |
| 2092 | EXPORT_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 | */ |
| 2111 | int |
| 2112 | scsi_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); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2124 | 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) && |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2161 | driver_byte(result) == DRIVER_SENSE) { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2162 | 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 | } |
| 2207 | EXPORT_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 |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2214 | * @sshdr: outpout pointer for decoded sense information. |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2215 | * |
| 2216 | * Returns zero if unsuccessful or an error if TUR failed. For |
| 2217 | * removable media, UNIT_ATTENTION sets ->changed flag. |
| 2218 | **/ |
| 2219 | int |
| 2220 | scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries, |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2221 | struct scsi_sense_hdr *sshdr) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2222 | { |
| 2223 | char cmd[] = { |
| 2224 | TEST_UNIT_READY, 0, 0, 0, 0, 0, |
| 2225 | }; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2226 | int result; |
| 2227 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2228 | /* 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, |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2231 | timeout, 1, NULL); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2232 | 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 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2238 | return result; |
| 2239 | } |
| 2240 | EXPORT_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 | * |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2247 | * Returns zero if successful or an error if the requested |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2248 | * transition is illegal. |
| 2249 | */ |
| 2250 | int |
| 2251 | scsi_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: |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2309 | case SDEV_OFFLINE: |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2310 | 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: |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2332 | 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: |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2345 | case SDEV_BLOCK: |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2346 | 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 | } |
| 2366 | EXPORT_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 | */ |
| 2375 | static 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; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2403 | case SDEV_EVT_POWER_ON_RESET_OCCURRED: |
| 2404 | envp[idx++] = "SDEV_UA=POWER_ON_RESET_OCCURRED"; |
| 2405 | break; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2406 | 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 | */ |
| 2423 | void 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 | */ |
| 2463 | void 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 | } |
| 2482 | EXPORT_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 | */ |
| 2491 | struct 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: |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2510 | case SDEV_EVT_POWER_ON_RESET_OCCURRED: |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2511 | default: |
| 2512 | /* do nothing */ |
| 2513 | break; |
| 2514 | } |
| 2515 | |
| 2516 | return evt; |
| 2517 | } |
| 2518 | EXPORT_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 | */ |
| 2528 | void 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 | } |
| 2540 | EXPORT_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 | */ |
| 2557 | int |
| 2558 | scsi_device_quiesce(struct scsi_device *sdev) |
| 2559 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2560 | struct request_queue *q = sdev->request_queue; |
| 2561 | int err; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2562 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2563 | /* |
| 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; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2594 | } |
| 2595 | EXPORT_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 | */ |
| 2606 | void 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 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2612 | 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); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2620 | } |
| 2621 | EXPORT_SYMBOL(scsi_device_resume); |
| 2622 | |
| 2623 | static void |
| 2624 | device_quiesce_fn(struct scsi_device *sdev, void *data) |
| 2625 | { |
| 2626 | scsi_device_quiesce(sdev); |
| 2627 | } |
| 2628 | |
| 2629 | void |
| 2630 | scsi_target_quiesce(struct scsi_target *starget) |
| 2631 | { |
| 2632 | starget_for_each_device(starget, NULL, device_quiesce_fn); |
| 2633 | } |
| 2634 | EXPORT_SYMBOL(scsi_target_quiesce); |
| 2635 | |
| 2636 | static void |
| 2637 | device_resume_fn(struct scsi_device *sdev, void *data) |
| 2638 | { |
| 2639 | scsi_device_resume(sdev); |
| 2640 | } |
| 2641 | |
| 2642 | void |
| 2643 | scsi_target_resume(struct scsi_target *starget) |
| 2644 | { |
| 2645 | starget_for_each_device(starget, NULL, device_resume_fn); |
| 2646 | } |
| 2647 | EXPORT_SYMBOL(scsi_target_resume); |
| 2648 | |
| 2649 | /** |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2650 | * scsi_internal_device_block_nowait - try to transition to the SDEV_BLOCK state |
| 2651 | * @sdev: device to block |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2652 | * |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2653 | * Pause SCSI command processing on the specified device. Does not sleep. |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2654 | * |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2655 | * Returns zero if successful or a negative error code upon failure. |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2656 | * |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2657 | * 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(). |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2662 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2663 | int scsi_internal_device_block_nowait(struct scsi_device *sdev) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2664 | { |
| 2665 | struct request_queue *q = sdev->request_queue; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2666 | 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 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2681 | blk_mq_quiesce_queue_nowait(q); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2682 | return 0; |
| 2683 | } |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2684 | EXPORT_SYMBOL_GPL(scsi_internal_device_block_nowait); |
| 2685 | |
Googler | 9726be6 | 2022-12-14 05:53:31 +0000 | [diff] [blame] | 2686 | /** |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2687 | * scsi_internal_device_block - try to transition to the SDEV_BLOCK state |
| 2688 | * @sdev: device to block |
Googler | 9726be6 | 2022-12-14 05:53:31 +0000 | [diff] [blame] | 2689 | * |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2690 | * 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. |
Googler | 9726be6 | 2022-12-14 05:53:31 +0000 | [diff] [blame] | 2692 | * |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2693 | * Returns zero if successful or a negative error code upon failure. |
Googler | 9726be6 | 2022-12-14 05:53:31 +0000 | [diff] [blame] | 2694 | * |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2695 | * 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(). |
Googler | 9726be6 | 2022-12-14 05:53:31 +0000 | [diff] [blame] | 2700 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2701 | static int scsi_internal_device_block(struct scsi_device *sdev) |
Googler | 0109c45 | 2022-10-13 17:50:39 +0800 | [diff] [blame] | 2702 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2703 | 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 | |
| 2715 | void 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 | */ |
| 2737 | int 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 | } |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2747 | |
Googler | 0109c45 | 2022-10-13 17:50:39 +0800 | [diff] [blame] | 2748 | /* |
| 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 | */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2752 | switch (sdev->sdev_state) { |
| 2753 | case SDEV_BLOCK: |
| 2754 | case SDEV_TRANSPORT_OFFLINE: |
Googler | 0109c45 | 2022-10-13 17:50:39 +0800 | [diff] [blame] | 2755 | sdev->sdev_state = new_state; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2756 | break; |
| 2757 | case SDEV_CREATED_BLOCK: |
Googler | 0109c45 | 2022-10-13 17:50:39 +0800 | [diff] [blame] | 2758 | 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; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2763 | break; |
| 2764 | case SDEV_CANCEL: |
| 2765 | case SDEV_OFFLINE: |
| 2766 | break; |
| 2767 | default: |
Googler | 0109c45 | 2022-10-13 17:50:39 +0800 | [diff] [blame] | 2768 | return -EINVAL; |
Googler | 0109c45 | 2022-10-13 17:50:39 +0800 | [diff] [blame] | 2769 | } |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2770 | scsi_start_queue(sdev); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2771 | |
| 2772 | return 0; |
| 2773 | } |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2774 | EXPORT_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 | */ |
| 2790 | static 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 | } |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2801 | |
| 2802 | static void |
| 2803 | device_block(struct scsi_device *sdev, void *data) |
| 2804 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2805 | 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); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | static int |
| 2814 | target_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 | |
| 2822 | void |
| 2823 | scsi_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 | } |
| 2831 | EXPORT_SYMBOL_GPL(scsi_target_block); |
| 2832 | |
| 2833 | static void |
| 2834 | device_unblock(struct scsi_device *sdev, void *data) |
| 2835 | { |
| 2836 | scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data); |
| 2837 | } |
| 2838 | |
| 2839 | static int |
| 2840 | target_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 | |
| 2848 | void |
| 2849 | scsi_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 | } |
| 2857 | EXPORT_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 | */ |
| 2868 | void *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 | } |
| 2907 | EXPORT_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 | */ |
| 2913 | void scsi_kunmap_atomic_sg(void *virt) |
| 2914 | { |
| 2915 | kunmap_atomic(virt); |
| 2916 | } |
| 2917 | EXPORT_SYMBOL(scsi_kunmap_atomic_sg); |
| 2918 | |
| 2919 | void sdev_disable_disk_events(struct scsi_device *sdev) |
| 2920 | { |
| 2921 | atomic_inc(&sdev->disk_events_disable_depth); |
| 2922 | } |
| 2923 | EXPORT_SYMBOL(sdev_disable_disk_events); |
| 2924 | |
| 2925 | void 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 | } |
| 2931 | EXPORT_SYMBOL(sdev_enable_disk_events); |
| 2932 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 2933 | static 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 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3005 | /** |
| 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 | */ |
| 3019 | int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len) |
| 3020 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 3021 | u8 cur_id_prio = 0; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3022 | u8 cur_id_size = 0; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 3023 | const unsigned char *d, *cur_id_str; |
| 3024 | const struct scsi_vpd *vpd_pg83; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3025 | 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 | |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3034 | /* 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); |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 3041 | 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) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3046 | 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; |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 3053 | cur_id_prio = prio; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3054 | 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; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3058 | id_size = snprintf(id, id_len, "t10.%*pE", |
| 3059 | cur_id_size, cur_id_str); |
| 3060 | break; |
| 3061 | case 0x2: |
| 3062 | /* EUI-64 */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 3063 | cur_id_prio = prio; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3064 | cur_id_size = d[3]; |
| 3065 | cur_id_str = d + 4; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3066 | 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: |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3083 | break; |
| 3084 | } |
| 3085 | break; |
| 3086 | case 0x3: |
| 3087 | /* NAA */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 3088 | cur_id_prio = prio; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3089 | cur_id_size = d[3]; |
| 3090 | cur_id_str = d + 4; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3091 | 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: |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3103 | break; |
| 3104 | } |
| 3105 | break; |
| 3106 | case 0x8: |
| 3107 | /* SCSI name string */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 3108 | if (cur_id_size > d[3]) |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3109 | break; |
| 3110 | /* Prefer others for truncated descriptor */ |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 3111 | if (d[3] > id_len) { |
| 3112 | prio = 2; |
| 3113 | if (cur_id_prio > prio) |
| 3114 | break; |
| 3115 | } |
| 3116 | cur_id_prio = prio; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3117 | cur_id_size = id_size = d[3]; |
| 3118 | cur_id_str = d + 4; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3119 | if (cur_id_size >= id_len) |
| 3120 | cur_id_size = id_len - 1; |
| 3121 | memcpy(id, cur_id_str, cur_id_size); |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3122 | break; |
| 3123 | default: |
| 3124 | break; |
| 3125 | } |
| 3126 | next_desig: |
| 3127 | d += d[3] + 4; |
| 3128 | } |
| 3129 | rcu_read_unlock(); |
| 3130 | |
| 3131 | return id_size; |
| 3132 | } |
| 3133 | EXPORT_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 | */ |
| 3144 | int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id) |
| 3145 | { |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 3146 | const unsigned char *d; |
| 3147 | const struct scsi_vpd *vpd_pg83; |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3148 | 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 | |
Googler | 9398cc3 | 2022-12-02 17:21:52 +0800 | [diff] [blame] | 3157 | d = vpd_pg83->data + 4; |
| 3158 | while (d < vpd_pg83->data + vpd_pg83->len) { |
Googler | af606d2 | 2022-10-26 21:40:12 -0700 | [diff] [blame] | 3159 | 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 | } |
| 3180 | EXPORT_SYMBOL(scsi_vpd_tpg_id); |