blob: 347fe18b1a41c0990c753aa29f2efaaea0cd119b [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 */
Paul Gortmakere4dd23d2011-05-27 15:35:46 -040032
33#include <linux/module.h>
34
Steve Wisecfdda9d2010-04-21 15:30:06 -070035#include "iw_cxgb4.h"
36
Vipul Pandya2c974782012-05-18 15:29:28 +053037static int db_delay_usecs = 1;
38module_param(db_delay_usecs, int, 0644);
39MODULE_PARM_DESC(db_delay_usecs, "Usecs to delay awaiting db fifo to drain");
40
Steve Wisea9c77192011-03-11 22:30:11 +000041static int ocqp_support = 1;
Steve Wisec6d7b262010-09-13 11:23:57 -050042module_param(ocqp_support, int, 0644);
Steve Wisea9c77192011-03-11 22:30:11 +000043MODULE_PARM_DESC(ocqp_support, "Support on-chip SQs (default=1)");
Steve Wisec6d7b262010-09-13 11:23:57 -050044
Vipul Pandya3cbdb922013-03-14 05:08:59 +000045int db_fc_threshold = 1000;
Vipul Pandya422eea02012-05-18 15:29:30 +053046module_param(db_fc_threshold, int, 0644);
Vipul Pandya3cbdb922013-03-14 05:08:59 +000047MODULE_PARM_DESC(db_fc_threshold,
48 "QP count/threshold that triggers"
49 " automatic db flow control mode (default = 1000)");
50
51int db_coalescing_threshold;
52module_param(db_coalescing_threshold, int, 0644);
53MODULE_PARM_DESC(db_coalescing_threshold,
54 "QP count/threshold that triggers"
55 " disabling db coalescing (default = 0)");
Vipul Pandya422eea02012-05-18 15:29:30 +053056
Vipul Pandya42b6a942013-03-14 05:09:01 +000057static int max_fr_immd = T4_MAX_FR_IMMD;
58module_param(max_fr_immd, int, 0644);
59MODULE_PARM_DESC(max_fr_immd, "fastreg threshold for using DSGL instead of immedate");
60
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +053061static int alloc_ird(struct c4iw_dev *dev, u32 ird)
62{
63 int ret = 0;
64
65 spin_lock_irq(&dev->lock);
66 if (ird <= dev->avail_ird)
67 dev->avail_ird -= ird;
68 else
69 ret = -ENOMEM;
70 spin_unlock_irq(&dev->lock);
71
72 if (ret)
73 dev_warn(&dev->rdev.lldi.pdev->dev,
74 "device IRD resources exhausted\n");
75
76 return ret;
77}
78
79static void free_ird(struct c4iw_dev *dev, int ird)
80{
81 spin_lock_irq(&dev->lock);
82 dev->avail_ird += ird;
83 spin_unlock_irq(&dev->lock);
84}
85
Steve Wise2f5b48c2010-09-10 11:15:36 -050086static void set_state(struct c4iw_qp *qhp, enum c4iw_qp_state state)
87{
88 unsigned long flag;
89 spin_lock_irqsave(&qhp->lock, flag);
90 qhp->attr.state = state;
91 spin_unlock_irqrestore(&qhp->lock, flag);
92}
93
Steve Wisec6d7b262010-09-13 11:23:57 -050094static void dealloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
95{
96 c4iw_ocqp_pool_free(rdev, sq->dma_addr, sq->memsize);
97}
98
99static void dealloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
100{
101 dma_free_coherent(&(rdev->lldi.pdev->dev), sq->memsize, sq->queue,
102 pci_unmap_addr(sq, mapping));
103}
104
105static void dealloc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
106{
107 if (t4_sq_onchip(sq))
108 dealloc_oc_sq(rdev, sq);
109 else
110 dealloc_host_sq(rdev, sq);
111}
112
113static int alloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
114{
Vipul Pandyaf079af72013-03-14 05:08:58 +0000115 if (!ocqp_support || !ocqp_supported(&rdev->lldi))
Steve Wisec6d7b262010-09-13 11:23:57 -0500116 return -ENOSYS;
117 sq->dma_addr = c4iw_ocqp_pool_alloc(rdev, sq->memsize);
118 if (!sq->dma_addr)
119 return -ENOMEM;
120 sq->phys_addr = rdev->oc_mw_pa + sq->dma_addr -
121 rdev->lldi.vr->ocq.start;
122 sq->queue = (__force union t4_wr *)(rdev->oc_mw_kva + sq->dma_addr -
123 rdev->lldi.vr->ocq.start);
124 sq->flags |= T4_SQ_ONCHIP;
125 return 0;
126}
127
128static int alloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
129{
130 sq->queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev), sq->memsize,
131 &(sq->dma_addr), GFP_KERNEL);
132 if (!sq->queue)
133 return -ENOMEM;
134 sq->phys_addr = virt_to_phys(sq->queue);
135 pci_unmap_addr_set(sq, mapping, sq->dma_addr);
136 return 0;
137}
138
Thadeu Lima de Souza Cascardo5b0c2752013-04-01 20:13:39 +0000139static int alloc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq, int user)
140{
141 int ret = -ENOSYS;
142 if (user)
143 ret = alloc_oc_sq(rdev, sq);
144 if (ret)
145 ret = alloc_host_sq(rdev, sq);
146 return ret;
147}
148
Steve Wisecfdda9d2010-04-21 15:30:06 -0700149static int destroy_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530150 struct c4iw_dev_ucontext *uctx, int has_rq)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700151{
152 /*
153 * uP clears EQ contexts when the connection exits rdma mode,
154 * so no need to post a RESET WR for these EQs.
155 */
Steve Wisec6d7b262010-09-13 11:23:57 -0500156 dealloc_sq(rdev, &wq->sq);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700157 kfree(wq->sq.sw_sq);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700158 c4iw_put_qpid(rdev, wq->sq.qid, uctx);
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530159
160 if (has_rq) {
161 dma_free_coherent(&rdev->lldi.pdev->dev,
162 wq->rq.memsize, wq->rq.queue,
163 dma_unmap_addr(&wq->rq, mapping));
164 c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
165 kfree(wq->rq.sw_rq);
166 c4iw_put_qpid(rdev, wq->rq.qid, uctx);
167 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700168 return 0;
169}
170
Hariprasad S74217d42015-06-09 18:23:12 +0530171/*
172 * Determine the BAR2 virtual address and qid. If pbar2_pa is not NULL,
173 * then this is a user mapping so compute the page-aligned physical address
174 * for mapping.
175 */
176void __iomem *c4iw_bar2_addrs(struct c4iw_rdev *rdev, unsigned int qid,
177 enum cxgb4_bar2_qtype qtype,
178 unsigned int *pbar2_qid, u64 *pbar2_pa)
179{
180 u64 bar2_qoffset;
181 int ret;
182
183 ret = cxgb4_bar2_sge_qregs(rdev->lldi.ports[0], qid, qtype,
184 pbar2_pa ? 1 : 0,
185 &bar2_qoffset, pbar2_qid);
186 if (ret)
187 return NULL;
188
189 if (pbar2_pa)
190 *pbar2_pa = (rdev->bar2_pa + bar2_qoffset) & PAGE_MASK;
Hariprasad S32cc92c2016-04-05 10:23:48 +0530191
192 if (is_t4(rdev->lldi.adapter_type))
193 return NULL;
194
Hariprasad S74217d42015-06-09 18:23:12 +0530195 return rdev->bar2_kva + bar2_qoffset;
196}
197
Steve Wisecfdda9d2010-04-21 15:30:06 -0700198static int create_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
199 struct t4_cq *rcq, struct t4_cq *scq,
Steve Wise7088a9b2017-09-26 13:11:36 -0700200 struct c4iw_dev_ucontext *uctx,
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530201 struct c4iw_wr_wait *wr_waitp,
202 int need_rq)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700203{
204 int user = (uctx != &rdev->uctx);
205 struct fw_ri_res_wr *res_wr;
206 struct fw_ri_res *res;
207 int wr_len;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700208 struct sk_buff *skb;
Vipul Pandya9919d5b2013-03-14 05:09:04 +0000209 int ret = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700210 int eqsize;
211
212 wq->sq.qid = c4iw_get_qpid(rdev, uctx);
213 if (!wq->sq.qid)
214 return -ENOMEM;
215
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530216 if (need_rq) {
217 wq->rq.qid = c4iw_get_qpid(rdev, uctx);
218 if (!wq->rq.qid) {
219 ret = -ENOMEM;
220 goto free_sq_qid;
221 }
Emil Goodec079c282012-08-19 17:59:40 +0000222 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700223
224 if (!user) {
Kees Cook6396bb22018-06-12 14:03:40 -0700225 wq->sq.sw_sq = kcalloc(wq->sq.size, sizeof(*wq->sq.sw_sq),
226 GFP_KERNEL);
Emil Goodec079c282012-08-19 17:59:40 +0000227 if (!wq->sq.sw_sq) {
228 ret = -ENOMEM;
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530229 goto free_rq_qid;//FIXME
Emil Goodec079c282012-08-19 17:59:40 +0000230 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700231
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530232 if (need_rq) {
233 wq->rq.sw_rq = kcalloc(wq->rq.size,
234 sizeof(*wq->rq.sw_rq),
235 GFP_KERNEL);
236 if (!wq->rq.sw_rq) {
237 ret = -ENOMEM;
238 goto free_sw_sq;
239 }
Emil Goodec079c282012-08-19 17:59:40 +0000240 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700241 }
242
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530243 if (need_rq) {
244 /*
245 * RQT must be a power of 2 and at least 16 deep.
246 */
247 wq->rq.rqt_size =
248 roundup_pow_of_two(max_t(u16, wq->rq.size, 16));
249 wq->rq.rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rq.rqt_size);
250 if (!wq->rq.rqt_hwaddr) {
251 ret = -ENOMEM;
252 goto free_sw_rq;
253 }
Emil Goodec079c282012-08-19 17:59:40 +0000254 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700255
Thadeu Lima de Souza Cascardo5b0c2752013-04-01 20:13:39 +0000256 ret = alloc_sq(rdev, &wq->sq, user);
257 if (ret)
258 goto free_hwaddr;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700259 memset(wq->sq.queue, 0, wq->sq.memsize);
FUJITA Tomonorif38926a2010-06-03 05:37:50 +0000260 dma_unmap_addr_set(&wq->sq, mapping, wq->sq.dma_addr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700261
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530262 if (need_rq) {
263 wq->rq.queue = dma_alloc_coherent(&rdev->lldi.pdev->dev,
264 wq->rq.memsize,
265 &wq->rq.dma_addr,
266 GFP_KERNEL);
267 if (!wq->rq.queue) {
268 ret = -ENOMEM;
269 goto free_sq;
270 }
271 pr_debug("sq base va 0x%p pa 0x%llx rq base va 0x%p pa 0x%llx\n",
272 wq->sq.queue,
273 (unsigned long long)virt_to_phys(wq->sq.queue),
274 wq->rq.queue,
275 (unsigned long long)virt_to_phys(wq->rq.queue));
276 memset(wq->rq.queue, 0, wq->rq.memsize);
277 dma_unmap_addr_set(&wq->rq, mapping, wq->rq.dma_addr);
Wei Yongjun55e57a72013-03-15 09:42:12 +0000278 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700279
280 wq->db = rdev->lldi.db_reg;
Steve Wisefa658a92014-04-09 09:38:25 -0500281
Hariprasad S74217d42015-06-09 18:23:12 +0530282 wq->sq.bar2_va = c4iw_bar2_addrs(rdev, wq->sq.qid, T4_BAR2_QTYPE_EGRESS,
283 &wq->sq.bar2_qid,
284 user ? &wq->sq.bar2_pa : NULL);
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530285 if (need_rq)
286 wq->rq.bar2_va = c4iw_bar2_addrs(rdev, wq->rq.qid,
287 T4_BAR2_QTYPE_EGRESS,
288 &wq->rq.bar2_qid,
289 user ? &wq->rq.bar2_pa : NULL);
Hariprasad S74217d42015-06-09 18:23:12 +0530290
291 /*
292 * User mode must have bar2 access.
293 */
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530294 if (user && (!wq->sq.bar2_pa || (need_rq && !wq->rq.bar2_pa))) {
Joe Perches700456b2017-02-09 14:23:50 -0800295 pr_warn("%s: sqid %u or rqid %u not in BAR2 range\n",
Hariprasad S74217d42015-06-09 18:23:12 +0530296 pci_name(rdev->lldi.pdev), wq->sq.qid, wq->rq.qid);
297 goto free_dma;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700298 }
Hariprasad S74217d42015-06-09 18:23:12 +0530299
Steve Wisecfdda9d2010-04-21 15:30:06 -0700300 wq->rdev = rdev;
301 wq->rq.msn = 1;
302
303 /* build fw_ri_res_wr */
304 wr_len = sizeof *res_wr + 2 * sizeof *res;
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530305 if (need_rq)
306 wr_len += sizeof(*res);
David Rientjesd3c814e2010-07-21 02:44:56 +0000307 skb = alloc_skb(wr_len, GFP_KERNEL);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700308 if (!skb) {
309 ret = -ENOMEM;
Emil Goodec079c282012-08-19 17:59:40 +0000310 goto free_dma;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700311 }
312 set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
313
yuan linyude77b962017-06-18 22:48:17 +0800314 res_wr = __skb_put_zero(skb, wr_len);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700315 res_wr->op_nres = cpu_to_be32(
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530316 FW_WR_OP_V(FW_RI_RES_WR) |
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530317 FW_RI_RES_WR_NRES_V(need_rq ? 2 : 1) |
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530318 FW_WR_COMPL_F);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700319 res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
Steve Wise7088a9b2017-09-26 13:11:36 -0700320 res_wr->cookie = (uintptr_t)wr_waitp;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700321 res = res_wr->res;
322 res->u.sqrq.restype = FW_RI_RES_TYPE_SQ;
323 res->u.sqrq.op = FW_RI_RES_OP_WRITE;
324
325 /*
326 * eqsize is the number of 64B entries plus the status page size.
327 */
Hariprasad Shenai04e10e22014-07-14 21:34:51 +0530328 eqsize = wq->sq.size * T4_SQ_NUM_SLOTS +
329 rdev->hw_queue.t4_eq_status_entries;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700330
331 res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530332 FW_RI_RES_WR_HOSTFCMODE_V(0) | /* no host cidx updates */
333 FW_RI_RES_WR_CPRIO_V(0) | /* don't keep in chip cache */
334 FW_RI_RES_WR_PCIECHN_V(0) | /* set by uP at ri_init time */
335 (t4_sq_onchip(&wq->sq) ? FW_RI_RES_WR_ONCHIP_F : 0) |
336 FW_RI_RES_WR_IQID_V(scq->cqid));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700337 res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530338 FW_RI_RES_WR_DCAEN_V(0) |
339 FW_RI_RES_WR_DCACPU_V(0) |
340 FW_RI_RES_WR_FBMIN_V(2) |
Steve Wiseb414fa02016-12-15 08:09:35 -0800341 (t4_sq_onchip(&wq->sq) ? FW_RI_RES_WR_FBMAX_V(2) :
342 FW_RI_RES_WR_FBMAX_V(3)) |
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530343 FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
344 FW_RI_RES_WR_CIDXFTHRESH_V(0) |
345 FW_RI_RES_WR_EQSIZE_V(eqsize));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700346 res->u.sqrq.eqid = cpu_to_be32(wq->sq.qid);
347 res->u.sqrq.eqaddr = cpu_to_be64(wq->sq.dma_addr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700348
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530349 if (need_rq) {
350 res++;
351 res->u.sqrq.restype = FW_RI_RES_TYPE_RQ;
352 res->u.sqrq.op = FW_RI_RES_OP_WRITE;
353
354 /*
355 * eqsize is the number of 64B entries plus the status page size
356 */
357 eqsize = wq->rq.size * T4_RQ_NUM_SLOTS +
358 rdev->hw_queue.t4_eq_status_entries;
359 res->u.sqrq.fetchszm_to_iqid =
360 /* no host cidx updates */
361 cpu_to_be32(FW_RI_RES_WR_HOSTFCMODE_V(0) |
362 /* don't keep in chip cache */
363 FW_RI_RES_WR_CPRIO_V(0) |
364 /* set by uP at ri_init time */
365 FW_RI_RES_WR_PCIECHN_V(0) |
366 FW_RI_RES_WR_IQID_V(rcq->cqid));
367 res->u.sqrq.dcaen_to_eqsize =
368 cpu_to_be32(FW_RI_RES_WR_DCAEN_V(0) |
369 FW_RI_RES_WR_DCACPU_V(0) |
370 FW_RI_RES_WR_FBMIN_V(2) |
371 FW_RI_RES_WR_FBMAX_V(3) |
372 FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
373 FW_RI_RES_WR_CIDXFTHRESH_V(0) |
374 FW_RI_RES_WR_EQSIZE_V(eqsize));
375 res->u.sqrq.eqid = cpu_to_be32(wq->rq.qid);
376 res->u.sqrq.eqaddr = cpu_to_be64(wq->rq.dma_addr);
377 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700378
Steve Wise7088a9b2017-09-26 13:11:36 -0700379 c4iw_init_wr_wait(wr_waitp);
Steve Wise2015f262017-09-26 13:13:17 -0700380 ret = c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, wq->sq.qid, __func__);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700381 if (ret)
Emil Goodec079c282012-08-19 17:59:40 +0000382 goto free_dma;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700383
Bharat Potnuri548ddb12017-09-27 13:05:49 +0530384 pr_debug("sqid 0x%x rqid 0x%x kdb 0x%p sq_bar2_addr %p rq_bar2_addr %p\n",
385 wq->sq.qid, wq->rq.qid, wq->db,
Joe Perchesa9a42882017-02-09 14:23:51 -0800386 wq->sq.bar2_va, wq->rq.bar2_va);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700387
388 return 0;
Emil Goodec079c282012-08-19 17:59:40 +0000389free_dma:
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530390 if (need_rq)
391 dma_free_coherent(&rdev->lldi.pdev->dev,
392 wq->rq.memsize, wq->rq.queue,
393 dma_unmap_addr(&wq->rq, mapping));
Emil Goodec079c282012-08-19 17:59:40 +0000394free_sq:
Steve Wisec6d7b262010-09-13 11:23:57 -0500395 dealloc_sq(rdev, &wq->sq);
Emil Goodec079c282012-08-19 17:59:40 +0000396free_hwaddr:
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530397 if (need_rq)
398 c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
Emil Goodec079c282012-08-19 17:59:40 +0000399free_sw_rq:
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530400 if (need_rq)
401 kfree(wq->rq.sw_rq);
Emil Goodec079c282012-08-19 17:59:40 +0000402free_sw_sq:
Steve Wisecfdda9d2010-04-21 15:30:06 -0700403 kfree(wq->sq.sw_sq);
Emil Goodec079c282012-08-19 17:59:40 +0000404free_rq_qid:
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530405 if (need_rq)
406 c4iw_put_qpid(rdev, wq->rq.qid, uctx);
Emil Goodec079c282012-08-19 17:59:40 +0000407free_sq_qid:
Steve Wisecfdda9d2010-04-21 15:30:06 -0700408 c4iw_put_qpid(rdev, wq->sq.qid, uctx);
Emil Goodec079c282012-08-19 17:59:40 +0000409 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700410}
411
Steve Wised37ac312010-06-10 19:03:00 +0000412static int build_immd(struct t4_sq *sq, struct fw_ri_immd *immdp,
Bart Van Asschef696bf62018-07-18 09:25:14 -0700413 const struct ib_send_wr *wr, int max, u32 *plenp)
Steve Wised37ac312010-06-10 19:03:00 +0000414{
415 u8 *dstp, *srcp;
416 u32 plen = 0;
417 int i;
418 int rem, len;
419
420 dstp = (u8 *)immdp->data;
421 for (i = 0; i < wr->num_sge; i++) {
422 if ((plen + wr->sg_list[i].length) > max)
423 return -EMSGSIZE;
424 srcp = (u8 *)(unsigned long)wr->sg_list[i].addr;
425 plen += wr->sg_list[i].length;
426 rem = wr->sg_list[i].length;
427 while (rem) {
428 if (dstp == (u8 *)&sq->queue[sq->size])
429 dstp = (u8 *)sq->queue;
430 if (rem <= (u8 *)&sq->queue[sq->size] - dstp)
431 len = rem;
432 else
433 len = (u8 *)&sq->queue[sq->size] - dstp;
434 memcpy(dstp, srcp, len);
435 dstp += len;
436 srcp += len;
437 rem -= len;
438 }
439 }
Steve Wise13fecb82010-09-10 11:14:53 -0500440 len = roundup(plen + sizeof *immdp, 16) - (plen + sizeof *immdp);
441 if (len)
442 memset(dstp, 0, len);
Steve Wised37ac312010-06-10 19:03:00 +0000443 immdp->op = FW_RI_DATA_IMMD;
444 immdp->r1 = 0;
445 immdp->r2 = 0;
446 immdp->immdlen = cpu_to_be32(plen);
447 *plenp = plen;
448 return 0;
449}
450
451static int build_isgl(__be64 *queue_start, __be64 *queue_end,
452 struct fw_ri_isgl *isglp, struct ib_sge *sg_list,
453 int num_sge, u32 *plenp)
454
Steve Wisecfdda9d2010-04-21 15:30:06 -0700455{
456 int i;
Steve Wised37ac312010-06-10 19:03:00 +0000457 u32 plen = 0;
Potnuri Bharat Teja94245f42018-08-02 11:33:04 +0530458 __be64 *flitp;
459
460 if ((__be64 *)isglp == queue_end)
461 isglp = (struct fw_ri_isgl *)queue_start;
462
463 flitp = (__be64 *)isglp->sge;
Steve Wised37ac312010-06-10 19:03:00 +0000464
465 for (i = 0; i < num_sge; i++) {
466 if ((plen + sg_list[i].length) < plen)
467 return -EMSGSIZE;
468 plen += sg_list[i].length;
469 *flitp = cpu_to_be64(((u64)sg_list[i].lkey << 32) |
470 sg_list[i].length);
471 if (++flitp == queue_end)
472 flitp = queue_start;
473 *flitp = cpu_to_be64(sg_list[i].addr);
474 if (++flitp == queue_end)
475 flitp = queue_start;
476 }
Steve Wise13fecb82010-09-10 11:14:53 -0500477 *flitp = (__force __be64)0;
Steve Wised37ac312010-06-10 19:03:00 +0000478 isglp->op = FW_RI_DATA_ISGL;
479 isglp->r1 = 0;
480 isglp->nsge = cpu_to_be16(num_sge);
481 isglp->r2 = 0;
482 if (plenp)
483 *plenp = plen;
484 return 0;
485}
486
487static int build_rdma_send(struct t4_sq *sq, union t4_wr *wqe,
Bart Van Asschef696bf62018-07-18 09:25:14 -0700488 const struct ib_send_wr *wr, u8 *len16)
Steve Wised37ac312010-06-10 19:03:00 +0000489{
Steve Wisecfdda9d2010-04-21 15:30:06 -0700490 u32 plen;
491 int size;
Steve Wised37ac312010-06-10 19:03:00 +0000492 int ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700493
494 if (wr->num_sge > T4_MAX_SEND_SGE)
495 return -EINVAL;
496 switch (wr->opcode) {
497 case IB_WR_SEND:
498 if (wr->send_flags & IB_SEND_SOLICITED)
499 wqe->send.sendop_pkd = cpu_to_be32(
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530500 FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_SE));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700501 else
502 wqe->send.sendop_pkd = cpu_to_be32(
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530503 FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700504 wqe->send.stag_inv = 0;
505 break;
506 case IB_WR_SEND_WITH_INV:
507 if (wr->send_flags & IB_SEND_SOLICITED)
508 wqe->send.sendop_pkd = cpu_to_be32(
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530509 FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_SE_INV));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700510 else
511 wqe->send.sendop_pkd = cpu_to_be32(
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +0530512 FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_INV));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700513 wqe->send.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
514 break;
515
516 default:
517 return -EINVAL;
518 }
Steve Wisec3f98fa2014-04-09 09:38:27 -0500519 wqe->send.r3 = 0;
520 wqe->send.r4 = 0;
Steve Wised37ac312010-06-10 19:03:00 +0000521
Steve Wisecfdda9d2010-04-21 15:30:06 -0700522 plen = 0;
523 if (wr->num_sge) {
524 if (wr->send_flags & IB_SEND_INLINE) {
Steve Wised37ac312010-06-10 19:03:00 +0000525 ret = build_immd(sq, wqe->send.u.immd_src, wr,
526 T4_MAX_SEND_INLINE, &plen);
527 if (ret)
528 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700529 size = sizeof wqe->send + sizeof(struct fw_ri_immd) +
530 plen;
531 } else {
Steve Wised37ac312010-06-10 19:03:00 +0000532 ret = build_isgl((__be64 *)sq->queue,
533 (__be64 *)&sq->queue[sq->size],
534 wqe->send.u.isgl_src,
535 wr->sg_list, wr->num_sge, &plen);
536 if (ret)
537 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700538 size = sizeof wqe->send + sizeof(struct fw_ri_isgl) +
539 wr->num_sge * sizeof(struct fw_ri_sge);
540 }
541 } else {
542 wqe->send.u.immd_src[0].op = FW_RI_DATA_IMMD;
543 wqe->send.u.immd_src[0].r1 = 0;
544 wqe->send.u.immd_src[0].r2 = 0;
545 wqe->send.u.immd_src[0].immdlen = 0;
546 size = sizeof wqe->send + sizeof(struct fw_ri_immd);
Steve Wised37ac312010-06-10 19:03:00 +0000547 plen = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700548 }
549 *len16 = DIV_ROUND_UP(size, 16);
550 wqe->send.plen = cpu_to_be32(plen);
551 return 0;
552}
553
Steve Wised37ac312010-06-10 19:03:00 +0000554static int build_rdma_write(struct t4_sq *sq, union t4_wr *wqe,
Bart Van Asschef696bf62018-07-18 09:25:14 -0700555 const struct ib_send_wr *wr, u8 *len16)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700556{
Steve Wisecfdda9d2010-04-21 15:30:06 -0700557 u32 plen;
558 int size;
Steve Wised37ac312010-06-10 19:03:00 +0000559 int ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700560
Steve Wised37ac312010-06-10 19:03:00 +0000561 if (wr->num_sge > T4_MAX_SEND_SGE)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700562 return -EINVAL;
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +0530563
564 /*
565 * iWARP protocol supports 64 bit immediate data but rdma api
566 * limits it to 32bit.
567 */
568 if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
569 wqe->write.iw_imm_data.ib_imm_data.imm_data32 = wr->ex.imm_data;
570 else
571 wqe->write.iw_imm_data.ib_imm_data.imm_data32 = 0;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100572 wqe->write.stag_sink = cpu_to_be32(rdma_wr(wr)->rkey);
573 wqe->write.to_sink = cpu_to_be64(rdma_wr(wr)->remote_addr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700574 if (wr->num_sge) {
575 if (wr->send_flags & IB_SEND_INLINE) {
Steve Wised37ac312010-06-10 19:03:00 +0000576 ret = build_immd(sq, wqe->write.u.immd_src, wr,
577 T4_MAX_WRITE_INLINE, &plen);
578 if (ret)
579 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700580 size = sizeof wqe->write + sizeof(struct fw_ri_immd) +
581 plen;
582 } else {
Steve Wised37ac312010-06-10 19:03:00 +0000583 ret = build_isgl((__be64 *)sq->queue,
584 (__be64 *)&sq->queue[sq->size],
585 wqe->write.u.isgl_src,
586 wr->sg_list, wr->num_sge, &plen);
587 if (ret)
588 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700589 size = sizeof wqe->write + sizeof(struct fw_ri_isgl) +
590 wr->num_sge * sizeof(struct fw_ri_sge);
591 }
592 } else {
593 wqe->write.u.immd_src[0].op = FW_RI_DATA_IMMD;
594 wqe->write.u.immd_src[0].r1 = 0;
595 wqe->write.u.immd_src[0].r2 = 0;
596 wqe->write.u.immd_src[0].immdlen = 0;
597 size = sizeof wqe->write + sizeof(struct fw_ri_immd);
Steve Wised37ac312010-06-10 19:03:00 +0000598 plen = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700599 }
600 *len16 = DIV_ROUND_UP(size, 16);
601 wqe->write.plen = cpu_to_be32(plen);
602 return 0;
603}
604
Potnuri Bharat Teja94245f42018-08-02 11:33:04 +0530605static void build_immd_cmpl(struct t4_sq *sq, struct fw_ri_immd_cmpl *immdp,
606 struct ib_send_wr *wr)
607{
608 memcpy((u8 *)immdp->data, (u8 *)(uintptr_t)wr->sg_list->addr, 16);
609 memset(immdp->r1, 0, 6);
610 immdp->op = FW_RI_DATA_IMMD;
611 immdp->immdlen = 16;
612}
613
614static void build_rdma_write_cmpl(struct t4_sq *sq,
615 struct fw_ri_rdma_write_cmpl_wr *wcwr,
616 const struct ib_send_wr *wr, u8 *len16)
617{
618 u32 plen;
619 int size;
620
621 /*
622 * This code assumes the struct fields preceding the write isgl
623 * fit in one 64B WR slot. This is because the WQE is built
624 * directly in the dma queue, and wrapping is only handled
625 * by the code buildling sgls. IE the "fixed part" of the wr
626 * structs must all fit in 64B. The WQE build code should probably be
627 * redesigned to avoid this restriction, but for now just add
628 * the BUILD_BUG_ON() to catch if this WQE struct gets too big.
629 */
630 BUILD_BUG_ON(offsetof(struct fw_ri_rdma_write_cmpl_wr, u) > 64);
631
632 wcwr->stag_sink = cpu_to_be32(rdma_wr(wr)->rkey);
633 wcwr->to_sink = cpu_to_be64(rdma_wr(wr)->remote_addr);
634 wcwr->stag_inv = cpu_to_be32(wr->next->ex.invalidate_rkey);
635 wcwr->r2 = 0;
636 wcwr->r3 = 0;
637
638 /* SEND_INV SGL */
639 if (wr->next->send_flags & IB_SEND_INLINE)
640 build_immd_cmpl(sq, &wcwr->u_cmpl.immd_src, wr->next);
641 else
642 build_isgl((__be64 *)sq->queue, (__be64 *)&sq->queue[sq->size],
643 &wcwr->u_cmpl.isgl_src, wr->next->sg_list, 1, NULL);
644
645 /* WRITE SGL */
646 build_isgl((__be64 *)sq->queue, (__be64 *)&sq->queue[sq->size],
647 wcwr->u.isgl_src, wr->sg_list, wr->num_sge, &plen);
648
649 size = sizeof(*wcwr) + sizeof(struct fw_ri_isgl) +
650 wr->num_sge * sizeof(struct fw_ri_sge);
651 wcwr->plen = cpu_to_be32(plen);
652 *len16 = DIV_ROUND_UP(size, 16);
653}
654
Bart Van Asschef696bf62018-07-18 09:25:14 -0700655static int build_rdma_read(union t4_wr *wqe, const struct ib_send_wr *wr,
656 u8 *len16)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700657{
658 if (wr->num_sge > 1)
659 return -EINVAL;
Ganesh Goudar720336c2017-06-21 19:55:43 +0530660 if (wr->num_sge && wr->sg_list[0].length) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100661 wqe->read.stag_src = cpu_to_be32(rdma_wr(wr)->rkey);
662 wqe->read.to_src_hi = cpu_to_be32((u32)(rdma_wr(wr)->remote_addr
Steve Wisecfdda9d2010-04-21 15:30:06 -0700663 >> 32));
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100664 wqe->read.to_src_lo = cpu_to_be32((u32)rdma_wr(wr)->remote_addr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700665 wqe->read.stag_sink = cpu_to_be32(wr->sg_list[0].lkey);
666 wqe->read.plen = cpu_to_be32(wr->sg_list[0].length);
667 wqe->read.to_sink_hi = cpu_to_be32((u32)(wr->sg_list[0].addr
668 >> 32));
669 wqe->read.to_sink_lo = cpu_to_be32((u32)(wr->sg_list[0].addr));
670 } else {
671 wqe->read.stag_src = cpu_to_be32(2);
672 wqe->read.to_src_hi = 0;
673 wqe->read.to_src_lo = 0;
674 wqe->read.stag_sink = cpu_to_be32(2);
675 wqe->read.plen = 0;
676 wqe->read.to_sink_hi = 0;
677 wqe->read.to_sink_lo = 0;
678 }
679 wqe->read.r2 = 0;
680 wqe->read.r5 = 0;
681 *len16 = DIV_ROUND_UP(sizeof wqe->read, 16);
682 return 0;
683}
684
Potnuri Bharat Teja94245f42018-08-02 11:33:04 +0530685static void post_write_cmpl(struct c4iw_qp *qhp, const struct ib_send_wr *wr)
686{
687 bool send_signaled = (wr->next->send_flags & IB_SEND_SIGNALED) ||
688 qhp->sq_sig_all;
689 bool write_signaled = (wr->send_flags & IB_SEND_SIGNALED) ||
690 qhp->sq_sig_all;
691 struct t4_swsqe *swsqe;
692 union t4_wr *wqe;
693 u16 write_wrid;
694 u8 len16;
695 u16 idx;
696
697 /*
698 * The sw_sq entries still look like a WRITE and a SEND and consume
699 * 2 slots. The FW WR, however, will be a single uber-WR.
700 */
701 wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
702 qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
703 build_rdma_write_cmpl(&qhp->wq.sq, &wqe->write_cmpl, wr, &len16);
704
705 /* WRITE swsqe */
706 swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
707 swsqe->opcode = FW_RI_RDMA_WRITE;
708 swsqe->idx = qhp->wq.sq.pidx;
709 swsqe->complete = 0;
710 swsqe->signaled = write_signaled;
711 swsqe->flushed = 0;
712 swsqe->wr_id = wr->wr_id;
713 if (c4iw_wr_log) {
714 swsqe->sge_ts =
715 cxgb4_read_sge_timestamp(qhp->rhp->rdev.lldi.ports[0]);
716 swsqe->host_time = ktime_get();
717 }
718
719 write_wrid = qhp->wq.sq.pidx;
720
721 /* just bump the sw_sq */
722 qhp->wq.sq.in_use++;
723 if (++qhp->wq.sq.pidx == qhp->wq.sq.size)
724 qhp->wq.sq.pidx = 0;
725
726 /* SEND_WITH_INV swsqe */
727 swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
728 swsqe->opcode = FW_RI_SEND_WITH_INV;
729 swsqe->idx = qhp->wq.sq.pidx;
730 swsqe->complete = 0;
731 swsqe->signaled = send_signaled;
732 swsqe->flushed = 0;
733 swsqe->wr_id = wr->next->wr_id;
734 if (c4iw_wr_log) {
735 swsqe->sge_ts =
736 cxgb4_read_sge_timestamp(qhp->rhp->rdev.lldi.ports[0]);
737 swsqe->host_time = ktime_get();
738 }
739
740 wqe->write_cmpl.flags_send = send_signaled ? FW_RI_COMPLETION_FLAG : 0;
741 wqe->write_cmpl.wrid_send = qhp->wq.sq.pidx;
742
743 init_wr_hdr(wqe, write_wrid, FW_RI_RDMA_WRITE_CMPL_WR,
744 write_signaled ? FW_RI_COMPLETION_FLAG : 0, len16);
745 t4_sq_produce(&qhp->wq, len16);
746 idx = DIV_ROUND_UP(len16 * 16, T4_EQ_ENTRY_SIZE);
747
748 t4_ring_sq_db(&qhp->wq, idx, wqe);
749}
750
Steve Wisecfdda9d2010-04-21 15:30:06 -0700751static int build_rdma_recv(struct c4iw_qp *qhp, union t4_recv_wr *wqe,
Bart Van Assched34ac5c2018-07-18 09:25:32 -0700752 const struct ib_recv_wr *wr, u8 *len16)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700753{
Steve Wised37ac312010-06-10 19:03:00 +0000754 int ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700755
Steve Wised37ac312010-06-10 19:03:00 +0000756 ret = build_isgl((__be64 *)qhp->wq.rq.queue,
757 (__be64 *)&qhp->wq.rq.queue[qhp->wq.rq.size],
758 &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
759 if (ret)
760 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700761 *len16 = DIV_ROUND_UP(sizeof wqe->recv +
762 wr->num_sge * sizeof(struct fw_ri_sge), 16);
763 return 0;
764}
765
Bart Van Assched34ac5c2018-07-18 09:25:32 -0700766static int build_srq_recv(union t4_recv_wr *wqe, const struct ib_recv_wr *wr,
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530767 u8 *len16)
768{
769 int ret;
770
771 ret = build_isgl((__be64 *)wqe, (__be64 *)(wqe + 1),
772 &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
773 if (ret)
774 return ret;
775 *len16 = DIV_ROUND_UP(sizeof(wqe->recv) +
776 wr->num_sge * sizeof(struct fw_ri_sge), 16);
777 return 0;
778}
779
Steve Wise49b53a92016-09-16 07:54:52 -0700780static void build_tpte_memreg(struct fw_ri_fr_nsmr_tpte_wr *fr,
Bart Van Asschef696bf62018-07-18 09:25:14 -0700781 const struct ib_reg_wr *wr, struct c4iw_mr *mhp,
Steve Wise49b53a92016-09-16 07:54:52 -0700782 u8 *len16)
Sagi Grimberg8376b862015-10-13 19:11:30 +0300783{
Steve Wise49b53a92016-09-16 07:54:52 -0700784 __be64 *p = (__be64 *)fr->pbl;
785
786 fr->r2 = cpu_to_be32(0);
787 fr->stag = cpu_to_be32(mhp->ibmr.rkey);
788
789 fr->tpte.valid_to_pdid = cpu_to_be32(FW_RI_TPTE_VALID_F |
790 FW_RI_TPTE_STAGKEY_V((mhp->ibmr.rkey & FW_RI_TPTE_STAGKEY_M)) |
791 FW_RI_TPTE_STAGSTATE_V(1) |
792 FW_RI_TPTE_STAGTYPE_V(FW_RI_STAG_NSMR) |
793 FW_RI_TPTE_PDID_V(mhp->attr.pdid));
794 fr->tpte.locread_to_qpid = cpu_to_be32(
795 FW_RI_TPTE_PERM_V(c4iw_ib_to_tpt_access(wr->access)) |
796 FW_RI_TPTE_ADDRTYPE_V(FW_RI_VA_BASED_TO) |
797 FW_RI_TPTE_PS_V(ilog2(wr->mr->page_size) - 12));
798 fr->tpte.nosnoop_pbladdr = cpu_to_be32(FW_RI_TPTE_PBLADDR_V(
799 PBL_OFF(&mhp->rhp->rdev, mhp->attr.pbl_addr)>>3));
800 fr->tpte.dca_mwbcnt_pstag = cpu_to_be32(0);
801 fr->tpte.len_hi = cpu_to_be32(0);
802 fr->tpte.len_lo = cpu_to_be32(mhp->ibmr.length);
803 fr->tpte.va_hi = cpu_to_be32(mhp->ibmr.iova >> 32);
804 fr->tpte.va_lo_fbo = cpu_to_be32(mhp->ibmr.iova & 0xffffffff);
805
806 p[0] = cpu_to_be64((u64)mhp->mpl[0]);
807 p[1] = cpu_to_be64((u64)mhp->mpl[1]);
808
809 *len16 = DIV_ROUND_UP(sizeof(*fr), 16);
810}
811
812static int build_memreg(struct t4_sq *sq, union t4_wr *wqe,
Bart Van Asschef696bf62018-07-18 09:25:14 -0700813 const struct ib_reg_wr *wr, struct c4iw_mr *mhp,
814 u8 *len16, bool dsgl_supported)
Steve Wise49b53a92016-09-16 07:54:52 -0700815{
Sagi Grimberg8376b862015-10-13 19:11:30 +0300816 struct fw_ri_immd *imdp;
817 __be64 *p;
818 int i;
819 int pbllen = roundup(mhp->mpl_len * sizeof(u64), 32);
820 int rem;
821
Hariprasad See30f7d2016-02-12 16:10:35 +0530822 if (mhp->mpl_len > t4_max_fr_depth(dsgl_supported && use_dsgl))
Sagi Grimberg8376b862015-10-13 19:11:30 +0300823 return -EINVAL;
824
825 wqe->fr.qpbinde_to_dcacpu = 0;
826 wqe->fr.pgsz_shift = ilog2(wr->mr->page_size) - 12;
827 wqe->fr.addr_type = FW_RI_VA_BASED_TO;
828 wqe->fr.mem_perms = c4iw_ib_to_tpt_access(wr->access);
829 wqe->fr.len_hi = 0;
830 wqe->fr.len_lo = cpu_to_be32(mhp->ibmr.length);
831 wqe->fr.stag = cpu_to_be32(wr->key);
832 wqe->fr.va_hi = cpu_to_be32(mhp->ibmr.iova >> 32);
833 wqe->fr.va_lo_fbo = cpu_to_be32(mhp->ibmr.iova &
834 0xffffffff);
835
Hariprasad See30f7d2016-02-12 16:10:35 +0530836 if (dsgl_supported && use_dsgl && (pbllen > max_fr_immd)) {
Sagi Grimberg8376b862015-10-13 19:11:30 +0300837 struct fw_ri_dsgl *sglp;
838
839 for (i = 0; i < mhp->mpl_len; i++)
840 mhp->mpl[i] = (__force u64)cpu_to_be64((u64)mhp->mpl[i]);
841
842 sglp = (struct fw_ri_dsgl *)(&wqe->fr + 1);
843 sglp->op = FW_RI_DATA_DSGL;
844 sglp->r1 = 0;
845 sglp->nsge = cpu_to_be16(1);
846 sglp->addr0 = cpu_to_be64(mhp->mpl_addr);
847 sglp->len0 = cpu_to_be32(pbllen);
848
849 *len16 = DIV_ROUND_UP(sizeof(wqe->fr) + sizeof(*sglp), 16);
850 } else {
851 imdp = (struct fw_ri_immd *)(&wqe->fr + 1);
852 imdp->op = FW_RI_DATA_IMMD;
853 imdp->r1 = 0;
854 imdp->r2 = 0;
855 imdp->immdlen = cpu_to_be32(pbllen);
856 p = (__be64 *)(imdp + 1);
857 rem = pbllen;
858 for (i = 0; i < mhp->mpl_len; i++) {
859 *p = cpu_to_be64((u64)mhp->mpl[i]);
860 rem -= sizeof(*p);
861 if (++p == (__be64 *)&sq->queue[sq->size])
862 p = (__be64 *)sq->queue;
863 }
Sagi Grimberg8376b862015-10-13 19:11:30 +0300864 while (rem) {
865 *p = 0;
866 rem -= sizeof(*p);
867 if (++p == (__be64 *)&sq->queue[sq->size])
868 p = (__be64 *)sq->queue;
869 }
870 *len16 = DIV_ROUND_UP(sizeof(wqe->fr) + sizeof(*imdp)
871 + pbllen, 16);
872 }
873 return 0;
874}
875
Bart Van Asschef696bf62018-07-18 09:25:14 -0700876static int build_inv_stag(union t4_wr *wqe, const struct ib_send_wr *wr,
877 u8 *len16)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700878{
879 wqe->inv.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
880 wqe->inv.r2 = 0;
881 *len16 = DIV_ROUND_UP(sizeof wqe->inv, 16);
882 return 0;
883}
884
Steve Wisec12a67f2016-12-22 07:40:36 -0800885static void free_qp_work(struct work_struct *work)
886{
887 struct c4iw_ucontext *ucontext;
888 struct c4iw_qp *qhp;
889 struct c4iw_dev *rhp;
890
891 qhp = container_of(work, struct c4iw_qp, free_work);
892 ucontext = qhp->ucontext;
893 rhp = qhp->rhp;
894
Bharat Potnuri548ddb12017-09-27 13:05:49 +0530895 pr_debug("qhp %p ucontext %p\n", qhp, ucontext);
Steve Wisec12a67f2016-12-22 07:40:36 -0800896 destroy_qp(&rhp->rdev, &qhp->wq,
Raju Rangoju6a0b6172018-07-25 21:22:14 +0530897 ucontext ? &ucontext->uctx : &rhp->rdev.uctx, !qhp->srq);
Steve Wisec12a67f2016-12-22 07:40:36 -0800898
899 if (ucontext)
900 c4iw_put_ucontext(ucontext);
Steve Wise2015f262017-09-26 13:13:17 -0700901 c4iw_put_wr_wait(qhp->wr_waitp);
Steve Wisec12a67f2016-12-22 07:40:36 -0800902 kfree(qhp);
903}
904
905static void queue_qp_free(struct kref *kref)
Steve Wisead61a4c2016-07-29 11:00:54 -0700906{
907 struct c4iw_qp *qhp;
908
909 qhp = container_of(kref, struct c4iw_qp, kref);
Bharat Potnuri548ddb12017-09-27 13:05:49 +0530910 pr_debug("qhp %p\n", qhp);
Steve Wisec12a67f2016-12-22 07:40:36 -0800911 queue_work(qhp->rhp->rdev.free_workq, &qhp->free_work);
Steve Wisead61a4c2016-07-29 11:00:54 -0700912}
913
Steve Wisecfdda9d2010-04-21 15:30:06 -0700914void c4iw_qp_add_ref(struct ib_qp *qp)
915{
Bharat Potnuri548ddb12017-09-27 13:05:49 +0530916 pr_debug("ib_qp %p\n", qp);
Steve Wisead61a4c2016-07-29 11:00:54 -0700917 kref_get(&to_c4iw_qp(qp)->kref);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700918}
919
920void c4iw_qp_rem_ref(struct ib_qp *qp)
921{
Bharat Potnuri548ddb12017-09-27 13:05:49 +0530922 pr_debug("ib_qp %p\n", qp);
Steve Wisec12a67f2016-12-22 07:40:36 -0800923 kref_put(&to_c4iw_qp(qp)->kref, queue_qp_free);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700924}
925
Steve Wise05eb2382014-03-14 21:52:08 +0530926static void add_to_fc_list(struct list_head *head, struct list_head *entry)
927{
928 if (list_empty(entry))
929 list_add_tail(entry, head);
930}
931
932static int ring_kernel_sq_db(struct c4iw_qp *qhp, u16 inc)
933{
934 unsigned long flags;
935
936 spin_lock_irqsave(&qhp->rhp->lock, flags);
937 spin_lock(&qhp->lock);
Steve Wisefa658a92014-04-09 09:38:25 -0500938 if (qhp->rhp->db_state == NORMAL)
Hariprasad S963cab52015-09-23 17:19:27 +0530939 t4_ring_sq_db(&qhp->wq, inc, NULL);
Steve Wisefa658a92014-04-09 09:38:25 -0500940 else {
Steve Wise05eb2382014-03-14 21:52:08 +0530941 add_to_fc_list(&qhp->rhp->db_fc_list, &qhp->db_fc_entry);
942 qhp->wq.sq.wq_pidx_inc += inc;
943 }
944 spin_unlock(&qhp->lock);
945 spin_unlock_irqrestore(&qhp->rhp->lock, flags);
946 return 0;
947}
948
949static int ring_kernel_rq_db(struct c4iw_qp *qhp, u16 inc)
950{
951 unsigned long flags;
952
953 spin_lock_irqsave(&qhp->rhp->lock, flags);
954 spin_lock(&qhp->lock);
Steve Wisefa658a92014-04-09 09:38:25 -0500955 if (qhp->rhp->db_state == NORMAL)
Hariprasad S963cab52015-09-23 17:19:27 +0530956 t4_ring_rq_db(&qhp->wq, inc, NULL);
Steve Wisefa658a92014-04-09 09:38:25 -0500957 else {
Steve Wise05eb2382014-03-14 21:52:08 +0530958 add_to_fc_list(&qhp->rhp->db_fc_list, &qhp->db_fc_entry);
959 qhp->wq.rq.wq_pidx_inc += inc;
960 }
961 spin_unlock(&qhp->lock);
962 spin_unlock_irqrestore(&qhp->rhp->lock, flags);
963 return 0;
964}
965
Steve Wise96a236e2017-12-19 10:29:25 -0800966static int ib_to_fw_opcode(int ib_opcode)
967{
968 int opcode;
969
970 switch (ib_opcode) {
971 case IB_WR_SEND_WITH_INV:
972 opcode = FW_RI_SEND_WITH_INV;
973 break;
974 case IB_WR_SEND:
975 opcode = FW_RI_SEND;
976 break;
977 case IB_WR_RDMA_WRITE:
978 opcode = FW_RI_RDMA_WRITE;
979 break;
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +0530980 case IB_WR_RDMA_WRITE_WITH_IMM:
981 opcode = FW_RI_WRITE_IMMEDIATE;
982 break;
Steve Wise96a236e2017-12-19 10:29:25 -0800983 case IB_WR_RDMA_READ:
984 case IB_WR_RDMA_READ_WITH_INV:
985 opcode = FW_RI_READ_REQ;
986 break;
987 case IB_WR_REG_MR:
988 opcode = FW_RI_FAST_REGISTER;
989 break;
990 case IB_WR_LOCAL_INV:
991 opcode = FW_RI_LOCAL_INV;
992 break;
993 default:
994 opcode = -EINVAL;
995 }
996 return opcode;
997}
998
Bart Van Asschef696bf62018-07-18 09:25:14 -0700999static int complete_sq_drain_wr(struct c4iw_qp *qhp,
1000 const struct ib_send_wr *wr)
Steve Wise4fe7c292016-12-22 07:04:59 -08001001{
1002 struct t4_cqe cqe = {};
1003 struct c4iw_cq *schp;
1004 unsigned long flag;
1005 struct t4_cq *cq;
Steve Wise96a236e2017-12-19 10:29:25 -08001006 int opcode;
Steve Wise4fe7c292016-12-22 07:04:59 -08001007
1008 schp = to_c4iw_cq(qhp->ibqp.send_cq);
1009 cq = &schp->cq;
1010
Steve Wise96a236e2017-12-19 10:29:25 -08001011 opcode = ib_to_fw_opcode(wr->opcode);
1012 if (opcode < 0)
1013 return opcode;
1014
Steve Wise4fe7c292016-12-22 07:04:59 -08001015 cqe.u.drain_cookie = wr->wr_id;
1016 cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
Steve Wise96a236e2017-12-19 10:29:25 -08001017 CQE_OPCODE_V(opcode) |
Steve Wise4fe7c292016-12-22 07:04:59 -08001018 CQE_TYPE_V(1) |
1019 CQE_SWCQE_V(1) |
Steve Wise96a236e2017-12-19 10:29:25 -08001020 CQE_DRAIN_V(1) |
Steve Wise4fe7c292016-12-22 07:04:59 -08001021 CQE_QPID_V(qhp->wq.sq.qid));
1022
1023 spin_lock_irqsave(&schp->lock, flag);
1024 cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
1025 cq->sw_queue[cq->sw_pidx] = cqe;
1026 t4_swcq_produce(cq);
1027 spin_unlock_irqrestore(&schp->lock, flag);
1028
Steve Wisecbb40fa2017-11-09 07:14:43 -08001029 if (t4_clear_cq_armed(&schp->cq)) {
1030 spin_lock_irqsave(&schp->comp_handler_lock, flag);
1031 (*schp->ibcq.comp_handler)(&schp->ibcq,
1032 schp->ibcq.cq_context);
1033 spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
1034 }
Steve Wise96a236e2017-12-19 10:29:25 -08001035 return 0;
Steve Wise4fe7c292016-12-22 07:04:59 -08001036}
1037
Bart Van Assched34ac5c2018-07-18 09:25:32 -07001038static int complete_sq_drain_wrs(struct c4iw_qp *qhp,
1039 const struct ib_send_wr *wr,
1040 const struct ib_send_wr **bad_wr)
Steve Wised1458732017-12-19 14:02:10 -08001041{
1042 int ret = 0;
1043
1044 while (wr) {
1045 ret = complete_sq_drain_wr(qhp, wr);
1046 if (ret) {
1047 *bad_wr = wr;
1048 break;
1049 }
1050 wr = wr->next;
1051 }
1052 return ret;
Steve Wise4fe7c292016-12-22 07:04:59 -08001053}
1054
Bart Van Assched34ac5c2018-07-18 09:25:32 -07001055static void complete_rq_drain_wr(struct c4iw_qp *qhp,
1056 const struct ib_recv_wr *wr)
Steve Wise4fe7c292016-12-22 07:04:59 -08001057{
1058 struct t4_cqe cqe = {};
1059 struct c4iw_cq *rchp;
1060 unsigned long flag;
1061 struct t4_cq *cq;
1062
1063 rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
1064 cq = &rchp->cq;
1065
1066 cqe.u.drain_cookie = wr->wr_id;
1067 cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
Steve Wise96a236e2017-12-19 10:29:25 -08001068 CQE_OPCODE_V(FW_RI_SEND) |
Steve Wise4fe7c292016-12-22 07:04:59 -08001069 CQE_TYPE_V(0) |
1070 CQE_SWCQE_V(1) |
Steve Wise96a236e2017-12-19 10:29:25 -08001071 CQE_DRAIN_V(1) |
Steve Wise4fe7c292016-12-22 07:04:59 -08001072 CQE_QPID_V(qhp->wq.sq.qid));
1073
1074 spin_lock_irqsave(&rchp->lock, flag);
1075 cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
1076 cq->sw_queue[cq->sw_pidx] = cqe;
1077 t4_swcq_produce(cq);
1078 spin_unlock_irqrestore(&rchp->lock, flag);
1079
Steve Wisecbb40fa2017-11-09 07:14:43 -08001080 if (t4_clear_cq_armed(&rchp->cq)) {
1081 spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1082 (*rchp->ibcq.comp_handler)(&rchp->ibcq,
1083 rchp->ibcq.cq_context);
1084 spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1085 }
Steve Wise4fe7c292016-12-22 07:04:59 -08001086}
1087
Bart Van Assched34ac5c2018-07-18 09:25:32 -07001088static void complete_rq_drain_wrs(struct c4iw_qp *qhp,
1089 const struct ib_recv_wr *wr)
Steve Wised1458732017-12-19 14:02:10 -08001090{
1091 while (wr) {
1092 complete_rq_drain_wr(qhp, wr);
1093 wr = wr->next;
1094 }
1095}
1096
Bart Van Assched34ac5c2018-07-18 09:25:32 -07001097int c4iw_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
1098 const struct ib_send_wr **bad_wr)
Steve Wisecfdda9d2010-04-21 15:30:06 -07001099{
1100 int err = 0;
1101 u8 len16 = 0;
1102 enum fw_wr_opcodes fw_opcode = 0;
1103 enum fw_ri_wr_flags fw_flags;
1104 struct c4iw_qp *qhp;
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +05301105 struct c4iw_dev *rhp;
Steve Wisefa658a92014-04-09 09:38:25 -05001106 union t4_wr *wqe = NULL;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001107 u32 num_wrs;
1108 struct t4_swsqe *swsqe;
1109 unsigned long flag;
1110 u16 idx = 0;
1111
1112 qhp = to_c4iw_qp(ibqp);
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +05301113 rhp = qhp->rhp;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001114 spin_lock_irqsave(&qhp->lock, flag);
Steve Wisec058ecf2017-11-27 13:16:32 -08001115
1116 /*
1117 * If the qp has been flushed, then just insert a special
1118 * drain cqe.
1119 */
1120 if (qhp->wq.flushed) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07001121 spin_unlock_irqrestore(&qhp->lock, flag);
Steve Wised1458732017-12-19 14:02:10 -08001122 err = complete_sq_drain_wrs(qhp, wr, bad_wr);
Steve Wise4fe7c292016-12-22 07:04:59 -08001123 return err;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001124 }
1125 num_wrs = t4_sq_avail(&qhp->wq);
1126 if (num_wrs == 0) {
1127 spin_unlock_irqrestore(&qhp->lock, flag);
Steve Wise4ff522e2016-10-18 14:04:39 -07001128 *bad_wr = wr;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001129 return -ENOMEM;
1130 }
Potnuri Bharat Teja94245f42018-08-02 11:33:04 +05301131
1132 /*
1133 * Fastpath for NVMe-oF target WRITE + SEND_WITH_INV wr chain which is
1134 * the response for small NVMEe-oF READ requests. If the chain is
1135 * exactly a WRITE->SEND_WITH_INV and the sgl depths and lengths
1136 * meet the requirements of the fw_ri_write_cmpl_wr work request,
1137 * then build and post the write_cmpl WR. If any of the tests
1138 * below are not true, then we continue on with the tradtional WRITE
1139 * and SEND WRs.
1140 */
1141 if (qhp->rhp->rdev.lldi.write_cmpl_support &&
1142 CHELSIO_CHIP_VERSION(qhp->rhp->rdev.lldi.adapter_type) >=
1143 CHELSIO_T5 &&
1144 wr && wr->next && !wr->next->next &&
1145 wr->opcode == IB_WR_RDMA_WRITE &&
1146 wr->sg_list[0].length && wr->num_sge <= T4_WRITE_CMPL_MAX_SGL &&
1147 wr->next->opcode == IB_WR_SEND_WITH_INV &&
1148 wr->next->sg_list[0].length == T4_WRITE_CMPL_MAX_CQE &&
1149 wr->next->num_sge == 1 && num_wrs >= 2) {
1150 post_write_cmpl(qhp, wr);
1151 spin_unlock_irqrestore(&qhp->lock, flag);
1152 return 0;
1153 }
1154
Steve Wisecfdda9d2010-04-21 15:30:06 -07001155 while (wr) {
1156 if (num_wrs == 0) {
1157 err = -ENOMEM;
1158 *bad_wr = wr;
1159 break;
1160 }
Steve Wised37ac312010-06-10 19:03:00 +00001161 wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
1162 qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
1163
Steve Wisecfdda9d2010-04-21 15:30:06 -07001164 fw_flags = 0;
1165 if (wr->send_flags & IB_SEND_SOLICITED)
1166 fw_flags |= FW_RI_SOLICITED_EVENT_FLAG;
Steve Wiseba32de92014-03-19 17:44:43 +05301167 if (wr->send_flags & IB_SEND_SIGNALED || qhp->sq_sig_all)
Steve Wisecfdda9d2010-04-21 15:30:06 -07001168 fw_flags |= FW_RI_COMPLETION_FLAG;
1169 swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
1170 switch (wr->opcode) {
1171 case IB_WR_SEND_WITH_INV:
1172 case IB_WR_SEND:
1173 if (wr->send_flags & IB_SEND_FENCE)
1174 fw_flags |= FW_RI_READ_FENCE_FLAG;
1175 fw_opcode = FW_RI_SEND_WR;
1176 if (wr->opcode == IB_WR_SEND)
1177 swsqe->opcode = FW_RI_SEND;
1178 else
1179 swsqe->opcode = FW_RI_SEND_WITH_INV;
Steve Wised37ac312010-06-10 19:03:00 +00001180 err = build_rdma_send(&qhp->wq.sq, wqe, wr, &len16);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001181 break;
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +05301182 case IB_WR_RDMA_WRITE_WITH_IMM:
1183 if (unlikely(!rhp->rdev.lldi.write_w_imm_support)) {
1184 err = -EINVAL;
1185 break;
1186 }
1187 fw_flags |= FW_RI_RDMA_WRITE_WITH_IMMEDIATE;
1188 /*FALLTHROUGH*/
Steve Wisecfdda9d2010-04-21 15:30:06 -07001189 case IB_WR_RDMA_WRITE:
1190 fw_opcode = FW_RI_RDMA_WRITE_WR;
1191 swsqe->opcode = FW_RI_RDMA_WRITE;
Steve Wised37ac312010-06-10 19:03:00 +00001192 err = build_rdma_write(&qhp->wq.sq, wqe, wr, &len16);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001193 break;
1194 case IB_WR_RDMA_READ:
Steve Wise2f1fb502010-05-20 16:58:16 -05001195 case IB_WR_RDMA_READ_WITH_INV:
Steve Wisecfdda9d2010-04-21 15:30:06 -07001196 fw_opcode = FW_RI_RDMA_READ_WR;
1197 swsqe->opcode = FW_RI_READ_REQ;
Steve Wise5c6b2aa2016-11-03 12:09:38 -07001198 if (wr->opcode == IB_WR_RDMA_READ_WITH_INV) {
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +05301199 c4iw_invalidate_mr(rhp, wr->sg_list[0].lkey);
Steve Wise410ade42010-09-17 15:40:09 -05001200 fw_flags = FW_RI_RDMA_READ_INVALIDATE;
Steve Wise5c6b2aa2016-11-03 12:09:38 -07001201 } else {
Steve Wise2f1fb502010-05-20 16:58:16 -05001202 fw_flags = 0;
Steve Wise5c6b2aa2016-11-03 12:09:38 -07001203 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001204 err = build_rdma_read(wqe, wr, &len16);
1205 if (err)
1206 break;
1207 swsqe->read_len = wr->sg_list[0].length;
1208 if (!qhp->wq.sq.oldest_read)
1209 qhp->wq.sq.oldest_read = swsqe;
1210 break;
Steve Wise49b53a92016-09-16 07:54:52 -07001211 case IB_WR_REG_MR: {
1212 struct c4iw_mr *mhp = to_c4iw_mr(reg_wr(wr)->mr);
1213
Sagi Grimberg8376b862015-10-13 19:11:30 +03001214 swsqe->opcode = FW_RI_FAST_REGISTER;
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +05301215 if (rhp->rdev.lldi.fr_nsmr_tpte_wr_support &&
Steve Wise49b53a92016-09-16 07:54:52 -07001216 !mhp->attr.state && mhp->mpl_len <= 2) {
1217 fw_opcode = FW_RI_FR_NSMR_TPTE_WR;
1218 build_tpte_memreg(&wqe->fr_tpte, reg_wr(wr),
1219 mhp, &len16);
1220 } else {
1221 fw_opcode = FW_RI_FR_NSMR_WR;
1222 err = build_memreg(&qhp->wq.sq, wqe, reg_wr(wr),
1223 mhp, &len16,
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +05301224 rhp->rdev.lldi.ulptx_memwrite_dsgl);
Steve Wise49b53a92016-09-16 07:54:52 -07001225 if (err)
1226 break;
1227 }
1228 mhp->attr.state = 1;
Sagi Grimberg8376b862015-10-13 19:11:30 +03001229 break;
Steve Wise49b53a92016-09-16 07:54:52 -07001230 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001231 case IB_WR_LOCAL_INV:
Steve Wise4ab1eb92010-05-20 16:58:10 -05001232 if (wr->send_flags & IB_SEND_FENCE)
1233 fw_flags |= FW_RI_LOCAL_FENCE_FLAG;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001234 fw_opcode = FW_RI_INV_LSTAG_WR;
1235 swsqe->opcode = FW_RI_LOCAL_INV;
Steve Wise5c6b2aa2016-11-03 12:09:38 -07001236 err = build_inv_stag(wqe, wr, &len16);
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +05301237 c4iw_invalidate_mr(rhp, wr->ex.invalidate_rkey);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001238 break;
1239 default:
Bharat Potnuri4d45b752017-09-27 13:05:50 +05301240 pr_warn("%s post of type=%d TBD!\n", __func__,
1241 wr->opcode);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001242 err = -EINVAL;
1243 }
1244 if (err) {
1245 *bad_wr = wr;
1246 break;
1247 }
1248 swsqe->idx = qhp->wq.sq.pidx;
1249 swsqe->complete = 0;
Steve Wiseba32de92014-03-19 17:44:43 +05301250 swsqe->signaled = (wr->send_flags & IB_SEND_SIGNALED) ||
1251 qhp->sq_sig_all;
Steve Wise1cf24dc2013-08-06 21:04:35 +05301252 swsqe->flushed = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001253 swsqe->wr_id = wr->wr_id;
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301254 if (c4iw_wr_log) {
1255 swsqe->sge_ts = cxgb4_read_sge_timestamp(
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +05301256 rhp->rdev.lldi.ports[0]);
Arnd Bergmannf8109d92017-11-27 12:44:53 +01001257 swsqe->host_time = ktime_get();
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301258 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001259
1260 init_wr_hdr(wqe, qhp->wq.sq.pidx, fw_opcode, fw_flags, len16);
1261
Bharat Potnuri548ddb12017-09-27 13:05:49 +05301262 pr_debug("cookie 0x%llx pidx 0x%x opcode 0x%x read_len %u\n",
Joe Perchesa9a42882017-02-09 14:23:51 -08001263 (unsigned long long)wr->wr_id, qhp->wq.sq.pidx,
1264 swsqe->opcode, swsqe->read_len);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001265 wr = wr->next;
1266 num_wrs--;
Steve Wised37ac312010-06-10 19:03:00 +00001267 t4_sq_produce(&qhp->wq, len16);
1268 idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001269 }
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +05301270 if (!rhp->rdev.status_page->db_off) {
Hariprasad S963cab52015-09-23 17:19:27 +05301271 t4_ring_sq_db(&qhp->wq, idx, wqe);
Steve Wise05eb2382014-03-14 21:52:08 +05301272 spin_unlock_irqrestore(&qhp->lock, flag);
1273 } else {
1274 spin_unlock_irqrestore(&qhp->lock, flag);
1275 ring_kernel_sq_db(qhp, idx);
1276 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001277 return err;
1278}
1279
Bart Van Assched34ac5c2018-07-18 09:25:32 -07001280int c4iw_post_receive(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
1281 const struct ib_recv_wr **bad_wr)
Steve Wisecfdda9d2010-04-21 15:30:06 -07001282{
1283 int err = 0;
1284 struct c4iw_qp *qhp;
Steve Wisefa658a92014-04-09 09:38:25 -05001285 union t4_recv_wr *wqe = NULL;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001286 u32 num_wrs;
1287 u8 len16 = 0;
1288 unsigned long flag;
1289 u16 idx = 0;
1290
1291 qhp = to_c4iw_qp(ibqp);
1292 spin_lock_irqsave(&qhp->lock, flag);
Steve Wisec058ecf2017-11-27 13:16:32 -08001293
1294 /*
1295 * If the qp has been flushed, then just insert a special
1296 * drain cqe.
1297 */
1298 if (qhp->wq.flushed) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07001299 spin_unlock_irqrestore(&qhp->lock, flag);
Steve Wised1458732017-12-19 14:02:10 -08001300 complete_rq_drain_wrs(qhp, wr);
Steve Wise4fe7c292016-12-22 07:04:59 -08001301 return err;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001302 }
1303 num_wrs = t4_rq_avail(&qhp->wq);
1304 if (num_wrs == 0) {
1305 spin_unlock_irqrestore(&qhp->lock, flag);
Steve Wise4ff522e2016-10-18 14:04:39 -07001306 *bad_wr = wr;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001307 return -ENOMEM;
1308 }
1309 while (wr) {
1310 if (wr->num_sge > T4_MAX_RECV_SGE) {
1311 err = -EINVAL;
1312 *bad_wr = wr;
1313 break;
1314 }
Steve Wised37ac312010-06-10 19:03:00 +00001315 wqe = (union t4_recv_wr *)((u8 *)qhp->wq.rq.queue +
1316 qhp->wq.rq.wq_pidx *
1317 T4_EQ_ENTRY_SIZE);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001318 if (num_wrs)
1319 err = build_rdma_recv(qhp, wqe, wr, &len16);
1320 else
1321 err = -ENOMEM;
1322 if (err) {
1323 *bad_wr = wr;
1324 break;
1325 }
1326
1327 qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].wr_id = wr->wr_id;
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301328 if (c4iw_wr_log) {
1329 qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].sge_ts =
1330 cxgb4_read_sge_timestamp(
1331 qhp->rhp->rdev.lldi.ports[0]);
Arnd Bergmannf8109d92017-11-27 12:44:53 +01001332 qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].host_time =
1333 ktime_get();
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301334 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001335
1336 wqe->recv.opcode = FW_RI_RECV_WR;
1337 wqe->recv.r1 = 0;
1338 wqe->recv.wrid = qhp->wq.rq.pidx;
1339 wqe->recv.r2[0] = 0;
1340 wqe->recv.r2[1] = 0;
1341 wqe->recv.r2[2] = 0;
1342 wqe->recv.len16 = len16;
Bharat Potnuri548ddb12017-09-27 13:05:49 +05301343 pr_debug("cookie 0x%llx pidx %u\n",
Joe Perchesa9a42882017-02-09 14:23:51 -08001344 (unsigned long long)wr->wr_id, qhp->wq.rq.pidx);
Steve Wised37ac312010-06-10 19:03:00 +00001345 t4_rq_produce(&qhp->wq, len16);
1346 idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001347 wr = wr->next;
1348 num_wrs--;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001349 }
Steve Wise05eb2382014-03-14 21:52:08 +05301350 if (!qhp->rhp->rdev.status_page->db_off) {
Hariprasad S963cab52015-09-23 17:19:27 +05301351 t4_ring_rq_db(&qhp->wq, idx, wqe);
Steve Wise05eb2382014-03-14 21:52:08 +05301352 spin_unlock_irqrestore(&qhp->lock, flag);
1353 } else {
1354 spin_unlock_irqrestore(&qhp->lock, flag);
1355 ring_kernel_rq_db(qhp, idx);
1356 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001357 return err;
1358}
1359
Raju Rangoju6a0b6172018-07-25 21:22:14 +05301360static void defer_srq_wr(struct t4_srq *srq, union t4_recv_wr *wqe,
1361 u64 wr_id, u8 len16)
1362{
1363 struct t4_srq_pending_wr *pwr = &srq->pending_wrs[srq->pending_pidx];
1364
1365 pr_debug("%s cidx %u pidx %u wq_pidx %u in_use %u ooo_count %u wr_id 0x%llx pending_cidx %u pending_pidx %u pending_in_use %u\n",
1366 __func__, srq->cidx, srq->pidx, srq->wq_pidx,
1367 srq->in_use, srq->ooo_count,
1368 (unsigned long long)wr_id, srq->pending_cidx,
1369 srq->pending_pidx, srq->pending_in_use);
1370 pwr->wr_id = wr_id;
1371 pwr->len16 = len16;
1372 memcpy(&pwr->wqe, wqe, len16 * 16);
1373 t4_srq_produce_pending_wr(srq);
1374}
1375
Bart Van Assched34ac5c2018-07-18 09:25:32 -07001376int c4iw_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
1377 const struct ib_recv_wr **bad_wr)
Raju Rangoju6a0b6172018-07-25 21:22:14 +05301378{
1379 union t4_recv_wr *wqe, lwqe;
1380 struct c4iw_srq *srq;
1381 unsigned long flag;
1382 u8 len16 = 0;
1383 u16 idx = 0;
1384 int err = 0;
1385 u32 num_wrs;
1386
1387 srq = to_c4iw_srq(ibsrq);
1388 spin_lock_irqsave(&srq->lock, flag);
1389 num_wrs = t4_srq_avail(&srq->wq);
1390 if (num_wrs == 0) {
1391 spin_unlock_irqrestore(&srq->lock, flag);
1392 return -ENOMEM;
1393 }
1394 while (wr) {
1395 if (wr->num_sge > T4_MAX_RECV_SGE) {
1396 err = -EINVAL;
1397 *bad_wr = wr;
1398 break;
1399 }
1400 wqe = &lwqe;
1401 if (num_wrs)
1402 err = build_srq_recv(wqe, wr, &len16);
1403 else
1404 err = -ENOMEM;
1405 if (err) {
1406 *bad_wr = wr;
1407 break;
1408 }
1409
1410 wqe->recv.opcode = FW_RI_RECV_WR;
1411 wqe->recv.r1 = 0;
1412 wqe->recv.wrid = srq->wq.pidx;
1413 wqe->recv.r2[0] = 0;
1414 wqe->recv.r2[1] = 0;
1415 wqe->recv.r2[2] = 0;
1416 wqe->recv.len16 = len16;
1417
1418 if (srq->wq.ooo_count ||
1419 srq->wq.pending_in_use ||
1420 srq->wq.sw_rq[srq->wq.pidx].valid) {
1421 defer_srq_wr(&srq->wq, wqe, wr->wr_id, len16);
1422 } else {
1423 srq->wq.sw_rq[srq->wq.pidx].wr_id = wr->wr_id;
1424 srq->wq.sw_rq[srq->wq.pidx].valid = 1;
1425 c4iw_copy_wr_to_srq(&srq->wq, wqe, len16);
1426 pr_debug("%s cidx %u pidx %u wq_pidx %u in_use %u wr_id 0x%llx\n",
1427 __func__, srq->wq.cidx,
1428 srq->wq.pidx, srq->wq.wq_pidx,
1429 srq->wq.in_use,
1430 (unsigned long long)wr->wr_id);
1431 t4_srq_produce(&srq->wq, len16);
1432 idx += DIV_ROUND_UP(len16 * 16, T4_EQ_ENTRY_SIZE);
1433 }
1434 wr = wr->next;
1435 num_wrs--;
1436 }
1437 if (idx)
1438 t4_ring_srq_db(&srq->wq, idx, len16, wqe);
1439 spin_unlock_irqrestore(&srq->lock, flag);
1440 return err;
1441}
1442
Steve Wisecfdda9d2010-04-21 15:30:06 -07001443static inline void build_term_codes(struct t4_cqe *err_cqe, u8 *layer_type,
1444 u8 *ecode)
1445{
1446 int status;
1447 int tagged;
1448 int opcode;
1449 int rqtype;
1450 int send_inv;
1451
1452 if (!err_cqe) {
1453 *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
1454 *ecode = 0;
1455 return;
1456 }
1457
1458 status = CQE_STATUS(err_cqe);
1459 opcode = CQE_OPCODE(err_cqe);
1460 rqtype = RQ_TYPE(err_cqe);
1461 send_inv = (opcode == FW_RI_SEND_WITH_INV) ||
1462 (opcode == FW_RI_SEND_WITH_SE_INV);
1463 tagged = (opcode == FW_RI_RDMA_WRITE) ||
1464 (rqtype && (opcode == FW_RI_READ_RESP));
1465
1466 switch (status) {
1467 case T4_ERR_STAG:
1468 if (send_inv) {
1469 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1470 *ecode = RDMAP_CANT_INV_STAG;
1471 } else {
1472 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1473 *ecode = RDMAP_INV_STAG;
1474 }
1475 break;
1476 case T4_ERR_PDID:
1477 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1478 if ((opcode == FW_RI_SEND_WITH_INV) ||
1479 (opcode == FW_RI_SEND_WITH_SE_INV))
1480 *ecode = RDMAP_CANT_INV_STAG;
1481 else
1482 *ecode = RDMAP_STAG_NOT_ASSOC;
1483 break;
1484 case T4_ERR_QPID:
1485 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1486 *ecode = RDMAP_STAG_NOT_ASSOC;
1487 break;
1488 case T4_ERR_ACCESS:
1489 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1490 *ecode = RDMAP_ACC_VIOL;
1491 break;
1492 case T4_ERR_WRAP:
1493 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1494 *ecode = RDMAP_TO_WRAP;
1495 break;
1496 case T4_ERR_BOUND:
1497 if (tagged) {
1498 *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1499 *ecode = DDPT_BASE_BOUNDS;
1500 } else {
1501 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1502 *ecode = RDMAP_BASE_BOUNDS;
1503 }
1504 break;
1505 case T4_ERR_INVALIDATE_SHARED_MR:
1506 case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
1507 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1508 *ecode = RDMAP_CANT_INV_STAG;
1509 break;
1510 case T4_ERR_ECC:
1511 case T4_ERR_ECC_PSTAG:
1512 case T4_ERR_INTERNAL_ERR:
1513 *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
1514 *ecode = 0;
1515 break;
1516 case T4_ERR_OUT_OF_RQE:
1517 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1518 *ecode = DDPU_INV_MSN_NOBUF;
1519 break;
1520 case T4_ERR_PBL_ADDR_BOUND:
1521 *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1522 *ecode = DDPT_BASE_BOUNDS;
1523 break;
1524 case T4_ERR_CRC:
1525 *layer_type = LAYER_MPA|DDP_LLP;
1526 *ecode = MPA_CRC_ERR;
1527 break;
1528 case T4_ERR_MARKER:
1529 *layer_type = LAYER_MPA|DDP_LLP;
1530 *ecode = MPA_MARKER_ERR;
1531 break;
1532 case T4_ERR_PDU_LEN_ERR:
1533 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1534 *ecode = DDPU_MSG_TOOBIG;
1535 break;
1536 case T4_ERR_DDP_VERSION:
1537 if (tagged) {
1538 *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1539 *ecode = DDPT_INV_VERS;
1540 } else {
1541 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1542 *ecode = DDPU_INV_VERS;
1543 }
1544 break;
1545 case T4_ERR_RDMA_VERSION:
1546 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1547 *ecode = RDMAP_INV_VERS;
1548 break;
1549 case T4_ERR_OPCODE:
1550 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1551 *ecode = RDMAP_INV_OPCODE;
1552 break;
1553 case T4_ERR_DDP_QUEUE_NUM:
1554 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1555 *ecode = DDPU_INV_QN;
1556 break;
1557 case T4_ERR_MSN:
1558 case T4_ERR_MSN_GAP:
1559 case T4_ERR_MSN_RANGE:
1560 case T4_ERR_IRD_OVERFLOW:
1561 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1562 *ecode = DDPU_INV_MSN_RANGE;
1563 break;
1564 case T4_ERR_TBIT:
1565 *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
1566 *ecode = 0;
1567 break;
1568 case T4_ERR_MO:
1569 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1570 *ecode = DDPU_INV_MO;
1571 break;
1572 default:
1573 *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
1574 *ecode = 0;
1575 break;
1576 }
1577}
1578
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001579static void post_terminate(struct c4iw_qp *qhp, struct t4_cqe *err_cqe,
1580 gfp_t gfp)
Steve Wisecfdda9d2010-04-21 15:30:06 -07001581{
1582 struct fw_ri_wr *wqe;
1583 struct sk_buff *skb;
1584 struct terminate_message *term;
1585
Bharat Potnuri548ddb12017-09-27 13:05:49 +05301586 pr_debug("qhp %p qid 0x%x tid %u\n", qhp, qhp->wq.sq.qid,
Joe Perchesa9a42882017-02-09 14:23:51 -08001587 qhp->ep->hwtid);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001588
Hariprasad S4a740832016-06-10 01:05:15 +05301589 skb = skb_dequeue(&qhp->ep->com.ep_skb_list);
1590 if (WARN_ON(!skb))
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001591 return;
Hariprasad S4a740832016-06-10 01:05:15 +05301592
Steve Wisecfdda9d2010-04-21 15:30:06 -07001593 set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
1594
YueHaibingecb238f2018-04-28 15:31:06 +08001595 wqe = __skb_put_zero(skb, sizeof(*wqe));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301596 wqe->op_compl = cpu_to_be32(FW_WR_OP_V(FW_RI_INIT_WR));
Steve Wisecfdda9d2010-04-21 15:30:06 -07001597 wqe->flowid_len16 = cpu_to_be32(
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301598 FW_WR_FLOWID_V(qhp->ep->hwtid) |
1599 FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
Steve Wisecfdda9d2010-04-21 15:30:06 -07001600
1601 wqe->u.terminate.type = FW_RI_TYPE_TERMINATE;
1602 wqe->u.terminate.immdlen = cpu_to_be32(sizeof *term);
1603 term = (struct terminate_message *)wqe->u.terminate.termmsg;
Kumar Sanghvid2fe99e2011-09-25 20:17:44 +05301604 if (qhp->attr.layer_etype == (LAYER_MPA|DDP_LLP)) {
1605 term->layer_etype = qhp->attr.layer_etype;
1606 term->ecode = qhp->attr.ecode;
1607 } else
1608 build_term_codes(err_cqe, &term->layer_etype, &term->ecode);
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001609 c4iw_ofld_send(&qhp->rhp->rdev, skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001610}
1611
1612/*
1613 * Assumes qhp lock is held.
1614 */
1615static void __flush_qp(struct c4iw_qp *qhp, struct c4iw_cq *rchp,
Steve Wise2f5b48c2010-09-10 11:15:36 -05001616 struct c4iw_cq *schp)
Steve Wisecfdda9d2010-04-21 15:30:06 -07001617{
1618 int count;
Raju Rangoju6a0b6172018-07-25 21:22:14 +05301619 int rq_flushed = 0, sq_flushed;
Steve Wise2f5b48c2010-09-10 11:15:36 -05001620 unsigned long flag;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001621
Bharat Potnuri548ddb12017-09-27 13:05:49 +05301622 pr_debug("qhp %p rchp %p schp %p\n", qhp, rchp, schp);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001623
Steve Wisebc52e9c2017-11-09 07:21:26 -08001624 /* locking hierarchy: cqs lock first, then qp lock. */
Steve Wise2f5b48c2010-09-10 11:15:36 -05001625 spin_lock_irqsave(&rchp->lock, flag);
Steve Wisebc52e9c2017-11-09 07:21:26 -08001626 if (schp != rchp)
1627 spin_lock(&schp->lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001628 spin_lock(&qhp->lock);
Steve Wise1cf24dc2013-08-06 21:04:35 +05301629
1630 if (qhp->wq.flushed) {
1631 spin_unlock(&qhp->lock);
Steve Wisebc52e9c2017-11-09 07:21:26 -08001632 if (schp != rchp)
1633 spin_unlock(&schp->lock);
Steve Wise1cf24dc2013-08-06 21:04:35 +05301634 spin_unlock_irqrestore(&rchp->lock, flag);
1635 return;
1636 }
1637 qhp->wq.flushed = 1;
Raju Rangoju6a0b6172018-07-25 21:22:14 +05301638 t4_set_wq_in_error(&qhp->wq, 0);
Steve Wise1cf24dc2013-08-06 21:04:35 +05301639
Bharat Potnuri2df19e12018-04-27 16:41:16 +05301640 c4iw_flush_hw_cq(rchp, qhp);
Raju Rangoju6a0b6172018-07-25 21:22:14 +05301641 if (!qhp->srq) {
1642 c4iw_count_rcqes(&rchp->cq, &qhp->wq, &count);
1643 rq_flushed = c4iw_flush_rq(&qhp->wq, &rchp->cq, count);
1644 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001645
Steve Wise1cf24dc2013-08-06 21:04:35 +05301646 if (schp != rchp)
Bharat Potnuri2df19e12018-04-27 16:41:16 +05301647 c4iw_flush_hw_cq(schp, qhp);
Steve Wise678ea9b2014-07-31 14:35:43 -05001648 sq_flushed = c4iw_flush_sq(qhp);
Steve Wisebc52e9c2017-11-09 07:21:26 -08001649
Steve Wisecfdda9d2010-04-21 15:30:06 -07001650 spin_unlock(&qhp->lock);
Steve Wisebc52e9c2017-11-09 07:21:26 -08001651 if (schp != rchp)
1652 spin_unlock(&schp->lock);
1653 spin_unlock_irqrestore(&rchp->lock, flag);
Steve Wise678ea9b2014-07-31 14:35:43 -05001654
1655 if (schp == rchp) {
Steve Wise335ebf62017-11-30 09:41:56 -08001656 if ((rq_flushed || sq_flushed) &&
1657 t4_clear_cq_armed(&rchp->cq)) {
Steve Wise678ea9b2014-07-31 14:35:43 -05001658 spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1659 (*rchp->ibcq.comp_handler)(&rchp->ibcq,
1660 rchp->ibcq.cq_context);
1661 spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1662 }
1663 } else {
Steve Wise335ebf62017-11-30 09:41:56 -08001664 if (rq_flushed && t4_clear_cq_armed(&rchp->cq)) {
Steve Wise678ea9b2014-07-31 14:35:43 -05001665 spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1666 (*rchp->ibcq.comp_handler)(&rchp->ibcq,
1667 rchp->ibcq.cq_context);
1668 spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1669 }
Steve Wise335ebf62017-11-30 09:41:56 -08001670 if (sq_flushed && t4_clear_cq_armed(&schp->cq)) {
Steve Wise678ea9b2014-07-31 14:35:43 -05001671 spin_lock_irqsave(&schp->comp_handler_lock, flag);
1672 (*schp->ibcq.comp_handler)(&schp->ibcq,
1673 schp->ibcq.cq_context);
1674 spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
1675 }
Kumar Sanghvi581bbe22011-10-24 21:20:21 +05301676 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001677}
1678
Steve Wise2f5b48c2010-09-10 11:15:36 -05001679static void flush_qp(struct c4iw_qp *qhp)
Steve Wisecfdda9d2010-04-21 15:30:06 -07001680{
1681 struct c4iw_cq *rchp, *schp;
Kumar Sanghvi581bbe22011-10-24 21:20:21 +05301682 unsigned long flag;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001683
Steve Wise1cf24dc2013-08-06 21:04:35 +05301684 rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
1685 schp = to_c4iw_cq(qhp->ibqp.send_cq);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001686
1687 if (qhp->ibqp.uobject) {
Steve Wise308aa2b2018-08-31 07:15:56 -07001688
1689 /* for user qps, qhp->wq.flushed is protected by qhp->mutex */
1690 if (qhp->wq.flushed)
1691 return;
1692
1693 qhp->wq.flushed = 1;
Raju Rangoju6a0b6172018-07-25 21:22:14 +05301694 t4_set_wq_in_error(&qhp->wq, 0);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001695 t4_set_cq_in_error(&rchp->cq);
Kumar Sanghvi581bbe22011-10-24 21:20:21 +05301696 spin_lock_irqsave(&rchp->comp_handler_lock, flag);
Kumar Sanghvi01e7da62011-10-13 13:51:30 +05301697 (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
Kumar Sanghvi581bbe22011-10-24 21:20:21 +05301698 spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
Kumar Sanghvi01e7da62011-10-13 13:51:30 +05301699 if (schp != rchp) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07001700 t4_set_cq_in_error(&schp->cq);
Kumar Sanghvi581bbe22011-10-24 21:20:21 +05301701 spin_lock_irqsave(&schp->comp_handler_lock, flag);
Kumar Sanghvi01e7da62011-10-13 13:51:30 +05301702 (*schp->ibcq.comp_handler)(&schp->ibcq,
1703 schp->ibcq.cq_context);
Kumar Sanghvi581bbe22011-10-24 21:20:21 +05301704 spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
Kumar Sanghvi01e7da62011-10-13 13:51:30 +05301705 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001706 return;
1707 }
Steve Wise2f5b48c2010-09-10 11:15:36 -05001708 __flush_qp(qhp, rchp, schp);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001709}
1710
Steve Wise73d6fca2010-07-23 19:12:27 +00001711static int rdma_fini(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
1712 struct c4iw_ep *ep)
Steve Wisecfdda9d2010-04-21 15:30:06 -07001713{
1714 struct fw_ri_wr *wqe;
1715 int ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001716 struct sk_buff *skb;
1717
Bharat Potnuri548ddb12017-09-27 13:05:49 +05301718 pr_debug("qhp %p qid 0x%x tid %u\n", qhp, qhp->wq.sq.qid, ep->hwtid);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001719
Hariprasad S4a740832016-06-10 01:05:15 +05301720 skb = skb_dequeue(&ep->com.ep_skb_list);
1721 if (WARN_ON(!skb))
Steve Wisecfdda9d2010-04-21 15:30:06 -07001722 return -ENOMEM;
Hariprasad S4a740832016-06-10 01:05:15 +05301723
Steve Wise73d6fca2010-07-23 19:12:27 +00001724 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001725
YueHaibingecb238f2018-04-28 15:31:06 +08001726 wqe = __skb_put_zero(skb, sizeof(*wqe));
Steve Wisecfdda9d2010-04-21 15:30:06 -07001727 wqe->op_compl = cpu_to_be32(
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301728 FW_WR_OP_V(FW_RI_INIT_WR) |
1729 FW_WR_COMPL_F);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001730 wqe->flowid_len16 = cpu_to_be32(
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301731 FW_WR_FLOWID_V(ep->hwtid) |
1732 FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
Steve Wiseef885dc2017-09-26 13:12:16 -07001733 wqe->cookie = (uintptr_t)ep->com.wr_waitp;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001734
1735 wqe->u.fini.type = FW_RI_TYPE_FINI;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001736
Steve Wise2015f262017-09-26 13:13:17 -07001737 ret = c4iw_ref_send_wait(&rhp->rdev, skb, ep->com.wr_waitp,
1738 qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
1739
Bharat Potnuri548ddb12017-09-27 13:05:49 +05301740 pr_debug("ret %d\n", ret);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001741 return ret;
1742}
1743
1744static void build_rtr_msg(u8 p2p_type, struct fw_ri_init *init)
1745{
Bharat Potnuri548ddb12017-09-27 13:05:49 +05301746 pr_debug("p2p_type = %d\n", p2p_type);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001747 memset(&init->u, 0, sizeof init->u);
1748 switch (p2p_type) {
1749 case FW_RI_INIT_P2PTYPE_RDMA_WRITE:
1750 init->u.write.opcode = FW_RI_RDMA_WRITE_WR;
1751 init->u.write.stag_sink = cpu_to_be32(1);
1752 init->u.write.to_sink = cpu_to_be64(1);
1753 init->u.write.u.immd_src[0].op = FW_RI_DATA_IMMD;
1754 init->u.write.len16 = DIV_ROUND_UP(sizeof init->u.write +
1755 sizeof(struct fw_ri_immd),
1756 16);
1757 break;
1758 case FW_RI_INIT_P2PTYPE_READ_REQ:
1759 init->u.write.opcode = FW_RI_RDMA_READ_WR;
1760 init->u.read.stag_src = cpu_to_be32(1);
1761 init->u.read.to_src_lo = cpu_to_be32(1);
1762 init->u.read.stag_sink = cpu_to_be32(1);
1763 init->u.read.to_sink_lo = cpu_to_be32(1);
1764 init->u.read.len16 = DIV_ROUND_UP(sizeof init->u.read, 16);
1765 break;
1766 }
1767}
1768
1769static int rdma_init(struct c4iw_dev *rhp, struct c4iw_qp *qhp)
1770{
1771 struct fw_ri_wr *wqe;
1772 int ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001773 struct sk_buff *skb;
1774
Bharat Potnuri548ddb12017-09-27 13:05:49 +05301775 pr_debug("qhp %p qid 0x%x tid %u ird %u ord %u\n", qhp,
Joe Perchesa9a42882017-02-09 14:23:51 -08001776 qhp->wq.sq.qid, qhp->ep->hwtid, qhp->ep->ird, qhp->ep->ord);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001777
David Rientjesd3c814e2010-07-21 02:44:56 +00001778 skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05301779 if (!skb) {
1780 ret = -ENOMEM;
1781 goto out;
1782 }
1783 ret = alloc_ird(rhp, qhp->attr.max_ird);
1784 if (ret) {
1785 qhp->attr.max_ird = 0;
1786 kfree_skb(skb);
1787 goto out;
1788 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001789 set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
1790
YueHaibingecb238f2018-04-28 15:31:06 +08001791 wqe = __skb_put_zero(skb, sizeof(*wqe));
Steve Wisecfdda9d2010-04-21 15:30:06 -07001792 wqe->op_compl = cpu_to_be32(
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301793 FW_WR_OP_V(FW_RI_INIT_WR) |
1794 FW_WR_COMPL_F);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001795 wqe->flowid_len16 = cpu_to_be32(
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301796 FW_WR_FLOWID_V(qhp->ep->hwtid) |
1797 FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
Steve Wisecfdda9d2010-04-21 15:30:06 -07001798
Steve Wiseef885dc2017-09-26 13:12:16 -07001799 wqe->cookie = (uintptr_t)qhp->ep->com.wr_waitp;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001800
1801 wqe->u.init.type = FW_RI_TYPE_INIT;
1802 wqe->u.init.mpareqbit_p2ptype =
Hariprasad Shenaicf7fe642015-01-16 09:24:48 +05301803 FW_RI_WR_MPAREQBIT_V(qhp->attr.mpa_attr.initiator) |
1804 FW_RI_WR_P2PTYPE_V(qhp->attr.mpa_attr.p2p_type);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001805 wqe->u.init.mpa_attrs = FW_RI_MPA_IETF_ENABLE;
1806 if (qhp->attr.mpa_attr.recv_marker_enabled)
1807 wqe->u.init.mpa_attrs |= FW_RI_MPA_RX_MARKER_ENABLE;
1808 if (qhp->attr.mpa_attr.xmit_marker_enabled)
1809 wqe->u.init.mpa_attrs |= FW_RI_MPA_TX_MARKER_ENABLE;
1810 if (qhp->attr.mpa_attr.crc_enabled)
1811 wqe->u.init.mpa_attrs |= FW_RI_MPA_CRC_ENABLE;
1812
1813 wqe->u.init.qp_caps = FW_RI_QP_RDMA_READ_ENABLE |
1814 FW_RI_QP_RDMA_WRITE_ENABLE |
1815 FW_RI_QP_BIND_ENABLE;
1816 if (!qhp->ibqp.uobject)
1817 wqe->u.init.qp_caps |= FW_RI_QP_FAST_REGISTER_ENABLE |
1818 FW_RI_QP_STAG0_ENABLE;
1819 wqe->u.init.nrqe = cpu_to_be16(t4_rqes_posted(&qhp->wq));
1820 wqe->u.init.pdid = cpu_to_be32(qhp->attr.pd);
1821 wqe->u.init.qpid = cpu_to_be32(qhp->wq.sq.qid);
1822 wqe->u.init.sq_eqid = cpu_to_be32(qhp->wq.sq.qid);
Raju Rangoju6a0b6172018-07-25 21:22:14 +05301823 if (qhp->srq) {
1824 wqe->u.init.rq_eqid = cpu_to_be32(FW_RI_INIT_RQEQID_SRQ |
1825 qhp->srq->idx);
1826 } else {
1827 wqe->u.init.rq_eqid = cpu_to_be32(qhp->wq.rq.qid);
1828 wqe->u.init.hwrqsize = cpu_to_be32(qhp->wq.rq.rqt_size);
1829 wqe->u.init.hwrqaddr = cpu_to_be32(qhp->wq.rq.rqt_hwaddr -
1830 rhp->rdev.lldi.vr->rq.start);
1831 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001832 wqe->u.init.scqid = cpu_to_be32(qhp->attr.scq);
1833 wqe->u.init.rcqid = cpu_to_be32(qhp->attr.rcq);
1834 wqe->u.init.ord_max = cpu_to_be32(qhp->attr.max_ord);
1835 wqe->u.init.ird_max = cpu_to_be32(qhp->attr.max_ird);
1836 wqe->u.init.iss = cpu_to_be32(qhp->ep->snd_seq);
1837 wqe->u.init.irs = cpu_to_be32(qhp->ep->rcv_seq);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001838 if (qhp->attr.mpa_attr.initiator)
1839 build_rtr_msg(qhp->attr.mpa_attr.p2p_type, &wqe->u.init);
1840
Steve Wise2015f262017-09-26 13:13:17 -07001841 ret = c4iw_ref_send_wait(&rhp->rdev, skb, qhp->ep->com.wr_waitp,
1842 qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05301843 if (!ret)
1844 goto out;
Steve Wise2015f262017-09-26 13:13:17 -07001845
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05301846 free_ird(rhp, qhp->attr.max_ird);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001847out:
Bharat Potnuri548ddb12017-09-27 13:05:49 +05301848 pr_debug("ret %d\n", ret);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001849 return ret;
1850}
1851
1852int c4iw_modify_qp(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
1853 enum c4iw_qp_attr_mask mask,
1854 struct c4iw_qp_attributes *attrs,
1855 int internal)
1856{
1857 int ret = 0;
1858 struct c4iw_qp_attributes newattr = qhp->attr;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001859 int disconnect = 0;
1860 int terminate = 0;
1861 int abort = 0;
1862 int free = 0;
1863 struct c4iw_ep *ep = NULL;
1864
Bharat Potnuri548ddb12017-09-27 13:05:49 +05301865 pr_debug("qhp %p sqid 0x%x rqid 0x%x ep %p state %d -> %d\n",
Joe Perchesa9a42882017-02-09 14:23:51 -08001866 qhp, qhp->wq.sq.qid, qhp->wq.rq.qid, qhp->ep, qhp->attr.state,
1867 (mask & C4IW_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001868
Steve Wise2f5b48c2010-09-10 11:15:36 -05001869 mutex_lock(&qhp->mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001870
1871 /* Process attr changes if in IDLE */
1872 if (mask & C4IW_QP_ATTR_VALID_MODIFY) {
1873 if (qhp->attr.state != C4IW_QP_STATE_IDLE) {
1874 ret = -EIO;
1875 goto out;
1876 }
1877 if (mask & C4IW_QP_ATTR_ENABLE_RDMA_READ)
1878 newattr.enable_rdma_read = attrs->enable_rdma_read;
1879 if (mask & C4IW_QP_ATTR_ENABLE_RDMA_WRITE)
1880 newattr.enable_rdma_write = attrs->enable_rdma_write;
1881 if (mask & C4IW_QP_ATTR_ENABLE_RDMA_BIND)
1882 newattr.enable_bind = attrs->enable_bind;
1883 if (mask & C4IW_QP_ATTR_MAX_ORD) {
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001884 if (attrs->max_ord > c4iw_max_read_depth) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07001885 ret = -EINVAL;
1886 goto out;
1887 }
1888 newattr.max_ord = attrs->max_ord;
1889 }
1890 if (mask & C4IW_QP_ATTR_MAX_IRD) {
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05301891 if (attrs->max_ird > cur_max_read_depth(rhp)) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07001892 ret = -EINVAL;
1893 goto out;
1894 }
1895 newattr.max_ird = attrs->max_ird;
1896 }
1897 qhp->attr = newattr;
1898 }
1899
Vipul Pandya2c974782012-05-18 15:29:28 +05301900 if (mask & C4IW_QP_ATTR_SQ_DB) {
Steve Wise05eb2382014-03-14 21:52:08 +05301901 ret = ring_kernel_sq_db(qhp, attrs->sq_db_inc);
Vipul Pandya2c974782012-05-18 15:29:28 +05301902 goto out;
1903 }
1904 if (mask & C4IW_QP_ATTR_RQ_DB) {
Steve Wise05eb2382014-03-14 21:52:08 +05301905 ret = ring_kernel_rq_db(qhp, attrs->rq_db_inc);
Vipul Pandya2c974782012-05-18 15:29:28 +05301906 goto out;
1907 }
1908
Steve Wisecfdda9d2010-04-21 15:30:06 -07001909 if (!(mask & C4IW_QP_ATTR_NEXT_STATE))
1910 goto out;
1911 if (qhp->attr.state == attrs->next_state)
1912 goto out;
1913
1914 switch (qhp->attr.state) {
1915 case C4IW_QP_STATE_IDLE:
1916 switch (attrs->next_state) {
1917 case C4IW_QP_STATE_RTS:
1918 if (!(mask & C4IW_QP_ATTR_LLP_STREAM_HANDLE)) {
1919 ret = -EINVAL;
1920 goto out;
1921 }
1922 if (!(mask & C4IW_QP_ATTR_MPA_ATTR)) {
1923 ret = -EINVAL;
1924 goto out;
1925 }
1926 qhp->attr.mpa_attr = attrs->mpa_attr;
1927 qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
1928 qhp->ep = qhp->attr.llp_stream_handle;
Steve Wise2f5b48c2010-09-10 11:15:36 -05001929 set_state(qhp, C4IW_QP_STATE_RTS);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001930
1931 /*
1932 * Ref the endpoint here and deref when we
1933 * disassociate the endpoint from the QP. This
1934 * happens in CLOSING->IDLE transition or *->ERROR
1935 * transition.
1936 */
1937 c4iw_get_ep(&qhp->ep->com);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001938 ret = rdma_init(rhp, qhp);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001939 if (ret)
1940 goto err;
1941 break;
1942 case C4IW_QP_STATE_ERROR:
Steve Wise2f5b48c2010-09-10 11:15:36 -05001943 set_state(qhp, C4IW_QP_STATE_ERROR);
1944 flush_qp(qhp);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001945 break;
1946 default:
1947 ret = -EINVAL;
1948 goto out;
1949 }
1950 break;
1951 case C4IW_QP_STATE_RTS:
1952 switch (attrs->next_state) {
1953 case C4IW_QP_STATE_CLOSING:
Raju Rangoju6a0b6172018-07-25 21:22:14 +05301954 t4_set_wq_in_error(&qhp->wq, 0);
Steve Wise2f5b48c2010-09-10 11:15:36 -05001955 set_state(qhp, C4IW_QP_STATE_CLOSING);
Steve Wise73d6fca2010-07-23 19:12:27 +00001956 ep = qhp->ep;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001957 if (!internal) {
1958 abort = 0;
1959 disconnect = 1;
Steve Wise2f5b48c2010-09-10 11:15:36 -05001960 c4iw_get_ep(&qhp->ep->com);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001961 }
Steve Wise73d6fca2010-07-23 19:12:27 +00001962 ret = rdma_fini(rhp, qhp, ep);
Steve Wise8da7e7a2011-06-14 20:59:27 +00001963 if (ret)
Steve Wisecfdda9d2010-04-21 15:30:06 -07001964 goto err;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001965 break;
1966 case C4IW_QP_STATE_TERMINATE:
Raju Rangoju6a0b6172018-07-25 21:22:14 +05301967 t4_set_wq_in_error(&qhp->wq, 0);
Steve Wise2f5b48c2010-09-10 11:15:36 -05001968 set_state(qhp, C4IW_QP_STATE_TERMINATE);
Kumar Sanghvid2fe99e2011-09-25 20:17:44 +05301969 qhp->attr.layer_etype = attrs->layer_etype;
1970 qhp->attr.ecode = attrs->ecode;
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001971 ep = qhp->ep;
Steve Wisecc18b932014-04-24 14:31:53 -05001972 if (!internal) {
1973 c4iw_get_ep(&qhp->ep->com);
Steve Wise0e42c1f2010-09-10 11:15:09 -05001974 terminate = 1;
Steve Wisecc18b932014-04-24 14:31:53 -05001975 disconnect = 1;
1976 } else {
1977 terminate = qhp->attr.send_term;
Steve Wise09992572013-08-06 21:04:40 +05301978 ret = rdma_fini(rhp, qhp, ep);
1979 if (ret)
1980 goto err;
1981 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001982 break;
1983 case C4IW_QP_STATE_ERROR:
Raju Rangoju6a0b6172018-07-25 21:22:14 +05301984 t4_set_wq_in_error(&qhp->wq, 0);
Steve Wiseb4e29012014-04-09 09:38:26 -05001985 set_state(qhp, C4IW_QP_STATE_ERROR);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001986 if (!internal) {
1987 abort = 1;
1988 disconnect = 1;
1989 ep = qhp->ep;
Steve Wise2f5b48c2010-09-10 11:15:36 -05001990 c4iw_get_ep(&qhp->ep->com);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001991 }
1992 goto err;
1993 break;
1994 default:
1995 ret = -EINVAL;
1996 goto out;
1997 }
1998 break;
1999 case C4IW_QP_STATE_CLOSING:
Steve Wise4fe7c292016-12-22 07:04:59 -08002000
2001 /*
2002 * Allow kernel users to move to ERROR for qp draining.
2003 */
2004 if (!internal && (qhp->ibqp.uobject || attrs->next_state !=
2005 C4IW_QP_STATE_ERROR)) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07002006 ret = -EINVAL;
2007 goto out;
2008 }
2009 switch (attrs->next_state) {
2010 case C4IW_QP_STATE_IDLE:
Steve Wise2f5b48c2010-09-10 11:15:36 -05002011 flush_qp(qhp);
2012 set_state(qhp, C4IW_QP_STATE_IDLE);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002013 qhp->attr.llp_stream_handle = NULL;
2014 c4iw_put_ep(&qhp->ep->com);
2015 qhp->ep = NULL;
2016 wake_up(&qhp->wait);
2017 break;
2018 case C4IW_QP_STATE_ERROR:
2019 goto err;
2020 default:
2021 ret = -EINVAL;
2022 goto err;
2023 }
2024 break;
2025 case C4IW_QP_STATE_ERROR:
2026 if (attrs->next_state != C4IW_QP_STATE_IDLE) {
2027 ret = -EINVAL;
2028 goto out;
2029 }
2030 if (!t4_sq_empty(&qhp->wq) || !t4_rq_empty(&qhp->wq)) {
2031 ret = -EINVAL;
2032 goto out;
2033 }
Steve Wise2f5b48c2010-09-10 11:15:36 -05002034 set_state(qhp, C4IW_QP_STATE_IDLE);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002035 break;
2036 case C4IW_QP_STATE_TERMINATE:
2037 if (!internal) {
2038 ret = -EINVAL;
2039 goto out;
2040 }
2041 goto err;
2042 break;
2043 default:
Joe Perches700456b2017-02-09 14:23:50 -08002044 pr_err("%s in a bad state %d\n", __func__, qhp->attr.state);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002045 ret = -EINVAL;
2046 goto err;
2047 break;
2048 }
2049 goto out;
2050err:
Bharat Potnuri548ddb12017-09-27 13:05:49 +05302051 pr_debug("disassociating ep %p qpid 0x%x\n", qhp->ep,
Joe Perchesa9a42882017-02-09 14:23:51 -08002052 qhp->wq.sq.qid);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002053
2054 /* disassociate the LLP connection */
2055 qhp->attr.llp_stream_handle = NULL;
Steve Wiseaf93fb52010-09-10 11:14:48 -05002056 if (!ep)
2057 ep = qhp->ep;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002058 qhp->ep = NULL;
Steve Wise2f5b48c2010-09-10 11:15:36 -05002059 set_state(qhp, C4IW_QP_STATE_ERROR);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002060 free = 1;
Vipul Pandya91e9c0712013-01-07 13:11:51 +00002061 abort = 1;
Steve Wise2f5b48c2010-09-10 11:15:36 -05002062 flush_qp(qhp);
Steve Wise5b3418082014-11-21 09:36:36 -06002063 wake_up(&qhp->wait);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002064out:
Steve Wise2f5b48c2010-09-10 11:15:36 -05002065 mutex_unlock(&qhp->mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002066
2067 if (terminate)
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002068 post_terminate(qhp, NULL, internal ? GFP_ATOMIC : GFP_KERNEL);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002069
2070 /*
2071 * If disconnect is 1, then we need to initiate a disconnect
2072 * on the EP. This can be a normal close (RTS->CLOSING) or
2073 * an abnormal close (RTS/CLOSING->ERROR).
2074 */
2075 if (disconnect) {
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07002076 c4iw_ep_disconnect(ep, abort, internal ? GFP_ATOMIC :
2077 GFP_KERNEL);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002078 c4iw_put_ep(&ep->com);
2079 }
2080
2081 /*
2082 * If free is 1, then we've disassociated the EP from the QP
2083 * and we need to dereference the EP.
2084 */
2085 if (free)
2086 c4iw_put_ep(&ep->com);
Bharat Potnuri548ddb12017-09-27 13:05:49 +05302087 pr_debug("exit state %d\n", qhp->attr.state);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002088 return ret;
2089}
2090
2091int c4iw_destroy_qp(struct ib_qp *ib_qp)
2092{
2093 struct c4iw_dev *rhp;
2094 struct c4iw_qp *qhp;
2095 struct c4iw_qp_attributes attrs;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002096
2097 qhp = to_c4iw_qp(ib_qp);
2098 rhp = qhp->rhp;
2099
2100 attrs.next_state = C4IW_QP_STATE_ERROR;
Kumar Sanghvid2fe99e2011-09-25 20:17:44 +05302101 if (qhp->attr.state == C4IW_QP_STATE_TERMINATE)
2102 c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2103 else
2104 c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002105 wait_event(qhp->wait, !qhp->ep);
2106
Steve Wise05eb2382014-03-14 21:52:08 +05302107 remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002108
Steve Wise05eb2382014-03-14 21:52:08 +05302109 spin_lock_irq(&rhp->lock);
2110 if (!list_empty(&qhp->db_fc_entry))
2111 list_del_init(&qhp->db_fc_entry);
2112 spin_unlock_irq(&rhp->lock);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05302113 free_ird(rhp, qhp->attr.max_ird);
Steve Wise05eb2382014-03-14 21:52:08 +05302114
Steve Wisead61a4c2016-07-29 11:00:54 -07002115 c4iw_qp_rem_ref(ib_qp);
2116
Bharat Potnuri548ddb12017-09-27 13:05:49 +05302117 pr_debug("ib_qp %p qpid 0x%0x\n", ib_qp, qhp->wq.sq.qid);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002118 return 0;
2119}
2120
2121struct ib_qp *c4iw_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
2122 struct ib_udata *udata)
2123{
2124 struct c4iw_dev *rhp;
2125 struct c4iw_qp *qhp;
2126 struct c4iw_pd *php;
2127 struct c4iw_cq *schp;
2128 struct c4iw_cq *rchp;
2129 struct c4iw_create_qp_resp uresp;
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302130 unsigned int sqsize, rqsize = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002131 struct c4iw_ucontext *ucontext;
2132 int ret;
Hariprasad Sa6054df2016-02-05 11:43:28 +05302133 struct c4iw_mm_entry *sq_key_mm, *rq_key_mm = NULL, *sq_db_key_mm;
2134 struct c4iw_mm_entry *rq_db_key_mm = NULL, *ma_sync_key_mm = NULL;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002135
Bharat Potnuri548ddb12017-09-27 13:05:49 +05302136 pr_debug("ib_pd %p\n", pd);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002137
2138 if (attrs->qp_type != IB_QPT_RC)
2139 return ERR_PTR(-EINVAL);
2140
2141 php = to_c4iw_pd(pd);
2142 rhp = php->rhp;
2143 schp = get_chp(rhp, ((struct c4iw_cq *)attrs->send_cq)->cq.cqid);
2144 rchp = get_chp(rhp, ((struct c4iw_cq *)attrs->recv_cq)->cq.cqid);
2145 if (!schp || !rchp)
2146 return ERR_PTR(-EINVAL);
2147
2148 if (attrs->cap.max_inline_data > T4_MAX_SEND_INLINE)
2149 return ERR_PTR(-EINVAL);
2150
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302151 if (!attrs->srq) {
2152 if (attrs->cap.max_recv_wr > rhp->rdev.hw_queue.t4_max_rq_size)
2153 return ERR_PTR(-E2BIG);
2154 rqsize = attrs->cap.max_recv_wr + 1;
2155 if (rqsize < 8)
2156 rqsize = 8;
2157 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07002158
Hariprasad Shenai66eb19a2014-07-21 20:55:15 +05302159 if (attrs->cap.max_send_wr > rhp->rdev.hw_queue.t4_max_sq_size)
Steve Wisecfdda9d2010-04-21 15:30:06 -07002160 return ERR_PTR(-E2BIG);
Hariprasad Shenai66eb19a2014-07-21 20:55:15 +05302161 sqsize = attrs->cap.max_send_wr + 1;
2162 if (sqsize < 8)
2163 sqsize = 8;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002164
2165 ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
2166
Steve Wisecfdda9d2010-04-21 15:30:06 -07002167 qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
2168 if (!qhp)
2169 return ERR_PTR(-ENOMEM);
Steve Wise7088a9b2017-09-26 13:11:36 -07002170
Steve Wise2015f262017-09-26 13:13:17 -07002171 qhp->wr_waitp = c4iw_alloc_wr_wait(GFP_KERNEL);
Steve Wise7088a9b2017-09-26 13:11:36 -07002172 if (!qhp->wr_waitp) {
2173 ret = -ENOMEM;
2174 goto err_free_qhp;
2175 }
2176
Steve Wisecfdda9d2010-04-21 15:30:06 -07002177 qhp->wq.sq.size = sqsize;
Hariprasad Shenai66eb19a2014-07-21 20:55:15 +05302178 qhp->wq.sq.memsize =
2179 (sqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
2180 sizeof(*qhp->wq.sq.queue) + 16 * sizeof(__be64);
Steve Wise1cf24dc2013-08-06 21:04:35 +05302181 qhp->wq.sq.flush_cidx = -1;
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302182 if (!attrs->srq) {
2183 qhp->wq.rq.size = rqsize;
2184 qhp->wq.rq.memsize =
2185 (rqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
2186 sizeof(*qhp->wq.rq.queue);
2187 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07002188
2189 if (ucontext) {
2190 qhp->wq.sq.memsize = roundup(qhp->wq.sq.memsize, PAGE_SIZE);
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302191 if (!attrs->srq)
2192 qhp->wq.rq.memsize =
2193 roundup(qhp->wq.rq.memsize, PAGE_SIZE);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002194 }
2195
Steve Wisecfdda9d2010-04-21 15:30:06 -07002196 ret = create_qp(&rhp->rdev, &qhp->wq, &schp->cq, &rchp->cq,
Steve Wise7088a9b2017-09-26 13:11:36 -07002197 ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302198 qhp->wr_waitp, !attrs->srq);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002199 if (ret)
Steve Wise7088a9b2017-09-26 13:11:36 -07002200 goto err_free_wr_wait;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002201
2202 attrs->cap.max_recv_wr = rqsize - 1;
2203 attrs->cap.max_send_wr = sqsize - 1;
2204 attrs->cap.max_inline_data = T4_MAX_SEND_INLINE;
2205
2206 qhp->rhp = rhp;
2207 qhp->attr.pd = php->pdid;
2208 qhp->attr.scq = ((struct c4iw_cq *) attrs->send_cq)->cq.cqid;
2209 qhp->attr.rcq = ((struct c4iw_cq *) attrs->recv_cq)->cq.cqid;
2210 qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002211 qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
2212 qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302213 if (!attrs->srq) {
2214 qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
2215 qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
2216 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07002217 qhp->attr.state = C4IW_QP_STATE_IDLE;
2218 qhp->attr.next_state = C4IW_QP_STATE_IDLE;
2219 qhp->attr.enable_rdma_read = 1;
2220 qhp->attr.enable_rdma_write = 1;
2221 qhp->attr.enable_bind = 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05302222 qhp->attr.max_ord = 0;
2223 qhp->attr.max_ird = 0;
Steve Wiseba32de92014-03-19 17:44:43 +05302224 qhp->sq_sig_all = attrs->sq_sig_type == IB_SIGNAL_ALL_WR;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002225 spin_lock_init(&qhp->lock);
Steve Wise2f5b48c2010-09-10 11:15:36 -05002226 mutex_init(&qhp->mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002227 init_waitqueue_head(&qhp->wait);
Steve Wisead61a4c2016-07-29 11:00:54 -07002228 kref_init(&qhp->kref);
Steve Wisec12a67f2016-12-22 07:40:36 -08002229 INIT_WORK(&qhp->free_work, free_qp_work);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002230
Steve Wise05eb2382014-03-14 21:52:08 +05302231 ret = insert_handle(rhp, &rhp->qpidr, qhp, qhp->wq.sq.qid);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002232 if (ret)
Steve Wise7088a9b2017-09-26 13:11:36 -07002233 goto err_destroy_qp;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002234
Leon Romanovsky9950acf2017-10-29 21:34:35 +02002235 if (udata && ucontext) {
Hariprasad Sa6054df2016-02-05 11:43:28 +05302236 sq_key_mm = kmalloc(sizeof(*sq_key_mm), GFP_KERNEL);
2237 if (!sq_key_mm) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07002238 ret = -ENOMEM;
Steve Wise7088a9b2017-09-26 13:11:36 -07002239 goto err_remove_handle;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002240 }
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302241 if (!attrs->srq) {
2242 rq_key_mm = kmalloc(sizeof(*rq_key_mm), GFP_KERNEL);
2243 if (!rq_key_mm) {
2244 ret = -ENOMEM;
2245 goto err_free_sq_key;
2246 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07002247 }
Hariprasad Sa6054df2016-02-05 11:43:28 +05302248 sq_db_key_mm = kmalloc(sizeof(*sq_db_key_mm), GFP_KERNEL);
2249 if (!sq_db_key_mm) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07002250 ret = -ENOMEM;
Steve Wise7088a9b2017-09-26 13:11:36 -07002251 goto err_free_rq_key;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002252 }
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302253 if (!attrs->srq) {
2254 rq_db_key_mm =
2255 kmalloc(sizeof(*rq_db_key_mm), GFP_KERNEL);
2256 if (!rq_db_key_mm) {
2257 ret = -ENOMEM;
2258 goto err_free_sq_db_key;
2259 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07002260 }
Dan Carpenter8001b712018-08-02 10:56:13 +03002261 memset(&uresp, 0, sizeof(uresp));
Steve Wisec6d7b262010-09-13 11:23:57 -05002262 if (t4_sq_onchip(&qhp->wq.sq)) {
Hariprasad Sa6054df2016-02-05 11:43:28 +05302263 ma_sync_key_mm = kmalloc(sizeof(*ma_sync_key_mm),
2264 GFP_KERNEL);
2265 if (!ma_sync_key_mm) {
Steve Wisec6d7b262010-09-13 11:23:57 -05002266 ret = -ENOMEM;
Steve Wise7088a9b2017-09-26 13:11:36 -07002267 goto err_free_rq_db_key;
Steve Wisec6d7b262010-09-13 11:23:57 -05002268 }
2269 uresp.flags = C4IW_QPF_ONCHIP;
Dan Carpenter8001b712018-08-02 10:56:13 +03002270 }
Potnuri Bharat Tejab9855f42018-08-02 11:33:03 +05302271 if (rhp->rdev.lldi.write_w_imm_support)
2272 uresp.flags |= C4IW_QPF_WRITE_W_IMM;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002273 uresp.qid_mask = rhp->rdev.qpmask;
2274 uresp.sqid = qhp->wq.sq.qid;
2275 uresp.sq_size = qhp->wq.sq.size;
2276 uresp.sq_memsize = qhp->wq.sq.memsize;
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302277 if (!attrs->srq) {
2278 uresp.rqid = qhp->wq.rq.qid;
2279 uresp.rq_size = qhp->wq.rq.size;
2280 uresp.rq_memsize = qhp->wq.rq.memsize;
2281 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07002282 spin_lock(&ucontext->mmap_lock);
Hariprasad Sa6054df2016-02-05 11:43:28 +05302283 if (ma_sync_key_mm) {
Steve Wisec6d7b262010-09-13 11:23:57 -05002284 uresp.ma_sync_key = ucontext->key;
2285 ucontext->key += PAGE_SIZE;
2286 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07002287 uresp.sq_key = ucontext->key;
2288 ucontext->key += PAGE_SIZE;
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302289 if (!attrs->srq) {
2290 uresp.rq_key = ucontext->key;
2291 ucontext->key += PAGE_SIZE;
2292 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07002293 uresp.sq_db_gts_key = ucontext->key;
2294 ucontext->key += PAGE_SIZE;
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302295 if (!attrs->srq) {
2296 uresp.rq_db_gts_key = ucontext->key;
2297 ucontext->key += PAGE_SIZE;
2298 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07002299 spin_unlock(&ucontext->mmap_lock);
2300 ret = ib_copy_to_udata(udata, &uresp, sizeof uresp);
2301 if (ret)
Steve Wise7088a9b2017-09-26 13:11:36 -07002302 goto err_free_ma_sync_key;
Hariprasad Sa6054df2016-02-05 11:43:28 +05302303 sq_key_mm->key = uresp.sq_key;
2304 sq_key_mm->addr = qhp->wq.sq.phys_addr;
2305 sq_key_mm->len = PAGE_ALIGN(qhp->wq.sq.memsize);
2306 insert_mmap(ucontext, sq_key_mm);
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302307 if (!attrs->srq) {
2308 rq_key_mm->key = uresp.rq_key;
2309 rq_key_mm->addr = virt_to_phys(qhp->wq.rq.queue);
2310 rq_key_mm->len = PAGE_ALIGN(qhp->wq.rq.memsize);
2311 insert_mmap(ucontext, rq_key_mm);
2312 }
Hariprasad Sa6054df2016-02-05 11:43:28 +05302313 sq_db_key_mm->key = uresp.sq_db_gts_key;
2314 sq_db_key_mm->addr = (u64)(unsigned long)qhp->wq.sq.bar2_pa;
2315 sq_db_key_mm->len = PAGE_SIZE;
2316 insert_mmap(ucontext, sq_db_key_mm);
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302317 if (!attrs->srq) {
2318 rq_db_key_mm->key = uresp.rq_db_gts_key;
2319 rq_db_key_mm->addr =
2320 (u64)(unsigned long)qhp->wq.rq.bar2_pa;
2321 rq_db_key_mm->len = PAGE_SIZE;
2322 insert_mmap(ucontext, rq_db_key_mm);
2323 }
Hariprasad Sa6054df2016-02-05 11:43:28 +05302324 if (ma_sync_key_mm) {
2325 ma_sync_key_mm->key = uresp.ma_sync_key;
2326 ma_sync_key_mm->addr =
2327 (pci_resource_start(rhp->rdev.lldi.pdev, 0) +
2328 PCIE_MA_SYNC_A) & PAGE_MASK;
2329 ma_sync_key_mm->len = PAGE_SIZE;
2330 insert_mmap(ucontext, ma_sync_key_mm);
Steve Wisec6d7b262010-09-13 11:23:57 -05002331 }
Steve Wisec12a67f2016-12-22 07:40:36 -08002332
2333 c4iw_get_ucontext(ucontext);
2334 qhp->ucontext = ucontext;
Steve Wisecfdda9d2010-04-21 15:30:06 -07002335 }
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302336 if (!attrs->srq) {
2337 qhp->wq.qp_errp =
2338 &qhp->wq.rq.queue[qhp->wq.rq.size].status.qp_err;
2339 } else {
2340 qhp->wq.qp_errp =
2341 &qhp->wq.sq.queue[qhp->wq.sq.size].status.qp_err;
2342 qhp->wq.srqidxp =
2343 &qhp->wq.sq.queue[qhp->wq.sq.size].status.srqidx;
2344 }
2345
Steve Wisecfdda9d2010-04-21 15:30:06 -07002346 qhp->ibqp.qp_num = qhp->wq.sq.qid;
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302347 if (attrs->srq)
2348 qhp->srq = to_c4iw_srq(attrs->srq);
Steve Wise05eb2382014-03-14 21:52:08 +05302349 INIT_LIST_HEAD(&qhp->db_fc_entry);
Bharat Potnuri548ddb12017-09-27 13:05:49 +05302350 pr_debug("sq id %u size %u memsize %zu num_entries %u rq id %u size %u memsize %zu num_entries %u\n",
Joe Perchesa9a42882017-02-09 14:23:51 -08002351 qhp->wq.sq.qid, qhp->wq.sq.size, qhp->wq.sq.memsize,
2352 attrs->cap.max_send_wr, qhp->wq.rq.qid, qhp->wq.rq.size,
2353 qhp->wq.rq.memsize, attrs->cap.max_recv_wr);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002354 return &qhp->ibqp;
Steve Wise7088a9b2017-09-26 13:11:36 -07002355err_free_ma_sync_key:
Hariprasad Sa6054df2016-02-05 11:43:28 +05302356 kfree(ma_sync_key_mm);
Steve Wise7088a9b2017-09-26 13:11:36 -07002357err_free_rq_db_key:
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302358 if (!attrs->srq)
2359 kfree(rq_db_key_mm);
Steve Wise7088a9b2017-09-26 13:11:36 -07002360err_free_sq_db_key:
Hariprasad Sa6054df2016-02-05 11:43:28 +05302361 kfree(sq_db_key_mm);
Steve Wise7088a9b2017-09-26 13:11:36 -07002362err_free_rq_key:
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302363 if (!attrs->srq)
2364 kfree(rq_key_mm);
Steve Wise7088a9b2017-09-26 13:11:36 -07002365err_free_sq_key:
Hariprasad Sa6054df2016-02-05 11:43:28 +05302366 kfree(sq_key_mm);
Steve Wise7088a9b2017-09-26 13:11:36 -07002367err_remove_handle:
Steve Wisecfdda9d2010-04-21 15:30:06 -07002368 remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
Steve Wise7088a9b2017-09-26 13:11:36 -07002369err_destroy_qp:
Steve Wisecfdda9d2010-04-21 15:30:06 -07002370 destroy_qp(&rhp->rdev, &qhp->wq,
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302371 ucontext ? &ucontext->uctx : &rhp->rdev.uctx, !attrs->srq);
Steve Wise7088a9b2017-09-26 13:11:36 -07002372err_free_wr_wait:
Steve Wise2015f262017-09-26 13:13:17 -07002373 c4iw_put_wr_wait(qhp->wr_waitp);
Steve Wise7088a9b2017-09-26 13:11:36 -07002374err_free_qhp:
Steve Wisecfdda9d2010-04-21 15:30:06 -07002375 kfree(qhp);
2376 return ERR_PTR(ret);
2377}
2378
2379int c4iw_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2380 int attr_mask, struct ib_udata *udata)
2381{
2382 struct c4iw_dev *rhp;
2383 struct c4iw_qp *qhp;
2384 enum c4iw_qp_attr_mask mask = 0;
2385 struct c4iw_qp_attributes attrs;
2386
Bharat Potnuri548ddb12017-09-27 13:05:49 +05302387 pr_debug("ib_qp %p\n", ibqp);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002388
2389 /* iwarp does not support the RTR state */
2390 if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
2391 attr_mask &= ~IB_QP_STATE;
2392
2393 /* Make sure we still have something left to do */
2394 if (!attr_mask)
2395 return 0;
2396
2397 memset(&attrs, 0, sizeof attrs);
2398 qhp = to_c4iw_qp(ibqp);
2399 rhp = qhp->rhp;
2400
2401 attrs.next_state = c4iw_convert_state(attr->qp_state);
2402 attrs.enable_rdma_read = (attr->qp_access_flags &
2403 IB_ACCESS_REMOTE_READ) ? 1 : 0;
2404 attrs.enable_rdma_write = (attr->qp_access_flags &
2405 IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
2406 attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
2407
2408
2409 mask |= (attr_mask & IB_QP_STATE) ? C4IW_QP_ATTR_NEXT_STATE : 0;
2410 mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
2411 (C4IW_QP_ATTR_ENABLE_RDMA_READ |
2412 C4IW_QP_ATTR_ENABLE_RDMA_WRITE |
2413 C4IW_QP_ATTR_ENABLE_RDMA_BIND) : 0;
2414
Vipul Pandya2c974782012-05-18 15:29:28 +05302415 /*
2416 * Use SQ_PSN and RQ_PSN to pass in IDX_INC values for
2417 * ringing the queue db when we're in DB_FULL mode.
Steve Wisec2f9da92014-04-24 14:32:04 -05002418 * Only allow this on T4 devices.
Vipul Pandya2c974782012-05-18 15:29:28 +05302419 */
2420 attrs.sq_db_inc = attr->sq_psn;
2421 attrs.rq_db_inc = attr->rq_psn;
2422 mask |= (attr_mask & IB_QP_SQ_PSN) ? C4IW_QP_ATTR_SQ_DB : 0;
2423 mask |= (attr_mask & IB_QP_RQ_PSN) ? C4IW_QP_ATTR_RQ_DB : 0;
Hariprasad S963cab52015-09-23 17:19:27 +05302424 if (!is_t4(to_c4iw_qp(ibqp)->rhp->rdev.lldi.adapter_type) &&
Steve Wisec2f9da92014-04-24 14:32:04 -05002425 (mask & (C4IW_QP_ATTR_SQ_DB|C4IW_QP_ATTR_RQ_DB)))
2426 return -EINVAL;
Vipul Pandya2c974782012-05-18 15:29:28 +05302427
Steve Wisecfdda9d2010-04-21 15:30:06 -07002428 return c4iw_modify_qp(rhp, qhp, mask, &attrs, 0);
2429}
2430
2431struct ib_qp *c4iw_get_qp(struct ib_device *dev, int qpn)
2432{
Bharat Potnuri548ddb12017-09-27 13:05:49 +05302433 pr_debug("ib_dev %p qpn 0x%x\n", dev, qpn);
Steve Wisecfdda9d2010-04-21 15:30:06 -07002434 return (struct ib_qp *)get_qhp(to_c4iw_dev(dev), qpn);
2435}
Vipul Pandya67bbc052012-05-18 15:29:33 +05302436
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302437void c4iw_dispatch_srq_limit_reached_event(struct c4iw_srq *srq)
2438{
Bart Van Asschedd708e72018-07-31 08:51:30 -07002439 struct ib_event event = {};
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302440
2441 event.device = &srq->rhp->ibdev;
2442 event.element.srq = &srq->ibsrq;
2443 event.event = IB_EVENT_SRQ_LIMIT_REACHED;
2444 ib_dispatch_event(&event);
2445}
2446
2447int c4iw_modify_srq(struct ib_srq *ib_srq, struct ib_srq_attr *attr,
2448 enum ib_srq_attr_mask srq_attr_mask,
2449 struct ib_udata *udata)
2450{
2451 struct c4iw_srq *srq = to_c4iw_srq(ib_srq);
2452 int ret = 0;
2453
2454 /*
2455 * XXX 0 mask == a SW interrupt for srq_limit reached...
2456 */
2457 if (udata && !srq_attr_mask) {
2458 c4iw_dispatch_srq_limit_reached_event(srq);
2459 goto out;
2460 }
2461
2462 /* no support for this yet */
2463 if (srq_attr_mask & IB_SRQ_MAX_WR) {
2464 ret = -EINVAL;
2465 goto out;
2466 }
2467
2468 if (!udata && (srq_attr_mask & IB_SRQ_LIMIT)) {
2469 srq->armed = true;
2470 srq->srq_limit = attr->srq_limit;
2471 }
2472out:
2473 return ret;
2474}
2475
Vipul Pandya67bbc052012-05-18 15:29:33 +05302476int c4iw_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2477 int attr_mask, struct ib_qp_init_attr *init_attr)
2478{
2479 struct c4iw_qp *qhp = to_c4iw_qp(ibqp);
2480
2481 memset(attr, 0, sizeof *attr);
2482 memset(init_attr, 0, sizeof *init_attr);
2483 attr->qp_state = to_ib_qp_state(qhp->attr.state);
Hariprasad Shenai3e5c02c2014-07-21 20:55:14 +05302484 init_attr->cap.max_send_wr = qhp->attr.sq_num_entries;
2485 init_attr->cap.max_recv_wr = qhp->attr.rq_num_entries;
2486 init_attr->cap.max_send_sge = qhp->attr.sq_max_sges;
2487 init_attr->cap.max_recv_sge = qhp->attr.sq_max_sges;
2488 init_attr->cap.max_inline_data = T4_MAX_SEND_INLINE;
2489 init_attr->sq_sig_type = qhp->sq_sig_all ? IB_SIGNAL_ALL_WR : 0;
Vipul Pandya67bbc052012-05-18 15:29:33 +05302490 return 0;
2491}
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302492
2493static void free_srq_queue(struct c4iw_srq *srq, struct c4iw_dev_ucontext *uctx,
2494 struct c4iw_wr_wait *wr_waitp)
2495{
2496 struct c4iw_rdev *rdev = &srq->rhp->rdev;
2497 struct sk_buff *skb = srq->destroy_skb;
2498 struct t4_srq *wq = &srq->wq;
2499 struct fw_ri_res_wr *res_wr;
2500 struct fw_ri_res *res;
2501 int wr_len;
2502
2503 wr_len = sizeof(*res_wr) + sizeof(*res);
2504 set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
2505
2506 res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
2507 memset(res_wr, 0, wr_len);
2508 res_wr->op_nres = cpu_to_be32(FW_WR_OP_V(FW_RI_RES_WR) |
2509 FW_RI_RES_WR_NRES_V(1) |
2510 FW_WR_COMPL_F);
2511 res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
2512 res_wr->cookie = (uintptr_t)wr_waitp;
2513 res = res_wr->res;
2514 res->u.srq.restype = FW_RI_RES_TYPE_SRQ;
2515 res->u.srq.op = FW_RI_RES_OP_RESET;
2516 res->u.srq.srqid = cpu_to_be32(srq->idx);
2517 res->u.srq.eqid = cpu_to_be32(wq->qid);
2518
2519 c4iw_init_wr_wait(wr_waitp);
2520 c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, 0, __func__);
2521
2522 dma_free_coherent(&rdev->lldi.pdev->dev,
2523 wq->memsize, wq->queue,
2524 pci_unmap_addr(wq, mapping));
2525 c4iw_rqtpool_free(rdev, wq->rqt_hwaddr, wq->rqt_size);
2526 kfree(wq->sw_rq);
2527 c4iw_put_qpid(rdev, wq->qid, uctx);
2528}
2529
2530static int alloc_srq_queue(struct c4iw_srq *srq, struct c4iw_dev_ucontext *uctx,
2531 struct c4iw_wr_wait *wr_waitp)
2532{
2533 struct c4iw_rdev *rdev = &srq->rhp->rdev;
2534 int user = (uctx != &rdev->uctx);
2535 struct t4_srq *wq = &srq->wq;
2536 struct fw_ri_res_wr *res_wr;
2537 struct fw_ri_res *res;
2538 struct sk_buff *skb;
2539 int wr_len;
2540 int eqsize;
2541 int ret = -ENOMEM;
2542
2543 wq->qid = c4iw_get_qpid(rdev, uctx);
2544 if (!wq->qid)
2545 goto err;
2546
2547 if (!user) {
2548 wq->sw_rq = kcalloc(wq->size, sizeof(*wq->sw_rq),
2549 GFP_KERNEL);
2550 if (!wq->sw_rq)
2551 goto err_put_qpid;
2552 wq->pending_wrs = kcalloc(srq->wq.size,
2553 sizeof(*srq->wq.pending_wrs),
2554 GFP_KERNEL);
2555 if (!wq->pending_wrs)
2556 goto err_free_sw_rq;
2557 }
2558
2559 wq->rqt_size = wq->size;
2560 wq->rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rqt_size);
2561 if (!wq->rqt_hwaddr)
2562 goto err_free_pending_wrs;
2563 wq->rqt_abs_idx = (wq->rqt_hwaddr - rdev->lldi.vr->rq.start) >>
2564 T4_RQT_ENTRY_SHIFT;
2565
2566 wq->queue = dma_alloc_coherent(&rdev->lldi.pdev->dev,
2567 wq->memsize, &wq->dma_addr,
2568 GFP_KERNEL);
2569 if (!wq->queue)
2570 goto err_free_rqtpool;
2571
2572 memset(wq->queue, 0, wq->memsize);
2573 pci_unmap_addr_set(wq, mapping, wq->dma_addr);
2574
2575 wq->bar2_va = c4iw_bar2_addrs(rdev, wq->qid, T4_BAR2_QTYPE_EGRESS,
2576 &wq->bar2_qid,
2577 user ? &wq->bar2_pa : NULL);
2578
2579 /*
2580 * User mode must have bar2 access.
2581 */
2582
2583 if (user && !wq->bar2_va) {
2584 pr_warn(MOD "%s: srqid %u not in BAR2 range.\n",
2585 pci_name(rdev->lldi.pdev), wq->qid);
2586 ret = -EINVAL;
2587 goto err_free_queue;
2588 }
2589
2590 /* build fw_ri_res_wr */
2591 wr_len = sizeof(*res_wr) + sizeof(*res);
2592
2593 skb = alloc_skb(wr_len, GFP_KERNEL | __GFP_NOFAIL);
2594 if (!skb)
2595 goto err_free_queue;
2596 set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
2597
2598 res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
2599 memset(res_wr, 0, wr_len);
2600 res_wr->op_nres = cpu_to_be32(FW_WR_OP_V(FW_RI_RES_WR) |
2601 FW_RI_RES_WR_NRES_V(1) |
2602 FW_WR_COMPL_F);
2603 res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
2604 res_wr->cookie = (uintptr_t)wr_waitp;
2605 res = res_wr->res;
2606 res->u.srq.restype = FW_RI_RES_TYPE_SRQ;
2607 res->u.srq.op = FW_RI_RES_OP_WRITE;
2608
2609 /*
2610 * eqsize is the number of 64B entries plus the status page size.
2611 */
2612 eqsize = wq->size * T4_RQ_NUM_SLOTS +
2613 rdev->hw_queue.t4_eq_status_entries;
2614 res->u.srq.eqid = cpu_to_be32(wq->qid);
2615 res->u.srq.fetchszm_to_iqid =
2616 /* no host cidx updates */
2617 cpu_to_be32(FW_RI_RES_WR_HOSTFCMODE_V(0) |
2618 FW_RI_RES_WR_CPRIO_V(0) | /* don't keep in chip cache */
2619 FW_RI_RES_WR_PCIECHN_V(0) | /* set by uP at ri_init time */
2620 FW_RI_RES_WR_FETCHRO_V(0)); /* relaxed_ordering */
2621 res->u.srq.dcaen_to_eqsize =
2622 cpu_to_be32(FW_RI_RES_WR_DCAEN_V(0) |
2623 FW_RI_RES_WR_DCACPU_V(0) |
2624 FW_RI_RES_WR_FBMIN_V(2) |
2625 FW_RI_RES_WR_FBMAX_V(3) |
2626 FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
2627 FW_RI_RES_WR_CIDXFTHRESH_V(0) |
2628 FW_RI_RES_WR_EQSIZE_V(eqsize));
2629 res->u.srq.eqaddr = cpu_to_be64(wq->dma_addr);
2630 res->u.srq.srqid = cpu_to_be32(srq->idx);
2631 res->u.srq.pdid = cpu_to_be32(srq->pdid);
2632 res->u.srq.hwsrqsize = cpu_to_be32(wq->rqt_size);
2633 res->u.srq.hwsrqaddr = cpu_to_be32(wq->rqt_hwaddr -
2634 rdev->lldi.vr->rq.start);
2635
2636 c4iw_init_wr_wait(wr_waitp);
2637
2638 ret = c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, wq->qid, __func__);
2639 if (ret)
2640 goto err_free_queue;
2641
2642 pr_debug("%s srq %u eqid %u pdid %u queue va %p pa 0x%llx\n"
2643 " bar2_addr %p rqt addr 0x%x size %d\n",
2644 __func__, srq->idx, wq->qid, srq->pdid, wq->queue,
2645 (u64)virt_to_phys(wq->queue), wq->bar2_va,
2646 wq->rqt_hwaddr, wq->rqt_size);
2647
2648 return 0;
2649err_free_queue:
2650 dma_free_coherent(&rdev->lldi.pdev->dev,
2651 wq->memsize, wq->queue,
2652 pci_unmap_addr(wq, mapping));
2653err_free_rqtpool:
2654 c4iw_rqtpool_free(rdev, wq->rqt_hwaddr, wq->rqt_size);
2655err_free_pending_wrs:
2656 if (!user)
2657 kfree(wq->pending_wrs);
2658err_free_sw_rq:
2659 if (!user)
2660 kfree(wq->sw_rq);
2661err_put_qpid:
2662 c4iw_put_qpid(rdev, wq->qid, uctx);
2663err:
2664 return ret;
2665}
2666
2667void c4iw_copy_wr_to_srq(struct t4_srq *srq, union t4_recv_wr *wqe, u8 len16)
2668{
2669 u64 *src, *dst;
2670
2671 src = (u64 *)wqe;
2672 dst = (u64 *)((u8 *)srq->queue + srq->wq_pidx * T4_EQ_ENTRY_SIZE);
2673 while (len16) {
2674 *dst++ = *src++;
2675 if (dst >= (u64 *)&srq->queue[srq->size])
2676 dst = (u64 *)srq->queue;
2677 *dst++ = *src++;
2678 if (dst >= (u64 *)&srq->queue[srq->size])
2679 dst = (u64 *)srq->queue;
2680 len16--;
2681 }
2682}
2683
2684struct ib_srq *c4iw_create_srq(struct ib_pd *pd, struct ib_srq_init_attr *attrs,
2685 struct ib_udata *udata)
2686{
2687 struct c4iw_dev *rhp;
2688 struct c4iw_srq *srq;
2689 struct c4iw_pd *php;
2690 struct c4iw_create_srq_resp uresp;
2691 struct c4iw_ucontext *ucontext;
2692 struct c4iw_mm_entry *srq_key_mm, *srq_db_key_mm;
2693 int rqsize;
2694 int ret;
2695 int wr_len;
2696
2697 pr_debug("%s ib_pd %p\n", __func__, pd);
2698
2699 php = to_c4iw_pd(pd);
2700 rhp = php->rhp;
2701
2702 if (!rhp->rdev.lldi.vr->srq.size)
2703 return ERR_PTR(-EINVAL);
2704 if (attrs->attr.max_wr > rhp->rdev.hw_queue.t4_max_rq_size)
2705 return ERR_PTR(-E2BIG);
2706 if (attrs->attr.max_sge > T4_MAX_RECV_SGE)
2707 return ERR_PTR(-E2BIG);
2708
2709 /*
2710 * SRQ RQT and RQ must be a power of 2 and at least 16 deep.
2711 */
2712 rqsize = attrs->attr.max_wr + 1;
2713 rqsize = roundup_pow_of_two(max_t(u16, rqsize, 16));
2714
2715 ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
2716
2717 srq = kzalloc(sizeof(*srq), GFP_KERNEL);
2718 if (!srq)
2719 return ERR_PTR(-ENOMEM);
2720
2721 srq->wr_waitp = c4iw_alloc_wr_wait(GFP_KERNEL);
2722 if (!srq->wr_waitp) {
2723 ret = -ENOMEM;
2724 goto err_free_srq;
2725 }
2726
2727 srq->idx = c4iw_alloc_srq_idx(&rhp->rdev);
2728 if (srq->idx < 0) {
2729 ret = -ENOMEM;
2730 goto err_free_wr_wait;
2731 }
2732
2733 wr_len = sizeof(struct fw_ri_res_wr) + sizeof(struct fw_ri_res);
2734 srq->destroy_skb = alloc_skb(wr_len, GFP_KERNEL);
2735 if (!srq->destroy_skb) {
2736 ret = -ENOMEM;
2737 goto err_free_srq_idx;
2738 }
2739
2740 srq->rhp = rhp;
2741 srq->pdid = php->pdid;
2742
2743 srq->wq.size = rqsize;
2744 srq->wq.memsize =
2745 (rqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
2746 sizeof(*srq->wq.queue);
2747 if (ucontext)
2748 srq->wq.memsize = roundup(srq->wq.memsize, PAGE_SIZE);
2749
2750 ret = alloc_srq_queue(srq, ucontext ? &ucontext->uctx :
2751 &rhp->rdev.uctx, srq->wr_waitp);
2752 if (ret)
2753 goto err_free_skb;
2754 attrs->attr.max_wr = rqsize - 1;
2755
2756 if (CHELSIO_CHIP_VERSION(rhp->rdev.lldi.adapter_type) > CHELSIO_T6)
2757 srq->flags = T4_SRQ_LIMIT_SUPPORT;
2758
2759 ret = insert_handle(rhp, &rhp->qpidr, srq, srq->wq.qid);
2760 if (ret)
2761 goto err_free_queue;
2762
2763 if (udata) {
2764 srq_key_mm = kmalloc(sizeof(*srq_key_mm), GFP_KERNEL);
2765 if (!srq_key_mm) {
2766 ret = -ENOMEM;
2767 goto err_remove_handle;
2768 }
2769 srq_db_key_mm = kmalloc(sizeof(*srq_db_key_mm), GFP_KERNEL);
2770 if (!srq_db_key_mm) {
2771 ret = -ENOMEM;
2772 goto err_free_srq_key_mm;
2773 }
Dan Carpenter8001b712018-08-02 10:56:13 +03002774 memset(&uresp, 0, sizeof(uresp));
Raju Rangoju6a0b6172018-07-25 21:22:14 +05302775 uresp.flags = srq->flags;
2776 uresp.qid_mask = rhp->rdev.qpmask;
2777 uresp.srqid = srq->wq.qid;
2778 uresp.srq_size = srq->wq.size;
2779 uresp.srq_memsize = srq->wq.memsize;
2780 uresp.rqt_abs_idx = srq->wq.rqt_abs_idx;
2781 spin_lock(&ucontext->mmap_lock);
2782 uresp.srq_key = ucontext->key;
2783 ucontext->key += PAGE_SIZE;
2784 uresp.srq_db_gts_key = ucontext->key;
2785 ucontext->key += PAGE_SIZE;
2786 spin_unlock(&ucontext->mmap_lock);
2787 ret = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
2788 if (ret)
2789 goto err_free_srq_db_key_mm;
2790 srq_key_mm->key = uresp.srq_key;
2791 srq_key_mm->addr = virt_to_phys(srq->wq.queue);
2792 srq_key_mm->len = PAGE_ALIGN(srq->wq.memsize);
2793 insert_mmap(ucontext, srq_key_mm);
2794 srq_db_key_mm->key = uresp.srq_db_gts_key;
2795 srq_db_key_mm->addr = (u64)(unsigned long)srq->wq.bar2_pa;
2796 srq_db_key_mm->len = PAGE_SIZE;
2797 insert_mmap(ucontext, srq_db_key_mm);
2798 }
2799
2800 pr_debug("%s srq qid %u idx %u size %u memsize %lu num_entries %u\n",
2801 __func__, srq->wq.qid, srq->idx, srq->wq.size,
2802 (unsigned long)srq->wq.memsize, attrs->attr.max_wr);
2803
2804 spin_lock_init(&srq->lock);
2805 return &srq->ibsrq;
2806err_free_srq_db_key_mm:
2807 kfree(srq_db_key_mm);
2808err_free_srq_key_mm:
2809 kfree(srq_key_mm);
2810err_remove_handle:
2811 remove_handle(rhp, &rhp->qpidr, srq->wq.qid);
2812err_free_queue:
2813 free_srq_queue(srq, ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
2814 srq->wr_waitp);
2815err_free_skb:
2816 if (srq->destroy_skb)
2817 kfree_skb(srq->destroy_skb);
2818err_free_srq_idx:
2819 c4iw_free_srq_idx(&rhp->rdev, srq->idx);
2820err_free_wr_wait:
2821 c4iw_put_wr_wait(srq->wr_waitp);
2822err_free_srq:
2823 kfree(srq);
2824 return ERR_PTR(ret);
2825}
2826
2827int c4iw_destroy_srq(struct ib_srq *ibsrq)
2828{
2829 struct c4iw_dev *rhp;
2830 struct c4iw_srq *srq;
2831 struct c4iw_ucontext *ucontext;
2832
2833 srq = to_c4iw_srq(ibsrq);
2834 rhp = srq->rhp;
2835
2836 pr_debug("%s id %d\n", __func__, srq->wq.qid);
2837
2838 remove_handle(rhp, &rhp->qpidr, srq->wq.qid);
2839 ucontext = ibsrq->uobject ?
2840 to_c4iw_ucontext(ibsrq->uobject->context) : NULL;
2841 free_srq_queue(srq, ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
2842 srq->wr_waitp);
2843 c4iw_free_srq_idx(&rhp->rdev, srq->idx);
2844 c4iw_put_wr_wait(srq->wr_waitp);
2845 kfree(srq);
2846 return 0;
2847}