Commit | Line | Data |
---|---|---|
af55ff67 MP |
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 | */ | |
18593088 | 56 | static void sd_dif_type1_generate(struct blk_integrity_iter *iter, csum_fn *fn) |
af55ff67 | 57 | { |
18593088 MP |
58 | void *buf = iter->data_buf; |
59 | struct sd_dif_tuple *sdt = iter->prot_buf; | |
60 | sector_t seed = iter->seed; | |
af55ff67 MP |
61 | unsigned int i; |
62 | ||
18593088 MP |
63 | for (i = 0 ; i < iter->data_size ; i += iter->interval, sdt++) { |
64 | sdt->guard_tag = fn(buf, iter->interval); | |
3be91c4a | 65 | sdt->ref_tag = cpu_to_be32(seed & 0xffffffff); |
af55ff67 MP |
66 | sdt->app_tag = 0; |
67 | ||
18593088 | 68 | buf += iter->interval; |
3be91c4a | 69 | seed++; |
af55ff67 MP |
70 | } |
71 | } | |
72 | ||
18593088 | 73 | static int sd_dif_type1_generate_crc(struct blk_integrity_iter *iter) |
af55ff67 | 74 | { |
18593088 MP |
75 | sd_dif_type1_generate(iter, sd_dif_crc_fn); |
76 | return 0; | |
af55ff67 MP |
77 | } |
78 | ||
18593088 | 79 | static int sd_dif_type1_generate_ip(struct blk_integrity_iter *iter) |
af55ff67 | 80 | { |
18593088 MP |
81 | sd_dif_type1_generate(iter, sd_dif_ip_fn); |
82 | return 0; | |
af55ff67 MP |
83 | } |
84 | ||
18593088 | 85 | static int sd_dif_type1_verify(struct blk_integrity_iter *iter, csum_fn *fn) |
af55ff67 | 86 | { |
18593088 MP |
87 | void *buf = iter->data_buf; |
88 | struct sd_dif_tuple *sdt = iter->prot_buf; | |
89 | sector_t seed = iter->seed; | |
af55ff67 MP |
90 | unsigned int i; |
91 | __u16 csum; | |
92 | ||
18593088 | 93 | for (i = 0 ; i < iter->data_size ; i += iter->interval, sdt++) { |
af55ff67 MP |
94 | /* Unwritten sectors */ |
95 | if (sdt->app_tag == 0xffff) | |
96 | return 0; | |
97 | ||
3be91c4a | 98 | if (be32_to_cpu(sdt->ref_tag) != (seed & 0xffffffff)) { |
af55ff67 MP |
99 | printk(KERN_ERR |
100 | "%s: ref tag error on sector %lu (rcvd %u)\n", | |
18593088 | 101 | iter->disk_name, (unsigned long)seed, |
af55ff67 MP |
102 | be32_to_cpu(sdt->ref_tag)); |
103 | return -EIO; | |
104 | } | |
105 | ||
18593088 | 106 | csum = fn(buf, iter->interval); |
af55ff67 MP |
107 | |
108 | if (sdt->guard_tag != csum) { | |
109 | printk(KERN_ERR "%s: guard tag error on sector %lu " \ | |
18593088 | 110 | "(rcvd %04x, data %04x)\n", iter->disk_name, |
3be91c4a | 111 | (unsigned long)seed, |
af55ff67 MP |
112 | be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); |
113 | return -EIO; | |
114 | } | |
115 | ||
18593088 | 116 | buf += iter->interval; |
3be91c4a | 117 | seed++; |
af55ff67 MP |
118 | } |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
18593088 | 123 | static int sd_dif_type1_verify_crc(struct blk_integrity_iter *iter) |
af55ff67 | 124 | { |
18593088 | 125 | return sd_dif_type1_verify(iter, sd_dif_crc_fn); |
af55ff67 MP |
126 | } |
127 | ||
18593088 | 128 | static int sd_dif_type1_verify_ip(struct blk_integrity_iter *iter) |
af55ff67 | 129 | { |
18593088 | 130 | return sd_dif_type1_verify(iter, sd_dif_ip_fn); |
af55ff67 MP |
131 | } |
132 | ||
af55ff67 MP |
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, | |
af55ff67 MP |
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, | |
af55ff67 MP |
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 | */ | |
18593088 | 154 | static void sd_dif_type3_generate(struct blk_integrity_iter *iter, csum_fn *fn) |
af55ff67 | 155 | { |
18593088 MP |
156 | void *buf = iter->data_buf; |
157 | struct sd_dif_tuple *sdt = iter->prot_buf; | |
af55ff67 MP |
158 | unsigned int i; |
159 | ||
18593088 MP |
160 | for (i = 0 ; i < iter->data_size ; i += iter->interval, sdt++) { |
161 | sdt->guard_tag = fn(buf, iter->interval); | |
af55ff67 MP |
162 | sdt->ref_tag = 0; |
163 | sdt->app_tag = 0; | |
164 | ||
18593088 | 165 | buf += iter->interval; |
af55ff67 MP |
166 | } |
167 | } | |
168 | ||
18593088 | 169 | static int sd_dif_type3_generate_crc(struct blk_integrity_iter *iter) |
af55ff67 | 170 | { |
18593088 MP |
171 | sd_dif_type3_generate(iter, sd_dif_crc_fn); |
172 | return 0; | |
af55ff67 MP |
173 | } |
174 | ||
18593088 | 175 | static int sd_dif_type3_generate_ip(struct blk_integrity_iter *iter) |
af55ff67 | 176 | { |
18593088 MP |
177 | sd_dif_type3_generate(iter, sd_dif_ip_fn); |
178 | return 0; | |
af55ff67 MP |
179 | } |
180 | ||
18593088 | 181 | static int sd_dif_type3_verify(struct blk_integrity_iter *iter, csum_fn *fn) |
af55ff67 | 182 | { |
18593088 MP |
183 | void *buf = iter->data_buf; |
184 | struct sd_dif_tuple *sdt = iter->prot_buf; | |
185 | sector_t seed = iter->seed; | |
af55ff67 MP |
186 | unsigned int i; |
187 | __u16 csum; | |
188 | ||
18593088 | 189 | for (i = 0 ; i < iter->data_size ; i += iter->interval, sdt++) { |
af55ff67 MP |
190 | /* Unwritten sectors */ |
191 | if (sdt->app_tag == 0xffff && sdt->ref_tag == 0xffffffff) | |
192 | return 0; | |
193 | ||
18593088 | 194 | csum = fn(buf, iter->interval); |
af55ff67 MP |
195 | |
196 | if (sdt->guard_tag != csum) { | |
197 | printk(KERN_ERR "%s: guard tag error on sector %lu " \ | |
18593088 | 198 | "(rcvd %04x, data %04x)\n", iter->disk_name, |
3be91c4a | 199 | (unsigned long)seed, |
af55ff67 MP |
200 | be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); |
201 | return -EIO; | |
202 | } | |
203 | ||
18593088 | 204 | buf += iter->interval; |
3be91c4a | 205 | seed++; |
af55ff67 MP |
206 | } |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
18593088 | 211 | static int sd_dif_type3_verify_crc(struct blk_integrity_iter *iter) |
af55ff67 | 212 | { |
18593088 | 213 | return sd_dif_type3_verify(iter, sd_dif_crc_fn); |
af55ff67 MP |
214 | } |
215 | ||
18593088 | 216 | static int sd_dif_type3_verify_ip(struct blk_integrity_iter *iter) |
af55ff67 | 217 | { |
18593088 | 218 | return sd_dif_type3_verify(iter, sd_dif_ip_fn); |
af55ff67 MP |
219 | } |
220 | ||
af55ff67 MP |
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, | |
af55ff67 MP |
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, | |
af55ff67 MP |
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; | |
9e06688e | 245 | int dif, dix; |
af55ff67 | 246 | |
9e06688e MP |
247 | dif = scsi_host_dif_capable(sdp->host, type); |
248 | dix = scsi_host_dix_capable(sdp->host, type); | |
af55ff67 | 249 | |
9e06688e MP |
250 | if (!dix && scsi_host_dix_capable(sdp->host, 0)) { |
251 | dif = 0; dix = 1; | |
252 | } | |
af55ff67 | 253 | |
9e06688e | 254 | if (!dix) |
af55ff67 | 255 | return; |
af55ff67 MP |
256 | |
257 | /* Enable DMA of protection information */ | |
258 | if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP) | |
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); | |
263 | else | |
264 | if (type == SD_DIF_TYPE3_PROTECTION) | |
265 | blk_integrity_register(disk, &dif_type3_integrity_crc); | |
266 | else | |
267 | blk_integrity_register(disk, &dif_type1_integrity_crc); | |
268 | ||
cbdc1445 | 269 | sd_printk(KERN_NOTICE, sdkp, |
9e06688e | 270 | "Enabling DIX %s protection\n", disk->integrity->name); |
af55ff67 MP |
271 | |
272 | /* Signal to block layer that we support sector tagging */ | |
3aec2f41 MP |
273 | if (dif && type) { |
274 | ||
275 | disk->integrity->flags |= BLK_INTEGRITY_DEVICE_CAPABLE; | |
276 | ||
277 | if (!sdkp) | |
278 | return; | |
279 | ||
af55ff67 MP |
280 | if (type == SD_DIF_TYPE3_PROTECTION) |
281 | disk->integrity->tag_size = sizeof(u16) + sizeof(u32); | |
282 | else | |
283 | disk->integrity->tag_size = sizeof(u16); | |
284 | ||
cbdc1445 | 285 | sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n", |
af55ff67 MP |
286 | disk->integrity->tag_size); |
287 | } | |
288 | } | |
289 | ||
af55ff67 MP |
290 | /* |
291 | * The virtual start sector is the one that was originally submitted | |
292 | * by the block layer. Due to partitioning, MD/DM cloning, etc. the | |
293 | * actual physical start sector is likely to be different. Remap | |
294 | * protection information to match the physical LBA. | |
295 | * | |
296 | * From a protocol perspective there's a slight difference between | |
297 | * Type 1 and 2. The latter uses 32-byte CDBs exclusively, and the | |
298 | * reference tag is seeded in the CDB. This gives us the potential to | |
299 | * avoid virt->phys remapping during write. However, at read time we | |
300 | * don't know whether the virt sector is the same as when we wrote it | |
301 | * (we could be reading from real disk as opposed to MD/DM device. So | |
302 | * we always remap Type 2 making it identical to Type 1. | |
303 | * | |
304 | * Type 3 does not have a reference tag so no remapping is required. | |
305 | */ | |
8c579ab6 MP |
306 | void sd_dif_prepare(struct request *rq, sector_t hw_sector, |
307 | unsigned int sector_sz) | |
af55ff67 MP |
308 | { |
309 | const int tuple_sz = sizeof(struct sd_dif_tuple); | |
310 | struct bio *bio; | |
311 | struct scsi_disk *sdkp; | |
312 | struct sd_dif_tuple *sdt; | |
af55ff67 MP |
313 | u32 phys, virt; |
314 | ||
af55ff67 MP |
315 | sdkp = rq->bio->bi_bdev->bd_disk->private_data; |
316 | ||
317 | if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION) | |
8c579ab6 | 318 | return; |
af55ff67 | 319 | |
af55ff67 MP |
320 | phys = hw_sector & 0xffffffff; |
321 | ||
322 | __rq_for_each_bio(bio, rq) { | |
18593088 | 323 | struct bio_integrity_payload *bip = bio_integrity(bio); |
d57a5f7c KO |
324 | struct bio_vec iv; |
325 | struct bvec_iter iter; | |
326 | unsigned int j; | |
af55ff67 | 327 | |
495d2b38 MP |
328 | /* Already remapped? */ |
329 | if (bio_flagged(bio, BIO_MAPPED_INTEGRITY)) | |
330 | break; | |
331 | ||
18593088 | 332 | virt = bip_get_seed(bip) & 0xffffffff; |
af55ff67 | 333 | |
18593088 | 334 | bip_for_each_vec(iv, bip, iter) { |
d57a5f7c KO |
335 | sdt = kmap_atomic(iv.bv_page) |
336 | + iv.bv_offset; | |
af55ff67 | 337 | |
d57a5f7c | 338 | for (j = 0; j < iv.bv_len; j += tuple_sz, sdt++) { |
af55ff67 | 339 | |
8c579ab6 MP |
340 | if (be32_to_cpu(sdt->ref_tag) == virt) |
341 | sdt->ref_tag = cpu_to_be32(phys); | |
af55ff67 | 342 | |
af55ff67 MP |
343 | virt++; |
344 | phys++; | |
345 | } | |
346 | ||
77dfce07 | 347 | kunmap_atomic(sdt); |
af55ff67 | 348 | } |
495d2b38 | 349 | |
9354f1b8 | 350 | bio->bi_flags |= (1 << BIO_MAPPED_INTEGRITY); |
af55ff67 | 351 | } |
af55ff67 MP |
352 | } |
353 | ||
354 | /* | |
355 | * Remap physical sector values in the reference tag to the virtual | |
356 | * values expected by the block layer. | |
357 | */ | |
358 | void sd_dif_complete(struct scsi_cmnd *scmd, unsigned int good_bytes) | |
359 | { | |
360 | const int tuple_sz = sizeof(struct sd_dif_tuple); | |
361 | struct scsi_disk *sdkp; | |
362 | struct bio *bio; | |
363 | struct sd_dif_tuple *sdt; | |
d57a5f7c | 364 | unsigned int j, sectors, sector_sz; |
af55ff67 MP |
365 | u32 phys, virt; |
366 | ||
367 | sdkp = scsi_disk(scmd->request->rq_disk); | |
368 | ||
369 | if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION || good_bytes == 0) | |
370 | return; | |
371 | ||
372 | sector_sz = scmd->device->sector_size; | |
373 | sectors = good_bytes / sector_sz; | |
374 | ||
83096ebf | 375 | phys = blk_rq_pos(scmd->request) & 0xffffffff; |
af55ff67 MP |
376 | if (sector_sz == 4096) |
377 | phys >>= 3; | |
378 | ||
379 | __rq_for_each_bio(bio, scmd->request) { | |
18593088 | 380 | struct bio_integrity_payload *bip = bio_integrity(bio); |
d57a5f7c KO |
381 | struct bio_vec iv; |
382 | struct bvec_iter iter; | |
af55ff67 | 383 | |
18593088 | 384 | virt = bip_get_seed(bip) & 0xffffffff; |
af55ff67 | 385 | |
18593088 | 386 | bip_for_each_vec(iv, bip, iter) { |
d57a5f7c KO |
387 | sdt = kmap_atomic(iv.bv_page) |
388 | + iv.bv_offset; | |
af55ff67 | 389 | |
d57a5f7c | 390 | for (j = 0; j < iv.bv_len; j += tuple_sz, sdt++) { |
af55ff67 MP |
391 | |
392 | if (sectors == 0) { | |
77dfce07 | 393 | kunmap_atomic(sdt); |
af55ff67 MP |
394 | return; |
395 | } | |
396 | ||
8c579ab6 | 397 | if (be32_to_cpu(sdt->ref_tag) == phys) |
af55ff67 MP |
398 | sdt->ref_tag = cpu_to_be32(virt); |
399 | ||
400 | virt++; | |
401 | phys++; | |
402 | sectors--; | |
403 | } | |
404 | ||
77dfce07 | 405 | kunmap_atomic(sdt); |
af55ff67 MP |
406 | } |
407 | } | |
408 | } | |
409 |