sd_dif.c 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * sd_dif.c - SCSI Data Integrity Field
 *
 * Copyright (C) 2007, 2008 Oracle Corporation
 * Written by: Martin K. Petersen <martin.petersen@oracle.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
 * USA.
 *
 */

#include <linux/blkdev.h>
24
#include <linux/t10-pi.h>
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_dbg.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_driver.h>
#include <scsi/scsi_eh.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/scsicam.h>

#include "sd.h"

/*
 * Configure exchange of protection information between OS and HBA.
 */
void sd_dif_config_host(struct scsi_disk *sdkp)
{
	struct scsi_device *sdp = sdkp->device;
	struct gendisk *disk = sdkp->disk;
	u8 type = sdkp->protection_type;
46
	int dif, dix;
47

48 49
	dif = scsi_host_dif_capable(sdp->host, type);
	dix = scsi_host_dix_capable(sdp->host, type);
50

51 52 53
	if (!dix && scsi_host_dix_capable(sdp->host, 0)) {
		dif = 0; dix = 1;
	}
54

55
	if (!dix)
56 57 58
		return;

	/* Enable DMA of protection information */
59
	if (scsi_host_get_guard(sdkp->device->host) & SHOST_DIX_GUARD_IP) {
60
		if (type == SD_DIF_TYPE3_PROTECTION)
61
			blk_integrity_register(disk, &t10_pi_type3_ip);
62
		else
63
			blk_integrity_register(disk, &t10_pi_type1_ip);
64 65 66

		disk->integrity->flags |= BLK_INTEGRITY_IP_CHECKSUM;
	} else
67
		if (type == SD_DIF_TYPE3_PROTECTION)
68
			blk_integrity_register(disk, &t10_pi_type3_crc);
69
		else
70
			blk_integrity_register(disk, &t10_pi_type1_crc);
71

72
	sd_printk(KERN_NOTICE, sdkp,
73
		  "Enabling DIX %s protection\n", disk->integrity->name);
74 75

	/* Signal to block layer that we support sector tagging */
76 77 78 79
	if (dif && type) {

		disk->integrity->flags |= BLK_INTEGRITY_DEVICE_CAPABLE;

80
		if (!sdkp->ATO)
81 82
			return;

83 84 85 86 87
		if (type == SD_DIF_TYPE3_PROTECTION)
			disk->integrity->tag_size = sizeof(u16) + sizeof(u32);
		else
			disk->integrity->tag_size = sizeof(u16);

88
		sd_printk(KERN_NOTICE, sdkp, "DIF application tag size %u\n",
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
			  disk->integrity->tag_size);
	}
}

/*
 * The virtual start sector is the one that was originally submitted
 * by the block layer.	Due to partitioning, MD/DM cloning, etc. the
 * actual physical start sector is likely to be different.  Remap
 * protection information to match the physical LBA.
 *
 * From a protocol perspective there's a slight difference between
 * Type 1 and 2.  The latter uses 32-byte CDBs exclusively, and the
 * reference tag is seeded in the CDB.  This gives us the potential to
 * avoid virt->phys remapping during write.  However, at read time we
 * don't know whether the virt sector is the same as when we wrote it
 * (we could be reading from real disk as opposed to MD/DM device.  So
 * we always remap Type 2 making it identical to Type 1.
 *
 * Type 3 does not have a reference tag so no remapping is required.
 */
109
void sd_dif_prepare(struct scsi_cmnd *scmd)
110
{
111
	const int tuple_sz = sizeof(struct t10_pi_tuple);
112 113
	struct bio *bio;
	struct scsi_disk *sdkp;
114
	struct t10_pi_tuple *pi;
115 116
	u32 phys, virt;

117
	sdkp = scsi_disk(scmd->request->rq_disk);
118 119

	if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION)
120
		return;
121

122
	phys = scsi_prot_ref_tag(scmd);
123

124
	__rq_for_each_bio(bio, scmd->request) {
125
		struct bio_integrity_payload *bip = bio_integrity(bio);
126 127 128
		struct bio_vec iv;
		struct bvec_iter iter;
		unsigned int j;
129

130
		/* Already remapped? */
131
		if (bip->bip_flags & BIP_MAPPED_INTEGRITY)
132 133
			break;

134
		virt = bip_get_seed(bip) & 0xffffffff;
135

136
		bip_for_each_vec(iv, bip, iter) {
137
			pi = kmap_atomic(iv.bv_page) + iv.bv_offset;
138

139
			for (j = 0; j < iv.bv_len; j += tuple_sz, pi++) {
140

141 142
				if (be32_to_cpu(pi->ref_tag) == virt)
					pi->ref_tag = cpu_to_be32(phys);
143 144 145 146 147

				virt++;
				phys++;
			}

148
			kunmap_atomic(pi);
149
		}
150

151
		bip->bip_flags |= BIP_MAPPED_INTEGRITY;
152 153 154 155 156 157 158 159 160
	}
}

/*
 * Remap physical sector values in the reference tag to the virtual
 * values expected by the block layer.
 */
void sd_dif_complete(struct scsi_cmnd *scmd, unsigned int good_bytes)
{
161
	const int tuple_sz = sizeof(struct t10_pi_tuple);
162 163
	struct scsi_disk *sdkp;
	struct bio *bio;
164
	struct t10_pi_tuple *pi;
165
	unsigned int j, intervals;
166 167 168 169 170 171 172
	u32 phys, virt;

	sdkp = scsi_disk(scmd->request->rq_disk);

	if (sdkp->protection_type == SD_DIF_TYPE3_PROTECTION || good_bytes == 0)
		return;

173 174
	intervals = good_bytes / scsi_prot_interval(scmd);
	phys = scsi_prot_ref_tag(scmd);
175 176

	__rq_for_each_bio(bio, scmd->request) {
177
		struct bio_integrity_payload *bip = bio_integrity(bio);
178 179
		struct bio_vec iv;
		struct bvec_iter iter;
180

181
		virt = bip_get_seed(bip) & 0xffffffff;
182

183
		bip_for_each_vec(iv, bip, iter) {
184
			pi = kmap_atomic(iv.bv_page) + iv.bv_offset;
185

186
			for (j = 0; j < iv.bv_len; j += tuple_sz, pi++) {
187

188
				if (intervals == 0) {
189
					kunmap_atomic(pi);
190 191 192
					return;
				}

193 194
				if (be32_to_cpu(pi->ref_tag) == phys)
					pi->ref_tag = cpu_to_be32(virt);
195 196 197

				virt++;
				phys++;
198
				intervals--;
199 200
			}

201
			kunmap_atomic(pi);
202 203 204 205
		}
	}
}