Back to home page

LXR

 
 

    


0001 /*
0002  * t10_pi.c - Functions for generating and verifying T10 Protection
0003  *        Information.
0004  *
0005  * Copyright (C) 2007, 2008, 2014 Oracle Corporation
0006  * Written by: Martin K. Petersen <martin.petersen@oracle.com>
0007  *
0008  * This program is free software; you can redistribute it and/or
0009  * modify it under the terms of the GNU General Public License version
0010  * 2 as published by the Free Software Foundation.
0011  *
0012  * This program is distributed in the hope that it will be useful, but
0013  * WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program; see the file COPYING.  If not, write to
0019  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
0020  * USA.
0021  *
0022  */
0023 
0024 #include <linux/t10-pi.h>
0025 #include <linux/blkdev.h>
0026 #include <linux/crc-t10dif.h>
0027 #include <net/checksum.h>
0028 
0029 typedef __be16 (csum_fn) (void *, unsigned int);
0030 
0031 static const __be16 APP_ESCAPE = (__force __be16) 0xffff;
0032 static const __be32 REF_ESCAPE = (__force __be32) 0xffffffff;
0033 
0034 static __be16 t10_pi_crc_fn(void *data, unsigned int len)
0035 {
0036     return cpu_to_be16(crc_t10dif(data, len));
0037 }
0038 
0039 static __be16 t10_pi_ip_fn(void *data, unsigned int len)
0040 {
0041     return (__force __be16)ip_compute_csum(data, len);
0042 }
0043 
0044 /*
0045  * Type 1 and Type 2 protection use the same format: 16 bit guard tag,
0046  * 16 bit app tag, 32 bit reference tag. Type 3 does not define the ref
0047  * tag.
0048  */
0049 static int t10_pi_generate(struct blk_integrity_iter *iter, csum_fn *fn,
0050                unsigned int type)
0051 {
0052     unsigned int i;
0053 
0054     for (i = 0 ; i < iter->data_size ; i += iter->interval) {
0055         struct t10_pi_tuple *pi = iter->prot_buf;
0056 
0057         pi->guard_tag = fn(iter->data_buf, iter->interval);
0058         pi->app_tag = 0;
0059 
0060         if (type == 1)
0061             pi->ref_tag = cpu_to_be32(lower_32_bits(iter->seed));
0062         else
0063             pi->ref_tag = 0;
0064 
0065         iter->data_buf += iter->interval;
0066         iter->prot_buf += sizeof(struct t10_pi_tuple);
0067         iter->seed++;
0068     }
0069 
0070     return 0;
0071 }
0072 
0073 static int t10_pi_verify(struct blk_integrity_iter *iter, csum_fn *fn,
0074                 unsigned int type)
0075 {
0076     unsigned int i;
0077 
0078     for (i = 0 ; i < iter->data_size ; i += iter->interval) {
0079         struct t10_pi_tuple *pi = iter->prot_buf;
0080         __be16 csum;
0081 
0082         switch (type) {
0083         case 1:
0084         case 2:
0085             if (pi->app_tag == APP_ESCAPE)
0086                 goto next;
0087 
0088             if (be32_to_cpu(pi->ref_tag) !=
0089                 lower_32_bits(iter->seed)) {
0090                 pr_err("%s: ref tag error at location %llu " \
0091                        "(rcvd %u)\n", iter->disk_name,
0092                        (unsigned long long)
0093                        iter->seed, be32_to_cpu(pi->ref_tag));
0094                 return -EILSEQ;
0095             }
0096             break;
0097         case 3:
0098             if (pi->app_tag == APP_ESCAPE &&
0099                 pi->ref_tag == REF_ESCAPE)
0100                 goto next;
0101             break;
0102         }
0103 
0104         csum = fn(iter->data_buf, iter->interval);
0105 
0106         if (pi->guard_tag != csum) {
0107             pr_err("%s: guard tag error at sector %llu " \
0108                    "(rcvd %04x, want %04x)\n", iter->disk_name,
0109                    (unsigned long long)iter->seed,
0110                    be16_to_cpu(pi->guard_tag), be16_to_cpu(csum));
0111             return -EILSEQ;
0112         }
0113 
0114 next:
0115         iter->data_buf += iter->interval;
0116         iter->prot_buf += sizeof(struct t10_pi_tuple);
0117         iter->seed++;
0118     }
0119 
0120     return 0;
0121 }
0122 
0123 static int t10_pi_type1_generate_crc(struct blk_integrity_iter *iter)
0124 {
0125     return t10_pi_generate(iter, t10_pi_crc_fn, 1);
0126 }
0127 
0128 static int t10_pi_type1_generate_ip(struct blk_integrity_iter *iter)
0129 {
0130     return t10_pi_generate(iter, t10_pi_ip_fn, 1);
0131 }
0132 
0133 static int t10_pi_type1_verify_crc(struct blk_integrity_iter *iter)
0134 {
0135     return t10_pi_verify(iter, t10_pi_crc_fn, 1);
0136 }
0137 
0138 static int t10_pi_type1_verify_ip(struct blk_integrity_iter *iter)
0139 {
0140     return t10_pi_verify(iter, t10_pi_ip_fn, 1);
0141 }
0142 
0143 static int t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
0144 {
0145     return t10_pi_generate(iter, t10_pi_crc_fn, 3);
0146 }
0147 
0148 static int t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
0149 {
0150     return t10_pi_generate(iter, t10_pi_ip_fn, 3);
0151 }
0152 
0153 static int t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
0154 {
0155     return t10_pi_verify(iter, t10_pi_crc_fn, 3);
0156 }
0157 
0158 static int t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
0159 {
0160     return t10_pi_verify(iter, t10_pi_ip_fn, 3);
0161 }
0162 
0163 struct blk_integrity_profile t10_pi_type1_crc = {
0164     .name           = "T10-DIF-TYPE1-CRC",
0165     .generate_fn        = t10_pi_type1_generate_crc,
0166     .verify_fn      = t10_pi_type1_verify_crc,
0167 };
0168 EXPORT_SYMBOL(t10_pi_type1_crc);
0169 
0170 struct blk_integrity_profile t10_pi_type1_ip = {
0171     .name           = "T10-DIF-TYPE1-IP",
0172     .generate_fn        = t10_pi_type1_generate_ip,
0173     .verify_fn      = t10_pi_type1_verify_ip,
0174 };
0175 EXPORT_SYMBOL(t10_pi_type1_ip);
0176 
0177 struct blk_integrity_profile t10_pi_type3_crc = {
0178     .name           = "T10-DIF-TYPE3-CRC",
0179     .generate_fn        = t10_pi_type3_generate_crc,
0180     .verify_fn      = t10_pi_type3_verify_crc,
0181 };
0182 EXPORT_SYMBOL(t10_pi_type3_crc);
0183 
0184 struct blk_integrity_profile t10_pi_type3_ip = {
0185     .name           = "T10-DIF-TYPE3-IP",
0186     .generate_fn        = t10_pi_type3_generate_ip,
0187     .verify_fn      = t10_pi_type3_verify_ip,
0188 };
0189 EXPORT_SYMBOL(t10_pi_type3_ip);