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 	rc = qed_iwarp_prealloc_ep(p_hwfn, true);
1414 	if (rc)
1415 		return rc;
1416 
1417 	return qed_ooo_alloc(p_hwfn);
1418 }
1419 
1420 void qed_iwarp_resc_free(struct qed_hwfn *p_hwfn)
1421 {
1422 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
1423 
1424 	qed_ooo_free(p_hwfn);
1425 	qed_rdma_bmap_free(p_hwfn, &p_hwfn->p_rdma_info->tcp_cid_map, 1);
1426 	kfree(iwarp_info->mpa_bufs);
1427 	kfree(iwarp_info->partial_fpdus);
1428 	kfree(iwarp_info->mpa_intermediate_buf);
1429 }
1430 
1431 int qed_iwarp_accept(void *rdma_cxt, struct qed_iwarp_accept_in *iparams)
1432 {
1433 	struct qed_hwfn *p_hwfn = rdma_cxt;
1434 	struct qed_iwarp_ep *ep;
1435 	u8 mpa_data_size = 0;
1436 	int rc;
1437 
1438 	ep = iparams->ep_context;
1439 	if (!ep) {
1440 		DP_ERR(p_hwfn, "Ep Context receive in accept is NULL\n");
1441 		return -EINVAL;
1442 	}
1443 
1444 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) EP(0x%x)\n",
1445 		   iparams->qp->icid, ep->tcp_cid);
1446 
1447 	if ((iparams->ord > QED_IWARP_ORD_DEFAULT) ||
1448 	    (iparams->ird > QED_IWARP_IRD_DEFAULT)) {
1449 		DP_VERBOSE(p_hwfn,
1450 			   QED_MSG_RDMA,
1451 			   "QP(0x%x) EP(0x%x) ERROR: Invalid ord(0x%x)/ird(0x%x)\n",
1452 			   iparams->qp->icid,
1453 			   ep->tcp_cid, iparams->ord, iparams->ord);
1454 		return -EINVAL;
1455 	}
1456 
1457 	qed_iwarp_prealloc_ep(p_hwfn, false);
1458 
1459 	ep->cb_context = iparams->cb_context;
1460 	ep->qp = iparams->qp;
1461 	ep->qp->ep = ep;
1462 
1463 	if (ep->mpa_rev == MPA_NEGOTIATION_TYPE_ENHANCED) {
1464 		/* Negotiate ord/ird: if upperlayer requested ord larger than
1465 		 * ird advertised by remote, we need to decrease our ord
1466 		 */
1467 		if (iparams->ord > ep->cm_info.ird)
1468 			iparams->ord = ep->cm_info.ird;
1469 
1470 		if ((ep->rtr_type & MPA_RTR_TYPE_ZERO_READ) &&
1471 		    (iparams->ird == 0))
1472 			iparams->ird = 1;
1473 	}
1474 
1475 	/* Update cm_info ord/ird to be negotiated values */
1476 	ep->cm_info.ord = iparams->ord;
1477 	ep->cm_info.ird = iparams->ird;
1478 
1479 	qed_iwarp_mpa_v2_set_private(p_hwfn, ep, &mpa_data_size);
1480 
1481 	ep->cm_info.private_data = ep->ep_buffer_virt->out_pdata;
1482 	ep->cm_info.private_data_len = iparams->private_data_len +
1483 				       mpa_data_size;
1484 
1485 	memcpy((u8 *)ep->ep_buffer_virt->out_pdata + mpa_data_size,
1486 	       iparams->private_data, iparams->private_data_len);
1487 
1488 	rc = qed_iwarp_mpa_offload(p_hwfn, ep);
1489 	if (rc)
1490 		qed_iwarp_modify_qp(p_hwfn,
1491 				    iparams->qp, QED_IWARP_QP_STATE_ERROR, 1);
1492 
1493 	return rc;
1494 }
1495 
1496 int qed_iwarp_reject(void *rdma_cxt, struct qed_iwarp_reject_in *iparams)
1497 {
1498 	struct qed_hwfn *p_hwfn = rdma_cxt;
1499 	struct qed_iwarp_ep *ep;
1500 	u8 mpa_data_size = 0;
1501 
1502 	ep = iparams->ep_context;
1503 	if (!ep) {
1504 		DP_ERR(p_hwfn, "Ep Context receive in reject is NULL\n");
1505 		return -EINVAL;
1506 	}
1507 
1508 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "EP(0x%x)\n", ep->tcp_cid);
1509 
1510 	ep->cb_context = iparams->cb_context;
1511 	ep->qp = NULL;
1512 
1513 	qed_iwarp_mpa_v2_set_private(p_hwfn, ep, &mpa_data_size);
1514 
1515 	ep->cm_info.private_data = ep->ep_buffer_virt->out_pdata;
1516 	ep->cm_info.private_data_len = iparams->private_data_len +
1517 				       mpa_data_size;
1518 
1519 	memcpy((u8 *)ep->ep_buffer_virt->out_pdata + mpa_data_size,
1520 	       iparams->private_data, iparams->private_data_len);
1521 
1522 	return qed_iwarp_mpa_offload(p_hwfn, ep);
1523 }
1524 
1525 static void
1526 qed_iwarp_print_cm_info(struct qed_hwfn *p_hwfn,
1527 			struct qed_iwarp_cm_info *cm_info)
1528 {
1529 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "ip_version = %d\n",
1530 		   cm_info->ip_version);
1531 
1532 	if (cm_info->ip_version == QED_TCP_IPV4)
1533 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1534 			   "remote_ip %pI4h:%x, local_ip %pI4h:%x vlan=%x\n",
1535 			   cm_info->remote_ip, cm_info->remote_port,
1536 			   cm_info->local_ip, cm_info->local_port,
1537 			   cm_info->vlan);
1538 	else
1539 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1540 			   "remote_ip %pI6:%x, local_ip %pI6:%x vlan=%x\n",
1541 			   cm_info->remote_ip, cm_info->remote_port,
1542 			   cm_info->local_ip, cm_info->local_port,
1543 			   cm_info->vlan);
1544 
1545 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1546 		   "private_data_len = %x ord = %d, ird = %d\n",
1547 		   cm_info->private_data_len, cm_info->ord, cm_info->ird);
1548 }
1549 
1550 static int
1551 qed_iwarp_ll2_post_rx(struct qed_hwfn *p_hwfn,
1552 		      struct qed_iwarp_ll2_buff *buf, u8 handle)
1553 {
1554 	int rc;
1555 
1556 	rc = qed_ll2_post_rx_buffer(p_hwfn, handle, buf->data_phys_addr,
1557 				    (u16)buf->buff_size, buf, 1);
1558 	if (rc) {
1559 		DP_NOTICE(p_hwfn,
1560 			  "Failed to repost rx buffer to ll2 rc = %d, handle=%d\n",
1561 			  rc, handle);
1562 		dma_free_coherent(&p_hwfn->cdev->pdev->dev, buf->buff_size,
1563 				  buf->data, buf->data_phys_addr);
1564 		kfree(buf);
1565 	}
1566 
1567 	return rc;
1568 }
1569 
1570 static bool
1571 qed_iwarp_ep_exists(struct qed_hwfn *p_hwfn, struct qed_iwarp_cm_info *cm_info)
1572 {
1573 	struct qed_iwarp_ep *ep = NULL;
1574 	bool found = false;
1575 
1576 	list_for_each_entry(ep,
1577 			    &p_hwfn->p_rdma_info->iwarp.ep_list,
1578 			    list_entry) {
1579 		if ((ep->cm_info.local_port == cm_info->local_port) &&
1580 		    (ep->cm_info.remote_port == cm_info->remote_port) &&
1581 		    (ep->cm_info.vlan == cm_info->vlan) &&
1582 		    !memcmp(&ep->cm_info.local_ip, cm_info->local_ip,
1583 			    sizeof(cm_info->local_ip)) &&
1584 		    !memcmp(&ep->cm_info.remote_ip, cm_info->remote_ip,
1585 			    sizeof(cm_info->remote_ip))) {
1586 			found = true;
1587 			break;
1588 		}
1589 	}
1590 
1591 	if (found) {
1592 		DP_NOTICE(p_hwfn,
1593 			  "SYN received on active connection - dropping\n");
1594 		qed_iwarp_print_cm_info(p_hwfn, cm_info);
1595 
1596 		return true;
1597 	}
1598 
1599 	return false;
1600 }
1601 
1602 static struct qed_iwarp_listener *
1603 qed_iwarp_get_listener(struct qed_hwfn *p_hwfn,
1604 		       struct qed_iwarp_cm_info *cm_info)
1605 {
1606 	struct qed_iwarp_listener *listener = NULL;
1607 	static const u32 ip_zero[4] = { 0, 0, 0, 0 };
1608 	bool found = false;
1609 
1610 	qed_iwarp_print_cm_info(p_hwfn, cm_info);
1611 
1612 	list_for_each_entry(listener,
1613 			    &p_hwfn->p_rdma_info->iwarp.listen_list,
1614 			    list_entry) {
1615 		if (listener->port == cm_info->local_port) {
1616 			if (!memcmp(listener->ip_addr,
1617 				    ip_zero, sizeof(ip_zero))) {
1618 				found = true;
1619 				break;
1620 			}
1621 
1622 			if (!memcmp(listener->ip_addr,
1623 				    cm_info->local_ip,
1624 				    sizeof(cm_info->local_ip)) &&
1625 			    (listener->vlan == cm_info->vlan)) {
1626 				found = true;
1627 				break;
1628 			}
1629 		}
1630 	}
1631 
1632 	if (found) {
1633 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "listener found = %p\n",
1634 			   listener);
1635 		return listener;
1636 	}
1637 
1638 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "listener not found\n");
1639 	return NULL;
1640 }
1641 
1642 static int
1643 qed_iwarp_parse_rx_pkt(struct qed_hwfn *p_hwfn,
1644 		       struct qed_iwarp_cm_info *cm_info,
1645 		       void *buf,
1646 		       u8 *remote_mac_addr,
1647 		       u8 *local_mac_addr,
1648 		       int *payload_len, int *tcp_start_offset)
1649 {
1650 	struct vlan_ethhdr *vethh;
1651 	bool vlan_valid = false;
1652 	struct ipv6hdr *ip6h;
1653 	struct ethhdr *ethh;
1654 	struct tcphdr *tcph;
1655 	struct iphdr *iph;
1656 	int eth_hlen;
1657 	int ip_hlen;
1658 	int eth_type;
1659 	int i;
1660 
1661 	ethh = buf;
1662 	eth_type = ntohs(ethh->h_proto);
1663 	if (eth_type == ETH_P_8021Q) {
1664 		vlan_valid = true;
1665 		vethh = (struct vlan_ethhdr *)ethh;
1666 		cm_info->vlan = ntohs(vethh->h_vlan_TCI) & VLAN_VID_MASK;
1667 		eth_type = ntohs(vethh->h_vlan_encapsulated_proto);
1668 	}
1669 
1670 	eth_hlen = ETH_HLEN + (vlan_valid ? sizeof(u32) : 0);
1671 
1672 	ether_addr_copy(remote_mac_addr, ethh->h_source);
1673 	ether_addr_copy(local_mac_addr, ethh->h_dest);
1674 
1675 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "eth_type =%d source mac: %pM\n",
1676 		   eth_type, ethh->h_source);
1677 
1678 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "eth_hlen=%d destination mac: %pM\n",
1679 		   eth_hlen, ethh->h_dest);
1680 
1681 	iph = (struct iphdr *)((u8 *)(ethh) + eth_hlen);
1682 
1683 	if (eth_type == ETH_P_IP) {
1684 		cm_info->local_ip[0] = ntohl(iph->daddr);
1685 		cm_info->remote_ip[0] = ntohl(iph->saddr);
1686 		cm_info->ip_version = TCP_IPV4;
1687 
1688 		ip_hlen = (iph->ihl) * sizeof(u32);
1689 		*payload_len = ntohs(iph->tot_len) - ip_hlen;
1690 	} else if (eth_type == ETH_P_IPV6) {
1691 		ip6h = (struct ipv6hdr *)iph;
1692 		for (i = 0; i < 4; i++) {
1693 			cm_info->local_ip[i] =
1694 			    ntohl(ip6h->daddr.in6_u.u6_addr32[i]);
1695 			cm_info->remote_ip[i] =
1696 			    ntohl(ip6h->saddr.in6_u.u6_addr32[i]);
1697 		}
1698 		cm_info->ip_version = TCP_IPV6;
1699 
1700 		ip_hlen = sizeof(*ip6h);
1701 		*payload_len = ntohs(ip6h->payload_len);
1702 	} else {
1703 		DP_NOTICE(p_hwfn, "Unexpected ethertype on ll2 %x\n", eth_type);
1704 		return -EINVAL;
1705 	}
1706 
1707 	tcph = (struct tcphdr *)((u8 *)iph + ip_hlen);
1708 
1709 	if (!tcph->syn) {
1710 		DP_NOTICE(p_hwfn,
1711 			  "Only SYN type packet expected on this ll2 conn, iph->ihl=%d source=%d dest=%d\n",
1712 			  iph->ihl, tcph->source, tcph->dest);
1713 		return -EINVAL;
1714 	}
1715 
1716 	cm_info->local_port = ntohs(tcph->dest);
1717 	cm_info->remote_port = ntohs(tcph->source);
1718 
1719 	qed_iwarp_print_cm_info(p_hwfn, cm_info);
1720 
1721 	*tcp_start_offset = eth_hlen + ip_hlen;
1722 
1723 	return 0;
1724 }
1725 
1726 static struct qed_iwarp_fpdu *qed_iwarp_get_curr_fpdu(struct qed_hwfn *p_hwfn,
1727 						      u16 cid)
1728 {
1729 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
1730 	struct qed_iwarp_fpdu *partial_fpdu;
1731 	u32 idx;
1732 
1733 	idx = cid - qed_cxt_get_proto_cid_start(p_hwfn, PROTOCOLID_IWARP);
1734 	if (idx >= iwarp_info->max_num_partial_fpdus) {
1735 		DP_ERR(p_hwfn, "Invalid cid %x max_num_partial_fpdus=%x\n", cid,
1736 		       iwarp_info->max_num_partial_fpdus);
1737 		return NULL;
1738 	}
1739 
1740 	partial_fpdu = &iwarp_info->partial_fpdus[idx];
1741 
1742 	return partial_fpdu;
1743 }
1744 
1745 enum qed_iwarp_mpa_pkt_type {
1746 	QED_IWARP_MPA_PKT_PACKED,
1747 	QED_IWARP_MPA_PKT_PARTIAL,
1748 	QED_IWARP_MPA_PKT_UNALIGNED
1749 };
1750 
1751 #define QED_IWARP_INVALID_FPDU_LENGTH 0xffff
1752 #define QED_IWARP_MPA_FPDU_LENGTH_SIZE (2)
1753 #define QED_IWARP_MPA_CRC32_DIGEST_SIZE (4)
1754 
1755 /* Pad to multiple of 4 */
1756 #define QED_IWARP_PDU_DATA_LEN_WITH_PAD(data_len) ALIGN(data_len, 4)
1757 #define QED_IWARP_FPDU_LEN_WITH_PAD(_mpa_len)				   \
1758 	(QED_IWARP_PDU_DATA_LEN_WITH_PAD((_mpa_len) +			   \
1759 					 QED_IWARP_MPA_FPDU_LENGTH_SIZE) + \
1760 					 QED_IWARP_MPA_CRC32_DIGEST_SIZE)
1761 
1762 /* fpdu can be fragmented over maximum 3 bds: header, partial mpa, unaligned */
1763 #define QED_IWARP_MAX_BDS_PER_FPDU 3
1764 
1765 char *pkt_type_str[] = {
1766 	"QED_IWARP_MPA_PKT_PACKED",
1767 	"QED_IWARP_MPA_PKT_PARTIAL",
1768 	"QED_IWARP_MPA_PKT_UNALIGNED"
1769 };
1770 
1771 static int
1772 qed_iwarp_recycle_pkt(struct qed_hwfn *p_hwfn,
1773 		      struct qed_iwarp_fpdu *fpdu,
1774 		      struct qed_iwarp_ll2_buff *buf);
1775 
1776 static enum qed_iwarp_mpa_pkt_type
1777 qed_iwarp_mpa_classify(struct qed_hwfn *p_hwfn,
1778 		       struct qed_iwarp_fpdu *fpdu,
1779 		       u16 tcp_payload_len, u8 *mpa_data)
1780 {
1781 	enum qed_iwarp_mpa_pkt_type pkt_type;
1782 	u16 mpa_len;
1783 
1784 	if (fpdu->incomplete_bytes) {
1785 		pkt_type = QED_IWARP_MPA_PKT_UNALIGNED;
1786 		goto out;
1787 	}
1788 
1789 	/* special case of one byte remaining...
1790 	 * lower byte will be read next packet
1791 	 */
1792 	if (tcp_payload_len == 1) {
1793 		fpdu->fpdu_length = *mpa_data << BITS_PER_BYTE;
1794 		pkt_type = QED_IWARP_MPA_PKT_PARTIAL;
1795 		goto out;
1796 	}
1797 
1798 	mpa_len = ntohs(*((u16 *)(mpa_data)));
1799 	fpdu->fpdu_length = QED_IWARP_FPDU_LEN_WITH_PAD(mpa_len);
1800 
1801 	if (fpdu->fpdu_length <= tcp_payload_len)
1802 		pkt_type = QED_IWARP_MPA_PKT_PACKED;
1803 	else
1804 		pkt_type = QED_IWARP_MPA_PKT_PARTIAL;
1805 
1806 out:
1807 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1808 		   "MPA_ALIGN: %s: fpdu_length=0x%x tcp_payload_len:0x%x\n",
1809 		   pkt_type_str[pkt_type], fpdu->fpdu_length, tcp_payload_len);
1810 
1811 	return pkt_type;
1812 }
1813 
1814 static void
1815 qed_iwarp_init_fpdu(struct qed_iwarp_ll2_buff *buf,
1816 		    struct qed_iwarp_fpdu *fpdu,
1817 		    struct unaligned_opaque_data *pkt_data,
1818 		    u16 tcp_payload_size, u8 placement_offset)
1819 {
1820 	fpdu->mpa_buf = buf;
1821 	fpdu->pkt_hdr = buf->data_phys_addr + placement_offset;
1822 	fpdu->pkt_hdr_size = pkt_data->tcp_payload_offset;
1823 	fpdu->mpa_frag = buf->data_phys_addr + pkt_data->first_mpa_offset;
1824 	fpdu->mpa_frag_virt = (u8 *)(buf->data) + pkt_data->first_mpa_offset;
1825 
1826 	if (tcp_payload_size == 1)
1827 		fpdu->incomplete_bytes = QED_IWARP_INVALID_FPDU_LENGTH;
1828 	else if (tcp_payload_size < fpdu->fpdu_length)
1829 		fpdu->incomplete_bytes = fpdu->fpdu_length - tcp_payload_size;
1830 	else
1831 		fpdu->incomplete_bytes = 0;	/* complete fpdu */
1832 
1833 	fpdu->mpa_frag_len = fpdu->fpdu_length - fpdu->incomplete_bytes;
1834 }
1835 
1836 static int
1837 qed_iwarp_cp_pkt(struct qed_hwfn *p_hwfn,
1838 		 struct qed_iwarp_fpdu *fpdu,
1839 		 struct unaligned_opaque_data *pkt_data,
1840 		 struct qed_iwarp_ll2_buff *buf, u16 tcp_payload_size)
1841 {
1842 	u8 *tmp_buf = p_hwfn->p_rdma_info->iwarp.mpa_intermediate_buf;
1843 	int rc;
1844 
1845 	/* need to copy the data from the partial packet stored in fpdu
1846 	 * to the new buf, for this we also need to move the data currently
1847 	 * placed on the buf. The assumption is that the buffer is big enough
1848 	 * since fpdu_length <= mss, we use an intermediate buffer since
1849 	 * we may need to copy the new data to an overlapping location
1850 	 */
1851 	if ((fpdu->mpa_frag_len + tcp_payload_size) > (u16)buf->buff_size) {
1852 		DP_ERR(p_hwfn,
1853 		       "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",
1854 		       buf->buff_size, fpdu->mpa_frag_len,
1855 		       tcp_payload_size, fpdu->incomplete_bytes);
1856 		return -EINVAL;
1857 	}
1858 
1859 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1860 		   "MPA ALIGN Copying fpdu: [%p, %d] [%p, %d]\n",
1861 		   fpdu->mpa_frag_virt, fpdu->mpa_frag_len,
1862 		   (u8 *)(buf->data) + pkt_data->first_mpa_offset,
1863 		   tcp_payload_size);
1864 
1865 	memcpy(tmp_buf, fpdu->mpa_frag_virt, fpdu->mpa_frag_len);
1866 	memcpy(tmp_buf + fpdu->mpa_frag_len,
1867 	       (u8 *)(buf->data) + pkt_data->first_mpa_offset,
1868 	       tcp_payload_size);
1869 
1870 	rc = qed_iwarp_recycle_pkt(p_hwfn, fpdu, fpdu->mpa_buf);
1871 	if (rc)
1872 		return rc;
1873 
1874 	/* If we managed to post the buffer copy the data to the new buffer
1875 	 * o/w this will occur in the next round...
1876 	 */
1877 	memcpy((u8 *)(buf->data), tmp_buf,
1878 	       fpdu->mpa_frag_len + tcp_payload_size);
1879 
1880 	fpdu->mpa_buf = buf;
1881 	/* fpdu->pkt_hdr remains as is */
1882 	/* fpdu->mpa_frag is overridden with new buf */
1883 	fpdu->mpa_frag = buf->data_phys_addr;
1884 	fpdu->mpa_frag_virt = buf->data;
1885 	fpdu->mpa_frag_len += tcp_payload_size;
1886 
1887 	fpdu->incomplete_bytes -= tcp_payload_size;
1888 
1889 	DP_VERBOSE(p_hwfn,
1890 		   QED_MSG_RDMA,
1891 		   "MPA ALIGN: split fpdu buff_size = %d mpa_frag_len = %d, tcp_payload_size = %d, incomplete_bytes = %d\n",
1892 		   buf->buff_size, fpdu->mpa_frag_len, tcp_payload_size,
1893 		   fpdu->incomplete_bytes);
1894 
1895 	return 0;
1896 }
1897 
1898 static void
1899 qed_iwarp_update_fpdu_length(struct qed_hwfn *p_hwfn,
1900 			     struct qed_iwarp_fpdu *fpdu, u8 *mpa_data)
1901 {
1902 	u16 mpa_len;
1903 
1904 	/* Update incomplete packets if needed */
1905 	if (fpdu->incomplete_bytes == QED_IWARP_INVALID_FPDU_LENGTH) {
1906 		/* Missing lower byte is now available */
1907 		mpa_len = fpdu->fpdu_length | *mpa_data;
1908 		fpdu->fpdu_length = QED_IWARP_FPDU_LEN_WITH_PAD(mpa_len);
1909 		fpdu->mpa_frag_len = fpdu->fpdu_length;
1910 		/* one byte of hdr */
1911 		fpdu->incomplete_bytes = fpdu->fpdu_length - 1;
1912 		DP_VERBOSE(p_hwfn,
1913 			   QED_MSG_RDMA,
1914 			   "MPA_ALIGN: Partial header mpa_len=%x fpdu_length=%x incomplete_bytes=%x\n",
1915 			   mpa_len, fpdu->fpdu_length, fpdu->incomplete_bytes);
1916 	}
1917 }
1918 
1919 #define QED_IWARP_IS_RIGHT_EDGE(_curr_pkt) \
1920 	(GET_FIELD((_curr_pkt)->flags,	   \
1921 		   UNALIGNED_OPAQUE_DATA_PKT_REACHED_WIN_RIGHT_EDGE))
1922 
1923 /* This function is used to recycle a buffer using the ll2 drop option. It
1924  * uses the mechanism to ensure that all buffers posted to tx before this one
1925  * were completed. The buffer sent here will be sent as a cookie in the tx
1926  * completion function and can then be reposted to rx chain when done. The flow
1927  * that requires this is the flow where a FPDU splits over more than 3 tcp
1928  * segments. In this case the driver needs to re-post a rx buffer instead of
1929  * the one received, but driver can't simply repost a buffer it copied from
1930  * as there is a case where the buffer was originally a packed FPDU, and is
1931  * partially posted to FW. Driver needs to ensure FW is done with it.
1932  */
1933 static int
1934 qed_iwarp_recycle_pkt(struct qed_hwfn *p_hwfn,
1935 		      struct qed_iwarp_fpdu *fpdu,
1936 		      struct qed_iwarp_ll2_buff *buf)
1937 {
1938 	struct qed_ll2_tx_pkt_info tx_pkt;
1939 	u8 ll2_handle;
1940 	int rc;
1941 
1942 	memset(&tx_pkt, 0, sizeof(tx_pkt));
1943 	tx_pkt.num_of_bds = 1;
1944 	tx_pkt.tx_dest = QED_LL2_TX_DEST_DROP;
1945 	tx_pkt.l4_hdr_offset_w = fpdu->pkt_hdr_size >> 2;
1946 	tx_pkt.first_frag = fpdu->pkt_hdr;
1947 	tx_pkt.first_frag_len = fpdu->pkt_hdr_size;
1948 	buf->piggy_buf = NULL;
1949 	tx_pkt.cookie = buf;
1950 
1951 	ll2_handle = p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle;
1952 
1953 	rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_handle, &tx_pkt, true);
1954 	if (rc)
1955 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1956 			   "Can't drop packet rc=%d\n", rc);
1957 
1958 	DP_VERBOSE(p_hwfn,
1959 		   QED_MSG_RDMA,
1960 		   "MPA_ALIGN: send drop tx packet [%lx, 0x%x], buf=%p, rc=%d\n",
1961 		   (unsigned long int)tx_pkt.first_frag,
1962 		   tx_pkt.first_frag_len, buf, rc);
1963 
1964 	return rc;
1965 }
1966 
1967 static int
1968 qed_iwarp_win_right_edge(struct qed_hwfn *p_hwfn, struct qed_iwarp_fpdu *fpdu)
1969 {
1970 	struct qed_ll2_tx_pkt_info tx_pkt;
1971 	u8 ll2_handle;
1972 	int rc;
1973 
1974 	memset(&tx_pkt, 0, sizeof(tx_pkt));
1975 	tx_pkt.num_of_bds = 1;
1976 	tx_pkt.tx_dest = QED_LL2_TX_DEST_LB;
1977 	tx_pkt.l4_hdr_offset_w = fpdu->pkt_hdr_size >> 2;
1978 
1979 	tx_pkt.first_frag = fpdu->pkt_hdr;
1980 	tx_pkt.first_frag_len = fpdu->pkt_hdr_size;
1981 	tx_pkt.enable_ip_cksum = true;
1982 	tx_pkt.enable_l4_cksum = true;
1983 	tx_pkt.calc_ip_len = true;
1984 	/* vlan overload with enum iwarp_ll2_tx_queues */
1985 	tx_pkt.vlan = IWARP_LL2_ALIGNED_RIGHT_TRIMMED_TX_QUEUE;
1986 
1987 	ll2_handle = p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle;
1988 
1989 	rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_handle, &tx_pkt, true);
1990 	if (rc)
1991 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1992 			   "Can't send right edge rc=%d\n", rc);
1993 	DP_VERBOSE(p_hwfn,
1994 		   QED_MSG_RDMA,
1995 		   "MPA_ALIGN: Sent right edge FPDU num_bds=%d [%lx, 0x%x], rc=%d\n",
1996 		   tx_pkt.num_of_bds,
1997 		   (unsigned long int)tx_pkt.first_frag,
1998 		   tx_pkt.first_frag_len, rc);
1999 
2000 	return rc;
2001 }
2002 
2003 static int
2004 qed_iwarp_send_fpdu(struct qed_hwfn *p_hwfn,
2005 		    struct qed_iwarp_fpdu *fpdu,
2006 		    struct unaligned_opaque_data *curr_pkt,
2007 		    struct qed_iwarp_ll2_buff *buf,
2008 		    u16 tcp_payload_size, enum qed_iwarp_mpa_pkt_type pkt_type)
2009 {
2010 	struct qed_ll2_tx_pkt_info tx_pkt;
2011 	u8 ll2_handle;
2012 	int rc;
2013 
2014 	memset(&tx_pkt, 0, sizeof(tx_pkt));
2015 
2016 	/* An unaligned packet means it's split over two tcp segments. So the
2017 	 * complete packet requires 3 bds, one for the header, one for the
2018 	 * part of the fpdu of the first tcp segment, and the last fragment
2019 	 * will point to the remainder of the fpdu. A packed pdu, requires only
2020 	 * two bds, one for the header and one for the data.
2021 	 */
2022 	tx_pkt.num_of_bds = (pkt_type == QED_IWARP_MPA_PKT_UNALIGNED) ? 3 : 2;
2023 	tx_pkt.tx_dest = QED_LL2_TX_DEST_LB;
2024 	tx_pkt.l4_hdr_offset_w = fpdu->pkt_hdr_size >> 2; /* offset in words */
2025 
2026 	/* Send the mpa_buf only with the last fpdu (in case of packed) */
2027 	if (pkt_type == QED_IWARP_MPA_PKT_UNALIGNED ||
2028 	    tcp_payload_size <= fpdu->fpdu_length)
2029 		tx_pkt.cookie = fpdu->mpa_buf;
2030 
2031 	tx_pkt.first_frag = fpdu->pkt_hdr;
2032 	tx_pkt.first_frag_len = fpdu->pkt_hdr_size;
2033 	tx_pkt.enable_ip_cksum = true;
2034 	tx_pkt.enable_l4_cksum = true;
2035 	tx_pkt.calc_ip_len = true;
2036 	/* vlan overload with enum iwarp_ll2_tx_queues */
2037 	tx_pkt.vlan = IWARP_LL2_ALIGNED_TX_QUEUE;
2038 
2039 	/* special case of unaligned packet and not packed, need to send
2040 	 * both buffers as cookie to release.
2041 	 */
2042 	if (tcp_payload_size == fpdu->incomplete_bytes)
2043 		fpdu->mpa_buf->piggy_buf = buf;
2044 
2045 	ll2_handle = p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle;
2046 
2047 	/* Set first fragment to header */
2048 	rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_handle, &tx_pkt, true);
2049 	if (rc)
2050 		goto out;
2051 
2052 	/* Set second fragment to first part of packet */
2053 	rc = qed_ll2_set_fragment_of_tx_packet(p_hwfn, ll2_handle,
2054 					       fpdu->mpa_frag,
2055 					       fpdu->mpa_frag_len);
2056 	if (rc)
2057 		goto out;
2058 
2059 	if (!fpdu->incomplete_bytes)
2060 		goto out;
2061 
2062 	/* Set third fragment to second part of the packet */
2063 	rc = qed_ll2_set_fragment_of_tx_packet(p_hwfn,
2064 					       ll2_handle,
2065 					       buf->data_phys_addr +
2066 					       curr_pkt->first_mpa_offset,
2067 					       fpdu->incomplete_bytes);
2068 out:
2069 	DP_VERBOSE(p_hwfn,
2070 		   QED_MSG_RDMA,
2071 		   "MPA_ALIGN: Sent FPDU num_bds=%d first_frag_len=%x, mpa_frag_len=0x%x, incomplete_bytes:0x%x rc=%d\n",
2072 		   tx_pkt.num_of_bds,
2073 		   tx_pkt.first_frag_len,
2074 		   fpdu->mpa_frag_len,
2075 		   fpdu->incomplete_bytes, rc);
2076 
2077 	return rc;
2078 }
2079 
2080 static void
2081 qed_iwarp_mpa_get_data(struct qed_hwfn *p_hwfn,
2082 		       struct unaligned_opaque_data *curr_pkt,
2083 		       u32 opaque_data0, u32 opaque_data1)
2084 {
2085 	u64 opaque_data;
2086 
2087 	opaque_data = HILO_64(opaque_data1, opaque_data0);
2088 	*curr_pkt = *((struct unaligned_opaque_data *)&opaque_data);
2089 
2090 	curr_pkt->first_mpa_offset = curr_pkt->tcp_payload_offset +
2091 				     le16_to_cpu(curr_pkt->first_mpa_offset);
2092 	curr_pkt->cid = le32_to_cpu(curr_pkt->cid);
2093 }
2094 
2095 /* This function is called when an unaligned or incomplete MPA packet arrives
2096  * driver needs to align the packet, perhaps using previous data and send
2097  * it down to FW once it is aligned.
2098  */
2099 static int
2100 qed_iwarp_process_mpa_pkt(struct qed_hwfn *p_hwfn,
2101 			  struct qed_iwarp_ll2_mpa_buf *mpa_buf)
2102 {
2103 	struct unaligned_opaque_data *curr_pkt = &mpa_buf->data;
2104 	struct qed_iwarp_ll2_buff *buf = mpa_buf->ll2_buf;
2105 	enum qed_iwarp_mpa_pkt_type pkt_type;
2106 	struct qed_iwarp_fpdu *fpdu;
2107 	int rc = -EINVAL;
2108 	u8 *mpa_data;
2109 
2110 	fpdu = qed_iwarp_get_curr_fpdu(p_hwfn, curr_pkt->cid & 0xffff);
2111 	if (!fpdu) { /* something corrupt with cid, post rx back */
2112 		DP_ERR(p_hwfn, "Invalid cid, drop and post back to rx cid=%x\n",
2113 		       curr_pkt->cid);
2114 		goto err;
2115 	}
2116 
2117 	do {
2118 		mpa_data = ((u8 *)(buf->data) + curr_pkt->first_mpa_offset);
2119 
2120 		pkt_type = qed_iwarp_mpa_classify(p_hwfn, fpdu,
2121 						  mpa_buf->tcp_payload_len,
2122 						  mpa_data);
2123 
2124 		switch (pkt_type) {
2125 		case QED_IWARP_MPA_PKT_PARTIAL:
2126 			qed_iwarp_init_fpdu(buf, fpdu,
2127 					    curr_pkt,
2128 					    mpa_buf->tcp_payload_len,
2129 					    mpa_buf->placement_offset);
2130 
2131 			if (!QED_IWARP_IS_RIGHT_EDGE(curr_pkt)) {
2132 				mpa_buf->tcp_payload_len = 0;
2133 				break;
2134 			}
2135 
2136 			rc = qed_iwarp_win_right_edge(p_hwfn, fpdu);
2137 
2138 			if (rc) {
2139 				DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2140 					   "Can't send FPDU:reset rc=%d\n", rc);
2141 				memset(fpdu, 0, sizeof(*fpdu));
2142 				break;
2143 			}
2144 
2145 			mpa_buf->tcp_payload_len = 0;
2146 			break;
2147 		case QED_IWARP_MPA_PKT_PACKED:
2148 			qed_iwarp_init_fpdu(buf, fpdu,
2149 					    curr_pkt,
2150 					    mpa_buf->tcp_payload_len,
2151 					    mpa_buf->placement_offset);
2152 
2153 			rc = qed_iwarp_send_fpdu(p_hwfn, fpdu, curr_pkt, buf,
2154 						 mpa_buf->tcp_payload_len,
2155 						 pkt_type);
2156 			if (rc) {
2157 				DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2158 					   "Can't send FPDU:reset rc=%d\n", rc);
2159 				memset(fpdu, 0, sizeof(*fpdu));
2160 				break;
2161 			}
2162 
2163 			mpa_buf->tcp_payload_len -= fpdu->fpdu_length;
2164 			curr_pkt->first_mpa_offset += fpdu->fpdu_length;
2165 			break;
2166 		case QED_IWARP_MPA_PKT_UNALIGNED:
2167 			qed_iwarp_update_fpdu_length(p_hwfn, fpdu, mpa_data);
2168 			if (mpa_buf->tcp_payload_len < fpdu->incomplete_bytes) {
2169 				/* special handling of fpdu split over more
2170 				 * than 2 segments
2171 				 */
2172 				if (QED_IWARP_IS_RIGHT_EDGE(curr_pkt)) {
2173 					rc = qed_iwarp_win_right_edge(p_hwfn,
2174 								      fpdu);
2175 					/* packet will be re-processed later */
2176 					if (rc)
2177 						return rc;
2178 				}
2179 
2180 				rc = qed_iwarp_cp_pkt(p_hwfn, fpdu, curr_pkt,
2181 						      buf,
2182 						      mpa_buf->tcp_payload_len);
2183 				if (rc) /* packet will be re-processed later */
2184 					return rc;
2185 
2186 				mpa_buf->tcp_payload_len = 0;
2187 				break;
2188 			}
2189 
2190 			rc = qed_iwarp_send_fpdu(p_hwfn, fpdu, curr_pkt, buf,
2191 						 mpa_buf->tcp_payload_len,
2192 						 pkt_type);
2193 			if (rc) {
2194 				DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2195 					   "Can't send FPDU:delay rc=%d\n", rc);
2196 				/* don't reset fpdu -> we need it for next
2197 				 * classify
2198 				 */
2199 				break;
2200 			}
2201 
2202 			mpa_buf->tcp_payload_len -= fpdu->incomplete_bytes;
2203 			curr_pkt->first_mpa_offset += fpdu->incomplete_bytes;
2204 			/* The framed PDU was sent - no more incomplete bytes */
2205 			fpdu->incomplete_bytes = 0;
2206 			break;
2207 		}
2208 	} while (mpa_buf->tcp_payload_len && !rc);
2209 
2210 	return rc;
2211 
2212 err:
2213 	qed_iwarp_ll2_post_rx(p_hwfn,
2214 			      buf,
2215 			      p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle);
2216 	return rc;
2217 }
2218 
2219 static void qed_iwarp_process_pending_pkts(struct qed_hwfn *p_hwfn)
2220 {
2221 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2222 	struct qed_iwarp_ll2_mpa_buf *mpa_buf = NULL;
2223 	int rc;
2224 
2225 	while (!list_empty(&iwarp_info->mpa_buf_pending_list)) {
2226 		mpa_buf = list_first_entry(&iwarp_info->mpa_buf_pending_list,
2227 					   struct qed_iwarp_ll2_mpa_buf,
2228 					   list_entry);
2229 
2230 		rc = qed_iwarp_process_mpa_pkt(p_hwfn, mpa_buf);
2231 
2232 		/* busy means break and continue processing later, don't
2233 		 * remove the buf from the pending list.
2234 		 */
2235 		if (rc == -EBUSY)
2236 			break;
2237 
2238 		list_del(&mpa_buf->list_entry);
2239 		list_add_tail(&mpa_buf->list_entry, &iwarp_info->mpa_buf_list);
2240 
2241 		if (rc) {	/* different error, don't continue */
2242 			DP_NOTICE(p_hwfn, "process pkts failed rc=%d\n", rc);
2243 			break;
2244 		}
2245 	}
2246 }
2247 
2248 static void
2249 qed_iwarp_ll2_comp_mpa_pkt(void *cxt, struct qed_ll2_comp_rx_data *data)
2250 {
2251 	struct qed_iwarp_ll2_mpa_buf *mpa_buf;
2252 	struct qed_iwarp_info *iwarp_info;
2253 	struct qed_hwfn *p_hwfn = cxt;
2254 
2255 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2256 	mpa_buf = list_first_entry(&iwarp_info->mpa_buf_list,
2257 				   struct qed_iwarp_ll2_mpa_buf, list_entry);
2258 	if (!mpa_buf) {
2259 		DP_ERR(p_hwfn, "No free mpa buf\n");
2260 		goto err;
2261 	}
2262 
2263 	list_del(&mpa_buf->list_entry);
2264 	qed_iwarp_mpa_get_data(p_hwfn, &mpa_buf->data,
2265 			       data->opaque_data_0, data->opaque_data_1);
2266 
2267 	DP_VERBOSE(p_hwfn,
2268 		   QED_MSG_RDMA,
2269 		   "LL2 MPA CompRx payload_len:0x%x\tfirst_mpa_offset:0x%x\ttcp_payload_offset:0x%x\tflags:0x%x\tcid:0x%x\n",
2270 		   data->length.packet_length, mpa_buf->data.first_mpa_offset,
2271 		   mpa_buf->data.tcp_payload_offset, mpa_buf->data.flags,
2272 		   mpa_buf->data.cid);
2273 
2274 	mpa_buf->ll2_buf = data->cookie;
2275 	mpa_buf->tcp_payload_len = data->length.packet_length -
2276 				   mpa_buf->data.first_mpa_offset;
2277 	mpa_buf->data.first_mpa_offset += data->u.placement_offset;
2278 	mpa_buf->placement_offset = data->u.placement_offset;
2279 
2280 	list_add_tail(&mpa_buf->list_entry, &iwarp_info->mpa_buf_pending_list);
2281 
2282 	qed_iwarp_process_pending_pkts(p_hwfn);
2283 	return;
2284 err:
2285 	qed_iwarp_ll2_post_rx(p_hwfn, data->cookie,
2286 			      iwarp_info->ll2_mpa_handle);
2287 }
2288 
2289 static void
2290 qed_iwarp_ll2_comp_syn_pkt(void *cxt, struct qed_ll2_comp_rx_data *data)
2291 {
2292 	struct qed_iwarp_ll2_buff *buf = data->cookie;
2293 	struct qed_iwarp_listener *listener;
2294 	struct qed_ll2_tx_pkt_info tx_pkt;
2295 	struct qed_iwarp_cm_info cm_info;
2296 	struct qed_hwfn *p_hwfn = cxt;
2297 	u8 remote_mac_addr[ETH_ALEN];
2298 	u8 local_mac_addr[ETH_ALEN];
2299 	struct qed_iwarp_ep *ep;
2300 	int tcp_start_offset;
2301 	u8 ts_hdr_size = 0;
2302 	u8 ll2_syn_handle;
2303 	int payload_len;
2304 	u32 hdr_size;
2305 	int rc;
2306 
2307 	memset(&cm_info, 0, sizeof(cm_info));
2308 	ll2_syn_handle = p_hwfn->p_rdma_info->iwarp.ll2_syn_handle;
2309 
2310 	/* Check if packet was received with errors... */
2311 	if (data->err_flags) {
2312 		DP_NOTICE(p_hwfn, "Error received on SYN packet: 0x%x\n",
2313 			  data->err_flags);
2314 		goto err;
2315 	}
2316 
2317 	if (GET_FIELD(data->parse_flags,
2318 		      PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED) &&
2319 	    GET_FIELD(data->parse_flags, PARSING_AND_ERR_FLAGS_L4CHKSMERROR)) {
2320 		DP_NOTICE(p_hwfn, "Syn packet received with checksum error\n");
2321 		goto err;
2322 	}
2323 
2324 	rc = qed_iwarp_parse_rx_pkt(p_hwfn, &cm_info, (u8 *)(buf->data) +
2325 				    data->u.placement_offset, remote_mac_addr,
2326 				    local_mac_addr, &payload_len,
2327 				    &tcp_start_offset);
2328 	if (rc)
2329 		goto err;
2330 
2331 	/* Check if there is a listener for this 4-tuple+vlan */
2332 	listener = qed_iwarp_get_listener(p_hwfn, &cm_info);
2333 	if (!listener) {
2334 		DP_VERBOSE(p_hwfn,
2335 			   QED_MSG_RDMA,
2336 			   "SYN received on tuple not listened on parse_flags=%d packet len=%d\n",
2337 			   data->parse_flags, data->length.packet_length);
2338 
2339 		memset(&tx_pkt, 0, sizeof(tx_pkt));
2340 		tx_pkt.num_of_bds = 1;
2341 		tx_pkt.vlan = data->vlan;
2342 
2343 		if (GET_FIELD(data->parse_flags,
2344 			      PARSING_AND_ERR_FLAGS_TAG8021QEXIST))
2345 			SET_FIELD(tx_pkt.bd_flags,
2346 				  CORE_TX_BD_DATA_VLAN_INSERTION, 1);
2347 
2348 		tx_pkt.l4_hdr_offset_w = (data->length.packet_length) >> 2;
2349 		tx_pkt.tx_dest = QED_LL2_TX_DEST_LB;
2350 		tx_pkt.first_frag = buf->data_phys_addr +
2351 				    data->u.placement_offset;
2352 		tx_pkt.first_frag_len = data->length.packet_length;
2353 		tx_pkt.cookie = buf;
2354 
2355 		rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_syn_handle,
2356 					       &tx_pkt, true);
2357 
2358 		if (rc) {
2359 			DP_NOTICE(p_hwfn,
2360 				  "Can't post SYN back to chip rc=%d\n", rc);
2361 			goto err;
2362 		}
2363 		return;
2364 	}
2365 
2366 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Received syn on listening port\n");
2367 	/* There may be an open ep on this connection if this is a syn
2368 	 * retrasnmit... need to make sure there isn't...
2369 	 */
2370 	if (qed_iwarp_ep_exists(p_hwfn, &cm_info))
2371 		goto err;
2372 
2373 	ep = qed_iwarp_get_free_ep(p_hwfn);
2374 	if (!ep)
2375 		goto err;
2376 
2377 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2378 	list_add_tail(&ep->list_entry, &p_hwfn->p_rdma_info->iwarp.ep_list);
2379 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2380 
2381 	ether_addr_copy(ep->remote_mac_addr, remote_mac_addr);
2382 	ether_addr_copy(ep->local_mac_addr, local_mac_addr);
2383 
2384 	memcpy(&ep->cm_info, &cm_info, sizeof(ep->cm_info));
2385 
2386 	if (p_hwfn->p_rdma_info->iwarp.tcp_flags & QED_IWARP_TS_EN)
2387 		ts_hdr_size = TIMESTAMP_HEADER_SIZE;
2388 
2389 	hdr_size = ((cm_info.ip_version == QED_TCP_IPV4) ? 40 : 60) +
2390 		   ts_hdr_size;
2391 	ep->mss = p_hwfn->p_rdma_info->iwarp.max_mtu - hdr_size;
2392 	ep->mss = min_t(u16, QED_IWARP_MAX_FW_MSS, ep->mss);
2393 
2394 	ep->event_cb = listener->event_cb;
2395 	ep->cb_context = listener->cb_context;
2396 	ep->connect_mode = TCP_CONNECT_PASSIVE;
2397 
2398 	ep->syn = buf;
2399 	ep->syn_ip_payload_length = (u16)payload_len;
2400 	ep->syn_phy_addr = buf->data_phys_addr + data->u.placement_offset +
2401 			   tcp_start_offset;
2402 
2403 	rc = qed_iwarp_tcp_offload(p_hwfn, ep);
2404 	if (rc) {
2405 		qed_iwarp_return_ep(p_hwfn, ep);
2406 		goto err;
2407 	}
2408 
2409 	return;
2410 err:
2411 	qed_iwarp_ll2_post_rx(p_hwfn, buf, ll2_syn_handle);
2412 }
2413 
2414 static void qed_iwarp_ll2_rel_rx_pkt(void *cxt, u8 connection_handle,
2415 				     void *cookie, dma_addr_t rx_buf_addr,
2416 				     bool b_last_packet)
2417 {
2418 	struct qed_iwarp_ll2_buff *buffer = cookie;
2419 	struct qed_hwfn *p_hwfn = cxt;
2420 
2421 	dma_free_coherent(&p_hwfn->cdev->pdev->dev, buffer->buff_size,
2422 			  buffer->data, buffer->data_phys_addr);
2423 	kfree(buffer);
2424 }
2425 
2426 static void qed_iwarp_ll2_comp_tx_pkt(void *cxt, u8 connection_handle,
2427 				      void *cookie, dma_addr_t first_frag_addr,
2428 				      bool b_last_fragment, bool b_last_packet)
2429 {
2430 	struct qed_iwarp_ll2_buff *buffer = cookie;
2431 	struct qed_iwarp_ll2_buff *piggy;
2432 	struct qed_hwfn *p_hwfn = cxt;
2433 
2434 	if (!buffer)		/* can happen in packed mpa unaligned... */
2435 		return;
2436 
2437 	/* this was originally an rx packet, post it back */
2438 	piggy = buffer->piggy_buf;
2439 	if (piggy) {
2440 		buffer->piggy_buf = NULL;
2441 		qed_iwarp_ll2_post_rx(p_hwfn, piggy, connection_handle);
2442 	}
2443 
2444 	qed_iwarp_ll2_post_rx(p_hwfn, buffer, connection_handle);
2445 
2446 	if (connection_handle == p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle)
2447 		qed_iwarp_process_pending_pkts(p_hwfn);
2448 
2449 	return;
2450 }
2451 
2452 static void qed_iwarp_ll2_rel_tx_pkt(void *cxt, u8 connection_handle,
2453 				     void *cookie, dma_addr_t first_frag_addr,
2454 				     bool b_last_fragment, bool b_last_packet)
2455 {
2456 	struct qed_iwarp_ll2_buff *buffer = cookie;
2457 	struct qed_hwfn *p_hwfn = cxt;
2458 
2459 	if (!buffer)
2460 		return;
2461 
2462 	if (buffer->piggy_buf) {
2463 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
2464 				  buffer->piggy_buf->buff_size,
2465 				  buffer->piggy_buf->data,
2466 				  buffer->piggy_buf->data_phys_addr);
2467 
2468 		kfree(buffer->piggy_buf);
2469 	}
2470 
2471 	dma_free_coherent(&p_hwfn->cdev->pdev->dev, buffer->buff_size,
2472 			  buffer->data, buffer->data_phys_addr);
2473 
2474 	kfree(buffer);
2475 }
2476 
2477 /* The only slowpath for iwarp ll2 is unalign flush. When this completion
2478  * is received, need to reset the FPDU.
2479  */
2480 void
2481 qed_iwarp_ll2_slowpath(void *cxt,
2482 		       u8 connection_handle,
2483 		       u32 opaque_data_0, u32 opaque_data_1)
2484 {
2485 	struct unaligned_opaque_data unalign_data;
2486 	struct qed_hwfn *p_hwfn = cxt;
2487 	struct qed_iwarp_fpdu *fpdu;
2488 
2489 	qed_iwarp_mpa_get_data(p_hwfn, &unalign_data,
2490 			       opaque_data_0, opaque_data_1);
2491 
2492 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "(0x%x) Flush fpdu\n",
2493 		   unalign_data.cid);
2494 
2495 	fpdu = qed_iwarp_get_curr_fpdu(p_hwfn, (u16)unalign_data.cid);
2496 	if (fpdu)
2497 		memset(fpdu, 0, sizeof(*fpdu));
2498 }
2499 
2500 static int qed_iwarp_ll2_stop(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2501 {
2502 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2503 	int rc = 0;
2504 
2505 	if (iwarp_info->ll2_syn_handle != QED_IWARP_HANDLE_INVAL) {
2506 		rc = qed_ll2_terminate_connection(p_hwfn,
2507 						  iwarp_info->ll2_syn_handle);
2508 		if (rc)
2509 			DP_INFO(p_hwfn, "Failed to terminate syn connection\n");
2510 
2511 		qed_ll2_release_connection(p_hwfn, iwarp_info->ll2_syn_handle);
2512 		iwarp_info->ll2_syn_handle = QED_IWARP_HANDLE_INVAL;
2513 	}
2514 
2515 	if (iwarp_info->ll2_ooo_handle != QED_IWARP_HANDLE_INVAL) {
2516 		rc = qed_ll2_terminate_connection(p_hwfn,
2517 						  iwarp_info->ll2_ooo_handle);
2518 		if (rc)
2519 			DP_INFO(p_hwfn, "Failed to terminate ooo connection\n");
2520 
2521 		qed_ll2_release_connection(p_hwfn, iwarp_info->ll2_ooo_handle);
2522 		iwarp_info->ll2_ooo_handle = QED_IWARP_HANDLE_INVAL;
2523 	}
2524 
2525 	if (iwarp_info->ll2_mpa_handle != QED_IWARP_HANDLE_INVAL) {
2526 		rc = qed_ll2_terminate_connection(p_hwfn,
2527 						  iwarp_info->ll2_mpa_handle);
2528 		if (rc)
2529 			DP_INFO(p_hwfn, "Failed to terminate mpa connection\n");
2530 
2531 		qed_ll2_release_connection(p_hwfn, iwarp_info->ll2_mpa_handle);
2532 		iwarp_info->ll2_mpa_handle = QED_IWARP_HANDLE_INVAL;
2533 	}
2534 
2535 	qed_llh_remove_mac_filter(p_hwfn,
2536 				  p_ptt, p_hwfn->p_rdma_info->iwarp.mac_addr);
2537 	return rc;
2538 }
2539 
2540 static int
2541 qed_iwarp_ll2_alloc_buffers(struct qed_hwfn *p_hwfn,
2542 			    int num_rx_bufs, int buff_size, u8 ll2_handle)
2543 {
2544 	struct qed_iwarp_ll2_buff *buffer;
2545 	int rc = 0;
2546 	int i;
2547 
2548 	for (i = 0; i < num_rx_bufs; i++) {
2549 		buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
2550 		if (!buffer) {
2551 			rc = -ENOMEM;
2552 			break;
2553 		}
2554 
2555 		buffer->data = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
2556 						  buff_size,
2557 						  &buffer->data_phys_addr,
2558 						  GFP_KERNEL);
2559 		if (!buffer->data) {
2560 			kfree(buffer);
2561 			rc = -ENOMEM;
2562 			break;
2563 		}
2564 
2565 		buffer->buff_size = buff_size;
2566 		rc = qed_iwarp_ll2_post_rx(p_hwfn, buffer, ll2_handle);
2567 		if (rc)
2568 			/* buffers will be deallocated by qed_ll2 */
2569 			break;
2570 	}
2571 	return rc;
2572 }
2573 
2574 #define QED_IWARP_MAX_BUF_SIZE(mtu)				     \
2575 	ALIGN((mtu) + ETH_HLEN + 2 * VLAN_HLEN + 2 + ETH_CACHE_LINE_SIZE, \
2576 		ETH_CACHE_LINE_SIZE)
2577 
2578 static int
2579 qed_iwarp_ll2_start(struct qed_hwfn *p_hwfn,
2580 		    struct qed_rdma_start_in_params *params,
2581 		    struct qed_ptt *p_ptt)
2582 {
2583 	struct qed_iwarp_info *iwarp_info;
2584 	struct qed_ll2_acquire_data data;
2585 	struct qed_ll2_cbs cbs;
2586 	u32 mpa_buff_size;
2587 	u16 n_ooo_bufs;
2588 	int rc = 0;
2589 	int i;
2590 
2591 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2592 	iwarp_info->ll2_syn_handle = QED_IWARP_HANDLE_INVAL;
2593 	iwarp_info->ll2_ooo_handle = QED_IWARP_HANDLE_INVAL;
2594 	iwarp_info->ll2_mpa_handle = QED_IWARP_HANDLE_INVAL;
2595 
2596 	iwarp_info->max_mtu = params->max_mtu;
2597 
2598 	ether_addr_copy(p_hwfn->p_rdma_info->iwarp.mac_addr, params->mac_addr);
2599 
2600 	rc = qed_llh_add_mac_filter(p_hwfn, p_ptt, params->mac_addr);
2601 	if (rc)
2602 		return rc;
2603 
2604 	/* Start SYN connection */
2605 	cbs.rx_comp_cb = qed_iwarp_ll2_comp_syn_pkt;
2606 	cbs.rx_release_cb = qed_iwarp_ll2_rel_rx_pkt;
2607 	cbs.tx_comp_cb = qed_iwarp_ll2_comp_tx_pkt;
2608 	cbs.tx_release_cb = qed_iwarp_ll2_rel_tx_pkt;
2609 	cbs.cookie = p_hwfn;
2610 
2611 	memset(&data, 0, sizeof(data));
2612 	data.input.conn_type = QED_LL2_TYPE_IWARP;
2613 	data.input.mtu = QED_IWARP_MAX_SYN_PKT_SIZE;
2614 	data.input.rx_num_desc = QED_IWARP_LL2_SYN_RX_SIZE;
2615 	data.input.tx_num_desc = QED_IWARP_LL2_SYN_TX_SIZE;
2616 	data.input.tx_max_bds_per_packet = 1;	/* will never be fragmented */
2617 	data.input.tx_tc = PKT_LB_TC;
2618 	data.input.tx_dest = QED_LL2_TX_DEST_LB;
2619 	data.p_connection_handle = &iwarp_info->ll2_syn_handle;
2620 	data.cbs = &cbs;
2621 
2622 	rc = qed_ll2_acquire_connection(p_hwfn, &data);
2623 	if (rc) {
2624 		DP_NOTICE(p_hwfn, "Failed to acquire LL2 connection\n");
2625 		qed_llh_remove_mac_filter(p_hwfn, p_ptt, params->mac_addr);
2626 		return rc;
2627 	}
2628 
2629 	rc = qed_ll2_establish_connection(p_hwfn, iwarp_info->ll2_syn_handle);
2630 	if (rc) {
2631 		DP_NOTICE(p_hwfn, "Failed to establish LL2 connection\n");
2632 		goto err;
2633 	}
2634 
2635 	rc = qed_iwarp_ll2_alloc_buffers(p_hwfn,
2636 					 QED_IWARP_LL2_SYN_RX_SIZE,
2637 					 QED_IWARP_MAX_SYN_PKT_SIZE,
2638 					 iwarp_info->ll2_syn_handle);
2639 	if (rc)
2640 		goto err;
2641 
2642 	/* Start OOO connection */
2643 	data.input.conn_type = QED_LL2_TYPE_OOO;
2644 	data.input.mtu = params->max_mtu;
2645 
2646 	n_ooo_bufs = (QED_IWARP_MAX_OOO * QED_IWARP_RCV_WND_SIZE_DEF) /
2647 		     iwarp_info->max_mtu;
2648 	n_ooo_bufs = min_t(u32, n_ooo_bufs, QED_IWARP_LL2_OOO_MAX_RX_SIZE);
2649 
2650 	data.input.rx_num_desc = n_ooo_bufs;
2651 	data.input.rx_num_ooo_buffers = n_ooo_bufs;
2652 
2653 	data.input.tx_max_bds_per_packet = 1;	/* will never be fragmented */
2654 	data.input.tx_num_desc = QED_IWARP_LL2_OOO_DEF_TX_SIZE;
2655 	data.p_connection_handle = &iwarp_info->ll2_ooo_handle;
2656 
2657 	rc = qed_ll2_acquire_connection(p_hwfn, &data);
2658 	if (rc)
2659 		goto err;
2660 
2661 	rc = qed_ll2_establish_connection(p_hwfn, iwarp_info->ll2_ooo_handle);
2662 	if (rc)
2663 		goto err;
2664 
2665 	/* Start Unaligned MPA connection */
2666 	cbs.rx_comp_cb = qed_iwarp_ll2_comp_mpa_pkt;
2667 	cbs.slowpath_cb = qed_iwarp_ll2_slowpath;
2668 
2669 	memset(&data, 0, sizeof(data));
2670 	data.input.conn_type = QED_LL2_TYPE_IWARP;
2671 	data.input.mtu = params->max_mtu;
2672 	/* FW requires that once a packet arrives OOO, it must have at
2673 	 * least 2 rx buffers available on the unaligned connection
2674 	 * for handling the case that it is a partial fpdu.
2675 	 */
2676 	data.input.rx_num_desc = n_ooo_bufs * 2;
2677 	data.input.tx_num_desc = data.input.rx_num_desc;
2678 	data.input.tx_max_bds_per_packet = QED_IWARP_MAX_BDS_PER_FPDU;
2679 	data.p_connection_handle = &iwarp_info->ll2_mpa_handle;
2680 	data.input.secondary_queue = true;
2681 	data.cbs = &cbs;
2682 
2683 	rc = qed_ll2_acquire_connection(p_hwfn, &data);
2684 	if (rc)
2685 		goto err;
2686 
2687 	rc = qed_ll2_establish_connection(p_hwfn, iwarp_info->ll2_mpa_handle);
2688 	if (rc)
2689 		goto err;
2690 
2691 	mpa_buff_size = QED_IWARP_MAX_BUF_SIZE(params->max_mtu);
2692 	rc = qed_iwarp_ll2_alloc_buffers(p_hwfn,
2693 					 data.input.rx_num_desc,
2694 					 mpa_buff_size,
2695 					 iwarp_info->ll2_mpa_handle);
2696 	if (rc)
2697 		goto err;
2698 
2699 	iwarp_info->partial_fpdus = kcalloc((u16)p_hwfn->p_rdma_info->num_qps,
2700 					    sizeof(*iwarp_info->partial_fpdus),
2701 					    GFP_KERNEL);
2702 	if (!iwarp_info->partial_fpdus)
2703 		goto err;
2704 
2705 	iwarp_info->max_num_partial_fpdus = (u16)p_hwfn->p_rdma_info->num_qps;
2706 
2707 	iwarp_info->mpa_intermediate_buf = kzalloc(mpa_buff_size, GFP_KERNEL);
2708 	if (!iwarp_info->mpa_intermediate_buf)
2709 		goto err;
2710 
2711 	/* The mpa_bufs array serves for pending RX packets received on the
2712 	 * mpa ll2 that don't have place on the tx ring and require later
2713 	 * processing. We can't fail on allocation of such a struct therefore
2714 	 * we allocate enough to take care of all rx packets
2715 	 */
2716 	iwarp_info->mpa_bufs = kcalloc(data.input.rx_num_desc,
2717 				       sizeof(*iwarp_info->mpa_bufs),
2718 				       GFP_KERNEL);
2719 	if (!iwarp_info->mpa_bufs)
2720 		goto err;
2721 
2722 	INIT_LIST_HEAD(&iwarp_info->mpa_buf_pending_list);
2723 	INIT_LIST_HEAD(&iwarp_info->mpa_buf_list);
2724 	for (i = 0; i < data.input.rx_num_desc; i++)
2725 		list_add_tail(&iwarp_info->mpa_bufs[i].list_entry,
2726 			      &iwarp_info->mpa_buf_list);
2727 	return rc;
2728 err:
2729 	qed_iwarp_ll2_stop(p_hwfn, p_ptt);
2730 
2731 	return rc;
2732 }
2733 
2734 int qed_iwarp_setup(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
2735 		    struct qed_rdma_start_in_params *params)
2736 {
2737 	struct qed_iwarp_info *iwarp_info;
2738 	u32 rcv_wnd_size;
2739 
2740 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2741 
2742 	iwarp_info->tcp_flags = QED_IWARP_TS_EN;
2743 	rcv_wnd_size = QED_IWARP_RCV_WND_SIZE_DEF;
2744 
2745 	/* value 0 is used for ilog2(QED_IWARP_RCV_WND_SIZE_MIN) */
2746 	iwarp_info->rcv_wnd_scale = ilog2(rcv_wnd_size) -
2747 	    ilog2(QED_IWARP_RCV_WND_SIZE_MIN);
2748 	iwarp_info->crc_needed = QED_IWARP_PARAM_CRC_NEEDED;
2749 	iwarp_info->mpa_rev = MPA_NEGOTIATION_TYPE_ENHANCED;
2750 
2751 	iwarp_info->peer2peer = QED_IWARP_PARAM_P2P;
2752 
2753 	iwarp_info->rtr_type =  MPA_RTR_TYPE_ZERO_SEND |
2754 				MPA_RTR_TYPE_ZERO_WRITE |
2755 				MPA_RTR_TYPE_ZERO_READ;
2756 
2757 	spin_lock_init(&p_hwfn->p_rdma_info->iwarp.qp_lock);
2758 	INIT_LIST_HEAD(&p_hwfn->p_rdma_info->iwarp.ep_list);
2759 	INIT_LIST_HEAD(&p_hwfn->p_rdma_info->iwarp.listen_list);
2760 
2761 	qed_spq_register_async_cb(p_hwfn, PROTOCOLID_IWARP,
2762 				  qed_iwarp_async_event);
2763 	qed_ooo_setup(p_hwfn);
2764 
2765 	return qed_iwarp_ll2_start(p_hwfn, params, p_ptt);
2766 }
2767 
2768 int qed_iwarp_stop(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2769 {
2770 	int rc;
2771 
2772 	qed_iwarp_free_prealloc_ep(p_hwfn);
2773 	rc = qed_iwarp_wait_for_all_cids(p_hwfn);
2774 	if (rc)
2775 		return rc;
2776 
2777 	qed_spq_unregister_async_cb(p_hwfn, PROTOCOLID_IWARP);
2778 
2779 	return qed_iwarp_ll2_stop(p_hwfn, p_ptt);
2780 }
2781 
2782 void qed_iwarp_qp_in_error(struct qed_hwfn *p_hwfn,
2783 			   struct qed_iwarp_ep *ep, u8 fw_return_code)
2784 {
2785 	struct qed_iwarp_cm_event_params params;
2786 
2787 	qed_iwarp_modify_qp(p_hwfn, ep->qp, QED_IWARP_QP_STATE_ERROR, true);
2788 
2789 	params.event = QED_IWARP_EVENT_CLOSE;
2790 	params.ep_context = ep;
2791 	params.cm_info = &ep->cm_info;
2792 	params.status = (fw_return_code == IWARP_QP_IN_ERROR_GOOD_CLOSE) ?
2793 			 0 : -ECONNRESET;
2794 
2795 	ep->state = QED_IWARP_EP_CLOSED;
2796 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2797 	list_del(&ep->list_entry);
2798 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2799 
2800 	ep->event_cb(ep->cb_context, &params);
2801 }
2802 
2803 void qed_iwarp_exception_received(struct qed_hwfn *p_hwfn,
2804 				  struct qed_iwarp_ep *ep, int fw_ret_code)
2805 {
2806 	struct qed_iwarp_cm_event_params params;
2807 	bool event_cb = false;
2808 
2809 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "EP(0x%x) fw_ret_code=%d\n",
2810 		   ep->cid, fw_ret_code);
2811 
2812 	switch (fw_ret_code) {
2813 	case IWARP_EXCEPTION_DETECTED_LLP_CLOSED:
2814 		params.status = 0;
2815 		params.event = QED_IWARP_EVENT_DISCONNECT;
2816 		event_cb = true;
2817 		break;
2818 	case IWARP_EXCEPTION_DETECTED_LLP_RESET:
2819 		params.status = -ECONNRESET;
2820 		params.event = QED_IWARP_EVENT_DISCONNECT;
2821 		event_cb = true;
2822 		break;
2823 	case IWARP_EXCEPTION_DETECTED_RQ_EMPTY:
2824 		params.event = QED_IWARP_EVENT_RQ_EMPTY;
2825 		event_cb = true;
2826 		break;
2827 	case IWARP_EXCEPTION_DETECTED_IRQ_FULL:
2828 		params.event = QED_IWARP_EVENT_IRQ_FULL;
2829 		event_cb = true;
2830 		break;
2831 	case IWARP_EXCEPTION_DETECTED_LLP_TIMEOUT:
2832 		params.event = QED_IWARP_EVENT_LLP_TIMEOUT;
2833 		event_cb = true;
2834 		break;
2835 	case IWARP_EXCEPTION_DETECTED_REMOTE_PROTECTION_ERROR:
2836 		params.event = QED_IWARP_EVENT_REMOTE_PROTECTION_ERROR;
2837 		event_cb = true;
2838 		break;
2839 	case IWARP_EXCEPTION_DETECTED_CQ_OVERFLOW:
2840 		params.event = QED_IWARP_EVENT_CQ_OVERFLOW;
2841 		event_cb = true;
2842 		break;
2843 	case IWARP_EXCEPTION_DETECTED_LOCAL_CATASTROPHIC:
2844 		params.event = QED_IWARP_EVENT_QP_CATASTROPHIC;
2845 		event_cb = true;
2846 		break;
2847 	case IWARP_EXCEPTION_DETECTED_LOCAL_ACCESS_ERROR:
2848 		params.event = QED_IWARP_EVENT_LOCAL_ACCESS_ERROR;
2849 		event_cb = true;
2850 		break;
2851 	case IWARP_EXCEPTION_DETECTED_REMOTE_OPERATION_ERROR:
2852 		params.event = QED_IWARP_EVENT_REMOTE_OPERATION_ERROR;
2853 		event_cb = true;
2854 		break;
2855 	case IWARP_EXCEPTION_DETECTED_TERMINATE_RECEIVED:
2856 		params.event = QED_IWARP_EVENT_TERMINATE_RECEIVED;
2857 		event_cb = true;
2858 		break;
2859 	default:
2860 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2861 			   "Unhandled exception received...fw_ret_code=%d\n",
2862 			   fw_ret_code);
2863 		break;
2864 	}
2865 
2866 	if (event_cb) {
2867 		params.ep_context = ep;
2868 		params.cm_info = &ep->cm_info;
2869 		ep->event_cb(ep->cb_context, &params);
2870 	}
2871 }
2872 
2873 static void
2874 qed_iwarp_tcp_connect_unsuccessful(struct qed_hwfn *p_hwfn,
2875 				   struct qed_iwarp_ep *ep, u8 fw_return_code)
2876 {
2877 	struct qed_iwarp_cm_event_params params;
2878 
2879 	memset(&params, 0, sizeof(params));
2880 	params.event = QED_IWARP_EVENT_ACTIVE_COMPLETE;
2881 	params.ep_context = ep;
2882 	params.cm_info = &ep->cm_info;
2883 	ep->state = QED_IWARP_EP_CLOSED;
2884 
2885 	switch (fw_return_code) {
2886 	case IWARP_CONN_ERROR_TCP_CONNECT_INVALID_PACKET:
2887 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2888 			   "%s(0x%x) TCP connect got invalid packet\n",
2889 			   QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
2890 		params.status = -ECONNRESET;
2891 		break;
2892 	case IWARP_CONN_ERROR_TCP_CONNECTION_RST:
2893 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2894 			   "%s(0x%x) TCP Connection Reset\n",
2895 			   QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
2896 		params.status = -ECONNRESET;
2897 		break;
2898 	case IWARP_CONN_ERROR_TCP_CONNECT_TIMEOUT:
2899 		DP_NOTICE(p_hwfn, "%s(0x%x) TCP timeout\n",
2900 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
2901 		params.status = -EBUSY;
2902 		break;
2903 	case IWARP_CONN_ERROR_MPA_NOT_SUPPORTED_VER:
2904 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA not supported VER\n",
2905 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
2906 		params.status = -ECONNREFUSED;
2907 		break;
2908 	case IWARP_CONN_ERROR_MPA_INVALID_PACKET:
2909 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA Invalid Packet\n",
2910 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
2911 		params.status = -ECONNRESET;
2912 		break;
2913 	default:
2914 		DP_ERR(p_hwfn,
2915 		       "%s(0x%x) Unexpected return code tcp connect: %d\n",
2916 		       QED_IWARP_CONNECT_MODE_STRING(ep),
2917 		       ep->tcp_cid, fw_return_code);
2918 		params.status = -ECONNRESET;
2919 		break;
2920 	}
2921 
2922 	if (ep->connect_mode == TCP_CONNECT_PASSIVE) {
2923 		ep->tcp_cid = QED_IWARP_INVALID_TCP_CID;
2924 		qed_iwarp_return_ep(p_hwfn, ep);
2925 	} else {
2926 		ep->event_cb(ep->cb_context, &params);
2927 		spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2928 		list_del(&ep->list_entry);
2929 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2930 	}
2931 }
2932 
2933 void
2934 qed_iwarp_connect_complete(struct qed_hwfn *p_hwfn,
2935 			   struct qed_iwarp_ep *ep, u8 fw_return_code)
2936 {
2937 	u8 ll2_syn_handle = p_hwfn->p_rdma_info->iwarp.ll2_syn_handle;
2938 
2939 	if (ep->connect_mode == TCP_CONNECT_PASSIVE) {
2940 		/* Done with the SYN packet, post back to ll2 rx */
2941 		qed_iwarp_ll2_post_rx(p_hwfn, ep->syn, ll2_syn_handle);
2942 
2943 		ep->syn = NULL;
2944 
2945 		/* If connect failed - upper layer doesn't know about it */
2946 		if (fw_return_code == RDMA_RETURN_OK)
2947 			qed_iwarp_mpa_received(p_hwfn, ep);
2948 		else
2949 			qed_iwarp_tcp_connect_unsuccessful(p_hwfn, ep,
2950 							   fw_return_code);
2951 	} else {
2952 		if (fw_return_code == RDMA_RETURN_OK)
2953 			qed_iwarp_mpa_offload(p_hwfn, ep);
2954 		else
2955 			qed_iwarp_tcp_connect_unsuccessful(p_hwfn, ep,
2956 							   fw_return_code);
2957 	}
2958 }
2959 
2960 static inline bool
2961 qed_iwarp_check_ep_ok(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
2962 {
2963 	if (!ep || (ep->sig != QED_EP_SIG)) {
2964 		DP_ERR(p_hwfn, "ERROR ON ASYNC ep=%p\n", ep);
2965 		return false;
2966 	}
2967 
2968 	return true;
2969 }
2970 
2971 static int qed_iwarp_async_event(struct qed_hwfn *p_hwfn,
2972 				 u8 fw_event_code, u16 echo,
2973 				 union event_ring_data *data,
2974 				 u8 fw_return_code)
2975 {
2976 	struct regpair *fw_handle = &data->rdma_data.async_handle;
2977 	struct qed_iwarp_ep *ep = NULL;
2978 	u16 cid;
2979 
2980 	ep = (struct qed_iwarp_ep *)(uintptr_t)HILO_64(fw_handle->hi,
2981 						       fw_handle->lo);
2982 
2983 	switch (fw_event_code) {
2984 	case IWARP_EVENT_TYPE_ASYNC_CONNECT_COMPLETE:
2985 		/* Async completion after TCP 3-way handshake */
2986 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
2987 			return -EINVAL;
2988 		DP_VERBOSE(p_hwfn,
2989 			   QED_MSG_RDMA,
2990 			   "EP(0x%x) IWARP_EVENT_TYPE_ASYNC_CONNECT_COMPLETE fw_ret_code=%d\n",
2991 			   ep->tcp_cid, fw_return_code);
2992 		qed_iwarp_connect_complete(p_hwfn, ep, fw_return_code);
2993 		break;
2994 	case IWARP_EVENT_TYPE_ASYNC_EXCEPTION_DETECTED:
2995 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
2996 			return -EINVAL;
2997 		DP_VERBOSE(p_hwfn,
2998 			   QED_MSG_RDMA,
2999 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_EXCEPTION_DETECTED fw_ret_code=%d\n",
3000 			   ep->cid, fw_return_code);
3001 		qed_iwarp_exception_received(p_hwfn, ep, fw_return_code);
3002 		break;
3003 	case IWARP_EVENT_TYPE_ASYNC_QP_IN_ERROR_STATE:
3004 		/* Async completion for Close Connection ramrod */
3005 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
3006 			return -EINVAL;
3007 		DP_VERBOSE(p_hwfn,
3008 			   QED_MSG_RDMA,
3009 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_QP_IN_ERROR_STATE fw_ret_code=%d\n",
3010 			   ep->cid, fw_return_code);
3011 		qed_iwarp_qp_in_error(p_hwfn, ep, fw_return_code);
3012 		break;
3013 	case IWARP_EVENT_TYPE_ASYNC_ENHANCED_MPA_REPLY_ARRIVED:
3014 		/* Async event for active side only */
3015 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
3016 			return -EINVAL;
3017 		DP_VERBOSE(p_hwfn,
3018 			   QED_MSG_RDMA,
3019 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_MPA_REPLY_ARRIVED fw_ret_code=%d\n",
3020 			   ep->cid, fw_return_code);
3021 		qed_iwarp_mpa_reply_arrived(p_hwfn, ep);
3022 		break;
3023 	case IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_COMPLETE:
3024 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
3025 			return -EINVAL;
3026 		DP_VERBOSE(p_hwfn,
3027 			   QED_MSG_RDMA,
3028 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_COMPLETE fw_ret_code=%d\n",
3029 			   ep->cid, fw_return_code);
3030 		qed_iwarp_mpa_complete(p_hwfn, ep, fw_return_code);
3031 		break;
3032 	case IWARP_EVENT_TYPE_ASYNC_CID_CLEANED:
3033 		cid = (u16)le32_to_cpu(fw_handle->lo);
3034 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
3035 			   "(0x%x)IWARP_EVENT_TYPE_ASYNC_CID_CLEANED\n", cid);
3036 		qed_iwarp_cid_cleaned(p_hwfn, cid);
3037 
3038 		break;
3039 	case IWARP_EVENT_TYPE_ASYNC_CQ_OVERFLOW:
3040 		DP_NOTICE(p_hwfn, "IWARP_EVENT_TYPE_ASYNC_CQ_OVERFLOW\n");
3041 
3042 		p_hwfn->p_rdma_info->events.affiliated_event(
3043 			p_hwfn->p_rdma_info->events.context,
3044 			QED_IWARP_EVENT_CQ_OVERFLOW,
3045 			(void *)fw_handle);
3046 		break;
3047 	default:
3048 		DP_ERR(p_hwfn, "Received unexpected async iwarp event %d\n",
3049 		       fw_event_code);
3050 		return -EINVAL;
3051 	}
3052 	return 0;
3053 }
3054 
3055 int
3056 qed_iwarp_create_listen(void *rdma_cxt,
3057 			struct qed_iwarp_listen_in *iparams,
3058 			struct qed_iwarp_listen_out *oparams)
3059 {
3060 	struct qed_hwfn *p_hwfn = rdma_cxt;
3061 	struct qed_iwarp_listener *listener;
3062 
3063 	listener = kzalloc(sizeof(*listener), GFP_KERNEL);
3064 	if (!listener)
3065 		return -ENOMEM;
3066 
3067 	listener->ip_version = iparams->ip_version;
3068 	memcpy(listener->ip_addr, iparams->ip_addr, sizeof(listener->ip_addr));
3069 	listener->port = iparams->port;
3070 	listener->vlan = iparams->vlan;
3071 
3072 	listener->event_cb = iparams->event_cb;
3073 	listener->cb_context = iparams->cb_context;
3074 	listener->max_backlog = iparams->max_backlog;
3075 	oparams->handle = listener;
3076 
3077 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
3078 	list_add_tail(&listener->list_entry,
3079 		      &p_hwfn->p_rdma_info->iwarp.listen_list);
3080 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
3081 
3082 	DP_VERBOSE(p_hwfn,
3083 		   QED_MSG_RDMA,
3084 		   "callback=%p handle=%p ip=%x:%x:%x:%x port=0x%x vlan=0x%x\n",
3085 		   listener->event_cb,
3086 		   listener,
3087 		   listener->ip_addr[0],
3088 		   listener->ip_addr[1],
3089 		   listener->ip_addr[2],
3090 		   listener->ip_addr[3], listener->port, listener->vlan);
3091 
3092 	return 0;
3093 }
3094 
3095 int qed_iwarp_destroy_listen(void *rdma_cxt, void *handle)
3096 {
3097 	struct qed_iwarp_listener *listener = handle;
3098 	struct qed_hwfn *p_hwfn = rdma_cxt;
3099 
3100 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "handle=%p\n", handle);
3101 
3102 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
3103 	list_del(&listener->list_entry);
3104 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
3105 
3106 	kfree(listener);
3107 
3108 	return 0;
3109 }
3110 
3111 int qed_iwarp_send_rtr(void *rdma_cxt, struct qed_iwarp_send_rtr_in *iparams)
3112 {
3113 	struct qed_hwfn *p_hwfn = rdma_cxt;
3114 	struct qed_sp_init_data init_data;
3115 	struct qed_spq_entry *p_ent;
3116 	struct qed_iwarp_ep *ep;
3117 	struct qed_rdma_qp *qp;
3118 	int rc;
3119 
3120 	ep = iparams->ep_context;
3121 	if (!ep) {
3122 		DP_ERR(p_hwfn, "Ep Context receive in send_rtr is NULL\n");
3123 		return -EINVAL;
3124 	}
3125 
3126 	qp = ep->qp;
3127 
3128 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) EP(0x%x)\n",
3129 		   qp->icid, ep->tcp_cid);
3130 
3131 	memset(&init_data, 0, sizeof(init_data));
3132 	init_data.cid = qp->icid;
3133 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
3134 	init_data.comp_mode = QED_SPQ_MODE_CB;
3135 
3136 	rc = qed_sp_init_request(p_hwfn, &p_ent,
3137 				 IWARP_RAMROD_CMD_ID_MPA_OFFLOAD_SEND_RTR,
3138 				 PROTOCOLID_IWARP, &init_data);
3139 
3140 	if (rc)
3141 		return rc;
3142 
3143 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
3144 
3145 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "rc = 0x%x\n", rc);
3146 
3147 	return rc;
3148 }
3149 
3150 void
3151 qed_iwarp_query_qp(struct qed_rdma_qp *qp,
3152 		   struct qed_rdma_query_qp_out_params *out_params)
3153 {
3154 	out_params->state = qed_iwarp2roce_state(qp->iwarp_state);
3155 }
3156