sd_dif.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sd_dif.c - SCSI Data Integrity Field
  4. *
  5. * Copyright (C) 2007, 2008 Oracle Corporation
  6. * Written by: Martin K. Petersen <[email protected]>
  7. */
  8. #include <linux/blk-integrity.h>
  9. #include <linux/t10-pi.h>
  10. #include <scsi/scsi.h>
  11. #include <scsi/scsi_cmnd.h>
  12. #include <scsi/scsi_dbg.h>
  13. #include <scsi/scsi_device.h>
  14. #include <scsi/scsi_driver.h>
  15. #include <scsi/scsi_eh.h>
  16. #include <scsi/scsi_host.h>
  17. #include <scsi/scsi_ioctl.h>
  18. #include <scsi/scsicam.h>
  19. #include "sd.h"
  20. /*
  21. * Configure exchange of protection information between OS and HBA.
  22. */
  23. void sd_dif_config_host(struct scsi_disk *sdkp)
  24. {
  25. struct scsi_device *sdp = sdkp->device;
  26. struct gendisk *disk = sdkp->disk;
  27. u8 type = sdkp->protection_type;
  28. struct blk_integrity bi;
  29. int dif, dix;
  30. dif = scsi_host_dif_capable(sdp->host, type);
  31. dix = scsi_host_dix_capable(sdp->host, type);
  32. if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
  33. dif = 0; dix = 1;
  34. }
  35. if (!dix)
  36. return;
  37. memset(&bi, 0, sizeof(bi));
  38. /* Enable DMA of protection information */
  39. if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP) {
  40. if (type == T10_PI_TYPE3_PROTECTION)
  41. bi.profile = &t10_pi_type3_ip;
  42. else
  43. bi.profile = &t10_pi_type1_ip;
  44. bi.flags |= BLK_INTEGRITY_IP_CHECKSUM;
  45. } else
  46. if (type == T10_PI_TYPE3_PROTECTION)
  47. bi.profile = &t10_pi_type3_crc;
  48. else
  49. bi.profile = &t10_pi_type1_crc;
  50. bi.tuple_size = sizeof(struct t10_pi_tuple);
  51. if (dif && type) {
  52. bi.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
  53. if (!sdkp->ATO)
  54. goto out;
  55. if (type == T10_PI_TYPE3_PROTECTION)
  56. bi.tag_size = sizeof(u16) + sizeof(u32);
  57. else
  58. bi.tag_size = sizeof(u16);
  59. }
  60. sd_printk(KERN_NOTICE, sdkp,
  61. "Enabling DIX %s, application tag size %u bytes\n",
  62. bi.profile->name, bi.tag_size);
  63. out:
  64. blk_integrity_register(disk, &bi);
  65. }