RDMA/cxgb4: Add Support for Chelsio T5 adapter
[linux-2.6-block.git] / drivers / infiniband / hw / cxgb4 / qp.c
CommitLineData
cfdda9d7
SW
1/*
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 */
e4dd23d7
PG
32
33#include <linux/module.h>
34
cfdda9d7
SW
35#include "iw_cxgb4.h"
36
2c974781
VP
37static 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
a9c77198 41static int ocqp_support = 1;
c6d7b267 42module_param(ocqp_support, int, 0644);
a9c77198 43MODULE_PARM_DESC(ocqp_support, "Support on-chip SQs (default=1)");
c6d7b267 44
422eea0a
VP
45int db_fc_threshold = 2000;
46module_param(db_fc_threshold, int, 0644);
47MODULE_PARM_DESC(db_fc_threshold, "QP count/threshold that triggers automatic "
48 "db flow control mode (default = 2000)");
49
2f5b48c3
SW
50static void set_state(struct c4iw_qp *qhp, enum c4iw_qp_state state)
51{
52 unsigned long flag;
53 spin_lock_irqsave(&qhp->lock, flag);
54 qhp->attr.state = state;
55 spin_unlock_irqrestore(&qhp->lock, flag);
56}
57
c6d7b267
SW
58static void dealloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
59{
60 c4iw_ocqp_pool_free(rdev, sq->dma_addr, sq->memsize);
61}
62
63static void dealloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
64{
65 dma_free_coherent(&(rdev->lldi.pdev->dev), sq->memsize, sq->queue,
66 pci_unmap_addr(sq, mapping));
67}
68
69static void dealloc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
70{
71 if (t4_sq_onchip(sq))
72 dealloc_oc_sq(rdev, sq);
73 else
74 dealloc_host_sq(rdev, sq);
75}
76
77static int alloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
78{
f079af7a 79 if (!ocqp_support || !ocqp_supported(&rdev->lldi))
c6d7b267
SW
80 return -ENOSYS;
81 sq->dma_addr = c4iw_ocqp_pool_alloc(rdev, sq->memsize);
82 if (!sq->dma_addr)
83 return -ENOMEM;
84 sq->phys_addr = rdev->oc_mw_pa + sq->dma_addr -
85 rdev->lldi.vr->ocq.start;
86 sq->queue = (__force union t4_wr *)(rdev->oc_mw_kva + sq->dma_addr -
87 rdev->lldi.vr->ocq.start);
88 sq->flags |= T4_SQ_ONCHIP;
89 return 0;
90}
91
92static int alloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
93{
94 sq->queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev), sq->memsize,
95 &(sq->dma_addr), GFP_KERNEL);
96 if (!sq->queue)
97 return -ENOMEM;
98 sq->phys_addr = virt_to_phys(sq->queue);
99 pci_unmap_addr_set(sq, mapping, sq->dma_addr);
100 return 0;
101}
102
cfdda9d7
SW
103static int destroy_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
104 struct c4iw_dev_ucontext *uctx)
105{
106 /*
107 * uP clears EQ contexts when the connection exits rdma mode,
108 * so no need to post a RESET WR for these EQs.
109 */
110 dma_free_coherent(&(rdev->lldi.pdev->dev),
111 wq->rq.memsize, wq->rq.queue,
f38926aa 112 dma_unmap_addr(&wq->rq, mapping));
c6d7b267 113 dealloc_sq(rdev, &wq->sq);
cfdda9d7
SW
114 c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
115 kfree(wq->rq.sw_rq);
116 kfree(wq->sq.sw_sq);
117 c4iw_put_qpid(rdev, wq->rq.qid, uctx);
118 c4iw_put_qpid(rdev, wq->sq.qid, uctx);
119 return 0;
120}
121
122static int create_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
123 struct t4_cq *rcq, struct t4_cq *scq,
124 struct c4iw_dev_ucontext *uctx)
125{
126 int user = (uctx != &rdev->uctx);
127 struct fw_ri_res_wr *res_wr;
128 struct fw_ri_res *res;
129 int wr_len;
130 struct c4iw_wr_wait wr_wait;
131 struct sk_buff *skb;
132 int ret;
133 int eqsize;
134
135 wq->sq.qid = c4iw_get_qpid(rdev, uctx);
136 if (!wq->sq.qid)
137 return -ENOMEM;
138
139 wq->rq.qid = c4iw_get_qpid(rdev, uctx);
c079c287
EG
140 if (!wq->rq.qid) {
141 ret = -ENOMEM;
142 goto free_sq_qid;
143 }
cfdda9d7
SW
144
145 if (!user) {
146 wq->sq.sw_sq = kzalloc(wq->sq.size * sizeof *wq->sq.sw_sq,
147 GFP_KERNEL);
c079c287
EG
148 if (!wq->sq.sw_sq) {
149 ret = -ENOMEM;
150 goto free_rq_qid;
151 }
cfdda9d7
SW
152
153 wq->rq.sw_rq = kzalloc(wq->rq.size * sizeof *wq->rq.sw_rq,
154 GFP_KERNEL);
c079c287
EG
155 if (!wq->rq.sw_rq) {
156 ret = -ENOMEM;
157 goto free_sw_sq;
158 }
cfdda9d7
SW
159 }
160
161 /*
162 * RQT must be a power of 2.
163 */
164 wq->rq.rqt_size = roundup_pow_of_two(wq->rq.size);
165 wq->rq.rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rq.rqt_size);
c079c287
EG
166 if (!wq->rq.rqt_hwaddr) {
167 ret = -ENOMEM;
168 goto free_sw_rq;
169 }
cfdda9d7 170
c6d7b267 171 if (user) {
c079c287
EG
172 ret = alloc_oc_sq(rdev, &wq->sq);
173 if (ret)
174 goto free_hwaddr;
175
176 ret = alloc_host_sq(rdev, &wq->sq);
177 if (ret)
178 goto free_sq;
c6d7b267 179 } else
c079c287
EG
180 ret = alloc_host_sq(rdev, &wq->sq);
181 if (ret)
182 goto free_hwaddr;
cfdda9d7 183 memset(wq->sq.queue, 0, wq->sq.memsize);
f38926aa 184 dma_unmap_addr_set(&wq->sq, mapping, wq->sq.dma_addr);
cfdda9d7
SW
185
186 wq->rq.queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev),
187 wq->rq.memsize, &(wq->rq.dma_addr),
188 GFP_KERNEL);
189 if (!wq->rq.queue)
c079c287 190 goto free_sq;
cfdda9d7
SW
191 PDBG("%s sq base va 0x%p pa 0x%llx rq base va 0x%p pa 0x%llx\n",
192 __func__, wq->sq.queue,
193 (unsigned long long)virt_to_phys(wq->sq.queue),
194 wq->rq.queue,
195 (unsigned long long)virt_to_phys(wq->rq.queue));
196 memset(wq->rq.queue, 0, wq->rq.memsize);
f38926aa 197 dma_unmap_addr_set(&wq->rq, mapping, wq->rq.dma_addr);
cfdda9d7
SW
198
199 wq->db = rdev->lldi.db_reg;
200 wq->gts = rdev->lldi.gts_reg;
201 if (user) {
202 wq->sq.udb = (u64)pci_resource_start(rdev->lldi.pdev, 2) +
203 (wq->sq.qid << rdev->qpshift);
204 wq->sq.udb &= PAGE_MASK;
205 wq->rq.udb = (u64)pci_resource_start(rdev->lldi.pdev, 2) +
206 (wq->rq.qid << rdev->qpshift);
207 wq->rq.udb &= PAGE_MASK;
208 }
209 wq->rdev = rdev;
210 wq->rq.msn = 1;
211
212 /* build fw_ri_res_wr */
213 wr_len = sizeof *res_wr + 2 * sizeof *res;
214
d3c814e8 215 skb = alloc_skb(wr_len, GFP_KERNEL);
cfdda9d7
SW
216 if (!skb) {
217 ret = -ENOMEM;
c079c287 218 goto free_dma;
cfdda9d7
SW
219 }
220 set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
221
222 res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
223 memset(res_wr, 0, wr_len);
224 res_wr->op_nres = cpu_to_be32(
225 FW_WR_OP(FW_RI_RES_WR) |
226 V_FW_RI_RES_WR_NRES(2) |
227 FW_WR_COMPL(1));
228 res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
c8e081a1 229 res_wr->cookie = (unsigned long) &wr_wait;
cfdda9d7
SW
230 res = res_wr->res;
231 res->u.sqrq.restype = FW_RI_RES_TYPE_SQ;
232 res->u.sqrq.op = FW_RI_RES_OP_WRITE;
233
234 /*
235 * eqsize is the number of 64B entries plus the status page size.
236 */
237 eqsize = wq->sq.size * T4_SQ_NUM_SLOTS + T4_EQ_STATUS_ENTRIES;
238
239 res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
240 V_FW_RI_RES_WR_HOSTFCMODE(0) | /* no host cidx updates */
241 V_FW_RI_RES_WR_CPRIO(0) | /* don't keep in chip cache */
242 V_FW_RI_RES_WR_PCIECHN(0) | /* set by uP at ri_init time */
85d215b0 243 (t4_sq_onchip(&wq->sq) ? F_FW_RI_RES_WR_ONCHIP : 0) |
cfdda9d7
SW
244 V_FW_RI_RES_WR_IQID(scq->cqid));
245 res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
246 V_FW_RI_RES_WR_DCAEN(0) |
247 V_FW_RI_RES_WR_DCACPU(0) |
d37ac31d 248 V_FW_RI_RES_WR_FBMIN(2) |
6a09a9d6 249 V_FW_RI_RES_WR_FBMAX(2) |
cfdda9d7
SW
250 V_FW_RI_RES_WR_CIDXFTHRESHO(0) |
251 V_FW_RI_RES_WR_CIDXFTHRESH(0) |
252 V_FW_RI_RES_WR_EQSIZE(eqsize));
253 res->u.sqrq.eqid = cpu_to_be32(wq->sq.qid);
254 res->u.sqrq.eqaddr = cpu_to_be64(wq->sq.dma_addr);
255 res++;
256 res->u.sqrq.restype = FW_RI_RES_TYPE_RQ;
257 res->u.sqrq.op = FW_RI_RES_OP_WRITE;
258
259 /*
260 * eqsize is the number of 64B entries plus the status page size.
261 */
262 eqsize = wq->rq.size * T4_RQ_NUM_SLOTS + T4_EQ_STATUS_ENTRIES;
263 res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
264 V_FW_RI_RES_WR_HOSTFCMODE(0) | /* no host cidx updates */
265 V_FW_RI_RES_WR_CPRIO(0) | /* don't keep in chip cache */
266 V_FW_RI_RES_WR_PCIECHN(0) | /* set by uP at ri_init time */
267 V_FW_RI_RES_WR_IQID(rcq->cqid));
268 res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
269 V_FW_RI_RES_WR_DCAEN(0) |
270 V_FW_RI_RES_WR_DCACPU(0) |
d37ac31d 271 V_FW_RI_RES_WR_FBMIN(2) |
6a09a9d6 272 V_FW_RI_RES_WR_FBMAX(2) |
cfdda9d7
SW
273 V_FW_RI_RES_WR_CIDXFTHRESHO(0) |
274 V_FW_RI_RES_WR_CIDXFTHRESH(0) |
275 V_FW_RI_RES_WR_EQSIZE(eqsize));
276 res->u.sqrq.eqid = cpu_to_be32(wq->rq.qid);
277 res->u.sqrq.eqaddr = cpu_to_be64(wq->rq.dma_addr);
278
279 c4iw_init_wr_wait(&wr_wait);
280
281 ret = c4iw_ofld_send(rdev, skb);
282 if (ret)
c079c287 283 goto free_dma;
aadc4df3 284 ret = c4iw_wait_for_reply(rdev, &wr_wait, 0, wq->sq.qid, __func__);
cfdda9d7 285 if (ret)
c079c287 286 goto free_dma;
cfdda9d7
SW
287
288 PDBG("%s sqid 0x%x rqid 0x%x kdb 0x%p squdb 0x%llx rqudb 0x%llx\n",
289 __func__, wq->sq.qid, wq->rq.qid, wq->db,
290 (unsigned long long)wq->sq.udb, (unsigned long long)wq->rq.udb);
291
292 return 0;
c079c287 293free_dma:
cfdda9d7
SW
294 dma_free_coherent(&(rdev->lldi.pdev->dev),
295 wq->rq.memsize, wq->rq.queue,
f38926aa 296 dma_unmap_addr(&wq->rq, mapping));
c079c287 297free_sq:
c6d7b267 298 dealloc_sq(rdev, &wq->sq);
c079c287 299free_hwaddr:
cfdda9d7 300 c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
c079c287 301free_sw_rq:
cfdda9d7 302 kfree(wq->rq.sw_rq);
c079c287 303free_sw_sq:
cfdda9d7 304 kfree(wq->sq.sw_sq);
c079c287 305free_rq_qid:
cfdda9d7 306 c4iw_put_qpid(rdev, wq->rq.qid, uctx);
c079c287 307free_sq_qid:
cfdda9d7 308 c4iw_put_qpid(rdev, wq->sq.qid, uctx);
c079c287 309 return ret;
cfdda9d7
SW
310}
311
d37ac31d
SW
312static int build_immd(struct t4_sq *sq, struct fw_ri_immd *immdp,
313 struct ib_send_wr *wr, int max, u32 *plenp)
cfdda9d7 314{
d37ac31d
SW
315 u8 *dstp, *srcp;
316 u32 plen = 0;
cfdda9d7 317 int i;
d37ac31d
SW
318 int rem, len;
319
320 dstp = (u8 *)immdp->data;
321 for (i = 0; i < wr->num_sge; i++) {
322 if ((plen + wr->sg_list[i].length) > max)
323 return -EMSGSIZE;
324 srcp = (u8 *)(unsigned long)wr->sg_list[i].addr;
325 plen += wr->sg_list[i].length;
326 rem = wr->sg_list[i].length;
327 while (rem) {
328 if (dstp == (u8 *)&sq->queue[sq->size])
329 dstp = (u8 *)sq->queue;
330 if (rem <= (u8 *)&sq->queue[sq->size] - dstp)
331 len = rem;
332 else
333 len = (u8 *)&sq->queue[sq->size] - dstp;
334 memcpy(dstp, srcp, len);
335 dstp += len;
336 srcp += len;
337 rem -= len;
338 }
339 }
13fecb83
SW
340 len = roundup(plen + sizeof *immdp, 16) - (plen + sizeof *immdp);
341 if (len)
342 memset(dstp, 0, len);
d37ac31d
SW
343 immdp->op = FW_RI_DATA_IMMD;
344 immdp->r1 = 0;
345 immdp->r2 = 0;
346 immdp->immdlen = cpu_to_be32(plen);
347 *plenp = plen;
348 return 0;
349}
350
351static int build_isgl(__be64 *queue_start, __be64 *queue_end,
352 struct fw_ri_isgl *isglp, struct ib_sge *sg_list,
353 int num_sge, u32 *plenp)
354
355{
356 int i;
357 u32 plen = 0;
358 __be64 *flitp = (__be64 *)isglp->sge;
359
360 for (i = 0; i < num_sge; i++) {
361 if ((plen + sg_list[i].length) < plen)
362 return -EMSGSIZE;
363 plen += sg_list[i].length;
364 *flitp = cpu_to_be64(((u64)sg_list[i].lkey << 32) |
365 sg_list[i].length);
366 if (++flitp == queue_end)
367 flitp = queue_start;
368 *flitp = cpu_to_be64(sg_list[i].addr);
369 if (++flitp == queue_end)
370 flitp = queue_start;
371 }
13fecb83 372 *flitp = (__force __be64)0;
d37ac31d
SW
373 isglp->op = FW_RI_DATA_ISGL;
374 isglp->r1 = 0;
375 isglp->nsge = cpu_to_be16(num_sge);
376 isglp->r2 = 0;
377 if (plenp)
378 *plenp = plen;
379 return 0;
380}
381
382static int build_rdma_send(struct t4_sq *sq, union t4_wr *wqe,
383 struct ib_send_wr *wr, u8 *len16)
384{
cfdda9d7
SW
385 u32 plen;
386 int size;
d37ac31d 387 int ret;
cfdda9d7
SW
388
389 if (wr->num_sge > T4_MAX_SEND_SGE)
390 return -EINVAL;
391 switch (wr->opcode) {
392 case IB_WR_SEND:
393 if (wr->send_flags & IB_SEND_SOLICITED)
394 wqe->send.sendop_pkd = cpu_to_be32(
395 V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_SE));
396 else
397 wqe->send.sendop_pkd = cpu_to_be32(
398 V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND));
399 wqe->send.stag_inv = 0;
400 break;
401 case IB_WR_SEND_WITH_INV:
402 if (wr->send_flags & IB_SEND_SOLICITED)
403 wqe->send.sendop_pkd = cpu_to_be32(
404 V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_SE_INV));
405 else
406 wqe->send.sendop_pkd = cpu_to_be32(
407 V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_INV));
408 wqe->send.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
409 break;
410
411 default:
412 return -EINVAL;
413 }
d37ac31d 414
cfdda9d7
SW
415 plen = 0;
416 if (wr->num_sge) {
417 if (wr->send_flags & IB_SEND_INLINE) {
d37ac31d
SW
418 ret = build_immd(sq, wqe->send.u.immd_src, wr,
419 T4_MAX_SEND_INLINE, &plen);
420 if (ret)
421 return ret;
cfdda9d7
SW
422 size = sizeof wqe->send + sizeof(struct fw_ri_immd) +
423 plen;
424 } else {
d37ac31d
SW
425 ret = build_isgl((__be64 *)sq->queue,
426 (__be64 *)&sq->queue[sq->size],
427 wqe->send.u.isgl_src,
428 wr->sg_list, wr->num_sge, &plen);
429 if (ret)
430 return ret;
cfdda9d7
SW
431 size = sizeof wqe->send + sizeof(struct fw_ri_isgl) +
432 wr->num_sge * sizeof(struct fw_ri_sge);
433 }
434 } else {
435 wqe->send.u.immd_src[0].op = FW_RI_DATA_IMMD;
436 wqe->send.u.immd_src[0].r1 = 0;
437 wqe->send.u.immd_src[0].r2 = 0;
438 wqe->send.u.immd_src[0].immdlen = 0;
439 size = sizeof wqe->send + sizeof(struct fw_ri_immd);
d37ac31d 440 plen = 0;
cfdda9d7
SW
441 }
442 *len16 = DIV_ROUND_UP(size, 16);
443 wqe->send.plen = cpu_to_be32(plen);
444 return 0;
445}
446
d37ac31d
SW
447static int build_rdma_write(struct t4_sq *sq, union t4_wr *wqe,
448 struct ib_send_wr *wr, u8 *len16)
cfdda9d7 449{
cfdda9d7
SW
450 u32 plen;
451 int size;
d37ac31d 452 int ret;
cfdda9d7 453
d37ac31d 454 if (wr->num_sge > T4_MAX_SEND_SGE)
cfdda9d7
SW
455 return -EINVAL;
456 wqe->write.r2 = 0;
457 wqe->write.stag_sink = cpu_to_be32(wr->wr.rdma.rkey);
458 wqe->write.to_sink = cpu_to_be64(wr->wr.rdma.remote_addr);
cfdda9d7
SW
459 if (wr->num_sge) {
460 if (wr->send_flags & IB_SEND_INLINE) {
d37ac31d
SW
461 ret = build_immd(sq, wqe->write.u.immd_src, wr,
462 T4_MAX_WRITE_INLINE, &plen);
463 if (ret)
464 return ret;
cfdda9d7
SW
465 size = sizeof wqe->write + sizeof(struct fw_ri_immd) +
466 plen;
467 } else {
d37ac31d
SW
468 ret = build_isgl((__be64 *)sq->queue,
469 (__be64 *)&sq->queue[sq->size],
470 wqe->write.u.isgl_src,
471 wr->sg_list, wr->num_sge, &plen);
472 if (ret)
473 return ret;
cfdda9d7
SW
474 size = sizeof wqe->write + sizeof(struct fw_ri_isgl) +
475 wr->num_sge * sizeof(struct fw_ri_sge);
476 }
477 } else {
478 wqe->write.u.immd_src[0].op = FW_RI_DATA_IMMD;
479 wqe->write.u.immd_src[0].r1 = 0;
480 wqe->write.u.immd_src[0].r2 = 0;
481 wqe->write.u.immd_src[0].immdlen = 0;
482 size = sizeof wqe->write + sizeof(struct fw_ri_immd);
d37ac31d 483 plen = 0;
cfdda9d7
SW
484 }
485 *len16 = DIV_ROUND_UP(size, 16);
486 wqe->write.plen = cpu_to_be32(plen);
487 return 0;
488}
489
490static int build_rdma_read(union t4_wr *wqe, struct ib_send_wr *wr, u8 *len16)
491{
492 if (wr->num_sge > 1)
493 return -EINVAL;
494 if (wr->num_sge) {
495 wqe->read.stag_src = cpu_to_be32(wr->wr.rdma.rkey);
496 wqe->read.to_src_hi = cpu_to_be32((u32)(wr->wr.rdma.remote_addr
497 >> 32));
498 wqe->read.to_src_lo = cpu_to_be32((u32)wr->wr.rdma.remote_addr);
499 wqe->read.stag_sink = cpu_to_be32(wr->sg_list[0].lkey);
500 wqe->read.plen = cpu_to_be32(wr->sg_list[0].length);
501 wqe->read.to_sink_hi = cpu_to_be32((u32)(wr->sg_list[0].addr
502 >> 32));
503 wqe->read.to_sink_lo = cpu_to_be32((u32)(wr->sg_list[0].addr));
504 } else {
505 wqe->read.stag_src = cpu_to_be32(2);
506 wqe->read.to_src_hi = 0;
507 wqe->read.to_src_lo = 0;
508 wqe->read.stag_sink = cpu_to_be32(2);
509 wqe->read.plen = 0;
510 wqe->read.to_sink_hi = 0;
511 wqe->read.to_sink_lo = 0;
512 }
513 wqe->read.r2 = 0;
514 wqe->read.r5 = 0;
515 *len16 = DIV_ROUND_UP(sizeof wqe->read, 16);
516 return 0;
517}
518
519static int build_rdma_recv(struct c4iw_qp *qhp, union t4_recv_wr *wqe,
520 struct ib_recv_wr *wr, u8 *len16)
521{
d37ac31d 522 int ret;
cfdda9d7 523
d37ac31d
SW
524 ret = build_isgl((__be64 *)qhp->wq.rq.queue,
525 (__be64 *)&qhp->wq.rq.queue[qhp->wq.rq.size],
526 &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
527 if (ret)
528 return ret;
cfdda9d7
SW
529 *len16 = DIV_ROUND_UP(sizeof wqe->recv +
530 wr->num_sge * sizeof(struct fw_ri_sge), 16);
531 return 0;
532}
533
40dbf6ee
SW
534static int build_fastreg(struct t4_sq *sq, union t4_wr *wqe,
535 struct ib_send_wr *wr, u8 *len16)
cfdda9d7
SW
536{
537
538 struct fw_ri_immd *imdp;
539 __be64 *p;
540 int i;
541 int pbllen = roundup(wr->wr.fast_reg.page_list_len * sizeof(u64), 32);
40dbf6ee 542 int rem;
cfdda9d7
SW
543
544 if (wr->wr.fast_reg.page_list_len > T4_MAX_FR_DEPTH)
545 return -EINVAL;
546
547 wqe->fr.qpbinde_to_dcacpu = 0;
548 wqe->fr.pgsz_shift = wr->wr.fast_reg.page_shift - 12;
549 wqe->fr.addr_type = FW_RI_VA_BASED_TO;
550 wqe->fr.mem_perms = c4iw_ib_to_tpt_access(wr->wr.fast_reg.access_flags);
551 wqe->fr.len_hi = 0;
552 wqe->fr.len_lo = cpu_to_be32(wr->wr.fast_reg.length);
553 wqe->fr.stag = cpu_to_be32(wr->wr.fast_reg.rkey);
554 wqe->fr.va_hi = cpu_to_be32(wr->wr.fast_reg.iova_start >> 32);
555 wqe->fr.va_lo_fbo = cpu_to_be32(wr->wr.fast_reg.iova_start &
556 0xffffffff);
40dbf6ee
SW
557 WARN_ON(pbllen > T4_MAX_FR_IMMD);
558 imdp = (struct fw_ri_immd *)(&wqe->fr + 1);
559 imdp->op = FW_RI_DATA_IMMD;
560 imdp->r1 = 0;
561 imdp->r2 = 0;
562 imdp->immdlen = cpu_to_be32(pbllen);
563 p = (__be64 *)(imdp + 1);
564 rem = pbllen;
565 for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
566 *p = cpu_to_be64((u64)wr->wr.fast_reg.page_list->page_list[i]);
567 rem -= sizeof *p;
568 if (++p == (__be64 *)&sq->queue[sq->size])
569 p = (__be64 *)sq->queue;
570 }
571 BUG_ON(rem < 0);
572 while (rem) {
573 *p = 0;
574 rem -= sizeof *p;
575 if (++p == (__be64 *)&sq->queue[sq->size])
576 p = (__be64 *)sq->queue;
cfdda9d7 577 }
40dbf6ee 578 *len16 = DIV_ROUND_UP(sizeof wqe->fr + sizeof *imdp + pbllen, 16);
cfdda9d7
SW
579 return 0;
580}
581
582static int build_inv_stag(union t4_wr *wqe, struct ib_send_wr *wr,
583 u8 *len16)
584{
585 wqe->inv.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
586 wqe->inv.r2 = 0;
587 *len16 = DIV_ROUND_UP(sizeof wqe->inv, 16);
588 return 0;
589}
590
591void c4iw_qp_add_ref(struct ib_qp *qp)
592{
593 PDBG("%s ib_qp %p\n", __func__, qp);
594 atomic_inc(&(to_c4iw_qp(qp)->refcnt));
595}
596
597void c4iw_qp_rem_ref(struct ib_qp *qp)
598{
599 PDBG("%s ib_qp %p\n", __func__, qp);
600 if (atomic_dec_and_test(&(to_c4iw_qp(qp)->refcnt)))
601 wake_up(&(to_c4iw_qp(qp)->wait));
602}
603
604int c4iw_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
605 struct ib_send_wr **bad_wr)
606{
607 int err = 0;
608 u8 len16 = 0;
609 enum fw_wr_opcodes fw_opcode = 0;
610 enum fw_ri_wr_flags fw_flags;
611 struct c4iw_qp *qhp;
612 union t4_wr *wqe;
613 u32 num_wrs;
614 struct t4_swsqe *swsqe;
615 unsigned long flag;
616 u16 idx = 0;
617
618 qhp = to_c4iw_qp(ibqp);
619 spin_lock_irqsave(&qhp->lock, flag);
620 if (t4_wq_in_error(&qhp->wq)) {
621 spin_unlock_irqrestore(&qhp->lock, flag);
622 return -EINVAL;
623 }
624 num_wrs = t4_sq_avail(&qhp->wq);
625 if (num_wrs == 0) {
626 spin_unlock_irqrestore(&qhp->lock, flag);
627 return -ENOMEM;
628 }
629 while (wr) {
630 if (num_wrs == 0) {
631 err = -ENOMEM;
632 *bad_wr = wr;
633 break;
634 }
d37ac31d
SW
635 wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
636 qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
637
cfdda9d7
SW
638 fw_flags = 0;
639 if (wr->send_flags & IB_SEND_SOLICITED)
640 fw_flags |= FW_RI_SOLICITED_EVENT_FLAG;
641 if (wr->send_flags & IB_SEND_SIGNALED)
642 fw_flags |= FW_RI_COMPLETION_FLAG;
643 swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
644 switch (wr->opcode) {
645 case IB_WR_SEND_WITH_INV:
646 case IB_WR_SEND:
647 if (wr->send_flags & IB_SEND_FENCE)
648 fw_flags |= FW_RI_READ_FENCE_FLAG;
649 fw_opcode = FW_RI_SEND_WR;
650 if (wr->opcode == IB_WR_SEND)
651 swsqe->opcode = FW_RI_SEND;
652 else
653 swsqe->opcode = FW_RI_SEND_WITH_INV;
d37ac31d 654 err = build_rdma_send(&qhp->wq.sq, wqe, wr, &len16);
cfdda9d7
SW
655 break;
656 case IB_WR_RDMA_WRITE:
657 fw_opcode = FW_RI_RDMA_WRITE_WR;
658 swsqe->opcode = FW_RI_RDMA_WRITE;
d37ac31d 659 err = build_rdma_write(&qhp->wq.sq, wqe, wr, &len16);
cfdda9d7
SW
660 break;
661 case IB_WR_RDMA_READ:
2f1fb507 662 case IB_WR_RDMA_READ_WITH_INV:
cfdda9d7
SW
663 fw_opcode = FW_RI_RDMA_READ_WR;
664 swsqe->opcode = FW_RI_READ_REQ;
2f1fb507 665 if (wr->opcode == IB_WR_RDMA_READ_WITH_INV)
410ade4c 666 fw_flags = FW_RI_RDMA_READ_INVALIDATE;
2f1fb507
SW
667 else
668 fw_flags = 0;
cfdda9d7
SW
669 err = build_rdma_read(wqe, wr, &len16);
670 if (err)
671 break;
672 swsqe->read_len = wr->sg_list[0].length;
673 if (!qhp->wq.sq.oldest_read)
674 qhp->wq.sq.oldest_read = swsqe;
675 break;
676 case IB_WR_FAST_REG_MR:
677 fw_opcode = FW_RI_FR_NSMR_WR;
678 swsqe->opcode = FW_RI_FAST_REGISTER;
40dbf6ee 679 err = build_fastreg(&qhp->wq.sq, wqe, wr, &len16);
cfdda9d7
SW
680 break;
681 case IB_WR_LOCAL_INV:
4ab1eb9c
SW
682 if (wr->send_flags & IB_SEND_FENCE)
683 fw_flags |= FW_RI_LOCAL_FENCE_FLAG;
cfdda9d7
SW
684 fw_opcode = FW_RI_INV_LSTAG_WR;
685 swsqe->opcode = FW_RI_LOCAL_INV;
686 err = build_inv_stag(wqe, wr, &len16);
687 break;
688 default:
689 PDBG("%s post of type=%d TBD!\n", __func__,
690 wr->opcode);
691 err = -EINVAL;
692 }
693 if (err) {
694 *bad_wr = wr;
695 break;
696 }
697 swsqe->idx = qhp->wq.sq.pidx;
698 swsqe->complete = 0;
699 swsqe->signaled = (wr->send_flags & IB_SEND_SIGNALED);
700 swsqe->wr_id = wr->wr_id;
701
702 init_wr_hdr(wqe, qhp->wq.sq.pidx, fw_opcode, fw_flags, len16);
703
704 PDBG("%s cookie 0x%llx pidx 0x%x opcode 0x%x read_len %u\n",
705 __func__, (unsigned long long)wr->wr_id, qhp->wq.sq.pidx,
706 swsqe->opcode, swsqe->read_len);
707 wr = wr->next;
708 num_wrs--;
d37ac31d
SW
709 t4_sq_produce(&qhp->wq, len16);
710 idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
cfdda9d7
SW
711 }
712 if (t4_wq_db_enabled(&qhp->wq))
713 t4_ring_sq_db(&qhp->wq, idx);
714 spin_unlock_irqrestore(&qhp->lock, flag);
715 return err;
716}
717
718int c4iw_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
719 struct ib_recv_wr **bad_wr)
720{
721 int err = 0;
722 struct c4iw_qp *qhp;
723 union t4_recv_wr *wqe;
724 u32 num_wrs;
725 u8 len16 = 0;
726 unsigned long flag;
727 u16 idx = 0;
728
729 qhp = to_c4iw_qp(ibqp);
730 spin_lock_irqsave(&qhp->lock, flag);
731 if (t4_wq_in_error(&qhp->wq)) {
732 spin_unlock_irqrestore(&qhp->lock, flag);
733 return -EINVAL;
734 }
735 num_wrs = t4_rq_avail(&qhp->wq);
736 if (num_wrs == 0) {
737 spin_unlock_irqrestore(&qhp->lock, flag);
738 return -ENOMEM;
739 }
740 while (wr) {
741 if (wr->num_sge > T4_MAX_RECV_SGE) {
742 err = -EINVAL;
743 *bad_wr = wr;
744 break;
745 }
d37ac31d
SW
746 wqe = (union t4_recv_wr *)((u8 *)qhp->wq.rq.queue +
747 qhp->wq.rq.wq_pidx *
748 T4_EQ_ENTRY_SIZE);
cfdda9d7
SW
749 if (num_wrs)
750 err = build_rdma_recv(qhp, wqe, wr, &len16);
751 else
752 err = -ENOMEM;
753 if (err) {
754 *bad_wr = wr;
755 break;
756 }
757
758 qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].wr_id = wr->wr_id;
759
760 wqe->recv.opcode = FW_RI_RECV_WR;
761 wqe->recv.r1 = 0;
762 wqe->recv.wrid = qhp->wq.rq.pidx;
763 wqe->recv.r2[0] = 0;
764 wqe->recv.r2[1] = 0;
765 wqe->recv.r2[2] = 0;
766 wqe->recv.len16 = len16;
cfdda9d7
SW
767 PDBG("%s cookie 0x%llx pidx %u\n", __func__,
768 (unsigned long long) wr->wr_id, qhp->wq.rq.pidx);
d37ac31d
SW
769 t4_rq_produce(&qhp->wq, len16);
770 idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
cfdda9d7
SW
771 wr = wr->next;
772 num_wrs--;
cfdda9d7
SW
773 }
774 if (t4_wq_db_enabled(&qhp->wq))
775 t4_ring_rq_db(&qhp->wq, idx);
776 spin_unlock_irqrestore(&qhp->lock, flag);
777 return err;
778}
779
780int c4iw_bind_mw(struct ib_qp *qp, struct ib_mw *mw, struct ib_mw_bind *mw_bind)
781{
782 return -ENOSYS;
783}
784
785static inline void build_term_codes(struct t4_cqe *err_cqe, u8 *layer_type,
786 u8 *ecode)
787{
788 int status;
789 int tagged;
790 int opcode;
791 int rqtype;
792 int send_inv;
793
794 if (!err_cqe) {
795 *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
796 *ecode = 0;
797 return;
798 }
799
800 status = CQE_STATUS(err_cqe);
801 opcode = CQE_OPCODE(err_cqe);
802 rqtype = RQ_TYPE(err_cqe);
803 send_inv = (opcode == FW_RI_SEND_WITH_INV) ||
804 (opcode == FW_RI_SEND_WITH_SE_INV);
805 tagged = (opcode == FW_RI_RDMA_WRITE) ||
806 (rqtype && (opcode == FW_RI_READ_RESP));
807
808 switch (status) {
809 case T4_ERR_STAG:
810 if (send_inv) {
811 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
812 *ecode = RDMAP_CANT_INV_STAG;
813 } else {
814 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
815 *ecode = RDMAP_INV_STAG;
816 }
817 break;
818 case T4_ERR_PDID:
819 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
820 if ((opcode == FW_RI_SEND_WITH_INV) ||
821 (opcode == FW_RI_SEND_WITH_SE_INV))
822 *ecode = RDMAP_CANT_INV_STAG;
823 else
824 *ecode = RDMAP_STAG_NOT_ASSOC;
825 break;
826 case T4_ERR_QPID:
827 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
828 *ecode = RDMAP_STAG_NOT_ASSOC;
829 break;
830 case T4_ERR_ACCESS:
831 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
832 *ecode = RDMAP_ACC_VIOL;
833 break;
834 case T4_ERR_WRAP:
835 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
836 *ecode = RDMAP_TO_WRAP;
837 break;
838 case T4_ERR_BOUND:
839 if (tagged) {
840 *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
841 *ecode = DDPT_BASE_BOUNDS;
842 } else {
843 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
844 *ecode = RDMAP_BASE_BOUNDS;
845 }
846 break;
847 case T4_ERR_INVALIDATE_SHARED_MR:
848 case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
849 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
850 *ecode = RDMAP_CANT_INV_STAG;
851 break;
852 case T4_ERR_ECC:
853 case T4_ERR_ECC_PSTAG:
854 case T4_ERR_INTERNAL_ERR:
855 *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
856 *ecode = 0;
857 break;
858 case T4_ERR_OUT_OF_RQE:
859 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
860 *ecode = DDPU_INV_MSN_NOBUF;
861 break;
862 case T4_ERR_PBL_ADDR_BOUND:
863 *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
864 *ecode = DDPT_BASE_BOUNDS;
865 break;
866 case T4_ERR_CRC:
867 *layer_type = LAYER_MPA|DDP_LLP;
868 *ecode = MPA_CRC_ERR;
869 break;
870 case T4_ERR_MARKER:
871 *layer_type = LAYER_MPA|DDP_LLP;
872 *ecode = MPA_MARKER_ERR;
873 break;
874 case T4_ERR_PDU_LEN_ERR:
875 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
876 *ecode = DDPU_MSG_TOOBIG;
877 break;
878 case T4_ERR_DDP_VERSION:
879 if (tagged) {
880 *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
881 *ecode = DDPT_INV_VERS;
882 } else {
883 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
884 *ecode = DDPU_INV_VERS;
885 }
886 break;
887 case T4_ERR_RDMA_VERSION:
888 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
889 *ecode = RDMAP_INV_VERS;
890 break;
891 case T4_ERR_OPCODE:
892 *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
893 *ecode = RDMAP_INV_OPCODE;
894 break;
895 case T4_ERR_DDP_QUEUE_NUM:
896 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
897 *ecode = DDPU_INV_QN;
898 break;
899 case T4_ERR_MSN:
900 case T4_ERR_MSN_GAP:
901 case T4_ERR_MSN_RANGE:
902 case T4_ERR_IRD_OVERFLOW:
903 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
904 *ecode = DDPU_INV_MSN_RANGE;
905 break;
906 case T4_ERR_TBIT:
907 *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
908 *ecode = 0;
909 break;
910 case T4_ERR_MO:
911 *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
912 *ecode = DDPU_INV_MO;
913 break;
914 default:
915 *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
916 *ecode = 0;
917 break;
918 }
919}
920
be4c9bad
RD
921static void post_terminate(struct c4iw_qp *qhp, struct t4_cqe *err_cqe,
922 gfp_t gfp)
cfdda9d7
SW
923{
924 struct fw_ri_wr *wqe;
925 struct sk_buff *skb;
926 struct terminate_message *term;
927
928 PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
929 qhp->ep->hwtid);
930
be4c9bad 931 skb = alloc_skb(sizeof *wqe, gfp);
cfdda9d7 932 if (!skb)
be4c9bad 933 return;
cfdda9d7
SW
934 set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
935
936 wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
937 memset(wqe, 0, sizeof *wqe);
938 wqe->op_compl = cpu_to_be32(FW_WR_OP(FW_RI_INIT_WR));
939 wqe->flowid_len16 = cpu_to_be32(
940 FW_WR_FLOWID(qhp->ep->hwtid) |
941 FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
942
943 wqe->u.terminate.type = FW_RI_TYPE_TERMINATE;
944 wqe->u.terminate.immdlen = cpu_to_be32(sizeof *term);
945 term = (struct terminate_message *)wqe->u.terminate.termmsg;
d2fe99e8
KS
946 if (qhp->attr.layer_etype == (LAYER_MPA|DDP_LLP)) {
947 term->layer_etype = qhp->attr.layer_etype;
948 term->ecode = qhp->attr.ecode;
949 } else
950 build_term_codes(err_cqe, &term->layer_etype, &term->ecode);
be4c9bad 951 c4iw_ofld_send(&qhp->rhp->rdev, skb);
cfdda9d7
SW
952}
953
954/*
955 * Assumes qhp lock is held.
956 */
957static void __flush_qp(struct c4iw_qp *qhp, struct c4iw_cq *rchp,
2f5b48c3 958 struct c4iw_cq *schp)
cfdda9d7
SW
959{
960 int count;
961 int flushed;
2f5b48c3 962 unsigned long flag;
cfdda9d7
SW
963
964 PDBG("%s qhp %p rchp %p schp %p\n", __func__, qhp, rchp, schp);
cfdda9d7 965
732bee7a 966 /* locking hierarchy: cq lock first, then qp lock. */
2f5b48c3 967 spin_lock_irqsave(&rchp->lock, flag);
cfdda9d7
SW
968 spin_lock(&qhp->lock);
969 c4iw_flush_hw_cq(&rchp->cq);
970 c4iw_count_rcqes(&rchp->cq, &qhp->wq, &count);
971 flushed = c4iw_flush_rq(&qhp->wq, &rchp->cq, count);
972 spin_unlock(&qhp->lock);
2f5b48c3 973 spin_unlock_irqrestore(&rchp->lock, flag);
581bbe2c
KS
974 if (flushed) {
975 spin_lock_irqsave(&rchp->comp_handler_lock, flag);
cfdda9d7 976 (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
581bbe2c
KS
977 spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
978 }
cfdda9d7 979
732bee7a 980 /* locking hierarchy: cq lock first, then qp lock. */
2f5b48c3 981 spin_lock_irqsave(&schp->lock, flag);
cfdda9d7
SW
982 spin_lock(&qhp->lock);
983 c4iw_flush_hw_cq(&schp->cq);
984 c4iw_count_scqes(&schp->cq, &qhp->wq, &count);
985 flushed = c4iw_flush_sq(&qhp->wq, &schp->cq, count);
986 spin_unlock(&qhp->lock);
2f5b48c3 987 spin_unlock_irqrestore(&schp->lock, flag);
581bbe2c
KS
988 if (flushed) {
989 spin_lock_irqsave(&schp->comp_handler_lock, flag);
cfdda9d7 990 (*schp->ibcq.comp_handler)(&schp->ibcq, schp->ibcq.cq_context);
581bbe2c
KS
991 spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
992 }
cfdda9d7
SW
993}
994
2f5b48c3 995static void flush_qp(struct c4iw_qp *qhp)
cfdda9d7
SW
996{
997 struct c4iw_cq *rchp, *schp;
581bbe2c 998 unsigned long flag;
cfdda9d7
SW
999
1000 rchp = get_chp(qhp->rhp, qhp->attr.rcq);
1001 schp = get_chp(qhp->rhp, qhp->attr.scq);
1002
1003 if (qhp->ibqp.uobject) {
1004 t4_set_wq_in_error(&qhp->wq);
1005 t4_set_cq_in_error(&rchp->cq);
581bbe2c 1006 spin_lock_irqsave(&rchp->comp_handler_lock, flag);
01e7da6b 1007 (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
581bbe2c 1008 spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
01e7da6b 1009 if (schp != rchp) {
cfdda9d7 1010 t4_set_cq_in_error(&schp->cq);
581bbe2c 1011 spin_lock_irqsave(&schp->comp_handler_lock, flag);
01e7da6b
KS
1012 (*schp->ibcq.comp_handler)(&schp->ibcq,
1013 schp->ibcq.cq_context);
581bbe2c 1014 spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
01e7da6b 1015 }
cfdda9d7
SW
1016 return;
1017 }
2f5b48c3 1018 __flush_qp(qhp, rchp, schp);
cfdda9d7
SW
1019}
1020
73d6fcad
SW
1021static int rdma_fini(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
1022 struct c4iw_ep *ep)
cfdda9d7
SW
1023{
1024 struct fw_ri_wr *wqe;
1025 int ret;
cfdda9d7
SW
1026 struct sk_buff *skb;
1027
1028 PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
73d6fcad 1029 ep->hwtid);
cfdda9d7 1030
d3c814e8 1031 skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
cfdda9d7
SW
1032 if (!skb)
1033 return -ENOMEM;
73d6fcad 1034 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
cfdda9d7
SW
1035
1036 wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
1037 memset(wqe, 0, sizeof *wqe);
1038 wqe->op_compl = cpu_to_be32(
1039 FW_WR_OP(FW_RI_INIT_WR) |
1040 FW_WR_COMPL(1));
1041 wqe->flowid_len16 = cpu_to_be32(
73d6fcad 1042 FW_WR_FLOWID(ep->hwtid) |
cfdda9d7 1043 FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
2f5b48c3 1044 wqe->cookie = (unsigned long) &ep->com.wr_wait;
cfdda9d7
SW
1045
1046 wqe->u.fini.type = FW_RI_TYPE_FINI;
cfdda9d7
SW
1047 ret = c4iw_ofld_send(&rhp->rdev, skb);
1048 if (ret)
1049 goto out;
1050
2f5b48c3 1051 ret = c4iw_wait_for_reply(&rhp->rdev, &ep->com.wr_wait, qhp->ep->hwtid,
aadc4df3 1052 qhp->wq.sq.qid, __func__);
cfdda9d7
SW
1053out:
1054 PDBG("%s ret %d\n", __func__, ret);
1055 return ret;
1056}
1057
1058static void build_rtr_msg(u8 p2p_type, struct fw_ri_init *init)
1059{
d2fe99e8 1060 PDBG("%s p2p_type = %d\n", __func__, p2p_type);
cfdda9d7
SW
1061 memset(&init->u, 0, sizeof init->u);
1062 switch (p2p_type) {
1063 case FW_RI_INIT_P2PTYPE_RDMA_WRITE:
1064 init->u.write.opcode = FW_RI_RDMA_WRITE_WR;
1065 init->u.write.stag_sink = cpu_to_be32(1);
1066 init->u.write.to_sink = cpu_to_be64(1);
1067 init->u.write.u.immd_src[0].op = FW_RI_DATA_IMMD;
1068 init->u.write.len16 = DIV_ROUND_UP(sizeof init->u.write +
1069 sizeof(struct fw_ri_immd),
1070 16);
1071 break;
1072 case FW_RI_INIT_P2PTYPE_READ_REQ:
1073 init->u.write.opcode = FW_RI_RDMA_READ_WR;
1074 init->u.read.stag_src = cpu_to_be32(1);
1075 init->u.read.to_src_lo = cpu_to_be32(1);
1076 init->u.read.stag_sink = cpu_to_be32(1);
1077 init->u.read.to_sink_lo = cpu_to_be32(1);
1078 init->u.read.len16 = DIV_ROUND_UP(sizeof init->u.read, 16);
1079 break;
1080 }
1081}
1082
1083static int rdma_init(struct c4iw_dev *rhp, struct c4iw_qp *qhp)
1084{
1085 struct fw_ri_wr *wqe;
1086 int ret;
cfdda9d7
SW
1087 struct sk_buff *skb;
1088
1089 PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
1090 qhp->ep->hwtid);
1091
d3c814e8 1092 skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
cfdda9d7
SW
1093 if (!skb)
1094 return -ENOMEM;
1095 set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
1096
1097 wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
1098 memset(wqe, 0, sizeof *wqe);
1099 wqe->op_compl = cpu_to_be32(
1100 FW_WR_OP(FW_RI_INIT_WR) |
1101 FW_WR_COMPL(1));
1102 wqe->flowid_len16 = cpu_to_be32(
1103 FW_WR_FLOWID(qhp->ep->hwtid) |
1104 FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
1105
2f5b48c3 1106 wqe->cookie = (unsigned long) &qhp->ep->com.wr_wait;
cfdda9d7
SW
1107
1108 wqe->u.init.type = FW_RI_TYPE_INIT;
1109 wqe->u.init.mpareqbit_p2ptype =
1110 V_FW_RI_WR_MPAREQBIT(qhp->attr.mpa_attr.initiator) |
1111 V_FW_RI_WR_P2PTYPE(qhp->attr.mpa_attr.p2p_type);
1112 wqe->u.init.mpa_attrs = FW_RI_MPA_IETF_ENABLE;
1113 if (qhp->attr.mpa_attr.recv_marker_enabled)
1114 wqe->u.init.mpa_attrs |= FW_RI_MPA_RX_MARKER_ENABLE;
1115 if (qhp->attr.mpa_attr.xmit_marker_enabled)
1116 wqe->u.init.mpa_attrs |= FW_RI_MPA_TX_MARKER_ENABLE;
1117 if (qhp->attr.mpa_attr.crc_enabled)
1118 wqe->u.init.mpa_attrs |= FW_RI_MPA_CRC_ENABLE;
1119
1120 wqe->u.init.qp_caps = FW_RI_QP_RDMA_READ_ENABLE |
1121 FW_RI_QP_RDMA_WRITE_ENABLE |
1122 FW_RI_QP_BIND_ENABLE;
1123 if (!qhp->ibqp.uobject)
1124 wqe->u.init.qp_caps |= FW_RI_QP_FAST_REGISTER_ENABLE |
1125 FW_RI_QP_STAG0_ENABLE;
1126 wqe->u.init.nrqe = cpu_to_be16(t4_rqes_posted(&qhp->wq));
1127 wqe->u.init.pdid = cpu_to_be32(qhp->attr.pd);
1128 wqe->u.init.qpid = cpu_to_be32(qhp->wq.sq.qid);
1129 wqe->u.init.sq_eqid = cpu_to_be32(qhp->wq.sq.qid);
1130 wqe->u.init.rq_eqid = cpu_to_be32(qhp->wq.rq.qid);
1131 wqe->u.init.scqid = cpu_to_be32(qhp->attr.scq);
1132 wqe->u.init.rcqid = cpu_to_be32(qhp->attr.rcq);
1133 wqe->u.init.ord_max = cpu_to_be32(qhp->attr.max_ord);
1134 wqe->u.init.ird_max = cpu_to_be32(qhp->attr.max_ird);
1135 wqe->u.init.iss = cpu_to_be32(qhp->ep->snd_seq);
1136 wqe->u.init.irs = cpu_to_be32(qhp->ep->rcv_seq);
1137 wqe->u.init.hwrqsize = cpu_to_be32(qhp->wq.rq.rqt_size);
1138 wqe->u.init.hwrqaddr = cpu_to_be32(qhp->wq.rq.rqt_hwaddr -
1139 rhp->rdev.lldi.vr->rq.start);
1140 if (qhp->attr.mpa_attr.initiator)
1141 build_rtr_msg(qhp->attr.mpa_attr.p2p_type, &wqe->u.init);
1142
cfdda9d7
SW
1143 ret = c4iw_ofld_send(&rhp->rdev, skb);
1144 if (ret)
1145 goto out;
1146
2f5b48c3
SW
1147 ret = c4iw_wait_for_reply(&rhp->rdev, &qhp->ep->com.wr_wait,
1148 qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
cfdda9d7
SW
1149out:
1150 PDBG("%s ret %d\n", __func__, ret);
1151 return ret;
1152}
1153
2c974781
VP
1154/*
1155 * Called by the library when the qp has user dbs disabled due to
1156 * a DB_FULL condition. This function will single-thread all user
1157 * DB rings to avoid overflowing the hw db-fifo.
1158 */
1159static int ring_kernel_db(struct c4iw_qp *qhp, u32 qid, u16 inc)
1160{
1161 int delay = db_delay_usecs;
1162
1163 mutex_lock(&qhp->rhp->db_mutex);
1164 do {
422eea0a
VP
1165
1166 /*
1167 * The interrupt threshold is dbfifo_int_thresh << 6. So
1168 * make sure we don't cross that and generate an interrupt.
1169 */
1170 if (cxgb4_dbfifo_count(qhp->rhp->rdev.lldi.ports[0], 1) <
1171 (qhp->rhp->rdev.lldi.dbfifo_int_thresh << 5)) {
e5619c12 1172 writel(QID(qid) | PIDX(inc), qhp->wq.db);
2c974781
VP
1173 break;
1174 }
1175 set_current_state(TASK_UNINTERRUPTIBLE);
1176 schedule_timeout(usecs_to_jiffies(delay));
422eea0a 1177 delay = min(delay << 1, 2000);
2c974781
VP
1178 } while (1);
1179 mutex_unlock(&qhp->rhp->db_mutex);
1180 return 0;
1181}
1182
cfdda9d7
SW
1183int c4iw_modify_qp(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
1184 enum c4iw_qp_attr_mask mask,
1185 struct c4iw_qp_attributes *attrs,
1186 int internal)
1187{
1188 int ret = 0;
1189 struct c4iw_qp_attributes newattr = qhp->attr;
cfdda9d7
SW
1190 int disconnect = 0;
1191 int terminate = 0;
1192 int abort = 0;
1193 int free = 0;
1194 struct c4iw_ep *ep = NULL;
1195
1196 PDBG("%s qhp %p sqid 0x%x rqid 0x%x ep %p state %d -> %d\n", __func__,
1197 qhp, qhp->wq.sq.qid, qhp->wq.rq.qid, qhp->ep, qhp->attr.state,
1198 (mask & C4IW_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
1199
2f5b48c3 1200 mutex_lock(&qhp->mutex);
cfdda9d7
SW
1201
1202 /* Process attr changes if in IDLE */
1203 if (mask & C4IW_QP_ATTR_VALID_MODIFY) {
1204 if (qhp->attr.state != C4IW_QP_STATE_IDLE) {
1205 ret = -EIO;
1206 goto out;
1207 }
1208 if (mask & C4IW_QP_ATTR_ENABLE_RDMA_READ)
1209 newattr.enable_rdma_read = attrs->enable_rdma_read;
1210 if (mask & C4IW_QP_ATTR_ENABLE_RDMA_WRITE)
1211 newattr.enable_rdma_write = attrs->enable_rdma_write;
1212 if (mask & C4IW_QP_ATTR_ENABLE_RDMA_BIND)
1213 newattr.enable_bind = attrs->enable_bind;
1214 if (mask & C4IW_QP_ATTR_MAX_ORD) {
be4c9bad 1215 if (attrs->max_ord > c4iw_max_read_depth) {
cfdda9d7
SW
1216 ret = -EINVAL;
1217 goto out;
1218 }
1219 newattr.max_ord = attrs->max_ord;
1220 }
1221 if (mask & C4IW_QP_ATTR_MAX_IRD) {
be4c9bad 1222 if (attrs->max_ird > c4iw_max_read_depth) {
cfdda9d7
SW
1223 ret = -EINVAL;
1224 goto out;
1225 }
1226 newattr.max_ird = attrs->max_ird;
1227 }
1228 qhp->attr = newattr;
1229 }
1230
2c974781
VP
1231 if (mask & C4IW_QP_ATTR_SQ_DB) {
1232 ret = ring_kernel_db(qhp, qhp->wq.sq.qid, attrs->sq_db_inc);
1233 goto out;
1234 }
1235 if (mask & C4IW_QP_ATTR_RQ_DB) {
1236 ret = ring_kernel_db(qhp, qhp->wq.rq.qid, attrs->rq_db_inc);
1237 goto out;
1238 }
1239
cfdda9d7
SW
1240 if (!(mask & C4IW_QP_ATTR_NEXT_STATE))
1241 goto out;
1242 if (qhp->attr.state == attrs->next_state)
1243 goto out;
1244
1245 switch (qhp->attr.state) {
1246 case C4IW_QP_STATE_IDLE:
1247 switch (attrs->next_state) {
1248 case C4IW_QP_STATE_RTS:
1249 if (!(mask & C4IW_QP_ATTR_LLP_STREAM_HANDLE)) {
1250 ret = -EINVAL;
1251 goto out;
1252 }
1253 if (!(mask & C4IW_QP_ATTR_MPA_ATTR)) {
1254 ret = -EINVAL;
1255 goto out;
1256 }
1257 qhp->attr.mpa_attr = attrs->mpa_attr;
1258 qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
1259 qhp->ep = qhp->attr.llp_stream_handle;
2f5b48c3 1260 set_state(qhp, C4IW_QP_STATE_RTS);
cfdda9d7
SW
1261
1262 /*
1263 * Ref the endpoint here and deref when we
1264 * disassociate the endpoint from the QP. This
1265 * happens in CLOSING->IDLE transition or *->ERROR
1266 * transition.
1267 */
1268 c4iw_get_ep(&qhp->ep->com);
cfdda9d7 1269 ret = rdma_init(rhp, qhp);
cfdda9d7
SW
1270 if (ret)
1271 goto err;
1272 break;
1273 case C4IW_QP_STATE_ERROR:
2f5b48c3
SW
1274 set_state(qhp, C4IW_QP_STATE_ERROR);
1275 flush_qp(qhp);
cfdda9d7
SW
1276 break;
1277 default:
1278 ret = -EINVAL;
1279 goto out;
1280 }
1281 break;
1282 case C4IW_QP_STATE_RTS:
1283 switch (attrs->next_state) {
1284 case C4IW_QP_STATE_CLOSING:
1285 BUG_ON(atomic_read(&qhp->ep->com.kref.refcount) < 2);
2f5b48c3 1286 set_state(qhp, C4IW_QP_STATE_CLOSING);
73d6fcad 1287 ep = qhp->ep;
cfdda9d7
SW
1288 if (!internal) {
1289 abort = 0;
1290 disconnect = 1;
2f5b48c3 1291 c4iw_get_ep(&qhp->ep->com);
cfdda9d7 1292 }
d32ae393
TT
1293 if (qhp->ibqp.uobject)
1294 t4_set_wq_in_error(&qhp->wq);
73d6fcad 1295 ret = rdma_fini(rhp, qhp, ep);
8da7e7a5 1296 if (ret)
cfdda9d7 1297 goto err;
cfdda9d7
SW
1298 break;
1299 case C4IW_QP_STATE_TERMINATE:
2f5b48c3 1300 set_state(qhp, C4IW_QP_STATE_TERMINATE);
d2fe99e8
KS
1301 qhp->attr.layer_etype = attrs->layer_etype;
1302 qhp->attr.ecode = attrs->ecode;
cfdda9d7
SW
1303 if (qhp->ibqp.uobject)
1304 t4_set_wq_in_error(&qhp->wq);
be4c9bad 1305 ep = qhp->ep;
0e42c1f4
SW
1306 if (!internal)
1307 terminate = 1;
be4c9bad 1308 disconnect = 1;
2f5b48c3 1309 c4iw_get_ep(&qhp->ep->com);
cfdda9d7
SW
1310 break;
1311 case C4IW_QP_STATE_ERROR:
2f5b48c3 1312 set_state(qhp, C4IW_QP_STATE_ERROR);
d32ae393
TT
1313 if (qhp->ibqp.uobject)
1314 t4_set_wq_in_error(&qhp->wq);
cfdda9d7
SW
1315 if (!internal) {
1316 abort = 1;
1317 disconnect = 1;
1318 ep = qhp->ep;
2f5b48c3 1319 c4iw_get_ep(&qhp->ep->com);
cfdda9d7
SW
1320 }
1321 goto err;
1322 break;
1323 default:
1324 ret = -EINVAL;
1325 goto out;
1326 }
1327 break;
1328 case C4IW_QP_STATE_CLOSING:
1329 if (!internal) {
1330 ret = -EINVAL;
1331 goto out;
1332 }
1333 switch (attrs->next_state) {
1334 case C4IW_QP_STATE_IDLE:
2f5b48c3
SW
1335 flush_qp(qhp);
1336 set_state(qhp, C4IW_QP_STATE_IDLE);
cfdda9d7
SW
1337 qhp->attr.llp_stream_handle = NULL;
1338 c4iw_put_ep(&qhp->ep->com);
1339 qhp->ep = NULL;
1340 wake_up(&qhp->wait);
1341 break;
1342 case C4IW_QP_STATE_ERROR:
1343 goto err;
1344 default:
1345 ret = -EINVAL;
1346 goto err;
1347 }
1348 break;
1349 case C4IW_QP_STATE_ERROR:
1350 if (attrs->next_state != C4IW_QP_STATE_IDLE) {
1351 ret = -EINVAL;
1352 goto out;
1353 }
1354 if (!t4_sq_empty(&qhp->wq) || !t4_rq_empty(&qhp->wq)) {
1355 ret = -EINVAL;
1356 goto out;
1357 }
2f5b48c3 1358 set_state(qhp, C4IW_QP_STATE_IDLE);
cfdda9d7
SW
1359 break;
1360 case C4IW_QP_STATE_TERMINATE:
1361 if (!internal) {
1362 ret = -EINVAL;
1363 goto out;
1364 }
1365 goto err;
1366 break;
1367 default:
1368 printk(KERN_ERR "%s in a bad state %d\n",
1369 __func__, qhp->attr.state);
1370 ret = -EINVAL;
1371 goto err;
1372 break;
1373 }
1374 goto out;
1375err:
1376 PDBG("%s disassociating ep %p qpid 0x%x\n", __func__, qhp->ep,
1377 qhp->wq.sq.qid);
1378
1379 /* disassociate the LLP connection */
1380 qhp->attr.llp_stream_handle = NULL;
af93fb5d
SW
1381 if (!ep)
1382 ep = qhp->ep;
cfdda9d7 1383 qhp->ep = NULL;
2f5b48c3 1384 set_state(qhp, C4IW_QP_STATE_ERROR);
cfdda9d7 1385 free = 1;
91e9c071 1386 abort = 1;
cfdda9d7
SW
1387 wake_up(&qhp->wait);
1388 BUG_ON(!ep);
2f5b48c3 1389 flush_qp(qhp);
cfdda9d7 1390out:
2f5b48c3 1391 mutex_unlock(&qhp->mutex);
cfdda9d7
SW
1392
1393 if (terminate)
be4c9bad 1394 post_terminate(qhp, NULL, internal ? GFP_ATOMIC : GFP_KERNEL);
cfdda9d7
SW
1395
1396 /*
1397 * If disconnect is 1, then we need to initiate a disconnect
1398 * on the EP. This can be a normal close (RTS->CLOSING) or
1399 * an abnormal close (RTS/CLOSING->ERROR).
1400 */
1401 if (disconnect) {
be4c9bad
RD
1402 c4iw_ep_disconnect(ep, abort, internal ? GFP_ATOMIC :
1403 GFP_KERNEL);
cfdda9d7
SW
1404 c4iw_put_ep(&ep->com);
1405 }
1406
1407 /*
1408 * If free is 1, then we've disassociated the EP from the QP
1409 * and we need to dereference the EP.
1410 */
1411 if (free)
1412 c4iw_put_ep(&ep->com);
cfdda9d7
SW
1413 PDBG("%s exit state %d\n", __func__, qhp->attr.state);
1414 return ret;
1415}
1416
422eea0a
VP
1417static int enable_qp_db(int id, void *p, void *data)
1418{
1419 struct c4iw_qp *qp = p;
1420
1421 t4_enable_wq_db(&qp->wq);
1422 return 0;
1423}
1424
cfdda9d7
SW
1425int c4iw_destroy_qp(struct ib_qp *ib_qp)
1426{
1427 struct c4iw_dev *rhp;
1428 struct c4iw_qp *qhp;
1429 struct c4iw_qp_attributes attrs;
1430 struct c4iw_ucontext *ucontext;
1431
1432 qhp = to_c4iw_qp(ib_qp);
1433 rhp = qhp->rhp;
1434
1435 attrs.next_state = C4IW_QP_STATE_ERROR;
d2fe99e8
KS
1436 if (qhp->attr.state == C4IW_QP_STATE_TERMINATE)
1437 c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1438 else
1439 c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
cfdda9d7
SW
1440 wait_event(qhp->wait, !qhp->ep);
1441
422eea0a
VP
1442 spin_lock_irq(&rhp->lock);
1443 remove_handle_nolock(rhp, &rhp->qpidr, qhp->wq.sq.qid);
1444 rhp->qpcnt--;
1445 BUG_ON(rhp->qpcnt < 0);
1446 if (rhp->qpcnt <= db_fc_threshold && rhp->db_state == FLOW_CONTROL) {
1447 rhp->rdev.stats.db_state_transitions++;
1448 rhp->db_state = NORMAL;
1449 idr_for_each(&rhp->qpidr, enable_qp_db, NULL);
1450 }
1451 spin_unlock_irq(&rhp->lock);
cfdda9d7
SW
1452 atomic_dec(&qhp->refcnt);
1453 wait_event(qhp->wait, !atomic_read(&qhp->refcnt));
1454
1455 ucontext = ib_qp->uobject ?
1456 to_c4iw_ucontext(ib_qp->uobject->context) : NULL;
1457 destroy_qp(&rhp->rdev, &qhp->wq,
1458 ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
1459
1460 PDBG("%s ib_qp %p qpid 0x%0x\n", __func__, ib_qp, qhp->wq.sq.qid);
1461 kfree(qhp);
1462 return 0;
1463}
1464
422eea0a
VP
1465static int disable_qp_db(int id, void *p, void *data)
1466{
1467 struct c4iw_qp *qp = p;
1468
1469 t4_disable_wq_db(&qp->wq);
1470 return 0;
1471}
1472
cfdda9d7
SW
1473struct ib_qp *c4iw_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
1474 struct ib_udata *udata)
1475{
1476 struct c4iw_dev *rhp;
1477 struct c4iw_qp *qhp;
1478 struct c4iw_pd *php;
1479 struct c4iw_cq *schp;
1480 struct c4iw_cq *rchp;
1481 struct c4iw_create_qp_resp uresp;
1482 int sqsize, rqsize;
1483 struct c4iw_ucontext *ucontext;
1484 int ret;
c6d7b267 1485 struct c4iw_mm_entry *mm1, *mm2, *mm3, *mm4, *mm5 = NULL;
cfdda9d7
SW
1486
1487 PDBG("%s ib_pd %p\n", __func__, pd);
1488
1489 if (attrs->qp_type != IB_QPT_RC)
1490 return ERR_PTR(-EINVAL);
1491
1492 php = to_c4iw_pd(pd);
1493 rhp = php->rhp;
1494 schp = get_chp(rhp, ((struct c4iw_cq *)attrs->send_cq)->cq.cqid);
1495 rchp = get_chp(rhp, ((struct c4iw_cq *)attrs->recv_cq)->cq.cqid);
1496 if (!schp || !rchp)
1497 return ERR_PTR(-EINVAL);
1498
1499 if (attrs->cap.max_inline_data > T4_MAX_SEND_INLINE)
1500 return ERR_PTR(-EINVAL);
1501
1502 rqsize = roundup(attrs->cap.max_recv_wr + 1, 16);
1503 if (rqsize > T4_MAX_RQ_SIZE)
1504 return ERR_PTR(-E2BIG);
1505
1506 sqsize = roundup(attrs->cap.max_send_wr + 1, 16);
1507 if (sqsize > T4_MAX_SQ_SIZE)
1508 return ERR_PTR(-E2BIG);
1509
1510 ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
1511
1512
1513 qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
1514 if (!qhp)
1515 return ERR_PTR(-ENOMEM);
1516 qhp->wq.sq.size = sqsize;
1517 qhp->wq.sq.memsize = (sqsize + 1) * sizeof *qhp->wq.sq.queue;
1518 qhp->wq.rq.size = rqsize;
1519 qhp->wq.rq.memsize = (rqsize + 1) * sizeof *qhp->wq.rq.queue;
1520
1521 if (ucontext) {
1522 qhp->wq.sq.memsize = roundup(qhp->wq.sq.memsize, PAGE_SIZE);
1523 qhp->wq.rq.memsize = roundup(qhp->wq.rq.memsize, PAGE_SIZE);
1524 }
1525
1526 PDBG("%s sqsize %u sqmemsize %zu rqsize %u rqmemsize %zu\n",
1527 __func__, sqsize, qhp->wq.sq.memsize, rqsize, qhp->wq.rq.memsize);
1528
1529 ret = create_qp(&rhp->rdev, &qhp->wq, &schp->cq, &rchp->cq,
1530 ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
1531 if (ret)
1532 goto err1;
1533
1534 attrs->cap.max_recv_wr = rqsize - 1;
1535 attrs->cap.max_send_wr = sqsize - 1;
1536 attrs->cap.max_inline_data = T4_MAX_SEND_INLINE;
1537
1538 qhp->rhp = rhp;
1539 qhp->attr.pd = php->pdid;
1540 qhp->attr.scq = ((struct c4iw_cq *) attrs->send_cq)->cq.cqid;
1541 qhp->attr.rcq = ((struct c4iw_cq *) attrs->recv_cq)->cq.cqid;
1542 qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
1543 qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
1544 qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
1545 qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
1546 qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
1547 qhp->attr.state = C4IW_QP_STATE_IDLE;
1548 qhp->attr.next_state = C4IW_QP_STATE_IDLE;
1549 qhp->attr.enable_rdma_read = 1;
1550 qhp->attr.enable_rdma_write = 1;
1551 qhp->attr.enable_bind = 1;
1552 qhp->attr.max_ord = 1;
1553 qhp->attr.max_ird = 1;
1554 spin_lock_init(&qhp->lock);
2f5b48c3 1555 mutex_init(&qhp->mutex);
cfdda9d7
SW
1556 init_waitqueue_head(&qhp->wait);
1557 atomic_set(&qhp->refcnt, 1);
1558
2c974781
VP
1559 spin_lock_irq(&rhp->lock);
1560 if (rhp->db_state != NORMAL)
1561 t4_disable_wq_db(&qhp->wq);
422eea0a
VP
1562 if (++rhp->qpcnt > db_fc_threshold && rhp->db_state == NORMAL) {
1563 rhp->rdev.stats.db_state_transitions++;
1564 rhp->db_state = FLOW_CONTROL;
1565 idr_for_each(&rhp->qpidr, disable_qp_db, NULL);
1566 }
2c974781
VP
1567 ret = insert_handle_nolock(rhp, &rhp->qpidr, qhp, qhp->wq.sq.qid);
1568 spin_unlock_irq(&rhp->lock);
cfdda9d7
SW
1569 if (ret)
1570 goto err2;
1571
cfdda9d7
SW
1572 if (udata) {
1573 mm1 = kmalloc(sizeof *mm1, GFP_KERNEL);
1574 if (!mm1) {
1575 ret = -ENOMEM;
30a6a62f 1576 goto err3;
cfdda9d7
SW
1577 }
1578 mm2 = kmalloc(sizeof *mm2, GFP_KERNEL);
1579 if (!mm2) {
1580 ret = -ENOMEM;
30a6a62f 1581 goto err4;
cfdda9d7
SW
1582 }
1583 mm3 = kmalloc(sizeof *mm3, GFP_KERNEL);
1584 if (!mm3) {
1585 ret = -ENOMEM;
30a6a62f 1586 goto err5;
cfdda9d7
SW
1587 }
1588 mm4 = kmalloc(sizeof *mm4, GFP_KERNEL);
1589 if (!mm4) {
1590 ret = -ENOMEM;
30a6a62f 1591 goto err6;
cfdda9d7 1592 }
c6d7b267
SW
1593 if (t4_sq_onchip(&qhp->wq.sq)) {
1594 mm5 = kmalloc(sizeof *mm5, GFP_KERNEL);
1595 if (!mm5) {
1596 ret = -ENOMEM;
1597 goto err7;
1598 }
1599 uresp.flags = C4IW_QPF_ONCHIP;
1600 } else
1601 uresp.flags = 0;
cfdda9d7
SW
1602 uresp.qid_mask = rhp->rdev.qpmask;
1603 uresp.sqid = qhp->wq.sq.qid;
1604 uresp.sq_size = qhp->wq.sq.size;
1605 uresp.sq_memsize = qhp->wq.sq.memsize;
1606 uresp.rqid = qhp->wq.rq.qid;
1607 uresp.rq_size = qhp->wq.rq.size;
1608 uresp.rq_memsize = qhp->wq.rq.memsize;
1609 spin_lock(&ucontext->mmap_lock);
c6d7b267
SW
1610 if (mm5) {
1611 uresp.ma_sync_key = ucontext->key;
1612 ucontext->key += PAGE_SIZE;
1613 }
cfdda9d7
SW
1614 uresp.sq_key = ucontext->key;
1615 ucontext->key += PAGE_SIZE;
1616 uresp.rq_key = ucontext->key;
1617 ucontext->key += PAGE_SIZE;
1618 uresp.sq_db_gts_key = ucontext->key;
1619 ucontext->key += PAGE_SIZE;
1620 uresp.rq_db_gts_key = ucontext->key;
1621 ucontext->key += PAGE_SIZE;
1622 spin_unlock(&ucontext->mmap_lock);
1623 ret = ib_copy_to_udata(udata, &uresp, sizeof uresp);
1624 if (ret)
c6d7b267 1625 goto err8;
cfdda9d7 1626 mm1->key = uresp.sq_key;
c6d7b267 1627 mm1->addr = qhp->wq.sq.phys_addr;
cfdda9d7
SW
1628 mm1->len = PAGE_ALIGN(qhp->wq.sq.memsize);
1629 insert_mmap(ucontext, mm1);
1630 mm2->key = uresp.rq_key;
1631 mm2->addr = virt_to_phys(qhp->wq.rq.queue);
1632 mm2->len = PAGE_ALIGN(qhp->wq.rq.memsize);
1633 insert_mmap(ucontext, mm2);
1634 mm3->key = uresp.sq_db_gts_key;
1635 mm3->addr = qhp->wq.sq.udb;
1636 mm3->len = PAGE_SIZE;
1637 insert_mmap(ucontext, mm3);
1638 mm4->key = uresp.rq_db_gts_key;
1639 mm4->addr = qhp->wq.rq.udb;
1640 mm4->len = PAGE_SIZE;
1641 insert_mmap(ucontext, mm4);
c6d7b267
SW
1642 if (mm5) {
1643 mm5->key = uresp.ma_sync_key;
1644 mm5->addr = (pci_resource_start(rhp->rdev.lldi.pdev, 0)
1645 + A_PCIE_MA_SYNC) & PAGE_MASK;
1646 mm5->len = PAGE_SIZE;
1647 insert_mmap(ucontext, mm5);
1648 }
cfdda9d7
SW
1649 }
1650 qhp->ibqp.qp_num = qhp->wq.sq.qid;
1651 init_timer(&(qhp->timer));
1652 PDBG("%s qhp %p sq_num_entries %d, rq_num_entries %d qpid 0x%0x\n",
1653 __func__, qhp, qhp->attr.sq_num_entries, qhp->attr.rq_num_entries,
1654 qhp->wq.sq.qid);
1655 return &qhp->ibqp;
c6d7b267
SW
1656err8:
1657 kfree(mm5);
cfdda9d7 1658err7:
30a6a62f 1659 kfree(mm4);
cfdda9d7 1660err6:
30a6a62f 1661 kfree(mm3);
cfdda9d7 1662err5:
30a6a62f 1663 kfree(mm2);
cfdda9d7 1664err4:
30a6a62f 1665 kfree(mm1);
cfdda9d7
SW
1666err3:
1667 remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
1668err2:
1669 destroy_qp(&rhp->rdev, &qhp->wq,
1670 ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
1671err1:
1672 kfree(qhp);
1673 return ERR_PTR(ret);
1674}
1675
1676int c4iw_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1677 int attr_mask, struct ib_udata *udata)
1678{
1679 struct c4iw_dev *rhp;
1680 struct c4iw_qp *qhp;
1681 enum c4iw_qp_attr_mask mask = 0;
1682 struct c4iw_qp_attributes attrs;
1683
1684 PDBG("%s ib_qp %p\n", __func__, ibqp);
1685
1686 /* iwarp does not support the RTR state */
1687 if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
1688 attr_mask &= ~IB_QP_STATE;
1689
1690 /* Make sure we still have something left to do */
1691 if (!attr_mask)
1692 return 0;
1693
1694 memset(&attrs, 0, sizeof attrs);
1695 qhp = to_c4iw_qp(ibqp);
1696 rhp = qhp->rhp;
1697
1698 attrs.next_state = c4iw_convert_state(attr->qp_state);
1699 attrs.enable_rdma_read = (attr->qp_access_flags &
1700 IB_ACCESS_REMOTE_READ) ? 1 : 0;
1701 attrs.enable_rdma_write = (attr->qp_access_flags &
1702 IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
1703 attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
1704
1705
1706 mask |= (attr_mask & IB_QP_STATE) ? C4IW_QP_ATTR_NEXT_STATE : 0;
1707 mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
1708 (C4IW_QP_ATTR_ENABLE_RDMA_READ |
1709 C4IW_QP_ATTR_ENABLE_RDMA_WRITE |
1710 C4IW_QP_ATTR_ENABLE_RDMA_BIND) : 0;
1711
2c974781
VP
1712 /*
1713 * Use SQ_PSN and RQ_PSN to pass in IDX_INC values for
1714 * ringing the queue db when we're in DB_FULL mode.
1715 */
1716 attrs.sq_db_inc = attr->sq_psn;
1717 attrs.rq_db_inc = attr->rq_psn;
1718 mask |= (attr_mask & IB_QP_SQ_PSN) ? C4IW_QP_ATTR_SQ_DB : 0;
1719 mask |= (attr_mask & IB_QP_RQ_PSN) ? C4IW_QP_ATTR_RQ_DB : 0;
1720
cfdda9d7
SW
1721 return c4iw_modify_qp(rhp, qhp, mask, &attrs, 0);
1722}
1723
1724struct ib_qp *c4iw_get_qp(struct ib_device *dev, int qpn)
1725{
1726 PDBG("%s ib_dev %p qpn 0x%x\n", __func__, dev, qpn);
1727 return (struct ib_qp *)get_qhp(to_c4iw_dev(dev), qpn);
1728}
67bbc055
VP
1729
1730int c4iw_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1731 int attr_mask, struct ib_qp_init_attr *init_attr)
1732{
1733 struct c4iw_qp *qhp = to_c4iw_qp(ibqp);
1734
1735 memset(attr, 0, sizeof *attr);
1736 memset(init_attr, 0, sizeof *init_attr);
1737 attr->qp_state = to_ib_qp_state(qhp->attr.state);
1738 return 0;
1739}