Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1 | /* |
| 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. Petersen | 2341c2f | 2014-09-26 19:20:07 -0400 | [diff] [blame] | 24 | #include <linux/t10-pi.h> |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 25 | |
| 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. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 36 | #include "sd.h" |
| 37 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 38 | /* |
| 39 | * Configure exchange of protection information between OS and HBA. |
| 40 | */ |
| 41 | void 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. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 46 | struct blk_integrity bi; |
Martin K. Petersen | 9e06688 | 2008-09-19 18:47:21 -0400 | [diff] [blame] | 47 | int dif, dix; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 48 | |
Martin K. Petersen | 9e06688 | 2008-09-19 18:47:21 -0400 | [diff] [blame] | 49 | dif = scsi_host_dif_capable(sdp->host, type); |
| 50 | dix = scsi_host_dix_capable(sdp->host, type); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 51 | |
Martin K. Petersen | 9e06688 | 2008-09-19 18:47:21 -0400 | [diff] [blame] | 52 | if (!dix && scsi_host_dix_capable(sdp->host, 0)) { |
| 53 | dif = 0; dix = 1; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Martin K. Petersen | 9e06688 | 2008-09-19 18:47:21 -0400 | [diff] [blame] | 56 | if (!dix) |
| 57 | return; |
| 58 | |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 59 | memset(&bi, 0, sizeof(bi)); |
| 60 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 61 | /* Enable DMA of protection information */ |
Martin K. Petersen | aae7df5 | 2014-09-26 19:20:05 -0400 | [diff] [blame] | 62 | if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP) { |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 63 | if (type == T10_PI_TYPE3_PROTECTION) |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 64 | bi.profile = &t10_pi_type3_ip; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 65 | else |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 66 | bi.profile = &t10_pi_type1_ip; |
Martin K. Petersen | aae7df5 | 2014-09-26 19:20:05 -0400 | [diff] [blame] | 67 | |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 68 | bi.flags |= BLK_INTEGRITY_IP_CHECKSUM; |
Martin K. Petersen | aae7df5 | 2014-09-26 19:20:05 -0400 | [diff] [blame] | 69 | } else |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 70 | if (type == T10_PI_TYPE3_PROTECTION) |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 71 | bi.profile = &t10_pi_type3_crc; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 72 | else |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 73 | bi.profile = &t10_pi_type1_crc; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 74 | |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 75 | bi.tuple_size = sizeof(struct t10_pi_tuple); |
Martin K. Petersen | cbdc144 | 2008-10-01 01:37:21 -0400 | [diff] [blame] | 76 | sd_printk(KERN_NOTICE, sdkp, |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 77 | "Enabling DIX %s protection\n", bi.profile->name); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 78 | |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 79 | if (dif && type) { |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 80 | bi.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 81 | |
Martin K. Petersen | e557990 | 2015-04-14 17:11:03 -0400 | [diff] [blame] | 82 | if (!sdkp->ATO) |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 83 | goto out; |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 84 | |
Christoph Hellwig | 8475c81 | 2016-09-11 19:35:41 +0200 | [diff] [blame] | 85 | if (type == T10_PI_TYPE3_PROTECTION) |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 86 | bi.tag_size = sizeof(u16) + sizeof(u32); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 87 | else |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 88 | bi.tag_size = sizeof(u16); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 89 | |
Martin K. Petersen | cbdc144 | 2008-10-01 01:37:21 -0400 | [diff] [blame] | 90 | sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n", |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 91 | bi.tag_size); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 92 | } |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 93 | |
| 94 | out: |
| 95 | blk_integrity_register(disk, &bi); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 96 | } |
| 97 | |