xref: /freebsd/sys/dev/cxgbe/iw_cxgbe/qp.c (revision 2c9a9dfc187d171de6b92654d71b977f067ed641)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009-2013 Chelsio, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_inet.h"
38 
39 #ifdef TCP_OFFLOAD
40 #include <sys/types.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/sockio.h>
45 #include <sys/taskqueue.h>
46 #include <netinet/in.h>
47 #include <net/route.h>
48 
49 #include <netinet/in_systm.h>
50 #include <netinet/in_pcb.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/tcp_var.h>
54 #include <netinet/tcp.h>
55 #include <netinet/tcpip.h>
56 
57 #include <netinet/toecore.h>
58 
59 struct sge_iq;
60 struct rss_header;
61 struct cpl_set_tcb_rpl;
62 #include <linux/types.h>
63 #include "offload.h"
64 #include "tom/t4_tom.h"
65 
66 #include "iw_cxgbe.h"
67 #include "user.h"
68 
69 static int creds(struct toepcb *toep, struct inpcb *inp, size_t wrsize);
70 static int max_fr_immd = T4_MAX_FR_IMMD;//SYSCTL parameter later...
71 
72 static int alloc_ird(struct c4iw_dev *dev, u32 ird)
73 {
74 	int ret = 0;
75 
76 	spin_lock_irq(&dev->lock);
77 	if (ird <= dev->avail_ird)
78 		dev->avail_ird -= ird;
79 	else
80 		ret = -ENOMEM;
81 	spin_unlock_irq(&dev->lock);
82 
83 	if (ret)
84 		log(LOG_WARNING, "%s: device IRD resources exhausted\n",
85 			device_get_nameunit(dev->rdev.adap->dev));
86 
87 	return ret;
88 }
89 
90 static void free_ird(struct c4iw_dev *dev, int ird)
91 {
92 	spin_lock_irq(&dev->lock);
93 	dev->avail_ird += ird;
94 	spin_unlock_irq(&dev->lock);
95 }
96 
97 static void set_state(struct c4iw_qp *qhp, enum c4iw_qp_state state)
98 {
99 	unsigned long flag;
100 	spin_lock_irqsave(&qhp->lock, flag);
101 	qhp->attr.state = state;
102 	spin_unlock_irqrestore(&qhp->lock, flag);
103 }
104 
105 static int destroy_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
106 		      struct c4iw_dev_ucontext *uctx)
107 {
108 	struct c4iw_dev *rhp = rdev_to_c4iw_dev(rdev);
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 	dma_free_coherent(rhp->ibdev.dma_device,
114 			wq->rq.memsize, wq->rq.queue,
115 			dma_unmap_addr(&wq->rq, mapping));
116 	dma_free_coherent(rhp->ibdev.dma_device,
117 			wq->sq.memsize, wq->sq.queue,
118 			dma_unmap_addr(&wq->sq, mapping));
119 	c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
120 	kfree(wq->rq.sw_rq);
121 	kfree(wq->sq.sw_sq);
122 	c4iw_put_qpid(rdev, wq->rq.qid, uctx);
123 	c4iw_put_qpid(rdev, wq->sq.qid, uctx);
124 	return 0;
125 }
126 
127 static int create_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
128 		     struct t4_cq *rcq, struct t4_cq *scq,
129 		     struct c4iw_dev_ucontext *uctx)
130 {
131 	struct adapter *sc = rdev->adap;
132 	struct c4iw_dev *rhp = rdev_to_c4iw_dev(rdev);
133 	int user = (uctx != &rdev->uctx);
134 	struct fw_ri_res_wr *res_wr;
135 	struct fw_ri_res *res;
136 	int wr_len;
137 	struct c4iw_wr_wait wr_wait;
138 	int ret = 0;
139 	int eqsize;
140 	struct wrqe *wr;
141 	u64 sq_bar2_qoffset = 0, rq_bar2_qoffset = 0;
142 
143 	wq->sq.qid = c4iw_get_qpid(rdev, uctx);
144 	if (!wq->sq.qid)
145 		return -ENOMEM;
146 
147 	wq->rq.qid = c4iw_get_qpid(rdev, uctx);
148 	if (!wq->rq.qid) {
149 		ret = -ENOMEM;
150 		goto free_sq_qid;
151 	}
152 
153 	if (!user) {
154 		wq->sq.sw_sq = kzalloc(wq->sq.size * sizeof *wq->sq.sw_sq,
155 				 GFP_KERNEL);
156 		if (!wq->sq.sw_sq) {
157 			ret = -ENOMEM;
158 			goto free_rq_qid;
159 		}
160 
161 		wq->rq.sw_rq = kzalloc(wq->rq.size * sizeof *wq->rq.sw_rq,
162 				 GFP_KERNEL);
163 		if (!wq->rq.sw_rq) {
164 			ret = -ENOMEM;
165 			goto free_sw_sq;
166 		}
167 	}
168 
169 	/*
170 	 * RQT must be a power of 2 and at least 16 deep.
171 	 */
172 	wq->rq.rqt_size = roundup_pow_of_two(max_t(u16, wq->rq.size, 16));
173 	wq->rq.rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rq.rqt_size);
174 	if (!wq->rq.rqt_hwaddr) {
175 		ret = -ENOMEM;
176 		goto free_sw_rq;
177 	}
178 
179 	/*QP memory, allocate DMAable memory for Send & Receive Queues */
180 	wq->sq.queue = dma_alloc_coherent(rhp->ibdev.dma_device, wq->sq.memsize,
181 				       &(wq->sq.dma_addr), GFP_KERNEL);
182 	if (!wq->sq.queue) {
183 		ret = -ENOMEM;
184 		goto free_hwaddr;
185 	}
186 	wq->sq.phys_addr = vtophys(wq->sq.queue);
187 	dma_unmap_addr_set(&wq->sq, mapping, wq->sq.dma_addr);
188 	memset(wq->sq.queue, 0, wq->sq.memsize);
189 
190 	wq->rq.queue = dma_alloc_coherent(rhp->ibdev.dma_device,
191 			wq->rq.memsize, &(wq->rq.dma_addr), GFP_KERNEL);
192 	if (!wq->rq.queue) {
193 		ret = -ENOMEM;
194 		goto free_sq_dma;
195 	}
196 	wq->rq.phys_addr = vtophys(wq->rq.queue);
197 	dma_unmap_addr_set(&wq->rq, mapping, wq->rq.dma_addr);
198 	memset(wq->rq.queue, 0, wq->rq.memsize);
199 
200 	CTR5(KTR_IW_CXGBE,
201 	    "%s QP sq base va 0x%p pa 0x%llx rq base va 0x%p pa 0x%llx",
202 	    __func__,
203 	    wq->sq.queue, (unsigned long long)wq->sq.phys_addr,
204 	    wq->rq.queue, (unsigned long long)wq->rq.phys_addr);
205 
206 	/* Doorbell/WC regions, determine the BAR2 queue offset and qid. */
207 	t4_bar2_sge_qregs(rdev->adap, wq->sq.qid, T4_BAR2_QTYPE_EGRESS, user,
208 			&sq_bar2_qoffset, &wq->sq.bar2_qid);
209 	t4_bar2_sge_qregs(rdev->adap, wq->rq.qid, T4_BAR2_QTYPE_EGRESS, user,
210 			&rq_bar2_qoffset, &wq->rq.bar2_qid);
211 
212 	if (user) {
213 		/* Compute BAR2 DB/WC physical address(page-aligned) for
214 		 * Userspace mapping.
215 		 */
216 		wq->sq.bar2_pa = (rdev->bar2_pa + sq_bar2_qoffset) & PAGE_MASK;
217 		wq->rq.bar2_pa = (rdev->bar2_pa + rq_bar2_qoffset) & PAGE_MASK;
218 		CTR3(KTR_IW_CXGBE,
219 			"%s BAR2 DB/WC sq base pa 0x%llx rq base pa 0x%llx",
220 			__func__, (unsigned long long)wq->sq.bar2_pa,
221 			(unsigned long long)wq->rq.bar2_pa);
222 	} else {
223 		/* Compute BAR2 DB/WC virtual address to access in kernel. */
224 		wq->sq.bar2_va = (void __iomem *)((u64)rdev->bar2_kva +
225 				sq_bar2_qoffset);
226 		wq->rq.bar2_va = (void __iomem *)((u64)rdev->bar2_kva +
227 				rq_bar2_qoffset);
228 		CTR3(KTR_IW_CXGBE, "%s BAR2 DB/WC sq base va %p rq base va %p",
229 			__func__, (unsigned long long)wq->sq.bar2_va,
230 			(unsigned long long)wq->rq.bar2_va);
231 	}
232 
233 	wq->rdev = rdev;
234 	wq->rq.msn = 1;
235 
236 	/* build fw_ri_res_wr */
237 	wr_len = sizeof *res_wr + 2 * sizeof *res;
238 
239 	wr = alloc_wrqe(wr_len, &sc->sge.ctrlq[0]);
240 	if (wr == NULL) {
241 		ret = -ENOMEM;
242 		goto free_rq_dma;
243 	}
244         res_wr = wrtod(wr);
245 
246 	memset(res_wr, 0, wr_len);
247 	res_wr->op_nres = cpu_to_be32(
248 			V_FW_WR_OP(FW_RI_RES_WR) |
249 			V_FW_RI_RES_WR_NRES(2) |
250 			F_FW_WR_COMPL);
251 	res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
252 	res_wr->cookie = (unsigned long) &wr_wait;
253 	res = res_wr->res;
254 	res->u.sqrq.restype = FW_RI_RES_TYPE_SQ;
255 	res->u.sqrq.op = FW_RI_RES_OP_WRITE;
256 
257 	/* eqsize is the number of 64B entries plus the status page size. */
258 	eqsize = wq->sq.size * T4_SQ_NUM_SLOTS +
259 			rdev->hw_queue.t4_eq_status_entries;
260 
261 	res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
262 		V_FW_RI_RES_WR_HOSTFCMODE(0) |	/* no host cidx updates */
263 		V_FW_RI_RES_WR_CPRIO(0) |	/* don't keep in chip cache */
264 		V_FW_RI_RES_WR_PCIECHN(0) |	/* set by uP at ri_init time */
265 		V_FW_RI_RES_WR_IQID(scq->cqid));
266 	res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
267 		V_FW_RI_RES_WR_DCAEN(0) |
268 		V_FW_RI_RES_WR_DCACPU(0) |
269 		V_FW_RI_RES_WR_FBMIN(chip_id(sc) <= CHELSIO_T5 ?
270 		    X_FETCHBURSTMIN_64B : X_FETCHBURSTMIN_64B_T6) |
271 		V_FW_RI_RES_WR_FBMAX(3) |
272 		V_FW_RI_RES_WR_CIDXFTHRESHO(0) |
273 		V_FW_RI_RES_WR_CIDXFTHRESH(0) |
274 		V_FW_RI_RES_WR_EQSIZE(eqsize));
275 	res->u.sqrq.eqid = cpu_to_be32(wq->sq.qid);
276 	res->u.sqrq.eqaddr = cpu_to_be64(wq->sq.dma_addr);
277 	res++;
278 	res->u.sqrq.restype = FW_RI_RES_TYPE_RQ;
279 	res->u.sqrq.op = FW_RI_RES_OP_WRITE;
280 
281 	/* eqsize is the number of 64B entries plus the status page size. */
282 	eqsize = wq->rq.size * T4_RQ_NUM_SLOTS +
283 			rdev->hw_queue.t4_eq_status_entries;
284 	res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
285 		V_FW_RI_RES_WR_HOSTFCMODE(0) |	/* no host cidx updates */
286 		V_FW_RI_RES_WR_CPRIO(0) |	/* don't keep in chip cache */
287 		V_FW_RI_RES_WR_PCIECHN(0) |	/* set by uP at ri_init time */
288 		V_FW_RI_RES_WR_IQID(rcq->cqid));
289 	res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
290 		V_FW_RI_RES_WR_DCAEN(0) |
291 		V_FW_RI_RES_WR_DCACPU(0) |
292 		V_FW_RI_RES_WR_FBMIN(chip_id(sc) <= CHELSIO_T5 ?
293 		    X_FETCHBURSTMIN_64B : X_FETCHBURSTMIN_64B_T6) |
294 		V_FW_RI_RES_WR_FBMAX(3) |
295 		V_FW_RI_RES_WR_CIDXFTHRESHO(0) |
296 		V_FW_RI_RES_WR_CIDXFTHRESH(0) |
297 		V_FW_RI_RES_WR_EQSIZE(eqsize));
298 	res->u.sqrq.eqid = cpu_to_be32(wq->rq.qid);
299 	res->u.sqrq.eqaddr = cpu_to_be64(wq->rq.dma_addr);
300 
301 	c4iw_init_wr_wait(&wr_wait);
302 
303 	t4_wrq_tx(sc, wr);
304 	ret = c4iw_wait_for_reply(rdev, &wr_wait, 0, wq->sq.qid,
305 			NULL, __func__);
306 	if (ret)
307 		goto free_rq_dma;
308 
309 	CTR5(KTR_IW_CXGBE,
310 	    "%s sqid 0x%x rqid 0x%x kdb 0x%p squdb 0x%llx rqudb 0x%llx",
311 	    __func__, wq->sq.qid, wq->rq.qid,
312 	    (unsigned long long)wq->sq.bar2_va,
313 	    (unsigned long long)wq->rq.bar2_va);
314 
315 	return 0;
316 free_rq_dma:
317 	dma_free_coherent(rhp->ibdev.dma_device,
318 			  wq->rq.memsize, wq->rq.queue,
319 			  dma_unmap_addr(&wq->rq, mapping));
320 free_sq_dma:
321 	dma_free_coherent(rhp->ibdev.dma_device,
322 			  wq->sq.memsize, wq->sq.queue,
323 			  dma_unmap_addr(&wq->sq, mapping));
324 free_hwaddr:
325 	c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
326 free_sw_rq:
327 	kfree(wq->rq.sw_rq);
328 free_sw_sq:
329 	kfree(wq->sq.sw_sq);
330 free_rq_qid:
331 	c4iw_put_qpid(rdev, wq->rq.qid, uctx);
332 free_sq_qid:
333 	c4iw_put_qpid(rdev, wq->sq.qid, uctx);
334 	return ret;
335 }
336 
337 static int build_immd(struct t4_sq *sq, struct fw_ri_immd *immdp,
338 		      struct ib_send_wr *wr, int max, u32 *plenp)
339 {
340 	u8 *dstp, *srcp;
341 	u32 plen = 0;
342 	int i;
343 	int rem, len;
344 
345 	dstp = (u8 *)immdp->data;
346 	for (i = 0; i < wr->num_sge; i++) {
347 		if ((plen + wr->sg_list[i].length) > max)
348 			return -EMSGSIZE;
349 		srcp = (u8 *)(unsigned long)wr->sg_list[i].addr;
350 		plen += wr->sg_list[i].length;
351 		rem = wr->sg_list[i].length;
352 		while (rem) {
353 			if (dstp == (u8 *)&sq->queue[sq->size])
354 				dstp = (u8 *)sq->queue;
355 			if (rem <= (u8 *)&sq->queue[sq->size] - dstp)
356 				len = rem;
357 			else
358 				len = (u8 *)&sq->queue[sq->size] - dstp;
359 			memcpy(dstp, srcp, len);
360 			dstp += len;
361 			srcp += len;
362 			rem -= len;
363 		}
364 	}
365 	len = roundup(plen + sizeof *immdp, 16) - (plen + sizeof *immdp);
366 	if (len)
367 		memset(dstp, 0, len);
368 	immdp->op = FW_RI_DATA_IMMD;
369 	immdp->r1 = 0;
370 	immdp->r2 = 0;
371 	immdp->immdlen = cpu_to_be32(plen);
372 	*plenp = plen;
373 	return 0;
374 }
375 
376 static int build_isgl(__be64 *queue_start, __be64 *queue_end,
377 		      struct fw_ri_isgl *isglp, struct ib_sge *sg_list,
378 		      int num_sge, u32 *plenp)
379 
380 {
381 	int i;
382 	u32 plen = 0;
383 	__be64 *flitp = (__be64 *)isglp->sge;
384 
385 	for (i = 0; i < num_sge; i++) {
386 		if ((plen + sg_list[i].length) < plen)
387 			return -EMSGSIZE;
388 		plen += sg_list[i].length;
389 		*flitp = cpu_to_be64(((u64)sg_list[i].lkey << 32) |
390 				     sg_list[i].length);
391 		if (++flitp == queue_end)
392 			flitp = queue_start;
393 		*flitp = cpu_to_be64(sg_list[i].addr);
394 		if (++flitp == queue_end)
395 			flitp = queue_start;
396 	}
397 	*flitp = (__force __be64)0;
398 	isglp->op = FW_RI_DATA_ISGL;
399 	isglp->r1 = 0;
400 	isglp->nsge = cpu_to_be16(num_sge);
401 	isglp->r2 = 0;
402 	if (plenp)
403 		*plenp = plen;
404 	return 0;
405 }
406 
407 static int build_rdma_send(struct t4_sq *sq, union t4_wr *wqe,
408 			   struct ib_send_wr *wr, u8 *len16)
409 {
410 	u32 plen;
411 	int size;
412 	int ret;
413 
414 	if (wr->num_sge > T4_MAX_SEND_SGE)
415 		return -EINVAL;
416 	switch (wr->opcode) {
417 	case IB_WR_SEND:
418 		if (wr->send_flags & IB_SEND_SOLICITED)
419 			wqe->send.sendop_pkd = cpu_to_be32(
420 				V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_SE));
421 		else
422 			wqe->send.sendop_pkd = cpu_to_be32(
423 				V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND));
424 		wqe->send.stag_inv = 0;
425 		break;
426 	case IB_WR_SEND_WITH_INV:
427 		if (wr->send_flags & IB_SEND_SOLICITED)
428 			wqe->send.sendop_pkd = cpu_to_be32(
429 				V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_SE_INV));
430 		else
431 			wqe->send.sendop_pkd = cpu_to_be32(
432 				V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_INV));
433 		wqe->send.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
434 		break;
435 
436 	default:
437 		return -EINVAL;
438 	}
439 	wqe->send.r3 = 0;
440 	wqe->send.r4 = 0;
441 
442 	plen = 0;
443 	if (wr->num_sge) {
444 		if (wr->send_flags & IB_SEND_INLINE) {
445 			ret = build_immd(sq, wqe->send.u.immd_src, wr,
446 					 T4_MAX_SEND_INLINE, &plen);
447 			if (ret)
448 				return ret;
449 			size = sizeof wqe->send + sizeof(struct fw_ri_immd) +
450 			       plen;
451 		} else {
452 			ret = build_isgl((__be64 *)sq->queue,
453 					 (__be64 *)&sq->queue[sq->size],
454 					 wqe->send.u.isgl_src,
455 					 wr->sg_list, wr->num_sge, &plen);
456 			if (ret)
457 				return ret;
458 			size = sizeof wqe->send + sizeof(struct fw_ri_isgl) +
459 			       wr->num_sge * sizeof(struct fw_ri_sge);
460 		}
461 	} else {
462 		wqe->send.u.immd_src[0].op = FW_RI_DATA_IMMD;
463 		wqe->send.u.immd_src[0].r1 = 0;
464 		wqe->send.u.immd_src[0].r2 = 0;
465 		wqe->send.u.immd_src[0].immdlen = 0;
466 		size = sizeof wqe->send + sizeof(struct fw_ri_immd);
467 		plen = 0;
468 	}
469 	*len16 = DIV_ROUND_UP(size, 16);
470 	wqe->send.plen = cpu_to_be32(plen);
471 	return 0;
472 }
473 
474 static int build_rdma_write(struct t4_sq *sq, union t4_wr *wqe,
475 			    struct ib_send_wr *wr, u8 *len16)
476 {
477 	u32 plen;
478 	int size;
479 	int ret;
480 
481 	if (wr->num_sge > T4_MAX_SEND_SGE)
482 		return -EINVAL;
483 	wqe->write.immd_data = 0;
484 	wqe->write.stag_sink = cpu_to_be32(rdma_wr(wr)->rkey);
485 	wqe->write.to_sink = cpu_to_be64(rdma_wr(wr)->remote_addr);
486 	if (wr->num_sge) {
487 		if (wr->send_flags & IB_SEND_INLINE) {
488 			ret = build_immd(sq, wqe->write.u.immd_src, wr,
489 					 T4_MAX_WRITE_INLINE, &plen);
490 			if (ret)
491 				return ret;
492 			size = sizeof wqe->write + sizeof(struct fw_ri_immd) +
493 			       plen;
494 		} else {
495 			ret = build_isgl((__be64 *)sq->queue,
496 					 (__be64 *)&sq->queue[sq->size],
497 					 wqe->write.u.isgl_src,
498 					 wr->sg_list, wr->num_sge, &plen);
499 			if (ret)
500 				return ret;
501 			size = sizeof wqe->write + sizeof(struct fw_ri_isgl) +
502 			       wr->num_sge * sizeof(struct fw_ri_sge);
503 		}
504 	} else {
505 		wqe->write.u.immd_src[0].op = FW_RI_DATA_IMMD;
506 		wqe->write.u.immd_src[0].r1 = 0;
507 		wqe->write.u.immd_src[0].r2 = 0;
508 		wqe->write.u.immd_src[0].immdlen = 0;
509 		size = sizeof wqe->write + sizeof(struct fw_ri_immd);
510 		plen = 0;
511 	}
512 	*len16 = DIV_ROUND_UP(size, 16);
513 	wqe->write.plen = cpu_to_be32(plen);
514 	return 0;
515 }
516 
517 static int build_rdma_read(union t4_wr *wqe, struct ib_send_wr *wr, u8 *len16)
518 {
519 	if (wr->num_sge > 1)
520 		return -EINVAL;
521 	if (wr->num_sge && wr->sg_list[0].length) {
522 		wqe->read.stag_src = cpu_to_be32(rdma_wr(wr)->rkey);
523 		wqe->read.to_src_hi = cpu_to_be32((u32)(rdma_wr(wr)->remote_addr
524 							>> 32));
525 		wqe->read.to_src_lo =
526 			cpu_to_be32((u32)rdma_wr(wr)->remote_addr);
527 		wqe->read.stag_sink = cpu_to_be32(wr->sg_list[0].lkey);
528 		wqe->read.plen = cpu_to_be32(wr->sg_list[0].length);
529 		wqe->read.to_sink_hi = cpu_to_be32((u32)(wr->sg_list[0].addr
530 							 >> 32));
531 		wqe->read.to_sink_lo = cpu_to_be32((u32)(wr->sg_list[0].addr));
532 	} else {
533 		wqe->read.stag_src = cpu_to_be32(2);
534 		wqe->read.to_src_hi = 0;
535 		wqe->read.to_src_lo = 0;
536 		wqe->read.stag_sink = cpu_to_be32(2);
537 		wqe->read.plen = 0;
538 		wqe->read.to_sink_hi = 0;
539 		wqe->read.to_sink_lo = 0;
540 	}
541 	wqe->read.r2 = 0;
542 	wqe->read.r5 = 0;
543 	*len16 = DIV_ROUND_UP(sizeof wqe->read, 16);
544 	return 0;
545 }
546 
547 static int build_rdma_recv(struct c4iw_qp *qhp, union t4_recv_wr *wqe,
548 			   struct ib_recv_wr *wr, u8 *len16)
549 {
550 	int ret;
551 
552 	ret = build_isgl((__be64 *)qhp->wq.rq.queue,
553 			 (__be64 *)&qhp->wq.rq.queue[qhp->wq.rq.size],
554 			 &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
555 	if (ret)
556 		return ret;
557 	*len16 = DIV_ROUND_UP(sizeof wqe->recv +
558 			      wr->num_sge * sizeof(struct fw_ri_sge), 16);
559 	return 0;
560 }
561 
562 static int build_inv_stag(union t4_wr *wqe, struct ib_send_wr *wr,
563 			  u8 *len16)
564 {
565 	wqe->inv.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
566 	wqe->inv.r2 = 0;
567 	*len16 = DIV_ROUND_UP(sizeof wqe->inv, 16);
568 	return 0;
569 }
570 
571 static void free_qp_work(struct work_struct *work)
572 {
573 	struct c4iw_ucontext *ucontext;
574 	struct c4iw_qp *qhp;
575 	struct c4iw_dev *rhp;
576 
577 	qhp = container_of(work, struct c4iw_qp, free_work);
578 	ucontext = qhp->ucontext;
579 	rhp = qhp->rhp;
580 
581 	CTR3(KTR_IW_CXGBE, "%s qhp %p ucontext %p", __func__,
582 			qhp, ucontext);
583 	destroy_qp(&rhp->rdev, &qhp->wq,
584 		   ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
585 
586 	if (ucontext)
587 		c4iw_put_ucontext(ucontext);
588 	kfree(qhp);
589 }
590 
591 static void queue_qp_free(struct kref *kref)
592 {
593 	struct c4iw_qp *qhp;
594 
595 	qhp = container_of(kref, struct c4iw_qp, kref);
596 	CTR2(KTR_IW_CXGBE, "%s qhp %p", __func__, qhp);
597 	queue_work(qhp->rhp->rdev.free_workq, &qhp->free_work);
598 }
599 
600 void c4iw_qp_add_ref(struct ib_qp *qp)
601 {
602 	CTR2(KTR_IW_CXGBE, "%s ib_qp %p", __func__, qp);
603 	kref_get(&to_c4iw_qp(qp)->kref);
604 }
605 
606 void c4iw_qp_rem_ref(struct ib_qp *qp)
607 {
608 	CTR2(KTR_IW_CXGBE, "%s ib_qp %p", __func__, qp);
609 	kref_put(&to_c4iw_qp(qp)->kref, queue_qp_free);
610 }
611 
612 static void complete_sq_drain_wr(struct c4iw_qp *qhp, struct ib_send_wr *wr)
613 {
614 	struct t4_cqe cqe = {};
615 	struct c4iw_cq *schp;
616 	unsigned long flag;
617 	struct t4_cq *cq;
618 
619 	schp = to_c4iw_cq(qhp->ibqp.send_cq);
620 	cq = &schp->cq;
621 
622 	PDBG("%s drain sq id %u\n", __func__, qhp->wq.sq.qid);
623 	cqe.u.drain_cookie = wr->wr_id;
624 	cqe.header = cpu_to_be32(V_CQE_STATUS(T4_ERR_SWFLUSH) |
625 				 V_CQE_OPCODE(C4IW_DRAIN_OPCODE) |
626 				 V_CQE_TYPE(1) |
627 				 V_CQE_SWCQE(1) |
628 				 V_CQE_QPID(qhp->wq.sq.qid));
629 
630 	spin_lock_irqsave(&schp->lock, flag);
631 	cqe.bits_type_ts = cpu_to_be64(V_CQE_GENBIT((u64)cq->gen));
632 	cq->sw_queue[cq->sw_pidx] = cqe;
633 	t4_swcq_produce(cq);
634 	spin_unlock_irqrestore(&schp->lock, flag);
635 
636 	spin_lock_irqsave(&schp->comp_handler_lock, flag);
637 	(*schp->ibcq.comp_handler)(&schp->ibcq,
638 				   schp->ibcq.cq_context);
639 	spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
640 }
641 
642 static void complete_rq_drain_wr(struct c4iw_qp *qhp, struct ib_recv_wr *wr)
643 {
644 	struct t4_cqe cqe = {};
645 	struct c4iw_cq *rchp;
646 	unsigned long flag;
647 	struct t4_cq *cq;
648 
649 	rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
650 	cq = &rchp->cq;
651 
652 	PDBG("%s drain rq id %u\n", __func__, qhp->wq.sq.qid);
653 	cqe.u.drain_cookie = wr->wr_id;
654 	cqe.header = cpu_to_be32(V_CQE_STATUS(T4_ERR_SWFLUSH) |
655 				 V_CQE_OPCODE(C4IW_DRAIN_OPCODE) |
656 				 V_CQE_TYPE(0) |
657 				 V_CQE_SWCQE(1) |
658 				 V_CQE_QPID(qhp->wq.sq.qid));
659 
660 	spin_lock_irqsave(&rchp->lock, flag);
661 	cqe.bits_type_ts = cpu_to_be64(V_CQE_GENBIT((u64)cq->gen));
662 	cq->sw_queue[cq->sw_pidx] = cqe;
663 	t4_swcq_produce(cq);
664 	spin_unlock_irqrestore(&rchp->lock, flag);
665 
666 	spin_lock_irqsave(&rchp->comp_handler_lock, flag);
667 	(*rchp->ibcq.comp_handler)(&rchp->ibcq,
668 				   rchp->ibcq.cq_context);
669 	spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
670 }
671 
672 static void build_tpte_memreg(struct fw_ri_fr_nsmr_tpte_wr *fr,
673 		struct ib_reg_wr *wr, struct c4iw_mr *mhp, u8 *len16)
674 {
675 	__be64 *p = (__be64 *)fr->pbl;
676 
677 	fr->r2 = cpu_to_be32(0);
678 	fr->stag = cpu_to_be32(mhp->ibmr.rkey);
679 
680 	fr->tpte.valid_to_pdid = cpu_to_be32(F_FW_RI_TPTE_VALID |
681 			V_FW_RI_TPTE_STAGKEY((mhp->ibmr.rkey & M_FW_RI_TPTE_STAGKEY)) |
682 			V_FW_RI_TPTE_STAGSTATE(1) |
683 			V_FW_RI_TPTE_STAGTYPE(FW_RI_STAG_NSMR) |
684 			V_FW_RI_TPTE_PDID(mhp->attr.pdid));
685 	fr->tpte.locread_to_qpid = cpu_to_be32(
686 			V_FW_RI_TPTE_PERM(c4iw_ib_to_tpt_access(wr->access)) |
687 			V_FW_RI_TPTE_ADDRTYPE(FW_RI_VA_BASED_TO) |
688 			V_FW_RI_TPTE_PS(ilog2(wr->mr->page_size) - 12));
689 	fr->tpte.nosnoop_pbladdr = cpu_to_be32(V_FW_RI_TPTE_PBLADDR(
690 			      PBL_OFF(&mhp->rhp->rdev, mhp->attr.pbl_addr)>>3));
691 	fr->tpte.dca_mwbcnt_pstag = cpu_to_be32(0);
692 	fr->tpte.len_hi = cpu_to_be32(mhp->ibmr.length >> 32);
693 	fr->tpte.len_lo = cpu_to_be32(mhp->ibmr.length & 0xffffffff);
694 	fr->tpte.va_hi = cpu_to_be32(mhp->ibmr.iova >> 32);
695 	fr->tpte.va_lo_fbo = cpu_to_be32(mhp->ibmr.iova & 0xffffffff);
696 
697 	p[0] = cpu_to_be64((u64)mhp->mpl[0]);
698 	p[1] = cpu_to_be64((u64)mhp->mpl[1]);
699 
700 	*len16 = DIV_ROUND_UP(sizeof(*fr), 16);
701 }
702 
703 static int build_memreg(struct t4_sq *sq, union t4_wr *wqe,
704 		struct ib_reg_wr *wr, struct c4iw_mr *mhp, u8 *len16,
705 		bool dsgl_supported)
706 {
707 	struct fw_ri_immd *imdp;
708 	__be64 *p;
709 	int i;
710 	int pbllen = roundup(mhp->mpl_len * sizeof(u64), 32);
711 	int rem;
712 
713 	if (mhp->mpl_len > t4_max_fr_depth(use_dsgl && dsgl_supported))
714 		return -EINVAL;
715 
716 	wqe->fr.qpbinde_to_dcacpu = 0;
717 	wqe->fr.pgsz_shift = ilog2(wr->mr->page_size) - 12;
718 	wqe->fr.addr_type = FW_RI_VA_BASED_TO;
719 	wqe->fr.mem_perms = c4iw_ib_to_tpt_access(wr->access);
720 	wqe->fr.len_hi = cpu_to_be32(mhp->ibmr.length >> 32);
721 	wqe->fr.len_lo = cpu_to_be32(mhp->ibmr.length & 0xffffffff);
722 	wqe->fr.stag = cpu_to_be32(wr->key);
723 	wqe->fr.va_hi = cpu_to_be32(mhp->ibmr.iova >> 32);
724 	wqe->fr.va_lo_fbo = cpu_to_be32(mhp->ibmr.iova & 0xffffffff);
725 
726 	if (dsgl_supported && use_dsgl && (pbllen > max_fr_immd)) {
727 		struct fw_ri_dsgl *sglp;
728 
729 		for (i = 0; i < mhp->mpl_len; i++)
730 			mhp->mpl[i] =
731 				     (__force u64)cpu_to_be64((u64)mhp->mpl[i]);
732 
733 		sglp = (struct fw_ri_dsgl *)(&wqe->fr + 1);
734 		sglp->op = FW_RI_DATA_DSGL;
735 		sglp->r1 = 0;
736 		sglp->nsge = cpu_to_be16(1);
737 		sglp->addr0 = cpu_to_be64(mhp->mpl_addr);
738 		sglp->len0 = cpu_to_be32(pbllen);
739 
740 		*len16 = DIV_ROUND_UP(sizeof(wqe->fr) + sizeof(*sglp), 16);
741 	} else {
742 		imdp = (struct fw_ri_immd *)(&wqe->fr + 1);
743 		imdp->op = FW_RI_DATA_IMMD;
744 		imdp->r1 = 0;
745 		imdp->r2 = 0;
746 		imdp->immdlen = cpu_to_be32(pbllen);
747 		p = (__be64 *)(imdp + 1);
748 		rem = pbllen;
749 		for (i = 0; i < mhp->mpl_len; i++) {
750 			*p = cpu_to_be64((u64)mhp->mpl[i]);
751 			rem -= sizeof(*p);
752 			if (++p == (__be64 *)&sq->queue[sq->size])
753 				p = (__be64 *)sq->queue;
754 		}
755 		BUG_ON(rem < 0);
756 		while (rem) {
757 			*p = 0;
758 			rem -= sizeof(*p);
759 			if (++p == (__be64 *)&sq->queue[sq->size])
760 				p = (__be64 *)sq->queue;
761 		}
762 		*len16 = DIV_ROUND_UP(sizeof(wqe->fr) + sizeof(*imdp)
763 				+ pbllen, 16);
764 	}
765 
766 	return 0;
767 }
768 
769 int c4iw_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
770 		   struct ib_send_wr **bad_wr)
771 {
772 	int err = 0;
773 	u8 len16 = 0;
774 	enum fw_wr_opcodes fw_opcode = 0;
775 	enum fw_ri_wr_flags fw_flags;
776 	struct c4iw_qp *qhp;
777 	union t4_wr *wqe = NULL;
778 	u32 num_wrs;
779 	struct t4_swsqe *swsqe;
780 	unsigned long flag;
781 	u16 idx = 0;
782 	struct c4iw_rdev *rdev;
783 
784 	qhp = to_c4iw_qp(ibqp);
785 	rdev = &qhp->rhp->rdev;
786 	spin_lock_irqsave(&qhp->lock, flag);
787 	if (t4_wq_in_error(&qhp->wq)) {
788 		spin_unlock_irqrestore(&qhp->lock, flag);
789 		complete_sq_drain_wr(qhp, wr);
790 		return err;
791 	}
792 	num_wrs = t4_sq_avail(&qhp->wq);
793 	if (num_wrs == 0) {
794 		spin_unlock_irqrestore(&qhp->lock, flag);
795 		*bad_wr = wr;
796 		return -ENOMEM;
797 	}
798 	while (wr) {
799 		if (num_wrs == 0) {
800 			err = -ENOMEM;
801 			*bad_wr = wr;
802 			break;
803 		}
804 		wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
805 		      qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
806 
807 		fw_flags = 0;
808 		if (wr->send_flags & IB_SEND_SOLICITED)
809 			fw_flags |= FW_RI_SOLICITED_EVENT_FLAG;
810 		if (wr->send_flags & IB_SEND_SIGNALED || qhp->sq_sig_all)
811 			fw_flags |= FW_RI_COMPLETION_FLAG;
812 		swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
813 		switch (wr->opcode) {
814 		case IB_WR_SEND_WITH_INV:
815 		case IB_WR_SEND:
816 			if (wr->send_flags & IB_SEND_FENCE)
817 				fw_flags |= FW_RI_READ_FENCE_FLAG;
818 			fw_opcode = FW_RI_SEND_WR;
819 			if (wr->opcode == IB_WR_SEND)
820 				swsqe->opcode = FW_RI_SEND;
821 			else
822 				swsqe->opcode = FW_RI_SEND_WITH_INV;
823 			err = build_rdma_send(&qhp->wq.sq, wqe, wr, &len16);
824 			break;
825 		case IB_WR_RDMA_WRITE:
826 			fw_opcode = FW_RI_RDMA_WRITE_WR;
827 			swsqe->opcode = FW_RI_RDMA_WRITE;
828 			err = build_rdma_write(&qhp->wq.sq, wqe, wr, &len16);
829 			break;
830 		case IB_WR_RDMA_READ:
831 		case IB_WR_RDMA_READ_WITH_INV:
832 			fw_opcode = FW_RI_RDMA_READ_WR;
833 			swsqe->opcode = FW_RI_READ_REQ;
834 			if (wr->opcode == IB_WR_RDMA_READ_WITH_INV) {
835 				c4iw_invalidate_mr(qhp->rhp,
836 						   wr->sg_list[0].lkey);
837 				fw_flags = FW_RI_RDMA_READ_INVALIDATE;
838 			} else {
839 				fw_flags = 0;
840 			}
841 			err = build_rdma_read(wqe, wr, &len16);
842 			if (err)
843 				break;
844 			swsqe->read_len = wr->sg_list[0].length;
845 			if (!qhp->wq.sq.oldest_read)
846 				qhp->wq.sq.oldest_read = swsqe;
847 			break;
848 		case IB_WR_REG_MR: {
849 			struct c4iw_mr *mhp = to_c4iw_mr(reg_wr(wr)->mr);
850 
851 			swsqe->opcode = FW_RI_FAST_REGISTER;
852 			if (rdev->adap->params.fr_nsmr_tpte_wr_support &&
853 					!mhp->attr.state && mhp->mpl_len <= 2) {
854 				fw_opcode = FW_RI_FR_NSMR_TPTE_WR;
855 				build_tpte_memreg(&wqe->fr_tpte, reg_wr(wr),
856 						mhp, &len16);
857 			} else {
858 				fw_opcode = FW_RI_FR_NSMR_WR;
859 				err = build_memreg(&qhp->wq.sq, wqe, reg_wr(wr),
860 					mhp, &len16,
861 					rdev->adap->params.ulptx_memwrite_dsgl);
862 				if (err)
863 					break;
864 			}
865 			mhp->attr.state = 1;
866 			break;
867 		}
868 		case IB_WR_LOCAL_INV:
869 			if (wr->send_flags & IB_SEND_FENCE)
870 				fw_flags |= FW_RI_LOCAL_FENCE_FLAG;
871 			fw_opcode = FW_RI_INV_LSTAG_WR;
872 			swsqe->opcode = FW_RI_LOCAL_INV;
873 			err = build_inv_stag(wqe, wr, &len16);
874 			c4iw_invalidate_mr(qhp->rhp, wr->ex.invalidate_rkey);
875 			break;
876 		default:
877 			CTR2(KTR_IW_CXGBE, "%s post of type =%d TBD!", __func__,
878 			     wr->opcode);
879 			err = -EINVAL;
880 		}
881 		if (err) {
882 			*bad_wr = wr;
883 			break;
884 		}
885 		swsqe->idx = qhp->wq.sq.pidx;
886 		swsqe->complete = 0;
887 		swsqe->signaled = (wr->send_flags & IB_SEND_SIGNALED) ||
888 					qhp->sq_sig_all;
889 		swsqe->flushed = 0;
890 		swsqe->wr_id = wr->wr_id;
891 
892 		init_wr_hdr(wqe, qhp->wq.sq.pidx, fw_opcode, fw_flags, len16);
893 
894 		CTR5(KTR_IW_CXGBE,
895 		    "%s cookie 0x%llx pidx 0x%x opcode 0x%x read_len %u",
896 		    __func__, (unsigned long long)wr->wr_id, qhp->wq.sq.pidx,
897 		    swsqe->opcode, swsqe->read_len);
898 		wr = wr->next;
899 		num_wrs--;
900 		t4_sq_produce(&qhp->wq, len16);
901 		idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
902 	}
903 
904 	t4_ring_sq_db(&qhp->wq, idx, wqe, rdev->adap->iwt.wc_en);
905 	spin_unlock_irqrestore(&qhp->lock, flag);
906 	return err;
907 }
908 
909 int c4iw_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
910 		      struct ib_recv_wr **bad_wr)
911 {
912 	int err = 0;
913 	struct c4iw_qp *qhp;
914 	union t4_recv_wr *wqe = NULL;
915 	u32 num_wrs;
916 	u8 len16 = 0;
917 	unsigned long flag;
918 	u16 idx = 0;
919 
920 	qhp = to_c4iw_qp(ibqp);
921 	spin_lock_irqsave(&qhp->lock, flag);
922 	if (t4_wq_in_error(&qhp->wq)) {
923 		spin_unlock_irqrestore(&qhp->lock, flag);
924 		complete_rq_drain_wr(qhp, wr);
925 		return err;
926 	}
927 	num_wrs = t4_rq_avail(&qhp->wq);
928 	if (num_wrs == 0) {
929 		spin_unlock_irqrestore(&qhp->lock, flag);
930 		*bad_wr = wr;
931 		return -ENOMEM;
932 	}
933 	while (wr) {
934 		if (wr->num_sge > T4_MAX_RECV_SGE) {
935 			err = -EINVAL;
936 			*bad_wr = wr;
937 			break;
938 		}
939 		wqe = (union t4_recv_wr *)((u8 *)qhp->wq.rq.queue +
940 					   qhp->wq.rq.wq_pidx *
941 					   T4_EQ_ENTRY_SIZE);
942 		if (num_wrs)
943 			err = build_rdma_recv(qhp, wqe, wr, &len16);
944 		else
945 			err = -ENOMEM;
946 		if (err) {
947 			*bad_wr = wr;
948 			break;
949 		}
950 
951 		qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].wr_id = wr->wr_id;
952 
953 		wqe->recv.opcode = FW_RI_RECV_WR;
954 		wqe->recv.r1 = 0;
955 		wqe->recv.wrid = qhp->wq.rq.pidx;
956 		wqe->recv.r2[0] = 0;
957 		wqe->recv.r2[1] = 0;
958 		wqe->recv.r2[2] = 0;
959 		wqe->recv.len16 = len16;
960 		CTR3(KTR_IW_CXGBE, "%s cookie 0x%llx pidx %u", __func__,
961 		     (unsigned long long) wr->wr_id, qhp->wq.rq.pidx);
962 		t4_rq_produce(&qhp->wq, len16);
963 		idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
964 		wr = wr->next;
965 		num_wrs--;
966 	}
967 
968 	t4_ring_rq_db(&qhp->wq, idx, wqe, qhp->rhp->rdev.adap->iwt.wc_en);
969 	spin_unlock_irqrestore(&qhp->lock, flag);
970 	return err;
971 }
972 
973 static inline void build_term_codes(struct t4_cqe *err_cqe, u8 *layer_type,
974 				    u8 *ecode)
975 {
976 	int status;
977 	int tagged;
978 	int opcode;
979 	int rqtype;
980 	int send_inv;
981 
982 	if (!err_cqe) {
983 		*layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
984 		*ecode = 0;
985 		return;
986 	}
987 
988 	status = CQE_STATUS(err_cqe);
989 	opcode = CQE_OPCODE(err_cqe);
990 	rqtype = RQ_TYPE(err_cqe);
991 	send_inv = (opcode == FW_RI_SEND_WITH_INV) ||
992 		   (opcode == FW_RI_SEND_WITH_SE_INV);
993 	tagged = (opcode == FW_RI_RDMA_WRITE) ||
994 		 (rqtype && (opcode == FW_RI_READ_RESP));
995 
996 	switch (status) {
997 	case T4_ERR_STAG:
998 		if (send_inv) {
999 			*layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1000 			*ecode = RDMAP_CANT_INV_STAG;
1001 		} else {
1002 			*layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1003 			*ecode = RDMAP_INV_STAG;
1004 		}
1005 		break;
1006 	case T4_ERR_PDID:
1007 		*layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1008 		if ((opcode == FW_RI_SEND_WITH_INV) ||
1009 		    (opcode == FW_RI_SEND_WITH_SE_INV))
1010 			*ecode = RDMAP_CANT_INV_STAG;
1011 		else
1012 			*ecode = RDMAP_STAG_NOT_ASSOC;
1013 		break;
1014 	case T4_ERR_QPID:
1015 		*layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1016 		*ecode = RDMAP_STAG_NOT_ASSOC;
1017 		break;
1018 	case T4_ERR_ACCESS:
1019 		*layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1020 		*ecode = RDMAP_ACC_VIOL;
1021 		break;
1022 	case T4_ERR_WRAP:
1023 		*layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1024 		*ecode = RDMAP_TO_WRAP;
1025 		break;
1026 	case T4_ERR_BOUND:
1027 		if (tagged) {
1028 			*layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1029 			*ecode = DDPT_BASE_BOUNDS;
1030 		} else {
1031 			*layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1032 			*ecode = RDMAP_BASE_BOUNDS;
1033 		}
1034 		break;
1035 	case T4_ERR_INVALIDATE_SHARED_MR:
1036 	case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
1037 		*layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1038 		*ecode = RDMAP_CANT_INV_STAG;
1039 		break;
1040 	case T4_ERR_ECC:
1041 	case T4_ERR_ECC_PSTAG:
1042 	case T4_ERR_INTERNAL_ERR:
1043 		*layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
1044 		*ecode = 0;
1045 		break;
1046 	case T4_ERR_OUT_OF_RQE:
1047 		*layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1048 		*ecode = DDPU_INV_MSN_NOBUF;
1049 		break;
1050 	case T4_ERR_PBL_ADDR_BOUND:
1051 		*layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1052 		*ecode = DDPT_BASE_BOUNDS;
1053 		break;
1054 	case T4_ERR_CRC:
1055 		*layer_type = LAYER_MPA|DDP_LLP;
1056 		*ecode = MPA_CRC_ERR;
1057 		break;
1058 	case T4_ERR_MARKER:
1059 		*layer_type = LAYER_MPA|DDP_LLP;
1060 		*ecode = MPA_MARKER_ERR;
1061 		break;
1062 	case T4_ERR_PDU_LEN_ERR:
1063 		*layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1064 		*ecode = DDPU_MSG_TOOBIG;
1065 		break;
1066 	case T4_ERR_DDP_VERSION:
1067 		if (tagged) {
1068 			*layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1069 			*ecode = DDPT_INV_VERS;
1070 		} else {
1071 			*layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1072 			*ecode = DDPU_INV_VERS;
1073 		}
1074 		break;
1075 	case T4_ERR_RDMA_VERSION:
1076 		*layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1077 		*ecode = RDMAP_INV_VERS;
1078 		break;
1079 	case T4_ERR_OPCODE:
1080 		*layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1081 		*ecode = RDMAP_INV_OPCODE;
1082 		break;
1083 	case T4_ERR_DDP_QUEUE_NUM:
1084 		*layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1085 		*ecode = DDPU_INV_QN;
1086 		break;
1087 	case T4_ERR_MSN:
1088 	case T4_ERR_MSN_GAP:
1089 	case T4_ERR_MSN_RANGE:
1090 	case T4_ERR_IRD_OVERFLOW:
1091 		*layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1092 		*ecode = DDPU_INV_MSN_RANGE;
1093 		break;
1094 	case T4_ERR_TBIT:
1095 		*layer_type = LAYER_DDP|DDP_LOCAL_CATA;
1096 		*ecode = 0;
1097 		break;
1098 	case T4_ERR_MO:
1099 		*layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1100 		*ecode = DDPU_INV_MO;
1101 		break;
1102 	default:
1103 		*layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
1104 		*ecode = 0;
1105 		break;
1106 	}
1107 }
1108 
1109 static void post_terminate(struct c4iw_qp *qhp, struct t4_cqe *err_cqe,
1110 			   gfp_t gfp)
1111 {
1112 	int ret;
1113 	struct fw_ri_wr *wqe;
1114 	struct terminate_message *term;
1115 	struct wrqe *wr;
1116 	struct socket *so = qhp->ep->com.so;
1117         struct inpcb *inp = sotoinpcb(so);
1118         struct tcpcb *tp = intotcpcb(inp);
1119         struct toepcb *toep = tp->t_toe;
1120 
1121 	CTR4(KTR_IW_CXGBE, "%s qhp %p qid 0x%x tid %u", __func__, qhp,
1122 	    qhp->wq.sq.qid, qhp->ep->hwtid);
1123 
1124 	wr = alloc_wrqe(sizeof(*wqe), toep->ofld_txq);
1125 	if (wr == NULL)
1126 		return;
1127         wqe = wrtod(wr);
1128 
1129 	memset(wqe, 0, sizeof *wqe);
1130 	wqe->op_compl = cpu_to_be32(V_FW_WR_OP(FW_RI_WR));
1131 	wqe->flowid_len16 = cpu_to_be32(
1132 		V_FW_WR_FLOWID(qhp->ep->hwtid) |
1133 		V_FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
1134 
1135 	wqe->u.terminate.type = FW_RI_TYPE_TERMINATE;
1136 	wqe->u.terminate.immdlen = cpu_to_be32(sizeof *term);
1137 	term = (struct terminate_message *)wqe->u.terminate.termmsg;
1138 	if (qhp->attr.layer_etype == (LAYER_MPA|DDP_LLP)) {
1139 		term->layer_etype = qhp->attr.layer_etype;
1140 		term->ecode = qhp->attr.ecode;
1141 	} else
1142 		build_term_codes(err_cqe, &term->layer_etype, &term->ecode);
1143 	ret = creds(toep, inp, sizeof(*wqe));
1144 	if (ret) {
1145 		free_wrqe(wr);
1146 		return;
1147 	}
1148 	t4_wrq_tx(qhp->rhp->rdev.adap, wr);
1149 }
1150 
1151 /* Assumes qhp lock is held. */
1152 static void __flush_qp(struct c4iw_qp *qhp, struct c4iw_cq *rchp,
1153 		       struct c4iw_cq *schp)
1154 {
1155 	int count;
1156 	int rq_flushed, sq_flushed;
1157 	unsigned long flag;
1158 
1159 	CTR4(KTR_IW_CXGBE, "%s qhp %p rchp %p schp %p", __func__, qhp, rchp,
1160 	    schp);
1161 
1162 	/* locking hierarchy: cq lock first, then qp lock. */
1163 	spin_lock_irqsave(&rchp->lock, flag);
1164 	spin_lock(&qhp->lock);
1165 
1166 	if (qhp->wq.flushed) {
1167 		spin_unlock(&qhp->lock);
1168 		spin_unlock_irqrestore(&rchp->lock, flag);
1169 		return;
1170 	}
1171 	qhp->wq.flushed = 1;
1172 
1173 	c4iw_flush_hw_cq(rchp);
1174 	c4iw_count_rcqes(&rchp->cq, &qhp->wq, &count);
1175 	rq_flushed = c4iw_flush_rq(&qhp->wq, &rchp->cq, count);
1176 	spin_unlock(&qhp->lock);
1177 	spin_unlock_irqrestore(&rchp->lock, flag);
1178 
1179 	/* locking hierarchy: cq lock first, then qp lock. */
1180 	spin_lock_irqsave(&schp->lock, flag);
1181 	spin_lock(&qhp->lock);
1182 	if (schp != rchp)
1183 		c4iw_flush_hw_cq(schp);
1184 	sq_flushed = c4iw_flush_sq(qhp);
1185 	spin_unlock(&qhp->lock);
1186 	spin_unlock_irqrestore(&schp->lock, flag);
1187 
1188 	if (schp == rchp) {
1189 		if (t4_clear_cq_armed(&rchp->cq) &&
1190 		    (rq_flushed || sq_flushed)) {
1191 			spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1192 			(*rchp->ibcq.comp_handler)(&rchp->ibcq,
1193 						   rchp->ibcq.cq_context);
1194 			spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1195 		}
1196 	} else {
1197 		if (t4_clear_cq_armed(&rchp->cq) && rq_flushed) {
1198 			spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1199 			(*rchp->ibcq.comp_handler)(&rchp->ibcq,
1200 						   rchp->ibcq.cq_context);
1201 			spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1202 		}
1203 		if (t4_clear_cq_armed(&schp->cq) && sq_flushed) {
1204 			spin_lock_irqsave(&schp->comp_handler_lock, flag);
1205 			(*schp->ibcq.comp_handler)(&schp->ibcq,
1206 						   schp->ibcq.cq_context);
1207 			spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
1208 		}
1209 	}
1210 }
1211 
1212 static void flush_qp(struct c4iw_qp *qhp)
1213 {
1214 	struct c4iw_cq *rchp, *schp;
1215 	unsigned long flag;
1216 
1217 	rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
1218 	schp = to_c4iw_cq(qhp->ibqp.send_cq);
1219 
1220 	t4_set_wq_in_error(&qhp->wq);
1221 	if (qhp->ibqp.uobject) {
1222 		t4_set_cq_in_error(&rchp->cq);
1223 		spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1224 		(*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
1225 		spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1226 		if (schp != rchp) {
1227 			t4_set_cq_in_error(&schp->cq);
1228 			spin_lock_irqsave(&schp->comp_handler_lock, flag);
1229 			(*schp->ibcq.comp_handler)(&schp->ibcq,
1230 					schp->ibcq.cq_context);
1231 			spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
1232 		}
1233 		return;
1234 	}
1235 	__flush_qp(qhp, rchp, schp);
1236 }
1237 
1238 static int
1239 rdma_fini(struct c4iw_dev *rhp, struct c4iw_qp *qhp, struct c4iw_ep *ep)
1240 {
1241 	struct c4iw_rdev *rdev = &rhp->rdev;
1242 	struct adapter *sc = rdev->adap;
1243 	struct fw_ri_wr *wqe;
1244 	int ret;
1245 	struct wrqe *wr;
1246 	struct socket *so = ep->com.so;
1247         struct inpcb *inp = sotoinpcb(so);
1248         struct tcpcb *tp = intotcpcb(inp);
1249         struct toepcb *toep = tp->t_toe;
1250 
1251 	KASSERT(rhp == qhp->rhp && ep == qhp->ep, ("%s: EDOOFUS", __func__));
1252 
1253 	CTR5(KTR_IW_CXGBE, "%s qhp %p qid 0x%x ep %p tid %u", __func__, qhp,
1254 	    qhp->wq.sq.qid, ep, ep->hwtid);
1255 
1256 	wr = alloc_wrqe(sizeof(*wqe), toep->ofld_txq);
1257 	if (wr == NULL)
1258 		return (0);
1259 	wqe = wrtod(wr);
1260 
1261 	memset(wqe, 0, sizeof *wqe);
1262 
1263 	wqe->op_compl = cpu_to_be32(V_FW_WR_OP(FW_RI_WR) | F_FW_WR_COMPL);
1264 	wqe->flowid_len16 = cpu_to_be32(V_FW_WR_FLOWID(ep->hwtid) |
1265 	    V_FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
1266 	wqe->cookie = (unsigned long) &ep->com.wr_wait;
1267 	wqe->u.fini.type = FW_RI_TYPE_FINI;
1268 
1269 	c4iw_init_wr_wait(&ep->com.wr_wait);
1270 
1271 	ret = creds(toep, inp, sizeof(*wqe));
1272 	if (ret) {
1273 		free_wrqe(wr);
1274 		return ret;
1275 	}
1276 	t4_wrq_tx(sc, wr);
1277 
1278 	ret = c4iw_wait_for_reply(rdev, &ep->com.wr_wait, ep->hwtid,
1279 			qhp->wq.sq.qid, ep->com.so, __func__);
1280 	return ret;
1281 }
1282 
1283 static void build_rtr_msg(u8 p2p_type, struct fw_ri_init *init)
1284 {
1285 	CTR2(KTR_IW_CXGBE, "%s p2p_type = %d", __func__, p2p_type);
1286 	memset(&init->u, 0, sizeof init->u);
1287 	switch (p2p_type) {
1288 	case FW_RI_INIT_P2PTYPE_RDMA_WRITE:
1289 		init->u.write.opcode = FW_RI_RDMA_WRITE_WR;
1290 		init->u.write.stag_sink = cpu_to_be32(1);
1291 		init->u.write.to_sink = cpu_to_be64(1);
1292 		init->u.write.u.immd_src[0].op = FW_RI_DATA_IMMD;
1293 		init->u.write.len16 = DIV_ROUND_UP(sizeof init->u.write +
1294 						   sizeof(struct fw_ri_immd),
1295 						   16);
1296 		break;
1297 	case FW_RI_INIT_P2PTYPE_READ_REQ:
1298 		init->u.write.opcode = FW_RI_RDMA_READ_WR;
1299 		init->u.read.stag_src = cpu_to_be32(1);
1300 		init->u.read.to_src_lo = cpu_to_be32(1);
1301 		init->u.read.stag_sink = cpu_to_be32(1);
1302 		init->u.read.to_sink_lo = cpu_to_be32(1);
1303 		init->u.read.len16 = DIV_ROUND_UP(sizeof init->u.read, 16);
1304 		break;
1305 	}
1306 }
1307 
1308 static int
1309 creds(struct toepcb *toep, struct inpcb *inp, size_t wrsize)
1310 {
1311 	struct ofld_tx_sdesc *txsd;
1312 
1313 	CTR3(KTR_IW_CXGBE, "%s:creB  %p %u", __func__, toep , wrsize);
1314 	INP_WLOCK(inp);
1315 	if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) != 0) {
1316 		INP_WUNLOCK(inp);
1317 		return (EINVAL);
1318 	}
1319 	txsd = &toep->txsd[toep->txsd_pidx];
1320 	txsd->tx_credits = howmany(wrsize, 16);
1321 	txsd->plen = 0;
1322 	KASSERT(toep->tx_credits >= txsd->tx_credits && toep->txsd_avail > 0,
1323 			("%s: not enough credits (%d)", __func__, toep->tx_credits));
1324 	toep->tx_credits -= txsd->tx_credits;
1325 	if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
1326 		toep->txsd_pidx = 0;
1327 	toep->txsd_avail--;
1328 	INP_WUNLOCK(inp);
1329 	CTR5(KTR_IW_CXGBE, "%s:creE  %p %u %u %u", __func__, toep ,
1330 	    txsd->tx_credits, toep->tx_credits, toep->txsd_pidx);
1331 	return (0);
1332 }
1333 
1334 static int rdma_init(struct c4iw_dev *rhp, struct c4iw_qp *qhp)
1335 {
1336 	struct fw_ri_wr *wqe;
1337 	int ret;
1338 	struct wrqe *wr;
1339 	struct c4iw_ep *ep = qhp->ep;
1340 	struct c4iw_rdev *rdev = &qhp->rhp->rdev;
1341 	struct adapter *sc = rdev->adap;
1342 	struct socket *so = ep->com.so;
1343         struct inpcb *inp = sotoinpcb(so);
1344         struct tcpcb *tp = intotcpcb(inp);
1345         struct toepcb *toep = tp->t_toe;
1346 
1347 	CTR5(KTR_IW_CXGBE, "%s qhp %p qid 0x%x ep %p tid %u", __func__, qhp,
1348 	    qhp->wq.sq.qid, ep, ep->hwtid);
1349 
1350 	wr = alloc_wrqe(sizeof(*wqe), toep->ofld_txq);
1351 	if (wr == NULL)
1352 		return (0);
1353 	wqe = wrtod(wr);
1354 	ret = alloc_ird(rhp, qhp->attr.max_ird);
1355 	if (ret) {
1356 		qhp->attr.max_ird = 0;
1357 		free_wrqe(wr);
1358 		return ret;
1359 	}
1360 
1361 	memset(wqe, 0, sizeof *wqe);
1362 
1363 	wqe->op_compl = cpu_to_be32(
1364 		V_FW_WR_OP(FW_RI_WR) |
1365 		F_FW_WR_COMPL);
1366 	wqe->flowid_len16 = cpu_to_be32(V_FW_WR_FLOWID(ep->hwtid) |
1367 	    V_FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
1368 
1369 	wqe->cookie = (unsigned long) &ep->com.wr_wait;
1370 
1371 	wqe->u.init.type = FW_RI_TYPE_INIT;
1372 	wqe->u.init.mpareqbit_p2ptype =
1373 		V_FW_RI_WR_MPAREQBIT(qhp->attr.mpa_attr.initiator) |
1374 		V_FW_RI_WR_P2PTYPE(qhp->attr.mpa_attr.p2p_type);
1375 	wqe->u.init.mpa_attrs = FW_RI_MPA_IETF_ENABLE;
1376 	if (qhp->attr.mpa_attr.recv_marker_enabled)
1377 		wqe->u.init.mpa_attrs |= FW_RI_MPA_RX_MARKER_ENABLE;
1378 	if (qhp->attr.mpa_attr.xmit_marker_enabled)
1379 		wqe->u.init.mpa_attrs |= FW_RI_MPA_TX_MARKER_ENABLE;
1380 	if (qhp->attr.mpa_attr.crc_enabled)
1381 		wqe->u.init.mpa_attrs |= FW_RI_MPA_CRC_ENABLE;
1382 
1383 	wqe->u.init.qp_caps = FW_RI_QP_RDMA_READ_ENABLE |
1384 			    FW_RI_QP_RDMA_WRITE_ENABLE |
1385 			    FW_RI_QP_BIND_ENABLE;
1386 	if (!qhp->ibqp.uobject)
1387 		wqe->u.init.qp_caps |= FW_RI_QP_FAST_REGISTER_ENABLE |
1388 				     FW_RI_QP_STAG0_ENABLE;
1389 	wqe->u.init.nrqe = cpu_to_be16(t4_rqes_posted(&qhp->wq));
1390 	wqe->u.init.pdid = cpu_to_be32(qhp->attr.pd);
1391 	wqe->u.init.qpid = cpu_to_be32(qhp->wq.sq.qid);
1392 	wqe->u.init.sq_eqid = cpu_to_be32(qhp->wq.sq.qid);
1393 	wqe->u.init.rq_eqid = cpu_to_be32(qhp->wq.rq.qid);
1394 	wqe->u.init.scqid = cpu_to_be32(qhp->attr.scq);
1395 	wqe->u.init.rcqid = cpu_to_be32(qhp->attr.rcq);
1396 	wqe->u.init.ord_max = cpu_to_be32(qhp->attr.max_ord);
1397 	wqe->u.init.ird_max = cpu_to_be32(qhp->attr.max_ird);
1398 	wqe->u.init.iss = cpu_to_be32(ep->snd_seq);
1399 	wqe->u.init.irs = cpu_to_be32(ep->rcv_seq);
1400 	wqe->u.init.hwrqsize = cpu_to_be32(qhp->wq.rq.rqt_size);
1401 	wqe->u.init.hwrqaddr = cpu_to_be32(qhp->wq.rq.rqt_hwaddr -
1402 	    sc->vres.rq.start);
1403 	if (qhp->attr.mpa_attr.initiator)
1404 		build_rtr_msg(qhp->attr.mpa_attr.p2p_type, &wqe->u.init);
1405 
1406 	c4iw_init_wr_wait(&ep->com.wr_wait);
1407 
1408 	ret = creds(toep, inp, sizeof(*wqe));
1409 	if (ret) {
1410 		free_wrqe(wr);
1411 		free_ird(rhp, qhp->attr.max_ird);
1412 		return ret;
1413 	}
1414 	t4_wrq_tx(sc, wr);
1415 
1416 	ret = c4iw_wait_for_reply(rdev, &ep->com.wr_wait, ep->hwtid,
1417 			qhp->wq.sq.qid, ep->com.so, __func__);
1418 
1419 	toep->params.ulp_mode = ULP_MODE_RDMA;
1420 	free_ird(rhp, qhp->attr.max_ird);
1421 
1422 	return ret;
1423 }
1424 
1425 int c4iw_modify_qp(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
1426 		   enum c4iw_qp_attr_mask mask,
1427 		   struct c4iw_qp_attributes *attrs,
1428 		   int internal)
1429 {
1430 	int ret = 0;
1431 	struct c4iw_qp_attributes newattr = qhp->attr;
1432 	int disconnect = 0;
1433 	int terminate = 0;
1434 	int abort = 0;
1435 	int free = 0;
1436 	struct c4iw_ep *ep = NULL;
1437 
1438 	CTR5(KTR_IW_CXGBE, "%s qhp %p sqid 0x%x rqid 0x%x ep %p", __func__, qhp,
1439 	    qhp->wq.sq.qid, qhp->wq.rq.qid, qhp->ep);
1440 	CTR3(KTR_IW_CXGBE, "%s state %d -> %d", __func__, qhp->attr.state,
1441 	    (mask & C4IW_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
1442 
1443 	mutex_lock(&qhp->mutex);
1444 
1445 	/* Process attr changes if in IDLE */
1446 	if (mask & C4IW_QP_ATTR_VALID_MODIFY) {
1447 		if (qhp->attr.state != C4IW_QP_STATE_IDLE) {
1448 			ret = -EIO;
1449 			goto out;
1450 		}
1451 		if (mask & C4IW_QP_ATTR_ENABLE_RDMA_READ)
1452 			newattr.enable_rdma_read = attrs->enable_rdma_read;
1453 		if (mask & C4IW_QP_ATTR_ENABLE_RDMA_WRITE)
1454 			newattr.enable_rdma_write = attrs->enable_rdma_write;
1455 		if (mask & C4IW_QP_ATTR_ENABLE_RDMA_BIND)
1456 			newattr.enable_bind = attrs->enable_bind;
1457 		if (mask & C4IW_QP_ATTR_MAX_ORD) {
1458 			if (attrs->max_ord > c4iw_max_read_depth) {
1459 				ret = -EINVAL;
1460 				goto out;
1461 			}
1462 			newattr.max_ord = attrs->max_ord;
1463 		}
1464 		if (mask & C4IW_QP_ATTR_MAX_IRD) {
1465 			if (attrs->max_ird > cur_max_read_depth(rhp)) {
1466 				ret = -EINVAL;
1467 				goto out;
1468 			}
1469 			newattr.max_ird = attrs->max_ird;
1470 		}
1471 		qhp->attr = newattr;
1472 	}
1473 
1474 	if (!(mask & C4IW_QP_ATTR_NEXT_STATE))
1475 		goto out;
1476 	if (qhp->attr.state == attrs->next_state)
1477 		goto out;
1478 
1479 	/* Return EINPROGRESS if QP is already in transition state.
1480 	 * Eg: CLOSING->IDLE transition or *->ERROR transition.
1481 	 * This can happen while connection is switching(due to rdma_fini)
1482 	 * from iWARP/RDDP to TOE mode and any inflight RDMA RX data will
1483 	 * reach TOE driver -> TCP stack -> iWARP driver. In this way
1484 	 * iWARP driver keep receiving inflight RDMA RX data until socket
1485 	 * is closed or aborted. And if iWARP CM is in FPDU sate, then
1486 	 * it tries to put QP in TERM state and disconnects endpoint.
1487 	 * But as QP is already in transition state, this event is ignored.
1488 	 */
1489 	if ((qhp->attr.state >= C4IW_QP_STATE_ERROR) &&
1490 		(attrs->next_state == C4IW_QP_STATE_TERMINATE)) {
1491 		ret = -EINPROGRESS;
1492 		goto out;
1493 	}
1494 
1495 	switch (qhp->attr.state) {
1496 	case C4IW_QP_STATE_IDLE:
1497 		switch (attrs->next_state) {
1498 		case C4IW_QP_STATE_RTS:
1499 			if (!(mask & C4IW_QP_ATTR_LLP_STREAM_HANDLE)) {
1500 				ret = -EINVAL;
1501 				goto out;
1502 			}
1503 			if (!(mask & C4IW_QP_ATTR_MPA_ATTR)) {
1504 				ret = -EINVAL;
1505 				goto out;
1506 			}
1507 			qhp->attr.mpa_attr = attrs->mpa_attr;
1508 			qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
1509 			qhp->ep = qhp->attr.llp_stream_handle;
1510 			set_state(qhp, C4IW_QP_STATE_RTS);
1511 
1512 			/*
1513 			 * Ref the endpoint here and deref when we
1514 			 * disassociate the endpoint from the QP.  This
1515 			 * happens in CLOSING->IDLE transition or *->ERROR
1516 			 * transition.
1517 			 */
1518 			c4iw_get_ep(&qhp->ep->com);
1519 			ret = rdma_init(rhp, qhp);
1520 			if (ret)
1521 				goto err;
1522 			break;
1523 		case C4IW_QP_STATE_ERROR:
1524 			set_state(qhp, C4IW_QP_STATE_ERROR);
1525 			flush_qp(qhp);
1526 			break;
1527 		default:
1528 			ret = -EINVAL;
1529 			goto out;
1530 		}
1531 		break;
1532 	case C4IW_QP_STATE_RTS:
1533 		switch (attrs->next_state) {
1534 		case C4IW_QP_STATE_CLOSING:
1535 			BUG_ON(atomic_read(&qhp->ep->com.kref.refcount) < 2);
1536 			t4_set_wq_in_error(&qhp->wq);
1537 			set_state(qhp, C4IW_QP_STATE_CLOSING);
1538 			ep = qhp->ep;
1539 			if (!internal) {
1540 				abort = 0;
1541 				disconnect = 1;
1542 				c4iw_get_ep(&qhp->ep->com);
1543 			}
1544 			ret = rdma_fini(rhp, qhp, ep);
1545 			if (ret)
1546 				goto err;
1547 			break;
1548 		case C4IW_QP_STATE_TERMINATE:
1549 			t4_set_wq_in_error(&qhp->wq);
1550 			set_state(qhp, C4IW_QP_STATE_TERMINATE);
1551 			qhp->attr.layer_etype = attrs->layer_etype;
1552 			qhp->attr.ecode = attrs->ecode;
1553 			ep = qhp->ep;
1554 			if (!internal) {
1555 				c4iw_get_ep(&qhp->ep->com);
1556 				terminate = 1;
1557 				disconnect = 1;
1558 			} else {
1559 				terminate = qhp->attr.send_term;
1560 				ret = rdma_fini(rhp, qhp, ep);
1561 				if (ret)
1562 					goto err;
1563 			}
1564 			break;
1565 		case C4IW_QP_STATE_ERROR:
1566 			t4_set_wq_in_error(&qhp->wq);
1567 			set_state(qhp, C4IW_QP_STATE_ERROR);
1568 			if (!internal) {
1569 				abort = 1;
1570 				disconnect = 1;
1571 				ep = qhp->ep;
1572 				c4iw_get_ep(&qhp->ep->com);
1573 			}
1574 			goto err;
1575 			break;
1576 		default:
1577 			ret = -EINVAL;
1578 			goto out;
1579 		}
1580 		break;
1581 	case C4IW_QP_STATE_CLOSING:
1582 
1583 		/*
1584 		 * Allow kernel users to move to ERROR for qp draining.
1585 		 */
1586 		if (!internal && (qhp->ibqp.uobject || attrs->next_state !=
1587 				  C4IW_QP_STATE_ERROR)) {
1588 			ret = -EINVAL;
1589 			goto out;
1590 		}
1591 		switch (attrs->next_state) {
1592 		case C4IW_QP_STATE_IDLE:
1593 			flush_qp(qhp);
1594 			set_state(qhp, C4IW_QP_STATE_IDLE);
1595 			qhp->attr.llp_stream_handle = NULL;
1596 			c4iw_put_ep(&qhp->ep->com);
1597 			qhp->ep = NULL;
1598 			wake_up(&qhp->wait);
1599 			break;
1600 		case C4IW_QP_STATE_ERROR:
1601 			goto err;
1602 		default:
1603 			ret = -EINVAL;
1604 			goto err;
1605 		}
1606 		break;
1607 	case C4IW_QP_STATE_ERROR:
1608 		if (attrs->next_state != C4IW_QP_STATE_IDLE) {
1609 			ret = -EINVAL;
1610 			goto out;
1611 		}
1612 		if (!t4_sq_empty(&qhp->wq) || !t4_rq_empty(&qhp->wq)) {
1613 			ret = -EINVAL;
1614 			goto out;
1615 		}
1616 		set_state(qhp, C4IW_QP_STATE_IDLE);
1617 		break;
1618 	case C4IW_QP_STATE_TERMINATE:
1619 		if (!internal) {
1620 			ret = -EINVAL;
1621 			goto out;
1622 		}
1623 		goto err;
1624 		break;
1625 	default:
1626 		printf("%s in a bad state %d\n",
1627 		       __func__, qhp->attr.state);
1628 		ret = -EINVAL;
1629 		goto err;
1630 		break;
1631 	}
1632 	goto out;
1633 err:
1634 	CTR3(KTR_IW_CXGBE, "%s disassociating ep %p qpid 0x%x", __func__,
1635 	    qhp->ep, qhp->wq.sq.qid);
1636 
1637 	/* disassociate the LLP connection */
1638 	qhp->attr.llp_stream_handle = NULL;
1639 	if (!ep)
1640 		ep = qhp->ep;
1641 	qhp->ep = NULL;
1642 	set_state(qhp, C4IW_QP_STATE_ERROR);
1643 	free = 1;
1644 	abort = 1;
1645 	BUG_ON(!ep);
1646 	flush_qp(qhp);
1647 	wake_up(&qhp->wait);
1648 out:
1649 	mutex_unlock(&qhp->mutex);
1650 
1651 	if (terminate)
1652 		post_terminate(qhp, NULL, internal ? GFP_ATOMIC : GFP_KERNEL);
1653 
1654 	/*
1655 	 * If disconnect is 1, then we need to initiate a disconnect
1656 	 * on the EP.  This can be a normal close (RTS->CLOSING) or
1657 	 * an abnormal close (RTS/CLOSING->ERROR).
1658 	 */
1659 	if (disconnect) {
1660 		__c4iw_ep_disconnect(ep, abort, internal ? GFP_ATOMIC :
1661 							 GFP_KERNEL);
1662 		c4iw_put_ep(&ep->com);
1663 	}
1664 
1665 	/*
1666 	 * If free is 1, then we've disassociated the EP from the QP
1667 	 * and we need to dereference the EP.
1668 	 */
1669 	if (free)
1670 		c4iw_put_ep(&ep->com);
1671 	CTR2(KTR_IW_CXGBE, "%s exit state %d", __func__, qhp->attr.state);
1672 	return ret;
1673 }
1674 
1675 int c4iw_destroy_qp(struct ib_qp *ib_qp)
1676 {
1677 	struct c4iw_dev *rhp;
1678 	struct c4iw_qp *qhp;
1679 	struct c4iw_qp_attributes attrs;
1680 
1681 	CTR2(KTR_IW_CXGBE, "%s ib_qp %p", __func__, ib_qp);
1682 	qhp = to_c4iw_qp(ib_qp);
1683 	rhp = qhp->rhp;
1684 
1685 	attrs.next_state = C4IW_QP_STATE_ERROR;
1686 	if (qhp->attr.state == C4IW_QP_STATE_TERMINATE)
1687 		c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1688 	else
1689 		c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
1690 	wait_event(qhp->wait, !qhp->ep);
1691 
1692 	remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
1693 
1694 	free_ird(rhp, qhp->attr.max_ird);
1695 	c4iw_qp_rem_ref(ib_qp);
1696 
1697 	CTR3(KTR_IW_CXGBE, "%s ib_qp %p qpid 0x%0x", __func__, ib_qp,
1698 	    qhp->wq.sq.qid);
1699 	return 0;
1700 }
1701 
1702 struct ib_qp *
1703 c4iw_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
1704     struct ib_udata *udata)
1705 {
1706 	struct c4iw_dev *rhp;
1707 	struct c4iw_qp *qhp;
1708 	struct c4iw_pd *php;
1709 	struct c4iw_cq *schp;
1710 	struct c4iw_cq *rchp;
1711 	struct c4iw_create_qp_resp uresp;
1712 	unsigned int sqsize, rqsize;
1713 	struct c4iw_ucontext *ucontext;
1714 	int ret;
1715 	struct c4iw_mm_entry *sq_key_mm = NULL, *rq_key_mm = NULL;
1716 	struct c4iw_mm_entry *sq_db_key_mm = NULL, *rq_db_key_mm = NULL;
1717 
1718 	CTR2(KTR_IW_CXGBE, "%s ib_pd %p", __func__, pd);
1719 
1720 	if (attrs->qp_type != IB_QPT_RC)
1721 		return ERR_PTR(-EINVAL);
1722 
1723 	php = to_c4iw_pd(pd);
1724 	rhp = php->rhp;
1725 	schp = get_chp(rhp, ((struct c4iw_cq *)attrs->send_cq)->cq.cqid);
1726 	rchp = get_chp(rhp, ((struct c4iw_cq *)attrs->recv_cq)->cq.cqid);
1727 	if (!schp || !rchp)
1728 		return ERR_PTR(-EINVAL);
1729 
1730 	if (attrs->cap.max_inline_data > T4_MAX_SEND_INLINE)
1731 		return ERR_PTR(-EINVAL);
1732 
1733 	if (attrs->cap.max_recv_wr > rhp->rdev.hw_queue.t4_max_rq_size)
1734 		return ERR_PTR(-E2BIG);
1735 	rqsize = attrs->cap.max_recv_wr + 1;
1736 	if (rqsize < 8)
1737 		rqsize = 8;
1738 
1739 	if (attrs->cap.max_send_wr > rhp->rdev.hw_queue.t4_max_sq_size)
1740 		return ERR_PTR(-E2BIG);
1741 	sqsize = attrs->cap.max_send_wr + 1;
1742 	if (sqsize < 8)
1743 		sqsize = 8;
1744 
1745 	ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
1746 
1747 	qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
1748 	if (!qhp)
1749 		return ERR_PTR(-ENOMEM);
1750 	qhp->wq.sq.size = sqsize;
1751 	qhp->wq.sq.memsize =
1752 		(sqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
1753 		sizeof(*qhp->wq.sq.queue) + 16 * sizeof(__be64);
1754 	qhp->wq.sq.flush_cidx = -1;
1755 	qhp->wq.rq.size = rqsize;
1756 	qhp->wq.rq.memsize =
1757 		(rqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
1758 		sizeof(*qhp->wq.rq.queue);
1759 
1760 	if (ucontext) {
1761 		qhp->wq.sq.memsize = roundup(qhp->wq.sq.memsize, PAGE_SIZE);
1762 		qhp->wq.rq.memsize = roundup(qhp->wq.rq.memsize, PAGE_SIZE);
1763 	}
1764 
1765 	CTR5(KTR_IW_CXGBE, "%s sqsize %u sqmemsize %zu rqsize %u rqmemsize %zu",
1766 	    __func__, sqsize, qhp->wq.sq.memsize, rqsize, qhp->wq.rq.memsize);
1767 
1768 	ret = create_qp(&rhp->rdev, &qhp->wq, &schp->cq, &rchp->cq,
1769 			ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
1770 	if (ret)
1771 		goto err1;
1772 
1773 	attrs->cap.max_recv_wr = rqsize - 1;
1774 	attrs->cap.max_send_wr = sqsize - 1;
1775 	attrs->cap.max_inline_data = T4_MAX_SEND_INLINE;
1776 
1777 	qhp->rhp = rhp;
1778 	qhp->attr.pd = php->pdid;
1779 	qhp->attr.scq = ((struct c4iw_cq *) attrs->send_cq)->cq.cqid;
1780 	qhp->attr.rcq = ((struct c4iw_cq *) attrs->recv_cq)->cq.cqid;
1781 	qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
1782 	qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
1783 	qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
1784 	qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
1785 	qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
1786 	qhp->attr.state = C4IW_QP_STATE_IDLE;
1787 	qhp->attr.next_state = C4IW_QP_STATE_IDLE;
1788 	qhp->attr.enable_rdma_read = 1;
1789 	qhp->attr.enable_rdma_write = 1;
1790 	qhp->attr.enable_bind = 1;
1791 	qhp->attr.max_ord = 0;
1792 	qhp->attr.max_ird = 0;
1793 	qhp->sq_sig_all = attrs->sq_sig_type == IB_SIGNAL_ALL_WR;
1794 	spin_lock_init(&qhp->lock);
1795 	mutex_init(&qhp->mutex);
1796 	init_waitqueue_head(&qhp->wait);
1797 	kref_init(&qhp->kref);
1798 	INIT_WORK(&qhp->free_work, free_qp_work);
1799 
1800 	ret = insert_handle(rhp, &rhp->qpidr, qhp, qhp->wq.sq.qid);
1801 	if (ret)
1802 		goto err2;
1803 
1804 	if (udata) {
1805 		sq_key_mm = kmalloc(sizeof(*sq_key_mm), GFP_KERNEL);
1806 		if (!sq_key_mm) {
1807 			ret = -ENOMEM;
1808 			goto err3;
1809 		}
1810 		rq_key_mm = kmalloc(sizeof(*rq_key_mm), GFP_KERNEL);
1811 		if (!rq_key_mm) {
1812 			ret = -ENOMEM;
1813 			goto err4;
1814 		}
1815 		sq_db_key_mm = kmalloc(sizeof(*sq_db_key_mm), GFP_KERNEL);
1816 		if (!sq_db_key_mm) {
1817 			ret = -ENOMEM;
1818 			goto err5;
1819 		}
1820 		rq_db_key_mm = kmalloc(sizeof(*rq_db_key_mm), GFP_KERNEL);
1821 		if (!rq_db_key_mm) {
1822 			ret = -ENOMEM;
1823 			goto err6;
1824 		}
1825 		uresp.flags = 0;
1826 		uresp.qid_mask = rhp->rdev.qpmask;
1827 		uresp.sqid = qhp->wq.sq.qid;
1828 		uresp.sq_size = qhp->wq.sq.size;
1829 		uresp.sq_memsize = qhp->wq.sq.memsize;
1830 		uresp.rqid = qhp->wq.rq.qid;
1831 		uresp.rq_size = qhp->wq.rq.size;
1832 		uresp.rq_memsize = qhp->wq.rq.memsize;
1833 		spin_lock(&ucontext->mmap_lock);
1834 		uresp.ma_sync_key =  0;
1835 		uresp.sq_key = ucontext->key;
1836 		ucontext->key += PAGE_SIZE;
1837 		uresp.rq_key = ucontext->key;
1838 		ucontext->key += PAGE_SIZE;
1839 		uresp.sq_db_gts_key = ucontext->key;
1840 		ucontext->key += PAGE_SIZE;
1841 		uresp.rq_db_gts_key = ucontext->key;
1842 		ucontext->key += PAGE_SIZE;
1843 		spin_unlock(&ucontext->mmap_lock);
1844 		ret = ib_copy_to_udata(udata, &uresp, sizeof uresp);
1845 		if (ret)
1846 			goto err7;
1847 		sq_key_mm->key = uresp.sq_key;
1848 		sq_key_mm->addr = qhp->wq.sq.phys_addr;
1849 		sq_key_mm->len = PAGE_ALIGN(qhp->wq.sq.memsize);
1850 		CTR4(KTR_IW_CXGBE, "%s sq_key_mm %x, %x, %d", __func__,
1851 				sq_key_mm->key, sq_key_mm->addr,
1852 				sq_key_mm->len);
1853 		insert_mmap(ucontext, sq_key_mm);
1854 		rq_key_mm->key = uresp.rq_key;
1855 		rq_key_mm->addr = qhp->wq.rq.phys_addr;
1856 		rq_key_mm->len = PAGE_ALIGN(qhp->wq.rq.memsize);
1857 		CTR4(KTR_IW_CXGBE, "%s rq_key_mm %x, %x, %d", __func__,
1858 				rq_key_mm->key, rq_key_mm->addr,
1859 				rq_key_mm->len);
1860 		insert_mmap(ucontext, rq_key_mm);
1861 		sq_db_key_mm->key = uresp.sq_db_gts_key;
1862 		sq_db_key_mm->addr = (u64)qhp->wq.sq.bar2_pa;
1863 		sq_db_key_mm->len = PAGE_SIZE;
1864 		CTR4(KTR_IW_CXGBE, "%s sq_db_key_mm %x, %x, %d", __func__,
1865 				sq_db_key_mm->key, sq_db_key_mm->addr,
1866 				sq_db_key_mm->len);
1867 		insert_mmap(ucontext, sq_db_key_mm);
1868 		rq_db_key_mm->key = uresp.rq_db_gts_key;
1869 		rq_db_key_mm->addr = (u64)qhp->wq.rq.bar2_pa;
1870 		rq_db_key_mm->len = PAGE_SIZE;
1871 		CTR4(KTR_IW_CXGBE, "%s rq_db_key_mm %x, %x, %d", __func__,
1872 				rq_db_key_mm->key, rq_db_key_mm->addr,
1873 				rq_db_key_mm->len);
1874 		insert_mmap(ucontext, rq_db_key_mm);
1875 
1876 		c4iw_get_ucontext(ucontext);
1877 		qhp->ucontext = ucontext;
1878 	}
1879 	qhp->ibqp.qp_num = qhp->wq.sq.qid;
1880 	init_timer(&(qhp->timer));
1881 
1882 	CTR5(KTR_IW_CXGBE, "%s sq id %u size %u memsize %zu num_entries %u",
1883 		 __func__, qhp->wq.sq.qid,
1884 		 qhp->wq.sq.size, qhp->wq.sq.memsize, attrs->cap.max_send_wr);
1885 	CTR5(KTR_IW_CXGBE, "%s rq id %u size %u memsize %zu num_entries %u",
1886 		 __func__, qhp->wq.rq.qid,
1887 		 qhp->wq.rq.size, qhp->wq.rq.memsize, attrs->cap.max_recv_wr);
1888 	return &qhp->ibqp;
1889 err7:
1890 	kfree(rq_db_key_mm);
1891 err6:
1892 	kfree(sq_db_key_mm);
1893 err5:
1894 	kfree(rq_key_mm);
1895 err4:
1896 	kfree(sq_key_mm);
1897 err3:
1898 	remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
1899 err2:
1900 	destroy_qp(&rhp->rdev, &qhp->wq,
1901 		   ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
1902 err1:
1903 	kfree(qhp);
1904 	return ERR_PTR(ret);
1905 }
1906 
1907 int c4iw_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1908 		      int attr_mask, struct ib_udata *udata)
1909 {
1910 	struct c4iw_dev *rhp;
1911 	struct c4iw_qp *qhp;
1912 	enum c4iw_qp_attr_mask mask = 0;
1913 	struct c4iw_qp_attributes attrs;
1914 
1915 	CTR2(KTR_IW_CXGBE, "%s ib_qp %p", __func__, ibqp);
1916 
1917 	/* iwarp does not support the RTR state */
1918 	if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
1919 		attr_mask &= ~IB_QP_STATE;
1920 
1921 	/* Make sure we still have something left to do */
1922 	if (!attr_mask)
1923 		return 0;
1924 
1925 	memset(&attrs, 0, sizeof attrs);
1926 	qhp = to_c4iw_qp(ibqp);
1927 	rhp = qhp->rhp;
1928 
1929 	attrs.next_state = c4iw_convert_state(attr->qp_state);
1930 	attrs.enable_rdma_read = (attr->qp_access_flags &
1931 			       IB_ACCESS_REMOTE_READ) ?  1 : 0;
1932 	attrs.enable_rdma_write = (attr->qp_access_flags &
1933 				IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
1934 	attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
1935 
1936 
1937 	mask |= (attr_mask & IB_QP_STATE) ? C4IW_QP_ATTR_NEXT_STATE : 0;
1938 	mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
1939 			(C4IW_QP_ATTR_ENABLE_RDMA_READ |
1940 			 C4IW_QP_ATTR_ENABLE_RDMA_WRITE |
1941 			 C4IW_QP_ATTR_ENABLE_RDMA_BIND) : 0;
1942 
1943 	return c4iw_modify_qp(rhp, qhp, mask, &attrs, 0);
1944 }
1945 
1946 struct ib_qp *c4iw_get_qp(struct ib_device *dev, int qpn)
1947 {
1948 	CTR3(KTR_IW_CXGBE, "%s ib_dev %p qpn 0x%x", __func__, dev, qpn);
1949 	return (struct ib_qp *)get_qhp(to_c4iw_dev(dev), qpn);
1950 }
1951 
1952 int c4iw_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1953 		     int attr_mask, struct ib_qp_init_attr *init_attr)
1954 {
1955 	struct c4iw_qp *qhp = to_c4iw_qp(ibqp);
1956 
1957 	memset(attr, 0, sizeof *attr);
1958 	memset(init_attr, 0, sizeof *init_attr);
1959 	attr->qp_state = to_ib_qp_state(qhp->attr.state);
1960 	init_attr->cap.max_send_wr = qhp->attr.sq_num_entries;
1961 	init_attr->cap.max_recv_wr = qhp->attr.rq_num_entries;
1962 	init_attr->cap.max_send_sge = qhp->attr.sq_max_sges;
1963 	init_attr->cap.max_recv_sge = qhp->attr.sq_max_sges;
1964 	init_attr->cap.max_inline_data = T4_MAX_SEND_INLINE;
1965 	init_attr->sq_sig_type = qhp->sq_sig_all ? IB_SIGNAL_ALL_WR : 0;
1966 	return 0;
1967 }
1968 #endif
1969