blob: d495675ea68de0e83533804b17c08bb1ef4cbd47 [file] [log] [blame]
Steve Wisecfdda9d2010-04-21 15:30:06 -07001/*
2 * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
Vipul Pandya42b6a942013-03-14 05:09:01 +000033#include <linux/module.h>
34#include <linux/moduleparam.h>
Steve Wisecfdda9d2010-04-21 15:30:06 -070035#include <rdma/ib_umem.h>
Arun Sharma600634972011-07-26 16:09:06 -070036#include <linux/atomic.h>
Matan Barakb2a239d2016-02-29 18:05:29 +020037#include <rdma/ib_user_verbs.h>
Steve Wisecfdda9d2010-04-21 15:30:06 -070038
39#include "iw_cxgb4.h"
40
Steve Wise96bb2702014-03-27 12:03:47 -050041int use_dsgl = 0;
Vipul Pandya42b6a942013-03-14 05:09:01 +000042module_param(use_dsgl, int, 0644);
Steve Wise96bb2702014-03-27 12:03:47 -050043MODULE_PARM_DESC(use_dsgl, "Use DSGL for PBL/FastReg (default=0)");
Vipul Pandya42b6a942013-03-14 05:09:01 +000044
Steve Wisecfdda9d2010-04-21 15:30:06 -070045#define T4_ULPTX_MIN_IO 32
46#define C4IW_MAX_INLINE_SIZE 96
Vipul Pandya42b6a942013-03-14 05:09:01 +000047#define T4_ULPTX_MAX_DMA 1024
48#define C4IW_INLINE_THRESHOLD 128
Steve Wisecfdda9d2010-04-21 15:30:06 -070049
Vipul Pandya42b6a942013-03-14 05:09:01 +000050static int inline_threshold = C4IW_INLINE_THRESHOLD;
51module_param(inline_threshold, int, 0644);
52MODULE_PARM_DESC(inline_threshold, "inline vs dsgl threshold (default=128)");
53
Hariprasad Shenai2550a882014-11-21 09:36:36 -060054static int mr_exceeds_hw_limits(struct c4iw_dev *dev, u64 length)
55{
56 return (is_t4(dev->rdev.lldi.adapter_type) ||
57 is_t5(dev->rdev.lldi.adapter_type)) &&
58 length >= 8*1024*1024*1024ULL;
59}
60
Vipul Pandya42b6a942013-03-14 05:09:01 +000061static int _c4iw_write_mem_dma_aligned(struct c4iw_rdev *rdev, u32 addr,
Vipul Pandya0e5eca72013-03-14 05:09:02 +000062 u32 len, dma_addr_t data, int wait)
Vipul Pandya42b6a942013-03-14 05:09:01 +000063{
64 struct sk_buff *skb;
65 struct ulp_mem_io *req;
66 struct ulptx_sgl *sgl;
67 u8 wr_len;
68 int ret = 0;
69 struct c4iw_wr_wait wr_wait;
70
71 addr &= 0x7FFFFFF;
72
73 if (wait)
74 c4iw_init_wr_wait(&wr_wait);
75 wr_len = roundup(sizeof(*req) + sizeof(*sgl), 16);
76
Michal Hockof72f1162015-04-14 13:24:33 -070077 skb = alloc_skb(wr_len, GFP_KERNEL);
Vipul Pandya42b6a942013-03-14 05:09:01 +000078 if (!skb)
79 return -ENOMEM;
80 set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
81
82 req = (struct ulp_mem_io *)__skb_put(skb, wr_len);
83 memset(req, 0, wr_len);
84 INIT_ULPTX_WR(req, wr_len, 0, 0);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +053085 req->wr.wr_hi = cpu_to_be32(FW_WR_OP_V(FW_ULPTX_WR) |
86 (wait ? FW_WR_COMPL_F : 0));
Paul Bolle298589b2014-01-09 11:53:27 +010087 req->wr.wr_lo = wait ? (__force __be64)(unsigned long) &wr_wait : 0L;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +053088 req->wr.wr_mid = cpu_to_be32(FW_WR_LEN16_V(DIV_ROUND_UP(wr_len, 16)));
Hariprasad S92f850e2016-05-06 22:17:56 +053089 req->cmd = cpu_to_be32(ULPTX_CMD_V(ULP_TX_MEM_WRITE) |
90 T5_ULP_MEMIO_ORDER_V(1) |
91 T5_ULP_MEMIO_FID_V(rdev->lldi.rxq_ids[0]));
Anish Bhattd7990b02014-11-12 17:15:57 -080092 req->dlen = cpu_to_be32(ULP_MEMIO_DATA_LEN_V(len>>5));
Vipul Pandya42b6a942013-03-14 05:09:01 +000093 req->len16 = cpu_to_be32(DIV_ROUND_UP(wr_len-sizeof(req->wr), 16));
Anish Bhattd7990b02014-11-12 17:15:57 -080094 req->lock_addr = cpu_to_be32(ULP_MEMIO_ADDR_V(addr));
Vipul Pandya42b6a942013-03-14 05:09:01 +000095
96 sgl = (struct ulptx_sgl *)(req + 1);
Anish Bhattd7990b02014-11-12 17:15:57 -080097 sgl->cmd_nsge = cpu_to_be32(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -080098 ULPTX_NSGE_V(1));
Vipul Pandya42b6a942013-03-14 05:09:01 +000099 sgl->len0 = cpu_to_be32(len);
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000100 sgl->addr0 = cpu_to_be64(data);
Vipul Pandya42b6a942013-03-14 05:09:01 +0000101
102 ret = c4iw_ofld_send(rdev, skb);
103 if (ret)
104 return ret;
105 if (wait)
106 ret = c4iw_wait_for_reply(rdev, &wr_wait, 0, 0, __func__);
107 return ret;
108}
109
110static int _c4iw_write_mem_inline(struct c4iw_rdev *rdev, u32 addr, u32 len,
111 void *data)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700112{
113 struct sk_buff *skb;
114 struct ulp_mem_io *req;
115 struct ulptx_idata *sc;
116 u8 wr_len, *to_dp, *from_dp;
117 int copy_len, num_wqe, i, ret = 0;
118 struct c4iw_wr_wait wr_wait;
Anish Bhattd7990b02014-11-12 17:15:57 -0800119 __be32 cmd = cpu_to_be32(ULPTX_CMD_V(ULP_TX_MEM_WRITE));
Vipul Pandya42b6a942013-03-14 05:09:01 +0000120
121 if (is_t4(rdev->lldi.adapter_type))
Anish Bhattd7990b02014-11-12 17:15:57 -0800122 cmd |= cpu_to_be32(ULP_MEMIO_ORDER_F);
Vipul Pandya42b6a942013-03-14 05:09:01 +0000123 else
Anish Bhattd7990b02014-11-12 17:15:57 -0800124 cmd |= cpu_to_be32(T5_ULP_MEMIO_IMM_F);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700125
126 addr &= 0x7FFFFFF;
127 PDBG("%s addr 0x%x len %u\n", __func__, addr, len);
128 num_wqe = DIV_ROUND_UP(len, C4IW_MAX_INLINE_SIZE);
129 c4iw_init_wr_wait(&wr_wait);
130 for (i = 0; i < num_wqe; i++) {
131
132 copy_len = len > C4IW_MAX_INLINE_SIZE ? C4IW_MAX_INLINE_SIZE :
133 len;
134 wr_len = roundup(sizeof *req + sizeof *sc +
135 roundup(copy_len, T4_ULPTX_MIN_IO), 16);
136
David Rientjesd3c814e2010-07-21 02:44:56 +0000137 skb = alloc_skb(wr_len, GFP_KERNEL);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700138 if (!skb)
139 return -ENOMEM;
140 set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
141
142 req = (struct ulp_mem_io *)__skb_put(skb, wr_len);
143 memset(req, 0, wr_len);
144 INIT_ULPTX_WR(req, wr_len, 0, 0);
145
146 if (i == (num_wqe-1)) {
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530147 req->wr.wr_hi = cpu_to_be32(FW_WR_OP_V(FW_ULPTX_WR) |
148 FW_WR_COMPL_F);
Arnd Bergmannb61e5642015-10-07 14:10:04 +0200149 req->wr.wr_lo = (__force __be64)(unsigned long)&wr_wait;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700150 } else
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530151 req->wr.wr_hi = cpu_to_be32(FW_WR_OP_V(FW_ULPTX_WR));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700152 req->wr.wr_mid = cpu_to_be32(
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530153 FW_WR_LEN16_V(DIV_ROUND_UP(wr_len, 16)));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700154
Vipul Pandya42b6a942013-03-14 05:09:01 +0000155 req->cmd = cmd;
Anish Bhattd7990b02014-11-12 17:15:57 -0800156 req->dlen = cpu_to_be32(ULP_MEMIO_DATA_LEN_V(
Steve Wisecfdda9d2010-04-21 15:30:06 -0700157 DIV_ROUND_UP(copy_len, T4_ULPTX_MIN_IO)));
158 req->len16 = cpu_to_be32(DIV_ROUND_UP(wr_len-sizeof(req->wr),
159 16));
Anish Bhattd7990b02014-11-12 17:15:57 -0800160 req->lock_addr = cpu_to_be32(ULP_MEMIO_ADDR_V(addr + i * 3));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700161
162 sc = (struct ulptx_idata *)(req + 1);
Anish Bhattd7990b02014-11-12 17:15:57 -0800163 sc->cmd_more = cpu_to_be32(ULPTX_CMD_V(ULP_TX_SC_IMM));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700164 sc->len = cpu_to_be32(roundup(copy_len, T4_ULPTX_MIN_IO));
165
166 to_dp = (u8 *)(sc + 1);
167 from_dp = (u8 *)data + i * C4IW_MAX_INLINE_SIZE;
168 if (data)
169 memcpy(to_dp, from_dp, copy_len);
170 else
171 memset(to_dp, 0, copy_len);
172 if (copy_len % T4_ULPTX_MIN_IO)
173 memset(to_dp + copy_len, 0, T4_ULPTX_MIN_IO -
174 (copy_len % T4_ULPTX_MIN_IO));
175 ret = c4iw_ofld_send(rdev, skb);
176 if (ret)
177 return ret;
178 len -= C4IW_MAX_INLINE_SIZE;
179 }
180
Steve Wiseaadc4df2010-09-10 11:15:25 -0500181 ret = c4iw_wait_for_reply(rdev, &wr_wait, 0, 0, __func__);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700182 return ret;
183}
184
Rashikac00850d2013-12-14 18:42:14 +0530185static int _c4iw_write_mem_dma(struct c4iw_rdev *rdev, u32 addr, u32 len, void *data)
Vipul Pandya42b6a942013-03-14 05:09:01 +0000186{
187 u32 remain = len;
188 u32 dmalen;
189 int ret = 0;
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000190 dma_addr_t daddr;
191 dma_addr_t save;
192
193 daddr = dma_map_single(&rdev->lldi.pdev->dev, data, len, DMA_TO_DEVICE);
194 if (dma_mapping_error(&rdev->lldi.pdev->dev, daddr))
195 return -1;
196 save = daddr;
Vipul Pandya42b6a942013-03-14 05:09:01 +0000197
198 while (remain > inline_threshold) {
199 if (remain < T4_ULPTX_MAX_DMA) {
200 if (remain & ~T4_ULPTX_MIN_IO)
201 dmalen = remain & ~(T4_ULPTX_MIN_IO-1);
202 else
203 dmalen = remain;
204 } else
205 dmalen = T4_ULPTX_MAX_DMA;
206 remain -= dmalen;
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000207 ret = _c4iw_write_mem_dma_aligned(rdev, addr, dmalen, daddr,
Vipul Pandya42b6a942013-03-14 05:09:01 +0000208 !remain);
209 if (ret)
210 goto out;
211 addr += dmalen >> 5;
212 data += dmalen;
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000213 daddr += dmalen;
Vipul Pandya42b6a942013-03-14 05:09:01 +0000214 }
215 if (remain)
216 ret = _c4iw_write_mem_inline(rdev, addr, remain, data);
217out:
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000218 dma_unmap_single(&rdev->lldi.pdev->dev, save, len, DMA_TO_DEVICE);
Vipul Pandya42b6a942013-03-14 05:09:01 +0000219 return ret;
220}
221
222/*
223 * write len bytes of data into addr (32B aligned address)
224 * If data is NULL, clear len byte of memory to zero.
225 */
226static int write_adapter_mem(struct c4iw_rdev *rdev, u32 addr, u32 len,
227 void *data)
228{
229 if (is_t5(rdev->lldi.adapter_type) && use_dsgl) {
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000230 if (len > inline_threshold) {
231 if (_c4iw_write_mem_dma(rdev, addr, len, data)) {
232 printk_ratelimited(KERN_WARNING
233 "%s: dma map"
234 " failure (non fatal)\n",
235 pci_name(rdev->lldi.pdev));
236 return _c4iw_write_mem_inline(rdev, addr, len,
237 data);
238 } else
239 return 0;
240 } else
Vipul Pandya42b6a942013-03-14 05:09:01 +0000241 return _c4iw_write_mem_inline(rdev, addr, len, data);
242 } else
243 return _c4iw_write_mem_inline(rdev, addr, len, data);
244}
245
Steve Wisecfdda9d2010-04-21 15:30:06 -0700246/*
247 * Build and write a TPT entry.
248 * IN: stag key, pdid, perm, bind_enabled, zbva, to, len, page_size,
249 * pbl_size and pbl_addr
250 * OUT: stag index
251 */
252static int write_tpt_entry(struct c4iw_rdev *rdev, u32 reset_tpt_entry,
253 u32 *stag, u8 stag_state, u32 pdid,
254 enum fw_ri_stag_type type, enum fw_ri_mem_perms perm,
255 int bind_enabled, u32 zbva, u64 to,
256 u64 len, u8 page_size, u32 pbl_size, u32 pbl_addr)
257{
258 int err;
259 struct fw_ri_tpte tpt;
260 u32 stag_idx;
261 static atomic_t key;
262
263 if (c4iw_fatal_error(rdev))
264 return -EIO;
265
266 stag_state = stag_state > 0;
267 stag_idx = (*stag) >> 8;
268
269 if ((!reset_tpt_entry) && (*stag == T4_STAG_UNSET)) {
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530270 stag_idx = c4iw_get_resource(&rdev->resource.tpt_table);
Steve Wise98a3e872014-04-09 09:38:28 -0500271 if (!stag_idx) {
272 mutex_lock(&rdev->stats.lock);
273 rdev->stats.stag.fail++;
274 mutex_unlock(&rdev->stats.lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700275 return -ENOMEM;
Steve Wise98a3e872014-04-09 09:38:28 -0500276 }
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530277 mutex_lock(&rdev->stats.lock);
278 rdev->stats.stag.cur += 32;
279 if (rdev->stats.stag.cur > rdev->stats.stag.max)
280 rdev->stats.stag.max = rdev->stats.stag.cur;
281 mutex_unlock(&rdev->stats.lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700282 *stag = (stag_idx << 8) | (atomic_inc_return(&key) & 0xff);
283 }
284 PDBG("%s stag_state 0x%0x type 0x%0x pdid 0x%0x, stag_idx 0x%x\n",
285 __func__, stag_state, type, pdid, stag_idx);
286
287 /* write TPT entry */
288 if (reset_tpt_entry)
289 memset(&tpt, 0, sizeof(tpt));
290 else {
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530291 tpt.valid_to_pdid = cpu_to_be32(FW_RI_TPTE_VALID_F |
292 FW_RI_TPTE_STAGKEY_V((*stag & FW_RI_TPTE_STAGKEY_M)) |
293 FW_RI_TPTE_STAGSTATE_V(stag_state) |
294 FW_RI_TPTE_STAGTYPE_V(type) | FW_RI_TPTE_PDID_V(pdid));
295 tpt.locread_to_qpid = cpu_to_be32(FW_RI_TPTE_PERM_V(perm) |
296 (bind_enabled ? FW_RI_TPTE_MWBINDEN_F : 0) |
297 FW_RI_TPTE_ADDRTYPE_V((zbva ? FW_RI_ZERO_BASED_TO :
Steve Wisecfdda9d2010-04-21 15:30:06 -0700298 FW_RI_VA_BASED_TO))|
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530299 FW_RI_TPTE_PS_V(page_size));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700300 tpt.nosnoop_pbladdr = !pbl_size ? 0 : cpu_to_be32(
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530301 FW_RI_TPTE_PBLADDR_V(PBL_OFF(rdev, pbl_addr)>>3));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700302 tpt.len_lo = cpu_to_be32((u32)(len & 0xffffffffUL));
303 tpt.va_hi = cpu_to_be32((u32)(to >> 32));
304 tpt.va_lo_fbo = cpu_to_be32((u32)(to & 0xffffffffUL));
305 tpt.dca_mwbcnt_pstag = cpu_to_be32(0);
306 tpt.len_hi = cpu_to_be32((u32)(len >> 32));
307 }
308 err = write_adapter_mem(rdev, stag_idx +
309 (rdev->lldi.vr->stag.start >> 5),
310 sizeof(tpt), &tpt);
311
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530312 if (reset_tpt_entry) {
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530313 c4iw_put_resource(&rdev->resource.tpt_table, stag_idx);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530314 mutex_lock(&rdev->stats.lock);
315 rdev->stats.stag.cur -= 32;
316 mutex_unlock(&rdev->stats.lock);
317 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700318 return err;
319}
320
321static int write_pbl(struct c4iw_rdev *rdev, __be64 *pbl,
322 u32 pbl_addr, u32 pbl_size)
323{
324 int err;
325
326 PDBG("%s *pdb_addr 0x%x, pbl_base 0x%x, pbl_size %d\n",
327 __func__, pbl_addr, rdev->lldi.vr->pbl.start,
328 pbl_size);
329
330 err = write_adapter_mem(rdev, pbl_addr >> 5, pbl_size << 3, pbl);
331 return err;
332}
333
334static int dereg_mem(struct c4iw_rdev *rdev, u32 stag, u32 pbl_size,
335 u32 pbl_addr)
336{
337 return write_tpt_entry(rdev, 1, &stag, 0, 0, 0, 0, 0, 0, 0UL, 0, 0,
338 pbl_size, pbl_addr);
339}
340
341static int allocate_window(struct c4iw_rdev *rdev, u32 * stag, u32 pdid)
342{
343 *stag = T4_STAG_UNSET;
344 return write_tpt_entry(rdev, 0, stag, 0, pdid, FW_RI_STAG_MW, 0, 0, 0,
345 0UL, 0, 0, 0, 0);
346}
347
348static int deallocate_window(struct c4iw_rdev *rdev, u32 stag)
349{
350 return write_tpt_entry(rdev, 1, &stag, 0, 0, 0, 0, 0, 0, 0UL, 0, 0, 0,
351 0);
352}
353
354static int allocate_stag(struct c4iw_rdev *rdev, u32 *stag, u32 pdid,
355 u32 pbl_size, u32 pbl_addr)
356{
357 *stag = T4_STAG_UNSET;
358 return write_tpt_entry(rdev, 0, stag, 0, pdid, FW_RI_STAG_NSMR, 0, 0, 0,
359 0UL, 0, 0, pbl_size, pbl_addr);
360}
361
362static int finish_mem_reg(struct c4iw_mr *mhp, u32 stag)
363{
364 u32 mmid;
365
366 mhp->attr.state = 1;
367 mhp->attr.stag = stag;
368 mmid = stag >> 8;
369 mhp->ibmr.rkey = mhp->ibmr.lkey = stag;
370 PDBG("%s mmid 0x%x mhp %p\n", __func__, mmid, mhp);
371 return insert_handle(mhp->rhp, &mhp->rhp->mmidr, mhp, mmid);
372}
373
374static int register_mem(struct c4iw_dev *rhp, struct c4iw_pd *php,
375 struct c4iw_mr *mhp, int shift)
376{
377 u32 stag = T4_STAG_UNSET;
378 int ret;
379
380 ret = write_tpt_entry(&rhp->rdev, 0, &stag, 1, mhp->attr.pdid,
Pramod Kumar123bc2a2014-11-21 09:36:35 -0600381 FW_RI_STAG_NSMR, mhp->attr.len ?
382 mhp->attr.perms : 0,
Steve Wisecfdda9d2010-04-21 15:30:06 -0700383 mhp->attr.mw_bind_enable, mhp->attr.zbva,
Pramod Kumar123bc2a2014-11-21 09:36:35 -0600384 mhp->attr.va_fbo, mhp->attr.len ?
385 mhp->attr.len : -1, shift - 12,
Steve Wisecfdda9d2010-04-21 15:30:06 -0700386 mhp->attr.pbl_size, mhp->attr.pbl_addr);
387 if (ret)
388 return ret;
389
390 ret = finish_mem_reg(mhp, stag);
391 if (ret)
392 dereg_mem(&rhp->rdev, mhp->attr.stag, mhp->attr.pbl_size,
393 mhp->attr.pbl_addr);
394 return ret;
395}
396
Steve Wisecfdda9d2010-04-21 15:30:06 -0700397static int alloc_pbl(struct c4iw_mr *mhp, int npages)
398{
399 mhp->attr.pbl_addr = c4iw_pblpool_alloc(&mhp->rhp->rdev,
400 npages << 3);
401
402 if (!mhp->attr.pbl_addr)
403 return -ENOMEM;
404
405 mhp->attr.pbl_size = npages;
406
407 return 0;
408}
409
Steve Wisecfdda9d2010-04-21 15:30:06 -0700410struct ib_mr *c4iw_get_dma_mr(struct ib_pd *pd, int acc)
411{
412 struct c4iw_dev *rhp;
413 struct c4iw_pd *php;
414 struct c4iw_mr *mhp;
415 int ret;
416 u32 stag = T4_STAG_UNSET;
417
418 PDBG("%s ib_pd %p\n", __func__, pd);
419 php = to_c4iw_pd(pd);
420 rhp = php->rhp;
421
422 mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
423 if (!mhp)
424 return ERR_PTR(-ENOMEM);
425
426 mhp->rhp = rhp;
427 mhp->attr.pdid = php->pdid;
428 mhp->attr.perms = c4iw_ib_to_tpt_access(acc);
429 mhp->attr.mw_bind_enable = (acc&IB_ACCESS_MW_BIND) == IB_ACCESS_MW_BIND;
430 mhp->attr.zbva = 0;
431 mhp->attr.va_fbo = 0;
432 mhp->attr.page_size = 0;
Hariprasad S6198dd82015-04-22 01:44:59 +0530433 mhp->attr.len = ~0ULL;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700434 mhp->attr.pbl_size = 0;
435
436 ret = write_tpt_entry(&rhp->rdev, 0, &stag, 1, php->pdid,
437 FW_RI_STAG_NSMR, mhp->attr.perms,
Hariprasad S6198dd82015-04-22 01:44:59 +0530438 mhp->attr.mw_bind_enable, 0, 0, ~0ULL, 0, 0, 0);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700439 if (ret)
440 goto err1;
441
442 ret = finish_mem_reg(mhp, stag);
443 if (ret)
444 goto err2;
445 return &mhp->ibmr;
446err2:
447 dereg_mem(&rhp->rdev, mhp->attr.stag, mhp->attr.pbl_size,
448 mhp->attr.pbl_addr);
449err1:
450 kfree(mhp);
451 return ERR_PTR(ret);
452}
453
454struct ib_mr *c4iw_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
455 u64 virt, int acc, struct ib_udata *udata)
456{
457 __be64 *pages;
458 int shift, n, len;
Yishai Hadaseeb84612014-01-28 13:40:15 +0200459 int i, k, entry;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700460 int err = 0;
Yishai Hadaseeb84612014-01-28 13:40:15 +0200461 struct scatterlist *sg;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700462 struct c4iw_dev *rhp;
463 struct c4iw_pd *php;
464 struct c4iw_mr *mhp;
465
466 PDBG("%s ib_pd %p\n", __func__, pd);
467
468 if (length == ~0ULL)
469 return ERR_PTR(-EINVAL);
470
471 if ((length + start) < start)
472 return ERR_PTR(-EINVAL);
473
474 php = to_c4iw_pd(pd);
475 rhp = php->rhp;
Hariprasad Shenai2550a882014-11-21 09:36:36 -0600476
477 if (mr_exceeds_hw_limits(rhp, length))
478 return ERR_PTR(-EINVAL);
479
Steve Wisecfdda9d2010-04-21 15:30:06 -0700480 mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
481 if (!mhp)
482 return ERR_PTR(-ENOMEM);
483
484 mhp->rhp = rhp;
485
486 mhp->umem = ib_umem_get(pd->uobject->context, start, length, acc, 0);
487 if (IS_ERR(mhp->umem)) {
488 err = PTR_ERR(mhp->umem);
489 kfree(mhp);
490 return ERR_PTR(err);
491 }
492
493 shift = ffs(mhp->umem->page_size) - 1;
494
Yishai Hadaseeb84612014-01-28 13:40:15 +0200495 n = mhp->umem->nmap;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700496 err = alloc_pbl(mhp, n);
497 if (err)
498 goto err;
499
500 pages = (__be64 *) __get_free_page(GFP_KERNEL);
501 if (!pages) {
502 err = -ENOMEM;
503 goto err_pbl;
504 }
505
506 i = n = 0;
507
Yishai Hadaseeb84612014-01-28 13:40:15 +0200508 for_each_sg(mhp->umem->sg_head.sgl, sg, mhp->umem->nmap, entry) {
509 len = sg_dma_len(sg) >> shift;
510 for (k = 0; k < len; ++k) {
511 pages[i++] = cpu_to_be64(sg_dma_address(sg) +
512 mhp->umem->page_size * k);
513 if (i == PAGE_SIZE / sizeof *pages) {
514 err = write_pbl(&mhp->rhp->rdev,
515 pages,
516 mhp->attr.pbl_addr + (n << 3), i);
517 if (err)
518 goto pbl_done;
519 n += i;
520 i = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700521 }
522 }
Yishai Hadaseeb84612014-01-28 13:40:15 +0200523 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700524
525 if (i)
526 err = write_pbl(&mhp->rhp->rdev, pages,
527 mhp->attr.pbl_addr + (n << 3), i);
528
529pbl_done:
530 free_page((unsigned long) pages);
531 if (err)
532 goto err_pbl;
533
534 mhp->attr.pdid = php->pdid;
535 mhp->attr.zbva = 0;
536 mhp->attr.perms = c4iw_ib_to_tpt_access(acc);
537 mhp->attr.va_fbo = virt;
538 mhp->attr.page_size = shift - 12;
Steve Wise301c2c3f2011-06-14 20:59:21 +0000539 mhp->attr.len = length;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700540
541 err = register_mem(rhp, php, mhp, shift);
542 if (err)
543 goto err_pbl;
544
545 return &mhp->ibmr;
546
547err_pbl:
548 c4iw_pblpool_free(&mhp->rhp->rdev, mhp->attr.pbl_addr,
549 mhp->attr.pbl_size << 3);
550
551err:
552 ib_umem_release(mhp->umem);
553 kfree(mhp);
554 return ERR_PTR(err);
555}
556
Matan Barakb2a239d2016-02-29 18:05:29 +0200557struct ib_mw *c4iw_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
558 struct ib_udata *udata)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700559{
560 struct c4iw_dev *rhp;
561 struct c4iw_pd *php;
562 struct c4iw_mw *mhp;
563 u32 mmid;
564 u32 stag = 0;
565 int ret;
566
Shani Michaeli7083e422013-02-06 16:19:12 +0000567 if (type != IB_MW_TYPE_1)
568 return ERR_PTR(-EINVAL);
569
Steve Wisecfdda9d2010-04-21 15:30:06 -0700570 php = to_c4iw_pd(pd);
571 rhp = php->rhp;
572 mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
573 if (!mhp)
574 return ERR_PTR(-ENOMEM);
575 ret = allocate_window(&rhp->rdev, &stag, php->pdid);
576 if (ret) {
577 kfree(mhp);
578 return ERR_PTR(ret);
579 }
580 mhp->rhp = rhp;
581 mhp->attr.pdid = php->pdid;
582 mhp->attr.type = FW_RI_STAG_MW;
583 mhp->attr.stag = stag;
584 mmid = (stag) >> 8;
585 mhp->ibmw.rkey = stag;
586 if (insert_handle(rhp, &rhp->mmidr, mhp, mmid)) {
587 deallocate_window(&rhp->rdev, mhp->attr.stag);
588 kfree(mhp);
589 return ERR_PTR(-ENOMEM);
590 }
591 PDBG("%s mmid 0x%x mhp %p stag 0x%x\n", __func__, mmid, mhp, stag);
592 return &(mhp->ibmw);
593}
594
595int c4iw_dealloc_mw(struct ib_mw *mw)
596{
597 struct c4iw_dev *rhp;
598 struct c4iw_mw *mhp;
599 u32 mmid;
600
601 mhp = to_c4iw_mw(mw);
602 rhp = mhp->rhp;
603 mmid = (mw->rkey) >> 8;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700604 remove_handle(rhp, &rhp->mmidr, mmid);
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530605 deallocate_window(&rhp->rdev, mhp->attr.stag);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700606 kfree(mhp);
607 PDBG("%s ib_mw %p mmid 0x%x ptr %p\n", __func__, mw, mmid, mhp);
608 return 0;
609}
610
Sagi Grimberga2164032015-07-30 10:32:44 +0300611struct ib_mr *c4iw_alloc_mr(struct ib_pd *pd,
612 enum ib_mr_type mr_type,
613 u32 max_num_sg)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700614{
615 struct c4iw_dev *rhp;
616 struct c4iw_pd *php;
617 struct c4iw_mr *mhp;
618 u32 mmid;
619 u32 stag = 0;
620 int ret = 0;
Sagi Grimberg8376b862015-10-13 19:11:30 +0300621 int length = roundup(max_num_sg * sizeof(u64), 32);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700622
623 php = to_c4iw_pd(pd);
624 rhp = php->rhp;
Hariprasad See30f7d2016-02-12 16:10:35 +0530625
626 if (mr_type != IB_MR_TYPE_MEM_REG ||
627 max_num_sg > t4_max_fr_depth(&rhp->rdev.lldi.ulptx_memwrite_dsgl &&
628 use_dsgl))
629 return ERR_PTR(-EINVAL);
630
Steve Wisecfdda9d2010-04-21 15:30:06 -0700631 mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
Steve Wise841dba92010-05-20 16:57:54 -0500632 if (!mhp) {
633 ret = -ENOMEM;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700634 goto err;
Steve Wise841dba92010-05-20 16:57:54 -0500635 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700636
Sagi Grimberg8376b862015-10-13 19:11:30 +0300637 mhp->mpl = dma_alloc_coherent(&rhp->rdev.lldi.pdev->dev,
638 length, &mhp->mpl_addr, GFP_KERNEL);
639 if (!mhp->mpl) {
640 ret = -ENOMEM;
641 goto err_mpl;
642 }
643 mhp->max_mpl_len = length;
644
Steve Wisecfdda9d2010-04-21 15:30:06 -0700645 mhp->rhp = rhp;
Sagi Grimberga2164032015-07-30 10:32:44 +0300646 ret = alloc_pbl(mhp, max_num_sg);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700647 if (ret)
648 goto err1;
Sagi Grimberga2164032015-07-30 10:32:44 +0300649 mhp->attr.pbl_size = max_num_sg;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700650 ret = allocate_stag(&rhp->rdev, &stag, php->pdid,
651 mhp->attr.pbl_size, mhp->attr.pbl_addr);
652 if (ret)
653 goto err2;
654 mhp->attr.pdid = php->pdid;
655 mhp->attr.type = FW_RI_STAG_NSMR;
656 mhp->attr.stag = stag;
657 mhp->attr.state = 1;
658 mmid = (stag) >> 8;
659 mhp->ibmr.rkey = mhp->ibmr.lkey = stag;
Steve Wise841dba92010-05-20 16:57:54 -0500660 if (insert_handle(rhp, &rhp->mmidr, mhp, mmid)) {
661 ret = -ENOMEM;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700662 goto err3;
Steve Wise841dba92010-05-20 16:57:54 -0500663 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700664
665 PDBG("%s mmid 0x%x mhp %p stag 0x%x\n", __func__, mmid, mhp, stag);
666 return &(mhp->ibmr);
667err3:
668 dereg_mem(&rhp->rdev, stag, mhp->attr.pbl_size,
669 mhp->attr.pbl_addr);
670err2:
671 c4iw_pblpool_free(&mhp->rhp->rdev, mhp->attr.pbl_addr,
672 mhp->attr.pbl_size << 3);
673err1:
Sagi Grimberg8376b862015-10-13 19:11:30 +0300674 dma_free_coherent(&mhp->rhp->rdev.lldi.pdev->dev,
675 mhp->max_mpl_len, mhp->mpl, mhp->mpl_addr);
676err_mpl:
Steve Wisecfdda9d2010-04-21 15:30:06 -0700677 kfree(mhp);
678err:
679 return ERR_PTR(ret);
680}
681
Sagi Grimberg8376b862015-10-13 19:11:30 +0300682static int c4iw_set_page(struct ib_mr *ibmr, u64 addr)
683{
684 struct c4iw_mr *mhp = to_c4iw_mr(ibmr);
685
686 if (unlikely(mhp->mpl_len == mhp->max_mpl_len))
687 return -ENOMEM;
688
689 mhp->mpl[mhp->mpl_len++] = addr;
690
691 return 0;
692}
693
694int c4iw_map_mr_sg(struct ib_mr *ibmr,
695 struct scatterlist *sg,
696 int sg_nents)
697{
698 struct c4iw_mr *mhp = to_c4iw_mr(ibmr);
699
700 mhp->mpl_len = 0;
701
702 return ib_sg_to_pages(ibmr, sg, sg_nents, c4iw_set_page);
703}
704
Steve Wisecfdda9d2010-04-21 15:30:06 -0700705int c4iw_dereg_mr(struct ib_mr *ib_mr)
706{
707 struct c4iw_dev *rhp;
708 struct c4iw_mr *mhp;
709 u32 mmid;
710
711 PDBG("%s ib_mr %p\n", __func__, ib_mr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700712
713 mhp = to_c4iw_mr(ib_mr);
714 rhp = mhp->rhp;
715 mmid = mhp->attr.stag >> 8;
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530716 remove_handle(rhp, &rhp->mmidr, mmid);
Sagi Grimberg8376b862015-10-13 19:11:30 +0300717 if (mhp->mpl)
718 dma_free_coherent(&mhp->rhp->rdev.lldi.pdev->dev,
719 mhp->max_mpl_len, mhp->mpl, mhp->mpl_addr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700720 dereg_mem(&rhp->rdev, mhp->attr.stag, mhp->attr.pbl_size,
721 mhp->attr.pbl_addr);
722 if (mhp->attr.pbl_size)
723 c4iw_pblpool_free(&mhp->rhp->rdev, mhp->attr.pbl_addr,
724 mhp->attr.pbl_size << 3);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700725 if (mhp->kva)
726 kfree((void *) (unsigned long) mhp->kva);
727 if (mhp->umem)
728 ib_umem_release(mhp->umem);
729 PDBG("%s mmid 0x%x ptr %p\n", __func__, mmid, mhp);
730 kfree(mhp);
731 return 0;
732}