GNU Linux-libre 4.19.263-gnu1
[releases.git] / drivers / net / ethernet / qlogic / qed / qed_ll2.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
33 #include <linux/types.h>
34 #include <asm/byteorder.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/if_vlan.h>
37 #include <linux/kernel.h>
38 #include <linux/pci.h>
39 #include <linux/slab.h>
40 #include <linux/stddef.h>
41 #include <linux/workqueue.h>
42 #include <net/ipv6.h>
43 #include <linux/bitops.h>
44 #include <linux/delay.h>
45 #include <linux/errno.h>
46 #include <linux/etherdevice.h>
47 #include <linux/io.h>
48 #include <linux/list.h>
49 #include <linux/mutex.h>
50 #include <linux/spinlock.h>
51 #include <linux/string.h>
52 #include <linux/qed/qed_ll2_if.h>
53 #include "qed.h"
54 #include "qed_cxt.h"
55 #include "qed_dev_api.h"
56 #include "qed_hsi.h"
57 #include "qed_hw.h"
58 #include "qed_int.h"
59 #include "qed_ll2.h"
60 #include "qed_mcp.h"
61 #include "qed_ooo.h"
62 #include "qed_reg_addr.h"
63 #include "qed_sp.h"
64 #include "qed_rdma.h"
65
66 #define QED_LL2_RX_REGISTERED(ll2)      ((ll2)->rx_queue.b_cb_registred)
67 #define QED_LL2_TX_REGISTERED(ll2)      ((ll2)->tx_queue.b_cb_registred)
68
69 #define QED_LL2_TX_SIZE (256)
70 #define QED_LL2_RX_SIZE (4096)
71
72 struct qed_cb_ll2_info {
73         int rx_cnt;
74         u32 rx_size;
75         u8 handle;
76
77         /* Lock protecting LL2 buffer lists in sleepless context */
78         spinlock_t lock;
79         struct list_head list;
80
81         const struct qed_ll2_cb_ops *cbs;
82         void *cb_cookie;
83 };
84
85 struct qed_ll2_buffer {
86         struct list_head list;
87         void *data;
88         dma_addr_t phys_addr;
89 };
90
91 static void qed_ll2b_complete_tx_packet(void *cxt,
92                                         u8 connection_handle,
93                                         void *cookie,
94                                         dma_addr_t first_frag_addr,
95                                         bool b_last_fragment,
96                                         bool b_last_packet)
97 {
98         struct qed_hwfn *p_hwfn = cxt;
99         struct qed_dev *cdev = p_hwfn->cdev;
100         struct sk_buff *skb = cookie;
101
102         /* All we need to do is release the mapping */
103         dma_unmap_single(&p_hwfn->cdev->pdev->dev, first_frag_addr,
104                          skb_headlen(skb), DMA_TO_DEVICE);
105
106         if (cdev->ll2->cbs && cdev->ll2->cbs->tx_cb)
107                 cdev->ll2->cbs->tx_cb(cdev->ll2->cb_cookie, skb,
108                                       b_last_fragment);
109
110         dev_kfree_skb_any(skb);
111 }
112
113 static int qed_ll2_alloc_buffer(struct qed_dev *cdev,
114                                 u8 **data, dma_addr_t *phys_addr)
115 {
116         *data = kmalloc(cdev->ll2->rx_size, GFP_ATOMIC);
117         if (!(*data)) {
118                 DP_INFO(cdev, "Failed to allocate LL2 buffer data\n");
119                 return -ENOMEM;
120         }
121
122         *phys_addr = dma_map_single(&cdev->pdev->dev,
123                                     ((*data) + NET_SKB_PAD),
124                                     cdev->ll2->rx_size, DMA_FROM_DEVICE);
125         if (dma_mapping_error(&cdev->pdev->dev, *phys_addr)) {
126                 DP_INFO(cdev, "Failed to map LL2 buffer data\n");
127                 kfree((*data));
128                 return -ENOMEM;
129         }
130
131         return 0;
132 }
133
134 static int qed_ll2_dealloc_buffer(struct qed_dev *cdev,
135                                  struct qed_ll2_buffer *buffer)
136 {
137         spin_lock_bh(&cdev->ll2->lock);
138
139         dma_unmap_single(&cdev->pdev->dev, buffer->phys_addr,
140                          cdev->ll2->rx_size, DMA_FROM_DEVICE);
141         kfree(buffer->data);
142         list_del(&buffer->list);
143
144         cdev->ll2->rx_cnt--;
145         if (!cdev->ll2->rx_cnt)
146                 DP_INFO(cdev, "All LL2 entries were removed\n");
147
148         spin_unlock_bh(&cdev->ll2->lock);
149
150         return 0;
151 }
152
153 static void qed_ll2_kill_buffers(struct qed_dev *cdev)
154 {
155         struct qed_ll2_buffer *buffer, *tmp_buffer;
156
157         list_for_each_entry_safe(buffer, tmp_buffer, &cdev->ll2->list, list)
158                 qed_ll2_dealloc_buffer(cdev, buffer);
159 }
160
161 static void qed_ll2b_complete_rx_packet(void *cxt,
162                                         struct qed_ll2_comp_rx_data *data)
163 {
164         struct qed_hwfn *p_hwfn = cxt;
165         struct qed_ll2_buffer *buffer = data->cookie;
166         struct qed_dev *cdev = p_hwfn->cdev;
167         dma_addr_t new_phys_addr;
168         struct sk_buff *skb;
169         bool reuse = false;
170         int rc = -EINVAL;
171         u8 *new_data;
172
173         DP_VERBOSE(p_hwfn,
174                    (NETIF_MSG_RX_STATUS | QED_MSG_STORAGE | NETIF_MSG_PKTDATA),
175                    "Got an LL2 Rx completion: [Buffer at phys 0x%llx, offset 0x%02x] Length 0x%04x Parse_flags 0x%04x vlan 0x%04x Opaque data [0x%08x:0x%08x]\n",
176                    (u64)data->rx_buf_addr,
177                    data->u.placement_offset,
178                    data->length.packet_length,
179                    data->parse_flags,
180                    data->vlan, data->opaque_data_0, data->opaque_data_1);
181
182         if ((cdev->dp_module & NETIF_MSG_PKTDATA) && buffer->data) {
183                 print_hex_dump(KERN_INFO, "",
184                                DUMP_PREFIX_OFFSET, 16, 1,
185                                buffer->data, data->length.packet_length, false);
186         }
187
188         /* Determine if data is valid */
189         if (data->length.packet_length < ETH_HLEN)
190                 reuse = true;
191
192         /* Allocate a replacement for buffer; Reuse upon failure */
193         if (!reuse)
194                 rc = qed_ll2_alloc_buffer(p_hwfn->cdev, &new_data,
195                                           &new_phys_addr);
196
197         /* If need to reuse or there's no replacement buffer, repost this */
198         if (rc)
199                 goto out_post;
200         dma_unmap_single(&cdev->pdev->dev, buffer->phys_addr,
201                          cdev->ll2->rx_size, DMA_FROM_DEVICE);
202
203         skb = build_skb(buffer->data, 0);
204         if (!skb) {
205                 DP_INFO(cdev, "Failed to build SKB\n");
206                 kfree(buffer->data);
207                 goto out_post1;
208         }
209
210         data->u.placement_offset += NET_SKB_PAD;
211         skb_reserve(skb, data->u.placement_offset);
212         skb_put(skb, data->length.packet_length);
213         skb_checksum_none_assert(skb);
214
215         /* Get parital ethernet information instead of eth_type_trans(),
216          * Since we don't have an associated net_device.
217          */
218         skb_reset_mac_header(skb);
219         skb->protocol = eth_hdr(skb)->h_proto;
220
221         /* Pass SKB onward */
222         if (cdev->ll2->cbs && cdev->ll2->cbs->rx_cb) {
223                 if (data->vlan)
224                         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
225                                                data->vlan);
226                 cdev->ll2->cbs->rx_cb(cdev->ll2->cb_cookie, skb,
227                                       data->opaque_data_0,
228                                       data->opaque_data_1);
229         } else {
230                 DP_VERBOSE(p_hwfn, (NETIF_MSG_RX_STATUS | NETIF_MSG_PKTDATA |
231                                     QED_MSG_LL2 | QED_MSG_STORAGE),
232                            "Dropping the packet\n");
233                 kfree(buffer->data);
234         }
235
236 out_post1:
237         /* Update Buffer information and update FW producer */
238         buffer->data = new_data;
239         buffer->phys_addr = new_phys_addr;
240
241 out_post:
242         rc = qed_ll2_post_rx_buffer(QED_LEADING_HWFN(cdev), cdev->ll2->handle,
243                                     buffer->phys_addr, 0,  buffer, 1);
244
245         if (rc)
246                 qed_ll2_dealloc_buffer(cdev, buffer);
247 }
248
249 static struct qed_ll2_info *__qed_ll2_handle_sanity(struct qed_hwfn *p_hwfn,
250                                                     u8 connection_handle,
251                                                     bool b_lock,
252                                                     bool b_only_active)
253 {
254         struct qed_ll2_info *p_ll2_conn, *p_ret = NULL;
255
256         if (connection_handle >= QED_MAX_NUM_OF_LL2_CONNECTIONS)
257                 return NULL;
258
259         if (!p_hwfn->p_ll2_info)
260                 return NULL;
261
262         p_ll2_conn = &p_hwfn->p_ll2_info[connection_handle];
263
264         if (b_only_active) {
265                 if (b_lock)
266                         mutex_lock(&p_ll2_conn->mutex);
267                 if (p_ll2_conn->b_active)
268                         p_ret = p_ll2_conn;
269                 if (b_lock)
270                         mutex_unlock(&p_ll2_conn->mutex);
271         } else {
272                 p_ret = p_ll2_conn;
273         }
274
275         return p_ret;
276 }
277
278 static struct qed_ll2_info *qed_ll2_handle_sanity(struct qed_hwfn *p_hwfn,
279                                                   u8 connection_handle)
280 {
281         return __qed_ll2_handle_sanity(p_hwfn, connection_handle, false, true);
282 }
283
284 static struct qed_ll2_info *qed_ll2_handle_sanity_lock(struct qed_hwfn *p_hwfn,
285                                                        u8 connection_handle)
286 {
287         return __qed_ll2_handle_sanity(p_hwfn, connection_handle, true, true);
288 }
289
290 static struct qed_ll2_info *qed_ll2_handle_sanity_inactive(struct qed_hwfn
291                                                            *p_hwfn,
292                                                            u8 connection_handle)
293 {
294         return __qed_ll2_handle_sanity(p_hwfn, connection_handle, false, false);
295 }
296
297 static void qed_ll2_txq_flush(struct qed_hwfn *p_hwfn, u8 connection_handle)
298 {
299         bool b_last_packet = false, b_last_frag = false;
300         struct qed_ll2_tx_packet *p_pkt = NULL;
301         struct qed_ll2_info *p_ll2_conn;
302         struct qed_ll2_tx_queue *p_tx;
303         unsigned long flags = 0;
304         dma_addr_t tx_frag;
305
306         p_ll2_conn = qed_ll2_handle_sanity_inactive(p_hwfn, connection_handle);
307         if (!p_ll2_conn)
308                 return;
309
310         p_tx = &p_ll2_conn->tx_queue;
311
312         spin_lock_irqsave(&p_tx->lock, flags);
313         while (!list_empty(&p_tx->active_descq)) {
314                 p_pkt = list_first_entry(&p_tx->active_descq,
315                                          struct qed_ll2_tx_packet, list_entry);
316                 if (!p_pkt)
317                         break;
318
319                 list_del(&p_pkt->list_entry);
320                 b_last_packet = list_empty(&p_tx->active_descq);
321                 list_add_tail(&p_pkt->list_entry, &p_tx->free_descq);
322                 spin_unlock_irqrestore(&p_tx->lock, flags);
323                 if (p_ll2_conn->input.conn_type == QED_LL2_TYPE_OOO) {
324                         struct qed_ooo_buffer *p_buffer;
325
326                         p_buffer = (struct qed_ooo_buffer *)p_pkt->cookie;
327                         qed_ooo_put_free_buffer(p_hwfn, p_hwfn->p_ooo_info,
328                                                 p_buffer);
329                 } else {
330                         p_tx->cur_completing_packet = *p_pkt;
331                         p_tx->cur_completing_bd_idx = 1;
332                         b_last_frag =
333                                 p_tx->cur_completing_bd_idx == p_pkt->bd_used;
334                         tx_frag = p_pkt->bds_set[0].tx_frag;
335                         p_ll2_conn->cbs.tx_release_cb(p_ll2_conn->cbs.cookie,
336                                                       p_ll2_conn->my_id,
337                                                       p_pkt->cookie,
338                                                       tx_frag,
339                                                       b_last_frag,
340                                                       b_last_packet);
341                 }
342                 spin_lock_irqsave(&p_tx->lock, flags);
343         }
344         spin_unlock_irqrestore(&p_tx->lock, flags);
345 }
346
347 static int qed_ll2_txq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
348 {
349         struct qed_ll2_info *p_ll2_conn = p_cookie;
350         struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
351         u16 new_idx = 0, num_bds = 0, num_bds_in_packet = 0;
352         struct qed_ll2_tx_packet *p_pkt;
353         bool b_last_frag = false;
354         unsigned long flags;
355         int rc = -EINVAL;
356
357         if (!p_ll2_conn)
358                 return rc;
359
360         spin_lock_irqsave(&p_tx->lock, flags);
361         if (p_tx->b_completing_packet) {
362                 rc = -EBUSY;
363                 goto out;
364         }
365
366         new_idx = le16_to_cpu(*p_tx->p_fw_cons);
367         num_bds = ((s16)new_idx - (s16)p_tx->bds_idx);
368         while (num_bds) {
369                 if (list_empty(&p_tx->active_descq))
370                         goto out;
371
372                 p_pkt = list_first_entry(&p_tx->active_descq,
373                                          struct qed_ll2_tx_packet, list_entry);
374                 if (!p_pkt)
375                         goto out;
376
377                 p_tx->b_completing_packet = true;
378                 p_tx->cur_completing_packet = *p_pkt;
379                 num_bds_in_packet = p_pkt->bd_used;
380                 list_del(&p_pkt->list_entry);
381
382                 if (num_bds < num_bds_in_packet) {
383                         DP_NOTICE(p_hwfn,
384                                   "Rest of BDs does not cover whole packet\n");
385                         goto out;
386                 }
387
388                 num_bds -= num_bds_in_packet;
389                 p_tx->bds_idx += num_bds_in_packet;
390                 while (num_bds_in_packet--)
391                         qed_chain_consume(&p_tx->txq_chain);
392
393                 p_tx->cur_completing_bd_idx = 1;
394                 b_last_frag = p_tx->cur_completing_bd_idx == p_pkt->bd_used;
395                 list_add_tail(&p_pkt->list_entry, &p_tx->free_descq);
396
397                 spin_unlock_irqrestore(&p_tx->lock, flags);
398
399                 p_ll2_conn->cbs.tx_comp_cb(p_ll2_conn->cbs.cookie,
400                                            p_ll2_conn->my_id,
401                                            p_pkt->cookie,
402                                            p_pkt->bds_set[0].tx_frag,
403                                            b_last_frag, !num_bds);
404
405                 spin_lock_irqsave(&p_tx->lock, flags);
406         }
407
408         p_tx->b_completing_packet = false;
409         rc = 0;
410 out:
411         spin_unlock_irqrestore(&p_tx->lock, flags);
412         return rc;
413 }
414
415 static void qed_ll2_rxq_parse_gsi(struct qed_hwfn *p_hwfn,
416                                   union core_rx_cqe_union *p_cqe,
417                                   struct qed_ll2_comp_rx_data *data)
418 {
419         data->parse_flags = le16_to_cpu(p_cqe->rx_cqe_gsi.parse_flags.flags);
420         data->length.data_length = le16_to_cpu(p_cqe->rx_cqe_gsi.data_length);
421         data->vlan = le16_to_cpu(p_cqe->rx_cqe_gsi.vlan);
422         data->opaque_data_0 = le32_to_cpu(p_cqe->rx_cqe_gsi.src_mac_addrhi);
423         data->opaque_data_1 = le16_to_cpu(p_cqe->rx_cqe_gsi.src_mac_addrlo);
424         data->u.data_length_error = p_cqe->rx_cqe_gsi.data_length_error;
425         data->qp_id = le16_to_cpu(p_cqe->rx_cqe_gsi.qp_id);
426
427         data->src_qp = le32_to_cpu(p_cqe->rx_cqe_gsi.src_qp);
428 }
429
430 static void qed_ll2_rxq_parse_reg(struct qed_hwfn *p_hwfn,
431                                   union core_rx_cqe_union *p_cqe,
432                                   struct qed_ll2_comp_rx_data *data)
433 {
434         data->parse_flags = le16_to_cpu(p_cqe->rx_cqe_fp.parse_flags.flags);
435         data->err_flags = le16_to_cpu(p_cqe->rx_cqe_fp.err_flags.flags);
436         data->length.packet_length =
437             le16_to_cpu(p_cqe->rx_cqe_fp.packet_length);
438         data->vlan = le16_to_cpu(p_cqe->rx_cqe_fp.vlan);
439         data->opaque_data_0 = le32_to_cpu(p_cqe->rx_cqe_fp.opaque_data.data[0]);
440         data->opaque_data_1 = le32_to_cpu(p_cqe->rx_cqe_fp.opaque_data.data[1]);
441         data->u.placement_offset = p_cqe->rx_cqe_fp.placement_offset;
442 }
443
444 static int
445 qed_ll2_handle_slowpath(struct qed_hwfn *p_hwfn,
446                         struct qed_ll2_info *p_ll2_conn,
447                         union core_rx_cqe_union *p_cqe,
448                         unsigned long *p_lock_flags)
449 {
450         struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
451         struct core_rx_slow_path_cqe *sp_cqe;
452
453         sp_cqe = &p_cqe->rx_cqe_sp;
454         if (sp_cqe->ramrod_cmd_id != CORE_RAMROD_RX_QUEUE_FLUSH) {
455                 DP_NOTICE(p_hwfn,
456                           "LL2 - unexpected Rx CQE slowpath ramrod_cmd_id:%d\n",
457                           sp_cqe->ramrod_cmd_id);
458                 return -EINVAL;
459         }
460
461         if (!p_ll2_conn->cbs.slowpath_cb) {
462                 DP_NOTICE(p_hwfn,
463                           "LL2 - received RX_QUEUE_FLUSH but no callback was provided\n");
464                 return -EINVAL;
465         }
466
467         spin_unlock_irqrestore(&p_rx->lock, *p_lock_flags);
468
469         p_ll2_conn->cbs.slowpath_cb(p_ll2_conn->cbs.cookie,
470                                     p_ll2_conn->my_id,
471                                     le32_to_cpu(sp_cqe->opaque_data.data[0]),
472                                     le32_to_cpu(sp_cqe->opaque_data.data[1]));
473
474         spin_lock_irqsave(&p_rx->lock, *p_lock_flags);
475
476         return 0;
477 }
478
479 static int
480 qed_ll2_rxq_handle_completion(struct qed_hwfn *p_hwfn,
481                               struct qed_ll2_info *p_ll2_conn,
482                               union core_rx_cqe_union *p_cqe,
483                               unsigned long *p_lock_flags, bool b_last_cqe)
484 {
485         struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
486         struct qed_ll2_rx_packet *p_pkt = NULL;
487         struct qed_ll2_comp_rx_data data;
488
489         if (!list_empty(&p_rx->active_descq))
490                 p_pkt = list_first_entry(&p_rx->active_descq,
491                                          struct qed_ll2_rx_packet, list_entry);
492         if (!p_pkt) {
493                 DP_NOTICE(p_hwfn,
494                           "[%d] LL2 Rx completion but active_descq is empty\n",
495                           p_ll2_conn->input.conn_type);
496
497                 return -EIO;
498         }
499         list_del(&p_pkt->list_entry);
500
501         if (p_cqe->rx_cqe_sp.type == CORE_RX_CQE_TYPE_REGULAR)
502                 qed_ll2_rxq_parse_reg(p_hwfn, p_cqe, &data);
503         else
504                 qed_ll2_rxq_parse_gsi(p_hwfn, p_cqe, &data);
505         if (qed_chain_consume(&p_rx->rxq_chain) != p_pkt->rxq_bd)
506                 DP_NOTICE(p_hwfn,
507                           "Mismatch between active_descq and the LL2 Rx chain\n");
508
509         list_add_tail(&p_pkt->list_entry, &p_rx->free_descq);
510
511         data.connection_handle = p_ll2_conn->my_id;
512         data.cookie = p_pkt->cookie;
513         data.rx_buf_addr = p_pkt->rx_buf_addr;
514         data.b_last_packet = b_last_cqe;
515
516         spin_unlock_irqrestore(&p_rx->lock, *p_lock_flags);
517         p_ll2_conn->cbs.rx_comp_cb(p_ll2_conn->cbs.cookie, &data);
518
519         spin_lock_irqsave(&p_rx->lock, *p_lock_flags);
520
521         return 0;
522 }
523
524 static int qed_ll2_rxq_completion(struct qed_hwfn *p_hwfn, void *cookie)
525 {
526         struct qed_ll2_info *p_ll2_conn = (struct qed_ll2_info *)cookie;
527         struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
528         union core_rx_cqe_union *cqe = NULL;
529         u16 cq_new_idx = 0, cq_old_idx = 0;
530         unsigned long flags = 0;
531         int rc = 0;
532
533         if (!p_ll2_conn)
534                 return rc;
535
536         spin_lock_irqsave(&p_rx->lock, flags);
537
538         if (!QED_LL2_RX_REGISTERED(p_ll2_conn)) {
539                 spin_unlock_irqrestore(&p_rx->lock, flags);
540                 return 0;
541         }
542
543         cq_new_idx = le16_to_cpu(*p_rx->p_fw_cons);
544         cq_old_idx = qed_chain_get_cons_idx(&p_rx->rcq_chain);
545
546         while (cq_new_idx != cq_old_idx) {
547                 bool b_last_cqe = (cq_new_idx == cq_old_idx);
548
549                 cqe =
550                     (union core_rx_cqe_union *)
551                     qed_chain_consume(&p_rx->rcq_chain);
552                 cq_old_idx = qed_chain_get_cons_idx(&p_rx->rcq_chain);
553
554                 DP_VERBOSE(p_hwfn,
555                            QED_MSG_LL2,
556                            "LL2 [sw. cons %04x, fw. at %04x] - Got Packet of type %02x\n",
557                            cq_old_idx, cq_new_idx, cqe->rx_cqe_sp.type);
558
559                 switch (cqe->rx_cqe_sp.type) {
560                 case CORE_RX_CQE_TYPE_SLOW_PATH:
561                         rc = qed_ll2_handle_slowpath(p_hwfn, p_ll2_conn,
562                                                      cqe, &flags);
563                         break;
564                 case CORE_RX_CQE_TYPE_GSI_OFFLOAD:
565                 case CORE_RX_CQE_TYPE_REGULAR:
566                         rc = qed_ll2_rxq_handle_completion(p_hwfn, p_ll2_conn,
567                                                            cqe, &flags,
568                                                            b_last_cqe);
569                         break;
570                 default:
571                         rc = -EIO;
572                 }
573         }
574
575         spin_unlock_irqrestore(&p_rx->lock, flags);
576         return rc;
577 }
578
579 static void qed_ll2_rxq_flush(struct qed_hwfn *p_hwfn, u8 connection_handle)
580 {
581         struct qed_ll2_info *p_ll2_conn = NULL;
582         struct qed_ll2_rx_packet *p_pkt = NULL;
583         struct qed_ll2_rx_queue *p_rx;
584         unsigned long flags = 0;
585
586         p_ll2_conn = qed_ll2_handle_sanity_inactive(p_hwfn, connection_handle);
587         if (!p_ll2_conn)
588                 return;
589
590         p_rx = &p_ll2_conn->rx_queue;
591
592         spin_lock_irqsave(&p_rx->lock, flags);
593         while (!list_empty(&p_rx->active_descq)) {
594                 p_pkt = list_first_entry(&p_rx->active_descq,
595                                          struct qed_ll2_rx_packet, list_entry);
596                 if (!p_pkt)
597                         break;
598                 list_move_tail(&p_pkt->list_entry, &p_rx->free_descq);
599                 spin_unlock_irqrestore(&p_rx->lock, flags);
600
601                 if (p_ll2_conn->input.conn_type == QED_LL2_TYPE_OOO) {
602                         struct qed_ooo_buffer *p_buffer;
603
604                         p_buffer = (struct qed_ooo_buffer *)p_pkt->cookie;
605                         qed_ooo_put_free_buffer(p_hwfn, p_hwfn->p_ooo_info,
606                                                 p_buffer);
607                 } else {
608                         dma_addr_t rx_buf_addr = p_pkt->rx_buf_addr;
609                         void *cookie = p_pkt->cookie;
610                         bool b_last;
611
612                         b_last = list_empty(&p_rx->active_descq);
613                         p_ll2_conn->cbs.rx_release_cb(p_ll2_conn->cbs.cookie,
614                                                       p_ll2_conn->my_id,
615                                                       cookie,
616                                                       rx_buf_addr, b_last);
617                 }
618                 spin_lock_irqsave(&p_rx->lock, flags);
619         }
620         spin_unlock_irqrestore(&p_rx->lock, flags);
621 }
622
623 static bool
624 qed_ll2_lb_rxq_handler_slowpath(struct qed_hwfn *p_hwfn,
625                                 struct core_rx_slow_path_cqe *p_cqe)
626 {
627         struct ooo_opaque *iscsi_ooo;
628         u32 cid;
629
630         if (p_cqe->ramrod_cmd_id != CORE_RAMROD_RX_QUEUE_FLUSH)
631                 return false;
632
633         iscsi_ooo = (struct ooo_opaque *)&p_cqe->opaque_data;
634         if (iscsi_ooo->ooo_opcode != TCP_EVENT_DELETE_ISLES)
635                 return false;
636
637         /* Need to make a flush */
638         cid = le32_to_cpu(iscsi_ooo->cid);
639         qed_ooo_release_connection_isles(p_hwfn, p_hwfn->p_ooo_info, cid);
640
641         return true;
642 }
643
644 static int qed_ll2_lb_rxq_handler(struct qed_hwfn *p_hwfn,
645                                   struct qed_ll2_info *p_ll2_conn)
646 {
647         struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
648         u16 packet_length = 0, parse_flags = 0, vlan = 0;
649         struct qed_ll2_rx_packet *p_pkt = NULL;
650         u32 num_ooo_add_to_peninsula = 0, cid;
651         union core_rx_cqe_union *cqe = NULL;
652         u16 cq_new_idx = 0, cq_old_idx = 0;
653         struct qed_ooo_buffer *p_buffer;
654         struct ooo_opaque *iscsi_ooo;
655         u8 placement_offset = 0;
656         u8 cqe_type;
657
658         cq_new_idx = le16_to_cpu(*p_rx->p_fw_cons);
659         cq_old_idx = qed_chain_get_cons_idx(&p_rx->rcq_chain);
660         if (cq_new_idx == cq_old_idx)
661                 return 0;
662
663         while (cq_new_idx != cq_old_idx) {
664                 struct core_rx_fast_path_cqe *p_cqe_fp;
665
666                 cqe = qed_chain_consume(&p_rx->rcq_chain);
667                 cq_old_idx = qed_chain_get_cons_idx(&p_rx->rcq_chain);
668                 cqe_type = cqe->rx_cqe_sp.type;
669
670                 if (cqe_type == CORE_RX_CQE_TYPE_SLOW_PATH)
671                         if (qed_ll2_lb_rxq_handler_slowpath(p_hwfn,
672                                                             &cqe->rx_cqe_sp))
673                                 continue;
674
675                 if (cqe_type != CORE_RX_CQE_TYPE_REGULAR) {
676                         DP_NOTICE(p_hwfn,
677                                   "Got a non-regular LB LL2 completion [type 0x%02x]\n",
678                                   cqe_type);
679                         return -EINVAL;
680                 }
681                 p_cqe_fp = &cqe->rx_cqe_fp;
682
683                 placement_offset = p_cqe_fp->placement_offset;
684                 parse_flags = le16_to_cpu(p_cqe_fp->parse_flags.flags);
685                 packet_length = le16_to_cpu(p_cqe_fp->packet_length);
686                 vlan = le16_to_cpu(p_cqe_fp->vlan);
687                 iscsi_ooo = (struct ooo_opaque *)&p_cqe_fp->opaque_data;
688                 qed_ooo_save_history_entry(p_hwfn, p_hwfn->p_ooo_info,
689                                            iscsi_ooo);
690                 cid = le32_to_cpu(iscsi_ooo->cid);
691
692                 /* Process delete isle first */
693                 if (iscsi_ooo->drop_size)
694                         qed_ooo_delete_isles(p_hwfn, p_hwfn->p_ooo_info, cid,
695                                              iscsi_ooo->drop_isle,
696                                              iscsi_ooo->drop_size);
697
698                 if (iscsi_ooo->ooo_opcode == TCP_EVENT_NOP)
699                         continue;
700
701                 /* Now process create/add/join isles */
702                 if (list_empty(&p_rx->active_descq)) {
703                         DP_NOTICE(p_hwfn,
704                                   "LL2 OOO RX chain has no submitted buffers\n"
705                                   );
706                         return -EIO;
707                 }
708
709                 p_pkt = list_first_entry(&p_rx->active_descq,
710                                          struct qed_ll2_rx_packet, list_entry);
711
712                 if ((iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_NEW_ISLE) ||
713                     (iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_ISLE_RIGHT) ||
714                     (iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_ISLE_LEFT) ||
715                     (iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_PEN) ||
716                     (iscsi_ooo->ooo_opcode == TCP_EVENT_JOIN)) {
717                         if (!p_pkt) {
718                                 DP_NOTICE(p_hwfn,
719                                           "LL2 OOO RX packet is not valid\n");
720                                 return -EIO;
721                         }
722                         list_del(&p_pkt->list_entry);
723                         p_buffer = (struct qed_ooo_buffer *)p_pkt->cookie;
724                         p_buffer->packet_length = packet_length;
725                         p_buffer->parse_flags = parse_flags;
726                         p_buffer->vlan = vlan;
727                         p_buffer->placement_offset = placement_offset;
728                         qed_chain_consume(&p_rx->rxq_chain);
729                         list_add_tail(&p_pkt->list_entry, &p_rx->free_descq);
730
731                         switch (iscsi_ooo->ooo_opcode) {
732                         case TCP_EVENT_ADD_NEW_ISLE:
733                                 qed_ooo_add_new_isle(p_hwfn,
734                                                      p_hwfn->p_ooo_info,
735                                                      cid,
736                                                      iscsi_ooo->ooo_isle,
737                                                      p_buffer);
738                                 break;
739                         case TCP_EVENT_ADD_ISLE_RIGHT:
740                                 qed_ooo_add_new_buffer(p_hwfn,
741                                                        p_hwfn->p_ooo_info,
742                                                        cid,
743                                                        iscsi_ooo->ooo_isle,
744                                                        p_buffer,
745                                                        QED_OOO_RIGHT_BUF);
746                                 break;
747                         case TCP_EVENT_ADD_ISLE_LEFT:
748                                 qed_ooo_add_new_buffer(p_hwfn,
749                                                        p_hwfn->p_ooo_info,
750                                                        cid,
751                                                        iscsi_ooo->ooo_isle,
752                                                        p_buffer,
753                                                        QED_OOO_LEFT_BUF);
754                                 break;
755                         case TCP_EVENT_JOIN:
756                                 qed_ooo_add_new_buffer(p_hwfn,
757                                                        p_hwfn->p_ooo_info,
758                                                        cid,
759                                                        iscsi_ooo->ooo_isle +
760                                                        1,
761                                                        p_buffer,
762                                                        QED_OOO_LEFT_BUF);
763                                 qed_ooo_join_isles(p_hwfn,
764                                                    p_hwfn->p_ooo_info,
765                                                    cid, iscsi_ooo->ooo_isle);
766                                 break;
767                         case TCP_EVENT_ADD_PEN:
768                                 num_ooo_add_to_peninsula++;
769                                 qed_ooo_put_ready_buffer(p_hwfn,
770                                                          p_hwfn->p_ooo_info,
771                                                          p_buffer, true);
772                                 break;
773                         }
774                 } else {
775                         DP_NOTICE(p_hwfn,
776                                   "Unexpected event (%d) TX OOO completion\n",
777                                   iscsi_ooo->ooo_opcode);
778                 }
779         }
780
781         return 0;
782 }
783
784 static void
785 qed_ooo_submit_tx_buffers(struct qed_hwfn *p_hwfn,
786                           struct qed_ll2_info *p_ll2_conn)
787 {
788         struct qed_ll2_tx_pkt_info tx_pkt;
789         struct qed_ooo_buffer *p_buffer;
790         u16 l4_hdr_offset_w;
791         dma_addr_t first_frag;
792         u8 bd_flags;
793         int rc;
794
795         /* Submit Tx buffers here */
796         while ((p_buffer = qed_ooo_get_ready_buffer(p_hwfn,
797                                                     p_hwfn->p_ooo_info))) {
798                 l4_hdr_offset_w = 0;
799                 bd_flags = 0;
800
801                 first_frag = p_buffer->rx_buffer_phys_addr +
802                              p_buffer->placement_offset;
803                 SET_FIELD(bd_flags, CORE_TX_BD_DATA_FORCE_VLAN_MODE, 1);
804                 SET_FIELD(bd_flags, CORE_TX_BD_DATA_L4_PROTOCOL, 1);
805
806                 memset(&tx_pkt, 0, sizeof(tx_pkt));
807                 tx_pkt.num_of_bds = 1;
808                 tx_pkt.vlan = p_buffer->vlan;
809                 tx_pkt.bd_flags = bd_flags;
810                 tx_pkt.l4_hdr_offset_w = l4_hdr_offset_w;
811                 switch (p_ll2_conn->tx_dest) {
812                 case CORE_TX_DEST_NW:
813                         tx_pkt.tx_dest = QED_LL2_TX_DEST_NW;
814                         break;
815                 case CORE_TX_DEST_LB:
816                         tx_pkt.tx_dest = QED_LL2_TX_DEST_LB;
817                         break;
818                 case CORE_TX_DEST_DROP:
819                 default:
820                         tx_pkt.tx_dest = QED_LL2_TX_DEST_DROP;
821                         break;
822                 }
823                 tx_pkt.first_frag = first_frag;
824                 tx_pkt.first_frag_len = p_buffer->packet_length;
825                 tx_pkt.cookie = p_buffer;
826
827                 rc = qed_ll2_prepare_tx_packet(p_hwfn, p_ll2_conn->my_id,
828                                                &tx_pkt, true);
829                 if (rc) {
830                         qed_ooo_put_ready_buffer(p_hwfn, p_hwfn->p_ooo_info,
831                                                  p_buffer, false);
832                         break;
833                 }
834         }
835 }
836
837 static void
838 qed_ooo_submit_rx_buffers(struct qed_hwfn *p_hwfn,
839                           struct qed_ll2_info *p_ll2_conn)
840 {
841         struct qed_ooo_buffer *p_buffer;
842         int rc;
843
844         while ((p_buffer = qed_ooo_get_free_buffer(p_hwfn,
845                                                    p_hwfn->p_ooo_info))) {
846                 rc = qed_ll2_post_rx_buffer(p_hwfn,
847                                             p_ll2_conn->my_id,
848                                             p_buffer->rx_buffer_phys_addr,
849                                             0, p_buffer, true);
850                 if (rc) {
851                         qed_ooo_put_free_buffer(p_hwfn,
852                                                 p_hwfn->p_ooo_info, p_buffer);
853                         break;
854                 }
855         }
856 }
857
858 static int qed_ll2_lb_rxq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
859 {
860         struct qed_ll2_info *p_ll2_conn = (struct qed_ll2_info *)p_cookie;
861         int rc;
862
863         if (!p_ll2_conn)
864                 return 0;
865
866         if (!QED_LL2_RX_REGISTERED(p_ll2_conn))
867                 return 0;
868
869         rc = qed_ll2_lb_rxq_handler(p_hwfn, p_ll2_conn);
870         if (rc)
871                 return rc;
872
873         qed_ooo_submit_rx_buffers(p_hwfn, p_ll2_conn);
874         qed_ooo_submit_tx_buffers(p_hwfn, p_ll2_conn);
875
876         return 0;
877 }
878
879 static int qed_ll2_lb_txq_completion(struct qed_hwfn *p_hwfn, void *p_cookie)
880 {
881         struct qed_ll2_info *p_ll2_conn = (struct qed_ll2_info *)p_cookie;
882         struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
883         struct qed_ll2_tx_packet *p_pkt = NULL;
884         struct qed_ooo_buffer *p_buffer;
885         bool b_dont_submit_rx = false;
886         u16 new_idx = 0, num_bds = 0;
887         int rc;
888
889         if (!p_ll2_conn)
890                 return 0;
891
892         if (!QED_LL2_TX_REGISTERED(p_ll2_conn))
893                 return 0;
894
895         new_idx = le16_to_cpu(*p_tx->p_fw_cons);
896         num_bds = ((s16)new_idx - (s16)p_tx->bds_idx);
897
898         if (!num_bds)
899                 return 0;
900
901         while (num_bds) {
902                 if (list_empty(&p_tx->active_descq))
903                         return -EINVAL;
904
905                 p_pkt = list_first_entry(&p_tx->active_descq,
906                                          struct qed_ll2_tx_packet, list_entry);
907                 if (!p_pkt)
908                         return -EINVAL;
909
910                 if (p_pkt->bd_used != 1) {
911                         DP_NOTICE(p_hwfn,
912                                   "Unexpectedly many BDs(%d) in TX OOO completion\n",
913                                   p_pkt->bd_used);
914                         return -EINVAL;
915                 }
916
917                 list_del(&p_pkt->list_entry);
918
919                 num_bds--;
920                 p_tx->bds_idx++;
921                 qed_chain_consume(&p_tx->txq_chain);
922
923                 p_buffer = (struct qed_ooo_buffer *)p_pkt->cookie;
924                 list_add_tail(&p_pkt->list_entry, &p_tx->free_descq);
925
926                 if (b_dont_submit_rx) {
927                         qed_ooo_put_free_buffer(p_hwfn, p_hwfn->p_ooo_info,
928                                                 p_buffer);
929                         continue;
930                 }
931
932                 rc = qed_ll2_post_rx_buffer(p_hwfn, p_ll2_conn->my_id,
933                                             p_buffer->rx_buffer_phys_addr, 0,
934                                             p_buffer, true);
935                 if (rc != 0) {
936                         qed_ooo_put_free_buffer(p_hwfn,
937                                                 p_hwfn->p_ooo_info, p_buffer);
938                         b_dont_submit_rx = true;
939                 }
940         }
941
942         qed_ooo_submit_tx_buffers(p_hwfn, p_ll2_conn);
943
944         return 0;
945 }
946
947 static void qed_ll2_stop_ooo(struct qed_dev *cdev)
948 {
949         struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
950         u8 *handle = &hwfn->pf_params.iscsi_pf_params.ll2_ooo_queue_id;
951
952         DP_VERBOSE(cdev, QED_MSG_STORAGE, "Stopping LL2 OOO queue [%02x]\n",
953                    *handle);
954
955         qed_ll2_terminate_connection(hwfn, *handle);
956         qed_ll2_release_connection(hwfn, *handle);
957         *handle = QED_LL2_UNUSED_HANDLE;
958 }
959
960 static int qed_sp_ll2_rx_queue_start(struct qed_hwfn *p_hwfn,
961                                      struct qed_ll2_info *p_ll2_conn,
962                                      u8 action_on_error)
963 {
964         enum qed_ll2_conn_type conn_type = p_ll2_conn->input.conn_type;
965         struct qed_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
966         struct core_rx_start_ramrod_data *p_ramrod = NULL;
967         struct qed_spq_entry *p_ent = NULL;
968         struct qed_sp_init_data init_data;
969         u16 cqe_pbl_size;
970         int rc = 0;
971
972         /* Get SPQ entry */
973         memset(&init_data, 0, sizeof(init_data));
974         init_data.cid = p_ll2_conn->cid;
975         init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
976         init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
977
978         rc = qed_sp_init_request(p_hwfn, &p_ent,
979                                  CORE_RAMROD_RX_QUEUE_START,
980                                  PROTOCOLID_CORE, &init_data);
981         if (rc)
982                 return rc;
983
984         p_ramrod = &p_ent->ramrod.core_rx_queue_start;
985
986         p_ramrod->sb_id = cpu_to_le16(qed_int_get_sp_sb_id(p_hwfn));
987         p_ramrod->sb_index = p_rx->rx_sb_index;
988         p_ramrod->complete_event_flg = 1;
989
990         p_ramrod->mtu = cpu_to_le16(p_ll2_conn->input.mtu);
991         DMA_REGPAIR_LE(p_ramrod->bd_base, p_rx->rxq_chain.p_phys_addr);
992         cqe_pbl_size = (u16)qed_chain_get_page_cnt(&p_rx->rcq_chain);
993         p_ramrod->num_of_pbl_pages = cpu_to_le16(cqe_pbl_size);
994         DMA_REGPAIR_LE(p_ramrod->cqe_pbl_addr,
995                        qed_chain_get_pbl_phys(&p_rx->rcq_chain));
996
997         p_ramrod->drop_ttl0_flg = p_ll2_conn->input.rx_drop_ttl0_flg;
998         p_ramrod->inner_vlan_stripping_en =
999                 p_ll2_conn->input.rx_vlan_removal_en;
1000
1001         if (test_bit(QED_MF_UFP_SPECIFIC, &p_hwfn->cdev->mf_bits) &&
1002             p_ll2_conn->input.conn_type == QED_LL2_TYPE_FCOE)
1003                 p_ramrod->report_outer_vlan = 1;
1004         p_ramrod->queue_id = p_ll2_conn->queue_id;
1005         p_ramrod->main_func_queue = p_ll2_conn->main_func_queue ? 1 : 0;
1006
1007         if (test_bit(QED_MF_LL2_NON_UNICAST, &p_hwfn->cdev->mf_bits) &&
1008             p_ramrod->main_func_queue && conn_type != QED_LL2_TYPE_ROCE &&
1009             conn_type != QED_LL2_TYPE_IWARP) {
1010                 p_ramrod->mf_si_bcast_accept_all = 1;
1011                 p_ramrod->mf_si_mcast_accept_all = 1;
1012         } else {
1013                 p_ramrod->mf_si_bcast_accept_all = 0;
1014                 p_ramrod->mf_si_mcast_accept_all = 0;
1015         }
1016
1017         p_ramrod->action_on_error.error_type = action_on_error;
1018         p_ramrod->gsi_offload_flag = p_ll2_conn->input.gsi_enable;
1019         return qed_spq_post(p_hwfn, p_ent, NULL);
1020 }
1021
1022 static int qed_sp_ll2_tx_queue_start(struct qed_hwfn *p_hwfn,
1023                                      struct qed_ll2_info *p_ll2_conn)
1024 {
1025         enum qed_ll2_conn_type conn_type = p_ll2_conn->input.conn_type;
1026         struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
1027         struct core_tx_start_ramrod_data *p_ramrod = NULL;
1028         struct qed_spq_entry *p_ent = NULL;
1029         struct qed_sp_init_data init_data;
1030         u16 pq_id = 0, pbl_size;
1031         int rc = -EINVAL;
1032
1033         if (!QED_LL2_TX_REGISTERED(p_ll2_conn))
1034                 return 0;
1035
1036         if (p_ll2_conn->input.conn_type == QED_LL2_TYPE_OOO)
1037                 p_ll2_conn->tx_stats_en = 0;
1038         else
1039                 p_ll2_conn->tx_stats_en = 1;
1040
1041         /* Get SPQ entry */
1042         memset(&init_data, 0, sizeof(init_data));
1043         init_data.cid = p_ll2_conn->cid;
1044         init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
1045         init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
1046
1047         rc = qed_sp_init_request(p_hwfn, &p_ent,
1048                                  CORE_RAMROD_TX_QUEUE_START,
1049                                  PROTOCOLID_CORE, &init_data);
1050         if (rc)
1051                 return rc;
1052
1053         p_ramrod = &p_ent->ramrod.core_tx_queue_start;
1054
1055         p_ramrod->sb_id = cpu_to_le16(qed_int_get_sp_sb_id(p_hwfn));
1056         p_ramrod->sb_index = p_tx->tx_sb_index;
1057         p_ramrod->mtu = cpu_to_le16(p_ll2_conn->input.mtu);
1058         p_ramrod->stats_en = p_ll2_conn->tx_stats_en;
1059         p_ramrod->stats_id = p_ll2_conn->tx_stats_id;
1060
1061         DMA_REGPAIR_LE(p_ramrod->pbl_base_addr,
1062                        qed_chain_get_pbl_phys(&p_tx->txq_chain));
1063         pbl_size = qed_chain_get_page_cnt(&p_tx->txq_chain);
1064         p_ramrod->pbl_size = cpu_to_le16(pbl_size);
1065
1066         switch (p_ll2_conn->input.tx_tc) {
1067         case PURE_LB_TC:
1068                 pq_id = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB);
1069                 break;
1070         case PKT_LB_TC:
1071                 pq_id = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OOO);
1072                 break;
1073         default:
1074                 pq_id = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
1075                 break;
1076         }
1077
1078         p_ramrod->qm_pq_id = cpu_to_le16(pq_id);
1079
1080         switch (conn_type) {
1081         case QED_LL2_TYPE_FCOE:
1082                 p_ramrod->conn_type = PROTOCOLID_FCOE;
1083                 break;
1084         case QED_LL2_TYPE_ISCSI:
1085                 p_ramrod->conn_type = PROTOCOLID_ISCSI;
1086                 break;
1087         case QED_LL2_TYPE_ROCE:
1088                 p_ramrod->conn_type = PROTOCOLID_ROCE;
1089                 break;
1090         case QED_LL2_TYPE_IWARP:
1091                 p_ramrod->conn_type = PROTOCOLID_IWARP;
1092                 break;
1093         case QED_LL2_TYPE_OOO:
1094                 if (p_hwfn->hw_info.personality == QED_PCI_ISCSI)
1095                         p_ramrod->conn_type = PROTOCOLID_ISCSI;
1096                 else
1097                         p_ramrod->conn_type = PROTOCOLID_IWARP;
1098                 break;
1099         default:
1100                 p_ramrod->conn_type = PROTOCOLID_ETH;
1101                 DP_NOTICE(p_hwfn, "Unknown connection type: %d\n", conn_type);
1102         }
1103
1104         p_ramrod->gsi_offload_flag = p_ll2_conn->input.gsi_enable;
1105
1106         return qed_spq_post(p_hwfn, p_ent, NULL);
1107 }
1108
1109 static int qed_sp_ll2_rx_queue_stop(struct qed_hwfn *p_hwfn,
1110                                     struct qed_ll2_info *p_ll2_conn)
1111 {
1112         struct core_rx_stop_ramrod_data *p_ramrod = NULL;
1113         struct qed_spq_entry *p_ent = NULL;
1114         struct qed_sp_init_data init_data;
1115         int rc = -EINVAL;
1116
1117         /* Get SPQ entry */
1118         memset(&init_data, 0, sizeof(init_data));
1119         init_data.cid = p_ll2_conn->cid;
1120         init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
1121         init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
1122
1123         rc = qed_sp_init_request(p_hwfn, &p_ent,
1124                                  CORE_RAMROD_RX_QUEUE_STOP,
1125                                  PROTOCOLID_CORE, &init_data);
1126         if (rc)
1127                 return rc;
1128
1129         p_ramrod = &p_ent->ramrod.core_rx_queue_stop;
1130
1131         p_ramrod->complete_event_flg = 1;
1132         p_ramrod->queue_id = p_ll2_conn->queue_id;
1133
1134         return qed_spq_post(p_hwfn, p_ent, NULL);
1135 }
1136
1137 static int qed_sp_ll2_tx_queue_stop(struct qed_hwfn *p_hwfn,
1138                                     struct qed_ll2_info *p_ll2_conn)
1139 {
1140         struct qed_spq_entry *p_ent = NULL;
1141         struct qed_sp_init_data init_data;
1142         int rc = -EINVAL;
1143
1144         /* Get SPQ entry */
1145         memset(&init_data, 0, sizeof(init_data));
1146         init_data.cid = p_ll2_conn->cid;
1147         init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
1148         init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
1149
1150         rc = qed_sp_init_request(p_hwfn, &p_ent,
1151                                  CORE_RAMROD_TX_QUEUE_STOP,
1152                                  PROTOCOLID_CORE, &init_data);
1153         if (rc)
1154                 return rc;
1155
1156         return qed_spq_post(p_hwfn, p_ent, NULL);
1157 }
1158
1159 static int
1160 qed_ll2_acquire_connection_rx(struct qed_hwfn *p_hwfn,
1161                               struct qed_ll2_info *p_ll2_info)
1162 {
1163         struct qed_ll2_rx_packet *p_descq;
1164         u32 capacity;
1165         int rc = 0;
1166
1167         if (!p_ll2_info->input.rx_num_desc)
1168                 goto out;
1169
1170         rc = qed_chain_alloc(p_hwfn->cdev,
1171                              QED_CHAIN_USE_TO_CONSUME_PRODUCE,
1172                              QED_CHAIN_MODE_NEXT_PTR,
1173                              QED_CHAIN_CNT_TYPE_U16,
1174                              p_ll2_info->input.rx_num_desc,
1175                              sizeof(struct core_rx_bd),
1176                              &p_ll2_info->rx_queue.rxq_chain, NULL);
1177         if (rc) {
1178                 DP_NOTICE(p_hwfn, "Failed to allocate ll2 rxq chain\n");
1179                 goto out;
1180         }
1181
1182         capacity = qed_chain_get_capacity(&p_ll2_info->rx_queue.rxq_chain);
1183         p_descq = kcalloc(capacity, sizeof(struct qed_ll2_rx_packet),
1184                           GFP_KERNEL);
1185         if (!p_descq) {
1186                 rc = -ENOMEM;
1187                 DP_NOTICE(p_hwfn, "Failed to allocate ll2 Rx desc\n");
1188                 goto out;
1189         }
1190         p_ll2_info->rx_queue.descq_array = p_descq;
1191
1192         rc = qed_chain_alloc(p_hwfn->cdev,
1193                              QED_CHAIN_USE_TO_CONSUME_PRODUCE,
1194                              QED_CHAIN_MODE_PBL,
1195                              QED_CHAIN_CNT_TYPE_U16,
1196                              p_ll2_info->input.rx_num_desc,
1197                              sizeof(struct core_rx_fast_path_cqe),
1198                              &p_ll2_info->rx_queue.rcq_chain, NULL);
1199         if (rc) {
1200                 DP_NOTICE(p_hwfn, "Failed to allocate ll2 rcq chain\n");
1201                 goto out;
1202         }
1203
1204         DP_VERBOSE(p_hwfn, QED_MSG_LL2,
1205                    "Allocated LL2 Rxq [Type %08x] with 0x%08x buffers\n",
1206                    p_ll2_info->input.conn_type, p_ll2_info->input.rx_num_desc);
1207
1208 out:
1209         return rc;
1210 }
1211
1212 static int qed_ll2_acquire_connection_tx(struct qed_hwfn *p_hwfn,
1213                                          struct qed_ll2_info *p_ll2_info)
1214 {
1215         struct qed_ll2_tx_packet *p_descq;
1216         u32 desc_size;
1217         u32 capacity;
1218         int rc = 0;
1219
1220         if (!p_ll2_info->input.tx_num_desc)
1221                 goto out;
1222
1223         rc = qed_chain_alloc(p_hwfn->cdev,
1224                              QED_CHAIN_USE_TO_CONSUME_PRODUCE,
1225                              QED_CHAIN_MODE_PBL,
1226                              QED_CHAIN_CNT_TYPE_U16,
1227                              p_ll2_info->input.tx_num_desc,
1228                              sizeof(struct core_tx_bd),
1229                              &p_ll2_info->tx_queue.txq_chain, NULL);
1230         if (rc)
1231                 goto out;
1232
1233         capacity = qed_chain_get_capacity(&p_ll2_info->tx_queue.txq_chain);
1234         /* First element is part of the packet, rest are flexibly added */
1235         desc_size = (sizeof(*p_descq) +
1236                      (p_ll2_info->input.tx_max_bds_per_packet - 1) *
1237                      sizeof(p_descq->bds_set));
1238
1239         p_descq = kcalloc(capacity, desc_size, GFP_KERNEL);
1240         if (!p_descq) {
1241                 rc = -ENOMEM;
1242                 goto out;
1243         }
1244         p_ll2_info->tx_queue.descq_mem = p_descq;
1245
1246         DP_VERBOSE(p_hwfn, QED_MSG_LL2,
1247                    "Allocated LL2 Txq [Type %08x] with 0x%08x buffers\n",
1248                    p_ll2_info->input.conn_type, p_ll2_info->input.tx_num_desc);
1249
1250 out:
1251         if (rc)
1252                 DP_NOTICE(p_hwfn,
1253                           "Can't allocate memory for Tx LL2 with 0x%08x buffers\n",
1254                           p_ll2_info->input.tx_num_desc);
1255         return rc;
1256 }
1257
1258 static int
1259 qed_ll2_acquire_connection_ooo(struct qed_hwfn *p_hwfn,
1260                                struct qed_ll2_info *p_ll2_info, u16 mtu)
1261 {
1262         struct qed_ooo_buffer *p_buf = NULL;
1263         void *p_virt;
1264         u16 buf_idx;
1265         int rc = 0;
1266
1267         if (p_ll2_info->input.conn_type != QED_LL2_TYPE_OOO)
1268                 return rc;
1269
1270         /* Correct number of requested OOO buffers if needed */
1271         if (!p_ll2_info->input.rx_num_ooo_buffers) {
1272                 u16 num_desc = p_ll2_info->input.rx_num_desc;
1273
1274                 if (!num_desc)
1275                         return -EINVAL;
1276                 p_ll2_info->input.rx_num_ooo_buffers = num_desc * 2;
1277         }
1278
1279         for (buf_idx = 0; buf_idx < p_ll2_info->input.rx_num_ooo_buffers;
1280              buf_idx++) {
1281                 p_buf = kzalloc(sizeof(*p_buf), GFP_KERNEL);
1282                 if (!p_buf) {
1283                         rc = -ENOMEM;
1284                         goto out;
1285                 }
1286
1287                 p_buf->rx_buffer_size = mtu + 26 + ETH_CACHE_LINE_SIZE;
1288                 p_buf->rx_buffer_size = (p_buf->rx_buffer_size +
1289                                          ETH_CACHE_LINE_SIZE - 1) &
1290                                         ~(ETH_CACHE_LINE_SIZE - 1);
1291                 p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
1292                                             p_buf->rx_buffer_size,
1293                                             &p_buf->rx_buffer_phys_addr,
1294                                             GFP_KERNEL);
1295                 if (!p_virt) {
1296                         kfree(p_buf);
1297                         rc = -ENOMEM;
1298                         goto out;
1299                 }
1300
1301                 p_buf->rx_buffer_virt_addr = p_virt;
1302                 qed_ooo_put_free_buffer(p_hwfn, p_hwfn->p_ooo_info, p_buf);
1303         }
1304
1305         DP_VERBOSE(p_hwfn, QED_MSG_LL2,
1306                    "Allocated [%04x] LL2 OOO buffers [each of size 0x%08x]\n",
1307                    p_ll2_info->input.rx_num_ooo_buffers, p_buf->rx_buffer_size);
1308
1309 out:
1310         return rc;
1311 }
1312
1313 static int
1314 qed_ll2_set_cbs(struct qed_ll2_info *p_ll2_info, const struct qed_ll2_cbs *cbs)
1315 {
1316         if (!cbs || (!cbs->rx_comp_cb ||
1317                      !cbs->rx_release_cb ||
1318                      !cbs->tx_comp_cb || !cbs->tx_release_cb || !cbs->cookie))
1319                 return -EINVAL;
1320
1321         p_ll2_info->cbs.rx_comp_cb = cbs->rx_comp_cb;
1322         p_ll2_info->cbs.rx_release_cb = cbs->rx_release_cb;
1323         p_ll2_info->cbs.tx_comp_cb = cbs->tx_comp_cb;
1324         p_ll2_info->cbs.tx_release_cb = cbs->tx_release_cb;
1325         p_ll2_info->cbs.slowpath_cb = cbs->slowpath_cb;
1326         p_ll2_info->cbs.cookie = cbs->cookie;
1327
1328         return 0;
1329 }
1330
1331 static enum core_error_handle
1332 qed_ll2_get_error_choice(enum qed_ll2_error_handle err)
1333 {
1334         switch (err) {
1335         case QED_LL2_DROP_PACKET:
1336                 return LL2_DROP_PACKET;
1337         case QED_LL2_DO_NOTHING:
1338                 return LL2_DO_NOTHING;
1339         case QED_LL2_ASSERT:
1340                 return LL2_ASSERT;
1341         default:
1342                 return LL2_DO_NOTHING;
1343         }
1344 }
1345
1346 int qed_ll2_acquire_connection(void *cxt, struct qed_ll2_acquire_data *data)
1347 {
1348         struct qed_hwfn *p_hwfn = cxt;
1349         qed_int_comp_cb_t comp_rx_cb, comp_tx_cb;
1350         struct qed_ll2_info *p_ll2_info = NULL;
1351         u8 i, *p_tx_max;
1352         int rc;
1353
1354         if (!data->p_connection_handle || !p_hwfn->p_ll2_info)
1355                 return -EINVAL;
1356
1357         /* Find a free connection to be used */
1358         for (i = 0; (i < QED_MAX_NUM_OF_LL2_CONNECTIONS); i++) {
1359                 mutex_lock(&p_hwfn->p_ll2_info[i].mutex);
1360                 if (p_hwfn->p_ll2_info[i].b_active) {
1361                         mutex_unlock(&p_hwfn->p_ll2_info[i].mutex);
1362                         continue;
1363                 }
1364
1365                 p_hwfn->p_ll2_info[i].b_active = true;
1366                 p_ll2_info = &p_hwfn->p_ll2_info[i];
1367                 mutex_unlock(&p_hwfn->p_ll2_info[i].mutex);
1368                 break;
1369         }
1370         if (!p_ll2_info)
1371                 return -EBUSY;
1372
1373         memcpy(&p_ll2_info->input, &data->input, sizeof(p_ll2_info->input));
1374
1375         switch (data->input.tx_dest) {
1376         case QED_LL2_TX_DEST_NW:
1377                 p_ll2_info->tx_dest = CORE_TX_DEST_NW;
1378                 break;
1379         case QED_LL2_TX_DEST_LB:
1380                 p_ll2_info->tx_dest = CORE_TX_DEST_LB;
1381                 break;
1382         case QED_LL2_TX_DEST_DROP:
1383                 p_ll2_info->tx_dest = CORE_TX_DEST_DROP;
1384                 break;
1385         default:
1386                 return -EINVAL;
1387         }
1388
1389         if (data->input.conn_type == QED_LL2_TYPE_OOO ||
1390             data->input.secondary_queue)
1391                 p_ll2_info->main_func_queue = false;
1392         else
1393                 p_ll2_info->main_func_queue = true;
1394
1395         /* Correct maximum number of Tx BDs */
1396         p_tx_max = &p_ll2_info->input.tx_max_bds_per_packet;
1397         if (*p_tx_max == 0)
1398                 *p_tx_max = CORE_LL2_TX_MAX_BDS_PER_PACKET;
1399         else
1400                 *p_tx_max = min_t(u8, *p_tx_max,
1401                                   CORE_LL2_TX_MAX_BDS_PER_PACKET);
1402
1403         rc = qed_ll2_set_cbs(p_ll2_info, data->cbs);
1404         if (rc) {
1405                 DP_NOTICE(p_hwfn, "Invalid callback functions\n");
1406                 goto q_allocate_fail;
1407         }
1408
1409         rc = qed_ll2_acquire_connection_rx(p_hwfn, p_ll2_info);
1410         if (rc)
1411                 goto q_allocate_fail;
1412
1413         rc = qed_ll2_acquire_connection_tx(p_hwfn, p_ll2_info);
1414         if (rc)
1415                 goto q_allocate_fail;
1416
1417         rc = qed_ll2_acquire_connection_ooo(p_hwfn, p_ll2_info,
1418                                             data->input.mtu);
1419         if (rc)
1420                 goto q_allocate_fail;
1421
1422         /* Register callbacks for the Rx/Tx queues */
1423         if (data->input.conn_type == QED_LL2_TYPE_OOO) {
1424                 comp_rx_cb = qed_ll2_lb_rxq_completion;
1425                 comp_tx_cb = qed_ll2_lb_txq_completion;
1426         } else {
1427                 comp_rx_cb = qed_ll2_rxq_completion;
1428                 comp_tx_cb = qed_ll2_txq_completion;
1429         }
1430
1431         if (data->input.rx_num_desc) {
1432                 qed_int_register_cb(p_hwfn, comp_rx_cb,
1433                                     &p_hwfn->p_ll2_info[i],
1434                                     &p_ll2_info->rx_queue.rx_sb_index,
1435                                     &p_ll2_info->rx_queue.p_fw_cons);
1436                 p_ll2_info->rx_queue.b_cb_registred = true;
1437         }
1438
1439         if (data->input.tx_num_desc) {
1440                 qed_int_register_cb(p_hwfn,
1441                                     comp_tx_cb,
1442                                     &p_hwfn->p_ll2_info[i],
1443                                     &p_ll2_info->tx_queue.tx_sb_index,
1444                                     &p_ll2_info->tx_queue.p_fw_cons);
1445                 p_ll2_info->tx_queue.b_cb_registred = true;
1446         }
1447
1448         *data->p_connection_handle = i;
1449         return rc;
1450
1451 q_allocate_fail:
1452         qed_ll2_release_connection(p_hwfn, i);
1453         return -ENOMEM;
1454 }
1455
1456 static int qed_ll2_establish_connection_rx(struct qed_hwfn *p_hwfn,
1457                                            struct qed_ll2_info *p_ll2_conn)
1458 {
1459         enum qed_ll2_error_handle error_input;
1460         enum core_error_handle error_mode;
1461         u8 action_on_error = 0;
1462
1463         if (!QED_LL2_RX_REGISTERED(p_ll2_conn))
1464                 return 0;
1465
1466         DIRECT_REG_WR(p_ll2_conn->rx_queue.set_prod_addr, 0x0);
1467         error_input = p_ll2_conn->input.ai_err_packet_too_big;
1468         error_mode = qed_ll2_get_error_choice(error_input);
1469         SET_FIELD(action_on_error,
1470                   CORE_RX_ACTION_ON_ERROR_PACKET_TOO_BIG, error_mode);
1471         error_input = p_ll2_conn->input.ai_err_no_buf;
1472         error_mode = qed_ll2_get_error_choice(error_input);
1473         SET_FIELD(action_on_error, CORE_RX_ACTION_ON_ERROR_NO_BUFF, error_mode);
1474
1475         return qed_sp_ll2_rx_queue_start(p_hwfn, p_ll2_conn, action_on_error);
1476 }
1477
1478 static void
1479 qed_ll2_establish_connection_ooo(struct qed_hwfn *p_hwfn,
1480                                  struct qed_ll2_info *p_ll2_conn)
1481 {
1482         if (p_ll2_conn->input.conn_type != QED_LL2_TYPE_OOO)
1483                 return;
1484
1485         qed_ooo_release_all_isles(p_hwfn, p_hwfn->p_ooo_info);
1486         qed_ooo_submit_rx_buffers(p_hwfn, p_ll2_conn);
1487 }
1488
1489 int qed_ll2_establish_connection(void *cxt, u8 connection_handle)
1490 {
1491         struct qed_hwfn *p_hwfn = cxt;
1492         struct qed_ll2_info *p_ll2_conn;
1493         struct qed_ll2_tx_packet *p_pkt;
1494         struct qed_ll2_rx_queue *p_rx;
1495         struct qed_ll2_tx_queue *p_tx;
1496         struct qed_ptt *p_ptt;
1497         int rc = -EINVAL;
1498         u32 i, capacity;
1499         u32 desc_size;
1500         u8 qid;
1501
1502         p_ptt = qed_ptt_acquire(p_hwfn);
1503         if (!p_ptt)
1504                 return -EAGAIN;
1505
1506         p_ll2_conn = qed_ll2_handle_sanity_lock(p_hwfn, connection_handle);
1507         if (!p_ll2_conn) {
1508                 rc = -EINVAL;
1509                 goto out;
1510         }
1511
1512         p_rx = &p_ll2_conn->rx_queue;
1513         p_tx = &p_ll2_conn->tx_queue;
1514
1515         qed_chain_reset(&p_rx->rxq_chain);
1516         qed_chain_reset(&p_rx->rcq_chain);
1517         INIT_LIST_HEAD(&p_rx->active_descq);
1518         INIT_LIST_HEAD(&p_rx->free_descq);
1519         INIT_LIST_HEAD(&p_rx->posting_descq);
1520         spin_lock_init(&p_rx->lock);
1521         capacity = qed_chain_get_capacity(&p_rx->rxq_chain);
1522         for (i = 0; i < capacity; i++)
1523                 list_add_tail(&p_rx->descq_array[i].list_entry,
1524                               &p_rx->free_descq);
1525         *p_rx->p_fw_cons = 0;
1526
1527         qed_chain_reset(&p_tx->txq_chain);
1528         INIT_LIST_HEAD(&p_tx->active_descq);
1529         INIT_LIST_HEAD(&p_tx->free_descq);
1530         INIT_LIST_HEAD(&p_tx->sending_descq);
1531         spin_lock_init(&p_tx->lock);
1532         capacity = qed_chain_get_capacity(&p_tx->txq_chain);
1533         /* First element is part of the packet, rest are flexibly added */
1534         desc_size = (sizeof(*p_pkt) +
1535                      (p_ll2_conn->input.tx_max_bds_per_packet - 1) *
1536                      sizeof(p_pkt->bds_set));
1537
1538         for (i = 0; i < capacity; i++) {
1539                 p_pkt = p_tx->descq_mem + desc_size * i;
1540                 list_add_tail(&p_pkt->list_entry, &p_tx->free_descq);
1541         }
1542         p_tx->cur_completing_bd_idx = 0;
1543         p_tx->bds_idx = 0;
1544         p_tx->b_completing_packet = false;
1545         p_tx->cur_send_packet = NULL;
1546         p_tx->cur_send_frag_num = 0;
1547         p_tx->cur_completing_frag_num = 0;
1548         *p_tx->p_fw_cons = 0;
1549
1550         rc = qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_ll2_conn->cid);
1551         if (rc)
1552                 goto out;
1553
1554         qid = p_hwfn->hw_info.resc_start[QED_LL2_QUEUE] + connection_handle;
1555         p_ll2_conn->queue_id = qid;
1556         p_ll2_conn->tx_stats_id = qid;
1557         p_rx->set_prod_addr = (u8 __iomem *)p_hwfn->regview +
1558                                             GTT_BAR0_MAP_REG_TSDM_RAM +
1559                                             TSTORM_LL2_RX_PRODS_OFFSET(qid);
1560         p_tx->doorbell_addr = (u8 __iomem *)p_hwfn->doorbells +
1561                                             qed_db_addr(p_ll2_conn->cid,
1562                                                         DQ_DEMS_LEGACY);
1563
1564         rc = qed_ll2_establish_connection_rx(p_hwfn, p_ll2_conn);
1565         if (rc)
1566                 goto out;
1567
1568         rc = qed_sp_ll2_tx_queue_start(p_hwfn, p_ll2_conn);
1569         if (rc)
1570                 goto out;
1571
1572         if (!QED_IS_RDMA_PERSONALITY(p_hwfn))
1573                 qed_wr(p_hwfn, p_ptt, PRS_REG_USE_LIGHT_L2, 1);
1574
1575         qed_ll2_establish_connection_ooo(p_hwfn, p_ll2_conn);
1576
1577         if (p_ll2_conn->input.conn_type == QED_LL2_TYPE_FCOE) {
1578                 if (!test_bit(QED_MF_UFP_SPECIFIC, &p_hwfn->cdev->mf_bits))
1579                         qed_llh_add_protocol_filter(p_hwfn, p_ptt,
1580                                                     ETH_P_FCOE, 0,
1581                                                     QED_LLH_FILTER_ETHERTYPE);
1582                 qed_llh_add_protocol_filter(p_hwfn, p_ptt,
1583                                             ETH_P_FIP, 0,
1584                                             QED_LLH_FILTER_ETHERTYPE);
1585         }
1586
1587 out:
1588         qed_ptt_release(p_hwfn, p_ptt);
1589         return rc;
1590 }
1591
1592 static void qed_ll2_post_rx_buffer_notify_fw(struct qed_hwfn *p_hwfn,
1593                                              struct qed_ll2_rx_queue *p_rx,
1594                                              struct qed_ll2_rx_packet *p_curp)
1595 {
1596         struct qed_ll2_rx_packet *p_posting_packet = NULL;
1597         struct core_ll2_rx_prod rx_prod = { 0, 0, 0 };
1598         bool b_notify_fw = false;
1599         u16 bd_prod, cq_prod;
1600
1601         /* This handles the flushing of already posted buffers */
1602         while (!list_empty(&p_rx->posting_descq)) {
1603                 p_posting_packet = list_first_entry(&p_rx->posting_descq,
1604                                                     struct qed_ll2_rx_packet,
1605                                                     list_entry);
1606                 list_move_tail(&p_posting_packet->list_entry,
1607                                &p_rx->active_descq);
1608                 b_notify_fw = true;
1609         }
1610
1611         /* This handles the supplied packet [if there is one] */
1612         if (p_curp) {
1613                 list_add_tail(&p_curp->list_entry, &p_rx->active_descq);
1614                 b_notify_fw = true;
1615         }
1616
1617         if (!b_notify_fw)
1618                 return;
1619
1620         bd_prod = qed_chain_get_prod_idx(&p_rx->rxq_chain);
1621         cq_prod = qed_chain_get_prod_idx(&p_rx->rcq_chain);
1622         rx_prod.bd_prod = cpu_to_le16(bd_prod);
1623         rx_prod.cqe_prod = cpu_to_le16(cq_prod);
1624
1625         /* Make sure chain element is updated before ringing the doorbell */
1626         dma_wmb();
1627
1628         DIRECT_REG_WR(p_rx->set_prod_addr, *((u32 *)&rx_prod));
1629 }
1630
1631 int qed_ll2_post_rx_buffer(void *cxt,
1632                            u8 connection_handle,
1633                            dma_addr_t addr,
1634                            u16 buf_len, void *cookie, u8 notify_fw)
1635 {
1636         struct qed_hwfn *p_hwfn = cxt;
1637         struct core_rx_bd_with_buff_len *p_curb = NULL;
1638         struct qed_ll2_rx_packet *p_curp = NULL;
1639         struct qed_ll2_info *p_ll2_conn;
1640         struct qed_ll2_rx_queue *p_rx;
1641         unsigned long flags;
1642         void *p_data;
1643         int rc = 0;
1644
1645         p_ll2_conn = qed_ll2_handle_sanity(p_hwfn, connection_handle);
1646         if (!p_ll2_conn)
1647                 return -EINVAL;
1648         p_rx = &p_ll2_conn->rx_queue;
1649         if (!p_rx->set_prod_addr)
1650                 return -EIO;
1651
1652         spin_lock_irqsave(&p_rx->lock, flags);
1653         if (!list_empty(&p_rx->free_descq))
1654                 p_curp = list_first_entry(&p_rx->free_descq,
1655                                           struct qed_ll2_rx_packet, list_entry);
1656         if (p_curp) {
1657                 if (qed_chain_get_elem_left(&p_rx->rxq_chain) &&
1658                     qed_chain_get_elem_left(&p_rx->rcq_chain)) {
1659                         p_data = qed_chain_produce(&p_rx->rxq_chain);
1660                         p_curb = (struct core_rx_bd_with_buff_len *)p_data;
1661                         qed_chain_produce(&p_rx->rcq_chain);
1662                 }
1663         }
1664
1665         /* If we're lacking entires, let's try to flush buffers to FW */
1666         if (!p_curp || !p_curb) {
1667                 rc = -EBUSY;
1668                 p_curp = NULL;
1669                 goto out_notify;
1670         }
1671
1672         /* We have an Rx packet we can fill */
1673         DMA_REGPAIR_LE(p_curb->addr, addr);
1674         p_curb->buff_length = cpu_to_le16(buf_len);
1675         p_curp->rx_buf_addr = addr;
1676         p_curp->cookie = cookie;
1677         p_curp->rxq_bd = p_curb;
1678         p_curp->buf_length = buf_len;
1679         list_del(&p_curp->list_entry);
1680
1681         /* Check if we only want to enqueue this packet without informing FW */
1682         if (!notify_fw) {
1683                 list_add_tail(&p_curp->list_entry, &p_rx->posting_descq);
1684                 goto out;
1685         }
1686
1687 out_notify:
1688         qed_ll2_post_rx_buffer_notify_fw(p_hwfn, p_rx, p_curp);
1689 out:
1690         spin_unlock_irqrestore(&p_rx->lock, flags);
1691         return rc;
1692 }
1693
1694 static void qed_ll2_prepare_tx_packet_set(struct qed_hwfn *p_hwfn,
1695                                           struct qed_ll2_tx_queue *p_tx,
1696                                           struct qed_ll2_tx_packet *p_curp,
1697                                           struct qed_ll2_tx_pkt_info *pkt,
1698                                           u8 notify_fw)
1699 {
1700         list_del(&p_curp->list_entry);
1701         p_curp->cookie = pkt->cookie;
1702         p_curp->bd_used = pkt->num_of_bds;
1703         p_curp->notify_fw = notify_fw;
1704         p_tx->cur_send_packet = p_curp;
1705         p_tx->cur_send_frag_num = 0;
1706
1707         p_curp->bds_set[p_tx->cur_send_frag_num].tx_frag = pkt->first_frag;
1708         p_curp->bds_set[p_tx->cur_send_frag_num].frag_len = pkt->first_frag_len;
1709         p_tx->cur_send_frag_num++;
1710 }
1711
1712 static void
1713 qed_ll2_prepare_tx_packet_set_bd(struct qed_hwfn *p_hwfn,
1714                                  struct qed_ll2_info *p_ll2,
1715                                  struct qed_ll2_tx_packet *p_curp,
1716                                  struct qed_ll2_tx_pkt_info *pkt)
1717 {
1718         struct qed_chain *p_tx_chain = &p_ll2->tx_queue.txq_chain;
1719         u16 prod_idx = qed_chain_get_prod_idx(p_tx_chain);
1720         struct core_tx_bd *start_bd = NULL;
1721         enum core_roce_flavor_type roce_flavor;
1722         enum core_tx_dest tx_dest;
1723         u16 bd_data = 0, frag_idx;
1724
1725         roce_flavor = (pkt->qed_roce_flavor == QED_LL2_ROCE) ? CORE_ROCE
1726                                                              : CORE_RROCE;
1727
1728         switch (pkt->tx_dest) {
1729         case QED_LL2_TX_DEST_NW:
1730                 tx_dest = CORE_TX_DEST_NW;
1731                 break;
1732         case QED_LL2_TX_DEST_LB:
1733                 tx_dest = CORE_TX_DEST_LB;
1734                 break;
1735         case QED_LL2_TX_DEST_DROP:
1736                 tx_dest = CORE_TX_DEST_DROP;
1737                 break;
1738         default:
1739                 tx_dest = CORE_TX_DEST_LB;
1740                 break;
1741         }
1742
1743         start_bd = (struct core_tx_bd *)qed_chain_produce(p_tx_chain);
1744         if (QED_IS_IWARP_PERSONALITY(p_hwfn) &&
1745             p_ll2->input.conn_type == QED_LL2_TYPE_OOO) {
1746                 start_bd->nw_vlan_or_lb_echo =
1747                     cpu_to_le16(IWARP_LL2_IN_ORDER_TX_QUEUE);
1748         } else {
1749                 start_bd->nw_vlan_or_lb_echo = cpu_to_le16(pkt->vlan);
1750                 if (test_bit(QED_MF_UFP_SPECIFIC, &p_hwfn->cdev->mf_bits) &&
1751                     p_ll2->input.conn_type == QED_LL2_TYPE_FCOE)
1752                         pkt->remove_stag = true;
1753         }
1754
1755         SET_FIELD(start_bd->bitfield1, CORE_TX_BD_L4_HDR_OFFSET_W,
1756                   cpu_to_le16(pkt->l4_hdr_offset_w));
1757         SET_FIELD(start_bd->bitfield1, CORE_TX_BD_TX_DST, tx_dest);
1758         bd_data |= pkt->bd_flags;
1759         SET_FIELD(bd_data, CORE_TX_BD_DATA_START_BD, 0x1);
1760         SET_FIELD(bd_data, CORE_TX_BD_DATA_NBDS, pkt->num_of_bds);
1761         SET_FIELD(bd_data, CORE_TX_BD_DATA_ROCE_FLAV, roce_flavor);
1762         SET_FIELD(bd_data, CORE_TX_BD_DATA_IP_CSUM, !!(pkt->enable_ip_cksum));
1763         SET_FIELD(bd_data, CORE_TX_BD_DATA_L4_CSUM, !!(pkt->enable_l4_cksum));
1764         SET_FIELD(bd_data, CORE_TX_BD_DATA_IP_LEN, !!(pkt->calc_ip_len));
1765         SET_FIELD(bd_data, CORE_TX_BD_DATA_DISABLE_STAG_INSERTION,
1766                   !!(pkt->remove_stag));
1767
1768         start_bd->bd_data.as_bitfield = cpu_to_le16(bd_data);
1769         DMA_REGPAIR_LE(start_bd->addr, pkt->first_frag);
1770         start_bd->nbytes = cpu_to_le16(pkt->first_frag_len);
1771
1772         DP_VERBOSE(p_hwfn,
1773                    (NETIF_MSG_TX_QUEUED | QED_MSG_LL2),
1774                    "LL2 [q 0x%02x cid 0x%08x type 0x%08x] Tx Producer at [0x%04x] - set with a %04x bytes %02x BDs buffer at %08x:%08x\n",
1775                    p_ll2->queue_id,
1776                    p_ll2->cid,
1777                    p_ll2->input.conn_type,
1778                    prod_idx,
1779                    pkt->first_frag_len,
1780                    pkt->num_of_bds,
1781                    le32_to_cpu(start_bd->addr.hi),
1782                    le32_to_cpu(start_bd->addr.lo));
1783
1784         if (p_ll2->tx_queue.cur_send_frag_num == pkt->num_of_bds)
1785                 return;
1786
1787         /* Need to provide the packet with additional BDs for frags */
1788         for (frag_idx = p_ll2->tx_queue.cur_send_frag_num;
1789              frag_idx < pkt->num_of_bds; frag_idx++) {
1790                 struct core_tx_bd **p_bd = &p_curp->bds_set[frag_idx].txq_bd;
1791
1792                 *p_bd = (struct core_tx_bd *)qed_chain_produce(p_tx_chain);
1793                 (*p_bd)->bd_data.as_bitfield = 0;
1794                 (*p_bd)->bitfield1 = 0;
1795                 p_curp->bds_set[frag_idx].tx_frag = 0;
1796                 p_curp->bds_set[frag_idx].frag_len = 0;
1797         }
1798 }
1799
1800 /* This should be called while the Txq spinlock is being held */
1801 static void qed_ll2_tx_packet_notify(struct qed_hwfn *p_hwfn,
1802                                      struct qed_ll2_info *p_ll2_conn)
1803 {
1804         bool b_notify = p_ll2_conn->tx_queue.cur_send_packet->notify_fw;
1805         struct qed_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
1806         struct qed_ll2_tx_packet *p_pkt = NULL;
1807         struct core_db_data db_msg = { 0, 0, 0 };
1808         u16 bd_prod;
1809
1810         /* If there are missing BDs, don't do anything now */
1811         if (p_ll2_conn->tx_queue.cur_send_frag_num !=
1812             p_ll2_conn->tx_queue.cur_send_packet->bd_used)
1813                 return;
1814
1815         /* Push the current packet to the list and clean after it */
1816         list_add_tail(&p_ll2_conn->tx_queue.cur_send_packet->list_entry,
1817                       &p_ll2_conn->tx_queue.sending_descq);
1818         p_ll2_conn->tx_queue.cur_send_packet = NULL;
1819         p_ll2_conn->tx_queue.cur_send_frag_num = 0;
1820
1821         /* Notify FW of packet only if requested to */
1822         if (!b_notify)
1823                 return;
1824
1825         bd_prod = qed_chain_get_prod_idx(&p_ll2_conn->tx_queue.txq_chain);
1826
1827         while (!list_empty(&p_tx->sending_descq)) {
1828                 p_pkt = list_first_entry(&p_tx->sending_descq,
1829                                          struct qed_ll2_tx_packet, list_entry);
1830                 if (!p_pkt)
1831                         break;
1832
1833                 list_move_tail(&p_pkt->list_entry, &p_tx->active_descq);
1834         }
1835
1836         SET_FIELD(db_msg.params, CORE_DB_DATA_DEST, DB_DEST_XCM);
1837         SET_FIELD(db_msg.params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_SET);
1838         SET_FIELD(db_msg.params, CORE_DB_DATA_AGG_VAL_SEL,
1839                   DQ_XCM_CORE_TX_BD_PROD_CMD);
1840         db_msg.agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
1841         db_msg.spq_prod = cpu_to_le16(bd_prod);
1842
1843         /* Make sure the BDs data is updated before ringing the doorbell */
1844         wmb();
1845
1846         DIRECT_REG_WR(p_tx->doorbell_addr, *((u32 *)&db_msg));
1847
1848         DP_VERBOSE(p_hwfn,
1849                    (NETIF_MSG_TX_QUEUED | QED_MSG_LL2),
1850                    "LL2 [q 0x%02x cid 0x%08x type 0x%08x] Doorbelled [producer 0x%04x]\n",
1851                    p_ll2_conn->queue_id,
1852                    p_ll2_conn->cid,
1853                    p_ll2_conn->input.conn_type, db_msg.spq_prod);
1854 }
1855
1856 int qed_ll2_prepare_tx_packet(void *cxt,
1857                               u8 connection_handle,
1858                               struct qed_ll2_tx_pkt_info *pkt,
1859                               bool notify_fw)
1860 {
1861         struct qed_hwfn *p_hwfn = cxt;
1862         struct qed_ll2_tx_packet *p_curp = NULL;
1863         struct qed_ll2_info *p_ll2_conn = NULL;
1864         struct qed_ll2_tx_queue *p_tx;
1865         struct qed_chain *p_tx_chain;
1866         unsigned long flags;
1867         int rc = 0;
1868
1869         p_ll2_conn = qed_ll2_handle_sanity(p_hwfn, connection_handle);
1870         if (!p_ll2_conn)
1871                 return -EINVAL;
1872         p_tx = &p_ll2_conn->tx_queue;
1873         p_tx_chain = &p_tx->txq_chain;
1874
1875         if (pkt->num_of_bds > p_ll2_conn->input.tx_max_bds_per_packet)
1876                 return -EIO;
1877
1878         spin_lock_irqsave(&p_tx->lock, flags);
1879         if (p_tx->cur_send_packet) {
1880                 rc = -EEXIST;
1881                 goto out;
1882         }
1883
1884         /* Get entry, but only if we have tx elements for it */
1885         if (!list_empty(&p_tx->free_descq))
1886                 p_curp = list_first_entry(&p_tx->free_descq,
1887                                           struct qed_ll2_tx_packet, list_entry);
1888         if (p_curp && qed_chain_get_elem_left(p_tx_chain) < pkt->num_of_bds)
1889                 p_curp = NULL;
1890
1891         if (!p_curp) {
1892                 rc = -EBUSY;
1893                 goto out;
1894         }
1895
1896         /* Prepare packet and BD, and perhaps send a doorbell to FW */
1897         qed_ll2_prepare_tx_packet_set(p_hwfn, p_tx, p_curp, pkt, notify_fw);
1898
1899         qed_ll2_prepare_tx_packet_set_bd(p_hwfn, p_ll2_conn, p_curp, pkt);
1900
1901         qed_ll2_tx_packet_notify(p_hwfn, p_ll2_conn);
1902
1903 out:
1904         spin_unlock_irqrestore(&p_tx->lock, flags);
1905         return rc;
1906 }
1907
1908 int qed_ll2_set_fragment_of_tx_packet(void *cxt,
1909                                       u8 connection_handle,
1910                                       dma_addr_t addr, u16 nbytes)
1911 {
1912         struct qed_ll2_tx_packet *p_cur_send_packet = NULL;
1913         struct qed_hwfn *p_hwfn = cxt;
1914         struct qed_ll2_info *p_ll2_conn = NULL;
1915         u16 cur_send_frag_num = 0;
1916         struct core_tx_bd *p_bd;
1917         unsigned long flags;
1918
1919         p_ll2_conn = qed_ll2_handle_sanity(p_hwfn, connection_handle);
1920         if (!p_ll2_conn)
1921                 return -EINVAL;
1922
1923         if (!p_ll2_conn->tx_queue.cur_send_packet)
1924                 return -EINVAL;
1925
1926         p_cur_send_packet = p_ll2_conn->tx_queue.cur_send_packet;
1927         cur_send_frag_num = p_ll2_conn->tx_queue.cur_send_frag_num;
1928
1929         if (cur_send_frag_num >= p_cur_send_packet->bd_used)
1930                 return -EINVAL;
1931
1932         /* Fill the BD information, and possibly notify FW */
1933         p_bd = p_cur_send_packet->bds_set[cur_send_frag_num].txq_bd;
1934         DMA_REGPAIR_LE(p_bd->addr, addr);
1935         p_bd->nbytes = cpu_to_le16(nbytes);
1936         p_cur_send_packet->bds_set[cur_send_frag_num].tx_frag = addr;
1937         p_cur_send_packet->bds_set[cur_send_frag_num].frag_len = nbytes;
1938
1939         p_ll2_conn->tx_queue.cur_send_frag_num++;
1940
1941         spin_lock_irqsave(&p_ll2_conn->tx_queue.lock, flags);
1942         qed_ll2_tx_packet_notify(p_hwfn, p_ll2_conn);
1943         spin_unlock_irqrestore(&p_ll2_conn->tx_queue.lock, flags);
1944
1945         return 0;
1946 }
1947
1948 int qed_ll2_terminate_connection(void *cxt, u8 connection_handle)
1949 {
1950         struct qed_hwfn *p_hwfn = cxt;
1951         struct qed_ll2_info *p_ll2_conn = NULL;
1952         int rc = -EINVAL;
1953         struct qed_ptt *p_ptt;
1954
1955         p_ptt = qed_ptt_acquire(p_hwfn);
1956         if (!p_ptt)
1957                 return -EAGAIN;
1958
1959         p_ll2_conn = qed_ll2_handle_sanity_lock(p_hwfn, connection_handle);
1960         if (!p_ll2_conn) {
1961                 rc = -EINVAL;
1962                 goto out;
1963         }
1964
1965         /* Stop Tx & Rx of connection, if needed */
1966         if (QED_LL2_TX_REGISTERED(p_ll2_conn)) {
1967                 p_ll2_conn->tx_queue.b_cb_registred = false;
1968                 smp_wmb(); /* Make sure this is seen by ll2_lb_rxq_completion */
1969                 rc = qed_sp_ll2_tx_queue_stop(p_hwfn, p_ll2_conn);
1970                 if (rc)
1971                         goto out;
1972
1973                 qed_ll2_txq_flush(p_hwfn, connection_handle);
1974                 qed_int_unregister_cb(p_hwfn, p_ll2_conn->tx_queue.tx_sb_index);
1975         }
1976
1977         if (QED_LL2_RX_REGISTERED(p_ll2_conn)) {
1978                 p_ll2_conn->rx_queue.b_cb_registred = false;
1979                 smp_wmb(); /* Make sure this is seen by ll2_lb_rxq_completion */
1980                 rc = qed_sp_ll2_rx_queue_stop(p_hwfn, p_ll2_conn);
1981                 if (rc)
1982                         goto out;
1983
1984                 qed_ll2_rxq_flush(p_hwfn, connection_handle);
1985                 qed_int_unregister_cb(p_hwfn, p_ll2_conn->rx_queue.rx_sb_index);
1986         }
1987
1988         if (p_ll2_conn->input.conn_type == QED_LL2_TYPE_OOO)
1989                 qed_ooo_release_all_isles(p_hwfn, p_hwfn->p_ooo_info);
1990
1991         if (p_ll2_conn->input.conn_type == QED_LL2_TYPE_FCOE) {
1992                 if (!test_bit(QED_MF_UFP_SPECIFIC, &p_hwfn->cdev->mf_bits))
1993                         qed_llh_remove_protocol_filter(p_hwfn, p_ptt,
1994                                                        ETH_P_FCOE, 0,
1995                                                       QED_LLH_FILTER_ETHERTYPE);
1996                 qed_llh_remove_protocol_filter(p_hwfn, p_ptt,
1997                                                ETH_P_FIP, 0,
1998                                                QED_LLH_FILTER_ETHERTYPE);
1999         }
2000
2001 out:
2002         qed_ptt_release(p_hwfn, p_ptt);
2003         return rc;
2004 }
2005
2006 static void qed_ll2_release_connection_ooo(struct qed_hwfn *p_hwfn,
2007                                            struct qed_ll2_info *p_ll2_conn)
2008 {
2009         struct qed_ooo_buffer *p_buffer;
2010
2011         if (p_ll2_conn->input.conn_type != QED_LL2_TYPE_OOO)
2012                 return;
2013
2014         qed_ooo_release_all_isles(p_hwfn, p_hwfn->p_ooo_info);
2015         while ((p_buffer = qed_ooo_get_free_buffer(p_hwfn,
2016                                                    p_hwfn->p_ooo_info))) {
2017                 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
2018                                   p_buffer->rx_buffer_size,
2019                                   p_buffer->rx_buffer_virt_addr,
2020                                   p_buffer->rx_buffer_phys_addr);
2021                 kfree(p_buffer);
2022         }
2023 }
2024
2025 void qed_ll2_release_connection(void *cxt, u8 connection_handle)
2026 {
2027         struct qed_hwfn *p_hwfn = cxt;
2028         struct qed_ll2_info *p_ll2_conn = NULL;
2029
2030         p_ll2_conn = qed_ll2_handle_sanity(p_hwfn, connection_handle);
2031         if (!p_ll2_conn)
2032                 return;
2033
2034         kfree(p_ll2_conn->tx_queue.descq_mem);
2035         qed_chain_free(p_hwfn->cdev, &p_ll2_conn->tx_queue.txq_chain);
2036
2037         kfree(p_ll2_conn->rx_queue.descq_array);
2038         qed_chain_free(p_hwfn->cdev, &p_ll2_conn->rx_queue.rxq_chain);
2039         qed_chain_free(p_hwfn->cdev, &p_ll2_conn->rx_queue.rcq_chain);
2040
2041         qed_cxt_release_cid(p_hwfn, p_ll2_conn->cid);
2042
2043         qed_ll2_release_connection_ooo(p_hwfn, p_ll2_conn);
2044
2045         mutex_lock(&p_ll2_conn->mutex);
2046         p_ll2_conn->b_active = false;
2047         mutex_unlock(&p_ll2_conn->mutex);
2048 }
2049
2050 int qed_ll2_alloc(struct qed_hwfn *p_hwfn)
2051 {
2052         struct qed_ll2_info *p_ll2_connections;
2053         u8 i;
2054
2055         /* Allocate LL2's set struct */
2056         p_ll2_connections = kcalloc(QED_MAX_NUM_OF_LL2_CONNECTIONS,
2057                                     sizeof(struct qed_ll2_info), GFP_KERNEL);
2058         if (!p_ll2_connections) {
2059                 DP_NOTICE(p_hwfn, "Failed to allocate `struct qed_ll2'\n");
2060                 return -ENOMEM;
2061         }
2062
2063         for (i = 0; i < QED_MAX_NUM_OF_LL2_CONNECTIONS; i++)
2064                 p_ll2_connections[i].my_id = i;
2065
2066         p_hwfn->p_ll2_info = p_ll2_connections;
2067         return 0;
2068 }
2069
2070 void qed_ll2_setup(struct qed_hwfn *p_hwfn)
2071 {
2072         int i;
2073
2074         for (i = 0; i < QED_MAX_NUM_OF_LL2_CONNECTIONS; i++)
2075                 mutex_init(&p_hwfn->p_ll2_info[i].mutex);
2076 }
2077
2078 void qed_ll2_free(struct qed_hwfn *p_hwfn)
2079 {
2080         if (!p_hwfn->p_ll2_info)
2081                 return;
2082
2083         kfree(p_hwfn->p_ll2_info);
2084         p_hwfn->p_ll2_info = NULL;
2085 }
2086
2087 static void _qed_ll2_get_port_stats(struct qed_hwfn *p_hwfn,
2088                                     struct qed_ptt *p_ptt,
2089                                     struct qed_ll2_stats *p_stats)
2090 {
2091         struct core_ll2_port_stats port_stats;
2092
2093         memset(&port_stats, 0, sizeof(port_stats));
2094         qed_memcpy_from(p_hwfn, p_ptt, &port_stats,
2095                         BAR0_MAP_REG_TSDM_RAM +
2096                         TSTORM_LL2_PORT_STAT_OFFSET(MFW_PORT(p_hwfn)),
2097                         sizeof(port_stats));
2098
2099         p_stats->gsi_invalid_hdr = HILO_64_REGPAIR(port_stats.gsi_invalid_hdr);
2100         p_stats->gsi_invalid_pkt_length =
2101             HILO_64_REGPAIR(port_stats.gsi_invalid_pkt_length);
2102         p_stats->gsi_unsupported_pkt_typ =
2103             HILO_64_REGPAIR(port_stats.gsi_unsupported_pkt_typ);
2104         p_stats->gsi_crcchksm_error =
2105             HILO_64_REGPAIR(port_stats.gsi_crcchksm_error);
2106 }
2107
2108 static void _qed_ll2_get_tstats(struct qed_hwfn *p_hwfn,
2109                                 struct qed_ptt *p_ptt,
2110                                 struct qed_ll2_info *p_ll2_conn,
2111                                 struct qed_ll2_stats *p_stats)
2112 {
2113         struct core_ll2_tstorm_per_queue_stat tstats;
2114         u8 qid = p_ll2_conn->queue_id;
2115         u32 tstats_addr;
2116
2117         memset(&tstats, 0, sizeof(tstats));
2118         tstats_addr = BAR0_MAP_REG_TSDM_RAM +
2119                       CORE_LL2_TSTORM_PER_QUEUE_STAT_OFFSET(qid);
2120         qed_memcpy_from(p_hwfn, p_ptt, &tstats, tstats_addr, sizeof(tstats));
2121
2122         p_stats->packet_too_big_discard =
2123                         HILO_64_REGPAIR(tstats.packet_too_big_discard);
2124         p_stats->no_buff_discard = HILO_64_REGPAIR(tstats.no_buff_discard);
2125 }
2126
2127 static void _qed_ll2_get_ustats(struct qed_hwfn *p_hwfn,
2128                                 struct qed_ptt *p_ptt,
2129                                 struct qed_ll2_info *p_ll2_conn,
2130                                 struct qed_ll2_stats *p_stats)
2131 {
2132         struct core_ll2_ustorm_per_queue_stat ustats;
2133         u8 qid = p_ll2_conn->queue_id;
2134         u32 ustats_addr;
2135
2136         memset(&ustats, 0, sizeof(ustats));
2137         ustats_addr = BAR0_MAP_REG_USDM_RAM +
2138                       CORE_LL2_USTORM_PER_QUEUE_STAT_OFFSET(qid);
2139         qed_memcpy_from(p_hwfn, p_ptt, &ustats, ustats_addr, sizeof(ustats));
2140
2141         p_stats->rcv_ucast_bytes = HILO_64_REGPAIR(ustats.rcv_ucast_bytes);
2142         p_stats->rcv_mcast_bytes = HILO_64_REGPAIR(ustats.rcv_mcast_bytes);
2143         p_stats->rcv_bcast_bytes = HILO_64_REGPAIR(ustats.rcv_bcast_bytes);
2144         p_stats->rcv_ucast_pkts = HILO_64_REGPAIR(ustats.rcv_ucast_pkts);
2145         p_stats->rcv_mcast_pkts = HILO_64_REGPAIR(ustats.rcv_mcast_pkts);
2146         p_stats->rcv_bcast_pkts = HILO_64_REGPAIR(ustats.rcv_bcast_pkts);
2147 }
2148
2149 static void _qed_ll2_get_pstats(struct qed_hwfn *p_hwfn,
2150                                 struct qed_ptt *p_ptt,
2151                                 struct qed_ll2_info *p_ll2_conn,
2152                                 struct qed_ll2_stats *p_stats)
2153 {
2154         struct core_ll2_pstorm_per_queue_stat pstats;
2155         u8 stats_id = p_ll2_conn->tx_stats_id;
2156         u32 pstats_addr;
2157
2158         memset(&pstats, 0, sizeof(pstats));
2159         pstats_addr = BAR0_MAP_REG_PSDM_RAM +
2160                       CORE_LL2_PSTORM_PER_QUEUE_STAT_OFFSET(stats_id);
2161         qed_memcpy_from(p_hwfn, p_ptt, &pstats, pstats_addr, sizeof(pstats));
2162
2163         p_stats->sent_ucast_bytes = HILO_64_REGPAIR(pstats.sent_ucast_bytes);
2164         p_stats->sent_mcast_bytes = HILO_64_REGPAIR(pstats.sent_mcast_bytes);
2165         p_stats->sent_bcast_bytes = HILO_64_REGPAIR(pstats.sent_bcast_bytes);
2166         p_stats->sent_ucast_pkts = HILO_64_REGPAIR(pstats.sent_ucast_pkts);
2167         p_stats->sent_mcast_pkts = HILO_64_REGPAIR(pstats.sent_mcast_pkts);
2168         p_stats->sent_bcast_pkts = HILO_64_REGPAIR(pstats.sent_bcast_pkts);
2169 }
2170
2171 int qed_ll2_get_stats(void *cxt,
2172                       u8 connection_handle, struct qed_ll2_stats *p_stats)
2173 {
2174         struct qed_hwfn *p_hwfn = cxt;
2175         struct qed_ll2_info *p_ll2_conn = NULL;
2176         struct qed_ptt *p_ptt;
2177
2178         memset(p_stats, 0, sizeof(*p_stats));
2179
2180         if ((connection_handle >= QED_MAX_NUM_OF_LL2_CONNECTIONS) ||
2181             !p_hwfn->p_ll2_info)
2182                 return -EINVAL;
2183
2184         p_ll2_conn = &p_hwfn->p_ll2_info[connection_handle];
2185
2186         p_ptt = qed_ptt_acquire(p_hwfn);
2187         if (!p_ptt) {
2188                 DP_ERR(p_hwfn, "Failed to acquire ptt\n");
2189                 return -EINVAL;
2190         }
2191
2192         if (p_ll2_conn->input.gsi_enable)
2193                 _qed_ll2_get_port_stats(p_hwfn, p_ptt, p_stats);
2194         _qed_ll2_get_tstats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
2195         _qed_ll2_get_ustats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
2196         if (p_ll2_conn->tx_stats_en)
2197                 _qed_ll2_get_pstats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
2198
2199         qed_ptt_release(p_hwfn, p_ptt);
2200         return 0;
2201 }
2202
2203 static void qed_ll2b_release_rx_packet(void *cxt,
2204                                        u8 connection_handle,
2205                                        void *cookie,
2206                                        dma_addr_t rx_buf_addr,
2207                                        bool b_last_packet)
2208 {
2209         struct qed_hwfn *p_hwfn = cxt;
2210
2211         qed_ll2_dealloc_buffer(p_hwfn->cdev, cookie);
2212 }
2213
2214 static void qed_ll2_register_cb_ops(struct qed_dev *cdev,
2215                                     const struct qed_ll2_cb_ops *ops,
2216                                     void *cookie)
2217 {
2218         cdev->ll2->cbs = ops;
2219         cdev->ll2->cb_cookie = cookie;
2220 }
2221
2222 struct qed_ll2_cbs ll2_cbs = {
2223         .rx_comp_cb = &qed_ll2b_complete_rx_packet,
2224         .rx_release_cb = &qed_ll2b_release_rx_packet,
2225         .tx_comp_cb = &qed_ll2b_complete_tx_packet,
2226         .tx_release_cb = &qed_ll2b_complete_tx_packet,
2227 };
2228
2229 static void qed_ll2_set_conn_data(struct qed_dev *cdev,
2230                                   struct qed_ll2_acquire_data *data,
2231                                   struct qed_ll2_params *params,
2232                                   enum qed_ll2_conn_type conn_type,
2233                                   u8 *handle, bool lb)
2234 {
2235         memset(data, 0, sizeof(*data));
2236
2237         data->input.conn_type = conn_type;
2238         data->input.mtu = params->mtu;
2239         data->input.rx_num_desc = QED_LL2_RX_SIZE;
2240         data->input.rx_drop_ttl0_flg = params->drop_ttl0_packets;
2241         data->input.rx_vlan_removal_en = params->rx_vlan_stripping;
2242         data->input.tx_num_desc = QED_LL2_TX_SIZE;
2243         data->p_connection_handle = handle;
2244         data->cbs = &ll2_cbs;
2245         ll2_cbs.cookie = QED_LEADING_HWFN(cdev);
2246
2247         if (lb) {
2248                 data->input.tx_tc = PKT_LB_TC;
2249                 data->input.tx_dest = QED_LL2_TX_DEST_LB;
2250         } else {
2251                 data->input.tx_tc = 0;
2252                 data->input.tx_dest = QED_LL2_TX_DEST_NW;
2253         }
2254 }
2255
2256 static int qed_ll2_start_ooo(struct qed_dev *cdev,
2257                              struct qed_ll2_params *params)
2258 {
2259         struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
2260         u8 *handle = &hwfn->pf_params.iscsi_pf_params.ll2_ooo_queue_id;
2261         struct qed_ll2_acquire_data data;
2262         int rc;
2263
2264         qed_ll2_set_conn_data(cdev, &data, params,
2265                               QED_LL2_TYPE_OOO, handle, true);
2266
2267         rc = qed_ll2_acquire_connection(hwfn, &data);
2268         if (rc) {
2269                 DP_INFO(cdev, "Failed to acquire LL2 OOO connection\n");
2270                 goto out;
2271         }
2272
2273         rc = qed_ll2_establish_connection(hwfn, *handle);
2274         if (rc) {
2275                 DP_INFO(cdev, "Failed to establist LL2 OOO connection\n");
2276                 goto fail;
2277         }
2278
2279         return 0;
2280
2281 fail:
2282         qed_ll2_release_connection(hwfn, *handle);
2283 out:
2284         *handle = QED_LL2_UNUSED_HANDLE;
2285         return rc;
2286 }
2287
2288 static int qed_ll2_start(struct qed_dev *cdev, struct qed_ll2_params *params)
2289 {
2290         struct qed_ll2_buffer *buffer, *tmp_buffer;
2291         enum qed_ll2_conn_type conn_type;
2292         struct qed_ll2_acquire_data data;
2293         struct qed_ptt *p_ptt;
2294         int rc, i;
2295
2296
2297         /* Initialize LL2 locks & lists */
2298         INIT_LIST_HEAD(&cdev->ll2->list);
2299         spin_lock_init(&cdev->ll2->lock);
2300         cdev->ll2->rx_size = NET_SKB_PAD + ETH_HLEN +
2301                              L1_CACHE_BYTES + params->mtu;
2302
2303         /*Allocate memory for LL2 */
2304         DP_INFO(cdev, "Allocating LL2 buffers of size %08x bytes\n",
2305                 cdev->ll2->rx_size);
2306         for (i = 0; i < QED_LL2_RX_SIZE; i++) {
2307                 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
2308                 if (!buffer) {
2309                         DP_INFO(cdev, "Failed to allocate LL2 buffers\n");
2310                         goto fail;
2311                 }
2312
2313                 rc = qed_ll2_alloc_buffer(cdev, (u8 **)&buffer->data,
2314                                           &buffer->phys_addr);
2315                 if (rc) {
2316                         kfree(buffer);
2317                         goto fail;
2318                 }
2319
2320                 list_add_tail(&buffer->list, &cdev->ll2->list);
2321         }
2322
2323         switch (QED_LEADING_HWFN(cdev)->hw_info.personality) {
2324         case QED_PCI_FCOE:
2325                 conn_type = QED_LL2_TYPE_FCOE;
2326                 break;
2327         case QED_PCI_ISCSI:
2328                 conn_type = QED_LL2_TYPE_ISCSI;
2329                 break;
2330         case QED_PCI_ETH_ROCE:
2331                 conn_type = QED_LL2_TYPE_ROCE;
2332                 break;
2333         default:
2334                 conn_type = QED_LL2_TYPE_TEST;
2335         }
2336
2337         qed_ll2_set_conn_data(cdev, &data, params, conn_type,
2338                               &cdev->ll2->handle, false);
2339
2340         rc = qed_ll2_acquire_connection(QED_LEADING_HWFN(cdev), &data);
2341         if (rc) {
2342                 DP_INFO(cdev, "Failed to acquire LL2 connection\n");
2343                 goto fail;
2344         }
2345
2346         rc = qed_ll2_establish_connection(QED_LEADING_HWFN(cdev),
2347                                           cdev->ll2->handle);
2348         if (rc) {
2349                 DP_INFO(cdev, "Failed to establish LL2 connection\n");
2350                 goto release_fail;
2351         }
2352
2353         /* Post all Rx buffers to FW */
2354         spin_lock_bh(&cdev->ll2->lock);
2355         list_for_each_entry_safe(buffer, tmp_buffer, &cdev->ll2->list, list) {
2356                 rc = qed_ll2_post_rx_buffer(QED_LEADING_HWFN(cdev),
2357                                             cdev->ll2->handle,
2358                                             buffer->phys_addr, 0, buffer, 1);
2359                 if (rc) {
2360                         DP_INFO(cdev,
2361                                 "Failed to post an Rx buffer; Deleting it\n");
2362                         dma_unmap_single(&cdev->pdev->dev, buffer->phys_addr,
2363                                          cdev->ll2->rx_size, DMA_FROM_DEVICE);
2364                         kfree(buffer->data);
2365                         list_del(&buffer->list);
2366                         kfree(buffer);
2367                 } else {
2368                         cdev->ll2->rx_cnt++;
2369                 }
2370         }
2371         spin_unlock_bh(&cdev->ll2->lock);
2372
2373         if (!cdev->ll2->rx_cnt) {
2374                 DP_INFO(cdev, "Failed passing even a single Rx buffer\n");
2375                 goto release_terminate;
2376         }
2377
2378         if (!is_valid_ether_addr(params->ll2_mac_address)) {
2379                 DP_INFO(cdev, "Invalid Ethernet address\n");
2380                 goto release_terminate;
2381         }
2382
2383         if (QED_LEADING_HWFN(cdev)->hw_info.personality == QED_PCI_ISCSI) {
2384                 DP_VERBOSE(cdev, QED_MSG_STORAGE, "Starting OOO LL2 queue\n");
2385                 rc = qed_ll2_start_ooo(cdev, params);
2386                 if (rc) {
2387                         DP_INFO(cdev,
2388                                 "Failed to initialize the OOO LL2 queue\n");
2389                         goto release_terminate;
2390                 }
2391         }
2392
2393         p_ptt = qed_ptt_acquire(QED_LEADING_HWFN(cdev));
2394         if (!p_ptt) {
2395                 DP_INFO(cdev, "Failed to acquire PTT\n");
2396                 goto release_terminate;
2397         }
2398
2399         rc = qed_llh_add_mac_filter(QED_LEADING_HWFN(cdev), p_ptt,
2400                                     params->ll2_mac_address);
2401         qed_ptt_release(QED_LEADING_HWFN(cdev), p_ptt);
2402         if (rc) {
2403                 DP_ERR(cdev, "Failed to allocate LLH filter\n");
2404                 goto release_terminate_all;
2405         }
2406
2407         ether_addr_copy(cdev->ll2_mac_address, params->ll2_mac_address);
2408         return 0;
2409
2410 release_terminate_all:
2411
2412 release_terminate:
2413         qed_ll2_terminate_connection(QED_LEADING_HWFN(cdev), cdev->ll2->handle);
2414 release_fail:
2415         qed_ll2_release_connection(QED_LEADING_HWFN(cdev), cdev->ll2->handle);
2416 fail:
2417         qed_ll2_kill_buffers(cdev);
2418         cdev->ll2->handle = QED_LL2_UNUSED_HANDLE;
2419         return -EINVAL;
2420 }
2421
2422 static int qed_ll2_stop(struct qed_dev *cdev)
2423 {
2424         struct qed_ptt *p_ptt;
2425         int rc;
2426
2427         if (cdev->ll2->handle == QED_LL2_UNUSED_HANDLE)
2428                 return 0;
2429
2430         p_ptt = qed_ptt_acquire(QED_LEADING_HWFN(cdev));
2431         if (!p_ptt) {
2432                 DP_INFO(cdev, "Failed to acquire PTT\n");
2433                 goto fail;
2434         }
2435
2436         qed_llh_remove_mac_filter(QED_LEADING_HWFN(cdev), p_ptt,
2437                                   cdev->ll2_mac_address);
2438         qed_ptt_release(QED_LEADING_HWFN(cdev), p_ptt);
2439         eth_zero_addr(cdev->ll2_mac_address);
2440
2441         if (QED_LEADING_HWFN(cdev)->hw_info.personality == QED_PCI_ISCSI)
2442                 qed_ll2_stop_ooo(cdev);
2443
2444         rc = qed_ll2_terminate_connection(QED_LEADING_HWFN(cdev),
2445                                           cdev->ll2->handle);
2446         if (rc)
2447                 DP_INFO(cdev, "Failed to terminate LL2 connection\n");
2448
2449         qed_ll2_kill_buffers(cdev);
2450
2451         qed_ll2_release_connection(QED_LEADING_HWFN(cdev), cdev->ll2->handle);
2452         cdev->ll2->handle = QED_LL2_UNUSED_HANDLE;
2453
2454         return rc;
2455 fail:
2456         return -EINVAL;
2457 }
2458
2459 static int qed_ll2_start_xmit(struct qed_dev *cdev, struct sk_buff *skb,
2460                               unsigned long xmit_flags)
2461 {
2462         struct qed_ll2_tx_pkt_info pkt;
2463         const skb_frag_t *frag;
2464         u8 flags = 0, nr_frags;
2465         int rc = -EINVAL, i;
2466         dma_addr_t mapping;
2467         u16 vlan = 0;
2468
2469         if (unlikely(skb->ip_summed != CHECKSUM_NONE)) {
2470                 DP_INFO(cdev, "Cannot transmit a checksummed packet\n");
2471                 return -EINVAL;
2472         }
2473
2474         /* Cache number of fragments from SKB since SKB may be freed by
2475          * the completion routine after calling qed_ll2_prepare_tx_packet()
2476          */
2477         nr_frags = skb_shinfo(skb)->nr_frags;
2478
2479         if (1 + nr_frags > CORE_LL2_TX_MAX_BDS_PER_PACKET) {
2480                 DP_ERR(cdev, "Cannot transmit a packet with %d fragments\n",
2481                        1 + nr_frags);
2482                 return -EINVAL;
2483         }
2484
2485         mapping = dma_map_single(&cdev->pdev->dev, skb->data,
2486                                  skb->len, DMA_TO_DEVICE);
2487         if (unlikely(dma_mapping_error(&cdev->pdev->dev, mapping))) {
2488                 DP_NOTICE(cdev, "SKB mapping failed\n");
2489                 return -EINVAL;
2490         }
2491
2492         /* Request HW to calculate IP csum */
2493         if (!((vlan_get_protocol(skb) == htons(ETH_P_IPV6)) &&
2494               ipv6_hdr(skb)->nexthdr == NEXTHDR_IPV6))
2495                 flags |= BIT(CORE_TX_BD_DATA_IP_CSUM_SHIFT);
2496
2497         if (skb_vlan_tag_present(skb)) {
2498                 vlan = skb_vlan_tag_get(skb);
2499                 flags |= BIT(CORE_TX_BD_DATA_VLAN_INSERTION_SHIFT);
2500         }
2501
2502         memset(&pkt, 0, sizeof(pkt));
2503         pkt.num_of_bds = 1 + nr_frags;
2504         pkt.vlan = vlan;
2505         pkt.bd_flags = flags;
2506         pkt.tx_dest = QED_LL2_TX_DEST_NW;
2507         pkt.first_frag = mapping;
2508         pkt.first_frag_len = skb->len;
2509         pkt.cookie = skb;
2510         if (test_bit(QED_MF_UFP_SPECIFIC, &cdev->mf_bits) &&
2511             test_bit(QED_LL2_XMIT_FLAGS_FIP_DISCOVERY, &xmit_flags))
2512                 pkt.remove_stag = true;
2513
2514         /* qed_ll2_prepare_tx_packet() may actually send the packet if
2515          * there are no fragments in the skb and subsequently the completion
2516          * routine may run and free the SKB, so no dereferencing the SKB
2517          * beyond this point unless skb has any fragments.
2518          */
2519         rc = qed_ll2_prepare_tx_packet(&cdev->hwfns[0], cdev->ll2->handle,
2520                                        &pkt, 1);
2521         if (rc)
2522                 goto err;
2523
2524         for (i = 0; i < nr_frags; i++) {
2525                 frag = &skb_shinfo(skb)->frags[i];
2526
2527                 mapping = skb_frag_dma_map(&cdev->pdev->dev, frag, 0,
2528                                            skb_frag_size(frag), DMA_TO_DEVICE);
2529
2530                 if (unlikely(dma_mapping_error(&cdev->pdev->dev, mapping))) {
2531                         DP_NOTICE(cdev,
2532                                   "Unable to map frag - dropping packet\n");
2533                         rc = -ENOMEM;
2534                         goto err;
2535                 }
2536
2537                 rc = qed_ll2_set_fragment_of_tx_packet(QED_LEADING_HWFN(cdev),
2538                                                        cdev->ll2->handle,
2539                                                        mapping,
2540                                                        skb_frag_size(frag));
2541
2542                 /* if failed not much to do here, partial packet has been posted
2543                  * we can't free memory, will need to wait for completion.
2544                  */
2545                 if (rc)
2546                         goto err2;
2547         }
2548
2549         return 0;
2550
2551 err:
2552         dma_unmap_single(&cdev->pdev->dev, mapping, skb->len, DMA_TO_DEVICE);
2553
2554 err2:
2555         return rc;
2556 }
2557
2558 static int qed_ll2_stats(struct qed_dev *cdev, struct qed_ll2_stats *stats)
2559 {
2560         if (!cdev->ll2)
2561                 return -EINVAL;
2562
2563         return qed_ll2_get_stats(QED_LEADING_HWFN(cdev),
2564                                  cdev->ll2->handle, stats);
2565 }
2566
2567 const struct qed_ll2_ops qed_ll2_ops_pass = {
2568         .start = &qed_ll2_start,
2569         .stop = &qed_ll2_stop,
2570         .start_xmit = &qed_ll2_start_xmit,
2571         .register_cb_ops = &qed_ll2_register_cb_ops,
2572         .get_stats = &qed_ll2_stats,
2573 };
2574
2575 int qed_ll2_alloc_if(struct qed_dev *cdev)
2576 {
2577         cdev->ll2 = kzalloc(sizeof(*cdev->ll2), GFP_KERNEL);
2578         return cdev->ll2 ? 0 : -ENOMEM;
2579 }
2580
2581 void qed_ll2_dealloc_if(struct qed_dev *cdev)
2582 {
2583         kfree(cdev->ll2);
2584         cdev->ll2 = NULL;
2585 }