xref: /linux/drivers/scsi/sd_dif.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * sd_dif.c - SCSI Data Integrity Field
4  *
5  * Copyright (C) 2007, 2008 Oracle Corporation
6  * Written by: Martin K. Petersen <martin.petersen@oracle.com>
7  */
8 
9 #include <linux/blk-integrity.h>
10 #include <linux/t10-pi.h>
11 
12 #include <scsi/scsi.h>
13 #include <scsi/scsi_cmnd.h>
14 #include <scsi/scsi_dbg.h>
15 #include <scsi/scsi_device.h>
16 #include <scsi/scsi_driver.h>
17 #include <scsi/scsi_eh.h>
18 #include <scsi/scsi_host.h>
19 #include <scsi/scsi_ioctl.h>
20 #include <scsi/scsicam.h>
21 
22 #include "sd.h"
23 
24 /*
25  * Configure exchange of protection information between OS and HBA.
26  */
27 void sd_dif_config_host(struct scsi_disk *sdkp, struct queue_limits *lim)
28 {
29 	struct scsi_device *sdp = sdkp->device;
30 	u8 type = sdkp->protection_type;
31 	struct blk_integrity *bi = &lim->integrity;
32 	int dif, dix;
33 
34 	memset(bi, 0, sizeof(*bi));
35 
36 	dif = scsi_host_dif_capable(sdp->host, type);
37 	dix = scsi_host_dix_capable(sdp->host, type);
38 
39 	if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
40 		dif = 0; dix = 1;
41 	}
42 
43 	if (!dix)
44 		return;
45 
46 	/* Enable DMA of protection information */
47 	if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP)
48 		bi->csum_type = BLK_INTEGRITY_CSUM_IP;
49 	else
50 		bi->csum_type = BLK_INTEGRITY_CSUM_CRC;
51 
52 	if (type != T10_PI_TYPE3_PROTECTION)
53 		bi->flags |= BLK_INTEGRITY_REF_TAG;
54 
55 	bi->metadata_size = sizeof(struct t10_pi_tuple);
56 	bi->pi_tuple_size = bi->metadata_size;
57 
58 	if (dif && type) {
59 		bi->flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
60 
61 		if (!sdkp->ATO)
62 			return;
63 
64 		if (type == T10_PI_TYPE3_PROTECTION)
65 			bi->tag_size = sizeof(u16) + sizeof(u32);
66 		else
67 			bi->tag_size = sizeof(u16);
68 	}
69 
70 	sd_first_printk(KERN_NOTICE, sdkp,
71 			"Enabling DIX %s, application tag size %u bytes\n",
72 			blk_integrity_profile_name(bi), bi->tag_size);
73 }
74