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