2 * t10_pi.c - Functions for generating and verifying T10 Protection
5 * Copyright (C) 2007, 2008, 2014 Oracle Corporation
6 * Written by: Martin K. Petersen <martin.petersen@oracle.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
24 #include <linux/t10-pi.h>
25 #include <linux/blkdev.h>
26 #include <linux/crc-t10dif.h>
27 #include <net/checksum.h>
29 typedef __be16 (csum_fn) (void *, unsigned int);
31 static __be16 t10_pi_crc_fn(void *data, unsigned int len)
33 return cpu_to_be16(crc_t10dif(data, len));
36 static __be16 t10_pi_ip_fn(void *data, unsigned int len)
38 return (__force __be16)ip_compute_csum(data, len);
42 * Type 1 and Type 2 protection use the same format: 16 bit guard tag,
43 * 16 bit app tag, 32 bit reference tag. Type 3 does not define the ref
46 static blk_status_t t10_pi_generate(struct blk_integrity_iter *iter,
47 csum_fn *fn, unsigned int type)
51 for (i = 0 ; i < iter->data_size ; i += iter->interval) {
52 struct t10_pi_tuple *pi = iter->prot_buf;
54 pi->guard_tag = fn(iter->data_buf, iter->interval);
58 pi->ref_tag = cpu_to_be32(lower_32_bits(iter->seed));
62 iter->data_buf += iter->interval;
63 iter->prot_buf += sizeof(struct t10_pi_tuple);
70 static blk_status_t t10_pi_verify(struct blk_integrity_iter *iter,
71 csum_fn *fn, unsigned int type)
75 for (i = 0 ; i < iter->data_size ; i += iter->interval) {
76 struct t10_pi_tuple *pi = iter->prot_buf;
82 if (pi->app_tag == T10_PI_APP_ESCAPE)
85 if (be32_to_cpu(pi->ref_tag) !=
86 lower_32_bits(iter->seed)) {
87 pr_err("%s: ref tag error at location %llu " \
88 "(rcvd %u)\n", iter->disk_name,
90 iter->seed, be32_to_cpu(pi->ref_tag));
91 return BLK_STS_PROTECTION;
95 if (pi->app_tag == T10_PI_APP_ESCAPE &&
96 pi->ref_tag == T10_PI_REF_ESCAPE)
101 csum = fn(iter->data_buf, iter->interval);
103 if (pi->guard_tag != csum) {
104 pr_err("%s: guard tag error at sector %llu " \
105 "(rcvd %04x, want %04x)\n", iter->disk_name,
106 (unsigned long long)iter->seed,
107 be16_to_cpu(pi->guard_tag), be16_to_cpu(csum));
108 return BLK_STS_PROTECTION;
112 iter->data_buf += iter->interval;
113 iter->prot_buf += sizeof(struct t10_pi_tuple);
120 static blk_status_t t10_pi_type1_generate_crc(struct blk_integrity_iter *iter)
122 return t10_pi_generate(iter, t10_pi_crc_fn, 1);
125 static blk_status_t t10_pi_type1_generate_ip(struct blk_integrity_iter *iter)
127 return t10_pi_generate(iter, t10_pi_ip_fn, 1);
130 static blk_status_t t10_pi_type1_verify_crc(struct blk_integrity_iter *iter)
132 return t10_pi_verify(iter, t10_pi_crc_fn, 1);
135 static blk_status_t t10_pi_type1_verify_ip(struct blk_integrity_iter *iter)
137 return t10_pi_verify(iter, t10_pi_ip_fn, 1);
140 static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
142 return t10_pi_generate(iter, t10_pi_crc_fn, 3);
145 static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
147 return t10_pi_generate(iter, t10_pi_ip_fn, 3);
150 static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
152 return t10_pi_verify(iter, t10_pi_crc_fn, 3);
155 static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
157 return t10_pi_verify(iter, t10_pi_ip_fn, 3);
160 const struct blk_integrity_profile t10_pi_type1_crc = {
161 .name = "T10-DIF-TYPE1-CRC",
162 .generate_fn = t10_pi_type1_generate_crc,
163 .verify_fn = t10_pi_type1_verify_crc,
165 EXPORT_SYMBOL(t10_pi_type1_crc);
167 const struct blk_integrity_profile t10_pi_type1_ip = {
168 .name = "T10-DIF-TYPE1-IP",
169 .generate_fn = t10_pi_type1_generate_ip,
170 .verify_fn = t10_pi_type1_verify_ip,
172 EXPORT_SYMBOL(t10_pi_type1_ip);
174 const struct blk_integrity_profile t10_pi_type3_crc = {
175 .name = "T10-DIF-TYPE3-CRC",
176 .generate_fn = t10_pi_type3_generate_crc,
177 .verify_fn = t10_pi_type3_verify_crc,
179 EXPORT_SYMBOL(t10_pi_type3_crc);
181 const struct blk_integrity_profile t10_pi_type3_ip = {
182 .name = "T10-DIF-TYPE3-IP",
183 .generate_fn = t10_pi_type3_generate_ip,
184 .verify_fn = t10_pi_type3_verify_ip,
186 EXPORT_SYMBOL(t10_pi_type3_ip);