^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * sd_dif.c - SCSI Data Integrity Field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2007, 2008 Oracle Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Written by: Martin K. Petersen <martin.petersen@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/t10-pi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <scsi/scsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <scsi/scsi_cmnd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <scsi/scsi_dbg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <scsi/scsi_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <scsi/scsi_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <scsi/scsi_eh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <scsi/scsi_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <scsi/scsi_ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <scsi/scsicam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "sd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Configure exchange of protection information between OS and HBA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void sd_dif_config_host(struct scsi_disk *sdkp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct scsi_device *sdp = sdkp->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct gendisk *disk = sdkp->disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u8 type = sdkp->protection_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct blk_integrity bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int dif, dix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) dif = scsi_host_dif_capable(sdp->host, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) dix = scsi_host_dix_capable(sdp->host, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) dif = 0; dix = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!dix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) memset(&bi, 0, sizeof(bi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Enable DMA of protection information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (type == T10_PI_TYPE3_PROTECTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) bi.profile = &t10_pi_type3_ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) bi.profile = &t10_pi_type1_ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) bi.flags |= BLK_INTEGRITY_IP_CHECKSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (type == T10_PI_TYPE3_PROTECTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) bi.profile = &t10_pi_type3_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) bi.profile = &t10_pi_type1_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) bi.tuple_size = sizeof(struct t10_pi_tuple);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) sd_printk(KERN_NOTICE, sdkp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) "Enabling DIX %s protection\n", bi.profile->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (dif && type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) bi.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (!sdkp->ATO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (type == T10_PI_TYPE3_PROTECTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) bi.tag_size = sizeof(u16) + sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) bi.tag_size = sizeof(u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) bi.tag_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) blk_integrity_register(disk, &bi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)