sd_dif.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #include <linux/blkdev.h>
  23. #include <linux/crc-t10dif.h>
  24. #include <scsi/scsi.h>
  25. #include <scsi/scsi_cmnd.h>
  26. #include <scsi/scsi_dbg.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_driver.h>
  29. #include <scsi/scsi_eh.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_ioctl.h>
  32. #include <scsi/scsicam.h>
  33. #include <net/checksum.h>
  34. #include "sd.h"
  35. typedef __u16 (csum_fn) (void *, unsigned int);
  36. static __u16 sd_dif_crc_fn(void *data, unsigned int len)
  37. {
  38. return cpu_to_be16(crc_t10dif(data, len));
  39. }
  40. static __u16 sd_dif_ip_fn(void *data, unsigned int len)
  41. {
  42. return ip_compute_csum(data, len);
  43. }
  44. /*
  45. * Type 1 and Type 2 protection use the same format: 16 bit guard tag,
  46. * 16 bit app tag, 32 bit reference tag.
  47. */
  48. static void sd_dif_type1_generate(struct blk_integrity_exchg *bix, csum_fn *fn)
  49. {
  50. void *buf = bix->data_buf;
  51. struct sd_dif_tuple *sdt = bix->prot_buf;
  52. sector_t sector = bix->sector;
  53. unsigned int i;
  54. for (i = 0 ; i < bix->data_size ; i += bix->sector_size, sdt++) {
  55. sdt->guard_tag = fn(buf, bix->sector_size);
  56. sdt->ref_tag = cpu_to_be32(sector & 0xffffffff);
  57. sdt->app_tag = 0;
  58. buf += bix->sector_size;
  59. sector++;
  60. }
  61. }
  62. static void sd_dif_type1_generate_crc(struct blk_integrity_exchg *bix)
  63. {
  64. sd_dif_type1_generate(bix, sd_dif_crc_fn);
  65. }
  66. static void sd_dif_type1_generate_ip(struct blk_integrity_exchg *bix)
  67. {
  68. sd_dif_type1_generate(bix, sd_dif_ip_fn);
  69. }
  70. static int sd_dif_type1_verify(struct blk_integrity_exchg *bix, csum_fn *fn)
  71. {
  72. void *buf = bix->data_buf;
  73. struct sd_dif_tuple *sdt = bix->prot_buf;
  74. sector_t sector = bix->sector;
  75. unsigned int i;
  76. __u16 csum;
  77. for (i = 0 ; i < bix->data_size ; i += bix->sector_size, sdt++) {
  78. /* Unwritten sectors */
  79. if (sdt->app_tag == 0xffff)
  80. return 0;
  81. if (be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
  82. printk(KERN_ERR
  83. "%s: ref tag error on sector %lu (rcvd %u)\n",
  84. bix->disk_name, (unsigned long)sector,
  85. be32_to_cpu(sdt->ref_tag));
  86. return -EIO;
  87. }
  88. csum = fn(buf, bix->sector_size);
  89. if (sdt->guard_tag != csum) {
  90. printk(KERN_ERR "%s: guard tag error on sector %lu " \
  91. "(rcvd %04x, data %04x)\n", bix->disk_name,
  92. (unsigned long)sector,
  93. be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum));
  94. return -EIO;
  95. }
  96. buf += bix->sector_size;
  97. sector++;
  98. }
  99. return 0;
  100. }
  101. static int sd_dif_type1_verify_crc(struct blk_integrity_exchg *bix)
  102. {
  103. return sd_dif_type1_verify(bix, sd_dif_crc_fn);
  104. }
  105. static int sd_dif_type1_verify_ip(struct blk_integrity_exchg *bix)
  106. {
  107. return sd_dif_type1_verify(bix, sd_dif_ip_fn);
  108. }
  109. /*
  110. * Functions for interleaving and deinterleaving application tags
  111. */
  112. static void sd_dif_type1_set_tag(void *prot, void *tag_buf, unsigned int sectors)
  113. {
  114. struct sd_dif_tuple *sdt = prot;
  115. u8 *tag = tag_buf;
  116. unsigned int i, j;
  117. for (i = 0, j = 0 ; i < sectors ; i++, j += 2, sdt++) {
  118. sdt->app_tag = tag[j] << 8 | tag[j+1];
  119. BUG_ON(sdt->app_tag == 0xffff);
  120. }
  121. }
  122. static void sd_dif_type1_get_tag(void *prot, void *tag_buf, unsigned int sectors)
  123. {
  124. struct sd_dif_tuple *sdt = prot;
  125. u8 *tag = tag_buf;
  126. unsigned int i, j;
  127. for (i = 0, j = 0 ; i < sectors ; i++, j += 2, sdt++) {
  128. tag[j] = (sdt->app_tag & 0xff00) >> 8;
  129. tag[j+1] = sdt->app_tag & 0xff;
  130. }
  131. }
  132. static struct blk_integrity dif_type1_integrity_crc = {
  133. .name = "T10-DIF-TYPE1-CRC",
  134. .generate_fn = sd_dif_type1_generate_crc,
  135. .verify_fn = sd_dif_type1_verify_crc,
  136. .get_tag_fn = sd_dif_type1_get_tag,
  137. .set_tag_fn = sd_dif_type1_set_tag,
  138. .tuple_size = sizeof(struct sd_dif_tuple),
  139. .tag_size = 0,
  140. };
  141. static struct blk_integrity dif_type1_integrity_ip = {
  142. .name = "T10-DIF-TYPE1-IP",
  143. .generate_fn = sd_dif_type1_generate_ip,
  144. .verify_fn = sd_dif_type1_verify_ip,
  145. .get_tag_fn = sd_dif_type1_get_tag,
  146. .set_tag_fn = sd_dif_type1_set_tag,
  147. .tuple_size = sizeof(struct sd_dif_tuple),
  148. .tag_size = 0,
  149. };
  150. /*
  151. * Type 3 protection has a 16-bit guard tag and 16 + 32 bits of opaque
  152. * tag space.
  153. */
  154. static void sd_dif_type3_generate(struct blk_integrity_exchg *bix, csum_fn *fn)
  155. {
  156. void *buf = bix->data_buf;
  157. struct sd_dif_tuple *sdt = bix->prot_buf;
  158. unsigned int i;
  159. for (i = 0 ; i < bix->data_size ; i += bix->sector_size, sdt++) {
  160. sdt->guard_tag = fn(buf, bix->sector_size);
  161. sdt->ref_tag = 0;
  162. sdt->app_tag = 0;
  163. buf += bix->sector_size;
  164. }
  165. }
  166. static void sd_dif_type3_generate_crc(struct blk_integrity_exchg *bix)
  167. {
  168. sd_dif_type3_generate(bix, sd_dif_crc_fn);
  169. }
  170. static void sd_dif_type3_generate_ip(struct blk_integrity_exchg *bix)
  171. {
  172. sd_dif_type3_generate(bix, sd_dif_ip_fn);
  173. }
  174. static int sd_dif_type3_verify(struct blk_integrity_exchg *bix, csum_fn *fn)
  175. {
  176. void *buf = bix->data_buf;
  177. struct sd_dif_tuple *sdt = bix->prot_buf;
  178. sector_t sector = bix->sector;
  179. unsigned int i;
  180. __u16 csum;
  181. for (i = 0 ; i < bix->data_size ; i += bix->sector_size, sdt++) {
  182. /* Unwritten sectors */
  183. if (sdt->app_tag == 0xffff && sdt->ref_tag == 0xffffffff)
  184. return 0;
  185. csum = fn(buf, bix->sector_size);
  186. if (sdt->guard_tag != csum) {
  187. printk(KERN_ERR "%s: guard tag error on sector %lu " \
  188. "(rcvd %04x, data %04x)\n", bix->disk_name,
  189. (unsigned long)sector,
  190. be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum));
  191. return -EIO;
  192. }
  193. buf += bix->sector_size;
  194. sector++;
  195. }
  196. return 0;
  197. }
  198. static int sd_dif_type3_verify_crc(struct blk_integrity_exchg *bix)
  199. {
  200. return sd_dif_type3_verify(bix, sd_dif_crc_fn);
  201. }
  202. static int sd_dif_type3_verify_ip(struct blk_integrity_exchg *bix)
  203. {
  204. return sd_dif_type3_verify(bix, sd_dif_ip_fn);
  205. }
  206. static void sd_dif_type3_set_tag(void *prot, void *tag_buf, unsigned int sectors)
  207. {
  208. struct sd_dif_tuple *sdt = prot;
  209. u8 *tag = tag_buf;
  210. unsigned int i, j;
  211. for (i = 0, j = 0 ; i < sectors ; i++, j += 6, sdt++) {
  212. sdt->app_tag = tag[j] << 8 | tag[j+1];
  213. sdt->ref_tag = tag[j+2] << 24 | tag[j+3] << 16 |
  214. tag[j+4] << 8 | tag[j+5];
  215. }
  216. }
  217. static void sd_dif_type3_get_tag(void *prot, void *tag_buf, unsigned int sectors)
  218. {
  219. struct sd_dif_tuple *sdt = prot;
  220. u8 *tag = tag_buf;
  221. unsigned int i, j;
  222. for (i = 0, j = 0 ; i < sectors ; i++, j += 2, sdt++) {
  223. tag[j] = (sdt->app_tag & 0xff00) >> 8;
  224. tag[j+1] = sdt->app_tag & 0xff;
  225. tag[j+2] = (sdt->ref_tag & 0xff000000) >> 24;
  226. tag[j+3] = (sdt->ref_tag & 0xff0000) >> 16;
  227. tag[j+4] = (sdt->ref_tag & 0xff00) >> 8;
  228. tag[j+5] = sdt->ref_tag & 0xff;
  229. BUG_ON(sdt->app_tag == 0xffff || sdt->ref_tag == 0xffffffff);
  230. }
  231. }
  232. static struct blk_integrity dif_type3_integrity_crc = {
  233. .name = "T10-DIF-TYPE3-CRC",
  234. .generate_fn = sd_dif_type3_generate_crc,
  235. .verify_fn = sd_dif_type3_verify_crc,
  236. .get_tag_fn = sd_dif_type3_get_tag,
  237. .set_tag_fn = sd_dif_type3_set_tag,
  238. .tuple_size = sizeof(struct sd_dif_tuple),
  239. .tag_size = 0,
  240. };
  241. static struct blk_integrity dif_type3_integrity_ip = {
  242. .name = "T10-DIF-TYPE3-IP",
  243. .generate_fn = sd_dif_type3_generate_ip,
  244. .verify_fn = sd_dif_type3_verify_ip,
  245. .get_tag_fn = sd_dif_type3_get_tag,
  246. .set_tag_fn = sd_dif_type3_set_tag,
  247. .tuple_size = sizeof(struct sd_dif_tuple),
  248. .tag_size = 0,
  249. };
  250. /*
  251. * Configure exchange of protection information between OS and HBA.
  252. */
  253. void sd_dif_config_host(struct scsi_disk *sdkp)
  254. {
  255. struct scsi_device *sdp = sdkp->device;
  256. struct gendisk *disk = sdkp->disk;
  257. u8 type = sdkp->protection_type;
  258. int dif, dix;
  259. dif = scsi_host_dif_capable(sdp->host, type);
  260. dix = scsi_host_dix_capable(sdp->host, type);
  261. if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
  262. dif = 0; dix = 1;
  263. }
  264. if (!dix)
  265. return;
  266. /* Enable DMA of protection information */
  267. if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP)
  268. if (type == SD_DIF_TYPE3_PROTECTION)
  269. blk_integrity_register(disk, &dif_type3_integrity_ip);
  270. else
  271. blk_integrity_register(disk, &dif_type1_integrity_ip);
  272. else
  273. if (type == SD_DIF_TYPE3_PROTECTION)
  274. blk_integrity_register(disk, &dif_type3_integrity_crc);
  275. else
  276. blk_integrity_register(disk, &dif_type1_integrity_crc);
  277. sd_printk(KERN_NOTICE, sdkp,
  278. "Enabling DIX %s protection\n", disk->integrity->name);
  279. /* Signal to block layer that we support sector tagging */
  280. if (dif && type && sdkp->ATO) {
  281. if (type == SD_DIF_TYPE3_PROTECTION)
  282. disk->integrity->tag_size = sizeof(u16) + sizeof(u32);
  283. else
  284. disk->integrity->tag_size = sizeof(u16);
  285. sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n",
  286. disk->integrity->tag_size);
  287. }
  288. }
  289. /*
  290. * The virtual start sector is the one that was originally submitted
  291. * by the block layer. Due to partitioning, MD/DM cloning, etc. the
  292. * actual physical start sector is likely to be different. Remap
  293. * protection information to match the physical LBA.
  294. *
  295. * From a protocol perspective there's a slight difference between
  296. * Type 1 and 2. The latter uses 32-byte CDBs exclusively, and the
  297. * reference tag is seeded in the CDB. This gives us the potential to
  298. * avoid virt->phys remapping during write. However, at read time we
  299. * don't know whether the virt sector is the same as when we wrote it
  300. * (we could be reading from real disk as opposed to MD/DM device. So
  301. * we always remap Type 2 making it identical to Type 1.
  302. *
  303. * Type 3 does not have a reference tag so no remapping is required.
  304. */
  305. void sd_dif_prepare(struct request *rq, sector_t hw_sector,
  306. unsigned int sector_sz)
  307. {
  308. const int tuple_sz = sizeof(struct sd_dif_tuple);
  309. struct bio *bio;
  310. struct scsi_disk *sdkp;
  311. struct sd_dif_tuple *sdt;
  312. unsigned int i, j;
  313. u32 phys, virt;
  314. sdkp = rq->bio->bi_bdev->bd_disk->private_data;
  315. if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION)
  316. return;
  317. phys = hw_sector & 0xffffffff;
  318. __rq_for_each_bio(bio, rq) {
  319. struct bio_vec *iv;
  320. /* Already remapped? */
  321. if (bio_flagged(bio, BIO_MAPPED_INTEGRITY))
  322. break;
  323. virt = bio->bi_integrity->bip_sector & 0xffffffff;
  324. bip_for_each_vec(iv, bio->bi_integrity, i) {
  325. sdt = kmap_atomic(iv->bv_page)
  326. + iv->bv_offset;
  327. for (j = 0 ; j < iv->bv_len ; j += tuple_sz, sdt++) {
  328. if (be32_to_cpu(sdt->ref_tag) == virt)
  329. sdt->ref_tag = cpu_to_be32(phys);
  330. virt++;
  331. phys++;
  332. }
  333. kunmap_atomic(sdt);
  334. }
  335. bio->bi_flags |= (1 << BIO_MAPPED_INTEGRITY);
  336. }
  337. }
  338. /*
  339. * Remap physical sector values in the reference tag to the virtual
  340. * values expected by the block layer.
  341. */
  342. void sd_dif_complete(struct scsi_cmnd *scmd, unsigned int good_bytes)
  343. {
  344. const int tuple_sz = sizeof(struct sd_dif_tuple);
  345. struct scsi_disk *sdkp;
  346. struct bio *bio;
  347. struct sd_dif_tuple *sdt;
  348. unsigned int i, j, sectors, sector_sz;
  349. u32 phys, virt;
  350. sdkp = scsi_disk(scmd->request->rq_disk);
  351. if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION || good_bytes == 0)
  352. return;
  353. sector_sz = scmd->device->sector_size;
  354. sectors = good_bytes / sector_sz;
  355. phys = blk_rq_pos(scmd->request) & 0xffffffff;
  356. if (sector_sz == 4096)
  357. phys >>= 3;
  358. __rq_for_each_bio(bio, scmd->request) {
  359. struct bio_vec *iv;
  360. virt = bio->bi_integrity->bip_sector & 0xffffffff;
  361. bip_for_each_vec(iv, bio->bi_integrity, i) {
  362. sdt = kmap_atomic(iv->bv_page)
  363. + iv->bv_offset;
  364. for (j = 0 ; j < iv->bv_len ; j += tuple_sz, sdt++) {
  365. if (sectors == 0) {
  366. kunmap_atomic(sdt);
  367. return;
  368. }
  369. if (be32_to_cpu(sdt->ref_tag) == phys)
  370. sdt->ref_tag = cpu_to_be32(virt);
  371. virt++;
  372. phys++;
  373. sectors--;
  374. }
  375. kunmap_atomic(sdt);
  376. }
  377. }
  378. }