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