| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * t10_pi.c - Functions for generating and verifying T10 Protection |
|---|
| 3 | 4 | * Information. |
|---|
| 4 | | - * |
|---|
| 5 | | - * Copyright (C) 2007, 2008, 2014 Oracle Corporation |
|---|
| 6 | | - * Written by: Martin K. Petersen <martin.petersen@oracle.com> |
|---|
| 7 | | - * |
|---|
| 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. |
|---|
| 11 | | - * |
|---|
| 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. |
|---|
| 16 | | - * |
|---|
| 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, |
|---|
| 20 | | - * USA. |
|---|
| 21 | | - * |
|---|
| 22 | 5 | */ |
|---|
| 23 | 6 | |
|---|
| 24 | 7 | #include <linux/t10-pi.h> |
|---|
| 25 | 8 | #include <linux/blkdev.h> |
|---|
| 26 | 9 | #include <linux/crc-t10dif.h> |
|---|
| 10 | +#include <linux/module.h> |
|---|
| 27 | 11 | #include <net/checksum.h> |
|---|
| 28 | 12 | |
|---|
| 29 | 13 | typedef __be16 (csum_fn) (void *, unsigned int); |
|---|
| .. | .. |
|---|
| 44 | 28 | * tag. |
|---|
| 45 | 29 | */ |
|---|
| 46 | 30 | static blk_status_t t10_pi_generate(struct blk_integrity_iter *iter, |
|---|
| 47 | | - csum_fn *fn, unsigned int type) |
|---|
| 31 | + csum_fn *fn, enum t10_dif_type type) |
|---|
| 48 | 32 | { |
|---|
| 49 | 33 | unsigned int i; |
|---|
| 50 | 34 | |
|---|
| .. | .. |
|---|
| 54 | 38 | pi->guard_tag = fn(iter->data_buf, iter->interval); |
|---|
| 55 | 39 | pi->app_tag = 0; |
|---|
| 56 | 40 | |
|---|
| 57 | | - if (type == 1) |
|---|
| 41 | + if (type == T10_PI_TYPE1_PROTECTION) |
|---|
| 58 | 42 | pi->ref_tag = cpu_to_be32(lower_32_bits(iter->seed)); |
|---|
| 59 | 43 | else |
|---|
| 60 | 44 | pi->ref_tag = 0; |
|---|
| .. | .. |
|---|
| 68 | 52 | } |
|---|
| 69 | 53 | |
|---|
| 70 | 54 | static blk_status_t t10_pi_verify(struct blk_integrity_iter *iter, |
|---|
| 71 | | - csum_fn *fn, unsigned int type) |
|---|
| 55 | + csum_fn *fn, enum t10_dif_type type) |
|---|
| 72 | 56 | { |
|---|
| 73 | 57 | unsigned int i; |
|---|
| 58 | + |
|---|
| 59 | + BUG_ON(type == T10_PI_TYPE0_PROTECTION); |
|---|
| 74 | 60 | |
|---|
| 75 | 61 | for (i = 0 ; i < iter->data_size ; i += iter->interval) { |
|---|
| 76 | 62 | struct t10_pi_tuple *pi = iter->prot_buf; |
|---|
| 77 | 63 | __be16 csum; |
|---|
| 78 | 64 | |
|---|
| 79 | | - switch (type) { |
|---|
| 80 | | - case 1: |
|---|
| 81 | | - case 2: |
|---|
| 65 | + if (type == T10_PI_TYPE1_PROTECTION || |
|---|
| 66 | + type == T10_PI_TYPE2_PROTECTION) { |
|---|
| 82 | 67 | if (pi->app_tag == T10_PI_APP_ESCAPE) |
|---|
| 83 | 68 | goto next; |
|---|
| 84 | 69 | |
|---|
| .. | .. |
|---|
| 90 | 75 | iter->seed, be32_to_cpu(pi->ref_tag)); |
|---|
| 91 | 76 | return BLK_STS_PROTECTION; |
|---|
| 92 | 77 | } |
|---|
| 93 | | - break; |
|---|
| 94 | | - case 3: |
|---|
| 78 | + } else if (type == T10_PI_TYPE3_PROTECTION) { |
|---|
| 95 | 79 | if (pi->app_tag == T10_PI_APP_ESCAPE && |
|---|
| 96 | 80 | pi->ref_tag == T10_PI_REF_ESCAPE) |
|---|
| 97 | 81 | goto next; |
|---|
| 98 | | - break; |
|---|
| 99 | 82 | } |
|---|
| 100 | 83 | |
|---|
| 101 | 84 | csum = fn(iter->data_buf, iter->interval); |
|---|
| .. | .. |
|---|
| 119 | 102 | |
|---|
| 120 | 103 | static blk_status_t t10_pi_type1_generate_crc(struct blk_integrity_iter *iter) |
|---|
| 121 | 104 | { |
|---|
| 122 | | - return t10_pi_generate(iter, t10_pi_crc_fn, 1); |
|---|
| 105 | + return t10_pi_generate(iter, t10_pi_crc_fn, T10_PI_TYPE1_PROTECTION); |
|---|
| 123 | 106 | } |
|---|
| 124 | 107 | |
|---|
| 125 | 108 | static blk_status_t t10_pi_type1_generate_ip(struct blk_integrity_iter *iter) |
|---|
| 126 | 109 | { |
|---|
| 127 | | - return t10_pi_generate(iter, t10_pi_ip_fn, 1); |
|---|
| 110 | + return t10_pi_generate(iter, t10_pi_ip_fn, T10_PI_TYPE1_PROTECTION); |
|---|
| 128 | 111 | } |
|---|
| 129 | 112 | |
|---|
| 130 | 113 | static blk_status_t t10_pi_type1_verify_crc(struct blk_integrity_iter *iter) |
|---|
| 131 | 114 | { |
|---|
| 132 | | - return t10_pi_verify(iter, t10_pi_crc_fn, 1); |
|---|
| 115 | + return t10_pi_verify(iter, t10_pi_crc_fn, T10_PI_TYPE1_PROTECTION); |
|---|
| 133 | 116 | } |
|---|
| 134 | 117 | |
|---|
| 135 | 118 | static blk_status_t t10_pi_type1_verify_ip(struct blk_integrity_iter *iter) |
|---|
| 136 | 119 | { |
|---|
| 137 | | - return t10_pi_verify(iter, t10_pi_ip_fn, 1); |
|---|
| 120 | + return t10_pi_verify(iter, t10_pi_ip_fn, T10_PI_TYPE1_PROTECTION); |
|---|
| 138 | 121 | } |
|---|
| 139 | | - |
|---|
| 140 | | -static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter) |
|---|
| 141 | | -{ |
|---|
| 142 | | - return t10_pi_generate(iter, t10_pi_crc_fn, 3); |
|---|
| 143 | | -} |
|---|
| 144 | | - |
|---|
| 145 | | -static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter) |
|---|
| 146 | | -{ |
|---|
| 147 | | - return t10_pi_generate(iter, t10_pi_ip_fn, 3); |
|---|
| 148 | | -} |
|---|
| 149 | | - |
|---|
| 150 | | -static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter) |
|---|
| 151 | | -{ |
|---|
| 152 | | - return t10_pi_verify(iter, t10_pi_crc_fn, 3); |
|---|
| 153 | | -} |
|---|
| 154 | | - |
|---|
| 155 | | -static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter) |
|---|
| 156 | | -{ |
|---|
| 157 | | - return t10_pi_verify(iter, t10_pi_ip_fn, 3); |
|---|
| 158 | | -} |
|---|
| 159 | | - |
|---|
| 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, |
|---|
| 164 | | -}; |
|---|
| 165 | | -EXPORT_SYMBOL(t10_pi_type1_crc); |
|---|
| 166 | | - |
|---|
| 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, |
|---|
| 171 | | -}; |
|---|
| 172 | | -EXPORT_SYMBOL(t10_pi_type1_ip); |
|---|
| 173 | | - |
|---|
| 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, |
|---|
| 178 | | -}; |
|---|
| 179 | | -EXPORT_SYMBOL(t10_pi_type3_crc); |
|---|
| 180 | | - |
|---|
| 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, |
|---|
| 185 | | -}; |
|---|
| 186 | | -EXPORT_SYMBOL(t10_pi_type3_ip); |
|---|
| 187 | 122 | |
|---|
| 188 | 123 | /** |
|---|
| 189 | | - * t10_pi_prepare - prepare PI prior submitting request to device |
|---|
| 124 | + * t10_pi_type1_prepare - prepare PI prior submitting request to device |
|---|
| 190 | 125 | * @rq: request with PI that should be prepared |
|---|
| 191 | | - * @protection_type: PI type (Type 1/Type 2/Type 3) |
|---|
| 192 | 126 | * |
|---|
| 193 | 127 | * For Type 1/Type 2, the virtual start sector is the one that was |
|---|
| 194 | 128 | * originally submitted by the block layer for the ref_tag usage. Due to |
|---|
| 195 | 129 | * partitioning, MD/DM cloning, etc. the actual physical start sector is |
|---|
| 196 | 130 | * likely to be different. Remap protection information to match the |
|---|
| 197 | 131 | * physical LBA. |
|---|
| 198 | | - * |
|---|
| 199 | | - * Type 3 does not have a reference tag so no remapping is required. |
|---|
| 200 | 132 | */ |
|---|
| 201 | | -void t10_pi_prepare(struct request *rq, u8 protection_type) |
|---|
| 133 | +static void t10_pi_type1_prepare(struct request *rq) |
|---|
| 202 | 134 | { |
|---|
| 203 | 135 | const int tuple_sz = rq->q->integrity.tuple_size; |
|---|
| 204 | 136 | u32 ref_tag = t10_pi_ref_tag(rq); |
|---|
| 205 | 137 | struct bio *bio; |
|---|
| 206 | | - |
|---|
| 207 | | - if (protection_type == T10_PI_TYPE3_PROTECTION) |
|---|
| 208 | | - return; |
|---|
| 209 | 138 | |
|---|
| 210 | 139 | __rq_for_each_bio(bio, rq) { |
|---|
| 211 | 140 | struct bio_integrity_payload *bip = bio_integrity(bio); |
|---|
| .. | .. |
|---|
| 239 | 168 | bip->bip_flags |= BIP_MAPPED_INTEGRITY; |
|---|
| 240 | 169 | } |
|---|
| 241 | 170 | } |
|---|
| 242 | | -EXPORT_SYMBOL(t10_pi_prepare); |
|---|
| 243 | 171 | |
|---|
| 244 | 172 | /** |
|---|
| 245 | | - * t10_pi_complete - prepare PI prior returning request to the block layer |
|---|
| 173 | + * t10_pi_type1_complete - prepare PI prior returning request to the blk layer |
|---|
| 246 | 174 | * @rq: request with PI that should be prepared |
|---|
| 247 | | - * @protection_type: PI type (Type 1/Type 2/Type 3) |
|---|
| 248 | | - * @intervals: total elements to prepare |
|---|
| 175 | + * @nr_bytes: total bytes to prepare |
|---|
| 249 | 176 | * |
|---|
| 250 | 177 | * For Type 1/Type 2, the virtual start sector is the one that was |
|---|
| 251 | 178 | * originally submitted by the block layer for the ref_tag usage. Due to |
|---|
| .. | .. |
|---|
| 253 | 180 | * likely to be different. Since the physical start sector was submitted |
|---|
| 254 | 181 | * to the device, we should remap it back to virtual values expected by the |
|---|
| 255 | 182 | * block layer. |
|---|
| 256 | | - * |
|---|
| 257 | | - * Type 3 does not have a reference tag so no remapping is required. |
|---|
| 258 | 183 | */ |
|---|
| 259 | | -void t10_pi_complete(struct request *rq, u8 protection_type, |
|---|
| 260 | | - unsigned int intervals) |
|---|
| 184 | +static void t10_pi_type1_complete(struct request *rq, unsigned int nr_bytes) |
|---|
| 261 | 185 | { |
|---|
| 186 | + unsigned intervals = nr_bytes >> rq->q->integrity.interval_exp; |
|---|
| 262 | 187 | const int tuple_sz = rq->q->integrity.tuple_size; |
|---|
| 263 | 188 | u32 ref_tag = t10_pi_ref_tag(rq); |
|---|
| 264 | 189 | struct bio *bio; |
|---|
| 265 | | - |
|---|
| 266 | | - if (protection_type == T10_PI_TYPE3_PROTECTION) |
|---|
| 267 | | - return; |
|---|
| 268 | 190 | |
|---|
| 269 | 191 | __rq_for_each_bio(bio, rq) { |
|---|
| 270 | 192 | struct bio_integrity_payload *bip = bio_integrity(bio); |
|---|
| .. | .. |
|---|
| 293 | 215 | } |
|---|
| 294 | 216 | } |
|---|
| 295 | 217 | } |
|---|
| 296 | | -EXPORT_SYMBOL(t10_pi_complete); |
|---|
| 218 | + |
|---|
| 219 | +static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter) |
|---|
| 220 | +{ |
|---|
| 221 | + return t10_pi_generate(iter, t10_pi_crc_fn, T10_PI_TYPE3_PROTECTION); |
|---|
| 222 | +} |
|---|
| 223 | + |
|---|
| 224 | +static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter) |
|---|
| 225 | +{ |
|---|
| 226 | + return t10_pi_generate(iter, t10_pi_ip_fn, T10_PI_TYPE3_PROTECTION); |
|---|
| 227 | +} |
|---|
| 228 | + |
|---|
| 229 | +static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter) |
|---|
| 230 | +{ |
|---|
| 231 | + return t10_pi_verify(iter, t10_pi_crc_fn, T10_PI_TYPE3_PROTECTION); |
|---|
| 232 | +} |
|---|
| 233 | + |
|---|
| 234 | +static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter) |
|---|
| 235 | +{ |
|---|
| 236 | + return t10_pi_verify(iter, t10_pi_ip_fn, T10_PI_TYPE3_PROTECTION); |
|---|
| 237 | +} |
|---|
| 238 | + |
|---|
| 239 | +/* Type 3 does not have a reference tag so no remapping is required. */ |
|---|
| 240 | +static void t10_pi_type3_prepare(struct request *rq) |
|---|
| 241 | +{ |
|---|
| 242 | +} |
|---|
| 243 | + |
|---|
| 244 | +/* Type 3 does not have a reference tag so no remapping is required. */ |
|---|
| 245 | +static void t10_pi_type3_complete(struct request *rq, unsigned int nr_bytes) |
|---|
| 246 | +{ |
|---|
| 247 | +} |
|---|
| 248 | + |
|---|
| 249 | +const struct blk_integrity_profile t10_pi_type1_crc = { |
|---|
| 250 | + .name = "T10-DIF-TYPE1-CRC", |
|---|
| 251 | + .generate_fn = t10_pi_type1_generate_crc, |
|---|
| 252 | + .verify_fn = t10_pi_type1_verify_crc, |
|---|
| 253 | + .prepare_fn = t10_pi_type1_prepare, |
|---|
| 254 | + .complete_fn = t10_pi_type1_complete, |
|---|
| 255 | +}; |
|---|
| 256 | +EXPORT_SYMBOL(t10_pi_type1_crc); |
|---|
| 257 | + |
|---|
| 258 | +const struct blk_integrity_profile t10_pi_type1_ip = { |
|---|
| 259 | + .name = "T10-DIF-TYPE1-IP", |
|---|
| 260 | + .generate_fn = t10_pi_type1_generate_ip, |
|---|
| 261 | + .verify_fn = t10_pi_type1_verify_ip, |
|---|
| 262 | + .prepare_fn = t10_pi_type1_prepare, |
|---|
| 263 | + .complete_fn = t10_pi_type1_complete, |
|---|
| 264 | +}; |
|---|
| 265 | +EXPORT_SYMBOL(t10_pi_type1_ip); |
|---|
| 266 | + |
|---|
| 267 | +const struct blk_integrity_profile t10_pi_type3_crc = { |
|---|
| 268 | + .name = "T10-DIF-TYPE3-CRC", |
|---|
| 269 | + .generate_fn = t10_pi_type3_generate_crc, |
|---|
| 270 | + .verify_fn = t10_pi_type3_verify_crc, |
|---|
| 271 | + .prepare_fn = t10_pi_type3_prepare, |
|---|
| 272 | + .complete_fn = t10_pi_type3_complete, |
|---|
| 273 | +}; |
|---|
| 274 | +EXPORT_SYMBOL(t10_pi_type3_crc); |
|---|
| 275 | + |
|---|
| 276 | +const struct blk_integrity_profile t10_pi_type3_ip = { |
|---|
| 277 | + .name = "T10-DIF-TYPE3-IP", |
|---|
| 278 | + .generate_fn = t10_pi_type3_generate_ip, |
|---|
| 279 | + .verify_fn = t10_pi_type3_verify_ip, |
|---|
| 280 | + .prepare_fn = t10_pi_type3_prepare, |
|---|
| 281 | + .complete_fn = t10_pi_type3_complete, |
|---|
| 282 | +}; |
|---|
| 283 | +EXPORT_SYMBOL(t10_pi_type3_ip); |
|---|
| 284 | + |
|---|
| 285 | +MODULE_LICENSE("GPL"); |
|---|