scsi_lib.c (11b68e36b16752ae3a48dc16229958ebc9bb324a) scsi_lib.c (fe45e630a1035aea94c29016f2598bbde149bbe3)
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 1999 Eric Youngdale
4 * Copyright (C) 2014 Christoph Hellwig
5 *
6 * SCSI queueing library.
7 * Initial versions: Eric Youngdale (eric@andante.org).
8 * Based upon conversations with large numbers

--- 7 unchanged lines hidden (view full) ---

16#include <linux/kernel.h>
17#include <linux/export.h>
18#include <linux/init.h>
19#include <linux/pci.h>
20#include <linux/delay.h>
21#include <linux/hardirq.h>
22#include <linux/scatterlist.h>
23#include <linux/blk-mq.h>
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 1999 Eric Youngdale
4 * Copyright (C) 2014 Christoph Hellwig
5 *
6 * SCSI queueing library.
7 * Initial versions: Eric Youngdale (eric@andante.org).
8 * Based upon conversations with large numbers

--- 7 unchanged lines hidden (view full) ---

16#include <linux/kernel.h>
17#include <linux/export.h>
18#include <linux/init.h>
19#include <linux/pci.h>
20#include <linux/delay.h>
21#include <linux/hardirq.h>
22#include <linux/scatterlist.h>
23#include <linux/blk-mq.h>
24#include <linux/blk-integrity.h>
24#include <linux/ratelimit.h>
25#include <asm/unaligned.h>
26
27#include <scsi/scsi.h>
28#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_dbg.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_driver.h>

--- 912 unchanged lines hidden (view full) ---

944 if (scsi_end_request(req, blk_stat, blk_rq_bytes(req)))
945 WARN_ONCE(true,
946 "Bytes remaining after failed, no-retry command");
947 return;
948 }
949
950 /*
951 * If there had been no error, but we have leftover bytes in the
25#include <linux/ratelimit.h>
26#include <asm/unaligned.h>
27
28#include <scsi/scsi.h>
29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_dbg.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_driver.h>

--- 912 unchanged lines hidden (view full) ---

945 if (scsi_end_request(req, blk_stat, blk_rq_bytes(req)))
946 WARN_ONCE(true,
947 "Bytes remaining after failed, no-retry command");
948 return;
949 }
950
951 /*
952 * If there had been no error, but we have leftover bytes in the
952 * request just queue the command up again.
953 * requeues just queue the command up again.
953 */
954 if (likely(result == 0))
955 scsi_io_completion_reprep(cmd, q);
956 else
957 scsi_io_completion_action(cmd, result);
958}
959
960static inline bool scsi_cmd_needs_dma_drain(struct scsi_device *sdev,

--- 554 unchanged lines hidden (view full) ---

1515 rtn = SCSI_MLQUEUE_HOST_BUSY;
1516
1517 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1518 "queuecommand : request rejected\n"));
1519 }
1520
1521 return rtn;
1522 done:
954 */
955 if (likely(result == 0))
956 scsi_io_completion_reprep(cmd, q);
957 else
958 scsi_io_completion_action(cmd, result);
959}
960
961static inline bool scsi_cmd_needs_dma_drain(struct scsi_device *sdev,

--- 554 unchanged lines hidden (view full) ---

1516 rtn = SCSI_MLQUEUE_HOST_BUSY;
1517
1518 SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
1519 "queuecommand : request rejected\n"));
1520 }
1521
1522 return rtn;
1523 done:
1523 scsi_done(cmd);
1524 cmd->scsi_done(cmd);
1524 return 0;
1525}
1526
1527/* Size in bytes of the sg-list stored in the scsi-mq command-private data. */
1528static unsigned int scsi_mq_inline_sgl_size(struct Scsi_Host *shost)
1529{
1530 return min_t(unsigned int, shost->sg_tablesize, SCSI_INLINE_SG_CNT) *
1531 sizeof(struct scatterlist);

--- 38 unchanged lines hidden (view full) ---

1570 return ret;
1571 }
1572
1573 cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd;
1574 memset(cmd->cmnd, 0, BLK_MAX_CDB);
1575 return scsi_cmd_to_driver(cmd)->init_command(cmd);
1576}
1577
1525 return 0;
1526}
1527
1528/* Size in bytes of the sg-list stored in the scsi-mq command-private data. */
1529static unsigned int scsi_mq_inline_sgl_size(struct Scsi_Host *shost)
1530{
1531 return min_t(unsigned int, shost->sg_tablesize, SCSI_INLINE_SG_CNT) *
1532 sizeof(struct scatterlist);

--- 38 unchanged lines hidden (view full) ---

1571 return ret;
1572 }
1573
1574 cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd;
1575 memset(cmd->cmnd, 0, BLK_MAX_CDB);
1576 return scsi_cmd_to_driver(cmd)->init_command(cmd);
1577}
1578
1578void scsi_done(struct scsi_cmnd *cmd)
1579static void scsi_mq_done(struct scsi_cmnd *cmd)
1579{
1580{
1580 switch (cmd->submitter) {
1581 case SUBMITTED_BY_BLOCK_LAYER:
1582 break;
1583 case SUBMITTED_BY_SCSI_ERROR_HANDLER:
1584 return scsi_eh_done(cmd);
1585 case SUBMITTED_BY_SCSI_RESET_IOCTL:
1586 return;
1587 }
1588
1589 if (unlikely(blk_should_fake_timeout(scsi_cmd_to_rq(cmd)->q)))
1590 return;
1591 if (unlikely(test_and_set_bit(SCMD_STATE_COMPLETE, &cmd->state)))
1592 return;
1593 trace_scsi_dispatch_cmd_done(cmd);
1594 blk_mq_complete_request(scsi_cmd_to_rq(cmd));
1595}
1581 if (unlikely(blk_should_fake_timeout(scsi_cmd_to_rq(cmd)->q)))
1582 return;
1583 if (unlikely(test_and_set_bit(SCMD_STATE_COMPLETE, &cmd->state)))
1584 return;
1585 trace_scsi_dispatch_cmd_done(cmd);
1586 blk_mq_complete_request(scsi_cmd_to_rq(cmd));
1587}
1596EXPORT_SYMBOL(scsi_done);
1597
1598static void scsi_mq_put_budget(struct request_queue *q, int budget_token)
1599{
1600 struct scsi_device *sdev = q->queuedata;
1601
1602 sbitmap_put(&sdev->budget_map, budget_token);
1603}
1604

--- 83 unchanged lines hidden (view full) ---

1688 cmd->flags &= SCMD_PRESERVED_FLAGS;
1689 if (sdev->simple_tags)
1690 cmd->flags |= SCMD_TAGGED;
1691 if (bd->last)
1692 cmd->flags |= SCMD_LAST;
1693
1694 scsi_set_resid(cmd, 0);
1695 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1588
1589static void scsi_mq_put_budget(struct request_queue *q, int budget_token)
1590{
1591 struct scsi_device *sdev = q->queuedata;
1592
1593 sbitmap_put(&sdev->budget_map, budget_token);
1594}
1595

--- 83 unchanged lines hidden (view full) ---

1679 cmd->flags &= SCMD_PRESERVED_FLAGS;
1680 if (sdev->simple_tags)
1681 cmd->flags |= SCMD_TAGGED;
1682 if (bd->last)
1683 cmd->flags |= SCMD_LAST;
1684
1685 scsi_set_resid(cmd, 0);
1686 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1696 cmd->submitter = SUBMITTED_BY_BLOCK_LAYER;
1687 cmd->scsi_done = scsi_mq_done;
1697
1698 blk_mq_start_request(req);
1699 reason = scsi_dispatch_cmd(cmd);
1700 if (reason) {
1701 scsi_set_blocked(cmd, reason);
1702 ret = BLK_STS_RESOURCE;
1703 goto out_dec_host_busy;
1704 }

--- 326 unchanged lines hidden (view full) ---

2031{
2032 unsigned char cmd[10];
2033 unsigned char *real_buffer;
2034 int ret;
2035
2036 memset(cmd, 0, sizeof(cmd));
2037 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2038
1688
1689 blk_mq_start_request(req);
1690 reason = scsi_dispatch_cmd(cmd);
1691 if (reason) {
1692 scsi_set_blocked(cmd, reason);
1693 ret = BLK_STS_RESOURCE;
1694 goto out_dec_host_busy;
1695 }

--- 326 unchanged lines hidden (view full) ---

2022{
2023 unsigned char cmd[10];
2024 unsigned char *real_buffer;
2025 int ret;
2026
2027 memset(cmd, 0, sizeof(cmd));
2028 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
2029
2039 /*
2040 * Use MODE SELECT(10) if the device asked for it or if the mode page
2041 * and the mode select header cannot fit within the maximumm 255 bytes
2042 * of the MODE SELECT(6) command.
2043 */
2044 if (sdev->use_10_for_ms ||
2045 len + 4 > 255 ||
2046 data->block_descriptor_length > 255) {
2047 if (len > 65535 - 8)
2030 if (sdev->use_10_for_ms) {
2031 if (len > 65535)
2048 return -EINVAL;
2049 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2050 if (!real_buffer)
2051 return -ENOMEM;
2052 memcpy(real_buffer + 8, buffer, len);
2053 len += 8;
2054 real_buffer[0] = 0;
2055 real_buffer[1] = 0;
2056 real_buffer[2] = data->medium_type;
2057 real_buffer[3] = data->device_specific;
2058 real_buffer[4] = data->longlba ? 0x01 : 0;
2059 real_buffer[5] = 0;
2032 return -EINVAL;
2033 real_buffer = kmalloc(8 + len, GFP_KERNEL);
2034 if (!real_buffer)
2035 return -ENOMEM;
2036 memcpy(real_buffer + 8, buffer, len);
2037 len += 8;
2038 real_buffer[0] = 0;
2039 real_buffer[1] = 0;
2040 real_buffer[2] = data->medium_type;
2041 real_buffer[3] = data->device_specific;
2042 real_buffer[4] = data->longlba ? 0x01 : 0;
2043 real_buffer[5] = 0;
2060 put_unaligned_be16(data->block_descriptor_length,
2061 &real_buffer[6]);
2044 real_buffer[6] = data->block_descriptor_length >> 8;
2045 real_buffer[7] = data->block_descriptor_length;
2062
2063 cmd[0] = MODE_SELECT_10;
2046
2047 cmd[0] = MODE_SELECT_10;
2064 put_unaligned_be16(len, &cmd[7]);
2048 cmd[7] = len >> 8;
2049 cmd[8] = len;
2065 } else {
2050 } else {
2066 if (data->longlba)
2051 if (len > 255 || data->block_descriptor_length > 255 ||
2052 data->longlba)
2067 return -EINVAL;
2068
2069 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2070 if (!real_buffer)
2071 return -ENOMEM;
2072 memcpy(real_buffer + 4, buffer, len);
2073 len += 4;
2074 real_buffer[0] = 0;

--- 10 unchanged lines hidden (view full) ---

2085 kfree(real_buffer);
2086 return ret;
2087}
2088EXPORT_SYMBOL_GPL(scsi_mode_select);
2089
2090/**
2091 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
2092 * @sdev: SCSI device to be queried
2053 return -EINVAL;
2054
2055 real_buffer = kmalloc(4 + len, GFP_KERNEL);
2056 if (!real_buffer)
2057 return -ENOMEM;
2058 memcpy(real_buffer + 4, buffer, len);
2059 len += 4;
2060 real_buffer[0] = 0;

--- 10 unchanged lines hidden (view full) ---

2071 kfree(real_buffer);
2072 return ret;
2073}
2074EXPORT_SYMBOL_GPL(scsi_mode_select);
2075
2076/**
2077 * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
2078 * @sdev: SCSI device to be queried
2093 * @dbd: set to prevent mode sense from returning block descriptors
2079 * @dbd: set if mode sense will allow block descriptors to be returned
2094 * @modepage: mode page being requested
2095 * @buffer: request buffer (may not be smaller than eight bytes)
2096 * @len: length of request buffer.
2097 * @timeout: command timeout
2098 * @retries: number of retries before failing
2099 * @data: returns a structure abstracting the mode header data
2100 * @sshdr: place to put sense data (or NULL if no sense to be collected).
2101 * must be SCSI_SENSE_BUFFERSIZE big.

--- 18 unchanged lines hidden (view full) ---

2120 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
2121 cmd[2] = modepage;
2122
2123 /* caller might not be interested in sense, but we need it */
2124 if (!sshdr)
2125 sshdr = &my_sshdr;
2126
2127 retry:
2080 * @modepage: mode page being requested
2081 * @buffer: request buffer (may not be smaller than eight bytes)
2082 * @len: length of request buffer.
2083 * @timeout: command timeout
2084 * @retries: number of retries before failing
2085 * @data: returns a structure abstracting the mode header data
2086 * @sshdr: place to put sense data (or NULL if no sense to be collected).
2087 * must be SCSI_SENSE_BUFFERSIZE big.

--- 18 unchanged lines hidden (view full) ---

2106 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
2107 cmd[2] = modepage;
2108
2109 /* caller might not be interested in sense, but we need it */
2110 if (!sshdr)
2111 sshdr = &my_sshdr;
2112
2113 retry:
2128 use_10_for_ms = sdev->use_10_for_ms || len > 255;
2114 use_10_for_ms = sdev->use_10_for_ms;
2129
2130 if (use_10_for_ms) {
2115
2116 if (use_10_for_ms) {
2131 if (len < 8 || len > 65535)
2132 return -EINVAL;
2117 if (len < 8)
2118 len = 8;
2133
2134 cmd[0] = MODE_SENSE_10;
2119
2120 cmd[0] = MODE_SENSE_10;
2135 put_unaligned_be16(len, &cmd[7]);
2121 cmd[8] = len;
2136 header_length = 8;
2137 } else {
2138 if (len < 4)
2122 header_length = 8;
2123 } else {
2124 if (len < 4)
2139 return -EINVAL;
2125 len = 4;
2140
2141 cmd[0] = MODE_SENSE;
2142 cmd[4] = len;
2143 header_length = 4;
2144 }
2145
2146 memset(buffer, 0, len);
2147

--- 7 unchanged lines hidden (view full) ---

2155 * byte as the problem. MODE_SENSE commands can return
2156 * ILLEGAL REQUEST if the code page isn't supported */
2157
2158 if (!scsi_status_is_good(result)) {
2159 if (scsi_sense_valid(sshdr)) {
2160 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2161 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
2162 /*
2126
2127 cmd[0] = MODE_SENSE;
2128 cmd[4] = len;
2129 header_length = 4;
2130 }
2131
2132 memset(buffer, 0, len);
2133

--- 7 unchanged lines hidden (view full) ---

2141 * byte as the problem. MODE_SENSE commands can return
2142 * ILLEGAL REQUEST if the code page isn't supported */
2143
2144 if (!scsi_status_is_good(result)) {
2145 if (scsi_sense_valid(sshdr)) {
2146 if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
2147 (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
2148 /*
2163 * Invalid command operation code: retry using
2164 * MODE SENSE(6) if this was a MODE SENSE(10)
2165 * request, except if the request mode page is
2166 * too large for MODE SENSE single byte
2167 * allocation length field.
2149 * Invalid command operation code
2168 */
2169 if (use_10_for_ms) {
2150 */
2151 if (use_10_for_ms) {
2170 if (len > 255)
2171 return -EIO;
2172 sdev->use_10_for_ms = 0;
2173 goto retry;
2174 }
2175 }
2176 if (scsi_status_is_check_condition(result) &&
2177 sshdr->sense_key == UNIT_ATTENTION &&
2178 retry_count) {
2179 retry_count--;

--- 7 unchanged lines hidden (view full) ---

2187 /* Initio breakage? */
2188 header_length = 0;
2189 data->length = 13;
2190 data->medium_type = 0;
2191 data->device_specific = 0;
2192 data->longlba = 0;
2193 data->block_descriptor_length = 0;
2194 } else if (use_10_for_ms) {
2152 sdev->use_10_for_ms = 0;
2153 goto retry;
2154 }
2155 }
2156 if (scsi_status_is_check_condition(result) &&
2157 sshdr->sense_key == UNIT_ATTENTION &&
2158 retry_count) {
2159 retry_count--;

--- 7 unchanged lines hidden (view full) ---

2167 /* Initio breakage? */
2168 header_length = 0;
2169 data->length = 13;
2170 data->medium_type = 0;
2171 data->device_specific = 0;
2172 data->longlba = 0;
2173 data->block_descriptor_length = 0;
2174 } else if (use_10_for_ms) {
2195 data->length = get_unaligned_be16(&buffer[0]) + 2;
2175 data->length = buffer[0]*256 + buffer[1] + 2;
2196 data->medium_type = buffer[2];
2197 data->device_specific = buffer[3];
2198 data->longlba = buffer[4] & 0x01;
2176 data->medium_type = buffer[2];
2177 data->device_specific = buffer[3];
2178 data->longlba = buffer[4] & 0x01;
2199 data->block_descriptor_length = get_unaligned_be16(&buffer[6]);
2179 data->block_descriptor_length = buffer[6]*256
2180 + buffer[7];
2200 } else {
2201 data->length = buffer[0] + 1;
2202 data->medium_type = buffer[1];
2203 data->device_specific = buffer[2];
2204 data->block_descriptor_length = buffer[3];
2205 }
2206 data->header_length = header_length;
2207

--- 1042 unchanged lines hidden ---
2181 } else {
2182 data->length = buffer[0] + 1;
2183 data->medium_type = buffer[1];
2184 data->device_specific = buffer[2];
2185 data->block_descriptor_length = buffer[3];
2186 }
2187 data->header_length = header_length;
2188

--- 1042 unchanged lines hidden ---