@@ -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
638653static 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
0 commit comments