blob: db72c82486e379ad6f92233cc0c3ceda6ce81bab [file] [log] [blame]
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001/*
2 * sd_dif.c - SCSI Data Integrity Field
3 *
4 * Copyright (C) 2007, 2008 Oracle Corporation
5 * Written by: Martin K. Petersen <martin.petersen@oracle.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
19 * USA.
20 *
21 */
22
23#include <linux/blkdev.h>
Martin K. Petersen2341c2f2014-09-26 19:20:07 -040024#include <linux/t10-pi.h>
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040025
26#include <scsi/scsi.h>
27#include <scsi/scsi_cmnd.h>
28#include <scsi/scsi_dbg.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_driver.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_host.h>
33#include <scsi/scsi_ioctl.h>
34#include <scsi/scsicam.h>
35
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040036#include "sd.h"
37
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040038/*
39 * Configure exchange of protection information between OS and HBA.
40 */
41void sd_dif_config_host(struct scsi_disk *sdkp)
42{
43 struct scsi_device *sdp = sdkp->device;
44 struct gendisk *disk = sdkp->disk;
45 u8 type = sdkp->protection_type;
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040046 struct blk_integrity bi;
Martin K. Petersen9e066882008-09-19 18:47:21 -040047 int dif, dix;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040048
Martin K. Petersen9e066882008-09-19 18:47:21 -040049 dif = scsi_host_dif_capable(sdp->host, type);
50 dix = scsi_host_dix_capable(sdp->host, type);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040051
Martin K. Petersen9e066882008-09-19 18:47:21 -040052 if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
53 dif = 0; dix = 1;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040054 }
55
Martin K. Petersen9e066882008-09-19 18:47:21 -040056 if (!dix)
57 return;
58
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040059 memset(&bi, 0, sizeof(bi));
60
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040061 /* Enable DMA of protection information */
Martin K. Petersenaae7df52014-09-26 19:20:05 -040062 if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP) {
Christoph Hellwig8475c812016-09-11 19:35:41 +020063 if (type == T10_PI_TYPE3_PROTECTION)
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040064 bi.profile = &t10_pi_type3_ip;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040065 else
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040066 bi.profile = &t10_pi_type1_ip;
Martin K. Petersenaae7df52014-09-26 19:20:05 -040067
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040068 bi.flags |= BLK_INTEGRITY_IP_CHECKSUM;
Martin K. Petersenaae7df52014-09-26 19:20:05 -040069 } else
Christoph Hellwig8475c812016-09-11 19:35:41 +020070 if (type == T10_PI_TYPE3_PROTECTION)
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040071 bi.profile = &t10_pi_type3_crc;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040072 else
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040073 bi.profile = &t10_pi_type1_crc;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040074
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040075 bi.tuple_size = sizeof(struct t10_pi_tuple);
Martin K. Petersencbdc1442008-10-01 01:37:21 -040076 sd_printk(KERN_NOTICE, sdkp,
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040077 "Enabling DIX %s protection\n", bi.profile->name);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040078
Martin K. Petersen3aec2f42014-09-26 19:20:03 -040079 if (dif && type) {
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040080 bi.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Martin K. Petersen3aec2f42014-09-26 19:20:03 -040081
Martin K. Petersene5579902015-04-14 17:11:03 -040082 if (!sdkp->ATO)
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040083 goto out;
Martin K. Petersen3aec2f42014-09-26 19:20:03 -040084
Christoph Hellwig8475c812016-09-11 19:35:41 +020085 if (type == T10_PI_TYPE3_PROTECTION)
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040086 bi.tag_size = sizeof(u16) + sizeof(u32);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040087 else
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040088 bi.tag_size = sizeof(u16);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040089
Martin K. Petersencbdc1442008-10-01 01:37:21 -040090 sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n",
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040091 bi.tag_size);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040092 }
Martin K. Petersen0f8087e2015-10-21 13:19:33 -040093
94out:
95 blk_integrity_register(disk, &bi);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040096}
97