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