be2net: SRIOV Queue distribution should factor in EQ-count of VFs
[cascardo/linux.git] / drivers / net / ethernet / emulex / benet / be_main.c
1 /*
2  * Copyright (C) 2005 - 2015 Emulex
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation.  The full GNU General
8  * Public License is included in this distribution in the file called COPYING.
9  *
10  * Contact Information:
11  * linux-drivers@emulex.com
12  *
13  * Emulex
14  * 3333 Susan Street
15  * Costa Mesa, CA 92626
16  */
17
18 #include <linux/prefetch.h>
19 #include <linux/module.h>
20 #include "be.h"
21 #include "be_cmds.h"
22 #include <asm/div64.h>
23 #include <linux/aer.h>
24 #include <linux/if_bridge.h>
25 #include <net/busy_poll.h>
26 #include <net/vxlan.h>
27
28 MODULE_VERSION(DRV_VER);
29 MODULE_DESCRIPTION(DRV_DESC " " DRV_VER);
30 MODULE_AUTHOR("Emulex Corporation");
31 MODULE_LICENSE("GPL");
32
33 /* num_vfs module param is obsolete.
34  * Use sysfs method to enable/disable VFs.
35  */
36 static unsigned int num_vfs;
37 module_param(num_vfs, uint, S_IRUGO);
38 MODULE_PARM_DESC(num_vfs, "Number of PCI VFs to initialize");
39
40 static ushort rx_frag_size = 2048;
41 module_param(rx_frag_size, ushort, S_IRUGO);
42 MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data.");
43
44 static const struct pci_device_id be_dev_ids[] = {
45         { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
46         { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
47         { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
48         { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
49         { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID3)},
50         { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID4)},
51         { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID5)},
52         { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID6)},
53         { 0 }
54 };
55 MODULE_DEVICE_TABLE(pci, be_dev_ids);
56 /* UE Status Low CSR */
57 static const char * const ue_status_low_desc[] = {
58         "CEV",
59         "CTX",
60         "DBUF",
61         "ERX",
62         "Host",
63         "MPU",
64         "NDMA",
65         "PTC ",
66         "RDMA ",
67         "RXF ",
68         "RXIPS ",
69         "RXULP0 ",
70         "RXULP1 ",
71         "RXULP2 ",
72         "TIM ",
73         "TPOST ",
74         "TPRE ",
75         "TXIPS ",
76         "TXULP0 ",
77         "TXULP1 ",
78         "UC ",
79         "WDMA ",
80         "TXULP2 ",
81         "HOST1 ",
82         "P0_OB_LINK ",
83         "P1_OB_LINK ",
84         "HOST_GPIO ",
85         "MBOX ",
86         "ERX2 ",
87         "SPARE ",
88         "JTAG ",
89         "MPU_INTPEND "
90 };
91
92 /* UE Status High CSR */
93 static const char * const ue_status_hi_desc[] = {
94         "LPCMEMHOST",
95         "MGMT_MAC",
96         "PCS0ONLINE",
97         "MPU_IRAM",
98         "PCS1ONLINE",
99         "PCTL0",
100         "PCTL1",
101         "PMEM",
102         "RR",
103         "TXPB",
104         "RXPP",
105         "XAUI",
106         "TXP",
107         "ARM",
108         "IPC",
109         "HOST2",
110         "HOST3",
111         "HOST4",
112         "HOST5",
113         "HOST6",
114         "HOST7",
115         "ECRC",
116         "Poison TLP",
117         "NETC",
118         "PERIPH",
119         "LLTXULP",
120         "D2P",
121         "RCON",
122         "LDMA",
123         "LLTXP",
124         "LLTXPB",
125         "Unknown"
126 };
127
128 static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
129 {
130         struct be_dma_mem *mem = &q->dma_mem;
131
132         if (mem->va) {
133                 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
134                                   mem->dma);
135                 mem->va = NULL;
136         }
137 }
138
139 static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
140                           u16 len, u16 entry_size)
141 {
142         struct be_dma_mem *mem = &q->dma_mem;
143
144         memset(q, 0, sizeof(*q));
145         q->len = len;
146         q->entry_size = entry_size;
147         mem->size = len * entry_size;
148         mem->va = dma_zalloc_coherent(&adapter->pdev->dev, mem->size, &mem->dma,
149                                       GFP_KERNEL);
150         if (!mem->va)
151                 return -ENOMEM;
152         return 0;
153 }
154
155 static void be_reg_intr_set(struct be_adapter *adapter, bool enable)
156 {
157         u32 reg, enabled;
158
159         pci_read_config_dword(adapter->pdev, PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET,
160                               &reg);
161         enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
162
163         if (!enabled && enable)
164                 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
165         else if (enabled && !enable)
166                 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
167         else
168                 return;
169
170         pci_write_config_dword(adapter->pdev,
171                                PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET, reg);
172 }
173
174 static void be_intr_set(struct be_adapter *adapter, bool enable)
175 {
176         int status = 0;
177
178         /* On lancer interrupts can't be controlled via this register */
179         if (lancer_chip(adapter))
180                 return;
181
182         if (be_check_error(adapter, BE_ERROR_EEH))
183                 return;
184
185         status = be_cmd_intr_set(adapter, enable);
186         if (status)
187                 be_reg_intr_set(adapter, enable);
188 }
189
190 static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
191 {
192         u32 val = 0;
193
194         if (be_check_error(adapter, BE_ERROR_HW))
195                 return;
196
197         val |= qid & DB_RQ_RING_ID_MASK;
198         val |= posted << DB_RQ_NUM_POSTED_SHIFT;
199
200         wmb();
201         iowrite32(val, adapter->db + DB_RQ_OFFSET);
202 }
203
204 static void be_txq_notify(struct be_adapter *adapter, struct be_tx_obj *txo,
205                           u16 posted)
206 {
207         u32 val = 0;
208
209         if (be_check_error(adapter, BE_ERROR_HW))
210                 return;
211
212         val |= txo->q.id & DB_TXULP_RING_ID_MASK;
213         val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
214
215         wmb();
216         iowrite32(val, adapter->db + txo->db_offset);
217 }
218
219 static void be_eq_notify(struct be_adapter *adapter, u16 qid,
220                          bool arm, bool clear_int, u16 num_popped,
221                          u32 eq_delay_mult_enc)
222 {
223         u32 val = 0;
224
225         val |= qid & DB_EQ_RING_ID_MASK;
226         val |= ((qid & DB_EQ_RING_ID_EXT_MASK) << DB_EQ_RING_ID_EXT_MASK_SHIFT);
227
228         if (be_check_error(adapter, BE_ERROR_HW))
229                 return;
230
231         if (arm)
232                 val |= 1 << DB_EQ_REARM_SHIFT;
233         if (clear_int)
234                 val |= 1 << DB_EQ_CLR_SHIFT;
235         val |= 1 << DB_EQ_EVNT_SHIFT;
236         val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
237         val |= eq_delay_mult_enc << DB_EQ_R2I_DLY_SHIFT;
238         iowrite32(val, adapter->db + DB_EQ_OFFSET);
239 }
240
241 void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
242 {
243         u32 val = 0;
244
245         val |= qid & DB_CQ_RING_ID_MASK;
246         val |= ((qid & DB_CQ_RING_ID_EXT_MASK) <<
247                         DB_CQ_RING_ID_EXT_MASK_SHIFT);
248
249         if (be_check_error(adapter, BE_ERROR_HW))
250                 return;
251
252         if (arm)
253                 val |= 1 << DB_CQ_REARM_SHIFT;
254         val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
255         iowrite32(val, adapter->db + DB_CQ_OFFSET);
256 }
257
258 static int be_mac_addr_set(struct net_device *netdev, void *p)
259 {
260         struct be_adapter *adapter = netdev_priv(netdev);
261         struct device *dev = &adapter->pdev->dev;
262         struct sockaddr *addr = p;
263         int status;
264         u8 mac[ETH_ALEN];
265         u32 old_pmac_id = adapter->pmac_id[0], curr_pmac_id = 0;
266
267         if (!is_valid_ether_addr(addr->sa_data))
268                 return -EADDRNOTAVAIL;
269
270         /* Proceed further only if, User provided MAC is different
271          * from active MAC
272          */
273         if (ether_addr_equal(addr->sa_data, netdev->dev_addr))
274                 return 0;
275
276         /* if device is not running, copy MAC to netdev->dev_addr */
277         if (!netif_running(netdev))
278                 goto done;
279
280         /* The PMAC_ADD cmd may fail if the VF doesn't have FILTMGMT
281          * privilege or if PF did not provision the new MAC address.
282          * On BE3, this cmd will always fail if the VF doesn't have the
283          * FILTMGMT privilege. This failure is OK, only if the PF programmed
284          * the MAC for the VF.
285          */
286         status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
287                                  adapter->if_handle, &adapter->pmac_id[0], 0);
288         if (!status) {
289                 curr_pmac_id = adapter->pmac_id[0];
290
291                 /* Delete the old programmed MAC. This call may fail if the
292                  * old MAC was already deleted by the PF driver.
293                  */
294                 if (adapter->pmac_id[0] != old_pmac_id)
295                         be_cmd_pmac_del(adapter, adapter->if_handle,
296                                         old_pmac_id, 0);
297         }
298
299         /* Decide if the new MAC is successfully activated only after
300          * querying the FW
301          */
302         status = be_cmd_get_active_mac(adapter, curr_pmac_id, mac,
303                                        adapter->if_handle, true, 0);
304         if (status)
305                 goto err;
306
307         /* The MAC change did not happen, either due to lack of privilege
308          * or PF didn't pre-provision.
309          */
310         if (!ether_addr_equal(addr->sa_data, mac)) {
311                 status = -EPERM;
312                 goto err;
313         }
314 done:
315         ether_addr_copy(netdev->dev_addr, addr->sa_data);
316         dev_info(dev, "MAC address changed to %pM\n", addr->sa_data);
317         return 0;
318 err:
319         dev_warn(dev, "MAC address change to %pM failed\n", addr->sa_data);
320         return status;
321 }
322
323 /* BE2 supports only v0 cmd */
324 static void *hw_stats_from_cmd(struct be_adapter *adapter)
325 {
326         if (BE2_chip(adapter)) {
327                 struct be_cmd_resp_get_stats_v0 *cmd = adapter->stats_cmd.va;
328
329                 return &cmd->hw_stats;
330         } else if (BE3_chip(adapter)) {
331                 struct be_cmd_resp_get_stats_v1 *cmd = adapter->stats_cmd.va;
332
333                 return &cmd->hw_stats;
334         } else {
335                 struct be_cmd_resp_get_stats_v2 *cmd = adapter->stats_cmd.va;
336
337                 return &cmd->hw_stats;
338         }
339 }
340
341 /* BE2 supports only v0 cmd */
342 static void *be_erx_stats_from_cmd(struct be_adapter *adapter)
343 {
344         if (BE2_chip(adapter)) {
345                 struct be_hw_stats_v0 *hw_stats = hw_stats_from_cmd(adapter);
346
347                 return &hw_stats->erx;
348         } else if (BE3_chip(adapter)) {
349                 struct be_hw_stats_v1 *hw_stats = hw_stats_from_cmd(adapter);
350
351                 return &hw_stats->erx;
352         } else {
353                 struct be_hw_stats_v2 *hw_stats = hw_stats_from_cmd(adapter);
354
355                 return &hw_stats->erx;
356         }
357 }
358
359 static void populate_be_v0_stats(struct be_adapter *adapter)
360 {
361         struct be_hw_stats_v0 *hw_stats = hw_stats_from_cmd(adapter);
362         struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
363         struct be_rxf_stats_v0 *rxf_stats = &hw_stats->rxf;
364         struct be_port_rxf_stats_v0 *port_stats =
365                                         &rxf_stats->port[adapter->port_num];
366         struct be_drv_stats *drvs = &adapter->drv_stats;
367
368         be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
369         drvs->rx_pause_frames = port_stats->rx_pause_frames;
370         drvs->rx_crc_errors = port_stats->rx_crc_errors;
371         drvs->rx_control_frames = port_stats->rx_control_frames;
372         drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
373         drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
374         drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
375         drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
376         drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
377         drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
378         drvs->rxpp_fifo_overflow_drop = port_stats->rx_fifo_overflow;
379         drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
380         drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
381         drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
382         drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
383         drvs->rx_input_fifo_overflow_drop = port_stats->rx_input_fifo_overflow;
384         drvs->rx_dropped_header_too_small =
385                 port_stats->rx_dropped_header_too_small;
386         drvs->rx_address_filtered =
387                                         port_stats->rx_address_filtered +
388                                         port_stats->rx_vlan_filtered;
389         drvs->rx_alignment_symbol_errors =
390                 port_stats->rx_alignment_symbol_errors;
391
392         drvs->tx_pauseframes = port_stats->tx_pauseframes;
393         drvs->tx_controlframes = port_stats->tx_controlframes;
394
395         if (adapter->port_num)
396                 drvs->jabber_events = rxf_stats->port1_jabber_events;
397         else
398                 drvs->jabber_events = rxf_stats->port0_jabber_events;
399         drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
400         drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
401         drvs->forwarded_packets = rxf_stats->forwarded_packets;
402         drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
403         drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
404         drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
405         adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
406 }
407
408 static void populate_be_v1_stats(struct be_adapter *adapter)
409 {
410         struct be_hw_stats_v1 *hw_stats = hw_stats_from_cmd(adapter);
411         struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
412         struct be_rxf_stats_v1 *rxf_stats = &hw_stats->rxf;
413         struct be_port_rxf_stats_v1 *port_stats =
414                                         &rxf_stats->port[adapter->port_num];
415         struct be_drv_stats *drvs = &adapter->drv_stats;
416
417         be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
418         drvs->pmem_fifo_overflow_drop = port_stats->pmem_fifo_overflow_drop;
419         drvs->rx_priority_pause_frames = port_stats->rx_priority_pause_frames;
420         drvs->rx_pause_frames = port_stats->rx_pause_frames;
421         drvs->rx_crc_errors = port_stats->rx_crc_errors;
422         drvs->rx_control_frames = port_stats->rx_control_frames;
423         drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
424         drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
425         drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
426         drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
427         drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
428         drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
429         drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
430         drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
431         drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
432         drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
433         drvs->rx_dropped_header_too_small =
434                 port_stats->rx_dropped_header_too_small;
435         drvs->rx_input_fifo_overflow_drop =
436                 port_stats->rx_input_fifo_overflow_drop;
437         drvs->rx_address_filtered = port_stats->rx_address_filtered;
438         drvs->rx_alignment_symbol_errors =
439                 port_stats->rx_alignment_symbol_errors;
440         drvs->rxpp_fifo_overflow_drop = port_stats->rxpp_fifo_overflow_drop;
441         drvs->tx_pauseframes = port_stats->tx_pauseframes;
442         drvs->tx_controlframes = port_stats->tx_controlframes;
443         drvs->tx_priority_pauseframes = port_stats->tx_priority_pauseframes;
444         drvs->jabber_events = port_stats->jabber_events;
445         drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
446         drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
447         drvs->forwarded_packets = rxf_stats->forwarded_packets;
448         drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
449         drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
450         drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
451         adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
452 }
453
454 static void populate_be_v2_stats(struct be_adapter *adapter)
455 {
456         struct be_hw_stats_v2 *hw_stats = hw_stats_from_cmd(adapter);
457         struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
458         struct be_rxf_stats_v2 *rxf_stats = &hw_stats->rxf;
459         struct be_port_rxf_stats_v2 *port_stats =
460                                         &rxf_stats->port[adapter->port_num];
461         struct be_drv_stats *drvs = &adapter->drv_stats;
462
463         be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
464         drvs->pmem_fifo_overflow_drop = port_stats->pmem_fifo_overflow_drop;
465         drvs->rx_priority_pause_frames = port_stats->rx_priority_pause_frames;
466         drvs->rx_pause_frames = port_stats->rx_pause_frames;
467         drvs->rx_crc_errors = port_stats->rx_crc_errors;
468         drvs->rx_control_frames = port_stats->rx_control_frames;
469         drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
470         drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
471         drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
472         drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
473         drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
474         drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
475         drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
476         drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
477         drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
478         drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
479         drvs->rx_dropped_header_too_small =
480                 port_stats->rx_dropped_header_too_small;
481         drvs->rx_input_fifo_overflow_drop =
482                 port_stats->rx_input_fifo_overflow_drop;
483         drvs->rx_address_filtered = port_stats->rx_address_filtered;
484         drvs->rx_alignment_symbol_errors =
485                 port_stats->rx_alignment_symbol_errors;
486         drvs->rxpp_fifo_overflow_drop = port_stats->rxpp_fifo_overflow_drop;
487         drvs->tx_pauseframes = port_stats->tx_pauseframes;
488         drvs->tx_controlframes = port_stats->tx_controlframes;
489         drvs->tx_priority_pauseframes = port_stats->tx_priority_pauseframes;
490         drvs->jabber_events = port_stats->jabber_events;
491         drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
492         drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
493         drvs->forwarded_packets = rxf_stats->forwarded_packets;
494         drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
495         drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
496         drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
497         adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
498         if (be_roce_supported(adapter)) {
499                 drvs->rx_roce_bytes_lsd = port_stats->roce_bytes_received_lsd;
500                 drvs->rx_roce_bytes_msd = port_stats->roce_bytes_received_msd;
501                 drvs->rx_roce_frames = port_stats->roce_frames_received;
502                 drvs->roce_drops_crc = port_stats->roce_drops_crc;
503                 drvs->roce_drops_payload_len =
504                         port_stats->roce_drops_payload_len;
505         }
506 }
507
508 static void populate_lancer_stats(struct be_adapter *adapter)
509 {
510         struct be_drv_stats *drvs = &adapter->drv_stats;
511         struct lancer_pport_stats *pport_stats = pport_stats_from_cmd(adapter);
512
513         be_dws_le_to_cpu(pport_stats, sizeof(*pport_stats));
514         drvs->rx_pause_frames = pport_stats->rx_pause_frames_lo;
515         drvs->rx_crc_errors = pport_stats->rx_crc_errors_lo;
516         drvs->rx_control_frames = pport_stats->rx_control_frames_lo;
517         drvs->rx_in_range_errors = pport_stats->rx_in_range_errors;
518         drvs->rx_frame_too_long = pport_stats->rx_frames_too_long_lo;
519         drvs->rx_dropped_runt = pport_stats->rx_dropped_runt;
520         drvs->rx_ip_checksum_errs = pport_stats->rx_ip_checksum_errors;
521         drvs->rx_tcp_checksum_errs = pport_stats->rx_tcp_checksum_errors;
522         drvs->rx_udp_checksum_errs = pport_stats->rx_udp_checksum_errors;
523         drvs->rx_dropped_tcp_length =
524                                 pport_stats->rx_dropped_invalid_tcp_length;
525         drvs->rx_dropped_too_small = pport_stats->rx_dropped_too_small;
526         drvs->rx_dropped_too_short = pport_stats->rx_dropped_too_short;
527         drvs->rx_out_range_errors = pport_stats->rx_out_of_range_errors;
528         drvs->rx_dropped_header_too_small =
529                                 pport_stats->rx_dropped_header_too_small;
530         drvs->rx_input_fifo_overflow_drop = pport_stats->rx_fifo_overflow;
531         drvs->rx_address_filtered =
532                                         pport_stats->rx_address_filtered +
533                                         pport_stats->rx_vlan_filtered;
534         drvs->rx_alignment_symbol_errors = pport_stats->rx_symbol_errors_lo;
535         drvs->rxpp_fifo_overflow_drop = pport_stats->rx_fifo_overflow;
536         drvs->tx_pauseframes = pport_stats->tx_pause_frames_lo;
537         drvs->tx_controlframes = pport_stats->tx_control_frames_lo;
538         drvs->jabber_events = pport_stats->rx_jabbers;
539         drvs->forwarded_packets = pport_stats->num_forwards_lo;
540         drvs->rx_drops_mtu = pport_stats->rx_drops_mtu_lo;
541         drvs->rx_drops_too_many_frags =
542                                 pport_stats->rx_drops_too_many_frags_lo;
543 }
544
545 static void accumulate_16bit_val(u32 *acc, u16 val)
546 {
547 #define lo(x)                   (x & 0xFFFF)
548 #define hi(x)                   (x & 0xFFFF0000)
549         bool wrapped = val < lo(*acc);
550         u32 newacc = hi(*acc) + val;
551
552         if (wrapped)
553                 newacc += 65536;
554         ACCESS_ONCE(*acc) = newacc;
555 }
556
557 static void populate_erx_stats(struct be_adapter *adapter,
558                                struct be_rx_obj *rxo, u32 erx_stat)
559 {
560         if (!BEx_chip(adapter))
561                 rx_stats(rxo)->rx_drops_no_frags = erx_stat;
562         else
563                 /* below erx HW counter can actually wrap around after
564                  * 65535. Driver accumulates a 32-bit value
565                  */
566                 accumulate_16bit_val(&rx_stats(rxo)->rx_drops_no_frags,
567                                      (u16)erx_stat);
568 }
569
570 void be_parse_stats(struct be_adapter *adapter)
571 {
572         struct be_erx_stats_v2 *erx = be_erx_stats_from_cmd(adapter);
573         struct be_rx_obj *rxo;
574         int i;
575         u32 erx_stat;
576
577         if (lancer_chip(adapter)) {
578                 populate_lancer_stats(adapter);
579         } else {
580                 if (BE2_chip(adapter))
581                         populate_be_v0_stats(adapter);
582                 else if (BE3_chip(adapter))
583                         /* for BE3 */
584                         populate_be_v1_stats(adapter);
585                 else
586                         populate_be_v2_stats(adapter);
587
588                 /* erx_v2 is longer than v0, v1. use v2 for v0, v1 access */
589                 for_all_rx_queues(adapter, rxo, i) {
590                         erx_stat = erx->rx_drops_no_fragments[rxo->q.id];
591                         populate_erx_stats(adapter, rxo, erx_stat);
592                 }
593         }
594 }
595
596 static struct rtnl_link_stats64 *be_get_stats64(struct net_device *netdev,
597                                                 struct rtnl_link_stats64 *stats)
598 {
599         struct be_adapter *adapter = netdev_priv(netdev);
600         struct be_drv_stats *drvs = &adapter->drv_stats;
601         struct be_rx_obj *rxo;
602         struct be_tx_obj *txo;
603         u64 pkts, bytes;
604         unsigned int start;
605         int i;
606
607         for_all_rx_queues(adapter, rxo, i) {
608                 const struct be_rx_stats *rx_stats = rx_stats(rxo);
609
610                 do {
611                         start = u64_stats_fetch_begin_irq(&rx_stats->sync);
612                         pkts = rx_stats(rxo)->rx_pkts;
613                         bytes = rx_stats(rxo)->rx_bytes;
614                 } while (u64_stats_fetch_retry_irq(&rx_stats->sync, start));
615                 stats->rx_packets += pkts;
616                 stats->rx_bytes += bytes;
617                 stats->multicast += rx_stats(rxo)->rx_mcast_pkts;
618                 stats->rx_dropped += rx_stats(rxo)->rx_drops_no_skbs +
619                                         rx_stats(rxo)->rx_drops_no_frags;
620         }
621
622         for_all_tx_queues(adapter, txo, i) {
623                 const struct be_tx_stats *tx_stats = tx_stats(txo);
624
625                 do {
626                         start = u64_stats_fetch_begin_irq(&tx_stats->sync);
627                         pkts = tx_stats(txo)->tx_pkts;
628                         bytes = tx_stats(txo)->tx_bytes;
629                 } while (u64_stats_fetch_retry_irq(&tx_stats->sync, start));
630                 stats->tx_packets += pkts;
631                 stats->tx_bytes += bytes;
632         }
633
634         /* bad pkts received */
635         stats->rx_errors = drvs->rx_crc_errors +
636                 drvs->rx_alignment_symbol_errors +
637                 drvs->rx_in_range_errors +
638                 drvs->rx_out_range_errors +
639                 drvs->rx_frame_too_long +
640                 drvs->rx_dropped_too_small +
641                 drvs->rx_dropped_too_short +
642                 drvs->rx_dropped_header_too_small +
643                 drvs->rx_dropped_tcp_length +
644                 drvs->rx_dropped_runt;
645
646         /* detailed rx errors */
647         stats->rx_length_errors = drvs->rx_in_range_errors +
648                 drvs->rx_out_range_errors +
649                 drvs->rx_frame_too_long;
650
651         stats->rx_crc_errors = drvs->rx_crc_errors;
652
653         /* frame alignment errors */
654         stats->rx_frame_errors = drvs->rx_alignment_symbol_errors;
655
656         /* receiver fifo overrun */
657         /* drops_no_pbuf is no per i/f, it's per BE card */
658         stats->rx_fifo_errors = drvs->rxpp_fifo_overflow_drop +
659                                 drvs->rx_input_fifo_overflow_drop +
660                                 drvs->rx_drops_no_pbuf;
661         return stats;
662 }
663
664 void be_link_status_update(struct be_adapter *adapter, u8 link_status)
665 {
666         struct net_device *netdev = adapter->netdev;
667
668         if (!(adapter->flags & BE_FLAGS_LINK_STATUS_INIT)) {
669                 netif_carrier_off(netdev);
670                 adapter->flags |= BE_FLAGS_LINK_STATUS_INIT;
671         }
672
673         if (link_status)
674                 netif_carrier_on(netdev);
675         else
676                 netif_carrier_off(netdev);
677
678         netdev_info(netdev, "Link is %s\n", link_status ? "Up" : "Down");
679 }
680
681 static void be_tx_stats_update(struct be_tx_obj *txo, struct sk_buff *skb)
682 {
683         struct be_tx_stats *stats = tx_stats(txo);
684         u64 tx_pkts = skb_shinfo(skb)->gso_segs ? : 1;
685
686         u64_stats_update_begin(&stats->sync);
687         stats->tx_reqs++;
688         stats->tx_bytes += skb->len;
689         stats->tx_pkts += tx_pkts;
690         if (skb->encapsulation && skb->ip_summed == CHECKSUM_PARTIAL)
691                 stats->tx_vxlan_offload_pkts += tx_pkts;
692         u64_stats_update_end(&stats->sync);
693 }
694
695 /* Returns number of WRBs needed for the skb */
696 static u32 skb_wrb_cnt(struct sk_buff *skb)
697 {
698         /* +1 for the header wrb */
699         return 1 + (skb_headlen(skb) ? 1 : 0) + skb_shinfo(skb)->nr_frags;
700 }
701
702 static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
703 {
704         wrb->frag_pa_hi = cpu_to_le32(upper_32_bits(addr));
705         wrb->frag_pa_lo = cpu_to_le32(lower_32_bits(addr));
706         wrb->frag_len = cpu_to_le32(len & ETH_WRB_FRAG_LEN_MASK);
707         wrb->rsvd0 = 0;
708 }
709
710 /* A dummy wrb is just all zeros. Using a separate routine for dummy-wrb
711  * to avoid the swap and shift/mask operations in wrb_fill().
712  */
713 static inline void wrb_fill_dummy(struct be_eth_wrb *wrb)
714 {
715         wrb->frag_pa_hi = 0;
716         wrb->frag_pa_lo = 0;
717         wrb->frag_len = 0;
718         wrb->rsvd0 = 0;
719 }
720
721 static inline u16 be_get_tx_vlan_tag(struct be_adapter *adapter,
722                                      struct sk_buff *skb)
723 {
724         u8 vlan_prio;
725         u16 vlan_tag;
726
727         vlan_tag = skb_vlan_tag_get(skb);
728         vlan_prio = (vlan_tag & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
729         /* If vlan priority provided by OS is NOT in available bmap */
730         if (!(adapter->vlan_prio_bmap & (1 << vlan_prio)))
731                 vlan_tag = (vlan_tag & ~VLAN_PRIO_MASK) |
732                                 adapter->recommended_prio_bits;
733
734         return vlan_tag;
735 }
736
737 /* Used only for IP tunnel packets */
738 static u16 skb_inner_ip_proto(struct sk_buff *skb)
739 {
740         return (inner_ip_hdr(skb)->version == 4) ?
741                 inner_ip_hdr(skb)->protocol : inner_ipv6_hdr(skb)->nexthdr;
742 }
743
744 static u16 skb_ip_proto(struct sk_buff *skb)
745 {
746         return (ip_hdr(skb)->version == 4) ?
747                 ip_hdr(skb)->protocol : ipv6_hdr(skb)->nexthdr;
748 }
749
750 static inline bool be_is_txq_full(struct be_tx_obj *txo)
751 {
752         return atomic_read(&txo->q.used) + BE_MAX_TX_FRAG_COUNT >= txo->q.len;
753 }
754
755 static inline bool be_can_txq_wake(struct be_tx_obj *txo)
756 {
757         return atomic_read(&txo->q.used) < txo->q.len / 2;
758 }
759
760 static inline bool be_is_tx_compl_pending(struct be_tx_obj *txo)
761 {
762         return atomic_read(&txo->q.used) > txo->pend_wrb_cnt;
763 }
764
765 static void be_get_wrb_params_from_skb(struct be_adapter *adapter,
766                                        struct sk_buff *skb,
767                                        struct be_wrb_params *wrb_params)
768 {
769         u16 proto;
770
771         if (skb_is_gso(skb)) {
772                 BE_WRB_F_SET(wrb_params->features, LSO, 1);
773                 wrb_params->lso_mss = skb_shinfo(skb)->gso_size;
774                 if (skb_is_gso_v6(skb) && !lancer_chip(adapter))
775                         BE_WRB_F_SET(wrb_params->features, LSO6, 1);
776         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
777                 if (skb->encapsulation) {
778                         BE_WRB_F_SET(wrb_params->features, IPCS, 1);
779                         proto = skb_inner_ip_proto(skb);
780                 } else {
781                         proto = skb_ip_proto(skb);
782                 }
783                 if (proto == IPPROTO_TCP)
784                         BE_WRB_F_SET(wrb_params->features, TCPCS, 1);
785                 else if (proto == IPPROTO_UDP)
786                         BE_WRB_F_SET(wrb_params->features, UDPCS, 1);
787         }
788
789         if (skb_vlan_tag_present(skb)) {
790                 BE_WRB_F_SET(wrb_params->features, VLAN, 1);
791                 wrb_params->vlan_tag = be_get_tx_vlan_tag(adapter, skb);
792         }
793
794         BE_WRB_F_SET(wrb_params->features, CRC, 1);
795 }
796
797 static void wrb_fill_hdr(struct be_adapter *adapter,
798                          struct be_eth_hdr_wrb *hdr,
799                          struct be_wrb_params *wrb_params,
800                          struct sk_buff *skb)
801 {
802         memset(hdr, 0, sizeof(*hdr));
803
804         SET_TX_WRB_HDR_BITS(crc, hdr,
805                             BE_WRB_F_GET(wrb_params->features, CRC));
806         SET_TX_WRB_HDR_BITS(ipcs, hdr,
807                             BE_WRB_F_GET(wrb_params->features, IPCS));
808         SET_TX_WRB_HDR_BITS(tcpcs, hdr,
809                             BE_WRB_F_GET(wrb_params->features, TCPCS));
810         SET_TX_WRB_HDR_BITS(udpcs, hdr,
811                             BE_WRB_F_GET(wrb_params->features, UDPCS));
812
813         SET_TX_WRB_HDR_BITS(lso, hdr,
814                             BE_WRB_F_GET(wrb_params->features, LSO));
815         SET_TX_WRB_HDR_BITS(lso6, hdr,
816                             BE_WRB_F_GET(wrb_params->features, LSO6));
817         SET_TX_WRB_HDR_BITS(lso_mss, hdr, wrb_params->lso_mss);
818
819         /* Hack to skip HW VLAN tagging needs evt = 1, compl = 0. When this
820          * hack is not needed, the evt bit is set while ringing DB.
821          */
822         SET_TX_WRB_HDR_BITS(event, hdr,
823                             BE_WRB_F_GET(wrb_params->features, VLAN_SKIP_HW));
824         SET_TX_WRB_HDR_BITS(vlan, hdr,
825                             BE_WRB_F_GET(wrb_params->features, VLAN));
826         SET_TX_WRB_HDR_BITS(vlan_tag, hdr, wrb_params->vlan_tag);
827
828         SET_TX_WRB_HDR_BITS(num_wrb, hdr, skb_wrb_cnt(skb));
829         SET_TX_WRB_HDR_BITS(len, hdr, skb->len);
830         SET_TX_WRB_HDR_BITS(mgmt, hdr,
831                             BE_WRB_F_GET(wrb_params->features, OS2BMC));
832 }
833
834 static void unmap_tx_frag(struct device *dev, struct be_eth_wrb *wrb,
835                           bool unmap_single)
836 {
837         dma_addr_t dma;
838         u32 frag_len = le32_to_cpu(wrb->frag_len);
839
840
841         dma = (u64)le32_to_cpu(wrb->frag_pa_hi) << 32 |
842                 (u64)le32_to_cpu(wrb->frag_pa_lo);
843         if (frag_len) {
844                 if (unmap_single)
845                         dma_unmap_single(dev, dma, frag_len, DMA_TO_DEVICE);
846                 else
847                         dma_unmap_page(dev, dma, frag_len, DMA_TO_DEVICE);
848         }
849 }
850
851 /* Grab a WRB header for xmit */
852 static u16 be_tx_get_wrb_hdr(struct be_tx_obj *txo)
853 {
854         u16 head = txo->q.head;
855
856         queue_head_inc(&txo->q);
857         return head;
858 }
859
860 /* Set up the WRB header for xmit */
861 static void be_tx_setup_wrb_hdr(struct be_adapter *adapter,
862                                 struct be_tx_obj *txo,
863                                 struct be_wrb_params *wrb_params,
864                                 struct sk_buff *skb, u16 head)
865 {
866         u32 num_frags = skb_wrb_cnt(skb);
867         struct be_queue_info *txq = &txo->q;
868         struct be_eth_hdr_wrb *hdr = queue_index_node(txq, head);
869
870         wrb_fill_hdr(adapter, hdr, wrb_params, skb);
871         be_dws_cpu_to_le(hdr, sizeof(*hdr));
872
873         BUG_ON(txo->sent_skb_list[head]);
874         txo->sent_skb_list[head] = skb;
875         txo->last_req_hdr = head;
876         atomic_add(num_frags, &txq->used);
877         txo->last_req_wrb_cnt = num_frags;
878         txo->pend_wrb_cnt += num_frags;
879 }
880
881 /* Setup a WRB fragment (buffer descriptor) for xmit */
882 static void be_tx_setup_wrb_frag(struct be_tx_obj *txo, dma_addr_t busaddr,
883                                  int len)
884 {
885         struct be_eth_wrb *wrb;
886         struct be_queue_info *txq = &txo->q;
887
888         wrb = queue_head_node(txq);
889         wrb_fill(wrb, busaddr, len);
890         queue_head_inc(txq);
891 }
892
893 /* Bring the queue back to the state it was in before be_xmit_enqueue() routine
894  * was invoked. The producer index is restored to the previous packet and the
895  * WRBs of the current packet are unmapped. Invoked to handle tx setup errors.
896  */
897 static void be_xmit_restore(struct be_adapter *adapter,
898                             struct be_tx_obj *txo, u16 head, bool map_single,
899                             u32 copied)
900 {
901         struct device *dev;
902         struct be_eth_wrb *wrb;
903         struct be_queue_info *txq = &txo->q;
904
905         dev = &adapter->pdev->dev;
906         txq->head = head;
907
908         /* skip the first wrb (hdr); it's not mapped */
909         queue_head_inc(txq);
910         while (copied) {
911                 wrb = queue_head_node(txq);
912                 unmap_tx_frag(dev, wrb, map_single);
913                 map_single = false;
914                 copied -= le32_to_cpu(wrb->frag_len);
915                 queue_head_inc(txq);
916         }
917
918         txq->head = head;
919 }
920
921 /* Enqueue the given packet for transmit. This routine allocates WRBs for the
922  * packet, dma maps the packet buffers and sets up the WRBs. Returns the number
923  * of WRBs used up by the packet.
924  */
925 static u32 be_xmit_enqueue(struct be_adapter *adapter, struct be_tx_obj *txo,
926                            struct sk_buff *skb,
927                            struct be_wrb_params *wrb_params)
928 {
929         u32 i, copied = 0, wrb_cnt = skb_wrb_cnt(skb);
930         struct device *dev = &adapter->pdev->dev;
931         struct be_queue_info *txq = &txo->q;
932         bool map_single = false;
933         u16 head = txq->head;
934         dma_addr_t busaddr;
935         int len;
936
937         head = be_tx_get_wrb_hdr(txo);
938
939         if (skb->len > skb->data_len) {
940                 len = skb_headlen(skb);
941
942                 busaddr = dma_map_single(dev, skb->data, len, DMA_TO_DEVICE);
943                 if (dma_mapping_error(dev, busaddr))
944                         goto dma_err;
945                 map_single = true;
946                 be_tx_setup_wrb_frag(txo, busaddr, len);
947                 copied += len;
948         }
949
950         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
951                 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
952                 len = skb_frag_size(frag);
953
954                 busaddr = skb_frag_dma_map(dev, frag, 0, len, DMA_TO_DEVICE);
955                 if (dma_mapping_error(dev, busaddr))
956                         goto dma_err;
957                 be_tx_setup_wrb_frag(txo, busaddr, len);
958                 copied += len;
959         }
960
961         be_tx_setup_wrb_hdr(adapter, txo, wrb_params, skb, head);
962
963         be_tx_stats_update(txo, skb);
964         return wrb_cnt;
965
966 dma_err:
967         adapter->drv_stats.dma_map_errors++;
968         be_xmit_restore(adapter, txo, head, map_single, copied);
969         return 0;
970 }
971
972 static inline int qnq_async_evt_rcvd(struct be_adapter *adapter)
973 {
974         return adapter->flags & BE_FLAGS_QNQ_ASYNC_EVT_RCVD;
975 }
976
977 static struct sk_buff *be_insert_vlan_in_pkt(struct be_adapter *adapter,
978                                              struct sk_buff *skb,
979                                              struct be_wrb_params
980                                              *wrb_params)
981 {
982         u16 vlan_tag = 0;
983
984         skb = skb_share_check(skb, GFP_ATOMIC);
985         if (unlikely(!skb))
986                 return skb;
987
988         if (skb_vlan_tag_present(skb))
989                 vlan_tag = be_get_tx_vlan_tag(adapter, skb);
990
991         if (qnq_async_evt_rcvd(adapter) && adapter->pvid) {
992                 if (!vlan_tag)
993                         vlan_tag = adapter->pvid;
994                 /* f/w workaround to set skip_hw_vlan = 1, informs the F/W to
995                  * skip VLAN insertion
996                  */
997                 BE_WRB_F_SET(wrb_params->features, VLAN_SKIP_HW, 1);
998         }
999
1000         if (vlan_tag) {
1001                 skb = vlan_insert_tag_set_proto(skb, htons(ETH_P_8021Q),
1002                                                 vlan_tag);
1003                 if (unlikely(!skb))
1004                         return skb;
1005                 skb->vlan_tci = 0;
1006         }
1007
1008         /* Insert the outer VLAN, if any */
1009         if (adapter->qnq_vid) {
1010                 vlan_tag = adapter->qnq_vid;
1011                 skb = vlan_insert_tag_set_proto(skb, htons(ETH_P_8021Q),
1012                                                 vlan_tag);
1013                 if (unlikely(!skb))
1014                         return skb;
1015                 BE_WRB_F_SET(wrb_params->features, VLAN_SKIP_HW, 1);
1016         }
1017
1018         return skb;
1019 }
1020
1021 static bool be_ipv6_exthdr_check(struct sk_buff *skb)
1022 {
1023         struct ethhdr *eh = (struct ethhdr *)skb->data;
1024         u16 offset = ETH_HLEN;
1025
1026         if (eh->h_proto == htons(ETH_P_IPV6)) {
1027                 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + offset);
1028
1029                 offset += sizeof(struct ipv6hdr);
1030                 if (ip6h->nexthdr != NEXTHDR_TCP &&
1031                     ip6h->nexthdr != NEXTHDR_UDP) {
1032                         struct ipv6_opt_hdr *ehdr =
1033                                 (struct ipv6_opt_hdr *)(skb->data + offset);
1034
1035                         /* offending pkt: 2nd byte following IPv6 hdr is 0xff */
1036                         if (ehdr->hdrlen == 0xff)
1037                                 return true;
1038                 }
1039         }
1040         return false;
1041 }
1042
1043 static int be_vlan_tag_tx_chk(struct be_adapter *adapter, struct sk_buff *skb)
1044 {
1045         return skb_vlan_tag_present(skb) || adapter->pvid || adapter->qnq_vid;
1046 }
1047
1048 static int be_ipv6_tx_stall_chk(struct be_adapter *adapter, struct sk_buff *skb)
1049 {
1050         return BE3_chip(adapter) && be_ipv6_exthdr_check(skb);
1051 }
1052
1053 static struct sk_buff *be_lancer_xmit_workarounds(struct be_adapter *adapter,
1054                                                   struct sk_buff *skb,
1055                                                   struct be_wrb_params
1056                                                   *wrb_params)
1057 {
1058         struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
1059         unsigned int eth_hdr_len;
1060         struct iphdr *ip;
1061
1062         /* For padded packets, BE HW modifies tot_len field in IP header
1063          * incorrecly when VLAN tag is inserted by HW.
1064          * For padded packets, Lancer computes incorrect checksum.
1065          */
1066         eth_hdr_len = ntohs(skb->protocol) == ETH_P_8021Q ?
1067                                                 VLAN_ETH_HLEN : ETH_HLEN;
1068         if (skb->len <= 60 &&
1069             (lancer_chip(adapter) || skb_vlan_tag_present(skb)) &&
1070             is_ipv4_pkt(skb)) {
1071                 ip = (struct iphdr *)ip_hdr(skb);
1072                 pskb_trim(skb, eth_hdr_len + ntohs(ip->tot_len));
1073         }
1074
1075         /* If vlan tag is already inlined in the packet, skip HW VLAN
1076          * tagging in pvid-tagging mode
1077          */
1078         if (be_pvid_tagging_enabled(adapter) &&
1079             veh->h_vlan_proto == htons(ETH_P_8021Q))
1080                 BE_WRB_F_SET(wrb_params->features, VLAN_SKIP_HW, 1);
1081
1082         /* HW has a bug wherein it will calculate CSUM for VLAN
1083          * pkts even though it is disabled.
1084          * Manually insert VLAN in pkt.
1085          */
1086         if (skb->ip_summed != CHECKSUM_PARTIAL &&
1087             skb_vlan_tag_present(skb)) {
1088                 skb = be_insert_vlan_in_pkt(adapter, skb, wrb_params);
1089                 if (unlikely(!skb))
1090                         goto err;
1091         }
1092
1093         /* HW may lockup when VLAN HW tagging is requested on
1094          * certain ipv6 packets. Drop such pkts if the HW workaround to
1095          * skip HW tagging is not enabled by FW.
1096          */
1097         if (unlikely(be_ipv6_tx_stall_chk(adapter, skb) &&
1098                      (adapter->pvid || adapter->qnq_vid) &&
1099                      !qnq_async_evt_rcvd(adapter)))
1100                 goto tx_drop;
1101
1102         /* Manual VLAN tag insertion to prevent:
1103          * ASIC lockup when the ASIC inserts VLAN tag into
1104          * certain ipv6 packets. Insert VLAN tags in driver,
1105          * and set event, completion, vlan bits accordingly
1106          * in the Tx WRB.
1107          */
1108         if (be_ipv6_tx_stall_chk(adapter, skb) &&
1109             be_vlan_tag_tx_chk(adapter, skb)) {
1110                 skb = be_insert_vlan_in_pkt(adapter, skb, wrb_params);
1111                 if (unlikely(!skb))
1112                         goto err;
1113         }
1114
1115         return skb;
1116 tx_drop:
1117         dev_kfree_skb_any(skb);
1118 err:
1119         return NULL;
1120 }
1121
1122 static struct sk_buff *be_xmit_workarounds(struct be_adapter *adapter,
1123                                            struct sk_buff *skb,
1124                                            struct be_wrb_params *wrb_params)
1125 {
1126         /* Lancer, SH and BE3 in SRIOV mode have a bug wherein
1127          * packets that are 32b or less may cause a transmit stall
1128          * on that port. The workaround is to pad such packets
1129          * (len <= 32 bytes) to a minimum length of 36b.
1130          */
1131         if (skb->len <= 32) {
1132                 if (skb_put_padto(skb, 36))
1133                         return NULL;
1134         }
1135
1136         if (BEx_chip(adapter) || lancer_chip(adapter)) {
1137                 skb = be_lancer_xmit_workarounds(adapter, skb, wrb_params);
1138                 if (!skb)
1139                         return NULL;
1140         }
1141
1142         return skb;
1143 }
1144
1145 static void be_xmit_flush(struct be_adapter *adapter, struct be_tx_obj *txo)
1146 {
1147         struct be_queue_info *txq = &txo->q;
1148         struct be_eth_hdr_wrb *hdr = queue_index_node(txq, txo->last_req_hdr);
1149
1150         /* Mark the last request eventable if it hasn't been marked already */
1151         if (!(hdr->dw[2] & cpu_to_le32(TX_HDR_WRB_EVT)))
1152                 hdr->dw[2] |= cpu_to_le32(TX_HDR_WRB_EVT | TX_HDR_WRB_COMPL);
1153
1154         /* compose a dummy wrb if there are odd set of wrbs to notify */
1155         if (!lancer_chip(adapter) && (txo->pend_wrb_cnt & 1)) {
1156                 wrb_fill_dummy(queue_head_node(txq));
1157                 queue_head_inc(txq);
1158                 atomic_inc(&txq->used);
1159                 txo->pend_wrb_cnt++;
1160                 hdr->dw[2] &= ~cpu_to_le32(TX_HDR_WRB_NUM_MASK <<
1161                                            TX_HDR_WRB_NUM_SHIFT);
1162                 hdr->dw[2] |= cpu_to_le32((txo->last_req_wrb_cnt + 1) <<
1163                                           TX_HDR_WRB_NUM_SHIFT);
1164         }
1165         be_txq_notify(adapter, txo, txo->pend_wrb_cnt);
1166         txo->pend_wrb_cnt = 0;
1167 }
1168
1169 /* OS2BMC related */
1170
1171 #define DHCP_CLIENT_PORT        68
1172 #define DHCP_SERVER_PORT        67
1173 #define NET_BIOS_PORT1          137
1174 #define NET_BIOS_PORT2          138
1175 #define DHCPV6_RAS_PORT         547
1176
1177 #define is_mc_allowed_on_bmc(adapter, eh)       \
1178         (!is_multicast_filt_enabled(adapter) && \
1179          is_multicast_ether_addr(eh->h_dest) && \
1180          !is_broadcast_ether_addr(eh->h_dest))
1181
1182 #define is_bc_allowed_on_bmc(adapter, eh)       \
1183         (!is_broadcast_filt_enabled(adapter) && \
1184          is_broadcast_ether_addr(eh->h_dest))
1185
1186 #define is_arp_allowed_on_bmc(adapter, skb)     \
1187         (is_arp(skb) && is_arp_filt_enabled(adapter))
1188
1189 #define is_broadcast_packet(eh, adapter)        \
1190                 (is_multicast_ether_addr(eh->h_dest) && \
1191                 !compare_ether_addr(eh->h_dest, adapter->netdev->broadcast))
1192
1193 #define is_arp(skb)     (skb->protocol == htons(ETH_P_ARP))
1194
1195 #define is_arp_filt_enabled(adapter)    \
1196                 (adapter->bmc_filt_mask & (BMC_FILT_BROADCAST_ARP))
1197
1198 #define is_dhcp_client_filt_enabled(adapter)    \
1199                 (adapter->bmc_filt_mask & BMC_FILT_BROADCAST_DHCP_CLIENT)
1200
1201 #define is_dhcp_srvr_filt_enabled(adapter)      \
1202                 (adapter->bmc_filt_mask & BMC_FILT_BROADCAST_DHCP_SERVER)
1203
1204 #define is_nbios_filt_enabled(adapter)  \
1205                 (adapter->bmc_filt_mask & BMC_FILT_BROADCAST_NET_BIOS)
1206
1207 #define is_ipv6_na_filt_enabled(adapter)        \
1208                 (adapter->bmc_filt_mask &       \
1209                         BMC_FILT_MULTICAST_IPV6_NEIGH_ADVER)
1210
1211 #define is_ipv6_ra_filt_enabled(adapter)        \
1212                 (adapter->bmc_filt_mask & BMC_FILT_MULTICAST_IPV6_RA)
1213
1214 #define is_ipv6_ras_filt_enabled(adapter)       \
1215                 (adapter->bmc_filt_mask & BMC_FILT_MULTICAST_IPV6_RAS)
1216
1217 #define is_broadcast_filt_enabled(adapter)      \
1218                 (adapter->bmc_filt_mask & BMC_FILT_BROADCAST)
1219
1220 #define is_multicast_filt_enabled(adapter)      \
1221                 (adapter->bmc_filt_mask & BMC_FILT_MULTICAST)
1222
1223 static bool be_send_pkt_to_bmc(struct be_adapter *adapter,
1224                                struct sk_buff **skb)
1225 {
1226         struct ethhdr *eh = (struct ethhdr *)(*skb)->data;
1227         bool os2bmc = false;
1228
1229         if (!be_is_os2bmc_enabled(adapter))
1230                 goto done;
1231
1232         if (!is_multicast_ether_addr(eh->h_dest))
1233                 goto done;
1234
1235         if (is_mc_allowed_on_bmc(adapter, eh) ||
1236             is_bc_allowed_on_bmc(adapter, eh) ||
1237             is_arp_allowed_on_bmc(adapter, (*skb))) {
1238                 os2bmc = true;
1239                 goto done;
1240         }
1241
1242         if ((*skb)->protocol == htons(ETH_P_IPV6)) {
1243                 struct ipv6hdr *hdr = ipv6_hdr((*skb));
1244                 u8 nexthdr = hdr->nexthdr;
1245
1246                 if (nexthdr == IPPROTO_ICMPV6) {
1247                         struct icmp6hdr *icmp6 = icmp6_hdr((*skb));
1248
1249                         switch (icmp6->icmp6_type) {
1250                         case NDISC_ROUTER_ADVERTISEMENT:
1251                                 os2bmc = is_ipv6_ra_filt_enabled(adapter);
1252                                 goto done;
1253                         case NDISC_NEIGHBOUR_ADVERTISEMENT:
1254                                 os2bmc = is_ipv6_na_filt_enabled(adapter);
1255                                 goto done;
1256                         default:
1257                                 break;
1258                         }
1259                 }
1260         }
1261
1262         if (is_udp_pkt((*skb))) {
1263                 struct udphdr *udp = udp_hdr((*skb));
1264
1265                 switch (ntohs(udp->dest)) {
1266                 case DHCP_CLIENT_PORT:
1267                         os2bmc = is_dhcp_client_filt_enabled(adapter);
1268                         goto done;
1269                 case DHCP_SERVER_PORT:
1270                         os2bmc = is_dhcp_srvr_filt_enabled(adapter);
1271                         goto done;
1272                 case NET_BIOS_PORT1:
1273                 case NET_BIOS_PORT2:
1274                         os2bmc = is_nbios_filt_enabled(adapter);
1275                         goto done;
1276                 case DHCPV6_RAS_PORT:
1277                         os2bmc = is_ipv6_ras_filt_enabled(adapter);
1278                         goto done;
1279                 default:
1280                         break;
1281                 }
1282         }
1283 done:
1284         /* For packets over a vlan, which are destined
1285          * to BMC, asic expects the vlan to be inline in the packet.
1286          */
1287         if (os2bmc)
1288                 *skb = be_insert_vlan_in_pkt(adapter, *skb, NULL);
1289
1290         return os2bmc;
1291 }
1292
1293 static netdev_tx_t be_xmit(struct sk_buff *skb, struct net_device *netdev)
1294 {
1295         struct be_adapter *adapter = netdev_priv(netdev);
1296         u16 q_idx = skb_get_queue_mapping(skb);
1297         struct be_tx_obj *txo = &adapter->tx_obj[q_idx];
1298         struct be_wrb_params wrb_params = { 0 };
1299         bool flush = !skb->xmit_more;
1300         u16 wrb_cnt;
1301
1302         skb = be_xmit_workarounds(adapter, skb, &wrb_params);
1303         if (unlikely(!skb))
1304                 goto drop;
1305
1306         be_get_wrb_params_from_skb(adapter, skb, &wrb_params);
1307
1308         wrb_cnt = be_xmit_enqueue(adapter, txo, skb, &wrb_params);
1309         if (unlikely(!wrb_cnt)) {
1310                 dev_kfree_skb_any(skb);
1311                 goto drop;
1312         }
1313
1314         /* if os2bmc is enabled and if the pkt is destined to bmc,
1315          * enqueue the pkt a 2nd time with mgmt bit set.
1316          */
1317         if (be_send_pkt_to_bmc(adapter, &skb)) {
1318                 BE_WRB_F_SET(wrb_params.features, OS2BMC, 1);
1319                 wrb_cnt = be_xmit_enqueue(adapter, txo, skb, &wrb_params);
1320                 if (unlikely(!wrb_cnt))
1321                         goto drop;
1322                 else
1323                         skb_get(skb);
1324         }
1325
1326         if (be_is_txq_full(txo)) {
1327                 netif_stop_subqueue(netdev, q_idx);
1328                 tx_stats(txo)->tx_stops++;
1329         }
1330
1331         if (flush || __netif_subqueue_stopped(netdev, q_idx))
1332                 be_xmit_flush(adapter, txo);
1333
1334         return NETDEV_TX_OK;
1335 drop:
1336         tx_stats(txo)->tx_drv_drops++;
1337         /* Flush the already enqueued tx requests */
1338         if (flush && txo->pend_wrb_cnt)
1339                 be_xmit_flush(adapter, txo);
1340
1341         return NETDEV_TX_OK;
1342 }
1343
1344 static int be_change_mtu(struct net_device *netdev, int new_mtu)
1345 {
1346         struct be_adapter *adapter = netdev_priv(netdev);
1347         struct device *dev = &adapter->pdev->dev;
1348
1349         if (new_mtu < BE_MIN_MTU || new_mtu > BE_MAX_MTU) {
1350                 dev_info(dev, "MTU must be between %d and %d bytes\n",
1351                          BE_MIN_MTU, BE_MAX_MTU);
1352                 return -EINVAL;
1353         }
1354
1355         dev_info(dev, "MTU changed from %d to %d bytes\n",
1356                  netdev->mtu, new_mtu);
1357         netdev->mtu = new_mtu;
1358         return 0;
1359 }
1360
1361 static inline bool be_in_all_promisc(struct be_adapter *adapter)
1362 {
1363         return (adapter->if_flags & BE_IF_FLAGS_ALL_PROMISCUOUS) ==
1364                         BE_IF_FLAGS_ALL_PROMISCUOUS;
1365 }
1366
1367 static int be_set_vlan_promisc(struct be_adapter *adapter)
1368 {
1369         struct device *dev = &adapter->pdev->dev;
1370         int status;
1371
1372         if (adapter->if_flags & BE_IF_FLAGS_VLAN_PROMISCUOUS)
1373                 return 0;
1374
1375         status = be_cmd_rx_filter(adapter, BE_IF_FLAGS_VLAN_PROMISCUOUS, ON);
1376         if (!status) {
1377                 dev_info(dev, "Enabled VLAN promiscuous mode\n");
1378                 adapter->if_flags |= BE_IF_FLAGS_VLAN_PROMISCUOUS;
1379         } else {
1380                 dev_err(dev, "Failed to enable VLAN promiscuous mode\n");
1381         }
1382         return status;
1383 }
1384
1385 static int be_clear_vlan_promisc(struct be_adapter *adapter)
1386 {
1387         struct device *dev = &adapter->pdev->dev;
1388         int status;
1389
1390         status = be_cmd_rx_filter(adapter, BE_IF_FLAGS_VLAN_PROMISCUOUS, OFF);
1391         if (!status) {
1392                 dev_info(dev, "Disabling VLAN promiscuous mode\n");
1393                 adapter->if_flags &= ~BE_IF_FLAGS_VLAN_PROMISCUOUS;
1394         }
1395         return status;
1396 }
1397
1398 /*
1399  * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
1400  * If the user configures more, place BE in vlan promiscuous mode.
1401  */
1402 static int be_vid_config(struct be_adapter *adapter)
1403 {
1404         struct device *dev = &adapter->pdev->dev;
1405         u16 vids[BE_NUM_VLANS_SUPPORTED];
1406         u16 num = 0, i = 0;
1407         int status = 0;
1408
1409         /* No need to further configure vids if in promiscuous mode */
1410         if (be_in_all_promisc(adapter))
1411                 return 0;
1412
1413         if (adapter->vlans_added > be_max_vlans(adapter))
1414                 return be_set_vlan_promisc(adapter);
1415
1416         /* Construct VLAN Table to give to HW */
1417         for_each_set_bit(i, adapter->vids, VLAN_N_VID)
1418                 vids[num++] = cpu_to_le16(i);
1419
1420         status = be_cmd_vlan_config(adapter, adapter->if_handle, vids, num, 0);
1421         if (status) {
1422                 dev_err(dev, "Setting HW VLAN filtering failed\n");
1423                 /* Set to VLAN promisc mode as setting VLAN filter failed */
1424                 if (addl_status(status) == MCC_ADDL_STATUS_INSUFFICIENT_VLANS ||
1425                     addl_status(status) ==
1426                                 MCC_ADDL_STATUS_INSUFFICIENT_RESOURCES)
1427                         return be_set_vlan_promisc(adapter);
1428         } else if (adapter->if_flags & BE_IF_FLAGS_VLAN_PROMISCUOUS) {
1429                 status = be_clear_vlan_promisc(adapter);
1430         }
1431         return status;
1432 }
1433
1434 static int be_vlan_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
1435 {
1436         struct be_adapter *adapter = netdev_priv(netdev);
1437         int status = 0;
1438
1439         /* Packets with VID 0 are always received by Lancer by default */
1440         if (lancer_chip(adapter) && vid == 0)
1441                 return status;
1442
1443         if (test_bit(vid, adapter->vids))
1444                 return status;
1445
1446         set_bit(vid, adapter->vids);
1447         adapter->vlans_added++;
1448
1449         status = be_vid_config(adapter);
1450         if (status) {
1451                 adapter->vlans_added--;
1452                 clear_bit(vid, adapter->vids);
1453         }
1454
1455         return status;
1456 }
1457
1458 static int be_vlan_rem_vid(struct net_device *netdev, __be16 proto, u16 vid)
1459 {
1460         struct be_adapter *adapter = netdev_priv(netdev);
1461
1462         /* Packets with VID 0 are always received by Lancer by default */
1463         if (lancer_chip(adapter) && vid == 0)
1464                 return 0;
1465
1466         if (!test_bit(vid, adapter->vids))
1467                 return 0;
1468
1469         clear_bit(vid, adapter->vids);
1470         adapter->vlans_added--;
1471
1472         return be_vid_config(adapter);
1473 }
1474
1475 static void be_clear_all_promisc(struct be_adapter *adapter)
1476 {
1477         be_cmd_rx_filter(adapter, BE_IF_FLAGS_ALL_PROMISCUOUS, OFF);
1478         adapter->if_flags &= ~BE_IF_FLAGS_ALL_PROMISCUOUS;
1479 }
1480
1481 static void be_set_all_promisc(struct be_adapter *adapter)
1482 {
1483         be_cmd_rx_filter(adapter, BE_IF_FLAGS_ALL_PROMISCUOUS, ON);
1484         adapter->if_flags |= BE_IF_FLAGS_ALL_PROMISCUOUS;
1485 }
1486
1487 static void be_set_mc_promisc(struct be_adapter *adapter)
1488 {
1489         int status;
1490
1491         if (adapter->if_flags & BE_IF_FLAGS_MCAST_PROMISCUOUS)
1492                 return;
1493
1494         status = be_cmd_rx_filter(adapter, BE_IF_FLAGS_MCAST_PROMISCUOUS, ON);
1495         if (!status)
1496                 adapter->if_flags |= BE_IF_FLAGS_MCAST_PROMISCUOUS;
1497 }
1498
1499 static void be_set_mc_list(struct be_adapter *adapter)
1500 {
1501         int status;
1502
1503         status = be_cmd_rx_filter(adapter, BE_IF_FLAGS_MULTICAST, ON);
1504         if (!status)
1505                 adapter->if_flags &= ~BE_IF_FLAGS_MCAST_PROMISCUOUS;
1506         else
1507                 be_set_mc_promisc(adapter);
1508 }
1509
1510 static void be_set_uc_list(struct be_adapter *adapter)
1511 {
1512         struct netdev_hw_addr *ha;
1513         int i = 1; /* First slot is claimed by the Primary MAC */
1514
1515         for (; adapter->uc_macs > 0; adapter->uc_macs--, i++)
1516                 be_cmd_pmac_del(adapter, adapter->if_handle,
1517                                 adapter->pmac_id[i], 0);
1518
1519         if (netdev_uc_count(adapter->netdev) > be_max_uc(adapter)) {
1520                 be_set_all_promisc(adapter);
1521                 return;
1522         }
1523
1524         netdev_for_each_uc_addr(ha, adapter->netdev) {
1525                 adapter->uc_macs++; /* First slot is for Primary MAC */
1526                 be_cmd_pmac_add(adapter, (u8 *)ha->addr, adapter->if_handle,
1527                                 &adapter->pmac_id[adapter->uc_macs], 0);
1528         }
1529 }
1530
1531 static void be_clear_uc_list(struct be_adapter *adapter)
1532 {
1533         int i;
1534
1535         for (i = 1; i < (adapter->uc_macs + 1); i++)
1536                 be_cmd_pmac_del(adapter, adapter->if_handle,
1537                                 adapter->pmac_id[i], 0);
1538         adapter->uc_macs = 0;
1539 }
1540
1541 static void be_set_rx_mode(struct net_device *netdev)
1542 {
1543         struct be_adapter *adapter = netdev_priv(netdev);
1544
1545         if (netdev->flags & IFF_PROMISC) {
1546                 be_set_all_promisc(adapter);
1547                 return;
1548         }
1549
1550         /* Interface was previously in promiscuous mode; disable it */
1551         if (be_in_all_promisc(adapter)) {
1552                 be_clear_all_promisc(adapter);
1553                 if (adapter->vlans_added)
1554                         be_vid_config(adapter);
1555         }
1556
1557         /* Enable multicast promisc if num configured exceeds what we support */
1558         if (netdev->flags & IFF_ALLMULTI ||
1559             netdev_mc_count(netdev) > be_max_mc(adapter)) {
1560                 be_set_mc_promisc(adapter);
1561                 return;
1562         }
1563
1564         if (netdev_uc_count(netdev) != adapter->uc_macs)
1565                 be_set_uc_list(adapter);
1566
1567         be_set_mc_list(adapter);
1568 }
1569
1570 static int be_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1571 {
1572         struct be_adapter *adapter = netdev_priv(netdev);
1573         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1574         int status;
1575
1576         if (!sriov_enabled(adapter))
1577                 return -EPERM;
1578
1579         if (!is_valid_ether_addr(mac) || vf >= adapter->num_vfs)
1580                 return -EINVAL;
1581
1582         /* Proceed further only if user provided MAC is different
1583          * from active MAC
1584          */
1585         if (ether_addr_equal(mac, vf_cfg->mac_addr))
1586                 return 0;
1587
1588         if (BEx_chip(adapter)) {
1589                 be_cmd_pmac_del(adapter, vf_cfg->if_handle, vf_cfg->pmac_id,
1590                                 vf + 1);
1591
1592                 status = be_cmd_pmac_add(adapter, mac, vf_cfg->if_handle,
1593                                          &vf_cfg->pmac_id, vf + 1);
1594         } else {
1595                 status = be_cmd_set_mac(adapter, mac, vf_cfg->if_handle,
1596                                         vf + 1);
1597         }
1598
1599         if (status) {
1600                 dev_err(&adapter->pdev->dev, "MAC %pM set on VF %d Failed: %#x",
1601                         mac, vf, status);
1602                 return be_cmd_status(status);
1603         }
1604
1605         ether_addr_copy(vf_cfg->mac_addr, mac);
1606
1607         return 0;
1608 }
1609
1610 static int be_get_vf_config(struct net_device *netdev, int vf,
1611                             struct ifla_vf_info *vi)
1612 {
1613         struct be_adapter *adapter = netdev_priv(netdev);
1614         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1615
1616         if (!sriov_enabled(adapter))
1617                 return -EPERM;
1618
1619         if (vf >= adapter->num_vfs)
1620                 return -EINVAL;
1621
1622         vi->vf = vf;
1623         vi->max_tx_rate = vf_cfg->tx_rate;
1624         vi->min_tx_rate = 0;
1625         vi->vlan = vf_cfg->vlan_tag & VLAN_VID_MASK;
1626         vi->qos = vf_cfg->vlan_tag >> VLAN_PRIO_SHIFT;
1627         memcpy(&vi->mac, vf_cfg->mac_addr, ETH_ALEN);
1628         vi->linkstate = adapter->vf_cfg[vf].plink_tracking;
1629         vi->spoofchk = adapter->vf_cfg[vf].spoofchk;
1630
1631         return 0;
1632 }
1633
1634 static int be_set_vf_tvt(struct be_adapter *adapter, int vf, u16 vlan)
1635 {
1636         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1637         u16 vids[BE_NUM_VLANS_SUPPORTED];
1638         int vf_if_id = vf_cfg->if_handle;
1639         int status;
1640
1641         /* Enable Transparent VLAN Tagging */
1642         status = be_cmd_set_hsw_config(adapter, vlan, vf + 1, vf_if_id, 0, 0);
1643         if (status)
1644                 return status;
1645
1646         /* Clear pre-programmed VLAN filters on VF if any, if TVT is enabled */
1647         vids[0] = 0;
1648         status = be_cmd_vlan_config(adapter, vf_if_id, vids, 1, vf + 1);
1649         if (!status)
1650                 dev_info(&adapter->pdev->dev,
1651                          "Cleared guest VLANs on VF%d", vf);
1652
1653         /* After TVT is enabled, disallow VFs to program VLAN filters */
1654         if (vf_cfg->privileges & BE_PRIV_FILTMGMT) {
1655                 status = be_cmd_set_fn_privileges(adapter, vf_cfg->privileges &
1656                                                   ~BE_PRIV_FILTMGMT, vf + 1);
1657                 if (!status)
1658                         vf_cfg->privileges &= ~BE_PRIV_FILTMGMT;
1659         }
1660         return 0;
1661 }
1662
1663 static int be_clear_vf_tvt(struct be_adapter *adapter, int vf)
1664 {
1665         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1666         struct device *dev = &adapter->pdev->dev;
1667         int status;
1668
1669         /* Reset Transparent VLAN Tagging. */
1670         status = be_cmd_set_hsw_config(adapter, BE_RESET_VLAN_TAG_ID, vf + 1,
1671                                        vf_cfg->if_handle, 0, 0);
1672         if (status)
1673                 return status;
1674
1675         /* Allow VFs to program VLAN filtering */
1676         if (!(vf_cfg->privileges & BE_PRIV_FILTMGMT)) {
1677                 status = be_cmd_set_fn_privileges(adapter, vf_cfg->privileges |
1678                                                   BE_PRIV_FILTMGMT, vf + 1);
1679                 if (!status) {
1680                         vf_cfg->privileges |= BE_PRIV_FILTMGMT;
1681                         dev_info(dev, "VF%d: FILTMGMT priv enabled", vf);
1682                 }
1683         }
1684
1685         dev_info(dev,
1686                  "Disable/re-enable i/f in VM to clear Transparent VLAN tag");
1687         return 0;
1688 }
1689
1690 static int be_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
1691 {
1692         struct be_adapter *adapter = netdev_priv(netdev);
1693         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1694         int status;
1695
1696         if (!sriov_enabled(adapter))
1697                 return -EPERM;
1698
1699         if (vf >= adapter->num_vfs || vlan > 4095 || qos > 7)
1700                 return -EINVAL;
1701
1702         if (vlan || qos) {
1703                 vlan |= qos << VLAN_PRIO_SHIFT;
1704                 status = be_set_vf_tvt(adapter, vf, vlan);
1705         } else {
1706                 status = be_clear_vf_tvt(adapter, vf);
1707         }
1708
1709         if (status) {
1710                 dev_err(&adapter->pdev->dev,
1711                         "VLAN %d config on VF %d failed : %#x\n", vlan, vf,
1712                         status);
1713                 return be_cmd_status(status);
1714         }
1715
1716         vf_cfg->vlan_tag = vlan;
1717         return 0;
1718 }
1719
1720 static int be_set_vf_tx_rate(struct net_device *netdev, int vf,
1721                              int min_tx_rate, int max_tx_rate)
1722 {
1723         struct be_adapter *adapter = netdev_priv(netdev);
1724         struct device *dev = &adapter->pdev->dev;
1725         int percent_rate, status = 0;
1726         u16 link_speed = 0;
1727         u8 link_status;
1728
1729         if (!sriov_enabled(adapter))
1730                 return -EPERM;
1731
1732         if (vf >= adapter->num_vfs)
1733                 return -EINVAL;
1734
1735         if (min_tx_rate)
1736                 return -EINVAL;
1737
1738         if (!max_tx_rate)
1739                 goto config_qos;
1740
1741         status = be_cmd_link_status_query(adapter, &link_speed,
1742                                           &link_status, 0);
1743         if (status)
1744                 goto err;
1745
1746         if (!link_status) {
1747                 dev_err(dev, "TX-rate setting not allowed when link is down\n");
1748                 status = -ENETDOWN;
1749                 goto err;
1750         }
1751
1752         if (max_tx_rate < 100 || max_tx_rate > link_speed) {
1753                 dev_err(dev, "TX-rate must be between 100 and %d Mbps\n",
1754                         link_speed);
1755                 status = -EINVAL;
1756                 goto err;
1757         }
1758
1759         /* On Skyhawk the QOS setting must be done only as a % value */
1760         percent_rate = link_speed / 100;
1761         if (skyhawk_chip(adapter) && (max_tx_rate % percent_rate)) {
1762                 dev_err(dev, "TX-rate must be a multiple of %d Mbps\n",
1763                         percent_rate);
1764                 status = -EINVAL;
1765                 goto err;
1766         }
1767
1768 config_qos:
1769         status = be_cmd_config_qos(adapter, max_tx_rate, link_speed, vf + 1);
1770         if (status)
1771                 goto err;
1772
1773         adapter->vf_cfg[vf].tx_rate = max_tx_rate;
1774         return 0;
1775
1776 err:
1777         dev_err(dev, "TX-rate setting of %dMbps on VF%d failed\n",
1778                 max_tx_rate, vf);
1779         return be_cmd_status(status);
1780 }
1781
1782 static int be_set_vf_link_state(struct net_device *netdev, int vf,
1783                                 int link_state)
1784 {
1785         struct be_adapter *adapter = netdev_priv(netdev);
1786         int status;
1787
1788         if (!sriov_enabled(adapter))
1789                 return -EPERM;
1790
1791         if (vf >= adapter->num_vfs)
1792                 return -EINVAL;
1793
1794         status = be_cmd_set_logical_link_config(adapter, link_state, vf+1);
1795         if (status) {
1796                 dev_err(&adapter->pdev->dev,
1797                         "Link state change on VF %d failed: %#x\n", vf, status);
1798                 return be_cmd_status(status);
1799         }
1800
1801         adapter->vf_cfg[vf].plink_tracking = link_state;
1802
1803         return 0;
1804 }
1805
1806 static int be_set_vf_spoofchk(struct net_device *netdev, int vf, bool enable)
1807 {
1808         struct be_adapter *adapter = netdev_priv(netdev);
1809         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1810         u8 spoofchk;
1811         int status;
1812
1813         if (!sriov_enabled(adapter))
1814                 return -EPERM;
1815
1816         if (vf >= adapter->num_vfs)
1817                 return -EINVAL;
1818
1819         if (BEx_chip(adapter))
1820                 return -EOPNOTSUPP;
1821
1822         if (enable == vf_cfg->spoofchk)
1823                 return 0;
1824
1825         spoofchk = enable ? ENABLE_MAC_SPOOFCHK : DISABLE_MAC_SPOOFCHK;
1826
1827         status = be_cmd_set_hsw_config(adapter, 0, vf + 1, vf_cfg->if_handle,
1828                                        0, spoofchk);
1829         if (status) {
1830                 dev_err(&adapter->pdev->dev,
1831                         "Spoofchk change on VF %d failed: %#x\n", vf, status);
1832                 return be_cmd_status(status);
1833         }
1834
1835         vf_cfg->spoofchk = enable;
1836         return 0;
1837 }
1838
1839 static void be_aic_update(struct be_aic_obj *aic, u64 rx_pkts, u64 tx_pkts,
1840                           ulong now)
1841 {
1842         aic->rx_pkts_prev = rx_pkts;
1843         aic->tx_reqs_prev = tx_pkts;
1844         aic->jiffies = now;
1845 }
1846
1847 static int be_get_new_eqd(struct be_eq_obj *eqo)
1848 {
1849         struct be_adapter *adapter = eqo->adapter;
1850         int eqd, start;
1851         struct be_aic_obj *aic;
1852         struct be_rx_obj *rxo;
1853         struct be_tx_obj *txo;
1854         u64 rx_pkts = 0, tx_pkts = 0;
1855         ulong now;
1856         u32 pps, delta;
1857         int i;
1858
1859         aic = &adapter->aic_obj[eqo->idx];
1860         if (!aic->enable) {
1861                 if (aic->jiffies)
1862                         aic->jiffies = 0;
1863                 eqd = aic->et_eqd;
1864                 return eqd;
1865         }
1866
1867         for_all_rx_queues_on_eq(adapter, eqo, rxo, i) {
1868                 do {
1869                         start = u64_stats_fetch_begin_irq(&rxo->stats.sync);
1870                         rx_pkts += rxo->stats.rx_pkts;
1871                 } while (u64_stats_fetch_retry_irq(&rxo->stats.sync, start));
1872         }
1873
1874         for_all_tx_queues_on_eq(adapter, eqo, txo, i) {
1875                 do {
1876                         start = u64_stats_fetch_begin_irq(&txo->stats.sync);
1877                         tx_pkts += txo->stats.tx_reqs;
1878                 } while (u64_stats_fetch_retry_irq(&txo->stats.sync, start));
1879         }
1880
1881         /* Skip, if wrapped around or first calculation */
1882         now = jiffies;
1883         if (!aic->jiffies || time_before(now, aic->jiffies) ||
1884             rx_pkts < aic->rx_pkts_prev ||
1885             tx_pkts < aic->tx_reqs_prev) {
1886                 be_aic_update(aic, rx_pkts, tx_pkts, now);
1887                 return aic->prev_eqd;
1888         }
1889
1890         delta = jiffies_to_msecs(now - aic->jiffies);
1891         if (delta == 0)
1892                 return aic->prev_eqd;
1893
1894         pps = (((u32)(rx_pkts - aic->rx_pkts_prev) * 1000) / delta) +
1895                 (((u32)(tx_pkts - aic->tx_reqs_prev) * 1000) / delta);
1896         eqd = (pps / 15000) << 2;
1897
1898         if (eqd < 8)
1899                 eqd = 0;
1900         eqd = min_t(u32, eqd, aic->max_eqd);
1901         eqd = max_t(u32, eqd, aic->min_eqd);
1902
1903         be_aic_update(aic, rx_pkts, tx_pkts, now);
1904
1905         return eqd;
1906 }
1907
1908 /* For Skyhawk-R only */
1909 static u32 be_get_eq_delay_mult_enc(struct be_eq_obj *eqo)
1910 {
1911         struct be_adapter *adapter = eqo->adapter;
1912         struct be_aic_obj *aic = &adapter->aic_obj[eqo->idx];
1913         ulong now = jiffies;
1914         int eqd;
1915         u32 mult_enc;
1916
1917         if (!aic->enable)
1918                 return 0;
1919
1920         if (time_before_eq(now, aic->jiffies) ||
1921             jiffies_to_msecs(now - aic->jiffies) < 1)
1922                 eqd = aic->prev_eqd;
1923         else
1924                 eqd = be_get_new_eqd(eqo);
1925
1926         if (eqd > 100)
1927                 mult_enc = R2I_DLY_ENC_1;
1928         else if (eqd > 60)
1929                 mult_enc = R2I_DLY_ENC_2;
1930         else if (eqd > 20)
1931                 mult_enc = R2I_DLY_ENC_3;
1932         else
1933                 mult_enc = R2I_DLY_ENC_0;
1934
1935         aic->prev_eqd = eqd;
1936
1937         return mult_enc;
1938 }
1939
1940 void be_eqd_update(struct be_adapter *adapter, bool force_update)
1941 {
1942         struct be_set_eqd set_eqd[MAX_EVT_QS];
1943         struct be_aic_obj *aic;
1944         struct be_eq_obj *eqo;
1945         int i, num = 0, eqd;
1946
1947         for_all_evt_queues(adapter, eqo, i) {
1948                 aic = &adapter->aic_obj[eqo->idx];
1949                 eqd = be_get_new_eqd(eqo);
1950                 if (force_update || eqd != aic->prev_eqd) {
1951                         set_eqd[num].delay_multiplier = (eqd * 65)/100;
1952                         set_eqd[num].eq_id = eqo->q.id;
1953                         aic->prev_eqd = eqd;
1954                         num++;
1955                 }
1956         }
1957
1958         if (num)
1959                 be_cmd_modify_eqd(adapter, set_eqd, num);
1960 }
1961
1962 static void be_rx_stats_update(struct be_rx_obj *rxo,
1963                                struct be_rx_compl_info *rxcp)
1964 {
1965         struct be_rx_stats *stats = rx_stats(rxo);
1966
1967         u64_stats_update_begin(&stats->sync);
1968         stats->rx_compl++;
1969         stats->rx_bytes += rxcp->pkt_size;
1970         stats->rx_pkts++;
1971         if (rxcp->tunneled)
1972                 stats->rx_vxlan_offload_pkts++;
1973         if (rxcp->pkt_type == BE_MULTICAST_PACKET)
1974                 stats->rx_mcast_pkts++;
1975         if (rxcp->err)
1976                 stats->rx_compl_err++;
1977         u64_stats_update_end(&stats->sync);
1978 }
1979
1980 static inline bool csum_passed(struct be_rx_compl_info *rxcp)
1981 {
1982         /* L4 checksum is not reliable for non TCP/UDP packets.
1983          * Also ignore ipcksm for ipv6 pkts
1984          */
1985         return (rxcp->tcpf || rxcp->udpf) && rxcp->l4_csum &&
1986                 (rxcp->ip_csum || rxcp->ipv6) && !rxcp->err;
1987 }
1988
1989 static struct be_rx_page_info *get_rx_page_info(struct be_rx_obj *rxo)
1990 {
1991         struct be_adapter *adapter = rxo->adapter;
1992         struct be_rx_page_info *rx_page_info;
1993         struct be_queue_info *rxq = &rxo->q;
1994         u16 frag_idx = rxq->tail;
1995
1996         rx_page_info = &rxo->page_info_tbl[frag_idx];
1997         BUG_ON(!rx_page_info->page);
1998
1999         if (rx_page_info->last_frag) {
2000                 dma_unmap_page(&adapter->pdev->dev,
2001                                dma_unmap_addr(rx_page_info, bus),
2002                                adapter->big_page_size, DMA_FROM_DEVICE);
2003                 rx_page_info->last_frag = false;
2004         } else {
2005                 dma_sync_single_for_cpu(&adapter->pdev->dev,
2006                                         dma_unmap_addr(rx_page_info, bus),
2007                                         rx_frag_size, DMA_FROM_DEVICE);
2008         }
2009
2010         queue_tail_inc(rxq);
2011         atomic_dec(&rxq->used);
2012         return rx_page_info;
2013 }
2014
2015 /* Throwaway the data in the Rx completion */
2016 static void be_rx_compl_discard(struct be_rx_obj *rxo,
2017                                 struct be_rx_compl_info *rxcp)
2018 {
2019         struct be_rx_page_info *page_info;
2020         u16 i, num_rcvd = rxcp->num_rcvd;
2021
2022         for (i = 0; i < num_rcvd; i++) {
2023                 page_info = get_rx_page_info(rxo);
2024                 put_page(page_info->page);
2025                 memset(page_info, 0, sizeof(*page_info));
2026         }
2027 }
2028
2029 /*
2030  * skb_fill_rx_data forms a complete skb for an ether frame
2031  * indicated by rxcp.
2032  */
2033 static void skb_fill_rx_data(struct be_rx_obj *rxo, struct sk_buff *skb,
2034                              struct be_rx_compl_info *rxcp)
2035 {
2036         struct be_rx_page_info *page_info;
2037         u16 i, j;
2038         u16 hdr_len, curr_frag_len, remaining;
2039         u8 *start;
2040
2041         page_info = get_rx_page_info(rxo);
2042         start = page_address(page_info->page) + page_info->page_offset;
2043         prefetch(start);
2044
2045         /* Copy data in the first descriptor of this completion */
2046         curr_frag_len = min(rxcp->pkt_size, rx_frag_size);
2047
2048         skb->len = curr_frag_len;
2049         if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
2050                 memcpy(skb->data, start, curr_frag_len);
2051                 /* Complete packet has now been moved to data */
2052                 put_page(page_info->page);
2053                 skb->data_len = 0;
2054                 skb->tail += curr_frag_len;
2055         } else {
2056                 hdr_len = ETH_HLEN;
2057                 memcpy(skb->data, start, hdr_len);
2058                 skb_shinfo(skb)->nr_frags = 1;
2059                 skb_frag_set_page(skb, 0, page_info->page);
2060                 skb_shinfo(skb)->frags[0].page_offset =
2061                                         page_info->page_offset + hdr_len;
2062                 skb_frag_size_set(&skb_shinfo(skb)->frags[0],
2063                                   curr_frag_len - hdr_len);
2064                 skb->data_len = curr_frag_len - hdr_len;
2065                 skb->truesize += rx_frag_size;
2066                 skb->tail += hdr_len;
2067         }
2068         page_info->page = NULL;
2069
2070         if (rxcp->pkt_size <= rx_frag_size) {
2071                 BUG_ON(rxcp->num_rcvd != 1);
2072                 return;
2073         }
2074
2075         /* More frags present for this completion */
2076         remaining = rxcp->pkt_size - curr_frag_len;
2077         for (i = 1, j = 0; i < rxcp->num_rcvd; i++) {
2078                 page_info = get_rx_page_info(rxo);
2079                 curr_frag_len = min(remaining, rx_frag_size);
2080
2081                 /* Coalesce all frags from the same physical page in one slot */
2082                 if (page_info->page_offset == 0) {
2083                         /* Fresh page */
2084                         j++;
2085                         skb_frag_set_page(skb, j, page_info->page);
2086                         skb_shinfo(skb)->frags[j].page_offset =
2087                                                         page_info->page_offset;
2088                         skb_frag_size_set(&skb_shinfo(skb)->frags[j], 0);
2089                         skb_shinfo(skb)->nr_frags++;
2090                 } else {
2091                         put_page(page_info->page);
2092                 }
2093
2094                 skb_frag_size_add(&skb_shinfo(skb)->frags[j], curr_frag_len);
2095                 skb->len += curr_frag_len;
2096                 skb->data_len += curr_frag_len;
2097                 skb->truesize += rx_frag_size;
2098                 remaining -= curr_frag_len;
2099                 page_info->page = NULL;
2100         }
2101         BUG_ON(j > MAX_SKB_FRAGS);
2102 }
2103
2104 /* Process the RX completion indicated by rxcp when GRO is disabled */
2105 static void be_rx_compl_process(struct be_rx_obj *rxo, struct napi_struct *napi,
2106                                 struct be_rx_compl_info *rxcp)
2107 {
2108         struct be_adapter *adapter = rxo->adapter;
2109         struct net_device *netdev = adapter->netdev;
2110         struct sk_buff *skb;
2111
2112         skb = netdev_alloc_skb_ip_align(netdev, BE_RX_SKB_ALLOC_SIZE);
2113         if (unlikely(!skb)) {
2114                 rx_stats(rxo)->rx_drops_no_skbs++;
2115                 be_rx_compl_discard(rxo, rxcp);
2116                 return;
2117         }
2118
2119         skb_fill_rx_data(rxo, skb, rxcp);
2120
2121         if (likely((netdev->features & NETIF_F_RXCSUM) && csum_passed(rxcp)))
2122                 skb->ip_summed = CHECKSUM_UNNECESSARY;
2123         else
2124                 skb_checksum_none_assert(skb);
2125
2126         skb->protocol = eth_type_trans(skb, netdev);
2127         skb_record_rx_queue(skb, rxo - &adapter->rx_obj[0]);
2128         if (netdev->features & NETIF_F_RXHASH)
2129                 skb_set_hash(skb, rxcp->rss_hash, PKT_HASH_TYPE_L3);
2130
2131         skb->csum_level = rxcp->tunneled;
2132         skb_mark_napi_id(skb, napi);
2133
2134         if (rxcp->vlanf)
2135                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rxcp->vlan_tag);
2136
2137         netif_receive_skb(skb);
2138 }
2139
2140 /* Process the RX completion indicated by rxcp when GRO is enabled */
2141 static void be_rx_compl_process_gro(struct be_rx_obj *rxo,
2142                                     struct napi_struct *napi,
2143                                     struct be_rx_compl_info *rxcp)
2144 {
2145         struct be_adapter *adapter = rxo->adapter;
2146         struct be_rx_page_info *page_info;
2147         struct sk_buff *skb = NULL;
2148         u16 remaining, curr_frag_len;
2149         u16 i, j;
2150
2151         skb = napi_get_frags(napi);
2152         if (!skb) {
2153                 be_rx_compl_discard(rxo, rxcp);
2154                 return;
2155         }
2156
2157         remaining = rxcp->pkt_size;
2158         for (i = 0, j = -1; i < rxcp->num_rcvd; i++) {
2159                 page_info = get_rx_page_info(rxo);
2160
2161                 curr_frag_len = min(remaining, rx_frag_size);
2162
2163                 /* Coalesce all frags from the same physical page in one slot */
2164                 if (i == 0 || page_info->page_offset == 0) {
2165                         /* First frag or Fresh page */
2166                         j++;
2167                         skb_frag_set_page(skb, j, page_info->page);
2168                         skb_shinfo(skb)->frags[j].page_offset =
2169                                                         page_info->page_offset;
2170                         skb_frag_size_set(&skb_shinfo(skb)->frags[j], 0);
2171                 } else {
2172                         put_page(page_info->page);
2173                 }
2174                 skb_frag_size_add(&skb_shinfo(skb)->frags[j], curr_frag_len);
2175                 skb->truesize += rx_frag_size;
2176                 remaining -= curr_frag_len;
2177                 memset(page_info, 0, sizeof(*page_info));
2178         }
2179         BUG_ON(j > MAX_SKB_FRAGS);
2180
2181         skb_shinfo(skb)->nr_frags = j + 1;
2182         skb->len = rxcp->pkt_size;
2183         skb->data_len = rxcp->pkt_size;
2184         skb->ip_summed = CHECKSUM_UNNECESSARY;
2185         skb_record_rx_queue(skb, rxo - &adapter->rx_obj[0]);
2186         if (adapter->netdev->features & NETIF_F_RXHASH)
2187                 skb_set_hash(skb, rxcp->rss_hash, PKT_HASH_TYPE_L3);
2188
2189         skb->csum_level = rxcp->tunneled;
2190
2191         if (rxcp->vlanf)
2192                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rxcp->vlan_tag);
2193
2194         napi_gro_frags(napi);
2195 }
2196
2197 static void be_parse_rx_compl_v1(struct be_eth_rx_compl *compl,
2198                                  struct be_rx_compl_info *rxcp)
2199 {
2200         rxcp->pkt_size = GET_RX_COMPL_V1_BITS(pktsize, compl);
2201         rxcp->vlanf = GET_RX_COMPL_V1_BITS(vtp, compl);
2202         rxcp->err = GET_RX_COMPL_V1_BITS(err, compl);
2203         rxcp->tcpf = GET_RX_COMPL_V1_BITS(tcpf, compl);
2204         rxcp->udpf = GET_RX_COMPL_V1_BITS(udpf, compl);
2205         rxcp->ip_csum = GET_RX_COMPL_V1_BITS(ipcksm, compl);
2206         rxcp->l4_csum = GET_RX_COMPL_V1_BITS(l4_cksm, compl);
2207         rxcp->ipv6 = GET_RX_COMPL_V1_BITS(ip_version, compl);
2208         rxcp->num_rcvd = GET_RX_COMPL_V1_BITS(numfrags, compl);
2209         rxcp->pkt_type = GET_RX_COMPL_V1_BITS(cast_enc, compl);
2210         rxcp->rss_hash = GET_RX_COMPL_V1_BITS(rsshash, compl);
2211         if (rxcp->vlanf) {
2212                 rxcp->qnq = GET_RX_COMPL_V1_BITS(qnq, compl);
2213                 rxcp->vlan_tag = GET_RX_COMPL_V1_BITS(vlan_tag, compl);
2214         }
2215         rxcp->port = GET_RX_COMPL_V1_BITS(port, compl);
2216         rxcp->tunneled =
2217                 GET_RX_COMPL_V1_BITS(tunneled, compl);
2218 }
2219
2220 static void be_parse_rx_compl_v0(struct be_eth_rx_compl *compl,
2221                                  struct be_rx_compl_info *rxcp)
2222 {
2223         rxcp->pkt_size = GET_RX_COMPL_V0_BITS(pktsize, compl);
2224         rxcp->vlanf = GET_RX_COMPL_V0_BITS(vtp, compl);
2225         rxcp->err = GET_RX_COMPL_V0_BITS(err, compl);
2226         rxcp->tcpf = GET_RX_COMPL_V0_BITS(tcpf, compl);
2227         rxcp->udpf = GET_RX_COMPL_V0_BITS(udpf, compl);
2228         rxcp->ip_csum = GET_RX_COMPL_V0_BITS(ipcksm, compl);
2229         rxcp->l4_csum = GET_RX_COMPL_V0_BITS(l4_cksm, compl);
2230         rxcp->ipv6 = GET_RX_COMPL_V0_BITS(ip_version, compl);
2231         rxcp->num_rcvd = GET_RX_COMPL_V0_BITS(numfrags, compl);
2232         rxcp->pkt_type = GET_RX_COMPL_V0_BITS(cast_enc, compl);
2233         rxcp->rss_hash = GET_RX_COMPL_V0_BITS(rsshash, compl);
2234         if (rxcp->vlanf) {
2235                 rxcp->qnq = GET_RX_COMPL_V0_BITS(qnq, compl);
2236                 rxcp->vlan_tag = GET_RX_COMPL_V0_BITS(vlan_tag, compl);
2237         }
2238         rxcp->port = GET_RX_COMPL_V0_BITS(port, compl);
2239         rxcp->ip_frag = GET_RX_COMPL_V0_BITS(ip_frag, compl);
2240 }
2241
2242 static struct be_rx_compl_info *be_rx_compl_get(struct be_rx_obj *rxo)
2243 {
2244         struct be_eth_rx_compl *compl = queue_tail_node(&rxo->cq);
2245         struct be_rx_compl_info *rxcp = &rxo->rxcp;
2246         struct be_adapter *adapter = rxo->adapter;
2247
2248         /* For checking the valid bit it is Ok to use either definition as the
2249          * valid bit is at the same position in both v0 and v1 Rx compl */
2250         if (compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] == 0)
2251                 return NULL;
2252
2253         rmb();
2254         be_dws_le_to_cpu(compl, sizeof(*compl));
2255
2256         if (adapter->be3_native)
2257                 be_parse_rx_compl_v1(compl, rxcp);
2258         else
2259                 be_parse_rx_compl_v0(compl, rxcp);
2260
2261         if (rxcp->ip_frag)
2262                 rxcp->l4_csum = 0;
2263
2264         if (rxcp->vlanf) {
2265                 /* In QNQ modes, if qnq bit is not set, then the packet was
2266                  * tagged only with the transparent outer vlan-tag and must
2267                  * not be treated as a vlan packet by host
2268                  */
2269                 if (be_is_qnq_mode(adapter) && !rxcp->qnq)
2270                         rxcp->vlanf = 0;
2271
2272                 if (!lancer_chip(adapter))
2273                         rxcp->vlan_tag = swab16(rxcp->vlan_tag);
2274
2275                 if (adapter->pvid == (rxcp->vlan_tag & VLAN_VID_MASK) &&
2276                     !test_bit(rxcp->vlan_tag, adapter->vids))
2277                         rxcp->vlanf = 0;
2278         }
2279
2280         /* As the compl has been parsed, reset it; we wont touch it again */
2281         compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] = 0;
2282
2283         queue_tail_inc(&rxo->cq);
2284         return rxcp;
2285 }
2286
2287 static inline struct page *be_alloc_pages(u32 size, gfp_t gfp)
2288 {
2289         u32 order = get_order(size);
2290
2291         if (order > 0)
2292                 gfp |= __GFP_COMP;
2293         return  alloc_pages(gfp, order);
2294 }
2295
2296 /*
2297  * Allocate a page, split it to fragments of size rx_frag_size and post as
2298  * receive buffers to BE
2299  */
2300 static void be_post_rx_frags(struct be_rx_obj *rxo, gfp_t gfp, u32 frags_needed)
2301 {
2302         struct be_adapter *adapter = rxo->adapter;
2303         struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
2304         struct be_queue_info *rxq = &rxo->q;
2305         struct page *pagep = NULL;
2306         struct device *dev = &adapter->pdev->dev;
2307         struct be_eth_rx_d *rxd;
2308         u64 page_dmaaddr = 0, frag_dmaaddr;
2309         u32 posted, page_offset = 0, notify = 0;
2310
2311         page_info = &rxo->page_info_tbl[rxq->head];
2312         for (posted = 0; posted < frags_needed && !page_info->page; posted++) {
2313                 if (!pagep) {
2314                         pagep = be_alloc_pages(adapter->big_page_size, gfp);
2315                         if (unlikely(!pagep)) {
2316                                 rx_stats(rxo)->rx_post_fail++;
2317                                 break;
2318                         }
2319                         page_dmaaddr = dma_map_page(dev, pagep, 0,
2320                                                     adapter->big_page_size,
2321                                                     DMA_FROM_DEVICE);
2322                         if (dma_mapping_error(dev, page_dmaaddr)) {
2323                                 put_page(pagep);
2324                                 pagep = NULL;
2325                                 adapter->drv_stats.dma_map_errors++;
2326                                 break;
2327                         }
2328                         page_offset = 0;
2329                 } else {
2330                         get_page(pagep);
2331                         page_offset += rx_frag_size;
2332                 }
2333                 page_info->page_offset = page_offset;
2334                 page_info->page = pagep;
2335
2336                 rxd = queue_head_node(rxq);
2337                 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
2338                 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
2339                 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
2340
2341                 /* Any space left in the current big page for another frag? */
2342                 if ((page_offset + rx_frag_size + rx_frag_size) >
2343                                         adapter->big_page_size) {
2344                         pagep = NULL;
2345                         page_info->last_frag = true;
2346                         dma_unmap_addr_set(page_info, bus, page_dmaaddr);
2347                 } else {
2348                         dma_unmap_addr_set(page_info, bus, frag_dmaaddr);
2349                 }
2350
2351                 prev_page_info = page_info;
2352                 queue_head_inc(rxq);
2353                 page_info = &rxo->page_info_tbl[rxq->head];
2354         }
2355
2356         /* Mark the last frag of a page when we break out of the above loop
2357          * with no more slots available in the RXQ
2358          */
2359         if (pagep) {
2360                 prev_page_info->last_frag = true;
2361                 dma_unmap_addr_set(prev_page_info, bus, page_dmaaddr);
2362         }
2363
2364         if (posted) {
2365                 atomic_add(posted, &rxq->used);
2366                 if (rxo->rx_post_starved)
2367                         rxo->rx_post_starved = false;
2368                 do {
2369                         notify = min(MAX_NUM_POST_ERX_DB, posted);
2370                         be_rxq_notify(adapter, rxq->id, notify);
2371                         posted -= notify;
2372                 } while (posted);
2373         } else if (atomic_read(&rxq->used) == 0) {
2374                 /* Let be_worker replenish when memory is available */
2375                 rxo->rx_post_starved = true;
2376         }
2377 }
2378
2379 static struct be_tx_compl_info *be_tx_compl_get(struct be_tx_obj *txo)
2380 {
2381         struct be_queue_info *tx_cq = &txo->cq;
2382         struct be_tx_compl_info *txcp = &txo->txcp;
2383         struct be_eth_tx_compl *compl = queue_tail_node(tx_cq);
2384
2385         if (compl->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
2386                 return NULL;
2387
2388         /* Ensure load ordering of valid bit dword and other dwords below */
2389         rmb();
2390         be_dws_le_to_cpu(compl, sizeof(*compl));
2391
2392         txcp->status = GET_TX_COMPL_BITS(status, compl);
2393         txcp->end_index = GET_TX_COMPL_BITS(wrb_index, compl);
2394
2395         compl->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
2396         queue_tail_inc(tx_cq);
2397         return txcp;
2398 }
2399
2400 static u16 be_tx_compl_process(struct be_adapter *adapter,
2401                                struct be_tx_obj *txo, u16 last_index)
2402 {
2403         struct sk_buff **sent_skbs = txo->sent_skb_list;
2404         struct be_queue_info *txq = &txo->q;
2405         u16 frag_index, num_wrbs = 0;
2406         struct sk_buff *skb = NULL;
2407         bool unmap_skb_hdr = false;
2408         struct be_eth_wrb *wrb;
2409
2410         do {
2411                 if (sent_skbs[txq->tail]) {
2412                         /* Free skb from prev req */
2413                         if (skb)
2414                                 dev_consume_skb_any(skb);
2415                         skb = sent_skbs[txq->tail];
2416                         sent_skbs[txq->tail] = NULL;
2417                         queue_tail_inc(txq);  /* skip hdr wrb */
2418                         num_wrbs++;
2419                         unmap_skb_hdr = true;
2420                 }
2421                 wrb = queue_tail_node(txq);
2422                 frag_index = txq->tail;
2423                 unmap_tx_frag(&adapter->pdev->dev, wrb,
2424                               (unmap_skb_hdr && skb_headlen(skb)));
2425                 unmap_skb_hdr = false;
2426                 queue_tail_inc(txq);
2427                 num_wrbs++;
2428         } while (frag_index != last_index);
2429         dev_consume_skb_any(skb);
2430
2431         return num_wrbs;
2432 }
2433
2434 /* Return the number of events in the event queue */
2435 static inline int events_get(struct be_eq_obj *eqo)
2436 {
2437         struct be_eq_entry *eqe;
2438         int num = 0;
2439
2440         do {
2441                 eqe = queue_tail_node(&eqo->q);
2442                 if (eqe->evt == 0)
2443                         break;
2444
2445                 rmb();
2446                 eqe->evt = 0;
2447                 num++;
2448                 queue_tail_inc(&eqo->q);
2449         } while (true);
2450
2451         return num;
2452 }
2453
2454 /* Leaves the EQ is disarmed state */
2455 static void be_eq_clean(struct be_eq_obj *eqo)
2456 {
2457         int num = events_get(eqo);
2458
2459         be_eq_notify(eqo->adapter, eqo->q.id, false, true, num, 0);
2460 }
2461
2462 /* Free posted rx buffers that were not used */
2463 static void be_rxq_clean(struct be_rx_obj *rxo)
2464 {
2465         struct be_queue_info *rxq = &rxo->q;
2466         struct be_rx_page_info *page_info;
2467
2468         while (atomic_read(&rxq->used) > 0) {
2469                 page_info = get_rx_page_info(rxo);
2470                 put_page(page_info->page);
2471                 memset(page_info, 0, sizeof(*page_info));
2472         }
2473         BUG_ON(atomic_read(&rxq->used));
2474         rxq->tail = 0;
2475         rxq->head = 0;
2476 }
2477
2478 static void be_rx_cq_clean(struct be_rx_obj *rxo)
2479 {
2480         struct be_queue_info *rx_cq = &rxo->cq;
2481         struct be_rx_compl_info *rxcp;
2482         struct be_adapter *adapter = rxo->adapter;
2483         int flush_wait = 0;
2484
2485         /* Consume pending rx completions.
2486          * Wait for the flush completion (identified by zero num_rcvd)
2487          * to arrive. Notify CQ even when there are no more CQ entries
2488          * for HW to flush partially coalesced CQ entries.
2489          * In Lancer, there is no need to wait for flush compl.
2490          */
2491         for (;;) {
2492                 rxcp = be_rx_compl_get(rxo);
2493                 if (!rxcp) {
2494                         if (lancer_chip(adapter))
2495                                 break;
2496
2497                         if (flush_wait++ > 50 ||
2498                             be_check_error(adapter,
2499                                            BE_ERROR_HW)) {
2500                                 dev_warn(&adapter->pdev->dev,
2501                                          "did not receive flush compl\n");
2502                                 break;
2503                         }
2504                         be_cq_notify(adapter, rx_cq->id, true, 0);
2505                         mdelay(1);
2506                 } else {
2507                         be_rx_compl_discard(rxo, rxcp);
2508                         be_cq_notify(adapter, rx_cq->id, false, 1);
2509                         if (rxcp->num_rcvd == 0)
2510                                 break;
2511                 }
2512         }
2513
2514         /* After cleanup, leave the CQ in unarmed state */
2515         be_cq_notify(adapter, rx_cq->id, false, 0);
2516 }
2517
2518 static void be_tx_compl_clean(struct be_adapter *adapter)
2519 {
2520         u16 end_idx, notified_idx, cmpl = 0, timeo = 0, num_wrbs = 0;
2521         struct device *dev = &adapter->pdev->dev;
2522         struct be_tx_compl_info *txcp;
2523         struct be_queue_info *txq;
2524         struct be_tx_obj *txo;
2525         int i, pending_txqs;
2526
2527         /* Stop polling for compls when HW has been silent for 10ms */
2528         do {
2529                 pending_txqs = adapter->num_tx_qs;
2530
2531                 for_all_tx_queues(adapter, txo, i) {
2532                         cmpl = 0;
2533                         num_wrbs = 0;
2534                         txq = &txo->q;
2535                         while ((txcp = be_tx_compl_get(txo))) {
2536                                 num_wrbs +=
2537                                         be_tx_compl_process(adapter, txo,
2538                                                             txcp->end_index);
2539                                 cmpl++;
2540                         }
2541                         if (cmpl) {
2542                                 be_cq_notify(adapter, txo->cq.id, false, cmpl);
2543                                 atomic_sub(num_wrbs, &txq->used);
2544                                 timeo = 0;
2545                         }
2546                         if (!be_is_tx_compl_pending(txo))
2547                                 pending_txqs--;
2548                 }
2549
2550                 if (pending_txqs == 0 || ++timeo > 10 ||
2551                     be_check_error(adapter, BE_ERROR_HW))
2552                         break;
2553
2554                 mdelay(1);
2555         } while (true);
2556
2557         /* Free enqueued TX that was never notified to HW */
2558         for_all_tx_queues(adapter, txo, i) {
2559                 txq = &txo->q;
2560
2561                 if (atomic_read(&txq->used)) {
2562                         dev_info(dev, "txq%d: cleaning %d pending tx-wrbs\n",
2563                                  i, atomic_read(&txq->used));
2564                         notified_idx = txq->tail;
2565                         end_idx = txq->tail;
2566                         index_adv(&end_idx, atomic_read(&txq->used) - 1,
2567                                   txq->len);
2568                         /* Use the tx-compl process logic to handle requests
2569                          * that were not sent to the HW.
2570                          */
2571                         num_wrbs = be_tx_compl_process(adapter, txo, end_idx);
2572                         atomic_sub(num_wrbs, &txq->used);
2573                         BUG_ON(atomic_read(&txq->used));
2574                         txo->pend_wrb_cnt = 0;
2575                         /* Since hw was never notified of these requests,
2576                          * reset TXQ indices
2577                          */
2578                         txq->head = notified_idx;
2579                         txq->tail = notified_idx;
2580                 }
2581         }
2582 }
2583
2584 static void be_evt_queues_destroy(struct be_adapter *adapter)
2585 {
2586         struct be_eq_obj *eqo;
2587         int i;
2588
2589         for_all_evt_queues(adapter, eqo, i) {
2590                 if (eqo->q.created) {
2591                         be_eq_clean(eqo);
2592                         be_cmd_q_destroy(adapter, &eqo->q, QTYPE_EQ);
2593                         napi_hash_del(&eqo->napi);
2594                         netif_napi_del(&eqo->napi);
2595                         free_cpumask_var(eqo->affinity_mask);
2596                 }
2597                 be_queue_free(adapter, &eqo->q);
2598         }
2599 }
2600
2601 static int be_evt_queues_create(struct be_adapter *adapter)
2602 {
2603         struct be_queue_info *eq;
2604         struct be_eq_obj *eqo;
2605         struct be_aic_obj *aic;
2606         int i, rc;
2607
2608         adapter->num_evt_qs = min_t(u16, num_irqs(adapter),
2609                                     adapter->cfg_num_qs);
2610
2611         for_all_evt_queues(adapter, eqo, i) {
2612                 int numa_node = dev_to_node(&adapter->pdev->dev);
2613
2614                 aic = &adapter->aic_obj[i];
2615                 eqo->adapter = adapter;
2616                 eqo->idx = i;
2617                 aic->max_eqd = BE_MAX_EQD;
2618                 aic->enable = true;
2619
2620                 eq = &eqo->q;
2621                 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
2622                                     sizeof(struct be_eq_entry));
2623                 if (rc)
2624                         return rc;
2625
2626                 rc = be_cmd_eq_create(adapter, eqo);
2627                 if (rc)
2628                         return rc;
2629
2630                 if (!zalloc_cpumask_var(&eqo->affinity_mask, GFP_KERNEL))
2631                         return -ENOMEM;
2632                 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
2633                                 eqo->affinity_mask);
2634                 netif_napi_add(adapter->netdev, &eqo->napi, be_poll,
2635                                BE_NAPI_WEIGHT);
2636         }
2637         return 0;
2638 }
2639
2640 static void be_mcc_queues_destroy(struct be_adapter *adapter)
2641 {
2642         struct be_queue_info *q;
2643
2644         q = &adapter->mcc_obj.q;
2645         if (q->created)
2646                 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
2647         be_queue_free(adapter, q);
2648
2649         q = &adapter->mcc_obj.cq;
2650         if (q->created)
2651                 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2652         be_queue_free(adapter, q);
2653 }
2654
2655 /* Must be called only after TX qs are created as MCC shares TX EQ */
2656 static int be_mcc_queues_create(struct be_adapter *adapter)
2657 {
2658         struct be_queue_info *q, *cq;
2659
2660         cq = &adapter->mcc_obj.cq;
2661         if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
2662                            sizeof(struct be_mcc_compl)))
2663                 goto err;
2664
2665         /* Use the default EQ for MCC completions */
2666         if (be_cmd_cq_create(adapter, cq, &mcc_eqo(adapter)->q, true, 0))
2667                 goto mcc_cq_free;
2668
2669         q = &adapter->mcc_obj.q;
2670         if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
2671                 goto mcc_cq_destroy;
2672
2673         if (be_cmd_mccq_create(adapter, q, cq))
2674                 goto mcc_q_free;
2675
2676         return 0;
2677
2678 mcc_q_free:
2679         be_queue_free(adapter, q);
2680 mcc_cq_destroy:
2681         be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
2682 mcc_cq_free:
2683         be_queue_free(adapter, cq);
2684 err:
2685         return -1;
2686 }
2687
2688 static void be_tx_queues_destroy(struct be_adapter *adapter)
2689 {
2690         struct be_queue_info *q;
2691         struct be_tx_obj *txo;
2692         u8 i;
2693
2694         for_all_tx_queues(adapter, txo, i) {
2695                 q = &txo->q;
2696                 if (q->created)
2697                         be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
2698                 be_queue_free(adapter, q);
2699
2700                 q = &txo->cq;
2701                 if (q->created)
2702                         be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2703                 be_queue_free(adapter, q);
2704         }
2705 }
2706
2707 static int be_tx_qs_create(struct be_adapter *adapter)
2708 {
2709         struct be_queue_info *cq;
2710         struct be_tx_obj *txo;
2711         struct be_eq_obj *eqo;
2712         int status, i;
2713
2714         adapter->num_tx_qs = min(adapter->num_evt_qs, be_max_txqs(adapter));
2715
2716         for_all_tx_queues(adapter, txo, i) {
2717                 cq = &txo->cq;
2718                 status = be_queue_alloc(adapter, cq, TX_CQ_LEN,
2719                                         sizeof(struct be_eth_tx_compl));
2720                 if (status)
2721                         return status;
2722
2723                 u64_stats_init(&txo->stats.sync);
2724                 u64_stats_init(&txo->stats.sync_compl);
2725
2726                 /* If num_evt_qs is less than num_tx_qs, then more than
2727                  * one txq share an eq
2728                  */
2729                 eqo = &adapter->eq_obj[i % adapter->num_evt_qs];
2730                 status = be_cmd_cq_create(adapter, cq, &eqo->q, false, 3);
2731                 if (status)
2732                         return status;
2733
2734                 status = be_queue_alloc(adapter, &txo->q, TX_Q_LEN,
2735                                         sizeof(struct be_eth_wrb));
2736                 if (status)
2737                         return status;
2738
2739                 status = be_cmd_txq_create(adapter, txo);
2740                 if (status)
2741                         return status;
2742
2743                 netif_set_xps_queue(adapter->netdev, eqo->affinity_mask,
2744                                     eqo->idx);
2745         }
2746
2747         dev_info(&adapter->pdev->dev, "created %d TX queue(s)\n",
2748                  adapter->num_tx_qs);
2749         return 0;
2750 }
2751
2752 static void be_rx_cqs_destroy(struct be_adapter *adapter)
2753 {
2754         struct be_queue_info *q;
2755         struct be_rx_obj *rxo;
2756         int i;
2757
2758         for_all_rx_queues(adapter, rxo, i) {
2759                 q = &rxo->cq;
2760                 if (q->created)
2761                         be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2762                 be_queue_free(adapter, q);
2763         }
2764 }
2765
2766 static int be_rx_cqs_create(struct be_adapter *adapter)
2767 {
2768         struct be_queue_info *eq, *cq;
2769         struct be_rx_obj *rxo;
2770         int rc, i;
2771
2772         /* We can create as many RSS rings as there are EQs. */
2773         adapter->num_rss_qs = adapter->num_evt_qs;
2774
2775         /* We'll use RSS only if atleast 2 RSS rings are supported. */
2776         if (adapter->num_rss_qs <= 1)
2777                 adapter->num_rss_qs = 0;
2778
2779         adapter->num_rx_qs = adapter->num_rss_qs + adapter->need_def_rxq;
2780
2781         /* When the interface is not capable of RSS rings (and there is no
2782          * need to create a default RXQ) we'll still need one RXQ
2783          */
2784         if (adapter->num_rx_qs == 0)
2785                 adapter->num_rx_qs = 1;
2786
2787         adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
2788         for_all_rx_queues(adapter, rxo, i) {
2789                 rxo->adapter = adapter;
2790                 cq = &rxo->cq;
2791                 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
2792                                     sizeof(struct be_eth_rx_compl));
2793                 if (rc)
2794                         return rc;
2795
2796                 u64_stats_init(&rxo->stats.sync);
2797                 eq = &adapter->eq_obj[i % adapter->num_evt_qs].q;
2798                 rc = be_cmd_cq_create(adapter, cq, eq, false, 3);
2799                 if (rc)
2800                         return rc;
2801         }
2802
2803         dev_info(&adapter->pdev->dev,
2804                  "created %d RX queue(s)\n", adapter->num_rx_qs);
2805         return 0;
2806 }
2807
2808 static irqreturn_t be_intx(int irq, void *dev)
2809 {
2810         struct be_eq_obj *eqo = dev;
2811         struct be_adapter *adapter = eqo->adapter;
2812         int num_evts = 0;
2813
2814         /* IRQ is not expected when NAPI is scheduled as the EQ
2815          * will not be armed.
2816          * But, this can happen on Lancer INTx where it takes
2817          * a while to de-assert INTx or in BE2 where occasionaly
2818          * an interrupt may be raised even when EQ is unarmed.
2819          * If NAPI is already scheduled, then counting & notifying
2820          * events will orphan them.
2821          */
2822         if (napi_schedule_prep(&eqo->napi)) {
2823                 num_evts = events_get(eqo);
2824                 __napi_schedule(&eqo->napi);
2825                 if (num_evts)
2826                         eqo->spurious_intr = 0;
2827         }
2828         be_eq_notify(adapter, eqo->q.id, false, true, num_evts, 0);
2829
2830         /* Return IRQ_HANDLED only for the the first spurious intr
2831          * after a valid intr to stop the kernel from branding
2832          * this irq as a bad one!
2833          */
2834         if (num_evts || eqo->spurious_intr++ == 0)
2835                 return IRQ_HANDLED;
2836         else
2837                 return IRQ_NONE;
2838 }
2839
2840 static irqreturn_t be_msix(int irq, void *dev)
2841 {
2842         struct be_eq_obj *eqo = dev;
2843
2844         be_eq_notify(eqo->adapter, eqo->q.id, false, true, 0, 0);
2845         napi_schedule(&eqo->napi);
2846         return IRQ_HANDLED;
2847 }
2848
2849 static inline bool do_gro(struct be_rx_compl_info *rxcp)
2850 {
2851         return (rxcp->tcpf && !rxcp->err && rxcp->l4_csum) ? true : false;
2852 }
2853
2854 static int be_process_rx(struct be_rx_obj *rxo, struct napi_struct *napi,
2855                          int budget, int polling)
2856 {
2857         struct be_adapter *adapter = rxo->adapter;
2858         struct be_queue_info *rx_cq = &rxo->cq;
2859         struct be_rx_compl_info *rxcp;
2860         u32 work_done;
2861         u32 frags_consumed = 0;
2862
2863         for (work_done = 0; work_done < budget; work_done++) {
2864                 rxcp = be_rx_compl_get(rxo);
2865                 if (!rxcp)
2866                         break;
2867
2868                 /* Is it a flush compl that has no data */
2869                 if (unlikely(rxcp->num_rcvd == 0))
2870                         goto loop_continue;
2871
2872                 /* Discard compl with partial DMA Lancer B0 */
2873                 if (unlikely(!rxcp->pkt_size)) {
2874                         be_rx_compl_discard(rxo, rxcp);
2875                         goto loop_continue;
2876                 }
2877
2878                 /* On BE drop pkts that arrive due to imperfect filtering in
2879                  * promiscuous mode on some skews
2880                  */
2881                 if (unlikely(rxcp->port != adapter->port_num &&
2882                              !lancer_chip(adapter))) {
2883                         be_rx_compl_discard(rxo, rxcp);
2884                         goto loop_continue;
2885                 }
2886
2887                 /* Don't do gro when we're busy_polling */
2888                 if (do_gro(rxcp) && polling != BUSY_POLLING)
2889                         be_rx_compl_process_gro(rxo, napi, rxcp);
2890                 else
2891                         be_rx_compl_process(rxo, napi, rxcp);
2892
2893 loop_continue:
2894                 frags_consumed += rxcp->num_rcvd;
2895                 be_rx_stats_update(rxo, rxcp);
2896         }
2897
2898         if (work_done) {
2899                 be_cq_notify(adapter, rx_cq->id, true, work_done);
2900
2901                 /* When an rx-obj gets into post_starved state, just
2902                  * let be_worker do the posting.
2903                  */
2904                 if (atomic_read(&rxo->q.used) < RX_FRAGS_REFILL_WM &&
2905                     !rxo->rx_post_starved)
2906                         be_post_rx_frags(rxo, GFP_ATOMIC,
2907                                          max_t(u32, MAX_RX_POST,
2908                                                frags_consumed));
2909         }
2910
2911         return work_done;
2912 }
2913
2914 static inline void be_update_tx_err(struct be_tx_obj *txo, u8 status)
2915 {
2916         switch (status) {
2917         case BE_TX_COMP_HDR_PARSE_ERR:
2918                 tx_stats(txo)->tx_hdr_parse_err++;
2919                 break;
2920         case BE_TX_COMP_NDMA_ERR:
2921                 tx_stats(txo)->tx_dma_err++;
2922                 break;
2923         case BE_TX_COMP_ACL_ERR:
2924                 tx_stats(txo)->tx_spoof_check_err++;
2925                 break;
2926         }
2927 }
2928
2929 static inline void lancer_update_tx_err(struct be_tx_obj *txo, u8 status)
2930 {
2931         switch (status) {
2932         case LANCER_TX_COMP_LSO_ERR:
2933                 tx_stats(txo)->tx_tso_err++;
2934                 break;
2935         case LANCER_TX_COMP_HSW_DROP_MAC_ERR:
2936         case LANCER_TX_COMP_HSW_DROP_VLAN_ERR:
2937                 tx_stats(txo)->tx_spoof_check_err++;
2938                 break;
2939         case LANCER_TX_COMP_QINQ_ERR:
2940                 tx_stats(txo)->tx_qinq_err++;
2941                 break;
2942         case LANCER_TX_COMP_PARITY_ERR:
2943                 tx_stats(txo)->tx_internal_parity_err++;
2944                 break;
2945         case LANCER_TX_COMP_DMA_ERR:
2946                 tx_stats(txo)->tx_dma_err++;
2947                 break;
2948         }
2949 }
2950
2951 static void be_process_tx(struct be_adapter *adapter, struct be_tx_obj *txo,
2952                           int idx)
2953 {
2954         int num_wrbs = 0, work_done = 0;
2955         struct be_tx_compl_info *txcp;
2956
2957         while ((txcp = be_tx_compl_get(txo))) {
2958                 num_wrbs += be_tx_compl_process(adapter, txo, txcp->end_index);
2959                 work_done++;
2960
2961                 if (txcp->status) {
2962                         if (lancer_chip(adapter))
2963                                 lancer_update_tx_err(txo, txcp->status);
2964                         else
2965                                 be_update_tx_err(txo, txcp->status);
2966                 }
2967         }
2968
2969         if (work_done) {
2970                 be_cq_notify(adapter, txo->cq.id, true, work_done);
2971                 atomic_sub(num_wrbs, &txo->q.used);
2972
2973                 /* As Tx wrbs have been freed up, wake up netdev queue
2974                  * if it was stopped due to lack of tx wrbs.  */
2975                 if (__netif_subqueue_stopped(adapter->netdev, idx) &&
2976                     be_can_txq_wake(txo)) {
2977                         netif_wake_subqueue(adapter->netdev, idx);
2978                 }
2979
2980                 u64_stats_update_begin(&tx_stats(txo)->sync_compl);
2981                 tx_stats(txo)->tx_compl += work_done;
2982                 u64_stats_update_end(&tx_stats(txo)->sync_compl);
2983         }
2984 }
2985
2986 #ifdef CONFIG_NET_RX_BUSY_POLL
2987 static inline bool be_lock_napi(struct be_eq_obj *eqo)
2988 {
2989         bool status = true;
2990
2991         spin_lock(&eqo->lock); /* BH is already disabled */
2992         if (eqo->state & BE_EQ_LOCKED) {
2993                 WARN_ON(eqo->state & BE_EQ_NAPI);
2994                 eqo->state |= BE_EQ_NAPI_YIELD;
2995                 status = false;
2996         } else {
2997                 eqo->state = BE_EQ_NAPI;
2998         }
2999         spin_unlock(&eqo->lock);
3000         return status;
3001 }
3002
3003 static inline void be_unlock_napi(struct be_eq_obj *eqo)
3004 {
3005         spin_lock(&eqo->lock); /* BH is already disabled */
3006
3007         WARN_ON(eqo->state & (BE_EQ_POLL | BE_EQ_NAPI_YIELD));
3008         eqo->state = BE_EQ_IDLE;
3009
3010         spin_unlock(&eqo->lock);
3011 }
3012
3013 static inline bool be_lock_busy_poll(struct be_eq_obj *eqo)
3014 {
3015         bool status = true;
3016
3017         spin_lock_bh(&eqo->lock);
3018         if (eqo->state & BE_EQ_LOCKED) {
3019                 eqo->state |= BE_EQ_POLL_YIELD;
3020                 status = false;
3021         } else {
3022                 eqo->state |= BE_EQ_POLL;
3023         }
3024         spin_unlock_bh(&eqo->lock);
3025         return status;
3026 }
3027
3028 static inline void be_unlock_busy_poll(struct be_eq_obj *eqo)
3029 {
3030         spin_lock_bh(&eqo->lock);
3031
3032         WARN_ON(eqo->state & (BE_EQ_NAPI));
3033         eqo->state = BE_EQ_IDLE;
3034
3035         spin_unlock_bh(&eqo->lock);
3036 }
3037
3038 static inline void be_enable_busy_poll(struct be_eq_obj *eqo)
3039 {
3040         spin_lock_init(&eqo->lock);
3041         eqo->state = BE_EQ_IDLE;
3042 }
3043
3044 static inline void be_disable_busy_poll(struct be_eq_obj *eqo)
3045 {
3046         local_bh_disable();
3047
3048         /* It's enough to just acquire napi lock on the eqo to stop
3049          * be_busy_poll() from processing any queueus.
3050          */
3051         while (!be_lock_napi(eqo))
3052                 mdelay(1);
3053
3054         local_bh_enable();
3055 }
3056
3057 #else /* CONFIG_NET_RX_BUSY_POLL */
3058
3059 static inline bool be_lock_napi(struct be_eq_obj *eqo)
3060 {
3061         return true;
3062 }
3063
3064 static inline void be_unlock_napi(struct be_eq_obj *eqo)
3065 {
3066 }
3067
3068 static inline bool be_lock_busy_poll(struct be_eq_obj *eqo)
3069 {
3070         return false;
3071 }
3072
3073 static inline void be_unlock_busy_poll(struct be_eq_obj *eqo)
3074 {
3075 }
3076
3077 static inline void be_enable_busy_poll(struct be_eq_obj *eqo)
3078 {
3079 }
3080
3081 static inline void be_disable_busy_poll(struct be_eq_obj *eqo)
3082 {
3083 }
3084 #endif /* CONFIG_NET_RX_BUSY_POLL */
3085
3086 int be_poll(struct napi_struct *napi, int budget)
3087 {
3088         struct be_eq_obj *eqo = container_of(napi, struct be_eq_obj, napi);
3089         struct be_adapter *adapter = eqo->adapter;
3090         int max_work = 0, work, i, num_evts;
3091         struct be_rx_obj *rxo;
3092         struct be_tx_obj *txo;
3093         u32 mult_enc = 0;
3094
3095         num_evts = events_get(eqo);
3096
3097         for_all_tx_queues_on_eq(adapter, eqo, txo, i)
3098                 be_process_tx(adapter, txo, i);
3099
3100         if (be_lock_napi(eqo)) {
3101                 /* This loop will iterate twice for EQ0 in which
3102                  * completions of the last RXQ (default one) are also processed
3103                  * For other EQs the loop iterates only once
3104                  */
3105                 for_all_rx_queues_on_eq(adapter, eqo, rxo, i) {
3106                         work = be_process_rx(rxo, napi, budget, NAPI_POLLING);
3107                         max_work = max(work, max_work);
3108                 }
3109                 be_unlock_napi(eqo);
3110         } else {
3111                 max_work = budget;
3112         }
3113
3114         if (is_mcc_eqo(eqo))
3115                 be_process_mcc(adapter);
3116
3117         if (max_work < budget) {
3118                 napi_complete(napi);
3119
3120                 /* Skyhawk EQ_DB has a provision to set the rearm to interrupt
3121                  * delay via a delay multiplier encoding value
3122                  */
3123                 if (skyhawk_chip(adapter))
3124                         mult_enc = be_get_eq_delay_mult_enc(eqo);
3125
3126                 be_eq_notify(adapter, eqo->q.id, true, false, num_evts,
3127                              mult_enc);
3128         } else {
3129                 /* As we'll continue in polling mode, count and clear events */
3130                 be_eq_notify(adapter, eqo->q.id, false, false, num_evts, 0);
3131         }
3132         return max_work;
3133 }
3134
3135 #ifdef CONFIG_NET_RX_BUSY_POLL
3136 static int be_busy_poll(struct napi_struct *napi)
3137 {
3138         struct be_eq_obj *eqo = container_of(napi, struct be_eq_obj, napi);
3139         struct be_adapter *adapter = eqo->adapter;
3140         struct be_rx_obj *rxo;
3141         int i, work = 0;
3142
3143         if (!be_lock_busy_poll(eqo))
3144                 return LL_FLUSH_BUSY;
3145
3146         for_all_rx_queues_on_eq(adapter, eqo, rxo, i) {
3147                 work = be_process_rx(rxo, napi, 4, BUSY_POLLING);
3148                 if (work)
3149                         break;
3150         }
3151
3152         be_unlock_busy_poll(eqo);
3153         return work;
3154 }
3155 #endif
3156
3157 void be_detect_error(struct be_adapter *adapter)
3158 {
3159         u32 ue_lo = 0, ue_hi = 0, ue_lo_mask = 0, ue_hi_mask = 0;
3160         u32 sliport_status = 0, sliport_err1 = 0, sliport_err2 = 0;
3161         u32 i;
3162         struct device *dev = &adapter->pdev->dev;
3163
3164         if (be_check_error(adapter, BE_ERROR_HW))
3165                 return;
3166
3167         if (lancer_chip(adapter)) {
3168                 sliport_status = ioread32(adapter->db + SLIPORT_STATUS_OFFSET);
3169                 if (sliport_status & SLIPORT_STATUS_ERR_MASK) {
3170                         be_set_error(adapter, BE_ERROR_UE);
3171                         sliport_err1 = ioread32(adapter->db +
3172                                                 SLIPORT_ERROR1_OFFSET);
3173                         sliport_err2 = ioread32(adapter->db +
3174                                                 SLIPORT_ERROR2_OFFSET);
3175                         /* Do not log error messages if its a FW reset */
3176                         if (sliport_err1 == SLIPORT_ERROR_FW_RESET1 &&
3177                             sliport_err2 == SLIPORT_ERROR_FW_RESET2) {
3178                                 dev_info(dev, "Firmware update in progress\n");
3179                         } else {
3180                                 dev_err(dev, "Error detected in the card\n");
3181                                 dev_err(dev, "ERR: sliport status 0x%x\n",
3182                                         sliport_status);
3183                                 dev_err(dev, "ERR: sliport error1 0x%x\n",
3184                                         sliport_err1);
3185                                 dev_err(dev, "ERR: sliport error2 0x%x\n",
3186                                         sliport_err2);
3187                         }
3188                 }
3189         } else {
3190                 ue_lo = ioread32(adapter->pcicfg + PCICFG_UE_STATUS_LOW);
3191                 ue_hi = ioread32(adapter->pcicfg + PCICFG_UE_STATUS_HIGH);
3192                 ue_lo_mask = ioread32(adapter->pcicfg +
3193                                       PCICFG_UE_STATUS_LOW_MASK);
3194                 ue_hi_mask = ioread32(adapter->pcicfg +
3195                                       PCICFG_UE_STATUS_HI_MASK);
3196
3197                 ue_lo = (ue_lo & ~ue_lo_mask);
3198                 ue_hi = (ue_hi & ~ue_hi_mask);
3199
3200                 /* On certain platforms BE hardware can indicate spurious UEs.
3201                  * Allow HW to stop working completely in case of a real UE.
3202                  * Hence not setting the hw_error for UE detection.
3203                  */
3204
3205                 if (ue_lo || ue_hi) {
3206                         dev_err(dev,
3207                                 "Unrecoverable Error detected in the adapter");
3208                         dev_err(dev, "Please reboot server to recover");
3209                         if (skyhawk_chip(adapter))
3210                                 be_set_error(adapter, BE_ERROR_UE);
3211
3212                         for (i = 0; ue_lo; ue_lo >>= 1, i++) {
3213                                 if (ue_lo & 1)
3214                                         dev_err(dev, "UE: %s bit set\n",
3215                                                 ue_status_low_desc[i]);
3216                         }
3217                         for (i = 0; ue_hi; ue_hi >>= 1, i++) {
3218                                 if (ue_hi & 1)
3219                                         dev_err(dev, "UE: %s bit set\n",
3220                                                 ue_status_hi_desc[i]);
3221                         }
3222                 }
3223         }
3224 }
3225
3226 static void be_msix_disable(struct be_adapter *adapter)
3227 {
3228         if (msix_enabled(adapter)) {
3229                 pci_disable_msix(adapter->pdev);
3230                 adapter->num_msix_vec = 0;
3231                 adapter->num_msix_roce_vec = 0;
3232         }
3233 }
3234
3235 static int be_msix_enable(struct be_adapter *adapter)
3236 {
3237         int i, num_vec;
3238         struct device *dev = &adapter->pdev->dev;
3239
3240         /* If RoCE is supported, program the max number of NIC vectors that
3241          * may be configured via set-channels, along with vectors needed for
3242          * RoCe. Else, just program the number we'll use initially.
3243          */
3244         if (be_roce_supported(adapter))
3245                 num_vec = min_t(int, 2 * be_max_eqs(adapter),
3246                                 2 * num_online_cpus());
3247         else
3248                 num_vec = adapter->cfg_num_qs;
3249
3250         for (i = 0; i < num_vec; i++)
3251                 adapter->msix_entries[i].entry = i;
3252
3253         num_vec = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
3254                                         MIN_MSIX_VECTORS, num_vec);
3255         if (num_vec < 0)
3256                 goto fail;
3257
3258         if (be_roce_supported(adapter) && num_vec > MIN_MSIX_VECTORS) {
3259                 adapter->num_msix_roce_vec = num_vec / 2;
3260                 dev_info(dev, "enabled %d MSI-x vector(s) for RoCE\n",
3261                          adapter->num_msix_roce_vec);
3262         }
3263
3264         adapter->num_msix_vec = num_vec - adapter->num_msix_roce_vec;
3265
3266         dev_info(dev, "enabled %d MSI-x vector(s) for NIC\n",
3267                  adapter->num_msix_vec);
3268         return 0;
3269
3270 fail:
3271         dev_warn(dev, "MSIx enable failed\n");
3272
3273         /* INTx is not supported in VFs, so fail probe if enable_msix fails */
3274         if (be_virtfn(adapter))
3275                 return num_vec;
3276         return 0;
3277 }
3278
3279 static inline int be_msix_vec_get(struct be_adapter *adapter,
3280                                   struct be_eq_obj *eqo)
3281 {
3282         return adapter->msix_entries[eqo->msix_idx].vector;
3283 }
3284
3285 static int be_msix_register(struct be_adapter *adapter)
3286 {
3287         struct net_device *netdev = adapter->netdev;
3288         struct be_eq_obj *eqo;
3289         int status, i, vec;
3290
3291         for_all_evt_queues(adapter, eqo, i) {
3292                 sprintf(eqo->desc, "%s-q%d", netdev->name, i);
3293                 vec = be_msix_vec_get(adapter, eqo);
3294                 status = request_irq(vec, be_msix, 0, eqo->desc, eqo);
3295                 if (status)
3296                         goto err_msix;
3297
3298                 irq_set_affinity_hint(vec, eqo->affinity_mask);
3299         }
3300
3301         return 0;
3302 err_msix:
3303         for (i--; i >= 0; i--) {
3304                 eqo = &adapter->eq_obj[i];
3305                 free_irq(be_msix_vec_get(adapter, eqo), eqo);
3306         }
3307         dev_warn(&adapter->pdev->dev, "MSIX Request IRQ failed - err %d\n",
3308                  status);
3309         be_msix_disable(adapter);
3310         return status;
3311 }
3312
3313 static int be_irq_register(struct be_adapter *adapter)
3314 {
3315         struct net_device *netdev = adapter->netdev;
3316         int status;
3317
3318         if (msix_enabled(adapter)) {
3319                 status = be_msix_register(adapter);
3320                 if (status == 0)
3321                         goto done;
3322                 /* INTx is not supported for VF */
3323                 if (be_virtfn(adapter))
3324                         return status;
3325         }
3326
3327         /* INTx: only the first EQ is used */
3328         netdev->irq = adapter->pdev->irq;
3329         status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
3330                              &adapter->eq_obj[0]);
3331         if (status) {
3332                 dev_err(&adapter->pdev->dev,
3333                         "INTx request IRQ failed - err %d\n", status);
3334                 return status;
3335         }
3336 done:
3337         adapter->isr_registered = true;
3338         return 0;
3339 }
3340
3341 static void be_irq_unregister(struct be_adapter *adapter)
3342 {
3343         struct net_device *netdev = adapter->netdev;
3344         struct be_eq_obj *eqo;
3345         int i, vec;
3346
3347         if (!adapter->isr_registered)
3348                 return;
3349
3350         /* INTx */
3351         if (!msix_enabled(adapter)) {
3352                 free_irq(netdev->irq, &adapter->eq_obj[0]);
3353                 goto done;
3354         }
3355
3356         /* MSIx */
3357         for_all_evt_queues(adapter, eqo, i) {
3358                 vec = be_msix_vec_get(adapter, eqo);
3359                 irq_set_affinity_hint(vec, NULL);
3360                 free_irq(vec, eqo);
3361         }
3362
3363 done:
3364         adapter->isr_registered = false;
3365 }
3366
3367 static void be_rx_qs_destroy(struct be_adapter *adapter)
3368 {
3369         struct be_queue_info *q;
3370         struct be_rx_obj *rxo;
3371         int i;
3372
3373         for_all_rx_queues(adapter, rxo, i) {
3374                 q = &rxo->q;
3375                 if (q->created) {
3376                         /* If RXQs are destroyed while in an "out of buffer"
3377                          * state, there is a possibility of an HW stall on
3378                          * Lancer. So, post 64 buffers to each queue to relieve
3379                          * the "out of buffer" condition.
3380                          * Make sure there's space in the RXQ before posting.
3381                          */
3382                         if (lancer_chip(adapter)) {
3383                                 be_rx_cq_clean(rxo);
3384                                 if (atomic_read(&q->used) == 0)
3385                                         be_post_rx_frags(rxo, GFP_KERNEL,
3386                                                          MAX_RX_POST);
3387                         }
3388
3389                         be_cmd_rxq_destroy(adapter, q);
3390                         be_rx_cq_clean(rxo);
3391                         be_rxq_clean(rxo);
3392                 }
3393                 be_queue_free(adapter, q);
3394         }
3395 }
3396
3397 static void be_disable_if_filters(struct be_adapter *adapter)
3398 {
3399         be_cmd_pmac_del(adapter, adapter->if_handle,
3400                         adapter->pmac_id[0], 0);
3401
3402         be_clear_uc_list(adapter);
3403
3404         /* The IFACE flags are enabled in the open path and cleared
3405          * in the close path. When a VF gets detached from the host and
3406          * assigned to a VM the following happens:
3407          *      - VF's IFACE flags get cleared in the detach path
3408          *      - IFACE create is issued by the VF in the attach path
3409          * Due to a bug in the BE3/Skyhawk-R FW
3410          * (Lancer FW doesn't have the bug), the IFACE capability flags
3411          * specified along with the IFACE create cmd issued by a VF are not
3412          * honoured by FW.  As a consequence, if a *new* driver
3413          * (that enables/disables IFACE flags in open/close)
3414          * is loaded in the host and an *old* driver is * used by a VM/VF,
3415          * the IFACE gets created *without* the needed flags.
3416          * To avoid this, disable RX-filter flags only for Lancer.
3417          */
3418         if (lancer_chip(adapter)) {
3419                 be_cmd_rx_filter(adapter, BE_IF_ALL_FILT_FLAGS, OFF);
3420                 adapter->if_flags &= ~BE_IF_ALL_FILT_FLAGS;
3421         }
3422 }
3423
3424 static int be_close(struct net_device *netdev)
3425 {
3426         struct be_adapter *adapter = netdev_priv(netdev);
3427         struct be_eq_obj *eqo;
3428         int i;
3429
3430         /* This protection is needed as be_close() may be called even when the
3431          * adapter is in cleared state (after eeh perm failure)
3432          */
3433         if (!(adapter->flags & BE_FLAGS_SETUP_DONE))
3434                 return 0;
3435
3436         be_disable_if_filters(adapter);
3437
3438         if (adapter->flags & BE_FLAGS_NAPI_ENABLED) {
3439                 for_all_evt_queues(adapter, eqo, i) {
3440                         napi_disable(&eqo->napi);
3441                         be_disable_busy_poll(eqo);
3442                 }
3443                 adapter->flags &= ~BE_FLAGS_NAPI_ENABLED;
3444         }
3445
3446         be_async_mcc_disable(adapter);
3447
3448         /* Wait for all pending tx completions to arrive so that
3449          * all tx skbs are freed.
3450          */
3451         netif_tx_disable(netdev);
3452         be_tx_compl_clean(adapter);
3453
3454         be_rx_qs_destroy(adapter);
3455
3456         for_all_evt_queues(adapter, eqo, i) {
3457                 if (msix_enabled(adapter))
3458                         synchronize_irq(be_msix_vec_get(adapter, eqo));
3459                 else
3460                         synchronize_irq(netdev->irq);
3461                 be_eq_clean(eqo);
3462         }
3463
3464         be_irq_unregister(adapter);
3465
3466         return 0;
3467 }
3468
3469 static int be_rx_qs_create(struct be_adapter *adapter)
3470 {
3471         struct rss_info *rss = &adapter->rss_info;
3472         u8 rss_key[RSS_HASH_KEY_LEN];
3473         struct be_rx_obj *rxo;
3474         int rc, i, j;
3475
3476         for_all_rx_queues(adapter, rxo, i) {
3477                 rc = be_queue_alloc(adapter, &rxo->q, RX_Q_LEN,
3478                                     sizeof(struct be_eth_rx_d));
3479                 if (rc)
3480                         return rc;
3481         }
3482
3483         if (adapter->need_def_rxq || !adapter->num_rss_qs) {
3484                 rxo = default_rxo(adapter);
3485                 rc = be_cmd_rxq_create(adapter, &rxo->q, rxo->cq.id,
3486                                        rx_frag_size, adapter->if_handle,
3487                                        false, &rxo->rss_id);
3488                 if (rc)
3489                         return rc;
3490         }
3491
3492         for_all_rss_queues(adapter, rxo, i) {
3493                 rc = be_cmd_rxq_create(adapter, &rxo->q, rxo->cq.id,
3494                                        rx_frag_size, adapter->if_handle,
3495                                        true, &rxo->rss_id);
3496                 if (rc)
3497                         return rc;
3498         }
3499
3500         if (be_multi_rxq(adapter)) {
3501                 for (j = 0; j < RSS_INDIR_TABLE_LEN; j += adapter->num_rss_qs) {
3502                         for_all_rss_queues(adapter, rxo, i) {
3503                                 if ((j + i) >= RSS_INDIR_TABLE_LEN)
3504                                         break;
3505                                 rss->rsstable[j + i] = rxo->rss_id;
3506                                 rss->rss_queue[j + i] = i;
3507                         }
3508                 }
3509                 rss->rss_flags = RSS_ENABLE_TCP_IPV4 | RSS_ENABLE_IPV4 |
3510                         RSS_ENABLE_TCP_IPV6 | RSS_ENABLE_IPV6;
3511
3512                 if (!BEx_chip(adapter))
3513                         rss->rss_flags |= RSS_ENABLE_UDP_IPV4 |
3514                                 RSS_ENABLE_UDP_IPV6;
3515         } else {
3516                 /* Disable RSS, if only default RX Q is created */
3517                 rss->rss_flags = RSS_ENABLE_NONE;
3518         }
3519
3520         netdev_rss_key_fill(rss_key, RSS_HASH_KEY_LEN);
3521         rc = be_cmd_rss_config(adapter, rss->rsstable, rss->rss_flags,
3522                                RSS_INDIR_TABLE_LEN, rss_key);
3523         if (rc) {
3524                 rss->rss_flags = RSS_ENABLE_NONE;
3525                 return rc;
3526         }
3527
3528         memcpy(rss->rss_hkey, rss_key, RSS_HASH_KEY_LEN);
3529
3530         /* Post 1 less than RXQ-len to avoid head being equal to tail,
3531          * which is a queue empty condition
3532          */
3533         for_all_rx_queues(adapter, rxo, i)
3534                 be_post_rx_frags(rxo, GFP_KERNEL, RX_Q_LEN - 1);
3535
3536         return 0;
3537 }
3538
3539 static int be_enable_if_filters(struct be_adapter *adapter)
3540 {
3541         int status;
3542
3543         status = be_cmd_rx_filter(adapter, BE_IF_EN_FLAGS, ON);
3544         if (status)
3545                 return status;
3546
3547         /* For BE3 VFs, the PF programs the initial MAC address */
3548         if (!(BEx_chip(adapter) && be_virtfn(adapter))) {
3549                 status = be_cmd_pmac_add(adapter, adapter->netdev->dev_addr,
3550                                          adapter->if_handle,
3551                                          &adapter->pmac_id[0], 0);
3552                 if (status)
3553                         return status;
3554         }
3555
3556         if (adapter->vlans_added)
3557                 be_vid_config(adapter);
3558
3559         be_set_rx_mode(adapter->netdev);
3560
3561         return 0;
3562 }
3563
3564 static int be_open(struct net_device *netdev)
3565 {
3566         struct be_adapter *adapter = netdev_priv(netdev);
3567         struct be_eq_obj *eqo;
3568         struct be_rx_obj *rxo;
3569         struct be_tx_obj *txo;
3570         u8 link_status;
3571         int status, i;
3572
3573         status = be_rx_qs_create(adapter);
3574         if (status)
3575                 goto err;
3576
3577         status = be_enable_if_filters(adapter);
3578         if (status)
3579                 goto err;
3580
3581         status = be_irq_register(adapter);
3582         if (status)
3583                 goto err;
3584
3585         for_all_rx_queues(adapter, rxo, i)
3586                 be_cq_notify(adapter, rxo->cq.id, true, 0);
3587
3588         for_all_tx_queues(adapter, txo, i)
3589                 be_cq_notify(adapter, txo->cq.id, true, 0);
3590
3591         be_async_mcc_enable(adapter);
3592
3593         for_all_evt_queues(adapter, eqo, i) {
3594                 napi_enable(&eqo->napi);
3595                 be_enable_busy_poll(eqo);
3596                 be_eq_notify(adapter, eqo->q.id, true, true, 0, 0);
3597         }
3598         adapter->flags |= BE_FLAGS_NAPI_ENABLED;
3599
3600         status = be_cmd_link_status_query(adapter, NULL, &link_status, 0);
3601         if (!status)
3602                 be_link_status_update(adapter, link_status);
3603
3604         netif_tx_start_all_queues(netdev);
3605 #ifdef CONFIG_BE2NET_VXLAN
3606         if (skyhawk_chip(adapter))
3607                 vxlan_get_rx_port(netdev);
3608 #endif
3609
3610         return 0;
3611 err:
3612         be_close(adapter->netdev);
3613         return -EIO;
3614 }
3615
3616 static int be_setup_wol(struct be_adapter *adapter, bool enable)
3617 {
3618         struct device *dev = &adapter->pdev->dev;
3619         struct be_dma_mem cmd;
3620         u8 mac[ETH_ALEN];
3621         int status;
3622
3623         eth_zero_addr(mac);
3624
3625         cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
3626         cmd.va = dma_zalloc_coherent(dev, cmd.size, &cmd.dma, GFP_KERNEL);
3627         if (!cmd.va)
3628                 return -ENOMEM;
3629
3630         if (enable) {
3631                 status = pci_write_config_dword(adapter->pdev,
3632                                                 PCICFG_PM_CONTROL_OFFSET,
3633                                                 PCICFG_PM_CONTROL_MASK);
3634                 if (status) {
3635                         dev_err(dev, "Could not enable Wake-on-lan\n");
3636                         goto err;
3637                 }
3638         } else {
3639                 ether_addr_copy(mac, adapter->netdev->dev_addr);
3640         }
3641
3642         status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
3643         pci_enable_wake(adapter->pdev, PCI_D3hot, enable);
3644         pci_enable_wake(adapter->pdev, PCI_D3cold, enable);
3645 err:
3646         dma_free_coherent(dev, cmd.size, cmd.va, cmd.dma);
3647         return status;
3648 }
3649
3650 static void be_vf_eth_addr_generate(struct be_adapter *adapter, u8 *mac)
3651 {
3652         u32 addr;
3653
3654         addr = jhash(adapter->netdev->dev_addr, ETH_ALEN, 0);
3655
3656         mac[5] = (u8)(addr & 0xFF);
3657         mac[4] = (u8)((addr >> 8) & 0xFF);
3658         mac[3] = (u8)((addr >> 16) & 0xFF);
3659         /* Use the OUI from the current MAC address */
3660         memcpy(mac, adapter->netdev->dev_addr, 3);
3661 }
3662
3663 /*
3664  * Generate a seed MAC address from the PF MAC Address using jhash.
3665  * MAC Address for VFs are assigned incrementally starting from the seed.
3666  * These addresses are programmed in the ASIC by the PF and the VF driver
3667  * queries for the MAC address during its probe.
3668  */
3669 static int be_vf_eth_addr_config(struct be_adapter *adapter)
3670 {
3671         u32 vf;
3672         int status = 0;
3673         u8 mac[ETH_ALEN];
3674         struct be_vf_cfg *vf_cfg;
3675
3676         be_vf_eth_addr_generate(adapter, mac);
3677
3678         for_all_vfs(adapter, vf_cfg, vf) {
3679                 if (BEx_chip(adapter))
3680                         status = be_cmd_pmac_add(adapter, mac,
3681                                                  vf_cfg->if_handle,
3682                                                  &vf_cfg->pmac_id, vf + 1);
3683                 else
3684                         status = be_cmd_set_mac(adapter, mac, vf_cfg->if_handle,
3685                                                 vf + 1);
3686
3687                 if (status)
3688                         dev_err(&adapter->pdev->dev,
3689                                 "Mac address assignment failed for VF %d\n",
3690                                 vf);
3691                 else
3692                         memcpy(vf_cfg->mac_addr, mac, ETH_ALEN);
3693
3694                 mac[5] += 1;
3695         }
3696         return status;
3697 }
3698
3699 static int be_vfs_mac_query(struct be_adapter *adapter)
3700 {
3701         int status, vf;
3702         u8 mac[ETH_ALEN];
3703         struct be_vf_cfg *vf_cfg;
3704
3705         for_all_vfs(adapter, vf_cfg, vf) {
3706                 status = be_cmd_get_active_mac(adapter, vf_cfg->pmac_id,
3707                                                mac, vf_cfg->if_handle,
3708                                                false, vf+1);
3709                 if (status)
3710                         return status;
3711                 memcpy(vf_cfg->mac_addr, mac, ETH_ALEN);
3712         }
3713         return 0;
3714 }
3715
3716 static void be_vf_clear(struct be_adapter *adapter)
3717 {
3718         struct be_vf_cfg *vf_cfg;
3719         u32 vf;
3720
3721         if (pci_vfs_assigned(adapter->pdev)) {
3722                 dev_warn(&adapter->pdev->dev,
3723                          "VFs are assigned to VMs: not disabling VFs\n");
3724                 goto done;
3725         }
3726
3727         pci_disable_sriov(adapter->pdev);
3728
3729         for_all_vfs(adapter, vf_cfg, vf) {
3730                 if (BEx_chip(adapter))
3731                         be_cmd_pmac_del(adapter, vf_cfg->if_handle,
3732                                         vf_cfg->pmac_id, vf + 1);
3733                 else
3734                         be_cmd_set_mac(adapter, NULL, vf_cfg->if_handle,
3735                                        vf + 1);
3736
3737                 be_cmd_if_destroy(adapter, vf_cfg->if_handle, vf + 1);
3738         }
3739 done:
3740         kfree(adapter->vf_cfg);
3741         adapter->num_vfs = 0;
3742         adapter->flags &= ~BE_FLAGS_SRIOV_ENABLED;
3743 }
3744
3745 static void be_clear_queues(struct be_adapter *adapter)
3746 {
3747         be_mcc_queues_destroy(adapter);
3748         be_rx_cqs_destroy(adapter);
3749         be_tx_queues_destroy(adapter);
3750         be_evt_queues_destroy(adapter);
3751 }
3752
3753 static void be_cancel_worker(struct be_adapter *adapter)
3754 {
3755         if (adapter->flags & BE_FLAGS_WORKER_SCHEDULED) {
3756                 cancel_delayed_work_sync(&adapter->work);
3757                 adapter->flags &= ~BE_FLAGS_WORKER_SCHEDULED;
3758         }
3759 }
3760
3761 static void be_cancel_err_detection(struct be_adapter *adapter)
3762 {
3763         if (adapter->flags & BE_FLAGS_ERR_DETECTION_SCHEDULED) {
3764                 cancel_delayed_work_sync(&adapter->be_err_detection_work);
3765                 adapter->flags &= ~BE_FLAGS_ERR_DETECTION_SCHEDULED;
3766         }
3767 }
3768
3769 #ifdef CONFIG_BE2NET_VXLAN
3770 static void be_disable_vxlan_offloads(struct be_adapter *adapter)
3771 {
3772         struct net_device *netdev = adapter->netdev;
3773
3774         if (adapter->flags & BE_FLAGS_VXLAN_OFFLOADS)
3775                 be_cmd_manage_iface(adapter, adapter->if_handle,
3776                                     OP_CONVERT_TUNNEL_TO_NORMAL);
3777
3778         if (adapter->vxlan_port)
3779                 be_cmd_set_vxlan_port(adapter, 0);
3780
3781         adapter->flags &= ~BE_FLAGS_VXLAN_OFFLOADS;
3782         adapter->vxlan_port = 0;
3783
3784         netdev->hw_enc_features = 0;
3785         netdev->hw_features &= ~(NETIF_F_GSO_UDP_TUNNEL);
3786         netdev->features &= ~(NETIF_F_GSO_UDP_TUNNEL);
3787 }
3788 #endif
3789
3790 static u16 be_calculate_vf_qs(struct be_adapter *adapter, u16 num_vfs)
3791 {
3792         struct be_resources res = adapter->pool_res;
3793         u16 num_vf_qs = 1;
3794
3795         /* Distribute the queue resources among the PF and it's VFs
3796          * Do not distribute queue resources in multi-channel configuration.
3797          */
3798         if (num_vfs && !be_is_mc(adapter)) {
3799                  /* Divide the qpairs evenly among the VFs and the PF, capped
3800                   * at VF-EQ-count. Any remainder qpairs belong to the PF.
3801                   */
3802                 num_vf_qs = min(SH_VF_MAX_NIC_EQS,
3803                                 res.max_rss_qs / (num_vfs + 1));
3804
3805                 /* Skyhawk-R chip supports only MAX_RSS_IFACES RSS capable
3806                  * interfaces per port. Provide RSS on VFs, only if number
3807                  * of VFs requested is less than MAX_RSS_IFACES limit.
3808                  */
3809                 if (num_vfs >= MAX_RSS_IFACES)
3810                         num_vf_qs = 1;
3811         }
3812         return num_vf_qs;
3813 }
3814
3815 static int be_clear(struct be_adapter *adapter)
3816 {
3817         struct pci_dev *pdev = adapter->pdev;
3818         u16 num_vf_qs;
3819
3820         be_cancel_worker(adapter);
3821
3822         if (sriov_enabled(adapter))
3823                 be_vf_clear(adapter);
3824
3825         /* Re-configure FW to distribute resources evenly across max-supported
3826          * number of VFs, only when VFs are not already enabled.
3827          */
3828         if (skyhawk_chip(adapter) && be_physfn(adapter) &&
3829             !pci_vfs_assigned(pdev)) {
3830                 num_vf_qs = be_calculate_vf_qs(adapter,
3831                                                pci_sriov_get_totalvfs(pdev));
3832                 be_cmd_set_sriov_config(adapter, adapter->pool_res,
3833                                         pci_sriov_get_totalvfs(pdev),
3834                                         num_vf_qs);
3835         }
3836
3837 #ifdef CONFIG_BE2NET_VXLAN
3838         be_disable_vxlan_offloads(adapter);
3839 #endif
3840         kfree(adapter->pmac_id);
3841         adapter->pmac_id = NULL;
3842
3843         be_cmd_if_destroy(adapter, adapter->if_handle,  0);
3844
3845         be_clear_queues(adapter);
3846
3847         be_msix_disable(adapter);
3848         adapter->flags &= ~BE_FLAGS_SETUP_DONE;
3849         return 0;
3850 }
3851
3852 static int be_vfs_if_create(struct be_adapter *adapter)
3853 {
3854         struct be_resources res = {0};
3855         u32 cap_flags, en_flags, vf;
3856         struct be_vf_cfg *vf_cfg;
3857         int status;
3858
3859         /* If a FW profile exists, then cap_flags are updated */
3860         cap_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
3861                     BE_IF_FLAGS_MULTICAST | BE_IF_FLAGS_PASS_L3L4_ERRORS;
3862
3863         for_all_vfs(adapter, vf_cfg, vf) {
3864                 if (!BE3_chip(adapter)) {
3865                         status = be_cmd_get_profile_config(adapter, &res,
3866                                                            RESOURCE_LIMITS,
3867                                                            vf + 1);
3868                         if (!status) {
3869                                 cap_flags = res.if_cap_flags;
3870                                 /* Prevent VFs from enabling VLAN promiscuous
3871                                  * mode
3872                                  */
3873                                 cap_flags &= ~BE_IF_FLAGS_VLAN_PROMISCUOUS;
3874                         }
3875                 }
3876
3877                 en_flags = cap_flags & (BE_IF_FLAGS_UNTAGGED |
3878                                         BE_IF_FLAGS_BROADCAST |
3879                                         BE_IF_FLAGS_MULTICAST |
3880                                         BE_IF_FLAGS_PASS_L3L4_ERRORS);
3881                 status = be_cmd_if_create(adapter, cap_flags, en_flags,
3882                                           &vf_cfg->if_handle, vf + 1);
3883                 if (status)
3884                         return status;
3885         }
3886
3887         return 0;
3888 }
3889
3890 static int be_vf_setup_init(struct be_adapter *adapter)
3891 {
3892         struct be_vf_cfg *vf_cfg;
3893         int vf;
3894
3895         adapter->vf_cfg = kcalloc(adapter->num_vfs, sizeof(*vf_cfg),
3896                                   GFP_KERNEL);
3897         if (!adapter->vf_cfg)
3898                 return -ENOMEM;
3899
3900         for_all_vfs(adapter, vf_cfg, vf) {
3901                 vf_cfg->if_handle = -1;
3902                 vf_cfg->pmac_id = -1;
3903         }
3904         return 0;
3905 }
3906
3907 static int be_vf_setup(struct be_adapter *adapter)
3908 {
3909         struct device *dev = &adapter->pdev->dev;
3910         struct be_vf_cfg *vf_cfg;
3911         int status, old_vfs, vf;
3912         bool spoofchk;
3913
3914         old_vfs = pci_num_vf(adapter->pdev);
3915
3916         status = be_vf_setup_init(adapter);
3917         if (status)
3918                 goto err;
3919
3920         if (old_vfs) {
3921                 for_all_vfs(adapter, vf_cfg, vf) {
3922                         status = be_cmd_get_if_id(adapter, vf_cfg, vf);
3923                         if (status)
3924                                 goto err;
3925                 }
3926
3927                 status = be_vfs_mac_query(adapter);
3928                 if (status)
3929                         goto err;
3930         } else {
3931                 status = be_vfs_if_create(adapter);
3932                 if (status)
3933                         goto err;
3934
3935                 status = be_vf_eth_addr_config(adapter);
3936                 if (status)
3937                         goto err;
3938         }
3939
3940         for_all_vfs(adapter, vf_cfg, vf) {
3941                 /* Allow VFs to programs MAC/VLAN filters */
3942                 status = be_cmd_get_fn_privileges(adapter, &vf_cfg->privileges,
3943                                                   vf + 1);
3944                 if (!status && !(vf_cfg->privileges & BE_PRIV_FILTMGMT)) {
3945                         status = be_cmd_set_fn_privileges(adapter,
3946                                                           vf_cfg->privileges |
3947                                                           BE_PRIV_FILTMGMT,
3948                                                           vf + 1);
3949                         if (!status) {
3950                                 vf_cfg->privileges |= BE_PRIV_FILTMGMT;
3951                                 dev_info(dev, "VF%d has FILTMGMT privilege\n",
3952                                          vf);
3953                         }
3954                 }
3955
3956                 /* Allow full available bandwidth */
3957                 if (!old_vfs)
3958                         be_cmd_config_qos(adapter, 0, 0, vf + 1);
3959
3960                 status = be_cmd_get_hsw_config(adapter, NULL, vf + 1,
3961                                                vf_cfg->if_handle, NULL,
3962                                                &spoofchk);
3963                 if (!status)
3964                         vf_cfg->spoofchk = spoofchk;
3965
3966                 if (!old_vfs) {
3967                         be_cmd_enable_vf(adapter, vf + 1);
3968                         be_cmd_set_logical_link_config(adapter,
3969                                                        IFLA_VF_LINK_STATE_AUTO,
3970                                                        vf+1);
3971                 }
3972         }
3973
3974         if (!old_vfs) {
3975                 status = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
3976                 if (status) {
3977                         dev_err(dev, "SRIOV enable failed\n");
3978                         adapter->num_vfs = 0;
3979                         goto err;
3980                 }
3981         }
3982
3983         adapter->flags |= BE_FLAGS_SRIOV_ENABLED;
3984         return 0;
3985 err:
3986         dev_err(dev, "VF setup failed\n");
3987         be_vf_clear(adapter);
3988         return status;
3989 }
3990
3991 /* Converting function_mode bits on BE3 to SH mc_type enums */
3992
3993 static u8 be_convert_mc_type(u32 function_mode)
3994 {
3995         if (function_mode & VNIC_MODE && function_mode & QNQ_MODE)
3996                 return vNIC1;
3997         else if (function_mode & QNQ_MODE)
3998                 return FLEX10;
3999         else if (function_mode & VNIC_MODE)
4000                 return vNIC2;
4001         else if (function_mode & UMC_ENABLED)
4002                 return UMC;
4003         else
4004                 return MC_NONE;
4005 }
4006
4007 /* On BE2/BE3 FW does not suggest the supported limits */
4008 static void BEx_get_resources(struct be_adapter *adapter,
4009                               struct be_resources *res)
4010 {
4011         bool use_sriov = adapter->num_vfs ? 1 : 0;
4012
4013         if (be_physfn(adapter))
4014                 res->max_uc_mac = BE_UC_PMAC_COUNT;
4015         else
4016                 res->max_uc_mac = BE_VF_UC_PMAC_COUNT;
4017
4018         adapter->mc_type = be_convert_mc_type(adapter->function_mode);
4019
4020         if (be_is_mc(adapter)) {
4021                 /* Assuming that there are 4 channels per port,
4022                  * when multi-channel is enabled
4023                  */
4024                 if (be_is_qnq_mode(adapter))
4025                         res->max_vlans = BE_NUM_VLANS_SUPPORTED/8;
4026                 else
4027                         /* In a non-qnq multichannel mode, the pvid
4028                          * takes up one vlan entry
4029                          */
4030                         res->max_vlans = (BE_NUM_VLANS_SUPPORTED / 4) - 1;
4031         } else {
4032                 res->max_vlans = BE_NUM_VLANS_SUPPORTED;
4033         }
4034
4035         res->max_mcast_mac = BE_MAX_MC;
4036
4037         /* 1) For BE3 1Gb ports, FW does not support multiple TXQs
4038          * 2) Create multiple TX rings on a BE3-R multi-channel interface
4039          *    *only* if it is RSS-capable.
4040          */
4041         if (BE2_chip(adapter) || use_sriov ||  (adapter->port_num > 1) ||
4042             be_virtfn(adapter) ||
4043             (be_is_mc(adapter) &&
4044              !(adapter->function_caps & BE_FUNCTION_CAPS_RSS))) {
4045                 res->max_tx_qs = 1;
4046         } else if (adapter->function_caps & BE_FUNCTION_CAPS_SUPER_NIC) {
4047                 struct be_resources super_nic_res = {0};
4048
4049                 /* On a SuperNIC profile, the driver needs to use the
4050                  * GET_PROFILE_CONFIG cmd to query the per-function TXQ limits
4051                  */
4052                 be_cmd_get_profile_config(adapter, &super_nic_res,
4053                                           RESOURCE_LIMITS, 0);
4054                 /* Some old versions of BE3 FW don't report max_tx_qs value */
4055                 res->max_tx_qs = super_nic_res.max_tx_qs ? : BE3_MAX_TX_QS;
4056         } else {
4057                 res->max_tx_qs = BE3_MAX_TX_QS;
4058         }
4059
4060         if ((adapter->function_caps & BE_FUNCTION_CAPS_RSS) &&
4061             !use_sriov && be_physfn(adapter))
4062                 res->max_rss_qs = (adapter->be3_native) ?
4063                                            BE3_MAX_RSS_QS : BE2_MAX_RSS_QS;
4064         res->max_rx_qs = res->max_rss_qs + 1;
4065
4066         if (be_physfn(adapter))
4067                 res->max_evt_qs = (be_max_vfs(adapter) > 0) ?
4068                                         BE3_SRIOV_MAX_EVT_QS : BE3_MAX_EVT_QS;
4069         else
4070                 res->max_evt_qs = 1;
4071
4072         res->if_cap_flags = BE_IF_CAP_FLAGS_WANT;
4073         res->if_cap_flags &= ~BE_IF_FLAGS_DEFQ_RSS;
4074         if (!(adapter->function_caps & BE_FUNCTION_CAPS_RSS))
4075                 res->if_cap_flags &= ~BE_IF_FLAGS_RSS;
4076 }
4077
4078 static void be_setup_init(struct be_adapter *adapter)
4079 {
4080         adapter->vlan_prio_bmap = 0xff;
4081         adapter->phy.link_speed = -1;
4082         adapter->if_handle = -1;
4083         adapter->be3_native = false;
4084         adapter->if_flags = 0;
4085         if (be_physfn(adapter))
4086                 adapter->cmd_privileges = MAX_PRIVILEGES;
4087         else
4088                 adapter->cmd_privileges = MIN_PRIVILEGES;
4089 }
4090
4091 static int be_get_sriov_config(struct be_adapter *adapter)
4092 {
4093         struct be_resources res = {0};
4094         int max_vfs, old_vfs;
4095
4096         be_cmd_get_profile_config(adapter, &res, RESOURCE_LIMITS, 0);
4097
4098         /* Some old versions of BE3 FW don't report max_vfs value */
4099         if (BE3_chip(adapter) && !res.max_vfs) {
4100                 max_vfs = pci_sriov_get_totalvfs(adapter->pdev);
4101                 res.max_vfs = max_vfs > 0 ? min(MAX_VFS, max_vfs) : 0;
4102         }
4103
4104         adapter->pool_res = res;
4105
4106         /* If during previous unload of the driver, the VFs were not disabled,
4107          * then we cannot rely on the PF POOL limits for the TotalVFs value.
4108          * Instead use the TotalVFs value stored in the pci-dev struct.
4109          */
4110         old_vfs = pci_num_vf(adapter->pdev);
4111         if (old_vfs) {
4112                 dev_info(&adapter->pdev->dev, "%d VFs are already enabled\n",
4113                          old_vfs);
4114
4115                 adapter->pool_res.max_vfs =
4116                         pci_sriov_get_totalvfs(adapter->pdev);
4117                 adapter->num_vfs = old_vfs;
4118         }
4119
4120         return 0;
4121 }
4122
4123 static void be_alloc_sriov_res(struct be_adapter *adapter)
4124 {
4125         int old_vfs = pci_num_vf(adapter->pdev);
4126         u16 num_vf_qs;
4127         int status;
4128
4129         be_get_sriov_config(adapter);
4130
4131         if (!old_vfs)
4132                 pci_sriov_set_totalvfs(adapter->pdev, be_max_vfs(adapter));
4133
4134         /* When the HW is in SRIOV capable configuration, the PF-pool
4135          * resources are given to PF during driver load, if there are no
4136          * old VFs. This facility is not available in BE3 FW.
4137          * Also, this is done by FW in Lancer chip.
4138          */
4139         if (skyhawk_chip(adapter) && be_max_vfs(adapter) && !old_vfs) {
4140                 num_vf_qs = be_calculate_vf_qs(adapter, 0);
4141                 status = be_cmd_set_sriov_config(adapter, adapter->pool_res, 0,
4142                                                  num_vf_qs);
4143                 if (status)
4144                         dev_err(&adapter->pdev->dev,
4145                                 "Failed to optimize SRIOV resources\n");
4146         }
4147 }
4148
4149 static int be_get_resources(struct be_adapter *adapter)
4150 {
4151         struct device *dev = &adapter->pdev->dev;
4152         struct be_resources res = {0};
4153         int status;
4154
4155         if (BEx_chip(adapter)) {
4156                 BEx_get_resources(adapter, &res);
4157                 adapter->res = res;
4158         }
4159
4160         /* For Lancer, SH etc read per-function resource limits from FW.
4161          * GET_FUNC_CONFIG returns per function guaranteed limits.
4162          * GET_PROFILE_CONFIG returns PCI-E related limits PF-pool limits
4163          */
4164         if (!BEx_chip(adapter)) {
4165                 status = be_cmd_get_func_config(adapter, &res);
4166                 if (status)
4167                         return status;
4168
4169                 /* If a deafault RXQ must be created, we'll use up one RSSQ*/
4170                 if (res.max_rss_qs && res.max_rss_qs == res.max_rx_qs &&
4171                     !(res.if_cap_flags & BE_IF_FLAGS_DEFQ_RSS))
4172                         res.max_rss_qs -= 1;
4173
4174                 /* If RoCE may be enabled stash away half the EQs for RoCE */
4175                 if (be_roce_supported(adapter))
4176                         res.max_evt_qs /= 2;
4177                 adapter->res = res;
4178         }
4179
4180         /* If FW supports RSS default queue, then skip creating non-RSS
4181          * queue for non-IP traffic.
4182          */
4183         adapter->need_def_rxq = (be_if_cap_flags(adapter) &
4184                                  BE_IF_FLAGS_DEFQ_RSS) ? 0 : 1;
4185
4186         dev_info(dev, "Max: txqs %d, rxqs %d, rss %d, eqs %d, vfs %d\n",
4187                  be_max_txqs(adapter), be_max_rxqs(adapter),
4188                  be_max_rss(adapter), be_max_eqs(adapter),
4189                  be_max_vfs(adapter));
4190         dev_info(dev, "Max: uc-macs %d, mc-macs %d, vlans %d\n",
4191                  be_max_uc(adapter), be_max_mc(adapter),
4192                  be_max_vlans(adapter));
4193
4194         /* Sanitize cfg_num_qs based on HW and platform limits */
4195         adapter->cfg_num_qs = min_t(u16, netif_get_num_default_rss_queues(),
4196                                     be_max_qs(adapter));
4197         return 0;
4198 }
4199
4200 static int be_get_config(struct be_adapter *adapter)
4201 {
4202         int status, level;
4203         u16 profile_id;
4204
4205         status = be_cmd_get_cntl_attributes(adapter);
4206         if (status)
4207                 return status;
4208
4209         status = be_cmd_query_fw_cfg(adapter);
4210         if (status)
4211                 return status;
4212
4213         if (!lancer_chip(adapter) && be_physfn(adapter))
4214                 be_cmd_get_fat_dump_len(adapter, &adapter->fat_dump_len);
4215
4216         if (BEx_chip(adapter)) {
4217                 level = be_cmd_get_fw_log_level(adapter);
4218                 adapter->msg_enable =
4219                         level <= FW_LOG_LEVEL_DEFAULT ? NETIF_MSG_HW : 0;
4220         }
4221
4222         be_cmd_get_acpi_wol_cap(adapter);
4223
4224         be_cmd_query_port_name(adapter);
4225
4226         if (be_physfn(adapter)) {
4227                 status = be_cmd_get_active_profile(adapter, &profile_id);
4228                 if (!status)
4229                         dev_info(&adapter->pdev->dev,
4230                                  "Using profile 0x%x\n", profile_id);
4231         }
4232
4233         status = be_get_resources(adapter);
4234         if (status)
4235                 return status;
4236
4237         adapter->pmac_id = kcalloc(be_max_uc(adapter),
4238                                    sizeof(*adapter->pmac_id), GFP_KERNEL);
4239         if (!adapter->pmac_id)
4240                 return -ENOMEM;
4241
4242         return 0;
4243 }
4244
4245 static int be_mac_setup(struct be_adapter *adapter)
4246 {
4247         u8 mac[ETH_ALEN];
4248         int status;
4249
4250         if (is_zero_ether_addr(adapter->netdev->dev_addr)) {
4251                 status = be_cmd_get_perm_mac(adapter, mac);
4252                 if (status)
4253                         return status;
4254
4255                 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
4256                 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
4257         }
4258
4259         return 0;
4260 }
4261
4262 static void be_schedule_worker(struct be_adapter *adapter)
4263 {
4264         schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
4265         adapter->flags |= BE_FLAGS_WORKER_SCHEDULED;
4266 }
4267
4268 static void be_schedule_err_detection(struct be_adapter *adapter)
4269 {
4270         schedule_delayed_work(&adapter->be_err_detection_work,
4271                               msecs_to_jiffies(1000));
4272         adapter->flags |= BE_FLAGS_ERR_DETECTION_SCHEDULED;
4273 }
4274
4275 static int be_setup_queues(struct be_adapter *adapter)
4276 {
4277         struct net_device *netdev = adapter->netdev;
4278         int status;
4279
4280         status = be_evt_queues_create(adapter);
4281         if (status)
4282                 goto err;
4283
4284         status = be_tx_qs_create(adapter);
4285         if (status)
4286                 goto err;
4287
4288         status = be_rx_cqs_create(adapter);
4289         if (status)
4290                 goto err;
4291
4292         status = be_mcc_queues_create(adapter);
4293         if (status)
4294                 goto err;
4295
4296         status = netif_set_real_num_rx_queues(netdev, adapter->num_rx_qs);
4297         if (status)
4298                 goto err;
4299
4300         status = netif_set_real_num_tx_queues(netdev, adapter->num_tx_qs);
4301         if (status)
4302                 goto err;
4303
4304         return 0;
4305 err:
4306         dev_err(&adapter->pdev->dev, "queue_setup failed\n");
4307         return status;
4308 }
4309
4310 int be_update_queues(struct be_adapter *adapter)
4311 {
4312         struct net_device *netdev = adapter->netdev;
4313         int status;
4314
4315         if (netif_running(netdev))
4316                 be_close(netdev);
4317
4318         be_cancel_worker(adapter);
4319
4320         /* If any vectors have been shared with RoCE we cannot re-program
4321          * the MSIx table.
4322          */
4323         if (!adapter->num_msix_roce_vec)
4324                 be_msix_disable(adapter);
4325
4326         be_clear_queues(adapter);
4327
4328         if (!msix_enabled(adapter)) {
4329                 status = be_msix_enable(adapter);
4330                 if (status)
4331                         return status;
4332         }
4333
4334         status = be_setup_queues(adapter);
4335         if (status)
4336                 return status;
4337
4338         be_schedule_worker(adapter);
4339
4340         if (netif_running(netdev))
4341                 status = be_open(netdev);
4342
4343         return status;
4344 }
4345
4346 static inline int fw_major_num(const char *fw_ver)
4347 {
4348         int fw_major = 0, i;
4349
4350         i = sscanf(fw_ver, "%d.", &fw_major);
4351         if (i != 1)
4352                 return 0;
4353
4354         return fw_major;
4355 }
4356
4357 /* If any VFs are already enabled don't FLR the PF */
4358 static bool be_reset_required(struct be_adapter *adapter)
4359 {
4360         return pci_num_vf(adapter->pdev) ? false : true;
4361 }
4362
4363 /* Wait for the FW to be ready and perform the required initialization */
4364 static int be_func_init(struct be_adapter *adapter)
4365 {
4366         int status;
4367
4368         status = be_fw_wait_ready(adapter);
4369         if (status)
4370                 return status;
4371
4372         if (be_reset_required(adapter)) {
4373                 status = be_cmd_reset_function(adapter);
4374                 if (status)
4375                         return status;
4376
4377                 /* Wait for interrupts to quiesce after an FLR */
4378                 msleep(100);
4379
4380                 /* We can clear all errors when function reset succeeds */
4381                 be_clear_error(adapter, BE_CLEAR_ALL);
4382         }
4383
4384         /* Tell FW we're ready to fire cmds */
4385         status = be_cmd_fw_init(adapter);
4386         if (status)
4387                 return status;
4388
4389         /* Allow interrupts for other ULPs running on NIC function */
4390         be_intr_set(adapter, true);
4391
4392         return 0;
4393 }
4394
4395 static int be_setup(struct be_adapter *adapter)
4396 {
4397         struct device *dev = &adapter->pdev->dev;
4398         u32 en_flags;
4399         int status;
4400
4401         status = be_func_init(adapter);
4402         if (status)
4403                 return status;
4404
4405         be_setup_init(adapter);
4406
4407         if (!lancer_chip(adapter))
4408                 be_cmd_req_native_mode(adapter);
4409
4410         /* invoke this cmd first to get pf_num and vf_num which are needed
4411          * for issuing profile related cmds
4412          */
4413         if (!BEx_chip(adapter)) {
4414                 status = be_cmd_get_func_config(adapter, NULL);
4415                 if (status)
4416                         return status;
4417         }
4418
4419         if (!BE2_chip(adapter) && be_physfn(adapter))
4420                 be_alloc_sriov_res(adapter);
4421
4422         status = be_get_config(adapter);
4423         if (status)
4424                 goto err;
4425
4426         status = be_msix_enable(adapter);
4427         if (status)
4428                 goto err;
4429
4430         /* will enable all the needed filter flags in be_open() */
4431         en_flags = BE_IF_FLAGS_RSS | BE_IF_FLAGS_DEFQ_RSS;
4432         en_flags = en_flags & be_if_cap_flags(adapter);
4433         status = be_cmd_if_create(adapter, be_if_cap_flags(adapter), en_flags,
4434                                   &adapter->if_handle, 0);
4435         if (status)
4436                 goto err;
4437
4438         /* Updating real_num_tx/rx_queues() requires rtnl_lock() */
4439         rtnl_lock();
4440         status = be_setup_queues(adapter);
4441         rtnl_unlock();
4442         if (status)
4443                 goto err;
4444
4445         be_cmd_get_fn_privileges(adapter, &adapter->cmd_privileges, 0);
4446
4447         status = be_mac_setup(adapter);
4448         if (status)
4449                 goto err;
4450
4451         be_cmd_get_fw_ver(adapter);
4452         dev_info(dev, "FW version is %s\n", adapter->fw_ver);
4453
4454         if (BE2_chip(adapter) && fw_major_num(adapter->fw_ver) < 4) {
4455                 dev_err(dev, "Firmware on card is old(%s), IRQs may not work",
4456                         adapter->fw_ver);
4457                 dev_err(dev, "Please upgrade firmware to version >= 4.0\n");
4458         }
4459
4460         status = be_cmd_set_flow_control(adapter, adapter->tx_fc,
4461                                          adapter->rx_fc);
4462         if (status)
4463                 be_cmd_get_flow_control(adapter, &adapter->tx_fc,
4464                                         &adapter->rx_fc);
4465
4466         dev_info(&adapter->pdev->dev, "HW Flow control - TX:%d RX:%d\n",
4467                  adapter->tx_fc, adapter->rx_fc);
4468
4469         if (be_physfn(adapter))
4470                 be_cmd_set_logical_link_config(adapter,
4471                                                IFLA_VF_LINK_STATE_AUTO, 0);
4472
4473         if (adapter->num_vfs)
4474                 be_vf_setup(adapter);
4475
4476         status = be_cmd_get_phy_info(adapter);
4477         if (!status && be_pause_supported(adapter))
4478                 adapter->phy.fc_autoneg = 1;
4479
4480         be_schedule_worker(adapter);
4481         adapter->flags |= BE_FLAGS_SETUP_DONE;
4482         return 0;
4483 err:
4484         be_clear(adapter);
4485         return status;
4486 }
4487
4488 #ifdef CONFIG_NET_POLL_CONTROLLER
4489 static void be_netpoll(struct net_device *netdev)
4490 {
4491         struct be_adapter *adapter = netdev_priv(netdev);
4492         struct be_eq_obj *eqo;
4493         int i;
4494
4495         for_all_evt_queues(adapter, eqo, i) {
4496                 be_eq_notify(eqo->adapter, eqo->q.id, false, true, 0, 0);
4497                 napi_schedule(&eqo->napi);
4498         }
4499 }
4500 #endif
4501
4502 int be_load_fw(struct be_adapter *adapter, u8 *fw_file)
4503 {
4504         const struct firmware *fw;
4505         int status;
4506
4507         if (!netif_running(adapter->netdev)) {
4508                 dev_err(&adapter->pdev->dev,
4509                         "Firmware load not allowed (interface is down)\n");
4510                 return -ENETDOWN;
4511         }
4512
4513         status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
4514         if (status)
4515                 goto fw_exit;
4516
4517         dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
4518
4519         if (lancer_chip(adapter))
4520                 status = lancer_fw_download(adapter, fw);
4521         else
4522                 status = be_fw_download(adapter, fw);
4523
4524         if (!status)
4525                 be_cmd_get_fw_ver(adapter);
4526
4527 fw_exit:
4528         release_firmware(fw);
4529         return status;
4530 }
4531
4532 static int be_ndo_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
4533                                  u16 flags)
4534 {
4535         struct be_adapter *adapter = netdev_priv(dev);
4536         struct nlattr *attr, *br_spec;
4537         int rem;
4538         int status = 0;
4539         u16 mode = 0;
4540
4541         if (!sriov_enabled(adapter))
4542                 return -EOPNOTSUPP;
4543
4544         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4545         if (!br_spec)
4546                 return -EINVAL;
4547
4548         nla_for_each_nested(attr, br_spec, rem) {
4549                 if (nla_type(attr) != IFLA_BRIDGE_MODE)
4550                         continue;
4551
4552                 if (nla_len(attr) < sizeof(mode))
4553                         return -EINVAL;
4554
4555                 mode = nla_get_u16(attr);
4556                 if (BE3_chip(adapter) && mode == BRIDGE_MODE_VEPA)
4557                         return -EOPNOTSUPP;
4558
4559                 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
4560                         return -EINVAL;
4561
4562                 status = be_cmd_set_hsw_config(adapter, 0, 0,
4563                                                adapter->if_handle,
4564                                                mode == BRIDGE_MODE_VEPA ?
4565                                                PORT_FWD_TYPE_VEPA :
4566                                                PORT_FWD_TYPE_VEB, 0);
4567                 if (status)
4568                         goto err;
4569
4570                 dev_info(&adapter->pdev->dev, "enabled switch mode: %s\n",
4571                          mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
4572
4573                 return status;
4574         }
4575 err:
4576         dev_err(&adapter->pdev->dev, "Failed to set switch mode %s\n",
4577                 mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
4578
4579         return status;
4580 }
4581
4582 static int be_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4583                                  struct net_device *dev, u32 filter_mask,
4584                                  int nlflags)
4585 {
4586         struct be_adapter *adapter = netdev_priv(dev);
4587         int status = 0;
4588         u8 hsw_mode;
4589
4590         /* BE and Lancer chips support VEB mode only */
4591         if (BEx_chip(adapter) || lancer_chip(adapter)) {
4592                 hsw_mode = PORT_FWD_TYPE_VEB;
4593         } else {
4594                 status = be_cmd_get_hsw_config(adapter, NULL, 0,
4595                                                adapter->if_handle, &hsw_mode,
4596                                                NULL);
4597                 if (status)
4598                         return 0;
4599
4600                 if (hsw_mode == PORT_FWD_TYPE_PASSTHRU)
4601                         return 0;
4602         }
4603
4604         return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
4605                                        hsw_mode == PORT_FWD_TYPE_VEPA ?
4606                                        BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB,
4607                                        0, 0, nlflags, filter_mask, NULL);
4608 }
4609
4610 #ifdef CONFIG_BE2NET_VXLAN
4611 /* VxLAN offload Notes:
4612  *
4613  * The stack defines tunnel offload flags (hw_enc_features) for IP and doesn't
4614  * distinguish various types of transports (VxLAN, GRE, NVGRE ..). So, offload
4615  * is expected to work across all types of IP tunnels once exported. Skyhawk
4616  * supports offloads for either VxLAN or NVGRE, exclusively. So we export VxLAN
4617  * offloads in hw_enc_features only when a VxLAN port is added. If other (non
4618  * VxLAN) tunnels are configured while VxLAN offloads are enabled, offloads for
4619  * those other tunnels are unexported on the fly through ndo_features_check().
4620  *
4621  * Skyhawk supports VxLAN offloads only for one UDP dport. So, if the stack
4622  * adds more than one port, disable offloads and don't re-enable them again
4623  * until after all the tunnels are removed.
4624  */
4625 static void be_add_vxlan_port(struct net_device *netdev, sa_family_t sa_family,
4626                               __be16 port)
4627 {
4628         struct be_adapter *adapter = netdev_priv(netdev);
4629         struct device *dev = &adapter->pdev->dev;
4630         int status;
4631
4632         if (lancer_chip(adapter) || BEx_chip(adapter) || be_is_mc(adapter))
4633                 return;
4634
4635         if (adapter->vxlan_port == port && adapter->vxlan_port_count) {
4636                 adapter->vxlan_port_aliases++;
4637                 return;
4638         }
4639
4640         if (adapter->flags & BE_FLAGS_VXLAN_OFFLOADS) {
4641                 dev_info(dev,
4642                          "Only one UDP port supported for VxLAN offloads\n");
4643                 dev_info(dev, "Disabling VxLAN offloads\n");
4644                 adapter->vxlan_port_count++;
4645                 goto err;
4646         }
4647
4648         if (adapter->vxlan_port_count++ >= 1)
4649                 return;
4650
4651         status = be_cmd_manage_iface(adapter, adapter->if_handle,
4652                                      OP_CONVERT_NORMAL_TO_TUNNEL);
4653         if (status) {
4654                 dev_warn(dev, "Failed to convert normal interface to tunnel\n");
4655                 goto err;
4656         }
4657
4658         status = be_cmd_set_vxlan_port(adapter, port);
4659         if (status) {
4660                 dev_warn(dev, "Failed to add VxLAN port\n");
4661                 goto err;
4662         }
4663         adapter->flags |= BE_FLAGS_VXLAN_OFFLOADS;
4664         adapter->vxlan_port = port;
4665
4666         netdev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4667                                    NETIF_F_TSO | NETIF_F_TSO6 |
4668                                    NETIF_F_GSO_UDP_TUNNEL;
4669         netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
4670         netdev->features |= NETIF_F_GSO_UDP_TUNNEL;
4671
4672         dev_info(dev, "Enabled VxLAN offloads for UDP port %d\n",
4673                  be16_to_cpu(port));
4674         return;
4675 err:
4676         be_disable_vxlan_offloads(adapter);
4677 }
4678
4679 static void be_del_vxlan_port(struct net_device *netdev, sa_family_t sa_family,
4680                               __be16 port)
4681 {
4682         struct be_adapter *adapter = netdev_priv(netdev);
4683
4684         if (lancer_chip(adapter) || BEx_chip(adapter) || be_is_mc(adapter))
4685                 return;
4686
4687         if (adapter->vxlan_port != port)
4688                 goto done;
4689
4690         if (adapter->vxlan_port_aliases) {
4691                 adapter->vxlan_port_aliases--;
4692                 return;
4693         }
4694
4695         be_disable_vxlan_offloads(adapter);
4696
4697         dev_info(&adapter->pdev->dev,
4698                  "Disabled VxLAN offloads for UDP port %d\n",
4699                  be16_to_cpu(port));
4700 done:
4701         adapter->vxlan_port_count--;
4702 }
4703
4704 static netdev_features_t be_features_check(struct sk_buff *skb,
4705                                            struct net_device *dev,
4706                                            netdev_features_t features)
4707 {
4708         struct be_adapter *adapter = netdev_priv(dev);
4709         u8 l4_hdr = 0;
4710
4711         /* The code below restricts offload features for some tunneled packets.
4712          * Offload features for normal (non tunnel) packets are unchanged.
4713          */
4714         if (!skb->encapsulation ||
4715             !(adapter->flags & BE_FLAGS_VXLAN_OFFLOADS))
4716                 return features;
4717
4718         /* It's an encapsulated packet and VxLAN offloads are enabled. We
4719          * should disable tunnel offload features if it's not a VxLAN packet,
4720          * as tunnel offloads have been enabled only for VxLAN. This is done to
4721          * allow other tunneled traffic like GRE work fine while VxLAN
4722          * offloads are configured in Skyhawk-R.
4723          */
4724         switch (vlan_get_protocol(skb)) {
4725         case htons(ETH_P_IP):
4726                 l4_hdr = ip_hdr(skb)->protocol;
4727                 break;
4728         case htons(ETH_P_IPV6):
4729                 l4_hdr = ipv6_hdr(skb)->nexthdr;
4730                 break;
4731         default:
4732                 return features;
4733         }
4734
4735         if (l4_hdr != IPPROTO_UDP ||
4736             skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
4737             skb->inner_protocol != htons(ETH_P_TEB) ||
4738             skb_inner_mac_header(skb) - skb_transport_header(skb) !=
4739             sizeof(struct udphdr) + sizeof(struct vxlanhdr))
4740                 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4741
4742         return features;
4743 }
4744 #endif
4745
4746 static int be_get_phys_port_id(struct net_device *dev,
4747                                struct netdev_phys_item_id *ppid)
4748 {
4749         int i, id_len = CNTL_SERIAL_NUM_WORDS * CNTL_SERIAL_NUM_WORD_SZ + 1;
4750         struct be_adapter *adapter = netdev_priv(dev);
4751         u8 *id;
4752
4753         if (MAX_PHYS_ITEM_ID_LEN < id_len)
4754                 return -ENOSPC;
4755
4756         ppid->id[0] = adapter->hba_port_num + 1;
4757         id = &ppid->id[1];
4758         for (i = CNTL_SERIAL_NUM_WORDS - 1; i >= 0;
4759              i--, id += CNTL_SERIAL_NUM_WORD_SZ)
4760                 memcpy(id, &adapter->serial_num[i], CNTL_SERIAL_NUM_WORD_SZ);
4761
4762         ppid->id_len = id_len;
4763
4764         return 0;
4765 }
4766
4767 static const struct net_device_ops be_netdev_ops = {
4768         .ndo_open               = be_open,
4769         .ndo_stop               = be_close,
4770         .ndo_start_xmit         = be_xmit,
4771         .ndo_set_rx_mode        = be_set_rx_mode,
4772         .ndo_set_mac_address    = be_mac_addr_set,
4773         .ndo_change_mtu         = be_change_mtu,
4774         .ndo_get_stats64        = be_get_stats64,
4775         .ndo_validate_addr      = eth_validate_addr,
4776         .ndo_vlan_rx_add_vid    = be_vlan_add_vid,
4777         .ndo_vlan_rx_kill_vid   = be_vlan_rem_vid,
4778         .ndo_set_vf_mac         = be_set_vf_mac,
4779         .ndo_set_vf_vlan        = be_set_vf_vlan,
4780         .ndo_set_vf_rate        = be_set_vf_tx_rate,
4781         .ndo_get_vf_config      = be_get_vf_config,
4782         .ndo_set_vf_link_state  = be_set_vf_link_state,
4783         .ndo_set_vf_spoofchk    = be_set_vf_spoofchk,
4784 #ifdef CONFIG_NET_POLL_CONTROLLER
4785         .ndo_poll_controller    = be_netpoll,
4786 #endif
4787         .ndo_bridge_setlink     = be_ndo_bridge_setlink,
4788         .ndo_bridge_getlink     = be_ndo_bridge_getlink,
4789 #ifdef CONFIG_NET_RX_BUSY_POLL
4790         .ndo_busy_poll          = be_busy_poll,
4791 #endif
4792 #ifdef CONFIG_BE2NET_VXLAN
4793         .ndo_add_vxlan_port     = be_add_vxlan_port,
4794         .ndo_del_vxlan_port     = be_del_vxlan_port,
4795         .ndo_features_check     = be_features_check,
4796 #endif
4797         .ndo_get_phys_port_id   = be_get_phys_port_id,
4798 };
4799
4800 static void be_netdev_init(struct net_device *netdev)
4801 {
4802         struct be_adapter *adapter = netdev_priv(netdev);
4803
4804         netdev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
4805                 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM |
4806                 NETIF_F_HW_VLAN_CTAG_TX;
4807         if (be_multi_rxq(adapter))
4808                 netdev->hw_features |= NETIF_F_RXHASH;
4809
4810         netdev->features |= netdev->hw_features |
4811                 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_FILTER;
4812
4813         netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
4814                 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
4815
4816         netdev->priv_flags |= IFF_UNICAST_FLT;
4817
4818         netdev->flags |= IFF_MULTICAST;
4819
4820         netif_set_gso_max_size(netdev, 65535 - ETH_HLEN);
4821
4822         netdev->netdev_ops = &be_netdev_ops;
4823
4824         netdev->ethtool_ops = &be_ethtool_ops;
4825 }
4826
4827 static void be_cleanup(struct be_adapter *adapter)
4828 {
4829         struct net_device *netdev = adapter->netdev;
4830
4831         rtnl_lock();
4832         netif_device_detach(netdev);
4833         if (netif_running(netdev))
4834                 be_close(netdev);
4835         rtnl_unlock();
4836
4837         be_clear(adapter);
4838 }
4839
4840 static int be_resume(struct be_adapter *adapter)
4841 {
4842         struct net_device *netdev = adapter->netdev;
4843         int status;
4844
4845         status = be_setup(adapter);
4846         if (status)
4847                 return status;
4848
4849         if (netif_running(netdev)) {
4850                 status = be_open(netdev);
4851                 if (status)
4852                         return status;
4853         }
4854
4855         netif_device_attach(netdev);
4856
4857         return 0;
4858 }
4859
4860 static int be_err_recover(struct be_adapter *adapter)
4861 {
4862         struct device *dev = &adapter->pdev->dev;
4863         int status;
4864
4865         status = be_resume(adapter);
4866         if (status)
4867                 goto err;
4868
4869         dev_info(dev, "Adapter recovery successful\n");
4870         return 0;
4871 err:
4872         if (be_physfn(adapter))
4873                 dev_err(dev, "Adapter recovery failed\n");
4874         else
4875                 dev_err(dev, "Re-trying adapter recovery\n");
4876
4877         return status;
4878 }
4879
4880 static void be_err_detection_task(struct work_struct *work)
4881 {
4882         struct be_adapter *adapter =
4883                                 container_of(work, struct be_adapter,
4884                                              be_err_detection_work.work);
4885         int status = 0;
4886
4887         be_detect_error(adapter);
4888
4889         if (be_check_error(adapter, BE_ERROR_HW)) {
4890                 be_cleanup(adapter);
4891
4892                 /* As of now error recovery support is in Lancer only */
4893                 if (lancer_chip(adapter))
4894                         status = be_err_recover(adapter);
4895         }
4896
4897         /* Always attempt recovery on VFs */
4898         if (!status || be_virtfn(adapter))
4899                 be_schedule_err_detection(adapter);
4900 }
4901
4902 static void be_log_sfp_info(struct be_adapter *adapter)
4903 {
4904         int status;
4905
4906         status = be_cmd_query_sfp_info(adapter);
4907         if (!status) {
4908                 dev_err(&adapter->pdev->dev,
4909                         "Unqualified SFP+ detected on %c from %s part no: %s",
4910                         adapter->port_name, adapter->phy.vendor_name,
4911                         adapter->phy.vendor_pn);
4912         }
4913         adapter->flags &= ~BE_FLAGS_EVT_INCOMPATIBLE_SFP;
4914 }
4915
4916 static void be_worker(struct work_struct *work)
4917 {
4918         struct be_adapter *adapter =
4919                 container_of(work, struct be_adapter, work.work);
4920         struct be_rx_obj *rxo;
4921         int i;
4922
4923         /* when interrupts are not yet enabled, just reap any pending
4924          * mcc completions
4925          */
4926         if (!netif_running(adapter->netdev)) {
4927                 local_bh_disable();
4928                 be_process_mcc(adapter);
4929                 local_bh_enable();
4930                 goto reschedule;
4931         }
4932
4933         if (!adapter->stats_cmd_sent) {
4934                 if (lancer_chip(adapter))
4935                         lancer_cmd_get_pport_stats(adapter,
4936                                                    &adapter->stats_cmd);
4937                 else
4938                         be_cmd_get_stats(adapter, &adapter->stats_cmd);
4939         }
4940
4941         if (be_physfn(adapter) &&
4942             MODULO(adapter->work_counter, adapter->be_get_temp_freq) == 0)
4943                 be_cmd_get_die_temperature(adapter);
4944
4945         for_all_rx_queues(adapter, rxo, i) {
4946                 /* Replenish RX-queues starved due to memory
4947                  * allocation failures.
4948                  */
4949                 if (rxo->rx_post_starved)
4950                         be_post_rx_frags(rxo, GFP_KERNEL, MAX_RX_POST);
4951         }
4952
4953         /* EQ-delay update for Skyhawk is done while notifying EQ */
4954         if (!skyhawk_chip(adapter))
4955                 be_eqd_update(adapter, false);
4956
4957         if (adapter->flags & BE_FLAGS_EVT_INCOMPATIBLE_SFP)
4958                 be_log_sfp_info(adapter);
4959
4960 reschedule:
4961         adapter->work_counter++;
4962         schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
4963 }
4964
4965 static void be_unmap_pci_bars(struct be_adapter *adapter)
4966 {
4967         if (adapter->csr)
4968                 pci_iounmap(adapter->pdev, adapter->csr);
4969         if (adapter->db)
4970                 pci_iounmap(adapter->pdev, adapter->db);
4971 }
4972
4973 static int db_bar(struct be_adapter *adapter)
4974 {
4975         if (lancer_chip(adapter) || be_virtfn(adapter))
4976                 return 0;
4977         else
4978                 return 4;
4979 }
4980
4981 static int be_roce_map_pci_bars(struct be_adapter *adapter)
4982 {
4983         if (skyhawk_chip(adapter)) {
4984                 adapter->roce_db.size = 4096;
4985                 adapter->roce_db.io_addr = pci_resource_start(adapter->pdev,
4986                                                               db_bar(adapter));
4987                 adapter->roce_db.total_size = pci_resource_len(adapter->pdev,
4988                                                                db_bar(adapter));
4989         }
4990         return 0;
4991 }
4992
4993 static int be_map_pci_bars(struct be_adapter *adapter)
4994 {
4995         struct pci_dev *pdev = adapter->pdev;
4996         u8 __iomem *addr;
4997         u32 sli_intf;
4998
4999         pci_read_config_dword(adapter->pdev, SLI_INTF_REG_OFFSET, &sli_intf);
5000         adapter->sli_family = (sli_intf & SLI_INTF_FAMILY_MASK) >>
5001                                 SLI_INTF_FAMILY_SHIFT;
5002         adapter->virtfn = (sli_intf & SLI_INTF_FT_MASK) ? 1 : 0;
5003
5004         if (BEx_chip(adapter) && be_physfn(adapter)) {
5005                 adapter->csr = pci_iomap(pdev, 2, 0);
5006                 if (!adapter->csr)
5007                         return -ENOMEM;
5008         }
5009
5010         addr = pci_iomap(pdev, db_bar(adapter), 0);
5011         if (!addr)
5012                 goto pci_map_err;
5013         adapter->db = addr;
5014
5015         if (skyhawk_chip(adapter) || BEx_chip(adapter)) {
5016                 if (be_physfn(adapter)) {
5017                         /* PCICFG is the 2nd BAR in BE2 */
5018                         addr = pci_iomap(pdev, BE2_chip(adapter) ? 1 : 0, 0);
5019                         if (!addr)
5020                                 goto pci_map_err;
5021                         adapter->pcicfg = addr;
5022                 } else {
5023                         adapter->pcicfg = adapter->db + SRIOV_VF_PCICFG_OFFSET;
5024                 }
5025         }
5026
5027         be_roce_map_pci_bars(adapter);
5028         return 0;
5029
5030 pci_map_err:
5031         dev_err(&pdev->dev, "Error in mapping PCI BARs\n");
5032         be_unmap_pci_bars(adapter);
5033         return -ENOMEM;
5034 }
5035
5036 static void be_drv_cleanup(struct be_adapter *adapter)
5037 {
5038         struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
5039         struct device *dev = &adapter->pdev->dev;
5040
5041         if (mem->va)
5042                 dma_free_coherent(dev, mem->size, mem->va, mem->dma);
5043
5044         mem = &adapter->rx_filter;
5045         if (mem->va)
5046                 dma_free_coherent(dev, mem->size, mem->va, mem->dma);
5047
5048         mem = &adapter->stats_cmd;
5049         if (mem->va)
5050                 dma_free_coherent(dev, mem->size, mem->va, mem->dma);
5051 }
5052
5053 /* Allocate and initialize various fields in be_adapter struct */
5054 static int be_drv_init(struct be_adapter *adapter)
5055 {
5056         struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
5057         struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
5058         struct be_dma_mem *rx_filter = &adapter->rx_filter;
5059         struct be_dma_mem *stats_cmd = &adapter->stats_cmd;
5060         struct device *dev = &adapter->pdev->dev;
5061         int status = 0;
5062
5063         mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
5064         mbox_mem_alloc->va = dma_zalloc_coherent(dev, mbox_mem_alloc->size,
5065                                                  &mbox_mem_alloc->dma,
5066                                                  GFP_KERNEL);
5067         if (!mbox_mem_alloc->va)
5068                 return -ENOMEM;
5069
5070         mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
5071         mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
5072         mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
5073
5074         rx_filter->size = sizeof(struct be_cmd_req_rx_filter);
5075         rx_filter->va = dma_zalloc_coherent(dev, rx_filter->size,
5076                                             &rx_filter->dma, GFP_KERNEL);
5077         if (!rx_filter->va) {
5078                 status = -ENOMEM;
5079                 goto free_mbox;
5080         }
5081
5082         if (lancer_chip(adapter))
5083                 stats_cmd->size = sizeof(struct lancer_cmd_req_pport_stats);
5084         else if (BE2_chip(adapter))
5085                 stats_cmd->size = sizeof(struct be_cmd_req_get_stats_v0);
5086         else if (BE3_chip(adapter))
5087                 stats_cmd->size = sizeof(struct be_cmd_req_get_stats_v1);
5088         else
5089                 stats_cmd->size = sizeof(struct be_cmd_req_get_stats_v2);
5090         stats_cmd->va = dma_zalloc_coherent(dev, stats_cmd->size,
5091                                             &stats_cmd->dma, GFP_KERNEL);
5092         if (!stats_cmd->va) {
5093                 status = -ENOMEM;
5094                 goto free_rx_filter;
5095         }
5096
5097         mutex_init(&adapter->mbox_lock);
5098         spin_lock_init(&adapter->mcc_lock);
5099         spin_lock_init(&adapter->mcc_cq_lock);
5100         init_completion(&adapter->et_cmd_compl);
5101
5102         pci_save_state(adapter->pdev);
5103
5104         INIT_DELAYED_WORK(&adapter->work, be_worker);
5105         INIT_DELAYED_WORK(&adapter->be_err_detection_work,
5106                           be_err_detection_task);
5107
5108         adapter->rx_fc = true;
5109         adapter->tx_fc = true;
5110
5111         /* Must be a power of 2 or else MODULO will BUG_ON */
5112         adapter->be_get_temp_freq = 64;
5113
5114         return 0;
5115
5116 free_rx_filter:
5117         dma_free_coherent(dev, rx_filter->size, rx_filter->va, rx_filter->dma);
5118 free_mbox:
5119         dma_free_coherent(dev, mbox_mem_alloc->size, mbox_mem_alloc->va,
5120                           mbox_mem_alloc->dma);
5121         return status;
5122 }
5123
5124 static void be_remove(struct pci_dev *pdev)
5125 {
5126         struct be_adapter *adapter = pci_get_drvdata(pdev);
5127
5128         if (!adapter)
5129                 return;
5130
5131         be_roce_dev_remove(adapter);
5132         be_intr_set(adapter, false);
5133
5134         be_cancel_err_detection(adapter);
5135
5136         unregister_netdev(adapter->netdev);
5137
5138         be_clear(adapter);
5139
5140         /* tell fw we're done with firing cmds */
5141         be_cmd_fw_clean(adapter);
5142
5143         be_unmap_pci_bars(adapter);
5144         be_drv_cleanup(adapter);
5145
5146         pci_disable_pcie_error_reporting(pdev);
5147
5148         pci_release_regions(pdev);
5149         pci_disable_device(pdev);
5150
5151         free_netdev(adapter->netdev);
5152 }
5153
5154 static ssize_t be_hwmon_show_temp(struct device *dev,
5155                                   struct device_attribute *dev_attr,
5156                                   char *buf)
5157 {
5158         struct be_adapter *adapter = dev_get_drvdata(dev);
5159
5160         /* Unit: millidegree Celsius */
5161         if (adapter->hwmon_info.be_on_die_temp == BE_INVALID_DIE_TEMP)
5162                 return -EIO;
5163         else
5164                 return sprintf(buf, "%u\n",
5165                                adapter->hwmon_info.be_on_die_temp * 1000);
5166 }
5167
5168 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
5169                           be_hwmon_show_temp, NULL, 1);
5170
5171 static struct attribute *be_hwmon_attrs[] = {
5172         &sensor_dev_attr_temp1_input.dev_attr.attr,
5173         NULL
5174 };
5175
5176 ATTRIBUTE_GROUPS(be_hwmon);
5177
5178 static char *mc_name(struct be_adapter *adapter)
5179 {
5180         char *str = ""; /* default */
5181
5182         switch (adapter->mc_type) {
5183         case UMC:
5184                 str = "UMC";
5185                 break;
5186         case FLEX10:
5187                 str = "FLEX10";
5188                 break;
5189         case vNIC1:
5190                 str = "vNIC-1";
5191                 break;
5192         case nPAR:
5193                 str = "nPAR";
5194                 break;
5195         case UFP:
5196                 str = "UFP";
5197                 break;
5198         case vNIC2:
5199                 str = "vNIC-2";
5200                 break;
5201         default:
5202                 str = "";
5203         }
5204
5205         return str;
5206 }
5207
5208 static inline char *func_name(struct be_adapter *adapter)
5209 {
5210         return be_physfn(adapter) ? "PF" : "VF";
5211 }
5212
5213 static inline char *nic_name(struct pci_dev *pdev)
5214 {
5215         switch (pdev->device) {
5216         case OC_DEVICE_ID1:
5217                 return OC_NAME;
5218         case OC_DEVICE_ID2:
5219                 return OC_NAME_BE;
5220         case OC_DEVICE_ID3:
5221         case OC_DEVICE_ID4:
5222                 return OC_NAME_LANCER;
5223         case BE_DEVICE_ID2:
5224                 return BE3_NAME;
5225         case OC_DEVICE_ID5:
5226         case OC_DEVICE_ID6:
5227                 return OC_NAME_SH;
5228         default:
5229                 return BE_NAME;
5230         }
5231 }
5232
5233 static int be_probe(struct pci_dev *pdev, const struct pci_device_id *pdev_id)
5234 {
5235         struct be_adapter *adapter;
5236         struct net_device *netdev;
5237         int status = 0;
5238
5239         dev_info(&pdev->dev, "%s version is %s\n", DRV_NAME, DRV_VER);
5240
5241         status = pci_enable_device(pdev);
5242         if (status)
5243                 goto do_none;
5244
5245         status = pci_request_regions(pdev, DRV_NAME);
5246         if (status)
5247                 goto disable_dev;
5248         pci_set_master(pdev);
5249
5250         netdev = alloc_etherdev_mqs(sizeof(*adapter), MAX_TX_QS, MAX_RX_QS);
5251         if (!netdev) {
5252                 status = -ENOMEM;
5253                 goto rel_reg;
5254         }
5255         adapter = netdev_priv(netdev);
5256         adapter->pdev = pdev;
5257         pci_set_drvdata(pdev, adapter);
5258         adapter->netdev = netdev;
5259         SET_NETDEV_DEV(netdev, &pdev->dev);
5260
5261         status = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
5262         if (!status) {
5263                 netdev->features |= NETIF_F_HIGHDMA;
5264         } else {
5265                 status = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
5266                 if (status) {
5267                         dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
5268                         goto free_netdev;
5269                 }
5270         }
5271
5272         status = pci_enable_pcie_error_reporting(pdev);
5273         if (!status)
5274                 dev_info(&pdev->dev, "PCIe error reporting enabled\n");
5275
5276         status = be_map_pci_bars(adapter);
5277         if (status)
5278                 goto free_netdev;
5279
5280         status = be_drv_init(adapter);
5281         if (status)
5282                 goto unmap_bars;
5283
5284         status = be_setup(adapter);
5285         if (status)
5286                 goto drv_cleanup;
5287
5288         be_netdev_init(netdev);
5289         status = register_netdev(netdev);
5290         if (status != 0)
5291                 goto unsetup;
5292
5293         be_roce_dev_add(adapter);
5294
5295         be_schedule_err_detection(adapter);
5296
5297         /* On Die temperature not supported for VF. */
5298         if (be_physfn(adapter) && IS_ENABLED(CONFIG_BE2NET_HWMON)) {
5299                 adapter->hwmon_info.hwmon_dev =
5300                         devm_hwmon_device_register_with_groups(&pdev->dev,
5301                                                                DRV_NAME,
5302                                                                adapter,
5303                                                                be_hwmon_groups);
5304                 adapter->hwmon_info.be_on_die_temp = BE_INVALID_DIE_TEMP;
5305         }
5306
5307         dev_info(&pdev->dev, "%s: %s %s port %c\n", nic_name(pdev),
5308                  func_name(adapter), mc_name(adapter), adapter->port_name);
5309
5310         return 0;
5311
5312 unsetup:
5313         be_clear(adapter);
5314 drv_cleanup:
5315         be_drv_cleanup(adapter);
5316 unmap_bars:
5317         be_unmap_pci_bars(adapter);
5318 free_netdev:
5319         free_netdev(netdev);
5320 rel_reg:
5321         pci_release_regions(pdev);
5322 disable_dev:
5323         pci_disable_device(pdev);
5324 do_none:
5325         dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
5326         return status;
5327 }
5328
5329 static int be_suspend(struct pci_dev *pdev, pm_message_t state)
5330 {
5331         struct be_adapter *adapter = pci_get_drvdata(pdev);
5332
5333         if (adapter->wol_en)
5334                 be_setup_wol(adapter, true);
5335
5336         be_intr_set(adapter, false);
5337         be_cancel_err_detection(adapter);
5338
5339         be_cleanup(adapter);
5340
5341         pci_save_state(pdev);
5342         pci_disable_device(pdev);
5343         pci_set_power_state(pdev, pci_choose_state(pdev, state));
5344         return 0;
5345 }
5346
5347 static int be_pci_resume(struct pci_dev *pdev)
5348 {
5349         struct be_adapter *adapter = pci_get_drvdata(pdev);
5350         int status = 0;
5351
5352         status = pci_enable_device(pdev);
5353         if (status)
5354                 return status;
5355
5356         pci_restore_state(pdev);
5357
5358         status = be_resume(adapter);
5359         if (status)
5360                 return status;
5361
5362         be_schedule_err_detection(adapter);
5363
5364         if (adapter->wol_en)
5365                 be_setup_wol(adapter, false);
5366
5367         return 0;
5368 }
5369
5370 /*
5371  * An FLR will stop BE from DMAing any data.
5372  */
5373 static void be_shutdown(struct pci_dev *pdev)
5374 {
5375         struct be_adapter *adapter = pci_get_drvdata(pdev);
5376
5377         if (!adapter)
5378                 return;
5379
5380         be_roce_dev_shutdown(adapter);
5381         cancel_delayed_work_sync(&adapter->work);
5382         be_cancel_err_detection(adapter);
5383
5384         netif_device_detach(adapter->netdev);
5385
5386         be_cmd_reset_function(adapter);
5387
5388         pci_disable_device(pdev);
5389 }
5390
5391 static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
5392                                             pci_channel_state_t state)
5393 {
5394         struct be_adapter *adapter = pci_get_drvdata(pdev);
5395
5396         dev_err(&adapter->pdev->dev, "EEH error detected\n");
5397
5398         if (!be_check_error(adapter, BE_ERROR_EEH)) {
5399                 be_set_error(adapter, BE_ERROR_EEH);
5400
5401                 be_cancel_err_detection(adapter);
5402
5403                 be_cleanup(adapter);
5404         }
5405
5406         if (state == pci_channel_io_perm_failure)
5407                 return PCI_ERS_RESULT_DISCONNECT;
5408
5409         pci_disable_device(pdev);
5410
5411         /* The error could cause the FW to trigger a flash debug dump.
5412          * Resetting the card while flash dump is in progress
5413          * can cause it not to recover; wait for it to finish.
5414          * Wait only for first function as it is needed only once per
5415          * adapter.
5416          */
5417         if (pdev->devfn == 0)
5418                 ssleep(30);
5419
5420         return PCI_ERS_RESULT_NEED_RESET;
5421 }
5422
5423 static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
5424 {
5425         struct be_adapter *adapter = pci_get_drvdata(pdev);
5426         int status;
5427
5428         dev_info(&adapter->pdev->dev, "EEH reset\n");
5429
5430         status = pci_enable_device(pdev);
5431         if (status)
5432                 return PCI_ERS_RESULT_DISCONNECT;
5433
5434         pci_set_master(pdev);
5435         pci_restore_state(pdev);
5436
5437         /* Check if card is ok and fw is ready */
5438         dev_info(&adapter->pdev->dev,
5439                  "Waiting for FW to be ready after EEH reset\n");
5440         status = be_fw_wait_ready(adapter);
5441         if (status)
5442                 return PCI_ERS_RESULT_DISCONNECT;
5443
5444         pci_cleanup_aer_uncorrect_error_status(pdev);
5445         be_clear_error(adapter, BE_CLEAR_ALL);
5446         return PCI_ERS_RESULT_RECOVERED;
5447 }
5448
5449 static void be_eeh_resume(struct pci_dev *pdev)
5450 {
5451         int status = 0;
5452         struct be_adapter *adapter = pci_get_drvdata(pdev);
5453
5454         dev_info(&adapter->pdev->dev, "EEH resume\n");
5455
5456         pci_save_state(pdev);
5457
5458         status = be_resume(adapter);
5459         if (status)
5460                 goto err;
5461
5462         be_schedule_err_detection(adapter);
5463         return;
5464 err:
5465         dev_err(&adapter->pdev->dev, "EEH resume failed\n");
5466 }
5467
5468 static int be_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
5469 {
5470         struct be_adapter *adapter = pci_get_drvdata(pdev);
5471         u16 num_vf_qs;
5472         int status;
5473
5474         if (!num_vfs)
5475                 be_vf_clear(adapter);
5476
5477         adapter->num_vfs = num_vfs;
5478
5479         if (adapter->num_vfs == 0 && pci_vfs_assigned(pdev)) {
5480                 dev_warn(&pdev->dev,
5481                          "Cannot disable VFs while they are assigned\n");
5482                 return -EBUSY;
5483         }
5484
5485         /* When the HW is in SRIOV capable configuration, the PF-pool resources
5486          * are equally distributed across the max-number of VFs. The user may
5487          * request only a subset of the max-vfs to be enabled.
5488          * Based on num_vfs, redistribute the resources across num_vfs so that
5489          * each VF will have access to more number of resources.
5490          * This facility is not available in BE3 FW.
5491          * Also, this is done by FW in Lancer chip.
5492          */
5493         if (skyhawk_chip(adapter) && !pci_num_vf(pdev)) {
5494                 num_vf_qs = be_calculate_vf_qs(adapter, adapter->num_vfs);
5495                 status = be_cmd_set_sriov_config(adapter, adapter->pool_res,
5496                                                  adapter->num_vfs, num_vf_qs);
5497                 if (status)
5498                         dev_err(&pdev->dev,
5499                                 "Failed to optimize SR-IOV resources\n");
5500         }
5501
5502         status = be_get_resources(adapter);
5503         if (status)
5504                 return be_cmd_status(status);
5505
5506         /* Updating real_num_tx/rx_queues() requires rtnl_lock() */
5507         rtnl_lock();
5508         status = be_update_queues(adapter);
5509         rtnl_unlock();
5510         if (status)
5511                 return be_cmd_status(status);
5512
5513         if (adapter->num_vfs)
5514                 status = be_vf_setup(adapter);
5515
5516         if (!status)
5517                 return adapter->num_vfs;
5518
5519         return 0;
5520 }
5521
5522 static const struct pci_error_handlers be_eeh_handlers = {
5523         .error_detected = be_eeh_err_detected,
5524         .slot_reset = be_eeh_reset,
5525         .resume = be_eeh_resume,
5526 };
5527
5528 static struct pci_driver be_driver = {
5529         .name = DRV_NAME,
5530         .id_table = be_dev_ids,
5531         .probe = be_probe,
5532         .remove = be_remove,
5533         .suspend = be_suspend,
5534         .resume = be_pci_resume,
5535         .shutdown = be_shutdown,
5536         .sriov_configure = be_pci_sriov_configure,
5537         .err_handler = &be_eeh_handlers
5538 };
5539
5540 static int __init be_init_module(void)
5541 {
5542         if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
5543             rx_frag_size != 2048) {
5544                 printk(KERN_WARNING DRV_NAME
5545                         " : Module param rx_frag_size must be 2048/4096/8192."
5546                         " Using 2048\n");
5547                 rx_frag_size = 2048;
5548         }
5549
5550         if (num_vfs > 0) {
5551                 pr_info(DRV_NAME " : Module param num_vfs is obsolete.");
5552                 pr_info(DRV_NAME " : Use sysfs method to enable VFs\n");
5553         }
5554
5555         return pci_register_driver(&be_driver);
5556 }
5557 module_init(be_init_module);
5558
5559 static void __exit be_exit_module(void)
5560 {
5561         pci_unregister_driver(&be_driver);
5562 }
5563 module_exit(be_exit_module);