blob: 1600270a46e547c315dadb2b70e6e7eee00563a3 [file] [log] [blame]
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001/*
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
40typedef __u16 (csum_fn) (void *, unsigned int);
41
42static __u16 sd_dif_crc_fn(void *data, unsigned int len)
43{
44 return cpu_to_be16(crc_t10dif(data, len));
45}
46
47static __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 */
56static 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. Petersen3be91c42014-09-26 19:19:59 -040060 sector_t seed = bix->seed;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040061 unsigned int i;
62
Martin K. Petersen3be91c42014-09-26 19:19:59 -040063 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. Petersenaf55ff62008-07-17 04:28:35 -040066 sdt->app_tag = 0;
67
Martin K. Petersen3be91c42014-09-26 19:19:59 -040068 buf += bix->interval;
69 seed++;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040070 }
71}
72
73static void sd_dif_type1_generate_crc(struct blk_integrity_exchg *bix)
74{
75 sd_dif_type1_generate(bix, sd_dif_crc_fn);
76}
77
78static void sd_dif_type1_generate_ip(struct blk_integrity_exchg *bix)
79{
80 sd_dif_type1_generate(bix, sd_dif_ip_fn);
81}
82
83static 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. Petersen3be91c42014-09-26 19:19:59 -040087 sector_t seed = bix->seed;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040088 unsigned int i;
89 __u16 csum;
90
Martin K. Petersen3be91c42014-09-26 19:19:59 -040091 for (i = 0 ; i < bix->data_size ; i += bix->interval, sdt++) {
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040092 /* Unwritten sectors */
93 if (sdt->app_tag == 0xffff)
94 return 0;
95
Martin K. Petersen3be91c42014-09-26 19:19:59 -040096 if (be32_to_cpu(sdt->ref_tag) != (seed & 0xffffffff)) {
Martin K. Petersenaf55ff62008-07-17 04:28:35 -040097 printk(KERN_ERR
98 "%s: ref tag error on sector %lu (rcvd %u)\n",
Martin K. Petersen3be91c42014-09-26 19:19:59 -040099 bix->disk_name, (unsigned long)seed,
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400100 be32_to_cpu(sdt->ref_tag));
101 return -EIO;
102 }
103
Martin K. Petersen3be91c42014-09-26 19:19:59 -0400104 csum = fn(buf, bix->interval);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400105
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. Petersen3be91c42014-09-26 19:19:59 -0400109 (unsigned long)seed,
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400110 be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum));
111 return -EIO;
112 }
113
Martin K. Petersen3be91c42014-09-26 19:19:59 -0400114 buf += bix->interval;
115 seed++;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400116 }
117
118 return 0;
119}
120
121static 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
126static 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. Petersenaf55ff62008-07-17 04:28:35 -0400131static 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. Petersenaf55ff62008-07-17 04:28:35 -0400135 .tuple_size = sizeof(struct sd_dif_tuple),
136 .tag_size = 0,
137};
138
139static 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. Petersenaf55ff62008-07-17 04:28:35 -0400143 .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 */
152static 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. Petersen3be91c42014-09-26 19:19:59 -0400158 for (i = 0 ; i < bix->data_size ; i += bix->interval, sdt++) {
159 sdt->guard_tag = fn(buf, bix->interval);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400160 sdt->ref_tag = 0;
161 sdt->app_tag = 0;
162
Martin K. Petersen3be91c42014-09-26 19:19:59 -0400163 buf += bix->interval;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400164 }
165}
166
167static void sd_dif_type3_generate_crc(struct blk_integrity_exchg *bix)
168{
169 sd_dif_type3_generate(bix, sd_dif_crc_fn);
170}
171
172static void sd_dif_type3_generate_ip(struct blk_integrity_exchg *bix)
173{
174 sd_dif_type3_generate(bix, sd_dif_ip_fn);
175}
176
177static 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. Petersen3be91c42014-09-26 19:19:59 -0400181 sector_t seed = bix->seed;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400182 unsigned int i;
183 __u16 csum;
184
Martin K. Petersen3be91c42014-09-26 19:19:59 -0400185 for (i = 0 ; i < bix->data_size ; i += bix->interval, sdt++) {
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400186 /* Unwritten sectors */
187 if (sdt->app_tag == 0xffff && sdt->ref_tag == 0xffffffff)
188 return 0;
189
Martin K. Petersen3be91c42014-09-26 19:19:59 -0400190 csum = fn(buf, bix->interval);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400191
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. Petersen3be91c42014-09-26 19:19:59 -0400195 (unsigned long)seed,
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400196 be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum));
197 return -EIO;
198 }
199
Martin K. Petersen3be91c42014-09-26 19:19:59 -0400200 buf += bix->interval;
201 seed++;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400202 }
203
204 return 0;
205}
206
207static 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
212static 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. Petersenaf55ff62008-07-17 04:28:35 -0400217static 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. Petersenaf55ff62008-07-17 04:28:35 -0400221 .tuple_size = sizeof(struct sd_dif_tuple),
222 .tag_size = 0,
223};
224
225static 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. Petersenaf55ff62008-07-17 04:28:35 -0400229 .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 */
236void 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. Petersen9e066882008-09-19 18:47:21 -0400241 int dif, dix;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400242
Martin K. Petersen9e066882008-09-19 18:47:21 -0400243 dif = scsi_host_dif_capable(sdp->host, type);
244 dix = scsi_host_dix_capable(sdp->host, type);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400245
Martin K. Petersen9e066882008-09-19 18:47:21 -0400246 if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
247 dif = 0; dix = 1;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400248 }
249
Martin K. Petersen9e066882008-09-19 18:47:21 -0400250 if (!dix)
251 return;
252
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400253 /* 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. Petersencbdc1442008-10-01 01:37:21 -0400265 sd_printk(KERN_NOTICE, sdkp,
Martin K. Petersen9e066882008-09-19 18:47:21 -0400266 "Enabling DIX %s protection\n", disk->integrity->name);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400267
268 /* Signal to block layer that we support sector tagging */
Martin K. Petersen9e066882008-09-19 18:47:21 -0400269 if (dif && type && sdkp->ATO) {
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400270 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. Petersencbdc1442008-10-01 01:37:21 -0400275 sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n",
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400276 disk->integrity->tag_size);
277 }
278}
279
280/*
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400281 * 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. Petersen8c579ab2012-08-28 14:29:33 -0400296void sd_dif_prepare(struct request *rq, sector_t hw_sector,
297 unsigned int sector_sz)
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400298{
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. Petersenaf55ff62008-07-17 04:28:35 -0400303 u32 phys, virt;
304
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400305 sdkp = rq->bio->bi_bdev->bd_disk->private_data;
306
307 if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION)
Martin K. Petersen8c579ab2012-08-28 14:29:33 -0400308 return;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400309
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400310 phys = hw_sector & 0xffffffff;
311
312 __rq_for_each_bio(bio, rq) {
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800313 struct bio_vec iv;
314 struct bvec_iter iter;
315 unsigned int j;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400316
Martin K. Petersen495d2b32010-10-15 15:49:20 +0200317 /* Already remapped? */
318 if (bio_flagged(bio, BIO_MAPPED_INTEGRITY))
319 break;
320
Martin K. Petersen180b2f92014-09-26 19:19:56 -0400321 virt = bio_integrity(bio)->bip_iter.bi_sector & 0xffffffff;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400322
Martin K. Petersen180b2f92014-09-26 19:19:56 -0400323 bip_for_each_vec(iv, bio_integrity(bio), iter) {
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800324 sdt = kmap_atomic(iv.bv_page)
325 + iv.bv_offset;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400326
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800327 for (j = 0; j < iv.bv_len; j += tuple_sz, sdt++) {
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400328
Martin K. Petersen8c579ab2012-08-28 14:29:33 -0400329 if (be32_to_cpu(sdt->ref_tag) == virt)
330 sdt->ref_tag = cpu_to_be32(phys);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400331
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400332 virt++;
333 phys++;
334 }
335
Cong Wang77dfce02011-11-25 23:14:23 +0800336 kunmap_atomic(sdt);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400337 }
Martin K. Petersen495d2b32010-10-15 15:49:20 +0200338
Muthu Kumar9354f1b2012-03-05 14:59:16 -0800339 bio->bi_flags |= (1 << BIO_MAPPED_INTEGRITY);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400340 }
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400341}
342
343/*
344 * Remap physical sector values in the reference tag to the virtual
345 * values expected by the block layer.
346 */
347void 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 Overstreetd57a5f72013-11-23 17:20:16 -0800353 unsigned int j, sectors, sector_sz;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400354 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 Heo83096eb2009-05-07 22:24:39 +0900364 phys = blk_rq_pos(scmd->request) & 0xffffffff;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400365 if (sector_sz == 4096)
366 phys >>= 3;
367
368 __rq_for_each_bio(bio, scmd->request) {
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800369 struct bio_vec iv;
370 struct bvec_iter iter;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400371
Martin K. Petersen180b2f92014-09-26 19:19:56 -0400372 virt = bio_integrity(bio)->bip_iter.bi_sector & 0xffffffff;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400373
Martin K. Petersen180b2f92014-09-26 19:19:56 -0400374 bip_for_each_vec(iv, bio_integrity(bio), iter) {
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800375 sdt = kmap_atomic(iv.bv_page)
376 + iv.bv_offset;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400377
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800378 for (j = 0; j < iv.bv_len; j += tuple_sz, sdt++) {
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400379
380 if (sectors == 0) {
Cong Wang77dfce02011-11-25 23:14:23 +0800381 kunmap_atomic(sdt);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400382 return;
383 }
384
Martin K. Petersen8c579ab2012-08-28 14:29:33 -0400385 if (be32_to_cpu(sdt->ref_tag) == phys)
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400386 sdt->ref_tag = cpu_to_be32(virt);
387
388 virt++;
389 phys++;
390 sectors--;
391 }
392
Cong Wang77dfce02011-11-25 23:14:23 +0800393 kunmap_atomic(sdt);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400394 }
395 }
396}
397