sd_dif.c 12 KB

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