]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/qlogic/qede/qede_main.c
qede: Add driver support for PTP
[karo-tx-linux.git] / drivers / net / ethernet / qlogic / qede / qede_main.c
1 /* QLogic qede NIC Driver
2  * Copyright (c) 2015-2017  QLogic Corporation
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and /or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 #include <linux/module.h>
33 #include <linux/pci.h>
34 #include <linux/version.h>
35 #include <linux/device.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/errno.h>
40 #include <linux/list.h>
41 #include <linux/string.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/interrupt.h>
44 #include <asm/byteorder.h>
45 #include <asm/param.h>
46 #include <linux/io.h>
47 #include <linux/netdev_features.h>
48 #include <linux/udp.h>
49 #include <linux/tcp.h>
50 #include <net/udp_tunnel.h>
51 #include <linux/ip.h>
52 #include <net/ipv6.h>
53 #include <net/tcp.h>
54 #include <linux/if_ether.h>
55 #include <linux/if_vlan.h>
56 #include <linux/pkt_sched.h>
57 #include <linux/ethtool.h>
58 #include <linux/in.h>
59 #include <linux/random.h>
60 #include <net/ip6_checksum.h>
61 #include <linux/bitops.h>
62 #include <linux/vmalloc.h>
63 #include <linux/qed/qede_roce.h>
64 #include "qede.h"
65 #include "qede_ptp.h"
66
67 static char version[] =
68         "QLogic FastLinQ 4xxxx Ethernet Driver qede " DRV_MODULE_VERSION "\n";
69
70 MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx Ethernet Driver");
71 MODULE_LICENSE("GPL");
72 MODULE_VERSION(DRV_MODULE_VERSION);
73
74 static uint debug;
75 module_param(debug, uint, 0);
76 MODULE_PARM_DESC(debug, " Default debug msglevel");
77
78 static const struct qed_eth_ops *qed_ops;
79
80 #define CHIP_NUM_57980S_40              0x1634
81 #define CHIP_NUM_57980S_10              0x1666
82 #define CHIP_NUM_57980S_MF              0x1636
83 #define CHIP_NUM_57980S_100             0x1644
84 #define CHIP_NUM_57980S_50              0x1654
85 #define CHIP_NUM_57980S_25              0x1656
86 #define CHIP_NUM_57980S_IOV             0x1664
87
88 #ifndef PCI_DEVICE_ID_NX2_57980E
89 #define PCI_DEVICE_ID_57980S_40         CHIP_NUM_57980S_40
90 #define PCI_DEVICE_ID_57980S_10         CHIP_NUM_57980S_10
91 #define PCI_DEVICE_ID_57980S_MF         CHIP_NUM_57980S_MF
92 #define PCI_DEVICE_ID_57980S_100        CHIP_NUM_57980S_100
93 #define PCI_DEVICE_ID_57980S_50         CHIP_NUM_57980S_50
94 #define PCI_DEVICE_ID_57980S_25         CHIP_NUM_57980S_25
95 #define PCI_DEVICE_ID_57980S_IOV        CHIP_NUM_57980S_IOV
96 #endif
97
98 enum qede_pci_private {
99         QEDE_PRIVATE_PF,
100         QEDE_PRIVATE_VF
101 };
102
103 static const struct pci_device_id qede_pci_tbl[] = {
104         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_40), QEDE_PRIVATE_PF},
105         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_10), QEDE_PRIVATE_PF},
106         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_MF), QEDE_PRIVATE_PF},
107         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_100), QEDE_PRIVATE_PF},
108         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_50), QEDE_PRIVATE_PF},
109         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_25), QEDE_PRIVATE_PF},
110 #ifdef CONFIG_QED_SRIOV
111         {PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_IOV), QEDE_PRIVATE_VF},
112 #endif
113         { 0 }
114 };
115
116 MODULE_DEVICE_TABLE(pci, qede_pci_tbl);
117
118 static int qede_probe(struct pci_dev *pdev, const struct pci_device_id *id);
119
120 #define TX_TIMEOUT              (5 * HZ)
121
122 /* Utilize last protocol index for XDP */
123 #define XDP_PI  11
124
125 static void qede_remove(struct pci_dev *pdev);
126 static void qede_shutdown(struct pci_dev *pdev);
127 static void qede_link_update(void *dev, struct qed_link_output *link);
128
129 /* The qede lock is used to protect driver state change and driver flows that
130  * are not reentrant.
131  */
132 void __qede_lock(struct qede_dev *edev)
133 {
134         mutex_lock(&edev->qede_lock);
135 }
136
137 void __qede_unlock(struct qede_dev *edev)
138 {
139         mutex_unlock(&edev->qede_lock);
140 }
141
142 #ifdef CONFIG_QED_SRIOV
143 static int qede_set_vf_vlan(struct net_device *ndev, int vf, u16 vlan, u8 qos,
144                             __be16 vlan_proto)
145 {
146         struct qede_dev *edev = netdev_priv(ndev);
147
148         if (vlan > 4095) {
149                 DP_NOTICE(edev, "Illegal vlan value %d\n", vlan);
150                 return -EINVAL;
151         }
152
153         if (vlan_proto != htons(ETH_P_8021Q))
154                 return -EPROTONOSUPPORT;
155
156         DP_VERBOSE(edev, QED_MSG_IOV, "Setting Vlan 0x%04x to VF [%d]\n",
157                    vlan, vf);
158
159         return edev->ops->iov->set_vlan(edev->cdev, vlan, vf);
160 }
161
162 static int qede_set_vf_mac(struct net_device *ndev, int vfidx, u8 *mac)
163 {
164         struct qede_dev *edev = netdev_priv(ndev);
165
166         DP_VERBOSE(edev, QED_MSG_IOV,
167                    "Setting MAC %02x:%02x:%02x:%02x:%02x:%02x to VF [%d]\n",
168                    mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], vfidx);
169
170         if (!is_valid_ether_addr(mac)) {
171                 DP_VERBOSE(edev, QED_MSG_IOV, "MAC address isn't valid\n");
172                 return -EINVAL;
173         }
174
175         return edev->ops->iov->set_mac(edev->cdev, mac, vfidx);
176 }
177
178 static int qede_sriov_configure(struct pci_dev *pdev, int num_vfs_param)
179 {
180         struct qede_dev *edev = netdev_priv(pci_get_drvdata(pdev));
181         struct qed_dev_info *qed_info = &edev->dev_info.common;
182         struct qed_update_vport_params *vport_params;
183         int rc;
184
185         vport_params = vzalloc(sizeof(*vport_params));
186         if (!vport_params)
187                 return -ENOMEM;
188         DP_VERBOSE(edev, QED_MSG_IOV, "Requested %d VFs\n", num_vfs_param);
189
190         rc = edev->ops->iov->configure(edev->cdev, num_vfs_param);
191
192         /* Enable/Disable Tx switching for PF */
193         if ((rc == num_vfs_param) && netif_running(edev->ndev) &&
194             qed_info->mf_mode != QED_MF_NPAR && qed_info->tx_switching) {
195                 vport_params->vport_id = 0;
196                 vport_params->update_tx_switching_flg = 1;
197                 vport_params->tx_switching_flg = num_vfs_param ? 1 : 0;
198                 edev->ops->vport_update(edev->cdev, vport_params);
199         }
200
201         vfree(vport_params);
202         return rc;
203 }
204 #endif
205
206 static struct pci_driver qede_pci_driver = {
207         .name = "qede",
208         .id_table = qede_pci_tbl,
209         .probe = qede_probe,
210         .remove = qede_remove,
211         .shutdown = qede_shutdown,
212 #ifdef CONFIG_QED_SRIOV
213         .sriov_configure = qede_sriov_configure,
214 #endif
215 };
216
217 static struct qed_eth_cb_ops qede_ll_ops = {
218         {
219                 .link_update = qede_link_update,
220         },
221         .force_mac = qede_force_mac,
222 };
223
224 static int qede_netdev_event(struct notifier_block *this, unsigned long event,
225                              void *ptr)
226 {
227         struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
228         struct ethtool_drvinfo drvinfo;
229         struct qede_dev *edev;
230
231         if (event != NETDEV_CHANGENAME && event != NETDEV_CHANGEADDR)
232                 goto done;
233
234         /* Check whether this is a qede device */
235         if (!ndev || !ndev->ethtool_ops || !ndev->ethtool_ops->get_drvinfo)
236                 goto done;
237
238         memset(&drvinfo, 0, sizeof(drvinfo));
239         ndev->ethtool_ops->get_drvinfo(ndev, &drvinfo);
240         if (strcmp(drvinfo.driver, "qede"))
241                 goto done;
242         edev = netdev_priv(ndev);
243
244         switch (event) {
245         case NETDEV_CHANGENAME:
246                 /* Notify qed of the name change */
247                 if (!edev->ops || !edev->ops->common)
248                         goto done;
249                 edev->ops->common->set_id(edev->cdev, edev->ndev->name, "qede");
250                 break;
251         case NETDEV_CHANGEADDR:
252                 edev = netdev_priv(ndev);
253                 qede_roce_event_changeaddr(edev);
254                 break;
255         }
256
257 done:
258         return NOTIFY_DONE;
259 }
260
261 static struct notifier_block qede_netdev_notifier = {
262         .notifier_call = qede_netdev_event,
263 };
264
265 static
266 int __init qede_init(void)
267 {
268         int ret;
269
270         pr_info("qede_init: %s\n", version);
271
272         qed_ops = qed_get_eth_ops();
273         if (!qed_ops) {
274                 pr_notice("Failed to get qed ethtool operations\n");
275                 return -EINVAL;
276         }
277
278         /* Must register notifier before pci ops, since we might miss
279          * interface rename after pci probe and netdev registeration.
280          */
281         ret = register_netdevice_notifier(&qede_netdev_notifier);
282         if (ret) {
283                 pr_notice("Failed to register netdevice_notifier\n");
284                 qed_put_eth_ops();
285                 return -EINVAL;
286         }
287
288         ret = pci_register_driver(&qede_pci_driver);
289         if (ret) {
290                 pr_notice("Failed to register driver\n");
291                 unregister_netdevice_notifier(&qede_netdev_notifier);
292                 qed_put_eth_ops();
293                 return -EINVAL;
294         }
295
296         return 0;
297 }
298
299 static void __exit qede_cleanup(void)
300 {
301         if (debug & QED_LOG_INFO_MASK)
302                 pr_info("qede_cleanup called\n");
303
304         unregister_netdevice_notifier(&qede_netdev_notifier);
305         pci_unregister_driver(&qede_pci_driver);
306         qed_put_eth_ops();
307 }
308
309 module_init(qede_init);
310 module_exit(qede_cleanup);
311
312 static int qede_open(struct net_device *ndev);
313 static int qede_close(struct net_device *ndev);
314
315 void qede_fill_by_demand_stats(struct qede_dev *edev)
316 {
317         struct qed_eth_stats stats;
318
319         edev->ops->get_vport_stats(edev->cdev, &stats);
320         edev->stats.no_buff_discards = stats.no_buff_discards;
321         edev->stats.packet_too_big_discard = stats.packet_too_big_discard;
322         edev->stats.ttl0_discard = stats.ttl0_discard;
323         edev->stats.rx_ucast_bytes = stats.rx_ucast_bytes;
324         edev->stats.rx_mcast_bytes = stats.rx_mcast_bytes;
325         edev->stats.rx_bcast_bytes = stats.rx_bcast_bytes;
326         edev->stats.rx_ucast_pkts = stats.rx_ucast_pkts;
327         edev->stats.rx_mcast_pkts = stats.rx_mcast_pkts;
328         edev->stats.rx_bcast_pkts = stats.rx_bcast_pkts;
329         edev->stats.mftag_filter_discards = stats.mftag_filter_discards;
330         edev->stats.mac_filter_discards = stats.mac_filter_discards;
331
332         edev->stats.tx_ucast_bytes = stats.tx_ucast_bytes;
333         edev->stats.tx_mcast_bytes = stats.tx_mcast_bytes;
334         edev->stats.tx_bcast_bytes = stats.tx_bcast_bytes;
335         edev->stats.tx_ucast_pkts = stats.tx_ucast_pkts;
336         edev->stats.tx_mcast_pkts = stats.tx_mcast_pkts;
337         edev->stats.tx_bcast_pkts = stats.tx_bcast_pkts;
338         edev->stats.tx_err_drop_pkts = stats.tx_err_drop_pkts;
339         edev->stats.coalesced_pkts = stats.tpa_coalesced_pkts;
340         edev->stats.coalesced_events = stats.tpa_coalesced_events;
341         edev->stats.coalesced_aborts_num = stats.tpa_aborts_num;
342         edev->stats.non_coalesced_pkts = stats.tpa_not_coalesced_pkts;
343         edev->stats.coalesced_bytes = stats.tpa_coalesced_bytes;
344
345         edev->stats.rx_64_byte_packets = stats.rx_64_byte_packets;
346         edev->stats.rx_65_to_127_byte_packets = stats.rx_65_to_127_byte_packets;
347         edev->stats.rx_128_to_255_byte_packets =
348                                 stats.rx_128_to_255_byte_packets;
349         edev->stats.rx_256_to_511_byte_packets =
350                                 stats.rx_256_to_511_byte_packets;
351         edev->stats.rx_512_to_1023_byte_packets =
352                                 stats.rx_512_to_1023_byte_packets;
353         edev->stats.rx_1024_to_1518_byte_packets =
354                                 stats.rx_1024_to_1518_byte_packets;
355         edev->stats.rx_1519_to_1522_byte_packets =
356                                 stats.rx_1519_to_1522_byte_packets;
357         edev->stats.rx_1519_to_2047_byte_packets =
358                                 stats.rx_1519_to_2047_byte_packets;
359         edev->stats.rx_2048_to_4095_byte_packets =
360                                 stats.rx_2048_to_4095_byte_packets;
361         edev->stats.rx_4096_to_9216_byte_packets =
362                                 stats.rx_4096_to_9216_byte_packets;
363         edev->stats.rx_9217_to_16383_byte_packets =
364                                 stats.rx_9217_to_16383_byte_packets;
365         edev->stats.rx_crc_errors = stats.rx_crc_errors;
366         edev->stats.rx_mac_crtl_frames = stats.rx_mac_crtl_frames;
367         edev->stats.rx_pause_frames = stats.rx_pause_frames;
368         edev->stats.rx_pfc_frames = stats.rx_pfc_frames;
369         edev->stats.rx_align_errors = stats.rx_align_errors;
370         edev->stats.rx_carrier_errors = stats.rx_carrier_errors;
371         edev->stats.rx_oversize_packets = stats.rx_oversize_packets;
372         edev->stats.rx_jabbers = stats.rx_jabbers;
373         edev->stats.rx_undersize_packets = stats.rx_undersize_packets;
374         edev->stats.rx_fragments = stats.rx_fragments;
375         edev->stats.tx_64_byte_packets = stats.tx_64_byte_packets;
376         edev->stats.tx_65_to_127_byte_packets = stats.tx_65_to_127_byte_packets;
377         edev->stats.tx_128_to_255_byte_packets =
378                                 stats.tx_128_to_255_byte_packets;
379         edev->stats.tx_256_to_511_byte_packets =
380                                 stats.tx_256_to_511_byte_packets;
381         edev->stats.tx_512_to_1023_byte_packets =
382                                 stats.tx_512_to_1023_byte_packets;
383         edev->stats.tx_1024_to_1518_byte_packets =
384                                 stats.tx_1024_to_1518_byte_packets;
385         edev->stats.tx_1519_to_2047_byte_packets =
386                                 stats.tx_1519_to_2047_byte_packets;
387         edev->stats.tx_2048_to_4095_byte_packets =
388                                 stats.tx_2048_to_4095_byte_packets;
389         edev->stats.tx_4096_to_9216_byte_packets =
390                                 stats.tx_4096_to_9216_byte_packets;
391         edev->stats.tx_9217_to_16383_byte_packets =
392                                 stats.tx_9217_to_16383_byte_packets;
393         edev->stats.tx_pause_frames = stats.tx_pause_frames;
394         edev->stats.tx_pfc_frames = stats.tx_pfc_frames;
395         edev->stats.tx_lpi_entry_count = stats.tx_lpi_entry_count;
396         edev->stats.tx_total_collisions = stats.tx_total_collisions;
397         edev->stats.brb_truncates = stats.brb_truncates;
398         edev->stats.brb_discards = stats.brb_discards;
399         edev->stats.tx_mac_ctrl_frames = stats.tx_mac_ctrl_frames;
400 }
401
402 static void qede_get_stats64(struct net_device *dev,
403                              struct rtnl_link_stats64 *stats)
404 {
405         struct qede_dev *edev = netdev_priv(dev);
406
407         qede_fill_by_demand_stats(edev);
408
409         stats->rx_packets = edev->stats.rx_ucast_pkts +
410                             edev->stats.rx_mcast_pkts +
411                             edev->stats.rx_bcast_pkts;
412         stats->tx_packets = edev->stats.tx_ucast_pkts +
413                             edev->stats.tx_mcast_pkts +
414                             edev->stats.tx_bcast_pkts;
415
416         stats->rx_bytes = edev->stats.rx_ucast_bytes +
417                           edev->stats.rx_mcast_bytes +
418                           edev->stats.rx_bcast_bytes;
419
420         stats->tx_bytes = edev->stats.tx_ucast_bytes +
421                           edev->stats.tx_mcast_bytes +
422                           edev->stats.tx_bcast_bytes;
423
424         stats->tx_errors = edev->stats.tx_err_drop_pkts;
425         stats->multicast = edev->stats.rx_mcast_pkts +
426                            edev->stats.rx_bcast_pkts;
427
428         stats->rx_fifo_errors = edev->stats.no_buff_discards;
429
430         stats->collisions = edev->stats.tx_total_collisions;
431         stats->rx_crc_errors = edev->stats.rx_crc_errors;
432         stats->rx_frame_errors = edev->stats.rx_align_errors;
433 }
434
435 #ifdef CONFIG_QED_SRIOV
436 static int qede_get_vf_config(struct net_device *dev, int vfidx,
437                               struct ifla_vf_info *ivi)
438 {
439         struct qede_dev *edev = netdev_priv(dev);
440
441         if (!edev->ops)
442                 return -EINVAL;
443
444         return edev->ops->iov->get_config(edev->cdev, vfidx, ivi);
445 }
446
447 static int qede_set_vf_rate(struct net_device *dev, int vfidx,
448                             int min_tx_rate, int max_tx_rate)
449 {
450         struct qede_dev *edev = netdev_priv(dev);
451
452         return edev->ops->iov->set_rate(edev->cdev, vfidx, min_tx_rate,
453                                         max_tx_rate);
454 }
455
456 static int qede_set_vf_spoofchk(struct net_device *dev, int vfidx, bool val)
457 {
458         struct qede_dev *edev = netdev_priv(dev);
459
460         if (!edev->ops)
461                 return -EINVAL;
462
463         return edev->ops->iov->set_spoof(edev->cdev, vfidx, val);
464 }
465
466 static int qede_set_vf_link_state(struct net_device *dev, int vfidx,
467                                   int link_state)
468 {
469         struct qede_dev *edev = netdev_priv(dev);
470
471         if (!edev->ops)
472                 return -EINVAL;
473
474         return edev->ops->iov->set_link_state(edev->cdev, vfidx, link_state);
475 }
476
477 static int qede_set_vf_trust(struct net_device *dev, int vfidx, bool setting)
478 {
479         struct qede_dev *edev = netdev_priv(dev);
480
481         if (!edev->ops)
482                 return -EINVAL;
483
484         return edev->ops->iov->set_trust(edev->cdev, vfidx, setting);
485 }
486 #endif
487
488 static int qede_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
489 {
490         struct qede_dev *edev = netdev_priv(dev);
491
492         if (!netif_running(dev))
493                 return -EAGAIN;
494
495         switch (cmd) {
496         case SIOCSHWTSTAMP:
497                 return qede_ptp_hw_ts(edev, ifr);
498         default:
499                 DP_VERBOSE(edev, QED_MSG_DEBUG,
500                            "default IOCTL cmd 0x%x\n", cmd);
501                 return -EOPNOTSUPP;
502         }
503
504         return 0;
505 }
506
507 static const struct net_device_ops qede_netdev_ops = {
508         .ndo_open = qede_open,
509         .ndo_stop = qede_close,
510         .ndo_start_xmit = qede_start_xmit,
511         .ndo_set_rx_mode = qede_set_rx_mode,
512         .ndo_set_mac_address = qede_set_mac_addr,
513         .ndo_validate_addr = eth_validate_addr,
514         .ndo_change_mtu = qede_change_mtu,
515         .ndo_do_ioctl = qede_ioctl,
516 #ifdef CONFIG_QED_SRIOV
517         .ndo_set_vf_mac = qede_set_vf_mac,
518         .ndo_set_vf_vlan = qede_set_vf_vlan,
519         .ndo_set_vf_trust = qede_set_vf_trust,
520 #endif
521         .ndo_vlan_rx_add_vid = qede_vlan_rx_add_vid,
522         .ndo_vlan_rx_kill_vid = qede_vlan_rx_kill_vid,
523         .ndo_set_features = qede_set_features,
524         .ndo_get_stats64 = qede_get_stats64,
525 #ifdef CONFIG_QED_SRIOV
526         .ndo_set_vf_link_state = qede_set_vf_link_state,
527         .ndo_set_vf_spoofchk = qede_set_vf_spoofchk,
528         .ndo_get_vf_config = qede_get_vf_config,
529         .ndo_set_vf_rate = qede_set_vf_rate,
530 #endif
531         .ndo_udp_tunnel_add = qede_udp_tunnel_add,
532         .ndo_udp_tunnel_del = qede_udp_tunnel_del,
533         .ndo_features_check = qede_features_check,
534         .ndo_xdp = qede_xdp,
535 };
536
537 /* -------------------------------------------------------------------------
538  * START OF PROBE / REMOVE
539  * -------------------------------------------------------------------------
540  */
541
542 static struct qede_dev *qede_alloc_etherdev(struct qed_dev *cdev,
543                                             struct pci_dev *pdev,
544                                             struct qed_dev_eth_info *info,
545                                             u32 dp_module, u8 dp_level)
546 {
547         struct net_device *ndev;
548         struct qede_dev *edev;
549
550         ndev = alloc_etherdev_mqs(sizeof(*edev),
551                                   info->num_queues, info->num_queues);
552         if (!ndev) {
553                 pr_err("etherdev allocation failed\n");
554                 return NULL;
555         }
556
557         edev = netdev_priv(ndev);
558         edev->ndev = ndev;
559         edev->cdev = cdev;
560         edev->pdev = pdev;
561         edev->dp_module = dp_module;
562         edev->dp_level = dp_level;
563         edev->ops = qed_ops;
564         edev->q_num_rx_buffers = NUM_RX_BDS_DEF;
565         edev->q_num_tx_buffers = NUM_TX_BDS_DEF;
566
567         DP_INFO(edev, "Allocated netdev with %d tx queues and %d rx queues\n",
568                 info->num_queues, info->num_queues);
569
570         SET_NETDEV_DEV(ndev, &pdev->dev);
571
572         memset(&edev->stats, 0, sizeof(edev->stats));
573         memcpy(&edev->dev_info, info, sizeof(*info));
574
575         INIT_LIST_HEAD(&edev->vlan_list);
576
577         return edev;
578 }
579
580 static void qede_init_ndev(struct qede_dev *edev)
581 {
582         struct net_device *ndev = edev->ndev;
583         struct pci_dev *pdev = edev->pdev;
584         u32 hw_features;
585
586         pci_set_drvdata(pdev, ndev);
587
588         ndev->mem_start = edev->dev_info.common.pci_mem_start;
589         ndev->base_addr = ndev->mem_start;
590         ndev->mem_end = edev->dev_info.common.pci_mem_end;
591         ndev->irq = edev->dev_info.common.pci_irq;
592
593         ndev->watchdog_timeo = TX_TIMEOUT;
594
595         ndev->netdev_ops = &qede_netdev_ops;
596
597         qede_set_ethtool_ops(ndev);
598
599         ndev->priv_flags |= IFF_UNICAST_FLT;
600
601         /* user-changeble features */
602         hw_features = NETIF_F_GRO | NETIF_F_SG |
603                       NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
604                       NETIF_F_TSO | NETIF_F_TSO6;
605
606         /* Encap features*/
607         hw_features |= NETIF_F_GSO_GRE | NETIF_F_GSO_UDP_TUNNEL |
608                        NETIF_F_TSO_ECN | NETIF_F_GSO_UDP_TUNNEL_CSUM |
609                        NETIF_F_GSO_GRE_CSUM;
610         ndev->hw_enc_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
611                                 NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO_ECN |
612                                 NETIF_F_TSO6 | NETIF_F_GSO_GRE |
613                                 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_RXCSUM |
614                                 NETIF_F_GSO_UDP_TUNNEL_CSUM |
615                                 NETIF_F_GSO_GRE_CSUM;
616
617         ndev->vlan_features = hw_features | NETIF_F_RXHASH | NETIF_F_RXCSUM |
618                               NETIF_F_HIGHDMA;
619         ndev->features = hw_features | NETIF_F_RXHASH | NETIF_F_RXCSUM |
620                          NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HIGHDMA |
621                          NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_TX;
622
623         ndev->hw_features = hw_features;
624
625         /* MTU range: 46 - 9600 */
626         ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
627         ndev->max_mtu = QEDE_MAX_JUMBO_PACKET_SIZE;
628
629         /* Set network device HW mac */
630         ether_addr_copy(edev->ndev->dev_addr, edev->dev_info.common.hw_mac);
631
632         ndev->mtu = edev->dev_info.common.mtu;
633 }
634
635 /* This function converts from 32b param to two params of level and module
636  * Input 32b decoding:
637  * b31 - enable all NOTICE prints. NOTICE prints are for deviation from the
638  * 'happy' flow, e.g. memory allocation failed.
639  * b30 - enable all INFO prints. INFO prints are for major steps in the flow
640  * and provide important parameters.
641  * b29-b0 - per-module bitmap, where each bit enables VERBOSE prints of that
642  * module. VERBOSE prints are for tracking the specific flow in low level.
643  *
644  * Notice that the level should be that of the lowest required logs.
645  */
646 void qede_config_debug(uint debug, u32 *p_dp_module, u8 *p_dp_level)
647 {
648         *p_dp_level = QED_LEVEL_NOTICE;
649         *p_dp_module = 0;
650
651         if (debug & QED_LOG_VERBOSE_MASK) {
652                 *p_dp_level = QED_LEVEL_VERBOSE;
653                 *p_dp_module = (debug & 0x3FFFFFFF);
654         } else if (debug & QED_LOG_INFO_MASK) {
655                 *p_dp_level = QED_LEVEL_INFO;
656         } else if (debug & QED_LOG_NOTICE_MASK) {
657                 *p_dp_level = QED_LEVEL_NOTICE;
658         }
659 }
660
661 static void qede_free_fp_array(struct qede_dev *edev)
662 {
663         if (edev->fp_array) {
664                 struct qede_fastpath *fp;
665                 int i;
666
667                 for_each_queue(i) {
668                         fp = &edev->fp_array[i];
669
670                         kfree(fp->sb_info);
671                         kfree(fp->rxq);
672                         kfree(fp->xdp_tx);
673                         kfree(fp->txq);
674                 }
675                 kfree(edev->fp_array);
676         }
677
678         edev->num_queues = 0;
679         edev->fp_num_tx = 0;
680         edev->fp_num_rx = 0;
681 }
682
683 static int qede_alloc_fp_array(struct qede_dev *edev)
684 {
685         u8 fp_combined, fp_rx = edev->fp_num_rx;
686         struct qede_fastpath *fp;
687         int i;
688
689         edev->fp_array = kcalloc(QEDE_QUEUE_CNT(edev),
690                                  sizeof(*edev->fp_array), GFP_KERNEL);
691         if (!edev->fp_array) {
692                 DP_NOTICE(edev, "fp array allocation failed\n");
693                 goto err;
694         }
695
696         fp_combined = QEDE_QUEUE_CNT(edev) - fp_rx - edev->fp_num_tx;
697
698         /* Allocate the FP elements for Rx queues followed by combined and then
699          * the Tx. This ordering should be maintained so that the respective
700          * queues (Rx or Tx) will be together in the fastpath array and the
701          * associated ids will be sequential.
702          */
703         for_each_queue(i) {
704                 fp = &edev->fp_array[i];
705
706                 fp->sb_info = kzalloc(sizeof(*fp->sb_info), GFP_KERNEL);
707                 if (!fp->sb_info) {
708                         DP_NOTICE(edev, "sb info struct allocation failed\n");
709                         goto err;
710                 }
711
712                 if (fp_rx) {
713                         fp->type = QEDE_FASTPATH_RX;
714                         fp_rx--;
715                 } else if (fp_combined) {
716                         fp->type = QEDE_FASTPATH_COMBINED;
717                         fp_combined--;
718                 } else {
719                         fp->type = QEDE_FASTPATH_TX;
720                 }
721
722                 if (fp->type & QEDE_FASTPATH_TX) {
723                         fp->txq = kzalloc(sizeof(*fp->txq), GFP_KERNEL);
724                         if (!fp->txq)
725                                 goto err;
726                 }
727
728                 if (fp->type & QEDE_FASTPATH_RX) {
729                         fp->rxq = kzalloc(sizeof(*fp->rxq), GFP_KERNEL);
730                         if (!fp->rxq)
731                                 goto err;
732
733                         if (edev->xdp_prog) {
734                                 fp->xdp_tx = kzalloc(sizeof(*fp->xdp_tx),
735                                                      GFP_KERNEL);
736                                 if (!fp->xdp_tx)
737                                         goto err;
738                                 fp->type |= QEDE_FASTPATH_XDP;
739                         }
740                 }
741         }
742
743         return 0;
744 err:
745         qede_free_fp_array(edev);
746         return -ENOMEM;
747 }
748
749 static void qede_sp_task(struct work_struct *work)
750 {
751         struct qede_dev *edev = container_of(work, struct qede_dev,
752                                              sp_task.work);
753         struct qed_dev *cdev = edev->cdev;
754
755         __qede_lock(edev);
756
757         if (test_and_clear_bit(QEDE_SP_RX_MODE, &edev->sp_flags))
758                 if (edev->state == QEDE_STATE_OPEN)
759                         qede_config_rx_mode(edev->ndev);
760
761         if (test_and_clear_bit(QEDE_SP_VXLAN_PORT_CONFIG, &edev->sp_flags)) {
762                 struct qed_tunn_params tunn_params;
763
764                 memset(&tunn_params, 0, sizeof(tunn_params));
765                 tunn_params.update_vxlan_port = 1;
766                 tunn_params.vxlan_port = edev->vxlan_dst_port;
767                 qed_ops->tunn_config(cdev, &tunn_params);
768         }
769
770         if (test_and_clear_bit(QEDE_SP_GENEVE_PORT_CONFIG, &edev->sp_flags)) {
771                 struct qed_tunn_params tunn_params;
772
773                 memset(&tunn_params, 0, sizeof(tunn_params));
774                 tunn_params.update_geneve_port = 1;
775                 tunn_params.geneve_port = edev->geneve_dst_port;
776                 qed_ops->tunn_config(cdev, &tunn_params);
777         }
778
779         __qede_unlock(edev);
780 }
781
782 static void qede_update_pf_params(struct qed_dev *cdev)
783 {
784         struct qed_pf_params pf_params;
785
786         /* 64 rx + 64 tx + 64 XDP */
787         memset(&pf_params, 0, sizeof(struct qed_pf_params));
788         pf_params.eth_pf_params.num_cons = (MAX_SB_PER_PF_MIMD - 1) * 3;
789         qed_ops->common->update_pf_params(cdev, &pf_params);
790 }
791
792 enum qede_probe_mode {
793         QEDE_PROBE_NORMAL,
794 };
795
796 static int __qede_probe(struct pci_dev *pdev, u32 dp_module, u8 dp_level,
797                         bool is_vf, enum qede_probe_mode mode)
798 {
799         struct qed_probe_params probe_params;
800         struct qed_slowpath_params sp_params;
801         struct qed_dev_eth_info dev_info;
802         struct qede_dev *edev;
803         struct qed_dev *cdev;
804         int rc;
805
806         if (unlikely(dp_level & QED_LEVEL_INFO))
807                 pr_notice("Starting qede probe\n");
808
809         memset(&probe_params, 0, sizeof(probe_params));
810         probe_params.protocol = QED_PROTOCOL_ETH;
811         probe_params.dp_module = dp_module;
812         probe_params.dp_level = dp_level;
813         probe_params.is_vf = is_vf;
814         cdev = qed_ops->common->probe(pdev, &probe_params);
815         if (!cdev) {
816                 rc = -ENODEV;
817                 goto err0;
818         }
819
820         qede_update_pf_params(cdev);
821
822         /* Start the Slowpath-process */
823         memset(&sp_params, 0, sizeof(sp_params));
824         sp_params.int_mode = QED_INT_MODE_MSIX;
825         sp_params.drv_major = QEDE_MAJOR_VERSION;
826         sp_params.drv_minor = QEDE_MINOR_VERSION;
827         sp_params.drv_rev = QEDE_REVISION_VERSION;
828         sp_params.drv_eng = QEDE_ENGINEERING_VERSION;
829         strlcpy(sp_params.name, "qede LAN", QED_DRV_VER_STR_SIZE);
830         rc = qed_ops->common->slowpath_start(cdev, &sp_params);
831         if (rc) {
832                 pr_notice("Cannot start slowpath\n");
833                 goto err1;
834         }
835
836         /* Learn information crucial for qede to progress */
837         rc = qed_ops->fill_dev_info(cdev, &dev_info);
838         if (rc)
839                 goto err2;
840
841         edev = qede_alloc_etherdev(cdev, pdev, &dev_info, dp_module,
842                                    dp_level);
843         if (!edev) {
844                 rc = -ENOMEM;
845                 goto err2;
846         }
847
848         if (is_vf)
849                 edev->flags |= QEDE_FLAG_IS_VF;
850
851         qede_init_ndev(edev);
852
853         rc = qede_roce_dev_add(edev);
854         if (rc)
855                 goto err3;
856
857         rc = register_netdev(edev->ndev);
858         if (rc) {
859                 DP_NOTICE(edev, "Cannot register net-device\n");
860                 goto err4;
861         }
862
863         edev->ops->common->set_id(cdev, edev->ndev->name, DRV_MODULE_VERSION);
864
865         /* PTP not supported on VFs */
866         if (!is_vf) {
867                 rc = qede_ptp_register_phc(edev);
868                 if (rc) {
869                         DP_NOTICE(edev, "Cannot register PHC\n");
870                         goto err5;
871                 }
872         }
873
874         edev->ops->register_ops(cdev, &qede_ll_ops, edev);
875
876 #ifdef CONFIG_DCB
877         if (!IS_VF(edev))
878                 qede_set_dcbnl_ops(edev->ndev);
879 #endif
880
881         INIT_DELAYED_WORK(&edev->sp_task, qede_sp_task);
882         mutex_init(&edev->qede_lock);
883         edev->rx_copybreak = QEDE_RX_HDR_SIZE;
884
885         DP_INFO(edev, "Ending successfully qede probe\n");
886
887         return 0;
888
889 err5:
890         unregister_netdev(edev->ndev);
891 err4:
892         qede_roce_dev_remove(edev);
893 err3:
894         free_netdev(edev->ndev);
895 err2:
896         qed_ops->common->slowpath_stop(cdev);
897 err1:
898         qed_ops->common->remove(cdev);
899 err0:
900         return rc;
901 }
902
903 static int qede_probe(struct pci_dev *pdev, const struct pci_device_id *id)
904 {
905         bool is_vf = false;
906         u32 dp_module = 0;
907         u8 dp_level = 0;
908
909         switch ((enum qede_pci_private)id->driver_data) {
910         case QEDE_PRIVATE_VF:
911                 if (debug & QED_LOG_VERBOSE_MASK)
912                         dev_err(&pdev->dev, "Probing a VF\n");
913                 is_vf = true;
914                 break;
915         default:
916                 if (debug & QED_LOG_VERBOSE_MASK)
917                         dev_err(&pdev->dev, "Probing a PF\n");
918         }
919
920         qede_config_debug(debug, &dp_module, &dp_level);
921
922         return __qede_probe(pdev, dp_module, dp_level, is_vf,
923                             QEDE_PROBE_NORMAL);
924 }
925
926 enum qede_remove_mode {
927         QEDE_REMOVE_NORMAL,
928 };
929
930 static void __qede_remove(struct pci_dev *pdev, enum qede_remove_mode mode)
931 {
932         struct net_device *ndev = pci_get_drvdata(pdev);
933         struct qede_dev *edev = netdev_priv(ndev);
934         struct qed_dev *cdev = edev->cdev;
935
936         DP_INFO(edev, "Starting qede_remove\n");
937
938         cancel_delayed_work_sync(&edev->sp_task);
939
940         unregister_netdev(ndev);
941
942         qede_ptp_remove(edev);
943
944         qede_roce_dev_remove(edev);
945
946         edev->ops->common->set_power_state(cdev, PCI_D0);
947
948         pci_set_drvdata(pdev, NULL);
949
950         /* Release edev's reference to XDP's bpf if such exist */
951         if (edev->xdp_prog)
952                 bpf_prog_put(edev->xdp_prog);
953
954         free_netdev(ndev);
955
956         /* Use global ops since we've freed edev */
957         qed_ops->common->slowpath_stop(cdev);
958         if (system_state == SYSTEM_POWER_OFF)
959                 return;
960         qed_ops->common->remove(cdev);
961
962         dev_info(&pdev->dev, "Ending qede_remove successfully\n");
963 }
964
965 static void qede_remove(struct pci_dev *pdev)
966 {
967         __qede_remove(pdev, QEDE_REMOVE_NORMAL);
968 }
969
970 static void qede_shutdown(struct pci_dev *pdev)
971 {
972         __qede_remove(pdev, QEDE_REMOVE_NORMAL);
973 }
974
975 /* -------------------------------------------------------------------------
976  * START OF LOAD / UNLOAD
977  * -------------------------------------------------------------------------
978  */
979
980 static int qede_set_num_queues(struct qede_dev *edev)
981 {
982         int rc;
983         u16 rss_num;
984
985         /* Setup queues according to possible resources*/
986         if (edev->req_queues)
987                 rss_num = edev->req_queues;
988         else
989                 rss_num = netif_get_num_default_rss_queues() *
990                           edev->dev_info.common.num_hwfns;
991
992         rss_num = min_t(u16, QEDE_MAX_RSS_CNT(edev), rss_num);
993
994         rc = edev->ops->common->set_fp_int(edev->cdev, rss_num);
995         if (rc > 0) {
996                 /* Managed to request interrupts for our queues */
997                 edev->num_queues = rc;
998                 DP_INFO(edev, "Managed %d [of %d] RSS queues\n",
999                         QEDE_QUEUE_CNT(edev), rss_num);
1000                 rc = 0;
1001         }
1002
1003         edev->fp_num_tx = edev->req_num_tx;
1004         edev->fp_num_rx = edev->req_num_rx;
1005
1006         return rc;
1007 }
1008
1009 static void qede_free_mem_sb(struct qede_dev *edev,
1010                              struct qed_sb_info *sb_info)
1011 {
1012         if (sb_info->sb_virt)
1013                 dma_free_coherent(&edev->pdev->dev, sizeof(*sb_info->sb_virt),
1014                                   (void *)sb_info->sb_virt, sb_info->sb_phys);
1015 }
1016
1017 /* This function allocates fast-path status block memory */
1018 static int qede_alloc_mem_sb(struct qede_dev *edev,
1019                              struct qed_sb_info *sb_info, u16 sb_id)
1020 {
1021         struct status_block *sb_virt;
1022         dma_addr_t sb_phys;
1023         int rc;
1024
1025         sb_virt = dma_alloc_coherent(&edev->pdev->dev,
1026                                      sizeof(*sb_virt), &sb_phys, GFP_KERNEL);
1027         if (!sb_virt) {
1028                 DP_ERR(edev, "Status block allocation failed\n");
1029                 return -ENOMEM;
1030         }
1031
1032         rc = edev->ops->common->sb_init(edev->cdev, sb_info,
1033                                         sb_virt, sb_phys, sb_id,
1034                                         QED_SB_TYPE_L2_QUEUE);
1035         if (rc) {
1036                 DP_ERR(edev, "Status block initialization failed\n");
1037                 dma_free_coherent(&edev->pdev->dev, sizeof(*sb_virt),
1038                                   sb_virt, sb_phys);
1039                 return rc;
1040         }
1041
1042         return 0;
1043 }
1044
1045 static void qede_free_rx_buffers(struct qede_dev *edev,
1046                                  struct qede_rx_queue *rxq)
1047 {
1048         u16 i;
1049
1050         for (i = rxq->sw_rx_cons; i != rxq->sw_rx_prod; i++) {
1051                 struct sw_rx_data *rx_buf;
1052                 struct page *data;
1053
1054                 rx_buf = &rxq->sw_rx_ring[i & NUM_RX_BDS_MAX];
1055                 data = rx_buf->data;
1056
1057                 dma_unmap_page(&edev->pdev->dev,
1058                                rx_buf->mapping, PAGE_SIZE, rxq->data_direction);
1059
1060                 rx_buf->data = NULL;
1061                 __free_page(data);
1062         }
1063 }
1064
1065 static void qede_free_sge_mem(struct qede_dev *edev, struct qede_rx_queue *rxq)
1066 {
1067         int i;
1068
1069         if (edev->gro_disable)
1070                 return;
1071
1072         for (i = 0; i < ETH_TPA_MAX_AGGS_NUM; i++) {
1073                 struct qede_agg_info *tpa_info = &rxq->tpa_info[i];
1074                 struct sw_rx_data *replace_buf = &tpa_info->buffer;
1075
1076                 if (replace_buf->data) {
1077                         dma_unmap_page(&edev->pdev->dev,
1078                                        replace_buf->mapping,
1079                                        PAGE_SIZE, DMA_FROM_DEVICE);
1080                         __free_page(replace_buf->data);
1081                 }
1082         }
1083 }
1084
1085 static void qede_free_mem_rxq(struct qede_dev *edev, struct qede_rx_queue *rxq)
1086 {
1087         qede_free_sge_mem(edev, rxq);
1088
1089         /* Free rx buffers */
1090         qede_free_rx_buffers(edev, rxq);
1091
1092         /* Free the parallel SW ring */
1093         kfree(rxq->sw_rx_ring);
1094
1095         /* Free the real RQ ring used by FW */
1096         edev->ops->common->chain_free(edev->cdev, &rxq->rx_bd_ring);
1097         edev->ops->common->chain_free(edev->cdev, &rxq->rx_comp_ring);
1098 }
1099
1100 static int qede_alloc_sge_mem(struct qede_dev *edev, struct qede_rx_queue *rxq)
1101 {
1102         dma_addr_t mapping;
1103         int i;
1104
1105         /* Don't perform FW aggregations in case of XDP */
1106         if (edev->xdp_prog)
1107                 edev->gro_disable = 1;
1108
1109         if (edev->gro_disable)
1110                 return 0;
1111
1112         if (edev->ndev->mtu > PAGE_SIZE) {
1113                 edev->gro_disable = 1;
1114                 return 0;
1115         }
1116
1117         for (i = 0; i < ETH_TPA_MAX_AGGS_NUM; i++) {
1118                 struct qede_agg_info *tpa_info = &rxq->tpa_info[i];
1119                 struct sw_rx_data *replace_buf = &tpa_info->buffer;
1120
1121                 replace_buf->data = alloc_pages(GFP_ATOMIC, 0);
1122                 if (unlikely(!replace_buf->data)) {
1123                         DP_NOTICE(edev,
1124                                   "Failed to allocate TPA skb pool [replacement buffer]\n");
1125                         goto err;
1126                 }
1127
1128                 mapping = dma_map_page(&edev->pdev->dev, replace_buf->data, 0,
1129                                        PAGE_SIZE, DMA_FROM_DEVICE);
1130                 if (unlikely(dma_mapping_error(&edev->pdev->dev, mapping))) {
1131                         DP_NOTICE(edev,
1132                                   "Failed to map TPA replacement buffer\n");
1133                         goto err;
1134                 }
1135
1136                 replace_buf->mapping = mapping;
1137                 tpa_info->buffer.page_offset = 0;
1138                 tpa_info->buffer_mapping = mapping;
1139                 tpa_info->state = QEDE_AGG_STATE_NONE;
1140         }
1141
1142         return 0;
1143 err:
1144         qede_free_sge_mem(edev, rxq);
1145         edev->gro_disable = 1;
1146         return -ENOMEM;
1147 }
1148
1149 /* This function allocates all memory needed per Rx queue */
1150 static int qede_alloc_mem_rxq(struct qede_dev *edev, struct qede_rx_queue *rxq)
1151 {
1152         int i, rc, size;
1153
1154         rxq->num_rx_buffers = edev->q_num_rx_buffers;
1155
1156         rxq->rx_buf_size = NET_IP_ALIGN + ETH_OVERHEAD + edev->ndev->mtu;
1157
1158         if (rxq->rx_buf_size > PAGE_SIZE)
1159                 rxq->rx_buf_size = PAGE_SIZE;
1160
1161         /* Segment size to spilt a page in multiple equal parts,
1162          * unless XDP is used in which case we'd use the entire page.
1163          */
1164         if (!edev->xdp_prog)
1165                 rxq->rx_buf_seg_size = roundup_pow_of_two(rxq->rx_buf_size);
1166         else
1167                 rxq->rx_buf_seg_size = PAGE_SIZE;
1168
1169         /* Allocate the parallel driver ring for Rx buffers */
1170         size = sizeof(*rxq->sw_rx_ring) * RX_RING_SIZE;
1171         rxq->sw_rx_ring = kzalloc(size, GFP_KERNEL);
1172         if (!rxq->sw_rx_ring) {
1173                 DP_ERR(edev, "Rx buffers ring allocation failed\n");
1174                 rc = -ENOMEM;
1175                 goto err;
1176         }
1177
1178         /* Allocate FW Rx ring  */
1179         rc = edev->ops->common->chain_alloc(edev->cdev,
1180                                             QED_CHAIN_USE_TO_CONSUME_PRODUCE,
1181                                             QED_CHAIN_MODE_NEXT_PTR,
1182                                             QED_CHAIN_CNT_TYPE_U16,
1183                                             RX_RING_SIZE,
1184                                             sizeof(struct eth_rx_bd),
1185                                             &rxq->rx_bd_ring);
1186
1187         if (rc)
1188                 goto err;
1189
1190         /* Allocate FW completion ring */
1191         rc = edev->ops->common->chain_alloc(edev->cdev,
1192                                             QED_CHAIN_USE_TO_CONSUME,
1193                                             QED_CHAIN_MODE_PBL,
1194                                             QED_CHAIN_CNT_TYPE_U16,
1195                                             RX_RING_SIZE,
1196                                             sizeof(union eth_rx_cqe),
1197                                             &rxq->rx_comp_ring);
1198         if (rc)
1199                 goto err;
1200
1201         /* Allocate buffers for the Rx ring */
1202         rxq->filled_buffers = 0;
1203         for (i = 0; i < rxq->num_rx_buffers; i++) {
1204                 rc = qede_alloc_rx_buffer(rxq, false);
1205                 if (rc) {
1206                         DP_ERR(edev,
1207                                "Rx buffers allocation failed at index %d\n", i);
1208                         goto err;
1209                 }
1210         }
1211
1212         rc = qede_alloc_sge_mem(edev, rxq);
1213 err:
1214         return rc;
1215 }
1216
1217 static void qede_free_mem_txq(struct qede_dev *edev, struct qede_tx_queue *txq)
1218 {
1219         /* Free the parallel SW ring */
1220         if (txq->is_xdp)
1221                 kfree(txq->sw_tx_ring.pages);
1222         else
1223                 kfree(txq->sw_tx_ring.skbs);
1224
1225         /* Free the real RQ ring used by FW */
1226         edev->ops->common->chain_free(edev->cdev, &txq->tx_pbl);
1227 }
1228
1229 /* This function allocates all memory needed per Tx queue */
1230 static int qede_alloc_mem_txq(struct qede_dev *edev, struct qede_tx_queue *txq)
1231 {
1232         union eth_tx_bd_types *p_virt;
1233         int size, rc;
1234
1235         txq->num_tx_buffers = edev->q_num_tx_buffers;
1236
1237         /* Allocate the parallel driver ring for Tx buffers */
1238         if (txq->is_xdp) {
1239                 size = sizeof(*txq->sw_tx_ring.pages) * TX_RING_SIZE;
1240                 txq->sw_tx_ring.pages = kzalloc(size, GFP_KERNEL);
1241                 if (!txq->sw_tx_ring.pages)
1242                         goto err;
1243         } else {
1244                 size = sizeof(*txq->sw_tx_ring.skbs) * TX_RING_SIZE;
1245                 txq->sw_tx_ring.skbs = kzalloc(size, GFP_KERNEL);
1246                 if (!txq->sw_tx_ring.skbs)
1247                         goto err;
1248         }
1249
1250         rc = edev->ops->common->chain_alloc(edev->cdev,
1251                                             QED_CHAIN_USE_TO_CONSUME_PRODUCE,
1252                                             QED_CHAIN_MODE_PBL,
1253                                             QED_CHAIN_CNT_TYPE_U16,
1254                                             TX_RING_SIZE,
1255                                             sizeof(*p_virt), &txq->tx_pbl);
1256         if (rc)
1257                 goto err;
1258
1259         return 0;
1260
1261 err:
1262         qede_free_mem_txq(edev, txq);
1263         return -ENOMEM;
1264 }
1265
1266 /* This function frees all memory of a single fp */
1267 static void qede_free_mem_fp(struct qede_dev *edev, struct qede_fastpath *fp)
1268 {
1269         qede_free_mem_sb(edev, fp->sb_info);
1270
1271         if (fp->type & QEDE_FASTPATH_RX)
1272                 qede_free_mem_rxq(edev, fp->rxq);
1273
1274         if (fp->type & QEDE_FASTPATH_TX)
1275                 qede_free_mem_txq(edev, fp->txq);
1276 }
1277
1278 /* This function allocates all memory needed for a single fp (i.e. an entity
1279  * which contains status block, one rx queue and/or multiple per-TC tx queues.
1280  */
1281 static int qede_alloc_mem_fp(struct qede_dev *edev, struct qede_fastpath *fp)
1282 {
1283         int rc = 0;
1284
1285         rc = qede_alloc_mem_sb(edev, fp->sb_info, fp->id);
1286         if (rc)
1287                 goto out;
1288
1289         if (fp->type & QEDE_FASTPATH_RX) {
1290                 rc = qede_alloc_mem_rxq(edev, fp->rxq);
1291                 if (rc)
1292                         goto out;
1293         }
1294
1295         if (fp->type & QEDE_FASTPATH_XDP) {
1296                 rc = qede_alloc_mem_txq(edev, fp->xdp_tx);
1297                 if (rc)
1298                         goto out;
1299         }
1300
1301         if (fp->type & QEDE_FASTPATH_TX) {
1302                 rc = qede_alloc_mem_txq(edev, fp->txq);
1303                 if (rc)
1304                         goto out;
1305         }
1306
1307 out:
1308         return rc;
1309 }
1310
1311 static void qede_free_mem_load(struct qede_dev *edev)
1312 {
1313         int i;
1314
1315         for_each_queue(i) {
1316                 struct qede_fastpath *fp = &edev->fp_array[i];
1317
1318                 qede_free_mem_fp(edev, fp);
1319         }
1320 }
1321
1322 /* This function allocates all qede memory at NIC load. */
1323 static int qede_alloc_mem_load(struct qede_dev *edev)
1324 {
1325         int rc = 0, queue_id;
1326
1327         for (queue_id = 0; queue_id < QEDE_QUEUE_CNT(edev); queue_id++) {
1328                 struct qede_fastpath *fp = &edev->fp_array[queue_id];
1329
1330                 rc = qede_alloc_mem_fp(edev, fp);
1331                 if (rc) {
1332                         DP_ERR(edev,
1333                                "Failed to allocate memory for fastpath - rss id = %d\n",
1334                                queue_id);
1335                         qede_free_mem_load(edev);
1336                         return rc;
1337                 }
1338         }
1339
1340         return 0;
1341 }
1342
1343 /* This function inits fp content and resets the SB, RXQ and TXQ structures */
1344 static void qede_init_fp(struct qede_dev *edev)
1345 {
1346         int queue_id, rxq_index = 0, txq_index = 0;
1347         struct qede_fastpath *fp;
1348
1349         for_each_queue(queue_id) {
1350                 fp = &edev->fp_array[queue_id];
1351
1352                 fp->edev = edev;
1353                 fp->id = queue_id;
1354
1355                 if (fp->type & QEDE_FASTPATH_XDP) {
1356                         fp->xdp_tx->index = QEDE_TXQ_IDX_TO_XDP(edev,
1357                                                                 rxq_index);
1358                         fp->xdp_tx->is_xdp = 1;
1359                 }
1360
1361                 if (fp->type & QEDE_FASTPATH_RX) {
1362                         fp->rxq->rxq_id = rxq_index++;
1363
1364                         /* Determine how to map buffers for this queue */
1365                         if (fp->type & QEDE_FASTPATH_XDP)
1366                                 fp->rxq->data_direction = DMA_BIDIRECTIONAL;
1367                         else
1368                                 fp->rxq->data_direction = DMA_FROM_DEVICE;
1369                         fp->rxq->dev = &edev->pdev->dev;
1370                 }
1371
1372                 if (fp->type & QEDE_FASTPATH_TX) {
1373                         fp->txq->index = txq_index++;
1374                         if (edev->dev_info.is_legacy)
1375                                 fp->txq->is_legacy = 1;
1376                         fp->txq->dev = &edev->pdev->dev;
1377                 }
1378
1379                 snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
1380                          edev->ndev->name, queue_id);
1381         }
1382
1383         edev->gro_disable = !(edev->ndev->features & NETIF_F_GRO);
1384 }
1385
1386 static int qede_set_real_num_queues(struct qede_dev *edev)
1387 {
1388         int rc = 0;
1389
1390         rc = netif_set_real_num_tx_queues(edev->ndev, QEDE_TSS_COUNT(edev));
1391         if (rc) {
1392                 DP_NOTICE(edev, "Failed to set real number of Tx queues\n");
1393                 return rc;
1394         }
1395
1396         rc = netif_set_real_num_rx_queues(edev->ndev, QEDE_RSS_COUNT(edev));
1397         if (rc) {
1398                 DP_NOTICE(edev, "Failed to set real number of Rx queues\n");
1399                 return rc;
1400         }
1401
1402         return 0;
1403 }
1404
1405 static void qede_napi_disable_remove(struct qede_dev *edev)
1406 {
1407         int i;
1408
1409         for_each_queue(i) {
1410                 napi_disable(&edev->fp_array[i].napi);
1411
1412                 netif_napi_del(&edev->fp_array[i].napi);
1413         }
1414 }
1415
1416 static void qede_napi_add_enable(struct qede_dev *edev)
1417 {
1418         int i;
1419
1420         /* Add NAPI objects */
1421         for_each_queue(i) {
1422                 netif_napi_add(edev->ndev, &edev->fp_array[i].napi,
1423                                qede_poll, NAPI_POLL_WEIGHT);
1424                 napi_enable(&edev->fp_array[i].napi);
1425         }
1426 }
1427
1428 static void qede_sync_free_irqs(struct qede_dev *edev)
1429 {
1430         int i;
1431
1432         for (i = 0; i < edev->int_info.used_cnt; i++) {
1433                 if (edev->int_info.msix_cnt) {
1434                         synchronize_irq(edev->int_info.msix[i].vector);
1435                         free_irq(edev->int_info.msix[i].vector,
1436                                  &edev->fp_array[i]);
1437                 } else {
1438                         edev->ops->common->simd_handler_clean(edev->cdev, i);
1439                 }
1440         }
1441
1442         edev->int_info.used_cnt = 0;
1443 }
1444
1445 static int qede_req_msix_irqs(struct qede_dev *edev)
1446 {
1447         int i, rc;
1448
1449         /* Sanitize number of interrupts == number of prepared RSS queues */
1450         if (QEDE_QUEUE_CNT(edev) > edev->int_info.msix_cnt) {
1451                 DP_ERR(edev,
1452                        "Interrupt mismatch: %d RSS queues > %d MSI-x vectors\n",
1453                        QEDE_QUEUE_CNT(edev), edev->int_info.msix_cnt);
1454                 return -EINVAL;
1455         }
1456
1457         for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
1458                 rc = request_irq(edev->int_info.msix[i].vector,
1459                                  qede_msix_fp_int, 0, edev->fp_array[i].name,
1460                                  &edev->fp_array[i]);
1461                 if (rc) {
1462                         DP_ERR(edev, "Request fp %d irq failed\n", i);
1463                         qede_sync_free_irqs(edev);
1464                         return rc;
1465                 }
1466                 DP_VERBOSE(edev, NETIF_MSG_INTR,
1467                            "Requested fp irq for %s [entry %d]. Cookie is at %p\n",
1468                            edev->fp_array[i].name, i,
1469                            &edev->fp_array[i]);
1470                 edev->int_info.used_cnt++;
1471         }
1472
1473         return 0;
1474 }
1475
1476 static void qede_simd_fp_handler(void *cookie)
1477 {
1478         struct qede_fastpath *fp = (struct qede_fastpath *)cookie;
1479
1480         napi_schedule_irqoff(&fp->napi);
1481 }
1482
1483 static int qede_setup_irqs(struct qede_dev *edev)
1484 {
1485         int i, rc = 0;
1486
1487         /* Learn Interrupt configuration */
1488         rc = edev->ops->common->get_fp_int(edev->cdev, &edev->int_info);
1489         if (rc)
1490                 return rc;
1491
1492         if (edev->int_info.msix_cnt) {
1493                 rc = qede_req_msix_irqs(edev);
1494                 if (rc)
1495                         return rc;
1496                 edev->ndev->irq = edev->int_info.msix[0].vector;
1497         } else {
1498                 const struct qed_common_ops *ops;
1499
1500                 /* qed should learn receive the RSS ids and callbacks */
1501                 ops = edev->ops->common;
1502                 for (i = 0; i < QEDE_QUEUE_CNT(edev); i++)
1503                         ops->simd_handler_config(edev->cdev,
1504                                                  &edev->fp_array[i], i,
1505                                                  qede_simd_fp_handler);
1506                 edev->int_info.used_cnt = QEDE_QUEUE_CNT(edev);
1507         }
1508         return 0;
1509 }
1510
1511 static int qede_drain_txq(struct qede_dev *edev,
1512                           struct qede_tx_queue *txq, bool allow_drain)
1513 {
1514         int rc, cnt = 1000;
1515
1516         while (txq->sw_tx_cons != txq->sw_tx_prod) {
1517                 if (!cnt) {
1518                         if (allow_drain) {
1519                                 DP_NOTICE(edev,
1520                                           "Tx queue[%d] is stuck, requesting MCP to drain\n",
1521                                           txq->index);
1522                                 rc = edev->ops->common->drain(edev->cdev);
1523                                 if (rc)
1524                                         return rc;
1525                                 return qede_drain_txq(edev, txq, false);
1526                         }
1527                         DP_NOTICE(edev,
1528                                   "Timeout waiting for tx queue[%d]: PROD=%d, CONS=%d\n",
1529                                   txq->index, txq->sw_tx_prod,
1530                                   txq->sw_tx_cons);
1531                         return -ENODEV;
1532                 }
1533                 cnt--;
1534                 usleep_range(1000, 2000);
1535                 barrier();
1536         }
1537
1538         /* FW finished processing, wait for HW to transmit all tx packets */
1539         usleep_range(1000, 2000);
1540
1541         return 0;
1542 }
1543
1544 static int qede_stop_txq(struct qede_dev *edev,
1545                          struct qede_tx_queue *txq, int rss_id)
1546 {
1547         return edev->ops->q_tx_stop(edev->cdev, rss_id, txq->handle);
1548 }
1549
1550 static int qede_stop_queues(struct qede_dev *edev)
1551 {
1552         struct qed_update_vport_params *vport_update_params;
1553         struct qed_dev *cdev = edev->cdev;
1554         struct qede_fastpath *fp;
1555         int rc, i;
1556
1557         /* Disable the vport */
1558         vport_update_params = vzalloc(sizeof(*vport_update_params));
1559         if (!vport_update_params)
1560                 return -ENOMEM;
1561
1562         vport_update_params->vport_id = 0;
1563         vport_update_params->update_vport_active_flg = 1;
1564         vport_update_params->vport_active_flg = 0;
1565         vport_update_params->update_rss_flg = 0;
1566
1567         rc = edev->ops->vport_update(cdev, vport_update_params);
1568         vfree(vport_update_params);
1569
1570         if (rc) {
1571                 DP_ERR(edev, "Failed to update vport\n");
1572                 return rc;
1573         }
1574
1575         /* Flush Tx queues. If needed, request drain from MCP */
1576         for_each_queue(i) {
1577                 fp = &edev->fp_array[i];
1578
1579                 if (fp->type & QEDE_FASTPATH_TX) {
1580                         rc = qede_drain_txq(edev, fp->txq, true);
1581                         if (rc)
1582                                 return rc;
1583                 }
1584
1585                 if (fp->type & QEDE_FASTPATH_XDP) {
1586                         rc = qede_drain_txq(edev, fp->xdp_tx, true);
1587                         if (rc)
1588                                 return rc;
1589                 }
1590         }
1591
1592         /* Stop all Queues in reverse order */
1593         for (i = QEDE_QUEUE_CNT(edev) - 1; i >= 0; i--) {
1594                 fp = &edev->fp_array[i];
1595
1596                 /* Stop the Tx Queue(s) */
1597                 if (fp->type & QEDE_FASTPATH_TX) {
1598                         rc = qede_stop_txq(edev, fp->txq, i);
1599                         if (rc)
1600                                 return rc;
1601                 }
1602
1603                 /* Stop the Rx Queue */
1604                 if (fp->type & QEDE_FASTPATH_RX) {
1605                         rc = edev->ops->q_rx_stop(cdev, i, fp->rxq->handle);
1606                         if (rc) {
1607                                 DP_ERR(edev, "Failed to stop RXQ #%d\n", i);
1608                                 return rc;
1609                         }
1610                 }
1611
1612                 /* Stop the XDP forwarding queue */
1613                 if (fp->type & QEDE_FASTPATH_XDP) {
1614                         rc = qede_stop_txq(edev, fp->xdp_tx, i);
1615                         if (rc)
1616                                 return rc;
1617
1618                         bpf_prog_put(fp->rxq->xdp_prog);
1619                 }
1620         }
1621
1622         /* Stop the vport */
1623         rc = edev->ops->vport_stop(cdev, 0);
1624         if (rc)
1625                 DP_ERR(edev, "Failed to stop VPORT\n");
1626
1627         return rc;
1628 }
1629
1630 static int qede_start_txq(struct qede_dev *edev,
1631                           struct qede_fastpath *fp,
1632                           struct qede_tx_queue *txq, u8 rss_id, u16 sb_idx)
1633 {
1634         dma_addr_t phys_table = qed_chain_get_pbl_phys(&txq->tx_pbl);
1635         u32 page_cnt = qed_chain_get_page_cnt(&txq->tx_pbl);
1636         struct qed_queue_start_common_params params;
1637         struct qed_txq_start_ret_params ret_params;
1638         int rc;
1639
1640         memset(&params, 0, sizeof(params));
1641         memset(&ret_params, 0, sizeof(ret_params));
1642
1643         /* Let the XDP queue share the queue-zone with one of the regular txq.
1644          * We don't really care about its coalescing.
1645          */
1646         if (txq->is_xdp)
1647                 params.queue_id = QEDE_TXQ_XDP_TO_IDX(edev, txq);
1648         else
1649                 params.queue_id = txq->index;
1650
1651         params.sb = fp->sb_info->igu_sb_id;
1652         params.sb_idx = sb_idx;
1653
1654         rc = edev->ops->q_tx_start(edev->cdev, rss_id, &params, phys_table,
1655                                    page_cnt, &ret_params);
1656         if (rc) {
1657                 DP_ERR(edev, "Start TXQ #%d failed %d\n", txq->index, rc);
1658                 return rc;
1659         }
1660
1661         txq->doorbell_addr = ret_params.p_doorbell;
1662         txq->handle = ret_params.p_handle;
1663
1664         /* Determine the FW consumer address associated */
1665         txq->hw_cons_ptr = &fp->sb_info->sb_virt->pi_array[sb_idx];
1666
1667         /* Prepare the doorbell parameters */
1668         SET_FIELD(txq->tx_db.data.params, ETH_DB_DATA_DEST, DB_DEST_XCM);
1669         SET_FIELD(txq->tx_db.data.params, ETH_DB_DATA_AGG_CMD, DB_AGG_CMD_SET);
1670         SET_FIELD(txq->tx_db.data.params, ETH_DB_DATA_AGG_VAL_SEL,
1671                   DQ_XCM_ETH_TX_BD_PROD_CMD);
1672         txq->tx_db.data.agg_flags = DQ_XCM_ETH_DQ_CF_CMD;
1673
1674         return rc;
1675 }
1676
1677 static int qede_start_queues(struct qede_dev *edev, bool clear_stats)
1678 {
1679         int vlan_removal_en = 1;
1680         struct qed_dev *cdev = edev->cdev;
1681         struct qed_dev_info *qed_info = &edev->dev_info.common;
1682         struct qed_update_vport_params *vport_update_params;
1683         struct qed_queue_start_common_params q_params;
1684         struct qed_start_vport_params start = {0};
1685         int rc, i;
1686
1687         if (!edev->num_queues) {
1688                 DP_ERR(edev,
1689                        "Cannot update V-VPORT as active as there are no Rx queues\n");
1690                 return -EINVAL;
1691         }
1692
1693         vport_update_params = vzalloc(sizeof(*vport_update_params));
1694         if (!vport_update_params)
1695                 return -ENOMEM;
1696
1697         start.handle_ptp_pkts = !!(edev->ptp);
1698         start.gro_enable = !edev->gro_disable;
1699         start.mtu = edev->ndev->mtu;
1700         start.vport_id = 0;
1701         start.drop_ttl0 = true;
1702         start.remove_inner_vlan = vlan_removal_en;
1703         start.clear_stats = clear_stats;
1704
1705         rc = edev->ops->vport_start(cdev, &start);
1706
1707         if (rc) {
1708                 DP_ERR(edev, "Start V-PORT failed %d\n", rc);
1709                 goto out;
1710         }
1711
1712         DP_VERBOSE(edev, NETIF_MSG_IFUP,
1713                    "Start vport ramrod passed, vport_id = %d, MTU = %d, vlan_removal_en = %d\n",
1714                    start.vport_id, edev->ndev->mtu + 0xe, vlan_removal_en);
1715
1716         for_each_queue(i) {
1717                 struct qede_fastpath *fp = &edev->fp_array[i];
1718                 dma_addr_t p_phys_table;
1719                 u32 page_cnt;
1720
1721                 if (fp->type & QEDE_FASTPATH_RX) {
1722                         struct qed_rxq_start_ret_params ret_params;
1723                         struct qede_rx_queue *rxq = fp->rxq;
1724                         __le16 *val;
1725
1726                         memset(&ret_params, 0, sizeof(ret_params));
1727                         memset(&q_params, 0, sizeof(q_params));
1728                         q_params.queue_id = rxq->rxq_id;
1729                         q_params.vport_id = 0;
1730                         q_params.sb = fp->sb_info->igu_sb_id;
1731                         q_params.sb_idx = RX_PI;
1732
1733                         p_phys_table =
1734                             qed_chain_get_pbl_phys(&rxq->rx_comp_ring);
1735                         page_cnt = qed_chain_get_page_cnt(&rxq->rx_comp_ring);
1736
1737                         rc = edev->ops->q_rx_start(cdev, i, &q_params,
1738                                                    rxq->rx_buf_size,
1739                                                    rxq->rx_bd_ring.p_phys_addr,
1740                                                    p_phys_table,
1741                                                    page_cnt, &ret_params);
1742                         if (rc) {
1743                                 DP_ERR(edev, "Start RXQ #%d failed %d\n", i,
1744                                        rc);
1745                                 goto out;
1746                         }
1747
1748                         /* Use the return parameters */
1749                         rxq->hw_rxq_prod_addr = ret_params.p_prod;
1750                         rxq->handle = ret_params.p_handle;
1751
1752                         val = &fp->sb_info->sb_virt->pi_array[RX_PI];
1753                         rxq->hw_cons_ptr = val;
1754
1755                         qede_update_rx_prod(edev, rxq);
1756                 }
1757
1758                 if (fp->type & QEDE_FASTPATH_XDP) {
1759                         rc = qede_start_txq(edev, fp, fp->xdp_tx, i, XDP_PI);
1760                         if (rc)
1761                                 goto out;
1762
1763                         fp->rxq->xdp_prog = bpf_prog_add(edev->xdp_prog, 1);
1764                         if (IS_ERR(fp->rxq->xdp_prog)) {
1765                                 rc = PTR_ERR(fp->rxq->xdp_prog);
1766                                 fp->rxq->xdp_prog = NULL;
1767                                 goto out;
1768                         }
1769                 }
1770
1771                 if (fp->type & QEDE_FASTPATH_TX) {
1772                         rc = qede_start_txq(edev, fp, fp->txq, i, TX_PI(0));
1773                         if (rc)
1774                                 goto out;
1775                 }
1776         }
1777
1778         /* Prepare and send the vport enable */
1779         vport_update_params->vport_id = start.vport_id;
1780         vport_update_params->update_vport_active_flg = 1;
1781         vport_update_params->vport_active_flg = 1;
1782
1783         if ((qed_info->mf_mode == QED_MF_NPAR || pci_num_vf(edev->pdev)) &&
1784             qed_info->tx_switching) {
1785                 vport_update_params->update_tx_switching_flg = 1;
1786                 vport_update_params->tx_switching_flg = 1;
1787         }
1788
1789         qede_fill_rss_params(edev, &vport_update_params->rss_params,
1790                              &vport_update_params->update_rss_flg);
1791
1792         rc = edev->ops->vport_update(cdev, vport_update_params);
1793         if (rc)
1794                 DP_ERR(edev, "Update V-PORT failed %d\n", rc);
1795
1796 out:
1797         vfree(vport_update_params);
1798         return rc;
1799 }
1800
1801 enum qede_unload_mode {
1802         QEDE_UNLOAD_NORMAL,
1803 };
1804
1805 static void qede_unload(struct qede_dev *edev, enum qede_unload_mode mode,
1806                         bool is_locked)
1807 {
1808         struct qed_link_params link_params;
1809         int rc;
1810
1811         DP_INFO(edev, "Starting qede unload\n");
1812
1813         if (!is_locked)
1814                 __qede_lock(edev);
1815
1816         qede_roce_dev_event_close(edev);
1817         edev->state = QEDE_STATE_CLOSED;
1818
1819         qede_ptp_stop(edev);
1820
1821         /* Close OS Tx */
1822         netif_tx_disable(edev->ndev);
1823         netif_carrier_off(edev->ndev);
1824
1825         /* Reset the link */
1826         memset(&link_params, 0, sizeof(link_params));
1827         link_params.link_up = false;
1828         edev->ops->common->set_link(edev->cdev, &link_params);
1829         rc = qede_stop_queues(edev);
1830         if (rc) {
1831                 qede_sync_free_irqs(edev);
1832                 goto out;
1833         }
1834
1835         DP_INFO(edev, "Stopped Queues\n");
1836
1837         qede_vlan_mark_nonconfigured(edev);
1838         edev->ops->fastpath_stop(edev->cdev);
1839
1840         /* Release the interrupts */
1841         qede_sync_free_irqs(edev);
1842         edev->ops->common->set_fp_int(edev->cdev, 0);
1843
1844         qede_napi_disable_remove(edev);
1845
1846         qede_free_mem_load(edev);
1847         qede_free_fp_array(edev);
1848
1849 out:
1850         if (!is_locked)
1851                 __qede_unlock(edev);
1852         DP_INFO(edev, "Ending qede unload\n");
1853 }
1854
1855 enum qede_load_mode {
1856         QEDE_LOAD_NORMAL,
1857         QEDE_LOAD_RELOAD,
1858 };
1859
1860 static int qede_load(struct qede_dev *edev, enum qede_load_mode mode,
1861                      bool is_locked)
1862 {
1863         struct qed_link_params link_params;
1864         struct qed_link_output link_output;
1865         int rc;
1866
1867         DP_INFO(edev, "Starting qede load\n");
1868
1869         if (!is_locked)
1870                 __qede_lock(edev);
1871
1872         rc = qede_set_num_queues(edev);
1873         if (rc)
1874                 goto out;
1875
1876         rc = qede_alloc_fp_array(edev);
1877         if (rc)
1878                 goto out;
1879
1880         qede_init_fp(edev);
1881
1882         rc = qede_alloc_mem_load(edev);
1883         if (rc)
1884                 goto err1;
1885         DP_INFO(edev, "Allocated %d Rx, %d Tx queues\n",
1886                 QEDE_RSS_COUNT(edev), QEDE_TSS_COUNT(edev));
1887
1888         rc = qede_set_real_num_queues(edev);
1889         if (rc)
1890                 goto err2;
1891
1892         qede_napi_add_enable(edev);
1893         DP_INFO(edev, "Napi added and enabled\n");
1894
1895         rc = qede_setup_irqs(edev);
1896         if (rc)
1897                 goto err3;
1898         DP_INFO(edev, "Setup IRQs succeeded\n");
1899
1900         rc = qede_start_queues(edev, mode != QEDE_LOAD_RELOAD);
1901         if (rc)
1902                 goto err4;
1903         DP_INFO(edev, "Start VPORT, RXQ and TXQ succeeded\n");
1904
1905         /* Add primary mac and set Rx filters */
1906         ether_addr_copy(edev->primary_mac, edev->ndev->dev_addr);
1907
1908         /* Program un-configured VLANs */
1909         qede_configure_vlan_filters(edev);
1910
1911         /* Ask for link-up using current configuration */
1912         memset(&link_params, 0, sizeof(link_params));
1913         link_params.link_up = true;
1914         edev->ops->common->set_link(edev->cdev, &link_params);
1915
1916         /* Query whether link is already-up */
1917         memset(&link_output, 0, sizeof(link_output));
1918         edev->ops->common->get_link(edev->cdev, &link_output);
1919         qede_roce_dev_event_open(edev);
1920         qede_link_update(edev, &link_output);
1921
1922         qede_ptp_start(edev, (mode == QEDE_LOAD_NORMAL));
1923
1924         edev->state = QEDE_STATE_OPEN;
1925
1926         DP_INFO(edev, "Ending successfully qede load\n");
1927
1928
1929         goto out;
1930 err4:
1931         qede_sync_free_irqs(edev);
1932         memset(&edev->int_info.msix_cnt, 0, sizeof(struct qed_int_info));
1933 err3:
1934         qede_napi_disable_remove(edev);
1935 err2:
1936         qede_free_mem_load(edev);
1937 err1:
1938         edev->ops->common->set_fp_int(edev->cdev, 0);
1939         qede_free_fp_array(edev);
1940         edev->num_queues = 0;
1941         edev->fp_num_tx = 0;
1942         edev->fp_num_rx = 0;
1943 out:
1944         if (!is_locked)
1945                 __qede_unlock(edev);
1946
1947         return rc;
1948 }
1949
1950 /* 'func' should be able to run between unload and reload assuming interface
1951  * is actually running, or afterwards in case it's currently DOWN.
1952  */
1953 void qede_reload(struct qede_dev *edev,
1954                  struct qede_reload_args *args, bool is_locked)
1955 {
1956         if (!is_locked)
1957                 __qede_lock(edev);
1958
1959         /* Since qede_lock is held, internal state wouldn't change even
1960          * if netdev state would start transitioning. Check whether current
1961          * internal configuration indicates device is up, then reload.
1962          */
1963         if (edev->state == QEDE_STATE_OPEN) {
1964                 qede_unload(edev, QEDE_UNLOAD_NORMAL, true);
1965                 if (args)
1966                         args->func(edev, args);
1967                 qede_load(edev, QEDE_LOAD_RELOAD, true);
1968
1969                 /* Since no one is going to do it for us, re-configure */
1970                 qede_config_rx_mode(edev->ndev);
1971         } else if (args) {
1972                 args->func(edev, args);
1973         }
1974
1975         if (!is_locked)
1976                 __qede_unlock(edev);
1977 }
1978
1979 /* called with rtnl_lock */
1980 static int qede_open(struct net_device *ndev)
1981 {
1982         struct qede_dev *edev = netdev_priv(ndev);
1983         int rc;
1984
1985         netif_carrier_off(ndev);
1986
1987         edev->ops->common->set_power_state(edev->cdev, PCI_D0);
1988
1989         rc = qede_load(edev, QEDE_LOAD_NORMAL, false);
1990         if (rc)
1991                 return rc;
1992
1993         udp_tunnel_get_rx_info(ndev);
1994
1995         edev->ops->common->update_drv_state(edev->cdev, true);
1996
1997         return 0;
1998 }
1999
2000 static int qede_close(struct net_device *ndev)
2001 {
2002         struct qede_dev *edev = netdev_priv(ndev);
2003
2004         qede_unload(edev, QEDE_UNLOAD_NORMAL, false);
2005
2006         edev->ops->common->update_drv_state(edev->cdev, false);
2007
2008         return 0;
2009 }
2010
2011 static void qede_link_update(void *dev, struct qed_link_output *link)
2012 {
2013         struct qede_dev *edev = dev;
2014
2015         if (!netif_running(edev->ndev)) {
2016                 DP_VERBOSE(edev, NETIF_MSG_LINK, "Interface is not running\n");
2017                 return;
2018         }
2019
2020         if (link->link_up) {
2021                 if (!netif_carrier_ok(edev->ndev)) {
2022                         DP_NOTICE(edev, "Link is up\n");
2023                         netif_tx_start_all_queues(edev->ndev);
2024                         netif_carrier_on(edev->ndev);
2025                 }
2026         } else {
2027                 if (netif_carrier_ok(edev->ndev)) {
2028                         DP_NOTICE(edev, "Link is down\n");
2029                         netif_tx_disable(edev->ndev);
2030                         netif_carrier_off(edev->ndev);
2031                 }
2032         }
2033 }