blob: ec3ce675fdffd4994f2ffaa83d182b4168431f1b [file] [log] [blame]
Steve Wisecfdda9d2010-04-21 15:30:06 -07001/*
2 * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32#include "iw_cxgb4.h"
33
Steve Wisea9c77192011-03-11 22:30:11 +000034static int ocqp_support = 1;
Steve Wisec6d7b262010-09-13 11:23:57 -050035module_param(ocqp_support, int, 0644);
Steve Wisea9c77192011-03-11 22:30:11 +000036MODULE_PARM_DESC(ocqp_support, "Support on-chip SQs (default=1)");
Steve Wisec6d7b262010-09-13 11:23:57 -050037
Steve Wise2f5b48c2010-09-10 11:15:36 -050038static void set_state(struct c4iw_qp *qhp, enum c4iw_qp_state state)
39{
40 unsigned long flag;
41 spin_lock_irqsave(&qhp->lock, flag);
42 qhp->attr.state = state;
43 spin_unlock_irqrestore(&qhp->lock, flag);
44}
45
Steve Wisec6d7b262010-09-13 11:23:57 -050046static void dealloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
47{
48 c4iw_ocqp_pool_free(rdev, sq->dma_addr, sq->memsize);
49}
50
51static void dealloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
52{
53 dma_free_coherent(&(rdev->lldi.pdev->dev), sq->memsize, sq->queue,
54 pci_unmap_addr(sq, mapping));
55}
56
57static void dealloc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
58{
59 if (t4_sq_onchip(sq))
60 dealloc_oc_sq(rdev, sq);
61 else
62 dealloc_host_sq(rdev, sq);
63}
64
65static int alloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
66{
67 if (!ocqp_support || !t4_ocqp_supported())
68 return -ENOSYS;
69 sq->dma_addr = c4iw_ocqp_pool_alloc(rdev, sq->memsize);
70 if (!sq->dma_addr)
71 return -ENOMEM;
72 sq->phys_addr = rdev->oc_mw_pa + sq->dma_addr -
73 rdev->lldi.vr->ocq.start;
74 sq->queue = (__force union t4_wr *)(rdev->oc_mw_kva + sq->dma_addr -
75 rdev->lldi.vr->ocq.start);
76 sq->flags |= T4_SQ_ONCHIP;
77 return 0;
78}
79
80static int alloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
81{
82 sq->queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev), sq->memsize,
83 &(sq->dma_addr), GFP_KERNEL);
84 if (!sq->queue)
85 return -ENOMEM;
86 sq->phys_addr = virt_to_phys(sq->queue);
87 pci_unmap_addr_set(sq, mapping, sq->dma_addr);
88 return 0;
89}
90
Steve Wisecfdda9d2010-04-21 15:30:06 -070091static int destroy_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
92 struct c4iw_dev_ucontext *uctx)
93{
94 /*
95 * uP clears EQ contexts when the connection exits rdma mode,
96 * so no need to post a RESET WR for these EQs.
97 */
98 dma_free_coherent(&(rdev->lldi.pdev->dev),
99 wq->rq.memsize, wq->rq.queue,
FUJITA Tomonorif38926a2010-06-03 05:37:50 +0000100 dma_unmap_addr(&wq->rq, mapping));
Steve Wisec6d7b262010-09-13 11:23:57 -0500101 dealloc_sq(rdev, &wq->sq);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700102 c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
103 kfree(wq->rq.sw_rq);
104 kfree(wq->sq.sw_sq);
105 c4iw_put_qpid(rdev, wq->rq.qid, uctx);
106 c4iw_put_qpid(rdev, wq->sq.qid, uctx);
107 return 0;
108}
109
110static int create_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
111 struct t4_cq *rcq, struct t4_cq *scq,
112 struct c4iw_dev_ucontext *uctx)
113{
114 int user = (uctx != &rdev->uctx);
115 struct fw_ri_res_wr *res_wr;
116 struct fw_ri_res *res;
117 int wr_len;
118 struct c4iw_wr_wait wr_wait;
119 struct sk_buff *skb;
120 int ret;
121 int eqsize;
122
123 wq->sq.qid = c4iw_get_qpid(rdev, uctx);
124 if (!wq->sq.qid)
125 return -ENOMEM;
126
127 wq->rq.qid = c4iw_get_qpid(rdev, uctx);
128 if (!wq->rq.qid)
129 goto err1;
130
131 if (!user) {
132 wq->sq.sw_sq = kzalloc(wq->sq.size * sizeof *wq->sq.sw_sq,
133 GFP_KERNEL);
134 if (!wq->sq.sw_sq)
135 goto err2;
136
137 wq->rq.sw_rq = kzalloc(wq->rq.size * sizeof *wq->rq.sw_rq,
138 GFP_KERNEL);
139 if (!wq->rq.sw_rq)
140 goto err3;
141 }
142
143 /*
144 * RQT must be a power of 2.
145 */
146 wq->rq.rqt_size = roundup_pow_of_two(wq->rq.size);
147 wq->rq.rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rq.rqt_size);
148 if (!wq->rq.rqt_hwaddr)
149 goto err4;
150
Steve Wisec6d7b262010-09-13 11:23:57 -0500151 if (user) {
152 if (alloc_oc_sq(rdev, &wq->sq) && alloc_host_sq(rdev, &wq->sq))
153 goto err5;
154 } else
155 if (alloc_host_sq(rdev, &wq->sq))
156 goto err5;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700157 memset(wq->sq.queue, 0, wq->sq.memsize);
FUJITA Tomonorif38926a2010-06-03 05:37:50 +0000158 dma_unmap_addr_set(&wq->sq, mapping, wq->sq.dma_addr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700159
160 wq->rq.queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev),
161 wq->rq.memsize, &(wq->rq.dma_addr),
162 GFP_KERNEL);
163 if (!wq->rq.queue)
164 goto err6;
165 PDBG("%s sq base va 0x%p pa 0x%llx rq base va 0x%p pa 0x%llx\n",
166 __func__, wq->sq.queue,
167 (unsigned long long)virt_to_phys(wq->sq.queue),
168 wq->rq.queue,
169 (unsigned long long)virt_to_phys(wq->rq.queue));
170 memset(wq->rq.queue, 0, wq->rq.memsize);
FUJITA Tomonorif38926a2010-06-03 05:37:50 +0000171 dma_unmap_addr_set(&wq->rq, mapping, wq->rq.dma_addr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700172
173 wq->db = rdev->lldi.db_reg;
174 wq->gts = rdev->lldi.gts_reg;
175 if (user) {
176 wq->sq.udb = (u64)pci_resource_start(rdev->lldi.pdev, 2) +
177 (wq->sq.qid << rdev->qpshift);
178 wq->sq.udb &= PAGE_MASK;
179 wq->rq.udb = (u64)pci_resource_start(rdev->lldi.pdev, 2) +
180 (wq->rq.qid << rdev->qpshift);
181 wq->rq.udb &= PAGE_MASK;
182 }
183 wq->rdev = rdev;
184 wq->rq.msn = 1;
185
186 /* build fw_ri_res_wr */
187 wr_len = sizeof *res_wr + 2 * sizeof *res;
188
David Rientjesd3c814e2010-07-21 02:44:56 +0000189 skb = alloc_skb(wr_len, GFP_KERNEL);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700190 if (!skb) {
191 ret = -ENOMEM;
192 goto err7;
193 }
194 set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
195
196 res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
197 memset(res_wr, 0, wr_len);
198 res_wr->op_nres = cpu_to_be32(
199 FW_WR_OP(FW_RI_RES_WR) |
200 V_FW_RI_RES_WR_NRES(2) |
201 FW_WR_COMPL(1));
202 res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
Roland Dreierc8e081a2010-09-27 17:51:04 -0700203 res_wr->cookie = (unsigned long) &wr_wait;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700204 res = res_wr->res;
205 res->u.sqrq.restype = FW_RI_RES_TYPE_SQ;
206 res->u.sqrq.op = FW_RI_RES_OP_WRITE;
207
208 /*
209 * eqsize is the number of 64B entries plus the status page size.
210 */
211 eqsize = wq->sq.size * T4_SQ_NUM_SLOTS + T4_EQ_STATUS_ENTRIES;
212
213 res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
214 V_FW_RI_RES_WR_HOSTFCMODE(0) | /* no host cidx updates */
215 V_FW_RI_RES_WR_CPRIO(0) | /* don't keep in chip cache */
216 V_FW_RI_RES_WR_PCIECHN(0) | /* set by uP at ri_init time */
Roel Kluin85d215b2011-05-09 22:06:22 -0700217 (t4_sq_onchip(&wq->sq) ? F_FW_RI_RES_WR_ONCHIP : 0) |
Steve Wisecfdda9d2010-04-21 15:30:06 -0700218 V_FW_RI_RES_WR_IQID(scq->cqid));
219 res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
220 V_FW_RI_RES_WR_DCAEN(0) |
221 V_FW_RI_RES_WR_DCACPU(0) |
Steve Wised37ac312010-06-10 19:03:00 +0000222 V_FW_RI_RES_WR_FBMIN(2) |
Steve Wise6a09a9d2011-01-21 17:00:29 +0000223 V_FW_RI_RES_WR_FBMAX(2) |
Steve Wisecfdda9d2010-04-21 15:30:06 -0700224 V_FW_RI_RES_WR_CIDXFTHRESHO(0) |
225 V_FW_RI_RES_WR_CIDXFTHRESH(0) |
226 V_FW_RI_RES_WR_EQSIZE(eqsize));
227 res->u.sqrq.eqid = cpu_to_be32(wq->sq.qid);
228 res->u.sqrq.eqaddr = cpu_to_be64(wq->sq.dma_addr);
229 res++;
230 res->u.sqrq.restype = FW_RI_RES_TYPE_RQ;
231 res->u.sqrq.op = FW_RI_RES_OP_WRITE;
232
233 /*
234 * eqsize is the number of 64B entries plus the status page size.
235 */
236 eqsize = wq->rq.size * T4_RQ_NUM_SLOTS + T4_EQ_STATUS_ENTRIES;
237 res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
238 V_FW_RI_RES_WR_HOSTFCMODE(0) | /* no host cidx updates */
239 V_FW_RI_RES_WR_CPRIO(0) | /* don't keep in chip cache */
240 V_FW_RI_RES_WR_PCIECHN(0) | /* set by uP at ri_init time */
241 V_FW_RI_RES_WR_IQID(rcq->cqid));
242 res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
243 V_FW_RI_RES_WR_DCAEN(0) |
244 V_FW_RI_RES_WR_DCACPU(0) |
Steve Wised37ac312010-06-10 19:03:00 +0000245 V_FW_RI_RES_WR_FBMIN(2) |
Steve Wise6a09a9d2011-01-21 17:00:29 +0000246 V_FW_RI_RES_WR_FBMAX(2) |
Steve Wisecfdda9d2010-04-21 15:30:06 -0700247 V_FW_RI_RES_WR_CIDXFTHRESHO(0) |
248 V_FW_RI_RES_WR_CIDXFTHRESH(0) |
249 V_FW_RI_RES_WR_EQSIZE(eqsize));
250 res->u.sqrq.eqid = cpu_to_be32(wq->rq.qid);
251 res->u.sqrq.eqaddr = cpu_to_be64(wq->rq.dma_addr);
252
253 c4iw_init_wr_wait(&wr_wait);
254
255 ret = c4iw_ofld_send(rdev, skb);
256 if (ret)
257 goto err7;
Steve Wiseaadc4df2010-09-10 11:15:25 -0500258 ret = c4iw_wait_for_reply(rdev, &wr_wait, 0, wq->sq.qid, __func__);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700259 if (ret)
260 goto err7;
261
262 PDBG("%s sqid 0x%x rqid 0x%x kdb 0x%p squdb 0x%llx rqudb 0x%llx\n",
263 __func__, wq->sq.qid, wq->rq.qid, wq->db,
264 (unsigned long long)wq->sq.udb, (unsigned long long)wq->rq.udb);
265
266 return 0;
267err7:
268 dma_free_coherent(&(rdev->lldi.pdev->dev),
269 wq->rq.memsize, wq->rq.queue,
FUJITA Tomonorif38926a2010-06-03 05:37:50 +0000270 dma_unmap_addr(&wq->rq, mapping));
Steve Wisecfdda9d2010-04-21 15:30:06 -0700271err6:
Steve Wisec6d7b262010-09-13 11:23:57 -0500272 dealloc_sq(rdev, &wq->sq);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700273err5:
274 c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
275err4:
276 kfree(wq->rq.sw_rq);
277err3:
278 kfree(wq->sq.sw_sq);
279err2:
280 c4iw_put_qpid(rdev, wq->rq.qid, uctx);
281err1:
282 c4iw_put_qpid(rdev, wq->sq.qid, uctx);
283 return -ENOMEM;
284}
285
Steve Wised37ac312010-06-10 19:03:00 +0000286static int build_immd(struct t4_sq *sq, struct fw_ri_immd *immdp,
287 struct ib_send_wr *wr, int max, u32 *plenp)
288{
289 u8 *dstp, *srcp;
290 u32 plen = 0;
291 int i;
292 int rem, len;
293
294 dstp = (u8 *)immdp->data;
295 for (i = 0; i < wr->num_sge; i++) {
296 if ((plen + wr->sg_list[i].length) > max)
297 return -EMSGSIZE;
298 srcp = (u8 *)(unsigned long)wr->sg_list[i].addr;
299 plen += wr->sg_list[i].length;
300 rem = wr->sg_list[i].length;
301 while (rem) {
302 if (dstp == (u8 *)&sq->queue[sq->size])
303 dstp = (u8 *)sq->queue;
304 if (rem <= (u8 *)&sq->queue[sq->size] - dstp)
305 len = rem;
306 else
307 len = (u8 *)&sq->queue[sq->size] - dstp;
308 memcpy(dstp, srcp, len);
309 dstp += len;
310 srcp += len;
311 rem -= len;
312 }
313 }
Steve Wise13fecb82010-09-10 11:14:53 -0500314 len = roundup(plen + sizeof *immdp, 16) - (plen + sizeof *immdp);
315 if (len)
316 memset(dstp, 0, len);
Steve Wised37ac312010-06-10 19:03:00 +0000317 immdp->op = FW_RI_DATA_IMMD;
318 immdp->r1 = 0;
319 immdp->r2 = 0;
320 immdp->immdlen = cpu_to_be32(plen);
321 *plenp = plen;
322 return 0;
323}
324
325static int build_isgl(__be64 *queue_start, __be64 *queue_end,
326 struct fw_ri_isgl *isglp, struct ib_sge *sg_list,
327 int num_sge, u32 *plenp)
328
Steve Wisecfdda9d2010-04-21 15:30:06 -0700329{
330 int i;
Steve Wised37ac312010-06-10 19:03:00 +0000331 u32 plen = 0;
332 __be64 *flitp = (__be64 *)isglp->sge;
333
334 for (i = 0; i < num_sge; i++) {
335 if ((plen + sg_list[i].length) < plen)
336 return -EMSGSIZE;
337 plen += sg_list[i].length;
338 *flitp = cpu_to_be64(((u64)sg_list[i].lkey << 32) |
339 sg_list[i].length);
340 if (++flitp == queue_end)
341 flitp = queue_start;
342 *flitp = cpu_to_be64(sg_list[i].addr);
343 if (++flitp == queue_end)
344 flitp = queue_start;
345 }
Steve Wise13fecb82010-09-10 11:14:53 -0500346 *flitp = (__force __be64)0;
Steve Wised37ac312010-06-10 19:03:00 +0000347 isglp->op = FW_RI_DATA_ISGL;
348 isglp->r1 = 0;
349 isglp->nsge = cpu_to_be16(num_sge);
350 isglp->r2 = 0;
351 if (plenp)
352 *plenp = plen;
353 return 0;
354}
355
356static int build_rdma_send(struct t4_sq *sq, union t4_wr *wqe,
357 struct ib_send_wr *wr, u8 *len16)
358{
Steve Wisecfdda9d2010-04-21 15:30:06 -0700359 u32 plen;
360 int size;
Steve Wised37ac312010-06-10 19:03:00 +0000361 int ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700362
363 if (wr->num_sge > T4_MAX_SEND_SGE)
364 return -EINVAL;
365 switch (wr->opcode) {
366 case IB_WR_SEND:
367 if (wr->send_flags & IB_SEND_SOLICITED)
368 wqe->send.sendop_pkd = cpu_to_be32(
369 V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_SE));
370 else
371 wqe->send.sendop_pkd = cpu_to_be32(
372 V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND));
373 wqe->send.stag_inv = 0;
374 break;
375 case IB_WR_SEND_WITH_INV:
376 if (wr->send_flags & IB_SEND_SOLICITED)
377 wqe->send.sendop_pkd = cpu_to_be32(
378 V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_SE_INV));
379 else
380 wqe->send.sendop_pkd = cpu_to_be32(
381 V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_INV));
382 wqe->send.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
383 break;
384
385 default:
386 return -EINVAL;
387 }
Steve Wised37ac312010-06-10 19:03:00 +0000388
Steve Wisecfdda9d2010-04-21 15:30:06 -0700389 plen = 0;
390 if (wr->num_sge) {
391 if (wr->send_flags & IB_SEND_INLINE) {
Steve Wised37ac312010-06-10 19:03:00 +0000392 ret = build_immd(sq, wqe->send.u.immd_src, wr,
393 T4_MAX_SEND_INLINE, &plen);
394 if (ret)
395 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700396 size = sizeof wqe->send + sizeof(struct fw_ri_immd) +
397 plen;
398 } else {
Steve Wised37ac312010-06-10 19:03:00 +0000399 ret = build_isgl((__be64 *)sq->queue,
400 (__be64 *)&sq->queue[sq->size],
401 wqe->send.u.isgl_src,
402 wr->sg_list, wr->num_sge, &plen);
403 if (ret)
404 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700405 size = sizeof wqe->send + sizeof(struct fw_ri_isgl) +
406 wr->num_sge * sizeof(struct fw_ri_sge);
407 }
408 } else {
409 wqe->send.u.immd_src[0].op = FW_RI_DATA_IMMD;
410 wqe->send.u.immd_src[0].r1 = 0;
411 wqe->send.u.immd_src[0].r2 = 0;
412 wqe->send.u.immd_src[0].immdlen = 0;
413 size = sizeof wqe->send + sizeof(struct fw_ri_immd);
Steve Wised37ac312010-06-10 19:03:00 +0000414 plen = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700415 }
416 *len16 = DIV_ROUND_UP(size, 16);
417 wqe->send.plen = cpu_to_be32(plen);
418 return 0;
419}
420
Steve Wised37ac312010-06-10 19:03:00 +0000421static int build_rdma_write(struct t4_sq *sq, union t4_wr *wqe,
422 struct ib_send_wr *wr, u8 *len16)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700423{
Steve Wisecfdda9d2010-04-21 15:30:06 -0700424 u32 plen;
425 int size;
Steve Wised37ac312010-06-10 19:03:00 +0000426 int ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700427
Steve Wised37ac312010-06-10 19:03:00 +0000428 if (wr->num_sge > T4_MAX_SEND_SGE)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700429 return -EINVAL;
430 wqe->write.r2 = 0;
431 wqe->write.stag_sink = cpu_to_be32(wr->wr.rdma.rkey);
432 wqe->write.to_sink = cpu_to_be64(wr->wr.rdma.remote_addr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700433 if (wr->num_sge) {
434 if (wr->send_flags & IB_SEND_INLINE) {
Steve Wised37ac312010-06-10 19:03:00 +0000435 ret = build_immd(sq, wqe->write.u.immd_src, wr,
436 T4_MAX_WRITE_INLINE, &plen);
437 if (ret)
438 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700439 size = sizeof wqe->write + sizeof(struct fw_ri_immd) +
440 plen;
441 } else {
Steve Wised37ac312010-06-10 19:03:00 +0000442 ret = build_isgl((__be64 *)sq->queue,
443 (__be64 *)&sq->queue[sq->size],
444 wqe->write.u.isgl_src,
445 wr->sg_list, wr->num_sge, &plen);
446 if (ret)
447 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700448 size = sizeof wqe->write + sizeof(struct fw_ri_isgl) +
449 wr->num_sge * sizeof(struct fw_ri_sge);
450 }
451 } else {
452 wqe->write.u.immd_src[0].op = FW_RI_DATA_IMMD;
453 wqe->write.u.immd_src[0].r1 = 0;
454 wqe->write.u.immd_src[0].r2 = 0;
455 wqe->write.u.immd_src[0].immdlen = 0;
456 size = sizeof wqe->write + sizeof(struct fw_ri_immd);
Steve Wised37ac312010-06-10 19:03:00 +0000457 plen = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700458 }
459 *len16 = DIV_ROUND_UP(size, 16);
460 wqe->write.plen = cpu_to_be32(plen);
461 return 0;
462}
463
464static int build_rdma_read(union t4_wr *wqe, struct ib_send_wr *wr, u8 *len16)
465{
466 if (wr->num_sge > 1)
467 return -EINVAL;
468 if (wr->num_sge) {
469 wqe->read.stag_src = cpu_to_be32(wr->wr.rdma.rkey);
470 wqe->read.to_src_hi = cpu_to_be32((u32)(wr->wr.rdma.remote_addr
471 >> 32));
472 wqe->read.to_src_lo = cpu_to_be32((u32)wr->wr.rdma.remote_addr);
473 wqe->read.stag_sink = cpu_to_be32(wr->sg_list[0].lkey);
474 wqe->read.plen = cpu_to_be32(wr->sg_list[0].length);
475 wqe->read.to_sink_hi = cpu_to_be32((u32)(wr->sg_list[0].addr
476 >> 32));
477 wqe->read.to_sink_lo = cpu_to_be32((u32)(wr->sg_list[0].addr));
478 } else {
479 wqe->read.stag_src = cpu_to_be32(2);
480 wqe->read.to_src_hi = 0;
481 wqe->read.to_src_lo = 0;
482 wqe->read.stag_sink = cpu_to_be32(2);
483 wqe->read.plen = 0;
484 wqe->read.to_sink_hi = 0;
485 wqe->read.to_sink_lo = 0;
486 }
487 wqe->read.r2 = 0;
488 wqe->read.r5 = 0;
489 *len16 = DIV_ROUND_UP(sizeof wqe->read, 16);
490 return 0;
491}
492
493static int build_rdma_recv(struct c4iw_qp *qhp, union t4_recv_wr *wqe,
494 struct ib_recv_wr *wr, u8 *len16)
495{
Steve Wised37ac312010-06-10 19:03:00 +0000496 int ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700497
Steve Wised37ac312010-06-10 19:03:00 +0000498 ret = build_isgl((__be64 *)qhp->wq.rq.queue,
499 (__be64 *)&qhp->wq.rq.queue[qhp->wq.rq.size],
500 &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
501 if (ret)
502 return ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700503 *len16 = DIV_ROUND_UP(sizeof wqe->recv +
504 wr->num_sge * sizeof(struct fw_ri_sge), 16);
505 return 0;
506}
507
Steve Wise40dbf6e2010-09-17 15:40:15 -0500508static int build_fastreg(struct t4_sq *sq, union t4_wr *wqe,
509 struct ib_send_wr *wr, u8 *len16)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700510{
511
512 struct fw_ri_immd *imdp;
513 __be64 *p;
514 int i;
515 int pbllen = roundup(wr->wr.fast_reg.page_list_len * sizeof(u64), 32);
Steve Wise40dbf6e2010-09-17 15:40:15 -0500516 int rem;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700517
518 if (wr->wr.fast_reg.page_list_len > T4_MAX_FR_DEPTH)
519 return -EINVAL;
520
521 wqe->fr.qpbinde_to_dcacpu = 0;
522 wqe->fr.pgsz_shift = wr->wr.fast_reg.page_shift - 12;
523 wqe->fr.addr_type = FW_RI_VA_BASED_TO;
524 wqe->fr.mem_perms = c4iw_ib_to_tpt_access(wr->wr.fast_reg.access_flags);
525 wqe->fr.len_hi = 0;
526 wqe->fr.len_lo = cpu_to_be32(wr->wr.fast_reg.length);
527 wqe->fr.stag = cpu_to_be32(wr->wr.fast_reg.rkey);
528 wqe->fr.va_hi = cpu_to_be32(wr->wr.fast_reg.iova_start >> 32);
529 wqe->fr.va_lo_fbo = cpu_to_be32(wr->wr.fast_reg.iova_start &
530 0xffffffff);
Steve Wise40dbf6e2010-09-17 15:40:15 -0500531 WARN_ON(pbllen > T4_MAX_FR_IMMD);
532 imdp = (struct fw_ri_immd *)(&wqe->fr + 1);
533 imdp->op = FW_RI_DATA_IMMD;
534 imdp->r1 = 0;
535 imdp->r2 = 0;
536 imdp->immdlen = cpu_to_be32(pbllen);
537 p = (__be64 *)(imdp + 1);
538 rem = pbllen;
539 for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
540 *p = cpu_to_be64((u64)wr->wr.fast_reg.page_list->page_list[i]);
541 rem -= sizeof *p;
542 if (++p == (__be64 *)&sq->queue[sq->size])
543 p = (__be64 *)sq->queue;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700544 }
Steve Wise40dbf6e2010-09-17 15:40:15 -0500545 BUG_ON(rem < 0);
546 while (rem) {
547 *p = 0;
548 rem -= sizeof *p;
549 if (++p == (__be64 *)&sq->queue[sq->size])
550 p = (__be64 *)sq->queue;
551 }
552 *len16 = DIV_ROUND_UP(sizeof wqe->fr + sizeof *imdp + pbllen, 16);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700553 return 0;
554}
555
556static int build_inv_stag(union t4_wr *wqe, struct ib_send_wr *wr,
557 u8 *len16)
558{
559 wqe->inv.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
560 wqe->inv.r2 = 0;
561 *len16 = DIV_ROUND_UP(sizeof wqe->inv, 16);
562 return 0;
563}
564
565void c4iw_qp_add_ref(struct ib_qp *qp)
566{
567 PDBG("%s ib_qp %p\n", __func__, qp);
568 atomic_inc(&(to_c4iw_qp(qp)->refcnt));
569}
570
571void c4iw_qp_rem_ref(struct ib_qp *qp)
572{
573 PDBG("%s ib_qp %p\n", __func__, qp);
574 if (atomic_dec_and_test(&(to_c4iw_qp(qp)->refcnt)))
575 wake_up(&(to_c4iw_qp(qp)->wait));
576}
577
578int c4iw_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
579 struct ib_send_wr **bad_wr)
580{
581 int err = 0;
582 u8 len16 = 0;
583 enum fw_wr_opcodes fw_opcode = 0;
584 enum fw_ri_wr_flags fw_flags;
585 struct c4iw_qp *qhp;
586 union t4_wr *wqe;
587 u32 num_wrs;
588 struct t4_swsqe *swsqe;
589 unsigned long flag;
590 u16 idx = 0;
591
592 qhp = to_c4iw_qp(ibqp);
593 spin_lock_irqsave(&qhp->lock, flag);
594 if (t4_wq_in_error(&qhp->wq)) {
595 spin_unlock_irqrestore(&qhp->lock, flag);
596 return -EINVAL;
597 }
598 num_wrs = t4_sq_avail(&qhp->wq);
599 if (num_wrs == 0) {
600 spin_unlock_irqrestore(&qhp->lock, flag);
601 return -ENOMEM;
602 }
603 while (wr) {
604 if (num_wrs == 0) {
605 err = -ENOMEM;
606 *bad_wr = wr;
607 break;
608 }
Steve Wised37ac312010-06-10 19:03:00 +0000609 wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
610 qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
611
Steve Wisecfdda9d2010-04-21 15:30:06 -0700612 fw_flags = 0;
613 if (wr->send_flags & IB_SEND_SOLICITED)
614 fw_flags |= FW_RI_SOLICITED_EVENT_FLAG;
615 if (wr->send_flags & IB_SEND_SIGNALED)
616 fw_flags |= FW_RI_COMPLETION_FLAG;
617 swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
618 switch (wr->opcode) {
619 case IB_WR_SEND_WITH_INV:
620 case IB_WR_SEND:
621 if (wr->send_flags & IB_SEND_FENCE)
622 fw_flags |= FW_RI_READ_FENCE_FLAG;
623 fw_opcode = FW_RI_SEND_WR;
624 if (wr->opcode == IB_WR_SEND)
625 swsqe->opcode = FW_RI_SEND;
626 else
627 swsqe->opcode = FW_RI_SEND_WITH_INV;
Steve Wised37ac312010-06-10 19:03:00 +0000628 err = build_rdma_send(&qhp->wq.sq, wqe, wr, &len16);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700629 break;
630 case IB_WR_RDMA_WRITE:
631 fw_opcode = FW_RI_RDMA_WRITE_WR;
632 swsqe->opcode = FW_RI_RDMA_WRITE;
Steve Wised37ac312010-06-10 19:03:00 +0000633 err = build_rdma_write(&qhp->wq.sq, wqe, wr, &len16);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700634 break;
635 case IB_WR_RDMA_READ:
Steve Wise2f1fb502010-05-20 16:58:16 -0500636 case IB_WR_RDMA_READ_WITH_INV:
Steve Wisecfdda9d2010-04-21 15:30:06 -0700637 fw_opcode = FW_RI_RDMA_READ_WR;
638 swsqe->opcode = FW_RI_READ_REQ;
Steve Wise2f1fb502010-05-20 16:58:16 -0500639 if (wr->opcode == IB_WR_RDMA_READ_WITH_INV)
Steve Wise410ade42010-09-17 15:40:09 -0500640 fw_flags = FW_RI_RDMA_READ_INVALIDATE;
Steve Wise2f1fb502010-05-20 16:58:16 -0500641 else
642 fw_flags = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700643 err = build_rdma_read(wqe, wr, &len16);
644 if (err)
645 break;
646 swsqe->read_len = wr->sg_list[0].length;
647 if (!qhp->wq.sq.oldest_read)
648 qhp->wq.sq.oldest_read = swsqe;
649 break;
650 case IB_WR_FAST_REG_MR:
651 fw_opcode = FW_RI_FR_NSMR_WR;
652 swsqe->opcode = FW_RI_FAST_REGISTER;
Steve Wise40dbf6e2010-09-17 15:40:15 -0500653 err = build_fastreg(&qhp->wq.sq, wqe, wr, &len16);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700654 break;
655 case IB_WR_LOCAL_INV:
Steve Wise4ab1eb92010-05-20 16:58:10 -0500656 if (wr->send_flags & IB_SEND_FENCE)
657 fw_flags |= FW_RI_LOCAL_FENCE_FLAG;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700658 fw_opcode = FW_RI_INV_LSTAG_WR;
659 swsqe->opcode = FW_RI_LOCAL_INV;
660 err = build_inv_stag(wqe, wr, &len16);
661 break;
662 default:
663 PDBG("%s post of type=%d TBD!\n", __func__,
664 wr->opcode);
665 err = -EINVAL;
666 }
667 if (err) {
668 *bad_wr = wr;
669 break;
670 }
671 swsqe->idx = qhp->wq.sq.pidx;
672 swsqe->complete = 0;
673 swsqe->signaled = (wr->send_flags & IB_SEND_SIGNALED);
674 swsqe->wr_id = wr->wr_id;
675
676 init_wr_hdr(wqe, qhp->wq.sq.pidx, fw_opcode, fw_flags, len16);
677
678 PDBG("%s cookie 0x%llx pidx 0x%x opcode 0x%x read_len %u\n",
679 __func__, (unsigned long long)wr->wr_id, qhp->wq.sq.pidx,
680 swsqe->opcode, swsqe->read_len);
681 wr = wr->next;
682 num_wrs--;
Steve Wised37ac312010-06-10 19:03:00 +0000683 t4_sq_produce(&qhp->wq, len16);
684 idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700685 }
686 if (t4_wq_db_enabled(&qhp->wq))
687 t4_ring_sq_db(&qhp->wq, idx);
688 spin_unlock_irqrestore(&qhp->lock, flag);
689 return err;
690}
691
692int c4iw_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
693 struct ib_recv_wr **bad_wr)
694{
695 int err = 0;
696 struct c4iw_qp *qhp;
697 union t4_recv_wr *wqe;
698 u32 num_wrs;
699 u8 len16 = 0;
700 unsigned long flag;
701 u16 idx = 0;
702
703 qhp = to_c4iw_qp(ibqp);
704 spin_lock_irqsave(&qhp->lock, flag);
705 if (t4_wq_in_error(&qhp->wq)) {
706 spin_unlock_irqrestore(&qhp->lock, flag);
707 return -EINVAL;
708 }
709 num_wrs = t4_rq_avail(&qhp->wq);
710 if (num_wrs == 0) {
711 spin_unlock_irqrestore(&qhp->lock, flag);
712 return -ENOMEM;
713 }
714 while (wr) {
715 if (wr->num_sge > T4_MAX_RECV_SGE) {
716 err = -EINVAL;
717 *bad_wr = wr;
718 break;
719 }
Steve Wised37ac312010-06-10 19:03:00 +0000720 wqe = (union t4_recv_wr *)((u8 *)qhp->wq.rq.queue +
721 qhp->wq.rq.wq_pidx *
722 T4_EQ_ENTRY_SIZE);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700723 if (num_wrs)
724 err = build_rdma_recv(qhp, wqe, wr, &len16);
725 else
726 err = -ENOMEM;
727 if (err) {
728 *bad_wr = wr;
729 break;
730 }
731
732 qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].wr_id = wr->wr_id;
733
734 wqe->recv.opcode = FW_RI_RECV_WR;
735 wqe->recv.r1 = 0;
736 wqe->recv.wrid = qhp->wq.rq.pidx;
737 wqe->recv.r2[0] = 0;
738 wqe->recv.r2[1] = 0;
739 wqe->recv.r2[2] = 0;
740 wqe->recv.len16 = len16;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700741 PDBG("%s cookie 0x%llx pidx %u\n", __func__,
742 (unsigned long long) wr->wr_id, qhp->wq.rq.pidx);
Steve Wised37ac312010-06-10 19:03:00 +0000743 t4_rq_produce(&qhp->wq, len16);
744 idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700745 wr = wr->next;
746 num_wrs--;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700747 }
748 if (t4_wq_db_enabled(&qhp->wq))
749 t4_ring_rq_db(&qhp->wq, idx);
750 spin_unlock_irqrestore(&qhp->lock, flag);
751 return err;
752}
753
754int c4iw_bind_mw(struct ib_qp *qp, struct ib_mw *mw, struct ib_mw_bind *mw_bind)
755{
756 return -ENOSYS;
757}
758
759static inline void build_term_codes(struct t4_cqe *err_cqe, u8 *layer_type,
760 u8 *ecode)
761{
762 int status;
763 int tagged;
764 int opcode;
765 int rqtype;
766 int send_inv;
767
768 if (!err_cqe) {
769 *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
770 *ecode = 0;
771 return;
772 }
773
774 status = CQE_STATUS(err_cqe);
775 opcode = CQE_OPCODE(err_cqe);
776 rqtype = RQ_TYPE(err_cqe);
777 send_inv = (opcode == FW_RI_SEND_WITH_INV) ||
778 (opcode == FW_RI_SEND_WITH_SE_INV);
779 tagged = (opcode == FW_RI_RDMA_WRITE) ||
780 (rqtype && (opcode == FW_RI_READ_RESP));
781
782 switch (status) {
783 case T4_ERR_STAG:
784 if (send_inv) {
785 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
786 *ecode = RDMAP_CANT_INV_STAG;
787 } else {
788 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
789 *ecode = RDMAP_INV_STAG;
790 }
791 break;
792 case T4_ERR_PDID:
793 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
794 if ((opcode == FW_RI_SEND_WITH_INV) ||
795 (opcode == FW_RI_SEND_WITH_SE_INV))
796 *ecode = RDMAP_CANT_INV_STAG;
797 else
798 *ecode = RDMAP_STAG_NOT_ASSOC;
799 break;
800 case T4_ERR_QPID:
801 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
802 *ecode = RDMAP_STAG_NOT_ASSOC;
803 break;
804 case T4_ERR_ACCESS:
805 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
806 *ecode = RDMAP_ACC_VIOL;
807 break;
808 case T4_ERR_WRAP:
809 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
810 *ecode = RDMAP_TO_WRAP;
811 break;
812 case T4_ERR_BOUND:
813 if (tagged) {
814 *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
815 *ecode = DDPT_BASE_BOUNDS;
816 } else {
817 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
818 *ecode = RDMAP_BASE_BOUNDS;
819 }
820 break;
821 case T4_ERR_INVALIDATE_SHARED_MR:
822 case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
823 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
824 *ecode = RDMAP_CANT_INV_STAG;
825 break;
826 case T4_ERR_ECC:
827 case T4_ERR_ECC_PSTAG:
828 case T4_ERR_INTERNAL_ERR:
829 *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
830 *ecode = 0;
831 break;
832 case T4_ERR_OUT_OF_RQE:
833 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
834 *ecode = DDPU_INV_MSN_NOBUF;
835 break;
836 case T4_ERR_PBL_ADDR_BOUND:
837 *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
838 *ecode = DDPT_BASE_BOUNDS;
839 break;
840 case T4_ERR_CRC:
841 *layer_type = LAYER_MPA|DDP_LLP;
842 *ecode = MPA_CRC_ERR;
843 break;
844 case T4_ERR_MARKER:
845 *layer_type = LAYER_MPA|DDP_LLP;
846 *ecode = MPA_MARKER_ERR;
847 break;
848 case T4_ERR_PDU_LEN_ERR:
849 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
850 *ecode = DDPU_MSG_TOOBIG;
851 break;
852 case T4_ERR_DDP_VERSION:
853 if (tagged) {
854 *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
855 *ecode = DDPT_INV_VERS;
856 } else {
857 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
858 *ecode = DDPU_INV_VERS;
859 }
860 break;
861 case T4_ERR_RDMA_VERSION:
862 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
863 *ecode = RDMAP_INV_VERS;
864 break;
865 case T4_ERR_OPCODE:
866 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
867 *ecode = RDMAP_INV_OPCODE;
868 break;
869 case T4_ERR_DDP_QUEUE_NUM:
870 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
871 *ecode = DDPU_INV_QN;
872 break;
873 case T4_ERR_MSN:
874 case T4_ERR_MSN_GAP:
875 case T4_ERR_MSN_RANGE:
876 case T4_ERR_IRD_OVERFLOW:
877 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
878 *ecode = DDPU_INV_MSN_RANGE;
879 break;
880 case T4_ERR_TBIT:
881 *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
882 *ecode = 0;
883 break;
884 case T4_ERR_MO:
885 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
886 *ecode = DDPU_INV_MO;
887 break;
888 default:
889 *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
890 *ecode = 0;
891 break;
892 }
893}
894
Roland Dreierbe4c9ba2010-05-05 14:45:40 -0700895static void post_terminate(struct c4iw_qp *qhp, struct t4_cqe *err_cqe,
896 gfp_t gfp)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700897{
898 struct fw_ri_wr *wqe;
899 struct sk_buff *skb;
900 struct terminate_message *term;
901
902 PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
903 qhp->ep->hwtid);
904
Roland Dreierbe4c9ba2010-05-05 14:45:40 -0700905 skb = alloc_skb(sizeof *wqe, gfp);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700906 if (!skb)
Roland Dreierbe4c9ba2010-05-05 14:45:40 -0700907 return;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700908 set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
909
910 wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
911 memset(wqe, 0, sizeof *wqe);
912 wqe->op_compl = cpu_to_be32(FW_WR_OP(FW_RI_INIT_WR));
913 wqe->flowid_len16 = cpu_to_be32(
914 FW_WR_FLOWID(qhp->ep->hwtid) |
915 FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
916
917 wqe->u.terminate.type = FW_RI_TYPE_TERMINATE;
918 wqe->u.terminate.immdlen = cpu_to_be32(sizeof *term);
919 term = (struct terminate_message *)wqe->u.terminate.termmsg;
Kumar Sanghvid2fe99e2011-09-25 20:17:44 +0530920 if (qhp->attr.layer_etype == (LAYER_MPA|DDP_LLP)) {
921 term->layer_etype = qhp->attr.layer_etype;
922 term->ecode = qhp->attr.ecode;
923 } else
924 build_term_codes(err_cqe, &term->layer_etype, &term->ecode);
Roland Dreierbe4c9ba2010-05-05 14:45:40 -0700925 c4iw_ofld_send(&qhp->rhp->rdev, skb);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700926}
927
928/*
929 * Assumes qhp lock is held.
930 */
931static void __flush_qp(struct c4iw_qp *qhp, struct c4iw_cq *rchp,
Steve Wise2f5b48c2010-09-10 11:15:36 -0500932 struct c4iw_cq *schp)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700933{
934 int count;
935 int flushed;
Steve Wise2f5b48c2010-09-10 11:15:36 -0500936 unsigned long flag;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700937
938 PDBG("%s qhp %p rchp %p schp %p\n", __func__, qhp, rchp, schp);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700939
Uwe Kleine-König732bee72010-06-11 12:16:59 +0200940 /* locking hierarchy: cq lock first, then qp lock. */
Steve Wise2f5b48c2010-09-10 11:15:36 -0500941 spin_lock_irqsave(&rchp->lock, flag);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700942 spin_lock(&qhp->lock);
943 c4iw_flush_hw_cq(&rchp->cq);
944 c4iw_count_rcqes(&rchp->cq, &qhp->wq, &count);
945 flushed = c4iw_flush_rq(&qhp->wq, &rchp->cq, count);
946 spin_unlock(&qhp->lock);
Steve Wise2f5b48c2010-09-10 11:15:36 -0500947 spin_unlock_irqrestore(&rchp->lock, flag);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700948 if (flushed)
949 (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
950
Uwe Kleine-König732bee72010-06-11 12:16:59 +0200951 /* locking hierarchy: cq lock first, then qp lock. */
Steve Wise2f5b48c2010-09-10 11:15:36 -0500952 spin_lock_irqsave(&schp->lock, flag);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700953 spin_lock(&qhp->lock);
954 c4iw_flush_hw_cq(&schp->cq);
955 c4iw_count_scqes(&schp->cq, &qhp->wq, &count);
956 flushed = c4iw_flush_sq(&qhp->wq, &schp->cq, count);
957 spin_unlock(&qhp->lock);
Steve Wise2f5b48c2010-09-10 11:15:36 -0500958 spin_unlock_irqrestore(&schp->lock, flag);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700959 if (flushed)
960 (*schp->ibcq.comp_handler)(&schp->ibcq, schp->ibcq.cq_context);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700961}
962
Steve Wise2f5b48c2010-09-10 11:15:36 -0500963static void flush_qp(struct c4iw_qp *qhp)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700964{
965 struct c4iw_cq *rchp, *schp;
966
967 rchp = get_chp(qhp->rhp, qhp->attr.rcq);
968 schp = get_chp(qhp->rhp, qhp->attr.scq);
969
970 if (qhp->ibqp.uobject) {
971 t4_set_wq_in_error(&qhp->wq);
972 t4_set_cq_in_error(&rchp->cq);
973 if (schp != rchp)
974 t4_set_cq_in_error(&schp->cq);
975 return;
976 }
Steve Wise2f5b48c2010-09-10 11:15:36 -0500977 __flush_qp(qhp, rchp, schp);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700978}
979
Steve Wise73d6fca2010-07-23 19:12:27 +0000980static int rdma_fini(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
981 struct c4iw_ep *ep)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700982{
983 struct fw_ri_wr *wqe;
984 int ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700985 struct sk_buff *skb;
986
987 PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
Steve Wise73d6fca2010-07-23 19:12:27 +0000988 ep->hwtid);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700989
David Rientjesd3c814e2010-07-21 02:44:56 +0000990 skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700991 if (!skb)
992 return -ENOMEM;
Steve Wise73d6fca2010-07-23 19:12:27 +0000993 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700994
995 wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
996 memset(wqe, 0, sizeof *wqe);
997 wqe->op_compl = cpu_to_be32(
998 FW_WR_OP(FW_RI_INIT_WR) |
999 FW_WR_COMPL(1));
1000 wqe->flowid_len16 = cpu_to_be32(
Steve Wise73d6fca2010-07-23 19:12:27 +00001001 FW_WR_FLOWID(ep->hwtid) |
Steve Wisecfdda9d2010-04-21 15:30:06 -07001002 FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
Steve Wise2f5b48c2010-09-10 11:15:36 -05001003 wqe->cookie = (unsigned long) &ep->com.wr_wait;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001004
1005 wqe->u.fini.type = FW_RI_TYPE_FINI;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001006 ret = c4iw_ofld_send(&rhp->rdev, skb);
1007 if (ret)
1008 goto out;
1009
Steve Wise2f5b48c2010-09-10 11:15:36 -05001010 ret = c4iw_wait_for_reply(&rhp->rdev, &ep->com.wr_wait, qhp->ep->hwtid,
Steve Wiseaadc4df2010-09-10 11:15:25 -05001011 qhp->wq.sq.qid, __func__);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001012out:
1013 PDBG("%s ret %d\n", __func__, ret);
1014 return ret;
1015}
1016
1017static void build_rtr_msg(u8 p2p_type, struct fw_ri_init *init)
1018{
Kumar Sanghvid2fe99e2011-09-25 20:17:44 +05301019 PDBG("%s p2p_type = %d\n", __func__, p2p_type);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001020 memset(&init->u, 0, sizeof init->u);
1021 switch (p2p_type) {
1022 case FW_RI_INIT_P2PTYPE_RDMA_WRITE:
1023 init->u.write.opcode = FW_RI_RDMA_WRITE_WR;
1024 init->u.write.stag_sink = cpu_to_be32(1);
1025 init->u.write.to_sink = cpu_to_be64(1);
1026 init->u.write.u.immd_src[0].op = FW_RI_DATA_IMMD;
1027 init->u.write.len16 = DIV_ROUND_UP(sizeof init->u.write +
1028 sizeof(struct fw_ri_immd),
1029 16);
1030 break;
1031 case FW_RI_INIT_P2PTYPE_READ_REQ:
1032 init->u.write.opcode = FW_RI_RDMA_READ_WR;
1033 init->u.read.stag_src = cpu_to_be32(1);
1034 init->u.read.to_src_lo = cpu_to_be32(1);
1035 init->u.read.stag_sink = cpu_to_be32(1);
1036 init->u.read.to_sink_lo = cpu_to_be32(1);
1037 init->u.read.len16 = DIV_ROUND_UP(sizeof init->u.read, 16);
1038 break;
1039 }
1040}
1041
1042static int rdma_init(struct c4iw_dev *rhp, struct c4iw_qp *qhp)
1043{
1044 struct fw_ri_wr *wqe;
1045 int ret;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001046 struct sk_buff *skb;
1047
1048 PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
1049 qhp->ep->hwtid);
1050
David Rientjesd3c814e2010-07-21 02:44:56 +00001051 skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001052 if (!skb)
1053 return -ENOMEM;
1054 set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
1055
1056 wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
1057 memset(wqe, 0, sizeof *wqe);
1058 wqe->op_compl = cpu_to_be32(
1059 FW_WR_OP(FW_RI_INIT_WR) |
1060 FW_WR_COMPL(1));
1061 wqe->flowid_len16 = cpu_to_be32(
1062 FW_WR_FLOWID(qhp->ep->hwtid) |
1063 FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
1064
Steve Wise2f5b48c2010-09-10 11:15:36 -05001065 wqe->cookie = (unsigned long) &qhp->ep->com.wr_wait;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001066
1067 wqe->u.init.type = FW_RI_TYPE_INIT;
1068 wqe->u.init.mpareqbit_p2ptype =
1069 V_FW_RI_WR_MPAREQBIT(qhp->attr.mpa_attr.initiator) |
1070 V_FW_RI_WR_P2PTYPE(qhp->attr.mpa_attr.p2p_type);
1071 wqe->u.init.mpa_attrs = FW_RI_MPA_IETF_ENABLE;
1072 if (qhp->attr.mpa_attr.recv_marker_enabled)
1073 wqe->u.init.mpa_attrs |= FW_RI_MPA_RX_MARKER_ENABLE;
1074 if (qhp->attr.mpa_attr.xmit_marker_enabled)
1075 wqe->u.init.mpa_attrs |= FW_RI_MPA_TX_MARKER_ENABLE;
1076 if (qhp->attr.mpa_attr.crc_enabled)
1077 wqe->u.init.mpa_attrs |= FW_RI_MPA_CRC_ENABLE;
1078
1079 wqe->u.init.qp_caps = FW_RI_QP_RDMA_READ_ENABLE |
1080 FW_RI_QP_RDMA_WRITE_ENABLE |
1081 FW_RI_QP_BIND_ENABLE;
1082 if (!qhp->ibqp.uobject)
1083 wqe->u.init.qp_caps |= FW_RI_QP_FAST_REGISTER_ENABLE |
1084 FW_RI_QP_STAG0_ENABLE;
1085 wqe->u.init.nrqe = cpu_to_be16(t4_rqes_posted(&qhp->wq));
1086 wqe->u.init.pdid = cpu_to_be32(qhp->attr.pd);
1087 wqe->u.init.qpid = cpu_to_be32(qhp->wq.sq.qid);
1088 wqe->u.init.sq_eqid = cpu_to_be32(qhp->wq.sq.qid);
1089 wqe->u.init.rq_eqid = cpu_to_be32(qhp->wq.rq.qid);
1090 wqe->u.init.scqid = cpu_to_be32(qhp->attr.scq);
1091 wqe->u.init.rcqid = cpu_to_be32(qhp->attr.rcq);
1092 wqe->u.init.ord_max = cpu_to_be32(qhp->attr.max_ord);
1093 wqe->u.init.ird_max = cpu_to_be32(qhp->attr.max_ird);
1094 wqe->u.init.iss = cpu_to_be32(qhp->ep->snd_seq);
1095 wqe->u.init.irs = cpu_to_be32(qhp->ep->rcv_seq);
1096 wqe->u.init.hwrqsize = cpu_to_be32(qhp->wq.rq.rqt_size);
1097 wqe->u.init.hwrqaddr = cpu_to_be32(qhp->wq.rq.rqt_hwaddr -
1098 rhp->rdev.lldi.vr->rq.start);
1099 if (qhp->attr.mpa_attr.initiator)
1100 build_rtr_msg(qhp->attr.mpa_attr.p2p_type, &wqe->u.init);
1101
Steve Wisecfdda9d2010-04-21 15:30:06 -07001102 ret = c4iw_ofld_send(&rhp->rdev, skb);
1103 if (ret)
1104 goto out;
1105
Steve Wise2f5b48c2010-09-10 11:15:36 -05001106 ret = c4iw_wait_for_reply(&rhp->rdev, &qhp->ep->com.wr_wait,
1107 qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001108out:
1109 PDBG("%s ret %d\n", __func__, ret);
1110 return ret;
1111}
1112
1113int c4iw_modify_qp(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
1114 enum c4iw_qp_attr_mask mask,
1115 struct c4iw_qp_attributes *attrs,
1116 int internal)
1117{
1118 int ret = 0;
1119 struct c4iw_qp_attributes newattr = qhp->attr;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001120 int disconnect = 0;
1121 int terminate = 0;
1122 int abort = 0;
1123 int free = 0;
1124 struct c4iw_ep *ep = NULL;
1125
1126 PDBG("%s qhp %p sqid 0x%x rqid 0x%x ep %p state %d -> %d\n", __func__,
1127 qhp, qhp->wq.sq.qid, qhp->wq.rq.qid, qhp->ep, qhp->attr.state,
1128 (mask & C4IW_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
1129
Steve Wise2f5b48c2010-09-10 11:15:36 -05001130 mutex_lock(&qhp->mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001131
1132 /* Process attr changes if in IDLE */
1133 if (mask & C4IW_QP_ATTR_VALID_MODIFY) {
1134 if (qhp->attr.state != C4IW_QP_STATE_IDLE) {
1135 ret = -EIO;
1136 goto out;
1137 }
1138 if (mask & C4IW_QP_ATTR_ENABLE_RDMA_READ)
1139 newattr.enable_rdma_read = attrs->enable_rdma_read;
1140 if (mask & C4IW_QP_ATTR_ENABLE_RDMA_WRITE)
1141 newattr.enable_rdma_write = attrs->enable_rdma_write;
1142 if (mask & C4IW_QP_ATTR_ENABLE_RDMA_BIND)
1143 newattr.enable_bind = attrs->enable_bind;
1144 if (mask & C4IW_QP_ATTR_MAX_ORD) {
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001145 if (attrs->max_ord > c4iw_max_read_depth) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07001146 ret = -EINVAL;
1147 goto out;
1148 }
1149 newattr.max_ord = attrs->max_ord;
1150 }
1151 if (mask & C4IW_QP_ATTR_MAX_IRD) {
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001152 if (attrs->max_ird > c4iw_max_read_depth) {
Steve Wisecfdda9d2010-04-21 15:30:06 -07001153 ret = -EINVAL;
1154 goto out;
1155 }
1156 newattr.max_ird = attrs->max_ird;
1157 }
1158 qhp->attr = newattr;
1159 }
1160
1161 if (!(mask & C4IW_QP_ATTR_NEXT_STATE))
1162 goto out;
1163 if (qhp->attr.state == attrs->next_state)
1164 goto out;
1165
1166 switch (qhp->attr.state) {
1167 case C4IW_QP_STATE_IDLE:
1168 switch (attrs->next_state) {
1169 case C4IW_QP_STATE_RTS:
1170 if (!(mask & C4IW_QP_ATTR_LLP_STREAM_HANDLE)) {
1171 ret = -EINVAL;
1172 goto out;
1173 }
1174 if (!(mask & C4IW_QP_ATTR_MPA_ATTR)) {
1175 ret = -EINVAL;
1176 goto out;
1177 }
1178 qhp->attr.mpa_attr = attrs->mpa_attr;
1179 qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
1180 qhp->ep = qhp->attr.llp_stream_handle;
Steve Wise2f5b48c2010-09-10 11:15:36 -05001181 set_state(qhp, C4IW_QP_STATE_RTS);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001182
1183 /*
1184 * Ref the endpoint here and deref when we
1185 * disassociate the endpoint from the QP. This
1186 * happens in CLOSING->IDLE transition or *->ERROR
1187 * transition.
1188 */
1189 c4iw_get_ep(&qhp->ep->com);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001190 ret = rdma_init(rhp, qhp);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001191 if (ret)
1192 goto err;
1193 break;
1194 case C4IW_QP_STATE_ERROR:
Steve Wise2f5b48c2010-09-10 11:15:36 -05001195 set_state(qhp, C4IW_QP_STATE_ERROR);
1196 flush_qp(qhp);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001197 break;
1198 default:
1199 ret = -EINVAL;
1200 goto out;
1201 }
1202 break;
1203 case C4IW_QP_STATE_RTS:
1204 switch (attrs->next_state) {
1205 case C4IW_QP_STATE_CLOSING:
1206 BUG_ON(atomic_read(&qhp->ep->com.kref.refcount) < 2);
Steve Wise2f5b48c2010-09-10 11:15:36 -05001207 set_state(qhp, C4IW_QP_STATE_CLOSING);
Steve Wise73d6fca2010-07-23 19:12:27 +00001208 ep = qhp->ep;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001209 if (!internal) {
1210 abort = 0;
1211 disconnect = 1;
Steve Wise2f5b48c2010-09-10 11:15:36 -05001212 c4iw_get_ep(&qhp->ep->com);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001213 }
Steve Wise73d6fca2010-07-23 19:12:27 +00001214 ret = rdma_fini(rhp, qhp, ep);
Steve Wise8da7e7a2011-06-14 20:59:27 +00001215 if (ret)
Steve Wisecfdda9d2010-04-21 15:30:06 -07001216 goto err;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001217 break;
1218 case C4IW_QP_STATE_TERMINATE:
Steve Wise2f5b48c2010-09-10 11:15:36 -05001219 set_state(qhp, C4IW_QP_STATE_TERMINATE);
Kumar Sanghvid2fe99e2011-09-25 20:17:44 +05301220 qhp->attr.layer_etype = attrs->layer_etype;
1221 qhp->attr.ecode = attrs->ecode;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001222 if (qhp->ibqp.uobject)
1223 t4_set_wq_in_error(&qhp->wq);
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001224 ep = qhp->ep;
Steve Wise0e42c1f2010-09-10 11:15:09 -05001225 if (!internal)
1226 terminate = 1;
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001227 disconnect = 1;
Steve Wise2f5b48c2010-09-10 11:15:36 -05001228 c4iw_get_ep(&qhp->ep->com);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001229 break;
1230 case C4IW_QP_STATE_ERROR:
Steve Wise2f5b48c2010-09-10 11:15:36 -05001231 set_state(qhp, C4IW_QP_STATE_ERROR);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001232 if (!internal) {
1233 abort = 1;
1234 disconnect = 1;
1235 ep = qhp->ep;
Steve Wise2f5b48c2010-09-10 11:15:36 -05001236 c4iw_get_ep(&qhp->ep->com);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001237 }
1238 goto err;
1239 break;
1240 default:
1241 ret = -EINVAL;
1242 goto out;
1243 }
1244 break;
1245 case C4IW_QP_STATE_CLOSING:
1246 if (!internal) {
1247 ret = -EINVAL;
1248 goto out;
1249 }
1250 switch (attrs->next_state) {
1251 case C4IW_QP_STATE_IDLE:
Steve Wise2f5b48c2010-09-10 11:15:36 -05001252 flush_qp(qhp);
1253 set_state(qhp, C4IW_QP_STATE_IDLE);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001254 qhp->attr.llp_stream_handle = NULL;
1255 c4iw_put_ep(&qhp->ep->com);
1256 qhp->ep = NULL;
1257 wake_up(&qhp->wait);
1258 break;
1259 case C4IW_QP_STATE_ERROR:
1260 goto err;
1261 default:
1262 ret = -EINVAL;
1263 goto err;
1264 }
1265 break;
1266 case C4IW_QP_STATE_ERROR:
1267 if (attrs->next_state != C4IW_QP_STATE_IDLE) {
1268 ret = -EINVAL;
1269 goto out;
1270 }
1271 if (!t4_sq_empty(&qhp->wq) || !t4_rq_empty(&qhp->wq)) {
1272 ret = -EINVAL;
1273 goto out;
1274 }
Steve Wise2f5b48c2010-09-10 11:15:36 -05001275 set_state(qhp, C4IW_QP_STATE_IDLE);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001276 break;
1277 case C4IW_QP_STATE_TERMINATE:
1278 if (!internal) {
1279 ret = -EINVAL;
1280 goto out;
1281 }
1282 goto err;
1283 break;
1284 default:
1285 printk(KERN_ERR "%s in a bad state %d\n",
1286 __func__, qhp->attr.state);
1287 ret = -EINVAL;
1288 goto err;
1289 break;
1290 }
1291 goto out;
1292err:
1293 PDBG("%s disassociating ep %p qpid 0x%x\n", __func__, qhp->ep,
1294 qhp->wq.sq.qid);
1295
1296 /* disassociate the LLP connection */
1297 qhp->attr.llp_stream_handle = NULL;
Steve Wiseaf93fb52010-09-10 11:14:48 -05001298 if (!ep)
1299 ep = qhp->ep;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001300 qhp->ep = NULL;
Steve Wise2f5b48c2010-09-10 11:15:36 -05001301 set_state(qhp, C4IW_QP_STATE_ERROR);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001302 free = 1;
1303 wake_up(&qhp->wait);
1304 BUG_ON(!ep);
Steve Wise2f5b48c2010-09-10 11:15:36 -05001305 flush_qp(qhp);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001306out:
Steve Wise2f5b48c2010-09-10 11:15:36 -05001307 mutex_unlock(&qhp->mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001308
1309 if (terminate)
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001310 post_terminate(qhp, NULL, internal ? GFP_ATOMIC : GFP_KERNEL);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001311
1312 /*
1313 * If disconnect is 1, then we need to initiate a disconnect
1314 * on the EP. This can be a normal close (RTS->CLOSING) or
1315 * an abnormal close (RTS/CLOSING->ERROR).
1316 */
1317 if (disconnect) {
Roland Dreierbe4c9ba2010-05-05 14:45:40 -07001318 c4iw_ep_disconnect(ep, abort, internal ? GFP_ATOMIC :
1319 GFP_KERNEL);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001320 c4iw_put_ep(&ep->com);
1321 }
1322
1323 /*
1324 * If free is 1, then we've disassociated the EP from the QP
1325 * and we need to dereference the EP.
1326 */
1327 if (free)
1328 c4iw_put_ep(&ep->com);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001329 PDBG("%s exit state %d\n", __func__, qhp->attr.state);
1330 return ret;
1331}
1332
1333int c4iw_destroy_qp(struct ib_qp *ib_qp)
1334{
1335 struct c4iw_dev *rhp;
1336 struct c4iw_qp *qhp;
1337 struct c4iw_qp_attributes attrs;
1338 struct c4iw_ucontext *ucontext;
1339
1340 qhp = to_c4iw_qp(ib_qp);
1341 rhp = qhp->rhp;
1342
1343 attrs.next_state = C4IW_QP_STATE_ERROR;
Kumar Sanghvid2fe99e2011-09-25 20:17:44 +05301344 if (qhp->attr.state == C4IW_QP_STATE_TERMINATE)
1345 c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1346 else
1347 c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001348 wait_event(qhp->wait, !qhp->ep);
1349
1350 remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001351 atomic_dec(&qhp->refcnt);
1352 wait_event(qhp->wait, !atomic_read(&qhp->refcnt));
1353
1354 ucontext = ib_qp->uobject ?
1355 to_c4iw_ucontext(ib_qp->uobject->context) : NULL;
1356 destroy_qp(&rhp->rdev, &qhp->wq,
1357 ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
1358
1359 PDBG("%s ib_qp %p qpid 0x%0x\n", __func__, ib_qp, qhp->wq.sq.qid);
1360 kfree(qhp);
1361 return 0;
1362}
1363
1364struct ib_qp *c4iw_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
1365 struct ib_udata *udata)
1366{
1367 struct c4iw_dev *rhp;
1368 struct c4iw_qp *qhp;
1369 struct c4iw_pd *php;
1370 struct c4iw_cq *schp;
1371 struct c4iw_cq *rchp;
1372 struct c4iw_create_qp_resp uresp;
1373 int sqsize, rqsize;
1374 struct c4iw_ucontext *ucontext;
1375 int ret;
Steve Wisec6d7b262010-09-13 11:23:57 -05001376 struct c4iw_mm_entry *mm1, *mm2, *mm3, *mm4, *mm5 = NULL;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001377
1378 PDBG("%s ib_pd %p\n", __func__, pd);
1379
1380 if (attrs->qp_type != IB_QPT_RC)
1381 return ERR_PTR(-EINVAL);
1382
1383 php = to_c4iw_pd(pd);
1384 rhp = php->rhp;
1385 schp = get_chp(rhp, ((struct c4iw_cq *)attrs->send_cq)->cq.cqid);
1386 rchp = get_chp(rhp, ((struct c4iw_cq *)attrs->recv_cq)->cq.cqid);
1387 if (!schp || !rchp)
1388 return ERR_PTR(-EINVAL);
1389
1390 if (attrs->cap.max_inline_data > T4_MAX_SEND_INLINE)
1391 return ERR_PTR(-EINVAL);
1392
1393 rqsize = roundup(attrs->cap.max_recv_wr + 1, 16);
1394 if (rqsize > T4_MAX_RQ_SIZE)
1395 return ERR_PTR(-E2BIG);
1396
1397 sqsize = roundup(attrs->cap.max_send_wr + 1, 16);
1398 if (sqsize > T4_MAX_SQ_SIZE)
1399 return ERR_PTR(-E2BIG);
1400
1401 ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
1402
1403
1404 qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
1405 if (!qhp)
1406 return ERR_PTR(-ENOMEM);
1407 qhp->wq.sq.size = sqsize;
1408 qhp->wq.sq.memsize = (sqsize + 1) * sizeof *qhp->wq.sq.queue;
1409 qhp->wq.rq.size = rqsize;
1410 qhp->wq.rq.memsize = (rqsize + 1) * sizeof *qhp->wq.rq.queue;
1411
1412 if (ucontext) {
1413 qhp->wq.sq.memsize = roundup(qhp->wq.sq.memsize, PAGE_SIZE);
1414 qhp->wq.rq.memsize = roundup(qhp->wq.rq.memsize, PAGE_SIZE);
1415 }
1416
1417 PDBG("%s sqsize %u sqmemsize %zu rqsize %u rqmemsize %zu\n",
1418 __func__, sqsize, qhp->wq.sq.memsize, rqsize, qhp->wq.rq.memsize);
1419
1420 ret = create_qp(&rhp->rdev, &qhp->wq, &schp->cq, &rchp->cq,
1421 ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
1422 if (ret)
1423 goto err1;
1424
1425 attrs->cap.max_recv_wr = rqsize - 1;
1426 attrs->cap.max_send_wr = sqsize - 1;
1427 attrs->cap.max_inline_data = T4_MAX_SEND_INLINE;
1428
1429 qhp->rhp = rhp;
1430 qhp->attr.pd = php->pdid;
1431 qhp->attr.scq = ((struct c4iw_cq *) attrs->send_cq)->cq.cqid;
1432 qhp->attr.rcq = ((struct c4iw_cq *) attrs->recv_cq)->cq.cqid;
1433 qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
1434 qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
1435 qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
1436 qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
1437 qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
1438 qhp->attr.state = C4IW_QP_STATE_IDLE;
1439 qhp->attr.next_state = C4IW_QP_STATE_IDLE;
1440 qhp->attr.enable_rdma_read = 1;
1441 qhp->attr.enable_rdma_write = 1;
1442 qhp->attr.enable_bind = 1;
1443 qhp->attr.max_ord = 1;
1444 qhp->attr.max_ird = 1;
1445 spin_lock_init(&qhp->lock);
Steve Wise2f5b48c2010-09-10 11:15:36 -05001446 mutex_init(&qhp->mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001447 init_waitqueue_head(&qhp->wait);
1448 atomic_set(&qhp->refcnt, 1);
1449
1450 ret = insert_handle(rhp, &rhp->qpidr, qhp, qhp->wq.sq.qid);
1451 if (ret)
1452 goto err2;
1453
Steve Wisecfdda9d2010-04-21 15:30:06 -07001454 if (udata) {
1455 mm1 = kmalloc(sizeof *mm1, GFP_KERNEL);
1456 if (!mm1) {
1457 ret = -ENOMEM;
Steve Wise30a6a622010-05-20 16:58:21 -05001458 goto err3;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001459 }
1460 mm2 = kmalloc(sizeof *mm2, GFP_KERNEL);
1461 if (!mm2) {
1462 ret = -ENOMEM;
Steve Wise30a6a622010-05-20 16:58:21 -05001463 goto err4;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001464 }
1465 mm3 = kmalloc(sizeof *mm3, GFP_KERNEL);
1466 if (!mm3) {
1467 ret = -ENOMEM;
Steve Wise30a6a622010-05-20 16:58:21 -05001468 goto err5;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001469 }
1470 mm4 = kmalloc(sizeof *mm4, GFP_KERNEL);
1471 if (!mm4) {
1472 ret = -ENOMEM;
Steve Wise30a6a622010-05-20 16:58:21 -05001473 goto err6;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001474 }
Steve Wisec6d7b262010-09-13 11:23:57 -05001475 if (t4_sq_onchip(&qhp->wq.sq)) {
1476 mm5 = kmalloc(sizeof *mm5, GFP_KERNEL);
1477 if (!mm5) {
1478 ret = -ENOMEM;
1479 goto err7;
1480 }
1481 uresp.flags = C4IW_QPF_ONCHIP;
1482 } else
1483 uresp.flags = 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001484 uresp.qid_mask = rhp->rdev.qpmask;
1485 uresp.sqid = qhp->wq.sq.qid;
1486 uresp.sq_size = qhp->wq.sq.size;
1487 uresp.sq_memsize = qhp->wq.sq.memsize;
1488 uresp.rqid = qhp->wq.rq.qid;
1489 uresp.rq_size = qhp->wq.rq.size;
1490 uresp.rq_memsize = qhp->wq.rq.memsize;
1491 spin_lock(&ucontext->mmap_lock);
Steve Wisec6d7b262010-09-13 11:23:57 -05001492 if (mm5) {
1493 uresp.ma_sync_key = ucontext->key;
1494 ucontext->key += PAGE_SIZE;
1495 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001496 uresp.sq_key = ucontext->key;
1497 ucontext->key += PAGE_SIZE;
1498 uresp.rq_key = ucontext->key;
1499 ucontext->key += PAGE_SIZE;
1500 uresp.sq_db_gts_key = ucontext->key;
1501 ucontext->key += PAGE_SIZE;
1502 uresp.rq_db_gts_key = ucontext->key;
1503 ucontext->key += PAGE_SIZE;
1504 spin_unlock(&ucontext->mmap_lock);
1505 ret = ib_copy_to_udata(udata, &uresp, sizeof uresp);
1506 if (ret)
Steve Wisec6d7b262010-09-13 11:23:57 -05001507 goto err8;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001508 mm1->key = uresp.sq_key;
Steve Wisec6d7b262010-09-13 11:23:57 -05001509 mm1->addr = qhp->wq.sq.phys_addr;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001510 mm1->len = PAGE_ALIGN(qhp->wq.sq.memsize);
1511 insert_mmap(ucontext, mm1);
1512 mm2->key = uresp.rq_key;
1513 mm2->addr = virt_to_phys(qhp->wq.rq.queue);
1514 mm2->len = PAGE_ALIGN(qhp->wq.rq.memsize);
1515 insert_mmap(ucontext, mm2);
1516 mm3->key = uresp.sq_db_gts_key;
1517 mm3->addr = qhp->wq.sq.udb;
1518 mm3->len = PAGE_SIZE;
1519 insert_mmap(ucontext, mm3);
1520 mm4->key = uresp.rq_db_gts_key;
1521 mm4->addr = qhp->wq.rq.udb;
1522 mm4->len = PAGE_SIZE;
1523 insert_mmap(ucontext, mm4);
Steve Wisec6d7b262010-09-13 11:23:57 -05001524 if (mm5) {
1525 mm5->key = uresp.ma_sync_key;
1526 mm5->addr = (pci_resource_start(rhp->rdev.lldi.pdev, 0)
1527 + A_PCIE_MA_SYNC) & PAGE_MASK;
1528 mm5->len = PAGE_SIZE;
1529 insert_mmap(ucontext, mm5);
1530 }
Steve Wisecfdda9d2010-04-21 15:30:06 -07001531 }
1532 qhp->ibqp.qp_num = qhp->wq.sq.qid;
1533 init_timer(&(qhp->timer));
1534 PDBG("%s qhp %p sq_num_entries %d, rq_num_entries %d qpid 0x%0x\n",
1535 __func__, qhp, qhp->attr.sq_num_entries, qhp->attr.rq_num_entries,
1536 qhp->wq.sq.qid);
1537 return &qhp->ibqp;
Steve Wisec6d7b262010-09-13 11:23:57 -05001538err8:
1539 kfree(mm5);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001540err7:
Steve Wise30a6a622010-05-20 16:58:21 -05001541 kfree(mm4);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001542err6:
Steve Wise30a6a622010-05-20 16:58:21 -05001543 kfree(mm3);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001544err5:
Steve Wise30a6a622010-05-20 16:58:21 -05001545 kfree(mm2);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001546err4:
Steve Wise30a6a622010-05-20 16:58:21 -05001547 kfree(mm1);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001548err3:
1549 remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
1550err2:
1551 destroy_qp(&rhp->rdev, &qhp->wq,
1552 ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
1553err1:
1554 kfree(qhp);
1555 return ERR_PTR(ret);
1556}
1557
1558int c4iw_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1559 int attr_mask, struct ib_udata *udata)
1560{
1561 struct c4iw_dev *rhp;
1562 struct c4iw_qp *qhp;
1563 enum c4iw_qp_attr_mask mask = 0;
1564 struct c4iw_qp_attributes attrs;
1565
1566 PDBG("%s ib_qp %p\n", __func__, ibqp);
1567
1568 /* iwarp does not support the RTR state */
1569 if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
1570 attr_mask &= ~IB_QP_STATE;
1571
1572 /* Make sure we still have something left to do */
1573 if (!attr_mask)
1574 return 0;
1575
1576 memset(&attrs, 0, sizeof attrs);
1577 qhp = to_c4iw_qp(ibqp);
1578 rhp = qhp->rhp;
1579
1580 attrs.next_state = c4iw_convert_state(attr->qp_state);
1581 attrs.enable_rdma_read = (attr->qp_access_flags &
1582 IB_ACCESS_REMOTE_READ) ? 1 : 0;
1583 attrs.enable_rdma_write = (attr->qp_access_flags &
1584 IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
1585 attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
1586
1587
1588 mask |= (attr_mask & IB_QP_STATE) ? C4IW_QP_ATTR_NEXT_STATE : 0;
1589 mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
1590 (C4IW_QP_ATTR_ENABLE_RDMA_READ |
1591 C4IW_QP_ATTR_ENABLE_RDMA_WRITE |
1592 C4IW_QP_ATTR_ENABLE_RDMA_BIND) : 0;
1593
1594 return c4iw_modify_qp(rhp, qhp, mask, &attrs, 0);
1595}
1596
1597struct ib_qp *c4iw_get_qp(struct ib_device *dev, int qpn)
1598{
1599 PDBG("%s ib_dev %p qpn 0x%x\n", __func__, dev, qpn);
1600 return (struct ib_qp *)get_qhp(to_c4iw_dev(dev), qpn);
1601}