1 /* 2 * Copyright (C) 1999 Eric Youngdale 3 * Copyright (C) 2014 Christoph Hellwig 4 * 5 * SCSI queueing library. 6 * Initial versions: Eric Youngdale (eric@andante.org). 7 * Based upon conversations with large numbers 8 * of people at Linux Expo. 9 */ 10 11 #include <linux/bio.h> 12 #include <linux/bitops.h> 13 #include <linux/blkdev.h> 14 #include <linux/completion.h> 15 #include <linux/kernel.h> 16 #include <linux/export.h> 17 #include <linux/init.h> 18 #include <linux/pci.h> 19 #include <linux/delay.h> 20 #include <linux/hardirq.h> 21 #include <linux/scatterlist.h> 22 #include <linux/blk-mq.h> 23 #include <linux/ratelimit.h> 24 #include <asm/unaligned.h> 25 26 #include <scsi/scsi.h> 27 #include <scsi/scsi_cmnd.h> 28 #include <scsi/scsi_dbg.h> 29 #include <scsi/scsi_device.h> 30 #include <scsi/scsi_driver.h> 31 #include <scsi/scsi_eh.h> 32 #include <scsi/scsi_host.h> 33 #include <scsi/scsi_dh.h> 34 35 #include <trace/events/scsi.h> 36 37 #include "scsi_priv.h" 38 #include "scsi_logging.h" 39 40 41 struct kmem_cache *scsi_sdb_cache; 42 43 /* 44 * When to reinvoke queueing after a resource shortage. It's 3 msecs to 45 * not change behaviour from the previous unplug mechanism, experimentation 46 * may prove this needs changing. 47 */ 48 #define SCSI_QUEUE_DELAY 3 49 50 static void 51 scsi_set_blocked(struct scsi_cmnd *cmd, int reason) 52 { 53 struct Scsi_Host *host = cmd->device->host; 54 struct scsi_device *device = cmd->device; 55 struct scsi_target *starget = scsi_target(device); 56 57 /* 58 * Set the appropriate busy bit for the device/host. 59 * 60 * If the host/device isn't busy, assume that something actually 61 * completed, and that we should be able to queue a command now. 62 * 63 * Note that the prior mid-layer assumption that any host could 64 * always queue at least one command is now broken. The mid-layer 65 * will implement a user specifiable stall (see 66 * scsi_host.max_host_blocked and scsi_device.max_device_blocked) 67 * if a command is requeued with no other commands outstanding 68 * either for the device or for the host. 69 */ 70 switch (reason) { 71 case SCSI_MLQUEUE_HOST_BUSY: 72 atomic_set(&host->host_blocked, host->max_host_blocked); 73 break; 74 case SCSI_MLQUEUE_DEVICE_BUSY: 75 case SCSI_MLQUEUE_EH_RETRY: 76 atomic_set(&device->device_blocked, 77 device->max_device_blocked); 78 break; 79 case SCSI_MLQUEUE_TARGET_BUSY: 80 atomic_set(&starget->target_blocked, 81 starget->max_target_blocked); 82 break; 83 } 84 } 85 86 static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd) 87 { 88 struct scsi_device *sdev = cmd->device; 89 struct request_queue *q = cmd->request->q; 90 91 blk_mq_requeue_request(cmd->request); 92 blk_mq_kick_requeue_list(q); 93 put_device(&sdev->sdev_gendev); 94 } 95 96 /** 97 * __scsi_queue_insert - private queue insertion 98 * @cmd: The SCSI command being requeued 99 * @reason: The reason for the requeue 100 * @unbusy: Whether the queue should be unbusied 101 * 102 * This is a private queue insertion. The public interface 103 * scsi_queue_insert() always assumes the queue should be unbusied 104 * because it's always called before the completion. This function is 105 * for a requeue after completion, which should only occur in this 106 * file. 107 */ 108 static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy) 109 { 110 struct scsi_device *device = cmd->device; 111 struct request_queue *q = device->request_queue; 112 unsigned long flags; 113 114 SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd, 115 "Inserting command %p into mlqueue\n", cmd)); 116 117 scsi_set_blocked(cmd, reason); 118 119 /* 120 * Decrement the counters, since these commands are no longer 121 * active on the host/device. 122 */ 123 if (unbusy) 124 scsi_device_unbusy(device); 125 126 /* 127 * Requeue this command. It will go before all other commands 128 * that are already in the queue. Schedule requeue work under 129 * lock such that the kblockd_schedule_work() call happens 130 * before blk_cleanup_queue() finishes. 131 */ 132 cmd->result = 0; 133 if (q->mq_ops) { 134 scsi_mq_requeue_cmd(cmd); 135 return; 136 } 137 spin_lock_irqsave(q->queue_lock, flags); 138 blk_requeue_request(q, cmd->request); 139 kblockd_schedule_work(&device->requeue_work); 140 spin_unlock_irqrestore(q->queue_lock, flags); 141 } 142 143 /* 144 * Function: scsi_queue_insert() 145 * 146 * Purpose: Insert a command in the midlevel queue. 147 * 148 * Arguments: cmd - command that we are adding to queue. 149 * reason - why we are inserting command to queue. 150 * 151 * Lock status: Assumed that lock is not held upon entry. 152 * 153 * Returns: Nothing. 154 * 155 * Notes: We do this for one of two cases. Either the host is busy 156 * and it cannot accept any more commands for the time being, 157 * or the device returned QUEUE_FULL and can accept no more 158 * commands. 159 * Notes: This could be called either from an interrupt context or a 160 * normal process context. 161 */ 162 void scsi_queue_insert(struct scsi_cmnd *cmd, int reason) 163 { 164 __scsi_queue_insert(cmd, reason, 1); 165 } 166 /** 167 * scsi_execute - insert request and wait for the result 168 * @sdev: scsi device 169 * @cmd: scsi command 170 * @data_direction: data direction 171 * @buffer: data buffer 172 * @bufflen: len of buffer 173 * @sense: optional sense buffer 174 * @timeout: request timeout in seconds 175 * @retries: number of times to retry request 176 * @flags: or into request flags; 177 * @resid: optional residual length 178 * 179 * returns the req->errors value which is the scsi_cmnd result 180 * field. 181 */ 182 int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd, 183 int data_direction, void *buffer, unsigned bufflen, 184 unsigned char *sense, int timeout, int retries, u64 flags, 185 int *resid) 186 { 187 struct request *req; 188 int write = (data_direction == DMA_TO_DEVICE); 189 int ret = DRIVER_ERROR << 24; 190 191 req = blk_get_request(sdev->request_queue, write, __GFP_RECLAIM); 192 if (IS_ERR(req)) 193 return ret; 194 blk_rq_set_block_pc(req); 195 196 if (bufflen && blk_rq_map_kern(sdev->request_queue, req, 197 buffer, bufflen, __GFP_RECLAIM)) 198 goto out; 199 200 req->cmd_len = COMMAND_SIZE(cmd[0]); 201 memcpy(req->cmd, cmd, req->cmd_len); 202 req->sense = sense; 203 req->sense_len = 0; 204 req->retries = retries; 205 req->timeout = timeout; 206 req->cmd_flags |= flags | REQ_QUIET | REQ_PREEMPT; 207 208 /* 209 * head injection *required* here otherwise quiesce won't work 210 */ 211 blk_execute_rq(req->q, NULL, req, 1); 212 213 /* 214 * Some devices (USB mass-storage in particular) may transfer 215 * garbage data together with a residue indicating that the data 216 * is invalid. Prevent the garbage from being misinterpreted 217 * and prevent security leaks by zeroing out the excess data. 218 */ 219 if (unlikely(req->resid_len > 0 && req->resid_len <= bufflen)) 220 memset(buffer + (bufflen - req->resid_len), 0, req->resid_len); 221 222 if (resid) 223 *resid = req->resid_len; 224 ret = req->errors; 225 out: 226 blk_put_request(req); 227 228 return ret; 229 } 230 EXPORT_SYMBOL(scsi_execute); 231 232 int scsi_execute_req_flags(struct scsi_device *sdev, const unsigned char *cmd, 233 int data_direction, void *buffer, unsigned bufflen, 234 struct scsi_sense_hdr *sshdr, int timeout, int retries, 235 int *resid, u64 flags) 236 { 237 char *sense = NULL; 238 int result; 239 240 if (sshdr) { 241 sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO); 242 if (!sense) 243 return DRIVER_ERROR << 24; 244 } 245 result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen, 246 sense, timeout, retries, flags, resid); 247 if (sshdr) 248 scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr); 249 250 kfree(sense); 251 return result; 252 } 253 EXPORT_SYMBOL(scsi_execute_req_flags); 254 255 /* 256 * Function: scsi_init_cmd_errh() 257 * 258 * Purpose: Initialize cmd fields related to error handling. 259 * 260 * Arguments: cmd - command that is ready to be queued. 261 * 262 * Notes: This function has the job of initializing a number of 263 * fields related to error handling. Typically this will 264 * be called once for each command, as required. 265 */ 266 static void scsi_init_cmd_errh(struct scsi_cmnd *cmd) 267 { 268 cmd->serial_number = 0; 269 scsi_set_resid(cmd, 0); 270 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); 271 if (cmd->cmd_len == 0) 272 cmd->cmd_len = scsi_command_size(cmd->cmnd); 273 } 274 275 void scsi_device_unbusy(struct scsi_device *sdev) 276 { 277 struct Scsi_Host *shost = sdev->host; 278 struct scsi_target *starget = scsi_target(sdev); 279 unsigned long flags; 280 281 atomic_dec(&shost->host_busy); 282 if (starget->can_queue > 0) 283 atomic_dec(&starget->target_busy); 284 285 if (unlikely(scsi_host_in_recovery(shost) && 286 (shost->host_failed || shost->host_eh_scheduled))) { 287 spin_lock_irqsave(shost->host_lock, flags); 288 scsi_eh_wakeup(shost); 289 spin_unlock_irqrestore(shost->host_lock, flags); 290 } 291 292 atomic_dec(&sdev->device_busy); 293 } 294 295 static void scsi_kick_queue(struct request_queue *q) 296 { 297 if (q->mq_ops) 298 blk_mq_start_hw_queues(q); 299 else 300 blk_run_queue(q); 301 } 302 303 /* 304 * Called for single_lun devices on IO completion. Clear starget_sdev_user, 305 * and call blk_run_queue for all the scsi_devices on the target - 306 * including current_sdev first. 307 * 308 * Called with *no* scsi locks held. 309 */ 310 static void scsi_single_lun_run(struct scsi_device *current_sdev) 311 { 312 struct Scsi_Host *shost = current_sdev->host; 313 struct scsi_device *sdev, *tmp; 314 struct scsi_target *starget = scsi_target(current_sdev); 315 unsigned long flags; 316 317 spin_lock_irqsave(shost->host_lock, flags); 318 starget->starget_sdev_user = NULL; 319 spin_unlock_irqrestore(shost->host_lock, flags); 320 321 /* 322 * Call blk_run_queue for all LUNs on the target, starting with 323 * current_sdev. We race with others (to set starget_sdev_user), 324 * but in most cases, we will be first. Ideally, each LU on the 325 * target would get some limited time or requests on the target. 326 */ 327 scsi_kick_queue(current_sdev->request_queue); 328 329 spin_lock_irqsave(shost->host_lock, flags); 330 if (starget->starget_sdev_user) 331 goto out; 332 list_for_each_entry_safe(sdev, tmp, &starget->devices, 333 same_target_siblings) { 334 if (sdev == current_sdev) 335 continue; 336 if (scsi_device_get(sdev)) 337 continue; 338 339 spin_unlock_irqrestore(shost->host_lock, flags); 340 scsi_kick_queue(sdev->request_queue); 341 spin_lock_irqsave(shost->host_lock, flags); 342 343 scsi_device_put(sdev); 344 } 345 out: 346 spin_unlock_irqrestore(shost->host_lock, flags); 347 } 348 349 static inline bool scsi_device_is_busy(struct scsi_device *sdev) 350 { 351 if (atomic_read(&sdev->device_busy) >= sdev->queue_depth) 352 return true; 353 if (atomic_read(&sdev->device_blocked) > 0) 354 return true; 355 return false; 356 } 357 358 static inline bool scsi_target_is_busy(struct scsi_target *starget) 359 { 360 if (starget->can_queue > 0) { 361 if (atomic_read(&starget->target_busy) >= starget->can_queue) 362 return true; 363 if (atomic_read(&starget->target_blocked) > 0) 364 return true; 365 } 366 return false; 367 } 368 369 static inline bool scsi_host_is_busy(struct Scsi_Host *shost) 370 { 371 if (shost->can_queue > 0 && 372 atomic_read(&shost->host_busy) >= shost->can_queue) 373 return true; 374 if (atomic_read(&shost->host_blocked) > 0) 375 return true; 376 if (shost->host_self_blocked) 377 return true; 378 return false; 379 } 380 381 static void scsi_starved_list_run(struct Scsi_Host *shost) 382 { 383 LIST_HEAD(starved_list); 384 struct scsi_device *sdev; 385 unsigned long flags; 386 387 spin_lock_irqsave(shost->host_lock, flags); 388 list_splice_init(&shost->starved_list, &starved_list); 389 390 while (!list_empty(&starved_list)) { 391 struct request_queue *slq; 392 393 /* 394 * As long as shost is accepting commands and we have 395 * starved queues, call blk_run_queue. scsi_request_fn 396 * drops the queue_lock and can add us back to the 397 * starved_list. 398 * 399 * host_lock protects the starved_list and starved_entry. 400 * scsi_request_fn must get the host_lock before checking 401 * or modifying starved_list or starved_entry. 402 */ 403 if (scsi_host_is_busy(shost)) 404 break; 405 406 sdev = list_entry(starved_list.next, 407 struct scsi_device, starved_entry); 408 list_del_init(&sdev->starved_entry); 409 if (scsi_target_is_busy(scsi_target(sdev))) { 410 list_move_tail(&sdev->starved_entry, 411 &shost->starved_list); 412 continue; 413 } 414 415 /* 416 * Once we drop the host lock, a racing scsi_remove_device() 417 * call may remove the sdev from the starved list and destroy 418 * it and the queue. Mitigate by taking a reference to the 419 * queue and never touching the sdev again after we drop the 420 * host lock. Note: if __scsi_remove_device() invokes 421 * blk_cleanup_queue() before the queue is run from this 422 * function then blk_run_queue() will return immediately since 423 * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING. 424 */ 425 slq = sdev->request_queue; 426 if (!blk_get_queue(slq)) 427 continue; 428 spin_unlock_irqrestore(shost->host_lock, flags); 429 430 scsi_kick_queue(slq); 431 blk_put_queue(slq); 432 433 spin_lock_irqsave(shost->host_lock, flags); 434 } 435 /* put any unprocessed entries back */ 436 list_splice(&starved_list, &shost->starved_list); 437 spin_unlock_irqrestore(shost->host_lock, flags); 438 } 439 440 /* 441 * Function: scsi_run_queue() 442 * 443 * Purpose: Select a proper request queue to serve next 444 * 445 * Arguments: q - last request's queue 446 * 447 * Returns: Nothing 448 * 449 * Notes: The previous command was completely finished, start 450 * a new one if possible. 451 */ 452 static void scsi_run_queue(struct request_queue *q) 453 { 454 struct scsi_device *sdev = q->queuedata; 455 456 if (scsi_target(sdev)->single_lun) 457 scsi_single_lun_run(sdev); 458 if (!list_empty(&sdev->host->starved_list)) 459 scsi_starved_list_run(sdev->host); 460 461 if (q->mq_ops) 462 blk_mq_start_stopped_hw_queues(q, false); 463 else 464 blk_run_queue(q); 465 } 466 467 void scsi_requeue_run_queue(struct work_struct *work) 468 { 469 struct scsi_device *sdev; 470 struct request_queue *q; 471 472 sdev = container_of(work, struct scsi_device, requeue_work); 473 q = sdev->request_queue; 474 scsi_run_queue(q); 475 } 476 477 /* 478 * Function: scsi_requeue_command() 479 * 480 * Purpose: Handle post-processing of completed commands. 481 * 482 * Arguments: q - queue to operate on 483 * cmd - command that may need to be requeued. 484 * 485 * Returns: Nothing 486 * 487 * Notes: After command completion, there may be blocks left 488 * over which weren't finished by the previous command 489 * this can be for a number of reasons - the main one is 490 * I/O errors in the middle of the request, in which case 491 * we need to request the blocks that come after the bad 492 * sector. 493 * Notes: Upon return, cmd is a stale pointer. 494 */ 495 static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd) 496 { 497 struct scsi_device *sdev = cmd->device; 498 struct request *req = cmd->request; 499 unsigned long flags; 500 501 spin_lock_irqsave(q->queue_lock, flags); 502 blk_unprep_request(req); 503 req->special = NULL; 504 scsi_put_command(cmd); 505 blk_requeue_request(q, req); 506 spin_unlock_irqrestore(q->queue_lock, flags); 507 508 scsi_run_queue(q); 509 510 put_device(&sdev->sdev_gendev); 511 } 512 513 void scsi_run_host_queues(struct Scsi_Host *shost) 514 { 515 struct scsi_device *sdev; 516 517 shost_for_each_device(sdev, shost) 518 scsi_run_queue(sdev->request_queue); 519 } 520 521 static void scsi_uninit_cmd(struct scsi_cmnd *cmd) 522 { 523 if (cmd->request->cmd_type == REQ_TYPE_FS) { 524 struct scsi_driver *drv = scsi_cmd_to_driver(cmd); 525 526 if (drv->uninit_command) 527 drv->uninit_command(cmd); 528 } 529 } 530 531 static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd) 532 { 533 struct scsi_data_buffer *sdb; 534 535 if (cmd->sdb.table.nents) 536 sg_free_table_chained(&cmd->sdb.table, true); 537 if (cmd->request->next_rq) { 538 sdb = cmd->request->next_rq->special; 539 if (sdb) 540 sg_free_table_chained(&sdb->table, true); 541 } 542 if (scsi_prot_sg_count(cmd)) 543 sg_free_table_chained(&cmd->prot_sdb->table, true); 544 } 545 546 static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd) 547 { 548 struct scsi_device *sdev = cmd->device; 549 struct Scsi_Host *shost = sdev->host; 550 unsigned long flags; 551 552 scsi_mq_free_sgtables(cmd); 553 scsi_uninit_cmd(cmd); 554 555 if (shost->use_cmd_list) { 556 BUG_ON(list_empty(&cmd->list)); 557 spin_lock_irqsave(&sdev->list_lock, flags); 558 list_del_init(&cmd->list); 559 spin_unlock_irqrestore(&sdev->list_lock, flags); 560 } 561 } 562 563 /* 564 * Function: scsi_release_buffers() 565 * 566 * Purpose: Free resources allocate for a scsi_command. 567 * 568 * Arguments: cmd - command that we are bailing. 569 * 570 * Lock status: Assumed that no lock is held upon entry. 571 * 572 * Returns: Nothing 573 * 574 * Notes: In the event that an upper level driver rejects a 575 * command, we must release resources allocated during 576 * the __init_io() function. Primarily this would involve 577 * the scatter-gather table. 578 */ 579 static void scsi_release_buffers(struct scsi_cmnd *cmd) 580 { 581 if (cmd->sdb.table.nents) 582 sg_free_table_chained(&cmd->sdb.table, false); 583 584 memset(&cmd->sdb, 0, sizeof(cmd->sdb)); 585 586 if (scsi_prot_sg_count(cmd)) 587 sg_free_table_chained(&cmd->prot_sdb->table, false); 588 } 589 590 static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd) 591 { 592 struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special; 593 594 sg_free_table_chained(&bidi_sdb->table, false); 595 kmem_cache_free(scsi_sdb_cache, bidi_sdb); 596 cmd->request->next_rq->special = NULL; 597 } 598 599 static bool scsi_end_request(struct request *req, int error, 600 unsigned int bytes, unsigned int bidi_bytes) 601 { 602 struct scsi_cmnd *cmd = req->special; 603 struct scsi_device *sdev = cmd->device; 604 struct request_queue *q = sdev->request_queue; 605 606 if (blk_update_request(req, error, bytes)) 607 return true; 608 609 /* Bidi request must be completed as a whole */ 610 if (unlikely(bidi_bytes) && 611 blk_update_request(req->next_rq, error, bidi_bytes)) 612 return true; 613 614 if (blk_queue_add_random(q)) 615 add_disk_randomness(req->rq_disk); 616 617 if (req->mq_ctx) { 618 /* 619 * In the MQ case the command gets freed by __blk_mq_end_request, 620 * so we have to do all cleanup that depends on it earlier. 621 * 622 * We also can't kick the queues from irq context, so we 623 * will have to defer it to a workqueue. 624 */ 625 scsi_mq_uninit_cmd(cmd); 626 627 __blk_mq_end_request(req, error); 628 629 if (scsi_target(sdev)->single_lun || 630 !list_empty(&sdev->host->starved_list)) 631 kblockd_schedule_work(&sdev->requeue_work); 632 else 633 blk_mq_start_stopped_hw_queues(q, true); 634 } else { 635 unsigned long flags; 636 637 if (bidi_bytes) 638 scsi_release_bidi_buffers(cmd); 639 640 spin_lock_irqsave(q->queue_lock, flags); 641 blk_finish_request(req, error); 642 spin_unlock_irqrestore(q->queue_lock, flags); 643 644 scsi_release_buffers(cmd); 645 646 scsi_put_command(cmd); 647 scsi_run_queue(q); 648 } 649 650 put_device(&sdev->sdev_gendev); 651 return false; 652 } 653 654 /** 655 * __scsi_error_from_host_byte - translate SCSI error code into errno 656 * @cmd: SCSI command (unused) 657 * @result: scsi error code 658 * 659 * Translate SCSI error code into standard UNIX errno. 660 * Return values: 661 * -ENOLINK temporary transport failure 662 * -EREMOTEIO permanent target failure, do not retry 663 * -EBADE permanent nexus failure, retry on other path 664 * -ENOSPC No write space available 665 * -ENODATA Medium error 666 * -EIO unspecified I/O error 667 */ 668 static int __scsi_error_from_host_byte(struct scsi_cmnd *cmd, int result) 669 { 670 int error = 0; 671 672 switch(host_byte(result)) { 673 case DID_TRANSPORT_FAILFAST: 674 error = -ENOLINK; 675 break; 676 case DID_TARGET_FAILURE: 677 set_host_byte(cmd, DID_OK); 678 error = -EREMOTEIO; 679 break; 680 case DID_NEXUS_FAILURE: 681 set_host_byte(cmd, DID_OK); 682 error = -EBADE; 683 break; 684 case DID_ALLOC_FAILURE: 685 set_host_byte(cmd, DID_OK); 686 error = -ENOSPC; 687 break; 688 case DID_MEDIUM_ERROR: 689 set_host_byte(cmd, DID_OK); 690 error = -ENODATA; 691 break; 692 default: 693 error = -EIO; 694 break; 695 } 696 697 return error; 698 } 699 700 /* 701 * Function: scsi_io_completion() 702 * 703 * Purpose: Completion processing for block device I/O requests. 704 * 705 * Arguments: cmd - command that is finished. 706 * 707 * Lock status: Assumed that no lock is held upon entry. 708 * 709 * Returns: Nothing 710 * 711 * Notes: We will finish off the specified number of sectors. If we 712 * are done, the command block will be released and the queue 713 * function will be goosed. If we are not done then we have to 714 * figure out what to do next: 715 * 716 * a) We can call scsi_requeue_command(). The request 717 * will be unprepared and put back on the queue. Then 718 * a new command will be created for it. This should 719 * be used if we made forward progress, or if we want 720 * to switch from READ(10) to READ(6) for example. 721 * 722 * b) We can call __scsi_queue_insert(). The request will 723 * be put back on the queue and retried using the same 724 * command as before, possibly after a delay. 725 * 726 * c) We can call scsi_end_request() with -EIO to fail 727 * the remainder of the request. 728 */ 729 void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes) 730 { 731 int result = cmd->result; 732 struct request_queue *q = cmd->device->request_queue; 733 struct request *req = cmd->request; 734 int error = 0; 735 struct scsi_sense_hdr sshdr; 736 bool sense_valid = false; 737 int sense_deferred = 0, level = 0; 738 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY, 739 ACTION_DELAYED_RETRY} action; 740 unsigned long wait_for = (cmd->allowed + 1) * req->timeout; 741 742 if (result) { 743 sense_valid = scsi_command_normalize_sense(cmd, &sshdr); 744 if (sense_valid) 745 sense_deferred = scsi_sense_is_deferred(&sshdr); 746 } 747 748 if (req->cmd_type == REQ_TYPE_BLOCK_PC) { /* SG_IO ioctl from block level */ 749 if (result) { 750 if (sense_valid && req->sense) { 751 /* 752 * SG_IO wants current and deferred errors 753 */ 754 int len = 8 + cmd->sense_buffer[7]; 755 756 if (len > SCSI_SENSE_BUFFERSIZE) 757 len = SCSI_SENSE_BUFFERSIZE; 758 memcpy(req->sense, cmd->sense_buffer, len); 759 req->sense_len = len; 760 } 761 if (!sense_deferred) 762 error = __scsi_error_from_host_byte(cmd, result); 763 } 764 /* 765 * __scsi_error_from_host_byte may have reset the host_byte 766 */ 767 req->errors = cmd->result; 768 769 req->resid_len = scsi_get_resid(cmd); 770 771 if (scsi_bidi_cmnd(cmd)) { 772 /* 773 * Bidi commands Must be complete as a whole, 774 * both sides at once. 775 */ 776 req->next_rq->resid_len = scsi_in(cmd)->resid; 777 if (scsi_end_request(req, 0, blk_rq_bytes(req), 778 blk_rq_bytes(req->next_rq))) 779 BUG(); 780 return; 781 } 782 } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) { 783 /* 784 * Certain non BLOCK_PC requests are commands that don't 785 * actually transfer anything (FLUSH), so cannot use 786 * good_bytes != blk_rq_bytes(req) as the signal for an error. 787 * This sets the error explicitly for the problem case. 788 */ 789 error = __scsi_error_from_host_byte(cmd, result); 790 } 791 792 /* no bidi support for !REQ_TYPE_BLOCK_PC yet */ 793 BUG_ON(blk_bidi_rq(req)); 794 795 /* 796 * Next deal with any sectors which we were able to correctly 797 * handle. 798 */ 799 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd, 800 "%u sectors total, %d bytes done.\n", 801 blk_rq_sectors(req), good_bytes)); 802 803 /* 804 * Recovered errors need reporting, but they're always treated 805 * as success, so fiddle the result code here. For BLOCK_PC 806 * we already took a copy of the original into rq->errors which 807 * is what gets returned to the user 808 */ 809 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) { 810 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip 811 * print since caller wants ATA registers. Only occurs on 812 * SCSI ATA PASS_THROUGH commands when CK_COND=1 813 */ 814 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d)) 815 ; 816 else if (!(req->cmd_flags & REQ_QUIET)) 817 scsi_print_sense(cmd); 818 result = 0; 819 /* BLOCK_PC may have set error */ 820 error = 0; 821 } 822 823 /* 824 * If we finished all bytes in the request we are done now. 825 */ 826 if (!scsi_end_request(req, error, good_bytes, 0)) 827 return; 828 829 /* 830 * Kill remainder if no retrys. 831 */ 832 if (error && scsi_noretry_cmd(cmd)) { 833 if (scsi_end_request(req, error, blk_rq_bytes(req), 0)) 834 BUG(); 835 return; 836 } 837 838 /* 839 * If there had been no error, but we have leftover bytes in the 840 * requeues just queue the command up again. 841 */ 842 if (result == 0) 843 goto requeue; 844 845 error = __scsi_error_from_host_byte(cmd, result); 846 847 if (host_byte(result) == DID_RESET) { 848 /* Third party bus reset or reset for error recovery 849 * reasons. Just retry the command and see what 850 * happens. 851 */ 852 action = ACTION_RETRY; 853 } else if (sense_valid && !sense_deferred) { 854 switch (sshdr.sense_key) { 855 case UNIT_ATTENTION: 856 if (cmd->device->removable) { 857 /* Detected disc change. Set a bit 858 * and quietly refuse further access. 859 */ 860 cmd->device->changed = 1; 861 action = ACTION_FAIL; 862 } else { 863 /* Must have been a power glitch, or a 864 * bus reset. Could not have been a 865 * media change, so we just retry the 866 * command and see what happens. 867 */ 868 action = ACTION_RETRY; 869 } 870 break; 871 case ILLEGAL_REQUEST: 872 /* If we had an ILLEGAL REQUEST returned, then 873 * we may have performed an unsupported 874 * command. The only thing this should be 875 * would be a ten byte read where only a six 876 * byte read was supported. Also, on a system 877 * where READ CAPACITY failed, we may have 878 * read past the end of the disk. 879 */ 880 if ((cmd->device->use_10_for_rw && 881 sshdr.asc == 0x20 && sshdr.ascq == 0x00) && 882 (cmd->cmnd[0] == READ_10 || 883 cmd->cmnd[0] == WRITE_10)) { 884 /* This will issue a new 6-byte command. */ 885 cmd->device->use_10_for_rw = 0; 886 action = ACTION_REPREP; 887 } else if (sshdr.asc == 0x10) /* DIX */ { 888 action = ACTION_FAIL; 889 error = -EILSEQ; 890 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */ 891 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) { 892 action = ACTION_FAIL; 893 error = -EREMOTEIO; 894 } else 895 action = ACTION_FAIL; 896 break; 897 case ABORTED_COMMAND: 898 action = ACTION_FAIL; 899 if (sshdr.asc == 0x10) /* DIF */ 900 error = -EILSEQ; 901 break; 902 case NOT_READY: 903 /* If the device is in the process of becoming 904 * ready, or has a temporary blockage, retry. 905 */ 906 if (sshdr.asc == 0x04) { 907 switch (sshdr.ascq) { 908 case 0x01: /* becoming ready */ 909 case 0x04: /* format in progress */ 910 case 0x05: /* rebuild in progress */ 911 case 0x06: /* recalculation in progress */ 912 case 0x07: /* operation in progress */ 913 case 0x08: /* Long write in progress */ 914 case 0x09: /* self test in progress */ 915 case 0x14: /* space allocation in progress */ 916 action = ACTION_DELAYED_RETRY; 917 break; 918 default: 919 action = ACTION_FAIL; 920 break; 921 } 922 } else 923 action = ACTION_FAIL; 924 break; 925 case VOLUME_OVERFLOW: 926 /* See SSC3rXX or current. */ 927 action = ACTION_FAIL; 928 break; 929 default: 930 action = ACTION_FAIL; 931 break; 932 } 933 } else 934 action = ACTION_FAIL; 935 936 if (action != ACTION_FAIL && 937 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) 938 action = ACTION_FAIL; 939 940 switch (action) { 941 case ACTION_FAIL: 942 /* Give up and fail the remainder of the request */ 943 if (!(req->cmd_flags & REQ_QUIET)) { 944 static DEFINE_RATELIMIT_STATE(_rs, 945 DEFAULT_RATELIMIT_INTERVAL, 946 DEFAULT_RATELIMIT_BURST); 947 948 if (unlikely(scsi_logging_level)) 949 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT, 950 SCSI_LOG_MLCOMPLETE_BITS); 951 952 /* 953 * if logging is enabled the failure will be printed 954 * in scsi_log_completion(), so avoid duplicate messages 955 */ 956 if (!level && __ratelimit(&_rs)) { 957 scsi_print_result(cmd, NULL, FAILED); 958 if (driver_byte(result) & DRIVER_SENSE) 959 scsi_print_sense(cmd); 960 scsi_print_command(cmd); 961 } 962 } 963 if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0)) 964 return; 965 /*FALLTHRU*/ 966 case ACTION_REPREP: 967 requeue: 968 /* Unprep the request and put it back at the head of the queue. 969 * A new command will be prepared and issued. 970 */ 971 if (q->mq_ops) { 972 cmd->request->cmd_flags &= ~REQ_DONTPREP; 973 scsi_mq_uninit_cmd(cmd); 974 scsi_mq_requeue_cmd(cmd); 975 } else { 976 scsi_release_buffers(cmd); 977 scsi_requeue_command(q, cmd); 978 } 979 break; 980 case ACTION_RETRY: 981 /* Retry the same command immediately */ 982 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0); 983 break; 984 case ACTION_DELAYED_RETRY: 985 /* Retry the same command after a delay */ 986 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0); 987 break; 988 } 989 } 990 991 static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb) 992 { 993 int count; 994 995 /* 996 * If sg table allocation fails, requeue request later. 997 */ 998 if (unlikely(sg_alloc_table_chained(&sdb->table, req->nr_phys_segments, 999 sdb->table.sgl))) 1000 return BLKPREP_DEFER; 1001 1002 /* 1003 * Next, walk the list, and fill in the addresses and sizes of 1004 * each segment. 1005 */ 1006 count = blk_rq_map_sg(req->q, req, sdb->table.sgl); 1007 BUG_ON(count > sdb->table.nents); 1008 sdb->table.nents = count; 1009 sdb->length = blk_rq_bytes(req); 1010 return BLKPREP_OK; 1011 } 1012 1013 /* 1014 * Function: scsi_init_io() 1015 * 1016 * Purpose: SCSI I/O initialize function. 1017 * 1018 * Arguments: cmd - Command descriptor we wish to initialize 1019 * 1020 * Returns: 0 on success 1021 * BLKPREP_DEFER if the failure is retryable 1022 * BLKPREP_KILL if the failure is fatal 1023 */ 1024 int scsi_init_io(struct scsi_cmnd *cmd) 1025 { 1026 struct scsi_device *sdev = cmd->device; 1027 struct request *rq = cmd->request; 1028 bool is_mq = (rq->mq_ctx != NULL); 1029 int error; 1030 1031 BUG_ON(!rq->nr_phys_segments); 1032 1033 error = scsi_init_sgtable(rq, &cmd->sdb); 1034 if (error) 1035 goto err_exit; 1036 1037 if (blk_bidi_rq(rq)) { 1038 if (!rq->q->mq_ops) { 1039 struct scsi_data_buffer *bidi_sdb = 1040 kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC); 1041 if (!bidi_sdb) { 1042 error = BLKPREP_DEFER; 1043 goto err_exit; 1044 } 1045 1046 rq->next_rq->special = bidi_sdb; 1047 } 1048 1049 error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special); 1050 if (error) 1051 goto err_exit; 1052 } 1053 1054 if (blk_integrity_rq(rq)) { 1055 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb; 1056 int ivecs, count; 1057 1058 if (prot_sdb == NULL) { 1059 /* 1060 * This can happen if someone (e.g. multipath) 1061 * queues a command to a device on an adapter 1062 * that does not support DIX. 1063 */ 1064 WARN_ON_ONCE(1); 1065 error = BLKPREP_KILL; 1066 goto err_exit; 1067 } 1068 1069 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio); 1070 1071 if (sg_alloc_table_chained(&prot_sdb->table, ivecs, 1072 prot_sdb->table.sgl)) { 1073 error = BLKPREP_DEFER; 1074 goto err_exit; 1075 } 1076 1077 count = blk_rq_map_integrity_sg(rq->q, rq->bio, 1078 prot_sdb->table.sgl); 1079 BUG_ON(unlikely(count > ivecs)); 1080 BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q))); 1081 1082 cmd->prot_sdb = prot_sdb; 1083 cmd->prot_sdb->table.nents = count; 1084 } 1085 1086 return BLKPREP_OK; 1087 err_exit: 1088 if (is_mq) { 1089 scsi_mq_free_sgtables(cmd); 1090 } else { 1091 scsi_release_buffers(cmd); 1092 cmd->request->special = NULL; 1093 scsi_put_command(cmd); 1094 put_device(&sdev->sdev_gendev); 1095 } 1096 return error; 1097 } 1098 EXPORT_SYMBOL(scsi_init_io); 1099 1100 static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev, 1101 struct request *req) 1102 { 1103 struct scsi_cmnd *cmd; 1104 1105 if (!req->special) { 1106 /* Bail if we can't get a reference to the device */ 1107 if (!get_device(&sdev->sdev_gendev)) 1108 return NULL; 1109 1110 cmd = scsi_get_command(sdev, GFP_ATOMIC); 1111 if (unlikely(!cmd)) { 1112 put_device(&sdev->sdev_gendev); 1113 return NULL; 1114 } 1115 req->special = cmd; 1116 } else { 1117 cmd = req->special; 1118 } 1119 1120 /* pull a tag out of the request if we have one */ 1121 cmd->tag = req->tag; 1122 cmd->request = req; 1123 1124 cmd->cmnd = req->cmd; 1125 cmd->prot_op = SCSI_PROT_NORMAL; 1126 1127 return cmd; 1128 } 1129 1130 static int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req) 1131 { 1132 struct scsi_cmnd *cmd = req->special; 1133 1134 /* 1135 * BLOCK_PC requests may transfer data, in which case they must 1136 * a bio attached to them. Or they might contain a SCSI command 1137 * that does not transfer data, in which case they may optionally 1138 * submit a request without an attached bio. 1139 */ 1140 if (req->bio) { 1141 int ret = scsi_init_io(cmd); 1142 if (unlikely(ret)) 1143 return ret; 1144 } else { 1145 BUG_ON(blk_rq_bytes(req)); 1146 1147 memset(&cmd->sdb, 0, sizeof(cmd->sdb)); 1148 } 1149 1150 cmd->cmd_len = req->cmd_len; 1151 cmd->transfersize = blk_rq_bytes(req); 1152 cmd->allowed = req->retries; 1153 return BLKPREP_OK; 1154 } 1155 1156 /* 1157 * Setup a REQ_TYPE_FS command. These are simple request from filesystems 1158 * that still need to be translated to SCSI CDBs from the ULD. 1159 */ 1160 static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req) 1161 { 1162 struct scsi_cmnd *cmd = req->special; 1163 1164 if (unlikely(sdev->handler && sdev->handler->prep_fn)) { 1165 int ret = sdev->handler->prep_fn(sdev, req); 1166 if (ret != BLKPREP_OK) 1167 return ret; 1168 } 1169 1170 memset(cmd->cmnd, 0, BLK_MAX_CDB); 1171 return scsi_cmd_to_driver(cmd)->init_command(cmd); 1172 } 1173 1174 static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req) 1175 { 1176 struct scsi_cmnd *cmd = req->special; 1177 1178 if (!blk_rq_bytes(req)) 1179 cmd->sc_data_direction = DMA_NONE; 1180 else if (rq_data_dir(req) == WRITE) 1181 cmd->sc_data_direction = DMA_TO_DEVICE; 1182 else 1183 cmd->sc_data_direction = DMA_FROM_DEVICE; 1184 1185 switch (req->cmd_type) { 1186 case REQ_TYPE_FS: 1187 return scsi_setup_fs_cmnd(sdev, req); 1188 case REQ_TYPE_BLOCK_PC: 1189 return scsi_setup_blk_pc_cmnd(sdev, req); 1190 default: 1191 return BLKPREP_KILL; 1192 } 1193 } 1194 1195 static int 1196 scsi_prep_state_check(struct scsi_device *sdev, struct request *req) 1197 { 1198 int ret = BLKPREP_OK; 1199 1200 /* 1201 * If the device is not in running state we will reject some 1202 * or all commands. 1203 */ 1204 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) { 1205 switch (sdev->sdev_state) { 1206 case SDEV_OFFLINE: 1207 case SDEV_TRANSPORT_OFFLINE: 1208 /* 1209 * If the device is offline we refuse to process any 1210 * commands. The device must be brought online 1211 * before trying any recovery commands. 1212 */ 1213 sdev_printk(KERN_ERR, sdev, 1214 "rejecting I/O to offline device\n"); 1215 ret = BLKPREP_KILL; 1216 break; 1217 case SDEV_DEL: 1218 /* 1219 * If the device is fully deleted, we refuse to 1220 * process any commands as well. 1221 */ 1222 sdev_printk(KERN_ERR, sdev, 1223 "rejecting I/O to dead device\n"); 1224 ret = BLKPREP_KILL; 1225 break; 1226 case SDEV_BLOCK: 1227 case SDEV_CREATED_BLOCK: 1228 ret = BLKPREP_DEFER; 1229 break; 1230 case SDEV_QUIESCE: 1231 /* 1232 * If the devices is blocked we defer normal commands. 1233 */ 1234 if (!(req->cmd_flags & REQ_PREEMPT)) 1235 ret = BLKPREP_DEFER; 1236 break; 1237 default: 1238 /* 1239 * For any other not fully online state we only allow 1240 * special commands. In particular any user initiated 1241 * command is not allowed. 1242 */ 1243 if (!(req->cmd_flags & REQ_PREEMPT)) 1244 ret = BLKPREP_KILL; 1245 break; 1246 } 1247 } 1248 return ret; 1249 } 1250 1251 static int 1252 scsi_prep_return(struct request_queue *q, struct request *req, int ret) 1253 { 1254 struct scsi_device *sdev = q->queuedata; 1255 1256 switch (ret) { 1257 case BLKPREP_KILL: 1258 case BLKPREP_INVALID: 1259 req->errors = DID_NO_CONNECT << 16; 1260 /* release the command and kill it */ 1261 if (req->special) { 1262 struct scsi_cmnd *cmd = req->special; 1263 scsi_release_buffers(cmd); 1264 scsi_put_command(cmd); 1265 put_device(&sdev->sdev_gendev); 1266 req->special = NULL; 1267 } 1268 break; 1269 case BLKPREP_DEFER: 1270 /* 1271 * If we defer, the blk_peek_request() returns NULL, but the 1272 * queue must be restarted, so we schedule a callback to happen 1273 * shortly. 1274 */ 1275 if (atomic_read(&sdev->device_busy) == 0) 1276 blk_delay_queue(q, SCSI_QUEUE_DELAY); 1277 break; 1278 default: 1279 req->cmd_flags |= REQ_DONTPREP; 1280 } 1281 1282 return ret; 1283 } 1284 1285 static int scsi_prep_fn(struct request_queue *q, struct request *req) 1286 { 1287 struct scsi_device *sdev = q->queuedata; 1288 struct scsi_cmnd *cmd; 1289 int ret; 1290 1291 ret = scsi_prep_state_check(sdev, req); 1292 if (ret != BLKPREP_OK) 1293 goto out; 1294 1295 cmd = scsi_get_cmd_from_req(sdev, req); 1296 if (unlikely(!cmd)) { 1297 ret = BLKPREP_DEFER; 1298 goto out; 1299 } 1300 1301 ret = scsi_setup_cmnd(sdev, req); 1302 out: 1303 return scsi_prep_return(q, req, ret); 1304 } 1305 1306 static void scsi_unprep_fn(struct request_queue *q, struct request *req) 1307 { 1308 scsi_uninit_cmd(req->special); 1309 } 1310 1311 /* 1312 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else 1313 * return 0. 1314 * 1315 * Called with the queue_lock held. 1316 */ 1317 static inline int scsi_dev_queue_ready(struct request_queue *q, 1318 struct scsi_device *sdev) 1319 { 1320 unsigned int busy; 1321 1322 busy = atomic_inc_return(&sdev->device_busy) - 1; 1323 if (atomic_read(&sdev->device_blocked)) { 1324 if (busy) 1325 goto out_dec; 1326 1327 /* 1328 * unblock after device_blocked iterates to zero 1329 */ 1330 if (atomic_dec_return(&sdev->device_blocked) > 0) { 1331 /* 1332 * For the MQ case we take care of this in the caller. 1333 */ 1334 if (!q->mq_ops) 1335 blk_delay_queue(q, SCSI_QUEUE_DELAY); 1336 goto out_dec; 1337 } 1338 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev, 1339 "unblocking device at zero depth\n")); 1340 } 1341 1342 if (busy >= sdev->queue_depth) 1343 goto out_dec; 1344 1345 return 1; 1346 out_dec: 1347 atomic_dec(&sdev->device_busy); 1348 return 0; 1349 } 1350 1351 /* 1352 * scsi_target_queue_ready: checks if there we can send commands to target 1353 * @sdev: scsi device on starget to check. 1354 */ 1355 static inline int scsi_target_queue_ready(struct Scsi_Host *shost, 1356 struct scsi_device *sdev) 1357 { 1358 struct scsi_target *starget = scsi_target(sdev); 1359 unsigned int busy; 1360 1361 if (starget->single_lun) { 1362 spin_lock_irq(shost->host_lock); 1363 if (starget->starget_sdev_user && 1364 starget->starget_sdev_user != sdev) { 1365 spin_unlock_irq(shost->host_lock); 1366 return 0; 1367 } 1368 starget->starget_sdev_user = sdev; 1369 spin_unlock_irq(shost->host_lock); 1370 } 1371 1372 if (starget->can_queue <= 0) 1373 return 1; 1374 1375 busy = atomic_inc_return(&starget->target_busy) - 1; 1376 if (atomic_read(&starget->target_blocked) > 0) { 1377 if (busy) 1378 goto starved; 1379 1380 /* 1381 * unblock after target_blocked iterates to zero 1382 */ 1383 if (atomic_dec_return(&starget->target_blocked) > 0) 1384 goto out_dec; 1385 1386 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget, 1387 "unblocking target at zero depth\n")); 1388 } 1389 1390 if (busy >= starget->can_queue) 1391 goto starved; 1392 1393 return 1; 1394 1395 starved: 1396 spin_lock_irq(shost->host_lock); 1397 list_move_tail(&sdev->starved_entry, &shost->starved_list); 1398 spin_unlock_irq(shost->host_lock); 1399 out_dec: 1400 if (starget->can_queue > 0) 1401 atomic_dec(&starget->target_busy); 1402 return 0; 1403 } 1404 1405 /* 1406 * scsi_host_queue_ready: if we can send requests to shost, return 1 else 1407 * return 0. We must end up running the queue again whenever 0 is 1408 * returned, else IO can hang. 1409 */ 1410 static inline int scsi_host_queue_ready(struct request_queue *q, 1411 struct Scsi_Host *shost, 1412 struct scsi_device *sdev) 1413 { 1414 unsigned int busy; 1415 1416 if (scsi_host_in_recovery(shost)) 1417 return 0; 1418 1419 busy = atomic_inc_return(&shost->host_busy) - 1; 1420 if (atomic_read(&shost->host_blocked) > 0) { 1421 if (busy) 1422 goto starved; 1423 1424 /* 1425 * unblock after host_blocked iterates to zero 1426 */ 1427 if (atomic_dec_return(&shost->host_blocked) > 0) 1428 goto out_dec; 1429 1430 SCSI_LOG_MLQUEUE(3, 1431 shost_printk(KERN_INFO, shost, 1432 "unblocking host at zero depth\n")); 1433 } 1434 1435 if (shost->can_queue > 0 && busy >= shost->can_queue) 1436 goto starved; 1437 if (shost->host_self_blocked) 1438 goto starved; 1439 1440 /* We're OK to process the command, so we can't be starved */ 1441 if (!list_empty(&sdev->starved_entry)) { 1442 spin_lock_irq(shost->host_lock); 1443 if (!list_empty(&sdev->starved_entry)) 1444 list_del_init(&sdev->starved_entry); 1445 spin_unlock_irq(shost->host_lock); 1446 } 1447 1448 return 1; 1449 1450 starved: 1451 spin_lock_irq(shost->host_lock); 1452 if (list_empty(&sdev->starved_entry)) 1453 list_add_tail(&sdev->starved_entry, &shost->starved_list); 1454 spin_unlock_irq(shost->host_lock); 1455 out_dec: 1456 atomic_dec(&shost->host_busy); 1457 return 0; 1458 } 1459 1460 /* 1461 * Busy state exporting function for request stacking drivers. 1462 * 1463 * For efficiency, no lock is taken to check the busy state of 1464 * shost/starget/sdev, since the returned value is not guaranteed and 1465 * may be changed after request stacking drivers call the function, 1466 * regardless of taking lock or not. 1467 * 1468 * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi 1469 * needs to return 'not busy'. Otherwise, request stacking drivers 1470 * may hold requests forever. 1471 */ 1472 static int scsi_lld_busy(struct request_queue *q) 1473 { 1474 struct scsi_device *sdev = q->queuedata; 1475 struct Scsi_Host *shost; 1476 1477 if (blk_queue_dying(q)) 1478 return 0; 1479 1480 shost = sdev->host; 1481 1482 /* 1483 * Ignore host/starget busy state. 1484 * Since block layer does not have a concept of fairness across 1485 * multiple queues, congestion of host/starget needs to be handled 1486 * in SCSI layer. 1487 */ 1488 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev)) 1489 return 1; 1490 1491 return 0; 1492 } 1493 1494 /* 1495 * Kill a request for a dead device 1496 */ 1497 static void scsi_kill_request(struct request *req, struct request_queue *q) 1498 { 1499 struct scsi_cmnd *cmd = req->special; 1500 struct scsi_device *sdev; 1501 struct scsi_target *starget; 1502 struct Scsi_Host *shost; 1503 1504 blk_start_request(req); 1505 1506 scmd_printk(KERN_INFO, cmd, "killing request\n"); 1507 1508 sdev = cmd->device; 1509 starget = scsi_target(sdev); 1510 shost = sdev->host; 1511 scsi_init_cmd_errh(cmd); 1512 cmd->result = DID_NO_CONNECT << 16; 1513 atomic_inc(&cmd->device->iorequest_cnt); 1514 1515 /* 1516 * SCSI request completion path will do scsi_device_unbusy(), 1517 * bump busy counts. To bump the counters, we need to dance 1518 * with the locks as normal issue path does. 1519 */ 1520 atomic_inc(&sdev->device_busy); 1521 atomic_inc(&shost->host_busy); 1522 if (starget->can_queue > 0) 1523 atomic_inc(&starget->target_busy); 1524 1525 blk_complete_request(req); 1526 } 1527 1528 static void scsi_softirq_done(struct request *rq) 1529 { 1530 struct scsi_cmnd *cmd = rq->special; 1531 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout; 1532 int disposition; 1533 1534 INIT_LIST_HEAD(&cmd->eh_entry); 1535 1536 atomic_inc(&cmd->device->iodone_cnt); 1537 if (cmd->result) 1538 atomic_inc(&cmd->device->ioerr_cnt); 1539 1540 disposition = scsi_decide_disposition(cmd); 1541 if (disposition != SUCCESS && 1542 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) { 1543 sdev_printk(KERN_ERR, cmd->device, 1544 "timing out command, waited %lus\n", 1545 wait_for/HZ); 1546 disposition = SUCCESS; 1547 } 1548 1549 scsi_log_completion(cmd, disposition); 1550 1551 switch (disposition) { 1552 case SUCCESS: 1553 scsi_finish_command(cmd); 1554 break; 1555 case NEEDS_RETRY: 1556 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY); 1557 break; 1558 case ADD_TO_MLQUEUE: 1559 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY); 1560 break; 1561 default: 1562 if (!scsi_eh_scmd_add(cmd, 0)) 1563 scsi_finish_command(cmd); 1564 } 1565 } 1566 1567 /** 1568 * scsi_dispatch_command - Dispatch a command to the low-level driver. 1569 * @cmd: command block we are dispatching. 1570 * 1571 * Return: nonzero return request was rejected and device's queue needs to be 1572 * plugged. 1573 */ 1574 static int scsi_dispatch_cmd(struct scsi_cmnd *cmd) 1575 { 1576 struct Scsi_Host *host = cmd->device->host; 1577 int rtn = 0; 1578 1579 atomic_inc(&cmd->device->iorequest_cnt); 1580 1581 /* check if the device is still usable */ 1582 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) { 1583 /* in SDEV_DEL we error all commands. DID_NO_CONNECT 1584 * returns an immediate error upwards, and signals 1585 * that the device is no longer present */ 1586 cmd->result = DID_NO_CONNECT << 16; 1587 goto done; 1588 } 1589 1590 /* Check to see if the scsi lld made this device blocked. */ 1591 if (unlikely(scsi_device_blocked(cmd->device))) { 1592 /* 1593 * in blocked state, the command is just put back on 1594 * the device queue. The suspend state has already 1595 * blocked the queue so future requests should not 1596 * occur until the device transitions out of the 1597 * suspend state. 1598 */ 1599 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd, 1600 "queuecommand : device blocked\n")); 1601 return SCSI_MLQUEUE_DEVICE_BUSY; 1602 } 1603 1604 /* Store the LUN value in cmnd, if needed. */ 1605 if (cmd->device->lun_in_cdb) 1606 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) | 1607 (cmd->device->lun << 5 & 0xe0); 1608 1609 scsi_log_send(cmd); 1610 1611 /* 1612 * Before we queue this command, check if the command 1613 * length exceeds what the host adapter can handle. 1614 */ 1615 if (cmd->cmd_len > cmd->device->host->max_cmd_len) { 1616 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd, 1617 "queuecommand : command too long. " 1618 "cdb_size=%d host->max_cmd_len=%d\n", 1619 cmd->cmd_len, cmd->device->host->max_cmd_len)); 1620 cmd->result = (DID_ABORT << 16); 1621 goto done; 1622 } 1623 1624 if (unlikely(host->shost_state == SHOST_DEL)) { 1625 cmd->result = (DID_NO_CONNECT << 16); 1626 goto done; 1627 1628 } 1629 1630 trace_scsi_dispatch_cmd_start(cmd); 1631 rtn = host->hostt->queuecommand(host, cmd); 1632 if (rtn) { 1633 trace_scsi_dispatch_cmd_error(cmd, rtn); 1634 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY && 1635 rtn != SCSI_MLQUEUE_TARGET_BUSY) 1636 rtn = SCSI_MLQUEUE_HOST_BUSY; 1637 1638 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd, 1639 "queuecommand : request rejected\n")); 1640 } 1641 1642 return rtn; 1643 done: 1644 cmd->scsi_done(cmd); 1645 return 0; 1646 } 1647 1648 /** 1649 * scsi_done - Invoke completion on finished SCSI command. 1650 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives 1651 * ownership back to SCSI Core -- i.e. the LLDD has finished with it. 1652 * 1653 * Description: This function is the mid-level's (SCSI Core) interrupt routine, 1654 * which regains ownership of the SCSI command (de facto) from a LLDD, and 1655 * calls blk_complete_request() for further processing. 1656 * 1657 * This function is interrupt context safe. 1658 */ 1659 static void scsi_done(struct scsi_cmnd *cmd) 1660 { 1661 trace_scsi_dispatch_cmd_done(cmd); 1662 blk_complete_request(cmd->request); 1663 } 1664 1665 /* 1666 * Function: scsi_request_fn() 1667 * 1668 * Purpose: Main strategy routine for SCSI. 1669 * 1670 * Arguments: q - Pointer to actual queue. 1671 * 1672 * Returns: Nothing 1673 * 1674 * Lock status: IO request lock assumed to be held when called. 1675 */ 1676 static void scsi_request_fn(struct request_queue *q) 1677 __releases(q->queue_lock) 1678 __acquires(q->queue_lock) 1679 { 1680 struct scsi_device *sdev = q->queuedata; 1681 struct Scsi_Host *shost; 1682 struct scsi_cmnd *cmd; 1683 struct request *req; 1684 1685 /* 1686 * To start with, we keep looping until the queue is empty, or until 1687 * the host is no longer able to accept any more requests. 1688 */ 1689 shost = sdev->host; 1690 for (;;) { 1691 int rtn; 1692 /* 1693 * get next queueable request. We do this early to make sure 1694 * that the request is fully prepared even if we cannot 1695 * accept it. 1696 */ 1697 req = blk_peek_request(q); 1698 if (!req) 1699 break; 1700 1701 if (unlikely(!scsi_device_online(sdev))) { 1702 sdev_printk(KERN_ERR, sdev, 1703 "rejecting I/O to offline device\n"); 1704 scsi_kill_request(req, q); 1705 continue; 1706 } 1707 1708 if (!scsi_dev_queue_ready(q, sdev)) 1709 break; 1710 1711 /* 1712 * Remove the request from the request list. 1713 */ 1714 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req))) 1715 blk_start_request(req); 1716 1717 spin_unlock_irq(q->queue_lock); 1718 cmd = req->special; 1719 if (unlikely(cmd == NULL)) { 1720 printk(KERN_CRIT "impossible request in %s.\n" 1721 "please mail a stack trace to " 1722 "linux-scsi@vger.kernel.org\n", 1723 __func__); 1724 blk_dump_rq_flags(req, "foo"); 1725 BUG(); 1726 } 1727 1728 /* 1729 * We hit this when the driver is using a host wide 1730 * tag map. For device level tag maps the queue_depth check 1731 * in the device ready fn would prevent us from trying 1732 * to allocate a tag. Since the map is a shared host resource 1733 * we add the dev to the starved list so it eventually gets 1734 * a run when a tag is freed. 1735 */ 1736 if (blk_queue_tagged(q) && !(req->cmd_flags & REQ_QUEUED)) { 1737 spin_lock_irq(shost->host_lock); 1738 if (list_empty(&sdev->starved_entry)) 1739 list_add_tail(&sdev->starved_entry, 1740 &shost->starved_list); 1741 spin_unlock_irq(shost->host_lock); 1742 goto not_ready; 1743 } 1744 1745 if (!scsi_target_queue_ready(shost, sdev)) 1746 goto not_ready; 1747 1748 if (!scsi_host_queue_ready(q, shost, sdev)) 1749 goto host_not_ready; 1750 1751 if (sdev->simple_tags) 1752 cmd->flags |= SCMD_TAGGED; 1753 else 1754 cmd->flags &= ~SCMD_TAGGED; 1755 1756 /* 1757 * Finally, initialize any error handling parameters, and set up 1758 * the timers for timeouts. 1759 */ 1760 scsi_init_cmd_errh(cmd); 1761 1762 /* 1763 * Dispatch the command to the low-level driver. 1764 */ 1765 cmd->scsi_done = scsi_done; 1766 rtn = scsi_dispatch_cmd(cmd); 1767 if (rtn) { 1768 scsi_queue_insert(cmd, rtn); 1769 spin_lock_irq(q->queue_lock); 1770 goto out_delay; 1771 } 1772 spin_lock_irq(q->queue_lock); 1773 } 1774 1775 return; 1776 1777 host_not_ready: 1778 if (scsi_target(sdev)->can_queue > 0) 1779 atomic_dec(&scsi_target(sdev)->target_busy); 1780 not_ready: 1781 /* 1782 * lock q, handle tag, requeue req, and decrement device_busy. We 1783 * must return with queue_lock held. 1784 * 1785 * Decrementing device_busy without checking it is OK, as all such 1786 * cases (host limits or settings) should run the queue at some 1787 * later time. 1788 */ 1789 spin_lock_irq(q->queue_lock); 1790 blk_requeue_request(q, req); 1791 atomic_dec(&sdev->device_busy); 1792 out_delay: 1793 if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev)) 1794 blk_delay_queue(q, SCSI_QUEUE_DELAY); 1795 } 1796 1797 static inline int prep_to_mq(int ret) 1798 { 1799 switch (ret) { 1800 case BLKPREP_OK: 1801 return 0; 1802 case BLKPREP_DEFER: 1803 return BLK_MQ_RQ_QUEUE_BUSY; 1804 default: 1805 return BLK_MQ_RQ_QUEUE_ERROR; 1806 } 1807 } 1808 1809 static int scsi_mq_prep_fn(struct request *req) 1810 { 1811 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); 1812 struct scsi_device *sdev = req->q->queuedata; 1813 struct Scsi_Host *shost = sdev->host; 1814 unsigned char *sense_buf = cmd->sense_buffer; 1815 struct scatterlist *sg; 1816 1817 memset(cmd, 0, sizeof(struct scsi_cmnd)); 1818 1819 req->special = cmd; 1820 1821 cmd->request = req; 1822 cmd->device = sdev; 1823 cmd->sense_buffer = sense_buf; 1824 1825 cmd->tag = req->tag; 1826 1827 cmd->cmnd = req->cmd; 1828 cmd->prot_op = SCSI_PROT_NORMAL; 1829 1830 INIT_LIST_HEAD(&cmd->list); 1831 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler); 1832 cmd->jiffies_at_alloc = jiffies; 1833 1834 if (shost->use_cmd_list) { 1835 spin_lock_irq(&sdev->list_lock); 1836 list_add_tail(&cmd->list, &sdev->cmd_list); 1837 spin_unlock_irq(&sdev->list_lock); 1838 } 1839 1840 sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size; 1841 cmd->sdb.table.sgl = sg; 1842 1843 if (scsi_host_get_prot(shost)) { 1844 cmd->prot_sdb = (void *)sg + 1845 min_t(unsigned int, 1846 shost->sg_tablesize, SG_CHUNK_SIZE) * 1847 sizeof(struct scatterlist); 1848 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer)); 1849 1850 cmd->prot_sdb->table.sgl = 1851 (struct scatterlist *)(cmd->prot_sdb + 1); 1852 } 1853 1854 if (blk_bidi_rq(req)) { 1855 struct request *next_rq = req->next_rq; 1856 struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq); 1857 1858 memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer)); 1859 bidi_sdb->table.sgl = 1860 (struct scatterlist *)(bidi_sdb + 1); 1861 1862 next_rq->special = bidi_sdb; 1863 } 1864 1865 blk_mq_start_request(req); 1866 1867 return scsi_setup_cmnd(sdev, req); 1868 } 1869 1870 static void scsi_mq_done(struct scsi_cmnd *cmd) 1871 { 1872 trace_scsi_dispatch_cmd_done(cmd); 1873 blk_mq_complete_request(cmd->request, cmd->request->errors); 1874 } 1875 1876 static int scsi_queue_rq(struct blk_mq_hw_ctx *hctx, 1877 const struct blk_mq_queue_data *bd) 1878 { 1879 struct request *req = bd->rq; 1880 struct request_queue *q = req->q; 1881 struct scsi_device *sdev = q->queuedata; 1882 struct Scsi_Host *shost = sdev->host; 1883 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req); 1884 int ret; 1885 int reason; 1886 1887 ret = prep_to_mq(scsi_prep_state_check(sdev, req)); 1888 if (ret) 1889 goto out; 1890 1891 ret = BLK_MQ_RQ_QUEUE_BUSY; 1892 if (!get_device(&sdev->sdev_gendev)) 1893 goto out; 1894 1895 if (!scsi_dev_queue_ready(q, sdev)) 1896 goto out_put_device; 1897 if (!scsi_target_queue_ready(shost, sdev)) 1898 goto out_dec_device_busy; 1899 if (!scsi_host_queue_ready(q, shost, sdev)) 1900 goto out_dec_target_busy; 1901 1902 1903 if (!(req->cmd_flags & REQ_DONTPREP)) { 1904 ret = prep_to_mq(scsi_mq_prep_fn(req)); 1905 if (ret) 1906 goto out_dec_host_busy; 1907 req->cmd_flags |= REQ_DONTPREP; 1908 } else { 1909 blk_mq_start_request(req); 1910 } 1911 1912 if (sdev->simple_tags) 1913 cmd->flags |= SCMD_TAGGED; 1914 else 1915 cmd->flags &= ~SCMD_TAGGED; 1916 1917 scsi_init_cmd_errh(cmd); 1918 cmd->scsi_done = scsi_mq_done; 1919 1920 reason = scsi_dispatch_cmd(cmd); 1921 if (reason) { 1922 scsi_set_blocked(cmd, reason); 1923 ret = BLK_MQ_RQ_QUEUE_BUSY; 1924 goto out_dec_host_busy; 1925 } 1926 1927 return BLK_MQ_RQ_QUEUE_OK; 1928 1929 out_dec_host_busy: 1930 atomic_dec(&shost->host_busy); 1931 out_dec_target_busy: 1932 if (scsi_target(sdev)->can_queue > 0) 1933 atomic_dec(&scsi_target(sdev)->target_busy); 1934 out_dec_device_busy: 1935 atomic_dec(&sdev->device_busy); 1936 out_put_device: 1937 put_device(&sdev->sdev_gendev); 1938 out: 1939 switch (ret) { 1940 case BLK_MQ_RQ_QUEUE_BUSY: 1941 blk_mq_stop_hw_queue(hctx); 1942 if (atomic_read(&sdev->device_busy) == 0 && 1943 !scsi_device_blocked(sdev)) 1944 blk_mq_delay_queue(hctx, SCSI_QUEUE_DELAY); 1945 break; 1946 case BLK_MQ_RQ_QUEUE_ERROR: 1947 /* 1948 * Make sure to release all allocated ressources when 1949 * we hit an error, as we will never see this command 1950 * again. 1951 */ 1952 if (req->cmd_flags & REQ_DONTPREP) 1953 scsi_mq_uninit_cmd(cmd); 1954 break; 1955 default: 1956 break; 1957 } 1958 return ret; 1959 } 1960 1961 static enum blk_eh_timer_return scsi_timeout(struct request *req, 1962 bool reserved) 1963 { 1964 if (reserved) 1965 return BLK_EH_RESET_TIMER; 1966 return scsi_times_out(req); 1967 } 1968 1969 static int scsi_init_request(void *data, struct request *rq, 1970 unsigned int hctx_idx, unsigned int request_idx, 1971 unsigned int numa_node) 1972 { 1973 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); 1974 1975 cmd->sense_buffer = kzalloc_node(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL, 1976 numa_node); 1977 if (!cmd->sense_buffer) 1978 return -ENOMEM; 1979 return 0; 1980 } 1981 1982 static void scsi_exit_request(void *data, struct request *rq, 1983 unsigned int hctx_idx, unsigned int request_idx) 1984 { 1985 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); 1986 1987 kfree(cmd->sense_buffer); 1988 } 1989 1990 static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost) 1991 { 1992 struct device *host_dev; 1993 u64 bounce_limit = 0xffffffff; 1994 1995 if (shost->unchecked_isa_dma) 1996 return BLK_BOUNCE_ISA; 1997 /* 1998 * Platforms with virtual-DMA translation 1999 * hardware have no practical limit. 2000 */ 2001 if (!PCI_DMA_BUS_IS_PHYS) 2002 return BLK_BOUNCE_ANY; 2003 2004 host_dev = scsi_get_device(shost); 2005 if (host_dev && host_dev->dma_mask) 2006 bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT; 2007 2008 return bounce_limit; 2009 } 2010 2011 static void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q) 2012 { 2013 struct device *dev = shost->dma_dev; 2014 2015 /* 2016 * this limit is imposed by hardware restrictions 2017 */ 2018 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize, 2019 SG_MAX_SEGMENTS)); 2020 2021 if (scsi_host_prot_dma(shost)) { 2022 shost->sg_prot_tablesize = 2023 min_not_zero(shost->sg_prot_tablesize, 2024 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS); 2025 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize); 2026 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize); 2027 } 2028 2029 blk_queue_max_hw_sectors(q, shost->max_sectors); 2030 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost)); 2031 blk_queue_segment_boundary(q, shost->dma_boundary); 2032 dma_set_seg_boundary(dev, shost->dma_boundary); 2033 2034 blk_queue_max_segment_size(q, dma_get_max_seg_size(dev)); 2035 2036 if (!shost->use_clustering) 2037 q->limits.cluster = 0; 2038 2039 /* 2040 * set a reasonable default alignment on word boundaries: the 2041 * host and device may alter it using 2042 * blk_queue_update_dma_alignment() later. 2043 */ 2044 blk_queue_dma_alignment(q, 0x03); 2045 } 2046 2047 struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost, 2048 request_fn_proc *request_fn) 2049 { 2050 struct request_queue *q; 2051 2052 q = blk_init_queue(request_fn, NULL); 2053 if (!q) 2054 return NULL; 2055 __scsi_init_queue(shost, q); 2056 return q; 2057 } 2058 EXPORT_SYMBOL(__scsi_alloc_queue); 2059 2060 struct request_queue *scsi_alloc_queue(struct scsi_device *sdev) 2061 { 2062 struct request_queue *q; 2063 2064 q = __scsi_alloc_queue(sdev->host, scsi_request_fn); 2065 if (!q) 2066 return NULL; 2067 2068 blk_queue_prep_rq(q, scsi_prep_fn); 2069 blk_queue_unprep_rq(q, scsi_unprep_fn); 2070 blk_queue_softirq_done(q, scsi_softirq_done); 2071 blk_queue_rq_timed_out(q, scsi_times_out); 2072 blk_queue_lld_busy(q, scsi_lld_busy); 2073 return q; 2074 } 2075 2076 static struct blk_mq_ops scsi_mq_ops = { 2077 .map_queue = blk_mq_map_queue, 2078 .queue_rq = scsi_queue_rq, 2079 .complete = scsi_softirq_done, 2080 .timeout = scsi_timeout, 2081 .init_request = scsi_init_request, 2082 .exit_request = scsi_exit_request, 2083 }; 2084 2085 struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev) 2086 { 2087 sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set); 2088 if (IS_ERR(sdev->request_queue)) 2089 return NULL; 2090 2091 sdev->request_queue->queuedata = sdev; 2092 __scsi_init_queue(sdev->host, sdev->request_queue); 2093 return sdev->request_queue; 2094 } 2095 2096 int scsi_mq_setup_tags(struct Scsi_Host *shost) 2097 { 2098 unsigned int cmd_size, sgl_size, tbl_size; 2099 2100 tbl_size = shost->sg_tablesize; 2101 if (tbl_size > SG_CHUNK_SIZE) 2102 tbl_size = SG_CHUNK_SIZE; 2103 sgl_size = tbl_size * sizeof(struct scatterlist); 2104 cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size; 2105 if (scsi_host_get_prot(shost)) 2106 cmd_size += sizeof(struct scsi_data_buffer) + sgl_size; 2107 2108 memset(&shost->tag_set, 0, sizeof(shost->tag_set)); 2109 shost->tag_set.ops = &scsi_mq_ops; 2110 shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1; 2111 shost->tag_set.queue_depth = shost->can_queue; 2112 shost->tag_set.cmd_size = cmd_size; 2113 shost->tag_set.numa_node = NUMA_NO_NODE; 2114 shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; 2115 shost->tag_set.flags |= 2116 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy); 2117 shost->tag_set.driver_data = shost; 2118 2119 return blk_mq_alloc_tag_set(&shost->tag_set); 2120 } 2121 2122 void scsi_mq_destroy_tags(struct Scsi_Host *shost) 2123 { 2124 blk_mq_free_tag_set(&shost->tag_set); 2125 } 2126 2127 /* 2128 * Function: scsi_block_requests() 2129 * 2130 * Purpose: Utility function used by low-level drivers to prevent further 2131 * commands from being queued to the device. 2132 * 2133 * Arguments: shost - Host in question 2134 * 2135 * Returns: Nothing 2136 * 2137 * Lock status: No locks are assumed held. 2138 * 2139 * Notes: There is no timer nor any other means by which the requests 2140 * get unblocked other than the low-level driver calling 2141 * scsi_unblock_requests(). 2142 */ 2143 void scsi_block_requests(struct Scsi_Host *shost) 2144 { 2145 shost->host_self_blocked = 1; 2146 } 2147 EXPORT_SYMBOL(scsi_block_requests); 2148 2149 /* 2150 * Function: scsi_unblock_requests() 2151 * 2152 * Purpose: Utility function used by low-level drivers to allow further 2153 * commands from being queued to the device. 2154 * 2155 * Arguments: shost - Host in question 2156 * 2157 * Returns: Nothing 2158 * 2159 * Lock status: No locks are assumed held. 2160 * 2161 * Notes: There is no timer nor any other means by which the requests 2162 * get unblocked other than the low-level driver calling 2163 * scsi_unblock_requests(). 2164 * 2165 * This is done as an API function so that changes to the 2166 * internals of the scsi mid-layer won't require wholesale 2167 * changes to drivers that use this feature. 2168 */ 2169 void scsi_unblock_requests(struct Scsi_Host *shost) 2170 { 2171 shost->host_self_blocked = 0; 2172 scsi_run_host_queues(shost); 2173 } 2174 EXPORT_SYMBOL(scsi_unblock_requests); 2175 2176 int __init scsi_init_queue(void) 2177 { 2178 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer", 2179 sizeof(struct scsi_data_buffer), 2180 0, 0, NULL); 2181 if (!scsi_sdb_cache) { 2182 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n"); 2183 return -ENOMEM; 2184 } 2185 2186 return 0; 2187 } 2188 2189 void scsi_exit_queue(void) 2190 { 2191 kmem_cache_destroy(scsi_sdb_cache); 2192 } 2193 2194 /** 2195 * scsi_mode_select - issue a mode select 2196 * @sdev: SCSI device to be queried 2197 * @pf: Page format bit (1 == standard, 0 == vendor specific) 2198 * @sp: Save page bit (0 == don't save, 1 == save) 2199 * @modepage: mode page being requested 2200 * @buffer: request buffer (may not be smaller than eight bytes) 2201 * @len: length of request buffer. 2202 * @timeout: command timeout 2203 * @retries: number of retries before failing 2204 * @data: returns a structure abstracting the mode header data 2205 * @sshdr: place to put sense data (or NULL if no sense to be collected). 2206 * must be SCSI_SENSE_BUFFERSIZE big. 2207 * 2208 * Returns zero if successful; negative error number or scsi 2209 * status on error 2210 * 2211 */ 2212 int 2213 scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage, 2214 unsigned char *buffer, int len, int timeout, int retries, 2215 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) 2216 { 2217 unsigned char cmd[10]; 2218 unsigned char *real_buffer; 2219 int ret; 2220 2221 memset(cmd, 0, sizeof(cmd)); 2222 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0); 2223 2224 if (sdev->use_10_for_ms) { 2225 if (len > 65535) 2226 return -EINVAL; 2227 real_buffer = kmalloc(8 + len, GFP_KERNEL); 2228 if (!real_buffer) 2229 return -ENOMEM; 2230 memcpy(real_buffer + 8, buffer, len); 2231 len += 8; 2232 real_buffer[0] = 0; 2233 real_buffer[1] = 0; 2234 real_buffer[2] = data->medium_type; 2235 real_buffer[3] = data->device_specific; 2236 real_buffer[4] = data->longlba ? 0x01 : 0; 2237 real_buffer[5] = 0; 2238 real_buffer[6] = data->block_descriptor_length >> 8; 2239 real_buffer[7] = data->block_descriptor_length; 2240 2241 cmd[0] = MODE_SELECT_10; 2242 cmd[7] = len >> 8; 2243 cmd[8] = len; 2244 } else { 2245 if (len > 255 || data->block_descriptor_length > 255 || 2246 data->longlba) 2247 return -EINVAL; 2248 2249 real_buffer = kmalloc(4 + len, GFP_KERNEL); 2250 if (!real_buffer) 2251 return -ENOMEM; 2252 memcpy(real_buffer + 4, buffer, len); 2253 len += 4; 2254 real_buffer[0] = 0; 2255 real_buffer[1] = data->medium_type; 2256 real_buffer[2] = data->device_specific; 2257 real_buffer[3] = data->block_descriptor_length; 2258 2259 2260 cmd[0] = MODE_SELECT; 2261 cmd[4] = len; 2262 } 2263 2264 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len, 2265 sshdr, timeout, retries, NULL); 2266 kfree(real_buffer); 2267 return ret; 2268 } 2269 EXPORT_SYMBOL_GPL(scsi_mode_select); 2270 2271 /** 2272 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary. 2273 * @sdev: SCSI device to be queried 2274 * @dbd: set if mode sense will allow block descriptors to be returned 2275 * @modepage: mode page being requested 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 unsuccessful, or the header offset (either 4 2285 * or 8 depending on whether a six or ten byte command was 2286 * issued) if successful. 2287 */ 2288 int 2289 scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage, 2290 unsigned char *buffer, int len, int timeout, int retries, 2291 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) 2292 { 2293 unsigned char cmd[12]; 2294 int use_10_for_ms; 2295 int header_length; 2296 int result, retry_count = retries; 2297 struct scsi_sense_hdr my_sshdr; 2298 2299 memset(data, 0, sizeof(*data)); 2300 memset(&cmd[0], 0, 12); 2301 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */ 2302 cmd[2] = modepage; 2303 2304 /* caller might not be interested in sense, but we need it */ 2305 if (!sshdr) 2306 sshdr = &my_sshdr; 2307 2308 retry: 2309 use_10_for_ms = sdev->use_10_for_ms; 2310 2311 if (use_10_for_ms) { 2312 if (len < 8) 2313 len = 8; 2314 2315 cmd[0] = MODE_SENSE_10; 2316 cmd[8] = len; 2317 header_length = 8; 2318 } else { 2319 if (len < 4) 2320 len = 4; 2321 2322 cmd[0] = MODE_SENSE; 2323 cmd[4] = len; 2324 header_length = 4; 2325 } 2326 2327 memset(buffer, 0, len); 2328 2329 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len, 2330 sshdr, timeout, retries, NULL); 2331 2332 /* This code looks awful: what it's doing is making sure an 2333 * ILLEGAL REQUEST sense return identifies the actual command 2334 * byte as the problem. MODE_SENSE commands can return 2335 * ILLEGAL REQUEST if the code page isn't supported */ 2336 2337 if (use_10_for_ms && !scsi_status_is_good(result) && 2338 (driver_byte(result) & DRIVER_SENSE)) { 2339 if (scsi_sense_valid(sshdr)) { 2340 if ((sshdr->sense_key == ILLEGAL_REQUEST) && 2341 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) { 2342 /* 2343 * Invalid command operation code 2344 */ 2345 sdev->use_10_for_ms = 0; 2346 goto retry; 2347 } 2348 } 2349 } 2350 2351 if(scsi_status_is_good(result)) { 2352 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b && 2353 (modepage == 6 || modepage == 8))) { 2354 /* Initio breakage? */ 2355 header_length = 0; 2356 data->length = 13; 2357 data->medium_type = 0; 2358 data->device_specific = 0; 2359 data->longlba = 0; 2360 data->block_descriptor_length = 0; 2361 } else if(use_10_for_ms) { 2362 data->length = buffer[0]*256 + buffer[1] + 2; 2363 data->medium_type = buffer[2]; 2364 data->device_specific = buffer[3]; 2365 data->longlba = buffer[4] & 0x01; 2366 data->block_descriptor_length = buffer[6]*256 2367 + buffer[7]; 2368 } else { 2369 data->length = buffer[0] + 1; 2370 data->medium_type = buffer[1]; 2371 data->device_specific = buffer[2]; 2372 data->block_descriptor_length = buffer[3]; 2373 } 2374 data->header_length = header_length; 2375 } else if ((status_byte(result) == CHECK_CONDITION) && 2376 scsi_sense_valid(sshdr) && 2377 sshdr->sense_key == UNIT_ATTENTION && retry_count) { 2378 retry_count--; 2379 goto retry; 2380 } 2381 2382 return result; 2383 } 2384 EXPORT_SYMBOL(scsi_mode_sense); 2385 2386 /** 2387 * scsi_test_unit_ready - test if unit is ready 2388 * @sdev: scsi device to change the state of. 2389 * @timeout: command timeout 2390 * @retries: number of retries before failing 2391 * @sshdr_external: Optional pointer to struct scsi_sense_hdr for 2392 * returning sense. Make sure that this is cleared before passing 2393 * in. 2394 * 2395 * Returns zero if unsuccessful or an error if TUR failed. For 2396 * removable media, UNIT_ATTENTION sets ->changed flag. 2397 **/ 2398 int 2399 scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries, 2400 struct scsi_sense_hdr *sshdr_external) 2401 { 2402 char cmd[] = { 2403 TEST_UNIT_READY, 0, 0, 0, 0, 0, 2404 }; 2405 struct scsi_sense_hdr *sshdr; 2406 int result; 2407 2408 if (!sshdr_external) 2409 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL); 2410 else 2411 sshdr = sshdr_external; 2412 2413 /* try to eat the UNIT_ATTENTION if there are enough retries */ 2414 do { 2415 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr, 2416 timeout, retries, NULL); 2417 if (sdev->removable && scsi_sense_valid(sshdr) && 2418 sshdr->sense_key == UNIT_ATTENTION) 2419 sdev->changed = 1; 2420 } while (scsi_sense_valid(sshdr) && 2421 sshdr->sense_key == UNIT_ATTENTION && --retries); 2422 2423 if (!sshdr_external) 2424 kfree(sshdr); 2425 return result; 2426 } 2427 EXPORT_SYMBOL(scsi_test_unit_ready); 2428 2429 /** 2430 * scsi_device_set_state - Take the given device through the device state model. 2431 * @sdev: scsi device to change the state of. 2432 * @state: state to change to. 2433 * 2434 * Returns zero if unsuccessful or an error if the requested 2435 * transition is illegal. 2436 */ 2437 int 2438 scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state) 2439 { 2440 enum scsi_device_state oldstate = sdev->sdev_state; 2441 2442 if (state == oldstate) 2443 return 0; 2444 2445 switch (state) { 2446 case SDEV_CREATED: 2447 switch (oldstate) { 2448 case SDEV_CREATED_BLOCK: 2449 break; 2450 default: 2451 goto illegal; 2452 } 2453 break; 2454 2455 case SDEV_RUNNING: 2456 switch (oldstate) { 2457 case SDEV_CREATED: 2458 case SDEV_OFFLINE: 2459 case SDEV_TRANSPORT_OFFLINE: 2460 case SDEV_QUIESCE: 2461 case SDEV_BLOCK: 2462 break; 2463 default: 2464 goto illegal; 2465 } 2466 break; 2467 2468 case SDEV_QUIESCE: 2469 switch (oldstate) { 2470 case SDEV_RUNNING: 2471 case SDEV_OFFLINE: 2472 case SDEV_TRANSPORT_OFFLINE: 2473 break; 2474 default: 2475 goto illegal; 2476 } 2477 break; 2478 2479 case SDEV_OFFLINE: 2480 case SDEV_TRANSPORT_OFFLINE: 2481 switch (oldstate) { 2482 case SDEV_CREATED: 2483 case SDEV_RUNNING: 2484 case SDEV_QUIESCE: 2485 case SDEV_BLOCK: 2486 break; 2487 default: 2488 goto illegal; 2489 } 2490 break; 2491 2492 case SDEV_BLOCK: 2493 switch (oldstate) { 2494 case SDEV_RUNNING: 2495 case SDEV_CREATED_BLOCK: 2496 break; 2497 default: 2498 goto illegal; 2499 } 2500 break; 2501 2502 case SDEV_CREATED_BLOCK: 2503 switch (oldstate) { 2504 case SDEV_CREATED: 2505 break; 2506 default: 2507 goto illegal; 2508 } 2509 break; 2510 2511 case SDEV_CANCEL: 2512 switch (oldstate) { 2513 case SDEV_CREATED: 2514 case SDEV_RUNNING: 2515 case SDEV_QUIESCE: 2516 case SDEV_OFFLINE: 2517 case SDEV_TRANSPORT_OFFLINE: 2518 case SDEV_BLOCK: 2519 break; 2520 default: 2521 goto illegal; 2522 } 2523 break; 2524 2525 case SDEV_DEL: 2526 switch (oldstate) { 2527 case SDEV_CREATED: 2528 case SDEV_RUNNING: 2529 case SDEV_OFFLINE: 2530 case SDEV_TRANSPORT_OFFLINE: 2531 case SDEV_CANCEL: 2532 case SDEV_CREATED_BLOCK: 2533 break; 2534 default: 2535 goto illegal; 2536 } 2537 break; 2538 2539 } 2540 sdev->sdev_state = state; 2541 return 0; 2542 2543 illegal: 2544 SCSI_LOG_ERROR_RECOVERY(1, 2545 sdev_printk(KERN_ERR, sdev, 2546 "Illegal state transition %s->%s", 2547 scsi_device_state_name(oldstate), 2548 scsi_device_state_name(state)) 2549 ); 2550 return -EINVAL; 2551 } 2552 EXPORT_SYMBOL(scsi_device_set_state); 2553 2554 /** 2555 * sdev_evt_emit - emit a single SCSI device uevent 2556 * @sdev: associated SCSI device 2557 * @evt: event to emit 2558 * 2559 * Send a single uevent (scsi_event) to the associated scsi_device. 2560 */ 2561 static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt) 2562 { 2563 int idx = 0; 2564 char *envp[3]; 2565 2566 switch (evt->evt_type) { 2567 case SDEV_EVT_MEDIA_CHANGE: 2568 envp[idx++] = "SDEV_MEDIA_CHANGE=1"; 2569 break; 2570 case SDEV_EVT_INQUIRY_CHANGE_REPORTED: 2571 scsi_rescan_device(&sdev->sdev_gendev); 2572 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED"; 2573 break; 2574 case SDEV_EVT_CAPACITY_CHANGE_REPORTED: 2575 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED"; 2576 break; 2577 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED: 2578 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED"; 2579 break; 2580 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED: 2581 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED"; 2582 break; 2583 case SDEV_EVT_LUN_CHANGE_REPORTED: 2584 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED"; 2585 break; 2586 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED: 2587 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED"; 2588 break; 2589 default: 2590 /* do nothing */ 2591 break; 2592 } 2593 2594 envp[idx++] = NULL; 2595 2596 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp); 2597 } 2598 2599 /** 2600 * sdev_evt_thread - send a uevent for each scsi event 2601 * @work: work struct for scsi_device 2602 * 2603 * Dispatch queued events to their associated scsi_device kobjects 2604 * as uevents. 2605 */ 2606 void scsi_evt_thread(struct work_struct *work) 2607 { 2608 struct scsi_device *sdev; 2609 enum scsi_device_event evt_type; 2610 LIST_HEAD(event_list); 2611 2612 sdev = container_of(work, struct scsi_device, event_work); 2613 2614 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++) 2615 if (test_and_clear_bit(evt_type, sdev->pending_events)) 2616 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL); 2617 2618 while (1) { 2619 struct scsi_event *evt; 2620 struct list_head *this, *tmp; 2621 unsigned long flags; 2622 2623 spin_lock_irqsave(&sdev->list_lock, flags); 2624 list_splice_init(&sdev->event_list, &event_list); 2625 spin_unlock_irqrestore(&sdev->list_lock, flags); 2626 2627 if (list_empty(&event_list)) 2628 break; 2629 2630 list_for_each_safe(this, tmp, &event_list) { 2631 evt = list_entry(this, struct scsi_event, node); 2632 list_del(&evt->node); 2633 scsi_evt_emit(sdev, evt); 2634 kfree(evt); 2635 } 2636 } 2637 } 2638 2639 /** 2640 * sdev_evt_send - send asserted event to uevent thread 2641 * @sdev: scsi_device event occurred on 2642 * @evt: event to send 2643 * 2644 * Assert scsi device event asynchronously. 2645 */ 2646 void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt) 2647 { 2648 unsigned long flags; 2649 2650 #if 0 2651 /* FIXME: currently this check eliminates all media change events 2652 * for polled devices. Need to update to discriminate between AN 2653 * and polled events */ 2654 if (!test_bit(evt->evt_type, sdev->supported_events)) { 2655 kfree(evt); 2656 return; 2657 } 2658 #endif 2659 2660 spin_lock_irqsave(&sdev->list_lock, flags); 2661 list_add_tail(&evt->node, &sdev->event_list); 2662 schedule_work(&sdev->event_work); 2663 spin_unlock_irqrestore(&sdev->list_lock, flags); 2664 } 2665 EXPORT_SYMBOL_GPL(sdev_evt_send); 2666 2667 /** 2668 * sdev_evt_alloc - allocate a new scsi event 2669 * @evt_type: type of event to allocate 2670 * @gfpflags: GFP flags for allocation 2671 * 2672 * Allocates and returns a new scsi_event. 2673 */ 2674 struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type, 2675 gfp_t gfpflags) 2676 { 2677 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags); 2678 if (!evt) 2679 return NULL; 2680 2681 evt->evt_type = evt_type; 2682 INIT_LIST_HEAD(&evt->node); 2683 2684 /* evt_type-specific initialization, if any */ 2685 switch (evt_type) { 2686 case SDEV_EVT_MEDIA_CHANGE: 2687 case SDEV_EVT_INQUIRY_CHANGE_REPORTED: 2688 case SDEV_EVT_CAPACITY_CHANGE_REPORTED: 2689 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED: 2690 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED: 2691 case SDEV_EVT_LUN_CHANGE_REPORTED: 2692 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED: 2693 default: 2694 /* do nothing */ 2695 break; 2696 } 2697 2698 return evt; 2699 } 2700 EXPORT_SYMBOL_GPL(sdev_evt_alloc); 2701 2702 /** 2703 * sdev_evt_send_simple - send asserted event to uevent thread 2704 * @sdev: scsi_device event occurred on 2705 * @evt_type: type of event to send 2706 * @gfpflags: GFP flags for allocation 2707 * 2708 * Assert scsi device event asynchronously, given an event type. 2709 */ 2710 void sdev_evt_send_simple(struct scsi_device *sdev, 2711 enum scsi_device_event evt_type, gfp_t gfpflags) 2712 { 2713 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags); 2714 if (!evt) { 2715 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n", 2716 evt_type); 2717 return; 2718 } 2719 2720 sdev_evt_send(sdev, evt); 2721 } 2722 EXPORT_SYMBOL_GPL(sdev_evt_send_simple); 2723 2724 /** 2725 * scsi_device_quiesce - Block user issued commands. 2726 * @sdev: scsi device to quiesce. 2727 * 2728 * This works by trying to transition to the SDEV_QUIESCE state 2729 * (which must be a legal transition). When the device is in this 2730 * state, only special requests will be accepted, all others will 2731 * be deferred. Since special requests may also be requeued requests, 2732 * a successful return doesn't guarantee the device will be 2733 * totally quiescent. 2734 * 2735 * Must be called with user context, may sleep. 2736 * 2737 * Returns zero if unsuccessful or an error if not. 2738 */ 2739 int 2740 scsi_device_quiesce(struct scsi_device *sdev) 2741 { 2742 int err = scsi_device_set_state(sdev, SDEV_QUIESCE); 2743 if (err) 2744 return err; 2745 2746 scsi_run_queue(sdev->request_queue); 2747 while (atomic_read(&sdev->device_busy)) { 2748 msleep_interruptible(200); 2749 scsi_run_queue(sdev->request_queue); 2750 } 2751 return 0; 2752 } 2753 EXPORT_SYMBOL(scsi_device_quiesce); 2754 2755 /** 2756 * scsi_device_resume - Restart user issued commands to a quiesced device. 2757 * @sdev: scsi device to resume. 2758 * 2759 * Moves the device from quiesced back to running and restarts the 2760 * queues. 2761 * 2762 * Must be called with user context, may sleep. 2763 */ 2764 void scsi_device_resume(struct scsi_device *sdev) 2765 { 2766 /* check if the device state was mutated prior to resume, and if 2767 * so assume the state is being managed elsewhere (for example 2768 * device deleted during suspend) 2769 */ 2770 if (sdev->sdev_state != SDEV_QUIESCE || 2771 scsi_device_set_state(sdev, SDEV_RUNNING)) 2772 return; 2773 scsi_run_queue(sdev->request_queue); 2774 } 2775 EXPORT_SYMBOL(scsi_device_resume); 2776 2777 static void 2778 device_quiesce_fn(struct scsi_device *sdev, void *data) 2779 { 2780 scsi_device_quiesce(sdev); 2781 } 2782 2783 void 2784 scsi_target_quiesce(struct scsi_target *starget) 2785 { 2786 starget_for_each_device(starget, NULL, device_quiesce_fn); 2787 } 2788 EXPORT_SYMBOL(scsi_target_quiesce); 2789 2790 static void 2791 device_resume_fn(struct scsi_device *sdev, void *data) 2792 { 2793 scsi_device_resume(sdev); 2794 } 2795 2796 void 2797 scsi_target_resume(struct scsi_target *starget) 2798 { 2799 starget_for_each_device(starget, NULL, device_resume_fn); 2800 } 2801 EXPORT_SYMBOL(scsi_target_resume); 2802 2803 /** 2804 * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state 2805 * @sdev: device to block 2806 * 2807 * Block request made by scsi lld's to temporarily stop all 2808 * scsi commands on the specified device. Called from interrupt 2809 * or normal process context. 2810 * 2811 * Returns zero if successful or error if not 2812 * 2813 * Notes: 2814 * This routine transitions the device to the SDEV_BLOCK state 2815 * (which must be a legal transition). When the device is in this 2816 * state, all commands are deferred until the scsi lld reenables 2817 * the device with scsi_device_unblock or device_block_tmo fires. 2818 */ 2819 int 2820 scsi_internal_device_block(struct scsi_device *sdev) 2821 { 2822 struct request_queue *q = sdev->request_queue; 2823 unsigned long flags; 2824 int err = 0; 2825 2826 err = scsi_device_set_state(sdev, SDEV_BLOCK); 2827 if (err) { 2828 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK); 2829 2830 if (err) 2831 return err; 2832 } 2833 2834 /* 2835 * The device has transitioned to SDEV_BLOCK. Stop the 2836 * block layer from calling the midlayer with this device's 2837 * request queue. 2838 */ 2839 if (q->mq_ops) { 2840 blk_mq_stop_hw_queues(q); 2841 } else { 2842 spin_lock_irqsave(q->queue_lock, flags); 2843 blk_stop_queue(q); 2844 spin_unlock_irqrestore(q->queue_lock, flags); 2845 } 2846 2847 return 0; 2848 } 2849 EXPORT_SYMBOL_GPL(scsi_internal_device_block); 2850 2851 /** 2852 * scsi_internal_device_unblock - resume a device after a block request 2853 * @sdev: device to resume 2854 * @new_state: state to set devices to after unblocking 2855 * 2856 * Called by scsi lld's or the midlayer to restart the device queue 2857 * for the previously suspended scsi device. Called from interrupt or 2858 * normal process context. 2859 * 2860 * Returns zero if successful or error if not. 2861 * 2862 * Notes: 2863 * This routine transitions the device to the SDEV_RUNNING state 2864 * or to one of the offline states (which must be a legal transition) 2865 * allowing the midlayer to goose the queue for this device. 2866 */ 2867 int 2868 scsi_internal_device_unblock(struct scsi_device *sdev, 2869 enum scsi_device_state new_state) 2870 { 2871 struct request_queue *q = sdev->request_queue; 2872 unsigned long flags; 2873 2874 /* 2875 * Try to transition the scsi device to SDEV_RUNNING or one of the 2876 * offlined states and goose the device queue if successful. 2877 */ 2878 if ((sdev->sdev_state == SDEV_BLOCK) || 2879 (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE)) 2880 sdev->sdev_state = new_state; 2881 else if (sdev->sdev_state == SDEV_CREATED_BLOCK) { 2882 if (new_state == SDEV_TRANSPORT_OFFLINE || 2883 new_state == SDEV_OFFLINE) 2884 sdev->sdev_state = new_state; 2885 else 2886 sdev->sdev_state = SDEV_CREATED; 2887 } else if (sdev->sdev_state != SDEV_CANCEL && 2888 sdev->sdev_state != SDEV_OFFLINE) 2889 return -EINVAL; 2890 2891 if (q->mq_ops) { 2892 blk_mq_start_stopped_hw_queues(q, false); 2893 } else { 2894 spin_lock_irqsave(q->queue_lock, flags); 2895 blk_start_queue(q); 2896 spin_unlock_irqrestore(q->queue_lock, flags); 2897 } 2898 2899 return 0; 2900 } 2901 EXPORT_SYMBOL_GPL(scsi_internal_device_unblock); 2902 2903 static void 2904 device_block(struct scsi_device *sdev, void *data) 2905 { 2906 scsi_internal_device_block(sdev); 2907 } 2908 2909 static int 2910 target_block(struct device *dev, void *data) 2911 { 2912 if (scsi_is_target_device(dev)) 2913 starget_for_each_device(to_scsi_target(dev), NULL, 2914 device_block); 2915 return 0; 2916 } 2917 2918 void 2919 scsi_target_block(struct device *dev) 2920 { 2921 if (scsi_is_target_device(dev)) 2922 starget_for_each_device(to_scsi_target(dev), NULL, 2923 device_block); 2924 else 2925 device_for_each_child(dev, NULL, target_block); 2926 } 2927 EXPORT_SYMBOL_GPL(scsi_target_block); 2928 2929 static void 2930 device_unblock(struct scsi_device *sdev, void *data) 2931 { 2932 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data); 2933 } 2934 2935 static int 2936 target_unblock(struct device *dev, void *data) 2937 { 2938 if (scsi_is_target_device(dev)) 2939 starget_for_each_device(to_scsi_target(dev), data, 2940 device_unblock); 2941 return 0; 2942 } 2943 2944 void 2945 scsi_target_unblock(struct device *dev, enum scsi_device_state new_state) 2946 { 2947 if (scsi_is_target_device(dev)) 2948 starget_for_each_device(to_scsi_target(dev), &new_state, 2949 device_unblock); 2950 else 2951 device_for_each_child(dev, &new_state, target_unblock); 2952 } 2953 EXPORT_SYMBOL_GPL(scsi_target_unblock); 2954 2955 /** 2956 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt 2957 * @sgl: scatter-gather list 2958 * @sg_count: number of segments in sg 2959 * @offset: offset in bytes into sg, on return offset into the mapped area 2960 * @len: bytes to map, on return number of bytes mapped 2961 * 2962 * Returns virtual address of the start of the mapped page 2963 */ 2964 void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count, 2965 size_t *offset, size_t *len) 2966 { 2967 int i; 2968 size_t sg_len = 0, len_complete = 0; 2969 struct scatterlist *sg; 2970 struct page *page; 2971 2972 WARN_ON(!irqs_disabled()); 2973 2974 for_each_sg(sgl, sg, sg_count, i) { 2975 len_complete = sg_len; /* Complete sg-entries */ 2976 sg_len += sg->length; 2977 if (sg_len > *offset) 2978 break; 2979 } 2980 2981 if (unlikely(i == sg_count)) { 2982 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, " 2983 "elements %d\n", 2984 __func__, sg_len, *offset, sg_count); 2985 WARN_ON(1); 2986 return NULL; 2987 } 2988 2989 /* Offset starting from the beginning of first page in this sg-entry */ 2990 *offset = *offset - len_complete + sg->offset; 2991 2992 /* Assumption: contiguous pages can be accessed as "page + i" */ 2993 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT)); 2994 *offset &= ~PAGE_MASK; 2995 2996 /* Bytes in this sg-entry from *offset to the end of the page */ 2997 sg_len = PAGE_SIZE - *offset; 2998 if (*len > sg_len) 2999 *len = sg_len; 3000 3001 return kmap_atomic(page); 3002 } 3003 EXPORT_SYMBOL(scsi_kmap_atomic_sg); 3004 3005 /** 3006 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg 3007 * @virt: virtual address to be unmapped 3008 */ 3009 void scsi_kunmap_atomic_sg(void *virt) 3010 { 3011 kunmap_atomic(virt); 3012 } 3013 EXPORT_SYMBOL(scsi_kunmap_atomic_sg); 3014 3015 void sdev_disable_disk_events(struct scsi_device *sdev) 3016 { 3017 atomic_inc(&sdev->disk_events_disable_depth); 3018 } 3019 EXPORT_SYMBOL(sdev_disable_disk_events); 3020 3021 void sdev_enable_disk_events(struct scsi_device *sdev) 3022 { 3023 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0)) 3024 return; 3025 atomic_dec(&sdev->disk_events_disable_depth); 3026 } 3027 EXPORT_SYMBOL(sdev_enable_disk_events); 3028 3029 /** 3030 * scsi_vpd_lun_id - return a unique device identification 3031 * @sdev: SCSI device 3032 * @id: buffer for the identification 3033 * @id_len: length of the buffer 3034 * 3035 * Copies a unique device identification into @id based 3036 * on the information in the VPD page 0x83 of the device. 3037 * The string will be formatted as a SCSI name string. 3038 * 3039 * Returns the length of the identification or error on failure. 3040 * If the identifier is longer than the supplied buffer the actual 3041 * identifier length is returned and the buffer is not zero-padded. 3042 */ 3043 int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len) 3044 { 3045 u8 cur_id_type = 0xff; 3046 u8 cur_id_size = 0; 3047 unsigned char *d, *cur_id_str; 3048 unsigned char __rcu *vpd_pg83; 3049 int id_size = -EINVAL; 3050 3051 rcu_read_lock(); 3052 vpd_pg83 = rcu_dereference(sdev->vpd_pg83); 3053 if (!vpd_pg83) { 3054 rcu_read_unlock(); 3055 return -ENXIO; 3056 } 3057 3058 /* 3059 * Look for the correct descriptor. 3060 * Order of preference for lun descriptor: 3061 * - SCSI name string 3062 * - NAA IEEE Registered Extended 3063 * - EUI-64 based 16-byte 3064 * - EUI-64 based 12-byte 3065 * - NAA IEEE Registered 3066 * - NAA IEEE Extended 3067 * - T10 Vendor ID 3068 * as longer descriptors reduce the likelyhood 3069 * of identification clashes. 3070 */ 3071 3072 /* The id string must be at least 20 bytes + terminating NULL byte */ 3073 if (id_len < 21) { 3074 rcu_read_unlock(); 3075 return -EINVAL; 3076 } 3077 3078 memset(id, 0, id_len); 3079 d = vpd_pg83 + 4; 3080 while (d < vpd_pg83 + sdev->vpd_pg83_len) { 3081 /* Skip designators not referring to the LUN */ 3082 if ((d[1] & 0x30) != 0x00) 3083 goto next_desig; 3084 3085 switch (d[1] & 0xf) { 3086 case 0x1: 3087 /* T10 Vendor ID */ 3088 if (cur_id_size > d[3]) 3089 break; 3090 /* Prefer anything */ 3091 if (cur_id_type > 0x01 && cur_id_type != 0xff) 3092 break; 3093 cur_id_size = d[3]; 3094 if (cur_id_size + 4 > id_len) 3095 cur_id_size = id_len - 4; 3096 cur_id_str = d + 4; 3097 cur_id_type = d[1] & 0xf; 3098 id_size = snprintf(id, id_len, "t10.%*pE", 3099 cur_id_size, cur_id_str); 3100 break; 3101 case 0x2: 3102 /* EUI-64 */ 3103 if (cur_id_size > d[3]) 3104 break; 3105 /* Prefer NAA IEEE Registered Extended */ 3106 if (cur_id_type == 0x3 && 3107 cur_id_size == d[3]) 3108 break; 3109 cur_id_size = d[3]; 3110 cur_id_str = d + 4; 3111 cur_id_type = d[1] & 0xf; 3112 switch (cur_id_size) { 3113 case 8: 3114 id_size = snprintf(id, id_len, 3115 "eui.%8phN", 3116 cur_id_str); 3117 break; 3118 case 12: 3119 id_size = snprintf(id, id_len, 3120 "eui.%12phN", 3121 cur_id_str); 3122 break; 3123 case 16: 3124 id_size = snprintf(id, id_len, 3125 "eui.%16phN", 3126 cur_id_str); 3127 break; 3128 default: 3129 cur_id_size = 0; 3130 break; 3131 } 3132 break; 3133 case 0x3: 3134 /* NAA */ 3135 if (cur_id_size > d[3]) 3136 break; 3137 cur_id_size = d[3]; 3138 cur_id_str = d + 4; 3139 cur_id_type = d[1] & 0xf; 3140 switch (cur_id_size) { 3141 case 8: 3142 id_size = snprintf(id, id_len, 3143 "naa.%8phN", 3144 cur_id_str); 3145 break; 3146 case 16: 3147 id_size = snprintf(id, id_len, 3148 "naa.%16phN", 3149 cur_id_str); 3150 break; 3151 default: 3152 cur_id_size = 0; 3153 break; 3154 } 3155 break; 3156 case 0x8: 3157 /* SCSI name string */ 3158 if (cur_id_size + 4 > d[3]) 3159 break; 3160 /* Prefer others for truncated descriptor */ 3161 if (cur_id_size && d[3] > id_len) 3162 break; 3163 cur_id_size = id_size = d[3]; 3164 cur_id_str = d + 4; 3165 cur_id_type = d[1] & 0xf; 3166 if (cur_id_size >= id_len) 3167 cur_id_size = id_len - 1; 3168 memcpy(id, cur_id_str, cur_id_size); 3169 /* Decrease priority for truncated descriptor */ 3170 if (cur_id_size != id_size) 3171 cur_id_size = 6; 3172 break; 3173 default: 3174 break; 3175 } 3176 next_desig: 3177 d += d[3] + 4; 3178 } 3179 rcu_read_unlock(); 3180 3181 return id_size; 3182 } 3183 EXPORT_SYMBOL(scsi_vpd_lun_id); 3184 3185 /* 3186 * scsi_vpd_tpg_id - return a target port group identifier 3187 * @sdev: SCSI device 3188 * 3189 * Returns the Target Port Group identifier from the information 3190 * froom VPD page 0x83 of the device. 3191 * 3192 * Returns the identifier or error on failure. 3193 */ 3194 int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id) 3195 { 3196 unsigned char *d; 3197 unsigned char __rcu *vpd_pg83; 3198 int group_id = -EAGAIN, rel_port = -1; 3199 3200 rcu_read_lock(); 3201 vpd_pg83 = rcu_dereference(sdev->vpd_pg83); 3202 if (!vpd_pg83) { 3203 rcu_read_unlock(); 3204 return -ENXIO; 3205 } 3206 3207 d = sdev->vpd_pg83 + 4; 3208 while (d < sdev->vpd_pg83 + sdev->vpd_pg83_len) { 3209 switch (d[1] & 0xf) { 3210 case 0x4: 3211 /* Relative target port */ 3212 rel_port = get_unaligned_be16(&d[6]); 3213 break; 3214 case 0x5: 3215 /* Target port group */ 3216 group_id = get_unaligned_be16(&d[6]); 3217 break; 3218 default: 3219 break; 3220 } 3221 d += d[3] + 4; 3222 } 3223 rcu_read_unlock(); 3224 3225 if (group_id >= 0 && rel_id && rel_port != -1) 3226 *rel_id = rel_port; 3227 3228 return group_id; 3229 } 3230 EXPORT_SYMBOL(scsi_vpd_tpg_id); 3231