blob: bcec0a4c9c30c701f87747163cad864fd515dea2 [file] [log] [blame]
Or Gerlitz6461f642006-05-11 10:03:08 +03001/*
2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
Or Gerlitz3ee07d22014-04-01 16:28:41 +03003 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
Or Gerlitz6461f642006-05-11 10:03:08 +03004 *
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:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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
31 * SOFTWARE.
Or Gerlitz6461f642006-05-11 10:03:08 +030032 */
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/slab.h>
36#include <linux/mm.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040037#include <linux/highmem.h>
Or Gerlitz6461f642006-05-11 10:03:08 +030038#include <linux/scatterlist.h>
39
40#include "iscsi_iser.h"
Sagi Grimberg32467c42015-08-06 18:33:02 +030041static
42int iser_fast_reg_fmr(struct iscsi_iser_task *iser_task,
43 struct iser_data_buf *mem,
44 struct iser_reg_resources *rsc,
45 struct iser_mem_reg *mem_reg);
46static
47int iser_fast_reg_mr(struct iscsi_iser_task *iser_task,
48 struct iser_data_buf *mem,
49 struct iser_reg_resources *rsc,
50 struct iser_mem_reg *mem_reg);
Or Gerlitz6461f642006-05-11 10:03:08 +030051
Julia Lawall2392a4c2015-11-28 16:52:04 +010052static const struct iser_reg_ops fastreg_ops = {
Sagi Grimberg48afbff2015-08-06 18:32:56 +030053 .alloc_reg_res = iser_alloc_fastreg_pool,
54 .free_reg_res = iser_free_fastreg_pool,
Sagi Grimberg32467c42015-08-06 18:33:02 +030055 .reg_mem = iser_fast_reg_mr,
56 .unreg_mem = iser_unreg_mem_fastreg,
Sagi Grimberg81722902015-08-06 18:33:01 +030057 .reg_desc_get = iser_reg_desc_get_fr,
58 .reg_desc_put = iser_reg_desc_put_fr,
Sagi Grimberg48afbff2015-08-06 18:32:56 +030059};
60
Julia Lawall2392a4c2015-11-28 16:52:04 +010061static const struct iser_reg_ops fmr_ops = {
Sagi Grimberg48afbff2015-08-06 18:32:56 +030062 .alloc_reg_res = iser_alloc_fmr_pool,
63 .free_reg_res = iser_free_fmr_pool,
Sagi Grimberg32467c42015-08-06 18:33:02 +030064 .reg_mem = iser_fast_reg_fmr,
65 .unreg_mem = iser_unreg_mem_fmr,
Sagi Grimberg81722902015-08-06 18:33:01 +030066 .reg_desc_get = iser_reg_desc_get_fmr,
67 .reg_desc_put = iser_reg_desc_put_fmr,
Sagi Grimberg48afbff2015-08-06 18:32:56 +030068};
69
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -080070void iser_reg_comp(struct ib_cq *cq, struct ib_wc *wc)
71{
72 iser_err_comp(wc, "memreg");
73}
74
Sagi Grimberg48afbff2015-08-06 18:32:56 +030075int iser_assign_reg_ops(struct iser_device *device)
76{
Or Gerlitz4a061b22015-12-18 10:59:46 +020077 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg48afbff2015-08-06 18:32:56 +030078
79 /* Assign function handles - based on FMR support */
Or Gerlitz4a061b22015-12-18 10:59:46 +020080 if (ib_dev->alloc_fmr && ib_dev->dealloc_fmr &&
81 ib_dev->map_phys_fmr && ib_dev->unmap_fmr) {
Sagi Grimberg48afbff2015-08-06 18:32:56 +030082 iser_info("FMR supported, using FMR for registration\n");
83 device->reg_ops = &fmr_ops;
Or Gerlitz4a061b22015-12-18 10:59:46 +020084 } else if (ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
Sagi Grimberg48afbff2015-08-06 18:32:56 +030085 iser_info("FastReg supported, using FastReg for registration\n");
86 device->reg_ops = &fastreg_ops;
87 } else {
88 iser_err("IB device does not support FMRs nor FastRegs, can't register memory\n");
89 return -1;
90 }
91
92 return 0;
93}
94
Sagi Grimberg5190cc22015-08-06 18:32:54 +030095struct iser_fr_desc *
Sagi Grimberg81722902015-08-06 18:33:01 +030096iser_reg_desc_get_fr(struct ib_conn *ib_conn)
Sagi Grimbergbd8b9442015-04-14 18:08:21 +030097{
Sagi Grimberg385ad872015-08-06 18:32:58 +030098 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
Sagi Grimberg5190cc22015-08-06 18:32:54 +030099 struct iser_fr_desc *desc;
Sagi Grimbergbd8b9442015-04-14 18:08:21 +0300100 unsigned long flags;
101
Sagi Grimberg385ad872015-08-06 18:32:58 +0300102 spin_lock_irqsave(&fr_pool->lock, flags);
Adir Lev2b3bf952015-08-06 18:32:59 +0300103 desc = list_first_entry(&fr_pool->list,
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300104 struct iser_fr_desc, list);
Sagi Grimbergbd8b9442015-04-14 18:08:21 +0300105 list_del(&desc->list);
Sagi Grimberg385ad872015-08-06 18:32:58 +0300106 spin_unlock_irqrestore(&fr_pool->lock, flags);
Sagi Grimbergbd8b9442015-04-14 18:08:21 +0300107
108 return desc;
109}
110
111void
Sagi Grimberg81722902015-08-06 18:33:01 +0300112iser_reg_desc_put_fr(struct ib_conn *ib_conn,
113 struct iser_fr_desc *desc)
Sagi Grimbergbd8b9442015-04-14 18:08:21 +0300114{
Sagi Grimberg385ad872015-08-06 18:32:58 +0300115 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
Sagi Grimbergbd8b9442015-04-14 18:08:21 +0300116 unsigned long flags;
117
Sagi Grimberg385ad872015-08-06 18:32:58 +0300118 spin_lock_irqsave(&fr_pool->lock, flags);
Adir Lev2b3bf952015-08-06 18:32:59 +0300119 list_add(&desc->list, &fr_pool->list);
Sagi Grimberg385ad872015-08-06 18:32:58 +0300120 spin_unlock_irqrestore(&fr_pool->lock, flags);
Sagi Grimbergbd8b9442015-04-14 18:08:21 +0300121}
122
Sagi Grimberg81722902015-08-06 18:33:01 +0300123struct iser_fr_desc *
124iser_reg_desc_get_fmr(struct ib_conn *ib_conn)
125{
126 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
127
128 return list_first_entry(&fr_pool->list,
129 struct iser_fr_desc, list);
130}
131
132void
133iser_reg_desc_put_fmr(struct ib_conn *ib_conn,
134 struct iser_fr_desc *desc)
135{
136}
137
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800138#define IS_4K_ALIGNED(addr) ((((unsigned long)addr) & ~MASK_4K) == 0)
139
Or Gerlitz6461f642006-05-11 10:03:08 +0300140/**
141 * iser_sg_to_page_vec - Translates scatterlist entries to physical addresses
142 * and returns the length of resulting physical address array (may be less than
143 * the original due to possible compaction).
144 *
145 * we build a "page vec" under the assumption that the SG meets the RDMA
146 * alignment requirements. Other then the first and last SG elements, all
147 * the "internal" elements can be compacted into a list whose elements are
148 * dma addresses of physical pages. The code supports also the weird case
149 * where --few fragments of the same page-- are present in the SG as
150 * consecutive elements. Also, it handles one entry SG.
151 */
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800152
Or Gerlitz6461f642006-05-11 10:03:08 +0300153static int iser_sg_to_page_vec(struct iser_data_buf *data,
Sagi Grimberg919fc272013-07-28 12:35:40 +0300154 struct ib_device *ibdev, u64 *pages,
155 int *offset, int *data_size)
Or Gerlitz6461f642006-05-11 10:03:08 +0300156{
Sagi Grimberge3784bd2015-04-14 18:08:15 +0300157 struct scatterlist *sg, *sgl = data->sg;
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800158 u64 start_addr, end_addr, page, chunk_start = 0;
Or Gerlitz6461f642006-05-11 10:03:08 +0300159 unsigned long total_sz = 0;
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800160 unsigned int dma_len;
161 int i, new_chunk, cur_page, last_ent = data->dma_nents - 1;
Or Gerlitz6461f642006-05-11 10:03:08 +0300162
163 /* compute the offset of first element */
Sagi Grimberg919fc272013-07-28 12:35:40 +0300164 *offset = (u64) sgl[0].offset & ~MASK_4K;
Or Gerlitz6461f642006-05-11 10:03:08 +0300165
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800166 new_chunk = 1;
167 cur_page = 0;
Jens Axboe53d412f2007-07-24 14:41:13 +0200168 for_each_sg(sgl, sg, data->dma_nents, i) {
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800169 start_addr = ib_sg_dma_address(ibdev, sg);
170 if (new_chunk)
171 chunk_start = start_addr;
172 dma_len = ib_sg_dma_len(ibdev, sg);
173 end_addr = start_addr + dma_len;
Ralph Campbell51803112006-12-12 14:31:00 -0800174 total_sz += dma_len;
175
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800176 /* collect page fragments until aligned or end of SG list */
177 if (!IS_4K_ALIGNED(end_addr) && i < last_ent) {
178 new_chunk = 0;
179 continue;
Or Gerlitz6461f642006-05-11 10:03:08 +0300180 }
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800181 new_chunk = 1;
Or Gerlitz6461f642006-05-11 10:03:08 +0300182
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800183 /* address of the first page in the contiguous chunk;
184 masking relevant for the very first SG entry,
185 which might be unaligned */
186 page = chunk_start & MASK_4K;
187 do {
Sagi Grimberg919fc272013-07-28 12:35:40 +0300188 pages[cur_page++] = page;
Erez Zilber8dfa0872006-09-11 12:22:30 +0300189 page += SIZE_4K;
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800190 } while (page < end_addr);
Or Gerlitz6461f642006-05-11 10:03:08 +0300191 }
Or Gerlitzc1ccaf22009-11-12 11:32:27 -0800192
Sagi Grimberg919fc272013-07-28 12:35:40 +0300193 *data_size = total_sz;
194 iser_dbg("page_vec->data_size:%d cur_page %d\n",
195 *data_size, cur_page);
Or Gerlitz6461f642006-05-11 10:03:08 +0300196 return cur_page;
197}
198
Ralph Campbell51803112006-12-12 14:31:00 -0800199static void iser_data_buf_dump(struct iser_data_buf *data,
200 struct ib_device *ibdev)
Or Gerlitz6461f642006-05-11 10:03:08 +0300201{
Jens Axboe53d412f2007-07-24 14:41:13 +0200202 struct scatterlist *sg;
Or Gerlitz6461f642006-05-11 10:03:08 +0300203 int i;
204
Sagi Grimberge3784bd2015-04-14 18:08:15 +0300205 for_each_sg(data->sg, sg, data->dma_nents, i)
Or Gerlitzf91424c2013-07-28 12:35:36 +0300206 iser_dbg("sg[%d] dma_addr:0x%lX page:0x%p "
Erez Zilbere981f1d2006-09-11 12:24:00 +0300207 "off:0x%x sz:0x%x dma_len:0x%x\n",
Jens Axboe53d412f2007-07-24 14:41:13 +0200208 i, (unsigned long)ib_sg_dma_address(ibdev, sg),
Jens Axboe45711f12007-10-22 21:19:53 +0200209 sg_page(sg), sg->offset,
Jens Axboe53d412f2007-07-24 14:41:13 +0200210 sg->length, ib_sg_dma_len(ibdev, sg));
Or Gerlitz6461f642006-05-11 10:03:08 +0300211}
212
213static void iser_dump_page_vec(struct iser_page_vec *page_vec)
214{
215 int i;
216
217 iser_err("page vec length %d data size %d\n",
218 page_vec->length, page_vec->data_size);
219 for (i = 0; i < page_vec->length; i++)
220 iser_err("%d %lx\n",i,(unsigned long)page_vec->pages[i]);
221}
222
Mike Christie2261ec32008-05-21 15:54:11 -0500223int iser_dma_map_task_data(struct iscsi_iser_task *iser_task,
224 struct iser_data_buf *data,
225 enum iser_data_dir iser_dir,
226 enum dma_data_direction dma_dir)
Erez Zilber74a20782006-09-27 16:43:06 +0300227{
Ralph Campbell51803112006-12-12 14:31:00 -0800228 struct ib_device *dev;
Erez Zilber74a20782006-09-27 16:43:06 +0300229
Mike Christie2261ec32008-05-21 15:54:11 -0500230 iser_task->dir[iser_dir] = 1;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300231 dev = iser_task->iser_conn->ib_conn.device->ib_device;
Erez Zilber74a20782006-09-27 16:43:06 +0300232
Sagi Grimberge3784bd2015-04-14 18:08:15 +0300233 data->dma_nents = ib_dma_map_sg(dev, data->sg, data->size, dma_dir);
Erez Zilber74a20782006-09-27 16:43:06 +0300234 if (data->dma_nents == 0) {
235 iser_err("dma_map_sg failed!!!\n");
236 return -EINVAL;
237 }
238 return 0;
239}
240
Sagi Grimberg9a8b08f2014-03-05 19:43:44 +0200241void iser_dma_unmap_task_data(struct iscsi_iser_task *iser_task,
Roi Dayanc6c95ef2014-12-28 14:26:11 +0200242 struct iser_data_buf *data,
243 enum dma_data_direction dir)
Erez Zilber74a20782006-09-27 16:43:06 +0300244{
Ralph Campbell51803112006-12-12 14:31:00 -0800245 struct ib_device *dev;
Erez Zilber74a20782006-09-27 16:43:06 +0300246
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300247 dev = iser_task->iser_conn->ib_conn.device->ib_device;
Sagi Grimberge3784bd2015-04-14 18:08:15 +0300248 ib_dma_unmap_sg(dev, data->sg, data->size, dir);
Erez Zilber74a20782006-09-27 16:43:06 +0300249}
250
Sagi Grimbergad1e5672015-04-14 18:08:26 +0300251static int
252iser_reg_dma(struct iser_device *device, struct iser_data_buf *mem,
253 struct iser_mem_reg *reg)
254{
255 struct scatterlist *sg = mem->sg;
256
Jason Gunthorpe256b7ad2015-07-30 17:22:20 -0600257 reg->sge.lkey = device->pd->local_dma_lkey;
Sagi Grimbergad1e5672015-04-14 18:08:26 +0300258 reg->rkey = device->mr->rkey;
259 reg->sge.addr = ib_sg_dma_address(device->ib_device, &sg[0]);
260 reg->sge.length = ib_sg_dma_len(device->ib_device, &sg[0]);
261
262 iser_dbg("Single DMA entry: lkey=0x%x, rkey=0x%x, addr=0x%llx,"
263 " length=0x%x\n", reg->sge.lkey, reg->rkey,
264 reg->sge.addr, reg->sge.length);
265
266 return 0;
267}
268
Or Gerlitz6461f642006-05-11 10:03:08 +0300269/**
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300270 * iser_reg_page_vec - Register physical memory
271 *
272 * returns: 0 on success, errno code on failure
273 */
274static
Sagi Grimberg7d0483c2015-08-06 18:33:00 +0300275int iser_fast_reg_fmr(struct iscsi_iser_task *iser_task,
Sagi Grimbergf0e35c22015-04-14 18:08:20 +0300276 struct iser_data_buf *mem,
Adir Lev2b3bf952015-08-06 18:32:59 +0300277 struct iser_reg_resources *rsc,
Sagi Grimberg7d0483c2015-08-06 18:33:00 +0300278 struct iser_mem_reg *reg)
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300279{
Sagi Grimbergf0e35c22015-04-14 18:08:20 +0300280 struct ib_conn *ib_conn = &iser_task->iser_conn->ib_conn;
281 struct iser_device *device = ib_conn->device;
Adir Lev2b3bf952015-08-06 18:32:59 +0300282 struct iser_page_vec *page_vec = rsc->page_vec;
283 struct ib_fmr_pool *fmr_pool = rsc->fmr_pool;
Sagi Grimbergf0e35c22015-04-14 18:08:20 +0300284 struct ib_pool_fmr *fmr;
285 int ret, plen;
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300286
Sagi Grimbergf0e35c22015-04-14 18:08:20 +0300287 plen = iser_sg_to_page_vec(mem, device->ib_device,
288 page_vec->pages,
289 &page_vec->offset,
290 &page_vec->data_size);
291 page_vec->length = plen;
292 if (plen * SIZE_4K < page_vec->data_size) {
293 iser_err("page vec too short to hold this SG\n");
294 iser_data_buf_dump(mem, device->ib_device);
295 iser_dump_page_vec(page_vec);
296 return -EINVAL;
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300297 }
298
Adir Lev2b3bf952015-08-06 18:32:59 +0300299 fmr = ib_fmr_pool_map_phys(fmr_pool,
Sagi Grimbergf0e35c22015-04-14 18:08:20 +0300300 page_vec->pages,
301 page_vec->length,
302 page_vec->pages[0]);
303 if (IS_ERR(fmr)) {
304 ret = PTR_ERR(fmr);
305 iser_err("ib_fmr_pool_map_phys failed: %d\n", ret);
306 return ret;
307 }
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300308
Sagi Grimberg7d0483c2015-08-06 18:33:00 +0300309 reg->sge.lkey = fmr->fmr->lkey;
310 reg->rkey = fmr->fmr->rkey;
311 reg->sge.addr = page_vec->pages[0] + page_vec->offset;
312 reg->sge.length = page_vec->data_size;
313 reg->mem_h = fmr;
Sagi Grimbergf0e35c22015-04-14 18:08:20 +0300314
Sagi Grimberg1b16c982015-08-06 18:33:05 +0300315 iser_dbg("fmr reg: lkey=0x%x, rkey=0x%x, addr=0x%llx,"
316 " length=0x%x\n", reg->sge.lkey, reg->rkey,
317 reg->sge.addr, reg->sge.length);
318
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300319 return 0;
320}
321
322/**
323 * Unregister (previosuly registered using FMR) memory.
324 * If memory is non-FMR does nothing.
325 */
326void iser_unreg_mem_fmr(struct iscsi_iser_task *iser_task,
327 enum iser_data_dir cmd_dir)
328{
Sagi Grimbergb130ede2015-04-14 18:08:19 +0300329 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300330 int ret;
331
332 if (!reg->mem_h)
333 return;
334
335 iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n", reg->mem_h);
336
337 ret = ib_fmr_pool_unmap((struct ib_pool_fmr *)reg->mem_h);
338 if (ret)
339 iser_err("ib_fmr_pool_unmap failed %d\n", ret);
340
341 reg->mem_h = NULL;
342}
343
344void iser_unreg_mem_fastreg(struct iscsi_iser_task *iser_task,
345 enum iser_data_dir cmd_dir)
346{
Sagi Grimberg81722902015-08-06 18:33:01 +0300347 struct iser_device *device = iser_task->iser_conn->ib_conn.device;
Sagi Grimbergb130ede2015-04-14 18:08:19 +0300348 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300349
Sagi Grimbergbd8b9442015-04-14 18:08:21 +0300350 if (!reg->mem_h)
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300351 return;
352
Sagi Grimberg81722902015-08-06 18:33:01 +0300353 device->reg_ops->reg_desc_put(&iser_task->iser_conn->ib_conn,
354 reg->mem_h);
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300355 reg->mem_h = NULL;
Sagi Grimbergd03e61d2015-04-14 18:08:17 +0300356}
357
Sagi Grimberg5bb6e542014-12-07 16:10:06 +0200358static void
Sagi Grimberg92792c02014-08-13 19:54:33 +0300359iser_set_dif_domain(struct scsi_cmnd *sc, struct ib_sig_attrs *sig_attrs,
360 struct ib_sig_domain *domain)
361{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +0300362 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg5bb6e542014-12-07 16:10:06 +0200363 domain->sig.dif.pi_interval = scsi_prot_interval(sc);
364 domain->sig.dif.ref_tag = scsi_prot_ref_tag(sc);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +0300365 /*
366 * At the moment we hard code those, but in the future
367 * we will take them from sc.
368 */
369 domain->sig.dif.apptag_check_mask = 0xffff;
370 domain->sig.dif.app_escape = true;
371 domain->sig.dif.ref_escape = true;
Sagi Grimberg5bb6e542014-12-07 16:10:06 +0200372 if (sc->prot_flags & SCSI_PROT_REF_INCREMENT)
Sagi Grimberg78eda2b2014-08-13 19:54:35 +0300373 domain->sig.dif.ref_remap = true;
Sagi Grimberg92792c02014-08-13 19:54:33 +0300374};
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200375
376static int
377iser_set_sig_attrs(struct scsi_cmnd *sc, struct ib_sig_attrs *sig_attrs)
378{
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200379 switch (scsi_get_prot_op(sc)) {
380 case SCSI_PROT_WRITE_INSERT:
381 case SCSI_PROT_READ_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +0300382 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg92792c02014-08-13 19:54:33 +0300383 iser_set_dif_domain(sc, sig_attrs, &sig_attrs->wire);
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200384 sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200385 break;
386 case SCSI_PROT_READ_INSERT:
387 case SCSI_PROT_WRITE_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +0300388 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg92792c02014-08-13 19:54:33 +0300389 iser_set_dif_domain(sc, sig_attrs, &sig_attrs->mem);
Sagi Grimberg5bb6e542014-12-07 16:10:06 +0200390 sig_attrs->mem.sig.dif.bg_type = sc->prot_flags & SCSI_PROT_IP_CHECKSUM ?
391 IB_T10DIF_CSUM : IB_T10DIF_CRC;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200392 break;
393 case SCSI_PROT_READ_PASS:
394 case SCSI_PROT_WRITE_PASS:
Sagi Grimberg92792c02014-08-13 19:54:33 +0300395 iser_set_dif_domain(sc, sig_attrs, &sig_attrs->wire);
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200396 sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
Sagi Grimberg92792c02014-08-13 19:54:33 +0300397 iser_set_dif_domain(sc, sig_attrs, &sig_attrs->mem);
Sagi Grimberg5bb6e542014-12-07 16:10:06 +0200398 sig_attrs->mem.sig.dif.bg_type = sc->prot_flags & SCSI_PROT_IP_CHECKSUM ?
399 IB_T10DIF_CSUM : IB_T10DIF_CRC;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200400 break;
401 default:
402 iser_err("Unsupported PI operation %d\n",
403 scsi_get_prot_op(sc));
404 return -EINVAL;
405 }
Sagi Grimberg78eda2b2014-08-13 19:54:35 +0300406
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200407 return 0;
408}
409
Sagi Grimberg5bb6e542014-12-07 16:10:06 +0200410static inline void
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200411iser_set_prot_checks(struct scsi_cmnd *sc, u8 *mask)
412{
Sagi Grimberg5bb6e542014-12-07 16:10:06 +0200413 *mask = 0;
414 if (sc->prot_flags & SCSI_PROT_REF_CHECK)
415 *mask |= ISER_CHECK_REFTAG;
416 if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
417 *mask |= ISER_CHECK_GUARD;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200418}
419
Sagi Grimberga11b3e62014-12-07 16:10:01 +0200420static void
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800421iser_inv_rkey(struct ib_send_wr *inv_wr,
422 struct ib_mr *mr,
423 struct ib_cqe *cqe)
Sagi Grimberga11b3e62014-12-07 16:10:01 +0200424{
425 u32 rkey;
426
Sagi Grimberga11b3e62014-12-07 16:10:01 +0200427 inv_wr->opcode = IB_WR_LOCAL_INV;
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800428 inv_wr->wr_cqe = cqe;
Sagi Grimberga11b3e62014-12-07 16:10:01 +0200429 inv_wr->ex.invalidate_rkey = mr->rkey;
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300430 inv_wr->send_flags = 0;
431 inv_wr->num_sge = 0;
Sagi Grimberga11b3e62014-12-07 16:10:01 +0200432
433 rkey = ib_inc_rkey(mr->rkey);
434 ib_update_fast_reg_key(mr, rkey);
435}
436
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200437static int
438iser_reg_sig_mr(struct iscsi_iser_task *iser_task,
Sagi Grimbergd711d812015-08-06 18:32:53 +0300439 struct iser_pi_context *pi_ctx,
Sagi Grimberg6ef8bb82015-04-14 18:08:25 +0300440 struct iser_mem_reg *data_reg,
441 struct iser_mem_reg *prot_reg,
442 struct iser_mem_reg *sig_reg)
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200443{
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300444 struct iser_tx_desc *tx_desc = &iser_task->desc;
445 struct ib_sig_attrs *sig_attrs = &tx_desc->sig_attrs;
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800446 struct ib_cqe *cqe = &iser_task->iser_conn->ib_conn.reg_cqe;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100447 struct ib_sig_handover_wr *wr;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200448 int ret;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200449
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300450 memset(sig_attrs, 0, sizeof(*sig_attrs));
451 ret = iser_set_sig_attrs(iser_task->sc, sig_attrs);
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200452 if (ret)
453 goto err;
454
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300455 iser_set_prot_checks(iser_task->sc, &sig_attrs->check_mask);
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200456
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100457 if (!pi_ctx->sig_mr_valid)
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800458 iser_inv_rkey(iser_tx_next_wr(tx_desc), pi_ctx->sig_mr, cqe);
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200459
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100460 wr = sig_handover_wr(iser_tx_next_wr(tx_desc));
461 wr->wr.opcode = IB_WR_REG_SIG_MR;
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800462 wr->wr.wr_cqe = cqe;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100463 wr->wr.sg_list = &data_reg->sge;
464 wr->wr.num_sge = 1;
465 wr->wr.send_flags = 0;
466 wr->sig_attrs = sig_attrs;
467 wr->sig_mr = pi_ctx->sig_mr;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200468 if (scsi_prot_sg_count(iser_task->sc))
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100469 wr->prot = &prot_reg->sge;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200470 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100471 wr->prot = NULL;
472 wr->access_flags = IB_ACCESS_LOCAL_WRITE |
473 IB_ACCESS_REMOTE_READ |
474 IB_ACCESS_REMOTE_WRITE;
Sagi Grimbergd711d812015-08-06 18:32:53 +0300475 pi_ctx->sig_mr_valid = 0;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200476
Sagi Grimberg6ef8bb82015-04-14 18:08:25 +0300477 sig_reg->sge.lkey = pi_ctx->sig_mr->lkey;
478 sig_reg->rkey = pi_ctx->sig_mr->rkey;
479 sig_reg->sge.addr = 0;
480 sig_reg->sge.length = scsi_transfer_length(iser_task->sc);
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200481
Sagi Grimberg39405882015-10-13 19:11:33 +0300482 iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=%u\n",
Sagi Grimberg6ef8bb82015-04-14 18:08:25 +0300483 sig_reg->sge.lkey, sig_reg->rkey, sig_reg->sge.addr,
484 sig_reg->sge.length);
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200485err:
486 return ret;
487}
488
Sagi Grimbergd11ec4e2014-03-05 19:43:40 +0200489static int iser_fast_reg_mr(struct iscsi_iser_task *iser_task,
Sagi Grimbergd11ec4e2014-03-05 19:43:40 +0200490 struct iser_data_buf *mem,
Sagi Grimbergd711d812015-08-06 18:32:53 +0300491 struct iser_reg_resources *rsc,
Sagi Grimberg6ef8bb82015-04-14 18:08:25 +0300492 struct iser_mem_reg *reg)
Sagi Grimberg55878562013-07-28 12:35:42 +0300493{
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300494 struct iser_tx_desc *tx_desc = &iser_task->desc;
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800495 struct ib_cqe *cqe = &iser_task->iser_conn->ib_conn.reg_cqe;
Sagi Grimberg39405882015-10-13 19:11:33 +0300496 struct ib_mr *mr = rsc->mr;
497 struct ib_reg_wr *wr;
498 int n;
Sagi Grimberg55878562013-07-28 12:35:42 +0300499
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100500 if (!rsc->mr_valid)
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800501 iser_inv_rkey(iser_tx_next_wr(tx_desc), mr, cqe);
Sagi Grimberg55878562013-07-28 12:35:42 +0300502
Sagi Grimberg39405882015-10-13 19:11:33 +0300503 n = ib_map_mr_sg(mr, mem->sg, mem->size, SIZE_4K);
504 if (unlikely(n != mem->size)) {
505 iser_err("failed to map sg (%d/%d)\n",
506 n, mem->size);
507 return n < 0 ? n : -EINVAL;
508 }
509
510 wr = reg_wr(iser_tx_next_wr(tx_desc));
511 wr->wr.opcode = IB_WR_REG_MR;
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800512 wr->wr.wr_cqe = cqe;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100513 wr->wr.send_flags = 0;
Sagi Grimberg39405882015-10-13 19:11:33 +0300514 wr->wr.num_sge = 0;
515 wr->mr = mr;
516 wr->key = mr->rkey;
517 wr->access = IB_ACCESS_LOCAL_WRITE |
518 IB_ACCESS_REMOTE_WRITE |
519 IB_ACCESS_REMOTE_READ;
520
Sagi Grimbergd711d812015-08-06 18:32:53 +0300521 rsc->mr_valid = 0;
Sagi Grimberg55878562013-07-28 12:35:42 +0300522
Sagi Grimberg6ef8bb82015-04-14 18:08:25 +0300523 reg->sge.lkey = mr->lkey;
524 reg->rkey = mr->rkey;
Sagi Grimberg39405882015-10-13 19:11:33 +0300525 reg->sge.addr = mr->iova;
526 reg->sge.length = mr->length;
Sagi Grimberg55878562013-07-28 12:35:42 +0300527
Sagi Grimberg39405882015-10-13 19:11:33 +0300528 iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=0x%x\n",
529 reg->sge.lkey, reg->rkey, reg->sge.addr, reg->sge.length);
Sagi Grimberg1b16c982015-08-06 18:33:05 +0300530
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300531 return 0;
Sagi Grimberg55878562013-07-28 12:35:42 +0300532}
533
Sagi Grimberg32467c42015-08-06 18:33:02 +0300534static int
Sagi Grimberg32467c42015-08-06 18:33:02 +0300535iser_reg_prot_sg(struct iscsi_iser_task *task,
536 struct iser_data_buf *mem,
537 struct iser_fr_desc *desc,
Sagi Grimberg3cffd932015-09-24 10:34:22 +0300538 bool use_dma_key,
Sagi Grimberg32467c42015-08-06 18:33:02 +0300539 struct iser_mem_reg *reg)
540{
541 struct iser_device *device = task->iser_conn->ib_conn.device;
542
Sagi Grimberg3cffd932015-09-24 10:34:22 +0300543 if (use_dma_key)
Sagi Grimberg32467c42015-08-06 18:33:02 +0300544 return iser_reg_dma(device, mem, reg);
545
546 return device->reg_ops->reg_mem(task, mem, &desc->pi_ctx->rsc, reg);
547}
548
549static int
550iser_reg_data_sg(struct iscsi_iser_task *task,
551 struct iser_data_buf *mem,
552 struct iser_fr_desc *desc,
Sagi Grimberg3cffd932015-09-24 10:34:22 +0300553 bool use_dma_key,
Sagi Grimberg32467c42015-08-06 18:33:02 +0300554 struct iser_mem_reg *reg)
555{
556 struct iser_device *device = task->iser_conn->ib_conn.device;
557
Sagi Grimberg3cffd932015-09-24 10:34:22 +0300558 if (use_dma_key)
Sagi Grimberg32467c42015-08-06 18:33:02 +0300559 return iser_reg_dma(device, mem, reg);
560
561 return device->reg_ops->reg_mem(task, mem, &desc->rsc, reg);
562}
563
564int iser_reg_rdma_mem(struct iscsi_iser_task *task,
565 enum iser_data_dir dir)
566{
567 struct ib_conn *ib_conn = &task->iser_conn->ib_conn;
568 struct iser_device *device = ib_conn->device;
569 struct iser_data_buf *mem = &task->data[dir];
570 struct iser_mem_reg *reg = &task->rdma_reg[dir];
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300571 struct iser_mem_reg *data_reg;
Sagi Grimberg32467c42015-08-06 18:33:02 +0300572 struct iser_fr_desc *desc = NULL;
Sagi Grimberg3cffd932015-09-24 10:34:22 +0300573 bool use_dma_key;
Sagi Grimberg32467c42015-08-06 18:33:02 +0300574 int err;
575
Sagi Grimberg3cffd932015-09-24 10:34:22 +0300576 use_dma_key = (mem->dma_nents == 1 && !iser_always_reg &&
577 scsi_get_prot_op(task->sc) == SCSI_PROT_NORMAL);
578
579 if (!use_dma_key) {
Sagi Grimberg81722902015-08-06 18:33:01 +0300580 desc = device->reg_ops->reg_desc_get(ib_conn);
Sagi Grimberg32467c42015-08-06 18:33:02 +0300581 reg->mem_h = desc;
Sagi Grimberg55878562013-07-28 12:35:42 +0300582 }
583
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300584 if (scsi_get_prot_op(task->sc) == SCSI_PROT_NORMAL)
585 data_reg = reg;
586 else
587 data_reg = &task->desc.data_reg;
588
Sagi Grimberg3cffd932015-09-24 10:34:22 +0300589 err = iser_reg_data_sg(task, mem, desc, use_dma_key, data_reg);
Sagi Grimberg32467c42015-08-06 18:33:02 +0300590 if (unlikely(err))
Sagi Grimbergd11ec4e2014-03-05 19:43:40 +0200591 goto err_reg;
592
Sagi Grimberg32467c42015-08-06 18:33:02 +0300593 if (scsi_get_prot_op(task->sc) != SCSI_PROT_NORMAL) {
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300594 struct iser_mem_reg *prot_reg = &task->desc.prot_reg;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200595
Sagi Grimberg32467c42015-08-06 18:33:02 +0300596 if (scsi_prot_sg_count(task->sc)) {
597 mem = &task->prot[dir];
Sagi Grimberg3cffd932015-09-24 10:34:22 +0300598 err = iser_reg_prot_sg(task, mem, desc,
599 use_dma_key, prot_reg);
Sagi Grimberg32467c42015-08-06 18:33:02 +0300600 if (unlikely(err))
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200601 goto err_reg;
602 }
603
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300604 err = iser_reg_sig_mr(task, desc->pi_ctx, data_reg,
605 prot_reg, reg);
Sagi Grimberg32467c42015-08-06 18:33:02 +0300606 if (unlikely(err))
607 goto err_reg;
608
Sagi Grimbergd711d812015-08-06 18:32:53 +0300609 desc->pi_ctx->sig_protected = 1;
Sagi Grimberg177e31bd2014-03-05 19:43:48 +0200610 }
Sagi Grimbergd11ec4e2014-03-05 19:43:40 +0200611
Sagi Grimberg55878562013-07-28 12:35:42 +0300612 return 0;
Sagi Grimberg32467c42015-08-06 18:33:02 +0300613
Sagi Grimberg55878562013-07-28 12:35:42 +0300614err_reg:
Sagi Grimbergbd8b9442015-04-14 18:08:21 +0300615 if (desc)
Sagi Grimberg81722902015-08-06 18:33:01 +0300616 device->reg_ops->reg_desc_put(ib_conn, desc);
Sagi Grimbergd11ec4e2014-03-05 19:43:40 +0200617
Sagi Grimberg55878562013-07-28 12:35:42 +0300618 return err;
619}
Sagi Grimberg32467c42015-08-06 18:33:02 +0300620
621void iser_unreg_rdma_mem(struct iscsi_iser_task *task,
622 enum iser_data_dir dir)
623{
624 struct iser_device *device = task->iser_conn->ib_conn.device;
625
626 device->reg_ops->unreg_mem(task, dir);
627}