blob: 0b91b0f4df71cf5265f75b57b5c9b0a8ea1a41cc [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,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +053062 u32 len, dma_addr_t data,
63 int wait, struct sk_buff *skb)
Vipul Pandya42b6a942013-03-14 05:09:01 +000064{
Vipul Pandya42b6a942013-03-14 05:09:01 +000065 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
Hariprasad S0f8ab0b2016-06-10 01:05:16 +053077 if (!skb) {
78 skb = alloc_skb(wr_len, GFP_KERNEL | __GFP_NOFAIL);
79 if (!skb)
80 return -ENOMEM;
81 }
Vipul Pandya42b6a942013-03-14 05:09:01 +000082 set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
83
84 req = (struct ulp_mem_io *)__skb_put(skb, wr_len);
85 memset(req, 0, wr_len);
86 INIT_ULPTX_WR(req, wr_len, 0, 0);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +053087 req->wr.wr_hi = cpu_to_be32(FW_WR_OP_V(FW_ULPTX_WR) |
88 (wait ? FW_WR_COMPL_F : 0));
Paul Bolle298589b2014-01-09 11:53:27 +010089 req->wr.wr_lo = wait ? (__force __be64)(unsigned long) &wr_wait : 0L;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +053090 req->wr.wr_mid = cpu_to_be32(FW_WR_LEN16_V(DIV_ROUND_UP(wr_len, 16)));
Hariprasad S92f850e2016-05-06 22:17:56 +053091 req->cmd = cpu_to_be32(ULPTX_CMD_V(ULP_TX_MEM_WRITE) |
92 T5_ULP_MEMIO_ORDER_V(1) |
93 T5_ULP_MEMIO_FID_V(rdev->lldi.rxq_ids[0]));
Anish Bhattd7990b02014-11-12 17:15:57 -080094 req->dlen = cpu_to_be32(ULP_MEMIO_DATA_LEN_V(len>>5));
Vipul Pandya42b6a942013-03-14 05:09:01 +000095 req->len16 = cpu_to_be32(DIV_ROUND_UP(wr_len-sizeof(req->wr), 16));
Anish Bhattd7990b02014-11-12 17:15:57 -080096 req->lock_addr = cpu_to_be32(ULP_MEMIO_ADDR_V(addr));
Vipul Pandya42b6a942013-03-14 05:09:01 +000097
98 sgl = (struct ulptx_sgl *)(req + 1);
Anish Bhattd7990b02014-11-12 17:15:57 -080099 sgl->cmd_nsge = cpu_to_be32(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800100 ULPTX_NSGE_V(1));
Vipul Pandya42b6a942013-03-14 05:09:01 +0000101 sgl->len0 = cpu_to_be32(len);
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000102 sgl->addr0 = cpu_to_be64(data);
Vipul Pandya42b6a942013-03-14 05:09:01 +0000103
104 ret = c4iw_ofld_send(rdev, skb);
105 if (ret)
106 return ret;
107 if (wait)
108 ret = c4iw_wait_for_reply(rdev, &wr_wait, 0, 0, __func__);
109 return ret;
110}
111
112static int _c4iw_write_mem_inline(struct c4iw_rdev *rdev, u32 addr, u32 len,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530113 void *data, struct sk_buff *skb)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700114{
Steve Wisecfdda9d2010-04-21 15:30:06 -0700115 struct ulp_mem_io *req;
116 struct ulptx_idata *sc;
117 u8 wr_len, *to_dp, *from_dp;
118 int copy_len, num_wqe, i, ret = 0;
119 struct c4iw_wr_wait wr_wait;
Anish Bhattd7990b02014-11-12 17:15:57 -0800120 __be32 cmd = cpu_to_be32(ULPTX_CMD_V(ULP_TX_MEM_WRITE));
Vipul Pandya42b6a942013-03-14 05:09:01 +0000121
122 if (is_t4(rdev->lldi.adapter_type))
Anish Bhattd7990b02014-11-12 17:15:57 -0800123 cmd |= cpu_to_be32(ULP_MEMIO_ORDER_F);
Vipul Pandya42b6a942013-03-14 05:09:01 +0000124 else
Anish Bhattd7990b02014-11-12 17:15:57 -0800125 cmd |= cpu_to_be32(T5_ULP_MEMIO_IMM_F);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700126
127 addr &= 0x7FFFFFF;
128 PDBG("%s addr 0x%x len %u\n", __func__, addr, len);
129 num_wqe = DIV_ROUND_UP(len, C4IW_MAX_INLINE_SIZE);
130 c4iw_init_wr_wait(&wr_wait);
131 for (i = 0; i < num_wqe; i++) {
132
133 copy_len = len > C4IW_MAX_INLINE_SIZE ? C4IW_MAX_INLINE_SIZE :
134 len;
135 wr_len = roundup(sizeof *req + sizeof *sc +
136 roundup(copy_len, T4_ULPTX_MIN_IO), 16);
137
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530138 if (!skb) {
139 skb = alloc_skb(wr_len, GFP_KERNEL | __GFP_NOFAIL);
140 if (!skb)
141 return -ENOMEM;
142 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700143 set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
144
145 req = (struct ulp_mem_io *)__skb_put(skb, wr_len);
146 memset(req, 0, wr_len);
147 INIT_ULPTX_WR(req, wr_len, 0, 0);
148
149 if (i == (num_wqe-1)) {
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530150 req->wr.wr_hi = cpu_to_be32(FW_WR_OP_V(FW_ULPTX_WR) |
151 FW_WR_COMPL_F);
Arnd Bergmannb61e5642015-10-07 14:10:04 +0200152 req->wr.wr_lo = (__force __be64)(unsigned long)&wr_wait;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700153 } else
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530154 req->wr.wr_hi = cpu_to_be32(FW_WR_OP_V(FW_ULPTX_WR));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700155 req->wr.wr_mid = cpu_to_be32(
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530156 FW_WR_LEN16_V(DIV_ROUND_UP(wr_len, 16)));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700157
Vipul Pandya42b6a942013-03-14 05:09:01 +0000158 req->cmd = cmd;
Anish Bhattd7990b02014-11-12 17:15:57 -0800159 req->dlen = cpu_to_be32(ULP_MEMIO_DATA_LEN_V(
Steve Wisecfdda9d2010-04-21 15:30:06 -0700160 DIV_ROUND_UP(copy_len, T4_ULPTX_MIN_IO)));
161 req->len16 = cpu_to_be32(DIV_ROUND_UP(wr_len-sizeof(req->wr),
162 16));
Anish Bhattd7990b02014-11-12 17:15:57 -0800163 req->lock_addr = cpu_to_be32(ULP_MEMIO_ADDR_V(addr + i * 3));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700164
165 sc = (struct ulptx_idata *)(req + 1);
Anish Bhattd7990b02014-11-12 17:15:57 -0800166 sc->cmd_more = cpu_to_be32(ULPTX_CMD_V(ULP_TX_SC_IMM));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700167 sc->len = cpu_to_be32(roundup(copy_len, T4_ULPTX_MIN_IO));
168
169 to_dp = (u8 *)(sc + 1);
170 from_dp = (u8 *)data + i * C4IW_MAX_INLINE_SIZE;
171 if (data)
172 memcpy(to_dp, from_dp, copy_len);
173 else
174 memset(to_dp, 0, copy_len);
175 if (copy_len % T4_ULPTX_MIN_IO)
176 memset(to_dp + copy_len, 0, T4_ULPTX_MIN_IO -
177 (copy_len % T4_ULPTX_MIN_IO));
178 ret = c4iw_ofld_send(rdev, skb);
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530179 skb = NULL;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700180 if (ret)
181 return ret;
182 len -= C4IW_MAX_INLINE_SIZE;
183 }
184
Steve Wiseaadc4df2010-09-10 11:15:25 -0500185 ret = c4iw_wait_for_reply(rdev, &wr_wait, 0, 0, __func__);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700186 return ret;
187}
188
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530189static int _c4iw_write_mem_dma(struct c4iw_rdev *rdev, u32 addr, u32 len,
190 void *data, struct sk_buff *skb)
Vipul Pandya42b6a942013-03-14 05:09:01 +0000191{
192 u32 remain = len;
193 u32 dmalen;
194 int ret = 0;
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000195 dma_addr_t daddr;
196 dma_addr_t save;
197
198 daddr = dma_map_single(&rdev->lldi.pdev->dev, data, len, DMA_TO_DEVICE);
199 if (dma_mapping_error(&rdev->lldi.pdev->dev, daddr))
200 return -1;
201 save = daddr;
Vipul Pandya42b6a942013-03-14 05:09:01 +0000202
203 while (remain > inline_threshold) {
204 if (remain < T4_ULPTX_MAX_DMA) {
205 if (remain & ~T4_ULPTX_MIN_IO)
206 dmalen = remain & ~(T4_ULPTX_MIN_IO-1);
207 else
208 dmalen = remain;
209 } else
210 dmalen = T4_ULPTX_MAX_DMA;
211 remain -= dmalen;
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000212 ret = _c4iw_write_mem_dma_aligned(rdev, addr, dmalen, daddr,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530213 !remain, skb);
Vipul Pandya42b6a942013-03-14 05:09:01 +0000214 if (ret)
215 goto out;
216 addr += dmalen >> 5;
217 data += dmalen;
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000218 daddr += dmalen;
Vipul Pandya42b6a942013-03-14 05:09:01 +0000219 }
220 if (remain)
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530221 ret = _c4iw_write_mem_inline(rdev, addr, remain, data, skb);
Vipul Pandya42b6a942013-03-14 05:09:01 +0000222out:
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000223 dma_unmap_single(&rdev->lldi.pdev->dev, save, len, DMA_TO_DEVICE);
Vipul Pandya42b6a942013-03-14 05:09:01 +0000224 return ret;
225}
226
227/*
228 * write len bytes of data into addr (32B aligned address)
229 * If data is NULL, clear len byte of memory to zero.
230 */
231static int write_adapter_mem(struct c4iw_rdev *rdev, u32 addr, u32 len,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530232 void *data, struct sk_buff *skb)
Vipul Pandya42b6a942013-03-14 05:09:01 +0000233{
234 if (is_t5(rdev->lldi.adapter_type) && use_dsgl) {
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000235 if (len > inline_threshold) {
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530236 if (_c4iw_write_mem_dma(rdev, addr, len, data, skb)) {
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000237 printk_ratelimited(KERN_WARNING
238 "%s: dma map"
239 " failure (non fatal)\n",
240 pci_name(rdev->lldi.pdev));
241 return _c4iw_write_mem_inline(rdev, addr, len,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530242 data, skb);
243 } else {
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000244 return 0;
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530245 }
Vipul Pandya0e5eca72013-03-14 05:09:02 +0000246 } else
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530247 return _c4iw_write_mem_inline(rdev, addr,
248 len, data, skb);
Vipul Pandya42b6a942013-03-14 05:09:01 +0000249 } else
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530250 return _c4iw_write_mem_inline(rdev, addr, len, data, skb);
Vipul Pandya42b6a942013-03-14 05:09:01 +0000251}
252
Steve Wisecfdda9d2010-04-21 15:30:06 -0700253/*
254 * Build and write a TPT entry.
255 * IN: stag key, pdid, perm, bind_enabled, zbva, to, len, page_size,
256 * pbl_size and pbl_addr
257 * OUT: stag index
258 */
259static int write_tpt_entry(struct c4iw_rdev *rdev, u32 reset_tpt_entry,
260 u32 *stag, u8 stag_state, u32 pdid,
261 enum fw_ri_stag_type type, enum fw_ri_mem_perms perm,
262 int bind_enabled, u32 zbva, u64 to,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530263 u64 len, u8 page_size, u32 pbl_size, u32 pbl_addr,
264 struct sk_buff *skb)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700265{
266 int err;
267 struct fw_ri_tpte tpt;
268 u32 stag_idx;
269 static atomic_t key;
270
271 if (c4iw_fatal_error(rdev))
272 return -EIO;
273
274 stag_state = stag_state > 0;
275 stag_idx = (*stag) >> 8;
276
277 if ((!reset_tpt_entry) && (*stag == T4_STAG_UNSET)) {
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530278 stag_idx = c4iw_get_resource(&rdev->resource.tpt_table);
Steve Wise98a3e872014-04-09 09:38:28 -0500279 if (!stag_idx) {
280 mutex_lock(&rdev->stats.lock);
281 rdev->stats.stag.fail++;
282 mutex_unlock(&rdev->stats.lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700283 return -ENOMEM;
Steve Wise98a3e872014-04-09 09:38:28 -0500284 }
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530285 mutex_lock(&rdev->stats.lock);
286 rdev->stats.stag.cur += 32;
287 if (rdev->stats.stag.cur > rdev->stats.stag.max)
288 rdev->stats.stag.max = rdev->stats.stag.cur;
289 mutex_unlock(&rdev->stats.lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700290 *stag = (stag_idx << 8) | (atomic_inc_return(&key) & 0xff);
291 }
292 PDBG("%s stag_state 0x%0x type 0x%0x pdid 0x%0x, stag_idx 0x%x\n",
293 __func__, stag_state, type, pdid, stag_idx);
294
295 /* write TPT entry */
296 if (reset_tpt_entry)
297 memset(&tpt, 0, sizeof(tpt));
298 else {
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530299 tpt.valid_to_pdid = cpu_to_be32(FW_RI_TPTE_VALID_F |
300 FW_RI_TPTE_STAGKEY_V((*stag & FW_RI_TPTE_STAGKEY_M)) |
301 FW_RI_TPTE_STAGSTATE_V(stag_state) |
302 FW_RI_TPTE_STAGTYPE_V(type) | FW_RI_TPTE_PDID_V(pdid));
303 tpt.locread_to_qpid = cpu_to_be32(FW_RI_TPTE_PERM_V(perm) |
304 (bind_enabled ? FW_RI_TPTE_MWBINDEN_F : 0) |
305 FW_RI_TPTE_ADDRTYPE_V((zbva ? FW_RI_ZERO_BASED_TO :
Steve Wisecfdda9d2010-04-21 15:30:06 -0700306 FW_RI_VA_BASED_TO))|
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530307 FW_RI_TPTE_PS_V(page_size));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700308 tpt.nosnoop_pbladdr = !pbl_size ? 0 : cpu_to_be32(
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530309 FW_RI_TPTE_PBLADDR_V(PBL_OFF(rdev, pbl_addr)>>3));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700310 tpt.len_lo = cpu_to_be32((u32)(len & 0xffffffffUL));
311 tpt.va_hi = cpu_to_be32((u32)(to >> 32));
312 tpt.va_lo_fbo = cpu_to_be32((u32)(to & 0xffffffffUL));
313 tpt.dca_mwbcnt_pstag = cpu_to_be32(0);
314 tpt.len_hi = cpu_to_be32((u32)(len >> 32));
315 }
316 err = write_adapter_mem(rdev, stag_idx +
317 (rdev->lldi.vr->stag.start >> 5),
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530318 sizeof(tpt), &tpt, skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700319
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530320 if (reset_tpt_entry) {
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530321 c4iw_put_resource(&rdev->resource.tpt_table, stag_idx);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530322 mutex_lock(&rdev->stats.lock);
323 rdev->stats.stag.cur -= 32;
324 mutex_unlock(&rdev->stats.lock);
325 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700326 return err;
327}
328
329static int write_pbl(struct c4iw_rdev *rdev, __be64 *pbl,
330 u32 pbl_addr, u32 pbl_size)
331{
332 int err;
333
334 PDBG("%s *pdb_addr 0x%x, pbl_base 0x%x, pbl_size %d\n",
335 __func__, pbl_addr, rdev->lldi.vr->pbl.start,
336 pbl_size);
337
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530338 err = write_adapter_mem(rdev, pbl_addr >> 5, pbl_size << 3, pbl, NULL);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700339 return err;
340}
341
342static int dereg_mem(struct c4iw_rdev *rdev, u32 stag, u32 pbl_size,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530343 u32 pbl_addr, struct sk_buff *skb)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700344{
345 return write_tpt_entry(rdev, 1, &stag, 0, 0, 0, 0, 0, 0, 0UL, 0, 0,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530346 pbl_size, pbl_addr, skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700347}
348
349static int allocate_window(struct c4iw_rdev *rdev, u32 * stag, u32 pdid)
350{
351 *stag = T4_STAG_UNSET;
352 return write_tpt_entry(rdev, 0, stag, 0, pdid, FW_RI_STAG_MW, 0, 0, 0,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530353 0UL, 0, 0, 0, 0, NULL);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700354}
355
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530356static int deallocate_window(struct c4iw_rdev *rdev, u32 stag,
357 struct sk_buff *skb)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700358{
359 return write_tpt_entry(rdev, 1, &stag, 0, 0, 0, 0, 0, 0, 0UL, 0, 0, 0,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530360 0, skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700361}
362
363static int allocate_stag(struct c4iw_rdev *rdev, u32 *stag, u32 pdid,
364 u32 pbl_size, u32 pbl_addr)
365{
366 *stag = T4_STAG_UNSET;
367 return write_tpt_entry(rdev, 0, stag, 0, pdid, FW_RI_STAG_NSMR, 0, 0, 0,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530368 0UL, 0, 0, pbl_size, pbl_addr, NULL);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700369}
370
371static int finish_mem_reg(struct c4iw_mr *mhp, u32 stag)
372{
373 u32 mmid;
374
375 mhp->attr.state = 1;
376 mhp->attr.stag = stag;
377 mmid = stag >> 8;
378 mhp->ibmr.rkey = mhp->ibmr.lkey = stag;
379 PDBG("%s mmid 0x%x mhp %p\n", __func__, mmid, mhp);
380 return insert_handle(mhp->rhp, &mhp->rhp->mmidr, mhp, mmid);
381}
382
383static int register_mem(struct c4iw_dev *rhp, struct c4iw_pd *php,
384 struct c4iw_mr *mhp, int shift)
385{
386 u32 stag = T4_STAG_UNSET;
387 int ret;
388
389 ret = write_tpt_entry(&rhp->rdev, 0, &stag, 1, mhp->attr.pdid,
Pramod Kumar123bc2a2014-11-21 09:36:35 -0600390 FW_RI_STAG_NSMR, mhp->attr.len ?
391 mhp->attr.perms : 0,
Steve Wisecfdda9d2010-04-21 15:30:06 -0700392 mhp->attr.mw_bind_enable, mhp->attr.zbva,
Pramod Kumar123bc2a2014-11-21 09:36:35 -0600393 mhp->attr.va_fbo, mhp->attr.len ?
394 mhp->attr.len : -1, shift - 12,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530395 mhp->attr.pbl_size, mhp->attr.pbl_addr, NULL);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700396 if (ret)
397 return ret;
398
399 ret = finish_mem_reg(mhp, stag);
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530400 if (ret) {
Steve Wisecfdda9d2010-04-21 15:30:06 -0700401 dereg_mem(&rhp->rdev, mhp->attr.stag, mhp->attr.pbl_size,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530402 mhp->attr.pbl_addr, mhp->dereg_skb);
403 mhp->dereg_skb = NULL;
404 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700405 return ret;
406}
407
Steve Wisecfdda9d2010-04-21 15:30:06 -0700408static int alloc_pbl(struct c4iw_mr *mhp, int npages)
409{
410 mhp->attr.pbl_addr = c4iw_pblpool_alloc(&mhp->rhp->rdev,
411 npages << 3);
412
413 if (!mhp->attr.pbl_addr)
414 return -ENOMEM;
415
416 mhp->attr.pbl_size = npages;
417
418 return 0;
419}
420
Steve Wisecfdda9d2010-04-21 15:30:06 -0700421struct ib_mr *c4iw_get_dma_mr(struct ib_pd *pd, int acc)
422{
423 struct c4iw_dev *rhp;
424 struct c4iw_pd *php;
425 struct c4iw_mr *mhp;
426 int ret;
427 u32 stag = T4_STAG_UNSET;
428
429 PDBG("%s ib_pd %p\n", __func__, pd);
430 php = to_c4iw_pd(pd);
431 rhp = php->rhp;
432
433 mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
434 if (!mhp)
435 return ERR_PTR(-ENOMEM);
436
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530437 mhp->dereg_skb = alloc_skb(SGE_MAX_WR_LEN, GFP_KERNEL);
438 if (!mhp->dereg_skb) {
439 ret = -ENOMEM;
440 goto err0;
441 }
442
Steve Wisecfdda9d2010-04-21 15:30:06 -0700443 mhp->rhp = rhp;
444 mhp->attr.pdid = php->pdid;
445 mhp->attr.perms = c4iw_ib_to_tpt_access(acc);
446 mhp->attr.mw_bind_enable = (acc&IB_ACCESS_MW_BIND) == IB_ACCESS_MW_BIND;
447 mhp->attr.zbva = 0;
448 mhp->attr.va_fbo = 0;
449 mhp->attr.page_size = 0;
Hariprasad S6198dd82015-04-22 01:44:59 +0530450 mhp->attr.len = ~0ULL;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700451 mhp->attr.pbl_size = 0;
452
453 ret = write_tpt_entry(&rhp->rdev, 0, &stag, 1, php->pdid,
454 FW_RI_STAG_NSMR, mhp->attr.perms,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530455 mhp->attr.mw_bind_enable, 0, 0, ~0ULL, 0, 0, 0,
456 NULL);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700457 if (ret)
458 goto err1;
459
460 ret = finish_mem_reg(mhp, stag);
461 if (ret)
462 goto err2;
463 return &mhp->ibmr;
464err2:
465 dereg_mem(&rhp->rdev, mhp->attr.stag, mhp->attr.pbl_size,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530466 mhp->attr.pbl_addr, mhp->dereg_skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700467err1:
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530468 kfree_skb(mhp->dereg_skb);
469err0:
Steve Wisecfdda9d2010-04-21 15:30:06 -0700470 kfree(mhp);
471 return ERR_PTR(ret);
472}
473
474struct ib_mr *c4iw_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
475 u64 virt, int acc, struct ib_udata *udata)
476{
477 __be64 *pages;
478 int shift, n, len;
Yishai Hadaseeb84612014-01-28 13:40:15 +0200479 int i, k, entry;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700480 int err = 0;
Yishai Hadaseeb84612014-01-28 13:40:15 +0200481 struct scatterlist *sg;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700482 struct c4iw_dev *rhp;
483 struct c4iw_pd *php;
484 struct c4iw_mr *mhp;
485
486 PDBG("%s ib_pd %p\n", __func__, pd);
487
488 if (length == ~0ULL)
489 return ERR_PTR(-EINVAL);
490
491 if ((length + start) < start)
492 return ERR_PTR(-EINVAL);
493
494 php = to_c4iw_pd(pd);
495 rhp = php->rhp;
Hariprasad Shenai2550a882014-11-21 09:36:36 -0600496
497 if (mr_exceeds_hw_limits(rhp, length))
498 return ERR_PTR(-EINVAL);
499
Steve Wisecfdda9d2010-04-21 15:30:06 -0700500 mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
501 if (!mhp)
502 return ERR_PTR(-ENOMEM);
503
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530504 mhp->dereg_skb = alloc_skb(SGE_MAX_WR_LEN, GFP_KERNEL);
505 if (!mhp->dereg_skb) {
506 kfree(mhp);
507 return ERR_PTR(-ENOMEM);
508 }
509
Steve Wisecfdda9d2010-04-21 15:30:06 -0700510 mhp->rhp = rhp;
511
512 mhp->umem = ib_umem_get(pd->uobject->context, start, length, acc, 0);
513 if (IS_ERR(mhp->umem)) {
514 err = PTR_ERR(mhp->umem);
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530515 kfree_skb(mhp->dereg_skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700516 kfree(mhp);
517 return ERR_PTR(err);
518 }
519
520 shift = ffs(mhp->umem->page_size) - 1;
521
Yishai Hadaseeb84612014-01-28 13:40:15 +0200522 n = mhp->umem->nmap;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700523 err = alloc_pbl(mhp, n);
524 if (err)
525 goto err;
526
527 pages = (__be64 *) __get_free_page(GFP_KERNEL);
528 if (!pages) {
529 err = -ENOMEM;
530 goto err_pbl;
531 }
532
533 i = n = 0;
534
Yishai Hadaseeb84612014-01-28 13:40:15 +0200535 for_each_sg(mhp->umem->sg_head.sgl, sg, mhp->umem->nmap, entry) {
536 len = sg_dma_len(sg) >> shift;
537 for (k = 0; k < len; ++k) {
538 pages[i++] = cpu_to_be64(sg_dma_address(sg) +
539 mhp->umem->page_size * k);
540 if (i == PAGE_SIZE / sizeof *pages) {
541 err = write_pbl(&mhp->rhp->rdev,
542 pages,
543 mhp->attr.pbl_addr + (n << 3), i);
544 if (err)
545 goto pbl_done;
546 n += i;
547 i = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700548 }
549 }
Yishai Hadaseeb84612014-01-28 13:40:15 +0200550 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700551
552 if (i)
553 err = write_pbl(&mhp->rhp->rdev, pages,
554 mhp->attr.pbl_addr + (n << 3), i);
555
556pbl_done:
557 free_page((unsigned long) pages);
558 if (err)
559 goto err_pbl;
560
561 mhp->attr.pdid = php->pdid;
562 mhp->attr.zbva = 0;
563 mhp->attr.perms = c4iw_ib_to_tpt_access(acc);
564 mhp->attr.va_fbo = virt;
565 mhp->attr.page_size = shift - 12;
Steve Wise301c2c32011-06-14 20:59:21 +0000566 mhp->attr.len = length;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700567
568 err = register_mem(rhp, php, mhp, shift);
569 if (err)
570 goto err_pbl;
571
572 return &mhp->ibmr;
573
574err_pbl:
575 c4iw_pblpool_free(&mhp->rhp->rdev, mhp->attr.pbl_addr,
576 mhp->attr.pbl_size << 3);
577
578err:
579 ib_umem_release(mhp->umem);
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530580 kfree_skb(mhp->dereg_skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700581 kfree(mhp);
582 return ERR_PTR(err);
583}
584
Matan Barakb2a239d2016-02-29 18:05:29 +0200585struct ib_mw *c4iw_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
586 struct ib_udata *udata)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700587{
588 struct c4iw_dev *rhp;
589 struct c4iw_pd *php;
590 struct c4iw_mw *mhp;
591 u32 mmid;
592 u32 stag = 0;
593 int ret;
594
Shani Michaeli7083e422013-02-06 16:19:12 +0000595 if (type != IB_MW_TYPE_1)
596 return ERR_PTR(-EINVAL);
597
Steve Wisecfdda9d2010-04-21 15:30:06 -0700598 php = to_c4iw_pd(pd);
599 rhp = php->rhp;
600 mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
601 if (!mhp)
602 return ERR_PTR(-ENOMEM);
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530603
604 mhp->dereg_skb = alloc_skb(SGE_MAX_WR_LEN, GFP_KERNEL);
605 if (!mhp->dereg_skb) {
Hariprasad S56b2eca2016-06-30 11:44:33 +0530606 ret = -ENOMEM;
607 goto free_mhp;
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530608 }
609
Steve Wisecfdda9d2010-04-21 15:30:06 -0700610 ret = allocate_window(&rhp->rdev, &stag, php->pdid);
Hariprasad S56b2eca2016-06-30 11:44:33 +0530611 if (ret)
612 goto free_skb;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700613 mhp->rhp = rhp;
614 mhp->attr.pdid = php->pdid;
615 mhp->attr.type = FW_RI_STAG_MW;
616 mhp->attr.stag = stag;
617 mmid = (stag) >> 8;
618 mhp->ibmw.rkey = stag;
619 if (insert_handle(rhp, &rhp->mmidr, mhp, mmid)) {
Hariprasad S56b2eca2016-06-30 11:44:33 +0530620 ret = -ENOMEM;
621 goto dealloc_win;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700622 }
623 PDBG("%s mmid 0x%x mhp %p stag 0x%x\n", __func__, mmid, mhp, stag);
624 return &(mhp->ibmw);
Hariprasad S56b2eca2016-06-30 11:44:33 +0530625
626dealloc_win:
627 deallocate_window(&rhp->rdev, mhp->attr.stag, mhp->dereg_skb);
628free_skb:
629 kfree_skb(mhp->dereg_skb);
630free_mhp:
631 kfree(mhp);
632 return ERR_PTR(ret);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700633}
634
635int c4iw_dealloc_mw(struct ib_mw *mw)
636{
637 struct c4iw_dev *rhp;
638 struct c4iw_mw *mhp;
639 u32 mmid;
640
641 mhp = to_c4iw_mw(mw);
642 rhp = mhp->rhp;
643 mmid = (mw->rkey) >> 8;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700644 remove_handle(rhp, &rhp->mmidr, mmid);
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530645 deallocate_window(&rhp->rdev, mhp->attr.stag, mhp->dereg_skb);
Hariprasad S56b2eca2016-06-30 11:44:33 +0530646 kfree_skb(mhp->dereg_skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700647 kfree(mhp);
648 PDBG("%s ib_mw %p mmid 0x%x ptr %p\n", __func__, mw, mmid, mhp);
649 return 0;
650}
651
Sagi Grimberga2164032015-07-30 10:32:44 +0300652struct ib_mr *c4iw_alloc_mr(struct ib_pd *pd,
653 enum ib_mr_type mr_type,
654 u32 max_num_sg)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700655{
656 struct c4iw_dev *rhp;
657 struct c4iw_pd *php;
658 struct c4iw_mr *mhp;
659 u32 mmid;
660 u32 stag = 0;
661 int ret = 0;
Sagi Grimberg8376b862015-10-13 19:11:30 +0300662 int length = roundup(max_num_sg * sizeof(u64), 32);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700663
664 php = to_c4iw_pd(pd);
665 rhp = php->rhp;
Hariprasad See30f7d2016-02-12 16:10:35 +0530666
667 if (mr_type != IB_MR_TYPE_MEM_REG ||
668 max_num_sg > t4_max_fr_depth(&rhp->rdev.lldi.ulptx_memwrite_dsgl &&
669 use_dsgl))
670 return ERR_PTR(-EINVAL);
671
Steve Wisecfdda9d2010-04-21 15:30:06 -0700672 mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
Steve Wise841dba92010-05-20 16:57:54 -0500673 if (!mhp) {
674 ret = -ENOMEM;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700675 goto err;
Steve Wise841dba92010-05-20 16:57:54 -0500676 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700677
Sagi Grimberg8376b862015-10-13 19:11:30 +0300678 mhp->mpl = dma_alloc_coherent(&rhp->rdev.lldi.pdev->dev,
679 length, &mhp->mpl_addr, GFP_KERNEL);
680 if (!mhp->mpl) {
681 ret = -ENOMEM;
682 goto err_mpl;
683 }
684 mhp->max_mpl_len = length;
685
Steve Wisecfdda9d2010-04-21 15:30:06 -0700686 mhp->rhp = rhp;
Sagi Grimberga2164032015-07-30 10:32:44 +0300687 ret = alloc_pbl(mhp, max_num_sg);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700688 if (ret)
689 goto err1;
Sagi Grimberga2164032015-07-30 10:32:44 +0300690 mhp->attr.pbl_size = max_num_sg;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700691 ret = allocate_stag(&rhp->rdev, &stag, php->pdid,
692 mhp->attr.pbl_size, mhp->attr.pbl_addr);
693 if (ret)
694 goto err2;
695 mhp->attr.pdid = php->pdid;
696 mhp->attr.type = FW_RI_STAG_NSMR;
697 mhp->attr.stag = stag;
698 mhp->attr.state = 1;
699 mmid = (stag) >> 8;
700 mhp->ibmr.rkey = mhp->ibmr.lkey = stag;
Steve Wise841dba92010-05-20 16:57:54 -0500701 if (insert_handle(rhp, &rhp->mmidr, mhp, mmid)) {
702 ret = -ENOMEM;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700703 goto err3;
Steve Wise841dba92010-05-20 16:57:54 -0500704 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700705
706 PDBG("%s mmid 0x%x mhp %p stag 0x%x\n", __func__, mmid, mhp, stag);
707 return &(mhp->ibmr);
708err3:
709 dereg_mem(&rhp->rdev, stag, mhp->attr.pbl_size,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530710 mhp->attr.pbl_addr, mhp->dereg_skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700711err2:
712 c4iw_pblpool_free(&mhp->rhp->rdev, mhp->attr.pbl_addr,
713 mhp->attr.pbl_size << 3);
714err1:
Sagi Grimberg8376b862015-10-13 19:11:30 +0300715 dma_free_coherent(&mhp->rhp->rdev.lldi.pdev->dev,
716 mhp->max_mpl_len, mhp->mpl, mhp->mpl_addr);
717err_mpl:
Steve Wisecfdda9d2010-04-21 15:30:06 -0700718 kfree(mhp);
719err:
720 return ERR_PTR(ret);
721}
722
Sagi Grimberg8376b862015-10-13 19:11:30 +0300723static int c4iw_set_page(struct ib_mr *ibmr, u64 addr)
724{
725 struct c4iw_mr *mhp = to_c4iw_mr(ibmr);
726
727 if (unlikely(mhp->mpl_len == mhp->max_mpl_len))
728 return -ENOMEM;
729
730 mhp->mpl[mhp->mpl_len++] = addr;
731
732 return 0;
733}
734
Christoph Hellwigff2ba992016-05-03 18:01:04 +0200735int c4iw_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
Bart Van Assche9aa8b322016-05-12 10:49:15 -0700736 unsigned int *sg_offset)
Sagi Grimberg8376b862015-10-13 19:11:30 +0300737{
738 struct c4iw_mr *mhp = to_c4iw_mr(ibmr);
739
740 mhp->mpl_len = 0;
741
Christoph Hellwigff2ba992016-05-03 18:01:04 +0200742 return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, c4iw_set_page);
Sagi Grimberg8376b862015-10-13 19:11:30 +0300743}
744
Steve Wisecfdda9d2010-04-21 15:30:06 -0700745int c4iw_dereg_mr(struct ib_mr *ib_mr)
746{
747 struct c4iw_dev *rhp;
748 struct c4iw_mr *mhp;
749 u32 mmid;
750
751 PDBG("%s ib_mr %p\n", __func__, ib_mr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700752
753 mhp = to_c4iw_mr(ib_mr);
754 rhp = mhp->rhp;
755 mmid = mhp->attr.stag >> 8;
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530756 remove_handle(rhp, &rhp->mmidr, mmid);
Sagi Grimberg8376b862015-10-13 19:11:30 +0300757 if (mhp->mpl)
758 dma_free_coherent(&mhp->rhp->rdev.lldi.pdev->dev,
759 mhp->max_mpl_len, mhp->mpl, mhp->mpl_addr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700760 dereg_mem(&rhp->rdev, mhp->attr.stag, mhp->attr.pbl_size,
Hariprasad S0f8ab0b2016-06-10 01:05:16 +0530761 mhp->attr.pbl_addr, mhp->dereg_skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700762 if (mhp->attr.pbl_size)
763 c4iw_pblpool_free(&mhp->rhp->rdev, mhp->attr.pbl_addr,
764 mhp->attr.pbl_size << 3);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700765 if (mhp->kva)
766 kfree((void *) (unsigned long) mhp->kva);
767 if (mhp->umem)
768 ib_umem_release(mhp->umem);
769 PDBG("%s mmid 0x%x ptr %p\n", __func__, mmid, mhp);
770 kfree(mhp);
771 return 0;
772}