diff options
author | Mark Brown <broonie@kernel.org> | 2016-11-04 12:16:38 -0600 |
---|---|---|
committer | Mark Brown <broonie@kernel.org> | 2016-11-04 12:16:38 -0600 |
commit | cc9b94029e9ef51787af908e9856b1eed314bc00 (patch) | |
tree | 9675310b89d0f6fb1f7bd9423f0638c4ee5226fd /drivers/scsi/scsi_debug.c | |
parent | 13bed58ce8748d430a26e353a09b89f9d613a71f (diff) | |
parent | 1b5b42216469b05ef4b5916cb40b127dfab1da88 (diff) |
Merge branch 'topic/error' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator into regulator-fixed
Diffstat (limited to 'drivers/scsi/scsi_debug.c')
-rw-r--r-- | drivers/scsi/scsi_debug.c | 147 |
1 files changed, 93 insertions, 54 deletions
diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c index 0f9ba41e27d8..c905709707f0 100644 --- a/drivers/scsi/scsi_debug.c +++ b/drivers/scsi/scsi_debug.c @@ -42,6 +42,7 @@ #include <linux/atomic.h> #include <linux/hrtimer.h> #include <linux/uuid.h> +#include <linux/t10-pi.h> #include <net/checksum.h> @@ -627,7 +628,7 @@ static LIST_HEAD(sdebug_host_list); static DEFINE_SPINLOCK(sdebug_host_list_lock); static unsigned char *fake_storep; /* ramdisk storage */ -static struct sd_dif_tuple *dif_storep; /* protection info */ +static struct t10_pi_tuple *dif_storep; /* protection info */ static void *map_storep; /* provisioning map */ static unsigned long map_size; @@ -682,7 +683,7 @@ static void *fake_store(unsigned long long lba) return fake_storep + lba * sdebug_sector_size; } -static struct sd_dif_tuple *dif_store(sector_t sector) +static struct t10_pi_tuple *dif_store(sector_t sector) { sector = sector_div(sector, sdebug_store_sectors); @@ -890,7 +891,7 @@ static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) return 0; } -/* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */ +/* Build SCSI "data-in" buffer. Returns 0 if ok else (DID_ERROR << 16). */ static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, int arr_len) { @@ -909,7 +910,35 @@ static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, return 0; } -/* Returns number of bytes fetched into 'arr' or -1 if error. */ +/* Partial build of SCSI "data-in" buffer. Returns 0 if ok else + * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple + * calls, not required to write in ascending offset order. Assumes resid + * set to scsi_bufflen() prior to any calls. + */ +static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr, + int arr_len, unsigned int off_dst) +{ + int act_len, n; + struct scsi_data_buffer *sdb = scsi_in(scp); + off_t skip = off_dst; + + if (sdb->length <= off_dst) + return 0; + if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE)) + return DID_ERROR << 16; + + act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents, + arr, arr_len, skip); + pr_debug("%s: off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n", + __func__, off_dst, scsi_bufflen(scp), act_len, sdb->resid); + n = (int)scsi_bufflen(scp) - ((int)off_dst + act_len); + sdb->resid = min(sdb->resid, n); + return 0; +} + +/* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into + * 'arr' or -1 if error. + */ static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, int arr_len) { @@ -1321,7 +1350,7 @@ static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) } else if (0x86 == cmd[2]) { /* extended inquiry */ arr[1] = cmd[2]; /*sanity */ arr[3] = 0x3c; /* number of following entries */ - if (sdebug_dif == SD_DIF_TYPE3_PROTECTION) + if (sdebug_dif == T10_PI_TYPE3_PROTECTION) arr[4] = 0x4; /* SPT: GRD_CHK:1 */ else if (have_dif_prot) arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */ @@ -2402,7 +2431,7 @@ static __be16 dif_compute_csum(const void *buf, int len) return csum; } -static int dif_verify(struct sd_dif_tuple *sdt, const void *data, +static int dif_verify(struct t10_pi_tuple *sdt, const void *data, sector_t sector, u32 ei_lba) { __be16 csum = dif_compute_csum(data, sdebug_sector_size); @@ -2414,13 +2443,13 @@ static int dif_verify(struct sd_dif_tuple *sdt, const void *data, be16_to_cpu(csum)); return 0x01; } - if (sdebug_dif == SD_DIF_TYPE1_PROTECTION && + if (sdebug_dif == T10_PI_TYPE1_PROTECTION && be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { pr_err("REF check failed on sector %lu\n", (unsigned long)sector); return 0x03; } - if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && + if (sdebug_dif == T10_PI_TYPE2_PROTECTION && be32_to_cpu(sdt->ref_tag) != ei_lba) { pr_err("REF check failed on sector %lu\n", (unsigned long)sector); @@ -2476,7 +2505,7 @@ static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec, unsigned int sectors, u32 ei_lba) { unsigned int i; - struct sd_dif_tuple *sdt; + struct t10_pi_tuple *sdt; sector_t sector; for (i = 0; i < sectors; i++, ei_lba++) { @@ -2552,13 +2581,13 @@ static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) break; } if (unlikely(have_dif_prot && check_prot)) { - if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && + if (sdebug_dif == T10_PI_TYPE2_PROTECTION && (cmd[1] & 0xe0)) { mk_sense_invalid_opcode(scp); return check_condition_result; } - if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || - sdebug_dif == SD_DIF_TYPE3_PROTECTION) && + if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || + sdebug_dif == T10_PI_TYPE3_PROTECTION) && (cmd[1] & 0xe0) == 0) sdev_printk(KERN_ERR, scp->device, "Unprotected RD " "to DIF device\n"); @@ -2668,7 +2697,7 @@ static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec, unsigned int sectors, u32 ei_lba) { int ret; - struct sd_dif_tuple *sdt; + struct t10_pi_tuple *sdt; void *daddr; sector_t sector = start_sec; int ppage_offset; @@ -2694,7 +2723,7 @@ static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec, } for (ppage_offset = 0; ppage_offset < piter.length; - ppage_offset += sizeof(struct sd_dif_tuple)) { + ppage_offset += sizeof(struct t10_pi_tuple)) { /* If we're at the end of the current * data page advance to the next one */ @@ -2865,13 +2894,13 @@ static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) break; } if (unlikely(have_dif_prot && check_prot)) { - if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && + if (sdebug_dif == T10_PI_TYPE2_PROTECTION && (cmd[1] & 0xe0)) { mk_sense_invalid_opcode(scp); return check_condition_result; } - if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || - sdebug_dif == SD_DIF_TYPE3_PROTECTION) && + if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || + sdebug_dif == T10_PI_TYPE3_PROTECTION) && (cmd[1] & 0xe0) == 0) sdev_printk(KERN_ERR, scp->device, "Unprotected WR " "to DIF device\n"); @@ -3107,13 +3136,13 @@ static int resp_comp_write(struct scsi_cmnd *scp, num = cmd[13]; /* 1 to a maximum of 255 logical blocks */ if (0 == num) return 0; /* degenerate case, not an error */ - if (sdebug_dif == SD_DIF_TYPE2_PROTECTION && + if (sdebug_dif == T10_PI_TYPE2_PROTECTION && (cmd[1] & 0xe0)) { mk_sense_invalid_opcode(scp); return check_condition_result; } - if ((sdebug_dif == SD_DIF_TYPE1_PROTECTION || - sdebug_dif == SD_DIF_TYPE3_PROTECTION) && + if ((sdebug_dif == T10_PI_TYPE1_PROTECTION || + sdebug_dif == T10_PI_TYPE3_PROTECTION) && (cmd[1] & 0xe0) == 0) sdev_printk(KERN_ERR, scp->device, "Unprotected WR " "to DIF device\n"); @@ -3269,6 +3298,8 @@ static int resp_get_lba_status(struct scsi_cmnd *scp, return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN); } +#define RL_BUCKET_ELEMS 8 + /* Even though each pseudo target has a REPORT LUNS "well known logical unit" * (W-LUN), the normal Linux scanning logic does not associate it with a * device (e.g. /dev/sg7). The following magic will make that association: @@ -3285,12 +3316,14 @@ static int resp_report_luns(struct scsi_cmnd *scp, unsigned char select_report; u64 lun; struct scsi_lun *lun_p; - u8 *arr; + u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)]; unsigned int lun_cnt; /* normal LUN count (max: 256) */ unsigned int wlun_cnt; /* report luns W-LUN count */ unsigned int tlun_cnt; /* total LUN count */ unsigned int rlen; /* response length (in bytes) */ - int i, res; + int k, j, n, res; + unsigned int off_rsp = 0; + const int sz_lun = sizeof(struct scsi_lun); clear_luns_changed_on_target(devip); @@ -3329,33 +3362,40 @@ static int resp_report_luns(struct scsi_cmnd *scp, --lun_cnt; tlun_cnt = lun_cnt + wlun_cnt; - - rlen = (tlun_cnt * sizeof(struct scsi_lun)) + 8; - arr = vmalloc(rlen); - if (!arr) { - mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC, - INSUFF_RES_ASCQ); - return check_condition_result; - } - memset(arr, 0, rlen); + rlen = tlun_cnt * sz_lun; /* excluding 8 byte header */ + scsi_set_resid(scp, scsi_bufflen(scp)); pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n", select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0); - /* luns start at byte 8 in response following the header */ - lun_p = (struct scsi_lun *)&arr[8]; - - /* LUNs use single level peripheral device addressing method */ + /* loops rely on sizeof response header same as sizeof lun (both 8) */ lun = sdebug_no_lun_0 ? 1 : 0; - for (i = 0; i < lun_cnt; i++) - int_to_scsilun(lun++, lun_p++); - - if (wlun_cnt) - int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p++); - - put_unaligned_be32(rlen - 8, &arr[0]); - - res = fill_from_dev_buffer(scp, arr, rlen); - vfree(arr); + for (k = 0, j = 0, res = 0; true; ++k, j = 0) { + memset(arr, 0, sizeof(arr)); + lun_p = (struct scsi_lun *)&arr[0]; + if (k == 0) { + put_unaligned_be32(rlen, &arr[0]); + ++lun_p; + j = 1; + } + for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) { + if ((k * RL_BUCKET_ELEMS) + j > lun_cnt) + break; + int_to_scsilun(lun++, lun_p); + } + if (j < RL_BUCKET_ELEMS) + break; + n = j * sz_lun; + res = p_fill_from_dev_buffer(scp, arr, n, off_rsp); + if (res) + return res; + off_rsp += n; + } + if (wlun_cnt) { + int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p); + ++j; + } + if (j > 0) + res = p_fill_from_dev_buffer(scp, arr, j * sz_lun, off_rsp); return res; } @@ -4900,12 +4940,11 @@ static int __init scsi_debug_init(void) } switch (sdebug_dif) { - - case SD_DIF_TYPE0_PROTECTION: + case T10_PI_TYPE0_PROTECTION: break; - case SD_DIF_TYPE1_PROTECTION: - case SD_DIF_TYPE2_PROTECTION: - case SD_DIF_TYPE3_PROTECTION: + case T10_PI_TYPE1_PROTECTION: + case T10_PI_TYPE2_PROTECTION: + case T10_PI_TYPE3_PROTECTION: have_dif_prot = true; break; @@ -4987,7 +5026,7 @@ static int __init scsi_debug_init(void) if (sdebug_dix) { int dif_size; - dif_size = sdebug_store_sectors * sizeof(struct sd_dif_tuple); + dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple); dif_storep = vmalloc(dif_size); pr_err("dif_storep %u bytes @ %p\n", dif_size, dif_storep); @@ -5441,19 +5480,19 @@ static int sdebug_driver_probe(struct device * dev) switch (sdebug_dif) { - case SD_DIF_TYPE1_PROTECTION: + case T10_PI_TYPE1_PROTECTION: hprot = SHOST_DIF_TYPE1_PROTECTION; if (sdebug_dix) hprot |= SHOST_DIX_TYPE1_PROTECTION; break; - case SD_DIF_TYPE2_PROTECTION: + case T10_PI_TYPE2_PROTECTION: hprot = SHOST_DIF_TYPE2_PROTECTION; if (sdebug_dix) hprot |= SHOST_DIX_TYPE2_PROTECTION; break; - case SD_DIF_TYPE3_PROTECTION: + case T10_PI_TYPE3_PROTECTION: hprot = SHOST_DIF_TYPE3_PROTECTION; if (sdebug_dix) hprot |= SHOST_DIX_TYPE3_PROTECTION; |