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