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