Skip to content

Commit c611529

Browse files
martinkpetersenaxboe
authored andcommitted
sd: Honor block layer integrity handling flags
A set of flags introduced in the block layer enable better control over how protection information is handled. These flags are useful for both error injection and data recovery purposes. Checking can be enabled and disabled for controller and disk, and the guard tag format is now a per-I/O property. Update sd_protect_op to communicate the relevant information to the low-level device driver via a set of flags in scsi_cmnd. Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com> Reviewed-by: Sagi Grimberg <sagig@mellanox.com> Acked-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 5829405 commit c611529

5 files changed

Lines changed: 166 additions & 65 deletions

File tree

‎drivers/scsi/sd.c‎

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -610,29 +610,44 @@ static void scsi_disk_put(struct scsi_disk *sdkp)
610610
mutex_unlock(&sd_ref_mutex);
611611
}
612612

613-
static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
614-
{
615-
unsigned int prot_op = SCSI_PROT_NORMAL;
616-
unsigned int dix = scsi_prot_sg_count(scmd);
617-
618-
if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
619-
if (dif && dix)
620-
prot_op = SCSI_PROT_READ_PASS;
621-
else if (dif && !dix)
622-
prot_op = SCSI_PROT_READ_STRIP;
623-
else if (!dif && dix)
624-
prot_op = SCSI_PROT_READ_INSERT;
625-
} else {
626-
if (dif && dix)
627-
prot_op = SCSI_PROT_WRITE_PASS;
628-
else if (dif && !dix)
629-
prot_op = SCSI_PROT_WRITE_INSERT;
630-
else if (!dif && dix)
631-
prot_op = SCSI_PROT_WRITE_STRIP;
613+
614+
615+
static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
616+
unsigned int dix, unsigned int dif)
617+
{
618+
struct bio *bio = scmd->request->bio;
619+
unsigned int prot_op = sd_prot_op(rq_data_dir(scmd->request), dix, dif);
620+
unsigned int protect = 0;
621+
622+
if (dix) { /* DIX Type 0, 1, 2, 3 */
623+
if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM))
624+
scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM;
625+
626+
if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
627+
scmd->prot_flags |= SCSI_PROT_GUARD_CHECK;
628+
}
629+
630+
if (dif != SD_DIF_TYPE3_PROTECTION) { /* DIX/DIF Type 0, 1, 2 */
631+
scmd->prot_flags |= SCSI_PROT_REF_INCREMENT;
632+
633+
if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
634+
scmd->prot_flags |= SCSI_PROT_REF_CHECK;
635+
}
636+
637+
if (dif) { /* DIX/DIF Type 1, 2, 3 */
638+
scmd->prot_flags |= SCSI_PROT_TRANSFER_PI;
639+
640+
if (bio_integrity_flagged(bio, BIP_DISK_NOCHECK))
641+
protect = 3 << 5; /* Disable target PI checking */
642+
else
643+
protect = 1 << 5; /* Enable target PI checking */
632644
}
633645

634646
scsi_set_prot_op(scmd, prot_op);
635647
scsi_set_prot_type(scmd, dif);
648+
scmd->prot_flags &= sd_prot_flag_mask(prot_op);
649+
650+
return protect;
636651
}
637652

638653
static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
@@ -893,7 +908,8 @@ static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
893908
sector_t block = blk_rq_pos(rq);
894909
sector_t threshold;
895910
unsigned int this_count = blk_rq_sectors(rq);
896-
int ret, host_dif;
911+
unsigned int dif, dix;
912+
int ret;
897913
unsigned char protect;
898914

899915
ret = scsi_init_io(SCpnt, GFP_ATOMIC);
@@ -995,7 +1011,7 @@ static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
9951011
SCpnt->cmnd[0] = WRITE_6;
9961012

9971013
if (blk_integrity_rq(rq))
998-
sd_dif_prepare(rq, block, sdp->sector_size);
1014+
sd_dif_prepare(SCpnt);
9991015

10001016
} else if (rq_data_dir(rq) == READ) {
10011017
SCpnt->cmnd[0] = READ_6;
@@ -1010,14 +1026,15 @@ static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
10101026
"writing" : "reading", this_count,
10111027
blk_rq_sectors(rq)));
10121028

1013-
/* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
1014-
host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
1015-
if (host_dif)
1016-
protect = 1 << 5;
1029+
dix = scsi_prot_sg_count(SCpnt);
1030+
dif = scsi_host_dif_capable(SCpnt->device->host, sdkp->protection_type);
1031+
1032+
if (dif || dix)
1033+
protect = sd_setup_protect_cmnd(SCpnt, dix, dif);
10171034
else
10181035
protect = 0;
10191036

1020-
if (host_dif == SD_DIF_TYPE2_PROTECTION) {
1037+
if (protect && sdkp->protection_type == SD_DIF_TYPE2_PROTECTION) {
10211038
SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
10221039

10231040
if (unlikely(SCpnt->cmnd == NULL)) {
@@ -1102,10 +1119,6 @@ static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
11021119
}
11031120
SCpnt->sdb.length = this_count * sdp->sector_size;
11041121

1105-
/* If DIF or DIX is enabled, tell HBA how to handle request */
1106-
if (host_dif || scsi_prot_sg_count(SCpnt))
1107-
sd_prot_op(SCpnt, host_dif);
1108-
11091122
/*
11101123
* We shouldn't disconnect in the middle of a sector, so with a dumb
11111124
* host adapter, it's safe to assume that we can at least transfer

‎drivers/scsi/sd.h‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,68 @@ enum sd_dif_target_protection_types {
166166
SD_DIF_TYPE3_PROTECTION = 0x3,
167167
};
168168

169+
/*
170+
* Look up the DIX operation based on whether the command is read or
171+
* write and whether dix and dif are enabled.
172+
*/
173+
static inline unsigned int sd_prot_op(bool write, bool dix, bool dif)
174+
{
175+
/* Lookup table: bit 2 (write), bit 1 (dix), bit 0 (dif) */
176+
const unsigned int ops[] = { /* wrt dix dif */
177+
SCSI_PROT_NORMAL, /* 0 0 0 */
178+
SCSI_PROT_READ_STRIP, /* 0 0 1 */
179+
SCSI_PROT_READ_INSERT, /* 0 1 0 */
180+
SCSI_PROT_READ_PASS, /* 0 1 1 */
181+
SCSI_PROT_NORMAL, /* 1 0 0 */
182+
SCSI_PROT_WRITE_INSERT, /* 1 0 1 */
183+
SCSI_PROT_WRITE_STRIP, /* 1 1 0 */
184+
SCSI_PROT_WRITE_PASS, /* 1 1 1 */
185+
};
186+
187+
return ops[write << 2 | dix << 1 | dif];
188+
}
189+
190+
/*
191+
* Returns a mask of the protection flags that are valid for a given DIX
192+
* operation.
193+
*/
194+
static inline unsigned int sd_prot_flag_mask(unsigned int prot_op)
195+
{
196+
const unsigned int flag_mask[] = {
197+
[SCSI_PROT_NORMAL] = 0,
198+
199+
[SCSI_PROT_READ_STRIP] = SCSI_PROT_TRANSFER_PI |
200+
SCSI_PROT_GUARD_CHECK |
201+
SCSI_PROT_REF_CHECK |
202+
SCSI_PROT_REF_INCREMENT,
203+
204+
[SCSI_PROT_READ_INSERT] = SCSI_PROT_REF_INCREMENT |
205+
SCSI_PROT_IP_CHECKSUM,
206+
207+
[SCSI_PROT_READ_PASS] = SCSI_PROT_TRANSFER_PI |
208+
SCSI_PROT_GUARD_CHECK |
209+
SCSI_PROT_REF_CHECK |
210+
SCSI_PROT_REF_INCREMENT |
211+
SCSI_PROT_IP_CHECKSUM,
212+
213+
[SCSI_PROT_WRITE_INSERT] = SCSI_PROT_TRANSFER_PI |
214+
SCSI_PROT_REF_INCREMENT,
215+
216+
[SCSI_PROT_WRITE_STRIP] = SCSI_PROT_GUARD_CHECK |
217+
SCSI_PROT_REF_CHECK |
218+
SCSI_PROT_REF_INCREMENT |
219+
SCSI_PROT_IP_CHECKSUM,
220+
221+
[SCSI_PROT_WRITE_PASS] = SCSI_PROT_TRANSFER_PI |
222+
SCSI_PROT_GUARD_CHECK |
223+
SCSI_PROT_REF_CHECK |
224+
SCSI_PROT_REF_INCREMENT |
225+
SCSI_PROT_IP_CHECKSUM,
226+
};
227+
228+
return flag_mask[prot_op];
229+
}
230+
169231
/*
170232
* Data Integrity Field tuple.
171233
*/
@@ -178,15 +240,15 @@ struct sd_dif_tuple {
178240
#ifdef CONFIG_BLK_DEV_INTEGRITY
179241

180242
extern void sd_dif_config_host(struct scsi_disk *);
181-
extern void sd_dif_prepare(struct request *rq, sector_t, unsigned int);
243+
extern void sd_dif_prepare(struct scsi_cmnd *scmd);
182244
extern void sd_dif_complete(struct scsi_cmnd *, unsigned int);
183245

184246
#else /* CONFIG_BLK_DEV_INTEGRITY */
185247

186248
static inline void sd_dif_config_host(struct scsi_disk *disk)
187249
{
188250
}
189-
static inline int sd_dif_prepare(struct request *rq, sector_t s, unsigned int a)
251+
static inline int sd_dif_prepare(struct scsi_cmnd *scmd)
190252
{
191253
return 0;
192254
}

‎drivers/scsi/sd_dif.c‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,22 @@ void sd_dif_config_host(struct scsi_disk *sdkp)
106106
*
107107
* Type 3 does not have a reference tag so no remapping is required.
108108
*/
109-
void sd_dif_prepare(struct request *rq, sector_t hw_sector,
110-
unsigned int sector_sz)
109+
void sd_dif_prepare(struct scsi_cmnd *scmd)
111110
{
112111
const int tuple_sz = sizeof(struct t10_pi_tuple);
113112
struct bio *bio;
114113
struct scsi_disk *sdkp;
115114
struct t10_pi_tuple *pi;
116115
u32 phys, virt;
117116

118-
sdkp = rq->bio->bi_bdev->bd_disk->private_data;
117+
sdkp = scsi_disk(scmd->request->rq_disk);
119118

120119
if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION)
121120
return;
122121

123-
phys = hw_sector & 0xffffffff;
122+
phys = scsi_prot_ref_tag(scmd);
124123

125-
__rq_for_each_bio(bio, rq) {
124+
__rq_for_each_bio(bio, scmd->request) {
126125
struct bio_integrity_payload *bip = bio_integrity(bio);
127126
struct bio_vec iv;
128127
struct bvec_iter iter;
@@ -163,20 +162,16 @@ void sd_dif_complete(struct scsi_cmnd *scmd, unsigned int good_bytes)
163162
struct scsi_disk *sdkp;
164163
struct bio *bio;
165164
struct t10_pi_tuple *pi;
166-
unsigned int j, sectors, sector_sz;
165+
unsigned int j, intervals;
167166
u32 phys, virt;
168167

169168
sdkp = scsi_disk(scmd->request->rq_disk);
170169

171170
if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION || good_bytes == 0)
172171
return;
173172

174-
sector_sz = scmd->device->sector_size;
175-
sectors = good_bytes / sector_sz;
176-
177-
phys = blk_rq_pos(scmd->request) & 0xffffffff;
178-
if (sector_sz == 4096)
179-
phys >>= 3;
173+
intervals = good_bytes / scsi_prot_interval(scmd);
174+
phys = scsi_prot_ref_tag(scmd);
180175

181176
__rq_for_each_bio(bio, scmd->request) {
182177
struct bio_integrity_payload *bip = bio_integrity(bio);
@@ -190,7 +185,7 @@ void sd_dif_complete(struct scsi_cmnd *scmd, unsigned int good_bytes)
190185

191186
for (j = 0; j < iv.bv_len; j += tuple_sz, pi++) {
192187

193-
if (sectors == 0) {
188+
if (intervals == 0) {
194189
kunmap_atomic(pi);
195190
return;
196191
}
@@ -200,7 +195,7 @@ void sd_dif_complete(struct scsi_cmnd *scmd, unsigned int good_bytes)
200195

201196
virt++;
202197
phys++;
203-
sectors--;
198+
intervals--;
204199
}
205200

206201
kunmap_atomic(pi);

‎include/linux/bio.h‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ static inline unsigned bio_segments(struct bio *bio)
292292
*/
293293
#define bio_get(bio) atomic_inc(&(bio)->bi_cnt)
294294

295+
enum bip_flags {
296+
BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
297+
BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
298+
BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
299+
BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
300+
BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
301+
};
302+
295303
#if defined(CONFIG_BLK_DEV_INTEGRITY)
296304

297305
static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
@@ -323,13 +331,15 @@ struct bio_integrity_payload {
323331
struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
324332
};
325333

326-
enum bip_flags {
327-
BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
328-
BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
329-
BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
330-
BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
331-
BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
332-
};
334+
static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
335+
{
336+
struct bio_integrity_payload *bip = bio_integrity(bio);
337+
338+
if (bip)
339+
return bip->bip_flags & flag;
340+
341+
return false;
342+
}
333343

334344
static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
335345
{
@@ -701,9 +711,9 @@ extern void bio_integrity_init(void);
701711

702712
#else /* CONFIG_BLK_DEV_INTEGRITY */
703713

704-
static inline int bio_integrity(struct bio *bio)
714+
static inline void *bio_integrity(struct bio *bio)
705715
{
706-
return 0;
716+
return NULL;
707717
}
708718

709719
static inline bool bio_integrity_enabled(struct bio *bio)
@@ -754,6 +764,11 @@ static inline void bio_integrity_init(void)
754764
return;
755765
}
756766

767+
static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
768+
{
769+
return false;
770+
}
771+
757772
#endif /* CONFIG_BLK_DEV_INTEGRITY */
758773

759774
#endif /* CONFIG_BLOCK */

0 commit comments

Comments
 (0)