1a98c5b19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2af55ff67SMartin K. Petersen /*
3af55ff67SMartin K. Petersen * sd_dif.c - SCSI Data Integrity Field
4af55ff67SMartin K. Petersen *
5af55ff67SMartin K. Petersen * Copyright (C) 2007, 2008 Oracle Corporation
6af55ff67SMartin K. Petersen * Written by: Martin K. Petersen <martin.petersen@oracle.com>
7af55ff67SMartin K. Petersen */
8af55ff67SMartin K. Petersen
9fe45e630SChristoph Hellwig #include <linux/blk-integrity.h>
102341c2f8SMartin K. Petersen #include <linux/t10-pi.h>
11af55ff67SMartin K. Petersen
12af55ff67SMartin K. Petersen #include <scsi/scsi.h>
13af55ff67SMartin K. Petersen #include <scsi/scsi_cmnd.h>
14af55ff67SMartin K. Petersen #include <scsi/scsi_dbg.h>
15af55ff67SMartin K. Petersen #include <scsi/scsi_device.h>
16af55ff67SMartin K. Petersen #include <scsi/scsi_driver.h>
17af55ff67SMartin K. Petersen #include <scsi/scsi_eh.h>
18af55ff67SMartin K. Petersen #include <scsi/scsi_host.h>
19af55ff67SMartin K. Petersen #include <scsi/scsi_ioctl.h>
20af55ff67SMartin K. Petersen #include <scsi/scsicam.h>
21af55ff67SMartin K. Petersen
22af55ff67SMartin K. Petersen #include "sd.h"
23af55ff67SMartin K. Petersen
24af55ff67SMartin K. Petersen /*
25af55ff67SMartin K. Petersen * Configure exchange of protection information between OS and HBA.
26af55ff67SMartin K. Petersen */
sd_dif_config_host(struct scsi_disk * sdkp,struct queue_limits * lim)27*c6e56cf6SChristoph Hellwig void sd_dif_config_host(struct scsi_disk *sdkp, struct queue_limits *lim)
28af55ff67SMartin K. Petersen {
29af55ff67SMartin K. Petersen struct scsi_device *sdp = sdkp->device;
30af55ff67SMartin K. Petersen u8 type = sdkp->protection_type;
31*c6e56cf6SChristoph Hellwig struct blk_integrity *bi = &lim->integrity;
329e06688eSMartin K. Petersen int dif, dix;
33af55ff67SMartin K. Petersen
34*c6e56cf6SChristoph Hellwig memset(bi, 0, sizeof(*bi));
35*c6e56cf6SChristoph Hellwig
369e06688eSMartin K. Petersen dif = scsi_host_dif_capable(sdp->host, type);
379e06688eSMartin K. Petersen dix = scsi_host_dix_capable(sdp->host, type);
38af55ff67SMartin K. Petersen
399e06688eSMartin K. Petersen if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
409e06688eSMartin K. Petersen dif = 0; dix = 1;
41af55ff67SMartin K. Petersen }
42af55ff67SMartin K. Petersen
43*c6e56cf6SChristoph Hellwig if (!dix)
449e06688eSMartin K. Petersen return;
450f8087ecSMartin K. Petersen
46af55ff67SMartin K. Petersen /* Enable DMA of protection information */
47e9f5f44aSChristoph Hellwig if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP)
48*c6e56cf6SChristoph Hellwig bi->csum_type = BLK_INTEGRITY_CSUM_IP;
49af55ff67SMartin K. Petersen else
50*c6e56cf6SChristoph Hellwig bi->csum_type = BLK_INTEGRITY_CSUM_CRC;
51aae7df50SMartin K. Petersen
52e9f5f44aSChristoph Hellwig if (type != T10_PI_TYPE3_PROTECTION)
53*c6e56cf6SChristoph Hellwig bi->flags |= BLK_INTEGRITY_REF_TAG;
54af55ff67SMartin K. Petersen
55*c6e56cf6SChristoph Hellwig bi->tuple_size = sizeof(struct t10_pi_tuple);
56af55ff67SMartin K. Petersen
573aec2f41SMartin K. Petersen if (dif && type) {
58*c6e56cf6SChristoph Hellwig bi->flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
593aec2f41SMartin K. Petersen
60e557990eSMartin K. Petersen if (!sdkp->ATO)
61*c6e56cf6SChristoph Hellwig return;
623aec2f41SMartin K. Petersen
638475c811SChristoph Hellwig if (type == T10_PI_TYPE3_PROTECTION)
64*c6e56cf6SChristoph Hellwig bi->tag_size = sizeof(u16) + sizeof(u32);
65af55ff67SMartin K. Petersen else
66*c6e56cf6SChristoph Hellwig bi->tag_size = sizeof(u16);
67af55ff67SMartin K. Petersen }
680f8087ecSMartin K. Petersen
6926a02d97SXingui Yang sd_first_printk(KERN_NOTICE, sdkp,
701e029397SMartin K. Petersen "Enabling DIX %s, application tag size %u bytes\n",
71*c6e56cf6SChristoph Hellwig blk_integrity_profile_name(bi), bi->tag_size);
72af55ff67SMartin K. Petersen }
73