2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
36 #include <linux/highmem.h>
37 #include <linux/scatterlist.h>
39 #include "iscsi_iser.h"
41 void iser_reg_comp(struct ib_cq *cq, struct ib_wc *wc)
43 iser_err_comp(wc, "memreg");
46 static struct iser_fr_desc *iser_reg_desc_get_fr(struct ib_conn *ib_conn)
48 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
49 struct iser_fr_desc *desc;
52 spin_lock_irqsave(&fr_pool->lock, flags);
53 desc = list_first_entry(&fr_pool->list,
54 struct iser_fr_desc, list);
55 list_del(&desc->list);
56 spin_unlock_irqrestore(&fr_pool->lock, flags);
61 static void iser_reg_desc_put_fr(struct ib_conn *ib_conn,
62 struct iser_fr_desc *desc)
64 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
67 spin_lock_irqsave(&fr_pool->lock, flags);
68 list_add(&desc->list, &fr_pool->list);
69 spin_unlock_irqrestore(&fr_pool->lock, flags);
72 int iser_dma_map_task_data(struct iscsi_iser_task *iser_task,
73 enum iser_data_dir iser_dir,
74 enum dma_data_direction dma_dir)
76 struct iser_data_buf *data = &iser_task->data[iser_dir];
77 struct ib_device *dev;
79 iser_task->dir[iser_dir] = 1;
80 dev = iser_task->iser_conn->ib_conn.device->ib_device;
82 data->dma_nents = ib_dma_map_sg(dev, data->sg, data->size, dma_dir);
83 if (unlikely(data->dma_nents == 0)) {
84 iser_err("dma_map_sg failed!!!\n");
88 if (scsi_prot_sg_count(iser_task->sc)) {
89 struct iser_data_buf *pdata = &iser_task->prot[iser_dir];
91 pdata->dma_nents = ib_dma_map_sg(dev, pdata->sg, pdata->size, dma_dir);
92 if (unlikely(pdata->dma_nents == 0)) {
93 iser_err("protection dma_map_sg failed!!!\n");
101 ib_dma_unmap_sg(dev, data->sg, data->size, dma_dir);
106 void iser_dma_unmap_task_data(struct iscsi_iser_task *iser_task,
107 enum iser_data_dir iser_dir,
108 enum dma_data_direction dma_dir)
110 struct iser_data_buf *data = &iser_task->data[iser_dir];
111 struct ib_device *dev;
113 dev = iser_task->iser_conn->ib_conn.device->ib_device;
114 ib_dma_unmap_sg(dev, data->sg, data->size, dma_dir);
116 if (scsi_prot_sg_count(iser_task->sc)) {
117 struct iser_data_buf *pdata = &iser_task->prot[iser_dir];
119 ib_dma_unmap_sg(dev, pdata->sg, pdata->size, dma_dir);
123 static int iser_reg_dma(struct iser_device *device, struct iser_data_buf *mem,
124 struct iser_mem_reg *reg)
126 struct scatterlist *sg = mem->sg;
128 reg->sge.lkey = device->pd->local_dma_lkey;
130 * FIXME: rework the registration code path to differentiate
131 * rkey/lkey use cases
134 if (device->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
135 reg->rkey = device->pd->unsafe_global_rkey;
138 reg->sge.addr = sg_dma_address(&sg[0]);
139 reg->sge.length = sg_dma_len(&sg[0]);
141 iser_dbg("Single DMA entry: lkey=0x%x, rkey=0x%x, addr=0x%llx,"
142 " length=0x%x\n", reg->sge.lkey, reg->rkey,
143 reg->sge.addr, reg->sge.length);
148 void iser_unreg_mem_fastreg(struct iscsi_iser_task *iser_task,
149 enum iser_data_dir cmd_dir)
151 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
152 struct iser_fr_desc *desc;
153 struct ib_mr_status mr_status;
160 * The signature MR cannot be invalidated and reused without checking.
161 * libiscsi calls the check_protection transport handler only if
162 * SCSI-Response is received. And the signature MR is not checked if
163 * the task is completed for some other reason like a timeout or error
164 * handling. That's why we must check the signature MR here before
165 * putting it to the free pool.
167 if (unlikely(desc->sig_protected)) {
168 desc->sig_protected = false;
169 ib_check_mr_status(desc->rsc.sig_mr, IB_MR_CHECK_SIG_STATUS,
172 iser_reg_desc_put_fr(&iser_task->iser_conn->ib_conn, reg->desc);
176 static void iser_set_dif_domain(struct scsi_cmnd *sc,
177 struct ib_sig_domain *domain)
179 domain->sig_type = IB_SIG_TYPE_T10_DIF;
180 domain->sig.dif.pi_interval = scsi_prot_interval(sc);
181 domain->sig.dif.ref_tag = t10_pi_ref_tag(scsi_cmd_to_rq(sc));
183 * At the moment we hard code those, but in the future
184 * we will take them from sc.
186 domain->sig.dif.apptag_check_mask = 0xffff;
187 domain->sig.dif.app_escape = true;
188 domain->sig.dif.ref_escape = true;
189 if (sc->prot_flags & SCSI_PROT_REF_INCREMENT)
190 domain->sig.dif.ref_remap = true;
193 static int iser_set_sig_attrs(struct scsi_cmnd *sc,
194 struct ib_sig_attrs *sig_attrs)
196 switch (scsi_get_prot_op(sc)) {
197 case SCSI_PROT_WRITE_INSERT:
198 case SCSI_PROT_READ_STRIP:
199 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
200 iser_set_dif_domain(sc, &sig_attrs->wire);
201 sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
203 case SCSI_PROT_READ_INSERT:
204 case SCSI_PROT_WRITE_STRIP:
205 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
206 iser_set_dif_domain(sc, &sig_attrs->mem);
207 sig_attrs->mem.sig.dif.bg_type = sc->prot_flags & SCSI_PROT_IP_CHECKSUM ?
208 IB_T10DIF_CSUM : IB_T10DIF_CRC;
210 case SCSI_PROT_READ_PASS:
211 case SCSI_PROT_WRITE_PASS:
212 iser_set_dif_domain(sc, &sig_attrs->wire);
213 sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
214 iser_set_dif_domain(sc, &sig_attrs->mem);
215 sig_attrs->mem.sig.dif.bg_type = sc->prot_flags & SCSI_PROT_IP_CHECKSUM ?
216 IB_T10DIF_CSUM : IB_T10DIF_CRC;
219 iser_err("Unsupported PI operation %d\n",
220 scsi_get_prot_op(sc));
227 static inline void iser_set_prot_checks(struct scsi_cmnd *sc, u8 *mask)
230 if (sc->prot_flags & SCSI_PROT_REF_CHECK)
231 *mask |= IB_SIG_CHECK_REFTAG;
232 if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
233 *mask |= IB_SIG_CHECK_GUARD;
236 static inline void iser_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr,
237 struct ib_cqe *cqe, struct ib_send_wr *next_wr)
239 inv_wr->opcode = IB_WR_LOCAL_INV;
240 inv_wr->wr_cqe = cqe;
241 inv_wr->ex.invalidate_rkey = mr->rkey;
242 inv_wr->send_flags = 0;
244 inv_wr->next = next_wr;
247 static int iser_reg_sig_mr(struct iscsi_iser_task *iser_task,
248 struct iser_data_buf *mem,
249 struct iser_data_buf *sig_mem,
250 struct iser_reg_resources *rsc,
251 struct iser_mem_reg *sig_reg)
253 struct iser_tx_desc *tx_desc = &iser_task->desc;
254 struct ib_cqe *cqe = &iser_task->iser_conn->ib_conn.reg_cqe;
255 struct ib_mr *mr = rsc->sig_mr;
256 struct ib_sig_attrs *sig_attrs = mr->sig_attrs;
257 struct ib_reg_wr *wr = &tx_desc->reg_wr;
260 memset(sig_attrs, 0, sizeof(*sig_attrs));
261 ret = iser_set_sig_attrs(iser_task->sc, sig_attrs);
265 iser_set_prot_checks(iser_task->sc, &sig_attrs->check_mask);
267 if (rsc->sig_mr->need_inval)
268 iser_inv_rkey(&tx_desc->inv_wr, mr, cqe, &wr->wr);
270 ib_update_fast_reg_key(mr, ib_inc_rkey(mr->rkey));
272 ret = ib_map_mr_sg_pi(mr, mem->sg, mem->dma_nents, NULL,
273 sig_mem->sg, sig_mem->dma_nents, NULL, SZ_4K);
275 iser_err("failed to map PI sg (%d)\n",
276 mem->dma_nents + sig_mem->dma_nents);
280 memset(wr, 0, sizeof(*wr));
281 wr->wr.next = &tx_desc->send_wr;
282 wr->wr.opcode = IB_WR_REG_MR_INTEGRITY;
285 wr->wr.send_flags = 0;
288 wr->access = IB_ACCESS_LOCAL_WRITE |
289 IB_ACCESS_REMOTE_READ |
290 IB_ACCESS_REMOTE_WRITE;
291 rsc->sig_mr->need_inval = true;
293 sig_reg->sge.lkey = mr->lkey;
294 sig_reg->rkey = mr->rkey;
295 sig_reg->sge.addr = mr->iova;
296 sig_reg->sge.length = mr->length;
298 iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=%u\n",
299 sig_reg->sge.lkey, sig_reg->rkey, sig_reg->sge.addr,
300 sig_reg->sge.length);
305 static int iser_fast_reg_mr(struct iscsi_iser_task *iser_task,
306 struct iser_data_buf *mem,
307 struct iser_reg_resources *rsc,
308 struct iser_mem_reg *reg)
310 struct iser_tx_desc *tx_desc = &iser_task->desc;
311 struct ib_cqe *cqe = &iser_task->iser_conn->ib_conn.reg_cqe;
312 struct ib_mr *mr = rsc->mr;
313 struct ib_reg_wr *wr = &tx_desc->reg_wr;
316 if (rsc->mr->need_inval)
317 iser_inv_rkey(&tx_desc->inv_wr, mr, cqe, &wr->wr);
319 ib_update_fast_reg_key(mr, ib_inc_rkey(mr->rkey));
321 n = ib_map_mr_sg(mr, mem->sg, mem->dma_nents, NULL, SZ_4K);
322 if (unlikely(n != mem->dma_nents)) {
323 iser_err("failed to map sg (%d/%d)\n",
325 return n < 0 ? n : -EINVAL;
328 wr->wr.next = &tx_desc->send_wr;
329 wr->wr.opcode = IB_WR_REG_MR;
331 wr->wr.send_flags = 0;
335 wr->access = IB_ACCESS_LOCAL_WRITE |
336 IB_ACCESS_REMOTE_WRITE |
337 IB_ACCESS_REMOTE_READ;
339 rsc->mr->need_inval = true;
341 reg->sge.lkey = mr->lkey;
342 reg->rkey = mr->rkey;
343 reg->sge.addr = mr->iova;
344 reg->sge.length = mr->length;
346 iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=0x%x\n",
347 reg->sge.lkey, reg->rkey, reg->sge.addr, reg->sge.length);
352 int iser_reg_mem_fastreg(struct iscsi_iser_task *task,
353 enum iser_data_dir dir,
356 struct ib_conn *ib_conn = &task->iser_conn->ib_conn;
357 struct iser_device *device = ib_conn->device;
358 struct iser_data_buf *mem = &task->data[dir];
359 struct iser_mem_reg *reg = &task->rdma_reg[dir];
360 struct iser_fr_desc *desc;
364 use_dma_key = mem->dma_nents == 1 && (all_imm || !iser_always_reg) &&
365 scsi_get_prot_op(task->sc) == SCSI_PROT_NORMAL;
367 return iser_reg_dma(device, mem, reg);
369 desc = iser_reg_desc_get_fr(ib_conn);
370 if (scsi_get_prot_op(task->sc) == SCSI_PROT_NORMAL) {
371 err = iser_fast_reg_mr(task, mem, &desc->rsc, reg);
375 err = iser_reg_sig_mr(task, mem, &task->prot[dir],
380 desc->sig_protected = true;
388 iser_reg_desc_put_fr(ib_conn, desc);