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 | */ | |
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; | |
3be91c4a | 60 | sector_t seed = bix->seed; |
af55ff67 MP |
61 | unsigned int i; |
62 | ||
3be91c4a MP |
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); | |
af55ff67 MP |
66 | sdt->app_tag = 0; |
67 | ||
3be91c4a MP |
68 | buf += bix->interval; |
69 | seed++; | |
af55ff67 MP |
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; | |
3be91c4a | 87 | sector_t seed = bix->seed; |
af55ff67 MP |
88 | unsigned int i; |
89 | __u16 csum; | |
90 | ||
3be91c4a | 91 | for (i = 0 ; i < bix->data_size ; i += bix->interval, sdt++) { |
af55ff67 MP |
92 | /* Unwritten sectors */ |
93 | if (sdt->app_tag == 0xffff) | |
94 | return 0; | |
95 | ||
3be91c4a | 96 | if (be32_to_cpu(sdt->ref_tag) != (seed & 0xffffffff)) { |
af55ff67 MP |
97 | printk(KERN_ERR |
98 | "%s: ref tag error on sector %lu (rcvd %u)\n", | |
3be91c4a | 99 | bix->disk_name, (unsigned long)seed, |
af55ff67 MP |
100 | be32_to_cpu(sdt->ref_tag)); |
101 | return -EIO; | |
102 | } | |
103 | ||
3be91c4a | 104 | csum = fn(buf, bix->interval); |
af55ff67 MP |
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, | |
3be91c4a | 109 | (unsigned long)seed, |
af55ff67 MP |
110 | be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); |
111 | return -EIO; | |
112 | } | |
113 | ||
3be91c4a MP |
114 | buf += bix->interval; |
115 | seed++; | |
af55ff67 MP |
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 | ||
af55ff67 MP |
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, | |
af55ff67 MP |
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, | |
af55ff67 MP |
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 | ||
3be91c4a MP |
158 | for (i = 0 ; i < bix->data_size ; i += bix->interval, sdt++) { |
159 | sdt->guard_tag = fn(buf, bix->interval); | |
af55ff67 MP |
160 | sdt->ref_tag = 0; |
161 | sdt->app_tag = 0; | |
162 | ||
3be91c4a | 163 | buf += bix->interval; |
af55ff67 MP |
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; | |
3be91c4a | 181 | sector_t seed = bix->seed; |
af55ff67 MP |
182 | unsigned int i; |
183 | __u16 csum; | |
184 | ||
3be91c4a | 185 | for (i = 0 ; i < bix->data_size ; i += bix->interval, sdt++) { |
af55ff67 MP |
186 | /* Unwritten sectors */ |
187 | if (sdt->app_tag == 0xffff && sdt->ref_tag == 0xffffffff) | |
188 | return 0; | |
189 | ||
3be91c4a | 190 | csum = fn(buf, bix->interval); |
af55ff67 MP |
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, | |
3be91c4a | 195 | (unsigned long)seed, |
af55ff67 MP |
196 | be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); |
197 | return -EIO; | |
198 | } | |
199 | ||
3be91c4a MP |
200 | buf += bix->interval; |
201 | seed++; | |
af55ff67 MP |
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 | ||
af55ff67 MP |
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, | |
af55ff67 MP |
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, | |
af55ff67 MP |
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; | |
9e06688e | 241 | int dif, dix; |
af55ff67 | 242 | |
9e06688e MP |
243 | dif = scsi_host_dif_capable(sdp->host, type); |
244 | dix = scsi_host_dix_capable(sdp->host, type); | |
af55ff67 | 245 | |
9e06688e MP |
246 | if (!dix && scsi_host_dix_capable(sdp->host, 0)) { |
247 | dif = 0; dix = 1; | |
248 | } | |
af55ff67 | 249 | |
9e06688e | 250 | if (!dix) |
af55ff67 | 251 | return; |
af55ff67 MP |
252 | |
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 | ||
cbdc1445 | 265 | sd_printk(KERN_NOTICE, sdkp, |
9e06688e | 266 | "Enabling DIX %s protection\n", disk->integrity->name); |
af55ff67 MP |
267 | |
268 | /* Signal to block layer that we support sector tagging */ | |
9e06688e | 269 | if (dif && type && sdkp->ATO) { |
af55ff67 MP |
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 | ||
cbdc1445 | 275 | sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n", |
af55ff67 MP |
276 | disk->integrity->tag_size); |
277 | } | |
278 | } | |
279 | ||
af55ff67 MP |
280 | /* |
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 | */ | |
8c579ab6 MP |
296 | void sd_dif_prepare(struct request *rq, sector_t hw_sector, |
297 | unsigned int sector_sz) | |
af55ff67 MP |
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; | |
af55ff67 MP |
303 | u32 phys, virt; |
304 | ||
af55ff67 MP |
305 | sdkp = rq->bio->bi_bdev->bd_disk->private_data; |
306 | ||
307 | if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION) | |
8c579ab6 | 308 | return; |
af55ff67 | 309 | |
af55ff67 MP |
310 | phys = hw_sector & 0xffffffff; |
311 | ||
312 | __rq_for_each_bio(bio, rq) { | |
d57a5f7c KO |
313 | struct bio_vec iv; |
314 | struct bvec_iter iter; | |
315 | unsigned int j; | |
af55ff67 | 316 | |
495d2b38 MP |
317 | /* Already remapped? */ |
318 | if (bio_flagged(bio, BIO_MAPPED_INTEGRITY)) | |
319 | break; | |
320 | ||
180b2f95 | 321 | virt = bio_integrity(bio)->bip_iter.bi_sector & 0xffffffff; |
af55ff67 | 322 | |
180b2f95 | 323 | bip_for_each_vec(iv, bio_integrity(bio), iter) { |
d57a5f7c KO |
324 | sdt = kmap_atomic(iv.bv_page) |
325 | + iv.bv_offset; | |
af55ff67 | 326 | |
d57a5f7c | 327 | for (j = 0; j < iv.bv_len; j += tuple_sz, sdt++) { |
af55ff67 | 328 | |
8c579ab6 MP |
329 | if (be32_to_cpu(sdt->ref_tag) == virt) |
330 | sdt->ref_tag = cpu_to_be32(phys); | |
af55ff67 | 331 | |
af55ff67 MP |
332 | virt++; |
333 | phys++; | |
334 | } | |
335 | ||
77dfce07 | 336 | kunmap_atomic(sdt); |
af55ff67 | 337 | } |
495d2b38 | 338 | |
9354f1b8 | 339 | bio->bi_flags |= (1 << BIO_MAPPED_INTEGRITY); |
af55ff67 | 340 | } |
af55ff67 MP |
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; | |
d57a5f7c | 353 | unsigned int j, sectors, sector_sz; |
af55ff67 MP |
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 | ||
83096ebf | 364 | phys = blk_rq_pos(scmd->request) & 0xffffffff; |
af55ff67 MP |
365 | if (sector_sz == 4096) |
366 | phys >>= 3; | |
367 | ||
368 | __rq_for_each_bio(bio, scmd->request) { | |
d57a5f7c KO |
369 | struct bio_vec iv; |
370 | struct bvec_iter iter; | |
af55ff67 | 371 | |
180b2f95 | 372 | virt = bio_integrity(bio)->bip_iter.bi_sector & 0xffffffff; |
af55ff67 | 373 | |
180b2f95 | 374 | bip_for_each_vec(iv, bio_integrity(bio), iter) { |
d57a5f7c KO |
375 | sdt = kmap_atomic(iv.bv_page) |
376 | + iv.bv_offset; | |
af55ff67 | 377 | |
d57a5f7c | 378 | for (j = 0; j < iv.bv_len; j += tuple_sz, sdt++) { |
af55ff67 MP |
379 | |
380 | if (sectors == 0) { | |
77dfce07 | 381 | kunmap_atomic(sdt); |
af55ff67 MP |
382 | return; |
383 | } | |
384 | ||
8c579ab6 | 385 | if (be32_to_cpu(sdt->ref_tag) == phys) |
af55ff67 MP |
386 | sdt->ref_tag = cpu_to_be32(virt); |
387 | ||
388 | virt++; | |
389 | phys++; | |
390 | sectors--; | |
391 | } | |
392 | ||
77dfce07 | 393 | kunmap_atomic(sdt); |
af55ff67 MP |
394 | } |
395 | } | |
396 | } | |
397 |