xref: /linux/drivers/net/ethernet/qlogic/qed/qed_iwarp.c (revision 39dbc646fd2c67ee9b71450ce172cbd714d4e7fb)
167b40dccSKalderon, Michal /* QLogic qed NIC Driver
267b40dccSKalderon, Michal  * Copyright (c) 2015-2017  QLogic Corporation
367b40dccSKalderon, Michal  *
467b40dccSKalderon, Michal  * This software is available to you under a choice of one of two
567b40dccSKalderon, Michal  * licenses.  You may choose to be licensed under the terms of the GNU
667b40dccSKalderon, Michal  * General Public License (GPL) Version 2, available from the file
767b40dccSKalderon, Michal  * COPYING in the main directory of this source tree, or the
867b40dccSKalderon, Michal  * OpenIB.org BSD license below:
967b40dccSKalderon, Michal  *
1067b40dccSKalderon, Michal  *     Redistribution and use in source and binary forms, with or
1167b40dccSKalderon, Michal  *     without modification, are permitted provided that the following
1267b40dccSKalderon, Michal  *     conditions are met:
1367b40dccSKalderon, Michal  *
1467b40dccSKalderon, Michal  *      - Redistributions of source code must retain the above
1567b40dccSKalderon, Michal  *        copyright notice, this list of conditions and the following
1667b40dccSKalderon, Michal  *        disclaimer.
1767b40dccSKalderon, Michal  *
1867b40dccSKalderon, Michal  *      - Redistributions in binary form must reproduce the above
1967b40dccSKalderon, Michal  *        copyright notice, this list of conditions and the following
2067b40dccSKalderon, Michal  *        disclaimer in the documentation and /or other materials
2167b40dccSKalderon, Michal  *        provided with the distribution.
2267b40dccSKalderon, Michal  *
2367b40dccSKalderon, Michal  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2467b40dccSKalderon, Michal  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2567b40dccSKalderon, Michal  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2667b40dccSKalderon, Michal  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2767b40dccSKalderon, Michal  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2867b40dccSKalderon, Michal  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2967b40dccSKalderon, Michal  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3067b40dccSKalderon, Michal  * SOFTWARE.
3167b40dccSKalderon, Michal  */
32b5c29ca7SKalderon, Michal #include <linux/if_ether.h>
33b5c29ca7SKalderon, Michal #include <linux/if_vlan.h>
3465a91a6cSKalderon, Michal #include <linux/ip.h>
3565a91a6cSKalderon, Michal #include <linux/ipv6.h>
3665a91a6cSKalderon, Michal #include <linux/spinlock.h>
3765a91a6cSKalderon, Michal #include <linux/tcp.h>
3867b40dccSKalderon, Michal #include "qed_cxt.h"
3967b40dccSKalderon, Michal #include "qed_hw.h"
40b5c29ca7SKalderon, Michal #include "qed_ll2.h"
4167b40dccSKalderon, Michal #include "qed_rdma.h"
4267b40dccSKalderon, Michal #include "qed_reg_addr.h"
4367b40dccSKalderon, Michal #include "qed_sp.h"
44d1abfd0bSMichal Kalderon #include "qed_ooo.h"
4567b40dccSKalderon, Michal 
4667b40dccSKalderon, Michal #define QED_IWARP_ORD_DEFAULT		32
4767b40dccSKalderon, Michal #define QED_IWARP_IRD_DEFAULT		32
48456a5849SKalderon, Michal #define QED_IWARP_MAX_FW_MSS		4120
49456a5849SKalderon, Michal 
50456a5849SKalderon, Michal #define QED_EP_SIG 0xecabcdef
51456a5849SKalderon, Michal 
52456a5849SKalderon, Michal struct mpa_v2_hdr {
53456a5849SKalderon, Michal 	__be16 ird;
54456a5849SKalderon, Michal 	__be16 ord;
55456a5849SKalderon, Michal };
56456a5849SKalderon, Michal 
57456a5849SKalderon, Michal #define MPA_V2_PEER2PEER_MODEL  0x8000
58456a5849SKalderon, Michal #define MPA_V2_SEND_RTR         0x4000	/* on ird */
59456a5849SKalderon, Michal #define MPA_V2_READ_RTR         0x4000	/* on ord */
60456a5849SKalderon, Michal #define MPA_V2_WRITE_RTR        0x8000
61456a5849SKalderon, Michal #define MPA_V2_IRD_ORD_MASK     0x3FFF
62456a5849SKalderon, Michal 
63456a5849SKalderon, Michal #define MPA_REV2(_mpa_rev) ((_mpa_rev) == MPA_NEGOTIATION_TYPE_ENHANCED)
64456a5849SKalderon, Michal 
65456a5849SKalderon, Michal #define QED_IWARP_INVALID_TCP_CID	0xffffffff
6667b40dccSKalderon, Michal #define QED_IWARP_RCV_WND_SIZE_DEF	(256 * 1024)
67da090917STomer Tayar #define QED_IWARP_RCV_WND_SIZE_MIN	(0xffff)
68456a5849SKalderon, Michal #define TIMESTAMP_HEADER_SIZE		(12)
69da090917STomer Tayar #define QED_IWARP_MAX_FIN_RT_DEFAULT	(2)
70456a5849SKalderon, Michal 
7167b40dccSKalderon, Michal #define QED_IWARP_TS_EN			BIT(0)
72456a5849SKalderon, Michal #define QED_IWARP_DA_EN			BIT(1)
7367b40dccSKalderon, Michal #define QED_IWARP_PARAM_CRC_NEEDED	(1)
7467b40dccSKalderon, Michal #define QED_IWARP_PARAM_P2P		(1)
7567b40dccSKalderon, Michal 
76da090917STomer Tayar #define QED_IWARP_DEF_MAX_RT_TIME	(0)
77da090917STomer Tayar #define QED_IWARP_DEF_CWND_FACTOR	(4)
78da090917STomer Tayar #define QED_IWARP_DEF_KA_MAX_PROBE_CNT	(5)
79da090917STomer Tayar #define QED_IWARP_DEF_KA_TIMEOUT	(1200000)	/* 20 min */
80da090917STomer Tayar #define QED_IWARP_DEF_KA_INTERVAL	(1000)		/* 1 sec */
81da090917STomer Tayar 
8267b40dccSKalderon, Michal static int qed_iwarp_async_event(struct qed_hwfn *p_hwfn,
8367b40dccSKalderon, Michal 				 u8 fw_event_code, u16 echo,
8467b40dccSKalderon, Michal 				 union event_ring_data *data,
8567b40dccSKalderon, Michal 				 u8 fw_return_code);
8667b40dccSKalderon, Michal 
8767b40dccSKalderon, Michal /* Override devinfo with iWARP specific values */
8867b40dccSKalderon, Michal void qed_iwarp_init_devinfo(struct qed_hwfn *p_hwfn)
8967b40dccSKalderon, Michal {
9067b40dccSKalderon, Michal 	struct qed_rdma_device *dev = p_hwfn->p_rdma_info->dev;
9167b40dccSKalderon, Michal 
9267b40dccSKalderon, Michal 	dev->max_inline = IWARP_REQ_MAX_INLINE_DATA_SIZE;
9367b40dccSKalderon, Michal 	dev->max_qp = min_t(u32,
9467b40dccSKalderon, Michal 			    IWARP_MAX_QPS,
95456a5849SKalderon, Michal 			    p_hwfn->p_rdma_info->num_qps) -
96456a5849SKalderon, Michal 		      QED_IWARP_PREALLOC_CNT;
9767b40dccSKalderon, Michal 
9867b40dccSKalderon, Michal 	dev->max_cq = dev->max_qp;
9967b40dccSKalderon, Michal 
10067b40dccSKalderon, Michal 	dev->max_qp_resp_rd_atomic_resc = QED_IWARP_IRD_DEFAULT;
10167b40dccSKalderon, Michal 	dev->max_qp_req_rd_atomic_resc = QED_IWARP_ORD_DEFAULT;
10267b40dccSKalderon, Michal }
10367b40dccSKalderon, Michal 
10467b40dccSKalderon, Michal void qed_iwarp_init_hw(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
10567b40dccSKalderon, Michal {
10667b40dccSKalderon, Michal 	p_hwfn->rdma_prs_search_reg = PRS_REG_SEARCH_TCP;
10767b40dccSKalderon, Michal 	qed_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 1);
10867b40dccSKalderon, Michal 	p_hwfn->b_rdma_enabled_in_prs = true;
10967b40dccSKalderon, Michal }
11067b40dccSKalderon, Michal 
111456a5849SKalderon, Michal /* We have two cid maps, one for tcp which should be used only from passive
112456a5849SKalderon, Michal  * syn processing and replacing a pre-allocated ep in the list. The second
113456a5849SKalderon, Michal  * for active tcp and for QPs.
114456a5849SKalderon, Michal  */
11567b40dccSKalderon, Michal static void qed_iwarp_cid_cleaned(struct qed_hwfn *p_hwfn, u32 cid)
11667b40dccSKalderon, Michal {
11767b40dccSKalderon, Michal 	cid -= qed_cxt_get_proto_cid_start(p_hwfn, p_hwfn->p_rdma_info->proto);
11867b40dccSKalderon, Michal 
11967b40dccSKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->lock);
120456a5849SKalderon, Michal 
121456a5849SKalderon, Michal 	if (cid < QED_IWARP_PREALLOC_CNT)
122456a5849SKalderon, Michal 		qed_bmap_release_id(p_hwfn, &p_hwfn->p_rdma_info->tcp_cid_map,
123456a5849SKalderon, Michal 				    cid);
124456a5849SKalderon, Michal 	else
12567b40dccSKalderon, Michal 		qed_bmap_release_id(p_hwfn, &p_hwfn->p_rdma_info->cid_map, cid);
126456a5849SKalderon, Michal 
12767b40dccSKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
12867b40dccSKalderon, Michal }
12967b40dccSKalderon, Michal 
130da090917STomer Tayar void
131da090917STomer Tayar qed_iwarp_init_fw_ramrod(struct qed_hwfn *p_hwfn,
132da090917STomer Tayar 			 struct iwarp_init_func_ramrod_data *p_ramrod)
133d1abfd0bSMichal Kalderon {
134da090917STomer Tayar 	p_ramrod->iwarp.ll2_ooo_q_index =
135da090917STomer Tayar 		RESC_START(p_hwfn, QED_LL2_QUEUE) +
136d1abfd0bSMichal Kalderon 		p_hwfn->p_rdma_info->iwarp.ll2_ooo_handle;
137da090917STomer Tayar 
138da090917STomer Tayar 	p_ramrod->tcp.max_fin_rt = QED_IWARP_MAX_FIN_RT_DEFAULT;
139da090917STomer Tayar 
140da090917STomer Tayar 	return;
141d1abfd0bSMichal Kalderon }
142d1abfd0bSMichal Kalderon 
14367b40dccSKalderon, Michal static int qed_iwarp_alloc_cid(struct qed_hwfn *p_hwfn, u32 *cid)
14467b40dccSKalderon, Michal {
14567b40dccSKalderon, Michal 	int rc;
14667b40dccSKalderon, Michal 
14767b40dccSKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->lock);
14867b40dccSKalderon, Michal 	rc = qed_rdma_bmap_alloc_id(p_hwfn, &p_hwfn->p_rdma_info->cid_map, cid);
14967b40dccSKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
15067b40dccSKalderon, Michal 	if (rc) {
15167b40dccSKalderon, Michal 		DP_NOTICE(p_hwfn, "Failed in allocating iwarp cid\n");
15267b40dccSKalderon, Michal 		return rc;
15367b40dccSKalderon, Michal 	}
15467b40dccSKalderon, Michal 	*cid += qed_cxt_get_proto_cid_start(p_hwfn, p_hwfn->p_rdma_info->proto);
15567b40dccSKalderon, Michal 
15667b40dccSKalderon, Michal 	rc = qed_cxt_dynamic_ilt_alloc(p_hwfn, QED_ELEM_CXT, *cid);
15767b40dccSKalderon, Michal 	if (rc)
15867b40dccSKalderon, Michal 		qed_iwarp_cid_cleaned(p_hwfn, *cid);
15967b40dccSKalderon, Michal 
16067b40dccSKalderon, Michal 	return rc;
16167b40dccSKalderon, Michal }
16267b40dccSKalderon, Michal 
163456a5849SKalderon, Michal static void qed_iwarp_set_tcp_cid(struct qed_hwfn *p_hwfn, u32 cid)
164456a5849SKalderon, Michal {
165456a5849SKalderon, Michal 	cid -= qed_cxt_get_proto_cid_start(p_hwfn, p_hwfn->p_rdma_info->proto);
166456a5849SKalderon, Michal 
167456a5849SKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->lock);
168456a5849SKalderon, Michal 	qed_bmap_set_id(p_hwfn, &p_hwfn->p_rdma_info->tcp_cid_map, cid);
169456a5849SKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
170456a5849SKalderon, Michal }
171456a5849SKalderon, Michal 
172456a5849SKalderon, Michal /* This function allocates a cid for passive tcp (called from syn receive)
173456a5849SKalderon, Michal  * the reason it's separate from the regular cid allocation is because it
174456a5849SKalderon, Michal  * is assured that these cids already have ilt allocated. They are preallocated
175456a5849SKalderon, Michal  * to ensure that we won't need to allocate memory during syn processing
176456a5849SKalderon, Michal  */
177456a5849SKalderon, Michal static int qed_iwarp_alloc_tcp_cid(struct qed_hwfn *p_hwfn, u32 *cid)
178456a5849SKalderon, Michal {
179456a5849SKalderon, Michal 	int rc;
180456a5849SKalderon, Michal 
181456a5849SKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->lock);
182456a5849SKalderon, Michal 
183456a5849SKalderon, Michal 	rc = qed_rdma_bmap_alloc_id(p_hwfn,
184456a5849SKalderon, Michal 				    &p_hwfn->p_rdma_info->tcp_cid_map, cid);
185456a5849SKalderon, Michal 
186456a5849SKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
187456a5849SKalderon, Michal 
188456a5849SKalderon, Michal 	if (rc) {
189456a5849SKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
190456a5849SKalderon, Michal 			   "can't allocate iwarp tcp cid max-count=%d\n",
191456a5849SKalderon, Michal 			   p_hwfn->p_rdma_info->tcp_cid_map.max_count);
192456a5849SKalderon, Michal 
193456a5849SKalderon, Michal 		*cid = QED_IWARP_INVALID_TCP_CID;
194456a5849SKalderon, Michal 		return rc;
195456a5849SKalderon, Michal 	}
196456a5849SKalderon, Michal 
197456a5849SKalderon, Michal 	*cid += qed_cxt_get_proto_cid_start(p_hwfn,
198456a5849SKalderon, Michal 					    p_hwfn->p_rdma_info->proto);
199456a5849SKalderon, Michal 	return 0;
200456a5849SKalderon, Michal }
201456a5849SKalderon, Michal 
20267b40dccSKalderon, Michal int qed_iwarp_create_qp(struct qed_hwfn *p_hwfn,
20367b40dccSKalderon, Michal 			struct qed_rdma_qp *qp,
20467b40dccSKalderon, Michal 			struct qed_rdma_create_qp_out_params *out_params)
20567b40dccSKalderon, Michal {
20667b40dccSKalderon, Michal 	struct iwarp_create_qp_ramrod_data *p_ramrod;
20767b40dccSKalderon, Michal 	struct qed_sp_init_data init_data;
20867b40dccSKalderon, Michal 	struct qed_spq_entry *p_ent;
20967b40dccSKalderon, Michal 	u16 physical_queue;
21067b40dccSKalderon, Michal 	u32 cid;
21167b40dccSKalderon, Michal 	int rc;
21267b40dccSKalderon, Michal 
21367b40dccSKalderon, Michal 	qp->shared_queue = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
21467b40dccSKalderon, Michal 					      IWARP_SHARED_QUEUE_PAGE_SIZE,
21567b40dccSKalderon, Michal 					      &qp->shared_queue_phys_addr,
21667b40dccSKalderon, Michal 					      GFP_KERNEL);
21767b40dccSKalderon, Michal 	if (!qp->shared_queue)
21867b40dccSKalderon, Michal 		return -ENOMEM;
21967b40dccSKalderon, Michal 
22067b40dccSKalderon, Michal 	out_params->sq_pbl_virt = (u8 *)qp->shared_queue +
22167b40dccSKalderon, Michal 	    IWARP_SHARED_QUEUE_PAGE_SQ_PBL_OFFSET;
22267b40dccSKalderon, Michal 	out_params->sq_pbl_phys = qp->shared_queue_phys_addr +
22367b40dccSKalderon, Michal 	    IWARP_SHARED_QUEUE_PAGE_SQ_PBL_OFFSET;
22467b40dccSKalderon, Michal 	out_params->rq_pbl_virt = (u8 *)qp->shared_queue +
22567b40dccSKalderon, Michal 	    IWARP_SHARED_QUEUE_PAGE_RQ_PBL_OFFSET;
22667b40dccSKalderon, Michal 	out_params->rq_pbl_phys = qp->shared_queue_phys_addr +
22767b40dccSKalderon, Michal 	    IWARP_SHARED_QUEUE_PAGE_RQ_PBL_OFFSET;
22867b40dccSKalderon, Michal 
22967b40dccSKalderon, Michal 	rc = qed_iwarp_alloc_cid(p_hwfn, &cid);
23067b40dccSKalderon, Michal 	if (rc)
23167b40dccSKalderon, Michal 		goto err1;
23267b40dccSKalderon, Michal 
23367b40dccSKalderon, Michal 	qp->icid = (u16)cid;
23467b40dccSKalderon, Michal 
23567b40dccSKalderon, Michal 	memset(&init_data, 0, sizeof(init_data));
23667b40dccSKalderon, Michal 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
23767b40dccSKalderon, Michal 	init_data.cid = qp->icid;
23867b40dccSKalderon, Michal 	init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
23967b40dccSKalderon, Michal 
24067b40dccSKalderon, Michal 	rc = qed_sp_init_request(p_hwfn, &p_ent,
24167b40dccSKalderon, Michal 				 IWARP_RAMROD_CMD_ID_CREATE_QP,
24267b40dccSKalderon, Michal 				 PROTOCOLID_IWARP, &init_data);
24367b40dccSKalderon, Michal 	if (rc)
24467b40dccSKalderon, Michal 		goto err2;
24567b40dccSKalderon, Michal 
24667b40dccSKalderon, Michal 	p_ramrod = &p_ent->ramrod.iwarp_create_qp;
24767b40dccSKalderon, Michal 
24867b40dccSKalderon, Michal 	SET_FIELD(p_ramrod->flags,
24967b40dccSKalderon, Michal 		  IWARP_CREATE_QP_RAMROD_DATA_FMR_AND_RESERVED_EN,
25067b40dccSKalderon, Michal 		  qp->fmr_and_reserved_lkey);
25167b40dccSKalderon, Michal 
25267b40dccSKalderon, Michal 	SET_FIELD(p_ramrod->flags,
25367b40dccSKalderon, Michal 		  IWARP_CREATE_QP_RAMROD_DATA_SIGNALED_COMP, qp->signal_all);
25467b40dccSKalderon, Michal 
25567b40dccSKalderon, Michal 	SET_FIELD(p_ramrod->flags,
25667b40dccSKalderon, Michal 		  IWARP_CREATE_QP_RAMROD_DATA_RDMA_RD_EN,
25767b40dccSKalderon, Michal 		  qp->incoming_rdma_read_en);
25867b40dccSKalderon, Michal 
25967b40dccSKalderon, Michal 	SET_FIELD(p_ramrod->flags,
26067b40dccSKalderon, Michal 		  IWARP_CREATE_QP_RAMROD_DATA_RDMA_WR_EN,
26167b40dccSKalderon, Michal 		  qp->incoming_rdma_write_en);
26267b40dccSKalderon, Michal 
26367b40dccSKalderon, Michal 	SET_FIELD(p_ramrod->flags,
26467b40dccSKalderon, Michal 		  IWARP_CREATE_QP_RAMROD_DATA_ATOMIC_EN,
26567b40dccSKalderon, Michal 		  qp->incoming_atomic_en);
26667b40dccSKalderon, Michal 
26767b40dccSKalderon, Michal 	SET_FIELD(p_ramrod->flags,
26867b40dccSKalderon, Michal 		  IWARP_CREATE_QP_RAMROD_DATA_SRQ_FLG, qp->use_srq);
26967b40dccSKalderon, Michal 
27067b40dccSKalderon, Michal 	p_ramrod->pd = qp->pd;
27167b40dccSKalderon, Michal 	p_ramrod->sq_num_pages = qp->sq_num_pages;
27267b40dccSKalderon, Michal 	p_ramrod->rq_num_pages = qp->rq_num_pages;
27367b40dccSKalderon, Michal 
274*39dbc646SYuval Bason 	p_ramrod->srq_id.srq_idx = cpu_to_le16(qp->srq_id);
275*39dbc646SYuval Bason 	p_ramrod->srq_id.opaque_fid = cpu_to_le16(p_hwfn->hw_info.opaque_fid);
27667b40dccSKalderon, Michal 	p_ramrod->qp_handle_for_cqe.hi = cpu_to_le32(qp->qp_handle.hi);
27767b40dccSKalderon, Michal 	p_ramrod->qp_handle_for_cqe.lo = cpu_to_le32(qp->qp_handle.lo);
27867b40dccSKalderon, Michal 
27967b40dccSKalderon, Michal 	p_ramrod->cq_cid_for_sq =
28067b40dccSKalderon, Michal 	    cpu_to_le32((p_hwfn->hw_info.opaque_fid << 16) | qp->sq_cq_id);
28167b40dccSKalderon, Michal 	p_ramrod->cq_cid_for_rq =
28267b40dccSKalderon, Michal 	    cpu_to_le32((p_hwfn->hw_info.opaque_fid << 16) | qp->rq_cq_id);
28367b40dccSKalderon, Michal 
28467b40dccSKalderon, Michal 	p_ramrod->dpi = cpu_to_le16(qp->dpi);
28567b40dccSKalderon, Michal 
28667b40dccSKalderon, Michal 	physical_queue = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
28767b40dccSKalderon, Michal 	p_ramrod->physical_q0 = cpu_to_le16(physical_queue);
28867b40dccSKalderon, Michal 	physical_queue = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_ACK);
28967b40dccSKalderon, Michal 	p_ramrod->physical_q1 = cpu_to_le16(physical_queue);
29067b40dccSKalderon, Michal 
29167b40dccSKalderon, Michal 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
29267b40dccSKalderon, Michal 	if (rc)
29367b40dccSKalderon, Michal 		goto err2;
29467b40dccSKalderon, Michal 
29567b40dccSKalderon, Michal 	return rc;
29667b40dccSKalderon, Michal 
29767b40dccSKalderon, Michal err2:
29867b40dccSKalderon, Michal 	qed_iwarp_cid_cleaned(p_hwfn, cid);
29967b40dccSKalderon, Michal err1:
30067b40dccSKalderon, Michal 	dma_free_coherent(&p_hwfn->cdev->pdev->dev,
30167b40dccSKalderon, Michal 			  IWARP_SHARED_QUEUE_PAGE_SIZE,
30267b40dccSKalderon, Michal 			  qp->shared_queue, qp->shared_queue_phys_addr);
30367b40dccSKalderon, Michal 
30467b40dccSKalderon, Michal 	return rc;
30567b40dccSKalderon, Michal }
30667b40dccSKalderon, Michal 
30767b40dccSKalderon, Michal static int qed_iwarp_modify_fw(struct qed_hwfn *p_hwfn, struct qed_rdma_qp *qp)
30867b40dccSKalderon, Michal {
30967b40dccSKalderon, Michal 	struct iwarp_modify_qp_ramrod_data *p_ramrod;
31067b40dccSKalderon, Michal 	struct qed_sp_init_data init_data;
31167b40dccSKalderon, Michal 	struct qed_spq_entry *p_ent;
31267b40dccSKalderon, Michal 	int rc;
31367b40dccSKalderon, Michal 
31467b40dccSKalderon, Michal 	/* Get SPQ entry */
31567b40dccSKalderon, Michal 	memset(&init_data, 0, sizeof(init_data));
31667b40dccSKalderon, Michal 	init_data.cid = qp->icid;
31767b40dccSKalderon, Michal 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
31867b40dccSKalderon, Michal 	init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
31967b40dccSKalderon, Michal 
32067b40dccSKalderon, Michal 	rc = qed_sp_init_request(p_hwfn, &p_ent,
32167b40dccSKalderon, Michal 				 IWARP_RAMROD_CMD_ID_MODIFY_QP,
32267b40dccSKalderon, Michal 				 p_hwfn->p_rdma_info->proto, &init_data);
32367b40dccSKalderon, Michal 	if (rc)
32467b40dccSKalderon, Michal 		return rc;
32567b40dccSKalderon, Michal 
32667b40dccSKalderon, Michal 	p_ramrod = &p_ent->ramrod.iwarp_modify_qp;
32767b40dccSKalderon, Michal 	SET_FIELD(p_ramrod->flags, IWARP_MODIFY_QP_RAMROD_DATA_STATE_TRANS_EN,
32867b40dccSKalderon, Michal 		  0x1);
32967b40dccSKalderon, Michal 	if (qp->iwarp_state == QED_IWARP_QP_STATE_CLOSING)
33067b40dccSKalderon, Michal 		p_ramrod->transition_to_state = IWARP_MODIFY_QP_STATE_CLOSING;
33167b40dccSKalderon, Michal 	else
33267b40dccSKalderon, Michal 		p_ramrod->transition_to_state = IWARP_MODIFY_QP_STATE_ERROR;
33367b40dccSKalderon, Michal 
33467b40dccSKalderon, Michal 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
33567b40dccSKalderon, Michal 
33667b40dccSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x)rc=%d\n", qp->icid, rc);
33767b40dccSKalderon, Michal 
33867b40dccSKalderon, Michal 	return rc;
33967b40dccSKalderon, Michal }
34067b40dccSKalderon, Michal 
34167b40dccSKalderon, Michal enum qed_iwarp_qp_state qed_roce2iwarp_state(enum qed_roce_qp_state state)
34267b40dccSKalderon, Michal {
34367b40dccSKalderon, Michal 	switch (state) {
34467b40dccSKalderon, Michal 	case QED_ROCE_QP_STATE_RESET:
34567b40dccSKalderon, Michal 	case QED_ROCE_QP_STATE_INIT:
34667b40dccSKalderon, Michal 	case QED_ROCE_QP_STATE_RTR:
34767b40dccSKalderon, Michal 		return QED_IWARP_QP_STATE_IDLE;
34867b40dccSKalderon, Michal 	case QED_ROCE_QP_STATE_RTS:
34967b40dccSKalderon, Michal 		return QED_IWARP_QP_STATE_RTS;
35067b40dccSKalderon, Michal 	case QED_ROCE_QP_STATE_SQD:
35167b40dccSKalderon, Michal 		return QED_IWARP_QP_STATE_CLOSING;
35267b40dccSKalderon, Michal 	case QED_ROCE_QP_STATE_ERR:
35367b40dccSKalderon, Michal 		return QED_IWARP_QP_STATE_ERROR;
35467b40dccSKalderon, Michal 	case QED_ROCE_QP_STATE_SQE:
35567b40dccSKalderon, Michal 		return QED_IWARP_QP_STATE_TERMINATE;
35667b40dccSKalderon, Michal 	default:
35767b40dccSKalderon, Michal 		return QED_IWARP_QP_STATE_ERROR;
35867b40dccSKalderon, Michal 	}
35967b40dccSKalderon, Michal }
36067b40dccSKalderon, Michal 
36167b40dccSKalderon, Michal static enum qed_roce_qp_state
36267b40dccSKalderon, Michal qed_iwarp2roce_state(enum qed_iwarp_qp_state state)
36367b40dccSKalderon, Michal {
36467b40dccSKalderon, Michal 	switch (state) {
36567b40dccSKalderon, Michal 	case QED_IWARP_QP_STATE_IDLE:
36667b40dccSKalderon, Michal 		return QED_ROCE_QP_STATE_INIT;
36767b40dccSKalderon, Michal 	case QED_IWARP_QP_STATE_RTS:
36867b40dccSKalderon, Michal 		return QED_ROCE_QP_STATE_RTS;
36967b40dccSKalderon, Michal 	case QED_IWARP_QP_STATE_TERMINATE:
37067b40dccSKalderon, Michal 		return QED_ROCE_QP_STATE_SQE;
37167b40dccSKalderon, Michal 	case QED_IWARP_QP_STATE_CLOSING:
37267b40dccSKalderon, Michal 		return QED_ROCE_QP_STATE_SQD;
37367b40dccSKalderon, Michal 	case QED_IWARP_QP_STATE_ERROR:
37467b40dccSKalderon, Michal 		return QED_ROCE_QP_STATE_ERR;
37567b40dccSKalderon, Michal 	default:
37667b40dccSKalderon, Michal 		return QED_ROCE_QP_STATE_ERR;
37767b40dccSKalderon, Michal 	}
37867b40dccSKalderon, Michal }
37967b40dccSKalderon, Michal 
38067b40dccSKalderon, Michal const char *iwarp_state_names[] = {
38167b40dccSKalderon, Michal 	"IDLE",
38267b40dccSKalderon, Michal 	"RTS",
38367b40dccSKalderon, Michal 	"TERMINATE",
38467b40dccSKalderon, Michal 	"CLOSING",
38567b40dccSKalderon, Michal 	"ERROR",
38667b40dccSKalderon, Michal };
38767b40dccSKalderon, Michal 
38867b40dccSKalderon, Michal int
38967b40dccSKalderon, Michal qed_iwarp_modify_qp(struct qed_hwfn *p_hwfn,
39067b40dccSKalderon, Michal 		    struct qed_rdma_qp *qp,
39167b40dccSKalderon, Michal 		    enum qed_iwarp_qp_state new_state, bool internal)
39267b40dccSKalderon, Michal {
39367b40dccSKalderon, Michal 	enum qed_iwarp_qp_state prev_iw_state;
39467b40dccSKalderon, Michal 	bool modify_fw = false;
39567b40dccSKalderon, Michal 	int rc = 0;
39667b40dccSKalderon, Michal 
39767b40dccSKalderon, Michal 	/* modify QP can be called from upper-layer or as a result of async
39867b40dccSKalderon, Michal 	 * RST/FIN... therefore need to protect
39967b40dccSKalderon, Michal 	 */
40067b40dccSKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.qp_lock);
40167b40dccSKalderon, Michal 	prev_iw_state = qp->iwarp_state;
40267b40dccSKalderon, Michal 
40367b40dccSKalderon, Michal 	if (prev_iw_state == new_state) {
40467b40dccSKalderon, Michal 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.qp_lock);
40567b40dccSKalderon, Michal 		return 0;
40667b40dccSKalderon, Michal 	}
40767b40dccSKalderon, Michal 
40867b40dccSKalderon, Michal 	switch (prev_iw_state) {
40967b40dccSKalderon, Michal 	case QED_IWARP_QP_STATE_IDLE:
41067b40dccSKalderon, Michal 		switch (new_state) {
41167b40dccSKalderon, Michal 		case QED_IWARP_QP_STATE_RTS:
41267b40dccSKalderon, Michal 			qp->iwarp_state = QED_IWARP_QP_STATE_RTS;
41367b40dccSKalderon, Michal 			break;
41467b40dccSKalderon, Michal 		case QED_IWARP_QP_STATE_ERROR:
41567b40dccSKalderon, Michal 			qp->iwarp_state = QED_IWARP_QP_STATE_ERROR;
41667b40dccSKalderon, Michal 			if (!internal)
41767b40dccSKalderon, Michal 				modify_fw = true;
41867b40dccSKalderon, Michal 			break;
41967b40dccSKalderon, Michal 		default:
42067b40dccSKalderon, Michal 			break;
42167b40dccSKalderon, Michal 		}
42267b40dccSKalderon, Michal 		break;
42367b40dccSKalderon, Michal 	case QED_IWARP_QP_STATE_RTS:
42467b40dccSKalderon, Michal 		switch (new_state) {
42567b40dccSKalderon, Michal 		case QED_IWARP_QP_STATE_CLOSING:
42667b40dccSKalderon, Michal 			if (!internal)
42767b40dccSKalderon, Michal 				modify_fw = true;
42867b40dccSKalderon, Michal 
42967b40dccSKalderon, Michal 			qp->iwarp_state = QED_IWARP_QP_STATE_CLOSING;
43067b40dccSKalderon, Michal 			break;
43167b40dccSKalderon, Michal 		case QED_IWARP_QP_STATE_ERROR:
43267b40dccSKalderon, Michal 			if (!internal)
43367b40dccSKalderon, Michal 				modify_fw = true;
43467b40dccSKalderon, Michal 			qp->iwarp_state = QED_IWARP_QP_STATE_ERROR;
43567b40dccSKalderon, Michal 			break;
43667b40dccSKalderon, Michal 		default:
43767b40dccSKalderon, Michal 			break;
43867b40dccSKalderon, Michal 		}
43967b40dccSKalderon, Michal 		break;
44067b40dccSKalderon, Michal 	case QED_IWARP_QP_STATE_ERROR:
44167b40dccSKalderon, Michal 		switch (new_state) {
44267b40dccSKalderon, Michal 		case QED_IWARP_QP_STATE_IDLE:
44367b40dccSKalderon, Michal 
44467b40dccSKalderon, Michal 			qp->iwarp_state = new_state;
44567b40dccSKalderon, Michal 			break;
44667b40dccSKalderon, Michal 		case QED_IWARP_QP_STATE_CLOSING:
44767b40dccSKalderon, Michal 			/* could happen due to race... do nothing.... */
44867b40dccSKalderon, Michal 			break;
44967b40dccSKalderon, Michal 		default:
45067b40dccSKalderon, Michal 			rc = -EINVAL;
45167b40dccSKalderon, Michal 		}
45267b40dccSKalderon, Michal 		break;
45367b40dccSKalderon, Michal 	case QED_IWARP_QP_STATE_TERMINATE:
45467b40dccSKalderon, Michal 	case QED_IWARP_QP_STATE_CLOSING:
45567b40dccSKalderon, Michal 		qp->iwarp_state = new_state;
45667b40dccSKalderon, Michal 		break;
45767b40dccSKalderon, Michal 	default:
45867b40dccSKalderon, Michal 		break;
45967b40dccSKalderon, Michal 	}
46067b40dccSKalderon, Michal 
46167b40dccSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) %s --> %s%s\n",
46267b40dccSKalderon, Michal 		   qp->icid,
46367b40dccSKalderon, Michal 		   iwarp_state_names[prev_iw_state],
46467b40dccSKalderon, Michal 		   iwarp_state_names[qp->iwarp_state],
46567b40dccSKalderon, Michal 		   internal ? "internal" : "");
46667b40dccSKalderon, Michal 
46767b40dccSKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.qp_lock);
46867b40dccSKalderon, Michal 
46967b40dccSKalderon, Michal 	if (modify_fw)
47067b40dccSKalderon, Michal 		rc = qed_iwarp_modify_fw(p_hwfn, qp);
47167b40dccSKalderon, Michal 
47267b40dccSKalderon, Michal 	return rc;
47367b40dccSKalderon, Michal }
47467b40dccSKalderon, Michal 
47567b40dccSKalderon, Michal int qed_iwarp_fw_destroy(struct qed_hwfn *p_hwfn, struct qed_rdma_qp *qp)
47667b40dccSKalderon, Michal {
47767b40dccSKalderon, Michal 	struct qed_sp_init_data init_data;
47867b40dccSKalderon, Michal 	struct qed_spq_entry *p_ent;
47967b40dccSKalderon, Michal 	int rc;
48067b40dccSKalderon, Michal 
48167b40dccSKalderon, Michal 	/* Get SPQ entry */
48267b40dccSKalderon, Michal 	memset(&init_data, 0, sizeof(init_data));
48367b40dccSKalderon, Michal 	init_data.cid = qp->icid;
48467b40dccSKalderon, Michal 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
48567b40dccSKalderon, Michal 	init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
48667b40dccSKalderon, Michal 
48767b40dccSKalderon, Michal 	rc = qed_sp_init_request(p_hwfn, &p_ent,
48867b40dccSKalderon, Michal 				 IWARP_RAMROD_CMD_ID_DESTROY_QP,
48967b40dccSKalderon, Michal 				 p_hwfn->p_rdma_info->proto, &init_data);
49067b40dccSKalderon, Michal 	if (rc)
49167b40dccSKalderon, Michal 		return rc;
49267b40dccSKalderon, Michal 
49367b40dccSKalderon, Michal 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
49467b40dccSKalderon, Michal 
49567b40dccSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) rc = %d\n", qp->icid, rc);
49667b40dccSKalderon, Michal 
49767b40dccSKalderon, Michal 	return rc;
49867b40dccSKalderon, Michal }
49967b40dccSKalderon, Michal 
500456a5849SKalderon, Michal static void qed_iwarp_destroy_ep(struct qed_hwfn *p_hwfn,
501456a5849SKalderon, Michal 				 struct qed_iwarp_ep *ep,
502456a5849SKalderon, Michal 				 bool remove_from_active_list)
503456a5849SKalderon, Michal {
504456a5849SKalderon, Michal 	dma_free_coherent(&p_hwfn->cdev->pdev->dev,
505456a5849SKalderon, Michal 			  sizeof(*ep->ep_buffer_virt),
506456a5849SKalderon, Michal 			  ep->ep_buffer_virt, ep->ep_buffer_phys);
507456a5849SKalderon, Michal 
508456a5849SKalderon, Michal 	if (remove_from_active_list) {
509456a5849SKalderon, Michal 		spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
510456a5849SKalderon, Michal 		list_del(&ep->list_entry);
511456a5849SKalderon, Michal 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
512456a5849SKalderon, Michal 	}
513456a5849SKalderon, Michal 
514456a5849SKalderon, Michal 	if (ep->qp)
515456a5849SKalderon, Michal 		ep->qp->ep = NULL;
516456a5849SKalderon, Michal 
517456a5849SKalderon, Michal 	kfree(ep);
518456a5849SKalderon, Michal }
519456a5849SKalderon, Michal 
52067b40dccSKalderon, Michal int qed_iwarp_destroy_qp(struct qed_hwfn *p_hwfn, struct qed_rdma_qp *qp)
52167b40dccSKalderon, Michal {
522fc4c6065SKalderon, Michal 	struct qed_iwarp_ep *ep = qp->ep;
523fc4c6065SKalderon, Michal 	int wait_count = 0;
52467b40dccSKalderon, Michal 	int rc = 0;
52567b40dccSKalderon, Michal 
52667b40dccSKalderon, Michal 	if (qp->iwarp_state != QED_IWARP_QP_STATE_ERROR) {
52767b40dccSKalderon, Michal 		rc = qed_iwarp_modify_qp(p_hwfn, qp,
52867b40dccSKalderon, Michal 					 QED_IWARP_QP_STATE_ERROR, false);
52967b40dccSKalderon, Michal 		if (rc)
53067b40dccSKalderon, Michal 			return rc;
53167b40dccSKalderon, Michal 	}
53267b40dccSKalderon, Michal 
533fc4c6065SKalderon, Michal 	/* Make sure ep is closed before returning and freeing memory. */
534fc4c6065SKalderon, Michal 	if (ep) {
535fc4c6065SKalderon, Michal 		while (ep->state != QED_IWARP_EP_CLOSED && wait_count++ < 200)
536fc4c6065SKalderon, Michal 			msleep(100);
537fc4c6065SKalderon, Michal 
538fc4c6065SKalderon, Michal 		if (ep->state != QED_IWARP_EP_CLOSED)
539fc4c6065SKalderon, Michal 			DP_NOTICE(p_hwfn, "ep state close timeout state=%x\n",
540fc4c6065SKalderon, Michal 				  ep->state);
541fc4c6065SKalderon, Michal 
542fc4c6065SKalderon, Michal 		qed_iwarp_destroy_ep(p_hwfn, ep, false);
543fc4c6065SKalderon, Michal 	}
544fc4c6065SKalderon, Michal 
54567b40dccSKalderon, Michal 	rc = qed_iwarp_fw_destroy(p_hwfn, qp);
54667b40dccSKalderon, Michal 
54767b40dccSKalderon, Michal 	if (qp->shared_queue)
54867b40dccSKalderon, Michal 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
54967b40dccSKalderon, Michal 				  IWARP_SHARED_QUEUE_PAGE_SIZE,
55067b40dccSKalderon, Michal 				  qp->shared_queue, qp->shared_queue_phys_addr);
55167b40dccSKalderon, Michal 
55267b40dccSKalderon, Michal 	return rc;
55367b40dccSKalderon, Michal }
55467b40dccSKalderon, Michal 
555456a5849SKalderon, Michal static int
556456a5849SKalderon, Michal qed_iwarp_create_ep(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep **ep_out)
557456a5849SKalderon, Michal {
558456a5849SKalderon, Michal 	struct qed_iwarp_ep *ep;
559456a5849SKalderon, Michal 	int rc;
560456a5849SKalderon, Michal 
561456a5849SKalderon, Michal 	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
562456a5849SKalderon, Michal 	if (!ep)
563456a5849SKalderon, Michal 		return -ENOMEM;
564456a5849SKalderon, Michal 
565456a5849SKalderon, Michal 	ep->state = QED_IWARP_EP_INIT;
566456a5849SKalderon, Michal 
567456a5849SKalderon, Michal 	ep->ep_buffer_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
568456a5849SKalderon, Michal 						sizeof(*ep->ep_buffer_virt),
569456a5849SKalderon, Michal 						&ep->ep_buffer_phys,
570456a5849SKalderon, Michal 						GFP_KERNEL);
571456a5849SKalderon, Michal 	if (!ep->ep_buffer_virt) {
572456a5849SKalderon, Michal 		rc = -ENOMEM;
573456a5849SKalderon, Michal 		goto err;
574456a5849SKalderon, Michal 	}
575456a5849SKalderon, Michal 
576456a5849SKalderon, Michal 	ep->sig = QED_EP_SIG;
577456a5849SKalderon, Michal 
578456a5849SKalderon, Michal 	*ep_out = ep;
579456a5849SKalderon, Michal 
580456a5849SKalderon, Michal 	return 0;
581456a5849SKalderon, Michal 
582456a5849SKalderon, Michal err:
583456a5849SKalderon, Michal 	kfree(ep);
584456a5849SKalderon, Michal 	return rc;
585456a5849SKalderon, Michal }
586456a5849SKalderon, Michal 
587456a5849SKalderon, Michal static void
588456a5849SKalderon, Michal qed_iwarp_print_tcp_ramrod(struct qed_hwfn *p_hwfn,
589456a5849SKalderon, Michal 			   struct iwarp_tcp_offload_ramrod_data *p_tcp_ramrod)
590456a5849SKalderon, Michal {
591456a5849SKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "local_mac=%x %x %x, remote_mac=%x %x %x\n",
592456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.local_mac_addr_lo,
593456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.local_mac_addr_mid,
594456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.local_mac_addr_hi,
595456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.remote_mac_addr_lo,
596456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.remote_mac_addr_mid,
597456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.remote_mac_addr_hi);
598456a5849SKalderon, Michal 
599456a5849SKalderon, Michal 	if (p_tcp_ramrod->tcp.ip_version == TCP_IPV4) {
600456a5849SKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
60191d1ae47SKalderon, Michal 			   "local_ip=%pI4h:%x, remote_ip=%pI4h:%x, vlan=%x\n",
602456a5849SKalderon, Michal 			   p_tcp_ramrod->tcp.local_ip,
603456a5849SKalderon, Michal 			   p_tcp_ramrod->tcp.local_port,
604456a5849SKalderon, Michal 			   p_tcp_ramrod->tcp.remote_ip,
605456a5849SKalderon, Michal 			   p_tcp_ramrod->tcp.remote_port,
606456a5849SKalderon, Michal 			   p_tcp_ramrod->tcp.vlan_id);
607456a5849SKalderon, Michal 	} else {
608456a5849SKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
60991d1ae47SKalderon, Michal 			   "local_ip=%pI6:%x, remote_ip=%pI6:%x, vlan=%x\n",
610456a5849SKalderon, Michal 			   p_tcp_ramrod->tcp.local_ip,
611456a5849SKalderon, Michal 			   p_tcp_ramrod->tcp.local_port,
612456a5849SKalderon, Michal 			   p_tcp_ramrod->tcp.remote_ip,
613456a5849SKalderon, Michal 			   p_tcp_ramrod->tcp.remote_port,
614456a5849SKalderon, Michal 			   p_tcp_ramrod->tcp.vlan_id);
615456a5849SKalderon, Michal 	}
616456a5849SKalderon, Michal 
617456a5849SKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
618456a5849SKalderon, Michal 		   "flow_label=%x, ttl=%x, tos_or_tc=%x, mss=%x, rcv_wnd_scale=%x, connect_mode=%x, flags=%x\n",
619456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.flow_label,
620456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.ttl,
621456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.tos_or_tc,
622456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.mss,
623456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.rcv_wnd_scale,
624456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.connect_mode,
625456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.flags);
626456a5849SKalderon, Michal 
627456a5849SKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "syn_ip_payload_length=%x, lo=%x, hi=%x\n",
628456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.syn_ip_payload_length,
629456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.syn_phy_addr_lo,
630456a5849SKalderon, Michal 		   p_tcp_ramrod->tcp.syn_phy_addr_hi);
631456a5849SKalderon, Michal }
632456a5849SKalderon, Michal 
633456a5849SKalderon, Michal static int
634456a5849SKalderon, Michal qed_iwarp_tcp_offload(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
635456a5849SKalderon, Michal {
636456a5849SKalderon, Michal 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
637456a5849SKalderon, Michal 	struct iwarp_tcp_offload_ramrod_data *p_tcp_ramrod;
638456a5849SKalderon, Michal 	struct tcp_offload_params_opt2 *tcp;
639456a5849SKalderon, Michal 	struct qed_sp_init_data init_data;
640456a5849SKalderon, Michal 	struct qed_spq_entry *p_ent;
641456a5849SKalderon, Michal 	dma_addr_t async_output_phys;
642456a5849SKalderon, Michal 	dma_addr_t in_pdata_phys;
643456a5849SKalderon, Michal 	u16 physical_q;
644456a5849SKalderon, Michal 	u8 tcp_flags;
645456a5849SKalderon, Michal 	int rc;
646456a5849SKalderon, Michal 	int i;
647456a5849SKalderon, Michal 
648456a5849SKalderon, Michal 	memset(&init_data, 0, sizeof(init_data));
649456a5849SKalderon, Michal 	init_data.cid = ep->tcp_cid;
650456a5849SKalderon, Michal 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
6514b0fdd7cSKalderon, Michal 	if (ep->connect_mode == TCP_CONNECT_PASSIVE)
652456a5849SKalderon, Michal 		init_data.comp_mode = QED_SPQ_MODE_CB;
6534b0fdd7cSKalderon, Michal 	else
6544b0fdd7cSKalderon, Michal 		init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
655456a5849SKalderon, Michal 
656456a5849SKalderon, Michal 	rc = qed_sp_init_request(p_hwfn, &p_ent,
657456a5849SKalderon, Michal 				 IWARP_RAMROD_CMD_ID_TCP_OFFLOAD,
658456a5849SKalderon, Michal 				 PROTOCOLID_IWARP, &init_data);
659456a5849SKalderon, Michal 	if (rc)
660456a5849SKalderon, Michal 		return rc;
661456a5849SKalderon, Michal 
662456a5849SKalderon, Michal 	p_tcp_ramrod = &p_ent->ramrod.iwarp_tcp_offload;
663456a5849SKalderon, Michal 
664456a5849SKalderon, Michal 	in_pdata_phys = ep->ep_buffer_phys +
665456a5849SKalderon, Michal 			offsetof(struct qed_iwarp_ep_memory, in_pdata);
666456a5849SKalderon, Michal 	DMA_REGPAIR_LE(p_tcp_ramrod->iwarp.incoming_ulp_buffer.addr,
667456a5849SKalderon, Michal 		       in_pdata_phys);
668456a5849SKalderon, Michal 
669456a5849SKalderon, Michal 	p_tcp_ramrod->iwarp.incoming_ulp_buffer.len =
670456a5849SKalderon, Michal 	    cpu_to_le16(sizeof(ep->ep_buffer_virt->in_pdata));
671456a5849SKalderon, Michal 
672456a5849SKalderon, Michal 	async_output_phys = ep->ep_buffer_phys +
673456a5849SKalderon, Michal 			    offsetof(struct qed_iwarp_ep_memory, async_output);
674456a5849SKalderon, Michal 	DMA_REGPAIR_LE(p_tcp_ramrod->iwarp.async_eqe_output_buf,
675456a5849SKalderon, Michal 		       async_output_phys);
676456a5849SKalderon, Michal 
677456a5849SKalderon, Michal 	p_tcp_ramrod->iwarp.handle_for_async.hi = cpu_to_le32(PTR_HI(ep));
678456a5849SKalderon, Michal 	p_tcp_ramrod->iwarp.handle_for_async.lo = cpu_to_le32(PTR_LO(ep));
679456a5849SKalderon, Michal 
680456a5849SKalderon, Michal 	physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
681456a5849SKalderon, Michal 	p_tcp_ramrod->iwarp.physical_q0 = cpu_to_le16(physical_q);
682456a5849SKalderon, Michal 	physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_ACK);
683456a5849SKalderon, Michal 	p_tcp_ramrod->iwarp.physical_q1 = cpu_to_le16(physical_q);
684456a5849SKalderon, Michal 	p_tcp_ramrod->iwarp.mpa_mode = iwarp_info->mpa_rev;
685456a5849SKalderon, Michal 
686456a5849SKalderon, Michal 	tcp = &p_tcp_ramrod->tcp;
687456a5849SKalderon, Michal 	qed_set_fw_mac_addr(&tcp->remote_mac_addr_hi,
688456a5849SKalderon, Michal 			    &tcp->remote_mac_addr_mid,
689456a5849SKalderon, Michal 			    &tcp->remote_mac_addr_lo, ep->remote_mac_addr);
690456a5849SKalderon, Michal 	qed_set_fw_mac_addr(&tcp->local_mac_addr_hi, &tcp->local_mac_addr_mid,
691456a5849SKalderon, Michal 			    &tcp->local_mac_addr_lo, ep->local_mac_addr);
692456a5849SKalderon, Michal 
693456a5849SKalderon, Michal 	tcp->vlan_id = cpu_to_le16(ep->cm_info.vlan);
694456a5849SKalderon, Michal 
695456a5849SKalderon, Michal 	tcp_flags = p_hwfn->p_rdma_info->iwarp.tcp_flags;
696456a5849SKalderon, Michal 	tcp->flags = 0;
697456a5849SKalderon, Michal 	SET_FIELD(tcp->flags, TCP_OFFLOAD_PARAMS_OPT2_TS_EN,
698456a5849SKalderon, Michal 		  !!(tcp_flags & QED_IWARP_TS_EN));
699456a5849SKalderon, Michal 
700456a5849SKalderon, Michal 	SET_FIELD(tcp->flags, TCP_OFFLOAD_PARAMS_OPT2_DA_EN,
701456a5849SKalderon, Michal 		  !!(tcp_flags & QED_IWARP_DA_EN));
702456a5849SKalderon, Michal 
703456a5849SKalderon, Michal 	tcp->ip_version = ep->cm_info.ip_version;
704456a5849SKalderon, Michal 
705456a5849SKalderon, Michal 	for (i = 0; i < 4; i++) {
706456a5849SKalderon, Michal 		tcp->remote_ip[i] = cpu_to_le32(ep->cm_info.remote_ip[i]);
707456a5849SKalderon, Michal 		tcp->local_ip[i] = cpu_to_le32(ep->cm_info.local_ip[i]);
708456a5849SKalderon, Michal 	}
709456a5849SKalderon, Michal 
710456a5849SKalderon, Michal 	tcp->remote_port = cpu_to_le16(ep->cm_info.remote_port);
711456a5849SKalderon, Michal 	tcp->local_port = cpu_to_le16(ep->cm_info.local_port);
712456a5849SKalderon, Michal 	tcp->mss = cpu_to_le16(ep->mss);
713456a5849SKalderon, Michal 	tcp->flow_label = 0;
714456a5849SKalderon, Michal 	tcp->ttl = 0x40;
715456a5849SKalderon, Michal 	tcp->tos_or_tc = 0;
716456a5849SKalderon, Michal 
717da090917STomer Tayar 	tcp->max_rt_time = QED_IWARP_DEF_MAX_RT_TIME;
718da090917STomer Tayar 	tcp->cwnd = QED_IWARP_DEF_CWND_FACTOR *  tcp->mss;
719da090917STomer Tayar 	tcp->ka_max_probe_cnt = QED_IWARP_DEF_KA_MAX_PROBE_CNT;
720da090917STomer Tayar 	tcp->ka_timeout = QED_IWARP_DEF_KA_TIMEOUT;
721da090917STomer Tayar 	tcp->ka_interval = QED_IWARP_DEF_KA_INTERVAL;
722da090917STomer Tayar 
723456a5849SKalderon, Michal 	tcp->rcv_wnd_scale = (u8)p_hwfn->p_rdma_info->iwarp.rcv_wnd_scale;
724456a5849SKalderon, Michal 	tcp->connect_mode = ep->connect_mode;
725456a5849SKalderon, Michal 
726456a5849SKalderon, Michal 	if (ep->connect_mode == TCP_CONNECT_PASSIVE) {
727456a5849SKalderon, Michal 		tcp->syn_ip_payload_length =
728456a5849SKalderon, Michal 			cpu_to_le16(ep->syn_ip_payload_length);
729456a5849SKalderon, Michal 		tcp->syn_phy_addr_hi = DMA_HI_LE(ep->syn_phy_addr);
730456a5849SKalderon, Michal 		tcp->syn_phy_addr_lo = DMA_LO_LE(ep->syn_phy_addr);
731456a5849SKalderon, Michal 	}
732456a5849SKalderon, Michal 
733456a5849SKalderon, Michal 	qed_iwarp_print_tcp_ramrod(p_hwfn, p_tcp_ramrod);
734456a5849SKalderon, Michal 
735456a5849SKalderon, Michal 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
736456a5849SKalderon, Michal 
737456a5849SKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
738456a5849SKalderon, Michal 		   "EP(0x%x) Offload completed rc=%d\n", ep->tcp_cid, rc);
739456a5849SKalderon, Michal 
740456a5849SKalderon, Michal 	return rc;
741456a5849SKalderon, Michal }
742456a5849SKalderon, Michal 
743456a5849SKalderon, Michal static void
744456a5849SKalderon, Michal qed_iwarp_mpa_received(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
745456a5849SKalderon, Michal {
746456a5849SKalderon, Michal 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
747456a5849SKalderon, Michal 	struct qed_iwarp_cm_event_params params;
748456a5849SKalderon, Michal 	struct mpa_v2_hdr *mpa_v2;
749456a5849SKalderon, Michal 	union async_output *async_data;
750456a5849SKalderon, Michal 	u16 mpa_ord, mpa_ird;
751456a5849SKalderon, Michal 	u8 mpa_hdr_size = 0;
752456a5849SKalderon, Michal 	u8 mpa_rev;
753456a5849SKalderon, Michal 
754456a5849SKalderon, Michal 	async_data = &ep->ep_buffer_virt->async_output;
755456a5849SKalderon, Michal 
756456a5849SKalderon, Michal 	mpa_rev = async_data->mpa_request.mpa_handshake_mode;
757456a5849SKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
758456a5849SKalderon, Michal 		   "private_data_len=%x handshake_mode=%x private_data=(%x)\n",
759456a5849SKalderon, Michal 		   async_data->mpa_request.ulp_data_len,
7604b0fdd7cSKalderon, Michal 		   mpa_rev, *((u32 *)(ep->ep_buffer_virt->in_pdata)));
761456a5849SKalderon, Michal 
762456a5849SKalderon, Michal 	if (mpa_rev == MPA_NEGOTIATION_TYPE_ENHANCED) {
763456a5849SKalderon, Michal 		/* Read ord/ird values from private data buffer */
764456a5849SKalderon, Michal 		mpa_v2 = (struct mpa_v2_hdr *)ep->ep_buffer_virt->in_pdata;
765456a5849SKalderon, Michal 		mpa_hdr_size = sizeof(*mpa_v2);
766456a5849SKalderon, Michal 
767456a5849SKalderon, Michal 		mpa_ord = ntohs(mpa_v2->ord);
768456a5849SKalderon, Michal 		mpa_ird = ntohs(mpa_v2->ird);
769456a5849SKalderon, Michal 
770456a5849SKalderon, Michal 		/* Temprary store in cm_info incoming ord/ird requested, later
771456a5849SKalderon, Michal 		 * replace with negotiated value during accept
772456a5849SKalderon, Michal 		 */
773456a5849SKalderon, Michal 		ep->cm_info.ord = (u8)min_t(u16,
774456a5849SKalderon, Michal 					    (mpa_ord & MPA_V2_IRD_ORD_MASK),
775456a5849SKalderon, Michal 					    QED_IWARP_ORD_DEFAULT);
776456a5849SKalderon, Michal 
777456a5849SKalderon, Michal 		ep->cm_info.ird = (u8)min_t(u16,
778456a5849SKalderon, Michal 					    (mpa_ird & MPA_V2_IRD_ORD_MASK),
779456a5849SKalderon, Michal 					    QED_IWARP_IRD_DEFAULT);
780456a5849SKalderon, Michal 
781456a5849SKalderon, Michal 		/* Peer2Peer negotiation */
782456a5849SKalderon, Michal 		ep->rtr_type = MPA_RTR_TYPE_NONE;
783456a5849SKalderon, Michal 		if (mpa_ird & MPA_V2_PEER2PEER_MODEL) {
784456a5849SKalderon, Michal 			if (mpa_ord & MPA_V2_WRITE_RTR)
785456a5849SKalderon, Michal 				ep->rtr_type |= MPA_RTR_TYPE_ZERO_WRITE;
786456a5849SKalderon, Michal 
787456a5849SKalderon, Michal 			if (mpa_ord & MPA_V2_READ_RTR)
788456a5849SKalderon, Michal 				ep->rtr_type |= MPA_RTR_TYPE_ZERO_READ;
789456a5849SKalderon, Michal 
790456a5849SKalderon, Michal 			if (mpa_ird & MPA_V2_SEND_RTR)
791456a5849SKalderon, Michal 				ep->rtr_type |= MPA_RTR_TYPE_ZERO_SEND;
792456a5849SKalderon, Michal 
793456a5849SKalderon, Michal 			ep->rtr_type &= iwarp_info->rtr_type;
794456a5849SKalderon, Michal 
795456a5849SKalderon, Michal 			/* if we're left with no match send our capabilities */
796456a5849SKalderon, Michal 			if (ep->rtr_type == MPA_RTR_TYPE_NONE)
797456a5849SKalderon, Michal 				ep->rtr_type = iwarp_info->rtr_type;
798456a5849SKalderon, Michal 		}
799456a5849SKalderon, Michal 
800456a5849SKalderon, Michal 		ep->mpa_rev = MPA_NEGOTIATION_TYPE_ENHANCED;
801456a5849SKalderon, Michal 	} else {
802456a5849SKalderon, Michal 		ep->cm_info.ord = QED_IWARP_ORD_DEFAULT;
803456a5849SKalderon, Michal 		ep->cm_info.ird = QED_IWARP_IRD_DEFAULT;
804456a5849SKalderon, Michal 		ep->mpa_rev = MPA_NEGOTIATION_TYPE_BASIC;
805456a5849SKalderon, Michal 	}
806456a5849SKalderon, Michal 
807456a5849SKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
808456a5849SKalderon, Michal 		   "MPA_NEGOTIATE (v%d): ORD: 0x%x IRD: 0x%x rtr:0x%x ulp_data_len = %x mpa_hdr_size = %x\n",
809456a5849SKalderon, Michal 		   mpa_rev, ep->cm_info.ord, ep->cm_info.ird, ep->rtr_type,
810456a5849SKalderon, Michal 		   async_data->mpa_request.ulp_data_len, mpa_hdr_size);
811456a5849SKalderon, Michal 
812456a5849SKalderon, Michal 	/* Strip mpa v2 hdr from private data before sending to upper layer */
813456a5849SKalderon, Michal 	ep->cm_info.private_data = ep->ep_buffer_virt->in_pdata + mpa_hdr_size;
814456a5849SKalderon, Michal 
815456a5849SKalderon, Michal 	ep->cm_info.private_data_len = async_data->mpa_request.ulp_data_len -
816456a5849SKalderon, Michal 				       mpa_hdr_size;
817456a5849SKalderon, Michal 
818456a5849SKalderon, Michal 	params.event = QED_IWARP_EVENT_MPA_REQUEST;
819456a5849SKalderon, Michal 	params.cm_info = &ep->cm_info;
820456a5849SKalderon, Michal 	params.ep_context = ep;
821456a5849SKalderon, Michal 	params.status = 0;
822456a5849SKalderon, Michal 
823456a5849SKalderon, Michal 	ep->state = QED_IWARP_EP_MPA_REQ_RCVD;
824456a5849SKalderon, Michal 	ep->event_cb(ep->cb_context, &params);
825456a5849SKalderon, Michal }
826456a5849SKalderon, Michal 
827456a5849SKalderon, Michal static int
828456a5849SKalderon, Michal qed_iwarp_mpa_offload(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
829456a5849SKalderon, Michal {
830456a5849SKalderon, Michal 	struct iwarp_mpa_offload_ramrod_data *p_mpa_ramrod;
831da090917STomer Tayar 	struct qed_iwarp_info *iwarp_info;
832456a5849SKalderon, Michal 	struct qed_sp_init_data init_data;
833456a5849SKalderon, Michal 	dma_addr_t async_output_phys;
834456a5849SKalderon, Michal 	struct qed_spq_entry *p_ent;
835456a5849SKalderon, Michal 	dma_addr_t out_pdata_phys;
836456a5849SKalderon, Michal 	dma_addr_t in_pdata_phys;
837456a5849SKalderon, Michal 	struct qed_rdma_qp *qp;
838456a5849SKalderon, Michal 	bool reject;
839456a5849SKalderon, Michal 	int rc;
840456a5849SKalderon, Michal 
841456a5849SKalderon, Michal 	if (!ep)
842456a5849SKalderon, Michal 		return -EINVAL;
843456a5849SKalderon, Michal 
844456a5849SKalderon, Michal 	qp = ep->qp;
845456a5849SKalderon, Michal 	reject = !qp;
846456a5849SKalderon, Michal 
847456a5849SKalderon, Michal 	memset(&init_data, 0, sizeof(init_data));
848456a5849SKalderon, Michal 	init_data.cid = reject ? ep->tcp_cid : qp->icid;
849456a5849SKalderon, Michal 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
850456a5849SKalderon, Michal 
8514b0fdd7cSKalderon, Michal 	if (ep->connect_mode == TCP_CONNECT_ACTIVE)
8524b0fdd7cSKalderon, Michal 		init_data.comp_mode = QED_SPQ_MODE_CB;
8534b0fdd7cSKalderon, Michal 	else
854456a5849SKalderon, Michal 		init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
855456a5849SKalderon, Michal 
856456a5849SKalderon, Michal 	rc = qed_sp_init_request(p_hwfn, &p_ent,
857456a5849SKalderon, Michal 				 IWARP_RAMROD_CMD_ID_MPA_OFFLOAD,
858456a5849SKalderon, Michal 				 PROTOCOLID_IWARP, &init_data);
859456a5849SKalderon, Michal 	if (rc)
860456a5849SKalderon, Michal 		return rc;
861456a5849SKalderon, Michal 
862456a5849SKalderon, Michal 	p_mpa_ramrod = &p_ent->ramrod.iwarp_mpa_offload;
863456a5849SKalderon, Michal 	out_pdata_phys = ep->ep_buffer_phys +
864456a5849SKalderon, Michal 			 offsetof(struct qed_iwarp_ep_memory, out_pdata);
865456a5849SKalderon, Michal 	DMA_REGPAIR_LE(p_mpa_ramrod->common.outgoing_ulp_buffer.addr,
866456a5849SKalderon, Michal 		       out_pdata_phys);
867456a5849SKalderon, Michal 	p_mpa_ramrod->common.outgoing_ulp_buffer.len =
868456a5849SKalderon, Michal 	    ep->cm_info.private_data_len;
869456a5849SKalderon, Michal 	p_mpa_ramrod->common.crc_needed = p_hwfn->p_rdma_info->iwarp.crc_needed;
870456a5849SKalderon, Michal 
871456a5849SKalderon, Michal 	p_mpa_ramrod->common.out_rq.ord = ep->cm_info.ord;
872456a5849SKalderon, Michal 	p_mpa_ramrod->common.out_rq.ird = ep->cm_info.ird;
873456a5849SKalderon, Michal 
874456a5849SKalderon, Michal 	p_mpa_ramrod->tcp_cid = p_hwfn->hw_info.opaque_fid << 16 | ep->tcp_cid;
875456a5849SKalderon, Michal 
876456a5849SKalderon, Michal 	in_pdata_phys = ep->ep_buffer_phys +
877456a5849SKalderon, Michal 			offsetof(struct qed_iwarp_ep_memory, in_pdata);
878456a5849SKalderon, Michal 	p_mpa_ramrod->tcp_connect_side = ep->connect_mode;
879456a5849SKalderon, Michal 	DMA_REGPAIR_LE(p_mpa_ramrod->incoming_ulp_buffer.addr,
880456a5849SKalderon, Michal 		       in_pdata_phys);
881456a5849SKalderon, Michal 	p_mpa_ramrod->incoming_ulp_buffer.len =
882456a5849SKalderon, Michal 	    cpu_to_le16(sizeof(ep->ep_buffer_virt->in_pdata));
883456a5849SKalderon, Michal 	async_output_phys = ep->ep_buffer_phys +
884456a5849SKalderon, Michal 			    offsetof(struct qed_iwarp_ep_memory, async_output);
885456a5849SKalderon, Michal 	DMA_REGPAIR_LE(p_mpa_ramrod->async_eqe_output_buf,
886456a5849SKalderon, Michal 		       async_output_phys);
887456a5849SKalderon, Michal 	p_mpa_ramrod->handle_for_async.hi = cpu_to_le32(PTR_HI(ep));
888456a5849SKalderon, Michal 	p_mpa_ramrod->handle_for_async.lo = cpu_to_le32(PTR_LO(ep));
889456a5849SKalderon, Michal 
890456a5849SKalderon, Michal 	if (!reject) {
891456a5849SKalderon, Michal 		DMA_REGPAIR_LE(p_mpa_ramrod->shared_queue_addr,
892456a5849SKalderon, Michal 			       qp->shared_queue_phys_addr);
893456a5849SKalderon, Michal 		p_mpa_ramrod->stats_counter_id =
894456a5849SKalderon, Michal 		    RESC_START(p_hwfn, QED_RDMA_STATS_QUEUE) + qp->stats_queue;
895456a5849SKalderon, Michal 	} else {
896456a5849SKalderon, Michal 		p_mpa_ramrod->common.reject = 1;
897456a5849SKalderon, Michal 	}
898456a5849SKalderon, Michal 
899da090917STomer Tayar 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
900da090917STomer Tayar 	p_mpa_ramrod->rcv_wnd = iwarp_info->rcv_wnd_size;
901456a5849SKalderon, Michal 	p_mpa_ramrod->mode = ep->mpa_rev;
902456a5849SKalderon, Michal 	SET_FIELD(p_mpa_ramrod->rtr_pref,
903456a5849SKalderon, Michal 		  IWARP_MPA_OFFLOAD_RAMROD_DATA_RTR_SUPPORTED, ep->rtr_type);
904456a5849SKalderon, Michal 
905456a5849SKalderon, Michal 	ep->state = QED_IWARP_EP_MPA_OFFLOADED;
906456a5849SKalderon, Michal 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
907456a5849SKalderon, Michal 	if (!reject)
908456a5849SKalderon, Michal 		ep->cid = qp->icid;	/* Now they're migrated. */
909456a5849SKalderon, Michal 
910456a5849SKalderon, Michal 	DP_VERBOSE(p_hwfn,
911456a5849SKalderon, Michal 		   QED_MSG_RDMA,
912456a5849SKalderon, Michal 		   "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",
913456a5849SKalderon, Michal 		   reject ? 0xffff : qp->icid,
914456a5849SKalderon, Michal 		   ep->tcp_cid,
915456a5849SKalderon, Michal 		   rc,
916456a5849SKalderon, Michal 		   ep->cm_info.ird,
917456a5849SKalderon, Michal 		   ep->cm_info.ord, ep->rtr_type, ep->mpa_rev, reject);
918456a5849SKalderon, Michal 	return rc;
919456a5849SKalderon, Michal }
920456a5849SKalderon, Michal 
921456a5849SKalderon, Michal static void
922456a5849SKalderon, Michal qed_iwarp_return_ep(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
923456a5849SKalderon, Michal {
924456a5849SKalderon, Michal 	ep->state = QED_IWARP_EP_INIT;
925456a5849SKalderon, Michal 	if (ep->qp)
926456a5849SKalderon, Michal 		ep->qp->ep = NULL;
927456a5849SKalderon, Michal 	ep->qp = NULL;
928456a5849SKalderon, Michal 	memset(&ep->cm_info, 0, sizeof(ep->cm_info));
929456a5849SKalderon, Michal 
930456a5849SKalderon, Michal 	if (ep->tcp_cid == QED_IWARP_INVALID_TCP_CID) {
931456a5849SKalderon, Michal 		/* We don't care about the return code, it's ok if tcp_cid
932456a5849SKalderon, Michal 		 * remains invalid...in this case we'll defer allocation
933456a5849SKalderon, Michal 		 */
934456a5849SKalderon, Michal 		qed_iwarp_alloc_tcp_cid(p_hwfn, &ep->tcp_cid);
935456a5849SKalderon, Michal 	}
936456a5849SKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
937456a5849SKalderon, Michal 
938456a5849SKalderon, Michal 	list_del(&ep->list_entry);
939456a5849SKalderon, Michal 	list_add_tail(&ep->list_entry,
940456a5849SKalderon, Michal 		      &p_hwfn->p_rdma_info->iwarp.ep_free_list);
941456a5849SKalderon, Michal 
942456a5849SKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
943456a5849SKalderon, Michal }
944456a5849SKalderon, Michal 
9454b0fdd7cSKalderon, Michal void
9464b0fdd7cSKalderon, Michal qed_iwarp_parse_private_data(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
9474b0fdd7cSKalderon, Michal {
9484b0fdd7cSKalderon, Michal 	struct mpa_v2_hdr *mpa_v2_params;
9494b0fdd7cSKalderon, Michal 	union async_output *async_data;
9504b0fdd7cSKalderon, Michal 	u16 mpa_ird, mpa_ord;
9514b0fdd7cSKalderon, Michal 	u8 mpa_data_size = 0;
9524b0fdd7cSKalderon, Michal 
9534b0fdd7cSKalderon, Michal 	if (MPA_REV2(p_hwfn->p_rdma_info->iwarp.mpa_rev)) {
9544b0fdd7cSKalderon, Michal 		mpa_v2_params =
9554b0fdd7cSKalderon, Michal 			(struct mpa_v2_hdr *)(ep->ep_buffer_virt->in_pdata);
9564b0fdd7cSKalderon, Michal 		mpa_data_size = sizeof(*mpa_v2_params);
9574b0fdd7cSKalderon, Michal 		mpa_ird = ntohs(mpa_v2_params->ird);
9584b0fdd7cSKalderon, Michal 		mpa_ord = ntohs(mpa_v2_params->ord);
9594b0fdd7cSKalderon, Michal 
9604b0fdd7cSKalderon, Michal 		ep->cm_info.ird = (u8)(mpa_ord & MPA_V2_IRD_ORD_MASK);
9614b0fdd7cSKalderon, Michal 		ep->cm_info.ord = (u8)(mpa_ird & MPA_V2_IRD_ORD_MASK);
9624b0fdd7cSKalderon, Michal 	}
9634b0fdd7cSKalderon, Michal 	async_data = &ep->ep_buffer_virt->async_output;
9644b0fdd7cSKalderon, Michal 
9654b0fdd7cSKalderon, Michal 	ep->cm_info.private_data = ep->ep_buffer_virt->in_pdata + mpa_data_size;
9664b0fdd7cSKalderon, Michal 	ep->cm_info.private_data_len = async_data->mpa_response.ulp_data_len -
9674b0fdd7cSKalderon, Michal 				       mpa_data_size;
9684b0fdd7cSKalderon, Michal }
9694b0fdd7cSKalderon, Michal 
9704b0fdd7cSKalderon, Michal void
9714b0fdd7cSKalderon, Michal qed_iwarp_mpa_reply_arrived(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
9724b0fdd7cSKalderon, Michal {
9734b0fdd7cSKalderon, Michal 	struct qed_iwarp_cm_event_params params;
9744b0fdd7cSKalderon, Michal 
9754b0fdd7cSKalderon, Michal 	if (ep->connect_mode == TCP_CONNECT_PASSIVE) {
9764b0fdd7cSKalderon, Michal 		DP_NOTICE(p_hwfn,
9774b0fdd7cSKalderon, Michal 			  "MPA reply event not expected on passive side!\n");
9784b0fdd7cSKalderon, Michal 		return;
9794b0fdd7cSKalderon, Michal 	}
9804b0fdd7cSKalderon, Michal 
9814b0fdd7cSKalderon, Michal 	params.event = QED_IWARP_EVENT_ACTIVE_MPA_REPLY;
9824b0fdd7cSKalderon, Michal 
9834b0fdd7cSKalderon, Michal 	qed_iwarp_parse_private_data(p_hwfn, ep);
9844b0fdd7cSKalderon, Michal 
9854b0fdd7cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
9864b0fdd7cSKalderon, Michal 		   "MPA_NEGOTIATE (v%d): ORD: 0x%x IRD: 0x%x\n",
9874b0fdd7cSKalderon, Michal 		   ep->mpa_rev, ep->cm_info.ord, ep->cm_info.ird);
9884b0fdd7cSKalderon, Michal 
9894b0fdd7cSKalderon, Michal 	params.cm_info = &ep->cm_info;
9904b0fdd7cSKalderon, Michal 	params.ep_context = ep;
9914b0fdd7cSKalderon, Michal 	params.status = 0;
9924b0fdd7cSKalderon, Michal 
9934b0fdd7cSKalderon, Michal 	ep->mpa_reply_processed = true;
9944b0fdd7cSKalderon, Michal 
9954b0fdd7cSKalderon, Michal 	ep->event_cb(ep->cb_context, &params);
9964b0fdd7cSKalderon, Michal }
9974b0fdd7cSKalderon, Michal 
998456a5849SKalderon, Michal #define QED_IWARP_CONNECT_MODE_STRING(ep) \
999456a5849SKalderon, Michal 	((ep)->connect_mode == TCP_CONNECT_PASSIVE) ? "Passive" : "Active"
1000456a5849SKalderon, Michal 
1001456a5849SKalderon, Michal /* Called as a result of the event:
1002456a5849SKalderon, Michal  * IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_COMPLETE
1003456a5849SKalderon, Michal  */
1004456a5849SKalderon, Michal static void
1005456a5849SKalderon, Michal qed_iwarp_mpa_complete(struct qed_hwfn *p_hwfn,
1006456a5849SKalderon, Michal 		       struct qed_iwarp_ep *ep, u8 fw_return_code)
1007456a5849SKalderon, Michal {
1008456a5849SKalderon, Michal 	struct qed_iwarp_cm_event_params params;
1009456a5849SKalderon, Michal 
10104b0fdd7cSKalderon, Michal 	if (ep->connect_mode == TCP_CONNECT_ACTIVE)
10114b0fdd7cSKalderon, Michal 		params.event = QED_IWARP_EVENT_ACTIVE_COMPLETE;
10124b0fdd7cSKalderon, Michal 	else
1013456a5849SKalderon, Michal 		params.event = QED_IWARP_EVENT_PASSIVE_COMPLETE;
1014456a5849SKalderon, Michal 
10154b0fdd7cSKalderon, Michal 	if (ep->connect_mode == TCP_CONNECT_ACTIVE && !ep->mpa_reply_processed)
10164b0fdd7cSKalderon, Michal 		qed_iwarp_parse_private_data(p_hwfn, ep);
10174b0fdd7cSKalderon, Michal 
1018456a5849SKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1019456a5849SKalderon, Michal 		   "MPA_NEGOTIATE (v%d): ORD: 0x%x IRD: 0x%x\n",
1020456a5849SKalderon, Michal 		   ep->mpa_rev, ep->cm_info.ord, ep->cm_info.ird);
1021456a5849SKalderon, Michal 
1022456a5849SKalderon, Michal 	params.cm_info = &ep->cm_info;
1023456a5849SKalderon, Michal 
1024456a5849SKalderon, Michal 	params.ep_context = ep;
1025456a5849SKalderon, Michal 
1026456a5849SKalderon, Michal 	ep->state = QED_IWARP_EP_CLOSED;
1027456a5849SKalderon, Michal 
1028456a5849SKalderon, Michal 	switch (fw_return_code) {
1029456a5849SKalderon, Michal 	case RDMA_RETURN_OK:
1030456a5849SKalderon, Michal 		ep->qp->max_rd_atomic_req = ep->cm_info.ord;
1031456a5849SKalderon, Michal 		ep->qp->max_rd_atomic_resp = ep->cm_info.ird;
1032456a5849SKalderon, Michal 		qed_iwarp_modify_qp(p_hwfn, ep->qp, QED_IWARP_QP_STATE_RTS, 1);
1033456a5849SKalderon, Michal 		ep->state = QED_IWARP_EP_ESTABLISHED;
1034456a5849SKalderon, Michal 		params.status = 0;
1035456a5849SKalderon, Michal 		break;
10369816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_TIMEOUT:
10379816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA timeout\n",
10389816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
10399816b614SKalderon, Michal 		params.status = -EBUSY;
10409816b614SKalderon, Michal 		break;
10419816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_ERROR_REJECT:
10429816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA Reject\n",
10439816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
10449816b614SKalderon, Michal 		params.status = -ECONNREFUSED;
10459816b614SKalderon, Michal 		break;
10469816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_RST:
10479816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA reset(tcp cid: 0x%x)\n",
10489816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid,
10499816b614SKalderon, Michal 			  ep->tcp_cid);
10509816b614SKalderon, Michal 		params.status = -ECONNRESET;
10519816b614SKalderon, Michal 		break;
10529816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_FIN:
10539816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA received FIN\n",
10549816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
10559816b614SKalderon, Michal 		params.status = -ECONNREFUSED;
10569816b614SKalderon, Michal 		break;
10579816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_INSUF_IRD:
10589816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA insufficient ird\n",
10599816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
10609816b614SKalderon, Michal 		params.status = -ECONNREFUSED;
10619816b614SKalderon, Michal 		break;
10629816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_RTR_MISMATCH:
10639816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA RTR MISMATCH\n",
10649816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
10659816b614SKalderon, Michal 		params.status = -ECONNREFUSED;
10669816b614SKalderon, Michal 		break;
10679816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_INVALID_PACKET:
10689816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA Invalid Packet\n",
10699816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
10709816b614SKalderon, Michal 		params.status = -ECONNREFUSED;
10719816b614SKalderon, Michal 		break;
10729816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_LOCAL_ERROR:
10739816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA Local Error\n",
10749816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
10759816b614SKalderon, Michal 		params.status = -ECONNREFUSED;
10769816b614SKalderon, Michal 		break;
10779816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_TERMINATE:
10789816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA TERMINATE\n",
10799816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->cid);
10809816b614SKalderon, Michal 		params.status = -ECONNREFUSED;
10819816b614SKalderon, Michal 		break;
1082456a5849SKalderon, Michal 	default:
1083456a5849SKalderon, Michal 		params.status = -ECONNRESET;
1084456a5849SKalderon, Michal 		break;
1085456a5849SKalderon, Michal 	}
1086456a5849SKalderon, Michal 
1087456a5849SKalderon, Michal 	ep->event_cb(ep->cb_context, &params);
10889816b614SKalderon, Michal 
10899816b614SKalderon, Michal 	/* on passive side, if there is no associated QP (REJECT) we need to
10909816b614SKalderon, Michal 	 * return the ep to the pool, (in the regular case we add an element
10919816b614SKalderon, Michal 	 * in accept instead of this one.
10929816b614SKalderon, Michal 	 * In both cases we need to remove it from the ep_list.
10939816b614SKalderon, Michal 	 */
10949816b614SKalderon, Michal 	if (fw_return_code != RDMA_RETURN_OK) {
10959816b614SKalderon, Michal 		ep->tcp_cid = QED_IWARP_INVALID_TCP_CID;
10969816b614SKalderon, Michal 		if ((ep->connect_mode == TCP_CONNECT_PASSIVE) &&
10979816b614SKalderon, Michal 		    (!ep->qp)) {	/* Rejected */
10989816b614SKalderon, Michal 			qed_iwarp_return_ep(p_hwfn, ep);
10999816b614SKalderon, Michal 		} else {
11009816b614SKalderon, Michal 			spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
11019816b614SKalderon, Michal 			list_del(&ep->list_entry);
11029816b614SKalderon, Michal 			spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
11039816b614SKalderon, Michal 		}
11049816b614SKalderon, Michal 	}
1105456a5849SKalderon, Michal }
1106456a5849SKalderon, Michal 
1107456a5849SKalderon, Michal static void
1108456a5849SKalderon, Michal qed_iwarp_mpa_v2_set_private(struct qed_hwfn *p_hwfn,
1109456a5849SKalderon, Michal 			     struct qed_iwarp_ep *ep, u8 *mpa_data_size)
1110456a5849SKalderon, Michal {
1111456a5849SKalderon, Michal 	struct mpa_v2_hdr *mpa_v2_params;
1112456a5849SKalderon, Michal 	u16 mpa_ird, mpa_ord;
1113456a5849SKalderon, Michal 
1114456a5849SKalderon, Michal 	*mpa_data_size = 0;
1115456a5849SKalderon, Michal 	if (MPA_REV2(ep->mpa_rev)) {
1116456a5849SKalderon, Michal 		mpa_v2_params =
1117456a5849SKalderon, Michal 		    (struct mpa_v2_hdr *)ep->ep_buffer_virt->out_pdata;
1118456a5849SKalderon, Michal 		*mpa_data_size = sizeof(*mpa_v2_params);
1119456a5849SKalderon, Michal 
1120456a5849SKalderon, Michal 		mpa_ird = (u16)ep->cm_info.ird;
1121456a5849SKalderon, Michal 		mpa_ord = (u16)ep->cm_info.ord;
1122456a5849SKalderon, Michal 
1123456a5849SKalderon, Michal 		if (ep->rtr_type != MPA_RTR_TYPE_NONE) {
1124456a5849SKalderon, Michal 			mpa_ird |= MPA_V2_PEER2PEER_MODEL;
1125456a5849SKalderon, Michal 
1126456a5849SKalderon, Michal 			if (ep->rtr_type & MPA_RTR_TYPE_ZERO_SEND)
1127456a5849SKalderon, Michal 				mpa_ird |= MPA_V2_SEND_RTR;
1128456a5849SKalderon, Michal 
1129456a5849SKalderon, Michal 			if (ep->rtr_type & MPA_RTR_TYPE_ZERO_WRITE)
1130456a5849SKalderon, Michal 				mpa_ord |= MPA_V2_WRITE_RTR;
1131456a5849SKalderon, Michal 
1132456a5849SKalderon, Michal 			if (ep->rtr_type & MPA_RTR_TYPE_ZERO_READ)
1133456a5849SKalderon, Michal 				mpa_ord |= MPA_V2_READ_RTR;
1134456a5849SKalderon, Michal 		}
1135456a5849SKalderon, Michal 
1136456a5849SKalderon, Michal 		mpa_v2_params->ird = htons(mpa_ird);
1137456a5849SKalderon, Michal 		mpa_v2_params->ord = htons(mpa_ord);
1138456a5849SKalderon, Michal 
1139456a5849SKalderon, Michal 		DP_VERBOSE(p_hwfn,
1140456a5849SKalderon, Michal 			   QED_MSG_RDMA,
1141456a5849SKalderon, Michal 			   "MPA_NEGOTIATE Header: [%x ord:%x ird] %x ord:%x ird:%x peer2peer:%x rtr_send:%x rtr_write:%x rtr_read:%x\n",
1142456a5849SKalderon, Michal 			   mpa_v2_params->ird,
1143456a5849SKalderon, Michal 			   mpa_v2_params->ord,
1144456a5849SKalderon, Michal 			   *((u32 *)mpa_v2_params),
1145456a5849SKalderon, Michal 			   mpa_ord & MPA_V2_IRD_ORD_MASK,
1146456a5849SKalderon, Michal 			   mpa_ird & MPA_V2_IRD_ORD_MASK,
1147456a5849SKalderon, Michal 			   !!(mpa_ird & MPA_V2_PEER2PEER_MODEL),
1148456a5849SKalderon, Michal 			   !!(mpa_ird & MPA_V2_SEND_RTR),
1149456a5849SKalderon, Michal 			   !!(mpa_ord & MPA_V2_WRITE_RTR),
1150456a5849SKalderon, Michal 			   !!(mpa_ord & MPA_V2_READ_RTR));
1151456a5849SKalderon, Michal 	}
1152456a5849SKalderon, Michal }
1153456a5849SKalderon, Michal 
11544b0fdd7cSKalderon, Michal int qed_iwarp_connect(void *rdma_cxt,
11554b0fdd7cSKalderon, Michal 		      struct qed_iwarp_connect_in *iparams,
11564b0fdd7cSKalderon, Michal 		      struct qed_iwarp_connect_out *oparams)
11574b0fdd7cSKalderon, Michal {
11584b0fdd7cSKalderon, Michal 	struct qed_hwfn *p_hwfn = rdma_cxt;
11594b0fdd7cSKalderon, Michal 	struct qed_iwarp_info *iwarp_info;
11604b0fdd7cSKalderon, Michal 	struct qed_iwarp_ep *ep;
11614b0fdd7cSKalderon, Michal 	u8 mpa_data_size = 0;
11624b0fdd7cSKalderon, Michal 	u8 ts_hdr_size = 0;
11634b0fdd7cSKalderon, Michal 	u32 cid;
11644b0fdd7cSKalderon, Michal 	int rc;
11654b0fdd7cSKalderon, Michal 
11664b0fdd7cSKalderon, Michal 	if ((iparams->cm_info.ord > QED_IWARP_ORD_DEFAULT) ||
11674b0fdd7cSKalderon, Michal 	    (iparams->cm_info.ird > QED_IWARP_IRD_DEFAULT)) {
11684b0fdd7cSKalderon, Michal 		DP_NOTICE(p_hwfn,
11694b0fdd7cSKalderon, Michal 			  "QP(0x%x) ERROR: Invalid ord(0x%x)/ird(0x%x)\n",
11704b0fdd7cSKalderon, Michal 			  iparams->qp->icid, iparams->cm_info.ord,
11714b0fdd7cSKalderon, Michal 			  iparams->cm_info.ird);
11724b0fdd7cSKalderon, Michal 
11734b0fdd7cSKalderon, Michal 		return -EINVAL;
11744b0fdd7cSKalderon, Michal 	}
11754b0fdd7cSKalderon, Michal 
11764b0fdd7cSKalderon, Michal 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
11774b0fdd7cSKalderon, Michal 
11784b0fdd7cSKalderon, Michal 	/* Allocate ep object */
11794b0fdd7cSKalderon, Michal 	rc = qed_iwarp_alloc_cid(p_hwfn, &cid);
11804b0fdd7cSKalderon, Michal 	if (rc)
11814b0fdd7cSKalderon, Michal 		return rc;
11824b0fdd7cSKalderon, Michal 
11834b0fdd7cSKalderon, Michal 	rc = qed_iwarp_create_ep(p_hwfn, &ep);
11844b0fdd7cSKalderon, Michal 	if (rc)
11854b0fdd7cSKalderon, Michal 		goto err;
11864b0fdd7cSKalderon, Michal 
11874b0fdd7cSKalderon, Michal 	ep->tcp_cid = cid;
11884b0fdd7cSKalderon, Michal 
11894b0fdd7cSKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
11904b0fdd7cSKalderon, Michal 	list_add_tail(&ep->list_entry, &p_hwfn->p_rdma_info->iwarp.ep_list);
11914b0fdd7cSKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
11924b0fdd7cSKalderon, Michal 
11934b0fdd7cSKalderon, Michal 	ep->qp = iparams->qp;
11944b0fdd7cSKalderon, Michal 	ep->qp->ep = ep;
11954b0fdd7cSKalderon, Michal 	ether_addr_copy(ep->remote_mac_addr, iparams->remote_mac_addr);
11964b0fdd7cSKalderon, Michal 	ether_addr_copy(ep->local_mac_addr, iparams->local_mac_addr);
11974b0fdd7cSKalderon, Michal 	memcpy(&ep->cm_info, &iparams->cm_info, sizeof(ep->cm_info));
11984b0fdd7cSKalderon, Michal 
11994b0fdd7cSKalderon, Michal 	ep->cm_info.ord = iparams->cm_info.ord;
12004b0fdd7cSKalderon, Michal 	ep->cm_info.ird = iparams->cm_info.ird;
12014b0fdd7cSKalderon, Michal 
12024b0fdd7cSKalderon, Michal 	ep->rtr_type = iwarp_info->rtr_type;
12034b0fdd7cSKalderon, Michal 	if (!iwarp_info->peer2peer)
12044b0fdd7cSKalderon, Michal 		ep->rtr_type = MPA_RTR_TYPE_NONE;
12054b0fdd7cSKalderon, Michal 
12064b0fdd7cSKalderon, Michal 	if ((ep->rtr_type & MPA_RTR_TYPE_ZERO_READ) && (ep->cm_info.ord == 0))
12074b0fdd7cSKalderon, Michal 		ep->cm_info.ord = 1;
12084b0fdd7cSKalderon, Michal 
12094b0fdd7cSKalderon, Michal 	ep->mpa_rev = iwarp_info->mpa_rev;
12104b0fdd7cSKalderon, Michal 
12114b0fdd7cSKalderon, Michal 	qed_iwarp_mpa_v2_set_private(p_hwfn, ep, &mpa_data_size);
12124b0fdd7cSKalderon, Michal 
12134b0fdd7cSKalderon, Michal 	ep->cm_info.private_data = ep->ep_buffer_virt->out_pdata;
12144b0fdd7cSKalderon, Michal 	ep->cm_info.private_data_len = iparams->cm_info.private_data_len +
12154b0fdd7cSKalderon, Michal 				       mpa_data_size;
12164b0fdd7cSKalderon, Michal 
12174b0fdd7cSKalderon, Michal 	memcpy((u8 *)ep->ep_buffer_virt->out_pdata + mpa_data_size,
12184b0fdd7cSKalderon, Michal 	       iparams->cm_info.private_data,
12194b0fdd7cSKalderon, Michal 	       iparams->cm_info.private_data_len);
12204b0fdd7cSKalderon, Michal 
12214b0fdd7cSKalderon, Michal 	if (p_hwfn->p_rdma_info->iwarp.tcp_flags & QED_IWARP_TS_EN)
12224b0fdd7cSKalderon, Michal 		ts_hdr_size = TIMESTAMP_HEADER_SIZE;
12234b0fdd7cSKalderon, Michal 
12244b0fdd7cSKalderon, Michal 	ep->mss = iparams->mss - ts_hdr_size;
12254b0fdd7cSKalderon, Michal 	ep->mss = min_t(u16, QED_IWARP_MAX_FW_MSS, ep->mss);
12264b0fdd7cSKalderon, Michal 
12274b0fdd7cSKalderon, Michal 	ep->event_cb = iparams->event_cb;
12284b0fdd7cSKalderon, Michal 	ep->cb_context = iparams->cb_context;
12294b0fdd7cSKalderon, Michal 	ep->connect_mode = TCP_CONNECT_ACTIVE;
12304b0fdd7cSKalderon, Michal 
12314b0fdd7cSKalderon, Michal 	oparams->ep_context = ep;
12324b0fdd7cSKalderon, Michal 
12334b0fdd7cSKalderon, Michal 	rc = qed_iwarp_tcp_offload(p_hwfn, ep);
12344b0fdd7cSKalderon, Michal 
12354b0fdd7cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) EP(0x%x) rc = %d\n",
12364b0fdd7cSKalderon, Michal 		   iparams->qp->icid, ep->tcp_cid, rc);
12374b0fdd7cSKalderon, Michal 
12384b0fdd7cSKalderon, Michal 	if (rc) {
12394b0fdd7cSKalderon, Michal 		qed_iwarp_destroy_ep(p_hwfn, ep, true);
12404b0fdd7cSKalderon, Michal 		goto err;
12414b0fdd7cSKalderon, Michal 	}
12424b0fdd7cSKalderon, Michal 
12434b0fdd7cSKalderon, Michal 	return rc;
12444b0fdd7cSKalderon, Michal err:
12454b0fdd7cSKalderon, Michal 	qed_iwarp_cid_cleaned(p_hwfn, cid);
12464b0fdd7cSKalderon, Michal 
12474b0fdd7cSKalderon, Michal 	return rc;
12484b0fdd7cSKalderon, Michal }
12494b0fdd7cSKalderon, Michal 
1250456a5849SKalderon, Michal static struct qed_iwarp_ep *qed_iwarp_get_free_ep(struct qed_hwfn *p_hwfn)
1251456a5849SKalderon, Michal {
1252456a5849SKalderon, Michal 	struct qed_iwarp_ep *ep = NULL;
1253456a5849SKalderon, Michal 	int rc;
1254456a5849SKalderon, Michal 
1255456a5849SKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1256456a5849SKalderon, Michal 
1257456a5849SKalderon, Michal 	if (list_empty(&p_hwfn->p_rdma_info->iwarp.ep_free_list)) {
1258456a5849SKalderon, Michal 		DP_ERR(p_hwfn, "Ep list is empty\n");
1259456a5849SKalderon, Michal 		goto out;
1260456a5849SKalderon, Michal 	}
1261456a5849SKalderon, Michal 
1262456a5849SKalderon, Michal 	ep = list_first_entry(&p_hwfn->p_rdma_info->iwarp.ep_free_list,
1263456a5849SKalderon, Michal 			      struct qed_iwarp_ep, list_entry);
1264456a5849SKalderon, Michal 
1265456a5849SKalderon, Michal 	/* in some cases we could have failed allocating a tcp cid when added
1266456a5849SKalderon, Michal 	 * from accept / failure... retry now..this is not the common case.
1267456a5849SKalderon, Michal 	 */
1268456a5849SKalderon, Michal 	if (ep->tcp_cid == QED_IWARP_INVALID_TCP_CID) {
1269456a5849SKalderon, Michal 		rc = qed_iwarp_alloc_tcp_cid(p_hwfn, &ep->tcp_cid);
1270456a5849SKalderon, Michal 
1271456a5849SKalderon, Michal 		/* if we fail we could look for another entry with a valid
1272456a5849SKalderon, Michal 		 * tcp_cid, but since we don't expect to reach this anyway
1273456a5849SKalderon, Michal 		 * it's not worth the handling
1274456a5849SKalderon, Michal 		 */
1275456a5849SKalderon, Michal 		if (rc) {
1276456a5849SKalderon, Michal 			ep->tcp_cid = QED_IWARP_INVALID_TCP_CID;
1277456a5849SKalderon, Michal 			ep = NULL;
1278456a5849SKalderon, Michal 			goto out;
1279456a5849SKalderon, Michal 		}
1280456a5849SKalderon, Michal 	}
1281456a5849SKalderon, Michal 
1282456a5849SKalderon, Michal 	list_del(&ep->list_entry);
1283456a5849SKalderon, Michal 
1284456a5849SKalderon, Michal out:
1285456a5849SKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1286456a5849SKalderon, Michal 	return ep;
1287456a5849SKalderon, Michal }
1288456a5849SKalderon, Michal 
128967b40dccSKalderon, Michal #define QED_IWARP_MAX_CID_CLEAN_TIME  100
129067b40dccSKalderon, Michal #define QED_IWARP_MAX_NO_PROGRESS_CNT 5
129167b40dccSKalderon, Michal 
129267b40dccSKalderon, Michal /* This function waits for all the bits of a bmap to be cleared, as long as
129367b40dccSKalderon, Michal  * there is progress ( i.e. the number of bits left to be cleared decreases )
129467b40dccSKalderon, Michal  * the function continues.
129567b40dccSKalderon, Michal  */
129667b40dccSKalderon, Michal static int
129767b40dccSKalderon, Michal qed_iwarp_wait_cid_map_cleared(struct qed_hwfn *p_hwfn, struct qed_bmap *bmap)
129867b40dccSKalderon, Michal {
129967b40dccSKalderon, Michal 	int prev_weight = 0;
130067b40dccSKalderon, Michal 	int wait_count = 0;
130167b40dccSKalderon, Michal 	int weight = 0;
130267b40dccSKalderon, Michal 
130367b40dccSKalderon, Michal 	weight = bitmap_weight(bmap->bitmap, bmap->max_count);
130467b40dccSKalderon, Michal 	prev_weight = weight;
130567b40dccSKalderon, Michal 
130667b40dccSKalderon, Michal 	while (weight) {
130767b40dccSKalderon, Michal 		msleep(QED_IWARP_MAX_CID_CLEAN_TIME);
130867b40dccSKalderon, Michal 
130967b40dccSKalderon, Michal 		weight = bitmap_weight(bmap->bitmap, bmap->max_count);
131067b40dccSKalderon, Michal 
131167b40dccSKalderon, Michal 		if (prev_weight == weight) {
131267b40dccSKalderon, Michal 			wait_count++;
131367b40dccSKalderon, Michal 		} else {
131467b40dccSKalderon, Michal 			prev_weight = weight;
131567b40dccSKalderon, Michal 			wait_count = 0;
131667b40dccSKalderon, Michal 		}
131767b40dccSKalderon, Michal 
131867b40dccSKalderon, Michal 		if (wait_count > QED_IWARP_MAX_NO_PROGRESS_CNT) {
131967b40dccSKalderon, Michal 			DP_NOTICE(p_hwfn,
132067b40dccSKalderon, Michal 				  "%s bitmap wait timed out (%d cids pending)\n",
132167b40dccSKalderon, Michal 				  bmap->name, weight);
132267b40dccSKalderon, Michal 			return -EBUSY;
132367b40dccSKalderon, Michal 		}
132467b40dccSKalderon, Michal 	}
132567b40dccSKalderon, Michal 	return 0;
132667b40dccSKalderon, Michal }
132767b40dccSKalderon, Michal 
132867b40dccSKalderon, Michal static int qed_iwarp_wait_for_all_cids(struct qed_hwfn *p_hwfn)
132967b40dccSKalderon, Michal {
1330456a5849SKalderon, Michal 	int rc;
1331456a5849SKalderon, Michal 	int i;
1332456a5849SKalderon, Michal 
1333456a5849SKalderon, Michal 	rc = qed_iwarp_wait_cid_map_cleared(p_hwfn,
1334456a5849SKalderon, Michal 					    &p_hwfn->p_rdma_info->tcp_cid_map);
1335456a5849SKalderon, Michal 	if (rc)
1336456a5849SKalderon, Michal 		return rc;
1337456a5849SKalderon, Michal 
1338456a5849SKalderon, Michal 	/* Now free the tcp cids from the main cid map */
1339456a5849SKalderon, Michal 	for (i = 0; i < QED_IWARP_PREALLOC_CNT; i++)
1340456a5849SKalderon, Michal 		qed_bmap_release_id(p_hwfn, &p_hwfn->p_rdma_info->cid_map, i);
1341456a5849SKalderon, Michal 
134267b40dccSKalderon, Michal 	/* Now wait for all cids to be completed */
134367b40dccSKalderon, Michal 	return qed_iwarp_wait_cid_map_cleared(p_hwfn,
134467b40dccSKalderon, Michal 					      &p_hwfn->p_rdma_info->cid_map);
134567b40dccSKalderon, Michal }
134667b40dccSKalderon, Michal 
1347456a5849SKalderon, Michal static void qed_iwarp_free_prealloc_ep(struct qed_hwfn *p_hwfn)
1348456a5849SKalderon, Michal {
1349456a5849SKalderon, Michal 	struct qed_iwarp_ep *ep;
1350456a5849SKalderon, Michal 
1351456a5849SKalderon, Michal 	while (!list_empty(&p_hwfn->p_rdma_info->iwarp.ep_free_list)) {
1352456a5849SKalderon, Michal 		spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1353456a5849SKalderon, Michal 
1354456a5849SKalderon, Michal 		ep = list_first_entry(&p_hwfn->p_rdma_info->iwarp.ep_free_list,
1355456a5849SKalderon, Michal 				      struct qed_iwarp_ep, list_entry);
1356456a5849SKalderon, Michal 
1357456a5849SKalderon, Michal 		if (!ep) {
1358456a5849SKalderon, Michal 			spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1359456a5849SKalderon, Michal 			break;
1360456a5849SKalderon, Michal 		}
1361456a5849SKalderon, Michal 		list_del(&ep->list_entry);
1362456a5849SKalderon, Michal 
1363456a5849SKalderon, Michal 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1364456a5849SKalderon, Michal 
1365456a5849SKalderon, Michal 		if (ep->tcp_cid != QED_IWARP_INVALID_TCP_CID)
1366456a5849SKalderon, Michal 			qed_iwarp_cid_cleaned(p_hwfn, ep->tcp_cid);
1367456a5849SKalderon, Michal 
1368456a5849SKalderon, Michal 		qed_iwarp_destroy_ep(p_hwfn, ep, false);
1369456a5849SKalderon, Michal 	}
1370456a5849SKalderon, Michal }
1371456a5849SKalderon, Michal 
1372456a5849SKalderon, Michal static int qed_iwarp_prealloc_ep(struct qed_hwfn *p_hwfn, bool init)
1373456a5849SKalderon, Michal {
1374456a5849SKalderon, Michal 	struct qed_iwarp_ep *ep;
1375456a5849SKalderon, Michal 	int rc = 0;
1376456a5849SKalderon, Michal 	int count;
1377456a5849SKalderon, Michal 	u32 cid;
1378456a5849SKalderon, Michal 	int i;
1379456a5849SKalderon, Michal 
1380456a5849SKalderon, Michal 	count = init ? QED_IWARP_PREALLOC_CNT : 1;
1381456a5849SKalderon, Michal 	for (i = 0; i < count; i++) {
1382456a5849SKalderon, Michal 		rc = qed_iwarp_create_ep(p_hwfn, &ep);
1383456a5849SKalderon, Michal 		if (rc)
1384456a5849SKalderon, Michal 			return rc;
1385456a5849SKalderon, Michal 
1386456a5849SKalderon, Michal 		/* During initialization we allocate from the main pool,
1387456a5849SKalderon, Michal 		 * afterwards we allocate only from the tcp_cid.
1388456a5849SKalderon, Michal 		 */
1389456a5849SKalderon, Michal 		if (init) {
1390456a5849SKalderon, Michal 			rc = qed_iwarp_alloc_cid(p_hwfn, &cid);
1391456a5849SKalderon, Michal 			if (rc)
1392456a5849SKalderon, Michal 				goto err;
1393456a5849SKalderon, Michal 			qed_iwarp_set_tcp_cid(p_hwfn, cid);
1394456a5849SKalderon, Michal 		} else {
1395456a5849SKalderon, Michal 			/* We don't care about the return code, it's ok if
1396456a5849SKalderon, Michal 			 * tcp_cid remains invalid...in this case we'll
1397456a5849SKalderon, Michal 			 * defer allocation
1398456a5849SKalderon, Michal 			 */
1399456a5849SKalderon, Michal 			qed_iwarp_alloc_tcp_cid(p_hwfn, &cid);
1400456a5849SKalderon, Michal 		}
1401456a5849SKalderon, Michal 
1402456a5849SKalderon, Michal 		ep->tcp_cid = cid;
1403456a5849SKalderon, Michal 
1404456a5849SKalderon, Michal 		spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1405456a5849SKalderon, Michal 		list_add_tail(&ep->list_entry,
1406456a5849SKalderon, Michal 			      &p_hwfn->p_rdma_info->iwarp.ep_free_list);
1407456a5849SKalderon, Michal 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
1408456a5849SKalderon, Michal 	}
1409456a5849SKalderon, Michal 
1410456a5849SKalderon, Michal 	return rc;
1411456a5849SKalderon, Michal 
1412456a5849SKalderon, Michal err:
1413456a5849SKalderon, Michal 	qed_iwarp_destroy_ep(p_hwfn, ep, false);
1414456a5849SKalderon, Michal 
1415456a5849SKalderon, Michal 	return rc;
1416456a5849SKalderon, Michal }
1417456a5849SKalderon, Michal 
141867b40dccSKalderon, Michal int qed_iwarp_alloc(struct qed_hwfn *p_hwfn)
141967b40dccSKalderon, Michal {
1420456a5849SKalderon, Michal 	int rc;
1421456a5849SKalderon, Michal 
1422456a5849SKalderon, Michal 	/* Allocate bitmap for tcp cid. These are used by passive side
1423456a5849SKalderon, Michal 	 * to ensure it can allocate a tcp cid during dpc that was
1424456a5849SKalderon, Michal 	 * pre-acquired and doesn't require dynamic allocation of ilt
1425456a5849SKalderon, Michal 	 */
1426456a5849SKalderon, Michal 	rc = qed_rdma_bmap_alloc(p_hwfn, &p_hwfn->p_rdma_info->tcp_cid_map,
1427456a5849SKalderon, Michal 				 QED_IWARP_PREALLOC_CNT, "TCP_CID");
1428456a5849SKalderon, Michal 	if (rc) {
1429456a5849SKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1430456a5849SKalderon, Michal 			   "Failed to allocate tcp cid, rc = %d\n", rc);
1431456a5849SKalderon, Michal 		return rc;
1432456a5849SKalderon, Michal 	}
1433456a5849SKalderon, Michal 
1434456a5849SKalderon, Michal 	INIT_LIST_HEAD(&p_hwfn->p_rdma_info->iwarp.ep_free_list);
143567b40dccSKalderon, Michal 	spin_lock_init(&p_hwfn->p_rdma_info->iwarp.iw_lock);
143667b40dccSKalderon, Michal 
1437f436baf3SMichal Kalderon 	rc = qed_iwarp_prealloc_ep(p_hwfn, true);
1438f436baf3SMichal Kalderon 	if (rc)
1439f436baf3SMichal Kalderon 		return rc;
1440f436baf3SMichal Kalderon 
1441f436baf3SMichal Kalderon 	return qed_ooo_alloc(p_hwfn);
144267b40dccSKalderon, Michal }
144367b40dccSKalderon, Michal 
144467b40dccSKalderon, Michal void qed_iwarp_resc_free(struct qed_hwfn *p_hwfn)
144567b40dccSKalderon, Michal {
1446fcb39f6cSMichal Kalderon 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
1447fcb39f6cSMichal Kalderon 
1448f436baf3SMichal Kalderon 	qed_ooo_free(p_hwfn);
1449456a5849SKalderon, Michal 	qed_rdma_bmap_free(p_hwfn, &p_hwfn->p_rdma_info->tcp_cid_map, 1);
1450fcb39f6cSMichal Kalderon 	kfree(iwarp_info->mpa_bufs);
1451469981b1SMichal Kalderon 	kfree(iwarp_info->partial_fpdus);
14521e28eaadSMichal Kalderon 	kfree(iwarp_info->mpa_intermediate_buf);
1453456a5849SKalderon, Michal }
1454456a5849SKalderon, Michal 
1455456a5849SKalderon, Michal int qed_iwarp_accept(void *rdma_cxt, struct qed_iwarp_accept_in *iparams)
1456456a5849SKalderon, Michal {
14574b0fdd7cSKalderon, Michal 	struct qed_hwfn *p_hwfn = rdma_cxt;
1458456a5849SKalderon, Michal 	struct qed_iwarp_ep *ep;
1459456a5849SKalderon, Michal 	u8 mpa_data_size = 0;
1460456a5849SKalderon, Michal 	int rc;
1461456a5849SKalderon, Michal 
14624b0fdd7cSKalderon, Michal 	ep = iparams->ep_context;
1463456a5849SKalderon, Michal 	if (!ep) {
1464456a5849SKalderon, Michal 		DP_ERR(p_hwfn, "Ep Context receive in accept is NULL\n");
1465456a5849SKalderon, Michal 		return -EINVAL;
1466456a5849SKalderon, Michal 	}
1467456a5849SKalderon, Michal 
1468456a5849SKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) EP(0x%x)\n",
1469456a5849SKalderon, Michal 		   iparams->qp->icid, ep->tcp_cid);
1470456a5849SKalderon, Michal 
1471456a5849SKalderon, Michal 	if ((iparams->ord > QED_IWARP_ORD_DEFAULT) ||
1472456a5849SKalderon, Michal 	    (iparams->ird > QED_IWARP_IRD_DEFAULT)) {
1473456a5849SKalderon, Michal 		DP_VERBOSE(p_hwfn,
1474456a5849SKalderon, Michal 			   QED_MSG_RDMA,
1475456a5849SKalderon, Michal 			   "QP(0x%x) EP(0x%x) ERROR: Invalid ord(0x%x)/ird(0x%x)\n",
1476456a5849SKalderon, Michal 			   iparams->qp->icid,
1477456a5849SKalderon, Michal 			   ep->tcp_cid, iparams->ord, iparams->ord);
1478456a5849SKalderon, Michal 		return -EINVAL;
1479456a5849SKalderon, Michal 	}
1480456a5849SKalderon, Michal 
1481456a5849SKalderon, Michal 	qed_iwarp_prealloc_ep(p_hwfn, false);
1482456a5849SKalderon, Michal 
1483456a5849SKalderon, Michal 	ep->cb_context = iparams->cb_context;
1484456a5849SKalderon, Michal 	ep->qp = iparams->qp;
1485456a5849SKalderon, Michal 	ep->qp->ep = ep;
1486456a5849SKalderon, Michal 
1487456a5849SKalderon, Michal 	if (ep->mpa_rev == MPA_NEGOTIATION_TYPE_ENHANCED) {
1488456a5849SKalderon, Michal 		/* Negotiate ord/ird: if upperlayer requested ord larger than
1489456a5849SKalderon, Michal 		 * ird advertised by remote, we need to decrease our ord
1490456a5849SKalderon, Michal 		 */
1491456a5849SKalderon, Michal 		if (iparams->ord > ep->cm_info.ird)
1492456a5849SKalderon, Michal 			iparams->ord = ep->cm_info.ird;
1493456a5849SKalderon, Michal 
1494456a5849SKalderon, Michal 		if ((ep->rtr_type & MPA_RTR_TYPE_ZERO_READ) &&
1495456a5849SKalderon, Michal 		    (iparams->ird == 0))
1496456a5849SKalderon, Michal 			iparams->ird = 1;
1497456a5849SKalderon, Michal 	}
1498456a5849SKalderon, Michal 
1499456a5849SKalderon, Michal 	/* Update cm_info ord/ird to be negotiated values */
1500456a5849SKalderon, Michal 	ep->cm_info.ord = iparams->ord;
1501456a5849SKalderon, Michal 	ep->cm_info.ird = iparams->ird;
1502456a5849SKalderon, Michal 
1503456a5849SKalderon, Michal 	qed_iwarp_mpa_v2_set_private(p_hwfn, ep, &mpa_data_size);
1504456a5849SKalderon, Michal 
1505456a5849SKalderon, Michal 	ep->cm_info.private_data = ep->ep_buffer_virt->out_pdata;
1506456a5849SKalderon, Michal 	ep->cm_info.private_data_len = iparams->private_data_len +
1507456a5849SKalderon, Michal 				       mpa_data_size;
1508456a5849SKalderon, Michal 
1509456a5849SKalderon, Michal 	memcpy((u8 *)ep->ep_buffer_virt->out_pdata + mpa_data_size,
1510456a5849SKalderon, Michal 	       iparams->private_data, iparams->private_data_len);
1511456a5849SKalderon, Michal 
1512456a5849SKalderon, Michal 	rc = qed_iwarp_mpa_offload(p_hwfn, ep);
1513456a5849SKalderon, Michal 	if (rc)
1514456a5849SKalderon, Michal 		qed_iwarp_modify_qp(p_hwfn,
1515456a5849SKalderon, Michal 				    iparams->qp, QED_IWARP_QP_STATE_ERROR, 1);
1516456a5849SKalderon, Michal 
1517456a5849SKalderon, Michal 	return rc;
1518456a5849SKalderon, Michal }
1519456a5849SKalderon, Michal 
1520456a5849SKalderon, Michal int qed_iwarp_reject(void *rdma_cxt, struct qed_iwarp_reject_in *iparams)
1521456a5849SKalderon, Michal {
1522456a5849SKalderon, Michal 	struct qed_hwfn *p_hwfn = rdma_cxt;
1523456a5849SKalderon, Michal 	struct qed_iwarp_ep *ep;
1524456a5849SKalderon, Michal 	u8 mpa_data_size = 0;
1525456a5849SKalderon, Michal 
1526456a5849SKalderon, Michal 	ep = iparams->ep_context;
1527456a5849SKalderon, Michal 	if (!ep) {
1528456a5849SKalderon, Michal 		DP_ERR(p_hwfn, "Ep Context receive in reject is NULL\n");
1529456a5849SKalderon, Michal 		return -EINVAL;
1530456a5849SKalderon, Michal 	}
1531456a5849SKalderon, Michal 
1532456a5849SKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "EP(0x%x)\n", ep->tcp_cid);
1533456a5849SKalderon, Michal 
1534456a5849SKalderon, Michal 	ep->cb_context = iparams->cb_context;
1535456a5849SKalderon, Michal 	ep->qp = NULL;
1536456a5849SKalderon, Michal 
1537456a5849SKalderon, Michal 	qed_iwarp_mpa_v2_set_private(p_hwfn, ep, &mpa_data_size);
1538456a5849SKalderon, Michal 
1539456a5849SKalderon, Michal 	ep->cm_info.private_data = ep->ep_buffer_virt->out_pdata;
1540456a5849SKalderon, Michal 	ep->cm_info.private_data_len = iparams->private_data_len +
1541456a5849SKalderon, Michal 				       mpa_data_size;
1542456a5849SKalderon, Michal 
1543456a5849SKalderon, Michal 	memcpy((u8 *)ep->ep_buffer_virt->out_pdata + mpa_data_size,
1544456a5849SKalderon, Michal 	       iparams->private_data, iparams->private_data_len);
1545456a5849SKalderon, Michal 
1546456a5849SKalderon, Michal 	return qed_iwarp_mpa_offload(p_hwfn, ep);
154767b40dccSKalderon, Michal }
154867b40dccSKalderon, Michal 
154965a91a6cSKalderon, Michal static void
155065a91a6cSKalderon, Michal qed_iwarp_print_cm_info(struct qed_hwfn *p_hwfn,
155165a91a6cSKalderon, Michal 			struct qed_iwarp_cm_info *cm_info)
155265a91a6cSKalderon, Michal {
155365a91a6cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "ip_version = %d\n",
155465a91a6cSKalderon, Michal 		   cm_info->ip_version);
155565a91a6cSKalderon, Michal 
155665a91a6cSKalderon, Michal 	if (cm_info->ip_version == QED_TCP_IPV4)
155765a91a6cSKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
155865a91a6cSKalderon, Michal 			   "remote_ip %pI4h:%x, local_ip %pI4h:%x vlan=%x\n",
155965a91a6cSKalderon, Michal 			   cm_info->remote_ip, cm_info->remote_port,
156065a91a6cSKalderon, Michal 			   cm_info->local_ip, cm_info->local_port,
156165a91a6cSKalderon, Michal 			   cm_info->vlan);
156265a91a6cSKalderon, Michal 	else
156365a91a6cSKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
156491d1ae47SKalderon, Michal 			   "remote_ip %pI6:%x, local_ip %pI6:%x vlan=%x\n",
156565a91a6cSKalderon, Michal 			   cm_info->remote_ip, cm_info->remote_port,
156665a91a6cSKalderon, Michal 			   cm_info->local_ip, cm_info->local_port,
156765a91a6cSKalderon, Michal 			   cm_info->vlan);
156865a91a6cSKalderon, Michal 
156965a91a6cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
157065a91a6cSKalderon, Michal 		   "private_data_len = %x ord = %d, ird = %d\n",
157165a91a6cSKalderon, Michal 		   cm_info->private_data_len, cm_info->ord, cm_info->ird);
157265a91a6cSKalderon, Michal }
157365a91a6cSKalderon, Michal 
1574b5c29ca7SKalderon, Michal static int
1575b5c29ca7SKalderon, Michal qed_iwarp_ll2_post_rx(struct qed_hwfn *p_hwfn,
1576b5c29ca7SKalderon, Michal 		      struct qed_iwarp_ll2_buff *buf, u8 handle)
1577b5c29ca7SKalderon, Michal {
1578b5c29ca7SKalderon, Michal 	int rc;
1579b5c29ca7SKalderon, Michal 
1580b5c29ca7SKalderon, Michal 	rc = qed_ll2_post_rx_buffer(p_hwfn, handle, buf->data_phys_addr,
1581b5c29ca7SKalderon, Michal 				    (u16)buf->buff_size, buf, 1);
1582b5c29ca7SKalderon, Michal 	if (rc) {
1583b5c29ca7SKalderon, Michal 		DP_NOTICE(p_hwfn,
1584b5c29ca7SKalderon, Michal 			  "Failed to repost rx buffer to ll2 rc = %d, handle=%d\n",
1585b5c29ca7SKalderon, Michal 			  rc, handle);
1586b5c29ca7SKalderon, Michal 		dma_free_coherent(&p_hwfn->cdev->pdev->dev, buf->buff_size,
1587b5c29ca7SKalderon, Michal 				  buf->data, buf->data_phys_addr);
1588b5c29ca7SKalderon, Michal 		kfree(buf);
1589b5c29ca7SKalderon, Michal 	}
1590b5c29ca7SKalderon, Michal 
1591b5c29ca7SKalderon, Michal 	return rc;
1592b5c29ca7SKalderon, Michal }
1593b5c29ca7SKalderon, Michal 
1594456a5849SKalderon, Michal static bool
1595456a5849SKalderon, Michal qed_iwarp_ep_exists(struct qed_hwfn *p_hwfn, struct qed_iwarp_cm_info *cm_info)
1596456a5849SKalderon, Michal {
1597456a5849SKalderon, Michal 	struct qed_iwarp_ep *ep = NULL;
1598456a5849SKalderon, Michal 	bool found = false;
1599456a5849SKalderon, Michal 
1600456a5849SKalderon, Michal 	list_for_each_entry(ep,
1601456a5849SKalderon, Michal 			    &p_hwfn->p_rdma_info->iwarp.ep_list,
1602456a5849SKalderon, Michal 			    list_entry) {
1603456a5849SKalderon, Michal 		if ((ep->cm_info.local_port == cm_info->local_port) &&
1604456a5849SKalderon, Michal 		    (ep->cm_info.remote_port == cm_info->remote_port) &&
1605456a5849SKalderon, Michal 		    (ep->cm_info.vlan == cm_info->vlan) &&
1606456a5849SKalderon, Michal 		    !memcmp(&ep->cm_info.local_ip, cm_info->local_ip,
1607456a5849SKalderon, Michal 			    sizeof(cm_info->local_ip)) &&
1608456a5849SKalderon, Michal 		    !memcmp(&ep->cm_info.remote_ip, cm_info->remote_ip,
1609456a5849SKalderon, Michal 			    sizeof(cm_info->remote_ip))) {
1610456a5849SKalderon, Michal 			found = true;
1611456a5849SKalderon, Michal 			break;
1612456a5849SKalderon, Michal 		}
1613456a5849SKalderon, Michal 	}
1614456a5849SKalderon, Michal 
1615456a5849SKalderon, Michal 	if (found) {
1616456a5849SKalderon, Michal 		DP_NOTICE(p_hwfn,
1617456a5849SKalderon, Michal 			  "SYN received on active connection - dropping\n");
1618456a5849SKalderon, Michal 		qed_iwarp_print_cm_info(p_hwfn, cm_info);
1619456a5849SKalderon, Michal 
1620456a5849SKalderon, Michal 		return true;
1621456a5849SKalderon, Michal 	}
1622456a5849SKalderon, Michal 
1623456a5849SKalderon, Michal 	return false;
1624456a5849SKalderon, Michal }
1625456a5849SKalderon, Michal 
162665a91a6cSKalderon, Michal static struct qed_iwarp_listener *
162765a91a6cSKalderon, Michal qed_iwarp_get_listener(struct qed_hwfn *p_hwfn,
162865a91a6cSKalderon, Michal 		       struct qed_iwarp_cm_info *cm_info)
162965a91a6cSKalderon, Michal {
163065a91a6cSKalderon, Michal 	struct qed_iwarp_listener *listener = NULL;
163165a91a6cSKalderon, Michal 	static const u32 ip_zero[4] = { 0, 0, 0, 0 };
163265a91a6cSKalderon, Michal 	bool found = false;
163365a91a6cSKalderon, Michal 
163465a91a6cSKalderon, Michal 	qed_iwarp_print_cm_info(p_hwfn, cm_info);
163565a91a6cSKalderon, Michal 
163665a91a6cSKalderon, Michal 	list_for_each_entry(listener,
163765a91a6cSKalderon, Michal 			    &p_hwfn->p_rdma_info->iwarp.listen_list,
163865a91a6cSKalderon, Michal 			    list_entry) {
163965a91a6cSKalderon, Michal 		if (listener->port == cm_info->local_port) {
164065a91a6cSKalderon, Michal 			if (!memcmp(listener->ip_addr,
164165a91a6cSKalderon, Michal 				    ip_zero, sizeof(ip_zero))) {
164265a91a6cSKalderon, Michal 				found = true;
164365a91a6cSKalderon, Michal 				break;
164465a91a6cSKalderon, Michal 			}
164565a91a6cSKalderon, Michal 
164665a91a6cSKalderon, Michal 			if (!memcmp(listener->ip_addr,
164765a91a6cSKalderon, Michal 				    cm_info->local_ip,
164865a91a6cSKalderon, Michal 				    sizeof(cm_info->local_ip)) &&
164965a91a6cSKalderon, Michal 			    (listener->vlan == cm_info->vlan)) {
165065a91a6cSKalderon, Michal 				found = true;
165165a91a6cSKalderon, Michal 				break;
165265a91a6cSKalderon, Michal 			}
165365a91a6cSKalderon, Michal 		}
165465a91a6cSKalderon, Michal 	}
165565a91a6cSKalderon, Michal 
165665a91a6cSKalderon, Michal 	if (found) {
165765a91a6cSKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "listener found = %p\n",
165865a91a6cSKalderon, Michal 			   listener);
165965a91a6cSKalderon, Michal 		return listener;
166065a91a6cSKalderon, Michal 	}
166165a91a6cSKalderon, Michal 
166265a91a6cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "listener not found\n");
166365a91a6cSKalderon, Michal 	return NULL;
166465a91a6cSKalderon, Michal }
166565a91a6cSKalderon, Michal 
166665a91a6cSKalderon, Michal static int
166765a91a6cSKalderon, Michal qed_iwarp_parse_rx_pkt(struct qed_hwfn *p_hwfn,
166865a91a6cSKalderon, Michal 		       struct qed_iwarp_cm_info *cm_info,
166965a91a6cSKalderon, Michal 		       void *buf,
167065a91a6cSKalderon, Michal 		       u8 *remote_mac_addr,
167165a91a6cSKalderon, Michal 		       u8 *local_mac_addr,
167265a91a6cSKalderon, Michal 		       int *payload_len, int *tcp_start_offset)
167365a91a6cSKalderon, Michal {
167465a91a6cSKalderon, Michal 	struct vlan_ethhdr *vethh;
167565a91a6cSKalderon, Michal 	bool vlan_valid = false;
167665a91a6cSKalderon, Michal 	struct ipv6hdr *ip6h;
167765a91a6cSKalderon, Michal 	struct ethhdr *ethh;
167865a91a6cSKalderon, Michal 	struct tcphdr *tcph;
167965a91a6cSKalderon, Michal 	struct iphdr *iph;
168065a91a6cSKalderon, Michal 	int eth_hlen;
168165a91a6cSKalderon, Michal 	int ip_hlen;
168265a91a6cSKalderon, Michal 	int eth_type;
168365a91a6cSKalderon, Michal 	int i;
168465a91a6cSKalderon, Michal 
168565a91a6cSKalderon, Michal 	ethh = buf;
168665a91a6cSKalderon, Michal 	eth_type = ntohs(ethh->h_proto);
168765a91a6cSKalderon, Michal 	if (eth_type == ETH_P_8021Q) {
168865a91a6cSKalderon, Michal 		vlan_valid = true;
168965a91a6cSKalderon, Michal 		vethh = (struct vlan_ethhdr *)ethh;
169065a91a6cSKalderon, Michal 		cm_info->vlan = ntohs(vethh->h_vlan_TCI) & VLAN_VID_MASK;
169165a91a6cSKalderon, Michal 		eth_type = ntohs(vethh->h_vlan_encapsulated_proto);
169265a91a6cSKalderon, Michal 	}
169365a91a6cSKalderon, Michal 
169465a91a6cSKalderon, Michal 	eth_hlen = ETH_HLEN + (vlan_valid ? sizeof(u32) : 0);
169565a91a6cSKalderon, Michal 
1696456a5849SKalderon, Michal 	ether_addr_copy(remote_mac_addr, ethh->h_source);
1697456a5849SKalderon, Michal 	ether_addr_copy(local_mac_addr, ethh->h_dest);
169865a91a6cSKalderon, Michal 
169965a91a6cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "eth_type =%d source mac: %pM\n",
170065a91a6cSKalderon, Michal 		   eth_type, ethh->h_source);
170165a91a6cSKalderon, Michal 
170265a91a6cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "eth_hlen=%d destination mac: %pM\n",
170365a91a6cSKalderon, Michal 		   eth_hlen, ethh->h_dest);
170465a91a6cSKalderon, Michal 
170565a91a6cSKalderon, Michal 	iph = (struct iphdr *)((u8 *)(ethh) + eth_hlen);
170665a91a6cSKalderon, Michal 
170765a91a6cSKalderon, Michal 	if (eth_type == ETH_P_IP) {
170816da0904SMichal Kalderon 		if (iph->protocol != IPPROTO_TCP) {
170916da0904SMichal Kalderon 			DP_NOTICE(p_hwfn,
171016da0904SMichal Kalderon 				  "Unexpected ip protocol on ll2 %x\n",
171116da0904SMichal Kalderon 				  iph->protocol);
171216da0904SMichal Kalderon 			return -EINVAL;
171316da0904SMichal Kalderon 		}
171416da0904SMichal Kalderon 
171565a91a6cSKalderon, Michal 		cm_info->local_ip[0] = ntohl(iph->daddr);
171665a91a6cSKalderon, Michal 		cm_info->remote_ip[0] = ntohl(iph->saddr);
171765a91a6cSKalderon, Michal 		cm_info->ip_version = TCP_IPV4;
171865a91a6cSKalderon, Michal 
171965a91a6cSKalderon, Michal 		ip_hlen = (iph->ihl) * sizeof(u32);
172065a91a6cSKalderon, Michal 		*payload_len = ntohs(iph->tot_len) - ip_hlen;
172165a91a6cSKalderon, Michal 	} else if (eth_type == ETH_P_IPV6) {
172265a91a6cSKalderon, Michal 		ip6h = (struct ipv6hdr *)iph;
172316da0904SMichal Kalderon 
172416da0904SMichal Kalderon 		if (ip6h->nexthdr != IPPROTO_TCP) {
172516da0904SMichal Kalderon 			DP_NOTICE(p_hwfn,
172616da0904SMichal Kalderon 				  "Unexpected ip protocol on ll2 %x\n",
172716da0904SMichal Kalderon 				  iph->protocol);
172816da0904SMichal Kalderon 			return -EINVAL;
172916da0904SMichal Kalderon 		}
173016da0904SMichal Kalderon 
173165a91a6cSKalderon, Michal 		for (i = 0; i < 4; i++) {
173265a91a6cSKalderon, Michal 			cm_info->local_ip[i] =
173365a91a6cSKalderon, Michal 			    ntohl(ip6h->daddr.in6_u.u6_addr32[i]);
173465a91a6cSKalderon, Michal 			cm_info->remote_ip[i] =
173565a91a6cSKalderon, Michal 			    ntohl(ip6h->saddr.in6_u.u6_addr32[i]);
173665a91a6cSKalderon, Michal 		}
173765a91a6cSKalderon, Michal 		cm_info->ip_version = TCP_IPV6;
173865a91a6cSKalderon, Michal 
173965a91a6cSKalderon, Michal 		ip_hlen = sizeof(*ip6h);
174065a91a6cSKalderon, Michal 		*payload_len = ntohs(ip6h->payload_len);
174165a91a6cSKalderon, Michal 	} else {
174265a91a6cSKalderon, Michal 		DP_NOTICE(p_hwfn, "Unexpected ethertype on ll2 %x\n", eth_type);
174365a91a6cSKalderon, Michal 		return -EINVAL;
174465a91a6cSKalderon, Michal 	}
174565a91a6cSKalderon, Michal 
174665a91a6cSKalderon, Michal 	tcph = (struct tcphdr *)((u8 *)iph + ip_hlen);
174765a91a6cSKalderon, Michal 
174865a91a6cSKalderon, Michal 	if (!tcph->syn) {
174965a91a6cSKalderon, Michal 		DP_NOTICE(p_hwfn,
175065a91a6cSKalderon, Michal 			  "Only SYN type packet expected on this ll2 conn, iph->ihl=%d source=%d dest=%d\n",
175165a91a6cSKalderon, Michal 			  iph->ihl, tcph->source, tcph->dest);
175265a91a6cSKalderon, Michal 		return -EINVAL;
175365a91a6cSKalderon, Michal 	}
175465a91a6cSKalderon, Michal 
175565a91a6cSKalderon, Michal 	cm_info->local_port = ntohs(tcph->dest);
175665a91a6cSKalderon, Michal 	cm_info->remote_port = ntohs(tcph->source);
175765a91a6cSKalderon, Michal 
175865a91a6cSKalderon, Michal 	qed_iwarp_print_cm_info(p_hwfn, cm_info);
175965a91a6cSKalderon, Michal 
176065a91a6cSKalderon, Michal 	*tcp_start_offset = eth_hlen + ip_hlen;
176165a91a6cSKalderon, Michal 
176265a91a6cSKalderon, Michal 	return 0;
176365a91a6cSKalderon, Michal }
176465a91a6cSKalderon, Michal 
1765469981b1SMichal Kalderon static struct qed_iwarp_fpdu *qed_iwarp_get_curr_fpdu(struct qed_hwfn *p_hwfn,
1766469981b1SMichal Kalderon 						      u16 cid)
1767469981b1SMichal Kalderon {
1768469981b1SMichal Kalderon 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
1769469981b1SMichal Kalderon 	struct qed_iwarp_fpdu *partial_fpdu;
1770469981b1SMichal Kalderon 	u32 idx;
1771469981b1SMichal Kalderon 
1772469981b1SMichal Kalderon 	idx = cid - qed_cxt_get_proto_cid_start(p_hwfn, PROTOCOLID_IWARP);
1773469981b1SMichal Kalderon 	if (idx >= iwarp_info->max_num_partial_fpdus) {
1774469981b1SMichal Kalderon 		DP_ERR(p_hwfn, "Invalid cid %x max_num_partial_fpdus=%x\n", cid,
1775469981b1SMichal Kalderon 		       iwarp_info->max_num_partial_fpdus);
1776469981b1SMichal Kalderon 		return NULL;
1777469981b1SMichal Kalderon 	}
1778469981b1SMichal Kalderon 
1779469981b1SMichal Kalderon 	partial_fpdu = &iwarp_info->partial_fpdus[idx];
1780469981b1SMichal Kalderon 
1781469981b1SMichal Kalderon 	return partial_fpdu;
1782469981b1SMichal Kalderon }
1783469981b1SMichal Kalderon 
1784469981b1SMichal Kalderon enum qed_iwarp_mpa_pkt_type {
1785469981b1SMichal Kalderon 	QED_IWARP_MPA_PKT_PACKED,
1786469981b1SMichal Kalderon 	QED_IWARP_MPA_PKT_PARTIAL,
1787469981b1SMichal Kalderon 	QED_IWARP_MPA_PKT_UNALIGNED
1788469981b1SMichal Kalderon };
1789469981b1SMichal Kalderon 
1790c7d1d839SMichal Kalderon #define QED_IWARP_INVALID_FPDU_LENGTH 0xffff
1791469981b1SMichal Kalderon #define QED_IWARP_MPA_FPDU_LENGTH_SIZE (2)
1792469981b1SMichal Kalderon #define QED_IWARP_MPA_CRC32_DIGEST_SIZE (4)
1793469981b1SMichal Kalderon 
1794469981b1SMichal Kalderon /* Pad to multiple of 4 */
1795469981b1SMichal Kalderon #define QED_IWARP_PDU_DATA_LEN_WITH_PAD(data_len) ALIGN(data_len, 4)
1796469981b1SMichal Kalderon #define QED_IWARP_FPDU_LEN_WITH_PAD(_mpa_len)				   \
1797469981b1SMichal Kalderon 	(QED_IWARP_PDU_DATA_LEN_WITH_PAD((_mpa_len) +			   \
1798469981b1SMichal Kalderon 					 QED_IWARP_MPA_FPDU_LENGTH_SIZE) + \
1799469981b1SMichal Kalderon 					 QED_IWARP_MPA_CRC32_DIGEST_SIZE)
1800469981b1SMichal Kalderon 
1801ae3488ffSMichal Kalderon /* fpdu can be fragmented over maximum 3 bds: header, partial mpa, unaligned */
1802ae3488ffSMichal Kalderon #define QED_IWARP_MAX_BDS_PER_FPDU 3
1803469981b1SMichal Kalderon 
18044f4aaa17SHernán Gonzalez static const char * const pkt_type_str[] = {
1805469981b1SMichal Kalderon 	"QED_IWARP_MPA_PKT_PACKED",
1806469981b1SMichal Kalderon 	"QED_IWARP_MPA_PKT_PARTIAL",
1807469981b1SMichal Kalderon 	"QED_IWARP_MPA_PKT_UNALIGNED"
1808469981b1SMichal Kalderon };
1809469981b1SMichal Kalderon 
18101e28eaadSMichal Kalderon static int
18111e28eaadSMichal Kalderon qed_iwarp_recycle_pkt(struct qed_hwfn *p_hwfn,
18121e28eaadSMichal Kalderon 		      struct qed_iwarp_fpdu *fpdu,
18131e28eaadSMichal Kalderon 		      struct qed_iwarp_ll2_buff *buf);
18141e28eaadSMichal Kalderon 
1815469981b1SMichal Kalderon static enum qed_iwarp_mpa_pkt_type
1816469981b1SMichal Kalderon qed_iwarp_mpa_classify(struct qed_hwfn *p_hwfn,
1817469981b1SMichal Kalderon 		       struct qed_iwarp_fpdu *fpdu,
1818469981b1SMichal Kalderon 		       u16 tcp_payload_len, u8 *mpa_data)
1819469981b1SMichal Kalderon {
1820469981b1SMichal Kalderon 	enum qed_iwarp_mpa_pkt_type pkt_type;
1821469981b1SMichal Kalderon 	u16 mpa_len;
1822469981b1SMichal Kalderon 
1823469981b1SMichal Kalderon 	if (fpdu->incomplete_bytes) {
1824469981b1SMichal Kalderon 		pkt_type = QED_IWARP_MPA_PKT_UNALIGNED;
1825469981b1SMichal Kalderon 		goto out;
1826469981b1SMichal Kalderon 	}
1827469981b1SMichal Kalderon 
1828c7d1d839SMichal Kalderon 	/* special case of one byte remaining...
1829c7d1d839SMichal Kalderon 	 * lower byte will be read next packet
1830c7d1d839SMichal Kalderon 	 */
1831c7d1d839SMichal Kalderon 	if (tcp_payload_len == 1) {
1832c7d1d839SMichal Kalderon 		fpdu->fpdu_length = *mpa_data << BITS_PER_BYTE;
1833c7d1d839SMichal Kalderon 		pkt_type = QED_IWARP_MPA_PKT_PARTIAL;
1834c7d1d839SMichal Kalderon 		goto out;
1835c7d1d839SMichal Kalderon 	}
1836c7d1d839SMichal Kalderon 
1837469981b1SMichal Kalderon 	mpa_len = ntohs(*((u16 *)(mpa_data)));
1838469981b1SMichal Kalderon 	fpdu->fpdu_length = QED_IWARP_FPDU_LEN_WITH_PAD(mpa_len);
1839469981b1SMichal Kalderon 
1840469981b1SMichal Kalderon 	if (fpdu->fpdu_length <= tcp_payload_len)
1841469981b1SMichal Kalderon 		pkt_type = QED_IWARP_MPA_PKT_PACKED;
1842469981b1SMichal Kalderon 	else
1843469981b1SMichal Kalderon 		pkt_type = QED_IWARP_MPA_PKT_PARTIAL;
1844469981b1SMichal Kalderon 
1845469981b1SMichal Kalderon out:
1846469981b1SMichal Kalderon 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
1847469981b1SMichal Kalderon 		   "MPA_ALIGN: %s: fpdu_length=0x%x tcp_payload_len:0x%x\n",
1848469981b1SMichal Kalderon 		   pkt_type_str[pkt_type], fpdu->fpdu_length, tcp_payload_len);
1849469981b1SMichal Kalderon 
1850469981b1SMichal Kalderon 	return pkt_type;
1851469981b1SMichal Kalderon }
1852469981b1SMichal Kalderon 
1853469981b1SMichal Kalderon static void
1854469981b1SMichal Kalderon qed_iwarp_init_fpdu(struct qed_iwarp_ll2_buff *buf,
1855469981b1SMichal Kalderon 		    struct qed_iwarp_fpdu *fpdu,
1856469981b1SMichal Kalderon 		    struct unaligned_opaque_data *pkt_data,
1857469981b1SMichal Kalderon 		    u16 tcp_payload_size, u8 placement_offset)
1858469981b1SMichal Kalderon {
1859469981b1SMichal Kalderon 	fpdu->mpa_buf = buf;
1860469981b1SMichal Kalderon 	fpdu->pkt_hdr = buf->data_phys_addr + placement_offset;
1861469981b1SMichal Kalderon 	fpdu->pkt_hdr_size = pkt_data->tcp_payload_offset;
1862469981b1SMichal Kalderon 	fpdu->mpa_frag = buf->data_phys_addr + pkt_data->first_mpa_offset;
1863469981b1SMichal Kalderon 	fpdu->mpa_frag_virt = (u8 *)(buf->data) + pkt_data->first_mpa_offset;
1864469981b1SMichal Kalderon 
1865c7d1d839SMichal Kalderon 	if (tcp_payload_size == 1)
1866c7d1d839SMichal Kalderon 		fpdu->incomplete_bytes = QED_IWARP_INVALID_FPDU_LENGTH;
1867c7d1d839SMichal Kalderon 	else if (tcp_payload_size < fpdu->fpdu_length)
1868469981b1SMichal Kalderon 		fpdu->incomplete_bytes = fpdu->fpdu_length - tcp_payload_size;
1869469981b1SMichal Kalderon 	else
1870469981b1SMichal Kalderon 		fpdu->incomplete_bytes = 0;	/* complete fpdu */
1871469981b1SMichal Kalderon 
1872469981b1SMichal Kalderon 	fpdu->mpa_frag_len = fpdu->fpdu_length - fpdu->incomplete_bytes;
1873469981b1SMichal Kalderon }
1874469981b1SMichal Kalderon 
18751e28eaadSMichal Kalderon static int
18761e28eaadSMichal Kalderon qed_iwarp_cp_pkt(struct qed_hwfn *p_hwfn,
18771e28eaadSMichal Kalderon 		 struct qed_iwarp_fpdu *fpdu,
18781e28eaadSMichal Kalderon 		 struct unaligned_opaque_data *pkt_data,
18791e28eaadSMichal Kalderon 		 struct qed_iwarp_ll2_buff *buf, u16 tcp_payload_size)
18801e28eaadSMichal Kalderon {
18811e28eaadSMichal Kalderon 	u8 *tmp_buf = p_hwfn->p_rdma_info->iwarp.mpa_intermediate_buf;
18821e28eaadSMichal Kalderon 	int rc;
18831e28eaadSMichal Kalderon 
18841e28eaadSMichal Kalderon 	/* need to copy the data from the partial packet stored in fpdu
18851e28eaadSMichal Kalderon 	 * to the new buf, for this we also need to move the data currently
18861e28eaadSMichal Kalderon 	 * placed on the buf. The assumption is that the buffer is big enough
18871e28eaadSMichal Kalderon 	 * since fpdu_length <= mss, we use an intermediate buffer since
18881e28eaadSMichal Kalderon 	 * we may need to copy the new data to an overlapping location
18891e28eaadSMichal Kalderon 	 */
18901e28eaadSMichal Kalderon 	if ((fpdu->mpa_frag_len + tcp_payload_size) > (u16)buf->buff_size) {
18911e28eaadSMichal Kalderon 		DP_ERR(p_hwfn,
18921e28eaadSMichal Kalderon 		       "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",
18931e28eaadSMichal Kalderon 		       buf->buff_size, fpdu->mpa_frag_len,
18941e28eaadSMichal Kalderon 		       tcp_payload_size, fpdu->incomplete_bytes);
18951e28eaadSMichal Kalderon 		return -EINVAL;
18961e28eaadSMichal Kalderon 	}
18971e28eaadSMichal Kalderon 
18981e28eaadSMichal Kalderon 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
18991e28eaadSMichal Kalderon 		   "MPA ALIGN Copying fpdu: [%p, %d] [%p, %d]\n",
19001e28eaadSMichal Kalderon 		   fpdu->mpa_frag_virt, fpdu->mpa_frag_len,
19011e28eaadSMichal Kalderon 		   (u8 *)(buf->data) + pkt_data->first_mpa_offset,
19021e28eaadSMichal Kalderon 		   tcp_payload_size);
19031e28eaadSMichal Kalderon 
19041e28eaadSMichal Kalderon 	memcpy(tmp_buf, fpdu->mpa_frag_virt, fpdu->mpa_frag_len);
19051e28eaadSMichal Kalderon 	memcpy(tmp_buf + fpdu->mpa_frag_len,
19061e28eaadSMichal Kalderon 	       (u8 *)(buf->data) + pkt_data->first_mpa_offset,
19071e28eaadSMichal Kalderon 	       tcp_payload_size);
19081e28eaadSMichal Kalderon 
19091e28eaadSMichal Kalderon 	rc = qed_iwarp_recycle_pkt(p_hwfn, fpdu, fpdu->mpa_buf);
19101e28eaadSMichal Kalderon 	if (rc)
19111e28eaadSMichal Kalderon 		return rc;
19121e28eaadSMichal Kalderon 
19131e28eaadSMichal Kalderon 	/* If we managed to post the buffer copy the data to the new buffer
19141e28eaadSMichal Kalderon 	 * o/w this will occur in the next round...
19151e28eaadSMichal Kalderon 	 */
19161e28eaadSMichal Kalderon 	memcpy((u8 *)(buf->data), tmp_buf,
19171e28eaadSMichal Kalderon 	       fpdu->mpa_frag_len + tcp_payload_size);
19181e28eaadSMichal Kalderon 
19191e28eaadSMichal Kalderon 	fpdu->mpa_buf = buf;
19201e28eaadSMichal Kalderon 	/* fpdu->pkt_hdr remains as is */
19211e28eaadSMichal Kalderon 	/* fpdu->mpa_frag is overridden with new buf */
19221e28eaadSMichal Kalderon 	fpdu->mpa_frag = buf->data_phys_addr;
19231e28eaadSMichal Kalderon 	fpdu->mpa_frag_virt = buf->data;
19241e28eaadSMichal Kalderon 	fpdu->mpa_frag_len += tcp_payload_size;
19251e28eaadSMichal Kalderon 
19261e28eaadSMichal Kalderon 	fpdu->incomplete_bytes -= tcp_payload_size;
19271e28eaadSMichal Kalderon 
19281e28eaadSMichal Kalderon 	DP_VERBOSE(p_hwfn,
19291e28eaadSMichal Kalderon 		   QED_MSG_RDMA,
19301e28eaadSMichal Kalderon 		   "MPA ALIGN: split fpdu buff_size = %d mpa_frag_len = %d, tcp_payload_size = %d, incomplete_bytes = %d\n",
19311e28eaadSMichal Kalderon 		   buf->buff_size, fpdu->mpa_frag_len, tcp_payload_size,
19321e28eaadSMichal Kalderon 		   fpdu->incomplete_bytes);
19331e28eaadSMichal Kalderon 
19341e28eaadSMichal Kalderon 	return 0;
19351e28eaadSMichal Kalderon }
19361e28eaadSMichal Kalderon 
1937c7d1d839SMichal Kalderon static void
1938c7d1d839SMichal Kalderon qed_iwarp_update_fpdu_length(struct qed_hwfn *p_hwfn,
1939c7d1d839SMichal Kalderon 			     struct qed_iwarp_fpdu *fpdu, u8 *mpa_data)
1940c7d1d839SMichal Kalderon {
1941c7d1d839SMichal Kalderon 	u16 mpa_len;
1942c7d1d839SMichal Kalderon 
1943c7d1d839SMichal Kalderon 	/* Update incomplete packets if needed */
1944c7d1d839SMichal Kalderon 	if (fpdu->incomplete_bytes == QED_IWARP_INVALID_FPDU_LENGTH) {
1945c7d1d839SMichal Kalderon 		/* Missing lower byte is now available */
1946c7d1d839SMichal Kalderon 		mpa_len = fpdu->fpdu_length | *mpa_data;
1947c7d1d839SMichal Kalderon 		fpdu->fpdu_length = QED_IWARP_FPDU_LEN_WITH_PAD(mpa_len);
1948c7d1d839SMichal Kalderon 		/* one byte of hdr */
1949933e8c91SMichal Kalderon 		fpdu->mpa_frag_len = 1;
1950c7d1d839SMichal Kalderon 		fpdu->incomplete_bytes = fpdu->fpdu_length - 1;
1951c7d1d839SMichal Kalderon 		DP_VERBOSE(p_hwfn,
1952c7d1d839SMichal Kalderon 			   QED_MSG_RDMA,
1953c7d1d839SMichal Kalderon 			   "MPA_ALIGN: Partial header mpa_len=%x fpdu_length=%x incomplete_bytes=%x\n",
1954c7d1d839SMichal Kalderon 			   mpa_len, fpdu->fpdu_length, fpdu->incomplete_bytes);
1955c7d1d839SMichal Kalderon 	}
1956c7d1d839SMichal Kalderon }
1957c7d1d839SMichal Kalderon 
19581e28eaadSMichal Kalderon #define QED_IWARP_IS_RIGHT_EDGE(_curr_pkt) \
19591e28eaadSMichal Kalderon 	(GET_FIELD((_curr_pkt)->flags,	   \
19601e28eaadSMichal Kalderon 		   UNALIGNED_OPAQUE_DATA_PKT_REACHED_WIN_RIGHT_EDGE))
19611e28eaadSMichal Kalderon 
19621e28eaadSMichal Kalderon /* This function is used to recycle a buffer using the ll2 drop option. It
19631e28eaadSMichal Kalderon  * uses the mechanism to ensure that all buffers posted to tx before this one
19641e28eaadSMichal Kalderon  * were completed. The buffer sent here will be sent as a cookie in the tx
19651e28eaadSMichal Kalderon  * completion function and can then be reposted to rx chain when done. The flow
19661e28eaadSMichal Kalderon  * that requires this is the flow where a FPDU splits over more than 3 tcp
19671e28eaadSMichal Kalderon  * segments. In this case the driver needs to re-post a rx buffer instead of
19681e28eaadSMichal Kalderon  * the one received, but driver can't simply repost a buffer it copied from
19691e28eaadSMichal Kalderon  * as there is a case where the buffer was originally a packed FPDU, and is
19701e28eaadSMichal Kalderon  * partially posted to FW. Driver needs to ensure FW is done with it.
19711e28eaadSMichal Kalderon  */
19721e28eaadSMichal Kalderon static int
19731e28eaadSMichal Kalderon qed_iwarp_recycle_pkt(struct qed_hwfn *p_hwfn,
19741e28eaadSMichal Kalderon 		      struct qed_iwarp_fpdu *fpdu,
19751e28eaadSMichal Kalderon 		      struct qed_iwarp_ll2_buff *buf)
19761e28eaadSMichal Kalderon {
19771e28eaadSMichal Kalderon 	struct qed_ll2_tx_pkt_info tx_pkt;
19781e28eaadSMichal Kalderon 	u8 ll2_handle;
19791e28eaadSMichal Kalderon 	int rc;
19801e28eaadSMichal Kalderon 
19811e28eaadSMichal Kalderon 	memset(&tx_pkt, 0, sizeof(tx_pkt));
19821e28eaadSMichal Kalderon 	tx_pkt.num_of_bds = 1;
19831e28eaadSMichal Kalderon 	tx_pkt.tx_dest = QED_LL2_TX_DEST_DROP;
19841e28eaadSMichal Kalderon 	tx_pkt.l4_hdr_offset_w = fpdu->pkt_hdr_size >> 2;
19851e28eaadSMichal Kalderon 	tx_pkt.first_frag = fpdu->pkt_hdr;
19861e28eaadSMichal Kalderon 	tx_pkt.first_frag_len = fpdu->pkt_hdr_size;
19871e28eaadSMichal Kalderon 	buf->piggy_buf = NULL;
19881e28eaadSMichal Kalderon 	tx_pkt.cookie = buf;
19891e28eaadSMichal Kalderon 
19901e28eaadSMichal Kalderon 	ll2_handle = p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle;
19911e28eaadSMichal Kalderon 
19921e28eaadSMichal Kalderon 	rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_handle, &tx_pkt, true);
19931e28eaadSMichal Kalderon 	if (rc)
19941e28eaadSMichal Kalderon 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
19951e28eaadSMichal Kalderon 			   "Can't drop packet rc=%d\n", rc);
19961e28eaadSMichal Kalderon 
19971e28eaadSMichal Kalderon 	DP_VERBOSE(p_hwfn,
19981e28eaadSMichal Kalderon 		   QED_MSG_RDMA,
19991e28eaadSMichal Kalderon 		   "MPA_ALIGN: send drop tx packet [%lx, 0x%x], buf=%p, rc=%d\n",
20001e28eaadSMichal Kalderon 		   (unsigned long int)tx_pkt.first_frag,
20011e28eaadSMichal Kalderon 		   tx_pkt.first_frag_len, buf, rc);
20021e28eaadSMichal Kalderon 
20031e28eaadSMichal Kalderon 	return rc;
20041e28eaadSMichal Kalderon }
20051e28eaadSMichal Kalderon 
20061e28eaadSMichal Kalderon static int
20071e28eaadSMichal Kalderon qed_iwarp_win_right_edge(struct qed_hwfn *p_hwfn, struct qed_iwarp_fpdu *fpdu)
20081e28eaadSMichal Kalderon {
20091e28eaadSMichal Kalderon 	struct qed_ll2_tx_pkt_info tx_pkt;
20101e28eaadSMichal Kalderon 	u8 ll2_handle;
20111e28eaadSMichal Kalderon 	int rc;
20121e28eaadSMichal Kalderon 
20131e28eaadSMichal Kalderon 	memset(&tx_pkt, 0, sizeof(tx_pkt));
20141e28eaadSMichal Kalderon 	tx_pkt.num_of_bds = 1;
20151e28eaadSMichal Kalderon 	tx_pkt.tx_dest = QED_LL2_TX_DEST_LB;
20161e28eaadSMichal Kalderon 	tx_pkt.l4_hdr_offset_w = fpdu->pkt_hdr_size >> 2;
20171e28eaadSMichal Kalderon 
20181e28eaadSMichal Kalderon 	tx_pkt.first_frag = fpdu->pkt_hdr;
20191e28eaadSMichal Kalderon 	tx_pkt.first_frag_len = fpdu->pkt_hdr_size;
20201e28eaadSMichal Kalderon 	tx_pkt.enable_ip_cksum = true;
20211e28eaadSMichal Kalderon 	tx_pkt.enable_l4_cksum = true;
20221e28eaadSMichal Kalderon 	tx_pkt.calc_ip_len = true;
20231e28eaadSMichal Kalderon 	/* vlan overload with enum iwarp_ll2_tx_queues */
20241e28eaadSMichal Kalderon 	tx_pkt.vlan = IWARP_LL2_ALIGNED_RIGHT_TRIMMED_TX_QUEUE;
20251e28eaadSMichal Kalderon 
20261e28eaadSMichal Kalderon 	ll2_handle = p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle;
20271e28eaadSMichal Kalderon 
20281e28eaadSMichal Kalderon 	rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_handle, &tx_pkt, true);
20291e28eaadSMichal Kalderon 	if (rc)
20301e28eaadSMichal Kalderon 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
20311e28eaadSMichal Kalderon 			   "Can't send right edge rc=%d\n", rc);
20321e28eaadSMichal Kalderon 	DP_VERBOSE(p_hwfn,
20331e28eaadSMichal Kalderon 		   QED_MSG_RDMA,
20341e28eaadSMichal Kalderon 		   "MPA_ALIGN: Sent right edge FPDU num_bds=%d [%lx, 0x%x], rc=%d\n",
20351e28eaadSMichal Kalderon 		   tx_pkt.num_of_bds,
20361e28eaadSMichal Kalderon 		   (unsigned long int)tx_pkt.first_frag,
20371e28eaadSMichal Kalderon 		   tx_pkt.first_frag_len, rc);
20381e28eaadSMichal Kalderon 
20391e28eaadSMichal Kalderon 	return rc;
20401e28eaadSMichal Kalderon }
20411e28eaadSMichal Kalderon 
2042469981b1SMichal Kalderon static int
2043469981b1SMichal Kalderon qed_iwarp_send_fpdu(struct qed_hwfn *p_hwfn,
2044469981b1SMichal Kalderon 		    struct qed_iwarp_fpdu *fpdu,
2045469981b1SMichal Kalderon 		    struct unaligned_opaque_data *curr_pkt,
2046469981b1SMichal Kalderon 		    struct qed_iwarp_ll2_buff *buf,
2047469981b1SMichal Kalderon 		    u16 tcp_payload_size, enum qed_iwarp_mpa_pkt_type pkt_type)
2048469981b1SMichal Kalderon {
2049469981b1SMichal Kalderon 	struct qed_ll2_tx_pkt_info tx_pkt;
2050469981b1SMichal Kalderon 	u8 ll2_handle;
2051469981b1SMichal Kalderon 	int rc;
2052469981b1SMichal Kalderon 
2053469981b1SMichal Kalderon 	memset(&tx_pkt, 0, sizeof(tx_pkt));
2054469981b1SMichal Kalderon 
2055469981b1SMichal Kalderon 	/* An unaligned packet means it's split over two tcp segments. So the
2056469981b1SMichal Kalderon 	 * complete packet requires 3 bds, one for the header, one for the
2057469981b1SMichal Kalderon 	 * part of the fpdu of the first tcp segment, and the last fragment
2058469981b1SMichal Kalderon 	 * will point to the remainder of the fpdu. A packed pdu, requires only
2059469981b1SMichal Kalderon 	 * two bds, one for the header and one for the data.
2060469981b1SMichal Kalderon 	 */
2061469981b1SMichal Kalderon 	tx_pkt.num_of_bds = (pkt_type == QED_IWARP_MPA_PKT_UNALIGNED) ? 3 : 2;
2062469981b1SMichal Kalderon 	tx_pkt.tx_dest = QED_LL2_TX_DEST_LB;
2063469981b1SMichal Kalderon 	tx_pkt.l4_hdr_offset_w = fpdu->pkt_hdr_size >> 2; /* offset in words */
2064469981b1SMichal Kalderon 
2065469981b1SMichal Kalderon 	/* Send the mpa_buf only with the last fpdu (in case of packed) */
2066469981b1SMichal Kalderon 	if (pkt_type == QED_IWARP_MPA_PKT_UNALIGNED ||
2067469981b1SMichal Kalderon 	    tcp_payload_size <= fpdu->fpdu_length)
2068469981b1SMichal Kalderon 		tx_pkt.cookie = fpdu->mpa_buf;
2069469981b1SMichal Kalderon 
2070469981b1SMichal Kalderon 	tx_pkt.first_frag = fpdu->pkt_hdr;
2071469981b1SMichal Kalderon 	tx_pkt.first_frag_len = fpdu->pkt_hdr_size;
2072469981b1SMichal Kalderon 	tx_pkt.enable_ip_cksum = true;
2073469981b1SMichal Kalderon 	tx_pkt.enable_l4_cksum = true;
2074469981b1SMichal Kalderon 	tx_pkt.calc_ip_len = true;
2075469981b1SMichal Kalderon 	/* vlan overload with enum iwarp_ll2_tx_queues */
2076469981b1SMichal Kalderon 	tx_pkt.vlan = IWARP_LL2_ALIGNED_TX_QUEUE;
2077469981b1SMichal Kalderon 
2078d531038eSMichal Kalderon 	/* special case of unaligned packet and not packed, need to send
2079d531038eSMichal Kalderon 	 * both buffers as cookie to release.
2080d531038eSMichal Kalderon 	 */
2081d531038eSMichal Kalderon 	if (tcp_payload_size == fpdu->incomplete_bytes)
2082d531038eSMichal Kalderon 		fpdu->mpa_buf->piggy_buf = buf;
2083d531038eSMichal Kalderon 
2084469981b1SMichal Kalderon 	ll2_handle = p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle;
2085469981b1SMichal Kalderon 
2086469981b1SMichal Kalderon 	/* Set first fragment to header */
2087469981b1SMichal Kalderon 	rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_handle, &tx_pkt, true);
2088469981b1SMichal Kalderon 	if (rc)
2089469981b1SMichal Kalderon 		goto out;
2090469981b1SMichal Kalderon 
2091469981b1SMichal Kalderon 	/* Set second fragment to first part of packet */
2092469981b1SMichal Kalderon 	rc = qed_ll2_set_fragment_of_tx_packet(p_hwfn, ll2_handle,
2093469981b1SMichal Kalderon 					       fpdu->mpa_frag,
2094469981b1SMichal Kalderon 					       fpdu->mpa_frag_len);
2095469981b1SMichal Kalderon 	if (rc)
2096469981b1SMichal Kalderon 		goto out;
2097469981b1SMichal Kalderon 
2098469981b1SMichal Kalderon 	if (!fpdu->incomplete_bytes)
2099469981b1SMichal Kalderon 		goto out;
2100469981b1SMichal Kalderon 
2101469981b1SMichal Kalderon 	/* Set third fragment to second part of the packet */
2102469981b1SMichal Kalderon 	rc = qed_ll2_set_fragment_of_tx_packet(p_hwfn,
2103469981b1SMichal Kalderon 					       ll2_handle,
2104469981b1SMichal Kalderon 					       buf->data_phys_addr +
2105469981b1SMichal Kalderon 					       curr_pkt->first_mpa_offset,
2106469981b1SMichal Kalderon 					       fpdu->incomplete_bytes);
2107469981b1SMichal Kalderon out:
2108469981b1SMichal Kalderon 	DP_VERBOSE(p_hwfn,
2109469981b1SMichal Kalderon 		   QED_MSG_RDMA,
2110469981b1SMichal Kalderon 		   "MPA_ALIGN: Sent FPDU num_bds=%d first_frag_len=%x, mpa_frag_len=0x%x, incomplete_bytes:0x%x rc=%d\n",
2111469981b1SMichal Kalderon 		   tx_pkt.num_of_bds,
2112469981b1SMichal Kalderon 		   tx_pkt.first_frag_len,
2113469981b1SMichal Kalderon 		   fpdu->mpa_frag_len,
2114469981b1SMichal Kalderon 		   fpdu->incomplete_bytes, rc);
2115469981b1SMichal Kalderon 
2116469981b1SMichal Kalderon 	return rc;
2117469981b1SMichal Kalderon }
2118469981b1SMichal Kalderon 
2119ae3488ffSMichal Kalderon static void
2120fcb39f6cSMichal Kalderon qed_iwarp_mpa_get_data(struct qed_hwfn *p_hwfn,
2121fcb39f6cSMichal Kalderon 		       struct unaligned_opaque_data *curr_pkt,
2122fcb39f6cSMichal Kalderon 		       u32 opaque_data0, u32 opaque_data1)
2123fcb39f6cSMichal Kalderon {
2124fcb39f6cSMichal Kalderon 	u64 opaque_data;
2125fcb39f6cSMichal Kalderon 
2126fcb39f6cSMichal Kalderon 	opaque_data = HILO_64(opaque_data1, opaque_data0);
2127fcb39f6cSMichal Kalderon 	*curr_pkt = *((struct unaligned_opaque_data *)&opaque_data);
2128fcb39f6cSMichal Kalderon 
2129fcb39f6cSMichal Kalderon 	curr_pkt->first_mpa_offset = curr_pkt->tcp_payload_offset +
2130fcb39f6cSMichal Kalderon 				     le16_to_cpu(curr_pkt->first_mpa_offset);
2131fcb39f6cSMichal Kalderon 	curr_pkt->cid = le32_to_cpu(curr_pkt->cid);
2132fcb39f6cSMichal Kalderon }
2133fcb39f6cSMichal Kalderon 
2134fcb39f6cSMichal Kalderon /* This function is called when an unaligned or incomplete MPA packet arrives
2135fcb39f6cSMichal Kalderon  * driver needs to align the packet, perhaps using previous data and send
2136fcb39f6cSMichal Kalderon  * it down to FW once it is aligned.
2137fcb39f6cSMichal Kalderon  */
2138fcb39f6cSMichal Kalderon static int
2139fcb39f6cSMichal Kalderon qed_iwarp_process_mpa_pkt(struct qed_hwfn *p_hwfn,
2140fcb39f6cSMichal Kalderon 			  struct qed_iwarp_ll2_mpa_buf *mpa_buf)
2141fcb39f6cSMichal Kalderon {
2142469981b1SMichal Kalderon 	struct unaligned_opaque_data *curr_pkt = &mpa_buf->data;
2143fcb39f6cSMichal Kalderon 	struct qed_iwarp_ll2_buff *buf = mpa_buf->ll2_buf;
2144469981b1SMichal Kalderon 	enum qed_iwarp_mpa_pkt_type pkt_type;
2145469981b1SMichal Kalderon 	struct qed_iwarp_fpdu *fpdu;
2146fcb39f6cSMichal Kalderon 	int rc = -EINVAL;
2147469981b1SMichal Kalderon 	u8 *mpa_data;
2148fcb39f6cSMichal Kalderon 
2149469981b1SMichal Kalderon 	fpdu = qed_iwarp_get_curr_fpdu(p_hwfn, curr_pkt->cid & 0xffff);
2150469981b1SMichal Kalderon 	if (!fpdu) { /* something corrupt with cid, post rx back */
2151469981b1SMichal Kalderon 		DP_ERR(p_hwfn, "Invalid cid, drop and post back to rx cid=%x\n",
2152469981b1SMichal Kalderon 		       curr_pkt->cid);
2153469981b1SMichal Kalderon 		goto err;
2154469981b1SMichal Kalderon 	}
2155469981b1SMichal Kalderon 
2156469981b1SMichal Kalderon 	do {
2157469981b1SMichal Kalderon 		mpa_data = ((u8 *)(buf->data) + curr_pkt->first_mpa_offset);
2158469981b1SMichal Kalderon 
2159469981b1SMichal Kalderon 		pkt_type = qed_iwarp_mpa_classify(p_hwfn, fpdu,
2160469981b1SMichal Kalderon 						  mpa_buf->tcp_payload_len,
2161469981b1SMichal Kalderon 						  mpa_data);
2162469981b1SMichal Kalderon 
2163469981b1SMichal Kalderon 		switch (pkt_type) {
2164469981b1SMichal Kalderon 		case QED_IWARP_MPA_PKT_PARTIAL:
2165469981b1SMichal Kalderon 			qed_iwarp_init_fpdu(buf, fpdu,
2166469981b1SMichal Kalderon 					    curr_pkt,
2167469981b1SMichal Kalderon 					    mpa_buf->tcp_payload_len,
2168469981b1SMichal Kalderon 					    mpa_buf->placement_offset);
2169469981b1SMichal Kalderon 
21701e28eaadSMichal Kalderon 			if (!QED_IWARP_IS_RIGHT_EDGE(curr_pkt)) {
21711e28eaadSMichal Kalderon 				mpa_buf->tcp_payload_len = 0;
21721e28eaadSMichal Kalderon 				break;
21731e28eaadSMichal Kalderon 			}
21741e28eaadSMichal Kalderon 
21751e28eaadSMichal Kalderon 			rc = qed_iwarp_win_right_edge(p_hwfn, fpdu);
21761e28eaadSMichal Kalderon 
21771e28eaadSMichal Kalderon 			if (rc) {
21781e28eaadSMichal Kalderon 				DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
21791e28eaadSMichal Kalderon 					   "Can't send FPDU:reset rc=%d\n", rc);
21801e28eaadSMichal Kalderon 				memset(fpdu, 0, sizeof(*fpdu));
21811e28eaadSMichal Kalderon 				break;
21821e28eaadSMichal Kalderon 			}
21831e28eaadSMichal Kalderon 
2184469981b1SMichal Kalderon 			mpa_buf->tcp_payload_len = 0;
2185469981b1SMichal Kalderon 			break;
2186469981b1SMichal Kalderon 		case QED_IWARP_MPA_PKT_PACKED:
2187469981b1SMichal Kalderon 			qed_iwarp_init_fpdu(buf, fpdu,
2188469981b1SMichal Kalderon 					    curr_pkt,
2189469981b1SMichal Kalderon 					    mpa_buf->tcp_payload_len,
2190469981b1SMichal Kalderon 					    mpa_buf->placement_offset);
2191469981b1SMichal Kalderon 
2192469981b1SMichal Kalderon 			rc = qed_iwarp_send_fpdu(p_hwfn, fpdu, curr_pkt, buf,
2193469981b1SMichal Kalderon 						 mpa_buf->tcp_payload_len,
2194469981b1SMichal Kalderon 						 pkt_type);
2195469981b1SMichal Kalderon 			if (rc) {
2196469981b1SMichal Kalderon 				DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2197469981b1SMichal Kalderon 					   "Can't send FPDU:reset rc=%d\n", rc);
2198469981b1SMichal Kalderon 				memset(fpdu, 0, sizeof(*fpdu));
2199469981b1SMichal Kalderon 				break;
2200469981b1SMichal Kalderon 			}
2201469981b1SMichal Kalderon 
2202469981b1SMichal Kalderon 			mpa_buf->tcp_payload_len -= fpdu->fpdu_length;
2203469981b1SMichal Kalderon 			curr_pkt->first_mpa_offset += fpdu->fpdu_length;
2204469981b1SMichal Kalderon 			break;
2205469981b1SMichal Kalderon 		case QED_IWARP_MPA_PKT_UNALIGNED:
2206c7d1d839SMichal Kalderon 			qed_iwarp_update_fpdu_length(p_hwfn, fpdu, mpa_data);
22071e28eaadSMichal Kalderon 			if (mpa_buf->tcp_payload_len < fpdu->incomplete_bytes) {
22081e28eaadSMichal Kalderon 				/* special handling of fpdu split over more
22091e28eaadSMichal Kalderon 				 * than 2 segments
22101e28eaadSMichal Kalderon 				 */
22111e28eaadSMichal Kalderon 				if (QED_IWARP_IS_RIGHT_EDGE(curr_pkt)) {
22121e28eaadSMichal Kalderon 					rc = qed_iwarp_win_right_edge(p_hwfn,
22131e28eaadSMichal Kalderon 								      fpdu);
22141e28eaadSMichal Kalderon 					/* packet will be re-processed later */
22151e28eaadSMichal Kalderon 					if (rc)
22161e28eaadSMichal Kalderon 						return rc;
22171e28eaadSMichal Kalderon 				}
22181e28eaadSMichal Kalderon 
22191e28eaadSMichal Kalderon 				rc = qed_iwarp_cp_pkt(p_hwfn, fpdu, curr_pkt,
22201e28eaadSMichal Kalderon 						      buf,
22211e28eaadSMichal Kalderon 						      mpa_buf->tcp_payload_len);
22221e28eaadSMichal Kalderon 				if (rc) /* packet will be re-processed later */
22231e28eaadSMichal Kalderon 					return rc;
22241e28eaadSMichal Kalderon 
22251e28eaadSMichal Kalderon 				mpa_buf->tcp_payload_len = 0;
22261e28eaadSMichal Kalderon 				break;
22271e28eaadSMichal Kalderon 			}
22281e28eaadSMichal Kalderon 
2229469981b1SMichal Kalderon 			rc = qed_iwarp_send_fpdu(p_hwfn, fpdu, curr_pkt, buf,
2230469981b1SMichal Kalderon 						 mpa_buf->tcp_payload_len,
2231469981b1SMichal Kalderon 						 pkt_type);
2232469981b1SMichal Kalderon 			if (rc) {
2233469981b1SMichal Kalderon 				DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2234469981b1SMichal Kalderon 					   "Can't send FPDU:delay rc=%d\n", rc);
2235469981b1SMichal Kalderon 				/* don't reset fpdu -> we need it for next
2236469981b1SMichal Kalderon 				 * classify
2237469981b1SMichal Kalderon 				 */
2238469981b1SMichal Kalderon 				break;
2239469981b1SMichal Kalderon 			}
2240469981b1SMichal Kalderon 
2241469981b1SMichal Kalderon 			mpa_buf->tcp_payload_len -= fpdu->incomplete_bytes;
2242469981b1SMichal Kalderon 			curr_pkt->first_mpa_offset += fpdu->incomplete_bytes;
2243469981b1SMichal Kalderon 			/* The framed PDU was sent - no more incomplete bytes */
2244469981b1SMichal Kalderon 			fpdu->incomplete_bytes = 0;
2245469981b1SMichal Kalderon 			break;
2246469981b1SMichal Kalderon 		}
2247469981b1SMichal Kalderon 	} while (mpa_buf->tcp_payload_len && !rc);
2248469981b1SMichal Kalderon 
2249469981b1SMichal Kalderon 	return rc;
2250469981b1SMichal Kalderon 
2251469981b1SMichal Kalderon err:
2252fcb39f6cSMichal Kalderon 	qed_iwarp_ll2_post_rx(p_hwfn,
2253fcb39f6cSMichal Kalderon 			      buf,
2254fcb39f6cSMichal Kalderon 			      p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle);
2255fcb39f6cSMichal Kalderon 	return rc;
2256fcb39f6cSMichal Kalderon }
2257fcb39f6cSMichal Kalderon 
2258fcb39f6cSMichal Kalderon static void qed_iwarp_process_pending_pkts(struct qed_hwfn *p_hwfn)
2259fcb39f6cSMichal Kalderon {
2260fcb39f6cSMichal Kalderon 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2261fcb39f6cSMichal Kalderon 	struct qed_iwarp_ll2_mpa_buf *mpa_buf = NULL;
2262fcb39f6cSMichal Kalderon 	int rc;
2263fcb39f6cSMichal Kalderon 
2264fcb39f6cSMichal Kalderon 	while (!list_empty(&iwarp_info->mpa_buf_pending_list)) {
2265fcb39f6cSMichal Kalderon 		mpa_buf = list_first_entry(&iwarp_info->mpa_buf_pending_list,
2266fcb39f6cSMichal Kalderon 					   struct qed_iwarp_ll2_mpa_buf,
2267fcb39f6cSMichal Kalderon 					   list_entry);
2268fcb39f6cSMichal Kalderon 
2269fcb39f6cSMichal Kalderon 		rc = qed_iwarp_process_mpa_pkt(p_hwfn, mpa_buf);
2270fcb39f6cSMichal Kalderon 
2271fcb39f6cSMichal Kalderon 		/* busy means break and continue processing later, don't
2272fcb39f6cSMichal Kalderon 		 * remove the buf from the pending list.
2273fcb39f6cSMichal Kalderon 		 */
2274fcb39f6cSMichal Kalderon 		if (rc == -EBUSY)
2275fcb39f6cSMichal Kalderon 			break;
2276fcb39f6cSMichal Kalderon 
2277fcb39f6cSMichal Kalderon 		list_del(&mpa_buf->list_entry);
2278fcb39f6cSMichal Kalderon 		list_add_tail(&mpa_buf->list_entry, &iwarp_info->mpa_buf_list);
2279fcb39f6cSMichal Kalderon 
2280fcb39f6cSMichal Kalderon 		if (rc) {	/* different error, don't continue */
2281fcb39f6cSMichal Kalderon 			DP_NOTICE(p_hwfn, "process pkts failed rc=%d\n", rc);
2282fcb39f6cSMichal Kalderon 			break;
2283fcb39f6cSMichal Kalderon 		}
2284fcb39f6cSMichal Kalderon 	}
2285fcb39f6cSMichal Kalderon }
2286fcb39f6cSMichal Kalderon 
2287fcb39f6cSMichal Kalderon static void
2288ae3488ffSMichal Kalderon qed_iwarp_ll2_comp_mpa_pkt(void *cxt, struct qed_ll2_comp_rx_data *data)
2289ae3488ffSMichal Kalderon {
2290fcb39f6cSMichal Kalderon 	struct qed_iwarp_ll2_mpa_buf *mpa_buf;
2291ae3488ffSMichal Kalderon 	struct qed_iwarp_info *iwarp_info;
2292ae3488ffSMichal Kalderon 	struct qed_hwfn *p_hwfn = cxt;
2293ae3488ffSMichal Kalderon 
2294ae3488ffSMichal Kalderon 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2295fcb39f6cSMichal Kalderon 	mpa_buf = list_first_entry(&iwarp_info->mpa_buf_list,
2296fcb39f6cSMichal Kalderon 				   struct qed_iwarp_ll2_mpa_buf, list_entry);
2297fcb39f6cSMichal Kalderon 	if (!mpa_buf) {
2298fcb39f6cSMichal Kalderon 		DP_ERR(p_hwfn, "No free mpa buf\n");
2299fcb39f6cSMichal Kalderon 		goto err;
2300fcb39f6cSMichal Kalderon 	}
2301fcb39f6cSMichal Kalderon 
2302fcb39f6cSMichal Kalderon 	list_del(&mpa_buf->list_entry);
2303fcb39f6cSMichal Kalderon 	qed_iwarp_mpa_get_data(p_hwfn, &mpa_buf->data,
2304fcb39f6cSMichal Kalderon 			       data->opaque_data_0, data->opaque_data_1);
2305fcb39f6cSMichal Kalderon 
2306fcb39f6cSMichal Kalderon 	DP_VERBOSE(p_hwfn,
2307fcb39f6cSMichal Kalderon 		   QED_MSG_RDMA,
2308fcb39f6cSMichal Kalderon 		   "LL2 MPA CompRx payload_len:0x%x\tfirst_mpa_offset:0x%x\ttcp_payload_offset:0x%x\tflags:0x%x\tcid:0x%x\n",
2309fcb39f6cSMichal Kalderon 		   data->length.packet_length, mpa_buf->data.first_mpa_offset,
2310fcb39f6cSMichal Kalderon 		   mpa_buf->data.tcp_payload_offset, mpa_buf->data.flags,
2311fcb39f6cSMichal Kalderon 		   mpa_buf->data.cid);
2312fcb39f6cSMichal Kalderon 
2313fcb39f6cSMichal Kalderon 	mpa_buf->ll2_buf = data->cookie;
2314fcb39f6cSMichal Kalderon 	mpa_buf->tcp_payload_len = data->length.packet_length -
2315fcb39f6cSMichal Kalderon 				   mpa_buf->data.first_mpa_offset;
2316fcb39f6cSMichal Kalderon 	mpa_buf->data.first_mpa_offset += data->u.placement_offset;
2317fcb39f6cSMichal Kalderon 	mpa_buf->placement_offset = data->u.placement_offset;
2318fcb39f6cSMichal Kalderon 
2319fcb39f6cSMichal Kalderon 	list_add_tail(&mpa_buf->list_entry, &iwarp_info->mpa_buf_pending_list);
2320fcb39f6cSMichal Kalderon 
2321fcb39f6cSMichal Kalderon 	qed_iwarp_process_pending_pkts(p_hwfn);
2322fcb39f6cSMichal Kalderon 	return;
2323fcb39f6cSMichal Kalderon err:
2324ae3488ffSMichal Kalderon 	qed_iwarp_ll2_post_rx(p_hwfn, data->cookie,
2325ae3488ffSMichal Kalderon 			      iwarp_info->ll2_mpa_handle);
2326ae3488ffSMichal Kalderon }
2327ae3488ffSMichal Kalderon 
2328b5c29ca7SKalderon, Michal static void
2329b5c29ca7SKalderon, Michal qed_iwarp_ll2_comp_syn_pkt(void *cxt, struct qed_ll2_comp_rx_data *data)
2330b5c29ca7SKalderon, Michal {
2331b5c29ca7SKalderon, Michal 	struct qed_iwarp_ll2_buff *buf = data->cookie;
233265a91a6cSKalderon, Michal 	struct qed_iwarp_listener *listener;
233365a91a6cSKalderon, Michal 	struct qed_ll2_tx_pkt_info tx_pkt;
233465a91a6cSKalderon, Michal 	struct qed_iwarp_cm_info cm_info;
2335b5c29ca7SKalderon, Michal 	struct qed_hwfn *p_hwfn = cxt;
233665a91a6cSKalderon, Michal 	u8 remote_mac_addr[ETH_ALEN];
233765a91a6cSKalderon, Michal 	u8 local_mac_addr[ETH_ALEN];
2338456a5849SKalderon, Michal 	struct qed_iwarp_ep *ep;
233965a91a6cSKalderon, Michal 	int tcp_start_offset;
2340456a5849SKalderon, Michal 	u8 ts_hdr_size = 0;
234165a91a6cSKalderon, Michal 	u8 ll2_syn_handle;
234265a91a6cSKalderon, Michal 	int payload_len;
2343456a5849SKalderon, Michal 	u32 hdr_size;
234465a91a6cSKalderon, Michal 	int rc;
234565a91a6cSKalderon, Michal 
234665a91a6cSKalderon, Michal 	memset(&cm_info, 0, sizeof(cm_info));
234725f4535aSMichal Kalderon 	ll2_syn_handle = p_hwfn->p_rdma_info->iwarp.ll2_syn_handle;
23481e99c497SMichal Kalderon 
23491e99c497SMichal Kalderon 	/* Check if packet was received with errors... */
23501e99c497SMichal Kalderon 	if (data->err_flags) {
23511e99c497SMichal Kalderon 		DP_NOTICE(p_hwfn, "Error received on SYN packet: 0x%x\n",
23521e99c497SMichal Kalderon 			  data->err_flags);
23531e99c497SMichal Kalderon 		goto err;
23541e99c497SMichal Kalderon 	}
23551e99c497SMichal Kalderon 
2356b5c29ca7SKalderon, Michal 	if (GET_FIELD(data->parse_flags,
2357b5c29ca7SKalderon, Michal 		      PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED) &&
2358b5c29ca7SKalderon, Michal 	    GET_FIELD(data->parse_flags, PARSING_AND_ERR_FLAGS_L4CHKSMERROR)) {
2359b5c29ca7SKalderon, Michal 		DP_NOTICE(p_hwfn, "Syn packet received with checksum error\n");
2360b5c29ca7SKalderon, Michal 		goto err;
2361b5c29ca7SKalderon, Michal 	}
2362b5c29ca7SKalderon, Michal 
236365a91a6cSKalderon, Michal 	rc = qed_iwarp_parse_rx_pkt(p_hwfn, &cm_info, (u8 *)(buf->data) +
236465a91a6cSKalderon, Michal 				    data->u.placement_offset, remote_mac_addr,
236565a91a6cSKalderon, Michal 				    local_mac_addr, &payload_len,
236665a91a6cSKalderon, Michal 				    &tcp_start_offset);
236765a91a6cSKalderon, Michal 	if (rc)
236865a91a6cSKalderon, Michal 		goto err;
2369b5c29ca7SKalderon, Michal 
237065a91a6cSKalderon, Michal 	/* Check if there is a listener for this 4-tuple+vlan */
237165a91a6cSKalderon, Michal 	listener = qed_iwarp_get_listener(p_hwfn, &cm_info);
237265a91a6cSKalderon, Michal 	if (!listener) {
237365a91a6cSKalderon, Michal 		DP_VERBOSE(p_hwfn,
237465a91a6cSKalderon, Michal 			   QED_MSG_RDMA,
237565a91a6cSKalderon, Michal 			   "SYN received on tuple not listened on parse_flags=%d packet len=%d\n",
237665a91a6cSKalderon, Michal 			   data->parse_flags, data->length.packet_length);
237765a91a6cSKalderon, Michal 
237865a91a6cSKalderon, Michal 		memset(&tx_pkt, 0, sizeof(tx_pkt));
237965a91a6cSKalderon, Michal 		tx_pkt.num_of_bds = 1;
238065a91a6cSKalderon, Michal 		tx_pkt.l4_hdr_offset_w = (data->length.packet_length) >> 2;
238165a91a6cSKalderon, Michal 		tx_pkt.tx_dest = QED_LL2_TX_DEST_LB;
238265a91a6cSKalderon, Michal 		tx_pkt.first_frag = buf->data_phys_addr +
238365a91a6cSKalderon, Michal 				    data->u.placement_offset;
238465a91a6cSKalderon, Michal 		tx_pkt.first_frag_len = data->length.packet_length;
238565a91a6cSKalderon, Michal 		tx_pkt.cookie = buf;
238665a91a6cSKalderon, Michal 
238765a91a6cSKalderon, Michal 		rc = qed_ll2_prepare_tx_packet(p_hwfn, ll2_syn_handle,
238865a91a6cSKalderon, Michal 					       &tx_pkt, true);
238965a91a6cSKalderon, Michal 
239065a91a6cSKalderon, Michal 		if (rc) {
239165a91a6cSKalderon, Michal 			DP_NOTICE(p_hwfn,
239265a91a6cSKalderon, Michal 				  "Can't post SYN back to chip rc=%d\n", rc);
239365a91a6cSKalderon, Michal 			goto err;
239465a91a6cSKalderon, Michal 		}
239565a91a6cSKalderon, Michal 		return;
239665a91a6cSKalderon, Michal 	}
239765a91a6cSKalderon, Michal 
239865a91a6cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Received syn on listening port\n");
2399456a5849SKalderon, Michal 	/* There may be an open ep on this connection if this is a syn
2400456a5849SKalderon, Michal 	 * retrasnmit... need to make sure there isn't...
2401456a5849SKalderon, Michal 	 */
2402456a5849SKalderon, Michal 	if (qed_iwarp_ep_exists(p_hwfn, &cm_info))
2403456a5849SKalderon, Michal 		goto err;
2404456a5849SKalderon, Michal 
2405456a5849SKalderon, Michal 	ep = qed_iwarp_get_free_ep(p_hwfn);
2406456a5849SKalderon, Michal 	if (!ep)
2407456a5849SKalderon, Michal 		goto err;
2408456a5849SKalderon, Michal 
2409456a5849SKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2410456a5849SKalderon, Michal 	list_add_tail(&ep->list_entry, &p_hwfn->p_rdma_info->iwarp.ep_list);
2411456a5849SKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2412456a5849SKalderon, Michal 
2413456a5849SKalderon, Michal 	ether_addr_copy(ep->remote_mac_addr, remote_mac_addr);
2414456a5849SKalderon, Michal 	ether_addr_copy(ep->local_mac_addr, local_mac_addr);
2415456a5849SKalderon, Michal 
2416456a5849SKalderon, Michal 	memcpy(&ep->cm_info, &cm_info, sizeof(ep->cm_info));
2417456a5849SKalderon, Michal 
2418456a5849SKalderon, Michal 	if (p_hwfn->p_rdma_info->iwarp.tcp_flags & QED_IWARP_TS_EN)
2419456a5849SKalderon, Michal 		ts_hdr_size = TIMESTAMP_HEADER_SIZE;
2420456a5849SKalderon, Michal 
2421456a5849SKalderon, Michal 	hdr_size = ((cm_info.ip_version == QED_TCP_IPV4) ? 40 : 60) +
2422456a5849SKalderon, Michal 		   ts_hdr_size;
2423456a5849SKalderon, Michal 	ep->mss = p_hwfn->p_rdma_info->iwarp.max_mtu - hdr_size;
2424456a5849SKalderon, Michal 	ep->mss = min_t(u16, QED_IWARP_MAX_FW_MSS, ep->mss);
2425456a5849SKalderon, Michal 
2426456a5849SKalderon, Michal 	ep->event_cb = listener->event_cb;
2427456a5849SKalderon, Michal 	ep->cb_context = listener->cb_context;
2428456a5849SKalderon, Michal 	ep->connect_mode = TCP_CONNECT_PASSIVE;
2429456a5849SKalderon, Michal 
2430456a5849SKalderon, Michal 	ep->syn = buf;
2431456a5849SKalderon, Michal 	ep->syn_ip_payload_length = (u16)payload_len;
2432456a5849SKalderon, Michal 	ep->syn_phy_addr = buf->data_phys_addr + data->u.placement_offset +
2433456a5849SKalderon, Michal 			   tcp_start_offset;
2434456a5849SKalderon, Michal 
2435456a5849SKalderon, Michal 	rc = qed_iwarp_tcp_offload(p_hwfn, ep);
2436456a5849SKalderon, Michal 	if (rc) {
2437456a5849SKalderon, Michal 		qed_iwarp_return_ep(p_hwfn, ep);
2438456a5849SKalderon, Michal 		goto err;
2439456a5849SKalderon, Michal 	}
2440456a5849SKalderon, Michal 
2441456a5849SKalderon, Michal 	return;
2442b5c29ca7SKalderon, Michal err:
244365a91a6cSKalderon, Michal 	qed_iwarp_ll2_post_rx(p_hwfn, buf, ll2_syn_handle);
2444b5c29ca7SKalderon, Michal }
2445b5c29ca7SKalderon, Michal 
2446b5c29ca7SKalderon, Michal static void qed_iwarp_ll2_rel_rx_pkt(void *cxt, u8 connection_handle,
2447b5c29ca7SKalderon, Michal 				     void *cookie, dma_addr_t rx_buf_addr,
2448b5c29ca7SKalderon, Michal 				     bool b_last_packet)
2449b5c29ca7SKalderon, Michal {
2450b5c29ca7SKalderon, Michal 	struct qed_iwarp_ll2_buff *buffer = cookie;
2451b5c29ca7SKalderon, Michal 	struct qed_hwfn *p_hwfn = cxt;
2452b5c29ca7SKalderon, Michal 
2453b5c29ca7SKalderon, Michal 	dma_free_coherent(&p_hwfn->cdev->pdev->dev, buffer->buff_size,
2454b5c29ca7SKalderon, Michal 			  buffer->data, buffer->data_phys_addr);
2455b5c29ca7SKalderon, Michal 	kfree(buffer);
2456b5c29ca7SKalderon, Michal }
2457b5c29ca7SKalderon, Michal 
2458b5c29ca7SKalderon, Michal static void qed_iwarp_ll2_comp_tx_pkt(void *cxt, u8 connection_handle,
2459b5c29ca7SKalderon, Michal 				      void *cookie, dma_addr_t first_frag_addr,
2460b5c29ca7SKalderon, Michal 				      bool b_last_fragment, bool b_last_packet)
2461b5c29ca7SKalderon, Michal {
2462b5c29ca7SKalderon, Michal 	struct qed_iwarp_ll2_buff *buffer = cookie;
2463d531038eSMichal Kalderon 	struct qed_iwarp_ll2_buff *piggy;
2464b5c29ca7SKalderon, Michal 	struct qed_hwfn *p_hwfn = cxt;
2465b5c29ca7SKalderon, Michal 
2466d531038eSMichal Kalderon 	if (!buffer)		/* can happen in packed mpa unaligned... */
2467d531038eSMichal Kalderon 		return;
2468d531038eSMichal Kalderon 
2469b5c29ca7SKalderon, Michal 	/* this was originally an rx packet, post it back */
2470d531038eSMichal Kalderon 	piggy = buffer->piggy_buf;
2471d531038eSMichal Kalderon 	if (piggy) {
2472d531038eSMichal Kalderon 		buffer->piggy_buf = NULL;
2473d531038eSMichal Kalderon 		qed_iwarp_ll2_post_rx(p_hwfn, piggy, connection_handle);
2474d531038eSMichal Kalderon 	}
2475d531038eSMichal Kalderon 
2476b5c29ca7SKalderon, Michal 	qed_iwarp_ll2_post_rx(p_hwfn, buffer, connection_handle);
2477fcb39f6cSMichal Kalderon 
2478fcb39f6cSMichal Kalderon 	if (connection_handle == p_hwfn->p_rdma_info->iwarp.ll2_mpa_handle)
2479fcb39f6cSMichal Kalderon 		qed_iwarp_process_pending_pkts(p_hwfn);
2480fcb39f6cSMichal Kalderon 
2481fcb39f6cSMichal Kalderon 	return;
2482b5c29ca7SKalderon, Michal }
2483b5c29ca7SKalderon, Michal 
2484b5c29ca7SKalderon, Michal static void qed_iwarp_ll2_rel_tx_pkt(void *cxt, u8 connection_handle,
2485b5c29ca7SKalderon, Michal 				     void *cookie, dma_addr_t first_frag_addr,
2486b5c29ca7SKalderon, Michal 				     bool b_last_fragment, bool b_last_packet)
2487b5c29ca7SKalderon, Michal {
2488b5c29ca7SKalderon, Michal 	struct qed_iwarp_ll2_buff *buffer = cookie;
2489b5c29ca7SKalderon, Michal 	struct qed_hwfn *p_hwfn = cxt;
2490b5c29ca7SKalderon, Michal 
2491b5c29ca7SKalderon, Michal 	if (!buffer)
2492b5c29ca7SKalderon, Michal 		return;
2493b5c29ca7SKalderon, Michal 
2494d531038eSMichal Kalderon 	if (buffer->piggy_buf) {
2495d531038eSMichal Kalderon 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
2496d531038eSMichal Kalderon 				  buffer->piggy_buf->buff_size,
2497d531038eSMichal Kalderon 				  buffer->piggy_buf->data,
2498d531038eSMichal Kalderon 				  buffer->piggy_buf->data_phys_addr);
2499d531038eSMichal Kalderon 
2500d531038eSMichal Kalderon 		kfree(buffer->piggy_buf);
2501d531038eSMichal Kalderon 	}
2502d531038eSMichal Kalderon 
2503b5c29ca7SKalderon, Michal 	dma_free_coherent(&p_hwfn->cdev->pdev->dev, buffer->buff_size,
2504b5c29ca7SKalderon, Michal 			  buffer->data, buffer->data_phys_addr);
2505b5c29ca7SKalderon, Michal 
2506b5c29ca7SKalderon, Michal 	kfree(buffer);
2507b5c29ca7SKalderon, Michal }
2508b5c29ca7SKalderon, Michal 
2509469981b1SMichal Kalderon /* The only slowpath for iwarp ll2 is unalign flush. When this completion
2510469981b1SMichal Kalderon  * is received, need to reset the FPDU.
2511469981b1SMichal Kalderon  */
2512ae3488ffSMichal Kalderon void
2513ae3488ffSMichal Kalderon qed_iwarp_ll2_slowpath(void *cxt,
2514ae3488ffSMichal Kalderon 		       u8 connection_handle,
2515ae3488ffSMichal Kalderon 		       u32 opaque_data_0, u32 opaque_data_1)
2516ae3488ffSMichal Kalderon {
2517469981b1SMichal Kalderon 	struct unaligned_opaque_data unalign_data;
2518469981b1SMichal Kalderon 	struct qed_hwfn *p_hwfn = cxt;
2519469981b1SMichal Kalderon 	struct qed_iwarp_fpdu *fpdu;
2520469981b1SMichal Kalderon 
2521469981b1SMichal Kalderon 	qed_iwarp_mpa_get_data(p_hwfn, &unalign_data,
2522469981b1SMichal Kalderon 			       opaque_data_0, opaque_data_1);
2523469981b1SMichal Kalderon 
2524469981b1SMichal Kalderon 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "(0x%x) Flush fpdu\n",
2525469981b1SMichal Kalderon 		   unalign_data.cid);
2526469981b1SMichal Kalderon 
2527469981b1SMichal Kalderon 	fpdu = qed_iwarp_get_curr_fpdu(p_hwfn, (u16)unalign_data.cid);
2528469981b1SMichal Kalderon 	if (fpdu)
2529469981b1SMichal Kalderon 		memset(fpdu, 0, sizeof(*fpdu));
2530ae3488ffSMichal Kalderon }
2531ae3488ffSMichal Kalderon 
2532b5c29ca7SKalderon, Michal static int qed_iwarp_ll2_stop(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2533b5c29ca7SKalderon, Michal {
2534b5c29ca7SKalderon, Michal 	struct qed_iwarp_info *iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2535b5c29ca7SKalderon, Michal 	int rc = 0;
2536b5c29ca7SKalderon, Michal 
2537b5c29ca7SKalderon, Michal 	if (iwarp_info->ll2_syn_handle != QED_IWARP_HANDLE_INVAL) {
2538b5c29ca7SKalderon, Michal 		rc = qed_ll2_terminate_connection(p_hwfn,
2539b5c29ca7SKalderon, Michal 						  iwarp_info->ll2_syn_handle);
2540b5c29ca7SKalderon, Michal 		if (rc)
2541b5c29ca7SKalderon, Michal 			DP_INFO(p_hwfn, "Failed to terminate syn connection\n");
2542b5c29ca7SKalderon, Michal 
2543b5c29ca7SKalderon, Michal 		qed_ll2_release_connection(p_hwfn, iwarp_info->ll2_syn_handle);
2544b5c29ca7SKalderon, Michal 		iwarp_info->ll2_syn_handle = QED_IWARP_HANDLE_INVAL;
2545b5c29ca7SKalderon, Michal 	}
2546b5c29ca7SKalderon, Michal 
2547d1abfd0bSMichal Kalderon 	if (iwarp_info->ll2_ooo_handle != QED_IWARP_HANDLE_INVAL) {
2548d1abfd0bSMichal Kalderon 		rc = qed_ll2_terminate_connection(p_hwfn,
2549d1abfd0bSMichal Kalderon 						  iwarp_info->ll2_ooo_handle);
2550d1abfd0bSMichal Kalderon 		if (rc)
2551d1abfd0bSMichal Kalderon 			DP_INFO(p_hwfn, "Failed to terminate ooo connection\n");
2552d1abfd0bSMichal Kalderon 
2553d1abfd0bSMichal Kalderon 		qed_ll2_release_connection(p_hwfn, iwarp_info->ll2_ooo_handle);
2554d1abfd0bSMichal Kalderon 		iwarp_info->ll2_ooo_handle = QED_IWARP_HANDLE_INVAL;
2555d1abfd0bSMichal Kalderon 	}
2556d1abfd0bSMichal Kalderon 
2557ae3488ffSMichal Kalderon 	if (iwarp_info->ll2_mpa_handle != QED_IWARP_HANDLE_INVAL) {
2558ae3488ffSMichal Kalderon 		rc = qed_ll2_terminate_connection(p_hwfn,
2559ae3488ffSMichal Kalderon 						  iwarp_info->ll2_mpa_handle);
2560ae3488ffSMichal Kalderon 		if (rc)
2561ae3488ffSMichal Kalderon 			DP_INFO(p_hwfn, "Failed to terminate mpa connection\n");
2562ae3488ffSMichal Kalderon 
2563ae3488ffSMichal Kalderon 		qed_ll2_release_connection(p_hwfn, iwarp_info->ll2_mpa_handle);
2564ae3488ffSMichal Kalderon 		iwarp_info->ll2_mpa_handle = QED_IWARP_HANDLE_INVAL;
2565ae3488ffSMichal Kalderon 	}
2566ae3488ffSMichal Kalderon 
2567b5c29ca7SKalderon, Michal 	qed_llh_remove_mac_filter(p_hwfn,
2568b5c29ca7SKalderon, Michal 				  p_ptt, p_hwfn->p_rdma_info->iwarp.mac_addr);
2569b5c29ca7SKalderon, Michal 	return rc;
2570b5c29ca7SKalderon, Michal }
2571b5c29ca7SKalderon, Michal 
2572b5c29ca7SKalderon, Michal static int
2573b5c29ca7SKalderon, Michal qed_iwarp_ll2_alloc_buffers(struct qed_hwfn *p_hwfn,
2574b5c29ca7SKalderon, Michal 			    int num_rx_bufs, int buff_size, u8 ll2_handle)
2575b5c29ca7SKalderon, Michal {
2576b5c29ca7SKalderon, Michal 	struct qed_iwarp_ll2_buff *buffer;
2577b5c29ca7SKalderon, Michal 	int rc = 0;
2578b5c29ca7SKalderon, Michal 	int i;
2579b5c29ca7SKalderon, Michal 
2580b5c29ca7SKalderon, Michal 	for (i = 0; i < num_rx_bufs; i++) {
2581b5c29ca7SKalderon, Michal 		buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
2582b5c29ca7SKalderon, Michal 		if (!buffer) {
2583b5c29ca7SKalderon, Michal 			rc = -ENOMEM;
2584b5c29ca7SKalderon, Michal 			break;
2585b5c29ca7SKalderon, Michal 		}
2586b5c29ca7SKalderon, Michal 
2587b5c29ca7SKalderon, Michal 		buffer->data = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
2588b5c29ca7SKalderon, Michal 						  buff_size,
2589b5c29ca7SKalderon, Michal 						  &buffer->data_phys_addr,
2590b5c29ca7SKalderon, Michal 						  GFP_KERNEL);
2591b5c29ca7SKalderon, Michal 		if (!buffer->data) {
2592b5c29ca7SKalderon, Michal 			kfree(buffer);
2593b5c29ca7SKalderon, Michal 			rc = -ENOMEM;
2594b5c29ca7SKalderon, Michal 			break;
2595b5c29ca7SKalderon, Michal 		}
2596b5c29ca7SKalderon, Michal 
2597b5c29ca7SKalderon, Michal 		buffer->buff_size = buff_size;
2598b5c29ca7SKalderon, Michal 		rc = qed_iwarp_ll2_post_rx(p_hwfn, buffer, ll2_handle);
2599b5c29ca7SKalderon, Michal 		if (rc)
2600b5c29ca7SKalderon, Michal 			/* buffers will be deallocated by qed_ll2 */
2601b5c29ca7SKalderon, Michal 			break;
2602b5c29ca7SKalderon, Michal 	}
2603b5c29ca7SKalderon, Michal 	return rc;
2604b5c29ca7SKalderon, Michal }
2605b5c29ca7SKalderon, Michal 
2606b5c29ca7SKalderon, Michal #define QED_IWARP_MAX_BUF_SIZE(mtu)				     \
2607b5c29ca7SKalderon, Michal 	ALIGN((mtu) + ETH_HLEN + 2 * VLAN_HLEN + 2 + ETH_CACHE_LINE_SIZE, \
2608b5c29ca7SKalderon, Michal 		ETH_CACHE_LINE_SIZE)
2609b5c29ca7SKalderon, Michal 
2610b5c29ca7SKalderon, Michal static int
2611b5c29ca7SKalderon, Michal qed_iwarp_ll2_start(struct qed_hwfn *p_hwfn,
2612b5c29ca7SKalderon, Michal 		    struct qed_rdma_start_in_params *params,
2613b5c29ca7SKalderon, Michal 		    struct qed_ptt *p_ptt)
2614b5c29ca7SKalderon, Michal {
2615b5c29ca7SKalderon, Michal 	struct qed_iwarp_info *iwarp_info;
2616b5c29ca7SKalderon, Michal 	struct qed_ll2_acquire_data data;
2617b5c29ca7SKalderon, Michal 	struct qed_ll2_cbs cbs;
2618ae3488ffSMichal Kalderon 	u32 mpa_buff_size;
2619d1abfd0bSMichal Kalderon 	u16 n_ooo_bufs;
2620b5c29ca7SKalderon, Michal 	int rc = 0;
2621fcb39f6cSMichal Kalderon 	int i;
2622b5c29ca7SKalderon, Michal 
2623b5c29ca7SKalderon, Michal 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
2624b5c29ca7SKalderon, Michal 	iwarp_info->ll2_syn_handle = QED_IWARP_HANDLE_INVAL;
2625d1abfd0bSMichal Kalderon 	iwarp_info->ll2_ooo_handle = QED_IWARP_HANDLE_INVAL;
2626ae3488ffSMichal Kalderon 	iwarp_info->ll2_mpa_handle = QED_IWARP_HANDLE_INVAL;
2627b5c29ca7SKalderon, Michal 
2628b5c29ca7SKalderon, Michal 	iwarp_info->max_mtu = params->max_mtu;
2629b5c29ca7SKalderon, Michal 
2630b5c29ca7SKalderon, Michal 	ether_addr_copy(p_hwfn->p_rdma_info->iwarp.mac_addr, params->mac_addr);
2631b5c29ca7SKalderon, Michal 
2632b5c29ca7SKalderon, Michal 	rc = qed_llh_add_mac_filter(p_hwfn, p_ptt, params->mac_addr);
2633b5c29ca7SKalderon, Michal 	if (rc)
2634b5c29ca7SKalderon, Michal 		return rc;
2635b5c29ca7SKalderon, Michal 
2636b5c29ca7SKalderon, Michal 	/* Start SYN connection */
2637b5c29ca7SKalderon, Michal 	cbs.rx_comp_cb = qed_iwarp_ll2_comp_syn_pkt;
2638b5c29ca7SKalderon, Michal 	cbs.rx_release_cb = qed_iwarp_ll2_rel_rx_pkt;
2639b5c29ca7SKalderon, Michal 	cbs.tx_comp_cb = qed_iwarp_ll2_comp_tx_pkt;
2640b5c29ca7SKalderon, Michal 	cbs.tx_release_cb = qed_iwarp_ll2_rel_tx_pkt;
2641b5c29ca7SKalderon, Michal 	cbs.cookie = p_hwfn;
2642b5c29ca7SKalderon, Michal 
2643b5c29ca7SKalderon, Michal 	memset(&data, 0, sizeof(data));
2644b5c29ca7SKalderon, Michal 	data.input.conn_type = QED_LL2_TYPE_IWARP;
2645b5c29ca7SKalderon, Michal 	data.input.mtu = QED_IWARP_MAX_SYN_PKT_SIZE;
2646b5c29ca7SKalderon, Michal 	data.input.rx_num_desc = QED_IWARP_LL2_SYN_RX_SIZE;
2647b5c29ca7SKalderon, Michal 	data.input.tx_num_desc = QED_IWARP_LL2_SYN_TX_SIZE;
2648b5c29ca7SKalderon, Michal 	data.input.tx_max_bds_per_packet = 1;	/* will never be fragmented */
2649b5c29ca7SKalderon, Michal 	data.input.tx_tc = PKT_LB_TC;
2650b5c29ca7SKalderon, Michal 	data.input.tx_dest = QED_LL2_TX_DEST_LB;
2651b5c29ca7SKalderon, Michal 	data.p_connection_handle = &iwarp_info->ll2_syn_handle;
2652b5c29ca7SKalderon, Michal 	data.cbs = &cbs;
2653b5c29ca7SKalderon, Michal 
2654b5c29ca7SKalderon, Michal 	rc = qed_ll2_acquire_connection(p_hwfn, &data);
2655b5c29ca7SKalderon, Michal 	if (rc) {
2656b5c29ca7SKalderon, Michal 		DP_NOTICE(p_hwfn, "Failed to acquire LL2 connection\n");
2657b5c29ca7SKalderon, Michal 		qed_llh_remove_mac_filter(p_hwfn, p_ptt, params->mac_addr);
2658b5c29ca7SKalderon, Michal 		return rc;
2659b5c29ca7SKalderon, Michal 	}
2660b5c29ca7SKalderon, Michal 
2661b5c29ca7SKalderon, Michal 	rc = qed_ll2_establish_connection(p_hwfn, iwarp_info->ll2_syn_handle);
2662b5c29ca7SKalderon, Michal 	if (rc) {
2663b5c29ca7SKalderon, Michal 		DP_NOTICE(p_hwfn, "Failed to establish LL2 connection\n");
2664b5c29ca7SKalderon, Michal 		goto err;
2665b5c29ca7SKalderon, Michal 	}
2666b5c29ca7SKalderon, Michal 
2667b5c29ca7SKalderon, Michal 	rc = qed_iwarp_ll2_alloc_buffers(p_hwfn,
2668b5c29ca7SKalderon, Michal 					 QED_IWARP_LL2_SYN_RX_SIZE,
2669b5c29ca7SKalderon, Michal 					 QED_IWARP_MAX_SYN_PKT_SIZE,
2670b5c29ca7SKalderon, Michal 					 iwarp_info->ll2_syn_handle);
2671b5c29ca7SKalderon, Michal 	if (rc)
2672b5c29ca7SKalderon, Michal 		goto err;
2673b5c29ca7SKalderon, Michal 
2674d1abfd0bSMichal Kalderon 	/* Start OOO connection */
2675d1abfd0bSMichal Kalderon 	data.input.conn_type = QED_LL2_TYPE_OOO;
2676d1abfd0bSMichal Kalderon 	data.input.mtu = params->max_mtu;
2677d1abfd0bSMichal Kalderon 
2678d1abfd0bSMichal Kalderon 	n_ooo_bufs = (QED_IWARP_MAX_OOO * QED_IWARP_RCV_WND_SIZE_DEF) /
2679d1abfd0bSMichal Kalderon 		     iwarp_info->max_mtu;
2680d1abfd0bSMichal Kalderon 	n_ooo_bufs = min_t(u32, n_ooo_bufs, QED_IWARP_LL2_OOO_MAX_RX_SIZE);
2681d1abfd0bSMichal Kalderon 
2682d1abfd0bSMichal Kalderon 	data.input.rx_num_desc = n_ooo_bufs;
2683d1abfd0bSMichal Kalderon 	data.input.rx_num_ooo_buffers = n_ooo_bufs;
2684d1abfd0bSMichal Kalderon 
2685d1abfd0bSMichal Kalderon 	data.input.tx_max_bds_per_packet = 1;	/* will never be fragmented */
2686d1abfd0bSMichal Kalderon 	data.input.tx_num_desc = QED_IWARP_LL2_OOO_DEF_TX_SIZE;
2687d1abfd0bSMichal Kalderon 	data.p_connection_handle = &iwarp_info->ll2_ooo_handle;
2688d1abfd0bSMichal Kalderon 
2689d1abfd0bSMichal Kalderon 	rc = qed_ll2_acquire_connection(p_hwfn, &data);
2690d1abfd0bSMichal Kalderon 	if (rc)
2691d1abfd0bSMichal Kalderon 		goto err;
2692d1abfd0bSMichal Kalderon 
2693d1abfd0bSMichal Kalderon 	rc = qed_ll2_establish_connection(p_hwfn, iwarp_info->ll2_ooo_handle);
2694d1abfd0bSMichal Kalderon 	if (rc)
2695d1abfd0bSMichal Kalderon 		goto err;
2696d1abfd0bSMichal Kalderon 
2697ae3488ffSMichal Kalderon 	/* Start Unaligned MPA connection */
2698ae3488ffSMichal Kalderon 	cbs.rx_comp_cb = qed_iwarp_ll2_comp_mpa_pkt;
2699ae3488ffSMichal Kalderon 	cbs.slowpath_cb = qed_iwarp_ll2_slowpath;
2700ae3488ffSMichal Kalderon 
2701ae3488ffSMichal Kalderon 	memset(&data, 0, sizeof(data));
2702ae3488ffSMichal Kalderon 	data.input.conn_type = QED_LL2_TYPE_IWARP;
2703ae3488ffSMichal Kalderon 	data.input.mtu = params->max_mtu;
2704ae3488ffSMichal Kalderon 	/* FW requires that once a packet arrives OOO, it must have at
2705ae3488ffSMichal Kalderon 	 * least 2 rx buffers available on the unaligned connection
2706ae3488ffSMichal Kalderon 	 * for handling the case that it is a partial fpdu.
2707ae3488ffSMichal Kalderon 	 */
2708ae3488ffSMichal Kalderon 	data.input.rx_num_desc = n_ooo_bufs * 2;
2709ae3488ffSMichal Kalderon 	data.input.tx_num_desc = data.input.rx_num_desc;
2710ae3488ffSMichal Kalderon 	data.input.tx_max_bds_per_packet = QED_IWARP_MAX_BDS_PER_FPDU;
2711ae3488ffSMichal Kalderon 	data.p_connection_handle = &iwarp_info->ll2_mpa_handle;
2712ae3488ffSMichal Kalderon 	data.input.secondary_queue = true;
2713ae3488ffSMichal Kalderon 	data.cbs = &cbs;
2714ae3488ffSMichal Kalderon 
2715ae3488ffSMichal Kalderon 	rc = qed_ll2_acquire_connection(p_hwfn, &data);
2716ae3488ffSMichal Kalderon 	if (rc)
2717ae3488ffSMichal Kalderon 		goto err;
2718ae3488ffSMichal Kalderon 
2719ae3488ffSMichal Kalderon 	rc = qed_ll2_establish_connection(p_hwfn, iwarp_info->ll2_mpa_handle);
2720ae3488ffSMichal Kalderon 	if (rc)
2721ae3488ffSMichal Kalderon 		goto err;
2722ae3488ffSMichal Kalderon 
2723ae3488ffSMichal Kalderon 	mpa_buff_size = QED_IWARP_MAX_BUF_SIZE(params->max_mtu);
2724ae3488ffSMichal Kalderon 	rc = qed_iwarp_ll2_alloc_buffers(p_hwfn,
2725ae3488ffSMichal Kalderon 					 data.input.rx_num_desc,
2726ae3488ffSMichal Kalderon 					 mpa_buff_size,
2727ae3488ffSMichal Kalderon 					 iwarp_info->ll2_mpa_handle);
2728ae3488ffSMichal Kalderon 	if (rc)
2729ae3488ffSMichal Kalderon 		goto err;
2730469981b1SMichal Kalderon 
2731469981b1SMichal Kalderon 	iwarp_info->partial_fpdus = kcalloc((u16)p_hwfn->p_rdma_info->num_qps,
2732469981b1SMichal Kalderon 					    sizeof(*iwarp_info->partial_fpdus),
2733469981b1SMichal Kalderon 					    GFP_KERNEL);
2734469981b1SMichal Kalderon 	if (!iwarp_info->partial_fpdus)
2735469981b1SMichal Kalderon 		goto err;
2736469981b1SMichal Kalderon 
2737469981b1SMichal Kalderon 	iwarp_info->max_num_partial_fpdus = (u16)p_hwfn->p_rdma_info->num_qps;
27381e28eaadSMichal Kalderon 
27391e28eaadSMichal Kalderon 	iwarp_info->mpa_intermediate_buf = kzalloc(mpa_buff_size, GFP_KERNEL);
27401e28eaadSMichal Kalderon 	if (!iwarp_info->mpa_intermediate_buf)
27411e28eaadSMichal Kalderon 		goto err;
27421e28eaadSMichal Kalderon 
2743fcb39f6cSMichal Kalderon 	/* The mpa_bufs array serves for pending RX packets received on the
2744fcb39f6cSMichal Kalderon 	 * mpa ll2 that don't have place on the tx ring and require later
2745fcb39f6cSMichal Kalderon 	 * processing. We can't fail on allocation of such a struct therefore
2746fcb39f6cSMichal Kalderon 	 * we allocate enough to take care of all rx packets
2747fcb39f6cSMichal Kalderon 	 */
2748fcb39f6cSMichal Kalderon 	iwarp_info->mpa_bufs = kcalloc(data.input.rx_num_desc,
2749fcb39f6cSMichal Kalderon 				       sizeof(*iwarp_info->mpa_bufs),
2750fcb39f6cSMichal Kalderon 				       GFP_KERNEL);
2751fcb39f6cSMichal Kalderon 	if (!iwarp_info->mpa_bufs)
2752fcb39f6cSMichal Kalderon 		goto err;
2753fcb39f6cSMichal Kalderon 
2754fcb39f6cSMichal Kalderon 	INIT_LIST_HEAD(&iwarp_info->mpa_buf_pending_list);
2755fcb39f6cSMichal Kalderon 	INIT_LIST_HEAD(&iwarp_info->mpa_buf_list);
2756fcb39f6cSMichal Kalderon 	for (i = 0; i < data.input.rx_num_desc; i++)
2757fcb39f6cSMichal Kalderon 		list_add_tail(&iwarp_info->mpa_bufs[i].list_entry,
2758fcb39f6cSMichal Kalderon 			      &iwarp_info->mpa_buf_list);
2759b5c29ca7SKalderon, Michal 	return rc;
2760b5c29ca7SKalderon, Michal err:
2761b5c29ca7SKalderon, Michal 	qed_iwarp_ll2_stop(p_hwfn, p_ptt);
2762b5c29ca7SKalderon, Michal 
2763b5c29ca7SKalderon, Michal 	return rc;
2764b5c29ca7SKalderon, Michal }
2765b5c29ca7SKalderon, Michal 
276667b40dccSKalderon, Michal int qed_iwarp_setup(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
276767b40dccSKalderon, Michal 		    struct qed_rdma_start_in_params *params)
276867b40dccSKalderon, Michal {
276967b40dccSKalderon, Michal 	struct qed_iwarp_info *iwarp_info;
277067b40dccSKalderon, Michal 	u32 rcv_wnd_size;
277167b40dccSKalderon, Michal 
277267b40dccSKalderon, Michal 	iwarp_info = &p_hwfn->p_rdma_info->iwarp;
277367b40dccSKalderon, Michal 
277467b40dccSKalderon, Michal 	iwarp_info->tcp_flags = QED_IWARP_TS_EN;
277567b40dccSKalderon, Michal 	rcv_wnd_size = QED_IWARP_RCV_WND_SIZE_DEF;
277667b40dccSKalderon, Michal 
277767b40dccSKalderon, Michal 	/* value 0 is used for ilog2(QED_IWARP_RCV_WND_SIZE_MIN) */
277867b40dccSKalderon, Michal 	iwarp_info->rcv_wnd_scale = ilog2(rcv_wnd_size) -
277967b40dccSKalderon, Michal 	    ilog2(QED_IWARP_RCV_WND_SIZE_MIN);
2780da090917STomer Tayar 	iwarp_info->rcv_wnd_size = rcv_wnd_size >> iwarp_info->rcv_wnd_scale;
278167b40dccSKalderon, Michal 	iwarp_info->crc_needed = QED_IWARP_PARAM_CRC_NEEDED;
278267b40dccSKalderon, Michal 	iwarp_info->mpa_rev = MPA_NEGOTIATION_TYPE_ENHANCED;
278367b40dccSKalderon, Michal 
278467b40dccSKalderon, Michal 	iwarp_info->peer2peer = QED_IWARP_PARAM_P2P;
278567b40dccSKalderon, Michal 
2786456a5849SKalderon, Michal 	iwarp_info->rtr_type =  MPA_RTR_TYPE_ZERO_SEND |
2787456a5849SKalderon, Michal 				MPA_RTR_TYPE_ZERO_WRITE |
2788456a5849SKalderon, Michal 				MPA_RTR_TYPE_ZERO_READ;
2789456a5849SKalderon, Michal 
279067b40dccSKalderon, Michal 	spin_lock_init(&p_hwfn->p_rdma_info->iwarp.qp_lock);
2791456a5849SKalderon, Michal 	INIT_LIST_HEAD(&p_hwfn->p_rdma_info->iwarp.ep_list);
279265a91a6cSKalderon, Michal 	INIT_LIST_HEAD(&p_hwfn->p_rdma_info->iwarp.listen_list);
279367b40dccSKalderon, Michal 
279467b40dccSKalderon, Michal 	qed_spq_register_async_cb(p_hwfn, PROTOCOLID_IWARP,
279567b40dccSKalderon, Michal 				  qed_iwarp_async_event);
2796d1abfd0bSMichal Kalderon 	qed_ooo_setup(p_hwfn);
279767b40dccSKalderon, Michal 
2798b5c29ca7SKalderon, Michal 	return qed_iwarp_ll2_start(p_hwfn, params, p_ptt);
279967b40dccSKalderon, Michal }
280067b40dccSKalderon, Michal 
280167b40dccSKalderon, Michal int qed_iwarp_stop(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
280267b40dccSKalderon, Michal {
280367b40dccSKalderon, Michal 	int rc;
280467b40dccSKalderon, Michal 
2805456a5849SKalderon, Michal 	qed_iwarp_free_prealloc_ep(p_hwfn);
280667b40dccSKalderon, Michal 	rc = qed_iwarp_wait_for_all_cids(p_hwfn);
280767b40dccSKalderon, Michal 	if (rc)
280867b40dccSKalderon, Michal 		return rc;
280967b40dccSKalderon, Michal 
281067b40dccSKalderon, Michal 	qed_spq_unregister_async_cb(p_hwfn, PROTOCOLID_IWARP);
281167b40dccSKalderon, Michal 
2812b5c29ca7SKalderon, Michal 	return qed_iwarp_ll2_stop(p_hwfn, p_ptt);
281367b40dccSKalderon, Michal }
281467b40dccSKalderon, Michal 
2815fc4c6065SKalderon, Michal void qed_iwarp_qp_in_error(struct qed_hwfn *p_hwfn,
2816fc4c6065SKalderon, Michal 			   struct qed_iwarp_ep *ep, u8 fw_return_code)
2817fc4c6065SKalderon, Michal {
2818fc4c6065SKalderon, Michal 	struct qed_iwarp_cm_event_params params;
2819fc4c6065SKalderon, Michal 
2820fc4c6065SKalderon, Michal 	qed_iwarp_modify_qp(p_hwfn, ep->qp, QED_IWARP_QP_STATE_ERROR, true);
2821fc4c6065SKalderon, Michal 
2822fc4c6065SKalderon, Michal 	params.event = QED_IWARP_EVENT_CLOSE;
2823fc4c6065SKalderon, Michal 	params.ep_context = ep;
2824fc4c6065SKalderon, Michal 	params.cm_info = &ep->cm_info;
2825fc4c6065SKalderon, Michal 	params.status = (fw_return_code == IWARP_QP_IN_ERROR_GOOD_CLOSE) ?
2826fc4c6065SKalderon, Michal 			 0 : -ECONNRESET;
2827fc4c6065SKalderon, Michal 
2828fc4c6065SKalderon, Michal 	ep->state = QED_IWARP_EP_CLOSED;
2829fc4c6065SKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2830fc4c6065SKalderon, Michal 	list_del(&ep->list_entry);
2831fc4c6065SKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
2832fc4c6065SKalderon, Michal 
2833fc4c6065SKalderon, Michal 	ep->event_cb(ep->cb_context, &params);
2834fc4c6065SKalderon, Michal }
2835fc4c6065SKalderon, Michal 
2836fc4c6065SKalderon, Michal void qed_iwarp_exception_received(struct qed_hwfn *p_hwfn,
2837fc4c6065SKalderon, Michal 				  struct qed_iwarp_ep *ep, int fw_ret_code)
2838fc4c6065SKalderon, Michal {
2839fc4c6065SKalderon, Michal 	struct qed_iwarp_cm_event_params params;
2840fc4c6065SKalderon, Michal 	bool event_cb = false;
2841fc4c6065SKalderon, Michal 
2842fc4c6065SKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "EP(0x%x) fw_ret_code=%d\n",
2843fc4c6065SKalderon, Michal 		   ep->cid, fw_ret_code);
2844fc4c6065SKalderon, Michal 
2845fc4c6065SKalderon, Michal 	switch (fw_ret_code) {
2846fc4c6065SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_LLP_CLOSED:
2847fc4c6065SKalderon, Michal 		params.status = 0;
2848fc4c6065SKalderon, Michal 		params.event = QED_IWARP_EVENT_DISCONNECT;
2849fc4c6065SKalderon, Michal 		event_cb = true;
2850fc4c6065SKalderon, Michal 		break;
2851fc4c6065SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_LLP_RESET:
2852fc4c6065SKalderon, Michal 		params.status = -ECONNRESET;
2853fc4c6065SKalderon, Michal 		params.event = QED_IWARP_EVENT_DISCONNECT;
2854fc4c6065SKalderon, Michal 		event_cb = true;
2855fc4c6065SKalderon, Michal 		break;
28569816b614SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_RQ_EMPTY:
28579816b614SKalderon, Michal 		params.event = QED_IWARP_EVENT_RQ_EMPTY;
28589816b614SKalderon, Michal 		event_cb = true;
28599816b614SKalderon, Michal 		break;
28609816b614SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_IRQ_FULL:
28619816b614SKalderon, Michal 		params.event = QED_IWARP_EVENT_IRQ_FULL;
28629816b614SKalderon, Michal 		event_cb = true;
28639816b614SKalderon, Michal 		break;
28649816b614SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_LLP_TIMEOUT:
28659816b614SKalderon, Michal 		params.event = QED_IWARP_EVENT_LLP_TIMEOUT;
28669816b614SKalderon, Michal 		event_cb = true;
28679816b614SKalderon, Michal 		break;
28689816b614SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_REMOTE_PROTECTION_ERROR:
28699816b614SKalderon, Michal 		params.event = QED_IWARP_EVENT_REMOTE_PROTECTION_ERROR;
28709816b614SKalderon, Michal 		event_cb = true;
28719816b614SKalderon, Michal 		break;
28729816b614SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_CQ_OVERFLOW:
28739816b614SKalderon, Michal 		params.event = QED_IWARP_EVENT_CQ_OVERFLOW;
28749816b614SKalderon, Michal 		event_cb = true;
28759816b614SKalderon, Michal 		break;
28769816b614SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_LOCAL_CATASTROPHIC:
28779816b614SKalderon, Michal 		params.event = QED_IWARP_EVENT_QP_CATASTROPHIC;
28789816b614SKalderon, Michal 		event_cb = true;
28799816b614SKalderon, Michal 		break;
28809816b614SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_LOCAL_ACCESS_ERROR:
28819816b614SKalderon, Michal 		params.event = QED_IWARP_EVENT_LOCAL_ACCESS_ERROR;
28829816b614SKalderon, Michal 		event_cb = true;
28839816b614SKalderon, Michal 		break;
28849816b614SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_REMOTE_OPERATION_ERROR:
28859816b614SKalderon, Michal 		params.event = QED_IWARP_EVENT_REMOTE_OPERATION_ERROR;
28869816b614SKalderon, Michal 		event_cb = true;
28879816b614SKalderon, Michal 		break;
28889816b614SKalderon, Michal 	case IWARP_EXCEPTION_DETECTED_TERMINATE_RECEIVED:
28899816b614SKalderon, Michal 		params.event = QED_IWARP_EVENT_TERMINATE_RECEIVED;
28909816b614SKalderon, Michal 		event_cb = true;
28919816b614SKalderon, Michal 		break;
2892fc4c6065SKalderon, Michal 	default:
2893fc4c6065SKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
2894fc4c6065SKalderon, Michal 			   "Unhandled exception received...fw_ret_code=%d\n",
2895fc4c6065SKalderon, Michal 			   fw_ret_code);
2896fc4c6065SKalderon, Michal 		break;
2897fc4c6065SKalderon, Michal 	}
2898fc4c6065SKalderon, Michal 
2899fc4c6065SKalderon, Michal 	if (event_cb) {
2900fc4c6065SKalderon, Michal 		params.ep_context = ep;
2901fc4c6065SKalderon, Michal 		params.cm_info = &ep->cm_info;
2902fc4c6065SKalderon, Michal 		ep->event_cb(ep->cb_context, &params);
2903fc4c6065SKalderon, Michal 	}
2904fc4c6065SKalderon, Michal }
2905fc4c6065SKalderon, Michal 
29069816b614SKalderon, Michal static void
29079816b614SKalderon, Michal qed_iwarp_tcp_connect_unsuccessful(struct qed_hwfn *p_hwfn,
29089816b614SKalderon, Michal 				   struct qed_iwarp_ep *ep, u8 fw_return_code)
29099816b614SKalderon, Michal {
29109816b614SKalderon, Michal 	struct qed_iwarp_cm_event_params params;
29119816b614SKalderon, Michal 
29129816b614SKalderon, Michal 	memset(&params, 0, sizeof(params));
29139816b614SKalderon, Michal 	params.event = QED_IWARP_EVENT_ACTIVE_COMPLETE;
29149816b614SKalderon, Michal 	params.ep_context = ep;
29159816b614SKalderon, Michal 	params.cm_info = &ep->cm_info;
29169816b614SKalderon, Michal 	ep->state = QED_IWARP_EP_CLOSED;
29179816b614SKalderon, Michal 
29189816b614SKalderon, Michal 	switch (fw_return_code) {
29199816b614SKalderon, Michal 	case IWARP_CONN_ERROR_TCP_CONNECT_INVALID_PACKET:
29209816b614SKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
29219816b614SKalderon, Michal 			   "%s(0x%x) TCP connect got invalid packet\n",
29229816b614SKalderon, Michal 			   QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
29239816b614SKalderon, Michal 		params.status = -ECONNRESET;
29249816b614SKalderon, Michal 		break;
29259816b614SKalderon, Michal 	case IWARP_CONN_ERROR_TCP_CONNECTION_RST:
29269816b614SKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
29279816b614SKalderon, Michal 			   "%s(0x%x) TCP Connection Reset\n",
29289816b614SKalderon, Michal 			   QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
29299816b614SKalderon, Michal 		params.status = -ECONNRESET;
29309816b614SKalderon, Michal 		break;
29319816b614SKalderon, Michal 	case IWARP_CONN_ERROR_TCP_CONNECT_TIMEOUT:
29329816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) TCP timeout\n",
29339816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
29349816b614SKalderon, Michal 		params.status = -EBUSY;
29359816b614SKalderon, Michal 		break;
29369816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_NOT_SUPPORTED_VER:
29379816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA not supported VER\n",
29389816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
29399816b614SKalderon, Michal 		params.status = -ECONNREFUSED;
29409816b614SKalderon, Michal 		break;
29419816b614SKalderon, Michal 	case IWARP_CONN_ERROR_MPA_INVALID_PACKET:
29429816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "%s(0x%x) MPA Invalid Packet\n",
29439816b614SKalderon, Michal 			  QED_IWARP_CONNECT_MODE_STRING(ep), ep->tcp_cid);
29449816b614SKalderon, Michal 		params.status = -ECONNRESET;
29459816b614SKalderon, Michal 		break;
29469816b614SKalderon, Michal 	default:
29479816b614SKalderon, Michal 		DP_ERR(p_hwfn,
29489816b614SKalderon, Michal 		       "%s(0x%x) Unexpected return code tcp connect: %d\n",
29499816b614SKalderon, Michal 		       QED_IWARP_CONNECT_MODE_STRING(ep),
29509816b614SKalderon, Michal 		       ep->tcp_cid, fw_return_code);
29519816b614SKalderon, Michal 		params.status = -ECONNRESET;
29529816b614SKalderon, Michal 		break;
29539816b614SKalderon, Michal 	}
29549816b614SKalderon, Michal 
29559816b614SKalderon, Michal 	if (ep->connect_mode == TCP_CONNECT_PASSIVE) {
29569816b614SKalderon, Michal 		ep->tcp_cid = QED_IWARP_INVALID_TCP_CID;
29579816b614SKalderon, Michal 		qed_iwarp_return_ep(p_hwfn, ep);
29589816b614SKalderon, Michal 	} else {
29599816b614SKalderon, Michal 		ep->event_cb(ep->cb_context, &params);
29609816b614SKalderon, Michal 		spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
29619816b614SKalderon, Michal 		list_del(&ep->list_entry);
29629816b614SKalderon, Michal 		spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
29639816b614SKalderon, Michal 	}
29649816b614SKalderon, Michal }
29659816b614SKalderon, Michal 
2966456a5849SKalderon, Michal void
2967456a5849SKalderon, Michal qed_iwarp_connect_complete(struct qed_hwfn *p_hwfn,
2968456a5849SKalderon, Michal 			   struct qed_iwarp_ep *ep, u8 fw_return_code)
2969456a5849SKalderon, Michal {
29704b0fdd7cSKalderon, Michal 	u8 ll2_syn_handle = p_hwfn->p_rdma_info->iwarp.ll2_syn_handle;
29714b0fdd7cSKalderon, Michal 
29724b0fdd7cSKalderon, Michal 	if (ep->connect_mode == TCP_CONNECT_PASSIVE) {
2973456a5849SKalderon, Michal 		/* Done with the SYN packet, post back to ll2 rx */
29744b0fdd7cSKalderon, Michal 		qed_iwarp_ll2_post_rx(p_hwfn, ep->syn, ll2_syn_handle);
29754b0fdd7cSKalderon, Michal 
2976456a5849SKalderon, Michal 		ep->syn = NULL;
2977456a5849SKalderon, Michal 
2978456a5849SKalderon, Michal 		/* If connect failed - upper layer doesn't know about it */
29799816b614SKalderon, Michal 		if (fw_return_code == RDMA_RETURN_OK)
2980456a5849SKalderon, Michal 			qed_iwarp_mpa_received(p_hwfn, ep);
29819816b614SKalderon, Michal 		else
29829816b614SKalderon, Michal 			qed_iwarp_tcp_connect_unsuccessful(p_hwfn, ep,
29839816b614SKalderon, Michal 							   fw_return_code);
29844b0fdd7cSKalderon, Michal 	} else {
29859816b614SKalderon, Michal 		if (fw_return_code == RDMA_RETURN_OK)
29864b0fdd7cSKalderon, Michal 			qed_iwarp_mpa_offload(p_hwfn, ep);
29879816b614SKalderon, Michal 		else
29889816b614SKalderon, Michal 			qed_iwarp_tcp_connect_unsuccessful(p_hwfn, ep,
29899816b614SKalderon, Michal 							   fw_return_code);
29904b0fdd7cSKalderon, Michal 	}
2991456a5849SKalderon, Michal }
2992456a5849SKalderon, Michal 
2993456a5849SKalderon, Michal static inline bool
2994456a5849SKalderon, Michal qed_iwarp_check_ep_ok(struct qed_hwfn *p_hwfn, struct qed_iwarp_ep *ep)
2995456a5849SKalderon, Michal {
2996456a5849SKalderon, Michal 	if (!ep || (ep->sig != QED_EP_SIG)) {
2997456a5849SKalderon, Michal 		DP_ERR(p_hwfn, "ERROR ON ASYNC ep=%p\n", ep);
2998456a5849SKalderon, Michal 		return false;
2999456a5849SKalderon, Michal 	}
3000456a5849SKalderon, Michal 
3001456a5849SKalderon, Michal 	return true;
3002456a5849SKalderon, Michal }
3003456a5849SKalderon, Michal 
300467b40dccSKalderon, Michal static int qed_iwarp_async_event(struct qed_hwfn *p_hwfn,
300567b40dccSKalderon, Michal 				 u8 fw_event_code, u16 echo,
300667b40dccSKalderon, Michal 				 union event_ring_data *data,
300767b40dccSKalderon, Michal 				 u8 fw_return_code)
300867b40dccSKalderon, Michal {
3009*39dbc646SYuval Bason 	struct qed_rdma_events events = p_hwfn->p_rdma_info->events;
3010456a5849SKalderon, Michal 	struct regpair *fw_handle = &data->rdma_data.async_handle;
3011456a5849SKalderon, Michal 	struct qed_iwarp_ep *ep = NULL;
3012*39dbc646SYuval Bason 	u16 srq_offset;
3013*39dbc646SYuval Bason 	u16 srq_id;
3014456a5849SKalderon, Michal 	u16 cid;
3015456a5849SKalderon, Michal 
3016456a5849SKalderon, Michal 	ep = (struct qed_iwarp_ep *)(uintptr_t)HILO_64(fw_handle->hi,
3017456a5849SKalderon, Michal 						       fw_handle->lo);
3018456a5849SKalderon, Michal 
3019456a5849SKalderon, Michal 	switch (fw_event_code) {
3020456a5849SKalderon, Michal 	case IWARP_EVENT_TYPE_ASYNC_CONNECT_COMPLETE:
3021456a5849SKalderon, Michal 		/* Async completion after TCP 3-way handshake */
3022456a5849SKalderon, Michal 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
3023456a5849SKalderon, Michal 			return -EINVAL;
3024456a5849SKalderon, Michal 		DP_VERBOSE(p_hwfn,
3025456a5849SKalderon, Michal 			   QED_MSG_RDMA,
3026456a5849SKalderon, Michal 			   "EP(0x%x) IWARP_EVENT_TYPE_ASYNC_CONNECT_COMPLETE fw_ret_code=%d\n",
3027456a5849SKalderon, Michal 			   ep->tcp_cid, fw_return_code);
3028456a5849SKalderon, Michal 		qed_iwarp_connect_complete(p_hwfn, ep, fw_return_code);
3029456a5849SKalderon, Michal 		break;
3030fc4c6065SKalderon, Michal 	case IWARP_EVENT_TYPE_ASYNC_EXCEPTION_DETECTED:
3031fc4c6065SKalderon, Michal 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
3032fc4c6065SKalderon, Michal 			return -EINVAL;
3033fc4c6065SKalderon, Michal 		DP_VERBOSE(p_hwfn,
3034fc4c6065SKalderon, Michal 			   QED_MSG_RDMA,
3035fc4c6065SKalderon, Michal 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_EXCEPTION_DETECTED fw_ret_code=%d\n",
3036fc4c6065SKalderon, Michal 			   ep->cid, fw_return_code);
3037fc4c6065SKalderon, Michal 		qed_iwarp_exception_received(p_hwfn, ep, fw_return_code);
3038fc4c6065SKalderon, Michal 		break;
3039fc4c6065SKalderon, Michal 	case IWARP_EVENT_TYPE_ASYNC_QP_IN_ERROR_STATE:
3040fc4c6065SKalderon, Michal 		/* Async completion for Close Connection ramrod */
3041fc4c6065SKalderon, Michal 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
3042fc4c6065SKalderon, Michal 			return -EINVAL;
3043fc4c6065SKalderon, Michal 		DP_VERBOSE(p_hwfn,
3044fc4c6065SKalderon, Michal 			   QED_MSG_RDMA,
3045fc4c6065SKalderon, Michal 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_QP_IN_ERROR_STATE fw_ret_code=%d\n",
3046fc4c6065SKalderon, Michal 			   ep->cid, fw_return_code);
3047fc4c6065SKalderon, Michal 		qed_iwarp_qp_in_error(p_hwfn, ep, fw_return_code);
3048fc4c6065SKalderon, Michal 		break;
30494b0fdd7cSKalderon, Michal 	case IWARP_EVENT_TYPE_ASYNC_ENHANCED_MPA_REPLY_ARRIVED:
3050fc4c6065SKalderon, Michal 		/* Async event for active side only */
30514b0fdd7cSKalderon, Michal 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
30524b0fdd7cSKalderon, Michal 			return -EINVAL;
30534b0fdd7cSKalderon, Michal 		DP_VERBOSE(p_hwfn,
30544b0fdd7cSKalderon, Michal 			   QED_MSG_RDMA,
30554b0fdd7cSKalderon, Michal 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_MPA_REPLY_ARRIVED fw_ret_code=%d\n",
30564b0fdd7cSKalderon, Michal 			   ep->cid, fw_return_code);
30574b0fdd7cSKalderon, Michal 		qed_iwarp_mpa_reply_arrived(p_hwfn, ep);
30584b0fdd7cSKalderon, Michal 		break;
3059456a5849SKalderon, Michal 	case IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_COMPLETE:
3060456a5849SKalderon, Michal 		if (!qed_iwarp_check_ep_ok(p_hwfn, ep))
3061456a5849SKalderon, Michal 			return -EINVAL;
3062456a5849SKalderon, Michal 		DP_VERBOSE(p_hwfn,
3063456a5849SKalderon, Michal 			   QED_MSG_RDMA,
3064456a5849SKalderon, Michal 			   "QP(0x%x) IWARP_EVENT_TYPE_ASYNC_MPA_HANDSHAKE_COMPLETE fw_ret_code=%d\n",
3065456a5849SKalderon, Michal 			   ep->cid, fw_return_code);
3066456a5849SKalderon, Michal 		qed_iwarp_mpa_complete(p_hwfn, ep, fw_return_code);
3067456a5849SKalderon, Michal 		break;
3068456a5849SKalderon, Michal 	case IWARP_EVENT_TYPE_ASYNC_CID_CLEANED:
3069456a5849SKalderon, Michal 		cid = (u16)le32_to_cpu(fw_handle->lo);
3070456a5849SKalderon, Michal 		DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
3071456a5849SKalderon, Michal 			   "(0x%x)IWARP_EVENT_TYPE_ASYNC_CID_CLEANED\n", cid);
3072456a5849SKalderon, Michal 		qed_iwarp_cid_cleaned(p_hwfn, cid);
3073456a5849SKalderon, Michal 
3074456a5849SKalderon, Michal 		break;
3075*39dbc646SYuval Bason 	case IWARP_EVENT_TYPE_ASYNC_SRQ_EMPTY:
3076*39dbc646SYuval Bason 		DP_NOTICE(p_hwfn, "IWARP_EVENT_TYPE_ASYNC_SRQ_EMPTY\n");
3077*39dbc646SYuval Bason 		srq_offset = p_hwfn->p_rdma_info->srq_id_offset;
3078*39dbc646SYuval Bason 		/* FW assigns value that is no greater than u16 */
3079*39dbc646SYuval Bason 		srq_id = ((u16)le32_to_cpu(fw_handle->lo)) - srq_offset;
3080*39dbc646SYuval Bason 		events.affiliated_event(events.context,
3081*39dbc646SYuval Bason 					QED_IWARP_EVENT_SRQ_EMPTY,
3082*39dbc646SYuval Bason 					&srq_id);
3083*39dbc646SYuval Bason 		break;
3084*39dbc646SYuval Bason 	case IWARP_EVENT_TYPE_ASYNC_SRQ_LIMIT:
3085*39dbc646SYuval Bason 		DP_NOTICE(p_hwfn, "IWARP_EVENT_TYPE_ASYNC_SRQ_LIMIT\n");
3086*39dbc646SYuval Bason 		srq_offset = p_hwfn->p_rdma_info->srq_id_offset;
3087*39dbc646SYuval Bason 		/* FW assigns value that is no greater than u16 */
3088*39dbc646SYuval Bason 		srq_id = ((u16)le32_to_cpu(fw_handle->lo)) - srq_offset;
3089*39dbc646SYuval Bason 		events.affiliated_event(events.context,
3090*39dbc646SYuval Bason 					QED_IWARP_EVENT_SRQ_LIMIT,
3091*39dbc646SYuval Bason 					&srq_id);
3092*39dbc646SYuval Bason 		break;
30939816b614SKalderon, Michal 	case IWARP_EVENT_TYPE_ASYNC_CQ_OVERFLOW:
30949816b614SKalderon, Michal 		DP_NOTICE(p_hwfn, "IWARP_EVENT_TYPE_ASYNC_CQ_OVERFLOW\n");
30959816b614SKalderon, Michal 
30969816b614SKalderon, Michal 		p_hwfn->p_rdma_info->events.affiliated_event(
30979816b614SKalderon, Michal 			p_hwfn->p_rdma_info->events.context,
30989816b614SKalderon, Michal 			QED_IWARP_EVENT_CQ_OVERFLOW,
30999816b614SKalderon, Michal 			(void *)fw_handle);
31009816b614SKalderon, Michal 		break;
31019816b614SKalderon, Michal 	default:
31029816b614SKalderon, Michal 		DP_ERR(p_hwfn, "Received unexpected async iwarp event %d\n",
31039816b614SKalderon, Michal 		       fw_event_code);
31049816b614SKalderon, Michal 		return -EINVAL;
3105456a5849SKalderon, Michal 	}
310667b40dccSKalderon, Michal 	return 0;
310767b40dccSKalderon, Michal }
310867b40dccSKalderon, Michal 
310965a91a6cSKalderon, Michal int
311065a91a6cSKalderon, Michal qed_iwarp_create_listen(void *rdma_cxt,
311165a91a6cSKalderon, Michal 			struct qed_iwarp_listen_in *iparams,
311265a91a6cSKalderon, Michal 			struct qed_iwarp_listen_out *oparams)
311365a91a6cSKalderon, Michal {
311465a91a6cSKalderon, Michal 	struct qed_hwfn *p_hwfn = rdma_cxt;
311565a91a6cSKalderon, Michal 	struct qed_iwarp_listener *listener;
311665a91a6cSKalderon, Michal 
311765a91a6cSKalderon, Michal 	listener = kzalloc(sizeof(*listener), GFP_KERNEL);
311865a91a6cSKalderon, Michal 	if (!listener)
311965a91a6cSKalderon, Michal 		return -ENOMEM;
312065a91a6cSKalderon, Michal 
312165a91a6cSKalderon, Michal 	listener->ip_version = iparams->ip_version;
312265a91a6cSKalderon, Michal 	memcpy(listener->ip_addr, iparams->ip_addr, sizeof(listener->ip_addr));
312365a91a6cSKalderon, Michal 	listener->port = iparams->port;
312465a91a6cSKalderon, Michal 	listener->vlan = iparams->vlan;
312565a91a6cSKalderon, Michal 
312665a91a6cSKalderon, Michal 	listener->event_cb = iparams->event_cb;
312765a91a6cSKalderon, Michal 	listener->cb_context = iparams->cb_context;
312865a91a6cSKalderon, Michal 	listener->max_backlog = iparams->max_backlog;
312965a91a6cSKalderon, Michal 	oparams->handle = listener;
313065a91a6cSKalderon, Michal 
313165a91a6cSKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
313265a91a6cSKalderon, Michal 	list_add_tail(&listener->list_entry,
313365a91a6cSKalderon, Michal 		      &p_hwfn->p_rdma_info->iwarp.listen_list);
313465a91a6cSKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
313565a91a6cSKalderon, Michal 
313665a91a6cSKalderon, Michal 	DP_VERBOSE(p_hwfn,
313765a91a6cSKalderon, Michal 		   QED_MSG_RDMA,
313865a91a6cSKalderon, Michal 		   "callback=%p handle=%p ip=%x:%x:%x:%x port=0x%x vlan=0x%x\n",
313965a91a6cSKalderon, Michal 		   listener->event_cb,
314065a91a6cSKalderon, Michal 		   listener,
314165a91a6cSKalderon, Michal 		   listener->ip_addr[0],
314265a91a6cSKalderon, Michal 		   listener->ip_addr[1],
314365a91a6cSKalderon, Michal 		   listener->ip_addr[2],
314465a91a6cSKalderon, Michal 		   listener->ip_addr[3], listener->port, listener->vlan);
314565a91a6cSKalderon, Michal 
314665a91a6cSKalderon, Michal 	return 0;
314765a91a6cSKalderon, Michal }
314865a91a6cSKalderon, Michal 
314965a91a6cSKalderon, Michal int qed_iwarp_destroy_listen(void *rdma_cxt, void *handle)
315065a91a6cSKalderon, Michal {
315165a91a6cSKalderon, Michal 	struct qed_iwarp_listener *listener = handle;
315265a91a6cSKalderon, Michal 	struct qed_hwfn *p_hwfn = rdma_cxt;
315365a91a6cSKalderon, Michal 
315465a91a6cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "handle=%p\n", handle);
315565a91a6cSKalderon, Michal 
315665a91a6cSKalderon, Michal 	spin_lock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
315765a91a6cSKalderon, Michal 	list_del(&listener->list_entry);
315865a91a6cSKalderon, Michal 	spin_unlock_bh(&p_hwfn->p_rdma_info->iwarp.iw_lock);
315965a91a6cSKalderon, Michal 
316065a91a6cSKalderon, Michal 	kfree(listener);
316165a91a6cSKalderon, Michal 
316265a91a6cSKalderon, Michal 	return 0;
316365a91a6cSKalderon, Michal }
316465a91a6cSKalderon, Michal 
31654b0fdd7cSKalderon, Michal int qed_iwarp_send_rtr(void *rdma_cxt, struct qed_iwarp_send_rtr_in *iparams)
31664b0fdd7cSKalderon, Michal {
31674b0fdd7cSKalderon, Michal 	struct qed_hwfn *p_hwfn = rdma_cxt;
31684b0fdd7cSKalderon, Michal 	struct qed_sp_init_data init_data;
31694b0fdd7cSKalderon, Michal 	struct qed_spq_entry *p_ent;
31704b0fdd7cSKalderon, Michal 	struct qed_iwarp_ep *ep;
31714b0fdd7cSKalderon, Michal 	struct qed_rdma_qp *qp;
31724b0fdd7cSKalderon, Michal 	int rc;
31734b0fdd7cSKalderon, Michal 
31744b0fdd7cSKalderon, Michal 	ep = iparams->ep_context;
31754b0fdd7cSKalderon, Michal 	if (!ep) {
31764b0fdd7cSKalderon, Michal 		DP_ERR(p_hwfn, "Ep Context receive in send_rtr is NULL\n");
31774b0fdd7cSKalderon, Michal 		return -EINVAL;
31784b0fdd7cSKalderon, Michal 	}
31794b0fdd7cSKalderon, Michal 
31804b0fdd7cSKalderon, Michal 	qp = ep->qp;
31814b0fdd7cSKalderon, Michal 
31824b0fdd7cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP(0x%x) EP(0x%x)\n",
31834b0fdd7cSKalderon, Michal 		   qp->icid, ep->tcp_cid);
31844b0fdd7cSKalderon, Michal 
31854b0fdd7cSKalderon, Michal 	memset(&init_data, 0, sizeof(init_data));
31864b0fdd7cSKalderon, Michal 	init_data.cid = qp->icid;
31874b0fdd7cSKalderon, Michal 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
31884b0fdd7cSKalderon, Michal 	init_data.comp_mode = QED_SPQ_MODE_CB;
31894b0fdd7cSKalderon, Michal 
31904b0fdd7cSKalderon, Michal 	rc = qed_sp_init_request(p_hwfn, &p_ent,
31914b0fdd7cSKalderon, Michal 				 IWARP_RAMROD_CMD_ID_MPA_OFFLOAD_SEND_RTR,
31924b0fdd7cSKalderon, Michal 				 PROTOCOLID_IWARP, &init_data);
31934b0fdd7cSKalderon, Michal 
31944b0fdd7cSKalderon, Michal 	if (rc)
31954b0fdd7cSKalderon, Michal 		return rc;
31964b0fdd7cSKalderon, Michal 
31974b0fdd7cSKalderon, Michal 	rc = qed_spq_post(p_hwfn, p_ent, NULL);
31984b0fdd7cSKalderon, Michal 
31994b0fdd7cSKalderon, Michal 	DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "rc = 0x%x\n", rc);
32004b0fdd7cSKalderon, Michal 
32014b0fdd7cSKalderon, Michal 	return rc;
32024b0fdd7cSKalderon, Michal }
32034b0fdd7cSKalderon, Michal 
320467b40dccSKalderon, Michal void
320567b40dccSKalderon, Michal qed_iwarp_query_qp(struct qed_rdma_qp *qp,
320667b40dccSKalderon, Michal 		   struct qed_rdma_query_qp_out_params *out_params)
320767b40dccSKalderon, Michal {
320867b40dccSKalderon, Michal 	out_params->state = qed_iwarp2roce_state(qp->iwarp_state);
320967b40dccSKalderon, Michal }
3210