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