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