xref: /linux/drivers/net/ethernet/qlogic/qed/qed_iwarp.c (revision 83a37b3292f4aca799b355179ad6fbdd78a08e10)
1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015-2017  QLogic Corporation
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 <linux/if_ether.h>
33 #include <linux/if_vlan.h>
34 #include <linux/ip.h>
35 #include <linux/ipv6.h>
36 #include <linux/spinlock.h>
37 #include <linux/tcp.h>
38 #include "qed_cxt.h"
39 #include "qed_hw.h"
40 #include "qed_ll2.h"
41 #include "qed_rdma.h"
42 #include "qed_reg_addr.h"
43 #include "qed_sp.h"
44 #include "qed_ooo.h"
45 
46 #define QED_IWARP_ORD_DEFAULT		32
47 #define QED_IWARP_IRD_DEFAULT		32
48 #define QED_IWARP_MAX_FW_MSS		4120
49 
50 #define QED_EP_SIG 0xecabcdef
51 
52 struct mpa_v2_hdr {
53 	__be16 ird;
54 	__be16 ord;
55 };
56 
57 #define MPA_V2_PEER2PEER_MODEL  0x8000
58 #define MPA_V2_SEND_RTR         0x4000	/* on ird */
59 #define MPA_V2_READ_RTR         0x4000	/* on ord */
60 #define MPA_V2_WRITE_RTR        0x8000
61 #define MPA_V2_IRD_ORD_MASK     0x3FFF
62 
63 #define MPA_REV2(_mpa_rev) ((_mpa_rev) == MPA_NEGOTIATION_TYPE_ENHANCED)
64 
65 #define QED_IWARP_INVALID_TCP_CID	0xffffffff
66 #define QED_IWARP_RCV_WND_SIZE_DEF	(256 * 1024)
67 #define QED_IWARP_RCV_WND_SIZE_MIN	(64 * 1024)
68 #define TIMESTAMP_HEADER_SIZE		(12)
69 
70 #define QED_IWARP_TS_EN			BIT(0)
71 #define QED_IWARP_DA_EN			BIT(1)
72 #define QED_IWARP_PARAM_CRC_NEEDED	(1)
73 #define QED_IWARP_PARAM_P2P		(1)
74 
75 static int qed_iwarp_async_event(struct qed_hwfn *p_hwfn,
76 				 u8 fw_event_code, u16 echo,
77 				 union event_ring_data *data,
78 				 u8 fw_return_code);
79 
80 /* Override devinfo with iWARP specific values */
81 void qed_iwarp_init_devinfo(struct qed_hwfn *p_hwfn)
82 {
83 	struct qed_rdma_device *dev = p_hwfn->p_rdma_info->dev;
84 
85 	dev->max_inline = IWARP_REQ_MAX_INLINE_DATA_SIZE;
86 	dev->max_qp = min_t(u32,
87 			    IWARP_MAX_QPS,
88 			    p_hwfn->p_rdma_info->num_qps) -
89 		      QED_IWARP_PREALLOC_CNT;
90 
91 	dev->max_cq = dev->max_qp;
92 
93 	dev->max_qp_resp_rd_atomic_resc = QED_IWARP_IRD_DEFAULT;
94 	dev->max_qp_req_rd_atomic_resc = QED_IWARP_ORD_DEFAULT;
95 }
96 
97 void qed_iwarp_init_hw(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
98 {
99 	p_hwfn->rdma_prs_search_reg = PRS_REG_SEARCH_TCP;
100 	qed_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 1);
101 	p_hwfn->b_rdma_enabled_in_prs = true;
102 }
103 
104 /* We have two cid maps, one for tcp which should be used only from passive
105  * syn processing and replacing a pre-allocated ep in the list. The second
106  * for active tcp and for QPs.
107  */
108 static void qed_iwarp_cid_cleaned(struct qed_hwfn *p_hwfn, u32 cid)
109 {
110 	cid -= qed_cxt_get_proto_cid_start(p_hwfn, p_hwfn->p_rdma_info->proto);
111 
112 	spin_lock_bh(&p_hwfn->p_rdma_info->lock);
113 
114 	if (cid < QED_IWARP_PREALLOC_CNT)
115 		qed_bmap_release_id(p_hwfn, &p_hwfn->p_rdma_info->tcp_cid_map,
116 				    cid);
117 	else
118 		qed_bmap_release_id(p_hwfn, &p_hwfn->p_rdma_info->cid_map, cid);
119 
120 	spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
121 }
122 
123 void qed_iwarp_init_fw_ramrod(struct qed_hwfn *p_hwfn,
124 			      struct iwarp_init_func_params *p_ramrod)
125 {
126 	p_ramrod->ll2_ooo_q_index = RESC_START(p_hwfn, QED_LL2_QUEUE) +
127 				    p_hwfn->p_rdma_info->iwarp.ll2_ooo_handle;
128 }
129 
130 static int qed_iwarp_alloc_cid(struct qed_hwfn *p_hwfn, u32 *cid)
131 {
132 	int rc;
133 
134 	spin_lock_bh(&p_hwfn->p_rdma_info->lock);
135 	rc = qed_rdma_bmap_alloc_id(p_hwfn, &p_hwfn->p_rdma_info->cid_map, cid);
136 	spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
137 	if (rc) {
138 		DP_NOTICE(p_hwfn, "Failed in allocating iwarp cid\n");
139 		return rc;
140 	}
141 	*cid += qed_cxt_get_proto_cid_start(p_hwfn, p_hwfn->p_rdma_info->proto);
142 
143 	rc = qed_cxt_dynamic_ilt_alloc(p_hwfn, QED_ELEM_CXT, *cid);
144 	if (rc)
145 		qed_iwarp_cid_cleaned(p_hwfn, *cid);
146 
147 	return rc;
148 }
149 
150 static void qed_iwarp_set_tcp_cid(struct qed_hwfn *p_hwfn, u32 cid)
151 {
152 	cid -= qed_cxt_get_proto_cid_start(p_hwfn, p_hwfn->p_rdma_info->proto);
153 
154 	spin_lock_bh(&p_hwfn->p_rdma_info->lock);
155 	qed_bmap_set_id(p_hwfn, &p_hwfn->p_rdma_info->tcp_cid_map, cid);
156 	spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
157 }
158 
159 /* This function allocates a cid for passive tcp (called from syn receive)
160  * the reason it's separate from the regular cid allocation is because it
161  * is assured that these cids already have ilt allocated. They are preallocated
162  * to ensure that we won't need to allocate memory during syn processing
163  */
164 static int qed_iwarp_alloc_tcp_cid(struct qed_hwfn *p_hwfn, u32 *cid)
165 {
166 	int rc;
167 
168 	spin_lock_bh(&p_hwfn->p_rdma_info->lock);
169 
170 	rc = qed_rdma_bmap_alloc_id(p_hwfn,
171 				    &p_hwfn->p_rdma_info->tcp_cid_map, cid);
172 
173 	spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
174 
175 	if (rc) {
176 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
177 			   "can't allocate iwarp tcp cid max-count=%d\n",
178 			   p_hwfn->p_rdma_info->tcp_cid_map.max_count);
179 
180 		*cid = QED_IWARP_INVALID_TCP_CID;
181 		return rc;
182 	}
183 
184 	*cid += qed_cxt_get_proto_cid_start(p_hwfn,
185 					    p_hwfn->p_rdma_info->proto);
186 	return 0;
187 }
188 
189 int qed_iwarp_create_qp(struct qed_hwfn *p_hwfn,
190 			struct qed_rdma_qp *qp,
191 			struct qed_rdma_create_qp_out_params *out_params)
192 {
193 	struct iwarp_create_qp_ramrod_data *p_ramrod;
194 	struct qed_sp_init_data init_data;
195 	struct qed_spq_entry *p_ent;
196 	u16 physical_queue;
197 	u32 cid;
198 	int rc;
199 
200 	qp->shared_queue = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
201 					      IWARP_SHARED_QUEUE_PAGE_SIZE,
202 					      &qp->shared_queue_phys_addr,
203 					      GFP_KERNEL);
204 	if (!qp->shared_queue)
205 		return -ENOMEM;
206 
207 	out_params->sq_pbl_virt = (u8 *)qp->shared_queue +
208 	    IWARP_SHARED_QUEUE_PAGE_SQ_PBL_OFFSET;
209 	out_params->sq_pbl_phys = qp->shared_queue_phys_addr +
210 	    IWARP_SHARED_QUEUE_PAGE_SQ_PBL_OFFSET;
211 	out_params->rq_pbl_virt = (u8 *)qp->shared_queue +
212 	    IWARP_SHARED_QUEUE_PAGE_RQ_PBL_OFFSET;
213 	out_params->rq_pbl_phys = qp->shared_queue_phys_addr +
214 	    IWARP_SHARED_QUEUE_PAGE_RQ_PBL_OFFSET;
215 
216 	rc = qed_iwarp_alloc_cid(p_hwfn, &cid);
217 	if (rc)
218 		goto err1;
219 
220 	qp->icid = (u16)cid;
221 
222 	memset(&init_data, 0, sizeof(init_data));
223 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
224 	init_data.cid = qp->icid;
225 	init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
226 
227 	rc = qed_sp_init_request(p_hwfn, &p_ent,
228 				 IWARP_RAMROD_CMD_ID_CREATE_QP,
229 				 PROTOCOLID_IWARP, &init_data);
230 	if (rc)
231 		goto err2;
232 
233 	p_ramrod = &p_ent->ramrod.iwarp_create_qp;
234 
235 	SET_FIELD(p_ramrod->flags,
236 		  IWARP_CREATE_QP_RAMROD_DATA_FMR_AND_RESERVED_EN,
237 		  qp->fmr_and_reserved_lkey);
238 
239 	SET_FIELD(p_ramrod->flags,
240 		  IWARP_CREATE_QP_RAMROD_DATA_SIGNALED_COMP, qp->signal_all);
241 
242 	SET_FIELD(p_ramrod->flags,
243 		  IWARP_CREATE_QP_RAMROD_DATA_RDMA_RD_EN,
244 		  qp->incoming_rdma_read_en);
245 
246 	SET_FIELD(p_ramrod->flags,
247 		  IWARP_CREATE_QP_RAMROD_DATA_RDMA_WR_EN,
248 		  qp->incoming_rdma_write_en);
249 
250 	SET_FIELD(p_ramrod->flags,
251 		  IWARP_CREATE_QP_RAMROD_DATA_ATOMIC_EN,
252 		  qp->incoming_atomic_en);
253 
254 	SET_FIELD(p_ramrod->flags,
255 		  IWARP_CREATE_QP_RAMROD_DATA_SRQ_FLG, qp->use_srq);
256 
257 	p_ramrod->pd = qp->pd;
258 	p_ramrod->sq_num_pages = qp->sq_num_pages;
259 	p_ramrod->rq_num_pages = qp->rq_num_pages;
260 
261 	p_ramrod->qp_handle_for_cqe.hi = cpu_to_le32(qp->qp_handle.hi);
262 	p_ramrod->qp_handle_for_cqe.lo = cpu_to_le32(qp->qp_handle.lo);
263 
264 	p_ramrod->cq_cid_for_sq =
265 	    cpu_to_le32((p_hwfn->hw_info.opaque_fid << 16) | qp->sq_cq_id);
266 	p_ramrod->cq_cid_for_rq =
267 	    cpu_to_le32((p_hwfn->hw_info.opaque_fid << 16) | qp->rq_cq_id);
268 
269 	p_ramrod->dpi = cpu_to_le16(qp->dpi);
270 
271 	physical_queue = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
272 	p_ramrod->physical_q0 = cpu_to_le16(physical_queue);
273 	physical_queue = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_ACK);
274 	p_ramrod->physical_q1 = cpu_to_le16(physical_queue);
275 
276 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
277 	if (rc)
278 		goto err2;
279 
280 	return rc;
281 
282 err2:
283 	qed_iwarp_cid_cleaned(p_hwfn, cid);
284 err1:
285 	dma_free_coherent(&p_hwfn->cdev->pdev->dev,
286 			  IWARP_SHARED_QUEUE_PAGE_SIZE,
287 			  qp->shared_queue, qp->shared_queue_phys_addr);
288 
289 	return rc;
290 }
291 
292 static int qed_iwarp_modify_fw(struct qed_hwfn *p_hwfn, struct qed_rdma_qp *qp)
293 {
294 	struct iwarp_modify_qp_ramrod_data *p_ramrod;
295 	struct qed_sp_init_data init_data;
296 	struct qed_spq_entry *p_ent;
297 	int rc;
298 
299 	/* Get SPQ entry */
300 	memset(&init_data, 0, sizeof(init_data));
301 	init_data.cid = qp->icid;
302 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
303 	init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
304 
305 	rc = qed_sp_init_request(p_hwfn, &p_ent,
306 				 IWARP_RAMROD_CMD_ID_MODIFY_QP,
307 				 p_hwfn->p_rdma_info->proto, &init_data);
308 	if (rc)
309 		return rc;
310 
311 	p_ramrod = &p_ent->ramrod.iwarp_modify_qp;
312 	SET_FIELD(p_ramrod->flags, IWARP_MODIFY_QP_RAMROD_DATA_STATE_TRANS_EN,
313 		  0x1);
314 	if (qp->iwarp_state == QED_IWARP_QP_STATE_CLOSING)
315 		p_ramrod->transition_to_state = IWARP_MODIFY_QP_STATE_CLOSING;
316 	else
317 		p_ramrod->transition_to_state = IWARP_MODIFY_QP_STATE_ERROR;
318 
319 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
320 
321 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x)rc=%d\n", qp->icid, rc);
322 
323 	return rc;
324 }
325 
326 enum qed_iwarp_qp_state qed_roce2iwarp_state(enum qed_roce_qp_state state)
327 {
328 	switch (state) {
329 	case QED_ROCE_QP_STATE_RESET:
330 	case QED_ROCE_QP_STATE_INIT:
331 	case QED_ROCE_QP_STATE_RTR:
332 		return QED_IWARP_QP_STATE_IDLE;
333 	case QED_ROCE_QP_STATE_RTS:
334 		return QED_IWARP_QP_STATE_RTS;
335 	case QED_ROCE_QP_STATE_SQD:
336 		return QED_IWARP_QP_STATE_CLOSING;
337 	case QED_ROCE_QP_STATE_ERR:
338 		return QED_IWARP_QP_STATE_ERROR;
339 	case QED_ROCE_QP_STATE_SQE:
340 		return QED_IWARP_QP_STATE_TERMINATE;
341 	default:
342 		return QED_IWARP_QP_STATE_ERROR;
343 	}
344 }
345 
346 static enum qed_roce_qp_state
347 qed_iwarp2roce_state(enum qed_iwarp_qp_state state)
348 {
349 	switch (state) {
350 	case QED_IWARP_QP_STATE_IDLE:
351 		return QED_ROCE_QP_STATE_INIT;
352 	case QED_IWARP_QP_STATE_RTS:
353 		return QED_ROCE_QP_STATE_RTS;
354 	case QED_IWARP_QP_STATE_TERMINATE:
355 		return QED_ROCE_QP_STATE_SQE;
356 	case QED_IWARP_QP_STATE_CLOSING:
357 		return QED_ROCE_QP_STATE_SQD;
358 	case QED_IWARP_QP_STATE_ERROR:
359 		return QED_ROCE_QP_STATE_ERR;
360 	default:
361 		return QED_ROCE_QP_STATE_ERR;
362 	}
363 }
364 
365 const char *iwarp_state_names[] = {
366 	"IDLE",
367 	"RTS",
368 	"TERMINATE",
369 	"CLOSING",
370 	"ERROR",
371 };
372 
373 int
374 qed_iwarp_modify_qp(struct qed_hwfn *p_hwfn,
375 		    struct qed_rdma_qp *qp,
376 		    enum qed_iwarp_qp_state new_state, bool internal)
377 {
378 	enum qed_iwarp_qp_state prev_iw_state;
379 	bool modify_fw = false;
380 	int rc = 0;
381 
382 	/* modify QP can be called from upper-layer or as a result of async
383 	 * RST/FIN... therefore need to protect
384 	 */
385 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.qp_lock);
386 	prev_iw_state = qp->iwarp_state;
387 
388 	if (prev_iw_state == new_state) {
389 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.qp_lock);
390 		return 0;
391 	}
392 
393 	switch (prev_iw_state) {
394 	case QED_IWARP_QP_STATE_IDLE:
395 		switch (new_state) {
396 		case QED_IWARP_QP_STATE_RTS:
397 			qp->iwarp_state = QED_IWARP_QP_STATE_RTS;
398 			break;
399 		case QED_IWARP_QP_STATE_ERROR:
400 			qp->iwarp_state = QED_IWARP_QP_STATE_ERROR;
401 			if (!internal)
402 				modify_fw = true;
403 			break;
404 		default:
405 			break;
406 		}
407 		break;
408 	case QED_IWARP_QP_STATE_RTS:
409 		switch (new_state) {
410 		case QED_IWARP_QP_STATE_CLOSING:
411 			if (!internal)
412 				modify_fw = true;
413 
414 			qp->iwarp_state = QED_IWARP_QP_STATE_CLOSING;
415 			break;
416 		case QED_IWARP_QP_STATE_ERROR:
417 			if (!internal)
418 				modify_fw = true;
419 			qp->iwarp_state = QED_IWARP_QP_STATE_ERROR;
420 			break;
421 		default:
422 			break;
423 		}
424 		break;
425 	case QED_IWARP_QP_STATE_ERROR:
426 		switch (new_state) {
427 		case QED_IWARP_QP_STATE_IDLE:
428 
429 			qp->iwarp_state = new_state;
430 			break;
431 		case QED_IWARP_QP_STATE_CLOSING:
432 			/* could happen due to race... do nothing.... */
433 			break;
434 		default:
435 			rc = -EINVAL;
436 		}
437 		break;
438 	case QED_IWARP_QP_STATE_TERMINATE:
439 	case QED_IWARP_QP_STATE_CLOSING:
440 		qp->iwarp_state = new_state;
441 		break;
442 	default:
443 		break;
444 	}
445 
446 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) %s --> %s%s\n",
447 		   qp->icid,
448 		   iwarp_state_names[prev_iw_state],
449 		   iwarp_state_names[qp->iwarp_state],
450 		   internal ? "internal" : "");
451 
452 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.qp_lock);
453 
454 	if (modify_fw)
455 		rc = qed_iwarp_modify_fw(p_hwfn, qp);
456 
457 	return rc;
458 }
459 
460 int qed_iwarp_fw_destroy(struct qed_hwfn *p_hwfn, struct qed_rdma_qp *qp)
461 {
462 	struct qed_sp_init_data init_data;
463 	struct qed_spq_entry *p_ent;
464 	int rc;
465 
466 	/* Get SPQ entry */
467 	memset(&init_data, 0, sizeof(init_data));
468 	init_data.cid = qp->icid;
469 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
470 	init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
471 
472 	rc = qed_sp_init_request(p_hwfn, &p_ent,
473 				 IWARP_RAMROD_CMD_ID_DESTROY_QP,
474 				 p_hwfn->p_rdma_info->proto, &init_data);
475 	if (rc)
476 		return rc;
477 
478 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
479 
480 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) rc = %d\n", qp->icid, rc);
481 
482 	return rc;
483 }
484 
485 static void qed_iwarp_destroy_ep(struct qed_hwfn *p_hwfn,
486 				 struct qed_iwarp_ep *ep,
487 				 bool remove_from_active_list)
488 {
489 	dma_free_coherent(&p_hwfn->cdev->pdev->dev,
490 			  sizeof(*ep->ep_buffer_virt),
491 			  ep->ep_buffer_virt, ep->ep_buffer_phys);
492 
493 	if (remove_from_active_list) {
494 		spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
495 		list_del(&ep->list_entry);
496 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
497 	}
498 
499 	if (ep->qp)
500 		ep->qp->ep = NULL;
501 
502 	kfree(ep);
503 }
504 
505 int qed_iwarp_destroy_qp(struct qed_hwfn *p_hwfn, struct qed_rdma_qp *qp)
506 {
507 	struct qed_iwarp_ep *ep = qp->ep;
508 	int wait_count = 0;
509 	int rc = 0;
510 
511 	if (qp->iwarp_state != QED_IWARP_QP_STATE_ERROR) {
512 		rc = qed_iwarp_modify_qp(p_hwfn, qp,
513 					 QED_IWARP_QP_STATE_ERROR, false);
514 		if (rc)
515 			return rc;
516 	}
517 
518 	/* Make sure ep is closed before returning and freeing memory. */
519 	if (ep) {
520 		while (ep->state != QED_IWARP_EP_CLOSED && wait_count++ < 200)
521 			msleep(100);
522 
523 		if (ep->state != QED_IWARP_EP_CLOSED)
524 			DP_NOTICE(p_hwfn, "ep state close timeout state=%x\n",
525 				  ep->state);
526 
527 		qed_iwarp_destroy_ep(p_hwfn, ep, false);
528 	}
529 
530 	rc = qed_iwarp_fw_destroy(p_hwfn, qp);
531 
532 	if (qp->shared_queue)
533 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
534 				  IWARP_SHARED_QUEUE_PAGE_SIZE,
535 				  qp->shared_queue, qp->shared_queue_phys_addr);
536 
537 	return rc;
538 }
539 
540 static int
541 qed_iwarp_create_ep(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep **ep_out)
542 {
543 	struct qed_iwarp_ep *ep;
544 	int rc;
545 
546 	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
547 	if (!ep)
548 		return -ENOMEM;
549 
550 	ep->state = QED_IWARP_EP_INIT;
551 
552 	ep->ep_buffer_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
553 						sizeof(*ep->ep_buffer_virt),
554 						&ep->ep_buffer_phys,
555 						GFP_KERNEL);
556 	if (!ep->ep_buffer_virt) {
557 		rc = -ENOMEM;
558 		goto err;
559 	}
560 
561 	ep->sig = QED_EP_SIG;
562 
563 	*ep_out = ep;
564 
565 	return 0;
566 
567 err:
568 	kfree(ep);
569 	return rc;
570 }
571 
572 static void
573 qed_iwarp_print_tcp_ramrod(struct qed_hwfn *p_hwfn,
574 			   struct iwarp_tcp_offload_ramrod_data *p_tcp_ramrod)
575 {
576 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "local_mac=%x %x %x, remote_mac=%x %x %x\n",
577 		   p_tcp_ramrod->tcp.local_mac_addr_lo,
578 		   p_tcp_ramrod->tcp.local_mac_addr_mid,
579 		   p_tcp_ramrod->tcp.local_mac_addr_hi,
580 		   p_tcp_ramrod->tcp.remote_mac_addr_lo,
581 		   p_tcp_ramrod->tcp.remote_mac_addr_mid,
582 		   p_tcp_ramrod->tcp.remote_mac_addr_hi);
583 
584 	if (p_tcp_ramrod->tcp.ip_version == TCP_IPV4) {
585 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
586 			   "local_ip=%pI4h:%x, remote_ip=%pI4h:%x, vlan=%x\n",
587 			   p_tcp_ramrod->tcp.local_ip,
588 			   p_tcp_ramrod->tcp.local_port,
589 			   p_tcp_ramrod->tcp.remote_ip,
590 			   p_tcp_ramrod->tcp.remote_port,
591 			   p_tcp_ramrod->tcp.vlan_id);
592 	} else {
593 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
594 			   "local_ip=%pI6:%x, remote_ip=%pI6:%x, vlan=%x\n",
595 			   p_tcp_ramrod->tcp.local_ip,
596 			   p_tcp_ramrod->tcp.local_port,
597 			   p_tcp_ramrod->tcp.remote_ip,
598 			   p_tcp_ramrod->tcp.remote_port,
599 			   p_tcp_ramrod->tcp.vlan_id);
600 	}
601 
602 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
603 		   "flow_label=%x, ttl=%x, tos_or_tc=%x, mss=%x, rcv_wnd_scale=%x, connect_mode=%x, flags=%x\n",
604 		   p_tcp_ramrod->tcp.flow_label,
605 		   p_tcp_ramrod->tcp.ttl,
606 		   p_tcp_ramrod->tcp.tos_or_tc,
607 		   p_tcp_ramrod->tcp.mss,
608 		   p_tcp_ramrod->tcp.rcv_wnd_scale,
609 		   p_tcp_ramrod->tcp.connect_mode,
610 		   p_tcp_ramrod->tcp.flags);
611 
612 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "syn_ip_payload_length=%x, lo=%x, hi=%x\n",
613 		   p_tcp_ramrod->tcp.syn_ip_payload_length,
614 		   p_tcp_ramrod->tcp.syn_phy_addr_lo,
615 		   p_tcp_ramrod->tcp.syn_phy_addr_hi);
616 }
617 
618 static int
619 qed_iwarp_tcp_offload(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
620 {
621 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
622 	struct iwarp_tcp_offload_ramrod_data *p_tcp_ramrod;
623 	struct tcp_offload_params_opt2 *tcp;
624 	struct qed_sp_init_data init_data;
625 	struct qed_spq_entry *p_ent;
626 	dma_addr_t async_output_phys;
627 	dma_addr_t in_pdata_phys;
628 	u16 physical_q;
629 	u8 tcp_flags;
630 	int rc;
631 	int i;
632 
633 	memset(&init_data, 0, sizeof(init_data));
634 	init_data.cid = ep->tcp_cid;
635 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
636 	if (ep->connect_mode == TCP_CONNECT_PASSIVE)
637 		init_data.comp_mode = QED_SPQ_MODE_CB;
638 	else
639 		init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
640 
641 	rc = qed_sp_init_request(p_hwfn, &p_ent,
642 				 IWARP_RAMROD_CMD_ID_TCP_OFFLOAD,
643 				 PROTOCOLID_IWARP, &init_data);
644 	if (rc)
645 		return rc;
646 
647 	p_tcp_ramrod = &p_ent->ramrod.iwarp_tcp_offload;
648 
649 	in_pdata_phys = ep->ep_buffer_phys +
650 			offsetof(struct qed_iwarp_ep_memory, in_pdata);
651 	DMA_REGPAIR_LE(p_tcp_ramrod->iwarp.incoming_ulp_buffer.addr,
652 		       in_pdata_phys);
653 
654 	p_tcp_ramrod->iwarp.incoming_ulp_buffer.len =
655 	    cpu_to_le16(sizeof(ep->ep_buffer_virt->in_pdata));
656 
657 	async_output_phys = ep->ep_buffer_phys +
658 			    offsetof(struct qed_iwarp_ep_memory, async_output);
659 	DMA_REGPAIR_LE(p_tcp_ramrod->iwarp.async_eqe_output_buf,
660 		       async_output_phys);
661 
662 	p_tcp_ramrod->iwarp.handle_for_async.hi = cpu_to_le32(PTR_HI(ep));
663 	p_tcp_ramrod->iwarp.handle_for_async.lo = cpu_to_le32(PTR_LO(ep));
664 
665 	physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
666 	p_tcp_ramrod->iwarp.physical_q0 = cpu_to_le16(physical_q);
667 	physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_ACK);
668 	p_tcp_ramrod->iwarp.physical_q1 = cpu_to_le16(physical_q);
669 	p_tcp_ramrod->iwarp.mpa_mode = iwarp_info->mpa_rev;
670 
671 	tcp = &p_tcp_ramrod->tcp;
672 	qed_set_fw_mac_addr(&tcp->remote_mac_addr_hi,
673 			    &tcp->remote_mac_addr_mid,
674 			    &tcp->remote_mac_addr_lo, ep->remote_mac_addr);
675 	qed_set_fw_mac_addr(&tcp->local_mac_addr_hi, &tcp->local_mac_addr_mid,
676 			    &tcp->local_mac_addr_lo, ep->local_mac_addr);
677 
678 	tcp->vlan_id = cpu_to_le16(ep->cm_info.vlan);
679 
680 	tcp_flags = p_hwfn->p_rdma_info->iwarp.tcp_flags;
681 	tcp->flags = 0;
682 	SET_FIELD(tcp->flags, TCP_OFFLOAD_PARAMS_OPT2_TS_EN,
683 		  !!(tcp_flags & QED_IWARP_TS_EN));
684 
685 	SET_FIELD(tcp->flags, TCP_OFFLOAD_PARAMS_OPT2_DA_EN,
686 		  !!(tcp_flags & QED_IWARP_DA_EN));
687 
688 	tcp->ip_version = ep->cm_info.ip_version;
689 
690 	for (i = 0; i < 4; i++) {
691 		tcp->remote_ip[i] = cpu_to_le32(ep->cm_info.remote_ip[i]);
692 		tcp->local_ip[i] = cpu_to_le32(ep->cm_info.local_ip[i]);
693 	}
694 
695 	tcp->remote_port = cpu_to_le16(ep->cm_info.remote_port);
696 	tcp->local_port = cpu_to_le16(ep->cm_info.local_port);
697 	tcp->mss = cpu_to_le16(ep->mss);
698 	tcp->flow_label = 0;
699 	tcp->ttl = 0x40;
700 	tcp->tos_or_tc = 0;
701 
702 	tcp->rcv_wnd_scale = (u8)p_hwfn->p_rdma_info->iwarp.rcv_wnd_scale;
703 	tcp->connect_mode = ep->connect_mode;
704 
705 	if (ep->connect_mode == TCP_CONNECT_PASSIVE) {
706 		tcp->syn_ip_payload_length =
707 			cpu_to_le16(ep->syn_ip_payload_length);
708 		tcp->syn_phy_addr_hi = DMA_HI_LE(ep->syn_phy_addr);
709 		tcp->syn_phy_addr_lo = DMA_LO_LE(ep->syn_phy_addr);
710 	}
711 
712 	qed_iwarp_print_tcp_ramrod(p_hwfn, p_tcp_ramrod);
713 
714 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
715 
716 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
717 		   "EP(0x%x) Offload completed rc=%d\n", ep->tcp_cid, rc);
718 
719 	return rc;
720 }
721 
722 static void
723 qed_iwarp_mpa_received(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
724 {
725 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
726 	struct qed_iwarp_cm_event_params params;
727 	struct mpa_v2_hdr *mpa_v2;
728 	union async_output *async_data;
729 	u16 mpa_ord, mpa_ird;
730 	u8 mpa_hdr_size = 0;
731 	u8 mpa_rev;
732 
733 	async_data = &ep->ep_buffer_virt->async_output;
734 
735 	mpa_rev = async_data->mpa_request.mpa_handshake_mode;
736 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
737 		   "private_data_len=%x handshake_mode=%x private_data=(%x)\n",
738 		   async_data->mpa_request.ulp_data_len,
739 		   mpa_rev, *((u32 *)(ep->ep_buffer_virt->in_pdata)));
740 
741 	if (mpa_rev == MPA_NEGOTIATION_TYPE_ENHANCED) {
742 		/* Read ord/ird values from private data buffer */
743 		mpa_v2 = (struct mpa_v2_hdr *)ep->ep_buffer_virt->in_pdata;
744 		mpa_hdr_size = sizeof(*mpa_v2);
745 
746 		mpa_ord = ntohs(mpa_v2->ord);
747 		mpa_ird = ntohs(mpa_v2->ird);
748 
749 		/* Temprary store in cm_info incoming ord/ird requested, later
750 		 * replace with negotiated value during accept
751 		 */
752 		ep->cm_info.ord = (u8)min_t(u16,
753 					    (mpa_ord & MPA_V2_IRD_ORD_MASK),
754 					    QED_IWARP_ORD_DEFAULT);
755 
756 		ep->cm_info.ird = (u8)min_t(u16,
757 					    (mpa_ird & MPA_V2_IRD_ORD_MASK),
758 					    QED_IWARP_IRD_DEFAULT);
759 
760 		/* Peer2Peer negotiation */
761 		ep->rtr_type = MPA_RTR_TYPE_NONE;
762 		if (mpa_ird & MPA_V2_PEER2PEER_MODEL) {
763 			if (mpa_ord & MPA_V2_WRITE_RTR)
764 				ep->rtr_type |= MPA_RTR_TYPE_ZERO_WRITE;
765 
766 			if (mpa_ord & MPA_V2_READ_RTR)
767 				ep->rtr_type |= MPA_RTR_TYPE_ZERO_READ;
768 
769 			if (mpa_ird & MPA_V2_SEND_RTR)
770 				ep->rtr_type |= MPA_RTR_TYPE_ZERO_SEND;
771 
772 			ep->rtr_type &= iwarp_info->rtr_type;
773 
774 			/* if we're left with no match send our capabilities */
775 			if (ep->rtr_type == MPA_RTR_TYPE_NONE)
776 				ep->rtr_type = iwarp_info->rtr_type;
777 		}
778 
779 		ep->mpa_rev = MPA_NEGOTIATION_TYPE_ENHANCED;
780 	} else {
781 		ep->cm_info.ord = QED_IWARP_ORD_DEFAULT;
782 		ep->cm_info.ird = QED_IWARP_IRD_DEFAULT;
783 		ep->mpa_rev = MPA_NEGOTIATION_TYPE_BASIC;
784 	}
785 
786 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
787 		   "MPA_NEGOTIATE (v%d): ORD: 0x%x IRD: 0x%x rtr:0x%x ulp_data_len = %x mpa_hdr_size = %x\n",
788 		   mpa_rev, ep->cm_info.ord, ep->cm_info.ird, ep->rtr_type,
789 		   async_data->mpa_request.ulp_data_len, mpa_hdr_size);
790 
791 	/* Strip mpa v2 hdr from private data before sending to upper layer */
792 	ep->cm_info.private_data = ep->ep_buffer_virt->in_pdata + mpa_hdr_size;
793 
794 	ep->cm_info.private_data_len = async_data->mpa_request.ulp_data_len -
795 				       mpa_hdr_size;
796 
797 	params.event = QED_IWARP_EVENT_MPA_REQUEST;
798 	params.cm_info = &ep->cm_info;
799 	params.ep_context = ep;
800 	params.status = 0;
801 
802 	ep->state = QED_IWARP_EP_MPA_REQ_RCVD;
803 	ep->event_cb(ep->cb_context, &params);
804 }
805 
806 static int
807 qed_iwarp_mpa_offload(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
808 {
809 	struct iwarp_mpa_offload_ramrod_data *p_mpa_ramrod;
810 	struct qed_sp_init_data init_data;
811 	dma_addr_t async_output_phys;
812 	struct qed_spq_entry *p_ent;
813 	dma_addr_t out_pdata_phys;
814 	dma_addr_t in_pdata_phys;
815 	struct qed_rdma_qp *qp;
816 	bool reject;
817 	int rc;
818 
819 	if (!ep)
820 		return -EINVAL;
821 
822 	qp = ep->qp;
823 	reject = !qp;
824 
825 	memset(&init_data, 0, sizeof(init_data));
826 	init_data.cid = reject ? ep->tcp_cid : qp->icid;
827 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
828 
829 	if (ep->connect_mode == TCP_CONNECT_ACTIVE)
830 		init_data.comp_mode = QED_SPQ_MODE_CB;
831 	else
832 		init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
833 
834 	rc = qed_sp_init_request(p_hwfn, &p_ent,
835 				 IWARP_RAMROD_CMD_ID_MPA_OFFLOAD,
836 				 PROTOCOLID_IWARP, &init_data);
837 	if (rc)
838 		return rc;
839 
840 	p_mpa_ramrod = &p_ent->ramrod.iwarp_mpa_offload;
841 	out_pdata_phys = ep->ep_buffer_phys +
842 			 offsetof(struct qed_iwarp_ep_memory, out_pdata);
843 	DMA_REGPAIR_LE(p_mpa_ramrod->common.outgoing_ulp_buffer.addr,
844 		       out_pdata_phys);
845 	p_mpa_ramrod->common.outgoing_ulp_buffer.len =
846 	    ep->cm_info.private_data_len;
847 	p_mpa_ramrod->common.crc_needed = p_hwfn->p_rdma_info->iwarp.crc_needed;
848 
849 	p_mpa_ramrod->common.out_rq.ord = ep->cm_info.ord;
850 	p_mpa_ramrod->common.out_rq.ird = ep->cm_info.ird;
851 
852 	p_mpa_ramrod->tcp_cid = p_hwfn->hw_info.opaque_fid << 16 | ep->tcp_cid;
853 
854 	in_pdata_phys = ep->ep_buffer_phys +
855 			offsetof(struct qed_iwarp_ep_memory, in_pdata);
856 	p_mpa_ramrod->tcp_connect_side = ep->connect_mode;
857 	DMA_REGPAIR_LE(p_mpa_ramrod->incoming_ulp_buffer.addr,
858 		       in_pdata_phys);
859 	p_mpa_ramrod->incoming_ulp_buffer.len =
860 	    cpu_to_le16(sizeof(ep->ep_buffer_virt->in_pdata));
861 	async_output_phys = ep->ep_buffer_phys +
862 			    offsetof(struct qed_iwarp_ep_memory, async_output);
863 	DMA_REGPAIR_LE(p_mpa_ramrod->async_eqe_output_buf,
864 		       async_output_phys);
865 	p_mpa_ramrod->handle_for_async.hi = cpu_to_le32(PTR_HI(ep));
866 	p_mpa_ramrod->handle_for_async.lo = cpu_to_le32(PTR_LO(ep));
867 
868 	if (!reject) {
869 		DMA_REGPAIR_LE(p_mpa_ramrod->shared_queue_addr,
870 			       qp->shared_queue_phys_addr);
871 		p_mpa_ramrod->stats_counter_id =
872 		    RESC_START(p_hwfn, QED_RDMA_STATS_QUEUE) + qp->stats_queue;
873 	} else {
874 		p_mpa_ramrod->common.reject = 1;
875 	}
876 
877 	p_mpa_ramrod->mode = ep->mpa_rev;
878 	SET_FIELD(p_mpa_ramrod->rtr_pref,
879 		  IWARP_MPA_OFFLOAD_RAMROD_DATA_RTR_SUPPORTED, ep->rtr_type);
880 
881 	ep->state = QED_IWARP_EP_MPA_OFFLOADED;
882 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
883 	if (!reject)
884 		ep->cid = qp->icid;	/* Now they're migrated. */
885 
886 	DP_VERBOSE(p_hwfn,
887 		   QED_MSG_RDMA,
888 		   "QP(0x%x) EP(0x%x) MPA Offload rc = %d IRD=0x%x ORD=0x%x rtr_type=%d mpa_rev=%d reject=%d\n",
889 		   reject ? 0xffff : qp->icid,
890 		   ep->tcp_cid,
891 		   rc,
892 		   ep->cm_info.ird,
893 		   ep->cm_info.ord, ep->rtr_type, ep->mpa_rev, reject);
894 	return rc;
895 }
896 
897 static void
898 qed_iwarp_return_ep(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
899 {
900 	ep->state = QED_IWARP_EP_INIT;
901 	if (ep->qp)
902 		ep->qp->ep = NULL;
903 	ep->qp = NULL;
904 	memset(&ep->cm_info, 0, sizeof(ep->cm_info));
905 
906 	if (ep->tcp_cid == QED_IWARP_INVALID_TCP_CID) {
907 		/* We don't care about the return code, it's ok if tcp_cid
908 		 * remains invalid...in this case we'll defer allocation
909 		 */
910 		qed_iwarp_alloc_tcp_cid(p_hwfn, &ep->tcp_cid);
911 	}
912 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
913 
914 	list_del(&ep->list_entry);
915 	list_add_tail(&ep->list_entry,
916 		      &p_hwfn->p_rdma_info->iwarp.ep_free_list);
917 
918 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
919 }
920 
921 void
922 qed_iwarp_parse_private_data(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
923 {
924 	struct mpa_v2_hdr *mpa_v2_params;
925 	union async_output *async_data;
926 	u16 mpa_ird, mpa_ord;
927 	u8 mpa_data_size = 0;
928 
929 	if (MPA_REV2(p_hwfn->p_rdma_info->iwarp.mpa_rev)) {
930 		mpa_v2_params =
931 			(struct mpa_v2_hdr *)(ep->ep_buffer_virt->in_pdata);
932 		mpa_data_size = sizeof(*mpa_v2_params);
933 		mpa_ird = ntohs(mpa_v2_params->ird);
934 		mpa_ord = ntohs(mpa_v2_params->ord);
935 
936 		ep->cm_info.ird = (u8)(mpa_ord & MPA_V2_IRD_ORD_MASK);
937 		ep->cm_info.ord = (u8)(mpa_ird & MPA_V2_IRD_ORD_MASK);
938 	}
939 	async_data = &ep->ep_buffer_virt->async_output;
940 
941 	ep->cm_info.private_data = ep->ep_buffer_virt->in_pdata + mpa_data_size;
942 	ep->cm_info.private_data_len = async_data->mpa_response.ulp_data_len -
943 				       mpa_data_size;
944 }
945 
946 void
947 qed_iwarp_mpa_reply_arrived(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
948 {
949 	struct qed_iwarp_cm_event_params params;
950 
951 	if (ep->connect_mode == TCP_CONNECT_PASSIVE) {
952 		DP_NOTICE(p_hwfn,
953 			  "MPA reply event not expected on passive side!\n");
954 		return;
955 	}
956 
957 	params.event = QED_IWARP_EVENT_ACTIVE_MPA_REPLY;
958 
959 	qed_iwarp_parse_private_data(p_hwfn, ep);
960 
961 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
962 		   "MPA_NEGOTIATE (v%d): ORD: 0x%x IRD: 0x%x\n",
963 		   ep->mpa_rev, ep->cm_info.ord, ep->cm_info.ird);
964 
965 	params.cm_info = &ep->cm_info;
966 	params.ep_context = ep;
967 	params.status = 0;
968 
969 	ep->mpa_reply_processed = true;
970 
971 	ep->event_cb(ep->cb_context, &params);
972 }
973 
974 #define QED_IWARP_CONNECT_MODE_STRING(ep) \
975 	((ep)->connect_mode == TCP_CONNECT_PASSIVE) ? "Passive" : "Active"
976 
977 /* Called as a result of the event:
978  * IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_COMPLETE
979  */
980 static void
981 qed_iwarp_mpa_complete(struct qed_hwfn *p_hwfn,
982 		       struct qed_iwarp_ep *ep, u8 fw_return_code)
983 {
984 	struct qed_iwarp_cm_event_params params;
985 
986 	if (ep->connect_mode == TCP_CONNECT_ACTIVE)
987 		params.event = QED_IWARP_EVENT_ACTIVE_COMPLETE;
988 	else
989 		params.event = QED_IWARP_EVENT_PASSIVE_COMPLETE;
990 
991 	if (ep->connect_mode == TCP_CONNECT_ACTIVE && !ep->mpa_reply_processed)
992 		qed_iwarp_parse_private_data(p_hwfn, ep);
993 
994 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
995 		   "MPA_NEGOTIATE (v%d): ORD: 0x%x IRD: 0x%x\n",
996 		   ep->mpa_rev, ep->cm_info.ord, ep->cm_info.ird);
997 
998 	params.cm_info = &ep->cm_info;
999 
1000 	params.ep_context = ep;
1001 
1002 	ep->state = QED_IWARP_EP_CLOSED;
1003 
1004 	switch (fw_return_code) {
1005 	case RDMA_RETURN_OK:
1006 		ep->qp->max_rd_atomic_req = ep->cm_info.ord;
1007 		ep->qp->max_rd_atomic_resp = ep->cm_info.ird;
1008 		qed_iwarp_modify_qp(p_hwfn, ep->qp, QED_IWARP_QP_STATE_RTS, 1);
1009 		ep->state = QED_IWARP_EP_ESTABLISHED;
1010 		params.status = 0;
1011 		break;
1012 	case IWARP_CONN_ERROR_MPA_TIMEOUT:
1013 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA timeout\n",
1014 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
1015 		params.status = -EBUSY;
1016 		break;
1017 	case IWARP_CONN_ERROR_MPA_ERROR_REJECT:
1018 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA Reject\n",
1019 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
1020 		params.status = -ECONNREFUSED;
1021 		break;
1022 	case IWARP_CONN_ERROR_MPA_RST:
1023 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA reset(tcp cid: 0x%x)\n",
1024 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid,
1025 			  ep->tcp_cid);
1026 		params.status = -ECONNRESET;
1027 		break;
1028 	case IWARP_CONN_ERROR_MPA_FIN:
1029 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA received FIN\n",
1030 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
1031 		params.status = -ECONNREFUSED;
1032 		break;
1033 	case IWARP_CONN_ERROR_MPA_INSUF_IRD:
1034 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA insufficient ird\n",
1035 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
1036 		params.status = -ECONNREFUSED;
1037 		break;
1038 	case IWARP_CONN_ERROR_MPA_RTR_MISMATCH:
1039 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA RTR MISMATCH\n",
1040 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
1041 		params.status = -ECONNREFUSED;
1042 		break;
1043 	case IWARP_CONN_ERROR_MPA_INVALID_PACKET:
1044 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA Invalid Packet\n",
1045 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
1046 		params.status = -ECONNREFUSED;
1047 		break;
1048 	case IWARP_CONN_ERROR_MPA_LOCAL_ERROR:
1049 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA Local Error\n",
1050 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
1051 		params.status = -ECONNREFUSED;
1052 		break;
1053 	case IWARP_CONN_ERROR_MPA_TERMINATE:
1054 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA TERMINATE\n",
1055 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
1056 		params.status = -ECONNREFUSED;
1057 		break;
1058 	default:
1059 		params.status = -ECONNRESET;
1060 		break;
1061 	}
1062 
1063 	ep->event_cb(ep->cb_context, &params);
1064 
1065 	/* on passive side, if there is no associated QP (REJECT) we need to
1066 	 * return the ep to the pool, (in the regular case we add an element
1067 	 * in accept instead of this one.
1068 	 * In both cases we need to remove it from the ep_list.
1069 	 */
1070 	if (fw_return_code != RDMA_RETURN_OK) {
1071 		ep->tcp_cid = QED_IWARP_INVALID_TCP_CID;
1072 		if ((ep->connect_mode == TCP_CONNECT_PASSIVE) &&
1073 		    (!ep->qp)) {	/* Rejected */
1074 			qed_iwarp_return_ep(p_hwfn, ep);
1075 		} else {
1076 			spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1077 			list_del(&ep->list_entry);
1078 			spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1079 		}
1080 	}
1081 }
1082 
1083 static void
1084 qed_iwarp_mpa_v2_set_private(struct qed_hwfn *p_hwfn,
1085 			     struct qed_iwarp_ep *ep, u8 *mpa_data_size)
1086 {
1087 	struct mpa_v2_hdr *mpa_v2_params;
1088 	u16 mpa_ird, mpa_ord;
1089 
1090 	*mpa_data_size = 0;
1091 	if (MPA_REV2(ep->mpa_rev)) {
1092 		mpa_v2_params =
1093 		    (struct mpa_v2_hdr *)ep->ep_buffer_virt->out_pdata;
1094 		*mpa_data_size = sizeof(*mpa_v2_params);
1095 
1096 		mpa_ird = (u16)ep->cm_info.ird;
1097 		mpa_ord = (u16)ep->cm_info.ord;
1098 
1099 		if (ep->rtr_type != MPA_RTR_TYPE_NONE) {
1100 			mpa_ird |= MPA_V2_PEER2PEER_MODEL;
1101 
1102 			if (ep->rtr_type & MPA_RTR_TYPE_ZERO_SEND)
1103 				mpa_ird |= MPA_V2_SEND_RTR;
1104 
1105 			if (ep->rtr_type & MPA_RTR_TYPE_ZERO_WRITE)
1106 				mpa_ord |= MPA_V2_WRITE_RTR;
1107 
1108 			if (ep->rtr_type & MPA_RTR_TYPE_ZERO_READ)
1109 				mpa_ord |= MPA_V2_READ_RTR;
1110 		}
1111 
1112 		mpa_v2_params->ird = htons(mpa_ird);
1113 		mpa_v2_params->ord = htons(mpa_ord);
1114 
1115 		DP_VERBOSE(p_hwfn,
1116 			   QED_MSG_RDMA,
1117 			   "MPA_NEGOTIATE Header: [%x ord:%x ird] %x ord:%x ird:%x peer2peer:%x rtr_send:%x rtr_write:%x rtr_read:%x\n",
1118 			   mpa_v2_params->ird,
1119 			   mpa_v2_params->ord,
1120 			   *((u32 *)mpa_v2_params),
1121 			   mpa_ord & MPA_V2_IRD_ORD_MASK,
1122 			   mpa_ird & MPA_V2_IRD_ORD_MASK,
1123 			   !!(mpa_ird & MPA_V2_PEER2PEER_MODEL),
1124 			   !!(mpa_ird & MPA_V2_SEND_RTR),
1125 			   !!(mpa_ord & MPA_V2_WRITE_RTR),
1126 			   !!(mpa_ord & MPA_V2_READ_RTR));
1127 	}
1128 }
1129 
1130 int qed_iwarp_connect(void *rdma_cxt,
1131 		      struct qed_iwarp_connect_in *iparams,
1132 		      struct qed_iwarp_connect_out *oparams)
1133 {
1134 	struct qed_hwfn *p_hwfn = rdma_cxt;
1135 	struct qed_iwarp_info *iwarp_info;
1136 	struct qed_iwarp_ep *ep;
1137 	u8 mpa_data_size = 0;
1138 	u8 ts_hdr_size = 0;
1139 	u32 cid;
1140 	int rc;
1141 
1142 	if ((iparams->cm_info.ord > QED_IWARP_ORD_DEFAULT) ||
1143 	    (iparams->cm_info.ird > QED_IWARP_IRD_DEFAULT)) {
1144 		DP_NOTICE(p_hwfn,
1145 			  "QP(0x%x) ERROR: Invalid ord(0x%x)/ird(0x%x)\n",
1146 			  iparams->qp->icid, iparams->cm_info.ord,
1147 			  iparams->cm_info.ird);
1148 
1149 		return -EINVAL;
1150 	}
1151 
1152 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
1153 
1154 	/* Allocate ep object */
1155 	rc = qed_iwarp_alloc_cid(p_hwfn, &cid);
1156 	if (rc)
1157 		return rc;
1158 
1159 	rc = qed_iwarp_create_ep(p_hwfn, &ep);
1160 	if (rc)
1161 		goto err;
1162 
1163 	ep->tcp_cid = cid;
1164 
1165 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1166 	list_add_tail(&ep->list_entry, &p_hwfn->p_rdma_info->iwarp.ep_list);
1167 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1168 
1169 	ep->qp = iparams->qp;
1170 	ep->qp->ep = ep;
1171 	ether_addr_copy(ep->remote_mac_addr, iparams->remote_mac_addr);
1172 	ether_addr_copy(ep->local_mac_addr, iparams->local_mac_addr);
1173 	memcpy(&ep->cm_info, &iparams->cm_info, sizeof(ep->cm_info));
1174 
1175 	ep->cm_info.ord = iparams->cm_info.ord;
1176 	ep->cm_info.ird = iparams->cm_info.ird;
1177 
1178 	ep->rtr_type = iwarp_info->rtr_type;
1179 	if (!iwarp_info->peer2peer)
1180 		ep->rtr_type = MPA_RTR_TYPE_NONE;
1181 
1182 	if ((ep->rtr_type & MPA_RTR_TYPE_ZERO_READ) && (ep->cm_info.ord == 0))
1183 		ep->cm_info.ord = 1;
1184 
1185 	ep->mpa_rev = iwarp_info->mpa_rev;
1186 
1187 	qed_iwarp_mpa_v2_set_private(p_hwfn, ep, &mpa_data_size);
1188 
1189 	ep->cm_info.private_data = ep->ep_buffer_virt->out_pdata;
1190 	ep->cm_info.private_data_len = iparams->cm_info.private_data_len +
1191 				       mpa_data_size;
1192 
1193 	memcpy((u8 *)ep->ep_buffer_virt->out_pdata + mpa_data_size,
1194 	       iparams->cm_info.private_data,
1195 	       iparams->cm_info.private_data_len);
1196 
1197 	if (p_hwfn->p_rdma_info->iwarp.tcp_flags & QED_IWARP_TS_EN)
1198 		ts_hdr_size = TIMESTAMP_HEADER_SIZE;
1199 
1200 	ep->mss = iparams->mss - ts_hdr_size;
1201 	ep->mss = min_t(u16, QED_IWARP_MAX_FW_MSS, ep->mss);
1202 
1203 	ep->event_cb = iparams->event_cb;
1204 	ep->cb_context = iparams->cb_context;
1205 	ep->connect_mode = TCP_CONNECT_ACTIVE;
1206 
1207 	oparams->ep_context = ep;
1208 
1209 	rc = qed_iwarp_tcp_offload(p_hwfn, ep);
1210 
1211 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) EP(0x%x) rc = %d\n",
1212 		   iparams->qp->icid, ep->tcp_cid, rc);
1213 
1214 	if (rc) {
1215 		qed_iwarp_destroy_ep(p_hwfn, ep, true);
1216 		goto err;
1217 	}
1218 
1219 	return rc;
1220 err:
1221 	qed_iwarp_cid_cleaned(p_hwfn, cid);
1222 
1223 	return rc;
1224 }
1225 
1226 static struct qed_iwarp_ep *qed_iwarp_get_free_ep(struct qed_hwfn *p_hwfn)
1227 {
1228 	struct qed_iwarp_ep *ep = NULL;
1229 	int rc;
1230 
1231 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1232 
1233 	if (list_empty(&p_hwfn->p_rdma_info->iwarp.ep_free_list)) {
1234 		DP_ERR(p_hwfn, "Ep list is empty\n");
1235 		goto out;
1236 	}
1237 
1238 	ep = list_first_entry(&p_hwfn->p_rdma_info->iwarp.ep_free_list,
1239 			      struct qed_iwarp_ep, list_entry);
1240 
1241 	/* in some cases we could have failed allocating a tcp cid when added
1242 	 * from accept / failure... retry now..this is not the common case.
1243 	 */
1244 	if (ep->tcp_cid == QED_IWARP_INVALID_TCP_CID) {
1245 		rc = qed_iwarp_alloc_tcp_cid(p_hwfn, &ep->tcp_cid);
1246 
1247 		/* if we fail we could look for another entry with a valid
1248 		 * tcp_cid, but since we don't expect to reach this anyway
1249 		 * it's not worth the handling
1250 		 */
1251 		if (rc) {
1252 			ep->tcp_cid = QED_IWARP_INVALID_TCP_CID;
1253 			ep = NULL;
1254 			goto out;
1255 		}
1256 	}
1257 
1258 	list_del(&ep->list_entry);
1259 
1260 out:
1261 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1262 	return ep;
1263 }
1264 
1265 #define QED_IWARP_MAX_CID_CLEAN_TIME  100
1266 #define QED_IWARP_MAX_NO_PROGRESS_CNT 5
1267 
1268 /* This function waits for all the bits of a bmap to be cleared, as long as
1269  * there is progress ( i.e. the number of bits left to be cleared decreases )
1270  * the function continues.
1271  */
1272 static int
1273 qed_iwarp_wait_cid_map_cleared(struct qed_hwfn *p_hwfn, struct qed_bmap *bmap)
1274 {
1275 	int prev_weight = 0;
1276 	int wait_count = 0;
1277 	int weight = 0;
1278 
1279 	weight = bitmap_weight(bmap->bitmap, bmap->max_count);
1280 	prev_weight = weight;
1281 
1282 	while (weight) {
1283 		msleep(QED_IWARP_MAX_CID_CLEAN_TIME);
1284 
1285 		weight = bitmap_weight(bmap->bitmap, bmap->max_count);
1286 
1287 		if (prev_weight == weight) {
1288 			wait_count++;
1289 		} else {
1290 			prev_weight = weight;
1291 			wait_count = 0;
1292 		}
1293 
1294 		if (wait_count > QED_IWARP_MAX_NO_PROGRESS_CNT) {
1295 			DP_NOTICE(p_hwfn,
1296 				  "%s bitmap wait timed out (%d cids pending)\n",
1297 				  bmap->name, weight);
1298 			return -EBUSY;
1299 		}
1300 	}
1301 	return 0;
1302 }
1303 
1304 static int qed_iwarp_wait_for_all_cids(struct qed_hwfn *p_hwfn)
1305 {
1306 	int rc;
1307 	int i;
1308 
1309 	rc = qed_iwarp_wait_cid_map_cleared(p_hwfn,
1310 					    &p_hwfn->p_rdma_info->tcp_cid_map);
1311 	if (rc)
1312 		return rc;
1313 
1314 	/* Now free the tcp cids from the main cid map */
1315 	for (i = 0; i < QED_IWARP_PREALLOC_CNT; i++)
1316 		qed_bmap_release_id(p_hwfn, &p_hwfn->p_rdma_info->cid_map, i);
1317 
1318 	/* Now wait for all cids to be completed */
1319 	return qed_iwarp_wait_cid_map_cleared(p_hwfn,
1320 					      &p_hwfn->p_rdma_info->cid_map);
1321 }
1322 
1323 static void qed_iwarp_free_prealloc_ep(struct qed_hwfn *p_hwfn)
1324 {
1325 	struct qed_iwarp_ep *ep;
1326 
1327 	while (!list_empty(&p_hwfn->p_rdma_info->iwarp.ep_free_list)) {
1328 		spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1329 
1330 		ep = list_first_entry(&p_hwfn->p_rdma_info->iwarp.ep_free_list,
1331 				      struct qed_iwarp_ep, list_entry);
1332 
1333 		if (!ep) {
1334 			spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1335 			break;
1336 		}
1337 		list_del(&ep->list_entry);
1338 
1339 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1340 
1341 		if (ep->tcp_cid != QED_IWARP_INVALID_TCP_CID)
1342 			qed_iwarp_cid_cleaned(p_hwfn, ep->tcp_cid);
1343 
1344 		qed_iwarp_destroy_ep(p_hwfn, ep, false);
1345 	}
1346 }
1347 
1348 static int qed_iwarp_prealloc_ep(struct qed_hwfn *p_hwfn, bool init)
1349 {
1350 	struct qed_iwarp_ep *ep;
1351 	int rc = 0;
1352 	int count;
1353 	u32 cid;
1354 	int i;
1355 
1356 	count = init ? QED_IWARP_PREALLOC_CNT : 1;
1357 	for (i = 0; i < count; i++) {
1358 		rc = qed_iwarp_create_ep(p_hwfn, &ep);
1359 		if (rc)
1360 			return rc;
1361 
1362 		/* During initialization we allocate from the main pool,
1363 		 * afterwards we allocate only from the tcp_cid.
1364 		 */
1365 		if (init) {
1366 			rc = qed_iwarp_alloc_cid(p_hwfn, &cid);
1367 			if (rc)
1368 				goto err;
1369 			qed_iwarp_set_tcp_cid(p_hwfn, cid);
1370 		} else {
1371 			/* We don't care about the return code, it's ok if
1372 			 * tcp_cid remains invalid...in this case we'll
1373 			 * defer allocation
1374 			 */
1375 			qed_iwarp_alloc_tcp_cid(p_hwfn, &cid);
1376 		}
1377 
1378 		ep->tcp_cid = cid;
1379 
1380 		spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1381 		list_add_tail(&ep->list_entry,
1382 			      &p_hwfn->p_rdma_info->iwarp.ep_free_list);
1383 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1384 	}
1385 
1386 	return rc;
1387 
1388 err:
1389 	qed_iwarp_destroy_ep(p_hwfn, ep, false);
1390 
1391 	return rc;
1392 }
1393 
1394 int qed_iwarp_alloc(struct qed_hwfn *p_hwfn)
1395 {
1396 	int rc;
1397 
1398 	/* Allocate bitmap for tcp cid. These are used by passive side
1399 	 * to ensure it can allocate a tcp cid during dpc that was
1400 	 * pre-acquired and doesn't require dynamic allocation of ilt
1401 	 */
1402 	rc = qed_rdma_bmap_alloc(p_hwfn, &p_hwfn->p_rdma_info->tcp_cid_map,
1403 				 QED_IWARP_PREALLOC_CNT, "TCP_CID");
1404 	if (rc) {
1405 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1406 			   "Failed to allocate tcp cid, rc = %d\n", rc);
1407 		return rc;
1408 	}
1409 
1410 	INIT_LIST_HEAD(&p_hwfn->p_rdma_info->iwarp.ep_free_list);
1411 	spin_lock_init(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1412 
1413 	return qed_iwarp_prealloc_ep(p_hwfn, true);
1414 }
1415 
1416 void qed_iwarp_resc_free(struct qed_hwfn *p_hwfn)
1417 {
1418 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
1419 
1420 	qed_rdma_bmap_free(p_hwfn, &p_hwfn->p_rdma_info->tcp_cid_map, 1);
1421 	kfree(iwarp_info->mpa_bufs);
1422 	kfree(iwarp_info->partial_fpdus);
1423 	kfree(iwarp_info->mpa_intermediate_buf);
1424 }
1425 
1426 int qed_iwarp_accept(void *rdma_cxt, struct qed_iwarp_accept_in *iparams)
1427 {
1428 	struct qed_hwfn *p_hwfn = rdma_cxt;
1429 	struct qed_iwarp_ep *ep;
1430 	u8 mpa_data_size = 0;
1431 	int rc;
1432 
1433 	ep = iparams->ep_context;
1434 	if (!ep) {
1435 		DP_ERR(p_hwfn, "Ep Context receive in accept is NULL\n");
1436 		return -EINVAL;
1437 	}
1438 
1439 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) EP(0x%x)\n",
1440 		   iparams->qp->icid, ep->tcp_cid);
1441 
1442 	if ((iparams->ord > QED_IWARP_ORD_DEFAULT) ||
1443 	    (iparams->ird > QED_IWARP_IRD_DEFAULT)) {
1444 		DP_VERBOSE(p_hwfn,
1445 			   QED_MSG_RDMA,
1446 			   "QP(0x%x) EP(0x%x) ERROR: Invalid ord(0x%x)/ird(0x%x)\n",
1447 			   iparams->qp->icid,
1448 			   ep->tcp_cid, iparams->ord, iparams->ord);
1449 		return -EINVAL;
1450 	}
1451 
1452 	qed_iwarp_prealloc_ep(p_hwfn, false);
1453 
1454 	ep->cb_context = iparams->cb_context;
1455 	ep->qp = iparams->qp;
1456 	ep->qp->ep = ep;
1457 
1458 	if (ep->mpa_rev == MPA_NEGOTIATION_TYPE_ENHANCED) {
1459 		/* Negotiate ord/ird: if upperlayer requested ord larger than
1460 		 * ird advertised by remote, we need to decrease our ord
1461 		 */
1462 		if (iparams->ord > ep->cm_info.ird)
1463 			iparams->ord = ep->cm_info.ird;
1464 
1465 		if ((ep->rtr_type & MPA_RTR_TYPE_ZERO_READ) &&
1466 		    (iparams->ird == 0))
1467 			iparams->ird = 1;
1468 	}
1469 
1470 	/* Update cm_info ord/ird to be negotiated values */
1471 	ep->cm_info.ord = iparams->ord;
1472 	ep->cm_info.ird = iparams->ird;
1473 
1474 	qed_iwarp_mpa_v2_set_private(p_hwfn, ep, &mpa_data_size);
1475 
1476 	ep->cm_info.private_data = ep->ep_buffer_virt->out_pdata;
1477 	ep->cm_info.private_data_len = iparams->private_data_len +
1478 				       mpa_data_size;
1479 
1480 	memcpy((u8 *)ep->ep_buffer_virt->out_pdata + mpa_data_size,
1481 	       iparams->private_data, iparams->private_data_len);
1482 
1483 	rc = qed_iwarp_mpa_offload(p_hwfn, ep);
1484 	if (rc)
1485 		qed_iwarp_modify_qp(p_hwfn,
1486 				    iparams->qp, QED_IWARP_QP_STATE_ERROR, 1);
1487 
1488 	return rc;
1489 }
1490 
1491 int qed_iwarp_reject(void *rdma_cxt, struct qed_iwarp_reject_in *iparams)
1492 {
1493 	struct qed_hwfn *p_hwfn = rdma_cxt;
1494 	struct qed_iwarp_ep *ep;
1495 	u8 mpa_data_size = 0;
1496 
1497 	ep = iparams->ep_context;
1498 	if (!ep) {
1499 		DP_ERR(p_hwfn, "Ep Context receive in reject is NULL\n");
1500 		return -EINVAL;
1501 	}
1502 
1503 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "EP(0x%x)\n", ep->tcp_cid);
1504 
1505 	ep->cb_context = iparams->cb_context;
1506 	ep->qp = NULL;
1507 
1508 	qed_iwarp_mpa_v2_set_private(p_hwfn, ep, &mpa_data_size);
1509 
1510 	ep->cm_info.private_data = ep->ep_buffer_virt->out_pdata;
1511 	ep->cm_info.private_data_len = iparams->private_data_len +
1512 				       mpa_data_size;
1513 
1514 	memcpy((u8 *)ep->ep_buffer_virt->out_pdata + mpa_data_size,
1515 	       iparams->private_data, iparams->private_data_len);
1516 
1517 	return qed_iwarp_mpa_offload(p_hwfn, ep);
1518 }
1519 
1520 static void
1521 qed_iwarp_print_cm_info(struct qed_hwfn *p_hwfn,
1522 			struct qed_iwarp_cm_info *cm_info)
1523 {
1524 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "ip_version = %d\n",
1525 		   cm_info->ip_version);
1526 
1527 	if (cm_info->ip_version == QED_TCP_IPV4)
1528 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1529 			   "remote_ip %pI4h:%x, local_ip %pI4h:%x vlan=%x\n",
1530 			   cm_info->remote_ip, cm_info->remote_port,
1531 			   cm_info->local_ip, cm_info->local_port,
1532 			   cm_info->vlan);
1533 	else
1534 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1535 			   "remote_ip %pI6:%x, local_ip %pI6:%x vlan=%x\n",
1536 			   cm_info->remote_ip, cm_info->remote_port,
1537 			   cm_info->local_ip, cm_info->local_port,
1538 			   cm_info->vlan);
1539 
1540 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1541 		   "private_data_len = %x ord = %d, ird = %d\n",
1542 		   cm_info->private_data_len, cm_info->ord, cm_info->ird);
1543 }
1544 
1545 static int
1546 qed_iwarp_ll2_post_rx(struct qed_hwfn *p_hwfn,
1547 		      struct qed_iwarp_ll2_buff *buf, u8 handle)
1548 {
1549 	int rc;
1550 
1551 	rc = qed_ll2_post_rx_buffer(p_hwfn, handle, buf->data_phys_addr,
1552 				    (u16)buf->buff_size, buf, 1);
1553 	if (rc) {
1554 		DP_NOTICE(p_hwfn,
1555 			  "Failed to repost rx buffer to ll2 rc = %d, handle=%d\n",
1556 			  rc, handle);
1557 		dma_free_coherent(&p_hwfn->cdev->pdev->dev, buf->buff_size,
1558 				  buf->data, buf->data_phys_addr);
1559 		kfree(buf);
1560 	}
1561 
1562 	return rc;
1563 }
1564 
1565 static bool
1566 qed_iwarp_ep_exists(struct qed_hwfn *p_hwfn, struct qed_iwarp_cm_info *cm_info)
1567 {
1568 	struct qed_iwarp_ep *ep = NULL;
1569 	bool found = false;
1570 
1571 	list_for_each_entry(ep,
1572 			    &p_hwfn->p_rdma_info->iwarp.ep_list,
1573 			    list_entry) {
1574 		if ((ep->cm_info.local_port == cm_info->local_port) &&
1575 		    (ep->cm_info.remote_port == cm_info->remote_port) &&
1576 		    (ep->cm_info.vlan == cm_info->vlan) &&
1577 		    !memcmp(&ep->cm_info.local_ip, cm_info->local_ip,
1578 			    sizeof(cm_info->local_ip)) &&
1579 		    !memcmp(&ep->cm_info.remote_ip, cm_info->remote_ip,
1580 			    sizeof(cm_info->remote_ip))) {
1581 			found = true;
1582 			break;
1583 		}
1584 	}
1585 
1586 	if (found) {
1587 		DP_NOTICE(p_hwfn,
1588 			  "SYN received on active connection - dropping\n");
1589 		qed_iwarp_print_cm_info(p_hwfn, cm_info);
1590 
1591 		return true;
1592 	}
1593 
1594 	return false;
1595 }
1596 
1597 static struct qed_iwarp_listener *
1598 qed_iwarp_get_listener(struct qed_hwfn *p_hwfn,
1599 		       struct qed_iwarp_cm_info *cm_info)
1600 {
1601 	struct qed_iwarp_listener *listener = NULL;
1602 	static const u32 ip_zero[4] = { 0, 0, 0, 0 };
1603 	bool found = false;
1604 
1605 	qed_iwarp_print_cm_info(p_hwfn, cm_info);
1606 
1607 	list_for_each_entry(listener,
1608 			    &p_hwfn->p_rdma_info->iwarp.listen_list,
1609 			    list_entry) {
1610 		if (listener->port == cm_info->local_port) {
1611 			if (!memcmp(listener->ip_addr,
1612 				    ip_zero, sizeof(ip_zero))) {
1613 				found = true;
1614 				break;
1615 			}
1616 
1617 			if (!memcmp(listener->ip_addr,
1618 				    cm_info->local_ip,
1619 				    sizeof(cm_info->local_ip)) &&
1620 			    (listener->vlan == cm_info->vlan)) {
1621 				found = true;
1622 				break;
1623 			}
1624 		}
1625 	}
1626 
1627 	if (found) {
1628 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "listener found = %p\n",
1629 			   listener);
1630 		return listener;
1631 	}
1632 
1633 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "listener not found\n");
1634 	return NULL;
1635 }
1636 
1637 static int
1638 qed_iwarp_parse_rx_pkt(struct qed_hwfn *p_hwfn,
1639 		       struct qed_iwarp_cm_info *cm_info,
1640 		       void *buf,
1641 		       u8 *remote_mac_addr,
1642 		       u8 *local_mac_addr,
1643 		       int *payload_len, int *tcp_start_offset)
1644 {
1645 	struct vlan_ethhdr *vethh;
1646 	bool vlan_valid = false;
1647 	struct ipv6hdr *ip6h;
1648 	struct ethhdr *ethh;
1649 	struct tcphdr *tcph;
1650 	struct iphdr *iph;
1651 	int eth_hlen;
1652 	int ip_hlen;
1653 	int eth_type;
1654 	int i;
1655 
1656 	ethh = buf;
1657 	eth_type = ntohs(ethh->h_proto);
1658 	if (eth_type == ETH_P_8021Q) {
1659 		vlan_valid = true;
1660 		vethh = (struct vlan_ethhdr *)ethh;
1661 		cm_info->vlan = ntohs(vethh->h_vlan_TCI) & VLAN_VID_MASK;
1662 		eth_type = ntohs(vethh->h_vlan_encapsulated_proto);
1663 	}
1664 
1665 	eth_hlen = ETH_HLEN + (vlan_valid ? sizeof(u32) : 0);
1666 
1667 	ether_addr_copy(remote_mac_addr, ethh->h_source);
1668 	ether_addr_copy(local_mac_addr, ethh->h_dest);
1669 
1670 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "eth_type =%d source mac: %pM\n",
1671 		   eth_type, ethh->h_source);
1672 
1673 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "eth_hlen=%d destination mac: %pM\n",
1674 		   eth_hlen, ethh->h_dest);
1675 
1676 	iph = (struct iphdr *)((u8 *)(ethh) + eth_hlen);
1677 
1678 	if (eth_type == ETH_P_IP) {
1679 		cm_info->local_ip[0] = ntohl(iph->daddr);
1680 		cm_info->remote_ip[0] = ntohl(iph->saddr);
1681 		cm_info->ip_version = TCP_IPV4;
1682 
1683 		ip_hlen = (iph->ihl) * sizeof(u32);
1684 		*payload_len = ntohs(iph->tot_len) - ip_hlen;
1685 	} else if (eth_type == ETH_P_IPV6) {
1686 		ip6h = (struct ipv6hdr *)iph;
1687 		for (i = 0; i < 4; i++) {
1688 			cm_info->local_ip[i] =
1689 			    ntohl(ip6h->daddr.in6_u.u6_addr32[i]);
1690 			cm_info->remote_ip[i] =
1691 			    ntohl(ip6h->saddr.in6_u.u6_addr32[i]);
1692 		}
1693 		cm_info->ip_version = TCP_IPV6;
1694 
1695 		ip_hlen = sizeof(*ip6h);
1696 		*payload_len = ntohs(ip6h->payload_len);
1697 	} else {
1698 		DP_NOTICE(p_hwfn, "Unexpected ethertype on ll2 %x\n", eth_type);
1699 		return -EINVAL;
1700 	}
1701 
1702 	tcph = (struct tcphdr *)((u8 *)iph + ip_hlen);
1703 
1704 	if (!tcph->syn) {
1705 		DP_NOTICE(p_hwfn,
1706 			  "Only SYN type packet expected on this ll2 conn, iph->ihl=%d source=%d dest=%d\n",
1707 			  iph->ihl, tcph->source, tcph->dest);
1708 		return -EINVAL;
1709 	}
1710 
1711 	cm_info->local_port = ntohs(tcph->dest);
1712 	cm_info->remote_port = ntohs(tcph->source);
1713 
1714 	qed_iwarp_print_cm_info(p_hwfn, cm_info);
1715 
1716 	*tcp_start_offset = eth_hlen + ip_hlen;
1717 
1718 	return 0;
1719 }
1720 
1721 static struct qed_iwarp_fpdu *qed_iwarp_get_curr_fpdu(struct qed_hwfn *p_hwfn,
1722 						      u16 cid)
1723 {
1724 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
1725 	struct qed_iwarp_fpdu *partial_fpdu;
1726 	u32 idx;
1727 
1728 	idx = cid - qed_cxt_get_proto_cid_start(p_hwfn, PROTOCOLID_IWARP);
1729 	if (idx >= iwarp_info->max_num_partial_fpdus) {
1730 		DP_ERR(p_hwfn, "Invalid cid %x max_num_partial_fpdus=%x\n", cid,
1731 		       iwarp_info->max_num_partial_fpdus);
1732 		return NULL;
1733 	}
1734 
1735 	partial_fpdu = &iwarp_info->partial_fpdus[idx];
1736 
1737 	return partial_fpdu;
1738 }
1739 
1740 enum qed_iwarp_mpa_pkt_type {
1741 	QED_IWARP_MPA_PKT_PACKED,
1742 	QED_IWARP_MPA_PKT_PARTIAL,
1743 	QED_IWARP_MPA_PKT_UNALIGNED
1744 };
1745 
1746 #define QED_IWARP_INVALID_FPDU_LENGTH 0xffff
1747 #define QED_IWARP_MPA_FPDU_LENGTH_SIZE (2)
1748 #define QED_IWARP_MPA_CRC32_DIGEST_SIZE (4)
1749 
1750 /* Pad to multiple of 4 */
1751 #define QED_IWARP_PDU_DATA_LEN_WITH_PAD(data_len) ALIGN(data_len, 4)
1752 #define QED_IWARP_FPDU_LEN_WITH_PAD(_mpa_len)				   \
1753 	(QED_IWARP_PDU_DATA_LEN_WITH_PAD((_mpa_len) +			   \
1754 					 QED_IWARP_MPA_FPDU_LENGTH_SIZE) + \
1755 					 QED_IWARP_MPA_CRC32_DIGEST_SIZE)
1756 
1757 /* fpdu can be fragmented over maximum 3 bds: header, partial mpa, unaligned */
1758 #define QED_IWARP_MAX_BDS_PER_FPDU 3
1759 
1760 char *pkt_type_str[] = {
1761 	"QED_IWARP_MPA_PKT_PACKED",
1762 	"QED_IWARP_MPA_PKT_PARTIAL",
1763 	"QED_IWARP_MPA_PKT_UNALIGNED"
1764 };
1765 
1766 static int
1767 qed_iwarp_recycle_pkt(struct qed_hwfn *p_hwfn,
1768 		      struct qed_iwarp_fpdu *fpdu,
1769 		      struct qed_iwarp_ll2_buff *buf);
1770 
1771 static enum qed_iwarp_mpa_pkt_type
1772 qed_iwarp_mpa_classify(struct qed_hwfn *p_hwfn,
1773 		       struct qed_iwarp_fpdu *fpdu,
1774 		       u16 tcp_payload_len, u8 *mpa_data)
1775 {
1776 	enum qed_iwarp_mpa_pkt_type pkt_type;
1777 	u16 mpa_len;
1778 
1779 	if (fpdu->incomplete_bytes) {
1780 		pkt_type = QED_IWARP_MPA_PKT_UNALIGNED;
1781 		goto out;
1782 	}
1783 
1784 	/* special case of one byte remaining...
1785 	 * lower byte will be read next packet
1786 	 */
1787 	if (tcp_payload_len == 1) {
1788 		fpdu->fpdu_length = *mpa_data << BITS_PER_BYTE;
1789 		pkt_type = QED_IWARP_MPA_PKT_PARTIAL;
1790 		goto out;
1791 	}
1792 
1793 	mpa_len = ntohs(*((u16 *)(mpa_data)));
1794 	fpdu->fpdu_length = QED_IWARP_FPDU_LEN_WITH_PAD(mpa_len);
1795 
1796 	if (fpdu->fpdu_length <= tcp_payload_len)
1797 		pkt_type = QED_IWARP_MPA_PKT_PACKED;
1798 	else
1799 		pkt_type = QED_IWARP_MPA_PKT_PARTIAL;
1800 
1801 out:
1802 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1803 		   "MPA_ALIGN: %s: fpdu_length=0x%x tcp_payload_len:0x%x\n",
1804 		   pkt_type_str[pkt_type], fpdu->fpdu_length, tcp_payload_len);
1805 
1806 	return pkt_type;
1807 }
1808 
1809 static void
1810 qed_iwarp_init_fpdu(struct qed_iwarp_ll2_buff *buf,
1811 		    struct qed_iwarp_fpdu *fpdu,
1812 		    struct unaligned_opaque_data *pkt_data,
1813 		    u16 tcp_payload_size, u8 placement_offset)
1814 {
1815 	fpdu->mpa_buf = buf;
1816 	fpdu->pkt_hdr = buf->data_phys_addr + placement_offset;
1817 	fpdu->pkt_hdr_size = pkt_data->tcp_payload_offset;
1818 	fpdu->mpa_frag = buf->data_phys_addr + pkt_data->first_mpa_offset;
1819 	fpdu->mpa_frag_virt = (u8 *)(buf->data) + pkt_data->first_mpa_offset;
1820 
1821 	if (tcp_payload_size == 1)
1822 		fpdu->incomplete_bytes = QED_IWARP_INVALID_FPDU_LENGTH;
1823 	else if (tcp_payload_size < fpdu->fpdu_length)
1824 		fpdu->incomplete_bytes = fpdu->fpdu_length - tcp_payload_size;
1825 	else
1826 		fpdu->incomplete_bytes = 0;	/* complete fpdu */
1827 
1828 	fpdu->mpa_frag_len = fpdu->fpdu_length - fpdu->incomplete_bytes;
1829 }
1830 
1831 static int
1832 qed_iwarp_cp_pkt(struct qed_hwfn *p_hwfn,
1833 		 struct qed_iwarp_fpdu *fpdu,
1834 		 struct unaligned_opaque_data *pkt_data,
1835 		 struct qed_iwarp_ll2_buff *buf, u16 tcp_payload_size)
1836 {
1837 	u8 *tmp_buf = p_hwfn->p_rdma_info->iwarp.mpa_intermediate_buf;
1838 	int rc;
1839 
1840 	/* need to copy the data from the partial packet stored in fpdu
1841 	 * to the new buf, for this we also need to move the data currently
1842 	 * placed on the buf. The assumption is that the buffer is big enough
1843 	 * since fpdu_length <= mss, we use an intermediate buffer since
1844 	 * we may need to copy the new data to an overlapping location
1845 	 */
1846 	if ((fpdu->mpa_frag_len + tcp_payload_size) > (u16)buf->buff_size) {
1847 		DP_ERR(p_hwfn,
1848 		       "MPA ALIGN: Unexpected: buffer is not large enough for split fpdu buff_size = %d mpa_frag_len = %d, tcp_payload_size = %d, incomplete_bytes = %d\n",
1849 		       buf->buff_size, fpdu->mpa_frag_len,
1850 		       tcp_payload_size, fpdu->incomplete_bytes);
1851 		return -EINVAL;
1852 	}
1853 
1854 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1855 		   "MPA ALIGN Copying fpdu: [%p, %d] [%p, %d]\n",
1856 		   fpdu->mpa_frag_virt, fpdu->mpa_frag_len,
1857 		   (u8 *)(buf->data) + pkt_data->first_mpa_offset,
1858 		   tcp_payload_size);
1859 
1860 	memcpy(tmp_buf, fpdu->mpa_frag_virt, fpdu->mpa_frag_len);
1861 	memcpy(tmp_buf + fpdu->mpa_frag_len,
1862 	       (u8 *)(buf->data) + pkt_data->first_mpa_offset,
1863 	       tcp_payload_size);
1864 
1865 	rc = qed_iwarp_recycle_pkt(p_hwfn, fpdu, fpdu->mpa_buf);
1866 	if (rc)
1867 		return rc;
1868 
1869 	/* If we managed to post the buffer copy the data to the new buffer
1870 	 * o/w this will occur in the next round...
1871 	 */
1872 	memcpy((u8 *)(buf->data), tmp_buf,
1873 	       fpdu->mpa_frag_len + tcp_payload_size);
1874 
1875 	fpdu->mpa_buf = buf;
1876 	/* fpdu->pkt_hdr remains as is */
1877 	/* fpdu->mpa_frag is overridden with new buf */
1878 	fpdu->mpa_frag = buf->data_phys_addr;
1879 	fpdu->mpa_frag_virt = buf->data;
1880 	fpdu->mpa_frag_len += tcp_payload_size;
1881 
1882 	fpdu->incomplete_bytes -= tcp_payload_size;
1883 
1884 	DP_VERBOSE(p_hwfn,
1885 		   QED_MSG_RDMA,
1886 		   "MPA ALIGN: split fpdu buff_size = %d mpa_frag_len = %d, tcp_payload_size = %d, incomplete_bytes = %d\n",
1887 		   buf->buff_size, fpdu->mpa_frag_len, tcp_payload_size,
1888 		   fpdu->incomplete_bytes);
1889 
1890 	return 0;
1891 }
1892 
1893 static void
1894 qed_iwarp_update_fpdu_length(struct qed_hwfn *p_hwfn,
1895 			     struct qed_iwarp_fpdu *fpdu, u8 *mpa_data)
1896 {
1897 	u16 mpa_len;
1898 
1899 	/* Update incomplete packets if needed */
1900 	if (fpdu->incomplete_bytes == QED_IWARP_INVALID_FPDU_LENGTH) {
1901 		/* Missing lower byte is now available */
1902 		mpa_len = fpdu->fpdu_length | *mpa_data;
1903 		fpdu->fpdu_length = QED_IWARP_FPDU_LEN_WITH_PAD(mpa_len);
1904 		fpdu->mpa_frag_len = fpdu->fpdu_length;
1905 		/* one byte of hdr */
1906 		fpdu->incomplete_bytes = fpdu->fpdu_length - 1;
1907 		DP_VERBOSE(p_hwfn,
1908 			   QED_MSG_RDMA,
1909 			   "MPA_ALIGN: Partial header mpa_len=%x fpdu_length=%x incomplete_bytes=%x\n",
1910 			   mpa_len, fpdu->fpdu_length, fpdu->incomplete_bytes);
1911 	}
1912 }
1913 
1914 #define QED_IWARP_IS_RIGHT_EDGE(_curr_pkt) \
1915 	(GET_FIELD((_curr_pkt)->flags,	   \
1916 		   UNALIGNED_OPAQUE_DATA_PKT_REACHED_WIN_RIGHT_EDGE))
1917 
1918 /* This function is used to recycle a buffer using the ll2 drop option. It
1919  * uses the mechanism to ensure that all buffers posted to tx before this one
1920  * were completed. The buffer sent here will be sent as a cookie in the tx
1921  * completion function and can then be reposted to rx chain when done. The flow
1922  * that requires this is the flow where a FPDU splits over more than 3 tcp
1923  * segments. In this case the driver needs to re-post a rx buffer instead of
1924  * the one received, but driver can't simply repost a buffer it copied from
1925  * as there is a case where the buffer was originally a packed FPDU, and is
1926  * partially posted to FW. Driver needs to ensure FW is done with it.
1927  */
1928 static int
1929 qed_iwarp_recycle_pkt(struct qed_hwfn *p_hwfn,
1930 		      struct qed_iwarp_fpdu *fpdu,
1931 		      struct qed_iwarp_ll2_buff *buf)
1932 {
1933 	struct qed_ll2_tx_pkt_info tx_pkt;
1934 	u8 ll2_handle;
1935 	int rc;
1936 
1937 	memset(&tx_pkt, 0, sizeof(tx_pkt));
1938 	tx_pkt.num_of_bds = 1;
1939 	tx_pkt.tx_dest = QED_LL2_TX_DEST_DROP;
1940 	tx_pkt.l4_hdr_offset_w = fpdu->pkt_hdr_size >> 2;
1941 	tx_pkt.first_frag = fpdu->pkt_hdr;
1942 	tx_pkt.first_frag_len = fpdu->pkt_hdr_size;
1943 	buf->piggy_buf = NULL;
1944 	tx_pkt.cookie = buf;
1945 
1946 	ll2_handle = p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle;
1947 
1948 	rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_handle, &tx_pkt, true);
1949 	if (rc)
1950 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1951 			   "Can't drop packet rc=%d\n", rc);
1952 
1953 	DP_VERBOSE(p_hwfn,
1954 		   QED_MSG_RDMA,
1955 		   "MPA_ALIGN: send drop tx packet [%lx, 0x%x], buf=%p, rc=%d\n",
1956 		   (unsigned long int)tx_pkt.first_frag,
1957 		   tx_pkt.first_frag_len, buf, rc);
1958 
1959 	return rc;
1960 }
1961 
1962 static int
1963 qed_iwarp_win_right_edge(struct qed_hwfn *p_hwfn, struct qed_iwarp_fpdu *fpdu)
1964 {
1965 	struct qed_ll2_tx_pkt_info tx_pkt;
1966 	u8 ll2_handle;
1967 	int rc;
1968 
1969 	memset(&tx_pkt, 0, sizeof(tx_pkt));
1970 	tx_pkt.num_of_bds = 1;
1971 	tx_pkt.tx_dest = QED_LL2_TX_DEST_LB;
1972 	tx_pkt.l4_hdr_offset_w = fpdu->pkt_hdr_size >> 2;
1973 
1974 	tx_pkt.first_frag = fpdu->pkt_hdr;
1975 	tx_pkt.first_frag_len = fpdu->pkt_hdr_size;
1976 	tx_pkt.enable_ip_cksum = true;
1977 	tx_pkt.enable_l4_cksum = true;
1978 	tx_pkt.calc_ip_len = true;
1979 	/* vlan overload with enum iwarp_ll2_tx_queues */
1980 	tx_pkt.vlan = IWARP_LL2_ALIGNED_RIGHT_TRIMMED_TX_QUEUE;
1981 
1982 	ll2_handle = p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle;
1983 
1984 	rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_handle, &tx_pkt, true);
1985 	if (rc)
1986 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1987 			   "Can't send right edge rc=%d\n", rc);
1988 	DP_VERBOSE(p_hwfn,
1989 		   QED_MSG_RDMA,
1990 		   "MPA_ALIGN: Sent right edge FPDU num_bds=%d [%lx, 0x%x], rc=%d\n",
1991 		   tx_pkt.num_of_bds,
1992 		   (unsigned long int)tx_pkt.first_frag,
1993 		   tx_pkt.first_frag_len, rc);
1994 
1995 	return rc;
1996 }
1997 
1998 static int
1999 qed_iwarp_send_fpdu(struct qed_hwfn *p_hwfn,
2000 		    struct qed_iwarp_fpdu *fpdu,
2001 		    struct unaligned_opaque_data *curr_pkt,
2002 		    struct qed_iwarp_ll2_buff *buf,
2003 		    u16 tcp_payload_size, enum qed_iwarp_mpa_pkt_type pkt_type)
2004 {
2005 	struct qed_ll2_tx_pkt_info tx_pkt;
2006 	u8 ll2_handle;
2007 	int rc;
2008 
2009 	memset(&tx_pkt, 0, sizeof(tx_pkt));
2010 
2011 	/* An unaligned packet means it's split over two tcp segments. So the
2012 	 * complete packet requires 3 bds, one for the header, one for the
2013 	 * part of the fpdu of the first tcp segment, and the last fragment
2014 	 * will point to the remainder of the fpdu. A packed pdu, requires only
2015 	 * two bds, one for the header and one for the data.
2016 	 */
2017 	tx_pkt.num_of_bds = (pkt_type == QED_IWARP_MPA_PKT_UNALIGNED) ? 3 : 2;
2018 	tx_pkt.tx_dest = QED_LL2_TX_DEST_LB;
2019 	tx_pkt.l4_hdr_offset_w = fpdu->pkt_hdr_size >> 2; /* offset in words */
2020 
2021 	/* Send the mpa_buf only with the last fpdu (in case of packed) */
2022 	if (pkt_type == QED_IWARP_MPA_PKT_UNALIGNED ||
2023 	    tcp_payload_size <= fpdu->fpdu_length)
2024 		tx_pkt.cookie = fpdu->mpa_buf;
2025 
2026 	tx_pkt.first_frag = fpdu->pkt_hdr;
2027 	tx_pkt.first_frag_len = fpdu->pkt_hdr_size;
2028 	tx_pkt.enable_ip_cksum = true;
2029 	tx_pkt.enable_l4_cksum = true;
2030 	tx_pkt.calc_ip_len = true;
2031 	/* vlan overload with enum iwarp_ll2_tx_queues */
2032 	tx_pkt.vlan = IWARP_LL2_ALIGNED_TX_QUEUE;
2033 
2034 	/* special case of unaligned packet and not packed, need to send
2035 	 * both buffers as cookie to release.
2036 	 */
2037 	if (tcp_payload_size == fpdu->incomplete_bytes)
2038 		fpdu->mpa_buf->piggy_buf = buf;
2039 
2040 	ll2_handle = p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle;
2041 
2042 	/* Set first fragment to header */
2043 	rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_handle, &tx_pkt, true);
2044 	if (rc)
2045 		goto out;
2046 
2047 	/* Set second fragment to first part of packet */
2048 	rc = qed_ll2_set_fragment_of_tx_packet(p_hwfn, ll2_handle,
2049 					       fpdu->mpa_frag,
2050 					       fpdu->mpa_frag_len);
2051 	if (rc)
2052 		goto out;
2053 
2054 	if (!fpdu->incomplete_bytes)
2055 		goto out;
2056 
2057 	/* Set third fragment to second part of the packet */
2058 	rc = qed_ll2_set_fragment_of_tx_packet(p_hwfn,
2059 					       ll2_handle,
2060 					       buf->data_phys_addr +
2061 					       curr_pkt->first_mpa_offset,
2062 					       fpdu->incomplete_bytes);
2063 out:
2064 	DP_VERBOSE(p_hwfn,
2065 		   QED_MSG_RDMA,
2066 		   "MPA_ALIGN: Sent FPDU num_bds=%d first_frag_len=%x, mpa_frag_len=0x%x, incomplete_bytes:0x%x rc=%d\n",
2067 		   tx_pkt.num_of_bds,
2068 		   tx_pkt.first_frag_len,
2069 		   fpdu->mpa_frag_len,
2070 		   fpdu->incomplete_bytes, rc);
2071 
2072 	return rc;
2073 }
2074 
2075 static void
2076 qed_iwarp_mpa_get_data(struct qed_hwfn *p_hwfn,
2077 		       struct unaligned_opaque_data *curr_pkt,
2078 		       u32 opaque_data0, u32 opaque_data1)
2079 {
2080 	u64 opaque_data;
2081 
2082 	opaque_data = HILO_64(opaque_data1, opaque_data0);
2083 	*curr_pkt = *((struct unaligned_opaque_data *)&opaque_data);
2084 
2085 	curr_pkt->first_mpa_offset = curr_pkt->tcp_payload_offset +
2086 				     le16_to_cpu(curr_pkt->first_mpa_offset);
2087 	curr_pkt->cid = le32_to_cpu(curr_pkt->cid);
2088 }
2089 
2090 /* This function is called when an unaligned or incomplete MPA packet arrives
2091  * driver needs to align the packet, perhaps using previous data and send
2092  * it down to FW once it is aligned.
2093  */
2094 static int
2095 qed_iwarp_process_mpa_pkt(struct qed_hwfn *p_hwfn,
2096 			  struct qed_iwarp_ll2_mpa_buf *mpa_buf)
2097 {
2098 	struct unaligned_opaque_data *curr_pkt = &mpa_buf->data;
2099 	struct qed_iwarp_ll2_buff *buf = mpa_buf->ll2_buf;
2100 	enum qed_iwarp_mpa_pkt_type pkt_type;
2101 	struct qed_iwarp_fpdu *fpdu;
2102 	int rc = -EINVAL;
2103 	u8 *mpa_data;
2104 
2105 	fpdu = qed_iwarp_get_curr_fpdu(p_hwfn, curr_pkt->cid & 0xffff);
2106 	if (!fpdu) { /* something corrupt with cid, post rx back */
2107 		DP_ERR(p_hwfn, "Invalid cid, drop and post back to rx cid=%x\n",
2108 		       curr_pkt->cid);
2109 		goto err;
2110 	}
2111 
2112 	do {
2113 		mpa_data = ((u8 *)(buf->data) + curr_pkt->first_mpa_offset);
2114 
2115 		pkt_type = qed_iwarp_mpa_classify(p_hwfn, fpdu,
2116 						  mpa_buf->tcp_payload_len,
2117 						  mpa_data);
2118 
2119 		switch (pkt_type) {
2120 		case QED_IWARP_MPA_PKT_PARTIAL:
2121 			qed_iwarp_init_fpdu(buf, fpdu,
2122 					    curr_pkt,
2123 					    mpa_buf->tcp_payload_len,
2124 					    mpa_buf->placement_offset);
2125 
2126 			if (!QED_IWARP_IS_RIGHT_EDGE(curr_pkt)) {
2127 				mpa_buf->tcp_payload_len = 0;
2128 				break;
2129 			}
2130 
2131 			rc = qed_iwarp_win_right_edge(p_hwfn, fpdu);
2132 
2133 			if (rc) {
2134 				DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2135 					   "Can't send FPDU:reset rc=%d\n", rc);
2136 				memset(fpdu, 0, sizeof(*fpdu));
2137 				break;
2138 			}
2139 
2140 			mpa_buf->tcp_payload_len = 0;
2141 			break;
2142 		case QED_IWARP_MPA_PKT_PACKED:
2143 			qed_iwarp_init_fpdu(buf, fpdu,
2144 					    curr_pkt,
2145 					    mpa_buf->tcp_payload_len,
2146 					    mpa_buf->placement_offset);
2147 
2148 			rc = qed_iwarp_send_fpdu(p_hwfn, fpdu, curr_pkt, buf,
2149 						 mpa_buf->tcp_payload_len,
2150 						 pkt_type);
2151 			if (rc) {
2152 				DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2153 					   "Can't send FPDU:reset rc=%d\n", rc);
2154 				memset(fpdu, 0, sizeof(*fpdu));
2155 				break;
2156 			}
2157 
2158 			mpa_buf->tcp_payload_len -= fpdu->fpdu_length;
2159 			curr_pkt->first_mpa_offset += fpdu->fpdu_length;
2160 			break;
2161 		case QED_IWARP_MPA_PKT_UNALIGNED:
2162 			qed_iwarp_update_fpdu_length(p_hwfn, fpdu, mpa_data);
2163 			if (mpa_buf->tcp_payload_len < fpdu->incomplete_bytes) {
2164 				/* special handling of fpdu split over more
2165 				 * than 2 segments
2166 				 */
2167 				if (QED_IWARP_IS_RIGHT_EDGE(curr_pkt)) {
2168 					rc = qed_iwarp_win_right_edge(p_hwfn,
2169 								      fpdu);
2170 					/* packet will be re-processed later */
2171 					if (rc)
2172 						return rc;
2173 				}
2174 
2175 				rc = qed_iwarp_cp_pkt(p_hwfn, fpdu, curr_pkt,
2176 						      buf,
2177 						      mpa_buf->tcp_payload_len);
2178 				if (rc) /* packet will be re-processed later */
2179 					return rc;
2180 
2181 				mpa_buf->tcp_payload_len = 0;
2182 				break;
2183 			}
2184 
2185 			rc = qed_iwarp_send_fpdu(p_hwfn, fpdu, curr_pkt, buf,
2186 						 mpa_buf->tcp_payload_len,
2187 						 pkt_type);
2188 			if (rc) {
2189 				DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2190 					   "Can't send FPDU:delay rc=%d\n", rc);
2191 				/* don't reset fpdu -> we need it for next
2192 				 * classify
2193 				 */
2194 				break;
2195 			}
2196 
2197 			mpa_buf->tcp_payload_len -= fpdu->incomplete_bytes;
2198 			curr_pkt->first_mpa_offset += fpdu->incomplete_bytes;
2199 			/* The framed PDU was sent - no more incomplete bytes */
2200 			fpdu->incomplete_bytes = 0;
2201 			break;
2202 		}
2203 	} while (mpa_buf->tcp_payload_len && !rc);
2204 
2205 	return rc;
2206 
2207 err:
2208 	qed_iwarp_ll2_post_rx(p_hwfn,
2209 			      buf,
2210 			      p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle);
2211 	return rc;
2212 }
2213 
2214 static void qed_iwarp_process_pending_pkts(struct qed_hwfn *p_hwfn)
2215 {
2216 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2217 	struct qed_iwarp_ll2_mpa_buf *mpa_buf = NULL;
2218 	int rc;
2219 
2220 	while (!list_empty(&iwarp_info->mpa_buf_pending_list)) {
2221 		mpa_buf = list_first_entry(&iwarp_info->mpa_buf_pending_list,
2222 					   struct qed_iwarp_ll2_mpa_buf,
2223 					   list_entry);
2224 
2225 		rc = qed_iwarp_process_mpa_pkt(p_hwfn, mpa_buf);
2226 
2227 		/* busy means break and continue processing later, don't
2228 		 * remove the buf from the pending list.
2229 		 */
2230 		if (rc == -EBUSY)
2231 			break;
2232 
2233 		list_del(&mpa_buf->list_entry);
2234 		list_add_tail(&mpa_buf->list_entry, &iwarp_info->mpa_buf_list);
2235 
2236 		if (rc) {	/* different error, don't continue */
2237 			DP_NOTICE(p_hwfn, "process pkts failed rc=%d\n", rc);
2238 			break;
2239 		}
2240 	}
2241 }
2242 
2243 static void
2244 qed_iwarp_ll2_comp_mpa_pkt(void *cxt, struct qed_ll2_comp_rx_data *data)
2245 {
2246 	struct qed_iwarp_ll2_mpa_buf *mpa_buf;
2247 	struct qed_iwarp_info *iwarp_info;
2248 	struct qed_hwfn *p_hwfn = cxt;
2249 
2250 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2251 	mpa_buf = list_first_entry(&iwarp_info->mpa_buf_list,
2252 				   struct qed_iwarp_ll2_mpa_buf, list_entry);
2253 	if (!mpa_buf) {
2254 		DP_ERR(p_hwfn, "No free mpa buf\n");
2255 		goto err;
2256 	}
2257 
2258 	list_del(&mpa_buf->list_entry);
2259 	qed_iwarp_mpa_get_data(p_hwfn, &mpa_buf->data,
2260 			       data->opaque_data_0, data->opaque_data_1);
2261 
2262 	DP_VERBOSE(p_hwfn,
2263 		   QED_MSG_RDMA,
2264 		   "LL2 MPA CompRx payload_len:0x%x\tfirst_mpa_offset:0x%x\ttcp_payload_offset:0x%x\tflags:0x%x\tcid:0x%x\n",
2265 		   data->length.packet_length, mpa_buf->data.first_mpa_offset,
2266 		   mpa_buf->data.tcp_payload_offset, mpa_buf->data.flags,
2267 		   mpa_buf->data.cid);
2268 
2269 	mpa_buf->ll2_buf = data->cookie;
2270 	mpa_buf->tcp_payload_len = data->length.packet_length -
2271 				   mpa_buf->data.first_mpa_offset;
2272 	mpa_buf->data.first_mpa_offset += data->u.placement_offset;
2273 	mpa_buf->placement_offset = data->u.placement_offset;
2274 
2275 	list_add_tail(&mpa_buf->list_entry, &iwarp_info->mpa_buf_pending_list);
2276 
2277 	qed_iwarp_process_pending_pkts(p_hwfn);
2278 	return;
2279 err:
2280 	qed_iwarp_ll2_post_rx(p_hwfn, data->cookie,
2281 			      iwarp_info->ll2_mpa_handle);
2282 }
2283 
2284 static void
2285 qed_iwarp_ll2_comp_syn_pkt(void *cxt, struct qed_ll2_comp_rx_data *data)
2286 {
2287 	struct qed_iwarp_ll2_buff *buf = data->cookie;
2288 	struct qed_iwarp_listener *listener;
2289 	struct qed_ll2_tx_pkt_info tx_pkt;
2290 	struct qed_iwarp_cm_info cm_info;
2291 	struct qed_hwfn *p_hwfn = cxt;
2292 	u8 remote_mac_addr[ETH_ALEN];
2293 	u8 local_mac_addr[ETH_ALEN];
2294 	struct qed_iwarp_ep *ep;
2295 	int tcp_start_offset;
2296 	u8 ts_hdr_size = 0;
2297 	u8 ll2_syn_handle;
2298 	int payload_len;
2299 	u32 hdr_size;
2300 	int rc;
2301 
2302 	memset(&cm_info, 0, sizeof(cm_info));
2303 	ll2_syn_handle = p_hwfn->p_rdma_info->iwarp.ll2_syn_handle;
2304 
2305 	/* Check if packet was received with errors... */
2306 	if (data->err_flags) {
2307 		DP_NOTICE(p_hwfn, "Error received on SYN packet: 0x%x\n",
2308 			  data->err_flags);
2309 		goto err;
2310 	}
2311 
2312 	if (GET_FIELD(data->parse_flags,
2313 		      PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED) &&
2314 	    GET_FIELD(data->parse_flags, PARSING_AND_ERR_FLAGS_L4CHKSMERROR)) {
2315 		DP_NOTICE(p_hwfn, "Syn packet received with checksum error\n");
2316 		goto err;
2317 	}
2318 
2319 	rc = qed_iwarp_parse_rx_pkt(p_hwfn, &cm_info, (u8 *)(buf->data) +
2320 				    data->u.placement_offset, remote_mac_addr,
2321 				    local_mac_addr, &payload_len,
2322 				    &tcp_start_offset);
2323 	if (rc)
2324 		goto err;
2325 
2326 	/* Check if there is a listener for this 4-tuple+vlan */
2327 	listener = qed_iwarp_get_listener(p_hwfn, &cm_info);
2328 	if (!listener) {
2329 		DP_VERBOSE(p_hwfn,
2330 			   QED_MSG_RDMA,
2331 			   "SYN received on tuple not listened on parse_flags=%d packet len=%d\n",
2332 			   data->parse_flags, data->length.packet_length);
2333 
2334 		memset(&tx_pkt, 0, sizeof(tx_pkt));
2335 		tx_pkt.num_of_bds = 1;
2336 		tx_pkt.vlan = data->vlan;
2337 
2338 		if (GET_FIELD(data->parse_flags,
2339 			      PARSING_AND_ERR_FLAGS_TAG8021QEXIST))
2340 			SET_FIELD(tx_pkt.bd_flags,
2341 				  CORE_TX_BD_DATA_VLAN_INSERTION, 1);
2342 
2343 		tx_pkt.l4_hdr_offset_w = (data->length.packet_length) >> 2;
2344 		tx_pkt.tx_dest = QED_LL2_TX_DEST_LB;
2345 		tx_pkt.first_frag = buf->data_phys_addr +
2346 				    data->u.placement_offset;
2347 		tx_pkt.first_frag_len = data->length.packet_length;
2348 		tx_pkt.cookie = buf;
2349 
2350 		rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_syn_handle,
2351 					       &tx_pkt, true);
2352 
2353 		if (rc) {
2354 			DP_NOTICE(p_hwfn,
2355 				  "Can't post SYN back to chip rc=%d\n", rc);
2356 			goto err;
2357 		}
2358 		return;
2359 	}
2360 
2361 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Received syn on listening port\n");
2362 	/* There may be an open ep on this connection if this is a syn
2363 	 * retrasnmit... need to make sure there isn't...
2364 	 */
2365 	if (qed_iwarp_ep_exists(p_hwfn, &cm_info))
2366 		goto err;
2367 
2368 	ep = qed_iwarp_get_free_ep(p_hwfn);
2369 	if (!ep)
2370 		goto err;
2371 
2372 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2373 	list_add_tail(&ep->list_entry, &p_hwfn->p_rdma_info->iwarp.ep_list);
2374 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2375 
2376 	ether_addr_copy(ep->remote_mac_addr, remote_mac_addr);
2377 	ether_addr_copy(ep->local_mac_addr, local_mac_addr);
2378 
2379 	memcpy(&ep->cm_info, &cm_info, sizeof(ep->cm_info));
2380 
2381 	if (p_hwfn->p_rdma_info->iwarp.tcp_flags & QED_IWARP_TS_EN)
2382 		ts_hdr_size = TIMESTAMP_HEADER_SIZE;
2383 
2384 	hdr_size = ((cm_info.ip_version == QED_TCP_IPV4) ? 40 : 60) +
2385 		   ts_hdr_size;
2386 	ep->mss = p_hwfn->p_rdma_info->iwarp.max_mtu - hdr_size;
2387 	ep->mss = min_t(u16, QED_IWARP_MAX_FW_MSS, ep->mss);
2388 
2389 	ep->event_cb = listener->event_cb;
2390 	ep->cb_context = listener->cb_context;
2391 	ep->connect_mode = TCP_CONNECT_PASSIVE;
2392 
2393 	ep->syn = buf;
2394 	ep->syn_ip_payload_length = (u16)payload_len;
2395 	ep->syn_phy_addr = buf->data_phys_addr + data->u.placement_offset +
2396 			   tcp_start_offset;
2397 
2398 	rc = qed_iwarp_tcp_offload(p_hwfn, ep);
2399 	if (rc) {
2400 		qed_iwarp_return_ep(p_hwfn, ep);
2401 		goto err;
2402 	}
2403 
2404 	return;
2405 err:
2406 	qed_iwarp_ll2_post_rx(p_hwfn, buf, ll2_syn_handle);
2407 }
2408 
2409 static void qed_iwarp_ll2_rel_rx_pkt(void *cxt, u8 connection_handle,
2410 				     void *cookie, dma_addr_t rx_buf_addr,
2411 				     bool b_last_packet)
2412 {
2413 	struct qed_iwarp_ll2_buff *buffer = cookie;
2414 	struct qed_hwfn *p_hwfn = cxt;
2415 
2416 	dma_free_coherent(&p_hwfn->cdev->pdev->dev, buffer->buff_size,
2417 			  buffer->data, buffer->data_phys_addr);
2418 	kfree(buffer);
2419 }
2420 
2421 static void qed_iwarp_ll2_comp_tx_pkt(void *cxt, u8 connection_handle,
2422 				      void *cookie, dma_addr_t first_frag_addr,
2423 				      bool b_last_fragment, bool b_last_packet)
2424 {
2425 	struct qed_iwarp_ll2_buff *buffer = cookie;
2426 	struct qed_iwarp_ll2_buff *piggy;
2427 	struct qed_hwfn *p_hwfn = cxt;
2428 
2429 	if (!buffer)		/* can happen in packed mpa unaligned... */
2430 		return;
2431 
2432 	/* this was originally an rx packet, post it back */
2433 	piggy = buffer->piggy_buf;
2434 	if (piggy) {
2435 		buffer->piggy_buf = NULL;
2436 		qed_iwarp_ll2_post_rx(p_hwfn, piggy, connection_handle);
2437 	}
2438 
2439 	qed_iwarp_ll2_post_rx(p_hwfn, buffer, connection_handle);
2440 
2441 	if (connection_handle == p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle)
2442 		qed_iwarp_process_pending_pkts(p_hwfn);
2443 
2444 	return;
2445 }
2446 
2447 static void qed_iwarp_ll2_rel_tx_pkt(void *cxt, u8 connection_handle,
2448 				     void *cookie, dma_addr_t first_frag_addr,
2449 				     bool b_last_fragment, bool b_last_packet)
2450 {
2451 	struct qed_iwarp_ll2_buff *buffer = cookie;
2452 	struct qed_hwfn *p_hwfn = cxt;
2453 
2454 	if (!buffer)
2455 		return;
2456 
2457 	if (buffer->piggy_buf) {
2458 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
2459 				  buffer->piggy_buf->buff_size,
2460 				  buffer->piggy_buf->data,
2461 				  buffer->piggy_buf->data_phys_addr);
2462 
2463 		kfree(buffer->piggy_buf);
2464 	}
2465 
2466 	dma_free_coherent(&p_hwfn->cdev->pdev->dev, buffer->buff_size,
2467 			  buffer->data, buffer->data_phys_addr);
2468 
2469 	kfree(buffer);
2470 }
2471 
2472 /* The only slowpath for iwarp ll2 is unalign flush. When this completion
2473  * is received, need to reset the FPDU.
2474  */
2475 void
2476 qed_iwarp_ll2_slowpath(void *cxt,
2477 		       u8 connection_handle,
2478 		       u32 opaque_data_0, u32 opaque_data_1)
2479 {
2480 	struct unaligned_opaque_data unalign_data;
2481 	struct qed_hwfn *p_hwfn = cxt;
2482 	struct qed_iwarp_fpdu *fpdu;
2483 
2484 	qed_iwarp_mpa_get_data(p_hwfn, &unalign_data,
2485 			       opaque_data_0, opaque_data_1);
2486 
2487 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "(0x%x) Flush fpdu\n",
2488 		   unalign_data.cid);
2489 
2490 	fpdu = qed_iwarp_get_curr_fpdu(p_hwfn, (u16)unalign_data.cid);
2491 	if (fpdu)
2492 		memset(fpdu, 0, sizeof(*fpdu));
2493 }
2494 
2495 static int qed_iwarp_ll2_stop(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2496 {
2497 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2498 	int rc = 0;
2499 
2500 	if (iwarp_info->ll2_syn_handle != QED_IWARP_HANDLE_INVAL) {
2501 		rc = qed_ll2_terminate_connection(p_hwfn,
2502 						  iwarp_info->ll2_syn_handle);
2503 		if (rc)
2504 			DP_INFO(p_hwfn, "Failed to terminate syn connection\n");
2505 
2506 		qed_ll2_release_connection(p_hwfn, iwarp_info->ll2_syn_handle);
2507 		iwarp_info->ll2_syn_handle = QED_IWARP_HANDLE_INVAL;
2508 	}
2509 
2510 	if (iwarp_info->ll2_ooo_handle != QED_IWARP_HANDLE_INVAL) {
2511 		rc = qed_ll2_terminate_connection(p_hwfn,
2512 						  iwarp_info->ll2_ooo_handle);
2513 		if (rc)
2514 			DP_INFO(p_hwfn, "Failed to terminate ooo connection\n");
2515 
2516 		qed_ll2_release_connection(p_hwfn, iwarp_info->ll2_ooo_handle);
2517 		iwarp_info->ll2_ooo_handle = QED_IWARP_HANDLE_INVAL;
2518 	}
2519 
2520 	if (iwarp_info->ll2_mpa_handle != QED_IWARP_HANDLE_INVAL) {
2521 		rc = qed_ll2_terminate_connection(p_hwfn,
2522 						  iwarp_info->ll2_mpa_handle);
2523 		if (rc)
2524 			DP_INFO(p_hwfn, "Failed to terminate mpa connection\n");
2525 
2526 		qed_ll2_release_connection(p_hwfn, iwarp_info->ll2_mpa_handle);
2527 		iwarp_info->ll2_mpa_handle = QED_IWARP_HANDLE_INVAL;
2528 	}
2529 
2530 	qed_llh_remove_mac_filter(p_hwfn,
2531 				  p_ptt, p_hwfn->p_rdma_info->iwarp.mac_addr);
2532 	return rc;
2533 }
2534 
2535 static int
2536 qed_iwarp_ll2_alloc_buffers(struct qed_hwfn *p_hwfn,
2537 			    int num_rx_bufs, int buff_size, u8 ll2_handle)
2538 {
2539 	struct qed_iwarp_ll2_buff *buffer;
2540 	int rc = 0;
2541 	int i;
2542 
2543 	for (i = 0; i < num_rx_bufs; i++) {
2544 		buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
2545 		if (!buffer) {
2546 			rc = -ENOMEM;
2547 			break;
2548 		}
2549 
2550 		buffer->data = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
2551 						  buff_size,
2552 						  &buffer->data_phys_addr,
2553 						  GFP_KERNEL);
2554 		if (!buffer->data) {
2555 			kfree(buffer);
2556 			rc = -ENOMEM;
2557 			break;
2558 		}
2559 
2560 		buffer->buff_size = buff_size;
2561 		rc = qed_iwarp_ll2_post_rx(p_hwfn, buffer, ll2_handle);
2562 		if (rc)
2563 			/* buffers will be deallocated by qed_ll2 */
2564 			break;
2565 	}
2566 	return rc;
2567 }
2568 
2569 #define QED_IWARP_MAX_BUF_SIZE(mtu)				     \
2570 	ALIGN((mtu) + ETH_HLEN + 2 * VLAN_HLEN + 2 + ETH_CACHE_LINE_SIZE, \
2571 		ETH_CACHE_LINE_SIZE)
2572 
2573 static int
2574 qed_iwarp_ll2_start(struct qed_hwfn *p_hwfn,
2575 		    struct qed_rdma_start_in_params *params,
2576 		    struct qed_ptt *p_ptt)
2577 {
2578 	struct qed_iwarp_info *iwarp_info;
2579 	struct qed_ll2_acquire_data data;
2580 	struct qed_ll2_cbs cbs;
2581 	u32 mpa_buff_size;
2582 	u16 n_ooo_bufs;
2583 	int rc = 0;
2584 	int i;
2585 
2586 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2587 	iwarp_info->ll2_syn_handle = QED_IWARP_HANDLE_INVAL;
2588 	iwarp_info->ll2_ooo_handle = QED_IWARP_HANDLE_INVAL;
2589 	iwarp_info->ll2_mpa_handle = QED_IWARP_HANDLE_INVAL;
2590 
2591 	iwarp_info->max_mtu = params->max_mtu;
2592 
2593 	ether_addr_copy(p_hwfn->p_rdma_info->iwarp.mac_addr, params->mac_addr);
2594 
2595 	rc = qed_llh_add_mac_filter(p_hwfn, p_ptt, params->mac_addr);
2596 	if (rc)
2597 		return rc;
2598 
2599 	/* Start SYN connection */
2600 	cbs.rx_comp_cb = qed_iwarp_ll2_comp_syn_pkt;
2601 	cbs.rx_release_cb = qed_iwarp_ll2_rel_rx_pkt;
2602 	cbs.tx_comp_cb = qed_iwarp_ll2_comp_tx_pkt;
2603 	cbs.tx_release_cb = qed_iwarp_ll2_rel_tx_pkt;
2604 	cbs.cookie = p_hwfn;
2605 
2606 	memset(&data, 0, sizeof(data));
2607 	data.input.conn_type = QED_LL2_TYPE_IWARP;
2608 	data.input.mtu = QED_IWARP_MAX_SYN_PKT_SIZE;
2609 	data.input.rx_num_desc = QED_IWARP_LL2_SYN_RX_SIZE;
2610 	data.input.tx_num_desc = QED_IWARP_LL2_SYN_TX_SIZE;
2611 	data.input.tx_max_bds_per_packet = 1;	/* will never be fragmented */
2612 	data.input.tx_tc = PKT_LB_TC;
2613 	data.input.tx_dest = QED_LL2_TX_DEST_LB;
2614 	data.p_connection_handle = &iwarp_info->ll2_syn_handle;
2615 	data.cbs = &cbs;
2616 
2617 	rc = qed_ll2_acquire_connection(p_hwfn, &data);
2618 	if (rc) {
2619 		DP_NOTICE(p_hwfn, "Failed to acquire LL2 connection\n");
2620 		qed_llh_remove_mac_filter(p_hwfn, p_ptt, params->mac_addr);
2621 		return rc;
2622 	}
2623 
2624 	rc = qed_ll2_establish_connection(p_hwfn, iwarp_info->ll2_syn_handle);
2625 	if (rc) {
2626 		DP_NOTICE(p_hwfn, "Failed to establish LL2 connection\n");
2627 		goto err;
2628 	}
2629 
2630 	rc = qed_iwarp_ll2_alloc_buffers(p_hwfn,
2631 					 QED_IWARP_LL2_SYN_RX_SIZE,
2632 					 QED_IWARP_MAX_SYN_PKT_SIZE,
2633 					 iwarp_info->ll2_syn_handle);
2634 	if (rc)
2635 		goto err;
2636 
2637 	/* Start OOO connection */
2638 	data.input.conn_type = QED_LL2_TYPE_OOO;
2639 	data.input.mtu = params->max_mtu;
2640 
2641 	n_ooo_bufs = (QED_IWARP_MAX_OOO * QED_IWARP_RCV_WND_SIZE_DEF) /
2642 		     iwarp_info->max_mtu;
2643 	n_ooo_bufs = min_t(u32, n_ooo_bufs, QED_IWARP_LL2_OOO_MAX_RX_SIZE);
2644 
2645 	data.input.rx_num_desc = n_ooo_bufs;
2646 	data.input.rx_num_ooo_buffers = n_ooo_bufs;
2647 
2648 	data.input.tx_max_bds_per_packet = 1;	/* will never be fragmented */
2649 	data.input.tx_num_desc = QED_IWARP_LL2_OOO_DEF_TX_SIZE;
2650 	data.p_connection_handle = &iwarp_info->ll2_ooo_handle;
2651 
2652 	rc = qed_ll2_acquire_connection(p_hwfn, &data);
2653 	if (rc)
2654 		goto err;
2655 
2656 	rc = qed_ll2_establish_connection(p_hwfn, iwarp_info->ll2_ooo_handle);
2657 	if (rc)
2658 		goto err;
2659 
2660 	/* Start Unaligned MPA connection */
2661 	cbs.rx_comp_cb = qed_iwarp_ll2_comp_mpa_pkt;
2662 	cbs.slowpath_cb = qed_iwarp_ll2_slowpath;
2663 
2664 	memset(&data, 0, sizeof(data));
2665 	data.input.conn_type = QED_LL2_TYPE_IWARP;
2666 	data.input.mtu = params->max_mtu;
2667 	/* FW requires that once a packet arrives OOO, it must have at
2668 	 * least 2 rx buffers available on the unaligned connection
2669 	 * for handling the case that it is a partial fpdu.
2670 	 */
2671 	data.input.rx_num_desc = n_ooo_bufs * 2;
2672 	data.input.tx_num_desc = data.input.rx_num_desc;
2673 	data.input.tx_max_bds_per_packet = QED_IWARP_MAX_BDS_PER_FPDU;
2674 	data.p_connection_handle = &iwarp_info->ll2_mpa_handle;
2675 	data.input.secondary_queue = true;
2676 	data.cbs = &cbs;
2677 
2678 	rc = qed_ll2_acquire_connection(p_hwfn, &data);
2679 	if (rc)
2680 		goto err;
2681 
2682 	rc = qed_ll2_establish_connection(p_hwfn, iwarp_info->ll2_mpa_handle);
2683 	if (rc)
2684 		goto err;
2685 
2686 	mpa_buff_size = QED_IWARP_MAX_BUF_SIZE(params->max_mtu);
2687 	rc = qed_iwarp_ll2_alloc_buffers(p_hwfn,
2688 					 data.input.rx_num_desc,
2689 					 mpa_buff_size,
2690 					 iwarp_info->ll2_mpa_handle);
2691 	if (rc)
2692 		goto err;
2693 
2694 	iwarp_info->partial_fpdus = kcalloc((u16)p_hwfn->p_rdma_info->num_qps,
2695 					    sizeof(*iwarp_info->partial_fpdus),
2696 					    GFP_KERNEL);
2697 	if (!iwarp_info->partial_fpdus)
2698 		goto err;
2699 
2700 	iwarp_info->max_num_partial_fpdus = (u16)p_hwfn->p_rdma_info->num_qps;
2701 
2702 	iwarp_info->mpa_intermediate_buf = kzalloc(mpa_buff_size, GFP_KERNEL);
2703 	if (!iwarp_info->mpa_intermediate_buf)
2704 		goto err;
2705 
2706 	/* The mpa_bufs array serves for pending RX packets received on the
2707 	 * mpa ll2 that don't have place on the tx ring and require later
2708 	 * processing. We can't fail on allocation of such a struct therefore
2709 	 * we allocate enough to take care of all rx packets
2710 	 */
2711 	iwarp_info->mpa_bufs = kcalloc(data.input.rx_num_desc,
2712 				       sizeof(*iwarp_info->mpa_bufs),
2713 				       GFP_KERNEL);
2714 	if (!iwarp_info->mpa_bufs)
2715 		goto err;
2716 
2717 	INIT_LIST_HEAD(&iwarp_info->mpa_buf_pending_list);
2718 	INIT_LIST_HEAD(&iwarp_info->mpa_buf_list);
2719 	for (i = 0; i < data.input.rx_num_desc; i++)
2720 		list_add_tail(&iwarp_info->mpa_bufs[i].list_entry,
2721 			      &iwarp_info->mpa_buf_list);
2722 	return rc;
2723 err:
2724 	qed_iwarp_ll2_stop(p_hwfn, p_ptt);
2725 
2726 	return rc;
2727 }
2728 
2729 int qed_iwarp_setup(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
2730 		    struct qed_rdma_start_in_params *params)
2731 {
2732 	struct qed_iwarp_info *iwarp_info;
2733 	u32 rcv_wnd_size;
2734 
2735 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2736 
2737 	iwarp_info->tcp_flags = QED_IWARP_TS_EN;
2738 	rcv_wnd_size = QED_IWARP_RCV_WND_SIZE_DEF;
2739 
2740 	/* value 0 is used for ilog2(QED_IWARP_RCV_WND_SIZE_MIN) */
2741 	iwarp_info->rcv_wnd_scale = ilog2(rcv_wnd_size) -
2742 	    ilog2(QED_IWARP_RCV_WND_SIZE_MIN);
2743 	iwarp_info->crc_needed = QED_IWARP_PARAM_CRC_NEEDED;
2744 	iwarp_info->mpa_rev = MPA_NEGOTIATION_TYPE_ENHANCED;
2745 
2746 	iwarp_info->peer2peer = QED_IWARP_PARAM_P2P;
2747 
2748 	iwarp_info->rtr_type =  MPA_RTR_TYPE_ZERO_SEND |
2749 				MPA_RTR_TYPE_ZERO_WRITE |
2750 				MPA_RTR_TYPE_ZERO_READ;
2751 
2752 	spin_lock_init(&p_hwfn->p_rdma_info->iwarp.qp_lock);
2753 	INIT_LIST_HEAD(&p_hwfn->p_rdma_info->iwarp.ep_list);
2754 	INIT_LIST_HEAD(&p_hwfn->p_rdma_info->iwarp.listen_list);
2755 
2756 	qed_spq_register_async_cb(p_hwfn, PROTOCOLID_IWARP,
2757 				  qed_iwarp_async_event);
2758 	qed_ooo_setup(p_hwfn);
2759 
2760 	return qed_iwarp_ll2_start(p_hwfn, params, p_ptt);
2761 }
2762 
2763 int qed_iwarp_stop(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2764 {
2765 	int rc;
2766 
2767 	qed_iwarp_free_prealloc_ep(p_hwfn);
2768 	rc = qed_iwarp_wait_for_all_cids(p_hwfn);
2769 	if (rc)
2770 		return rc;
2771 
2772 	qed_spq_unregister_async_cb(p_hwfn, PROTOCOLID_IWARP);
2773 
2774 	return qed_iwarp_ll2_stop(p_hwfn, p_ptt);
2775 }
2776 
2777 void qed_iwarp_qp_in_error(struct qed_hwfn *p_hwfn,
2778 			   struct qed_iwarp_ep *ep, u8 fw_return_code)
2779 {
2780 	struct qed_iwarp_cm_event_params params;
2781 
2782 	qed_iwarp_modify_qp(p_hwfn, ep->qp, QED_IWARP_QP_STATE_ERROR, true);
2783 
2784 	params.event = QED_IWARP_EVENT_CLOSE;
2785 	params.ep_context = ep;
2786 	params.cm_info = &ep->cm_info;
2787 	params.status = (fw_return_code == IWARP_QP_IN_ERROR_GOOD_CLOSE) ?
2788 			 0 : -ECONNRESET;
2789 
2790 	ep->state = QED_IWARP_EP_CLOSED;
2791 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2792 	list_del(&ep->list_entry);
2793 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2794 
2795 	ep->event_cb(ep->cb_context, &params);
2796 }
2797 
2798 void qed_iwarp_exception_received(struct qed_hwfn *p_hwfn,
2799 				  struct qed_iwarp_ep *ep, int fw_ret_code)
2800 {
2801 	struct qed_iwarp_cm_event_params params;
2802 	bool event_cb = false;
2803 
2804 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "EP(0x%x) fw_ret_code=%d\n",
2805 		   ep->cid, fw_ret_code);
2806 
2807 	switch (fw_ret_code) {
2808 	case IWARP_EXCEPTION_DETECTED_LLP_CLOSED:
2809 		params.status = 0;
2810 		params.event = QED_IWARP_EVENT_DISCONNECT;
2811 		event_cb = true;
2812 		break;
2813 	case IWARP_EXCEPTION_DETECTED_LLP_RESET:
2814 		params.status = -ECONNRESET;
2815 		params.event = QED_IWARP_EVENT_DISCONNECT;
2816 		event_cb = true;
2817 		break;
2818 	case IWARP_EXCEPTION_DETECTED_RQ_EMPTY:
2819 		params.event = QED_IWARP_EVENT_RQ_EMPTY;
2820 		event_cb = true;
2821 		break;
2822 	case IWARP_EXCEPTION_DETECTED_IRQ_FULL:
2823 		params.event = QED_IWARP_EVENT_IRQ_FULL;
2824 		event_cb = true;
2825 		break;
2826 	case IWARP_EXCEPTION_DETECTED_LLP_TIMEOUT:
2827 		params.event = QED_IWARP_EVENT_LLP_TIMEOUT;
2828 		event_cb = true;
2829 		break;
2830 	case IWARP_EXCEPTION_DETECTED_REMOTE_PROTECTION_ERROR:
2831 		params.event = QED_IWARP_EVENT_REMOTE_PROTECTION_ERROR;
2832 		event_cb = true;
2833 		break;
2834 	case IWARP_EXCEPTION_DETECTED_CQ_OVERFLOW:
2835 		params.event = QED_IWARP_EVENT_CQ_OVERFLOW;
2836 		event_cb = true;
2837 		break;
2838 	case IWARP_EXCEPTION_DETECTED_LOCAL_CATASTROPHIC:
2839 		params.event = QED_IWARP_EVENT_QP_CATASTROPHIC;
2840 		event_cb = true;
2841 		break;
2842 	case IWARP_EXCEPTION_DETECTED_LOCAL_ACCESS_ERROR:
2843 		params.event = QED_IWARP_EVENT_LOCAL_ACCESS_ERROR;
2844 		event_cb = true;
2845 		break;
2846 	case IWARP_EXCEPTION_DETECTED_REMOTE_OPERATION_ERROR:
2847 		params.event = QED_IWARP_EVENT_REMOTE_OPERATION_ERROR;
2848 		event_cb = true;
2849 		break;
2850 	case IWARP_EXCEPTION_DETECTED_TERMINATE_RECEIVED:
2851 		params.event = QED_IWARP_EVENT_TERMINATE_RECEIVED;
2852 		event_cb = true;
2853 		break;
2854 	default:
2855 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2856 			   "Unhandled exception received...fw_ret_code=%d\n",
2857 			   fw_ret_code);
2858 		break;
2859 	}
2860 
2861 	if (event_cb) {
2862 		params.ep_context = ep;
2863 		params.cm_info = &ep->cm_info;
2864 		ep->event_cb(ep->cb_context, &params);
2865 	}
2866 }
2867 
2868 static void
2869 qed_iwarp_tcp_connect_unsuccessful(struct qed_hwfn *p_hwfn,
2870 				   struct qed_iwarp_ep *ep, u8 fw_return_code)
2871 {
2872 	struct qed_iwarp_cm_event_params params;
2873 
2874 	memset(&params, 0, sizeof(params));
2875 	params.event = QED_IWARP_EVENT_ACTIVE_COMPLETE;
2876 	params.ep_context = ep;
2877 	params.cm_info = &ep->cm_info;
2878 	ep->state = QED_IWARP_EP_CLOSED;
2879 
2880 	switch (fw_return_code) {
2881 	case IWARP_CONN_ERROR_TCP_CONNECT_INVALID_PACKET:
2882 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2883 			   "%s(0x%x) TCP connect got invalid packet\n",
2884 			   QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
2885 		params.status = -ECONNRESET;
2886 		break;
2887 	case IWARP_CONN_ERROR_TCP_CONNECTION_RST:
2888 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2889 			   "%s(0x%x) TCP Connection Reset\n",
2890 			   QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
2891 		params.status = -ECONNRESET;
2892 		break;
2893 	case IWARP_CONN_ERROR_TCP_CONNECT_TIMEOUT:
2894 		DP_NOTICE(p_hwfn, "%s(0x%x) TCP timeout\n",
2895 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
2896 		params.status = -EBUSY;
2897 		break;
2898 	case IWARP_CONN_ERROR_MPA_NOT_SUPPORTED_VER:
2899 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA not supported VER\n",
2900 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
2901 		params.status = -ECONNREFUSED;
2902 		break;
2903 	case IWARP_CONN_ERROR_MPA_INVALID_PACKET:
2904 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA Invalid Packet\n",
2905 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
2906 		params.status = -ECONNRESET;
2907 		break;
2908 	default:
2909 		DP_ERR(p_hwfn,
2910 		       "%s(0x%x) Unexpected return code tcp connect: %d\n",
2911 		       QED_IWARP_CONNECT_MODE_STRING(ep),
2912 		       ep->tcp_cid, fw_return_code);
2913 		params.status = -ECONNRESET;
2914 		break;
2915 	}
2916 
2917 	if (ep->connect_mode == TCP_CONNECT_PASSIVE) {
2918 		ep->tcp_cid = QED_IWARP_INVALID_TCP_CID;
2919 		qed_iwarp_return_ep(p_hwfn, ep);
2920 	} else {
2921 		ep->event_cb(ep->cb_context, &params);
2922 		spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2923 		list_del(&ep->list_entry);
2924 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2925 	}
2926 }
2927 
2928 void
2929 qed_iwarp_connect_complete(struct qed_hwfn *p_hwfn,
2930 			   struct qed_iwarp_ep *ep, u8 fw_return_code)
2931 {
2932 	u8 ll2_syn_handle = p_hwfn->p_rdma_info->iwarp.ll2_syn_handle;
2933 
2934 	if (ep->connect_mode == TCP_CONNECT_PASSIVE) {
2935 		/* Done with the SYN packet, post back to ll2 rx */
2936 		qed_iwarp_ll2_post_rx(p_hwfn, ep->syn, ll2_syn_handle);
2937 
2938 		ep->syn = NULL;
2939 
2940 		/* If connect failed - upper layer doesn't know about it */
2941 		if (fw_return_code == RDMA_RETURN_OK)
2942 			qed_iwarp_mpa_received(p_hwfn, ep);
2943 		else
2944 			qed_iwarp_tcp_connect_unsuccessful(p_hwfn, ep,
2945 							   fw_return_code);
2946 	} else {
2947 		if (fw_return_code == RDMA_RETURN_OK)
2948 			qed_iwarp_mpa_offload(p_hwfn, ep);
2949 		else
2950 			qed_iwarp_tcp_connect_unsuccessful(p_hwfn, ep,
2951 							   fw_return_code);
2952 	}
2953 }
2954 
2955 static inline bool
2956 qed_iwarp_check_ep_ok(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
2957 {
2958 	if (!ep || (ep->sig != QED_EP_SIG)) {
2959 		DP_ERR(p_hwfn, "ERROR ON ASYNC ep=%p\n", ep);
2960 		return false;
2961 	}
2962 
2963 	return true;
2964 }
2965 
2966 static int qed_iwarp_async_event(struct qed_hwfn *p_hwfn,
2967 				 u8 fw_event_code, u16 echo,
2968 				 union event_ring_data *data,
2969 				 u8 fw_return_code)
2970 {
2971 	struct regpair *fw_handle = &data->rdma_data.async_handle;
2972 	struct qed_iwarp_ep *ep = NULL;
2973 	u16 cid;
2974 
2975 	ep = (struct qed_iwarp_ep *)(uintptr_t)HILO_64(fw_handle->hi,
2976 						       fw_handle->lo);
2977 
2978 	switch (fw_event_code) {
2979 	case IWARP_EVENT_TYPE_ASYNC_CONNECT_COMPLETE:
2980 		/* Async completion after TCP 3-way handshake */
2981 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
2982 			return -EINVAL;
2983 		DP_VERBOSE(p_hwfn,
2984 			   QED_MSG_RDMA,
2985 			   "EP(0x%x) IWARP_EVENT_TYPE_ASYNC_CONNECT_COMPLETE fw_ret_code=%d\n",
2986 			   ep->tcp_cid, fw_return_code);
2987 		qed_iwarp_connect_complete(p_hwfn, ep, fw_return_code);
2988 		break;
2989 	case IWARP_EVENT_TYPE_ASYNC_EXCEPTION_DETECTED:
2990 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
2991 			return -EINVAL;
2992 		DP_VERBOSE(p_hwfn,
2993 			   QED_MSG_RDMA,
2994 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_EXCEPTION_DETECTED fw_ret_code=%d\n",
2995 			   ep->cid, fw_return_code);
2996 		qed_iwarp_exception_received(p_hwfn, ep, fw_return_code);
2997 		break;
2998 	case IWARP_EVENT_TYPE_ASYNC_QP_IN_ERROR_STATE:
2999 		/* Async completion for Close Connection ramrod */
3000 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
3001 			return -EINVAL;
3002 		DP_VERBOSE(p_hwfn,
3003 			   QED_MSG_RDMA,
3004 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_QP_IN_ERROR_STATE fw_ret_code=%d\n",
3005 			   ep->cid, fw_return_code);
3006 		qed_iwarp_qp_in_error(p_hwfn, ep, fw_return_code);
3007 		break;
3008 	case IWARP_EVENT_TYPE_ASYNC_ENHANCED_MPA_REPLY_ARRIVED:
3009 		/* Async event for active side only */
3010 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
3011 			return -EINVAL;
3012 		DP_VERBOSE(p_hwfn,
3013 			   QED_MSG_RDMA,
3014 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_MPA_REPLY_ARRIVED fw_ret_code=%d\n",
3015 			   ep->cid, fw_return_code);
3016 		qed_iwarp_mpa_reply_arrived(p_hwfn, ep);
3017 		break;
3018 	case IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_COMPLETE:
3019 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
3020 			return -EINVAL;
3021 		DP_VERBOSE(p_hwfn,
3022 			   QED_MSG_RDMA,
3023 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_COMPLETE fw_ret_code=%d\n",
3024 			   ep->cid, fw_return_code);
3025 		qed_iwarp_mpa_complete(p_hwfn, ep, fw_return_code);
3026 		break;
3027 	case IWARP_EVENT_TYPE_ASYNC_CID_CLEANED:
3028 		cid = (u16)le32_to_cpu(fw_handle->lo);
3029 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
3030 			   "(0x%x)IWARP_EVENT_TYPE_ASYNC_CID_CLEANED\n", cid);
3031 		qed_iwarp_cid_cleaned(p_hwfn, cid);
3032 
3033 		break;
3034 	case IWARP_EVENT_TYPE_ASYNC_CQ_OVERFLOW:
3035 		DP_NOTICE(p_hwfn, "IWARP_EVENT_TYPE_ASYNC_CQ_OVERFLOW\n");
3036 
3037 		p_hwfn->p_rdma_info->events.affiliated_event(
3038 			p_hwfn->p_rdma_info->events.context,
3039 			QED_IWARP_EVENT_CQ_OVERFLOW,
3040 			(void *)fw_handle);
3041 		break;
3042 	default:
3043 		DP_ERR(p_hwfn, "Received unexpected async iwarp event %d\n",
3044 		       fw_event_code);
3045 		return -EINVAL;
3046 	}
3047 	return 0;
3048 }
3049 
3050 int
3051 qed_iwarp_create_listen(void *rdma_cxt,
3052 			struct qed_iwarp_listen_in *iparams,
3053 			struct qed_iwarp_listen_out *oparams)
3054 {
3055 	struct qed_hwfn *p_hwfn = rdma_cxt;
3056 	struct qed_iwarp_listener *listener;
3057 
3058 	listener = kzalloc(sizeof(*listener), GFP_KERNEL);
3059 	if (!listener)
3060 		return -ENOMEM;
3061 
3062 	listener->ip_version = iparams->ip_version;
3063 	memcpy(listener->ip_addr, iparams->ip_addr, sizeof(listener->ip_addr));
3064 	listener->port = iparams->port;
3065 	listener->vlan = iparams->vlan;
3066 
3067 	listener->event_cb = iparams->event_cb;
3068 	listener->cb_context = iparams->cb_context;
3069 	listener->max_backlog = iparams->max_backlog;
3070 	oparams->handle = listener;
3071 
3072 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
3073 	list_add_tail(&listener->list_entry,
3074 		      &p_hwfn->p_rdma_info->iwarp.listen_list);
3075 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
3076 
3077 	DP_VERBOSE(p_hwfn,
3078 		   QED_MSG_RDMA,
3079 		   "callback=%p handle=%p ip=%x:%x:%x:%x port=0x%x vlan=0x%x\n",
3080 		   listener->event_cb,
3081 		   listener,
3082 		   listener->ip_addr[0],
3083 		   listener->ip_addr[1],
3084 		   listener->ip_addr[2],
3085 		   listener->ip_addr[3], listener->port, listener->vlan);
3086 
3087 	return 0;
3088 }
3089 
3090 int qed_iwarp_destroy_listen(void *rdma_cxt, void *handle)
3091 {
3092 	struct qed_iwarp_listener *listener = handle;
3093 	struct qed_hwfn *p_hwfn = rdma_cxt;
3094 
3095 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "handle=%p\n", handle);
3096 
3097 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
3098 	list_del(&listener->list_entry);
3099 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
3100 
3101 	kfree(listener);
3102 
3103 	return 0;
3104 }
3105 
3106 int qed_iwarp_send_rtr(void *rdma_cxt, struct qed_iwarp_send_rtr_in *iparams)
3107 {
3108 	struct qed_hwfn *p_hwfn = rdma_cxt;
3109 	struct qed_sp_init_data init_data;
3110 	struct qed_spq_entry *p_ent;
3111 	struct qed_iwarp_ep *ep;
3112 	struct qed_rdma_qp *qp;
3113 	int rc;
3114 
3115 	ep = iparams->ep_context;
3116 	if (!ep) {
3117 		DP_ERR(p_hwfn, "Ep Context receive in send_rtr is NULL\n");
3118 		return -EINVAL;
3119 	}
3120 
3121 	qp = ep->qp;
3122 
3123 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) EP(0x%x)\n",
3124 		   qp->icid, ep->tcp_cid);
3125 
3126 	memset(&init_data, 0, sizeof(init_data));
3127 	init_data.cid = qp->icid;
3128 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
3129 	init_data.comp_mode = QED_SPQ_MODE_CB;
3130 
3131 	rc = qed_sp_init_request(p_hwfn, &p_ent,
3132 				 IWARP_RAMROD_CMD_ID_MPA_OFFLOAD_SEND_RTR,
3133 				 PROTOCOLID_IWARP, &init_data);
3134 
3135 	if (rc)
3136 		return rc;
3137 
3138 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
3139 
3140 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "rc = 0x%x\n", rc);
3141 
3142 	return rc;
3143 }
3144 
3145 void
3146 qed_iwarp_query_qp(struct qed_rdma_qp *qp,
3147 		   struct qed_rdma_query_qp_out_params *out_params)
3148 {
3149 	out_params->state = qed_iwarp2roce_state(qp->iwarp_state);
3150 }
3151