0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/bio.h>
0013 #include <linux/bitops.h>
0014 #include <linux/blkdev.h>
0015 #include <linux/completion.h>
0016 #include <linux/kernel.h>
0017 #include <linux/export.h>
0018 #include <linux/init.h>
0019 #include <linux/pci.h>
0020 #include <linux/delay.h>
0021 #include <linux/hardirq.h>
0022 #include <linux/scatterlist.h>
0023 #include <linux/blk-mq.h>
0024 #include <linux/blk-integrity.h>
0025 #include <linux/ratelimit.h>
0026 #include <asm/unaligned.h>
0027
0028 #include <scsi/scsi.h>
0029 #include <scsi/scsi_cmnd.h>
0030 #include <scsi/scsi_dbg.h>
0031 #include <scsi/scsi_device.h>
0032 #include <scsi/scsi_driver.h>
0033 #include <scsi/scsi_eh.h>
0034 #include <scsi/scsi_host.h>
0035 #include <scsi/scsi_transport.h> /* __scsi_init_queue() */
0036 #include <scsi/scsi_dh.h>
0037
0038 #include <trace/events/scsi.h>
0039
0040 #include "scsi_debugfs.h"
0041 #include "scsi_priv.h"
0042 #include "scsi_logging.h"
0043
0044
0045
0046
0047
0048 #ifdef CONFIG_ARCH_NO_SG_CHAIN
0049 #define SCSI_INLINE_PROT_SG_CNT 0
0050 #define SCSI_INLINE_SG_CNT 0
0051 #else
0052 #define SCSI_INLINE_PROT_SG_CNT 1
0053 #define SCSI_INLINE_SG_CNT 2
0054 #endif
0055
0056 static struct kmem_cache *scsi_sense_cache;
0057 static DEFINE_MUTEX(scsi_sense_cache_mutex);
0058
0059 static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd);
0060
0061 int scsi_init_sense_cache(struct Scsi_Host *shost)
0062 {
0063 int ret = 0;
0064
0065 mutex_lock(&scsi_sense_cache_mutex);
0066 if (!scsi_sense_cache) {
0067 scsi_sense_cache =
0068 kmem_cache_create_usercopy("scsi_sense_cache",
0069 SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN,
0070 0, SCSI_SENSE_BUFFERSIZE, NULL);
0071 if (!scsi_sense_cache)
0072 ret = -ENOMEM;
0073 }
0074 mutex_unlock(&scsi_sense_cache_mutex);
0075 return ret;
0076 }
0077
0078 static void
0079 scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
0080 {
0081 struct Scsi_Host *host = cmd->device->host;
0082 struct scsi_device *device = cmd->device;
0083 struct scsi_target *starget = scsi_target(device);
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 switch (reason) {
0099 case SCSI_MLQUEUE_HOST_BUSY:
0100 atomic_set(&host->host_blocked, host->max_host_blocked);
0101 break;
0102 case SCSI_MLQUEUE_DEVICE_BUSY:
0103 case SCSI_MLQUEUE_EH_RETRY:
0104 atomic_set(&device->device_blocked,
0105 device->max_device_blocked);
0106 break;
0107 case SCSI_MLQUEUE_TARGET_BUSY:
0108 atomic_set(&starget->target_blocked,
0109 starget->max_target_blocked);
0110 break;
0111 }
0112 }
0113
0114 static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd, unsigned long msecs)
0115 {
0116 struct request *rq = scsi_cmd_to_rq(cmd);
0117
0118 if (rq->rq_flags & RQF_DONTPREP) {
0119 rq->rq_flags &= ~RQF_DONTPREP;
0120 scsi_mq_uninit_cmd(cmd);
0121 } else {
0122 WARN_ON_ONCE(true);
0123 }
0124
0125 if (msecs) {
0126 blk_mq_requeue_request(rq, false);
0127 blk_mq_delay_kick_requeue_list(rq->q, msecs);
0128 } else
0129 blk_mq_requeue_request(rq, true);
0130 }
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144 static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, bool unbusy)
0145 {
0146 struct scsi_device *device = cmd->device;
0147
0148 SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
0149 "Inserting command %p into mlqueue\n", cmd));
0150
0151 scsi_set_blocked(cmd, reason);
0152
0153
0154
0155
0156
0157 if (unbusy)
0158 scsi_device_unbusy(device, cmd);
0159
0160
0161
0162
0163
0164
0165
0166 cmd->result = 0;
0167
0168 blk_mq_requeue_request(scsi_cmd_to_rq(cmd), true);
0169 }
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183 void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
0184 {
0185 __scsi_queue_insert(cmd, reason, true);
0186 }
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207 int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
0208 int data_direction, void *buffer, unsigned bufflen,
0209 unsigned char *sense, struct scsi_sense_hdr *sshdr,
0210 int timeout, int retries, blk_opf_t flags,
0211 req_flags_t rq_flags, int *resid)
0212 {
0213 struct request *req;
0214 struct scsi_cmnd *scmd;
0215 int ret;
0216
0217 req = scsi_alloc_request(sdev->request_queue,
0218 data_direction == DMA_TO_DEVICE ?
0219 REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
0220 rq_flags & RQF_PM ? BLK_MQ_REQ_PM : 0);
0221 if (IS_ERR(req))
0222 return PTR_ERR(req);
0223
0224 if (bufflen) {
0225 ret = blk_rq_map_kern(sdev->request_queue, req,
0226 buffer, bufflen, GFP_NOIO);
0227 if (ret)
0228 goto out;
0229 }
0230 scmd = blk_mq_rq_to_pdu(req);
0231 scmd->cmd_len = COMMAND_SIZE(cmd[0]);
0232 memcpy(scmd->cmnd, cmd, scmd->cmd_len);
0233 scmd->allowed = retries;
0234 req->timeout = timeout;
0235 req->cmd_flags |= flags;
0236 req->rq_flags |= rq_flags | RQF_QUIET;
0237
0238
0239
0240
0241 blk_execute_rq(req, true);
0242
0243
0244
0245
0246
0247
0248
0249 if (unlikely(scmd->resid_len > 0 && scmd->resid_len <= bufflen))
0250 memset(buffer + bufflen - scmd->resid_len, 0, scmd->resid_len);
0251
0252 if (resid)
0253 *resid = scmd->resid_len;
0254 if (sense && scmd->sense_len)
0255 memcpy(sense, scmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
0256 if (sshdr)
0257 scsi_normalize_sense(scmd->sense_buffer, scmd->sense_len,
0258 sshdr);
0259 ret = scmd->result;
0260 out:
0261 blk_mq_free_request(req);
0262
0263 return ret;
0264 }
0265 EXPORT_SYMBOL(__scsi_execute);
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 static void scsi_dec_host_busy(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
0277 {
0278 unsigned long flags;
0279
0280 rcu_read_lock();
0281 __clear_bit(SCMD_STATE_INFLIGHT, &cmd->state);
0282 if (unlikely(scsi_host_in_recovery(shost))) {
0283 spin_lock_irqsave(shost->host_lock, flags);
0284 if (shost->host_failed || shost->host_eh_scheduled)
0285 scsi_eh_wakeup(shost);
0286 spin_unlock_irqrestore(shost->host_lock, flags);
0287 }
0288 rcu_read_unlock();
0289 }
0290
0291 void scsi_device_unbusy(struct scsi_device *sdev, struct scsi_cmnd *cmd)
0292 {
0293 struct Scsi_Host *shost = sdev->host;
0294 struct scsi_target *starget = scsi_target(sdev);
0295
0296 scsi_dec_host_busy(shost, cmd);
0297
0298 if (starget->can_queue > 0)
0299 atomic_dec(&starget->target_busy);
0300
0301 sbitmap_put(&sdev->budget_map, cmd->budget_token);
0302 cmd->budget_token = -1;
0303 }
0304
0305 static void scsi_kick_queue(struct request_queue *q)
0306 {
0307 blk_mq_run_hw_queues(q, false);
0308 }
0309
0310
0311
0312
0313
0314
0315
0316
0317 static void scsi_single_lun_run(struct scsi_device *current_sdev)
0318 {
0319 struct Scsi_Host *shost = current_sdev->host;
0320 struct scsi_device *sdev, *tmp;
0321 struct scsi_target *starget = scsi_target(current_sdev);
0322 unsigned long flags;
0323
0324 spin_lock_irqsave(shost->host_lock, flags);
0325 starget->starget_sdev_user = NULL;
0326 spin_unlock_irqrestore(shost->host_lock, flags);
0327
0328
0329
0330
0331
0332
0333
0334 scsi_kick_queue(current_sdev->request_queue);
0335
0336 spin_lock_irqsave(shost->host_lock, flags);
0337 if (starget->starget_sdev_user)
0338 goto out;
0339 list_for_each_entry_safe(sdev, tmp, &starget->devices,
0340 same_target_siblings) {
0341 if (sdev == current_sdev)
0342 continue;
0343 if (scsi_device_get(sdev))
0344 continue;
0345
0346 spin_unlock_irqrestore(shost->host_lock, flags);
0347 scsi_kick_queue(sdev->request_queue);
0348 spin_lock_irqsave(shost->host_lock, flags);
0349
0350 scsi_device_put(sdev);
0351 }
0352 out:
0353 spin_unlock_irqrestore(shost->host_lock, flags);
0354 }
0355
0356 static inline bool scsi_device_is_busy(struct scsi_device *sdev)
0357 {
0358 if (scsi_device_busy(sdev) >= sdev->queue_depth)
0359 return true;
0360 if (atomic_read(&sdev->device_blocked) > 0)
0361 return true;
0362 return false;
0363 }
0364
0365 static inline bool scsi_target_is_busy(struct scsi_target *starget)
0366 {
0367 if (starget->can_queue > 0) {
0368 if (atomic_read(&starget->target_busy) >= starget->can_queue)
0369 return true;
0370 if (atomic_read(&starget->target_blocked) > 0)
0371 return true;
0372 }
0373 return false;
0374 }
0375
0376 static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
0377 {
0378 if (atomic_read(&shost->host_blocked) > 0)
0379 return true;
0380 if (shost->host_self_blocked)
0381 return true;
0382 return false;
0383 }
0384
0385 static void scsi_starved_list_run(struct Scsi_Host *shost)
0386 {
0387 LIST_HEAD(starved_list);
0388 struct scsi_device *sdev;
0389 unsigned long flags;
0390
0391 spin_lock_irqsave(shost->host_lock, flags);
0392 list_splice_init(&shost->starved_list, &starved_list);
0393
0394 while (!list_empty(&starved_list)) {
0395 struct request_queue *slq;
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407 if (scsi_host_is_busy(shost))
0408 break;
0409
0410 sdev = list_entry(starved_list.next,
0411 struct scsi_device, starved_entry);
0412 list_del_init(&sdev->starved_entry);
0413 if (scsi_target_is_busy(scsi_target(sdev))) {
0414 list_move_tail(&sdev->starved_entry,
0415 &shost->starved_list);
0416 continue;
0417 }
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429 slq = sdev->request_queue;
0430 if (!blk_get_queue(slq))
0431 continue;
0432 spin_unlock_irqrestore(shost->host_lock, flags);
0433
0434 scsi_kick_queue(slq);
0435 blk_put_queue(slq);
0436
0437 spin_lock_irqsave(shost->host_lock, flags);
0438 }
0439
0440 list_splice(&starved_list, &shost->starved_list);
0441 spin_unlock_irqrestore(shost->host_lock, flags);
0442 }
0443
0444
0445
0446
0447
0448
0449
0450 static void scsi_run_queue(struct request_queue *q)
0451 {
0452 struct scsi_device *sdev = q->queuedata;
0453
0454 if (scsi_target(sdev)->single_lun)
0455 scsi_single_lun_run(sdev);
0456 if (!list_empty(&sdev->host->starved_list))
0457 scsi_starved_list_run(sdev->host);
0458
0459 blk_mq_run_hw_queues(q, false);
0460 }
0461
0462 void scsi_requeue_run_queue(struct work_struct *work)
0463 {
0464 struct scsi_device *sdev;
0465 struct request_queue *q;
0466
0467 sdev = container_of(work, struct scsi_device, requeue_work);
0468 q = sdev->request_queue;
0469 scsi_run_queue(q);
0470 }
0471
0472 void scsi_run_host_queues(struct Scsi_Host *shost)
0473 {
0474 struct scsi_device *sdev;
0475
0476 shost_for_each_device(sdev, shost)
0477 scsi_run_queue(sdev->request_queue);
0478 }
0479
0480 static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
0481 {
0482 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(cmd))) {
0483 struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
0484
0485 if (drv->uninit_command)
0486 drv->uninit_command(cmd);
0487 }
0488 }
0489
0490 void scsi_free_sgtables(struct scsi_cmnd *cmd)
0491 {
0492 if (cmd->sdb.table.nents)
0493 sg_free_table_chained(&cmd->sdb.table,
0494 SCSI_INLINE_SG_CNT);
0495 if (scsi_prot_sg_count(cmd))
0496 sg_free_table_chained(&cmd->prot_sdb->table,
0497 SCSI_INLINE_PROT_SG_CNT);
0498 }
0499 EXPORT_SYMBOL_GPL(scsi_free_sgtables);
0500
0501 static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
0502 {
0503 scsi_free_sgtables(cmd);
0504 scsi_uninit_cmd(cmd);
0505 }
0506
0507 static void scsi_run_queue_async(struct scsi_device *sdev)
0508 {
0509 if (scsi_target(sdev)->single_lun ||
0510 !list_empty(&sdev->host->starved_list)) {
0511 kblockd_schedule_work(&sdev->requeue_work);
0512 } else {
0513
0514
0515
0516
0517
0518 int old = atomic_read(&sdev->restarts);
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528 if (old && atomic_cmpxchg(&sdev->restarts, old, 0) == old)
0529 blk_mq_run_hw_queues(sdev->request_queue, true);
0530 }
0531 }
0532
0533
0534 static bool scsi_end_request(struct request *req, blk_status_t error,
0535 unsigned int bytes)
0536 {
0537 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
0538 struct scsi_device *sdev = cmd->device;
0539 struct request_queue *q = sdev->request_queue;
0540
0541 if (blk_update_request(req, error, bytes))
0542 return true;
0543
0544
0545 if (blk_queue_add_random(q))
0546 add_disk_randomness(req->q->disk);
0547
0548 if (!blk_rq_is_passthrough(req)) {
0549 WARN_ON_ONCE(!(cmd->flags & SCMD_INITIALIZED));
0550 cmd->flags &= ~SCMD_INITIALIZED;
0551 }
0552
0553
0554
0555
0556
0557
0558
0559 destroy_rcu_head(&cmd->rcu);
0560
0561
0562
0563
0564
0565
0566
0567
0568 scsi_mq_uninit_cmd(cmd);
0569
0570
0571
0572
0573
0574 percpu_ref_get(&q->q_usage_counter);
0575
0576 __blk_mq_end_request(req, error);
0577
0578 scsi_run_queue_async(sdev);
0579
0580 percpu_ref_put(&q->q_usage_counter);
0581 return false;
0582 }
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592 static blk_status_t scsi_result_to_blk_status(struct scsi_cmnd *cmd, int result)
0593 {
0594 switch (host_byte(result)) {
0595 case DID_OK:
0596 if (scsi_status_is_good(result))
0597 return BLK_STS_OK;
0598 return BLK_STS_IOERR;
0599 case DID_TRANSPORT_FAILFAST:
0600 case DID_TRANSPORT_MARGINAL:
0601 return BLK_STS_TRANSPORT;
0602 case DID_TARGET_FAILURE:
0603 set_host_byte(cmd, DID_OK);
0604 return BLK_STS_TARGET;
0605 case DID_NEXUS_FAILURE:
0606 set_host_byte(cmd, DID_OK);
0607 return BLK_STS_NEXUS;
0608 case DID_ALLOC_FAILURE:
0609 set_host_byte(cmd, DID_OK);
0610 return BLK_STS_NOSPC;
0611 case DID_MEDIUM_ERROR:
0612 set_host_byte(cmd, DID_OK);
0613 return BLK_STS_MEDIUM;
0614 default:
0615 return BLK_STS_IOERR;
0616 }
0617 }
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632 static unsigned int scsi_rq_err_bytes(const struct request *rq)
0633 {
0634 blk_opf_t ff = rq->cmd_flags & REQ_FAILFAST_MASK;
0635 unsigned int bytes = 0;
0636 struct bio *bio;
0637
0638 if (!(rq->rq_flags & RQF_MIXED_MERGE))
0639 return blk_rq_bytes(rq);
0640
0641
0642
0643
0644
0645
0646
0647
0648 for (bio = rq->bio; bio; bio = bio->bi_next) {
0649 if ((bio->bi_opf & ff) != ff)
0650 break;
0651 bytes += bio->bi_iter.bi_size;
0652 }
0653
0654
0655 BUG_ON(blk_rq_bytes(rq) && !bytes);
0656 return bytes;
0657 }
0658
0659 static bool scsi_cmd_runtime_exceeced(struct scsi_cmnd *cmd)
0660 {
0661 struct request *req = scsi_cmd_to_rq(cmd);
0662 unsigned long wait_for;
0663
0664 if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
0665 return false;
0666
0667 wait_for = (cmd->allowed + 1) * req->timeout;
0668 if (time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
0669 scmd_printk(KERN_ERR, cmd, "timing out command, waited %lus\n",
0670 wait_for/HZ);
0671 return true;
0672 }
0673 return false;
0674 }
0675
0676
0677
0678
0679
0680
0681
0682 #define ALUA_TRANSITION_REPREP_DELAY 1000
0683
0684
0685 static void scsi_io_completion_action(struct scsi_cmnd *cmd, int result)
0686 {
0687 struct request *req = scsi_cmd_to_rq(cmd);
0688 int level = 0;
0689 enum {ACTION_FAIL, ACTION_REPREP, ACTION_DELAYED_REPREP,
0690 ACTION_RETRY, ACTION_DELAYED_RETRY} action;
0691 struct scsi_sense_hdr sshdr;
0692 bool sense_valid;
0693 bool sense_current = true;
0694 blk_status_t blk_stat;
0695
0696 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
0697 if (sense_valid)
0698 sense_current = !scsi_sense_is_deferred(&sshdr);
0699
0700 blk_stat = scsi_result_to_blk_status(cmd, result);
0701
0702 if (host_byte(result) == DID_RESET) {
0703
0704
0705
0706
0707 action = ACTION_RETRY;
0708 } else if (sense_valid && sense_current) {
0709 switch (sshdr.sense_key) {
0710 case UNIT_ATTENTION:
0711 if (cmd->device->removable) {
0712
0713
0714
0715 cmd->device->changed = 1;
0716 action = ACTION_FAIL;
0717 } else {
0718
0719
0720
0721
0722
0723 action = ACTION_RETRY;
0724 }
0725 break;
0726 case ILLEGAL_REQUEST:
0727
0728
0729
0730
0731
0732
0733
0734
0735 if ((cmd->device->use_10_for_rw &&
0736 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
0737 (cmd->cmnd[0] == READ_10 ||
0738 cmd->cmnd[0] == WRITE_10)) {
0739
0740 cmd->device->use_10_for_rw = 0;
0741 action = ACTION_REPREP;
0742 } else if (sshdr.asc == 0x10) {
0743 action = ACTION_FAIL;
0744 blk_stat = BLK_STS_PROTECTION;
0745
0746 } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
0747 action = ACTION_FAIL;
0748 blk_stat = BLK_STS_TARGET;
0749 } else
0750 action = ACTION_FAIL;
0751 break;
0752 case ABORTED_COMMAND:
0753 action = ACTION_FAIL;
0754 if (sshdr.asc == 0x10)
0755 blk_stat = BLK_STS_PROTECTION;
0756 break;
0757 case NOT_READY:
0758
0759
0760
0761 if (sshdr.asc == 0x04) {
0762 switch (sshdr.ascq) {
0763 case 0x01:
0764 case 0x04:
0765 case 0x05:
0766 case 0x06:
0767 case 0x07:
0768 case 0x08:
0769 case 0x09:
0770 case 0x11:
0771 case 0x14:
0772 case 0x1a:
0773 case 0x1b:
0774 case 0x1d:
0775 case 0x24:
0776 action = ACTION_DELAYED_RETRY;
0777 break;
0778 case 0x0a:
0779 action = ACTION_DELAYED_REPREP;
0780 break;
0781 default:
0782 action = ACTION_FAIL;
0783 break;
0784 }
0785 } else
0786 action = ACTION_FAIL;
0787 break;
0788 case VOLUME_OVERFLOW:
0789
0790 action = ACTION_FAIL;
0791 break;
0792 case DATA_PROTECT:
0793 action = ACTION_FAIL;
0794 if ((sshdr.asc == 0x0C && sshdr.ascq == 0x12) ||
0795 (sshdr.asc == 0x55 &&
0796 (sshdr.ascq == 0x0E || sshdr.ascq == 0x0F))) {
0797
0798 blk_stat = BLK_STS_ZONE_OPEN_RESOURCE;
0799 }
0800 break;
0801 default:
0802 action = ACTION_FAIL;
0803 break;
0804 }
0805 } else
0806 action = ACTION_FAIL;
0807
0808 if (action != ACTION_FAIL && scsi_cmd_runtime_exceeced(cmd))
0809 action = ACTION_FAIL;
0810
0811 switch (action) {
0812 case ACTION_FAIL:
0813
0814 if (!(req->rq_flags & RQF_QUIET)) {
0815 static DEFINE_RATELIMIT_STATE(_rs,
0816 DEFAULT_RATELIMIT_INTERVAL,
0817 DEFAULT_RATELIMIT_BURST);
0818
0819 if (unlikely(scsi_logging_level))
0820 level =
0821 SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
0822 SCSI_LOG_MLCOMPLETE_BITS);
0823
0824
0825
0826
0827
0828 if (!level && __ratelimit(&_rs)) {
0829 scsi_print_result(cmd, NULL, FAILED);
0830 if (sense_valid)
0831 scsi_print_sense(cmd);
0832 scsi_print_command(cmd);
0833 }
0834 }
0835 if (!scsi_end_request(req, blk_stat, scsi_rq_err_bytes(req)))
0836 return;
0837 fallthrough;
0838 case ACTION_REPREP:
0839 scsi_mq_requeue_cmd(cmd, 0);
0840 break;
0841 case ACTION_DELAYED_REPREP:
0842 scsi_mq_requeue_cmd(cmd, ALUA_TRANSITION_REPREP_DELAY);
0843 break;
0844 case ACTION_RETRY:
0845
0846 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, false);
0847 break;
0848 case ACTION_DELAYED_RETRY:
0849
0850 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, false);
0851 break;
0852 }
0853 }
0854
0855
0856
0857
0858
0859
0860 static int scsi_io_completion_nz_result(struct scsi_cmnd *cmd, int result,
0861 blk_status_t *blk_statp)
0862 {
0863 bool sense_valid;
0864 bool sense_current = true;
0865 struct request *req = scsi_cmd_to_rq(cmd);
0866 struct scsi_sense_hdr sshdr;
0867
0868 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
0869 if (sense_valid)
0870 sense_current = !scsi_sense_is_deferred(&sshdr);
0871
0872 if (blk_rq_is_passthrough(req)) {
0873 if (sense_valid) {
0874
0875
0876
0877 cmd->sense_len = min(8 + cmd->sense_buffer[7],
0878 SCSI_SENSE_BUFFERSIZE);
0879 }
0880 if (sense_current)
0881 *blk_statp = scsi_result_to_blk_status(cmd, result);
0882 } else if (blk_rq_bytes(req) == 0 && sense_current) {
0883
0884
0885
0886
0887
0888 *blk_statp = scsi_result_to_blk_status(cmd, result);
0889 }
0890
0891
0892
0893
0894
0895
0896 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
0897 bool do_print = true;
0898
0899
0900
0901
0902
0903 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
0904 do_print = false;
0905 else if (req->rq_flags & RQF_QUIET)
0906 do_print = false;
0907 if (do_print)
0908 scsi_print_sense(cmd);
0909 result = 0;
0910
0911 *blk_statp = BLK_STS_OK;
0912 }
0913
0914
0915
0916
0917
0918
0919
0920 if ((result & 0xff) && scsi_status_is_good(result)) {
0921 result = 0;
0922 *blk_statp = BLK_STS_OK;
0923 }
0924 return result;
0925 }
0926
0927
0928
0929
0930
0931
0932
0933
0934
0935
0936
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946
0947
0948
0949 void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
0950 {
0951 int result = cmd->result;
0952 struct request *req = scsi_cmd_to_rq(cmd);
0953 blk_status_t blk_stat = BLK_STS_OK;
0954
0955 if (unlikely(result))
0956 result = scsi_io_completion_nz_result(cmd, result, &blk_stat);
0957
0958
0959
0960
0961
0962 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
0963 "%u sectors total, %d bytes done.\n",
0964 blk_rq_sectors(req), good_bytes));
0965
0966
0967
0968
0969
0970 if (likely(blk_rq_bytes(req) > 0 || blk_stat == BLK_STS_OK)) {
0971 if (likely(!scsi_end_request(req, blk_stat, good_bytes)))
0972 return;
0973 }
0974
0975
0976 if (unlikely(blk_stat && scsi_noretry_cmd(cmd))) {
0977 if (scsi_end_request(req, blk_stat, blk_rq_bytes(req)))
0978 WARN_ONCE(true,
0979 "Bytes remaining after failed, no-retry command");
0980 return;
0981 }
0982
0983
0984
0985
0986
0987 if (likely(result == 0))
0988 scsi_mq_requeue_cmd(cmd, 0);
0989 else
0990 scsi_io_completion_action(cmd, result);
0991 }
0992
0993 static inline bool scsi_cmd_needs_dma_drain(struct scsi_device *sdev,
0994 struct request *rq)
0995 {
0996 return sdev->dma_drain_len && blk_rq_is_passthrough(rq) &&
0997 !op_is_write(req_op(rq)) &&
0998 sdev->host->hostt->dma_need_drain(rq);
0999 }
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013 blk_status_t scsi_alloc_sgtables(struct scsi_cmnd *cmd)
1014 {
1015 struct scsi_device *sdev = cmd->device;
1016 struct request *rq = scsi_cmd_to_rq(cmd);
1017 unsigned short nr_segs = blk_rq_nr_phys_segments(rq);
1018 struct scatterlist *last_sg = NULL;
1019 blk_status_t ret;
1020 bool need_drain = scsi_cmd_needs_dma_drain(sdev, rq);
1021 int count;
1022
1023 if (WARN_ON_ONCE(!nr_segs))
1024 return BLK_STS_IOERR;
1025
1026
1027
1028
1029
1030 if (need_drain)
1031 nr_segs++;
1032
1033
1034
1035
1036 if (unlikely(sg_alloc_table_chained(&cmd->sdb.table, nr_segs,
1037 cmd->sdb.table.sgl, SCSI_INLINE_SG_CNT)))
1038 return BLK_STS_RESOURCE;
1039
1040
1041
1042
1043
1044 count = __blk_rq_map_sg(rq->q, rq, cmd->sdb.table.sgl, &last_sg);
1045
1046 if (blk_rq_bytes(rq) & rq->q->dma_pad_mask) {
1047 unsigned int pad_len =
1048 (rq->q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
1049
1050 last_sg->length += pad_len;
1051 cmd->extra_len += pad_len;
1052 }
1053
1054 if (need_drain) {
1055 sg_unmark_end(last_sg);
1056 last_sg = sg_next(last_sg);
1057 sg_set_buf(last_sg, sdev->dma_drain_buf, sdev->dma_drain_len);
1058 sg_mark_end(last_sg);
1059
1060 cmd->extra_len += sdev->dma_drain_len;
1061 count++;
1062 }
1063
1064 BUG_ON(count > cmd->sdb.table.nents);
1065 cmd->sdb.table.nents = count;
1066 cmd->sdb.length = blk_rq_payload_bytes(rq);
1067
1068 if (blk_integrity_rq(rq)) {
1069 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1070 int ivecs;
1071
1072 if (WARN_ON_ONCE(!prot_sdb)) {
1073
1074
1075
1076
1077
1078 ret = BLK_STS_IOERR;
1079 goto out_free_sgtables;
1080 }
1081
1082 ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
1083
1084 if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
1085 prot_sdb->table.sgl,
1086 SCSI_INLINE_PROT_SG_CNT)) {
1087 ret = BLK_STS_RESOURCE;
1088 goto out_free_sgtables;
1089 }
1090
1091 count = blk_rq_map_integrity_sg(rq->q, rq->bio,
1092 prot_sdb->table.sgl);
1093 BUG_ON(count > ivecs);
1094 BUG_ON(count > queue_max_integrity_segments(rq->q));
1095
1096 cmd->prot_sdb = prot_sdb;
1097 cmd->prot_sdb->table.nents = count;
1098 }
1099
1100 return BLK_STS_OK;
1101 out_free_sgtables:
1102 scsi_free_sgtables(cmd);
1103 return ret;
1104 }
1105 EXPORT_SYMBOL(scsi_alloc_sgtables);
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115 static void scsi_initialize_rq(struct request *rq)
1116 {
1117 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1118
1119 memset(cmd->cmnd, 0, sizeof(cmd->cmnd));
1120 cmd->cmd_len = MAX_COMMAND_SIZE;
1121 cmd->sense_len = 0;
1122 init_rcu_head(&cmd->rcu);
1123 cmd->jiffies_at_alloc = jiffies;
1124 cmd->retries = 0;
1125 }
1126
1127 struct request *scsi_alloc_request(struct request_queue *q, blk_opf_t opf,
1128 blk_mq_req_flags_t flags)
1129 {
1130 struct request *rq;
1131
1132 rq = blk_mq_alloc_request(q, opf, flags);
1133 if (!IS_ERR(rq))
1134 scsi_initialize_rq(rq);
1135 return rq;
1136 }
1137 EXPORT_SYMBOL_GPL(scsi_alloc_request);
1138
1139
1140
1141
1142
1143 static void scsi_cleanup_rq(struct request *rq)
1144 {
1145 if (rq->rq_flags & RQF_DONTPREP) {
1146 scsi_mq_uninit_cmd(blk_mq_rq_to_pdu(rq));
1147 rq->rq_flags &= ~RQF_DONTPREP;
1148 }
1149 }
1150
1151
1152 void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd)
1153 {
1154 struct request *rq = scsi_cmd_to_rq(cmd);
1155
1156 if (!blk_rq_is_passthrough(rq) && !(cmd->flags & SCMD_INITIALIZED)) {
1157 cmd->flags |= SCMD_INITIALIZED;
1158 scsi_initialize_rq(rq);
1159 }
1160
1161 cmd->device = dev;
1162 INIT_LIST_HEAD(&cmd->eh_entry);
1163 INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
1164 }
1165
1166 static blk_status_t scsi_setup_scsi_cmnd(struct scsi_device *sdev,
1167 struct request *req)
1168 {
1169 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1170
1171
1172
1173
1174
1175
1176
1177 if (req->bio) {
1178 blk_status_t ret = scsi_alloc_sgtables(cmd);
1179 if (unlikely(ret != BLK_STS_OK))
1180 return ret;
1181 } else {
1182 BUG_ON(blk_rq_bytes(req));
1183
1184 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
1185 }
1186
1187 cmd->transfersize = blk_rq_bytes(req);
1188 return BLK_STS_OK;
1189 }
1190
1191 static blk_status_t
1192 scsi_device_state_check(struct scsi_device *sdev, struct request *req)
1193 {
1194 switch (sdev->sdev_state) {
1195 case SDEV_CREATED:
1196 return BLK_STS_OK;
1197 case SDEV_OFFLINE:
1198 case SDEV_TRANSPORT_OFFLINE:
1199
1200
1201
1202
1203
1204 if (!sdev->offline_already) {
1205 sdev->offline_already = true;
1206 sdev_printk(KERN_ERR, sdev,
1207 "rejecting I/O to offline device\n");
1208 }
1209 return BLK_STS_IOERR;
1210 case SDEV_DEL:
1211
1212
1213
1214
1215 sdev_printk(KERN_ERR, sdev,
1216 "rejecting I/O to dead device\n");
1217 return BLK_STS_IOERR;
1218 case SDEV_BLOCK:
1219 case SDEV_CREATED_BLOCK:
1220 return BLK_STS_RESOURCE;
1221 case SDEV_QUIESCE:
1222
1223
1224
1225
1226 if (req && WARN_ON_ONCE(!(req->rq_flags & RQF_PM)))
1227 return BLK_STS_RESOURCE;
1228 return BLK_STS_OK;
1229 default:
1230
1231
1232
1233
1234 if (req && !(req->rq_flags & RQF_PM))
1235 return BLK_STS_OFFLINE;
1236 return BLK_STS_OK;
1237 }
1238 }
1239
1240
1241
1242
1243
1244 static inline int scsi_dev_queue_ready(struct request_queue *q,
1245 struct scsi_device *sdev)
1246 {
1247 int token;
1248
1249 token = sbitmap_get(&sdev->budget_map);
1250 if (atomic_read(&sdev->device_blocked)) {
1251 if (token < 0)
1252 goto out;
1253
1254 if (scsi_device_busy(sdev) > 1)
1255 goto out_dec;
1256
1257
1258
1259
1260 if (atomic_dec_return(&sdev->device_blocked) > 0)
1261 goto out_dec;
1262 SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
1263 "unblocking device at zero depth\n"));
1264 }
1265
1266 return token;
1267 out_dec:
1268 if (token >= 0)
1269 sbitmap_put(&sdev->budget_map, token);
1270 out:
1271 return -1;
1272 }
1273
1274
1275
1276
1277
1278 static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1279 struct scsi_device *sdev)
1280 {
1281 struct scsi_target *starget = scsi_target(sdev);
1282 unsigned int busy;
1283
1284 if (starget->single_lun) {
1285 spin_lock_irq(shost->host_lock);
1286 if (starget->starget_sdev_user &&
1287 starget->starget_sdev_user != sdev) {
1288 spin_unlock_irq(shost->host_lock);
1289 return 0;
1290 }
1291 starget->starget_sdev_user = sdev;
1292 spin_unlock_irq(shost->host_lock);
1293 }
1294
1295 if (starget->can_queue <= 0)
1296 return 1;
1297
1298 busy = atomic_inc_return(&starget->target_busy) - 1;
1299 if (atomic_read(&starget->target_blocked) > 0) {
1300 if (busy)
1301 goto starved;
1302
1303
1304
1305
1306 if (atomic_dec_return(&starget->target_blocked) > 0)
1307 goto out_dec;
1308
1309 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1310 "unblocking target at zero depth\n"));
1311 }
1312
1313 if (busy >= starget->can_queue)
1314 goto starved;
1315
1316 return 1;
1317
1318 starved:
1319 spin_lock_irq(shost->host_lock);
1320 list_move_tail(&sdev->starved_entry, &shost->starved_list);
1321 spin_unlock_irq(shost->host_lock);
1322 out_dec:
1323 if (starget->can_queue > 0)
1324 atomic_dec(&starget->target_busy);
1325 return 0;
1326 }
1327
1328
1329
1330
1331
1332
1333 static inline int scsi_host_queue_ready(struct request_queue *q,
1334 struct Scsi_Host *shost,
1335 struct scsi_device *sdev,
1336 struct scsi_cmnd *cmd)
1337 {
1338 if (scsi_host_in_recovery(shost))
1339 return 0;
1340
1341 if (atomic_read(&shost->host_blocked) > 0) {
1342 if (scsi_host_busy(shost) > 0)
1343 goto starved;
1344
1345
1346
1347
1348 if (atomic_dec_return(&shost->host_blocked) > 0)
1349 goto out_dec;
1350
1351 SCSI_LOG_MLQUEUE(3,
1352 shost_printk(KERN_INFO, shost,
1353 "unblocking host at zero depth\n"));
1354 }
1355
1356 if (shost->host_self_blocked)
1357 goto starved;
1358
1359
1360 if (!list_empty(&sdev->starved_entry)) {
1361 spin_lock_irq(shost->host_lock);
1362 if (!list_empty(&sdev->starved_entry))
1363 list_del_init(&sdev->starved_entry);
1364 spin_unlock_irq(shost->host_lock);
1365 }
1366
1367 __set_bit(SCMD_STATE_INFLIGHT, &cmd->state);
1368
1369 return 1;
1370
1371 starved:
1372 spin_lock_irq(shost->host_lock);
1373 if (list_empty(&sdev->starved_entry))
1374 list_add_tail(&sdev->starved_entry, &shost->starved_list);
1375 spin_unlock_irq(shost->host_lock);
1376 out_dec:
1377 scsi_dec_host_busy(shost, cmd);
1378 return 0;
1379 }
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393 static bool scsi_mq_lld_busy(struct request_queue *q)
1394 {
1395 struct scsi_device *sdev = q->queuedata;
1396 struct Scsi_Host *shost;
1397
1398 if (blk_queue_dying(q))
1399 return false;
1400
1401 shost = sdev->host;
1402
1403
1404
1405
1406
1407
1408
1409 if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
1410 return true;
1411
1412 return false;
1413 }
1414
1415
1416
1417
1418
1419 static void scsi_complete(struct request *rq)
1420 {
1421 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1422 enum scsi_disposition disposition;
1423
1424 INIT_LIST_HEAD(&cmd->eh_entry);
1425
1426 atomic_inc(&cmd->device->iodone_cnt);
1427 if (cmd->result)
1428 atomic_inc(&cmd->device->ioerr_cnt);
1429
1430 disposition = scsi_decide_disposition(cmd);
1431 if (disposition != SUCCESS && scsi_cmd_runtime_exceeced(cmd))
1432 disposition = SUCCESS;
1433
1434 scsi_log_completion(cmd, disposition);
1435
1436 switch (disposition) {
1437 case SUCCESS:
1438 scsi_finish_command(cmd);
1439 break;
1440 case NEEDS_RETRY:
1441 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
1442 break;
1443 case ADD_TO_MLQUEUE:
1444 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1445 break;
1446 default:
1447 scsi_eh_scmd_add(cmd);
1448 break;
1449 }
1450 }
1451
1452
1453
1454
1455
1456
1457
1458
1459 static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
1460 {
1461 struct Scsi_Host *host = cmd->device->host;
1462 int rtn = 0;
1463
1464 atomic_inc(&cmd->device->iorequest_cnt);
1465
1466
1467 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
1468
1469
1470
1471 cmd->result = DID_NO_CONNECT << 16;
1472 goto done;
1473 }
1474
1475
1476 if (unlikely(scsi_device_blocked(cmd->device))) {
1477
1478
1479
1480
1481
1482
1483
1484 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1485 "queuecommand : device blocked\n"));
1486 return SCSI_MLQUEUE_DEVICE_BUSY;
1487 }
1488
1489
1490 if (cmd->device->lun_in_cdb)
1491 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
1492 (cmd->device->lun << 5 & 0xe0);
1493
1494 scsi_log_send(cmd);
1495
1496
1497
1498
1499
1500 if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
1501 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1502 "queuecommand : command too long. "
1503 "cdb_size=%d host->max_cmd_len=%d\n",
1504 cmd->cmd_len, cmd->device->host->max_cmd_len));
1505 cmd->result = (DID_ABORT << 16);
1506 goto done;
1507 }
1508
1509 if (unlikely(host->shost_state == SHOST_DEL)) {
1510 cmd->result = (DID_NO_CONNECT << 16);
1511 goto done;
1512
1513 }
1514
1515 trace_scsi_dispatch_cmd_start(cmd);
1516 rtn = host->hostt->queuecommand(host, cmd);
1517 if (rtn) {
1518 trace_scsi_dispatch_cmd_error(cmd, rtn);
1519 if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
1520 rtn != SCSI_MLQUEUE_TARGET_BUSY)
1521 rtn = SCSI_MLQUEUE_HOST_BUSY;
1522
1523 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1524 "queuecommand : request rejected\n"));
1525 }
1526
1527 return rtn;
1528 done:
1529 scsi_done(cmd);
1530 return 0;
1531 }
1532
1533
1534 static unsigned int scsi_mq_inline_sgl_size(struct Scsi_Host *shost)
1535 {
1536 return min_t(unsigned int, shost->sg_tablesize, SCSI_INLINE_SG_CNT) *
1537 sizeof(struct scatterlist);
1538 }
1539
1540 static blk_status_t scsi_prepare_cmd(struct request *req)
1541 {
1542 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1543 struct scsi_device *sdev = req->q->queuedata;
1544 struct Scsi_Host *shost = sdev->host;
1545 bool in_flight = test_bit(SCMD_STATE_INFLIGHT, &cmd->state);
1546 struct scatterlist *sg;
1547
1548 scsi_init_command(sdev, cmd);
1549
1550 cmd->eh_eflags = 0;
1551 cmd->prot_type = 0;
1552 cmd->prot_flags = 0;
1553 cmd->submitter = 0;
1554 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
1555 cmd->underflow = 0;
1556 cmd->transfersize = 0;
1557 cmd->host_scribble = NULL;
1558 cmd->result = 0;
1559 cmd->extra_len = 0;
1560 cmd->state = 0;
1561 if (in_flight)
1562 __set_bit(SCMD_STATE_INFLIGHT, &cmd->state);
1563
1564
1565
1566
1567
1568 if (!shost->hostt->init_cmd_priv)
1569 memset(cmd + 1, 0, shost->hostt->cmd_size);
1570
1571 cmd->prot_op = SCSI_PROT_NORMAL;
1572 if (blk_rq_bytes(req))
1573 cmd->sc_data_direction = rq_dma_dir(req);
1574 else
1575 cmd->sc_data_direction = DMA_NONE;
1576
1577 sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
1578 cmd->sdb.table.sgl = sg;
1579
1580 if (scsi_host_get_prot(shost)) {
1581 memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
1582
1583 cmd->prot_sdb->table.sgl =
1584 (struct scatterlist *)(cmd->prot_sdb + 1);
1585 }
1586
1587
1588
1589
1590
1591 if (blk_rq_is_passthrough(req))
1592 return scsi_setup_scsi_cmnd(sdev, req);
1593
1594 if (sdev->handler && sdev->handler->prep_fn) {
1595 blk_status_t ret = sdev->handler->prep_fn(sdev, req);
1596
1597 if (ret != BLK_STS_OK)
1598 return ret;
1599 }
1600
1601
1602 cmd->allowed = 0;
1603 memset(cmd->cmnd, 0, sizeof(cmd->cmnd));
1604 return scsi_cmd_to_driver(cmd)->init_command(cmd);
1605 }
1606
1607 static void scsi_done_internal(struct scsi_cmnd *cmd, bool complete_directly)
1608 {
1609 struct request *req = scsi_cmd_to_rq(cmd);
1610
1611 switch (cmd->submitter) {
1612 case SUBMITTED_BY_BLOCK_LAYER:
1613 break;
1614 case SUBMITTED_BY_SCSI_ERROR_HANDLER:
1615 return scsi_eh_done(cmd);
1616 case SUBMITTED_BY_SCSI_RESET_IOCTL:
1617 return;
1618 }
1619
1620 if (unlikely(blk_should_fake_timeout(scsi_cmd_to_rq(cmd)->q)))
1621 return;
1622 if (unlikely(test_and_set_bit(SCMD_STATE_COMPLETE, &cmd->state)))
1623 return;
1624 trace_scsi_dispatch_cmd_done(cmd);
1625
1626 if (complete_directly)
1627 blk_mq_complete_request_direct(req, scsi_complete);
1628 else
1629 blk_mq_complete_request(req);
1630 }
1631
1632 void scsi_done(struct scsi_cmnd *cmd)
1633 {
1634 scsi_done_internal(cmd, false);
1635 }
1636 EXPORT_SYMBOL(scsi_done);
1637
1638 void scsi_done_direct(struct scsi_cmnd *cmd)
1639 {
1640 scsi_done_internal(cmd, true);
1641 }
1642 EXPORT_SYMBOL(scsi_done_direct);
1643
1644 static void scsi_mq_put_budget(struct request_queue *q, int budget_token)
1645 {
1646 struct scsi_device *sdev = q->queuedata;
1647
1648 sbitmap_put(&sdev->budget_map, budget_token);
1649 }
1650
1651
1652
1653
1654
1655
1656 #define SCSI_QUEUE_DELAY 3
1657
1658 static int scsi_mq_get_budget(struct request_queue *q)
1659 {
1660 struct scsi_device *sdev = q->queuedata;
1661 int token = scsi_dev_queue_ready(q, sdev);
1662
1663 if (token >= 0)
1664 return token;
1665
1666 atomic_inc(&sdev->restarts);
1667
1668
1669
1670
1671
1672
1673 smp_mb__after_atomic();
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683 if (unlikely(scsi_device_busy(sdev) == 0 &&
1684 !scsi_device_blocked(sdev)))
1685 blk_mq_delay_run_hw_queues(sdev->request_queue, SCSI_QUEUE_DELAY);
1686 return -1;
1687 }
1688
1689 static void scsi_mq_set_rq_budget_token(struct request *req, int token)
1690 {
1691 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1692
1693 cmd->budget_token = token;
1694 }
1695
1696 static int scsi_mq_get_rq_budget_token(struct request *req)
1697 {
1698 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1699
1700 return cmd->budget_token;
1701 }
1702
1703 static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
1704 const struct blk_mq_queue_data *bd)
1705 {
1706 struct request *req = bd->rq;
1707 struct request_queue *q = req->q;
1708 struct scsi_device *sdev = q->queuedata;
1709 struct Scsi_Host *shost = sdev->host;
1710 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
1711 blk_status_t ret;
1712 int reason;
1713
1714 WARN_ON_ONCE(cmd->budget_token < 0);
1715
1716
1717
1718
1719
1720 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
1721 ret = scsi_device_state_check(sdev, req);
1722 if (ret != BLK_STS_OK)
1723 goto out_put_budget;
1724 }
1725
1726 ret = BLK_STS_RESOURCE;
1727 if (!scsi_target_queue_ready(shost, sdev))
1728 goto out_put_budget;
1729 if (!scsi_host_queue_ready(q, shost, sdev, cmd))
1730 goto out_dec_target_busy;
1731
1732 if (!(req->rq_flags & RQF_DONTPREP)) {
1733 ret = scsi_prepare_cmd(req);
1734 if (ret != BLK_STS_OK)
1735 goto out_dec_host_busy;
1736 req->rq_flags |= RQF_DONTPREP;
1737 } else {
1738 clear_bit(SCMD_STATE_COMPLETE, &cmd->state);
1739 }
1740
1741 cmd->flags &= SCMD_PRESERVED_FLAGS;
1742 if (sdev->simple_tags)
1743 cmd->flags |= SCMD_TAGGED;
1744 if (bd->last)
1745 cmd->flags |= SCMD_LAST;
1746
1747 scsi_set_resid(cmd, 0);
1748 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1749 cmd->submitter = SUBMITTED_BY_BLOCK_LAYER;
1750
1751 blk_mq_start_request(req);
1752 reason = scsi_dispatch_cmd(cmd);
1753 if (reason) {
1754 scsi_set_blocked(cmd, reason);
1755 ret = BLK_STS_RESOURCE;
1756 goto out_dec_host_busy;
1757 }
1758
1759 return BLK_STS_OK;
1760
1761 out_dec_host_busy:
1762 scsi_dec_host_busy(shost, cmd);
1763 out_dec_target_busy:
1764 if (scsi_target(sdev)->can_queue > 0)
1765 atomic_dec(&scsi_target(sdev)->target_busy);
1766 out_put_budget:
1767 scsi_mq_put_budget(q, cmd->budget_token);
1768 cmd->budget_token = -1;
1769 switch (ret) {
1770 case BLK_STS_OK:
1771 break;
1772 case BLK_STS_RESOURCE:
1773 case BLK_STS_ZONE_RESOURCE:
1774 if (scsi_device_blocked(sdev))
1775 ret = BLK_STS_DEV_RESOURCE;
1776 break;
1777 case BLK_STS_AGAIN:
1778 cmd->result = DID_BUS_BUSY << 16;
1779 if (req->rq_flags & RQF_DONTPREP)
1780 scsi_mq_uninit_cmd(cmd);
1781 break;
1782 default:
1783 if (unlikely(!scsi_device_online(sdev)))
1784 cmd->result = DID_NO_CONNECT << 16;
1785 else
1786 cmd->result = DID_ERROR << 16;
1787
1788
1789
1790
1791
1792 if (req->rq_flags & RQF_DONTPREP)
1793 scsi_mq_uninit_cmd(cmd);
1794 scsi_run_queue_async(sdev);
1795 break;
1796 }
1797 return ret;
1798 }
1799
1800 static int scsi_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
1801 unsigned int hctx_idx, unsigned int numa_node)
1802 {
1803 struct Scsi_Host *shost = set->driver_data;
1804 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1805 struct scatterlist *sg;
1806 int ret = 0;
1807
1808 cmd->sense_buffer =
1809 kmem_cache_alloc_node(scsi_sense_cache, GFP_KERNEL, numa_node);
1810 if (!cmd->sense_buffer)
1811 return -ENOMEM;
1812
1813 if (scsi_host_get_prot(shost)) {
1814 sg = (void *)cmd + sizeof(struct scsi_cmnd) +
1815 shost->hostt->cmd_size;
1816 cmd->prot_sdb = (void *)sg + scsi_mq_inline_sgl_size(shost);
1817 }
1818
1819 if (shost->hostt->init_cmd_priv) {
1820 ret = shost->hostt->init_cmd_priv(shost, cmd);
1821 if (ret < 0)
1822 kmem_cache_free(scsi_sense_cache, cmd->sense_buffer);
1823 }
1824
1825 return ret;
1826 }
1827
1828 static void scsi_mq_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1829 unsigned int hctx_idx)
1830 {
1831 struct Scsi_Host *shost = set->driver_data;
1832 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
1833
1834 if (shost->hostt->exit_cmd_priv)
1835 shost->hostt->exit_cmd_priv(shost, cmd);
1836 kmem_cache_free(scsi_sense_cache, cmd->sense_buffer);
1837 }
1838
1839
1840 static int scsi_mq_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
1841 {
1842 struct Scsi_Host *shost = hctx->driver_data;
1843
1844 if (shost->hostt->mq_poll)
1845 return shost->hostt->mq_poll(shost, hctx->queue_num);
1846
1847 return 0;
1848 }
1849
1850 static int scsi_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1851 unsigned int hctx_idx)
1852 {
1853 struct Scsi_Host *shost = data;
1854
1855 hctx->driver_data = shost;
1856 return 0;
1857 }
1858
1859 static int scsi_map_queues(struct blk_mq_tag_set *set)
1860 {
1861 struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
1862
1863 if (shost->hostt->map_queues)
1864 return shost->hostt->map_queues(shost);
1865 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
1866 }
1867
1868 void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
1869 {
1870 struct device *dev = shost->dma_dev;
1871
1872
1873
1874
1875 blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
1876 SG_MAX_SEGMENTS));
1877
1878 if (scsi_host_prot_dma(shost)) {
1879 shost->sg_prot_tablesize =
1880 min_not_zero(shost->sg_prot_tablesize,
1881 (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
1882 BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
1883 blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
1884 }
1885
1886 blk_queue_max_hw_sectors(q, shost->max_sectors);
1887 blk_queue_segment_boundary(q, shost->dma_boundary);
1888 dma_set_seg_boundary(dev, shost->dma_boundary);
1889
1890 blk_queue_max_segment_size(q, shost->max_segment_size);
1891 blk_queue_virt_boundary(q, shost->virt_boundary_mask);
1892 dma_set_max_seg_size(dev, queue_max_segment_size(q));
1893
1894
1895
1896
1897
1898
1899
1900
1901 blk_queue_dma_alignment(q, max(4, dma_get_cache_alignment()) - 1);
1902 }
1903 EXPORT_SYMBOL_GPL(__scsi_init_queue);
1904
1905 static const struct blk_mq_ops scsi_mq_ops_no_commit = {
1906 .get_budget = scsi_mq_get_budget,
1907 .put_budget = scsi_mq_put_budget,
1908 .queue_rq = scsi_queue_rq,
1909 .complete = scsi_complete,
1910 .timeout = scsi_timeout,
1911 #ifdef CONFIG_BLK_DEBUG_FS
1912 .show_rq = scsi_show_rq,
1913 #endif
1914 .init_request = scsi_mq_init_request,
1915 .exit_request = scsi_mq_exit_request,
1916 .cleanup_rq = scsi_cleanup_rq,
1917 .busy = scsi_mq_lld_busy,
1918 .map_queues = scsi_map_queues,
1919 .init_hctx = scsi_init_hctx,
1920 .poll = scsi_mq_poll,
1921 .set_rq_budget_token = scsi_mq_set_rq_budget_token,
1922 .get_rq_budget_token = scsi_mq_get_rq_budget_token,
1923 };
1924
1925
1926 static void scsi_commit_rqs(struct blk_mq_hw_ctx *hctx)
1927 {
1928 struct Scsi_Host *shost = hctx->driver_data;
1929
1930 shost->hostt->commit_rqs(shost, hctx->queue_num);
1931 }
1932
1933 static const struct blk_mq_ops scsi_mq_ops = {
1934 .get_budget = scsi_mq_get_budget,
1935 .put_budget = scsi_mq_put_budget,
1936 .queue_rq = scsi_queue_rq,
1937 .commit_rqs = scsi_commit_rqs,
1938 .complete = scsi_complete,
1939 .timeout = scsi_timeout,
1940 #ifdef CONFIG_BLK_DEBUG_FS
1941 .show_rq = scsi_show_rq,
1942 #endif
1943 .init_request = scsi_mq_init_request,
1944 .exit_request = scsi_mq_exit_request,
1945 .cleanup_rq = scsi_cleanup_rq,
1946 .busy = scsi_mq_lld_busy,
1947 .map_queues = scsi_map_queues,
1948 .init_hctx = scsi_init_hctx,
1949 .poll = scsi_mq_poll,
1950 .set_rq_budget_token = scsi_mq_set_rq_budget_token,
1951 .get_rq_budget_token = scsi_mq_get_rq_budget_token,
1952 };
1953
1954 int scsi_mq_setup_tags(struct Scsi_Host *shost)
1955 {
1956 unsigned int cmd_size, sgl_size;
1957 struct blk_mq_tag_set *tag_set = &shost->tag_set;
1958
1959 sgl_size = max_t(unsigned int, sizeof(struct scatterlist),
1960 scsi_mq_inline_sgl_size(shost));
1961 cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
1962 if (scsi_host_get_prot(shost))
1963 cmd_size += sizeof(struct scsi_data_buffer) +
1964 sizeof(struct scatterlist) * SCSI_INLINE_PROT_SG_CNT;
1965
1966 memset(tag_set, 0, sizeof(*tag_set));
1967 if (shost->hostt->commit_rqs)
1968 tag_set->ops = &scsi_mq_ops;
1969 else
1970 tag_set->ops = &scsi_mq_ops_no_commit;
1971 tag_set->nr_hw_queues = shost->nr_hw_queues ? : 1;
1972 tag_set->nr_maps = shost->nr_maps ? : 1;
1973 tag_set->queue_depth = shost->can_queue;
1974 tag_set->cmd_size = cmd_size;
1975 tag_set->numa_node = dev_to_node(shost->dma_dev);
1976 tag_set->flags = BLK_MQ_F_SHOULD_MERGE;
1977 tag_set->flags |=
1978 BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
1979 tag_set->driver_data = shost;
1980 if (shost->host_tagset)
1981 tag_set->flags |= BLK_MQ_F_TAG_HCTX_SHARED;
1982
1983 return blk_mq_alloc_tag_set(tag_set);
1984 }
1985
1986 void scsi_mq_free_tags(struct kref *kref)
1987 {
1988 struct Scsi_Host *shost = container_of(kref, typeof(*shost),
1989 tagset_refcnt);
1990
1991 blk_mq_free_tag_set(&shost->tag_set);
1992 complete(&shost->tagset_freed);
1993 }
1994
1995
1996
1997
1998
1999
2000
2001
2002 struct scsi_device *scsi_device_from_queue(struct request_queue *q)
2003 {
2004 struct scsi_device *sdev = NULL;
2005
2006 if (q->mq_ops == &scsi_mq_ops_no_commit ||
2007 q->mq_ops == &scsi_mq_ops)
2008 sdev = q->queuedata;
2009 if (!sdev || !get_device(&sdev->sdev_gendev))
2010 sdev = NULL;
2011
2012 return sdev;
2013 }
2014
2015
2016
2017
2018
2019 #ifdef CONFIG_CDROM_PKTCDVD_MODULE
2020 EXPORT_SYMBOL_GPL(scsi_device_from_queue);
2021 #endif
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031 void scsi_block_requests(struct Scsi_Host *shost)
2032 {
2033 shost->host_self_blocked = 1;
2034 }
2035 EXPORT_SYMBOL(scsi_block_requests);
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047 void scsi_unblock_requests(struct Scsi_Host *shost)
2048 {
2049 shost->host_self_blocked = 0;
2050 scsi_run_host_queues(shost);
2051 }
2052 EXPORT_SYMBOL(scsi_unblock_requests);
2053
2054 void scsi_exit_queue(void)
2055 {
2056 kmem_cache_destroy(scsi_sense_cache);
2057 }
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076 int scsi_mode_select(struct scsi_device *sdev, int pf, int sp,
2077 unsigned char *buffer, int len, int timeout, int retries,
2078 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2079 {
2080 unsigned char cmd[10];
2081 unsigned char *real_buffer;
2082 int ret;
2083
2084 memset(cmd, 0, sizeof(cmd));
2085 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2086
2087
2088
2089
2090
2091
2092 if (sdev->use_10_for_ms ||
2093 len + 4 > 255 ||
2094 data->block_descriptor_length > 255) {
2095 if (len > 65535 - 8)
2096 return -EINVAL;
2097 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2098 if (!real_buffer)
2099 return -ENOMEM;
2100 memcpy(real_buffer + 8, buffer, len);
2101 len += 8;
2102 real_buffer[0] = 0;
2103 real_buffer[1] = 0;
2104 real_buffer[2] = data->medium_type;
2105 real_buffer[3] = data->device_specific;
2106 real_buffer[4] = data->longlba ? 0x01 : 0;
2107 real_buffer[5] = 0;
2108 put_unaligned_be16(data->block_descriptor_length,
2109 &real_buffer[6]);
2110
2111 cmd[0] = MODE_SELECT_10;
2112 put_unaligned_be16(len, &cmd[7]);
2113 } else {
2114 if (data->longlba)
2115 return -EINVAL;
2116
2117 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2118 if (!real_buffer)
2119 return -ENOMEM;
2120 memcpy(real_buffer + 4, buffer, len);
2121 len += 4;
2122 real_buffer[0] = 0;
2123 real_buffer[1] = data->medium_type;
2124 real_buffer[2] = data->device_specific;
2125 real_buffer[3] = data->block_descriptor_length;
2126
2127 cmd[0] = MODE_SELECT;
2128 cmd[4] = len;
2129 }
2130
2131 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
2132 sshdr, timeout, retries, NULL);
2133 kfree(real_buffer);
2134 return ret;
2135 }
2136 EXPORT_SYMBOL_GPL(scsi_mode_select);
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153 int
2154 scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
2155 unsigned char *buffer, int len, int timeout, int retries,
2156 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
2157 {
2158 unsigned char cmd[12];
2159 int use_10_for_ms;
2160 int header_length;
2161 int result, retry_count = retries;
2162 struct scsi_sense_hdr my_sshdr;
2163
2164 memset(data, 0, sizeof(*data));
2165 memset(&cmd[0], 0, 12);
2166
2167 dbd = sdev->set_dbd_for_ms ? 8 : dbd;
2168 cmd[1] = dbd & 0x18;
2169 cmd[2] = modepage;
2170
2171
2172 if (!sshdr)
2173 sshdr = &my_sshdr;
2174
2175 retry:
2176 use_10_for_ms = sdev->use_10_for_ms || len > 255;
2177
2178 if (use_10_for_ms) {
2179 if (len < 8 || len > 65535)
2180 return -EINVAL;
2181
2182 cmd[0] = MODE_SENSE_10;
2183 put_unaligned_be16(len, &cmd[7]);
2184 header_length = 8;
2185 } else {
2186 if (len < 4)
2187 return -EINVAL;
2188
2189 cmd[0] = MODE_SENSE;
2190 cmd[4] = len;
2191 header_length = 4;
2192 }
2193
2194 memset(buffer, 0, len);
2195
2196 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
2197 sshdr, timeout, retries, NULL);
2198 if (result < 0)
2199 return result;
2200
2201
2202
2203
2204
2205
2206 if (!scsi_status_is_good(result)) {
2207 if (scsi_sense_valid(sshdr)) {
2208 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2209 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
2210
2211
2212
2213
2214
2215
2216
2217 if (use_10_for_ms) {
2218 if (len > 255)
2219 return -EIO;
2220 sdev->use_10_for_ms = 0;
2221 goto retry;
2222 }
2223 }
2224 if (scsi_status_is_check_condition(result) &&
2225 sshdr->sense_key == UNIT_ATTENTION &&
2226 retry_count) {
2227 retry_count--;
2228 goto retry;
2229 }
2230 }
2231 return -EIO;
2232 }
2233 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
2234 (modepage == 6 || modepage == 8))) {
2235
2236 header_length = 0;
2237 data->length = 13;
2238 data->medium_type = 0;
2239 data->device_specific = 0;
2240 data->longlba = 0;
2241 data->block_descriptor_length = 0;
2242 } else if (use_10_for_ms) {
2243 data->length = get_unaligned_be16(&buffer[0]) + 2;
2244 data->medium_type = buffer[2];
2245 data->device_specific = buffer[3];
2246 data->longlba = buffer[4] & 0x01;
2247 data->block_descriptor_length = get_unaligned_be16(&buffer[6]);
2248 } else {
2249 data->length = buffer[0] + 1;
2250 data->medium_type = buffer[1];
2251 data->device_specific = buffer[2];
2252 data->block_descriptor_length = buffer[3];
2253 }
2254 data->header_length = header_length;
2255
2256 return 0;
2257 }
2258 EXPORT_SYMBOL(scsi_mode_sense);
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270 int
2271 scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
2272 struct scsi_sense_hdr *sshdr)
2273 {
2274 char cmd[] = {
2275 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2276 };
2277 int result;
2278
2279
2280 do {
2281 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
2282 timeout, 1, NULL);
2283 if (sdev->removable && scsi_sense_valid(sshdr) &&
2284 sshdr->sense_key == UNIT_ATTENTION)
2285 sdev->changed = 1;
2286 } while (scsi_sense_valid(sshdr) &&
2287 sshdr->sense_key == UNIT_ATTENTION && --retries);
2288
2289 return result;
2290 }
2291 EXPORT_SYMBOL(scsi_test_unit_ready);
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301 int
2302 scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2303 {
2304 enum scsi_device_state oldstate = sdev->sdev_state;
2305
2306 if (state == oldstate)
2307 return 0;
2308
2309 switch (state) {
2310 case SDEV_CREATED:
2311 switch (oldstate) {
2312 case SDEV_CREATED_BLOCK:
2313 break;
2314 default:
2315 goto illegal;
2316 }
2317 break;
2318
2319 case SDEV_RUNNING:
2320 switch (oldstate) {
2321 case SDEV_CREATED:
2322 case SDEV_OFFLINE:
2323 case SDEV_TRANSPORT_OFFLINE:
2324 case SDEV_QUIESCE:
2325 case SDEV_BLOCK:
2326 break;
2327 default:
2328 goto illegal;
2329 }
2330 break;
2331
2332 case SDEV_QUIESCE:
2333 switch (oldstate) {
2334 case SDEV_RUNNING:
2335 case SDEV_OFFLINE:
2336 case SDEV_TRANSPORT_OFFLINE:
2337 break;
2338 default:
2339 goto illegal;
2340 }
2341 break;
2342
2343 case SDEV_OFFLINE:
2344 case SDEV_TRANSPORT_OFFLINE:
2345 switch (oldstate) {
2346 case SDEV_CREATED:
2347 case SDEV_RUNNING:
2348 case SDEV_QUIESCE:
2349 case SDEV_BLOCK:
2350 break;
2351 default:
2352 goto illegal;
2353 }
2354 break;
2355
2356 case SDEV_BLOCK:
2357 switch (oldstate) {
2358 case SDEV_RUNNING:
2359 case SDEV_CREATED_BLOCK:
2360 case SDEV_QUIESCE:
2361 case SDEV_OFFLINE:
2362 break;
2363 default:
2364 goto illegal;
2365 }
2366 break;
2367
2368 case SDEV_CREATED_BLOCK:
2369 switch (oldstate) {
2370 case SDEV_CREATED:
2371 break;
2372 default:
2373 goto illegal;
2374 }
2375 break;
2376
2377 case SDEV_CANCEL:
2378 switch (oldstate) {
2379 case SDEV_CREATED:
2380 case SDEV_RUNNING:
2381 case SDEV_QUIESCE:
2382 case SDEV_OFFLINE:
2383 case SDEV_TRANSPORT_OFFLINE:
2384 break;
2385 default:
2386 goto illegal;
2387 }
2388 break;
2389
2390 case SDEV_DEL:
2391 switch (oldstate) {
2392 case SDEV_CREATED:
2393 case SDEV_RUNNING:
2394 case SDEV_OFFLINE:
2395 case SDEV_TRANSPORT_OFFLINE:
2396 case SDEV_CANCEL:
2397 case SDEV_BLOCK:
2398 case SDEV_CREATED_BLOCK:
2399 break;
2400 default:
2401 goto illegal;
2402 }
2403 break;
2404
2405 }
2406 sdev->offline_already = false;
2407 sdev->sdev_state = state;
2408 return 0;
2409
2410 illegal:
2411 SCSI_LOG_ERROR_RECOVERY(1,
2412 sdev_printk(KERN_ERR, sdev,
2413 "Illegal state transition %s->%s",
2414 scsi_device_state_name(oldstate),
2415 scsi_device_state_name(state))
2416 );
2417 return -EINVAL;
2418 }
2419 EXPORT_SYMBOL(scsi_device_set_state);
2420
2421
2422
2423
2424
2425
2426
2427
2428 static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2429 {
2430 int idx = 0;
2431 char *envp[3];
2432
2433 switch (evt->evt_type) {
2434 case SDEV_EVT_MEDIA_CHANGE:
2435 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2436 break;
2437 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2438 scsi_rescan_device(&sdev->sdev_gendev);
2439 envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
2440 break;
2441 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2442 envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
2443 break;
2444 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2445 envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
2446 break;
2447 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2448 envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
2449 break;
2450 case SDEV_EVT_LUN_CHANGE_REPORTED:
2451 envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
2452 break;
2453 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2454 envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
2455 break;
2456 case SDEV_EVT_POWER_ON_RESET_OCCURRED:
2457 envp[idx++] = "SDEV_UA=POWER_ON_RESET_OCCURRED";
2458 break;
2459 default:
2460
2461 break;
2462 }
2463
2464 envp[idx++] = NULL;
2465
2466 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2467 }
2468
2469
2470
2471
2472
2473
2474
2475
2476 void scsi_evt_thread(struct work_struct *work)
2477 {
2478 struct scsi_device *sdev;
2479 enum scsi_device_event evt_type;
2480 LIST_HEAD(event_list);
2481
2482 sdev = container_of(work, struct scsi_device, event_work);
2483
2484 for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
2485 if (test_and_clear_bit(evt_type, sdev->pending_events))
2486 sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
2487
2488 while (1) {
2489 struct scsi_event *evt;
2490 struct list_head *this, *tmp;
2491 unsigned long flags;
2492
2493 spin_lock_irqsave(&sdev->list_lock, flags);
2494 list_splice_init(&sdev->event_list, &event_list);
2495 spin_unlock_irqrestore(&sdev->list_lock, flags);
2496
2497 if (list_empty(&event_list))
2498 break;
2499
2500 list_for_each_safe(this, tmp, &event_list) {
2501 evt = list_entry(this, struct scsi_event, node);
2502 list_del(&evt->node);
2503 scsi_evt_emit(sdev, evt);
2504 kfree(evt);
2505 }
2506 }
2507 }
2508
2509
2510
2511
2512
2513
2514
2515
2516 void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2517 {
2518 unsigned long flags;
2519
2520 #if 0
2521
2522
2523
2524 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2525 kfree(evt);
2526 return;
2527 }
2528 #endif
2529
2530 spin_lock_irqsave(&sdev->list_lock, flags);
2531 list_add_tail(&evt->node, &sdev->event_list);
2532 schedule_work(&sdev->event_work);
2533 spin_unlock_irqrestore(&sdev->list_lock, flags);
2534 }
2535 EXPORT_SYMBOL_GPL(sdev_evt_send);
2536
2537
2538
2539
2540
2541
2542
2543
2544 struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2545 gfp_t gfpflags)
2546 {
2547 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2548 if (!evt)
2549 return NULL;
2550
2551 evt->evt_type = evt_type;
2552 INIT_LIST_HEAD(&evt->node);
2553
2554
2555 switch (evt_type) {
2556 case SDEV_EVT_MEDIA_CHANGE:
2557 case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
2558 case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
2559 case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
2560 case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
2561 case SDEV_EVT_LUN_CHANGE_REPORTED:
2562 case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
2563 case SDEV_EVT_POWER_ON_RESET_OCCURRED:
2564 default:
2565
2566 break;
2567 }
2568
2569 return evt;
2570 }
2571 EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581 void sdev_evt_send_simple(struct scsi_device *sdev,
2582 enum scsi_device_event evt_type, gfp_t gfpflags)
2583 {
2584 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2585 if (!evt) {
2586 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2587 evt_type);
2588 return;
2589 }
2590
2591 sdev_evt_send(sdev, evt);
2592 }
2593 EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608 int
2609 scsi_device_quiesce(struct scsi_device *sdev)
2610 {
2611 struct request_queue *q = sdev->request_queue;
2612 int err;
2613
2614
2615
2616
2617
2618
2619 WARN_ON_ONCE(sdev->quiesced_by && sdev->quiesced_by != current);
2620
2621 if (sdev->quiesced_by == current)
2622 return 0;
2623
2624 blk_set_pm_only(q);
2625
2626 blk_mq_freeze_queue(q);
2627
2628
2629
2630
2631
2632
2633 synchronize_rcu();
2634 blk_mq_unfreeze_queue(q);
2635
2636 mutex_lock(&sdev->state_mutex);
2637 err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2638 if (err == 0)
2639 sdev->quiesced_by = current;
2640 else
2641 blk_clear_pm_only(q);
2642 mutex_unlock(&sdev->state_mutex);
2643
2644 return err;
2645 }
2646 EXPORT_SYMBOL(scsi_device_quiesce);
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657 void scsi_device_resume(struct scsi_device *sdev)
2658 {
2659
2660
2661
2662
2663 mutex_lock(&sdev->state_mutex);
2664 if (sdev->sdev_state == SDEV_QUIESCE)
2665 scsi_device_set_state(sdev, SDEV_RUNNING);
2666 if (sdev->quiesced_by) {
2667 sdev->quiesced_by = NULL;
2668 blk_clear_pm_only(sdev->request_queue);
2669 }
2670 mutex_unlock(&sdev->state_mutex);
2671 }
2672 EXPORT_SYMBOL(scsi_device_resume);
2673
2674 static void
2675 device_quiesce_fn(struct scsi_device *sdev, void *data)
2676 {
2677 scsi_device_quiesce(sdev);
2678 }
2679
2680 void
2681 scsi_target_quiesce(struct scsi_target *starget)
2682 {
2683 starget_for_each_device(starget, NULL, device_quiesce_fn);
2684 }
2685 EXPORT_SYMBOL(scsi_target_quiesce);
2686
2687 static void
2688 device_resume_fn(struct scsi_device *sdev, void *data)
2689 {
2690 scsi_device_resume(sdev);
2691 }
2692
2693 void
2694 scsi_target_resume(struct scsi_target *starget)
2695 {
2696 starget_for_each_device(starget, NULL, device_resume_fn);
2697 }
2698 EXPORT_SYMBOL(scsi_target_resume);
2699
2700 static int __scsi_internal_device_block_nowait(struct scsi_device *sdev)
2701 {
2702 if (scsi_device_set_state(sdev, SDEV_BLOCK))
2703 return scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2704
2705 return 0;
2706 }
2707
2708 void scsi_start_queue(struct scsi_device *sdev)
2709 {
2710 if (cmpxchg(&sdev->queue_stopped, 1, 0))
2711 blk_mq_unquiesce_queue(sdev->request_queue);
2712 }
2713
2714 static void scsi_stop_queue(struct scsi_device *sdev, bool nowait)
2715 {
2716
2717
2718
2719
2720
2721
2722
2723 if (!cmpxchg(&sdev->queue_stopped, 0, 1)) {
2724 if (nowait)
2725 blk_mq_quiesce_queue_nowait(sdev->request_queue);
2726 else
2727 blk_mq_quiesce_queue(sdev->request_queue);
2728 } else {
2729 if (!nowait)
2730 blk_mq_wait_quiesce_done(sdev->request_queue);
2731 }
2732 }
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748 int scsi_internal_device_block_nowait(struct scsi_device *sdev)
2749 {
2750 int ret = __scsi_internal_device_block_nowait(sdev);
2751
2752
2753
2754
2755
2756
2757 if (!ret)
2758 scsi_stop_queue(sdev, true);
2759 return ret;
2760 }
2761 EXPORT_SYMBOL_GPL(scsi_internal_device_block_nowait);
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778 static int scsi_internal_device_block(struct scsi_device *sdev)
2779 {
2780 int err;
2781
2782 mutex_lock(&sdev->state_mutex);
2783 err = __scsi_internal_device_block_nowait(sdev);
2784 if (err == 0)
2785 scsi_stop_queue(sdev, false);
2786 mutex_unlock(&sdev->state_mutex);
2787
2788 return err;
2789 }
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806 int scsi_internal_device_unblock_nowait(struct scsi_device *sdev,
2807 enum scsi_device_state new_state)
2808 {
2809 switch (new_state) {
2810 case SDEV_RUNNING:
2811 case SDEV_TRANSPORT_OFFLINE:
2812 break;
2813 default:
2814 return -EINVAL;
2815 }
2816
2817
2818
2819
2820
2821 switch (sdev->sdev_state) {
2822 case SDEV_BLOCK:
2823 case SDEV_TRANSPORT_OFFLINE:
2824 sdev->sdev_state = new_state;
2825 break;
2826 case SDEV_CREATED_BLOCK:
2827 if (new_state == SDEV_TRANSPORT_OFFLINE ||
2828 new_state == SDEV_OFFLINE)
2829 sdev->sdev_state = new_state;
2830 else
2831 sdev->sdev_state = SDEV_CREATED;
2832 break;
2833 case SDEV_CANCEL:
2834 case SDEV_OFFLINE:
2835 break;
2836 default:
2837 return -EINVAL;
2838 }
2839 scsi_start_queue(sdev);
2840
2841 return 0;
2842 }
2843 EXPORT_SYMBOL_GPL(scsi_internal_device_unblock_nowait);
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859 static int scsi_internal_device_unblock(struct scsi_device *sdev,
2860 enum scsi_device_state new_state)
2861 {
2862 int ret;
2863
2864 mutex_lock(&sdev->state_mutex);
2865 ret = scsi_internal_device_unblock_nowait(sdev, new_state);
2866 mutex_unlock(&sdev->state_mutex);
2867
2868 return ret;
2869 }
2870
2871 static void
2872 device_block(struct scsi_device *sdev, void *data)
2873 {
2874 int ret;
2875
2876 ret = scsi_internal_device_block(sdev);
2877
2878 WARN_ONCE(ret, "scsi_internal_device_block(%s) failed: ret = %d\n",
2879 dev_name(&sdev->sdev_gendev), ret);
2880 }
2881
2882 static int
2883 target_block(struct device *dev, void *data)
2884 {
2885 if (scsi_is_target_device(dev))
2886 starget_for_each_device(to_scsi_target(dev), NULL,
2887 device_block);
2888 return 0;
2889 }
2890
2891 void
2892 scsi_target_block(struct device *dev)
2893 {
2894 if (scsi_is_target_device(dev))
2895 starget_for_each_device(to_scsi_target(dev), NULL,
2896 device_block);
2897 else
2898 device_for_each_child(dev, NULL, target_block);
2899 }
2900 EXPORT_SYMBOL_GPL(scsi_target_block);
2901
2902 static void
2903 device_unblock(struct scsi_device *sdev, void *data)
2904 {
2905 scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
2906 }
2907
2908 static int
2909 target_unblock(struct device *dev, void *data)
2910 {
2911 if (scsi_is_target_device(dev))
2912 starget_for_each_device(to_scsi_target(dev), data,
2913 device_unblock);
2914 return 0;
2915 }
2916
2917 void
2918 scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
2919 {
2920 if (scsi_is_target_device(dev))
2921 starget_for_each_device(to_scsi_target(dev), &new_state,
2922 device_unblock);
2923 else
2924 device_for_each_child(dev, &new_state, target_unblock);
2925 }
2926 EXPORT_SYMBOL_GPL(scsi_target_unblock);
2927
2928 int
2929 scsi_host_block(struct Scsi_Host *shost)
2930 {
2931 struct scsi_device *sdev;
2932 int ret = 0;
2933
2934
2935
2936
2937
2938 shost_for_each_device(sdev, shost) {
2939 mutex_lock(&sdev->state_mutex);
2940 ret = scsi_internal_device_block_nowait(sdev);
2941 mutex_unlock(&sdev->state_mutex);
2942 if (ret) {
2943 scsi_device_put(sdev);
2944 break;
2945 }
2946 }
2947
2948
2949
2950
2951
2952 WARN_ON_ONCE(shost->tag_set.flags & BLK_MQ_F_BLOCKING);
2953
2954 if (!ret)
2955 synchronize_rcu();
2956
2957 return ret;
2958 }
2959 EXPORT_SYMBOL_GPL(scsi_host_block);
2960
2961 int
2962 scsi_host_unblock(struct Scsi_Host *shost, int new_state)
2963 {
2964 struct scsi_device *sdev;
2965 int ret = 0;
2966
2967 shost_for_each_device(sdev, shost) {
2968 ret = scsi_internal_device_unblock(sdev, new_state);
2969 if (ret) {
2970 scsi_device_put(sdev);
2971 break;
2972 }
2973 }
2974 return ret;
2975 }
2976 EXPORT_SYMBOL_GPL(scsi_host_unblock);
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987 void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
2988 size_t *offset, size_t *len)
2989 {
2990 int i;
2991 size_t sg_len = 0, len_complete = 0;
2992 struct scatterlist *sg;
2993 struct page *page;
2994
2995 WARN_ON(!irqs_disabled());
2996
2997 for_each_sg(sgl, sg, sg_count, i) {
2998 len_complete = sg_len;
2999 sg_len += sg->length;
3000 if (sg_len > *offset)
3001 break;
3002 }
3003
3004 if (unlikely(i == sg_count)) {
3005 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
3006 "elements %d\n",
3007 __func__, sg_len, *offset, sg_count);
3008 WARN_ON(1);
3009 return NULL;
3010 }
3011
3012
3013 *offset = *offset - len_complete + sg->offset;
3014
3015
3016 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
3017 *offset &= ~PAGE_MASK;
3018
3019
3020 sg_len = PAGE_SIZE - *offset;
3021 if (*len > sg_len)
3022 *len = sg_len;
3023
3024 return kmap_atomic(page);
3025 }
3026 EXPORT_SYMBOL(scsi_kmap_atomic_sg);
3027
3028
3029
3030
3031
3032 void scsi_kunmap_atomic_sg(void *virt)
3033 {
3034 kunmap_atomic(virt);
3035 }
3036 EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
3037
3038 void sdev_disable_disk_events(struct scsi_device *sdev)
3039 {
3040 atomic_inc(&sdev->disk_events_disable_depth);
3041 }
3042 EXPORT_SYMBOL(sdev_disable_disk_events);
3043
3044 void sdev_enable_disk_events(struct scsi_device *sdev)
3045 {
3046 if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
3047 return;
3048 atomic_dec(&sdev->disk_events_disable_depth);
3049 }
3050 EXPORT_SYMBOL(sdev_enable_disk_events);
3051
3052 static unsigned char designator_prio(const unsigned char *d)
3053 {
3054 if (d[1] & 0x30)
3055
3056 return 0;
3057
3058 if (d[3] == 0)
3059
3060 return 0;
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077 switch (d[1] & 0xf) {
3078 case 8:
3079
3080 return 9;
3081 case 3:
3082 switch (d[4] >> 4) {
3083 case 6:
3084
3085 return 8;
3086 case 5:
3087
3088 return 5;
3089 case 4:
3090
3091 return 4;
3092 case 3:
3093
3094 return 1;
3095 default:
3096 break;
3097 }
3098 break;
3099 case 2:
3100 switch (d[3]) {
3101 case 16:
3102
3103 return 7;
3104 case 12:
3105
3106 return 6;
3107 case 8:
3108
3109 return 3;
3110 default:
3111 break;
3112 }
3113 break;
3114 case 1:
3115
3116 return 1;
3117 default:
3118 break;
3119 }
3120
3121 return 0;
3122 }
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138 int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
3139 {
3140 u8 cur_id_prio = 0;
3141 u8 cur_id_size = 0;
3142 const unsigned char *d, *cur_id_str;
3143 const struct scsi_vpd *vpd_pg83;
3144 int id_size = -EINVAL;
3145
3146 rcu_read_lock();
3147 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3148 if (!vpd_pg83) {
3149 rcu_read_unlock();
3150 return -ENXIO;
3151 }
3152
3153
3154 if (id_len < 21) {
3155 rcu_read_unlock();
3156 return -EINVAL;
3157 }
3158
3159 memset(id, 0, id_len);
3160 for (d = vpd_pg83->data + 4;
3161 d < vpd_pg83->data + vpd_pg83->len;
3162 d += d[3] + 4) {
3163 u8 prio = designator_prio(d);
3164
3165 if (prio == 0 || cur_id_prio > prio)
3166 continue;
3167
3168 switch (d[1] & 0xf) {
3169 case 0x1:
3170
3171 if (cur_id_size > d[3])
3172 break;
3173 cur_id_prio = prio;
3174 cur_id_size = d[3];
3175 if (cur_id_size + 4 > id_len)
3176 cur_id_size = id_len - 4;
3177 cur_id_str = d + 4;
3178 id_size = snprintf(id, id_len, "t10.%*pE",
3179 cur_id_size, cur_id_str);
3180 break;
3181 case 0x2:
3182
3183 cur_id_prio = prio;
3184 cur_id_size = d[3];
3185 cur_id_str = d + 4;
3186 switch (cur_id_size) {
3187 case 8:
3188 id_size = snprintf(id, id_len,
3189 "eui.%8phN",
3190 cur_id_str);
3191 break;
3192 case 12:
3193 id_size = snprintf(id, id_len,
3194 "eui.%12phN",
3195 cur_id_str);
3196 break;
3197 case 16:
3198 id_size = snprintf(id, id_len,
3199 "eui.%16phN",
3200 cur_id_str);
3201 break;
3202 default:
3203 break;
3204 }
3205 break;
3206 case 0x3:
3207
3208 cur_id_prio = prio;
3209 cur_id_size = d[3];
3210 cur_id_str = d + 4;
3211 switch (cur_id_size) {
3212 case 8:
3213 id_size = snprintf(id, id_len,
3214 "naa.%8phN",
3215 cur_id_str);
3216 break;
3217 case 16:
3218 id_size = snprintf(id, id_len,
3219 "naa.%16phN",
3220 cur_id_str);
3221 break;
3222 default:
3223 break;
3224 }
3225 break;
3226 case 0x8:
3227
3228 if (cur_id_size > d[3])
3229 break;
3230
3231 if (d[3] > id_len) {
3232 prio = 2;
3233 if (cur_id_prio > prio)
3234 break;
3235 }
3236 cur_id_prio = prio;
3237 cur_id_size = id_size = d[3];
3238 cur_id_str = d + 4;
3239 if (cur_id_size >= id_len)
3240 cur_id_size = id_len - 1;
3241 memcpy(id, cur_id_str, cur_id_size);
3242 break;
3243 default:
3244 break;
3245 }
3246 }
3247 rcu_read_unlock();
3248
3249 return id_size;
3250 }
3251 EXPORT_SYMBOL(scsi_vpd_lun_id);
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262 int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
3263 {
3264 const unsigned char *d;
3265 const struct scsi_vpd *vpd_pg83;
3266 int group_id = -EAGAIN, rel_port = -1;
3267
3268 rcu_read_lock();
3269 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
3270 if (!vpd_pg83) {
3271 rcu_read_unlock();
3272 return -ENXIO;
3273 }
3274
3275 d = vpd_pg83->data + 4;
3276 while (d < vpd_pg83->data + vpd_pg83->len) {
3277 switch (d[1] & 0xf) {
3278 case 0x4:
3279
3280 rel_port = get_unaligned_be16(&d[6]);
3281 break;
3282 case 0x5:
3283
3284 group_id = get_unaligned_be16(&d[6]);
3285 break;
3286 default:
3287 break;
3288 }
3289 d += d[3] + 4;
3290 }
3291 rcu_read_unlock();
3292
3293 if (group_id >= 0 && rel_id && rel_port != -1)
3294 *rel_id = rel_port;
3295
3296 return group_id;
3297 }
3298 EXPORT_SYMBOL(scsi_vpd_tpg_id);
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310 void scsi_build_sense(struct scsi_cmnd *scmd, int desc, u8 key, u8 asc, u8 ascq)
3311 {
3312 scsi_build_sense_buffer(desc, scmd->sense_buffer, key, asc, ascq);
3313 scmd->result = SAM_STAT_CHECK_CONDITION;
3314 }
3315 EXPORT_SYMBOL_GPL(scsi_build_sense);