blob: 349950616adc504925e39ad3950308f7190d7ab9 [file] [log] [blame]
Thomas Gleixnera98c5b12019-06-01 10:08:25 +02001// SPDX-License-Identifier: GPL-2.0-only
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04002/*
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>
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04007 */
8
Christoph Hellwigfe45e632021-09-20 14:33:27 +02009#include <linux/blk-integrity.h>
Martin K. Petersen2341c2f82014-09-26 19:20:07 -040010#include <linux/t10-pi.h>
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040011
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
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040022#include "sd.h"
23
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040024/*
25 * Configure exchange of protection information between OS and HBA.
26 */
27void sd_dif_config_host(struct scsi_disk *sdkp)
28{
29 struct scsi_device *sdp = sdkp->device;
30 struct gendisk *disk = sdkp->disk;
31 u8 type = sdkp->protection_type;
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040032 struct blk_integrity bi;
Martin K. Petersen9e066882008-09-19 18:47:21 -040033 int dif, dix;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040034
Martin K. Petersen9e066882008-09-19 18:47:21 -040035 dif = scsi_host_dif_capable(sdp->host, type);
36 dix = scsi_host_dix_capable(sdp->host, type);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040037
Martin K. Petersen9e066882008-09-19 18:47:21 -040038 if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
39 dif = 0; dix = 1;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040040 }
41
Martin K. Petersen9e066882008-09-19 18:47:21 -040042 if (!dix)
43 return;
44
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040045 memset(&bi, 0, sizeof(bi));
46
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040047 /* Enable DMA of protection information */
Martin K. Petersenaae7df52014-09-26 19:20:05 -040048 if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP) {
Christoph Hellwig8475c812016-09-11 19:35:41 +020049 if (type == T10_PI_TYPE3_PROTECTION)
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040050 bi.profile = &t10_pi_type3_ip;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040051 else
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040052 bi.profile = &t10_pi_type1_ip;
Martin K. Petersenaae7df52014-09-26 19:20:05 -040053
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040054 bi.flags |= BLK_INTEGRITY_IP_CHECKSUM;
Martin K. Petersenaae7df52014-09-26 19:20:05 -040055 } else
Christoph Hellwig8475c812016-09-11 19:35:41 +020056 if (type == T10_PI_TYPE3_PROTECTION)
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040057 bi.profile = &t10_pi_type3_crc;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040058 else
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040059 bi.profile = &t10_pi_type1_crc;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040060
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040061 bi.tuple_size = sizeof(struct t10_pi_tuple);
Martin K. Petersencbdc1442008-10-01 01:37:21 -040062 sd_printk(KERN_NOTICE, sdkp,
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040063 "Enabling DIX %s protection\n", bi.profile->name);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040064
Martin K. Petersen3aec2f42014-09-26 19:20:03 -040065 if (dif && type) {
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040066 bi.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Martin K. Petersen3aec2f42014-09-26 19:20:03 -040067
Martin K. Petersene5579902015-04-14 17:11:03 -040068 if (!sdkp->ATO)
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040069 goto out;
Martin K. Petersen3aec2f42014-09-26 19:20:03 -040070
Christoph Hellwig8475c812016-09-11 19:35:41 +020071 if (type == T10_PI_TYPE3_PROTECTION)
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040072 bi.tag_size = sizeof(u16) + sizeof(u32);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040073 else
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040074 bi.tag_size = sizeof(u16);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040075
Martin K. Petersencbdc1442008-10-01 01:37:21 -040076 sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n",
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040077 bi.tag_size);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040078 }
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040079
80out:
81 blk_integrity_register(disk, &bi);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040082}
83