net: Update API for VF vlan protocol 802.1ad support
[cascardo/linux.git] / drivers / net / ethernet / qlogic / qede / qede_main.c
1 /* QLogic qede NIC Driver
2 * Copyright (c) 2015 QLogic Corporation
3 *
4 * This software is available under the terms of the GNU General Public License
5 * (GPL) Version 2, available from the file COPYING in the main directory of
6 * this source tree.
7 */
8
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/version.h>
12 #include <linux/device.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/skbuff.h>
16 #include <linux/errno.h>
17 #include <linux/list.h>
18 #include <linux/string.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/interrupt.h>
21 #include <asm/byteorder.h>
22 #include <asm/param.h>
23 #include <linux/io.h>
24 #include <linux/netdev_features.h>
25 #include <linux/udp.h>
26 #include <linux/tcp.h>
27 #include <net/udp_tunnel.h>
28 #include <linux/ip.h>
29 #include <net/ipv6.h>
30 #include <net/tcp.h>
31 #include <linux/if_ether.h>
32 #include <linux/if_vlan.h>
33 #include <linux/pkt_sched.h>
34 #include <linux/ethtool.h>
35 #include <linux/in.h>
36 #include <linux/random.h>
37 #include <net/ip6_checksum.h>
38 #include <linux/bitops.h>
39
40 #include "qede.h"
41
42 static char version[] =
43         "QLogic FastLinQ 4xxxx Ethernet Driver qede " DRV_MODULE_VERSION "\n";
44
45 MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx Ethernet Driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(DRV_MODULE_VERSION);
48
49 static uint debug;
50 module_param(debug, uint, 0);
51 MODULE_PARM_DESC(debug, " Default debug msglevel");
52
53 static const struct qed_eth_ops *qed_ops;
54
55 #define CHIP_NUM_57980S_40              0x1634
56 #define CHIP_NUM_57980S_10              0x1666
57 #define CHIP_NUM_57980S_MF              0x1636
58 #define CHIP_NUM_57980S_100             0x1644
59 #define CHIP_NUM_57980S_50              0x1654
60 #define CHIP_NUM_57980S_25              0x1656
61 #define CHIP_NUM_57980S_IOV             0x1664
62
63 #ifndef PCI_DEVICE_ID_NX2_57980E
64 #define PCI_DEVICE_ID_57980S_40         CHIP_NUM_57980S_40
65 #define PCI_DEVICE_ID_57980S_10         CHIP_NUM_57980S_10
66 #define PCI_DEVICE_ID_57980S_MF         CHIP_NUM_57980S_MF
67 #define PCI_DEVICE_ID_57980S_100        CHIP_NUM_57980S_100
68 #define PCI_DEVICE_ID_57980S_50         CHIP_NUM_57980S_50
69 #define PCI_DEVICE_ID_57980S_25         CHIP_NUM_57980S_25
70 #define PCI_DEVICE_ID_57980S_IOV        CHIP_NUM_57980S_IOV
71 #endif
72
73 enum qede_pci_private {
74         QEDE_PRIVATE_PF,
75         QEDE_PRIVATE_VF
76 };
77
78 static const struct pci_device_id qede_pci_tbl[] = {
79         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_40), QEDE_PRIVATE_PF},
80         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_10), QEDE_PRIVATE_PF},
81         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_MF), QEDE_PRIVATE_PF},
82         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_100), QEDE_PRIVATE_PF},
83         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_50), QEDE_PRIVATE_PF},
84         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_25), QEDE_PRIVATE_PF},
85 #ifdef CONFIG_QED_SRIOV
86         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_IOV), QEDE_PRIVATE_VF},
87 #endif
88         { 0 }
89 };
90
91 MODULE_DEVICE_TABLE(pci, qede_pci_tbl);
92
93 static int qede_probe(struct pci_dev *pdev, const struct pci_device_id *id);
94
95 #define TX_TIMEOUT              (5 * HZ)
96
97 static void qede_remove(struct pci_dev *pdev);
98 static int qede_alloc_rx_buffer(struct qede_dev *edev,
99                                 struct qede_rx_queue *rxq);
100 static void qede_link_update(void *dev, struct qed_link_output *link);
101
102 #ifdef CONFIG_QED_SRIOV
103 static int qede_set_vf_vlan(struct net_device *ndev, int vf, u16 vlan, u8 qos,
104                             __be16 vlan_proto)
105 {
106         struct qede_dev *edev = netdev_priv(ndev);
107
108         if (vlan > 4095) {
109                 DP_NOTICE(edev, "Illegal vlan value %d\n", vlan);
110                 return -EINVAL;
111         }
112
113         if (vlan_proto != htons(ETH_P_8021Q))
114                 return -EPROTONOSUPPORT;
115
116         DP_VERBOSE(edev, QED_MSG_IOV, "Setting Vlan 0x%04x to VF [%d]\n",
117                    vlan, vf);
118
119         return edev->ops->iov->set_vlan(edev->cdev, vlan, vf);
120 }
121
122 static int qede_set_vf_mac(struct net_device *ndev, int vfidx, u8 *mac)
123 {
124         struct qede_dev *edev = netdev_priv(ndev);
125
126         DP_VERBOSE(edev, QED_MSG_IOV,
127                    "Setting MAC %02x:%02x:%02x:%02x:%02x:%02x to VF [%d]\n",
128                    mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], vfidx);
129
130         if (!is_valid_ether_addr(mac)) {
131                 DP_VERBOSE(edev, QED_MSG_IOV, "MAC address isn't valid\n");
132                 return -EINVAL;
133         }
134
135         return edev->ops->iov->set_mac(edev->cdev, mac, vfidx);
136 }
137
138 static int qede_sriov_configure(struct pci_dev *pdev, int num_vfs_param)
139 {
140         struct qede_dev *edev = netdev_priv(pci_get_drvdata(pdev));
141         struct qed_dev_info *qed_info = &edev->dev_info.common;
142         int rc;
143
144         DP_VERBOSE(edev, QED_MSG_IOV, "Requested %d VFs\n", num_vfs_param);
145
146         rc = edev->ops->iov->configure(edev->cdev, num_vfs_param);
147
148         /* Enable/Disable Tx switching for PF */
149         if ((rc == num_vfs_param) && netif_running(edev->ndev) &&
150             qed_info->mf_mode != QED_MF_NPAR && qed_info->tx_switching) {
151                 struct qed_update_vport_params params;
152
153                 memset(&params, 0, sizeof(params));
154                 params.vport_id = 0;
155                 params.update_tx_switching_flg = 1;
156                 params.tx_switching_flg = num_vfs_param ? 1 : 0;
157                 edev->ops->vport_update(edev->cdev, &params);
158         }
159
160         return rc;
161 }
162 #endif
163
164 static struct pci_driver qede_pci_driver = {
165         .name = "qede",
166         .id_table = qede_pci_tbl,
167         .probe = qede_probe,
168         .remove = qede_remove,
169 #ifdef CONFIG_QED_SRIOV
170         .sriov_configure = qede_sriov_configure,
171 #endif
172 };
173
174 static void qede_force_mac(void *dev, u8 *mac)
175 {
176         struct qede_dev *edev = dev;
177
178         ether_addr_copy(edev->ndev->dev_addr, mac);
179         ether_addr_copy(edev->primary_mac, mac);
180 }
181
182 static struct qed_eth_cb_ops qede_ll_ops = {
183         {
184                 .link_update = qede_link_update,
185         },
186         .force_mac = qede_force_mac,
187 };
188
189 static int qede_netdev_event(struct notifier_block *this, unsigned long event,
190                              void *ptr)
191 {
192         struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
193         struct ethtool_drvinfo drvinfo;
194         struct qede_dev *edev;
195
196         /* Currently only support name change */
197         if (event != NETDEV_CHANGENAME)
198                 goto done;
199
200         /* Check whether this is a qede device */
201         if (!ndev || !ndev->ethtool_ops || !ndev->ethtool_ops->get_drvinfo)
202                 goto done;
203
204         memset(&drvinfo, 0, sizeof(drvinfo));
205         ndev->ethtool_ops->get_drvinfo(ndev, &drvinfo);
206         if (strcmp(drvinfo.driver, "qede"))
207                 goto done;
208         edev = netdev_priv(ndev);
209
210         /* Notify qed of the name change */
211         if (!edev->ops || !edev->ops->common)
212                 goto done;
213         edev->ops->common->set_id(edev->cdev, edev->ndev->name,
214                                   "qede");
215
216 done:
217         return NOTIFY_DONE;
218 }
219
220 static struct notifier_block qede_netdev_notifier = {
221         .notifier_call = qede_netdev_event,
222 };
223
224 static
225 int __init qede_init(void)
226 {
227         int ret;
228
229         pr_info("qede_init: %s\n", version);
230
231         qed_ops = qed_get_eth_ops();
232         if (!qed_ops) {
233                 pr_notice("Failed to get qed ethtool operations\n");
234                 return -EINVAL;
235         }
236
237         /* Must register notifier before pci ops, since we might miss
238          * interface rename after pci probe and netdev registeration.
239          */
240         ret = register_netdevice_notifier(&qede_netdev_notifier);
241         if (ret) {
242                 pr_notice("Failed to register netdevice_notifier\n");
243                 qed_put_eth_ops();
244                 return -EINVAL;
245         }
246
247         ret = pci_register_driver(&qede_pci_driver);
248         if (ret) {
249                 pr_notice("Failed to register driver\n");
250                 unregister_netdevice_notifier(&qede_netdev_notifier);
251                 qed_put_eth_ops();
252                 return -EINVAL;
253         }
254
255         return 0;
256 }
257
258 static void __exit qede_cleanup(void)
259 {
260         if (debug & QED_LOG_INFO_MASK)
261                 pr_info("qede_cleanup called\n");
262
263         unregister_netdevice_notifier(&qede_netdev_notifier);
264         pci_unregister_driver(&qede_pci_driver);
265         qed_put_eth_ops();
266 }
267
268 module_init(qede_init);
269 module_exit(qede_cleanup);
270
271 /* -------------------------------------------------------------------------
272  * START OF FAST-PATH
273  * -------------------------------------------------------------------------
274  */
275
276 /* Unmap the data and free skb */
277 static int qede_free_tx_pkt(struct qede_dev *edev,
278                             struct qede_tx_queue *txq, int *len)
279 {
280         u16 idx = txq->sw_tx_cons & NUM_TX_BDS_MAX;
281         struct sk_buff *skb = txq->sw_tx_ring[idx].skb;
282         struct eth_tx_1st_bd *first_bd;
283         struct eth_tx_bd *tx_data_bd;
284         int bds_consumed = 0;
285         int nbds;
286         bool data_split = txq->sw_tx_ring[idx].flags & QEDE_TSO_SPLIT_BD;
287         int i, split_bd_len = 0;
288
289         if (unlikely(!skb)) {
290                 DP_ERR(edev,
291                        "skb is null for txq idx=%d txq->sw_tx_cons=%d txq->sw_tx_prod=%d\n",
292                        idx, txq->sw_tx_cons, txq->sw_tx_prod);
293                 return -1;
294         }
295
296         *len = skb->len;
297
298         first_bd = (struct eth_tx_1st_bd *)qed_chain_consume(&txq->tx_pbl);
299
300         bds_consumed++;
301
302         nbds = first_bd->data.nbds;
303
304         if (data_split) {
305                 struct eth_tx_bd *split = (struct eth_tx_bd *)
306                         qed_chain_consume(&txq->tx_pbl);
307                 split_bd_len = BD_UNMAP_LEN(split);
308                 bds_consumed++;
309         }
310         dma_unmap_page(&edev->pdev->dev, BD_UNMAP_ADDR(first_bd),
311                        BD_UNMAP_LEN(first_bd) + split_bd_len, DMA_TO_DEVICE);
312
313         /* Unmap the data of the skb frags */
314         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, bds_consumed++) {
315                 tx_data_bd = (struct eth_tx_bd *)
316                         qed_chain_consume(&txq->tx_pbl);
317                 dma_unmap_page(&edev->pdev->dev, BD_UNMAP_ADDR(tx_data_bd),
318                                BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
319         }
320
321         while (bds_consumed++ < nbds)
322                 qed_chain_consume(&txq->tx_pbl);
323
324         /* Free skb */
325         dev_kfree_skb_any(skb);
326         txq->sw_tx_ring[idx].skb = NULL;
327         txq->sw_tx_ring[idx].flags = 0;
328
329         return 0;
330 }
331
332 /* Unmap the data and free skb when mapping failed during start_xmit */
333 static void qede_free_failed_tx_pkt(struct qede_dev *edev,
334                                     struct qede_tx_queue *txq,
335                                     struct eth_tx_1st_bd *first_bd,
336                                     int nbd, bool data_split)
337 {
338         u16 idx = txq->sw_tx_prod & NUM_TX_BDS_MAX;
339         struct sk_buff *skb = txq->sw_tx_ring[idx].skb;
340         struct eth_tx_bd *tx_data_bd;
341         int i, split_bd_len = 0;
342
343         /* Return prod to its position before this skb was handled */
344         qed_chain_set_prod(&txq->tx_pbl,
345                            le16_to_cpu(txq->tx_db.data.bd_prod), first_bd);
346
347         first_bd = (struct eth_tx_1st_bd *)qed_chain_produce(&txq->tx_pbl);
348
349         if (data_split) {
350                 struct eth_tx_bd *split = (struct eth_tx_bd *)
351                                           qed_chain_produce(&txq->tx_pbl);
352                 split_bd_len = BD_UNMAP_LEN(split);
353                 nbd--;
354         }
355
356         dma_unmap_page(&edev->pdev->dev, BD_UNMAP_ADDR(first_bd),
357                        BD_UNMAP_LEN(first_bd) + split_bd_len, DMA_TO_DEVICE);
358
359         /* Unmap the data of the skb frags */
360         for (i = 0; i < nbd; i++) {
361                 tx_data_bd = (struct eth_tx_bd *)
362                         qed_chain_produce(&txq->tx_pbl);
363                 if (tx_data_bd->nbytes)
364                         dma_unmap_page(&edev->pdev->dev,
365                                        BD_UNMAP_ADDR(tx_data_bd),
366                                        BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
367         }
368
369         /* Return again prod to its position before this skb was handled */
370         qed_chain_set_prod(&txq->tx_pbl,
371                            le16_to_cpu(txq->tx_db.data.bd_prod), first_bd);
372
373         /* Free skb */
374         dev_kfree_skb_any(skb);
375         txq->sw_tx_ring[idx].skb = NULL;
376         txq->sw_tx_ring[idx].flags = 0;
377 }
378
379 static u32 qede_xmit_type(struct qede_dev *edev,
380                           struct sk_buff *skb, int *ipv6_ext)
381 {
382         u32 rc = XMIT_L4_CSUM;
383         __be16 l3_proto;
384
385         if (skb->ip_summed != CHECKSUM_PARTIAL)
386                 return XMIT_PLAIN;
387
388         l3_proto = vlan_get_protocol(skb);
389         if (l3_proto == htons(ETH_P_IPV6) &&
390             (ipv6_hdr(skb)->nexthdr == NEXTHDR_IPV6))
391                 *ipv6_ext = 1;
392
393         if (skb->encapsulation)
394                 rc |= XMIT_ENC;
395
396         if (skb_is_gso(skb))
397                 rc |= XMIT_LSO;
398
399         return rc;
400 }
401
402 static void qede_set_params_for_ipv6_ext(struct sk_buff *skb,
403                                          struct eth_tx_2nd_bd *second_bd,
404                                          struct eth_tx_3rd_bd *third_bd)
405 {
406         u8 l4_proto;
407         u16 bd2_bits1 = 0, bd2_bits2 = 0;
408
409         bd2_bits1 |= (1 << ETH_TX_DATA_2ND_BD_IPV6_EXT_SHIFT);
410
411         bd2_bits2 |= ((((u8 *)skb_transport_header(skb) - skb->data) >> 1) &
412                      ETH_TX_DATA_2ND_BD_L4_HDR_START_OFFSET_W_MASK)
413                     << ETH_TX_DATA_2ND_BD_L4_HDR_START_OFFSET_W_SHIFT;
414
415         bd2_bits1 |= (ETH_L4_PSEUDO_CSUM_CORRECT_LENGTH <<
416                       ETH_TX_DATA_2ND_BD_L4_PSEUDO_CSUM_MODE_SHIFT);
417
418         if (vlan_get_protocol(skb) == htons(ETH_P_IPV6))
419                 l4_proto = ipv6_hdr(skb)->nexthdr;
420         else
421                 l4_proto = ip_hdr(skb)->protocol;
422
423         if (l4_proto == IPPROTO_UDP)
424                 bd2_bits1 |= 1 << ETH_TX_DATA_2ND_BD_L4_UDP_SHIFT;
425
426         if (third_bd)
427                 third_bd->data.bitfields |=
428                         cpu_to_le16(((tcp_hdrlen(skb) / 4) &
429                                 ETH_TX_DATA_3RD_BD_TCP_HDR_LEN_DW_MASK) <<
430                                 ETH_TX_DATA_3RD_BD_TCP_HDR_LEN_DW_SHIFT);
431
432         second_bd->data.bitfields1 = cpu_to_le16(bd2_bits1);
433         second_bd->data.bitfields2 = cpu_to_le16(bd2_bits2);
434 }
435
436 static int map_frag_to_bd(struct qede_dev *edev,
437                           skb_frag_t *frag, struct eth_tx_bd *bd)
438 {
439         dma_addr_t mapping;
440
441         /* Map skb non-linear frag data for DMA */
442         mapping = skb_frag_dma_map(&edev->pdev->dev, frag, 0,
443                                    skb_frag_size(frag), DMA_TO_DEVICE);
444         if (unlikely(dma_mapping_error(&edev->pdev->dev, mapping))) {
445                 DP_NOTICE(edev, "Unable to map frag - dropping packet\n");
446                 return -ENOMEM;
447         }
448
449         /* Setup the data pointer of the frag data */
450         BD_SET_UNMAP_ADDR_LEN(bd, mapping, skb_frag_size(frag));
451
452         return 0;
453 }
454
455 static u16 qede_get_skb_hlen(struct sk_buff *skb, bool is_encap_pkt)
456 {
457         if (is_encap_pkt)
458                 return (skb_inner_transport_header(skb) +
459                         inner_tcp_hdrlen(skb) - skb->data);
460         else
461                 return (skb_transport_header(skb) +
462                         tcp_hdrlen(skb) - skb->data);
463 }
464
465 /* +2 for 1st BD for headers and 2nd BD for headlen (if required) */
466 #if ((MAX_SKB_FRAGS + 2) > ETH_TX_MAX_BDS_PER_NON_LSO_PACKET)
467 static bool qede_pkt_req_lin(struct qede_dev *edev, struct sk_buff *skb,
468                              u8 xmit_type)
469 {
470         int allowed_frags = ETH_TX_MAX_BDS_PER_NON_LSO_PACKET - 1;
471
472         if (xmit_type & XMIT_LSO) {
473                 int hlen;
474
475                 hlen = qede_get_skb_hlen(skb, xmit_type & XMIT_ENC);
476
477                 /* linear payload would require its own BD */
478                 if (skb_headlen(skb) > hlen)
479                         allowed_frags--;
480         }
481
482         return (skb_shinfo(skb)->nr_frags > allowed_frags);
483 }
484 #endif
485
486 static inline void qede_update_tx_producer(struct qede_tx_queue *txq)
487 {
488         /* wmb makes sure that the BDs data is updated before updating the
489          * producer, otherwise FW may read old data from the BDs.
490          */
491         wmb();
492         barrier();
493         writel(txq->tx_db.raw, txq->doorbell_addr);
494
495         /* mmiowb is needed to synchronize doorbell writes from more than one
496          * processor. It guarantees that the write arrives to the device before
497          * the queue lock is released and another start_xmit is called (possibly
498          * on another CPU). Without this barrier, the next doorbell can bypass
499          * this doorbell. This is applicable to IA64/Altix systems.
500          */
501         mmiowb();
502 }
503
504 /* Main transmit function */
505 static netdev_tx_t qede_start_xmit(struct sk_buff *skb,
506                                    struct net_device *ndev)
507 {
508         struct qede_dev *edev = netdev_priv(ndev);
509         struct netdev_queue *netdev_txq;
510         struct qede_tx_queue *txq;
511         struct eth_tx_1st_bd *first_bd;
512         struct eth_tx_2nd_bd *second_bd = NULL;
513         struct eth_tx_3rd_bd *third_bd = NULL;
514         struct eth_tx_bd *tx_data_bd = NULL;
515         u16 txq_index;
516         u8 nbd = 0;
517         dma_addr_t mapping;
518         int rc, frag_idx = 0, ipv6_ext = 0;
519         u8 xmit_type;
520         u16 idx;
521         u16 hlen;
522         bool data_split = false;
523
524         /* Get tx-queue context and netdev index */
525         txq_index = skb_get_queue_mapping(skb);
526         WARN_ON(txq_index >= QEDE_TSS_COUNT(edev));
527         txq = QEDE_TX_QUEUE(edev, txq_index);
528         netdev_txq = netdev_get_tx_queue(ndev, txq_index);
529
530         WARN_ON(qed_chain_get_elem_left(&txq->tx_pbl) < (MAX_SKB_FRAGS + 1));
531
532         xmit_type = qede_xmit_type(edev, skb, &ipv6_ext);
533
534 #if ((MAX_SKB_FRAGS + 2) > ETH_TX_MAX_BDS_PER_NON_LSO_PACKET)
535         if (qede_pkt_req_lin(edev, skb, xmit_type)) {
536                 if (skb_linearize(skb)) {
537                         DP_NOTICE(edev,
538                                   "SKB linearization failed - silently dropping this SKB\n");
539                         dev_kfree_skb_any(skb);
540                         return NETDEV_TX_OK;
541                 }
542         }
543 #endif
544
545         /* Fill the entry in the SW ring and the BDs in the FW ring */
546         idx = txq->sw_tx_prod & NUM_TX_BDS_MAX;
547         txq->sw_tx_ring[idx].skb = skb;
548         first_bd = (struct eth_tx_1st_bd *)
549                    qed_chain_produce(&txq->tx_pbl);
550         memset(first_bd, 0, sizeof(*first_bd));
551         first_bd->data.bd_flags.bitfields =
552                 1 << ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT;
553
554         /* Map skb linear data for DMA and set in the first BD */
555         mapping = dma_map_single(&edev->pdev->dev, skb->data,
556                                  skb_headlen(skb), DMA_TO_DEVICE);
557         if (unlikely(dma_mapping_error(&edev->pdev->dev, mapping))) {
558                 DP_NOTICE(edev, "SKB mapping failed\n");
559                 qede_free_failed_tx_pkt(edev, txq, first_bd, 0, false);
560                 qede_update_tx_producer(txq);
561                 return NETDEV_TX_OK;
562         }
563         nbd++;
564         BD_SET_UNMAP_ADDR_LEN(first_bd, mapping, skb_headlen(skb));
565
566         /* In case there is IPv6 with extension headers or LSO we need 2nd and
567          * 3rd BDs.
568          */
569         if (unlikely((xmit_type & XMIT_LSO) | ipv6_ext)) {
570                 second_bd = (struct eth_tx_2nd_bd *)
571                         qed_chain_produce(&txq->tx_pbl);
572                 memset(second_bd, 0, sizeof(*second_bd));
573
574                 nbd++;
575                 third_bd = (struct eth_tx_3rd_bd *)
576                         qed_chain_produce(&txq->tx_pbl);
577                 memset(third_bd, 0, sizeof(*third_bd));
578
579                 nbd++;
580                 /* We need to fill in additional data in second_bd... */
581                 tx_data_bd = (struct eth_tx_bd *)second_bd;
582         }
583
584         if (skb_vlan_tag_present(skb)) {
585                 first_bd->data.vlan = cpu_to_le16(skb_vlan_tag_get(skb));
586                 first_bd->data.bd_flags.bitfields |=
587                         1 << ETH_TX_1ST_BD_FLAGS_VLAN_INSERTION_SHIFT;
588         }
589
590         /* Fill the parsing flags & params according to the requested offload */
591         if (xmit_type & XMIT_L4_CSUM) {
592                 /* We don't re-calculate IP checksum as it is already done by
593                  * the upper stack
594                  */
595                 first_bd->data.bd_flags.bitfields |=
596                         1 << ETH_TX_1ST_BD_FLAGS_L4_CSUM_SHIFT;
597
598                 if (xmit_type & XMIT_ENC) {
599                         first_bd->data.bd_flags.bitfields |=
600                                 1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
601                         first_bd->data.bitfields |=
602                             1 << ETH_TX_DATA_1ST_BD_TUNN_FLAG_SHIFT;
603                 }
604
605                 /* Legacy FW had flipped behavior in regard to this bit -
606                  * I.e., needed to set to prevent FW from touching encapsulated
607                  * packets when it didn't need to.
608                  */
609                 if (unlikely(txq->is_legacy))
610                         first_bd->data.bitfields ^=
611                             1 << ETH_TX_DATA_1ST_BD_TUNN_FLAG_SHIFT;
612
613                 /* If the packet is IPv6 with extension header, indicate that
614                  * to FW and pass few params, since the device cracker doesn't
615                  * support parsing IPv6 with extension header/s.
616                  */
617                 if (unlikely(ipv6_ext))
618                         qede_set_params_for_ipv6_ext(skb, second_bd, third_bd);
619         }
620
621         if (xmit_type & XMIT_LSO) {
622                 first_bd->data.bd_flags.bitfields |=
623                         (1 << ETH_TX_1ST_BD_FLAGS_LSO_SHIFT);
624                 third_bd->data.lso_mss =
625                         cpu_to_le16(skb_shinfo(skb)->gso_size);
626
627                 if (unlikely(xmit_type & XMIT_ENC)) {
628                         first_bd->data.bd_flags.bitfields |=
629                                 1 << ETH_TX_1ST_BD_FLAGS_TUNN_IP_CSUM_SHIFT;
630                         hlen = qede_get_skb_hlen(skb, true);
631                 } else {
632                         first_bd->data.bd_flags.bitfields |=
633                                 1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
634                         hlen = qede_get_skb_hlen(skb, false);
635                 }
636
637                 /* @@@TBD - if will not be removed need to check */
638                 third_bd->data.bitfields |=
639                         cpu_to_le16((1 << ETH_TX_DATA_3RD_BD_HDR_NBD_SHIFT));
640
641                 /* Make life easier for FW guys who can't deal with header and
642                  * data on same BD. If we need to split, use the second bd...
643                  */
644                 if (unlikely(skb_headlen(skb) > hlen)) {
645                         DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
646                                    "TSO split header size is %d (%x:%x)\n",
647                                    first_bd->nbytes, first_bd->addr.hi,
648                                    first_bd->addr.lo);
649
650                         mapping = HILO_U64(le32_to_cpu(first_bd->addr.hi),
651                                            le32_to_cpu(first_bd->addr.lo)) +
652                                            hlen;
653
654                         BD_SET_UNMAP_ADDR_LEN(tx_data_bd, mapping,
655                                               le16_to_cpu(first_bd->nbytes) -
656                                               hlen);
657
658                         /* this marks the BD as one that has no
659                          * individual mapping
660                          */
661                         txq->sw_tx_ring[idx].flags |= QEDE_TSO_SPLIT_BD;
662
663                         first_bd->nbytes = cpu_to_le16(hlen);
664
665                         tx_data_bd = (struct eth_tx_bd *)third_bd;
666                         data_split = true;
667                 }
668         } else {
669                 first_bd->data.bitfields |=
670                     (skb->len & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK) <<
671                     ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT;
672         }
673
674         /* Handle fragmented skb */
675         /* special handle for frags inside 2nd and 3rd bds.. */
676         while (tx_data_bd && frag_idx < skb_shinfo(skb)->nr_frags) {
677                 rc = map_frag_to_bd(edev,
678                                     &skb_shinfo(skb)->frags[frag_idx],
679                                     tx_data_bd);
680                 if (rc) {
681                         qede_free_failed_tx_pkt(edev, txq, first_bd, nbd,
682                                                 data_split);
683                         qede_update_tx_producer(txq);
684                         return NETDEV_TX_OK;
685                 }
686
687                 if (tx_data_bd == (struct eth_tx_bd *)second_bd)
688                         tx_data_bd = (struct eth_tx_bd *)third_bd;
689                 else
690                         tx_data_bd = NULL;
691
692                 frag_idx++;
693         }
694
695         /* map last frags into 4th, 5th .... */
696         for (; frag_idx < skb_shinfo(skb)->nr_frags; frag_idx++, nbd++) {
697                 tx_data_bd = (struct eth_tx_bd *)
698                              qed_chain_produce(&txq->tx_pbl);
699
700                 memset(tx_data_bd, 0, sizeof(*tx_data_bd));
701
702                 rc = map_frag_to_bd(edev,
703                                     &skb_shinfo(skb)->frags[frag_idx],
704                                     tx_data_bd);
705                 if (rc) {
706                         qede_free_failed_tx_pkt(edev, txq, first_bd, nbd,
707                                                 data_split);
708                         qede_update_tx_producer(txq);
709                         return NETDEV_TX_OK;
710                 }
711         }
712
713         /* update the first BD with the actual num BDs */
714         first_bd->data.nbds = nbd;
715
716         netdev_tx_sent_queue(netdev_txq, skb->len);
717
718         skb_tx_timestamp(skb);
719
720         /* Advance packet producer only before sending the packet since mapping
721          * of pages may fail.
722          */
723         txq->sw_tx_prod++;
724
725         /* 'next page' entries are counted in the producer value */
726         txq->tx_db.data.bd_prod =
727                 cpu_to_le16(qed_chain_get_prod_idx(&txq->tx_pbl));
728
729         if (!skb->xmit_more || netif_xmit_stopped(netdev_txq))
730                 qede_update_tx_producer(txq);
731
732         if (unlikely(qed_chain_get_elem_left(&txq->tx_pbl)
733                       < (MAX_SKB_FRAGS + 1))) {
734                 if (skb->xmit_more)
735                         qede_update_tx_producer(txq);
736
737                 netif_tx_stop_queue(netdev_txq);
738                 txq->stopped_cnt++;
739                 DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
740                            "Stop queue was called\n");
741                 /* paired memory barrier is in qede_tx_int(), we have to keep
742                  * ordering of set_bit() in netif_tx_stop_queue() and read of
743                  * fp->bd_tx_cons
744                  */
745                 smp_mb();
746
747                 if (qed_chain_get_elem_left(&txq->tx_pbl)
748                      >= (MAX_SKB_FRAGS + 1) &&
749                     (edev->state == QEDE_STATE_OPEN)) {
750                         netif_tx_wake_queue(netdev_txq);
751                         DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
752                                    "Wake queue was called\n");
753                 }
754         }
755
756         return NETDEV_TX_OK;
757 }
758
759 int qede_txq_has_work(struct qede_tx_queue *txq)
760 {
761         u16 hw_bd_cons;
762
763         /* Tell compiler that consumer and producer can change */
764         barrier();
765         hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
766         if (qed_chain_get_cons_idx(&txq->tx_pbl) == hw_bd_cons + 1)
767                 return 0;
768
769         return hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl);
770 }
771
772 static int qede_tx_int(struct qede_dev *edev, struct qede_tx_queue *txq)
773 {
774         struct netdev_queue *netdev_txq;
775         u16 hw_bd_cons;
776         unsigned int pkts_compl = 0, bytes_compl = 0;
777         int rc;
778
779         netdev_txq = netdev_get_tx_queue(edev->ndev, txq->index);
780
781         hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
782         barrier();
783
784         while (hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl)) {
785                 int len = 0;
786
787                 rc = qede_free_tx_pkt(edev, txq, &len);
788                 if (rc) {
789                         DP_NOTICE(edev, "hw_bd_cons = %d, chain_cons=%d\n",
790                                   hw_bd_cons,
791                                   qed_chain_get_cons_idx(&txq->tx_pbl));
792                         break;
793                 }
794
795                 bytes_compl += len;
796                 pkts_compl++;
797                 txq->sw_tx_cons++;
798                 txq->xmit_pkts++;
799         }
800
801         netdev_tx_completed_queue(netdev_txq, pkts_compl, bytes_compl);
802
803         /* Need to make the tx_bd_cons update visible to start_xmit()
804          * before checking for netif_tx_queue_stopped().  Without the
805          * memory barrier, there is a small possibility that
806          * start_xmit() will miss it and cause the queue to be stopped
807          * forever.
808          * On the other hand we need an rmb() here to ensure the proper
809          * ordering of bit testing in the following
810          * netif_tx_queue_stopped(txq) call.
811          */
812         smp_mb();
813
814         if (unlikely(netif_tx_queue_stopped(netdev_txq))) {
815                 /* Taking tx_lock is needed to prevent reenabling the queue
816                  * while it's empty. This could have happen if rx_action() gets
817                  * suspended in qede_tx_int() after the condition before
818                  * netif_tx_wake_queue(), while tx_action (qede_start_xmit()):
819                  *
820                  * stops the queue->sees fresh tx_bd_cons->releases the queue->
821                  * sends some packets consuming the whole queue again->
822                  * stops the queue
823                  */
824
825                 __netif_tx_lock(netdev_txq, smp_processor_id());
826
827                 if ((netif_tx_queue_stopped(netdev_txq)) &&
828                     (edev->state == QEDE_STATE_OPEN) &&
829                     (qed_chain_get_elem_left(&txq->tx_pbl)
830                       >= (MAX_SKB_FRAGS + 1))) {
831                         netif_tx_wake_queue(netdev_txq);
832                         DP_VERBOSE(edev, NETIF_MSG_TX_DONE,
833                                    "Wake queue was called\n");
834                 }
835
836                 __netif_tx_unlock(netdev_txq);
837         }
838
839         return 0;
840 }
841
842 bool qede_has_rx_work(struct qede_rx_queue *rxq)
843 {
844         u16 hw_comp_cons, sw_comp_cons;
845
846         /* Tell compiler that status block fields can change */
847         barrier();
848
849         hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
850         sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
851
852         return hw_comp_cons != sw_comp_cons;
853 }
854
855 static bool qede_has_tx_work(struct qede_fastpath *fp)
856 {
857         u8 tc;
858
859         for (tc = 0; tc < fp->edev->num_tc; tc++)
860                 if (qede_txq_has_work(&fp->txqs[tc]))
861                         return true;
862         return false;
863 }
864
865 static inline void qede_rx_bd_ring_consume(struct qede_rx_queue *rxq)
866 {
867         qed_chain_consume(&rxq->rx_bd_ring);
868         rxq->sw_rx_cons++;
869 }
870
871 /* This function reuses the buffer(from an offset) from
872  * consumer index to producer index in the bd ring
873  */
874 static inline void qede_reuse_page(struct qede_dev *edev,
875                                    struct qede_rx_queue *rxq,
876                                    struct sw_rx_data *curr_cons)
877 {
878         struct eth_rx_bd *rx_bd_prod = qed_chain_produce(&rxq->rx_bd_ring);
879         struct sw_rx_data *curr_prod;
880         dma_addr_t new_mapping;
881
882         curr_prod = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
883         *curr_prod = *curr_cons;
884
885         new_mapping = curr_prod->mapping + curr_prod->page_offset;
886
887         rx_bd_prod->addr.hi = cpu_to_le32(upper_32_bits(new_mapping));
888         rx_bd_prod->addr.lo = cpu_to_le32(lower_32_bits(new_mapping));
889
890         rxq->sw_rx_prod++;
891         curr_cons->data = NULL;
892 }
893
894 /* In case of allocation failures reuse buffers
895  * from consumer index to produce buffers for firmware
896  */
897 void qede_recycle_rx_bd_ring(struct qede_rx_queue *rxq,
898                              struct qede_dev *edev, u8 count)
899 {
900         struct sw_rx_data *curr_cons;
901
902         for (; count > 0; count--) {
903                 curr_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS_MAX];
904                 qede_reuse_page(edev, rxq, curr_cons);
905                 qede_rx_bd_ring_consume(rxq);
906         }
907 }
908
909 static inline int qede_realloc_rx_buffer(struct qede_dev *edev,
910                                          struct qede_rx_queue *rxq,
911                                          struct sw_rx_data *curr_cons)
912 {
913         /* Move to the next segment in the page */
914         curr_cons->page_offset += rxq->rx_buf_seg_size;
915
916         if (curr_cons->page_offset == PAGE_SIZE) {
917                 if (unlikely(qede_alloc_rx_buffer(edev, rxq))) {
918                         /* Since we failed to allocate new buffer
919                          * current buffer can be used again.
920                          */
921                         curr_cons->page_offset -= rxq->rx_buf_seg_size;
922
923                         return -ENOMEM;
924                 }
925
926                 dma_unmap_page(&edev->pdev->dev, curr_cons->mapping,
927                                PAGE_SIZE, DMA_FROM_DEVICE);
928         } else {
929                 /* Increment refcount of the page as we don't want
930                  * network stack to take the ownership of the page
931                  * which can be recycled multiple times by the driver.
932                  */
933                 page_ref_inc(curr_cons->data);
934                 qede_reuse_page(edev, rxq, curr_cons);
935         }
936
937         return 0;
938 }
939
940 static inline void qede_update_rx_prod(struct qede_dev *edev,
941                                        struct qede_rx_queue *rxq)
942 {
943         u16 bd_prod = qed_chain_get_prod_idx(&rxq->rx_bd_ring);
944         u16 cqe_prod = qed_chain_get_prod_idx(&rxq->rx_comp_ring);
945         struct eth_rx_prod_data rx_prods = {0};
946
947         /* Update producers */
948         rx_prods.bd_prod = cpu_to_le16(bd_prod);
949         rx_prods.cqe_prod = cpu_to_le16(cqe_prod);
950
951         /* Make sure that the BD and SGE data is updated before updating the
952          * producers since FW might read the BD/SGE right after the producer
953          * is updated.
954          */
955         wmb();
956
957         internal_ram_wr(rxq->hw_rxq_prod_addr, sizeof(rx_prods),
958                         (u32 *)&rx_prods);
959
960         /* mmiowb is needed to synchronize doorbell writes from more than one
961          * processor. It guarantees that the write arrives to the device before
962          * the napi lock is released and another qede_poll is called (possibly
963          * on another CPU). Without this barrier, the next doorbell can bypass
964          * this doorbell. This is applicable to IA64/Altix systems.
965          */
966         mmiowb();
967 }
968
969 static u32 qede_get_rxhash(struct qede_dev *edev,
970                            u8 bitfields,
971                            __le32 rss_hash, enum pkt_hash_types *rxhash_type)
972 {
973         enum rss_hash_type htype;
974
975         htype = GET_FIELD(bitfields, ETH_FAST_PATH_RX_REG_CQE_RSS_HASH_TYPE);
976
977         if ((edev->ndev->features & NETIF_F_RXHASH) && htype) {
978                 *rxhash_type = ((htype == RSS_HASH_TYPE_IPV4) ||
979                                 (htype == RSS_HASH_TYPE_IPV6)) ?
980                                 PKT_HASH_TYPE_L3 : PKT_HASH_TYPE_L4;
981                 return le32_to_cpu(rss_hash);
982         }
983         *rxhash_type = PKT_HASH_TYPE_NONE;
984         return 0;
985 }
986
987 static void qede_set_skb_csum(struct sk_buff *skb, u8 csum_flag)
988 {
989         skb_checksum_none_assert(skb);
990
991         if (csum_flag & QEDE_CSUM_UNNECESSARY)
992                 skb->ip_summed = CHECKSUM_UNNECESSARY;
993
994         if (csum_flag & QEDE_TUNN_CSUM_UNNECESSARY)
995                 skb->csum_level = 1;
996 }
997
998 static inline void qede_skb_receive(struct qede_dev *edev,
999                                     struct qede_fastpath *fp,
1000                                     struct sk_buff *skb, u16 vlan_tag)
1001 {
1002         if (vlan_tag)
1003                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
1004
1005         napi_gro_receive(&fp->napi, skb);
1006 }
1007
1008 static void qede_set_gro_params(struct qede_dev *edev,
1009                                 struct sk_buff *skb,
1010                                 struct eth_fast_path_rx_tpa_start_cqe *cqe)
1011 {
1012         u16 parsing_flags = le16_to_cpu(cqe->pars_flags.flags);
1013
1014         if (((parsing_flags >> PARSING_AND_ERR_FLAGS_L3TYPE_SHIFT) &
1015             PARSING_AND_ERR_FLAGS_L3TYPE_MASK) == 2)
1016                 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1017         else
1018                 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1019
1020         skb_shinfo(skb)->gso_size = __le16_to_cpu(cqe->len_on_first_bd) -
1021                                         cqe->header_len;
1022 }
1023
1024 static int qede_fill_frag_skb(struct qede_dev *edev,
1025                               struct qede_rx_queue *rxq,
1026                               u8 tpa_agg_index, u16 len_on_bd)
1027 {
1028         struct sw_rx_data *current_bd = &rxq->sw_rx_ring[rxq->sw_rx_cons &
1029                                                          NUM_RX_BDS_MAX];
1030         struct qede_agg_info *tpa_info = &rxq->tpa_info[tpa_agg_index];
1031         struct sk_buff *skb = tpa_info->skb;
1032
1033         if (unlikely(tpa_info->agg_state != QEDE_AGG_STATE_START))
1034                 goto out;
1035
1036         /* Add one frag and update the appropriate fields in the skb */
1037         skb_fill_page_desc(skb, tpa_info->frag_id++,
1038                            current_bd->data, current_bd->page_offset,
1039                            len_on_bd);
1040
1041         if (unlikely(qede_realloc_rx_buffer(edev, rxq, current_bd))) {
1042                 /* Incr page ref count to reuse on allocation failure
1043                  * so that it doesn't get freed while freeing SKB.
1044                  */
1045                 page_ref_inc(current_bd->data);
1046                 goto out;
1047         }
1048
1049         qed_chain_consume(&rxq->rx_bd_ring);
1050         rxq->sw_rx_cons++;
1051
1052         skb->data_len += len_on_bd;
1053         skb->truesize += rxq->rx_buf_seg_size;
1054         skb->len += len_on_bd;
1055
1056         return 0;
1057
1058 out:
1059         tpa_info->agg_state = QEDE_AGG_STATE_ERROR;
1060         qede_recycle_rx_bd_ring(rxq, edev, 1);
1061         return -ENOMEM;
1062 }
1063
1064 static void qede_tpa_start(struct qede_dev *edev,
1065                            struct qede_rx_queue *rxq,
1066                            struct eth_fast_path_rx_tpa_start_cqe *cqe)
1067 {
1068         struct qede_agg_info *tpa_info = &rxq->tpa_info[cqe->tpa_agg_index];
1069         struct eth_rx_bd *rx_bd_cons = qed_chain_consume(&rxq->rx_bd_ring);
1070         struct eth_rx_bd *rx_bd_prod = qed_chain_produce(&rxq->rx_bd_ring);
1071         struct sw_rx_data *replace_buf = &tpa_info->replace_buf;
1072         dma_addr_t mapping = tpa_info->replace_buf_mapping;
1073         struct sw_rx_data *sw_rx_data_cons;
1074         struct sw_rx_data *sw_rx_data_prod;
1075         enum pkt_hash_types rxhash_type;
1076         u32 rxhash;
1077
1078         sw_rx_data_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS_MAX];
1079         sw_rx_data_prod = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
1080
1081         /* Use pre-allocated replacement buffer - we can't release the agg.
1082          * start until its over and we don't want to risk allocation failing
1083          * here, so re-allocate when aggregation will be over.
1084          */
1085         sw_rx_data_prod->mapping = replace_buf->mapping;
1086
1087         sw_rx_data_prod->data = replace_buf->data;
1088         rx_bd_prod->addr.hi = cpu_to_le32(upper_32_bits(mapping));
1089         rx_bd_prod->addr.lo = cpu_to_le32(lower_32_bits(mapping));
1090         sw_rx_data_prod->page_offset = replace_buf->page_offset;
1091
1092         rxq->sw_rx_prod++;
1093
1094         /* move partial skb from cons to pool (don't unmap yet)
1095          * save mapping, incase we drop the packet later on.
1096          */
1097         tpa_info->start_buf = *sw_rx_data_cons;
1098         mapping = HILO_U64(le32_to_cpu(rx_bd_cons->addr.hi),
1099                            le32_to_cpu(rx_bd_cons->addr.lo));
1100
1101         tpa_info->start_buf_mapping = mapping;
1102         rxq->sw_rx_cons++;
1103
1104         /* set tpa state to start only if we are able to allocate skb
1105          * for this aggregation, otherwise mark as error and aggregation will
1106          * be dropped
1107          */
1108         tpa_info->skb = netdev_alloc_skb(edev->ndev,
1109                                          le16_to_cpu(cqe->len_on_first_bd));
1110         if (unlikely(!tpa_info->skb)) {
1111                 DP_NOTICE(edev, "Failed to allocate SKB for gro\n");
1112                 tpa_info->agg_state = QEDE_AGG_STATE_ERROR;
1113                 goto cons_buf;
1114         }
1115
1116         skb_put(tpa_info->skb, le16_to_cpu(cqe->len_on_first_bd));
1117         memcpy(&tpa_info->start_cqe, cqe, sizeof(tpa_info->start_cqe));
1118
1119         /* Start filling in the aggregation info */
1120         tpa_info->frag_id = 0;
1121         tpa_info->agg_state = QEDE_AGG_STATE_START;
1122
1123         rxhash = qede_get_rxhash(edev, cqe->bitfields,
1124                                  cqe->rss_hash, &rxhash_type);
1125         skb_set_hash(tpa_info->skb, rxhash, rxhash_type);
1126         if ((le16_to_cpu(cqe->pars_flags.flags) >>
1127              PARSING_AND_ERR_FLAGS_TAG8021QEXIST_SHIFT) &
1128                     PARSING_AND_ERR_FLAGS_TAG8021QEXIST_MASK)
1129                 tpa_info->vlan_tag = le16_to_cpu(cqe->vlan_tag);
1130         else
1131                 tpa_info->vlan_tag = 0;
1132
1133         /* This is needed in order to enable forwarding support */
1134         qede_set_gro_params(edev, tpa_info->skb, cqe);
1135
1136 cons_buf: /* We still need to handle bd_len_list to consume buffers */
1137         if (likely(cqe->ext_bd_len_list[0]))
1138                 qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
1139                                    le16_to_cpu(cqe->ext_bd_len_list[0]));
1140
1141         if (unlikely(cqe->ext_bd_len_list[1])) {
1142                 DP_ERR(edev,
1143                        "Unlikely - got a TPA aggregation with more than one ext_bd_len_list entry in the TPA start\n");
1144                 tpa_info->agg_state = QEDE_AGG_STATE_ERROR;
1145         }
1146 }
1147
1148 #ifdef CONFIG_INET
1149 static void qede_gro_ip_csum(struct sk_buff *skb)
1150 {
1151         const struct iphdr *iph = ip_hdr(skb);
1152         struct tcphdr *th;
1153
1154         skb_set_transport_header(skb, sizeof(struct iphdr));
1155         th = tcp_hdr(skb);
1156
1157         th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
1158                                   iph->saddr, iph->daddr, 0);
1159
1160         tcp_gro_complete(skb);
1161 }
1162
1163 static void qede_gro_ipv6_csum(struct sk_buff *skb)
1164 {
1165         struct ipv6hdr *iph = ipv6_hdr(skb);
1166         struct tcphdr *th;
1167
1168         skb_set_transport_header(skb, sizeof(struct ipv6hdr));
1169         th = tcp_hdr(skb);
1170
1171         th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
1172                                   &iph->saddr, &iph->daddr, 0);
1173         tcp_gro_complete(skb);
1174 }
1175 #endif
1176
1177 static void qede_gro_receive(struct qede_dev *edev,
1178                              struct qede_fastpath *fp,
1179                              struct sk_buff *skb,
1180                              u16 vlan_tag)
1181 {
1182         /* FW can send a single MTU sized packet from gro flow
1183          * due to aggregation timeout/last segment etc. which
1184          * is not expected to be a gro packet. If a skb has zero
1185          * frags then simply push it in the stack as non gso skb.
1186          */
1187         if (unlikely(!skb->data_len)) {
1188                 skb_shinfo(skb)->gso_type = 0;
1189                 skb_shinfo(skb)->gso_size = 0;
1190                 goto send_skb;
1191         }
1192
1193 #ifdef CONFIG_INET
1194         if (skb_shinfo(skb)->gso_size) {
1195                 skb_set_network_header(skb, 0);
1196
1197                 switch (skb->protocol) {
1198                 case htons(ETH_P_IP):
1199                         qede_gro_ip_csum(skb);
1200                         break;
1201                 case htons(ETH_P_IPV6):
1202                         qede_gro_ipv6_csum(skb);
1203                         break;
1204                 default:
1205                         DP_ERR(edev,
1206                                "Error: FW GRO supports only IPv4/IPv6, not 0x%04x\n",
1207                                ntohs(skb->protocol));
1208                 }
1209         }
1210 #endif
1211
1212 send_skb:
1213         skb_record_rx_queue(skb, fp->rxq->rxq_id);
1214         qede_skb_receive(edev, fp, skb, vlan_tag);
1215 }
1216
1217 static inline void qede_tpa_cont(struct qede_dev *edev,
1218                                  struct qede_rx_queue *rxq,
1219                                  struct eth_fast_path_rx_tpa_cont_cqe *cqe)
1220 {
1221         int i;
1222
1223         for (i = 0; cqe->len_list[i]; i++)
1224                 qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
1225                                    le16_to_cpu(cqe->len_list[i]));
1226
1227         if (unlikely(i > 1))
1228                 DP_ERR(edev,
1229                        "Strange - TPA cont with more than a single len_list entry\n");
1230 }
1231
1232 static void qede_tpa_end(struct qede_dev *edev,
1233                          struct qede_fastpath *fp,
1234                          struct eth_fast_path_rx_tpa_end_cqe *cqe)
1235 {
1236         struct qede_rx_queue *rxq = fp->rxq;
1237         struct qede_agg_info *tpa_info;
1238         struct sk_buff *skb;
1239         int i;
1240
1241         tpa_info = &rxq->tpa_info[cqe->tpa_agg_index];
1242         skb = tpa_info->skb;
1243
1244         for (i = 0; cqe->len_list[i]; i++)
1245                 qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
1246                                    le16_to_cpu(cqe->len_list[i]));
1247         if (unlikely(i > 1))
1248                 DP_ERR(edev,
1249                        "Strange - TPA emd with more than a single len_list entry\n");
1250
1251         if (unlikely(tpa_info->agg_state != QEDE_AGG_STATE_START))
1252                 goto err;
1253
1254         /* Sanity */
1255         if (unlikely(cqe->num_of_bds != tpa_info->frag_id + 1))
1256                 DP_ERR(edev,
1257                        "Strange - TPA had %02x BDs, but SKB has only %d frags\n",
1258                        cqe->num_of_bds, tpa_info->frag_id);
1259         if (unlikely(skb->len != le16_to_cpu(cqe->total_packet_len)))
1260                 DP_ERR(edev,
1261                        "Strange - total packet len [cqe] is %4x but SKB has len %04x\n",
1262                        le16_to_cpu(cqe->total_packet_len), skb->len);
1263
1264         memcpy(skb->data,
1265                page_address(tpa_info->start_buf.data) +
1266                 tpa_info->start_cqe.placement_offset +
1267                 tpa_info->start_buf.page_offset,
1268                le16_to_cpu(tpa_info->start_cqe.len_on_first_bd));
1269
1270         /* Recycle [mapped] start buffer for the next replacement */
1271         tpa_info->replace_buf = tpa_info->start_buf;
1272         tpa_info->replace_buf_mapping = tpa_info->start_buf_mapping;
1273
1274         /* Finalize the SKB */
1275         skb->protocol = eth_type_trans(skb, edev->ndev);
1276         skb->ip_summed = CHECKSUM_UNNECESSARY;
1277
1278         /* tcp_gro_complete() will copy NAPI_GRO_CB(skb)->count
1279          * to skb_shinfo(skb)->gso_segs
1280          */
1281         NAPI_GRO_CB(skb)->count = le16_to_cpu(cqe->num_of_coalesced_segs);
1282
1283         qede_gro_receive(edev, fp, skb, tpa_info->vlan_tag);
1284
1285         tpa_info->agg_state = QEDE_AGG_STATE_NONE;
1286
1287         return;
1288 err:
1289         /* The BD starting the aggregation is still mapped; Re-use it for
1290          * future aggregations [as replacement buffer]
1291          */
1292         memcpy(&tpa_info->replace_buf, &tpa_info->start_buf,
1293                sizeof(struct sw_rx_data));
1294         tpa_info->replace_buf_mapping = tpa_info->start_buf_mapping;
1295         tpa_info->start_buf.data = NULL;
1296         tpa_info->agg_state = QEDE_AGG_STATE_NONE;
1297         dev_kfree_skb_any(tpa_info->skb);
1298         tpa_info->skb = NULL;
1299 }
1300
1301 static bool qede_tunn_exist(u16 flag)
1302 {
1303         return !!(flag & (PARSING_AND_ERR_FLAGS_TUNNELEXIST_MASK <<
1304                           PARSING_AND_ERR_FLAGS_TUNNELEXIST_SHIFT));
1305 }
1306
1307 static u8 qede_check_tunn_csum(u16 flag)
1308 {
1309         u16 csum_flag = 0;
1310         u8 tcsum = 0;
1311
1312         if (flag & (PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_MASK <<
1313                     PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_SHIFT))
1314                 csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_MASK <<
1315                              PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_SHIFT;
1316
1317         if (flag & (PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
1318                     PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT)) {
1319                 csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
1320                              PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
1321                 tcsum = QEDE_TUNN_CSUM_UNNECESSARY;
1322         }
1323
1324         csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_MASK <<
1325                      PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_SHIFT |
1326                      PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
1327                      PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;
1328
1329         if (csum_flag & flag)
1330                 return QEDE_CSUM_ERROR;
1331
1332         return QEDE_CSUM_UNNECESSARY | tcsum;
1333 }
1334
1335 static u8 qede_check_notunn_csum(u16 flag)
1336 {
1337         u16 csum_flag = 0;
1338         u8 csum = 0;
1339
1340         if (flag & (PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
1341                     PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT)) {
1342                 csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
1343                              PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
1344                 csum = QEDE_CSUM_UNNECESSARY;
1345         }
1346
1347         csum_flag |= PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
1348                      PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;
1349
1350         if (csum_flag & flag)
1351                 return QEDE_CSUM_ERROR;
1352
1353         return csum;
1354 }
1355
1356 static u8 qede_check_csum(u16 flag)
1357 {
1358         if (!qede_tunn_exist(flag))
1359                 return qede_check_notunn_csum(flag);
1360         else
1361                 return qede_check_tunn_csum(flag);
1362 }
1363
1364 static bool qede_pkt_is_ip_fragmented(struct eth_fast_path_rx_reg_cqe *cqe,
1365                                       u16 flag)
1366 {
1367         u8 tun_pars_flg = cqe->tunnel_pars_flags.flags;
1368
1369         if ((tun_pars_flg & (ETH_TUNNEL_PARSING_FLAGS_IPV4_FRAGMENT_MASK <<
1370                              ETH_TUNNEL_PARSING_FLAGS_IPV4_FRAGMENT_SHIFT)) ||
1371             (flag & (PARSING_AND_ERR_FLAGS_IPV4FRAG_MASK <<
1372                      PARSING_AND_ERR_FLAGS_IPV4FRAG_SHIFT)))
1373                 return true;
1374
1375         return false;
1376 }
1377
1378 static int qede_rx_int(struct qede_fastpath *fp, int budget)
1379 {
1380         struct qede_dev *edev = fp->edev;
1381         struct qede_rx_queue *rxq = fp->rxq;
1382
1383         u16 hw_comp_cons, sw_comp_cons, sw_rx_index, parse_flag;
1384         int rx_pkt = 0;
1385         u8 csum_flag;
1386
1387         hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
1388         sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1389
1390         /* Memory barrier to prevent the CPU from doing speculative reads of CQE
1391          * / BD in the while-loop before reading hw_comp_cons. If the CQE is
1392          * read before it is written by FW, then FW writes CQE and SB, and then
1393          * the CPU reads the hw_comp_cons, it will use an old CQE.
1394          */
1395         rmb();
1396
1397         /* Loop to complete all indicated BDs */
1398         while (sw_comp_cons != hw_comp_cons) {
1399                 struct eth_fast_path_rx_reg_cqe *fp_cqe;
1400                 enum pkt_hash_types rxhash_type;
1401                 enum eth_rx_cqe_type cqe_type;
1402                 struct sw_rx_data *sw_rx_data;
1403                 union eth_rx_cqe *cqe;
1404                 struct sk_buff *skb;
1405                 struct page *data;
1406                 __le16 flags;
1407                 u16 len, pad;
1408                 u32 rx_hash;
1409
1410                 /* Get the CQE from the completion ring */
1411                 cqe = (union eth_rx_cqe *)
1412                         qed_chain_consume(&rxq->rx_comp_ring);
1413                 cqe_type = cqe->fast_path_regular.type;
1414
1415                 if (unlikely(cqe_type == ETH_RX_CQE_TYPE_SLOW_PATH)) {
1416                         edev->ops->eth_cqe_completion(
1417                                         edev->cdev, fp->id,
1418                                         (struct eth_slow_path_rx_cqe *)cqe);
1419                         goto next_cqe;
1420                 }
1421
1422                 if (cqe_type != ETH_RX_CQE_TYPE_REGULAR) {
1423                         switch (cqe_type) {
1424                         case ETH_RX_CQE_TYPE_TPA_START:
1425                                 qede_tpa_start(edev, rxq,
1426                                                &cqe->fast_path_tpa_start);
1427                                 goto next_cqe;
1428                         case ETH_RX_CQE_TYPE_TPA_CONT:
1429                                 qede_tpa_cont(edev, rxq,
1430                                               &cqe->fast_path_tpa_cont);
1431                                 goto next_cqe;
1432                         case ETH_RX_CQE_TYPE_TPA_END:
1433                                 qede_tpa_end(edev, fp,
1434                                              &cqe->fast_path_tpa_end);
1435                                 goto next_rx_only;
1436                         default:
1437                                 break;
1438                         }
1439                 }
1440
1441                 /* Get the data from the SW ring */
1442                 sw_rx_index = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1443                 sw_rx_data = &rxq->sw_rx_ring[sw_rx_index];
1444                 data = sw_rx_data->data;
1445
1446                 fp_cqe = &cqe->fast_path_regular;
1447                 len =  le16_to_cpu(fp_cqe->len_on_first_bd);
1448                 pad = fp_cqe->placement_offset;
1449                 flags = cqe->fast_path_regular.pars_flags.flags;
1450
1451                 /* If this is an error packet then drop it */
1452                 parse_flag = le16_to_cpu(flags);
1453
1454                 csum_flag = qede_check_csum(parse_flag);
1455                 if (unlikely(csum_flag == QEDE_CSUM_ERROR)) {
1456                         if (qede_pkt_is_ip_fragmented(&cqe->fast_path_regular,
1457                                                       parse_flag)) {
1458                                 rxq->rx_ip_frags++;
1459                                 goto alloc_skb;
1460                         }
1461
1462                         DP_NOTICE(edev,
1463                                   "CQE in CONS = %u has error, flags = %x, dropping incoming packet\n",
1464                                   sw_comp_cons, parse_flag);
1465                         rxq->rx_hw_errors++;
1466                         qede_recycle_rx_bd_ring(rxq, edev, fp_cqe->bd_num);
1467                         goto next_cqe;
1468                 }
1469
1470 alloc_skb:
1471                 skb = netdev_alloc_skb(edev->ndev, QEDE_RX_HDR_SIZE);
1472                 if (unlikely(!skb)) {
1473                         DP_NOTICE(edev,
1474                                   "skb allocation failed, dropping incoming packet\n");
1475                         qede_recycle_rx_bd_ring(rxq, edev, fp_cqe->bd_num);
1476                         rxq->rx_alloc_errors++;
1477                         goto next_cqe;
1478                 }
1479
1480                 /* Copy data into SKB */
1481                 if (len + pad <= edev->rx_copybreak) {
1482                         memcpy(skb_put(skb, len),
1483                                page_address(data) + pad +
1484                                 sw_rx_data->page_offset, len);
1485                         qede_reuse_page(edev, rxq, sw_rx_data);
1486                 } else {
1487                         struct skb_frag_struct *frag;
1488                         unsigned int pull_len;
1489                         unsigned char *va;
1490
1491                         frag = &skb_shinfo(skb)->frags[0];
1492
1493                         skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, data,
1494                                         pad + sw_rx_data->page_offset,
1495                                         len, rxq->rx_buf_seg_size);
1496
1497                         va = skb_frag_address(frag);
1498                         pull_len = eth_get_headlen(va, QEDE_RX_HDR_SIZE);
1499
1500                         /* Align the pull_len to optimize memcpy */
1501                         memcpy(skb->data, va, ALIGN(pull_len, sizeof(long)));
1502
1503                         skb_frag_size_sub(frag, pull_len);
1504                         frag->page_offset += pull_len;
1505                         skb->data_len -= pull_len;
1506                         skb->tail += pull_len;
1507
1508                         if (unlikely(qede_realloc_rx_buffer(edev, rxq,
1509                                                             sw_rx_data))) {
1510                                 DP_ERR(edev, "Failed to allocate rx buffer\n");
1511                                 /* Incr page ref count to reuse on allocation
1512                                  * failure so that it doesn't get freed while
1513                                  * freeing SKB.
1514                                  */
1515
1516                                 page_ref_inc(sw_rx_data->data);
1517                                 rxq->rx_alloc_errors++;
1518                                 qede_recycle_rx_bd_ring(rxq, edev,
1519                                                         fp_cqe->bd_num);
1520                                 dev_kfree_skb_any(skb);
1521                                 goto next_cqe;
1522                         }
1523                 }
1524
1525                 qede_rx_bd_ring_consume(rxq);
1526
1527                 if (fp_cqe->bd_num != 1) {
1528                         u16 pkt_len = le16_to_cpu(fp_cqe->pkt_len);
1529                         u8 num_frags;
1530
1531                         pkt_len -= len;
1532
1533                         for (num_frags = fp_cqe->bd_num - 1; num_frags > 0;
1534                              num_frags--) {
1535                                 u16 cur_size = pkt_len > rxq->rx_buf_size ?
1536                                                 rxq->rx_buf_size : pkt_len;
1537                                 if (unlikely(!cur_size)) {
1538                                         DP_ERR(edev,
1539                                                "Still got %d BDs for mapping jumbo, but length became 0\n",
1540                                                num_frags);
1541                                         qede_recycle_rx_bd_ring(rxq, edev,
1542                                                                 num_frags);
1543                                         dev_kfree_skb_any(skb);
1544                                         goto next_cqe;
1545                                 }
1546
1547                                 if (unlikely(qede_alloc_rx_buffer(edev, rxq))) {
1548                                         qede_recycle_rx_bd_ring(rxq, edev,
1549                                                                 num_frags);
1550                                         dev_kfree_skb_any(skb);
1551                                         goto next_cqe;
1552                                 }
1553
1554                                 sw_rx_index = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1555                                 sw_rx_data = &rxq->sw_rx_ring[sw_rx_index];
1556                                 qede_rx_bd_ring_consume(rxq);
1557
1558                                 dma_unmap_page(&edev->pdev->dev,
1559                                                sw_rx_data->mapping,
1560                                                PAGE_SIZE, DMA_FROM_DEVICE);
1561
1562                                 skb_fill_page_desc(skb,
1563                                                    skb_shinfo(skb)->nr_frags++,
1564                                                    sw_rx_data->data, 0,
1565                                                    cur_size);
1566
1567                                 skb->truesize += PAGE_SIZE;
1568                                 skb->data_len += cur_size;
1569                                 skb->len += cur_size;
1570                                 pkt_len -= cur_size;
1571                         }
1572
1573                         if (unlikely(pkt_len))
1574                                 DP_ERR(edev,
1575                                        "Mapped all BDs of jumbo, but still have %d bytes\n",
1576                                        pkt_len);
1577                 }
1578
1579                 skb->protocol = eth_type_trans(skb, edev->ndev);
1580
1581                 rx_hash = qede_get_rxhash(edev, fp_cqe->bitfields,
1582                                           fp_cqe->rss_hash, &rxhash_type);
1583
1584                 skb_set_hash(skb, rx_hash, rxhash_type);
1585
1586                 qede_set_skb_csum(skb, csum_flag);
1587
1588                 skb_record_rx_queue(skb, fp->rxq->rxq_id);
1589
1590                 qede_skb_receive(edev, fp, skb, le16_to_cpu(fp_cqe->vlan_tag));
1591 next_rx_only:
1592                 rx_pkt++;
1593
1594 next_cqe: /* don't consume bd rx buffer */
1595                 qed_chain_recycle_consumed(&rxq->rx_comp_ring);
1596                 sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1597                 /* CR TPA - revisit how to handle budget in TPA perhaps
1598                  * increase on "end"
1599                  */
1600                 if (rx_pkt == budget)
1601                         break;
1602         } /* repeat while sw_comp_cons != hw_comp_cons... */
1603
1604         /* Update producers */
1605         qede_update_rx_prod(edev, rxq);
1606
1607         rxq->rcv_pkts += rx_pkt;
1608
1609         return rx_pkt;
1610 }
1611
1612 static int qede_poll(struct napi_struct *napi, int budget)
1613 {
1614         struct qede_fastpath *fp = container_of(napi, struct qede_fastpath,
1615                                                 napi);
1616         struct qede_dev *edev = fp->edev;
1617         int rx_work_done = 0;
1618         u8 tc;
1619
1620         for (tc = 0; tc < edev->num_tc; tc++)
1621                 if (likely(fp->type & QEDE_FASTPATH_TX) &&
1622                     qede_txq_has_work(&fp->txqs[tc]))
1623                         qede_tx_int(edev, &fp->txqs[tc]);
1624
1625         rx_work_done = (likely(fp->type & QEDE_FASTPATH_RX) &&
1626                         qede_has_rx_work(fp->rxq)) ?
1627                         qede_rx_int(fp, budget) : 0;
1628         if (rx_work_done < budget) {
1629                 qed_sb_update_sb_idx(fp->sb_info);
1630                 /* *_has_*_work() reads the status block,
1631                  * thus we need to ensure that status block indices
1632                  * have been actually read (qed_sb_update_sb_idx)
1633                  * prior to this check (*_has_*_work) so that
1634                  * we won't write the "newer" value of the status block
1635                  * to HW (if there was a DMA right after
1636                  * qede_has_rx_work and if there is no rmb, the memory
1637                  * reading (qed_sb_update_sb_idx) may be postponed
1638                  * to right before *_ack_sb). In this case there
1639                  * will never be another interrupt until there is
1640                  * another update of the status block, while there
1641                  * is still unhandled work.
1642                  */
1643                 rmb();
1644
1645                 /* Fall out from the NAPI loop if needed */
1646                 if (!((likely(fp->type & QEDE_FASTPATH_RX) &&
1647                        qede_has_rx_work(fp->rxq)) ||
1648                       (likely(fp->type & QEDE_FASTPATH_TX) &&
1649                        qede_has_tx_work(fp)))) {
1650                         napi_complete(napi);
1651
1652                         /* Update and reenable interrupts */
1653                         qed_sb_ack(fp->sb_info, IGU_INT_ENABLE,
1654                                    1 /*update*/);
1655                 } else {
1656                         rx_work_done = budget;
1657                 }
1658         }
1659
1660         return rx_work_done;
1661 }
1662
1663 static irqreturn_t qede_msix_fp_int(int irq, void *fp_cookie)
1664 {
1665         struct qede_fastpath *fp = fp_cookie;
1666
1667         qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
1668
1669         napi_schedule_irqoff(&fp->napi);
1670         return IRQ_HANDLED;
1671 }
1672
1673 /* -------------------------------------------------------------------------
1674  * END OF FAST-PATH
1675  * -------------------------------------------------------------------------
1676  */
1677
1678 static int qede_open(struct net_device *ndev);
1679 static int qede_close(struct net_device *ndev);
1680 static int qede_set_mac_addr(struct net_device *ndev, void *p);
1681 static void qede_set_rx_mode(struct net_device *ndev);
1682 static void qede_config_rx_mode(struct net_device *ndev);
1683
1684 static int qede_set_ucast_rx_mac(struct qede_dev *edev,
1685                                  enum qed_filter_xcast_params_type opcode,
1686                                  unsigned char mac[ETH_ALEN])
1687 {
1688         struct qed_filter_params filter_cmd;
1689
1690         memset(&filter_cmd, 0, sizeof(filter_cmd));
1691         filter_cmd.type = QED_FILTER_TYPE_UCAST;
1692         filter_cmd.filter.ucast.type = opcode;
1693         filter_cmd.filter.ucast.mac_valid = 1;
1694         ether_addr_copy(filter_cmd.filter.ucast.mac, mac);
1695
1696         return edev->ops->filter_config(edev->cdev, &filter_cmd);
1697 }
1698
1699 static int qede_set_ucast_rx_vlan(struct qede_dev *edev,
1700                                   enum qed_filter_xcast_params_type opcode,
1701                                   u16 vid)
1702 {
1703         struct qed_filter_params filter_cmd;
1704
1705         memset(&filter_cmd, 0, sizeof(filter_cmd));
1706         filter_cmd.type = QED_FILTER_TYPE_UCAST;
1707         filter_cmd.filter.ucast.type = opcode;
1708         filter_cmd.filter.ucast.vlan_valid = 1;
1709         filter_cmd.filter.ucast.vlan = vid;
1710
1711         return edev->ops->filter_config(edev->cdev, &filter_cmd);
1712 }
1713
1714 void qede_fill_by_demand_stats(struct qede_dev *edev)
1715 {
1716         struct qed_eth_stats stats;
1717
1718         edev->ops->get_vport_stats(edev->cdev, &stats);
1719         edev->stats.no_buff_discards = stats.no_buff_discards;
1720         edev->stats.packet_too_big_discard = stats.packet_too_big_discard;
1721         edev->stats.ttl0_discard = stats.ttl0_discard;
1722         edev->stats.rx_ucast_bytes = stats.rx_ucast_bytes;
1723         edev->stats.rx_mcast_bytes = stats.rx_mcast_bytes;
1724         edev->stats.rx_bcast_bytes = stats.rx_bcast_bytes;
1725         edev->stats.rx_ucast_pkts = stats.rx_ucast_pkts;
1726         edev->stats.rx_mcast_pkts = stats.rx_mcast_pkts;
1727         edev->stats.rx_bcast_pkts = stats.rx_bcast_pkts;
1728         edev->stats.mftag_filter_discards = stats.mftag_filter_discards;
1729         edev->stats.mac_filter_discards = stats.mac_filter_discards;
1730
1731         edev->stats.tx_ucast_bytes = stats.tx_ucast_bytes;
1732         edev->stats.tx_mcast_bytes = stats.tx_mcast_bytes;
1733         edev->stats.tx_bcast_bytes = stats.tx_bcast_bytes;
1734         edev->stats.tx_ucast_pkts = stats.tx_ucast_pkts;
1735         edev->stats.tx_mcast_pkts = stats.tx_mcast_pkts;
1736         edev->stats.tx_bcast_pkts = stats.tx_bcast_pkts;
1737         edev->stats.tx_err_drop_pkts = stats.tx_err_drop_pkts;
1738         edev->stats.coalesced_pkts = stats.tpa_coalesced_pkts;
1739         edev->stats.coalesced_events = stats.tpa_coalesced_events;
1740         edev->stats.coalesced_aborts_num = stats.tpa_aborts_num;
1741         edev->stats.non_coalesced_pkts = stats.tpa_not_coalesced_pkts;
1742         edev->stats.coalesced_bytes = stats.tpa_coalesced_bytes;
1743
1744         edev->stats.rx_64_byte_packets = stats.rx_64_byte_packets;
1745         edev->stats.rx_65_to_127_byte_packets = stats.rx_65_to_127_byte_packets;
1746         edev->stats.rx_128_to_255_byte_packets =
1747                                 stats.rx_128_to_255_byte_packets;
1748         edev->stats.rx_256_to_511_byte_packets =
1749                                 stats.rx_256_to_511_byte_packets;
1750         edev->stats.rx_512_to_1023_byte_packets =
1751                                 stats.rx_512_to_1023_byte_packets;
1752         edev->stats.rx_1024_to_1518_byte_packets =
1753                                 stats.rx_1024_to_1518_byte_packets;
1754         edev->stats.rx_1519_to_1522_byte_packets =
1755                                 stats.rx_1519_to_1522_byte_packets;
1756         edev->stats.rx_1519_to_2047_byte_packets =
1757                                 stats.rx_1519_to_2047_byte_packets;
1758         edev->stats.rx_2048_to_4095_byte_packets =
1759                                 stats.rx_2048_to_4095_byte_packets;
1760         edev->stats.rx_4096_to_9216_byte_packets =
1761                                 stats.rx_4096_to_9216_byte_packets;
1762         edev->stats.rx_9217_to_16383_byte_packets =
1763                                 stats.rx_9217_to_16383_byte_packets;
1764         edev->stats.rx_crc_errors = stats.rx_crc_errors;
1765         edev->stats.rx_mac_crtl_frames = stats.rx_mac_crtl_frames;
1766         edev->stats.rx_pause_frames = stats.rx_pause_frames;
1767         edev->stats.rx_pfc_frames = stats.rx_pfc_frames;
1768         edev->stats.rx_align_errors = stats.rx_align_errors;
1769         edev->stats.rx_carrier_errors = stats.rx_carrier_errors;
1770         edev->stats.rx_oversize_packets = stats.rx_oversize_packets;
1771         edev->stats.rx_jabbers = stats.rx_jabbers;
1772         edev->stats.rx_undersize_packets = stats.rx_undersize_packets;
1773         edev->stats.rx_fragments = stats.rx_fragments;
1774         edev->stats.tx_64_byte_packets = stats.tx_64_byte_packets;
1775         edev->stats.tx_65_to_127_byte_packets = stats.tx_65_to_127_byte_packets;
1776         edev->stats.tx_128_to_255_byte_packets =
1777                                 stats.tx_128_to_255_byte_packets;
1778         edev->stats.tx_256_to_511_byte_packets =
1779                                 stats.tx_256_to_511_byte_packets;
1780         edev->stats.tx_512_to_1023_byte_packets =
1781                                 stats.tx_512_to_1023_byte_packets;
1782         edev->stats.tx_1024_to_1518_byte_packets =
1783                                 stats.tx_1024_to_1518_byte_packets;
1784         edev->stats.tx_1519_to_2047_byte_packets =
1785                                 stats.tx_1519_to_2047_byte_packets;
1786         edev->stats.tx_2048_to_4095_byte_packets =
1787                                 stats.tx_2048_to_4095_byte_packets;
1788         edev->stats.tx_4096_to_9216_byte_packets =
1789                                 stats.tx_4096_to_9216_byte_packets;
1790         edev->stats.tx_9217_to_16383_byte_packets =
1791                                 stats.tx_9217_to_16383_byte_packets;
1792         edev->stats.tx_pause_frames = stats.tx_pause_frames;
1793         edev->stats.tx_pfc_frames = stats.tx_pfc_frames;
1794         edev->stats.tx_lpi_entry_count = stats.tx_lpi_entry_count;
1795         edev->stats.tx_total_collisions = stats.tx_total_collisions;
1796         edev->stats.brb_truncates = stats.brb_truncates;
1797         edev->stats.brb_discards = stats.brb_discards;
1798         edev->stats.tx_mac_ctrl_frames = stats.tx_mac_ctrl_frames;
1799 }
1800
1801 static
1802 struct rtnl_link_stats64 *qede_get_stats64(struct net_device *dev,
1803                                            struct rtnl_link_stats64 *stats)
1804 {
1805         struct qede_dev *edev = netdev_priv(dev);
1806
1807         qede_fill_by_demand_stats(edev);
1808
1809         stats->rx_packets = edev->stats.rx_ucast_pkts +
1810                             edev->stats.rx_mcast_pkts +
1811                             edev->stats.rx_bcast_pkts;
1812         stats->tx_packets = edev->stats.tx_ucast_pkts +
1813                             edev->stats.tx_mcast_pkts +
1814                             edev->stats.tx_bcast_pkts;
1815
1816         stats->rx_bytes = edev->stats.rx_ucast_bytes +
1817                           edev->stats.rx_mcast_bytes +
1818                           edev->stats.rx_bcast_bytes;
1819
1820         stats->tx_bytes = edev->stats.tx_ucast_bytes +
1821                           edev->stats.tx_mcast_bytes +
1822                           edev->stats.tx_bcast_bytes;
1823
1824         stats->tx_errors = edev->stats.tx_err_drop_pkts;
1825         stats->multicast = edev->stats.rx_mcast_pkts +
1826                            edev->stats.rx_bcast_pkts;
1827
1828         stats->rx_fifo_errors = edev->stats.no_buff_discards;
1829
1830         stats->collisions = edev->stats.tx_total_collisions;
1831         stats->rx_crc_errors = edev->stats.rx_crc_errors;
1832         stats->rx_frame_errors = edev->stats.rx_align_errors;
1833
1834         return stats;
1835 }
1836
1837 #ifdef CONFIG_QED_SRIOV
1838 static int qede_get_vf_config(struct net_device *dev, int vfidx,
1839                               struct ifla_vf_info *ivi)
1840 {
1841         struct qede_dev *edev = netdev_priv(dev);
1842
1843         if (!edev->ops)
1844                 return -EINVAL;
1845
1846         return edev->ops->iov->get_config(edev->cdev, vfidx, ivi);
1847 }
1848
1849 static int qede_set_vf_rate(struct net_device *dev, int vfidx,
1850                             int min_tx_rate, int max_tx_rate)
1851 {
1852         struct qede_dev *edev = netdev_priv(dev);
1853
1854         return edev->ops->iov->set_rate(edev->cdev, vfidx, min_tx_rate,
1855                                         max_tx_rate);
1856 }
1857
1858 static int qede_set_vf_spoofchk(struct net_device *dev, int vfidx, bool val)
1859 {
1860         struct qede_dev *edev = netdev_priv(dev);
1861
1862         if (!edev->ops)
1863                 return -EINVAL;
1864
1865         return edev->ops->iov->set_spoof(edev->cdev, vfidx, val);
1866 }
1867
1868 static int qede_set_vf_link_state(struct net_device *dev, int vfidx,
1869                                   int link_state)
1870 {
1871         struct qede_dev *edev = netdev_priv(dev);
1872
1873         if (!edev->ops)
1874                 return -EINVAL;
1875
1876         return edev->ops->iov->set_link_state(edev->cdev, vfidx, link_state);
1877 }
1878 #endif
1879
1880 static void qede_config_accept_any_vlan(struct qede_dev *edev, bool action)
1881 {
1882         struct qed_update_vport_params params;
1883         int rc;
1884
1885         /* Proceed only if action actually needs to be performed */
1886         if (edev->accept_any_vlan == action)
1887                 return;
1888
1889         memset(&params, 0, sizeof(params));
1890
1891         params.vport_id = 0;
1892         params.accept_any_vlan = action;
1893         params.update_accept_any_vlan_flg = 1;
1894
1895         rc = edev->ops->vport_update(edev->cdev, &params);
1896         if (rc) {
1897                 DP_ERR(edev, "Failed to %s accept-any-vlan\n",
1898                        action ? "enable" : "disable");
1899         } else {
1900                 DP_INFO(edev, "%s accept-any-vlan\n",
1901                         action ? "enabled" : "disabled");
1902                 edev->accept_any_vlan = action;
1903         }
1904 }
1905
1906 static int qede_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1907 {
1908         struct qede_dev *edev = netdev_priv(dev);
1909         struct qede_vlan *vlan, *tmp;
1910         int rc;
1911
1912         DP_VERBOSE(edev, NETIF_MSG_IFUP, "Adding vlan 0x%04x\n", vid);
1913
1914         vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1915         if (!vlan) {
1916                 DP_INFO(edev, "Failed to allocate struct for vlan\n");
1917                 return -ENOMEM;
1918         }
1919         INIT_LIST_HEAD(&vlan->list);
1920         vlan->vid = vid;
1921         vlan->configured = false;
1922
1923         /* Verify vlan isn't already configured */
1924         list_for_each_entry(tmp, &edev->vlan_list, list) {
1925                 if (tmp->vid == vlan->vid) {
1926                         DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
1927                                    "vlan already configured\n");
1928                         kfree(vlan);
1929                         return -EEXIST;
1930                 }
1931         }
1932
1933         /* If interface is down, cache this VLAN ID and return */
1934         if (edev->state != QEDE_STATE_OPEN) {
1935                 DP_VERBOSE(edev, NETIF_MSG_IFDOWN,
1936                            "Interface is down, VLAN %d will be configured when interface is up\n",
1937                            vid);
1938                 if (vid != 0)
1939                         edev->non_configured_vlans++;
1940                 list_add(&vlan->list, &edev->vlan_list);
1941
1942                 return 0;
1943         }
1944
1945         /* Check for the filter limit.
1946          * Note - vlan0 has a reserved filter and can be added without
1947          * worrying about quota
1948          */
1949         if ((edev->configured_vlans < edev->dev_info.num_vlan_filters) ||
1950             (vlan->vid == 0)) {
1951                 rc = qede_set_ucast_rx_vlan(edev,
1952                                             QED_FILTER_XCAST_TYPE_ADD,
1953                                             vlan->vid);
1954                 if (rc) {
1955                         DP_ERR(edev, "Failed to configure VLAN %d\n",
1956                                vlan->vid);
1957                         kfree(vlan);
1958                         return -EINVAL;
1959                 }
1960                 vlan->configured = true;
1961
1962                 /* vlan0 filter isn't consuming out of our quota */
1963                 if (vlan->vid != 0)
1964                         edev->configured_vlans++;
1965         } else {
1966                 /* Out of quota; Activate accept-any-VLAN mode */
1967                 if (!edev->non_configured_vlans)
1968                         qede_config_accept_any_vlan(edev, true);
1969
1970                 edev->non_configured_vlans++;
1971         }
1972
1973         list_add(&vlan->list, &edev->vlan_list);
1974
1975         return 0;
1976 }
1977
1978 static void qede_del_vlan_from_list(struct qede_dev *edev,
1979                                     struct qede_vlan *vlan)
1980 {
1981         /* vlan0 filter isn't consuming out of our quota */
1982         if (vlan->vid != 0) {
1983                 if (vlan->configured)
1984                         edev->configured_vlans--;
1985                 else
1986                         edev->non_configured_vlans--;
1987         }
1988
1989         list_del(&vlan->list);
1990         kfree(vlan);
1991 }
1992
1993 static int qede_configure_vlan_filters(struct qede_dev *edev)
1994 {
1995         int rc = 0, real_rc = 0, accept_any_vlan = 0;
1996         struct qed_dev_eth_info *dev_info;
1997         struct qede_vlan *vlan = NULL;
1998
1999         if (list_empty(&edev->vlan_list))
2000                 return 0;
2001
2002         dev_info = &edev->dev_info;
2003
2004         /* Configure non-configured vlans */
2005         list_for_each_entry(vlan, &edev->vlan_list, list) {
2006                 if (vlan->configured)
2007                         continue;
2008
2009                 /* We have used all our credits, now enable accept_any_vlan */
2010                 if ((vlan->vid != 0) &&
2011                     (edev->configured_vlans == dev_info->num_vlan_filters)) {
2012                         accept_any_vlan = 1;
2013                         continue;
2014                 }
2015
2016                 DP_VERBOSE(edev, NETIF_MSG_IFUP, "Adding vlan %d\n", vlan->vid);
2017
2018                 rc = qede_set_ucast_rx_vlan(edev, QED_FILTER_XCAST_TYPE_ADD,
2019                                             vlan->vid);
2020                 if (rc) {
2021                         DP_ERR(edev, "Failed to configure VLAN %u\n",
2022                                vlan->vid);
2023                         real_rc = rc;
2024                         continue;
2025                 }
2026
2027                 vlan->configured = true;
2028                 /* vlan0 filter doesn't consume our VLAN filter's quota */
2029                 if (vlan->vid != 0) {
2030                         edev->non_configured_vlans--;
2031                         edev->configured_vlans++;
2032                 }
2033         }
2034
2035         /* enable accept_any_vlan mode if we have more VLANs than credits,
2036          * or remove accept_any_vlan mode if we've actually removed
2037          * a non-configured vlan, and all remaining vlans are truly configured.
2038          */
2039
2040         if (accept_any_vlan)
2041                 qede_config_accept_any_vlan(edev, true);
2042         else if (!edev->non_configured_vlans)
2043                 qede_config_accept_any_vlan(edev, false);
2044
2045         return real_rc;
2046 }
2047
2048 static int qede_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
2049 {
2050         struct qede_dev *edev = netdev_priv(dev);
2051         struct qede_vlan *vlan = NULL;
2052         int rc;
2053
2054         DP_VERBOSE(edev, NETIF_MSG_IFDOWN, "Removing vlan 0x%04x\n", vid);
2055
2056         /* Find whether entry exists */
2057         list_for_each_entry(vlan, &edev->vlan_list, list)
2058                 if (vlan->vid == vid)
2059                         break;
2060
2061         if (!vlan || (vlan->vid != vid)) {
2062                 DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
2063                            "Vlan isn't configured\n");
2064                 return 0;
2065         }
2066
2067         if (edev->state != QEDE_STATE_OPEN) {
2068                 /* As interface is already down, we don't have a VPORT
2069                  * instance to remove vlan filter. So just update vlan list
2070                  */
2071                 DP_VERBOSE(edev, NETIF_MSG_IFDOWN,
2072                            "Interface is down, removing VLAN from list only\n");
2073                 qede_del_vlan_from_list(edev, vlan);
2074                 return 0;
2075         }
2076
2077         /* Remove vlan */
2078         if (vlan->configured) {
2079                 rc = qede_set_ucast_rx_vlan(edev, QED_FILTER_XCAST_TYPE_DEL,
2080                                             vid);
2081                 if (rc) {
2082                         DP_ERR(edev, "Failed to remove VLAN %d\n", vid);
2083                         return -EINVAL;
2084                 }
2085         }
2086
2087         qede_del_vlan_from_list(edev, vlan);
2088
2089         /* We have removed a VLAN - try to see if we can
2090          * configure non-configured VLAN from the list.
2091          */
2092         rc = qede_configure_vlan_filters(edev);
2093
2094         return rc;
2095 }
2096
2097 static void qede_vlan_mark_nonconfigured(struct qede_dev *edev)
2098 {
2099         struct qede_vlan *vlan = NULL;
2100
2101         if (list_empty(&edev->vlan_list))
2102                 return;
2103
2104         list_for_each_entry(vlan, &edev->vlan_list, list) {
2105                 if (!vlan->configured)
2106                         continue;
2107
2108                 vlan->configured = false;
2109
2110                 /* vlan0 filter isn't consuming out of our quota */
2111                 if (vlan->vid != 0) {
2112                         edev->non_configured_vlans++;
2113                         edev->configured_vlans--;
2114                 }
2115
2116                 DP_VERBOSE(edev, NETIF_MSG_IFDOWN,
2117                            "marked vlan %d as non-configured\n", vlan->vid);
2118         }
2119
2120         edev->accept_any_vlan = false;
2121 }
2122
2123 static int qede_set_features(struct net_device *dev, netdev_features_t features)
2124 {
2125         struct qede_dev *edev = netdev_priv(dev);
2126         netdev_features_t changes = features ^ dev->features;
2127         bool need_reload = false;
2128
2129         /* No action needed if hardware GRO is disabled during driver load */
2130         if (changes & NETIF_F_GRO) {
2131                 if (dev->features & NETIF_F_GRO)
2132                         need_reload = !edev->gro_disable;
2133                 else
2134                         need_reload = edev->gro_disable;
2135         }
2136
2137         if (need_reload && netif_running(edev->ndev)) {
2138                 dev->features = features;
2139                 qede_reload(edev, NULL, NULL);
2140                 return 1;
2141         }
2142
2143         return 0;
2144 }
2145
2146 static void qede_udp_tunnel_add(struct net_device *dev,
2147                                 struct udp_tunnel_info *ti)
2148 {
2149         struct qede_dev *edev = netdev_priv(dev);
2150         u16 t_port = ntohs(ti->port);
2151
2152         switch (ti->type) {
2153         case UDP_TUNNEL_TYPE_VXLAN:
2154                 if (edev->vxlan_dst_port)
2155                         return;
2156
2157                 edev->vxlan_dst_port = t_port;
2158
2159                 DP_VERBOSE(edev, QED_MSG_DEBUG, "Added vxlan port=%d\n",
2160                            t_port);
2161
2162                 set_bit(QEDE_SP_VXLAN_PORT_CONFIG, &edev->sp_flags);
2163                 break;
2164         case UDP_TUNNEL_TYPE_GENEVE:
2165                 if (edev->geneve_dst_port)
2166                         return;
2167
2168                 edev->geneve_dst_port = t_port;
2169
2170                 DP_VERBOSE(edev, QED_MSG_DEBUG, "Added geneve port=%d\n",
2171                            t_port);
2172                 set_bit(QEDE_SP_GENEVE_PORT_CONFIG, &edev->sp_flags);
2173                 break;
2174         default:
2175                 return;
2176         }
2177
2178         schedule_delayed_work(&edev->sp_task, 0);
2179 }
2180
2181 static void qede_udp_tunnel_del(struct net_device *dev,
2182                                 struct udp_tunnel_info *ti)
2183 {
2184         struct qede_dev *edev = netdev_priv(dev);
2185         u16 t_port = ntohs(ti->port);
2186
2187         switch (ti->type) {
2188         case UDP_TUNNEL_TYPE_VXLAN:
2189                 if (t_port != edev->vxlan_dst_port)
2190                         return;
2191
2192                 edev->vxlan_dst_port = 0;
2193
2194                 DP_VERBOSE(edev, QED_MSG_DEBUG, "Deleted vxlan port=%d\n",
2195                            t_port);
2196
2197                 set_bit(QEDE_SP_VXLAN_PORT_CONFIG, &edev->sp_flags);
2198                 break;
2199         case UDP_TUNNEL_TYPE_GENEVE:
2200                 if (t_port != edev->geneve_dst_port)
2201                         return;
2202
2203                 edev->geneve_dst_port = 0;
2204
2205                 DP_VERBOSE(edev, QED_MSG_DEBUG, "Deleted geneve port=%d\n",
2206                            t_port);
2207                 set_bit(QEDE_SP_GENEVE_PORT_CONFIG, &edev->sp_flags);
2208                 break;
2209         default:
2210                 return;
2211         }
2212
2213         schedule_delayed_work(&edev->sp_task, 0);
2214 }
2215
2216 static const struct net_device_ops qede_netdev_ops = {
2217         .ndo_open = qede_open,
2218         .ndo_stop = qede_close,
2219         .ndo_start_xmit = qede_start_xmit,
2220         .ndo_set_rx_mode = qede_set_rx_mode,
2221         .ndo_set_mac_address = qede_set_mac_addr,
2222         .ndo_validate_addr = eth_validate_addr,
2223         .ndo_change_mtu = qede_change_mtu,
2224 #ifdef CONFIG_QED_SRIOV
2225         .ndo_set_vf_mac = qede_set_vf_mac,
2226         .ndo_set_vf_vlan = qede_set_vf_vlan,
2227 #endif
2228         .ndo_vlan_rx_add_vid = qede_vlan_rx_add_vid,
2229         .ndo_vlan_rx_kill_vid = qede_vlan_rx_kill_vid,
2230         .ndo_set_features = qede_set_features,
2231         .ndo_get_stats64 = qede_get_stats64,
2232 #ifdef CONFIG_QED_SRIOV
2233         .ndo_set_vf_link_state = qede_set_vf_link_state,
2234         .ndo_set_vf_spoofchk = qede_set_vf_spoofchk,
2235         .ndo_get_vf_config = qede_get_vf_config,
2236         .ndo_set_vf_rate = qede_set_vf_rate,
2237 #endif
2238         .ndo_udp_tunnel_add = qede_udp_tunnel_add,
2239         .ndo_udp_tunnel_del = qede_udp_tunnel_del,
2240 };
2241
2242 /* -------------------------------------------------------------------------
2243  * START OF PROBE / REMOVE
2244  * -------------------------------------------------------------------------
2245  */
2246
2247 static struct qede_dev *qede_alloc_etherdev(struct qed_dev *cdev,
2248                                             struct pci_dev *pdev,
2249                                             struct qed_dev_eth_info *info,
2250                                             u32 dp_module, u8 dp_level)
2251 {
2252         struct net_device *ndev;
2253         struct qede_dev *edev;
2254
2255         ndev = alloc_etherdev_mqs(sizeof(*edev),
2256                                   info->num_queues, info->num_queues);
2257         if (!ndev) {
2258                 pr_err("etherdev allocation failed\n");
2259                 return NULL;
2260         }
2261
2262         edev = netdev_priv(ndev);
2263         edev->ndev = ndev;
2264         edev->cdev = cdev;
2265         edev->pdev = pdev;
2266         edev->dp_module = dp_module;
2267         edev->dp_level = dp_level;
2268         edev->ops = qed_ops;
2269         edev->q_num_rx_buffers = NUM_RX_BDS_DEF;
2270         edev->q_num_tx_buffers = NUM_TX_BDS_DEF;
2271
2272         DP_INFO(edev, "Allocated netdev with %d tx queues and %d rx queues\n",
2273                 info->num_queues, info->num_queues);
2274
2275         SET_NETDEV_DEV(ndev, &pdev->dev);
2276
2277         memset(&edev->stats, 0, sizeof(edev->stats));
2278         memcpy(&edev->dev_info, info, sizeof(*info));
2279
2280         edev->num_tc = edev->dev_info.num_tc;
2281
2282         INIT_LIST_HEAD(&edev->vlan_list);
2283
2284         return edev;
2285 }
2286
2287 static void qede_init_ndev(struct qede_dev *edev)
2288 {
2289         struct net_device *ndev = edev->ndev;
2290         struct pci_dev *pdev = edev->pdev;
2291         u32 hw_features;
2292
2293         pci_set_drvdata(pdev, ndev);
2294
2295         ndev->mem_start = edev->dev_info.common.pci_mem_start;
2296         ndev->base_addr = ndev->mem_start;
2297         ndev->mem_end = edev->dev_info.common.pci_mem_end;
2298         ndev->irq = edev->dev_info.common.pci_irq;
2299
2300         ndev->watchdog_timeo = TX_TIMEOUT;
2301
2302         ndev->netdev_ops = &qede_netdev_ops;
2303
2304         qede_set_ethtool_ops(ndev);
2305
2306         /* user-changeble features */
2307         hw_features = NETIF_F_GRO | NETIF_F_SG |
2308                       NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2309                       NETIF_F_TSO | NETIF_F_TSO6;
2310
2311         /* Encap features*/
2312         hw_features |= NETIF_F_GSO_GRE | NETIF_F_GSO_UDP_TUNNEL |
2313                        NETIF_F_TSO_ECN;
2314         ndev->hw_enc_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2315                                 NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO_ECN |
2316                                 NETIF_F_TSO6 | NETIF_F_GSO_GRE |
2317                                 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_RXCSUM;
2318
2319         ndev->vlan_features = hw_features | NETIF_F_RXHASH | NETIF_F_RXCSUM |
2320                               NETIF_F_HIGHDMA;
2321         ndev->features = hw_features | NETIF_F_RXHASH | NETIF_F_RXCSUM |
2322                          NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HIGHDMA |
2323                          NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_TX;
2324
2325         ndev->hw_features = hw_features;
2326
2327         /* Set network device HW mac */
2328         ether_addr_copy(edev->ndev->dev_addr, edev->dev_info.common.hw_mac);
2329 }
2330
2331 /* This function converts from 32b param to two params of level and module
2332  * Input 32b decoding:
2333  * b31 - enable all NOTICE prints. NOTICE prints are for deviation from the
2334  * 'happy' flow, e.g. memory allocation failed.
2335  * b30 - enable all INFO prints. INFO prints are for major steps in the flow
2336  * and provide important parameters.
2337  * b29-b0 - per-module bitmap, where each bit enables VERBOSE prints of that
2338  * module. VERBOSE prints are for tracking the specific flow in low level.
2339  *
2340  * Notice that the level should be that of the lowest required logs.
2341  */
2342 void qede_config_debug(uint debug, u32 *p_dp_module, u8 *p_dp_level)
2343 {
2344         *p_dp_level = QED_LEVEL_NOTICE;
2345         *p_dp_module = 0;
2346
2347         if (debug & QED_LOG_VERBOSE_MASK) {
2348                 *p_dp_level = QED_LEVEL_VERBOSE;
2349                 *p_dp_module = (debug & 0x3FFFFFFF);
2350         } else if (debug & QED_LOG_INFO_MASK) {
2351                 *p_dp_level = QED_LEVEL_INFO;
2352         } else if (debug & QED_LOG_NOTICE_MASK) {
2353                 *p_dp_level = QED_LEVEL_NOTICE;
2354         }
2355 }
2356
2357 static void qede_free_fp_array(struct qede_dev *edev)
2358 {
2359         if (edev->fp_array) {
2360                 struct qede_fastpath *fp;
2361                 int i;
2362
2363                 for_each_queue(i) {
2364                         fp = &edev->fp_array[i];
2365
2366                         kfree(fp->sb_info);
2367                         kfree(fp->rxq);
2368                         kfree(fp->txqs);
2369                 }
2370                 kfree(edev->fp_array);
2371         }
2372
2373         edev->num_queues = 0;
2374         edev->fp_num_tx = 0;
2375         edev->fp_num_rx = 0;
2376 }
2377
2378 static int qede_alloc_fp_array(struct qede_dev *edev)
2379 {
2380         u8 fp_combined, fp_rx = edev->fp_num_rx;
2381         struct qede_fastpath *fp;
2382         int i;
2383
2384         edev->fp_array = kcalloc(QEDE_QUEUE_CNT(edev),
2385                                  sizeof(*edev->fp_array), GFP_KERNEL);
2386         if (!edev->fp_array) {
2387                 DP_NOTICE(edev, "fp array allocation failed\n");
2388                 goto err;
2389         }
2390
2391         fp_combined = QEDE_QUEUE_CNT(edev) - fp_rx - edev->fp_num_tx;
2392
2393         /* Allocate the FP elements for Rx queues followed by combined and then
2394          * the Tx. This ordering should be maintained so that the respective
2395          * queues (Rx or Tx) will be together in the fastpath array and the
2396          * associated ids will be sequential.
2397          */
2398         for_each_queue(i) {
2399                 fp = &edev->fp_array[i];
2400
2401                 fp->sb_info = kcalloc(1, sizeof(*fp->sb_info), GFP_KERNEL);
2402                 if (!fp->sb_info) {
2403                         DP_NOTICE(edev, "sb info struct allocation failed\n");
2404                         goto err;
2405                 }
2406
2407                 if (fp_rx) {
2408                         fp->type = QEDE_FASTPATH_RX;
2409                         fp_rx--;
2410                 } else if (fp_combined) {
2411                         fp->type = QEDE_FASTPATH_COMBINED;
2412                         fp_combined--;
2413                 } else {
2414                         fp->type = QEDE_FASTPATH_TX;
2415                 }
2416
2417                 if (fp->type & QEDE_FASTPATH_TX) {
2418                         fp->txqs = kcalloc(edev->num_tc, sizeof(*fp->txqs),
2419                                            GFP_KERNEL);
2420                         if (!fp->txqs) {
2421                                 DP_NOTICE(edev,
2422                                           "TXQ array allocation failed\n");
2423                                 goto err;
2424                         }
2425                 }
2426
2427                 if (fp->type & QEDE_FASTPATH_RX) {
2428                         fp->rxq = kcalloc(1, sizeof(*fp->rxq), GFP_KERNEL);
2429                         if (!fp->rxq) {
2430                                 DP_NOTICE(edev,
2431                                           "RXQ struct allocation failed\n");
2432                                 goto err;
2433                         }
2434                 }
2435         }
2436
2437         return 0;
2438 err:
2439         qede_free_fp_array(edev);
2440         return -ENOMEM;
2441 }
2442
2443 static void qede_sp_task(struct work_struct *work)
2444 {
2445         struct qede_dev *edev = container_of(work, struct qede_dev,
2446                                              sp_task.work);
2447         struct qed_dev *cdev = edev->cdev;
2448
2449         mutex_lock(&edev->qede_lock);
2450
2451         if (edev->state == QEDE_STATE_OPEN) {
2452                 if (test_and_clear_bit(QEDE_SP_RX_MODE, &edev->sp_flags))
2453                         qede_config_rx_mode(edev->ndev);
2454         }
2455
2456         if (test_and_clear_bit(QEDE_SP_VXLAN_PORT_CONFIG, &edev->sp_flags)) {
2457                 struct qed_tunn_params tunn_params;
2458
2459                 memset(&tunn_params, 0, sizeof(tunn_params));
2460                 tunn_params.update_vxlan_port = 1;
2461                 tunn_params.vxlan_port = edev->vxlan_dst_port;
2462                 qed_ops->tunn_config(cdev, &tunn_params);
2463         }
2464
2465         if (test_and_clear_bit(QEDE_SP_GENEVE_PORT_CONFIG, &edev->sp_flags)) {
2466                 struct qed_tunn_params tunn_params;
2467
2468                 memset(&tunn_params, 0, sizeof(tunn_params));
2469                 tunn_params.update_geneve_port = 1;
2470                 tunn_params.geneve_port = edev->geneve_dst_port;
2471                 qed_ops->tunn_config(cdev, &tunn_params);
2472         }
2473
2474         mutex_unlock(&edev->qede_lock);
2475 }
2476
2477 static void qede_update_pf_params(struct qed_dev *cdev)
2478 {
2479         struct qed_pf_params pf_params;
2480
2481         /* 64 rx + 64 tx */
2482         memset(&pf_params, 0, sizeof(struct qed_pf_params));
2483         pf_params.eth_pf_params.num_cons = 128;
2484         qed_ops->common->update_pf_params(cdev, &pf_params);
2485 }
2486
2487 enum qede_probe_mode {
2488         QEDE_PROBE_NORMAL,
2489 };
2490
2491 static int __qede_probe(struct pci_dev *pdev, u32 dp_module, u8 dp_level,
2492                         bool is_vf, enum qede_probe_mode mode)
2493 {
2494         struct qed_probe_params probe_params;
2495         struct qed_slowpath_params sp_params;
2496         struct qed_dev_eth_info dev_info;
2497         struct qede_dev *edev;
2498         struct qed_dev *cdev;
2499         int rc;
2500
2501         if (unlikely(dp_level & QED_LEVEL_INFO))
2502                 pr_notice("Starting qede probe\n");
2503
2504         memset(&probe_params, 0, sizeof(probe_params));
2505         probe_params.protocol = QED_PROTOCOL_ETH;
2506         probe_params.dp_module = dp_module;
2507         probe_params.dp_level = dp_level;
2508         probe_params.is_vf = is_vf;
2509         cdev = qed_ops->common->probe(pdev, &probe_params);
2510         if (!cdev) {
2511                 rc = -ENODEV;
2512                 goto err0;
2513         }
2514
2515         qede_update_pf_params(cdev);
2516
2517         /* Start the Slowpath-process */
2518         memset(&sp_params, 0, sizeof(sp_params));
2519         sp_params.int_mode = QED_INT_MODE_MSIX;
2520         sp_params.drv_major = QEDE_MAJOR_VERSION;
2521         sp_params.drv_minor = QEDE_MINOR_VERSION;
2522         sp_params.drv_rev = QEDE_REVISION_VERSION;
2523         sp_params.drv_eng = QEDE_ENGINEERING_VERSION;
2524         strlcpy(sp_params.name, "qede LAN", QED_DRV_VER_STR_SIZE);
2525         rc = qed_ops->common->slowpath_start(cdev, &sp_params);
2526         if (rc) {
2527                 pr_notice("Cannot start slowpath\n");
2528                 goto err1;
2529         }
2530
2531         /* Learn information crucial for qede to progress */
2532         rc = qed_ops->fill_dev_info(cdev, &dev_info);
2533         if (rc)
2534                 goto err2;
2535
2536         edev = qede_alloc_etherdev(cdev, pdev, &dev_info, dp_module,
2537                                    dp_level);
2538         if (!edev) {
2539                 rc = -ENOMEM;
2540                 goto err2;
2541         }
2542
2543         if (is_vf)
2544                 edev->flags |= QEDE_FLAG_IS_VF;
2545
2546         qede_init_ndev(edev);
2547
2548         rc = register_netdev(edev->ndev);
2549         if (rc) {
2550                 DP_NOTICE(edev, "Cannot register net-device\n");
2551                 goto err3;
2552         }
2553
2554         edev->ops->common->set_id(cdev, edev->ndev->name, DRV_MODULE_VERSION);
2555
2556         edev->ops->register_ops(cdev, &qede_ll_ops, edev);
2557
2558 #ifdef CONFIG_DCB
2559         if (!IS_VF(edev))
2560                 qede_set_dcbnl_ops(edev->ndev);
2561 #endif
2562
2563         INIT_DELAYED_WORK(&edev->sp_task, qede_sp_task);
2564         mutex_init(&edev->qede_lock);
2565         edev->rx_copybreak = QEDE_RX_HDR_SIZE;
2566
2567         DP_INFO(edev, "Ending successfully qede probe\n");
2568
2569         return 0;
2570
2571 err3:
2572         free_netdev(edev->ndev);
2573 err2:
2574         qed_ops->common->slowpath_stop(cdev);
2575 err1:
2576         qed_ops->common->remove(cdev);
2577 err0:
2578         return rc;
2579 }
2580
2581 static int qede_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2582 {
2583         bool is_vf = false;
2584         u32 dp_module = 0;
2585         u8 dp_level = 0;
2586
2587         switch ((enum qede_pci_private)id->driver_data) {
2588         case QEDE_PRIVATE_VF:
2589                 if (debug & QED_LOG_VERBOSE_MASK)
2590                         dev_err(&pdev->dev, "Probing a VF\n");
2591                 is_vf = true;
2592                 break;
2593         default:
2594                 if (debug & QED_LOG_VERBOSE_MASK)
2595                         dev_err(&pdev->dev, "Probing a PF\n");
2596         }
2597
2598         qede_config_debug(debug, &dp_module, &dp_level);
2599
2600         return __qede_probe(pdev, dp_module, dp_level, is_vf,
2601                             QEDE_PROBE_NORMAL);
2602 }
2603
2604 enum qede_remove_mode {
2605         QEDE_REMOVE_NORMAL,
2606 };
2607
2608 static void __qede_remove(struct pci_dev *pdev, enum qede_remove_mode mode)
2609 {
2610         struct net_device *ndev = pci_get_drvdata(pdev);
2611         struct qede_dev *edev = netdev_priv(ndev);
2612         struct qed_dev *cdev = edev->cdev;
2613
2614         DP_INFO(edev, "Starting qede_remove\n");
2615
2616         cancel_delayed_work_sync(&edev->sp_task);
2617         unregister_netdev(ndev);
2618
2619         edev->ops->common->set_power_state(cdev, PCI_D0);
2620
2621         pci_set_drvdata(pdev, NULL);
2622
2623         free_netdev(ndev);
2624
2625         /* Use global ops since we've freed edev */
2626         qed_ops->common->slowpath_stop(cdev);
2627         qed_ops->common->remove(cdev);
2628
2629         dev_info(&pdev->dev, "Ending qede_remove successfully\n");
2630 }
2631
2632 static void qede_remove(struct pci_dev *pdev)
2633 {
2634         __qede_remove(pdev, QEDE_REMOVE_NORMAL);
2635 }
2636
2637 /* -------------------------------------------------------------------------
2638  * START OF LOAD / UNLOAD
2639  * -------------------------------------------------------------------------
2640  */
2641
2642 static int qede_set_num_queues(struct qede_dev *edev)
2643 {
2644         int rc;
2645         u16 rss_num;
2646
2647         /* Setup queues according to possible resources*/
2648         if (edev->req_queues)
2649                 rss_num = edev->req_queues;
2650         else
2651                 rss_num = netif_get_num_default_rss_queues() *
2652                           edev->dev_info.common.num_hwfns;
2653
2654         rss_num = min_t(u16, QEDE_MAX_RSS_CNT(edev), rss_num);
2655
2656         rc = edev->ops->common->set_fp_int(edev->cdev, rss_num);
2657         if (rc > 0) {
2658                 /* Managed to request interrupts for our queues */
2659                 edev->num_queues = rc;
2660                 DP_INFO(edev, "Managed %d [of %d] RSS queues\n",
2661                         QEDE_QUEUE_CNT(edev), rss_num);
2662                 rc = 0;
2663         }
2664
2665         edev->fp_num_tx = edev->req_num_tx;
2666         edev->fp_num_rx = edev->req_num_rx;
2667
2668         return rc;
2669 }
2670
2671 static void qede_free_mem_sb(struct qede_dev *edev,
2672                              struct qed_sb_info *sb_info)
2673 {
2674         if (sb_info->sb_virt)
2675                 dma_free_coherent(&edev->pdev->dev, sizeof(*sb_info->sb_virt),
2676                                   (void *)sb_info->sb_virt, sb_info->sb_phys);
2677 }
2678
2679 /* This function allocates fast-path status block memory */
2680 static int qede_alloc_mem_sb(struct qede_dev *edev,
2681                              struct qed_sb_info *sb_info, u16 sb_id)
2682 {
2683         struct status_block *sb_virt;
2684         dma_addr_t sb_phys;
2685         int rc;
2686
2687         sb_virt = dma_alloc_coherent(&edev->pdev->dev,
2688                                      sizeof(*sb_virt), &sb_phys, GFP_KERNEL);
2689         if (!sb_virt) {
2690                 DP_ERR(edev, "Status block allocation failed\n");
2691                 return -ENOMEM;
2692         }
2693
2694         rc = edev->ops->common->sb_init(edev->cdev, sb_info,
2695                                         sb_virt, sb_phys, sb_id,
2696                                         QED_SB_TYPE_L2_QUEUE);
2697         if (rc) {
2698                 DP_ERR(edev, "Status block initialization failed\n");
2699                 dma_free_coherent(&edev->pdev->dev, sizeof(*sb_virt),
2700                                   sb_virt, sb_phys);
2701                 return rc;
2702         }
2703
2704         return 0;
2705 }
2706
2707 static void qede_free_rx_buffers(struct qede_dev *edev,
2708                                  struct qede_rx_queue *rxq)
2709 {
2710         u16 i;
2711
2712         for (i = rxq->sw_rx_cons; i != rxq->sw_rx_prod; i++) {
2713                 struct sw_rx_data *rx_buf;
2714                 struct page *data;
2715
2716                 rx_buf = &rxq->sw_rx_ring[i & NUM_RX_BDS_MAX];
2717                 data = rx_buf->data;
2718
2719                 dma_unmap_page(&edev->pdev->dev,
2720                                rx_buf->mapping, PAGE_SIZE, DMA_FROM_DEVICE);
2721
2722                 rx_buf->data = NULL;
2723                 __free_page(data);
2724         }
2725 }
2726
2727 static void qede_free_sge_mem(struct qede_dev *edev, struct qede_rx_queue *rxq)
2728 {
2729         int i;
2730
2731         if (edev->gro_disable)
2732                 return;
2733
2734         for (i = 0; i < ETH_TPA_MAX_AGGS_NUM; i++) {
2735                 struct qede_agg_info *tpa_info = &rxq->tpa_info[i];
2736                 struct sw_rx_data *replace_buf = &tpa_info->replace_buf;
2737
2738                 if (replace_buf->data) {
2739                         dma_unmap_page(&edev->pdev->dev,
2740                                        replace_buf->mapping,
2741                                        PAGE_SIZE, DMA_FROM_DEVICE);
2742                         __free_page(replace_buf->data);
2743                 }
2744         }
2745 }
2746
2747 static void qede_free_mem_rxq(struct qede_dev *edev, struct qede_rx_queue *rxq)
2748 {
2749         qede_free_sge_mem(edev, rxq);
2750
2751         /* Free rx buffers */
2752         qede_free_rx_buffers(edev, rxq);
2753
2754         /* Free the parallel SW ring */
2755         kfree(rxq->sw_rx_ring);
2756
2757         /* Free the real RQ ring used by FW */
2758         edev->ops->common->chain_free(edev->cdev, &rxq->rx_bd_ring);
2759         edev->ops->common->chain_free(edev->cdev, &rxq->rx_comp_ring);
2760 }
2761
2762 static int qede_alloc_rx_buffer(struct qede_dev *edev,
2763                                 struct qede_rx_queue *rxq)
2764 {
2765         struct sw_rx_data *sw_rx_data;
2766         struct eth_rx_bd *rx_bd;
2767         dma_addr_t mapping;
2768         struct page *data;
2769
2770         data = alloc_pages(GFP_ATOMIC, 0);
2771         if (unlikely(!data)) {
2772                 DP_NOTICE(edev, "Failed to allocate Rx data [page]\n");
2773                 return -ENOMEM;
2774         }
2775
2776         /* Map the entire page as it would be used
2777          * for multiple RX buffer segment size mapping.
2778          */
2779         mapping = dma_map_page(&edev->pdev->dev, data, 0,
2780                                PAGE_SIZE, DMA_FROM_DEVICE);
2781         if (unlikely(dma_mapping_error(&edev->pdev->dev, mapping))) {
2782                 __free_page(data);
2783                 DP_NOTICE(edev, "Failed to map Rx buffer\n");
2784                 return -ENOMEM;
2785         }
2786
2787         sw_rx_data = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
2788         sw_rx_data->page_offset = 0;
2789         sw_rx_data->data = data;
2790         sw_rx_data->mapping = mapping;
2791
2792         /* Advance PROD and get BD pointer */
2793         rx_bd = (struct eth_rx_bd *)qed_chain_produce(&rxq->rx_bd_ring);
2794         WARN_ON(!rx_bd);
2795         rx_bd->addr.hi = cpu_to_le32(upper_32_bits(mapping));
2796         rx_bd->addr.lo = cpu_to_le32(lower_32_bits(mapping));
2797
2798         rxq->sw_rx_prod++;
2799
2800         return 0;
2801 }
2802
2803 static int qede_alloc_sge_mem(struct qede_dev *edev, struct qede_rx_queue *rxq)
2804 {
2805         dma_addr_t mapping;
2806         int i;
2807
2808         if (edev->gro_disable)
2809                 return 0;
2810
2811         if (edev->ndev->mtu > PAGE_SIZE) {
2812                 edev->gro_disable = 1;
2813                 return 0;
2814         }
2815
2816         for (i = 0; i < ETH_TPA_MAX_AGGS_NUM; i++) {
2817                 struct qede_agg_info *tpa_info = &rxq->tpa_info[i];
2818                 struct sw_rx_data *replace_buf = &tpa_info->replace_buf;
2819
2820                 replace_buf->data = alloc_pages(GFP_ATOMIC, 0);
2821                 if (unlikely(!replace_buf->data)) {
2822                         DP_NOTICE(edev,
2823                                   "Failed to allocate TPA skb pool [replacement buffer]\n");
2824                         goto err;
2825                 }
2826
2827                 mapping = dma_map_page(&edev->pdev->dev, replace_buf->data, 0,
2828                                        rxq->rx_buf_size, DMA_FROM_DEVICE);
2829                 if (unlikely(dma_mapping_error(&edev->pdev->dev, mapping))) {
2830                         DP_NOTICE(edev,
2831                                   "Failed to map TPA replacement buffer\n");
2832                         goto err;
2833                 }
2834
2835                 replace_buf->mapping = mapping;
2836                 tpa_info->replace_buf.page_offset = 0;
2837
2838                 tpa_info->replace_buf_mapping = mapping;
2839                 tpa_info->agg_state = QEDE_AGG_STATE_NONE;
2840         }
2841
2842         return 0;
2843 err:
2844         qede_free_sge_mem(edev, rxq);
2845         edev->gro_disable = 1;
2846         return -ENOMEM;
2847 }
2848
2849 /* This function allocates all memory needed per Rx queue */
2850 static int qede_alloc_mem_rxq(struct qede_dev *edev, struct qede_rx_queue *rxq)
2851 {
2852         int i, rc, size;
2853
2854         rxq->num_rx_buffers = edev->q_num_rx_buffers;
2855
2856         rxq->rx_buf_size = NET_IP_ALIGN + ETH_OVERHEAD + edev->ndev->mtu;
2857
2858         if (rxq->rx_buf_size > PAGE_SIZE)
2859                 rxq->rx_buf_size = PAGE_SIZE;
2860
2861         /* Segment size to spilt a page in multiple equal parts */
2862         rxq->rx_buf_seg_size = roundup_pow_of_two(rxq->rx_buf_size);
2863
2864         /* Allocate the parallel driver ring for Rx buffers */
2865         size = sizeof(*rxq->sw_rx_ring) * RX_RING_SIZE;
2866         rxq->sw_rx_ring = kzalloc(size, GFP_KERNEL);
2867         if (!rxq->sw_rx_ring) {
2868                 DP_ERR(edev, "Rx buffers ring allocation failed\n");
2869                 rc = -ENOMEM;
2870                 goto err;
2871         }
2872
2873         /* Allocate FW Rx ring  */
2874         rc = edev->ops->common->chain_alloc(edev->cdev,
2875                                             QED_CHAIN_USE_TO_CONSUME_PRODUCE,
2876                                             QED_CHAIN_MODE_NEXT_PTR,
2877                                             QED_CHAIN_CNT_TYPE_U16,
2878                                             RX_RING_SIZE,
2879                                             sizeof(struct eth_rx_bd),
2880                                             &rxq->rx_bd_ring);
2881
2882         if (rc)
2883                 goto err;
2884
2885         /* Allocate FW completion ring */
2886         rc = edev->ops->common->chain_alloc(edev->cdev,
2887                                             QED_CHAIN_USE_TO_CONSUME,
2888                                             QED_CHAIN_MODE_PBL,
2889                                             QED_CHAIN_CNT_TYPE_U16,
2890                                             RX_RING_SIZE,
2891                                             sizeof(union eth_rx_cqe),
2892                                             &rxq->rx_comp_ring);
2893         if (rc)
2894                 goto err;
2895
2896         /* Allocate buffers for the Rx ring */
2897         for (i = 0; i < rxq->num_rx_buffers; i++) {
2898                 rc = qede_alloc_rx_buffer(edev, rxq);
2899                 if (rc) {
2900                         DP_ERR(edev,
2901                                "Rx buffers allocation failed at index %d\n", i);
2902                         goto err;
2903                 }
2904         }
2905
2906         rc = qede_alloc_sge_mem(edev, rxq);
2907 err:
2908         return rc;
2909 }
2910
2911 static void qede_free_mem_txq(struct qede_dev *edev, struct qede_tx_queue *txq)
2912 {
2913         /* Free the parallel SW ring */
2914         kfree(txq->sw_tx_ring);
2915
2916         /* Free the real RQ ring used by FW */
2917         edev->ops->common->chain_free(edev->cdev, &txq->tx_pbl);
2918 }
2919
2920 /* This function allocates all memory needed per Tx queue */
2921 static int qede_alloc_mem_txq(struct qede_dev *edev, struct qede_tx_queue *txq)
2922 {
2923         int size, rc;
2924         union eth_tx_bd_types *p_virt;
2925
2926         txq->num_tx_buffers = edev->q_num_tx_buffers;
2927
2928         /* Allocate the parallel driver ring for Tx buffers */
2929         size = sizeof(*txq->sw_tx_ring) * NUM_TX_BDS_MAX;
2930         txq->sw_tx_ring = kzalloc(size, GFP_KERNEL);
2931         if (!txq->sw_tx_ring) {
2932                 DP_NOTICE(edev, "Tx buffers ring allocation failed\n");
2933                 goto err;
2934         }
2935
2936         rc = edev->ops->common->chain_alloc(edev->cdev,
2937                                             QED_CHAIN_USE_TO_CONSUME_PRODUCE,
2938                                             QED_CHAIN_MODE_PBL,
2939                                             QED_CHAIN_CNT_TYPE_U16,
2940                                             NUM_TX_BDS_MAX,
2941                                             sizeof(*p_virt), &txq->tx_pbl);
2942         if (rc)
2943                 goto err;
2944
2945         return 0;
2946
2947 err:
2948         qede_free_mem_txq(edev, txq);
2949         return -ENOMEM;
2950 }
2951
2952 /* This function frees all memory of a single fp */
2953 static void qede_free_mem_fp(struct qede_dev *edev, struct qede_fastpath *fp)
2954 {
2955         int tc;
2956
2957         qede_free_mem_sb(edev, fp->sb_info);
2958
2959         if (fp->type & QEDE_FASTPATH_RX)
2960                 qede_free_mem_rxq(edev, fp->rxq);
2961
2962         if (fp->type & QEDE_FASTPATH_TX)
2963                 for (tc = 0; tc < edev->num_tc; tc++)
2964                         qede_free_mem_txq(edev, &fp->txqs[tc]);
2965 }
2966
2967 /* This function allocates all memory needed for a single fp (i.e. an entity
2968  * which contains status block, one rx queue and/or multiple per-TC tx queues.
2969  */
2970 static int qede_alloc_mem_fp(struct qede_dev *edev, struct qede_fastpath *fp)
2971 {
2972         int rc, tc;
2973
2974         rc = qede_alloc_mem_sb(edev, fp->sb_info, fp->id);
2975         if (rc)
2976                 goto err;
2977
2978         if (fp->type & QEDE_FASTPATH_RX) {
2979                 rc = qede_alloc_mem_rxq(edev, fp->rxq);
2980                 if (rc)
2981                         goto err;
2982         }
2983
2984         if (fp->type & QEDE_FASTPATH_TX) {
2985                 for (tc = 0; tc < edev->num_tc; tc++) {
2986                         rc = qede_alloc_mem_txq(edev, &fp->txqs[tc]);
2987                         if (rc)
2988                                 goto err;
2989                 }
2990         }
2991
2992         return 0;
2993 err:
2994         return rc;
2995 }
2996
2997 static void qede_free_mem_load(struct qede_dev *edev)
2998 {
2999         int i;
3000
3001         for_each_queue(i) {
3002                 struct qede_fastpath *fp = &edev->fp_array[i];
3003
3004                 qede_free_mem_fp(edev, fp);
3005         }
3006 }
3007
3008 /* This function allocates all qede memory at NIC load. */
3009 static int qede_alloc_mem_load(struct qede_dev *edev)
3010 {
3011         int rc = 0, queue_id;
3012
3013         for (queue_id = 0; queue_id < QEDE_QUEUE_CNT(edev); queue_id++) {
3014                 struct qede_fastpath *fp = &edev->fp_array[queue_id];
3015
3016                 rc = qede_alloc_mem_fp(edev, fp);
3017                 if (rc) {
3018                         DP_ERR(edev,
3019                                "Failed to allocate memory for fastpath - rss id = %d\n",
3020                                queue_id);
3021                         qede_free_mem_load(edev);
3022                         return rc;
3023                 }
3024         }
3025
3026         return 0;
3027 }
3028
3029 /* This function inits fp content and resets the SB, RXQ and TXQ structures */
3030 static void qede_init_fp(struct qede_dev *edev)
3031 {
3032         int queue_id, rxq_index = 0, txq_index = 0, tc;
3033         struct qede_fastpath *fp;
3034
3035         for_each_queue(queue_id) {
3036                 fp = &edev->fp_array[queue_id];
3037
3038                 fp->edev = edev;
3039                 fp->id = queue_id;
3040
3041                 memset((void *)&fp->napi, 0, sizeof(fp->napi));
3042
3043                 memset((void *)fp->sb_info, 0, sizeof(*fp->sb_info));
3044
3045                 if (fp->type & QEDE_FASTPATH_RX) {
3046                         memset((void *)fp->rxq, 0, sizeof(*fp->rxq));
3047                         fp->rxq->rxq_id = rxq_index++;
3048                 }
3049
3050                 if (fp->type & QEDE_FASTPATH_TX) {
3051                         memset((void *)fp->txqs, 0,
3052                                (edev->num_tc * sizeof(*fp->txqs)));
3053                         for (tc = 0; tc < edev->num_tc; tc++) {
3054                                 fp->txqs[tc].index = txq_index +
3055                                     tc * QEDE_TSS_COUNT(edev);
3056                                 if (edev->dev_info.is_legacy)
3057                                         fp->txqs[tc].is_legacy = true;
3058                         }
3059                         txq_index++;
3060                 }
3061
3062                 snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
3063                          edev->ndev->name, queue_id);
3064         }
3065
3066         edev->gro_disable = !(edev->ndev->features & NETIF_F_GRO);
3067 }
3068
3069 static int qede_set_real_num_queues(struct qede_dev *edev)
3070 {
3071         int rc = 0;
3072
3073         rc = netif_set_real_num_tx_queues(edev->ndev, QEDE_TSS_COUNT(edev));
3074         if (rc) {
3075                 DP_NOTICE(edev, "Failed to set real number of Tx queues\n");
3076                 return rc;
3077         }
3078
3079         rc = netif_set_real_num_rx_queues(edev->ndev, QEDE_RSS_COUNT(edev));
3080         if (rc) {
3081                 DP_NOTICE(edev, "Failed to set real number of Rx queues\n");
3082                 return rc;
3083         }
3084
3085         return 0;
3086 }
3087
3088 static void qede_napi_disable_remove(struct qede_dev *edev)
3089 {
3090         int i;
3091
3092         for_each_queue(i) {
3093                 napi_disable(&edev->fp_array[i].napi);
3094
3095                 netif_napi_del(&edev->fp_array[i].napi);
3096         }
3097 }
3098
3099 static void qede_napi_add_enable(struct qede_dev *edev)
3100 {
3101         int i;
3102
3103         /* Add NAPI objects */
3104         for_each_queue(i) {
3105                 netif_napi_add(edev->ndev, &edev->fp_array[i].napi,
3106                                qede_poll, NAPI_POLL_WEIGHT);
3107                 napi_enable(&edev->fp_array[i].napi);
3108         }
3109 }
3110
3111 static void qede_sync_free_irqs(struct qede_dev *edev)
3112 {
3113         int i;
3114
3115         for (i = 0; i < edev->int_info.used_cnt; i++) {
3116                 if (edev->int_info.msix_cnt) {
3117                         synchronize_irq(edev->int_info.msix[i].vector);
3118                         free_irq(edev->int_info.msix[i].vector,
3119                                  &edev->fp_array[i]);
3120                 } else {
3121                         edev->ops->common->simd_handler_clean(edev->cdev, i);
3122                 }
3123         }
3124
3125         edev->int_info.used_cnt = 0;
3126 }
3127
3128 static int qede_req_msix_irqs(struct qede_dev *edev)
3129 {
3130         int i, rc;
3131
3132         /* Sanitize number of interrupts == number of prepared RSS queues */
3133         if (QEDE_QUEUE_CNT(edev) > edev->int_info.msix_cnt) {
3134                 DP_ERR(edev,
3135                        "Interrupt mismatch: %d RSS queues > %d MSI-x vectors\n",
3136                        QEDE_QUEUE_CNT(edev), edev->int_info.msix_cnt);
3137                 return -EINVAL;
3138         }
3139
3140         for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
3141                 rc = request_irq(edev->int_info.msix[i].vector,
3142                                  qede_msix_fp_int, 0, edev->fp_array[i].name,
3143                                  &edev->fp_array[i]);
3144                 if (rc) {
3145                         DP_ERR(edev, "Request fp %d irq failed\n", i);
3146                         qede_sync_free_irqs(edev);
3147                         return rc;
3148                 }
3149                 DP_VERBOSE(edev, NETIF_MSG_INTR,
3150                            "Requested fp irq for %s [entry %d]. Cookie is at %p\n",
3151                            edev->fp_array[i].name, i,
3152                            &edev->fp_array[i]);
3153                 edev->int_info.used_cnt++;
3154         }
3155
3156         return 0;
3157 }
3158
3159 static void qede_simd_fp_handler(void *cookie)
3160 {
3161         struct qede_fastpath *fp = (struct qede_fastpath *)cookie;
3162
3163         napi_schedule_irqoff(&fp->napi);
3164 }
3165
3166 static int qede_setup_irqs(struct qede_dev *edev)
3167 {
3168         int i, rc = 0;
3169
3170         /* Learn Interrupt configuration */
3171         rc = edev->ops->common->get_fp_int(edev->cdev, &edev->int_info);
3172         if (rc)
3173                 return rc;
3174
3175         if (edev->int_info.msix_cnt) {
3176                 rc = qede_req_msix_irqs(edev);
3177                 if (rc)
3178                         return rc;
3179                 edev->ndev->irq = edev->int_info.msix[0].vector;
3180         } else {
3181                 const struct qed_common_ops *ops;
3182
3183                 /* qed should learn receive the RSS ids and callbacks */
3184                 ops = edev->ops->common;
3185                 for (i = 0; i < QEDE_QUEUE_CNT(edev); i++)
3186                         ops->simd_handler_config(edev->cdev,
3187                                                  &edev->fp_array[i], i,
3188                                                  qede_simd_fp_handler);
3189                 edev->int_info.used_cnt = QEDE_QUEUE_CNT(edev);
3190         }
3191         return 0;
3192 }
3193
3194 static int qede_drain_txq(struct qede_dev *edev,
3195                           struct qede_tx_queue *txq, bool allow_drain)
3196 {
3197         int rc, cnt = 1000;
3198
3199         while (txq->sw_tx_cons != txq->sw_tx_prod) {
3200                 if (!cnt) {
3201                         if (allow_drain) {
3202                                 DP_NOTICE(edev,
3203                                           "Tx queue[%d] is stuck, requesting MCP to drain\n",
3204                                           txq->index);
3205                                 rc = edev->ops->common->drain(edev->cdev);
3206                                 if (rc)
3207                                         return rc;
3208                                 return qede_drain_txq(edev, txq, false);
3209                         }
3210                         DP_NOTICE(edev,
3211                                   "Timeout waiting for tx queue[%d]: PROD=%d, CONS=%d\n",
3212                                   txq->index, txq->sw_tx_prod,
3213                                   txq->sw_tx_cons);
3214                         return -ENODEV;
3215                 }
3216                 cnt--;
3217                 usleep_range(1000, 2000);
3218                 barrier();
3219         }
3220
3221         /* FW finished processing, wait for HW to transmit all tx packets */
3222         usleep_range(1000, 2000);
3223
3224         return 0;
3225 }
3226
3227 static int qede_stop_queues(struct qede_dev *edev)
3228 {
3229         struct qed_update_vport_params vport_update_params;
3230         struct qed_dev *cdev = edev->cdev;
3231         int rc, tc, i;
3232
3233         /* Disable the vport */
3234         memset(&vport_update_params, 0, sizeof(vport_update_params));
3235         vport_update_params.vport_id = 0;
3236         vport_update_params.update_vport_active_flg = 1;
3237         vport_update_params.vport_active_flg = 0;
3238         vport_update_params.update_rss_flg = 0;
3239
3240         rc = edev->ops->vport_update(cdev, &vport_update_params);
3241         if (rc) {
3242                 DP_ERR(edev, "Failed to update vport\n");
3243                 return rc;
3244         }
3245
3246         /* Flush Tx queues. If needed, request drain from MCP */
3247         for_each_queue(i) {
3248                 struct qede_fastpath *fp = &edev->fp_array[i];
3249
3250                 if (fp->type & QEDE_FASTPATH_TX) {
3251                         for (tc = 0; tc < edev->num_tc; tc++) {
3252                                 struct qede_tx_queue *txq = &fp->txqs[tc];
3253
3254                                 rc = qede_drain_txq(edev, txq, true);
3255                                 if (rc)
3256                                         return rc;
3257                         }
3258                 }
3259         }
3260
3261         /* Stop all Queues in reverse order */
3262         for (i = QEDE_QUEUE_CNT(edev) - 1; i >= 0; i--) {
3263                 struct qed_stop_rxq_params rx_params;
3264
3265                 /* Stop the Tx Queue(s) */
3266                 if (edev->fp_array[i].type & QEDE_FASTPATH_TX) {
3267                         for (tc = 0; tc < edev->num_tc; tc++) {
3268                                 struct qed_stop_txq_params tx_params;
3269                                 u8 val;
3270
3271                                 tx_params.rss_id = i;
3272                                 val = edev->fp_array[i].txqs[tc].index;
3273                                 tx_params.tx_queue_id = val;
3274                                 rc = edev->ops->q_tx_stop(cdev, &tx_params);
3275                                 if (rc) {
3276                                         DP_ERR(edev, "Failed to stop TXQ #%d\n",
3277                                                tx_params.tx_queue_id);
3278                                         return rc;
3279                                 }
3280                         }
3281                 }
3282
3283                 /* Stop the Rx Queue */
3284                 if (edev->fp_array[i].type & QEDE_FASTPATH_RX) {
3285                         memset(&rx_params, 0, sizeof(rx_params));
3286                         rx_params.rss_id = i;
3287                         rx_params.rx_queue_id = edev->fp_array[i].rxq->rxq_id;
3288
3289                         rc = edev->ops->q_rx_stop(cdev, &rx_params);
3290                         if (rc) {
3291                                 DP_ERR(edev, "Failed to stop RXQ #%d\n", i);
3292                                 return rc;
3293                         }
3294                 }
3295         }
3296
3297         /* Stop the vport */
3298         rc = edev->ops->vport_stop(cdev, 0);
3299         if (rc)
3300                 DP_ERR(edev, "Failed to stop VPORT\n");
3301
3302         return rc;
3303 }
3304
3305 static int qede_start_queues(struct qede_dev *edev, bool clear_stats)
3306 {
3307         int rc, tc, i;
3308         int vlan_removal_en = 1;
3309         struct qed_dev *cdev = edev->cdev;
3310         struct qed_update_vport_params vport_update_params;
3311         struct qed_queue_start_common_params q_params;
3312         struct qed_dev_info *qed_info = &edev->dev_info.common;
3313         struct qed_start_vport_params start = {0};
3314         bool reset_rss_indir = false;
3315
3316         if (!edev->num_queues) {
3317                 DP_ERR(edev,
3318                        "Cannot update V-VPORT as active as there are no Rx queues\n");
3319                 return -EINVAL;
3320         }
3321
3322         start.gro_enable = !edev->gro_disable;
3323         start.mtu = edev->ndev->mtu;
3324         start.vport_id = 0;
3325         start.drop_ttl0 = true;
3326         start.remove_inner_vlan = vlan_removal_en;
3327         start.clear_stats = clear_stats;
3328
3329         rc = edev->ops->vport_start(cdev, &start);
3330
3331         if (rc) {
3332                 DP_ERR(edev, "Start V-PORT failed %d\n", rc);
3333                 return rc;
3334         }
3335
3336         DP_VERBOSE(edev, NETIF_MSG_IFUP,
3337                    "Start vport ramrod passed, vport_id = %d, MTU = %d, vlan_removal_en = %d\n",
3338                    start.vport_id, edev->ndev->mtu + 0xe, vlan_removal_en);
3339
3340         for_each_queue(i) {
3341                 struct qede_fastpath *fp = &edev->fp_array[i];
3342                 dma_addr_t p_phys_table;
3343                 u32 page_cnt;
3344
3345                 if (fp->type & QEDE_FASTPATH_RX) {
3346                         struct qede_rx_queue *rxq = fp->rxq;
3347                         __le16 *val;
3348
3349                         memset(&q_params, 0, sizeof(q_params));
3350                         q_params.rss_id = i;
3351                         q_params.queue_id = rxq->rxq_id;
3352                         q_params.vport_id = 0;
3353                         q_params.sb = fp->sb_info->igu_sb_id;
3354                         q_params.sb_idx = RX_PI;
3355
3356                         p_phys_table =
3357                             qed_chain_get_pbl_phys(&rxq->rx_comp_ring);
3358                         page_cnt = qed_chain_get_page_cnt(&rxq->rx_comp_ring);
3359
3360                         rc = edev->ops->q_rx_start(cdev, &q_params,
3361                                                    rxq->rx_buf_size,
3362                                                    rxq->rx_bd_ring.p_phys_addr,
3363                                                    p_phys_table,
3364                                                    page_cnt,
3365                                                    &rxq->hw_rxq_prod_addr);
3366                         if (rc) {
3367                                 DP_ERR(edev, "Start RXQ #%d failed %d\n", i,
3368                                        rc);
3369                                 return rc;
3370                         }
3371
3372                         val = &fp->sb_info->sb_virt->pi_array[RX_PI];
3373                         rxq->hw_cons_ptr = val;
3374
3375                         qede_update_rx_prod(edev, rxq);
3376                 }
3377
3378                 if (!(fp->type & QEDE_FASTPATH_TX))
3379                         continue;
3380
3381                 for (tc = 0; tc < edev->num_tc; tc++) {
3382                         struct qede_tx_queue *txq = &fp->txqs[tc];
3383
3384                         p_phys_table = qed_chain_get_pbl_phys(&txq->tx_pbl);
3385                         page_cnt = qed_chain_get_page_cnt(&txq->tx_pbl);
3386
3387                         memset(&q_params, 0, sizeof(q_params));
3388                         q_params.rss_id = i;
3389                         q_params.queue_id = txq->index;
3390                         q_params.vport_id = 0;
3391                         q_params.sb = fp->sb_info->igu_sb_id;
3392                         q_params.sb_idx = TX_PI(tc);
3393
3394                         rc = edev->ops->q_tx_start(cdev, &q_params,
3395                                                    p_phys_table, page_cnt,
3396                                                    &txq->doorbell_addr);
3397                         if (rc) {
3398                                 DP_ERR(edev, "Start TXQ #%d failed %d\n",
3399                                        txq->index, rc);
3400                                 return rc;
3401                         }
3402
3403                         txq->hw_cons_ptr =
3404                                 &fp->sb_info->sb_virt->pi_array[TX_PI(tc)];
3405                         SET_FIELD(txq->tx_db.data.params,
3406                                   ETH_DB_DATA_DEST, DB_DEST_XCM);
3407                         SET_FIELD(txq->tx_db.data.params, ETH_DB_DATA_AGG_CMD,
3408                                   DB_AGG_CMD_SET);
3409                         SET_FIELD(txq->tx_db.data.params,
3410                                   ETH_DB_DATA_AGG_VAL_SEL,
3411                                   DQ_XCM_ETH_TX_BD_PROD_CMD);
3412
3413                         txq->tx_db.data.agg_flags = DQ_XCM_ETH_DQ_CF_CMD;
3414                 }
3415         }
3416
3417         /* Prepare and send the vport enable */
3418         memset(&vport_update_params, 0, sizeof(vport_update_params));
3419         vport_update_params.vport_id = start.vport_id;
3420         vport_update_params.update_vport_active_flg = 1;
3421         vport_update_params.vport_active_flg = 1;
3422
3423         if ((qed_info->mf_mode == QED_MF_NPAR || pci_num_vf(edev->pdev)) &&
3424             qed_info->tx_switching) {
3425                 vport_update_params.update_tx_switching_flg = 1;
3426                 vport_update_params.tx_switching_flg = 1;
3427         }
3428
3429         /* Fill struct with RSS params */
3430         if (QEDE_RSS_COUNT(edev) > 1) {
3431                 vport_update_params.update_rss_flg = 1;
3432
3433                 /* Need to validate current RSS config uses valid entries */
3434                 for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++) {
3435                         if (edev->rss_params.rss_ind_table[i] >=
3436                             QEDE_RSS_COUNT(edev)) {
3437                                 reset_rss_indir = true;
3438                                 break;
3439                         }
3440                 }
3441
3442                 if (!(edev->rss_params_inited & QEDE_RSS_INDIR_INITED) ||
3443                     reset_rss_indir) {
3444                         u16 val;
3445
3446                         for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++) {
3447                                 u16 indir_val;
3448
3449                                 val = QEDE_RSS_COUNT(edev);
3450                                 indir_val = ethtool_rxfh_indir_default(i, val);
3451                                 edev->rss_params.rss_ind_table[i] = indir_val;
3452                         }
3453                         edev->rss_params_inited |= QEDE_RSS_INDIR_INITED;
3454                 }
3455
3456                 if (!(edev->rss_params_inited & QEDE_RSS_KEY_INITED)) {
3457                         netdev_rss_key_fill(edev->rss_params.rss_key,
3458                                             sizeof(edev->rss_params.rss_key));
3459                         edev->rss_params_inited |= QEDE_RSS_KEY_INITED;
3460                 }
3461
3462                 if (!(edev->rss_params_inited & QEDE_RSS_CAPS_INITED)) {
3463                         edev->rss_params.rss_caps = QED_RSS_IPV4 |
3464                                                     QED_RSS_IPV6 |
3465                                                     QED_RSS_IPV4_TCP |
3466                                                     QED_RSS_IPV6_TCP;
3467                         edev->rss_params_inited |= QEDE_RSS_CAPS_INITED;
3468                 }
3469
3470                 memcpy(&vport_update_params.rss_params, &edev->rss_params,
3471                        sizeof(vport_update_params.rss_params));
3472         } else {
3473                 memset(&vport_update_params.rss_params, 0,
3474                        sizeof(vport_update_params.rss_params));
3475         }
3476
3477         rc = edev->ops->vport_update(cdev, &vport_update_params);
3478         if (rc) {
3479                 DP_ERR(edev, "Update V-PORT failed %d\n", rc);
3480                 return rc;
3481         }
3482
3483         return 0;
3484 }
3485
3486 static int qede_set_mcast_rx_mac(struct qede_dev *edev,
3487                                  enum qed_filter_xcast_params_type opcode,
3488                                  unsigned char *mac, int num_macs)
3489 {
3490         struct qed_filter_params filter_cmd;
3491         int i;
3492
3493         memset(&filter_cmd, 0, sizeof(filter_cmd));
3494         filter_cmd.type = QED_FILTER_TYPE_MCAST;
3495         filter_cmd.filter.mcast.type = opcode;
3496         filter_cmd.filter.mcast.num = num_macs;
3497
3498         for (i = 0; i < num_macs; i++, mac += ETH_ALEN)
3499                 ether_addr_copy(filter_cmd.filter.mcast.mac[i], mac);
3500
3501         return edev->ops->filter_config(edev->cdev, &filter_cmd);
3502 }
3503
3504 enum qede_unload_mode {
3505         QEDE_UNLOAD_NORMAL,
3506 };
3507
3508 static void qede_unload(struct qede_dev *edev, enum qede_unload_mode mode)
3509 {
3510         struct qed_link_params link_params;
3511         int rc;
3512
3513         DP_INFO(edev, "Starting qede unload\n");
3514
3515         mutex_lock(&edev->qede_lock);
3516         edev->state = QEDE_STATE_CLOSED;
3517
3518         /* Close OS Tx */
3519         netif_tx_disable(edev->ndev);
3520         netif_carrier_off(edev->ndev);
3521
3522         /* Reset the link */
3523         memset(&link_params, 0, sizeof(link_params));
3524         link_params.link_up = false;
3525         edev->ops->common->set_link(edev->cdev, &link_params);
3526         rc = qede_stop_queues(edev);
3527         if (rc) {
3528                 qede_sync_free_irqs(edev);
3529                 goto out;
3530         }
3531
3532         DP_INFO(edev, "Stopped Queues\n");
3533
3534         qede_vlan_mark_nonconfigured(edev);
3535         edev->ops->fastpath_stop(edev->cdev);
3536
3537         /* Release the interrupts */
3538         qede_sync_free_irqs(edev);
3539         edev->ops->common->set_fp_int(edev->cdev, 0);
3540
3541         qede_napi_disable_remove(edev);
3542
3543         qede_free_mem_load(edev);
3544         qede_free_fp_array(edev);
3545
3546 out:
3547         mutex_unlock(&edev->qede_lock);
3548         DP_INFO(edev, "Ending qede unload\n");
3549 }
3550
3551 enum qede_load_mode {
3552         QEDE_LOAD_NORMAL,
3553         QEDE_LOAD_RELOAD,
3554 };
3555
3556 static int qede_load(struct qede_dev *edev, enum qede_load_mode mode)
3557 {
3558         struct qed_link_params link_params;
3559         struct qed_link_output link_output;
3560         int rc;
3561
3562         DP_INFO(edev, "Starting qede load\n");
3563
3564         rc = qede_set_num_queues(edev);
3565         if (rc)
3566                 goto err0;
3567
3568         rc = qede_alloc_fp_array(edev);
3569         if (rc)
3570                 goto err0;
3571
3572         qede_init_fp(edev);
3573
3574         rc = qede_alloc_mem_load(edev);
3575         if (rc)
3576                 goto err1;
3577         DP_INFO(edev, "Allocated %d RSS queues on %d TC/s\n",
3578                 QEDE_QUEUE_CNT(edev), edev->num_tc);
3579
3580         rc = qede_set_real_num_queues(edev);
3581         if (rc)
3582                 goto err2;
3583
3584         qede_napi_add_enable(edev);
3585         DP_INFO(edev, "Napi added and enabled\n");
3586
3587         rc = qede_setup_irqs(edev);
3588         if (rc)
3589                 goto err3;
3590         DP_INFO(edev, "Setup IRQs succeeded\n");
3591
3592         rc = qede_start_queues(edev, mode != QEDE_LOAD_RELOAD);
3593         if (rc)
3594                 goto err4;
3595         DP_INFO(edev, "Start VPORT, RXQ and TXQ succeeded\n");
3596
3597         /* Add primary mac and set Rx filters */
3598         ether_addr_copy(edev->primary_mac, edev->ndev->dev_addr);
3599
3600         mutex_lock(&edev->qede_lock);
3601         edev->state = QEDE_STATE_OPEN;
3602         mutex_unlock(&edev->qede_lock);
3603
3604         /* Program un-configured VLANs */
3605         qede_configure_vlan_filters(edev);
3606
3607         /* Ask for link-up using current configuration */
3608         memset(&link_params, 0, sizeof(link_params));
3609         link_params.link_up = true;
3610         edev->ops->common->set_link(edev->cdev, &link_params);
3611
3612         /* Query whether link is already-up */
3613         memset(&link_output, 0, sizeof(link_output));
3614         edev->ops->common->get_link(edev->cdev, &link_output);
3615         qede_link_update(edev, &link_output);
3616
3617         DP_INFO(edev, "Ending successfully qede load\n");
3618
3619         return 0;
3620
3621 err4:
3622         qede_sync_free_irqs(edev);
3623         memset(&edev->int_info.msix_cnt, 0, sizeof(struct qed_int_info));
3624 err3:
3625         qede_napi_disable_remove(edev);
3626 err2:
3627         qede_free_mem_load(edev);
3628 err1:
3629         edev->ops->common->set_fp_int(edev->cdev, 0);
3630         qede_free_fp_array(edev);
3631         edev->num_queues = 0;
3632         edev->fp_num_tx = 0;
3633         edev->fp_num_rx = 0;
3634 err0:
3635         return rc;
3636 }
3637
3638 void qede_reload(struct qede_dev *edev,
3639                  void (*func)(struct qede_dev *, union qede_reload_args *),
3640                  union qede_reload_args *args)
3641 {
3642         qede_unload(edev, QEDE_UNLOAD_NORMAL);
3643         /* Call function handler to update parameters
3644          * needed for function load.
3645          */
3646         if (func)
3647                 func(edev, args);
3648
3649         qede_load(edev, QEDE_LOAD_RELOAD);
3650
3651         mutex_lock(&edev->qede_lock);
3652         qede_config_rx_mode(edev->ndev);
3653         mutex_unlock(&edev->qede_lock);
3654 }
3655
3656 /* called with rtnl_lock */
3657 static int qede_open(struct net_device *ndev)
3658 {
3659         struct qede_dev *edev = netdev_priv(ndev);
3660         int rc;
3661
3662         netif_carrier_off(ndev);
3663
3664         edev->ops->common->set_power_state(edev->cdev, PCI_D0);
3665
3666         rc = qede_load(edev, QEDE_LOAD_NORMAL);
3667
3668         if (rc)
3669                 return rc;
3670
3671         udp_tunnel_get_rx_info(ndev);
3672
3673         return 0;
3674 }
3675
3676 static int qede_close(struct net_device *ndev)
3677 {
3678         struct qede_dev *edev = netdev_priv(ndev);
3679
3680         qede_unload(edev, QEDE_UNLOAD_NORMAL);
3681
3682         return 0;
3683 }
3684
3685 static void qede_link_update(void *dev, struct qed_link_output *link)
3686 {
3687         struct qede_dev *edev = dev;
3688
3689         if (!netif_running(edev->ndev)) {
3690                 DP_VERBOSE(edev, NETIF_MSG_LINK, "Interface is not running\n");
3691                 return;
3692         }
3693
3694         if (link->link_up) {
3695                 if (!netif_carrier_ok(edev->ndev)) {
3696                         DP_NOTICE(edev, "Link is up\n");
3697                         netif_tx_start_all_queues(edev->ndev);
3698                         netif_carrier_on(edev->ndev);
3699                 }
3700         } else {
3701                 if (netif_carrier_ok(edev->ndev)) {
3702                         DP_NOTICE(edev, "Link is down\n");
3703                         netif_tx_disable(edev->ndev);
3704                         netif_carrier_off(edev->ndev);
3705                 }
3706         }
3707 }
3708
3709 static int qede_set_mac_addr(struct net_device *ndev, void *p)
3710 {
3711         struct qede_dev *edev = netdev_priv(ndev);
3712         struct sockaddr *addr = p;
3713         int rc;
3714
3715         ASSERT_RTNL(); /* @@@TBD To be removed */
3716
3717         DP_INFO(edev, "Set_mac_addr called\n");
3718
3719         if (!is_valid_ether_addr(addr->sa_data)) {
3720                 DP_NOTICE(edev, "The MAC address is not valid\n");
3721                 return -EFAULT;
3722         }
3723
3724         if (!edev->ops->check_mac(edev->cdev, addr->sa_data)) {
3725                 DP_NOTICE(edev, "qed prevents setting MAC\n");
3726                 return -EINVAL;
3727         }
3728
3729         ether_addr_copy(ndev->dev_addr, addr->sa_data);
3730
3731         if (!netif_running(ndev))  {
3732                 DP_NOTICE(edev, "The device is currently down\n");
3733                 return 0;
3734         }
3735
3736         /* Remove the previous primary mac */
3737         rc = qede_set_ucast_rx_mac(edev, QED_FILTER_XCAST_TYPE_DEL,
3738                                    edev->primary_mac);
3739         if (rc)
3740                 return rc;
3741
3742         /* Add MAC filter according to the new unicast HW MAC address */
3743         ether_addr_copy(edev->primary_mac, ndev->dev_addr);
3744         return qede_set_ucast_rx_mac(edev, QED_FILTER_XCAST_TYPE_ADD,
3745                                       edev->primary_mac);
3746 }
3747
3748 static int
3749 qede_configure_mcast_filtering(struct net_device *ndev,
3750                                enum qed_filter_rx_mode_type *accept_flags)
3751 {
3752         struct qede_dev *edev = netdev_priv(ndev);
3753         unsigned char *mc_macs, *temp;
3754         struct netdev_hw_addr *ha;
3755         int rc = 0, mc_count;
3756         size_t size;
3757
3758         size = 64 * ETH_ALEN;
3759
3760         mc_macs = kzalloc(size, GFP_KERNEL);
3761         if (!mc_macs) {
3762                 DP_NOTICE(edev,
3763                           "Failed to allocate memory for multicast MACs\n");
3764                 rc = -ENOMEM;
3765                 goto exit;
3766         }
3767
3768         temp = mc_macs;
3769
3770         /* Remove all previously configured MAC filters */
3771         rc = qede_set_mcast_rx_mac(edev, QED_FILTER_XCAST_TYPE_DEL,
3772                                    mc_macs, 1);
3773         if (rc)
3774                 goto exit;
3775
3776         netif_addr_lock_bh(ndev);
3777
3778         mc_count = netdev_mc_count(ndev);
3779         if (mc_count < 64) {
3780                 netdev_for_each_mc_addr(ha, ndev) {
3781                         ether_addr_copy(temp, ha->addr);
3782                         temp += ETH_ALEN;
3783                 }
3784         }
3785
3786         netif_addr_unlock_bh(ndev);
3787
3788         /* Check for all multicast @@@TBD resource allocation */
3789         if ((ndev->flags & IFF_ALLMULTI) ||
3790             (mc_count > 64)) {
3791                 if (*accept_flags == QED_FILTER_RX_MODE_TYPE_REGULAR)
3792                         *accept_flags = QED_FILTER_RX_MODE_TYPE_MULTI_PROMISC;
3793         } else {
3794                 /* Add all multicast MAC filters */
3795                 rc = qede_set_mcast_rx_mac(edev, QED_FILTER_XCAST_TYPE_ADD,
3796                                            mc_macs, mc_count);
3797         }
3798
3799 exit:
3800         kfree(mc_macs);
3801         return rc;
3802 }
3803
3804 static void qede_set_rx_mode(struct net_device *ndev)
3805 {
3806         struct qede_dev *edev = netdev_priv(ndev);
3807
3808         DP_INFO(edev, "qede_set_rx_mode called\n");
3809
3810         if (edev->state != QEDE_STATE_OPEN) {
3811                 DP_INFO(edev,
3812                         "qede_set_rx_mode called while interface is down\n");
3813         } else {
3814                 set_bit(QEDE_SP_RX_MODE, &edev->sp_flags);
3815                 schedule_delayed_work(&edev->sp_task, 0);
3816         }
3817 }
3818
3819 /* Must be called with qede_lock held */
3820 static void qede_config_rx_mode(struct net_device *ndev)
3821 {
3822         enum qed_filter_rx_mode_type accept_flags = QED_FILTER_TYPE_UCAST;
3823         struct qede_dev *edev = netdev_priv(ndev);
3824         struct qed_filter_params rx_mode;
3825         unsigned char *uc_macs, *temp;
3826         struct netdev_hw_addr *ha;
3827         int rc, uc_count;
3828         size_t size;
3829
3830         netif_addr_lock_bh(ndev);
3831
3832         uc_count = netdev_uc_count(ndev);
3833         size = uc_count * ETH_ALEN;
3834
3835         uc_macs = kzalloc(size, GFP_ATOMIC);
3836         if (!uc_macs) {
3837                 DP_NOTICE(edev, "Failed to allocate memory for unicast MACs\n");
3838                 netif_addr_unlock_bh(ndev);
3839                 return;
3840         }
3841
3842         temp = uc_macs;
3843         netdev_for_each_uc_addr(ha, ndev) {
3844                 ether_addr_copy(temp, ha->addr);
3845                 temp += ETH_ALEN;
3846         }
3847
3848         netif_addr_unlock_bh(ndev);
3849
3850         /* Configure the struct for the Rx mode */
3851         memset(&rx_mode, 0, sizeof(struct qed_filter_params));
3852         rx_mode.type = QED_FILTER_TYPE_RX_MODE;
3853
3854         /* Remove all previous unicast secondary macs and multicast macs
3855          * (configrue / leave the primary mac)
3856          */
3857         rc = qede_set_ucast_rx_mac(edev, QED_FILTER_XCAST_TYPE_REPLACE,
3858                                    edev->primary_mac);
3859         if (rc)
3860                 goto out;
3861
3862         /* Check for promiscuous */
3863         if ((ndev->flags & IFF_PROMISC) ||
3864             (uc_count > 15)) { /* @@@TBD resource allocation - 1 */
3865                 accept_flags = QED_FILTER_RX_MODE_TYPE_PROMISC;
3866         } else {
3867                 /* Add MAC filters according to the unicast secondary macs */
3868                 int i;
3869
3870                 temp = uc_macs;
3871                 for (i = 0; i < uc_count; i++) {
3872                         rc = qede_set_ucast_rx_mac(edev,
3873                                                    QED_FILTER_XCAST_TYPE_ADD,
3874                                                    temp);
3875                         if (rc)
3876                                 goto out;
3877
3878                         temp += ETH_ALEN;
3879                 }
3880
3881                 rc = qede_configure_mcast_filtering(ndev, &accept_flags);
3882                 if (rc)
3883                         goto out;
3884         }
3885
3886         /* take care of VLAN mode */
3887         if (ndev->flags & IFF_PROMISC) {
3888                 qede_config_accept_any_vlan(edev, true);
3889         } else if (!edev->non_configured_vlans) {
3890                 /* It's possible that accept_any_vlan mode is set due to a
3891                  * previous setting of IFF_PROMISC. If vlan credits are
3892                  * sufficient, disable accept_any_vlan.
3893                  */
3894                 qede_config_accept_any_vlan(edev, false);
3895         }
3896
3897         rx_mode.filter.accept_flags = accept_flags;
3898         edev->ops->filter_config(edev->cdev, &rx_mode);
3899 out:
3900         kfree(uc_macs);
3901 }