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> |
| 24 | #include <linux/crc-t10dif.h> |
| 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 | |
| 36 | #include <net/checksum.h> |
| 37 | |
| 38 | #include "sd.h" |
| 39 | |
| 40 | typedef __u16 (csum_fn) (void *, unsigned int); |
| 41 | |
| 42 | static __u16 sd_dif_crc_fn(void *data, unsigned int len) |
| 43 | { |
| 44 | return cpu_to_be16(crc_t10dif(data, len)); |
| 45 | } |
| 46 | |
| 47 | static __u16 sd_dif_ip_fn(void *data, unsigned int len) |
| 48 | { |
| 49 | return ip_compute_csum(data, len); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Type 1 and Type 2 protection use the same format: 16 bit guard tag, |
| 54 | * 16 bit app tag, 32 bit reference tag. |
| 55 | */ |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 56 | static void sd_dif_type1_generate(struct blk_integrity_iter *iter, csum_fn *fn) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 57 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 58 | void *buf = iter->data_buf; |
| 59 | struct sd_dif_tuple *sdt = iter->prot_buf; |
| 60 | sector_t seed = iter->seed; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 61 | unsigned int i; |
| 62 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 63 | for (i = 0 ; i < iter->data_size ; i += iter->interval, sdt++) { |
| 64 | sdt->guard_tag = fn(buf, iter->interval); |
Martin K. Petersen | 3be91c4 | 2014-09-26 19:19:59 -0400 | [diff] [blame] | 65 | sdt->ref_tag = cpu_to_be32(seed & 0xffffffff); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 66 | sdt->app_tag = 0; |
| 67 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 68 | buf += iter->interval; |
Martin K. Petersen | 3be91c4 | 2014-09-26 19:19:59 -0400 | [diff] [blame] | 69 | seed++; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 73 | static int sd_dif_type1_generate_crc(struct blk_integrity_iter *iter) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 74 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 75 | sd_dif_type1_generate(iter, sd_dif_crc_fn); |
| 76 | return 0; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 79 | static int sd_dif_type1_generate_ip(struct blk_integrity_iter *iter) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 80 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 81 | sd_dif_type1_generate(iter, sd_dif_ip_fn); |
| 82 | return 0; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 85 | static int sd_dif_type1_verify(struct blk_integrity_iter *iter, csum_fn *fn) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 86 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 87 | void *buf = iter->data_buf; |
| 88 | struct sd_dif_tuple *sdt = iter->prot_buf; |
| 89 | sector_t seed = iter->seed; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 90 | unsigned int i; |
| 91 | __u16 csum; |
| 92 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 93 | for (i = 0 ; i < iter->data_size ; i += iter->interval, sdt++) { |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 94 | /* Unwritten sectors */ |
| 95 | if (sdt->app_tag == 0xffff) |
| 96 | return 0; |
| 97 | |
Martin K. Petersen | 3be91c4 | 2014-09-26 19:19:59 -0400 | [diff] [blame] | 98 | if (be32_to_cpu(sdt->ref_tag) != (seed & 0xffffffff)) { |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 99 | printk(KERN_ERR |
| 100 | "%s: ref tag error on sector %lu (rcvd %u)\n", |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 101 | iter->disk_name, (unsigned long)seed, |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 102 | be32_to_cpu(sdt->ref_tag)); |
| 103 | return -EIO; |
| 104 | } |
| 105 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 106 | csum = fn(buf, iter->interval); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 107 | |
| 108 | if (sdt->guard_tag != csum) { |
| 109 | printk(KERN_ERR "%s: guard tag error on sector %lu " \ |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 110 | "(rcvd %04x, data %04x)\n", iter->disk_name, |
Martin K. Petersen | 3be91c4 | 2014-09-26 19:19:59 -0400 | [diff] [blame] | 111 | (unsigned long)seed, |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 112 | be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); |
| 113 | return -EIO; |
| 114 | } |
| 115 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 116 | buf += iter->interval; |
Martin K. Petersen | 3be91c4 | 2014-09-26 19:19:59 -0400 | [diff] [blame] | 117 | seed++; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 123 | static int sd_dif_type1_verify_crc(struct blk_integrity_iter *iter) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 124 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 125 | return sd_dif_type1_verify(iter, sd_dif_crc_fn); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 128 | static int sd_dif_type1_verify_ip(struct blk_integrity_iter *iter) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 129 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 130 | return sd_dif_type1_verify(iter, sd_dif_ip_fn); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 133 | static struct blk_integrity dif_type1_integrity_crc = { |
| 134 | .name = "T10-DIF-TYPE1-CRC", |
| 135 | .generate_fn = sd_dif_type1_generate_crc, |
| 136 | .verify_fn = sd_dif_type1_verify_crc, |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 137 | .tuple_size = sizeof(struct sd_dif_tuple), |
| 138 | .tag_size = 0, |
| 139 | }; |
| 140 | |
| 141 | static struct blk_integrity dif_type1_integrity_ip = { |
| 142 | .name = "T10-DIF-TYPE1-IP", |
| 143 | .generate_fn = sd_dif_type1_generate_ip, |
| 144 | .verify_fn = sd_dif_type1_verify_ip, |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 145 | .tuple_size = sizeof(struct sd_dif_tuple), |
| 146 | .tag_size = 0, |
| 147 | }; |
| 148 | |
| 149 | |
| 150 | /* |
| 151 | * Type 3 protection has a 16-bit guard tag and 16 + 32 bits of opaque |
| 152 | * tag space. |
| 153 | */ |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 154 | static void sd_dif_type3_generate(struct blk_integrity_iter *iter, csum_fn *fn) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 155 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 156 | void *buf = iter->data_buf; |
| 157 | struct sd_dif_tuple *sdt = iter->prot_buf; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 158 | unsigned int i; |
| 159 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 160 | for (i = 0 ; i < iter->data_size ; i += iter->interval, sdt++) { |
| 161 | sdt->guard_tag = fn(buf, iter->interval); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 162 | sdt->ref_tag = 0; |
| 163 | sdt->app_tag = 0; |
| 164 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 165 | buf += iter->interval; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 169 | static int sd_dif_type3_generate_crc(struct blk_integrity_iter *iter) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 170 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 171 | sd_dif_type3_generate(iter, sd_dif_crc_fn); |
| 172 | return 0; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 175 | static int sd_dif_type3_generate_ip(struct blk_integrity_iter *iter) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 176 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 177 | sd_dif_type3_generate(iter, sd_dif_ip_fn); |
| 178 | return 0; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 181 | static int sd_dif_type3_verify(struct blk_integrity_iter *iter, csum_fn *fn) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 182 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 183 | void *buf = iter->data_buf; |
| 184 | struct sd_dif_tuple *sdt = iter->prot_buf; |
| 185 | sector_t seed = iter->seed; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 186 | unsigned int i; |
| 187 | __u16 csum; |
| 188 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 189 | for (i = 0 ; i < iter->data_size ; i += iter->interval, sdt++) { |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 190 | /* Unwritten sectors */ |
| 191 | if (sdt->app_tag == 0xffff && sdt->ref_tag == 0xffffffff) |
| 192 | return 0; |
| 193 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 194 | csum = fn(buf, iter->interval); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 195 | |
| 196 | if (sdt->guard_tag != csum) { |
| 197 | printk(KERN_ERR "%s: guard tag error on sector %lu " \ |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 198 | "(rcvd %04x, data %04x)\n", iter->disk_name, |
Martin K. Petersen | 3be91c4 | 2014-09-26 19:19:59 -0400 | [diff] [blame] | 199 | (unsigned long)seed, |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 200 | be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); |
| 201 | return -EIO; |
| 202 | } |
| 203 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 204 | buf += iter->interval; |
Martin K. Petersen | 3be91c4 | 2014-09-26 19:19:59 -0400 | [diff] [blame] | 205 | seed++; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 211 | static int sd_dif_type3_verify_crc(struct blk_integrity_iter *iter) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 212 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 213 | return sd_dif_type3_verify(iter, sd_dif_crc_fn); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 216 | static int sd_dif_type3_verify_ip(struct blk_integrity_iter *iter) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 217 | { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 218 | return sd_dif_type3_verify(iter, sd_dif_ip_fn); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 221 | static struct blk_integrity dif_type3_integrity_crc = { |
| 222 | .name = "T10-DIF-TYPE3-CRC", |
| 223 | .generate_fn = sd_dif_type3_generate_crc, |
| 224 | .verify_fn = sd_dif_type3_verify_crc, |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 225 | .tuple_size = sizeof(struct sd_dif_tuple), |
| 226 | .tag_size = 0, |
| 227 | }; |
| 228 | |
| 229 | static struct blk_integrity dif_type3_integrity_ip = { |
| 230 | .name = "T10-DIF-TYPE3-IP", |
| 231 | .generate_fn = sd_dif_type3_generate_ip, |
| 232 | .verify_fn = sd_dif_type3_verify_ip, |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 233 | .tuple_size = sizeof(struct sd_dif_tuple), |
| 234 | .tag_size = 0, |
| 235 | }; |
| 236 | |
| 237 | /* |
| 238 | * Configure exchange of protection information between OS and HBA. |
| 239 | */ |
| 240 | void sd_dif_config_host(struct scsi_disk *sdkp) |
| 241 | { |
| 242 | struct scsi_device *sdp = sdkp->device; |
| 243 | struct gendisk *disk = sdkp->disk; |
| 244 | u8 type = sdkp->protection_type; |
Martin K. Petersen | 9e06688 | 2008-09-19 18:47:21 -0400 | [diff] [blame] | 245 | int dif, dix; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 246 | |
Martin K. Petersen | 9e06688 | 2008-09-19 18:47:21 -0400 | [diff] [blame] | 247 | dif = scsi_host_dif_capable(sdp->host, type); |
| 248 | dix = scsi_host_dix_capable(sdp->host, type); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 249 | |
Martin K. Petersen | 9e06688 | 2008-09-19 18:47:21 -0400 | [diff] [blame] | 250 | if (!dix && scsi_host_dix_capable(sdp->host, 0)) { |
| 251 | dif = 0; dix = 1; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Martin K. Petersen | 9e06688 | 2008-09-19 18:47:21 -0400 | [diff] [blame] | 254 | if (!dix) |
| 255 | return; |
| 256 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 257 | /* Enable DMA of protection information */ |
Martin K. Petersen | aae7df5 | 2014-09-26 19:20:05 -0400 | [diff] [blame^] | 258 | if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP) { |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 259 | if (type == SD_DIF_TYPE3_PROTECTION) |
| 260 | blk_integrity_register(disk, &dif_type3_integrity_ip); |
| 261 | else |
| 262 | blk_integrity_register(disk, &dif_type1_integrity_ip); |
Martin K. Petersen | aae7df5 | 2014-09-26 19:20:05 -0400 | [diff] [blame^] | 263 | |
| 264 | disk->integrity->flags |= BLK_INTEGRITY_IP_CHECKSUM; |
| 265 | } else |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 266 | if (type == SD_DIF_TYPE3_PROTECTION) |
| 267 | blk_integrity_register(disk, &dif_type3_integrity_crc); |
| 268 | else |
| 269 | blk_integrity_register(disk, &dif_type1_integrity_crc); |
| 270 | |
Martin K. Petersen | cbdc144 | 2008-10-01 01:37:21 -0400 | [diff] [blame] | 271 | sd_printk(KERN_NOTICE, sdkp, |
Martin K. Petersen | 9e06688 | 2008-09-19 18:47:21 -0400 | [diff] [blame] | 272 | "Enabling DIX %s protection\n", disk->integrity->name); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 273 | |
| 274 | /* Signal to block layer that we support sector tagging */ |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 275 | if (dif && type) { |
| 276 | |
| 277 | disk->integrity->flags |= BLK_INTEGRITY_DEVICE_CAPABLE; |
| 278 | |
| 279 | if (!sdkp) |
| 280 | return; |
| 281 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 282 | if (type == SD_DIF_TYPE3_PROTECTION) |
| 283 | disk->integrity->tag_size = sizeof(u16) + sizeof(u32); |
| 284 | else |
| 285 | disk->integrity->tag_size = sizeof(u16); |
| 286 | |
Martin K. Petersen | cbdc144 | 2008-10-01 01:37:21 -0400 | [diff] [blame] | 287 | sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n", |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 288 | disk->integrity->tag_size); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 293 | * The virtual start sector is the one that was originally submitted |
| 294 | * by the block layer. Due to partitioning, MD/DM cloning, etc. the |
| 295 | * actual physical start sector is likely to be different. Remap |
| 296 | * protection information to match the physical LBA. |
| 297 | * |
| 298 | * From a protocol perspective there's a slight difference between |
| 299 | * Type 1 and 2. The latter uses 32-byte CDBs exclusively, and the |
| 300 | * reference tag is seeded in the CDB. This gives us the potential to |
| 301 | * avoid virt->phys remapping during write. However, at read time we |
| 302 | * don't know whether the virt sector is the same as when we wrote it |
| 303 | * (we could be reading from real disk as opposed to MD/DM device. So |
| 304 | * we always remap Type 2 making it identical to Type 1. |
| 305 | * |
| 306 | * Type 3 does not have a reference tag so no remapping is required. |
| 307 | */ |
Martin K. Petersen | 8c579ab | 2012-08-28 14:29:33 -0400 | [diff] [blame] | 308 | void sd_dif_prepare(struct request *rq, sector_t hw_sector, |
| 309 | unsigned int sector_sz) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 310 | { |
| 311 | const int tuple_sz = sizeof(struct sd_dif_tuple); |
| 312 | struct bio *bio; |
| 313 | struct scsi_disk *sdkp; |
| 314 | struct sd_dif_tuple *sdt; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 315 | u32 phys, virt; |
| 316 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 317 | sdkp = rq->bio->bi_bdev->bd_disk->private_data; |
| 318 | |
| 319 | if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION) |
Martin K. Petersen | 8c579ab | 2012-08-28 14:29:33 -0400 | [diff] [blame] | 320 | return; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 321 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 322 | phys = hw_sector & 0xffffffff; |
| 323 | |
| 324 | __rq_for_each_bio(bio, rq) { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 325 | struct bio_integrity_payload *bip = bio_integrity(bio); |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 326 | struct bio_vec iv; |
| 327 | struct bvec_iter iter; |
| 328 | unsigned int j; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 329 | |
Martin K. Petersen | 495d2b3 | 2010-10-15 15:49:20 +0200 | [diff] [blame] | 330 | /* Already remapped? */ |
Martin K. Petersen | b1f013885 | 2014-09-26 19:20:04 -0400 | [diff] [blame] | 331 | if (bip->bip_flags & BIP_MAPPED_INTEGRITY) |
Martin K. Petersen | 495d2b3 | 2010-10-15 15:49:20 +0200 | [diff] [blame] | 332 | break; |
| 333 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 334 | virt = bip_get_seed(bip) & 0xffffffff; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 335 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 336 | bip_for_each_vec(iv, bip, iter) { |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 337 | sdt = kmap_atomic(iv.bv_page) |
| 338 | + iv.bv_offset; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 339 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 340 | for (j = 0; j < iv.bv_len; j += tuple_sz, sdt++) { |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 341 | |
Martin K. Petersen | 8c579ab | 2012-08-28 14:29:33 -0400 | [diff] [blame] | 342 | if (be32_to_cpu(sdt->ref_tag) == virt) |
| 343 | sdt->ref_tag = cpu_to_be32(phys); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 344 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 345 | virt++; |
| 346 | phys++; |
| 347 | } |
| 348 | |
Cong Wang | 77dfce0 | 2011-11-25 23:14:23 +0800 | [diff] [blame] | 349 | kunmap_atomic(sdt); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 350 | } |
Martin K. Petersen | 495d2b3 | 2010-10-15 15:49:20 +0200 | [diff] [blame] | 351 | |
Martin K. Petersen | b1f013885 | 2014-09-26 19:20:04 -0400 | [diff] [blame] | 352 | bip->bip_flags |= BIP_MAPPED_INTEGRITY; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 353 | } |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* |
| 357 | * Remap physical sector values in the reference tag to the virtual |
| 358 | * values expected by the block layer. |
| 359 | */ |
| 360 | void sd_dif_complete(struct scsi_cmnd *scmd, unsigned int good_bytes) |
| 361 | { |
| 362 | const int tuple_sz = sizeof(struct sd_dif_tuple); |
| 363 | struct scsi_disk *sdkp; |
| 364 | struct bio *bio; |
| 365 | struct sd_dif_tuple *sdt; |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 366 | unsigned int j, sectors, sector_sz; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 367 | u32 phys, virt; |
| 368 | |
| 369 | sdkp = scsi_disk(scmd->request->rq_disk); |
| 370 | |
| 371 | if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION || good_bytes == 0) |
| 372 | return; |
| 373 | |
| 374 | sector_sz = scmd->device->sector_size; |
| 375 | sectors = good_bytes / sector_sz; |
| 376 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 377 | phys = blk_rq_pos(scmd->request) & 0xffffffff; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 378 | if (sector_sz == 4096) |
| 379 | phys >>= 3; |
| 380 | |
| 381 | __rq_for_each_bio(bio, scmd->request) { |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 382 | struct bio_integrity_payload *bip = bio_integrity(bio); |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 383 | struct bio_vec iv; |
| 384 | struct bvec_iter iter; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 385 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 386 | virt = bip_get_seed(bip) & 0xffffffff; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 387 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 388 | bip_for_each_vec(iv, bip, iter) { |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 389 | sdt = kmap_atomic(iv.bv_page) |
| 390 | + iv.bv_offset; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 391 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 392 | for (j = 0; j < iv.bv_len; j += tuple_sz, sdt++) { |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 393 | |
| 394 | if (sectors == 0) { |
Cong Wang | 77dfce0 | 2011-11-25 23:14:23 +0800 | [diff] [blame] | 395 | kunmap_atomic(sdt); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 396 | return; |
| 397 | } |
| 398 | |
Martin K. Petersen | 8c579ab | 2012-08-28 14:29:33 -0400 | [diff] [blame] | 399 | if (be32_to_cpu(sdt->ref_tag) == phys) |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 400 | sdt->ref_tag = cpu_to_be32(virt); |
| 401 | |
| 402 | virt++; |
| 403 | phys++; |
| 404 | sectors--; |
| 405 | } |
| 406 | |
Cong Wang | 77dfce0 | 2011-11-25 23:14:23 +0800 | [diff] [blame] | 407 | kunmap_atomic(sdt); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | |