Merge tag 'sunxi-fixes-for-4.3' of https://git.kernel.org/pub/scm/linux/kernel/git...
[linux-drm-fsl-dcu.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;
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-R ASICs have a bug wherein Packets that are 32 bytes or
1127          * less may cause a transmit stall on that port. So the work-around is
1128          * to pad short packets (<= 32 bytes) to a 36-byte length.
1129          */
1130         if (unlikely(!BEx_chip(adapter) && skb->len <= 32)) {
1131                 if (skb_put_padto(skb, 36))
1132                         return NULL;
1133         }
1134
1135         if (BEx_chip(adapter) || lancer_chip(adapter)) {
1136                 skb = be_lancer_xmit_workarounds(adapter, skb, wrb_params);
1137                 if (!skb)
1138                         return NULL;
1139         }
1140
1141         return skb;
1142 }
1143
1144 static void be_xmit_flush(struct be_adapter *adapter, struct be_tx_obj *txo)
1145 {
1146         struct be_queue_info *txq = &txo->q;
1147         struct be_eth_hdr_wrb *hdr = queue_index_node(txq, txo->last_req_hdr);
1148
1149         /* Mark the last request eventable if it hasn't been marked already */
1150         if (!(hdr->dw[2] & cpu_to_le32(TX_HDR_WRB_EVT)))
1151                 hdr->dw[2] |= cpu_to_le32(TX_HDR_WRB_EVT | TX_HDR_WRB_COMPL);
1152
1153         /* compose a dummy wrb if there are odd set of wrbs to notify */
1154         if (!lancer_chip(adapter) && (txo->pend_wrb_cnt & 1)) {
1155                 wrb_fill_dummy(queue_head_node(txq));
1156                 queue_head_inc(txq);
1157                 atomic_inc(&txq->used);
1158                 txo->pend_wrb_cnt++;
1159                 hdr->dw[2] &= ~cpu_to_le32(TX_HDR_WRB_NUM_MASK <<
1160                                            TX_HDR_WRB_NUM_SHIFT);
1161                 hdr->dw[2] |= cpu_to_le32((txo->last_req_wrb_cnt + 1) <<
1162                                           TX_HDR_WRB_NUM_SHIFT);
1163         }
1164         be_txq_notify(adapter, txo, txo->pend_wrb_cnt);
1165         txo->pend_wrb_cnt = 0;
1166 }
1167
1168 /* OS2BMC related */
1169
1170 #define DHCP_CLIENT_PORT        68
1171 #define DHCP_SERVER_PORT        67
1172 #define NET_BIOS_PORT1          137
1173 #define NET_BIOS_PORT2          138
1174 #define DHCPV6_RAS_PORT         547
1175
1176 #define is_mc_allowed_on_bmc(adapter, eh)       \
1177         (!is_multicast_filt_enabled(adapter) && \
1178          is_multicast_ether_addr(eh->h_dest) && \
1179          !is_broadcast_ether_addr(eh->h_dest))
1180
1181 #define is_bc_allowed_on_bmc(adapter, eh)       \
1182         (!is_broadcast_filt_enabled(adapter) && \
1183          is_broadcast_ether_addr(eh->h_dest))
1184
1185 #define is_arp_allowed_on_bmc(adapter, skb)     \
1186         (is_arp(skb) && is_arp_filt_enabled(adapter))
1187
1188 #define is_broadcast_packet(eh, adapter)        \
1189                 (is_multicast_ether_addr(eh->h_dest) && \
1190                 !compare_ether_addr(eh->h_dest, adapter->netdev->broadcast))
1191
1192 #define is_arp(skb)     (skb->protocol == htons(ETH_P_ARP))
1193
1194 #define is_arp_filt_enabled(adapter)    \
1195                 (adapter->bmc_filt_mask & (BMC_FILT_BROADCAST_ARP))
1196
1197 #define is_dhcp_client_filt_enabled(adapter)    \
1198                 (adapter->bmc_filt_mask & BMC_FILT_BROADCAST_DHCP_CLIENT)
1199
1200 #define is_dhcp_srvr_filt_enabled(adapter)      \
1201                 (adapter->bmc_filt_mask & BMC_FILT_BROADCAST_DHCP_SERVER)
1202
1203 #define is_nbios_filt_enabled(adapter)  \
1204                 (adapter->bmc_filt_mask & BMC_FILT_BROADCAST_NET_BIOS)
1205
1206 #define is_ipv6_na_filt_enabled(adapter)        \
1207                 (adapter->bmc_filt_mask &       \
1208                         BMC_FILT_MULTICAST_IPV6_NEIGH_ADVER)
1209
1210 #define is_ipv6_ra_filt_enabled(adapter)        \
1211                 (adapter->bmc_filt_mask & BMC_FILT_MULTICAST_IPV6_RA)
1212
1213 #define is_ipv6_ras_filt_enabled(adapter)       \
1214                 (adapter->bmc_filt_mask & BMC_FILT_MULTICAST_IPV6_RAS)
1215
1216 #define is_broadcast_filt_enabled(adapter)      \
1217                 (adapter->bmc_filt_mask & BMC_FILT_BROADCAST)
1218
1219 #define is_multicast_filt_enabled(adapter)      \
1220                 (adapter->bmc_filt_mask & BMC_FILT_MULTICAST)
1221
1222 static bool be_send_pkt_to_bmc(struct be_adapter *adapter,
1223                                struct sk_buff **skb)
1224 {
1225         struct ethhdr *eh = (struct ethhdr *)(*skb)->data;
1226         bool os2bmc = false;
1227
1228         if (!be_is_os2bmc_enabled(adapter))
1229                 goto done;
1230
1231         if (!is_multicast_ether_addr(eh->h_dest))
1232                 goto done;
1233
1234         if (is_mc_allowed_on_bmc(adapter, eh) ||
1235             is_bc_allowed_on_bmc(adapter, eh) ||
1236             is_arp_allowed_on_bmc(adapter, (*skb))) {
1237                 os2bmc = true;
1238                 goto done;
1239         }
1240
1241         if ((*skb)->protocol == htons(ETH_P_IPV6)) {
1242                 struct ipv6hdr *hdr = ipv6_hdr((*skb));
1243                 u8 nexthdr = hdr->nexthdr;
1244
1245                 if (nexthdr == IPPROTO_ICMPV6) {
1246                         struct icmp6hdr *icmp6 = icmp6_hdr((*skb));
1247
1248                         switch (icmp6->icmp6_type) {
1249                         case NDISC_ROUTER_ADVERTISEMENT:
1250                                 os2bmc = is_ipv6_ra_filt_enabled(adapter);
1251                                 goto done;
1252                         case NDISC_NEIGHBOUR_ADVERTISEMENT:
1253                                 os2bmc = is_ipv6_na_filt_enabled(adapter);
1254                                 goto done;
1255                         default:
1256                                 break;
1257                         }
1258                 }
1259         }
1260
1261         if (is_udp_pkt((*skb))) {
1262                 struct udphdr *udp = udp_hdr((*skb));
1263
1264                 switch (ntohs(udp->dest)) {
1265                 case DHCP_CLIENT_PORT:
1266                         os2bmc = is_dhcp_client_filt_enabled(adapter);
1267                         goto done;
1268                 case DHCP_SERVER_PORT:
1269                         os2bmc = is_dhcp_srvr_filt_enabled(adapter);
1270                         goto done;
1271                 case NET_BIOS_PORT1:
1272                 case NET_BIOS_PORT2:
1273                         os2bmc = is_nbios_filt_enabled(adapter);
1274                         goto done;
1275                 case DHCPV6_RAS_PORT:
1276                         os2bmc = is_ipv6_ras_filt_enabled(adapter);
1277                         goto done;
1278                 default:
1279                         break;
1280                 }
1281         }
1282 done:
1283         /* For packets over a vlan, which are destined
1284          * to BMC, asic expects the vlan to be inline in the packet.
1285          */
1286         if (os2bmc)
1287                 *skb = be_insert_vlan_in_pkt(adapter, *skb, NULL);
1288
1289         return os2bmc;
1290 }
1291
1292 static netdev_tx_t be_xmit(struct sk_buff *skb, struct net_device *netdev)
1293 {
1294         struct be_adapter *adapter = netdev_priv(netdev);
1295         u16 q_idx = skb_get_queue_mapping(skb);
1296         struct be_tx_obj *txo = &adapter->tx_obj[q_idx];
1297         struct be_wrb_params wrb_params = { 0 };
1298         bool flush = !skb->xmit_more;
1299         u16 wrb_cnt;
1300
1301         skb = be_xmit_workarounds(adapter, skb, &wrb_params);
1302         if (unlikely(!skb))
1303                 goto drop;
1304
1305         be_get_wrb_params_from_skb(adapter, skb, &wrb_params);
1306
1307         wrb_cnt = be_xmit_enqueue(adapter, txo, skb, &wrb_params);
1308         if (unlikely(!wrb_cnt)) {
1309                 dev_kfree_skb_any(skb);
1310                 goto drop;
1311         }
1312
1313         /* if os2bmc is enabled and if the pkt is destined to bmc,
1314          * enqueue the pkt a 2nd time with mgmt bit set.
1315          */
1316         if (be_send_pkt_to_bmc(adapter, &skb)) {
1317                 BE_WRB_F_SET(wrb_params.features, OS2BMC, 1);
1318                 wrb_cnt = be_xmit_enqueue(adapter, txo, skb, &wrb_params);
1319                 if (unlikely(!wrb_cnt))
1320                         goto drop;
1321                 else
1322                         skb_get(skb);
1323         }
1324
1325         if (be_is_txq_full(txo)) {
1326                 netif_stop_subqueue(netdev, q_idx);
1327                 tx_stats(txo)->tx_stops++;
1328         }
1329
1330         if (flush || __netif_subqueue_stopped(netdev, q_idx))
1331                 be_xmit_flush(adapter, txo);
1332
1333         return NETDEV_TX_OK;
1334 drop:
1335         tx_stats(txo)->tx_drv_drops++;
1336         /* Flush the already enqueued tx requests */
1337         if (flush && txo->pend_wrb_cnt)
1338                 be_xmit_flush(adapter, txo);
1339
1340         return NETDEV_TX_OK;
1341 }
1342
1343 static int be_change_mtu(struct net_device *netdev, int new_mtu)
1344 {
1345         struct be_adapter *adapter = netdev_priv(netdev);
1346         struct device *dev = &adapter->pdev->dev;
1347
1348         if (new_mtu < BE_MIN_MTU || new_mtu > BE_MAX_MTU) {
1349                 dev_info(dev, "MTU must be between %d and %d bytes\n",
1350                          BE_MIN_MTU, BE_MAX_MTU);
1351                 return -EINVAL;
1352         }
1353
1354         dev_info(dev, "MTU changed from %d to %d bytes\n",
1355                  netdev->mtu, new_mtu);
1356         netdev->mtu = new_mtu;
1357         return 0;
1358 }
1359
1360 static inline bool be_in_all_promisc(struct be_adapter *adapter)
1361 {
1362         return (adapter->if_flags & BE_IF_FLAGS_ALL_PROMISCUOUS) ==
1363                         BE_IF_FLAGS_ALL_PROMISCUOUS;
1364 }
1365
1366 static int be_set_vlan_promisc(struct be_adapter *adapter)
1367 {
1368         struct device *dev = &adapter->pdev->dev;
1369         int status;
1370
1371         if (adapter->if_flags & BE_IF_FLAGS_VLAN_PROMISCUOUS)
1372                 return 0;
1373
1374         status = be_cmd_rx_filter(adapter, BE_IF_FLAGS_VLAN_PROMISCUOUS, ON);
1375         if (!status) {
1376                 dev_info(dev, "Enabled VLAN promiscuous mode\n");
1377                 adapter->if_flags |= BE_IF_FLAGS_VLAN_PROMISCUOUS;
1378         } else {
1379                 dev_err(dev, "Failed to enable VLAN promiscuous mode\n");
1380         }
1381         return status;
1382 }
1383
1384 static int be_clear_vlan_promisc(struct be_adapter *adapter)
1385 {
1386         struct device *dev = &adapter->pdev->dev;
1387         int status;
1388
1389         status = be_cmd_rx_filter(adapter, BE_IF_FLAGS_VLAN_PROMISCUOUS, OFF);
1390         if (!status) {
1391                 dev_info(dev, "Disabling VLAN promiscuous mode\n");
1392                 adapter->if_flags &= ~BE_IF_FLAGS_VLAN_PROMISCUOUS;
1393         }
1394         return status;
1395 }
1396
1397 /*
1398  * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
1399  * If the user configures more, place BE in vlan promiscuous mode.
1400  */
1401 static int be_vid_config(struct be_adapter *adapter)
1402 {
1403         struct device *dev = &adapter->pdev->dev;
1404         u16 vids[BE_NUM_VLANS_SUPPORTED];
1405         u16 num = 0, i = 0;
1406         int status = 0;
1407
1408         /* No need to further configure vids if in promiscuous mode */
1409         if (be_in_all_promisc(adapter))
1410                 return 0;
1411
1412         if (adapter->vlans_added > be_max_vlans(adapter))
1413                 return be_set_vlan_promisc(adapter);
1414
1415         /* Construct VLAN Table to give to HW */
1416         for_each_set_bit(i, adapter->vids, VLAN_N_VID)
1417                 vids[num++] = cpu_to_le16(i);
1418
1419         status = be_cmd_vlan_config(adapter, adapter->if_handle, vids, num, 0);
1420         if (status) {
1421                 dev_err(dev, "Setting HW VLAN filtering failed\n");
1422                 /* Set to VLAN promisc mode as setting VLAN filter failed */
1423                 if (addl_status(status) == MCC_ADDL_STATUS_INSUFFICIENT_VLANS ||
1424                     addl_status(status) ==
1425                                 MCC_ADDL_STATUS_INSUFFICIENT_RESOURCES)
1426                         return be_set_vlan_promisc(adapter);
1427         } else if (adapter->if_flags & BE_IF_FLAGS_VLAN_PROMISCUOUS) {
1428                 status = be_clear_vlan_promisc(adapter);
1429         }
1430         return status;
1431 }
1432
1433 static int be_vlan_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
1434 {
1435         struct be_adapter *adapter = netdev_priv(netdev);
1436         int status = 0;
1437
1438         /* Packets with VID 0 are always received by Lancer by default */
1439         if (lancer_chip(adapter) && vid == 0)
1440                 return status;
1441
1442         if (test_bit(vid, adapter->vids))
1443                 return status;
1444
1445         set_bit(vid, adapter->vids);
1446         adapter->vlans_added++;
1447
1448         status = be_vid_config(adapter);
1449         if (status) {
1450                 adapter->vlans_added--;
1451                 clear_bit(vid, adapter->vids);
1452         }
1453
1454         return status;
1455 }
1456
1457 static int be_vlan_rem_vid(struct net_device *netdev, __be16 proto, u16 vid)
1458 {
1459         struct be_adapter *adapter = netdev_priv(netdev);
1460
1461         /* Packets with VID 0 are always received by Lancer by default */
1462         if (lancer_chip(adapter) && vid == 0)
1463                 return 0;
1464
1465         clear_bit(vid, adapter->vids);
1466         adapter->vlans_added--;
1467
1468         return be_vid_config(adapter);
1469 }
1470
1471 static void be_clear_all_promisc(struct be_adapter *adapter)
1472 {
1473         be_cmd_rx_filter(adapter, BE_IF_FLAGS_ALL_PROMISCUOUS, OFF);
1474         adapter->if_flags &= ~BE_IF_FLAGS_ALL_PROMISCUOUS;
1475 }
1476
1477 static void be_set_all_promisc(struct be_adapter *adapter)
1478 {
1479         be_cmd_rx_filter(adapter, BE_IF_FLAGS_ALL_PROMISCUOUS, ON);
1480         adapter->if_flags |= BE_IF_FLAGS_ALL_PROMISCUOUS;
1481 }
1482
1483 static void be_set_mc_promisc(struct be_adapter *adapter)
1484 {
1485         int status;
1486
1487         if (adapter->if_flags & BE_IF_FLAGS_MCAST_PROMISCUOUS)
1488                 return;
1489
1490         status = be_cmd_rx_filter(adapter, BE_IF_FLAGS_MCAST_PROMISCUOUS, ON);
1491         if (!status)
1492                 adapter->if_flags |= BE_IF_FLAGS_MCAST_PROMISCUOUS;
1493 }
1494
1495 static void be_set_mc_list(struct be_adapter *adapter)
1496 {
1497         int status;
1498
1499         status = be_cmd_rx_filter(adapter, BE_IF_FLAGS_MULTICAST, ON);
1500         if (!status)
1501                 adapter->if_flags &= ~BE_IF_FLAGS_MCAST_PROMISCUOUS;
1502         else
1503                 be_set_mc_promisc(adapter);
1504 }
1505
1506 static void be_set_uc_list(struct be_adapter *adapter)
1507 {
1508         struct netdev_hw_addr *ha;
1509         int i = 1; /* First slot is claimed by the Primary MAC */
1510
1511         for (; adapter->uc_macs > 0; adapter->uc_macs--, i++)
1512                 be_cmd_pmac_del(adapter, adapter->if_handle,
1513                                 adapter->pmac_id[i], 0);
1514
1515         if (netdev_uc_count(adapter->netdev) > be_max_uc(adapter)) {
1516                 be_set_all_promisc(adapter);
1517                 return;
1518         }
1519
1520         netdev_for_each_uc_addr(ha, adapter->netdev) {
1521                 adapter->uc_macs++; /* First slot is for Primary MAC */
1522                 be_cmd_pmac_add(adapter, (u8 *)ha->addr, adapter->if_handle,
1523                                 &adapter->pmac_id[adapter->uc_macs], 0);
1524         }
1525 }
1526
1527 static void be_clear_uc_list(struct be_adapter *adapter)
1528 {
1529         int i;
1530
1531         for (i = 1; i < (adapter->uc_macs + 1); i++)
1532                 be_cmd_pmac_del(adapter, adapter->if_handle,
1533                                 adapter->pmac_id[i], 0);
1534         adapter->uc_macs = 0;
1535 }
1536
1537 static void be_set_rx_mode(struct net_device *netdev)
1538 {
1539         struct be_adapter *adapter = netdev_priv(netdev);
1540
1541         if (netdev->flags & IFF_PROMISC) {
1542                 be_set_all_promisc(adapter);
1543                 return;
1544         }
1545
1546         /* Interface was previously in promiscuous mode; disable it */
1547         if (be_in_all_promisc(adapter)) {
1548                 be_clear_all_promisc(adapter);
1549                 if (adapter->vlans_added)
1550                         be_vid_config(adapter);
1551         }
1552
1553         /* Enable multicast promisc if num configured exceeds what we support */
1554         if (netdev->flags & IFF_ALLMULTI ||
1555             netdev_mc_count(netdev) > be_max_mc(adapter)) {
1556                 be_set_mc_promisc(adapter);
1557                 return;
1558         }
1559
1560         if (netdev_uc_count(netdev) != adapter->uc_macs)
1561                 be_set_uc_list(adapter);
1562
1563         be_set_mc_list(adapter);
1564 }
1565
1566 static int be_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1567 {
1568         struct be_adapter *adapter = netdev_priv(netdev);
1569         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1570         int status;
1571
1572         if (!sriov_enabled(adapter))
1573                 return -EPERM;
1574
1575         if (!is_valid_ether_addr(mac) || vf >= adapter->num_vfs)
1576                 return -EINVAL;
1577
1578         /* Proceed further only if user provided MAC is different
1579          * from active MAC
1580          */
1581         if (ether_addr_equal(mac, vf_cfg->mac_addr))
1582                 return 0;
1583
1584         if (BEx_chip(adapter)) {
1585                 be_cmd_pmac_del(adapter, vf_cfg->if_handle, vf_cfg->pmac_id,
1586                                 vf + 1);
1587
1588                 status = be_cmd_pmac_add(adapter, mac, vf_cfg->if_handle,
1589                                          &vf_cfg->pmac_id, vf + 1);
1590         } else {
1591                 status = be_cmd_set_mac(adapter, mac, vf_cfg->if_handle,
1592                                         vf + 1);
1593         }
1594
1595         if (status) {
1596                 dev_err(&adapter->pdev->dev, "MAC %pM set on VF %d Failed: %#x",
1597                         mac, vf, status);
1598                 return be_cmd_status(status);
1599         }
1600
1601         ether_addr_copy(vf_cfg->mac_addr, mac);
1602
1603         return 0;
1604 }
1605
1606 static int be_get_vf_config(struct net_device *netdev, int vf,
1607                             struct ifla_vf_info *vi)
1608 {
1609         struct be_adapter *adapter = netdev_priv(netdev);
1610         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1611
1612         if (!sriov_enabled(adapter))
1613                 return -EPERM;
1614
1615         if (vf >= adapter->num_vfs)
1616                 return -EINVAL;
1617
1618         vi->vf = vf;
1619         vi->max_tx_rate = vf_cfg->tx_rate;
1620         vi->min_tx_rate = 0;
1621         vi->vlan = vf_cfg->vlan_tag & VLAN_VID_MASK;
1622         vi->qos = vf_cfg->vlan_tag >> VLAN_PRIO_SHIFT;
1623         memcpy(&vi->mac, vf_cfg->mac_addr, ETH_ALEN);
1624         vi->linkstate = adapter->vf_cfg[vf].plink_tracking;
1625         vi->spoofchk = adapter->vf_cfg[vf].spoofchk;
1626
1627         return 0;
1628 }
1629
1630 static int be_set_vf_tvt(struct be_adapter *adapter, int vf, u16 vlan)
1631 {
1632         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1633         u16 vids[BE_NUM_VLANS_SUPPORTED];
1634         int vf_if_id = vf_cfg->if_handle;
1635         int status;
1636
1637         /* Enable Transparent VLAN Tagging */
1638         status = be_cmd_set_hsw_config(adapter, vlan, vf + 1, vf_if_id, 0, 0);
1639         if (status)
1640                 return status;
1641
1642         /* Clear pre-programmed VLAN filters on VF if any, if TVT is enabled */
1643         vids[0] = 0;
1644         status = be_cmd_vlan_config(adapter, vf_if_id, vids, 1, vf + 1);
1645         if (!status)
1646                 dev_info(&adapter->pdev->dev,
1647                          "Cleared guest VLANs on VF%d", vf);
1648
1649         /* After TVT is enabled, disallow VFs to program VLAN filters */
1650         if (vf_cfg->privileges & BE_PRIV_FILTMGMT) {
1651                 status = be_cmd_set_fn_privileges(adapter, vf_cfg->privileges &
1652                                                   ~BE_PRIV_FILTMGMT, vf + 1);
1653                 if (!status)
1654                         vf_cfg->privileges &= ~BE_PRIV_FILTMGMT;
1655         }
1656         return 0;
1657 }
1658
1659 static int be_clear_vf_tvt(struct be_adapter *adapter, int vf)
1660 {
1661         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1662         struct device *dev = &adapter->pdev->dev;
1663         int status;
1664
1665         /* Reset Transparent VLAN Tagging. */
1666         status = be_cmd_set_hsw_config(adapter, BE_RESET_VLAN_TAG_ID, vf + 1,
1667                                        vf_cfg->if_handle, 0, 0);
1668         if (status)
1669                 return status;
1670
1671         /* Allow VFs to program VLAN filtering */
1672         if (!(vf_cfg->privileges & BE_PRIV_FILTMGMT)) {
1673                 status = be_cmd_set_fn_privileges(adapter, vf_cfg->privileges |
1674                                                   BE_PRIV_FILTMGMT, vf + 1);
1675                 if (!status) {
1676                         vf_cfg->privileges |= BE_PRIV_FILTMGMT;
1677                         dev_info(dev, "VF%d: FILTMGMT priv enabled", vf);
1678                 }
1679         }
1680
1681         dev_info(dev,
1682                  "Disable/re-enable i/f in VM to clear Transparent VLAN tag");
1683         return 0;
1684 }
1685
1686 static int be_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
1687 {
1688         struct be_adapter *adapter = netdev_priv(netdev);
1689         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1690         int status;
1691
1692         if (!sriov_enabled(adapter))
1693                 return -EPERM;
1694
1695         if (vf >= adapter->num_vfs || vlan > 4095 || qos > 7)
1696                 return -EINVAL;
1697
1698         if (vlan || qos) {
1699                 vlan |= qos << VLAN_PRIO_SHIFT;
1700                 status = be_set_vf_tvt(adapter, vf, vlan);
1701         } else {
1702                 status = be_clear_vf_tvt(adapter, vf);
1703         }
1704
1705         if (status) {
1706                 dev_err(&adapter->pdev->dev,
1707                         "VLAN %d config on VF %d failed : %#x\n", vlan, vf,
1708                         status);
1709                 return be_cmd_status(status);
1710         }
1711
1712         vf_cfg->vlan_tag = vlan;
1713         return 0;
1714 }
1715
1716 static int be_set_vf_tx_rate(struct net_device *netdev, int vf,
1717                              int min_tx_rate, int max_tx_rate)
1718 {
1719         struct be_adapter *adapter = netdev_priv(netdev);
1720         struct device *dev = &adapter->pdev->dev;
1721         int percent_rate, status = 0;
1722         u16 link_speed = 0;
1723         u8 link_status;
1724
1725         if (!sriov_enabled(adapter))
1726                 return -EPERM;
1727
1728         if (vf >= adapter->num_vfs)
1729                 return -EINVAL;
1730
1731         if (min_tx_rate)
1732                 return -EINVAL;
1733
1734         if (!max_tx_rate)
1735                 goto config_qos;
1736
1737         status = be_cmd_link_status_query(adapter, &link_speed,
1738                                           &link_status, 0);
1739         if (status)
1740                 goto err;
1741
1742         if (!link_status) {
1743                 dev_err(dev, "TX-rate setting not allowed when link is down\n");
1744                 status = -ENETDOWN;
1745                 goto err;
1746         }
1747
1748         if (max_tx_rate < 100 || max_tx_rate > link_speed) {
1749                 dev_err(dev, "TX-rate must be between 100 and %d Mbps\n",
1750                         link_speed);
1751                 status = -EINVAL;
1752                 goto err;
1753         }
1754
1755         /* On Skyhawk the QOS setting must be done only as a % value */
1756         percent_rate = link_speed / 100;
1757         if (skyhawk_chip(adapter) && (max_tx_rate % percent_rate)) {
1758                 dev_err(dev, "TX-rate must be a multiple of %d Mbps\n",
1759                         percent_rate);
1760                 status = -EINVAL;
1761                 goto err;
1762         }
1763
1764 config_qos:
1765         status = be_cmd_config_qos(adapter, max_tx_rate, link_speed, vf + 1);
1766         if (status)
1767                 goto err;
1768
1769         adapter->vf_cfg[vf].tx_rate = max_tx_rate;
1770         return 0;
1771
1772 err:
1773         dev_err(dev, "TX-rate setting of %dMbps on VF%d failed\n",
1774                 max_tx_rate, vf);
1775         return be_cmd_status(status);
1776 }
1777
1778 static int be_set_vf_link_state(struct net_device *netdev, int vf,
1779                                 int link_state)
1780 {
1781         struct be_adapter *adapter = netdev_priv(netdev);
1782         int status;
1783
1784         if (!sriov_enabled(adapter))
1785                 return -EPERM;
1786
1787         if (vf >= adapter->num_vfs)
1788                 return -EINVAL;
1789
1790         status = be_cmd_set_logical_link_config(adapter, link_state, vf+1);
1791         if (status) {
1792                 dev_err(&adapter->pdev->dev,
1793                         "Link state change on VF %d failed: %#x\n", vf, status);
1794                 return be_cmd_status(status);
1795         }
1796
1797         adapter->vf_cfg[vf].plink_tracking = link_state;
1798
1799         return 0;
1800 }
1801
1802 static int be_set_vf_spoofchk(struct net_device *netdev, int vf, bool enable)
1803 {
1804         struct be_adapter *adapter = netdev_priv(netdev);
1805         struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1806         u8 spoofchk;
1807         int status;
1808
1809         if (!sriov_enabled(adapter))
1810                 return -EPERM;
1811
1812         if (vf >= adapter->num_vfs)
1813                 return -EINVAL;
1814
1815         if (BEx_chip(adapter))
1816                 return -EOPNOTSUPP;
1817
1818         if (enable == vf_cfg->spoofchk)
1819                 return 0;
1820
1821         spoofchk = enable ? ENABLE_MAC_SPOOFCHK : DISABLE_MAC_SPOOFCHK;
1822
1823         status = be_cmd_set_hsw_config(adapter, 0, vf + 1, vf_cfg->if_handle,
1824                                        0, spoofchk);
1825         if (status) {
1826                 dev_err(&adapter->pdev->dev,
1827                         "Spoofchk change on VF %d failed: %#x\n", vf, status);
1828                 return be_cmd_status(status);
1829         }
1830
1831         vf_cfg->spoofchk = enable;
1832         return 0;
1833 }
1834
1835 static void be_aic_update(struct be_aic_obj *aic, u64 rx_pkts, u64 tx_pkts,
1836                           ulong now)
1837 {
1838         aic->rx_pkts_prev = rx_pkts;
1839         aic->tx_reqs_prev = tx_pkts;
1840         aic->jiffies = now;
1841 }
1842
1843 static int be_get_new_eqd(struct be_eq_obj *eqo)
1844 {
1845         struct be_adapter *adapter = eqo->adapter;
1846         int eqd, start;
1847         struct be_aic_obj *aic;
1848         struct be_rx_obj *rxo;
1849         struct be_tx_obj *txo;
1850         u64 rx_pkts = 0, tx_pkts = 0;
1851         ulong now;
1852         u32 pps, delta;
1853         int i;
1854
1855         aic = &adapter->aic_obj[eqo->idx];
1856         if (!aic->enable) {
1857                 if (aic->jiffies)
1858                         aic->jiffies = 0;
1859                 eqd = aic->et_eqd;
1860                 return eqd;
1861         }
1862
1863         for_all_rx_queues_on_eq(adapter, eqo, rxo, i) {
1864                 do {
1865                         start = u64_stats_fetch_begin_irq(&rxo->stats.sync);
1866                         rx_pkts += rxo->stats.rx_pkts;
1867                 } while (u64_stats_fetch_retry_irq(&rxo->stats.sync, start));
1868         }
1869
1870         for_all_tx_queues_on_eq(adapter, eqo, txo, i) {
1871                 do {
1872                         start = u64_stats_fetch_begin_irq(&txo->stats.sync);
1873                         tx_pkts += txo->stats.tx_reqs;
1874                 } while (u64_stats_fetch_retry_irq(&txo->stats.sync, start));
1875         }
1876
1877         /* Skip, if wrapped around or first calculation */
1878         now = jiffies;
1879         if (!aic->jiffies || time_before(now, aic->jiffies) ||
1880             rx_pkts < aic->rx_pkts_prev ||
1881             tx_pkts < aic->tx_reqs_prev) {
1882                 be_aic_update(aic, rx_pkts, tx_pkts, now);
1883                 return aic->prev_eqd;
1884         }
1885
1886         delta = jiffies_to_msecs(now - aic->jiffies);
1887         if (delta == 0)
1888                 return aic->prev_eqd;
1889
1890         pps = (((u32)(rx_pkts - aic->rx_pkts_prev) * 1000) / delta) +
1891                 (((u32)(tx_pkts - aic->tx_reqs_prev) * 1000) / delta);
1892         eqd = (pps / 15000) << 2;
1893
1894         if (eqd < 8)
1895                 eqd = 0;
1896         eqd = min_t(u32, eqd, aic->max_eqd);
1897         eqd = max_t(u32, eqd, aic->min_eqd);
1898
1899         be_aic_update(aic, rx_pkts, tx_pkts, now);
1900
1901         return eqd;
1902 }
1903
1904 /* For Skyhawk-R only */
1905 static u32 be_get_eq_delay_mult_enc(struct be_eq_obj *eqo)
1906 {
1907         struct be_adapter *adapter = eqo->adapter;
1908         struct be_aic_obj *aic = &adapter->aic_obj[eqo->idx];
1909         ulong now = jiffies;
1910         int eqd;
1911         u32 mult_enc;
1912
1913         if (!aic->enable)
1914                 return 0;
1915
1916         if (time_before_eq(now, aic->jiffies) ||
1917             jiffies_to_msecs(now - aic->jiffies) < 1)
1918                 eqd = aic->prev_eqd;
1919         else
1920                 eqd = be_get_new_eqd(eqo);
1921
1922         if (eqd > 100)
1923                 mult_enc = R2I_DLY_ENC_1;
1924         else if (eqd > 60)
1925                 mult_enc = R2I_DLY_ENC_2;
1926         else if (eqd > 20)
1927                 mult_enc = R2I_DLY_ENC_3;
1928         else
1929                 mult_enc = R2I_DLY_ENC_0;
1930
1931         aic->prev_eqd = eqd;
1932
1933         return mult_enc;
1934 }
1935
1936 void be_eqd_update(struct be_adapter *adapter, bool force_update)
1937 {
1938         struct be_set_eqd set_eqd[MAX_EVT_QS];
1939         struct be_aic_obj *aic;
1940         struct be_eq_obj *eqo;
1941         int i, num = 0, eqd;
1942
1943         for_all_evt_queues(adapter, eqo, i) {
1944                 aic = &adapter->aic_obj[eqo->idx];
1945                 eqd = be_get_new_eqd(eqo);
1946                 if (force_update || eqd != aic->prev_eqd) {
1947                         set_eqd[num].delay_multiplier = (eqd * 65)/100;
1948                         set_eqd[num].eq_id = eqo->q.id;
1949                         aic->prev_eqd = eqd;
1950                         num++;
1951                 }
1952         }
1953
1954         if (num)
1955                 be_cmd_modify_eqd(adapter, set_eqd, num);
1956 }
1957
1958 static void be_rx_stats_update(struct be_rx_obj *rxo,
1959                                struct be_rx_compl_info *rxcp)
1960 {
1961         struct be_rx_stats *stats = rx_stats(rxo);
1962
1963         u64_stats_update_begin(&stats->sync);
1964         stats->rx_compl++;
1965         stats->rx_bytes += rxcp->pkt_size;
1966         stats->rx_pkts++;
1967         if (rxcp->tunneled)
1968                 stats->rx_vxlan_offload_pkts++;
1969         if (rxcp->pkt_type == BE_MULTICAST_PACKET)
1970                 stats->rx_mcast_pkts++;
1971         if (rxcp->err)
1972                 stats->rx_compl_err++;
1973         u64_stats_update_end(&stats->sync);
1974 }
1975
1976 static inline bool csum_passed(struct be_rx_compl_info *rxcp)
1977 {
1978         /* L4 checksum is not reliable for non TCP/UDP packets.
1979          * Also ignore ipcksm for ipv6 pkts
1980          */
1981         return (rxcp->tcpf || rxcp->udpf) && rxcp->l4_csum &&
1982                 (rxcp->ip_csum || rxcp->ipv6) && !rxcp->err;
1983 }
1984
1985 static struct be_rx_page_info *get_rx_page_info(struct be_rx_obj *rxo)
1986 {
1987         struct be_adapter *adapter = rxo->adapter;
1988         struct be_rx_page_info *rx_page_info;
1989         struct be_queue_info *rxq = &rxo->q;
1990         u16 frag_idx = rxq->tail;
1991
1992         rx_page_info = &rxo->page_info_tbl[frag_idx];
1993         BUG_ON(!rx_page_info->page);
1994
1995         if (rx_page_info->last_frag) {
1996                 dma_unmap_page(&adapter->pdev->dev,
1997                                dma_unmap_addr(rx_page_info, bus),
1998                                adapter->big_page_size, DMA_FROM_DEVICE);
1999                 rx_page_info->last_frag = false;
2000         } else {
2001                 dma_sync_single_for_cpu(&adapter->pdev->dev,
2002                                         dma_unmap_addr(rx_page_info, bus),
2003                                         rx_frag_size, DMA_FROM_DEVICE);
2004         }
2005
2006         queue_tail_inc(rxq);
2007         atomic_dec(&rxq->used);
2008         return rx_page_info;
2009 }
2010
2011 /* Throwaway the data in the Rx completion */
2012 static void be_rx_compl_discard(struct be_rx_obj *rxo,
2013                                 struct be_rx_compl_info *rxcp)
2014 {
2015         struct be_rx_page_info *page_info;
2016         u16 i, num_rcvd = rxcp->num_rcvd;
2017
2018         for (i = 0; i < num_rcvd; i++) {
2019                 page_info = get_rx_page_info(rxo);
2020                 put_page(page_info->page);
2021                 memset(page_info, 0, sizeof(*page_info));
2022         }
2023 }
2024
2025 /*
2026  * skb_fill_rx_data forms a complete skb for an ether frame
2027  * indicated by rxcp.
2028  */
2029 static void skb_fill_rx_data(struct be_rx_obj *rxo, struct sk_buff *skb,
2030                              struct be_rx_compl_info *rxcp)
2031 {
2032         struct be_rx_page_info *page_info;
2033         u16 i, j;
2034         u16 hdr_len, curr_frag_len, remaining;
2035         u8 *start;
2036
2037         page_info = get_rx_page_info(rxo);
2038         start = page_address(page_info->page) + page_info->page_offset;
2039         prefetch(start);
2040
2041         /* Copy data in the first descriptor of this completion */
2042         curr_frag_len = min(rxcp->pkt_size, rx_frag_size);
2043
2044         skb->len = curr_frag_len;
2045         if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
2046                 memcpy(skb->data, start, curr_frag_len);
2047                 /* Complete packet has now been moved to data */
2048                 put_page(page_info->page);
2049                 skb->data_len = 0;
2050                 skb->tail += curr_frag_len;
2051         } else {
2052                 hdr_len = ETH_HLEN;
2053                 memcpy(skb->data, start, hdr_len);
2054                 skb_shinfo(skb)->nr_frags = 1;
2055                 skb_frag_set_page(skb, 0, page_info->page);
2056                 skb_shinfo(skb)->frags[0].page_offset =
2057                                         page_info->page_offset + hdr_len;
2058                 skb_frag_size_set(&skb_shinfo(skb)->frags[0],
2059                                   curr_frag_len - hdr_len);
2060                 skb->data_len = curr_frag_len - hdr_len;
2061                 skb->truesize += rx_frag_size;
2062                 skb->tail += hdr_len;
2063         }
2064         page_info->page = NULL;
2065
2066         if (rxcp->pkt_size <= rx_frag_size) {
2067                 BUG_ON(rxcp->num_rcvd != 1);
2068                 return;
2069         }
2070
2071         /* More frags present for this completion */
2072         remaining = rxcp->pkt_size - curr_frag_len;
2073         for (i = 1, j = 0; i < rxcp->num_rcvd; i++) {
2074                 page_info = get_rx_page_info(rxo);
2075                 curr_frag_len = min(remaining, rx_frag_size);
2076
2077                 /* Coalesce all frags from the same physical page in one slot */
2078                 if (page_info->page_offset == 0) {
2079                         /* Fresh page */
2080                         j++;
2081                         skb_frag_set_page(skb, j, page_info->page);
2082                         skb_shinfo(skb)->frags[j].page_offset =
2083                                                         page_info->page_offset;
2084                         skb_frag_size_set(&skb_shinfo(skb)->frags[j], 0);
2085                         skb_shinfo(skb)->nr_frags++;
2086                 } else {
2087                         put_page(page_info->page);
2088                 }
2089
2090                 skb_frag_size_add(&skb_shinfo(skb)->frags[j], curr_frag_len);
2091                 skb->len += curr_frag_len;
2092                 skb->data_len += curr_frag_len;
2093                 skb->truesize += rx_frag_size;
2094                 remaining -= curr_frag_len;
2095                 page_info->page = NULL;
2096         }
2097         BUG_ON(j > MAX_SKB_FRAGS);
2098 }
2099
2100 /* Process the RX completion indicated by rxcp when GRO is disabled */
2101 static void be_rx_compl_process(struct be_rx_obj *rxo, struct napi_struct *napi,
2102                                 struct be_rx_compl_info *rxcp)
2103 {
2104         struct be_adapter *adapter = rxo->adapter;
2105         struct net_device *netdev = adapter->netdev;
2106         struct sk_buff *skb;
2107
2108         skb = netdev_alloc_skb_ip_align(netdev, BE_RX_SKB_ALLOC_SIZE);
2109         if (unlikely(!skb)) {
2110                 rx_stats(rxo)->rx_drops_no_skbs++;
2111                 be_rx_compl_discard(rxo, rxcp);
2112                 return;
2113         }
2114
2115         skb_fill_rx_data(rxo, skb, rxcp);
2116
2117         if (likely((netdev->features & NETIF_F_RXCSUM) && csum_passed(rxcp)))
2118                 skb->ip_summed = CHECKSUM_UNNECESSARY;
2119         else
2120                 skb_checksum_none_assert(skb);
2121
2122         skb->protocol = eth_type_trans(skb, netdev);
2123         skb_record_rx_queue(skb, rxo - &adapter->rx_obj[0]);
2124         if (netdev->features & NETIF_F_RXHASH)
2125                 skb_set_hash(skb, rxcp->rss_hash, PKT_HASH_TYPE_L3);
2126
2127         skb->csum_level = rxcp->tunneled;
2128         skb_mark_napi_id(skb, napi);
2129
2130         if (rxcp->vlanf)
2131                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rxcp->vlan_tag);
2132
2133         netif_receive_skb(skb);
2134 }
2135
2136 /* Process the RX completion indicated by rxcp when GRO is enabled */
2137 static void be_rx_compl_process_gro(struct be_rx_obj *rxo,
2138                                     struct napi_struct *napi,
2139                                     struct be_rx_compl_info *rxcp)
2140 {
2141         struct be_adapter *adapter = rxo->adapter;
2142         struct be_rx_page_info *page_info;
2143         struct sk_buff *skb = NULL;
2144         u16 remaining, curr_frag_len;
2145         u16 i, j;
2146
2147         skb = napi_get_frags(napi);
2148         if (!skb) {
2149                 be_rx_compl_discard(rxo, rxcp);
2150                 return;
2151         }
2152
2153         remaining = rxcp->pkt_size;
2154         for (i = 0, j = -1; i < rxcp->num_rcvd; i++) {
2155                 page_info = get_rx_page_info(rxo);
2156
2157                 curr_frag_len = min(remaining, rx_frag_size);
2158
2159                 /* Coalesce all frags from the same physical page in one slot */
2160                 if (i == 0 || page_info->page_offset == 0) {
2161                         /* First frag or Fresh page */
2162                         j++;
2163                         skb_frag_set_page(skb, j, page_info->page);
2164                         skb_shinfo(skb)->frags[j].page_offset =
2165                                                         page_info->page_offset;
2166                         skb_frag_size_set(&skb_shinfo(skb)->frags[j], 0);
2167                 } else {
2168                         put_page(page_info->page);
2169                 }
2170                 skb_frag_size_add(&skb_shinfo(skb)->frags[j], curr_frag_len);
2171                 skb->truesize += rx_frag_size;
2172                 remaining -= curr_frag_len;
2173                 memset(page_info, 0, sizeof(*page_info));
2174         }
2175         BUG_ON(j > MAX_SKB_FRAGS);
2176
2177         skb_shinfo(skb)->nr_frags = j + 1;
2178         skb->len = rxcp->pkt_size;
2179         skb->data_len = rxcp->pkt_size;
2180         skb->ip_summed = CHECKSUM_UNNECESSARY;
2181         skb_record_rx_queue(skb, rxo - &adapter->rx_obj[0]);
2182         if (adapter->netdev->features & NETIF_F_RXHASH)
2183                 skb_set_hash(skb, rxcp->rss_hash, PKT_HASH_TYPE_L3);
2184
2185         skb->csum_level = rxcp->tunneled;
2186         skb_mark_napi_id(skb, napi);
2187
2188         if (rxcp->vlanf)
2189                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rxcp->vlan_tag);
2190
2191         napi_gro_frags(napi);
2192 }
2193
2194 static void be_parse_rx_compl_v1(struct be_eth_rx_compl *compl,
2195                                  struct be_rx_compl_info *rxcp)
2196 {
2197         rxcp->pkt_size = GET_RX_COMPL_V1_BITS(pktsize, compl);
2198         rxcp->vlanf = GET_RX_COMPL_V1_BITS(vtp, compl);
2199         rxcp->err = GET_RX_COMPL_V1_BITS(err, compl);
2200         rxcp->tcpf = GET_RX_COMPL_V1_BITS(tcpf, compl);
2201         rxcp->udpf = GET_RX_COMPL_V1_BITS(udpf, compl);
2202         rxcp->ip_csum = GET_RX_COMPL_V1_BITS(ipcksm, compl);
2203         rxcp->l4_csum = GET_RX_COMPL_V1_BITS(l4_cksm, compl);
2204         rxcp->ipv6 = GET_RX_COMPL_V1_BITS(ip_version, compl);
2205         rxcp->num_rcvd = GET_RX_COMPL_V1_BITS(numfrags, compl);
2206         rxcp->pkt_type = GET_RX_COMPL_V1_BITS(cast_enc, compl);
2207         rxcp->rss_hash = GET_RX_COMPL_V1_BITS(rsshash, compl);
2208         if (rxcp->vlanf) {
2209                 rxcp->qnq = GET_RX_COMPL_V1_BITS(qnq, compl);
2210                 rxcp->vlan_tag = GET_RX_COMPL_V1_BITS(vlan_tag, compl);
2211         }
2212         rxcp->port = GET_RX_COMPL_V1_BITS(port, compl);
2213         rxcp->tunneled =
2214                 GET_RX_COMPL_V1_BITS(tunneled, compl);
2215 }
2216
2217 static void be_parse_rx_compl_v0(struct be_eth_rx_compl *compl,
2218                                  struct be_rx_compl_info *rxcp)
2219 {
2220         rxcp->pkt_size = GET_RX_COMPL_V0_BITS(pktsize, compl);
2221         rxcp->vlanf = GET_RX_COMPL_V0_BITS(vtp, compl);
2222         rxcp->err = GET_RX_COMPL_V0_BITS(err, compl);
2223         rxcp->tcpf = GET_RX_COMPL_V0_BITS(tcpf, compl);
2224         rxcp->udpf = GET_RX_COMPL_V0_BITS(udpf, compl);
2225         rxcp->ip_csum = GET_RX_COMPL_V0_BITS(ipcksm, compl);
2226         rxcp->l4_csum = GET_RX_COMPL_V0_BITS(l4_cksm, compl);
2227         rxcp->ipv6 = GET_RX_COMPL_V0_BITS(ip_version, compl);
2228         rxcp->num_rcvd = GET_RX_COMPL_V0_BITS(numfrags, compl);
2229         rxcp->pkt_type = GET_RX_COMPL_V0_BITS(cast_enc, compl);
2230         rxcp->rss_hash = GET_RX_COMPL_V0_BITS(rsshash, compl);
2231         if (rxcp->vlanf) {
2232                 rxcp->qnq = GET_RX_COMPL_V0_BITS(qnq, compl);
2233                 rxcp->vlan_tag = GET_RX_COMPL_V0_BITS(vlan_tag, compl);
2234         }
2235         rxcp->port = GET_RX_COMPL_V0_BITS(port, compl);
2236         rxcp->ip_frag = GET_RX_COMPL_V0_BITS(ip_frag, compl);
2237 }
2238
2239 static struct be_rx_compl_info *be_rx_compl_get(struct be_rx_obj *rxo)
2240 {
2241         struct be_eth_rx_compl *compl = queue_tail_node(&rxo->cq);
2242         struct be_rx_compl_info *rxcp = &rxo->rxcp;
2243         struct be_adapter *adapter = rxo->adapter;
2244
2245         /* For checking the valid bit it is Ok to use either definition as the
2246          * valid bit is at the same position in both v0 and v1 Rx compl */
2247         if (compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] == 0)
2248                 return NULL;
2249
2250         rmb();
2251         be_dws_le_to_cpu(compl, sizeof(*compl));
2252
2253         if (adapter->be3_native)
2254                 be_parse_rx_compl_v1(compl, rxcp);
2255         else
2256                 be_parse_rx_compl_v0(compl, rxcp);
2257
2258         if (rxcp->ip_frag)
2259                 rxcp->l4_csum = 0;
2260
2261         if (rxcp->vlanf) {
2262                 /* In QNQ modes, if qnq bit is not set, then the packet was
2263                  * tagged only with the transparent outer vlan-tag and must
2264                  * not be treated as a vlan packet by host
2265                  */
2266                 if (be_is_qnq_mode(adapter) && !rxcp->qnq)
2267                         rxcp->vlanf = 0;
2268
2269                 if (!lancer_chip(adapter))
2270                         rxcp->vlan_tag = swab16(rxcp->vlan_tag);
2271
2272                 if (adapter->pvid == (rxcp->vlan_tag & VLAN_VID_MASK) &&
2273                     !test_bit(rxcp->vlan_tag, adapter->vids))
2274                         rxcp->vlanf = 0;
2275         }
2276
2277         /* As the compl has been parsed, reset it; we wont touch it again */
2278         compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] = 0;
2279
2280         queue_tail_inc(&rxo->cq);
2281         return rxcp;
2282 }
2283
2284 static inline struct page *be_alloc_pages(u32 size, gfp_t gfp)
2285 {
2286         u32 order = get_order(size);
2287
2288         if (order > 0)
2289                 gfp |= __GFP_COMP;
2290         return  alloc_pages(gfp, order);
2291 }
2292
2293 /*
2294  * Allocate a page, split it to fragments of size rx_frag_size and post as
2295  * receive buffers to BE
2296  */
2297 static void be_post_rx_frags(struct be_rx_obj *rxo, gfp_t gfp, u32 frags_needed)
2298 {
2299         struct be_adapter *adapter = rxo->adapter;
2300         struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
2301         struct be_queue_info *rxq = &rxo->q;
2302         struct page *pagep = NULL;
2303         struct device *dev = &adapter->pdev->dev;
2304         struct be_eth_rx_d *rxd;
2305         u64 page_dmaaddr = 0, frag_dmaaddr;
2306         u32 posted, page_offset = 0, notify = 0;
2307
2308         page_info = &rxo->page_info_tbl[rxq->head];
2309         for (posted = 0; posted < frags_needed && !page_info->page; posted++) {
2310                 if (!pagep) {
2311                         pagep = be_alloc_pages(adapter->big_page_size, gfp);
2312                         if (unlikely(!pagep)) {
2313                                 rx_stats(rxo)->rx_post_fail++;
2314                                 break;
2315                         }
2316                         page_dmaaddr = dma_map_page(dev, pagep, 0,
2317                                                     adapter->big_page_size,
2318                                                     DMA_FROM_DEVICE);
2319                         if (dma_mapping_error(dev, page_dmaaddr)) {
2320                                 put_page(pagep);
2321                                 pagep = NULL;
2322                                 adapter->drv_stats.dma_map_errors++;
2323                                 break;
2324                         }
2325                         page_offset = 0;
2326                 } else {
2327                         get_page(pagep);
2328                         page_offset += rx_frag_size;
2329                 }
2330                 page_info->page_offset = page_offset;
2331                 page_info->page = pagep;
2332
2333                 rxd = queue_head_node(rxq);
2334                 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
2335                 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
2336                 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
2337
2338                 /* Any space left in the current big page for another frag? */
2339                 if ((page_offset + rx_frag_size + rx_frag_size) >
2340                                         adapter->big_page_size) {
2341                         pagep = NULL;
2342                         page_info->last_frag = true;
2343                         dma_unmap_addr_set(page_info, bus, page_dmaaddr);
2344                 } else {
2345                         dma_unmap_addr_set(page_info, bus, frag_dmaaddr);
2346                 }
2347
2348                 prev_page_info = page_info;
2349                 queue_head_inc(rxq);
2350                 page_info = &rxo->page_info_tbl[rxq->head];
2351         }
2352
2353         /* Mark the last frag of a page when we break out of the above loop
2354          * with no more slots available in the RXQ
2355          */
2356         if (pagep) {
2357                 prev_page_info->last_frag = true;
2358                 dma_unmap_addr_set(prev_page_info, bus, page_dmaaddr);
2359         }
2360
2361         if (posted) {
2362                 atomic_add(posted, &rxq->used);
2363                 if (rxo->rx_post_starved)
2364                         rxo->rx_post_starved = false;
2365                 do {
2366                         notify = min(MAX_NUM_POST_ERX_DB, posted);
2367                         be_rxq_notify(adapter, rxq->id, notify);
2368                         posted -= notify;
2369                 } while (posted);
2370         } else if (atomic_read(&rxq->used) == 0) {
2371                 /* Let be_worker replenish when memory is available */
2372                 rxo->rx_post_starved = true;
2373         }
2374 }
2375
2376 static struct be_tx_compl_info *be_tx_compl_get(struct be_tx_obj *txo)
2377 {
2378         struct be_queue_info *tx_cq = &txo->cq;
2379         struct be_tx_compl_info *txcp = &txo->txcp;
2380         struct be_eth_tx_compl *compl = queue_tail_node(tx_cq);
2381
2382         if (compl->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
2383                 return NULL;
2384
2385         /* Ensure load ordering of valid bit dword and other dwords below */
2386         rmb();
2387         be_dws_le_to_cpu(compl, sizeof(*compl));
2388
2389         txcp->status = GET_TX_COMPL_BITS(status, compl);
2390         txcp->end_index = GET_TX_COMPL_BITS(wrb_index, compl);
2391
2392         compl->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
2393         queue_tail_inc(tx_cq);
2394         return txcp;
2395 }
2396
2397 static u16 be_tx_compl_process(struct be_adapter *adapter,
2398                                struct be_tx_obj *txo, u16 last_index)
2399 {
2400         struct sk_buff **sent_skbs = txo->sent_skb_list;
2401         struct be_queue_info *txq = &txo->q;
2402         u16 frag_index, num_wrbs = 0;
2403         struct sk_buff *skb = NULL;
2404         bool unmap_skb_hdr = false;
2405         struct be_eth_wrb *wrb;
2406
2407         do {
2408                 if (sent_skbs[txq->tail]) {
2409                         /* Free skb from prev req */
2410                         if (skb)
2411                                 dev_consume_skb_any(skb);
2412                         skb = sent_skbs[txq->tail];
2413                         sent_skbs[txq->tail] = NULL;
2414                         queue_tail_inc(txq);  /* skip hdr wrb */
2415                         num_wrbs++;
2416                         unmap_skb_hdr = true;
2417                 }
2418                 wrb = queue_tail_node(txq);
2419                 frag_index = txq->tail;
2420                 unmap_tx_frag(&adapter->pdev->dev, wrb,
2421                               (unmap_skb_hdr && skb_headlen(skb)));
2422                 unmap_skb_hdr = false;
2423                 queue_tail_inc(txq);
2424                 num_wrbs++;
2425         } while (frag_index != last_index);
2426         dev_consume_skb_any(skb);
2427
2428         return num_wrbs;
2429 }
2430
2431 /* Return the number of events in the event queue */
2432 static inline int events_get(struct be_eq_obj *eqo)
2433 {
2434         struct be_eq_entry *eqe;
2435         int num = 0;
2436
2437         do {
2438                 eqe = queue_tail_node(&eqo->q);
2439                 if (eqe->evt == 0)
2440                         break;
2441
2442                 rmb();
2443                 eqe->evt = 0;
2444                 num++;
2445                 queue_tail_inc(&eqo->q);
2446         } while (true);
2447
2448         return num;
2449 }
2450
2451 /* Leaves the EQ is disarmed state */
2452 static void be_eq_clean(struct be_eq_obj *eqo)
2453 {
2454         int num = events_get(eqo);
2455
2456         be_eq_notify(eqo->adapter, eqo->q.id, false, true, num, 0);
2457 }
2458
2459 /* Free posted rx buffers that were not used */
2460 static void be_rxq_clean(struct be_rx_obj *rxo)
2461 {
2462         struct be_queue_info *rxq = &rxo->q;
2463         struct be_rx_page_info *page_info;
2464
2465         while (atomic_read(&rxq->used) > 0) {
2466                 page_info = get_rx_page_info(rxo);
2467                 put_page(page_info->page);
2468                 memset(page_info, 0, sizeof(*page_info));
2469         }
2470         BUG_ON(atomic_read(&rxq->used));
2471         rxq->tail = 0;
2472         rxq->head = 0;
2473 }
2474
2475 static void be_rx_cq_clean(struct be_rx_obj *rxo)
2476 {
2477         struct be_queue_info *rx_cq = &rxo->cq;
2478         struct be_rx_compl_info *rxcp;
2479         struct be_adapter *adapter = rxo->adapter;
2480         int flush_wait = 0;
2481
2482         /* Consume pending rx completions.
2483          * Wait for the flush completion (identified by zero num_rcvd)
2484          * to arrive. Notify CQ even when there are no more CQ entries
2485          * for HW to flush partially coalesced CQ entries.
2486          * In Lancer, there is no need to wait for flush compl.
2487          */
2488         for (;;) {
2489                 rxcp = be_rx_compl_get(rxo);
2490                 if (!rxcp) {
2491                         if (lancer_chip(adapter))
2492                                 break;
2493
2494                         if (flush_wait++ > 50 ||
2495                             be_check_error(adapter,
2496                                            BE_ERROR_HW)) {
2497                                 dev_warn(&adapter->pdev->dev,
2498                                          "did not receive flush compl\n");
2499                                 break;
2500                         }
2501                         be_cq_notify(adapter, rx_cq->id, true, 0);
2502                         mdelay(1);
2503                 } else {
2504                         be_rx_compl_discard(rxo, rxcp);
2505                         be_cq_notify(adapter, rx_cq->id, false, 1);
2506                         if (rxcp->num_rcvd == 0)
2507                                 break;
2508                 }
2509         }
2510
2511         /* After cleanup, leave the CQ in unarmed state */
2512         be_cq_notify(adapter, rx_cq->id, false, 0);
2513 }
2514
2515 static void be_tx_compl_clean(struct be_adapter *adapter)
2516 {
2517         u16 end_idx, notified_idx, cmpl = 0, timeo = 0, num_wrbs = 0;
2518         struct device *dev = &adapter->pdev->dev;
2519         struct be_tx_compl_info *txcp;
2520         struct be_queue_info *txq;
2521         struct be_tx_obj *txo;
2522         int i, pending_txqs;
2523
2524         /* Stop polling for compls when HW has been silent for 10ms */
2525         do {
2526                 pending_txqs = adapter->num_tx_qs;
2527
2528                 for_all_tx_queues(adapter, txo, i) {
2529                         cmpl = 0;
2530                         num_wrbs = 0;
2531                         txq = &txo->q;
2532                         while ((txcp = be_tx_compl_get(txo))) {
2533                                 num_wrbs +=
2534                                         be_tx_compl_process(adapter, txo,
2535                                                             txcp->end_index);
2536                                 cmpl++;
2537                         }
2538                         if (cmpl) {
2539                                 be_cq_notify(adapter, txo->cq.id, false, cmpl);
2540                                 atomic_sub(num_wrbs, &txq->used);
2541                                 timeo = 0;
2542                         }
2543                         if (!be_is_tx_compl_pending(txo))
2544                                 pending_txqs--;
2545                 }
2546
2547                 if (pending_txqs == 0 || ++timeo > 10 ||
2548                     be_check_error(adapter, BE_ERROR_HW))
2549                         break;
2550
2551                 mdelay(1);
2552         } while (true);
2553
2554         /* Free enqueued TX that was never notified to HW */
2555         for_all_tx_queues(adapter, txo, i) {
2556                 txq = &txo->q;
2557
2558                 if (atomic_read(&txq->used)) {
2559                         dev_info(dev, "txq%d: cleaning %d pending tx-wrbs\n",
2560                                  i, atomic_read(&txq->used));
2561                         notified_idx = txq->tail;
2562                         end_idx = txq->tail;
2563                         index_adv(&end_idx, atomic_read(&txq->used) - 1,
2564                                   txq->len);
2565                         /* Use the tx-compl process logic to handle requests
2566                          * that were not sent to the HW.
2567                          */
2568                         num_wrbs = be_tx_compl_process(adapter, txo, end_idx);
2569                         atomic_sub(num_wrbs, &txq->used);
2570                         BUG_ON(atomic_read(&txq->used));
2571                         txo->pend_wrb_cnt = 0;
2572                         /* Since hw was never notified of these requests,
2573                          * reset TXQ indices
2574                          */
2575                         txq->head = notified_idx;
2576                         txq->tail = notified_idx;
2577                 }
2578         }
2579 }
2580
2581 static void be_evt_queues_destroy(struct be_adapter *adapter)
2582 {
2583         struct be_eq_obj *eqo;
2584         int i;
2585
2586         for_all_evt_queues(adapter, eqo, i) {
2587                 if (eqo->q.created) {
2588                         be_eq_clean(eqo);
2589                         be_cmd_q_destroy(adapter, &eqo->q, QTYPE_EQ);
2590                         napi_hash_del(&eqo->napi);
2591                         netif_napi_del(&eqo->napi);
2592                         free_cpumask_var(eqo->affinity_mask);
2593                 }
2594                 be_queue_free(adapter, &eqo->q);
2595         }
2596 }
2597
2598 static int be_evt_queues_create(struct be_adapter *adapter)
2599 {
2600         struct be_queue_info *eq;
2601         struct be_eq_obj *eqo;
2602         struct be_aic_obj *aic;
2603         int i, rc;
2604
2605         adapter->num_evt_qs = min_t(u16, num_irqs(adapter),
2606                                     adapter->cfg_num_qs);
2607
2608         for_all_evt_queues(adapter, eqo, i) {
2609                 int numa_node = dev_to_node(&adapter->pdev->dev);
2610
2611                 aic = &adapter->aic_obj[i];
2612                 eqo->adapter = adapter;
2613                 eqo->idx = i;
2614                 aic->max_eqd = BE_MAX_EQD;
2615                 aic->enable = true;
2616
2617                 eq = &eqo->q;
2618                 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
2619                                     sizeof(struct be_eq_entry));
2620                 if (rc)
2621                         return rc;
2622
2623                 rc = be_cmd_eq_create(adapter, eqo);
2624                 if (rc)
2625                         return rc;
2626
2627                 if (!zalloc_cpumask_var(&eqo->affinity_mask, GFP_KERNEL))
2628                         return -ENOMEM;
2629                 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
2630                                 eqo->affinity_mask);
2631                 netif_napi_add(adapter->netdev, &eqo->napi, be_poll,
2632                                BE_NAPI_WEIGHT);
2633                 napi_hash_add(&eqo->napi);
2634         }
2635         return 0;
2636 }
2637
2638 static void be_mcc_queues_destroy(struct be_adapter *adapter)
2639 {
2640         struct be_queue_info *q;
2641
2642         q = &adapter->mcc_obj.q;
2643         if (q->created)
2644                 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
2645         be_queue_free(adapter, q);
2646
2647         q = &adapter->mcc_obj.cq;
2648         if (q->created)
2649                 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2650         be_queue_free(adapter, q);
2651 }
2652
2653 /* Must be called only after TX qs are created as MCC shares TX EQ */
2654 static int be_mcc_queues_create(struct be_adapter *adapter)
2655 {
2656         struct be_queue_info *q, *cq;
2657
2658         cq = &adapter->mcc_obj.cq;
2659         if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
2660                            sizeof(struct be_mcc_compl)))
2661                 goto err;
2662
2663         /* Use the default EQ for MCC completions */
2664         if (be_cmd_cq_create(adapter, cq, &mcc_eqo(adapter)->q, true, 0))
2665                 goto mcc_cq_free;
2666
2667         q = &adapter->mcc_obj.q;
2668         if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
2669                 goto mcc_cq_destroy;
2670
2671         if (be_cmd_mccq_create(adapter, q, cq))
2672                 goto mcc_q_free;
2673
2674         return 0;
2675
2676 mcc_q_free:
2677         be_queue_free(adapter, q);
2678 mcc_cq_destroy:
2679         be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
2680 mcc_cq_free:
2681         be_queue_free(adapter, cq);
2682 err:
2683         return -1;
2684 }
2685
2686 static void be_tx_queues_destroy(struct be_adapter *adapter)
2687 {
2688         struct be_queue_info *q;
2689         struct be_tx_obj *txo;
2690         u8 i;
2691
2692         for_all_tx_queues(adapter, txo, i) {
2693                 q = &txo->q;
2694                 if (q->created)
2695                         be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
2696                 be_queue_free(adapter, q);
2697
2698                 q = &txo->cq;
2699                 if (q->created)
2700                         be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2701                 be_queue_free(adapter, q);
2702         }
2703 }
2704
2705 static int be_tx_qs_create(struct be_adapter *adapter)
2706 {
2707         struct be_queue_info *cq;
2708         struct be_tx_obj *txo;
2709         struct be_eq_obj *eqo;
2710         int status, i;
2711
2712         adapter->num_tx_qs = min(adapter->num_evt_qs, be_max_txqs(adapter));
2713
2714         for_all_tx_queues(adapter, txo, i) {
2715                 cq = &txo->cq;
2716                 status = be_queue_alloc(adapter, cq, TX_CQ_LEN,
2717                                         sizeof(struct be_eth_tx_compl));
2718                 if (status)
2719                         return status;
2720
2721                 u64_stats_init(&txo->stats.sync);
2722                 u64_stats_init(&txo->stats.sync_compl);
2723
2724                 /* If num_evt_qs is less than num_tx_qs, then more than
2725                  * one txq share an eq
2726                  */
2727                 eqo = &adapter->eq_obj[i % adapter->num_evt_qs];
2728                 status = be_cmd_cq_create(adapter, cq, &eqo->q, false, 3);
2729                 if (status)
2730                         return status;
2731
2732                 status = be_queue_alloc(adapter, &txo->q, TX_Q_LEN,
2733                                         sizeof(struct be_eth_wrb));
2734                 if (status)
2735                         return status;
2736
2737                 status = be_cmd_txq_create(adapter, txo);
2738                 if (status)
2739                         return status;
2740
2741                 netif_set_xps_queue(adapter->netdev, eqo->affinity_mask,
2742                                     eqo->idx);
2743         }
2744
2745         dev_info(&adapter->pdev->dev, "created %d TX queue(s)\n",
2746                  adapter->num_tx_qs);
2747         return 0;
2748 }
2749
2750 static void be_rx_cqs_destroy(struct be_adapter *adapter)
2751 {
2752         struct be_queue_info *q;
2753         struct be_rx_obj *rxo;
2754         int i;
2755
2756         for_all_rx_queues(adapter, rxo, i) {
2757                 q = &rxo->cq;
2758                 if (q->created)
2759                         be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2760                 be_queue_free(adapter, q);
2761         }
2762 }
2763
2764 static int be_rx_cqs_create(struct be_adapter *adapter)
2765 {
2766         struct be_queue_info *eq, *cq;
2767         struct be_rx_obj *rxo;
2768         int rc, i;
2769
2770         /* We can create as many RSS rings as there are EQs. */
2771         adapter->num_rss_qs = adapter->num_evt_qs;
2772
2773         /* We'll use RSS only if atleast 2 RSS rings are supported. */
2774         if (adapter->num_rss_qs <= 1)
2775                 adapter->num_rss_qs = 0;
2776
2777         adapter->num_rx_qs = adapter->num_rss_qs + adapter->need_def_rxq;
2778
2779         /* When the interface is not capable of RSS rings (and there is no
2780          * need to create a default RXQ) we'll still need one RXQ
2781          */
2782         if (adapter->num_rx_qs == 0)
2783                 adapter->num_rx_qs = 1;
2784
2785         adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
2786         for_all_rx_queues(adapter, rxo, i) {
2787                 rxo->adapter = adapter;
2788                 cq = &rxo->cq;
2789                 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
2790                                     sizeof(struct be_eth_rx_compl));
2791                 if (rc)
2792                         return rc;
2793
2794                 u64_stats_init(&rxo->stats.sync);
2795                 eq = &adapter->eq_obj[i % adapter->num_evt_qs].q;
2796                 rc = be_cmd_cq_create(adapter, cq, eq, false, 3);
2797                 if (rc)
2798                         return rc;
2799         }
2800
2801         dev_info(&adapter->pdev->dev,
2802                  "created %d RX queue(s)\n", adapter->num_rx_qs);
2803         return 0;
2804 }
2805
2806 static irqreturn_t be_intx(int irq, void *dev)
2807 {
2808         struct be_eq_obj *eqo = dev;
2809         struct be_adapter *adapter = eqo->adapter;
2810         int num_evts = 0;
2811
2812         /* IRQ is not expected when NAPI is scheduled as the EQ
2813          * will not be armed.
2814          * But, this can happen on Lancer INTx where it takes
2815          * a while to de-assert INTx or in BE2 where occasionaly
2816          * an interrupt may be raised even when EQ is unarmed.
2817          * If NAPI is already scheduled, then counting & notifying
2818          * events will orphan them.
2819          */
2820         if (napi_schedule_prep(&eqo->napi)) {
2821                 num_evts = events_get(eqo);
2822                 __napi_schedule(&eqo->napi);
2823                 if (num_evts)
2824                         eqo->spurious_intr = 0;
2825         }
2826         be_eq_notify(adapter, eqo->q.id, false, true, num_evts, 0);
2827
2828         /* Return IRQ_HANDLED only for the the first spurious intr
2829          * after a valid intr to stop the kernel from branding
2830          * this irq as a bad one!
2831          */
2832         if (num_evts || eqo->spurious_intr++ == 0)
2833                 return IRQ_HANDLED;
2834         else
2835                 return IRQ_NONE;
2836 }
2837
2838 static irqreturn_t be_msix(int irq, void *dev)
2839 {
2840         struct be_eq_obj *eqo = dev;
2841
2842         be_eq_notify(eqo->adapter, eqo->q.id, false, true, 0, 0);
2843         napi_schedule(&eqo->napi);
2844         return IRQ_HANDLED;
2845 }
2846
2847 static inline bool do_gro(struct be_rx_compl_info *rxcp)
2848 {
2849         return (rxcp->tcpf && !rxcp->err && rxcp->l4_csum) ? true : false;
2850 }
2851
2852 static int be_process_rx(struct be_rx_obj *rxo, struct napi_struct *napi,
2853                          int budget, int polling)
2854 {
2855         struct be_adapter *adapter = rxo->adapter;
2856         struct be_queue_info *rx_cq = &rxo->cq;
2857         struct be_rx_compl_info *rxcp;
2858         u32 work_done;
2859         u32 frags_consumed = 0;
2860
2861         for (work_done = 0; work_done < budget; work_done++) {
2862                 rxcp = be_rx_compl_get(rxo);
2863                 if (!rxcp)
2864                         break;
2865
2866                 /* Is it a flush compl that has no data */
2867                 if (unlikely(rxcp->num_rcvd == 0))
2868                         goto loop_continue;
2869
2870                 /* Discard compl with partial DMA Lancer B0 */
2871                 if (unlikely(!rxcp->pkt_size)) {
2872                         be_rx_compl_discard(rxo, rxcp);
2873                         goto loop_continue;
2874                 }
2875
2876                 /* On BE drop pkts that arrive due to imperfect filtering in
2877                  * promiscuous mode on some skews
2878                  */
2879                 if (unlikely(rxcp->port != adapter->port_num &&
2880                              !lancer_chip(adapter))) {
2881                         be_rx_compl_discard(rxo, rxcp);
2882                         goto loop_continue;
2883                 }
2884
2885                 /* Don't do gro when we're busy_polling */
2886                 if (do_gro(rxcp) && polling != BUSY_POLLING)
2887                         be_rx_compl_process_gro(rxo, napi, rxcp);
2888                 else
2889                         be_rx_compl_process(rxo, napi, rxcp);
2890
2891 loop_continue:
2892                 frags_consumed += rxcp->num_rcvd;
2893                 be_rx_stats_update(rxo, rxcp);
2894         }
2895
2896         if (work_done) {
2897                 be_cq_notify(adapter, rx_cq->id, true, work_done);
2898
2899                 /* When an rx-obj gets into post_starved state, just
2900                  * let be_worker do the posting.
2901                  */
2902                 if (atomic_read(&rxo->q.used) < RX_FRAGS_REFILL_WM &&
2903                     !rxo->rx_post_starved)
2904                         be_post_rx_frags(rxo, GFP_ATOMIC,
2905                                          max_t(u32, MAX_RX_POST,
2906                                                frags_consumed));
2907         }
2908
2909         return work_done;
2910 }
2911
2912 static inline void be_update_tx_err(struct be_tx_obj *txo, u8 status)
2913 {
2914         switch (status) {
2915         case BE_TX_COMP_HDR_PARSE_ERR:
2916                 tx_stats(txo)->tx_hdr_parse_err++;
2917                 break;
2918         case BE_TX_COMP_NDMA_ERR:
2919                 tx_stats(txo)->tx_dma_err++;
2920                 break;
2921         case BE_TX_COMP_ACL_ERR:
2922                 tx_stats(txo)->tx_spoof_check_err++;
2923                 break;
2924         }
2925 }
2926
2927 static inline void lancer_update_tx_err(struct be_tx_obj *txo, u8 status)
2928 {
2929         switch (status) {
2930         case LANCER_TX_COMP_LSO_ERR:
2931                 tx_stats(txo)->tx_tso_err++;
2932                 break;
2933         case LANCER_TX_COMP_HSW_DROP_MAC_ERR:
2934         case LANCER_TX_COMP_HSW_DROP_VLAN_ERR:
2935                 tx_stats(txo)->tx_spoof_check_err++;
2936                 break;
2937         case LANCER_TX_COMP_QINQ_ERR:
2938                 tx_stats(txo)->tx_qinq_err++;
2939                 break;
2940         case LANCER_TX_COMP_PARITY_ERR:
2941                 tx_stats(txo)->tx_internal_parity_err++;
2942                 break;
2943         case LANCER_TX_COMP_DMA_ERR:
2944                 tx_stats(txo)->tx_dma_err++;
2945                 break;
2946         }
2947 }
2948
2949 static void be_process_tx(struct be_adapter *adapter, struct be_tx_obj *txo,
2950                           int idx)
2951 {
2952         int num_wrbs = 0, work_done = 0;
2953         struct be_tx_compl_info *txcp;
2954
2955         while ((txcp = be_tx_compl_get(txo))) {
2956                 num_wrbs += be_tx_compl_process(adapter, txo, txcp->end_index);
2957                 work_done++;
2958
2959                 if (txcp->status) {
2960                         if (lancer_chip(adapter))
2961                                 lancer_update_tx_err(txo, txcp->status);
2962                         else
2963                                 be_update_tx_err(txo, txcp->status);
2964                 }
2965         }
2966
2967         if (work_done) {
2968                 be_cq_notify(adapter, txo->cq.id, true, work_done);
2969                 atomic_sub(num_wrbs, &txo->q.used);
2970
2971                 /* As Tx wrbs have been freed up, wake up netdev queue
2972                  * if it was stopped due to lack of tx wrbs.  */
2973                 if (__netif_subqueue_stopped(adapter->netdev, idx) &&
2974                     be_can_txq_wake(txo)) {
2975                         netif_wake_subqueue(adapter->netdev, idx);
2976                 }
2977
2978                 u64_stats_update_begin(&tx_stats(txo)->sync_compl);
2979                 tx_stats(txo)->tx_compl += work_done;
2980                 u64_stats_update_end(&tx_stats(txo)->sync_compl);
2981         }
2982 }
2983
2984 #ifdef CONFIG_NET_RX_BUSY_POLL
2985 static inline bool be_lock_napi(struct be_eq_obj *eqo)
2986 {
2987         bool status = true;
2988
2989         spin_lock(&eqo->lock); /* BH is already disabled */
2990         if (eqo->state & BE_EQ_LOCKED) {
2991                 WARN_ON(eqo->state & BE_EQ_NAPI);
2992                 eqo->state |= BE_EQ_NAPI_YIELD;
2993                 status = false;
2994         } else {
2995                 eqo->state = BE_EQ_NAPI;
2996         }
2997         spin_unlock(&eqo->lock);
2998         return status;
2999 }
3000
3001 static inline void be_unlock_napi(struct be_eq_obj *eqo)
3002 {
3003         spin_lock(&eqo->lock); /* BH is already disabled */
3004
3005         WARN_ON(eqo->state & (BE_EQ_POLL | BE_EQ_NAPI_YIELD));
3006         eqo->state = BE_EQ_IDLE;
3007
3008         spin_unlock(&eqo->lock);
3009 }
3010
3011 static inline bool be_lock_busy_poll(struct be_eq_obj *eqo)
3012 {
3013         bool status = true;
3014
3015         spin_lock_bh(&eqo->lock);
3016         if (eqo->state & BE_EQ_LOCKED) {
3017                 eqo->state |= BE_EQ_POLL_YIELD;
3018                 status = false;
3019         } else {
3020                 eqo->state |= BE_EQ_POLL;
3021         }
3022         spin_unlock_bh(&eqo->lock);
3023         return status;
3024 }
3025
3026 static inline void be_unlock_busy_poll(struct be_eq_obj *eqo)
3027 {
3028         spin_lock_bh(&eqo->lock);
3029
3030         WARN_ON(eqo->state & (BE_EQ_NAPI));
3031         eqo->state = BE_EQ_IDLE;
3032
3033         spin_unlock_bh(&eqo->lock);
3034 }
3035
3036 static inline void be_enable_busy_poll(struct be_eq_obj *eqo)
3037 {
3038         spin_lock_init(&eqo->lock);
3039         eqo->state = BE_EQ_IDLE;
3040 }
3041
3042 static inline void be_disable_busy_poll(struct be_eq_obj *eqo)
3043 {
3044         local_bh_disable();
3045
3046         /* It's enough to just acquire napi lock on the eqo to stop
3047          * be_busy_poll() from processing any queueus.
3048          */
3049         while (!be_lock_napi(eqo))
3050                 mdelay(1);
3051
3052         local_bh_enable();
3053 }
3054
3055 #else /* CONFIG_NET_RX_BUSY_POLL */
3056
3057 static inline bool be_lock_napi(struct be_eq_obj *eqo)
3058 {
3059         return true;
3060 }
3061
3062 static inline void be_unlock_napi(struct be_eq_obj *eqo)
3063 {
3064 }
3065
3066 static inline bool be_lock_busy_poll(struct be_eq_obj *eqo)
3067 {
3068         return false;
3069 }
3070
3071 static inline void be_unlock_busy_poll(struct be_eq_obj *eqo)
3072 {
3073 }
3074
3075 static inline void be_enable_busy_poll(struct be_eq_obj *eqo)
3076 {
3077 }
3078
3079 static inline void be_disable_busy_poll(struct be_eq_obj *eqo)
3080 {
3081 }
3082 #endif /* CONFIG_NET_RX_BUSY_POLL */
3083
3084 int be_poll(struct napi_struct *napi, int budget)
3085 {
3086         struct be_eq_obj *eqo = container_of(napi, struct be_eq_obj, napi);
3087         struct be_adapter *adapter = eqo->adapter;
3088         int max_work = 0, work, i, num_evts;
3089         struct be_rx_obj *rxo;
3090         struct be_tx_obj *txo;
3091         u32 mult_enc = 0;
3092
3093         num_evts = events_get(eqo);
3094
3095         for_all_tx_queues_on_eq(adapter, eqo, txo, i)
3096                 be_process_tx(adapter, txo, i);
3097
3098         if (be_lock_napi(eqo)) {
3099                 /* This loop will iterate twice for EQ0 in which
3100                  * completions of the last RXQ (default one) are also processed
3101                  * For other EQs the loop iterates only once
3102                  */
3103                 for_all_rx_queues_on_eq(adapter, eqo, rxo, i) {
3104                         work = be_process_rx(rxo, napi, budget, NAPI_POLLING);
3105                         max_work = max(work, max_work);
3106                 }
3107                 be_unlock_napi(eqo);
3108         } else {
3109                 max_work = budget;
3110         }
3111
3112         if (is_mcc_eqo(eqo))
3113                 be_process_mcc(adapter);
3114
3115         if (max_work < budget) {
3116                 napi_complete(napi);
3117
3118                 /* Skyhawk EQ_DB has a provision to set the rearm to interrupt
3119                  * delay via a delay multiplier encoding value
3120                  */
3121                 if (skyhawk_chip(adapter))
3122                         mult_enc = be_get_eq_delay_mult_enc(eqo);
3123
3124                 be_eq_notify(adapter, eqo->q.id, true, false, num_evts,
3125                              mult_enc);
3126         } else {
3127                 /* As we'll continue in polling mode, count and clear events */
3128                 be_eq_notify(adapter, eqo->q.id, false, false, num_evts, 0);
3129         }
3130         return max_work;
3131 }
3132
3133 #ifdef CONFIG_NET_RX_BUSY_POLL
3134 static int be_busy_poll(struct napi_struct *napi)
3135 {
3136         struct be_eq_obj *eqo = container_of(napi, struct be_eq_obj, napi);
3137         struct be_adapter *adapter = eqo->adapter;
3138         struct be_rx_obj *rxo;
3139         int i, work = 0;
3140
3141         if (!be_lock_busy_poll(eqo))
3142                 return LL_FLUSH_BUSY;
3143
3144         for_all_rx_queues_on_eq(adapter, eqo, rxo, i) {
3145                 work = be_process_rx(rxo, napi, 4, BUSY_POLLING);
3146                 if (work)
3147                         break;
3148         }
3149
3150         be_unlock_busy_poll(eqo);
3151         return work;
3152 }
3153 #endif
3154
3155 void be_detect_error(struct be_adapter *adapter)
3156 {
3157         u32 ue_lo = 0, ue_hi = 0, ue_lo_mask = 0, ue_hi_mask = 0;
3158         u32 sliport_status = 0, sliport_err1 = 0, sliport_err2 = 0;
3159         u32 i;
3160         struct device *dev = &adapter->pdev->dev;
3161
3162         if (be_check_error(adapter, BE_ERROR_HW))
3163                 return;
3164
3165         if (lancer_chip(adapter)) {
3166                 sliport_status = ioread32(adapter->db + SLIPORT_STATUS_OFFSET);
3167                 if (sliport_status & SLIPORT_STATUS_ERR_MASK) {
3168                         be_set_error(adapter, BE_ERROR_UE);
3169                         sliport_err1 = ioread32(adapter->db +
3170                                                 SLIPORT_ERROR1_OFFSET);
3171                         sliport_err2 = ioread32(adapter->db +
3172                                                 SLIPORT_ERROR2_OFFSET);
3173                         /* Do not log error messages if its a FW reset */
3174                         if (sliport_err1 == SLIPORT_ERROR_FW_RESET1 &&
3175                             sliport_err2 == SLIPORT_ERROR_FW_RESET2) {
3176                                 dev_info(dev, "Firmware update in progress\n");
3177                         } else {
3178                                 dev_err(dev, "Error detected in the card\n");
3179                                 dev_err(dev, "ERR: sliport status 0x%x\n",
3180                                         sliport_status);
3181                                 dev_err(dev, "ERR: sliport error1 0x%x\n",
3182                                         sliport_err1);
3183                                 dev_err(dev, "ERR: sliport error2 0x%x\n",
3184                                         sliport_err2);
3185                         }
3186                 }
3187         } else {
3188                 ue_lo = ioread32(adapter->pcicfg + PCICFG_UE_STATUS_LOW);
3189                 ue_hi = ioread32(adapter->pcicfg + PCICFG_UE_STATUS_HIGH);
3190                 ue_lo_mask = ioread32(adapter->pcicfg +
3191                                       PCICFG_UE_STATUS_LOW_MASK);
3192                 ue_hi_mask = ioread32(adapter->pcicfg +
3193                                       PCICFG_UE_STATUS_HI_MASK);
3194
3195                 ue_lo = (ue_lo & ~ue_lo_mask);
3196                 ue_hi = (ue_hi & ~ue_hi_mask);
3197
3198                 /* On certain platforms BE hardware can indicate spurious UEs.
3199                  * Allow HW to stop working completely in case of a real UE.
3200                  * Hence not setting the hw_error for UE detection.
3201                  */
3202
3203                 if (ue_lo || ue_hi) {
3204                         dev_err(dev,
3205                                 "Unrecoverable Error detected in the adapter");
3206                         dev_err(dev, "Please reboot server to recover");
3207                         if (skyhawk_chip(adapter))
3208                                 be_set_error(adapter, BE_ERROR_UE);
3209
3210                         for (i = 0; ue_lo; ue_lo >>= 1, i++) {
3211                                 if (ue_lo & 1)
3212                                         dev_err(dev, "UE: %s bit set\n",
3213                                                 ue_status_low_desc[i]);
3214                         }
3215                         for (i = 0; ue_hi; ue_hi >>= 1, i++) {
3216                                 if (ue_hi & 1)
3217                                         dev_err(dev, "UE: %s bit set\n",
3218                                                 ue_status_hi_desc[i]);
3219                         }
3220                 }
3221         }
3222 }
3223
3224 static void be_msix_disable(struct be_adapter *adapter)
3225 {
3226         if (msix_enabled(adapter)) {
3227                 pci_disable_msix(adapter->pdev);
3228                 adapter->num_msix_vec = 0;
3229                 adapter->num_msix_roce_vec = 0;
3230         }
3231 }
3232
3233 static int be_msix_enable(struct be_adapter *adapter)
3234 {
3235         int i, num_vec;
3236         struct device *dev = &adapter->pdev->dev;
3237
3238         /* If RoCE is supported, program the max number of NIC vectors that
3239          * may be configured via set-channels, along with vectors needed for
3240          * RoCe. Else, just program the number we'll use initially.
3241          */
3242         if (be_roce_supported(adapter))
3243                 num_vec = min_t(int, 2 * be_max_eqs(adapter),
3244                                 2 * num_online_cpus());
3245         else
3246                 num_vec = adapter->cfg_num_qs;
3247
3248         for (i = 0; i < num_vec; i++)
3249                 adapter->msix_entries[i].entry = i;
3250
3251         num_vec = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
3252                                         MIN_MSIX_VECTORS, num_vec);
3253         if (num_vec < 0)
3254                 goto fail;
3255
3256         if (be_roce_supported(adapter) && num_vec > MIN_MSIX_VECTORS) {
3257                 adapter->num_msix_roce_vec = num_vec / 2;
3258                 dev_info(dev, "enabled %d MSI-x vector(s) for RoCE\n",
3259                          adapter->num_msix_roce_vec);
3260         }
3261
3262         adapter->num_msix_vec = num_vec - adapter->num_msix_roce_vec;
3263
3264         dev_info(dev, "enabled %d MSI-x vector(s) for NIC\n",
3265                  adapter->num_msix_vec);
3266         return 0;
3267
3268 fail:
3269         dev_warn(dev, "MSIx enable failed\n");
3270
3271         /* INTx is not supported in VFs, so fail probe if enable_msix fails */
3272         if (be_virtfn(adapter))
3273                 return num_vec;
3274         return 0;
3275 }
3276
3277 static inline int be_msix_vec_get(struct be_adapter *adapter,
3278                                   struct be_eq_obj *eqo)
3279 {
3280         return adapter->msix_entries[eqo->msix_idx].vector;
3281 }
3282
3283 static int be_msix_register(struct be_adapter *adapter)
3284 {
3285         struct net_device *netdev = adapter->netdev;
3286         struct be_eq_obj *eqo;
3287         int status, i, vec;
3288
3289         for_all_evt_queues(adapter, eqo, i) {
3290                 sprintf(eqo->desc, "%s-q%d", netdev->name, i);
3291                 vec = be_msix_vec_get(adapter, eqo);
3292                 status = request_irq(vec, be_msix, 0, eqo->desc, eqo);
3293                 if (status)
3294                         goto err_msix;
3295
3296                 irq_set_affinity_hint(vec, eqo->affinity_mask);
3297         }
3298
3299         return 0;
3300 err_msix:
3301         for (i--, eqo = &adapter->eq_obj[i]; i >= 0; i--, eqo--)
3302                 free_irq(be_msix_vec_get(adapter, eqo), eqo);
3303         dev_warn(&adapter->pdev->dev, "MSIX Request IRQ failed - err %d\n",
3304                  status);
3305         be_msix_disable(adapter);
3306         return status;
3307 }
3308
3309 static int be_irq_register(struct be_adapter *adapter)
3310 {
3311         struct net_device *netdev = adapter->netdev;
3312         int status;
3313
3314         if (msix_enabled(adapter)) {
3315                 status = be_msix_register(adapter);
3316                 if (status == 0)
3317                         goto done;
3318                 /* INTx is not supported for VF */
3319                 if (be_virtfn(adapter))
3320                         return status;
3321         }
3322
3323         /* INTx: only the first EQ is used */
3324         netdev->irq = adapter->pdev->irq;
3325         status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
3326                              &adapter->eq_obj[0]);
3327         if (status) {
3328                 dev_err(&adapter->pdev->dev,
3329                         "INTx request IRQ failed - err %d\n", status);
3330                 return status;
3331         }
3332 done:
3333         adapter->isr_registered = true;
3334         return 0;
3335 }
3336
3337 static void be_irq_unregister(struct be_adapter *adapter)
3338 {
3339         struct net_device *netdev = adapter->netdev;
3340         struct be_eq_obj *eqo;
3341         int i, vec;
3342
3343         if (!adapter->isr_registered)
3344                 return;
3345
3346         /* INTx */
3347         if (!msix_enabled(adapter)) {
3348                 free_irq(netdev->irq, &adapter->eq_obj[0]);
3349                 goto done;
3350         }
3351
3352         /* MSIx */
3353         for_all_evt_queues(adapter, eqo, i) {
3354                 vec = be_msix_vec_get(adapter, eqo);
3355                 irq_set_affinity_hint(vec, NULL);
3356                 free_irq(vec, eqo);
3357         }
3358
3359 done:
3360         adapter->isr_registered = false;
3361 }
3362
3363 static void be_rx_qs_destroy(struct be_adapter *adapter)
3364 {
3365         struct be_queue_info *q;
3366         struct be_rx_obj *rxo;
3367         int i;
3368
3369         for_all_rx_queues(adapter, rxo, i) {
3370                 q = &rxo->q;
3371                 if (q->created) {
3372                         /* If RXQs are destroyed while in an "out of buffer"
3373                          * state, there is a possibility of an HW stall on
3374                          * Lancer. So, post 64 buffers to each queue to relieve
3375                          * the "out of buffer" condition.
3376                          * Make sure there's space in the RXQ before posting.
3377                          */
3378                         if (lancer_chip(adapter)) {
3379                                 be_rx_cq_clean(rxo);
3380                                 if (atomic_read(&q->used) == 0)
3381                                         be_post_rx_frags(rxo, GFP_KERNEL,
3382                                                          MAX_RX_POST);
3383                         }
3384
3385                         be_cmd_rxq_destroy(adapter, q);
3386                         be_rx_cq_clean(rxo);
3387                         be_rxq_clean(rxo);
3388                 }
3389                 be_queue_free(adapter, q);
3390         }
3391 }
3392
3393 static void be_disable_if_filters(struct be_adapter *adapter)
3394 {
3395         be_cmd_pmac_del(adapter, adapter->if_handle,
3396                         adapter->pmac_id[0], 0);
3397
3398         be_clear_uc_list(adapter);
3399
3400         /* The IFACE flags are enabled in the open path and cleared
3401          * in the close path. When a VF gets detached from the host and
3402          * assigned to a VM the following happens:
3403          *      - VF's IFACE flags get cleared in the detach path
3404          *      - IFACE create is issued by the VF in the attach path
3405          * Due to a bug in the BE3/Skyhawk-R FW
3406          * (Lancer FW doesn't have the bug), the IFACE capability flags
3407          * specified along with the IFACE create cmd issued by a VF are not
3408          * honoured by FW.  As a consequence, if a *new* driver
3409          * (that enables/disables IFACE flags in open/close)
3410          * is loaded in the host and an *old* driver is * used by a VM/VF,
3411          * the IFACE gets created *without* the needed flags.
3412          * To avoid this, disable RX-filter flags only for Lancer.
3413          */
3414         if (lancer_chip(adapter)) {
3415                 be_cmd_rx_filter(adapter, BE_IF_ALL_FILT_FLAGS, OFF);
3416                 adapter->if_flags &= ~BE_IF_ALL_FILT_FLAGS;
3417         }
3418 }
3419
3420 static int be_close(struct net_device *netdev)
3421 {
3422         struct be_adapter *adapter = netdev_priv(netdev);
3423         struct be_eq_obj *eqo;
3424         int i;
3425
3426         /* This protection is needed as be_close() may be called even when the
3427          * adapter is in cleared state (after eeh perm failure)
3428          */
3429         if (!(adapter->flags & BE_FLAGS_SETUP_DONE))
3430                 return 0;
3431
3432         be_disable_if_filters(adapter);
3433
3434         be_roce_dev_close(adapter);
3435
3436         if (adapter->flags & BE_FLAGS_NAPI_ENABLED) {
3437                 for_all_evt_queues(adapter, eqo, i) {
3438                         napi_disable(&eqo->napi);
3439                         be_disable_busy_poll(eqo);
3440                 }
3441                 adapter->flags &= ~BE_FLAGS_NAPI_ENABLED;
3442         }
3443
3444         be_async_mcc_disable(adapter);
3445
3446         /* Wait for all pending tx completions to arrive so that
3447          * all tx skbs are freed.
3448          */
3449         netif_tx_disable(netdev);
3450         be_tx_compl_clean(adapter);
3451
3452         be_rx_qs_destroy(adapter);
3453
3454         for_all_evt_queues(adapter, eqo, i) {
3455                 if (msix_enabled(adapter))
3456                         synchronize_irq(be_msix_vec_get(adapter, eqo));
3457                 else
3458                         synchronize_irq(netdev->irq);
3459                 be_eq_clean(eqo);
3460         }
3461
3462         be_irq_unregister(adapter);
3463
3464         return 0;
3465 }
3466
3467 static int be_rx_qs_create(struct be_adapter *adapter)
3468 {
3469         struct rss_info *rss = &adapter->rss_info;
3470         u8 rss_key[RSS_HASH_KEY_LEN];
3471         struct be_rx_obj *rxo;
3472         int rc, i, j;
3473
3474         for_all_rx_queues(adapter, rxo, i) {
3475                 rc = be_queue_alloc(adapter, &rxo->q, RX_Q_LEN,
3476                                     sizeof(struct be_eth_rx_d));
3477                 if (rc)
3478                         return rc;
3479         }
3480
3481         if (adapter->need_def_rxq || !adapter->num_rss_qs) {
3482                 rxo = default_rxo(adapter);
3483                 rc = be_cmd_rxq_create(adapter, &rxo->q, rxo->cq.id,
3484                                        rx_frag_size, adapter->if_handle,
3485                                        false, &rxo->rss_id);
3486                 if (rc)
3487                         return rc;
3488         }
3489
3490         for_all_rss_queues(adapter, rxo, i) {
3491                 rc = be_cmd_rxq_create(adapter, &rxo->q, rxo->cq.id,
3492                                        rx_frag_size, adapter->if_handle,
3493                                        true, &rxo->rss_id);
3494                 if (rc)
3495                         return rc;
3496         }
3497
3498         if (be_multi_rxq(adapter)) {
3499                 for (j = 0; j < RSS_INDIR_TABLE_LEN; j += adapter->num_rss_qs) {
3500                         for_all_rss_queues(adapter, rxo, i) {
3501                                 if ((j + i) >= RSS_INDIR_TABLE_LEN)
3502                                         break;
3503                                 rss->rsstable[j + i] = rxo->rss_id;
3504                                 rss->rss_queue[j + i] = i;
3505                         }
3506                 }
3507                 rss->rss_flags = RSS_ENABLE_TCP_IPV4 | RSS_ENABLE_IPV4 |
3508                         RSS_ENABLE_TCP_IPV6 | RSS_ENABLE_IPV6;
3509
3510                 if (!BEx_chip(adapter))
3511                         rss->rss_flags |= RSS_ENABLE_UDP_IPV4 |
3512                                 RSS_ENABLE_UDP_IPV6;
3513         } else {
3514                 /* Disable RSS, if only default RX Q is created */
3515                 rss->rss_flags = RSS_ENABLE_NONE;
3516         }
3517
3518         netdev_rss_key_fill(rss_key, RSS_HASH_KEY_LEN);
3519         rc = be_cmd_rss_config(adapter, rss->rsstable, rss->rss_flags,
3520                                128, rss_key);
3521         if (rc) {
3522                 rss->rss_flags = RSS_ENABLE_NONE;
3523                 return rc;
3524         }
3525
3526         memcpy(rss->rss_hkey, rss_key, RSS_HASH_KEY_LEN);
3527
3528         /* Post 1 less than RXQ-len to avoid head being equal to tail,
3529          * which is a queue empty condition
3530          */
3531         for_all_rx_queues(adapter, rxo, i)
3532                 be_post_rx_frags(rxo, GFP_KERNEL, RX_Q_LEN - 1);
3533
3534         return 0;
3535 }
3536
3537 static int be_enable_if_filters(struct be_adapter *adapter)
3538 {
3539         int status;
3540
3541         status = be_cmd_rx_filter(adapter, BE_IF_EN_FLAGS, ON);
3542         if (status)
3543                 return status;
3544
3545         /* For BE3 VFs, the PF programs the initial MAC address */
3546         if (!(BEx_chip(adapter) && be_virtfn(adapter))) {
3547                 status = be_cmd_pmac_add(adapter, adapter->netdev->dev_addr,
3548                                          adapter->if_handle,
3549                                          &adapter->pmac_id[0], 0);
3550                 if (status)
3551                         return status;
3552         }
3553
3554         if (adapter->vlans_added)
3555                 be_vid_config(adapter);
3556
3557         be_set_rx_mode(adapter->netdev);
3558
3559         return 0;
3560 }
3561
3562 static int be_open(struct net_device *netdev)
3563 {
3564         struct be_adapter *adapter = netdev_priv(netdev);
3565         struct be_eq_obj *eqo;
3566         struct be_rx_obj *rxo;
3567         struct be_tx_obj *txo;
3568         u8 link_status;
3569         int status, i;
3570
3571         status = be_rx_qs_create(adapter);
3572         if (status)
3573                 goto err;
3574
3575         status = be_enable_if_filters(adapter);
3576         if (status)
3577                 goto err;
3578
3579         status = be_irq_register(adapter);
3580         if (status)
3581                 goto err;
3582
3583         for_all_rx_queues(adapter, rxo, i)
3584                 be_cq_notify(adapter, rxo->cq.id, true, 0);
3585
3586         for_all_tx_queues(adapter, txo, i)
3587                 be_cq_notify(adapter, txo->cq.id, true, 0);
3588
3589         be_async_mcc_enable(adapter);
3590
3591         for_all_evt_queues(adapter, eqo, i) {
3592                 napi_enable(&eqo->napi);
3593                 be_enable_busy_poll(eqo);
3594                 be_eq_notify(adapter, eqo->q.id, true, true, 0, 0);
3595         }
3596         adapter->flags |= BE_FLAGS_NAPI_ENABLED;
3597
3598         status = be_cmd_link_status_query(adapter, NULL, &link_status, 0);
3599         if (!status)
3600                 be_link_status_update(adapter, link_status);
3601
3602         netif_tx_start_all_queues(netdev);
3603         be_roce_dev_open(adapter);
3604
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 equally 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                 /* If number of VFs requested is 8 less than max supported,
3800                  * assign 8 queue pairs to the PF and divide the remaining
3801                  * resources evenly among the VFs
3802                  */
3803                 if (num_vfs < (be_max_vfs(adapter) - 8))
3804                         num_vf_qs = (res.max_rss_qs - 8) / num_vfs;
3805                 else
3806                         num_vf_qs = res.max_rss_qs / num_vfs;
3807
3808                 /* Skyhawk-R chip supports only MAX_RSS_IFACES RSS capable
3809                  * interfaces per port. Provide RSS on VFs, only if number
3810                  * of VFs requested is less than MAX_RSS_IFACES limit.
3811                  */
3812                 if (num_vfs >= MAX_RSS_IFACES)
3813                         num_vf_qs = 1;
3814         }
3815         return num_vf_qs;
3816 }
3817
3818 static int be_clear(struct be_adapter *adapter)
3819 {
3820         struct pci_dev *pdev = adapter->pdev;
3821         u16 num_vf_qs;
3822
3823         be_cancel_worker(adapter);
3824
3825         if (sriov_enabled(adapter))
3826                 be_vf_clear(adapter);
3827
3828         /* Re-configure FW to distribute resources evenly across max-supported
3829          * number of VFs, only when VFs are not already enabled.
3830          */
3831         if (skyhawk_chip(adapter) && be_physfn(adapter) &&
3832             !pci_vfs_assigned(pdev)) {
3833                 num_vf_qs = be_calculate_vf_qs(adapter,
3834                                                pci_sriov_get_totalvfs(pdev));
3835                 be_cmd_set_sriov_config(adapter, adapter->pool_res,
3836                                         pci_sriov_get_totalvfs(pdev),
3837                                         num_vf_qs);
3838         }
3839
3840 #ifdef CONFIG_BE2NET_VXLAN
3841         be_disable_vxlan_offloads(adapter);
3842 #endif
3843         kfree(adapter->pmac_id);
3844         adapter->pmac_id = NULL;
3845
3846         be_cmd_if_destroy(adapter, adapter->if_handle,  0);
3847
3848         be_clear_queues(adapter);
3849
3850         be_msix_disable(adapter);
3851         adapter->flags &= ~BE_FLAGS_SETUP_DONE;
3852         return 0;
3853 }
3854
3855 static int be_vfs_if_create(struct be_adapter *adapter)
3856 {
3857         struct be_resources res = {0};
3858         u32 cap_flags, en_flags, vf;
3859         struct be_vf_cfg *vf_cfg;
3860         int status;
3861
3862         /* If a FW profile exists, then cap_flags are updated */
3863         cap_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
3864                     BE_IF_FLAGS_MULTICAST | BE_IF_FLAGS_PASS_L3L4_ERRORS;
3865
3866         for_all_vfs(adapter, vf_cfg, vf) {
3867                 if (!BE3_chip(adapter)) {
3868                         status = be_cmd_get_profile_config(adapter, &res,
3869                                                            RESOURCE_LIMITS,
3870                                                            vf + 1);
3871                         if (!status) {
3872                                 cap_flags = res.if_cap_flags;
3873                                 /* Prevent VFs from enabling VLAN promiscuous
3874                                  * mode
3875                                  */
3876                                 cap_flags &= ~BE_IF_FLAGS_VLAN_PROMISCUOUS;
3877                         }
3878                 }
3879
3880                 en_flags = cap_flags & (BE_IF_FLAGS_UNTAGGED |
3881                                         BE_IF_FLAGS_BROADCAST |
3882                                         BE_IF_FLAGS_MULTICAST |
3883                                         BE_IF_FLAGS_PASS_L3L4_ERRORS);
3884                 status = be_cmd_if_create(adapter, cap_flags, en_flags,
3885                                           &vf_cfg->if_handle, vf + 1);
3886                 if (status)
3887                         return status;
3888         }
3889
3890         return 0;
3891 }
3892
3893 static int be_vf_setup_init(struct be_adapter *adapter)
3894 {
3895         struct be_vf_cfg *vf_cfg;
3896         int vf;
3897
3898         adapter->vf_cfg = kcalloc(adapter->num_vfs, sizeof(*vf_cfg),
3899                                   GFP_KERNEL);
3900         if (!adapter->vf_cfg)
3901                 return -ENOMEM;
3902
3903         for_all_vfs(adapter, vf_cfg, vf) {
3904                 vf_cfg->if_handle = -1;
3905                 vf_cfg->pmac_id = -1;
3906         }
3907         return 0;
3908 }
3909
3910 static int be_vf_setup(struct be_adapter *adapter)
3911 {
3912         struct device *dev = &adapter->pdev->dev;
3913         struct be_vf_cfg *vf_cfg;
3914         int status, old_vfs, vf;
3915         bool spoofchk;
3916
3917         old_vfs = pci_num_vf(adapter->pdev);
3918
3919         status = be_vf_setup_init(adapter);
3920         if (status)
3921                 goto err;
3922
3923         if (old_vfs) {
3924                 for_all_vfs(adapter, vf_cfg, vf) {
3925                         status = be_cmd_get_if_id(adapter, vf_cfg, vf);
3926                         if (status)
3927                                 goto err;
3928                 }
3929
3930                 status = be_vfs_mac_query(adapter);
3931                 if (status)
3932                         goto err;
3933         } else {
3934                 status = be_vfs_if_create(adapter);
3935                 if (status)
3936                         goto err;
3937
3938                 status = be_vf_eth_addr_config(adapter);
3939                 if (status)
3940                         goto err;
3941         }
3942
3943         for_all_vfs(adapter, vf_cfg, vf) {
3944                 /* Allow VFs to programs MAC/VLAN filters */
3945                 status = be_cmd_get_fn_privileges(adapter, &vf_cfg->privileges,
3946                                                   vf + 1);
3947                 if (!status && !(vf_cfg->privileges & BE_PRIV_FILTMGMT)) {
3948                         status = be_cmd_set_fn_privileges(adapter,
3949                                                           vf_cfg->privileges |
3950                                                           BE_PRIV_FILTMGMT,
3951                                                           vf + 1);
3952                         if (!status) {
3953                                 vf_cfg->privileges |= BE_PRIV_FILTMGMT;
3954                                 dev_info(dev, "VF%d has FILTMGMT privilege\n",
3955                                          vf);
3956                         }
3957                 }
3958
3959                 /* Allow full available bandwidth */
3960                 if (!old_vfs)
3961                         be_cmd_config_qos(adapter, 0, 0, vf + 1);
3962
3963                 status = be_cmd_get_hsw_config(adapter, NULL, vf + 1,
3964                                                vf_cfg->if_handle, NULL,
3965                                                &spoofchk);
3966                 if (!status)
3967                         vf_cfg->spoofchk = spoofchk;
3968
3969                 if (!old_vfs) {
3970                         be_cmd_enable_vf(adapter, vf + 1);
3971                         be_cmd_set_logical_link_config(adapter,
3972                                                        IFLA_VF_LINK_STATE_AUTO,
3973                                                        vf+1);
3974                 }
3975         }
3976
3977         if (!old_vfs) {
3978                 status = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
3979                 if (status) {
3980                         dev_err(dev, "SRIOV enable failed\n");
3981                         adapter->num_vfs = 0;
3982                         goto err;
3983                 }
3984         }
3985
3986         adapter->flags |= BE_FLAGS_SRIOV_ENABLED;
3987         return 0;
3988 err:
3989         dev_err(dev, "VF setup failed\n");
3990         be_vf_clear(adapter);
3991         return status;
3992 }
3993
3994 /* Converting function_mode bits on BE3 to SH mc_type enums */
3995
3996 static u8 be_convert_mc_type(u32 function_mode)
3997 {
3998         if (function_mode & VNIC_MODE && function_mode & QNQ_MODE)
3999                 return vNIC1;
4000         else if (function_mode & QNQ_MODE)
4001                 return FLEX10;
4002         else if (function_mode & VNIC_MODE)
4003                 return vNIC2;
4004         else if (function_mode & UMC_ENABLED)
4005                 return UMC;
4006         else
4007                 return MC_NONE;
4008 }
4009
4010 /* On BE2/BE3 FW does not suggest the supported limits */
4011 static void BEx_get_resources(struct be_adapter *adapter,
4012                               struct be_resources *res)
4013 {
4014         bool use_sriov = adapter->num_vfs ? 1 : 0;
4015
4016         if (be_physfn(adapter))
4017                 res->max_uc_mac = BE_UC_PMAC_COUNT;
4018         else
4019                 res->max_uc_mac = BE_VF_UC_PMAC_COUNT;
4020
4021         adapter->mc_type = be_convert_mc_type(adapter->function_mode);
4022
4023         if (be_is_mc(adapter)) {
4024                 /* Assuming that there are 4 channels per port,
4025                  * when multi-channel is enabled
4026                  */
4027                 if (be_is_qnq_mode(adapter))
4028                         res->max_vlans = BE_NUM_VLANS_SUPPORTED/8;
4029                 else
4030                         /* In a non-qnq multichannel mode, the pvid
4031                          * takes up one vlan entry
4032                          */
4033                         res->max_vlans = (BE_NUM_VLANS_SUPPORTED / 4) - 1;
4034         } else {
4035                 res->max_vlans = BE_NUM_VLANS_SUPPORTED;
4036         }
4037
4038         res->max_mcast_mac = BE_MAX_MC;
4039
4040         /* 1) For BE3 1Gb ports, FW does not support multiple TXQs
4041          * 2) Create multiple TX rings on a BE3-R multi-channel interface
4042          *    *only* if it is RSS-capable.
4043          */
4044         if (BE2_chip(adapter) || use_sriov ||  (adapter->port_num > 1) ||
4045             be_virtfn(adapter) ||
4046             (be_is_mc(adapter) &&
4047              !(adapter->function_caps & BE_FUNCTION_CAPS_RSS))) {
4048                 res->max_tx_qs = 1;
4049         } else if (adapter->function_caps & BE_FUNCTION_CAPS_SUPER_NIC) {
4050                 struct be_resources super_nic_res = {0};
4051
4052                 /* On a SuperNIC profile, the driver needs to use the
4053                  * GET_PROFILE_CONFIG cmd to query the per-function TXQ limits
4054                  */
4055                 be_cmd_get_profile_config(adapter, &super_nic_res,
4056                                           RESOURCE_LIMITS, 0);
4057                 /* Some old versions of BE3 FW don't report max_tx_qs value */
4058                 res->max_tx_qs = super_nic_res.max_tx_qs ? : BE3_MAX_TX_QS;
4059         } else {
4060                 res->max_tx_qs = BE3_MAX_TX_QS;
4061         }
4062
4063         if ((adapter->function_caps & BE_FUNCTION_CAPS_RSS) &&
4064             !use_sriov && be_physfn(adapter))
4065                 res->max_rss_qs = (adapter->be3_native) ?
4066                                            BE3_MAX_RSS_QS : BE2_MAX_RSS_QS;
4067         res->max_rx_qs = res->max_rss_qs + 1;
4068
4069         if (be_physfn(adapter))
4070                 res->max_evt_qs = (be_max_vfs(adapter) > 0) ?
4071                                         BE3_SRIOV_MAX_EVT_QS : BE3_MAX_EVT_QS;
4072         else
4073                 res->max_evt_qs = 1;
4074
4075         res->if_cap_flags = BE_IF_CAP_FLAGS_WANT;
4076         res->if_cap_flags &= ~BE_IF_FLAGS_DEFQ_RSS;
4077         if (!(adapter->function_caps & BE_FUNCTION_CAPS_RSS))
4078                 res->if_cap_flags &= ~BE_IF_FLAGS_RSS;
4079 }
4080
4081 static void be_setup_init(struct be_adapter *adapter)
4082 {
4083         adapter->vlan_prio_bmap = 0xff;
4084         adapter->phy.link_speed = -1;
4085         adapter->if_handle = -1;
4086         adapter->be3_native = false;
4087         adapter->if_flags = 0;
4088         if (be_physfn(adapter))
4089                 adapter->cmd_privileges = MAX_PRIVILEGES;
4090         else
4091                 adapter->cmd_privileges = MIN_PRIVILEGES;
4092 }
4093
4094 static int be_get_sriov_config(struct be_adapter *adapter)
4095 {
4096         struct be_resources res = {0};
4097         int max_vfs, old_vfs;
4098
4099         be_cmd_get_profile_config(adapter, &res, RESOURCE_LIMITS, 0);
4100
4101         /* Some old versions of BE3 FW don't report max_vfs value */
4102         if (BE3_chip(adapter) && !res.max_vfs) {
4103                 max_vfs = pci_sriov_get_totalvfs(adapter->pdev);
4104                 res.max_vfs = max_vfs > 0 ? min(MAX_VFS, max_vfs) : 0;
4105         }
4106
4107         adapter->pool_res = res;
4108
4109         /* If during previous unload of the driver, the VFs were not disabled,
4110          * then we cannot rely on the PF POOL limits for the TotalVFs value.
4111          * Instead use the TotalVFs value stored in the pci-dev struct.
4112          */
4113         old_vfs = pci_num_vf(adapter->pdev);
4114         if (old_vfs) {
4115                 dev_info(&adapter->pdev->dev, "%d VFs are already enabled\n",
4116                          old_vfs);
4117
4118                 adapter->pool_res.max_vfs =
4119                         pci_sriov_get_totalvfs(adapter->pdev);
4120                 adapter->num_vfs = old_vfs;
4121         }
4122
4123         return 0;
4124 }
4125
4126 static void be_alloc_sriov_res(struct be_adapter *adapter)
4127 {
4128         int old_vfs = pci_num_vf(adapter->pdev);
4129         u16 num_vf_qs;
4130         int status;
4131
4132         be_get_sriov_config(adapter);
4133
4134         if (!old_vfs)
4135                 pci_sriov_set_totalvfs(adapter->pdev, be_max_vfs(adapter));
4136
4137         /* When the HW is in SRIOV capable configuration, the PF-pool
4138          * resources are given to PF during driver load, if there are no
4139          * old VFs. This facility is not available in BE3 FW.
4140          * Also, this is done by FW in Lancer chip.
4141          */
4142         if (skyhawk_chip(adapter) && be_max_vfs(adapter) && !old_vfs) {
4143                 num_vf_qs = be_calculate_vf_qs(adapter, 0);
4144                 status = be_cmd_set_sriov_config(adapter, adapter->pool_res, 0,
4145                                                  num_vf_qs);
4146                 if (status)
4147                         dev_err(&adapter->pdev->dev,
4148                                 "Failed to optimize SRIOV resources\n");
4149         }
4150 }
4151
4152 static int be_get_resources(struct be_adapter *adapter)
4153 {
4154         struct device *dev = &adapter->pdev->dev;
4155         struct be_resources res = {0};
4156         int status;
4157
4158         if (BEx_chip(adapter)) {
4159                 BEx_get_resources(adapter, &res);
4160                 adapter->res = res;
4161         }
4162
4163         /* For Lancer, SH etc read per-function resource limits from FW.
4164          * GET_FUNC_CONFIG returns per function guaranteed limits.
4165          * GET_PROFILE_CONFIG returns PCI-E related limits PF-pool limits
4166          */
4167         if (!BEx_chip(adapter)) {
4168                 status = be_cmd_get_func_config(adapter, &res);
4169                 if (status)
4170                         return status;
4171
4172                 /* If a deafault RXQ must be created, we'll use up one RSSQ*/
4173                 if (res.max_rss_qs && res.max_rss_qs == res.max_rx_qs &&
4174                     !(res.if_cap_flags & BE_IF_FLAGS_DEFQ_RSS))
4175                         res.max_rss_qs -= 1;
4176
4177                 /* If RoCE may be enabled stash away half the EQs for RoCE */
4178                 if (be_roce_supported(adapter))
4179                         res.max_evt_qs /= 2;
4180                 adapter->res = res;
4181         }
4182
4183         /* If FW supports RSS default queue, then skip creating non-RSS
4184          * queue for non-IP traffic.
4185          */
4186         adapter->need_def_rxq = (be_if_cap_flags(adapter) &
4187                                  BE_IF_FLAGS_DEFQ_RSS) ? 0 : 1;
4188
4189         dev_info(dev, "Max: txqs %d, rxqs %d, rss %d, eqs %d, vfs %d\n",
4190                  be_max_txqs(adapter), be_max_rxqs(adapter),
4191                  be_max_rss(adapter), be_max_eqs(adapter),
4192                  be_max_vfs(adapter));
4193         dev_info(dev, "Max: uc-macs %d, mc-macs %d, vlans %d\n",
4194                  be_max_uc(adapter), be_max_mc(adapter),
4195                  be_max_vlans(adapter));
4196
4197         /* Sanitize cfg_num_qs based on HW and platform limits */
4198         adapter->cfg_num_qs = min_t(u16, netif_get_num_default_rss_queues(),
4199                                     be_max_qs(adapter));
4200         return 0;
4201 }
4202
4203 static int be_get_config(struct be_adapter *adapter)
4204 {
4205         int status, level;
4206         u16 profile_id;
4207
4208         status = be_cmd_get_cntl_attributes(adapter);
4209         if (status)
4210                 return status;
4211
4212         status = be_cmd_query_fw_cfg(adapter);
4213         if (status)
4214                 return status;
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         if (!BE2_chip(adapter) && be_physfn(adapter))
4411                 be_alloc_sriov_res(adapter);
4412
4413         status = be_get_config(adapter);
4414         if (status)
4415                 goto err;
4416
4417         status = be_msix_enable(adapter);
4418         if (status)
4419                 goto err;
4420
4421         /* will enable all the needed filter flags in be_open() */
4422         en_flags = BE_IF_FLAGS_RSS | BE_IF_FLAGS_DEFQ_RSS;
4423         en_flags = en_flags & be_if_cap_flags(adapter);
4424         status = be_cmd_if_create(adapter, be_if_cap_flags(adapter), en_flags,
4425                                   &adapter->if_handle, 0);
4426         if (status)
4427                 goto err;
4428
4429         /* Updating real_num_tx/rx_queues() requires rtnl_lock() */
4430         rtnl_lock();
4431         status = be_setup_queues(adapter);
4432         rtnl_unlock();
4433         if (status)
4434                 goto err;
4435
4436         be_cmd_get_fn_privileges(adapter, &adapter->cmd_privileges, 0);
4437
4438         status = be_mac_setup(adapter);
4439         if (status)
4440                 goto err;
4441
4442         be_cmd_get_fw_ver(adapter);
4443         dev_info(dev, "FW version is %s\n", adapter->fw_ver);
4444
4445         if (BE2_chip(adapter) && fw_major_num(adapter->fw_ver) < 4) {
4446                 dev_err(dev, "Firmware on card is old(%s), IRQs may not work",
4447                         adapter->fw_ver);
4448                 dev_err(dev, "Please upgrade firmware to version >= 4.0\n");
4449         }
4450
4451         status = be_cmd_set_flow_control(adapter, adapter->tx_fc,
4452                                          adapter->rx_fc);
4453         if (status)
4454                 be_cmd_get_flow_control(adapter, &adapter->tx_fc,
4455                                         &adapter->rx_fc);
4456
4457         dev_info(&adapter->pdev->dev, "HW Flow control - TX:%d RX:%d\n",
4458                  adapter->tx_fc, adapter->rx_fc);
4459
4460         if (be_physfn(adapter))
4461                 be_cmd_set_logical_link_config(adapter,
4462                                                IFLA_VF_LINK_STATE_AUTO, 0);
4463
4464         if (adapter->num_vfs)
4465                 be_vf_setup(adapter);
4466
4467         status = be_cmd_get_phy_info(adapter);
4468         if (!status && be_pause_supported(adapter))
4469                 adapter->phy.fc_autoneg = 1;
4470
4471         be_schedule_worker(adapter);
4472         adapter->flags |= BE_FLAGS_SETUP_DONE;
4473         return 0;
4474 err:
4475         be_clear(adapter);
4476         return status;
4477 }
4478
4479 #ifdef CONFIG_NET_POLL_CONTROLLER
4480 static void be_netpoll(struct net_device *netdev)
4481 {
4482         struct be_adapter *adapter = netdev_priv(netdev);
4483         struct be_eq_obj *eqo;
4484         int i;
4485
4486         for_all_evt_queues(adapter, eqo, i) {
4487                 be_eq_notify(eqo->adapter, eqo->q.id, false, true, 0, 0);
4488                 napi_schedule(&eqo->napi);
4489         }
4490 }
4491 #endif
4492
4493 static char flash_cookie[2][16] = {"*** SE FLAS", "H DIRECTORY *** "};
4494
4495 static bool phy_flashing_required(struct be_adapter *adapter)
4496 {
4497         return (adapter->phy.phy_type == PHY_TYPE_TN_8022 &&
4498                 adapter->phy.interface_type == PHY_TYPE_BASET_10GB);
4499 }
4500
4501 static bool is_comp_in_ufi(struct be_adapter *adapter,
4502                            struct flash_section_info *fsec, int type)
4503 {
4504         int i = 0, img_type = 0;
4505         struct flash_section_info_g2 *fsec_g2 = NULL;
4506
4507         if (BE2_chip(adapter))
4508                 fsec_g2 = (struct flash_section_info_g2 *)fsec;
4509
4510         for (i = 0; i < MAX_FLASH_COMP; i++) {
4511                 if (fsec_g2)
4512                         img_type = le32_to_cpu(fsec_g2->fsec_entry[i].type);
4513                 else
4514                         img_type = le32_to_cpu(fsec->fsec_entry[i].type);
4515
4516                 if (img_type == type)
4517                         return true;
4518         }
4519         return false;
4520
4521 }
4522
4523 static struct flash_section_info *get_fsec_info(struct be_adapter *adapter,
4524                                                 int header_size,
4525                                                 const struct firmware *fw)
4526 {
4527         struct flash_section_info *fsec = NULL;
4528         const u8 *p = fw->data;
4529
4530         p += header_size;
4531         while (p < (fw->data + fw->size)) {
4532                 fsec = (struct flash_section_info *)p;
4533                 if (!memcmp(flash_cookie, fsec->cookie, sizeof(flash_cookie)))
4534                         return fsec;
4535                 p += 32;
4536         }
4537         return NULL;
4538 }
4539
4540 static int be_check_flash_crc(struct be_adapter *adapter, const u8 *p,
4541                               u32 img_offset, u32 img_size, int hdr_size,
4542                               u16 img_optype, bool *crc_match)
4543 {
4544         u32 crc_offset;
4545         int status;
4546         u8 crc[4];
4547
4548         status = be_cmd_get_flash_crc(adapter, crc, img_optype, img_offset,
4549                                       img_size - 4);
4550         if (status)
4551                 return status;
4552
4553         crc_offset = hdr_size + img_offset + img_size - 4;
4554
4555         /* Skip flashing, if crc of flashed region matches */
4556         if (!memcmp(crc, p + crc_offset, 4))
4557                 *crc_match = true;
4558         else
4559                 *crc_match = false;
4560
4561         return status;
4562 }
4563
4564 static int be_flash(struct be_adapter *adapter, const u8 *img,
4565                     struct be_dma_mem *flash_cmd, int optype, int img_size,
4566                     u32 img_offset)
4567 {
4568         u32 flash_op, num_bytes, total_bytes = img_size, bytes_sent = 0;
4569         struct be_cmd_write_flashrom *req = flash_cmd->va;
4570         int status;
4571
4572         while (total_bytes) {
4573                 num_bytes = min_t(u32, 32*1024, total_bytes);
4574
4575                 total_bytes -= num_bytes;
4576
4577                 if (!total_bytes) {
4578                         if (optype == OPTYPE_PHY_FW)
4579                                 flash_op = FLASHROM_OPER_PHY_FLASH;
4580                         else
4581                                 flash_op = FLASHROM_OPER_FLASH;
4582                 } else {
4583                         if (optype == OPTYPE_PHY_FW)
4584                                 flash_op = FLASHROM_OPER_PHY_SAVE;
4585                         else
4586                                 flash_op = FLASHROM_OPER_SAVE;
4587                 }
4588
4589                 memcpy(req->data_buf, img, num_bytes);
4590                 img += num_bytes;
4591                 status = be_cmd_write_flashrom(adapter, flash_cmd, optype,
4592                                                flash_op, img_offset +
4593                                                bytes_sent, num_bytes);
4594                 if (base_status(status) == MCC_STATUS_ILLEGAL_REQUEST &&
4595                     optype == OPTYPE_PHY_FW)
4596                         break;
4597                 else if (status)
4598                         return status;
4599
4600                 bytes_sent += num_bytes;
4601         }
4602         return 0;
4603 }
4604
4605 /* For BE2, BE3 and BE3-R */
4606 static int be_flash_BEx(struct be_adapter *adapter,
4607                         const struct firmware *fw,
4608                         struct be_dma_mem *flash_cmd, int num_of_images)
4609 {
4610         int img_hdrs_size = (num_of_images * sizeof(struct image_hdr));
4611         struct device *dev = &adapter->pdev->dev;
4612         struct flash_section_info *fsec = NULL;
4613         int status, i, filehdr_size, num_comp;
4614         const struct flash_comp *pflashcomp;
4615         bool crc_match;
4616         const u8 *p;
4617
4618         struct flash_comp gen3_flash_types[] = {
4619                 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, OPTYPE_ISCSI_ACTIVE,
4620                         FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_iSCSI},
4621                 { FLASH_REDBOOT_START_g3, OPTYPE_REDBOOT,
4622                         FLASH_REDBOOT_IMAGE_MAX_SIZE_g3, IMAGE_BOOT_CODE},
4623                 { FLASH_iSCSI_BIOS_START_g3, OPTYPE_BIOS,
4624                         FLASH_BIOS_IMAGE_MAX_SIZE_g3, IMAGE_OPTION_ROM_ISCSI},
4625                 { FLASH_PXE_BIOS_START_g3, OPTYPE_PXE_BIOS,
4626                         FLASH_BIOS_IMAGE_MAX_SIZE_g3, IMAGE_OPTION_ROM_PXE},
4627                 { FLASH_FCoE_BIOS_START_g3, OPTYPE_FCOE_BIOS,
4628                         FLASH_BIOS_IMAGE_MAX_SIZE_g3, IMAGE_OPTION_ROM_FCoE},
4629                 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, OPTYPE_ISCSI_BACKUP,
4630                         FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_BACKUP_iSCSI},
4631                 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, OPTYPE_FCOE_FW_ACTIVE,
4632                         FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_FCoE},
4633                 { FLASH_FCoE_BACKUP_IMAGE_START_g3, OPTYPE_FCOE_FW_BACKUP,
4634                         FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_BACKUP_FCoE},
4635                 { FLASH_NCSI_START_g3, OPTYPE_NCSI_FW,
4636                         FLASH_NCSI_IMAGE_MAX_SIZE_g3, IMAGE_NCSI},
4637                 { FLASH_PHY_FW_START_g3, OPTYPE_PHY_FW,
4638                         FLASH_PHY_FW_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_PHY}
4639         };
4640
4641         struct flash_comp gen2_flash_types[] = {
4642                 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, OPTYPE_ISCSI_ACTIVE,
4643                         FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_iSCSI},
4644                 { FLASH_REDBOOT_START_g2, OPTYPE_REDBOOT,
4645                         FLASH_REDBOOT_IMAGE_MAX_SIZE_g2, IMAGE_BOOT_CODE},
4646                 { FLASH_iSCSI_BIOS_START_g2, OPTYPE_BIOS,
4647                         FLASH_BIOS_IMAGE_MAX_SIZE_g2, IMAGE_OPTION_ROM_ISCSI},
4648                 { FLASH_PXE_BIOS_START_g2, OPTYPE_PXE_BIOS,
4649                         FLASH_BIOS_IMAGE_MAX_SIZE_g2, IMAGE_OPTION_ROM_PXE},
4650                 { FLASH_FCoE_BIOS_START_g2, OPTYPE_FCOE_BIOS,
4651                         FLASH_BIOS_IMAGE_MAX_SIZE_g2, IMAGE_OPTION_ROM_FCoE},
4652                 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, OPTYPE_ISCSI_BACKUP,
4653                         FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_BACKUP_iSCSI},
4654                 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, OPTYPE_FCOE_FW_ACTIVE,
4655                         FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_FCoE},
4656                 { FLASH_FCoE_BACKUP_IMAGE_START_g2, OPTYPE_FCOE_FW_BACKUP,
4657                          FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_BACKUP_FCoE}
4658         };
4659
4660         if (BE3_chip(adapter)) {
4661                 pflashcomp = gen3_flash_types;
4662                 filehdr_size = sizeof(struct flash_file_hdr_g3);
4663                 num_comp = ARRAY_SIZE(gen3_flash_types);
4664         } else {
4665                 pflashcomp = gen2_flash_types;
4666                 filehdr_size = sizeof(struct flash_file_hdr_g2);
4667                 num_comp = ARRAY_SIZE(gen2_flash_types);
4668                 img_hdrs_size = 0;
4669         }
4670
4671         /* Get flash section info*/
4672         fsec = get_fsec_info(adapter, filehdr_size + img_hdrs_size, fw);
4673         if (!fsec) {
4674                 dev_err(dev, "Invalid Cookie. FW image may be corrupted\n");
4675                 return -1;
4676         }
4677         for (i = 0; i < num_comp; i++) {
4678                 if (!is_comp_in_ufi(adapter, fsec, pflashcomp[i].img_type))
4679                         continue;
4680
4681                 if ((pflashcomp[i].optype == OPTYPE_NCSI_FW) &&
4682                     memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
4683                         continue;
4684
4685                 if (pflashcomp[i].optype == OPTYPE_PHY_FW  &&
4686                     !phy_flashing_required(adapter))
4687                                 continue;
4688
4689                 if (pflashcomp[i].optype == OPTYPE_REDBOOT) {
4690                         status = be_check_flash_crc(adapter, fw->data,
4691                                                     pflashcomp[i].offset,
4692                                                     pflashcomp[i].size,
4693                                                     filehdr_size +
4694                                                     img_hdrs_size,
4695                                                     OPTYPE_REDBOOT, &crc_match);
4696                         if (status) {
4697                                 dev_err(dev,
4698                                         "Could not get CRC for 0x%x region\n",
4699                                         pflashcomp[i].optype);
4700                                 continue;
4701                         }
4702
4703                         if (crc_match)
4704                                 continue;
4705                 }
4706
4707                 p = fw->data + filehdr_size + pflashcomp[i].offset +
4708                         img_hdrs_size;
4709                 if (p + pflashcomp[i].size > fw->data + fw->size)
4710                         return -1;
4711
4712                 status = be_flash(adapter, p, flash_cmd, pflashcomp[i].optype,
4713                                   pflashcomp[i].size, 0);
4714                 if (status) {
4715                         dev_err(dev, "Flashing section type 0x%x failed\n",
4716                                 pflashcomp[i].img_type);
4717                         return status;
4718                 }
4719         }
4720         return 0;
4721 }
4722
4723 static u16 be_get_img_optype(struct flash_section_entry fsec_entry)
4724 {
4725         u32 img_type = le32_to_cpu(fsec_entry.type);
4726         u16 img_optype = le16_to_cpu(fsec_entry.optype);
4727
4728         if (img_optype != 0xFFFF)
4729                 return img_optype;
4730
4731         switch (img_type) {
4732         case IMAGE_FIRMWARE_iSCSI:
4733                 img_optype = OPTYPE_ISCSI_ACTIVE;
4734                 break;
4735         case IMAGE_BOOT_CODE:
4736                 img_optype = OPTYPE_REDBOOT;
4737                 break;
4738         case IMAGE_OPTION_ROM_ISCSI:
4739                 img_optype = OPTYPE_BIOS;
4740                 break;
4741         case IMAGE_OPTION_ROM_PXE:
4742                 img_optype = OPTYPE_PXE_BIOS;
4743                 break;
4744         case IMAGE_OPTION_ROM_FCoE:
4745                 img_optype = OPTYPE_FCOE_BIOS;
4746                 break;
4747         case IMAGE_FIRMWARE_BACKUP_iSCSI:
4748                 img_optype = OPTYPE_ISCSI_BACKUP;
4749                 break;
4750         case IMAGE_NCSI:
4751                 img_optype = OPTYPE_NCSI_FW;
4752                 break;
4753         case IMAGE_FLASHISM_JUMPVECTOR:
4754                 img_optype = OPTYPE_FLASHISM_JUMPVECTOR;
4755                 break;
4756         case IMAGE_FIRMWARE_PHY:
4757                 img_optype = OPTYPE_SH_PHY_FW;
4758                 break;
4759         case IMAGE_REDBOOT_DIR:
4760                 img_optype = OPTYPE_REDBOOT_DIR;
4761                 break;
4762         case IMAGE_REDBOOT_CONFIG:
4763                 img_optype = OPTYPE_REDBOOT_CONFIG;
4764                 break;
4765         case IMAGE_UFI_DIR:
4766                 img_optype = OPTYPE_UFI_DIR;
4767                 break;
4768         default:
4769                 break;
4770         }
4771
4772         return img_optype;
4773 }
4774
4775 static int be_flash_skyhawk(struct be_adapter *adapter,
4776                             const struct firmware *fw,
4777                             struct be_dma_mem *flash_cmd, int num_of_images)
4778 {
4779         int img_hdrs_size = num_of_images * sizeof(struct image_hdr);
4780         bool crc_match, old_fw_img, flash_offset_support = true;
4781         struct device *dev = &adapter->pdev->dev;
4782         struct flash_section_info *fsec = NULL;
4783         u32 img_offset, img_size, img_type;
4784         u16 img_optype, flash_optype;
4785         int status, i, filehdr_size;
4786         const u8 *p;
4787
4788         filehdr_size = sizeof(struct flash_file_hdr_g3);
4789         fsec = get_fsec_info(adapter, filehdr_size + img_hdrs_size, fw);
4790         if (!fsec) {
4791                 dev_err(dev, "Invalid Cookie. FW image may be corrupted\n");
4792                 return -EINVAL;
4793         }
4794
4795 retry_flash:
4796         for (i = 0; i < le32_to_cpu(fsec->fsec_hdr.num_images); i++) {
4797                 img_offset = le32_to_cpu(fsec->fsec_entry[i].offset);
4798                 img_size   = le32_to_cpu(fsec->fsec_entry[i].pad_size);
4799                 img_type   = le32_to_cpu(fsec->fsec_entry[i].type);
4800                 img_optype = be_get_img_optype(fsec->fsec_entry[i]);
4801                 old_fw_img = fsec->fsec_entry[i].optype == 0xFFFF;
4802
4803                 if (img_optype == 0xFFFF)
4804                         continue;
4805
4806                 if (flash_offset_support)
4807                         flash_optype = OPTYPE_OFFSET_SPECIFIED;
4808                 else
4809                         flash_optype = img_optype;
4810
4811                 /* Don't bother verifying CRC if an old FW image is being
4812                  * flashed
4813                  */
4814                 if (old_fw_img)
4815                         goto flash;
4816
4817                 status = be_check_flash_crc(adapter, fw->data, img_offset,
4818                                             img_size, filehdr_size +
4819                                             img_hdrs_size, flash_optype,
4820                                             &crc_match);
4821                 if (base_status(status) == MCC_STATUS_ILLEGAL_REQUEST ||
4822                     base_status(status) == MCC_STATUS_ILLEGAL_FIELD) {
4823                         /* The current FW image on the card does not support
4824                          * OFFSET based flashing. Retry using older mechanism
4825                          * of OPTYPE based flashing
4826                          */
4827                         if (flash_optype == OPTYPE_OFFSET_SPECIFIED) {
4828                                 flash_offset_support = false;
4829                                 goto retry_flash;
4830                         }
4831
4832                         /* The current FW image on the card does not recognize
4833                          * the new FLASH op_type. The FW download is partially
4834                          * complete. Reboot the server now to enable FW image
4835                          * to recognize the new FLASH op_type. To complete the
4836                          * remaining process, download the same FW again after
4837                          * the reboot.
4838                          */
4839                         dev_err(dev, "Flash incomplete. Reset the server\n");
4840                         dev_err(dev, "Download FW image again after reset\n");
4841                         return -EAGAIN;
4842                 } else if (status) {
4843                         dev_err(dev, "Could not get CRC for 0x%x region\n",
4844                                 img_optype);
4845                         return -EFAULT;
4846                 }
4847
4848                 if (crc_match)
4849                         continue;
4850
4851 flash:
4852                 p = fw->data + filehdr_size + img_offset + img_hdrs_size;
4853                 if (p + img_size > fw->data + fw->size)
4854                         return -1;
4855
4856                 status = be_flash(adapter, p, flash_cmd, flash_optype, img_size,
4857                                   img_offset);
4858
4859                 /* The current FW image on the card does not support OFFSET
4860                  * based flashing. Retry using older mechanism of OPTYPE based
4861                  * flashing
4862                  */
4863                 if (base_status(status) == MCC_STATUS_ILLEGAL_FIELD &&
4864                     flash_optype == OPTYPE_OFFSET_SPECIFIED) {
4865                         flash_offset_support = false;
4866                         goto retry_flash;
4867                 }
4868
4869                 /* For old FW images ignore ILLEGAL_FIELD error or errors on
4870                  * UFI_DIR region
4871                  */
4872                 if (old_fw_img &&
4873                     (base_status(status) == MCC_STATUS_ILLEGAL_FIELD ||
4874                      (img_optype == OPTYPE_UFI_DIR &&
4875                       base_status(status) == MCC_STATUS_FAILED))) {
4876                         continue;
4877                 } else if (status) {
4878                         dev_err(dev, "Flashing section type 0x%x failed\n",
4879                                 img_type);
4880                         return -EFAULT;
4881                 }
4882         }
4883         return 0;
4884 }
4885
4886 static int lancer_fw_download(struct be_adapter *adapter,
4887                               const struct firmware *fw)
4888 {
4889 #define LANCER_FW_DOWNLOAD_CHUNK      (32 * 1024)
4890 #define LANCER_FW_DOWNLOAD_LOCATION   "/prg"
4891         struct device *dev = &adapter->pdev->dev;
4892         struct be_dma_mem flash_cmd;
4893         const u8 *data_ptr = NULL;
4894         u8 *dest_image_ptr = NULL;
4895         size_t image_size = 0;
4896         u32 chunk_size = 0;
4897         u32 data_written = 0;
4898         u32 offset = 0;
4899         int status = 0;
4900         u8 add_status = 0;
4901         u8 change_status;
4902
4903         if (!IS_ALIGNED(fw->size, sizeof(u32))) {
4904                 dev_err(dev, "FW image size should be multiple of 4\n");
4905                 return -EINVAL;
4906         }
4907
4908         flash_cmd.size = sizeof(struct lancer_cmd_req_write_object)
4909                                 + LANCER_FW_DOWNLOAD_CHUNK;
4910         flash_cmd.va = dma_zalloc_coherent(dev, flash_cmd.size,
4911                                            &flash_cmd.dma, GFP_KERNEL);
4912         if (!flash_cmd.va)
4913                 return -ENOMEM;
4914
4915         dest_image_ptr = flash_cmd.va +
4916                                 sizeof(struct lancer_cmd_req_write_object);
4917         image_size = fw->size;
4918         data_ptr = fw->data;
4919
4920         while (image_size) {
4921                 chunk_size = min_t(u32, image_size, LANCER_FW_DOWNLOAD_CHUNK);
4922
4923                 /* Copy the image chunk content. */
4924                 memcpy(dest_image_ptr, data_ptr, chunk_size);
4925
4926                 status = lancer_cmd_write_object(adapter, &flash_cmd,
4927                                                  chunk_size, offset,
4928                                                  LANCER_FW_DOWNLOAD_LOCATION,
4929                                                  &data_written, &change_status,
4930                                                  &add_status);
4931                 if (status)
4932                         break;
4933
4934                 offset += data_written;
4935                 data_ptr += data_written;
4936                 image_size -= data_written;
4937         }
4938
4939         if (!status) {
4940                 /* Commit the FW written */
4941                 status = lancer_cmd_write_object(adapter, &flash_cmd,
4942                                                  0, offset,
4943                                                  LANCER_FW_DOWNLOAD_LOCATION,
4944                                                  &data_written, &change_status,
4945                                                  &add_status);
4946         }
4947
4948         dma_free_coherent(dev, flash_cmd.size, flash_cmd.va, flash_cmd.dma);
4949         if (status) {
4950                 dev_err(dev, "Firmware load error\n");
4951                 return be_cmd_status(status);
4952         }
4953
4954         dev_info(dev, "Firmware flashed successfully\n");
4955
4956         if (change_status == LANCER_FW_RESET_NEEDED) {
4957                 dev_info(dev, "Resetting adapter to activate new FW\n");
4958                 status = lancer_physdev_ctrl(adapter,
4959                                              PHYSDEV_CONTROL_FW_RESET_MASK);
4960                 if (status) {
4961                         dev_err(dev, "Adapter busy, could not reset FW\n");
4962                         dev_err(dev, "Reboot server to activate new FW\n");
4963                 }
4964         } else if (change_status != LANCER_NO_RESET_NEEDED) {
4965                 dev_info(dev, "Reboot server to activate new FW\n");
4966         }
4967
4968         return 0;
4969 }
4970
4971 /* Check if the flash image file is compatible with the adapter that
4972  * is being flashed.
4973  */
4974 static bool be_check_ufi_compatibility(struct be_adapter *adapter,
4975                                        struct flash_file_hdr_g3 *fhdr)
4976 {
4977         if (!fhdr) {
4978                 dev_err(&adapter->pdev->dev, "Invalid FW UFI file");
4979                 return false;
4980         }
4981
4982         /* First letter of the build version is used to identify
4983          * which chip this image file is meant for.
4984          */
4985         switch (fhdr->build[0]) {
4986         case BLD_STR_UFI_TYPE_SH:
4987                 if (!skyhawk_chip(adapter))
4988                         return false;
4989                 break;
4990         case BLD_STR_UFI_TYPE_BE3:
4991                 if (!BE3_chip(adapter))
4992                         return false;
4993                 break;
4994         case BLD_STR_UFI_TYPE_BE2:
4995                 if (!BE2_chip(adapter))
4996                         return false;
4997                 break;
4998         default:
4999                 return false;
5000         }
5001
5002         return (fhdr->asic_type_rev >= adapter->asic_rev);
5003 }
5004
5005 static int be_fw_download(struct be_adapter *adapter, const struct firmware* fw)
5006 {
5007         struct device *dev = &adapter->pdev->dev;
5008         struct flash_file_hdr_g3 *fhdr3;
5009         struct image_hdr *img_hdr_ptr;
5010         int status = 0, i, num_imgs;
5011         struct be_dma_mem flash_cmd;
5012
5013         fhdr3 = (struct flash_file_hdr_g3 *)fw->data;
5014         if (!be_check_ufi_compatibility(adapter, fhdr3)) {
5015                 dev_err(dev, "Flash image is not compatible with adapter\n");
5016                 return -EINVAL;
5017         }
5018
5019         flash_cmd.size = sizeof(struct be_cmd_write_flashrom);
5020         flash_cmd.va = dma_zalloc_coherent(dev, flash_cmd.size, &flash_cmd.dma,
5021                                            GFP_KERNEL);
5022         if (!flash_cmd.va)
5023                 return -ENOMEM;
5024
5025         num_imgs = le32_to_cpu(fhdr3->num_imgs);
5026         for (i = 0; i < num_imgs; i++) {
5027                 img_hdr_ptr = (struct image_hdr *)(fw->data +
5028                                 (sizeof(struct flash_file_hdr_g3) +
5029                                  i * sizeof(struct image_hdr)));
5030                 if (!BE2_chip(adapter) &&
5031                     le32_to_cpu(img_hdr_ptr->imageid) != 1)
5032                         continue;
5033
5034                 if (skyhawk_chip(adapter))
5035                         status = be_flash_skyhawk(adapter, fw, &flash_cmd,
5036                                                   num_imgs);
5037                 else
5038                         status = be_flash_BEx(adapter, fw, &flash_cmd,
5039                                               num_imgs);
5040         }
5041
5042         dma_free_coherent(dev, flash_cmd.size, flash_cmd.va, flash_cmd.dma);
5043         if (!status)
5044                 dev_info(dev, "Firmware flashed successfully\n");
5045
5046         return status;
5047 }
5048
5049 int be_load_fw(struct be_adapter *adapter, u8 *fw_file)
5050 {
5051         const struct firmware *fw;
5052         int status;
5053
5054         if (!netif_running(adapter->netdev)) {
5055                 dev_err(&adapter->pdev->dev,
5056                         "Firmware load not allowed (interface is down)\n");
5057                 return -ENETDOWN;
5058         }
5059
5060         status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
5061         if (status)
5062                 goto fw_exit;
5063
5064         dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
5065
5066         if (lancer_chip(adapter))
5067                 status = lancer_fw_download(adapter, fw);
5068         else
5069                 status = be_fw_download(adapter, fw);
5070
5071         if (!status)
5072                 be_cmd_get_fw_ver(adapter);
5073
5074 fw_exit:
5075         release_firmware(fw);
5076         return status;
5077 }
5078
5079 static int be_ndo_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
5080                                  u16 flags)
5081 {
5082         struct be_adapter *adapter = netdev_priv(dev);
5083         struct nlattr *attr, *br_spec;
5084         int rem;
5085         int status = 0;
5086         u16 mode = 0;
5087
5088         if (!sriov_enabled(adapter))
5089                 return -EOPNOTSUPP;
5090
5091         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5092         if (!br_spec)
5093                 return -EINVAL;
5094
5095         nla_for_each_nested(attr, br_spec, rem) {
5096                 if (nla_type(attr) != IFLA_BRIDGE_MODE)
5097                         continue;
5098
5099                 if (nla_len(attr) < sizeof(mode))
5100                         return -EINVAL;
5101
5102                 mode = nla_get_u16(attr);
5103                 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
5104                         return -EINVAL;
5105
5106                 status = be_cmd_set_hsw_config(adapter, 0, 0,
5107                                                adapter->if_handle,
5108                                                mode == BRIDGE_MODE_VEPA ?
5109                                                PORT_FWD_TYPE_VEPA :
5110                                                PORT_FWD_TYPE_VEB, 0);
5111                 if (status)
5112                         goto err;
5113
5114                 dev_info(&adapter->pdev->dev, "enabled switch mode: %s\n",
5115                          mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
5116
5117                 return status;
5118         }
5119 err:
5120         dev_err(&adapter->pdev->dev, "Failed to set switch mode %s\n",
5121                 mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
5122
5123         return status;
5124 }
5125
5126 static int be_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
5127                                  struct net_device *dev, u32 filter_mask,
5128                                  int nlflags)
5129 {
5130         struct be_adapter *adapter = netdev_priv(dev);
5131         int status = 0;
5132         u8 hsw_mode;
5133
5134         /* BE and Lancer chips support VEB mode only */
5135         if (BEx_chip(adapter) || lancer_chip(adapter)) {
5136                 hsw_mode = PORT_FWD_TYPE_VEB;
5137         } else {
5138                 status = be_cmd_get_hsw_config(adapter, NULL, 0,
5139                                                adapter->if_handle, &hsw_mode,
5140                                                NULL);
5141                 if (status)
5142                         return 0;
5143
5144                 if (hsw_mode == PORT_FWD_TYPE_PASSTHRU)
5145                         return 0;
5146         }
5147
5148         return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
5149                                        hsw_mode == PORT_FWD_TYPE_VEPA ?
5150                                        BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB,
5151                                        0, 0, nlflags, filter_mask, NULL);
5152 }
5153
5154 #ifdef CONFIG_BE2NET_VXLAN
5155 /* VxLAN offload Notes:
5156  *
5157  * The stack defines tunnel offload flags (hw_enc_features) for IP and doesn't
5158  * distinguish various types of transports (VxLAN, GRE, NVGRE ..). So, offload
5159  * is expected to work across all types of IP tunnels once exported. Skyhawk
5160  * supports offloads for either VxLAN or NVGRE, exclusively. So we export VxLAN
5161  * offloads in hw_enc_features only when a VxLAN port is added. If other (non
5162  * VxLAN) tunnels are configured while VxLAN offloads are enabled, offloads for
5163  * those other tunnels are unexported on the fly through ndo_features_check().
5164  *
5165  * Skyhawk supports VxLAN offloads only for one UDP dport. So, if the stack
5166  * adds more than one port, disable offloads and don't re-enable them again
5167  * until after all the tunnels are removed.
5168  */
5169 static void be_add_vxlan_port(struct net_device *netdev, sa_family_t sa_family,
5170                               __be16 port)
5171 {
5172         struct be_adapter *adapter = netdev_priv(netdev);
5173         struct device *dev = &adapter->pdev->dev;
5174         int status;
5175
5176         if (lancer_chip(adapter) || BEx_chip(adapter) || be_is_mc(adapter))
5177                 return;
5178
5179         if (adapter->vxlan_port == port && adapter->vxlan_port_count) {
5180                 adapter->vxlan_port_aliases++;
5181                 return;
5182         }
5183
5184         if (adapter->flags & BE_FLAGS_VXLAN_OFFLOADS) {
5185                 dev_info(dev,
5186                          "Only one UDP port supported for VxLAN offloads\n");
5187                 dev_info(dev, "Disabling VxLAN offloads\n");
5188                 adapter->vxlan_port_count++;
5189                 goto err;
5190         }
5191
5192         if (adapter->vxlan_port_count++ >= 1)
5193                 return;
5194
5195         status = be_cmd_manage_iface(adapter, adapter->if_handle,
5196                                      OP_CONVERT_NORMAL_TO_TUNNEL);
5197         if (status) {
5198                 dev_warn(dev, "Failed to convert normal interface to tunnel\n");
5199                 goto err;
5200         }
5201
5202         status = be_cmd_set_vxlan_port(adapter, port);
5203         if (status) {
5204                 dev_warn(dev, "Failed to add VxLAN port\n");
5205                 goto err;
5206         }
5207         adapter->flags |= BE_FLAGS_VXLAN_OFFLOADS;
5208         adapter->vxlan_port = port;
5209
5210         netdev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5211                                    NETIF_F_TSO | NETIF_F_TSO6 |
5212                                    NETIF_F_GSO_UDP_TUNNEL;
5213         netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
5214         netdev->features |= NETIF_F_GSO_UDP_TUNNEL;
5215
5216         dev_info(dev, "Enabled VxLAN offloads for UDP port %d\n",
5217                  be16_to_cpu(port));
5218         return;
5219 err:
5220         be_disable_vxlan_offloads(adapter);
5221 }
5222
5223 static void be_del_vxlan_port(struct net_device *netdev, sa_family_t sa_family,
5224                               __be16 port)
5225 {
5226         struct be_adapter *adapter = netdev_priv(netdev);
5227
5228         if (lancer_chip(adapter) || BEx_chip(adapter) || be_is_mc(adapter))
5229                 return;
5230
5231         if (adapter->vxlan_port != port)
5232                 goto done;
5233
5234         if (adapter->vxlan_port_aliases) {
5235                 adapter->vxlan_port_aliases--;
5236                 return;
5237         }
5238
5239         be_disable_vxlan_offloads(adapter);
5240
5241         dev_info(&adapter->pdev->dev,
5242                  "Disabled VxLAN offloads for UDP port %d\n",
5243                  be16_to_cpu(port));
5244 done:
5245         adapter->vxlan_port_count--;
5246 }
5247
5248 static netdev_features_t be_features_check(struct sk_buff *skb,
5249                                            struct net_device *dev,
5250                                            netdev_features_t features)
5251 {
5252         struct be_adapter *adapter = netdev_priv(dev);
5253         u8 l4_hdr = 0;
5254
5255         /* The code below restricts offload features for some tunneled packets.
5256          * Offload features for normal (non tunnel) packets are unchanged.
5257          */
5258         if (!skb->encapsulation ||
5259             !(adapter->flags & BE_FLAGS_VXLAN_OFFLOADS))
5260                 return features;
5261
5262         /* It's an encapsulated packet and VxLAN offloads are enabled. We
5263          * should disable tunnel offload features if it's not a VxLAN packet,
5264          * as tunnel offloads have been enabled only for VxLAN. This is done to
5265          * allow other tunneled traffic like GRE work fine while VxLAN
5266          * offloads are configured in Skyhawk-R.
5267          */
5268         switch (vlan_get_protocol(skb)) {
5269         case htons(ETH_P_IP):
5270                 l4_hdr = ip_hdr(skb)->protocol;
5271                 break;
5272         case htons(ETH_P_IPV6):
5273                 l4_hdr = ipv6_hdr(skb)->nexthdr;
5274                 break;
5275         default:
5276                 return features;
5277         }
5278
5279         if (l4_hdr != IPPROTO_UDP ||
5280             skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
5281             skb->inner_protocol != htons(ETH_P_TEB) ||
5282             skb_inner_mac_header(skb) - skb_transport_header(skb) !=
5283             sizeof(struct udphdr) + sizeof(struct vxlanhdr))
5284                 return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
5285
5286         return features;
5287 }
5288 #endif
5289
5290 static int be_get_phys_port_id(struct net_device *dev,
5291                                struct netdev_phys_item_id *ppid)
5292 {
5293         int i, id_len = CNTL_SERIAL_NUM_WORDS * CNTL_SERIAL_NUM_WORD_SZ + 1;
5294         struct be_adapter *adapter = netdev_priv(dev);
5295         u8 *id;
5296
5297         if (MAX_PHYS_ITEM_ID_LEN < id_len)
5298                 return -ENOSPC;
5299
5300         ppid->id[0] = adapter->hba_port_num + 1;
5301         id = &ppid->id[1];
5302         for (i = CNTL_SERIAL_NUM_WORDS - 1; i >= 0;
5303              i--, id += CNTL_SERIAL_NUM_WORD_SZ)
5304                 memcpy(id, &adapter->serial_num[i], CNTL_SERIAL_NUM_WORD_SZ);
5305
5306         ppid->id_len = id_len;
5307
5308         return 0;
5309 }
5310
5311 static const struct net_device_ops be_netdev_ops = {
5312         .ndo_open               = be_open,
5313         .ndo_stop               = be_close,
5314         .ndo_start_xmit         = be_xmit,
5315         .ndo_set_rx_mode        = be_set_rx_mode,
5316         .ndo_set_mac_address    = be_mac_addr_set,
5317         .ndo_change_mtu         = be_change_mtu,
5318         .ndo_get_stats64        = be_get_stats64,
5319         .ndo_validate_addr      = eth_validate_addr,
5320         .ndo_vlan_rx_add_vid    = be_vlan_add_vid,
5321         .ndo_vlan_rx_kill_vid   = be_vlan_rem_vid,
5322         .ndo_set_vf_mac         = be_set_vf_mac,
5323         .ndo_set_vf_vlan        = be_set_vf_vlan,
5324         .ndo_set_vf_rate        = be_set_vf_tx_rate,
5325         .ndo_get_vf_config      = be_get_vf_config,
5326         .ndo_set_vf_link_state  = be_set_vf_link_state,
5327         .ndo_set_vf_spoofchk    = be_set_vf_spoofchk,
5328 #ifdef CONFIG_NET_POLL_CONTROLLER
5329         .ndo_poll_controller    = be_netpoll,
5330 #endif
5331         .ndo_bridge_setlink     = be_ndo_bridge_setlink,
5332         .ndo_bridge_getlink     = be_ndo_bridge_getlink,
5333 #ifdef CONFIG_NET_RX_BUSY_POLL
5334         .ndo_busy_poll          = be_busy_poll,
5335 #endif
5336 #ifdef CONFIG_BE2NET_VXLAN
5337         .ndo_add_vxlan_port     = be_add_vxlan_port,
5338         .ndo_del_vxlan_port     = be_del_vxlan_port,
5339         .ndo_features_check     = be_features_check,
5340 #endif
5341         .ndo_get_phys_port_id   = be_get_phys_port_id,
5342 };
5343
5344 static void be_netdev_init(struct net_device *netdev)
5345 {
5346         struct be_adapter *adapter = netdev_priv(netdev);
5347
5348         netdev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
5349                 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM |
5350                 NETIF_F_HW_VLAN_CTAG_TX;
5351         if (be_multi_rxq(adapter))
5352                 netdev->hw_features |= NETIF_F_RXHASH;
5353
5354         netdev->features |= netdev->hw_features |
5355                 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_FILTER;
5356
5357         netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
5358                 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
5359
5360         netdev->priv_flags |= IFF_UNICAST_FLT;
5361
5362         netdev->flags |= IFF_MULTICAST;
5363
5364         netif_set_gso_max_size(netdev, 65535 - ETH_HLEN);
5365
5366         netdev->netdev_ops = &be_netdev_ops;
5367
5368         netdev->ethtool_ops = &be_ethtool_ops;
5369 }
5370
5371 static void be_cleanup(struct be_adapter *adapter)
5372 {
5373         struct net_device *netdev = adapter->netdev;
5374
5375         rtnl_lock();
5376         netif_device_detach(netdev);
5377         if (netif_running(netdev))
5378                 be_close(netdev);
5379         rtnl_unlock();
5380
5381         be_clear(adapter);
5382 }
5383
5384 static int be_resume(struct be_adapter *adapter)
5385 {
5386         struct net_device *netdev = adapter->netdev;
5387         int status;
5388
5389         status = be_setup(adapter);
5390         if (status)
5391                 return status;
5392
5393         if (netif_running(netdev)) {
5394                 status = be_open(netdev);
5395                 if (status)
5396                         return status;
5397         }
5398
5399         netif_device_attach(netdev);
5400
5401         return 0;
5402 }
5403
5404 static int be_err_recover(struct be_adapter *adapter)
5405 {
5406         struct device *dev = &adapter->pdev->dev;
5407         int status;
5408
5409         status = be_resume(adapter);
5410         if (status)
5411                 goto err;
5412
5413         dev_info(dev, "Adapter recovery successful\n");
5414         return 0;
5415 err:
5416         if (be_physfn(adapter))
5417                 dev_err(dev, "Adapter recovery failed\n");
5418         else
5419                 dev_err(dev, "Re-trying adapter recovery\n");
5420
5421         return status;
5422 }
5423
5424 static void be_err_detection_task(struct work_struct *work)
5425 {
5426         struct be_adapter *adapter =
5427                                 container_of(work, struct be_adapter,
5428                                              be_err_detection_work.work);
5429         int status = 0;
5430
5431         be_detect_error(adapter);
5432
5433         if (be_check_error(adapter, BE_ERROR_HW)) {
5434                 be_cleanup(adapter);
5435
5436                 /* As of now error recovery support is in Lancer only */
5437                 if (lancer_chip(adapter))
5438                         status = be_err_recover(adapter);
5439         }
5440
5441         /* Always attempt recovery on VFs */
5442         if (!status || be_virtfn(adapter))
5443                 be_schedule_err_detection(adapter);
5444 }
5445
5446 static void be_log_sfp_info(struct be_adapter *adapter)
5447 {
5448         int status;
5449
5450         status = be_cmd_query_sfp_info(adapter);
5451         if (!status) {
5452                 dev_err(&adapter->pdev->dev,
5453                         "Unqualified SFP+ detected on %c from %s part no: %s",
5454                         adapter->port_name, adapter->phy.vendor_name,
5455                         adapter->phy.vendor_pn);
5456         }
5457         adapter->flags &= ~BE_FLAGS_EVT_INCOMPATIBLE_SFP;
5458 }
5459
5460 static void be_worker(struct work_struct *work)
5461 {
5462         struct be_adapter *adapter =
5463                 container_of(work, struct be_adapter, work.work);
5464         struct be_rx_obj *rxo;
5465         int i;
5466
5467         /* when interrupts are not yet enabled, just reap any pending
5468          * mcc completions
5469          */
5470         if (!netif_running(adapter->netdev)) {
5471                 local_bh_disable();
5472                 be_process_mcc(adapter);
5473                 local_bh_enable();
5474                 goto reschedule;
5475         }
5476
5477         if (!adapter->stats_cmd_sent) {
5478                 if (lancer_chip(adapter))
5479                         lancer_cmd_get_pport_stats(adapter,
5480                                                    &adapter->stats_cmd);
5481                 else
5482                         be_cmd_get_stats(adapter, &adapter->stats_cmd);
5483         }
5484
5485         if (be_physfn(adapter) &&
5486             MODULO(adapter->work_counter, adapter->be_get_temp_freq) == 0)
5487                 be_cmd_get_die_temperature(adapter);
5488
5489         for_all_rx_queues(adapter, rxo, i) {
5490                 /* Replenish RX-queues starved due to memory
5491                  * allocation failures.
5492                  */
5493                 if (rxo->rx_post_starved)
5494                         be_post_rx_frags(rxo, GFP_KERNEL, MAX_RX_POST);
5495         }
5496
5497         /* EQ-delay update for Skyhawk is done while notifying EQ */
5498         if (!skyhawk_chip(adapter))
5499                 be_eqd_update(adapter, false);
5500
5501         if (adapter->flags & BE_FLAGS_EVT_INCOMPATIBLE_SFP)
5502                 be_log_sfp_info(adapter);
5503
5504 reschedule:
5505         adapter->work_counter++;
5506         schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
5507 }
5508
5509 static void be_unmap_pci_bars(struct be_adapter *adapter)
5510 {
5511         if (adapter->csr)
5512                 pci_iounmap(adapter->pdev, adapter->csr);
5513         if (adapter->db)
5514                 pci_iounmap(adapter->pdev, adapter->db);
5515 }
5516
5517 static int db_bar(struct be_adapter *adapter)
5518 {
5519         if (lancer_chip(adapter) || be_virtfn(adapter))
5520                 return 0;
5521         else
5522                 return 4;
5523 }
5524
5525 static int be_roce_map_pci_bars(struct be_adapter *adapter)
5526 {
5527         if (skyhawk_chip(adapter)) {
5528                 adapter->roce_db.size = 4096;
5529                 adapter->roce_db.io_addr = pci_resource_start(adapter->pdev,
5530                                                               db_bar(adapter));
5531                 adapter->roce_db.total_size = pci_resource_len(adapter->pdev,
5532                                                                db_bar(adapter));
5533         }
5534         return 0;
5535 }
5536
5537 static int be_map_pci_bars(struct be_adapter *adapter)
5538 {
5539         struct pci_dev *pdev = adapter->pdev;
5540         u8 __iomem *addr;
5541         u32 sli_intf;
5542
5543         pci_read_config_dword(adapter->pdev, SLI_INTF_REG_OFFSET, &sli_intf);
5544         adapter->sli_family = (sli_intf & SLI_INTF_FAMILY_MASK) >>
5545                                 SLI_INTF_FAMILY_SHIFT;
5546         adapter->virtfn = (sli_intf & SLI_INTF_FT_MASK) ? 1 : 0;
5547
5548         if (BEx_chip(adapter) && be_physfn(adapter)) {
5549                 adapter->csr = pci_iomap(pdev, 2, 0);
5550                 if (!adapter->csr)
5551                         return -ENOMEM;
5552         }
5553
5554         addr = pci_iomap(pdev, db_bar(adapter), 0);
5555         if (!addr)
5556                 goto pci_map_err;
5557         adapter->db = addr;
5558
5559         if (skyhawk_chip(adapter) || BEx_chip(adapter)) {
5560                 if (be_physfn(adapter)) {
5561                         /* PCICFG is the 2nd BAR in BE2 */
5562                         addr = pci_iomap(pdev, BE2_chip(adapter) ? 1 : 0, 0);
5563                         if (!addr)
5564                                 goto pci_map_err;
5565                         adapter->pcicfg = addr;
5566                 } else {
5567                         adapter->pcicfg = adapter->db + SRIOV_VF_PCICFG_OFFSET;
5568                 }
5569         }
5570
5571         be_roce_map_pci_bars(adapter);
5572         return 0;
5573
5574 pci_map_err:
5575         dev_err(&pdev->dev, "Error in mapping PCI BARs\n");
5576         be_unmap_pci_bars(adapter);
5577         return -ENOMEM;
5578 }
5579
5580 static void be_drv_cleanup(struct be_adapter *adapter)
5581 {
5582         struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
5583         struct device *dev = &adapter->pdev->dev;
5584
5585         if (mem->va)
5586                 dma_free_coherent(dev, mem->size, mem->va, mem->dma);
5587
5588         mem = &adapter->rx_filter;
5589         if (mem->va)
5590                 dma_free_coherent(dev, mem->size, mem->va, mem->dma);
5591
5592         mem = &adapter->stats_cmd;
5593         if (mem->va)
5594                 dma_free_coherent(dev, mem->size, mem->va, mem->dma);
5595 }
5596
5597 /* Allocate and initialize various fields in be_adapter struct */
5598 static int be_drv_init(struct be_adapter *adapter)
5599 {
5600         struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
5601         struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
5602         struct be_dma_mem *rx_filter = &adapter->rx_filter;
5603         struct be_dma_mem *stats_cmd = &adapter->stats_cmd;
5604         struct device *dev = &adapter->pdev->dev;
5605         int status = 0;
5606
5607         mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
5608         mbox_mem_alloc->va = dma_zalloc_coherent(dev, mbox_mem_alloc->size,
5609                                                  &mbox_mem_alloc->dma,
5610                                                  GFP_KERNEL);
5611         if (!mbox_mem_alloc->va)
5612                 return -ENOMEM;
5613
5614         mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
5615         mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
5616         mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
5617
5618         rx_filter->size = sizeof(struct be_cmd_req_rx_filter);
5619         rx_filter->va = dma_zalloc_coherent(dev, rx_filter->size,
5620                                             &rx_filter->dma, GFP_KERNEL);
5621         if (!rx_filter->va) {
5622                 status = -ENOMEM;
5623                 goto free_mbox;
5624         }
5625
5626         if (lancer_chip(adapter))
5627                 stats_cmd->size = sizeof(struct lancer_cmd_req_pport_stats);
5628         else if (BE2_chip(adapter))
5629                 stats_cmd->size = sizeof(struct be_cmd_req_get_stats_v0);
5630         else if (BE3_chip(adapter))
5631                 stats_cmd->size = sizeof(struct be_cmd_req_get_stats_v1);
5632         else
5633                 stats_cmd->size = sizeof(struct be_cmd_req_get_stats_v2);
5634         stats_cmd->va = dma_zalloc_coherent(dev, stats_cmd->size,
5635                                             &stats_cmd->dma, GFP_KERNEL);
5636         if (!stats_cmd->va) {
5637                 status = -ENOMEM;
5638                 goto free_rx_filter;
5639         }
5640
5641         mutex_init(&adapter->mbox_lock);
5642         spin_lock_init(&adapter->mcc_lock);
5643         spin_lock_init(&adapter->mcc_cq_lock);
5644         init_completion(&adapter->et_cmd_compl);
5645
5646         pci_save_state(adapter->pdev);
5647
5648         INIT_DELAYED_WORK(&adapter->work, be_worker);
5649         INIT_DELAYED_WORK(&adapter->be_err_detection_work,
5650                           be_err_detection_task);
5651
5652         adapter->rx_fc = true;
5653         adapter->tx_fc = true;
5654
5655         /* Must be a power of 2 or else MODULO will BUG_ON */
5656         adapter->be_get_temp_freq = 64;
5657
5658         return 0;
5659
5660 free_rx_filter:
5661         dma_free_coherent(dev, rx_filter->size, rx_filter->va, rx_filter->dma);
5662 free_mbox:
5663         dma_free_coherent(dev, mbox_mem_alloc->size, mbox_mem_alloc->va,
5664                           mbox_mem_alloc->dma);
5665         return status;
5666 }
5667
5668 static void be_remove(struct pci_dev *pdev)
5669 {
5670         struct be_adapter *adapter = pci_get_drvdata(pdev);
5671
5672         if (!adapter)
5673                 return;
5674
5675         be_roce_dev_remove(adapter);
5676         be_intr_set(adapter, false);
5677
5678         be_cancel_err_detection(adapter);
5679
5680         unregister_netdev(adapter->netdev);
5681
5682         be_clear(adapter);
5683
5684         /* tell fw we're done with firing cmds */
5685         be_cmd_fw_clean(adapter);
5686
5687         be_unmap_pci_bars(adapter);
5688         be_drv_cleanup(adapter);
5689
5690         pci_disable_pcie_error_reporting(pdev);
5691
5692         pci_release_regions(pdev);
5693         pci_disable_device(pdev);
5694
5695         free_netdev(adapter->netdev);
5696 }
5697
5698 static ssize_t be_hwmon_show_temp(struct device *dev,
5699                                   struct device_attribute *dev_attr,
5700                                   char *buf)
5701 {
5702         struct be_adapter *adapter = dev_get_drvdata(dev);
5703
5704         /* Unit: millidegree Celsius */
5705         if (adapter->hwmon_info.be_on_die_temp == BE_INVALID_DIE_TEMP)
5706                 return -EIO;
5707         else
5708                 return sprintf(buf, "%u\n",
5709                                adapter->hwmon_info.be_on_die_temp * 1000);
5710 }
5711
5712 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
5713                           be_hwmon_show_temp, NULL, 1);
5714
5715 static struct attribute *be_hwmon_attrs[] = {
5716         &sensor_dev_attr_temp1_input.dev_attr.attr,
5717         NULL
5718 };
5719
5720 ATTRIBUTE_GROUPS(be_hwmon);
5721
5722 static char *mc_name(struct be_adapter *adapter)
5723 {
5724         char *str = ""; /* default */
5725
5726         switch (adapter->mc_type) {
5727         case UMC:
5728                 str = "UMC";
5729                 break;
5730         case FLEX10:
5731                 str = "FLEX10";
5732                 break;
5733         case vNIC1:
5734                 str = "vNIC-1";
5735                 break;
5736         case nPAR:
5737                 str = "nPAR";
5738                 break;
5739         case UFP:
5740                 str = "UFP";
5741                 break;
5742         case vNIC2:
5743                 str = "vNIC-2";
5744                 break;
5745         default:
5746                 str = "";
5747         }
5748
5749         return str;
5750 }
5751
5752 static inline char *func_name(struct be_adapter *adapter)
5753 {
5754         return be_physfn(adapter) ? "PF" : "VF";
5755 }
5756
5757 static inline char *nic_name(struct pci_dev *pdev)
5758 {
5759         switch (pdev->device) {
5760         case OC_DEVICE_ID1:
5761                 return OC_NAME;
5762         case OC_DEVICE_ID2:
5763                 return OC_NAME_BE;
5764         case OC_DEVICE_ID3:
5765         case OC_DEVICE_ID4:
5766                 return OC_NAME_LANCER;
5767         case BE_DEVICE_ID2:
5768                 return BE3_NAME;
5769         case OC_DEVICE_ID5:
5770         case OC_DEVICE_ID6:
5771                 return OC_NAME_SH;
5772         default:
5773                 return BE_NAME;
5774         }
5775 }
5776
5777 static int be_probe(struct pci_dev *pdev, const struct pci_device_id *pdev_id)
5778 {
5779         struct be_adapter *adapter;
5780         struct net_device *netdev;
5781         int status = 0;
5782
5783         dev_info(&pdev->dev, "%s version is %s\n", DRV_NAME, DRV_VER);
5784
5785         status = pci_enable_device(pdev);
5786         if (status)
5787                 goto do_none;
5788
5789         status = pci_request_regions(pdev, DRV_NAME);
5790         if (status)
5791                 goto disable_dev;
5792         pci_set_master(pdev);
5793
5794         netdev = alloc_etherdev_mqs(sizeof(*adapter), MAX_TX_QS, MAX_RX_QS);
5795         if (!netdev) {
5796                 status = -ENOMEM;
5797                 goto rel_reg;
5798         }
5799         adapter = netdev_priv(netdev);
5800         adapter->pdev = pdev;
5801         pci_set_drvdata(pdev, adapter);
5802         adapter->netdev = netdev;
5803         SET_NETDEV_DEV(netdev, &pdev->dev);
5804
5805         status = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
5806         if (!status) {
5807                 netdev->features |= NETIF_F_HIGHDMA;
5808         } else {
5809                 status = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
5810                 if (status) {
5811                         dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
5812                         goto free_netdev;
5813                 }
5814         }
5815
5816         status = pci_enable_pcie_error_reporting(pdev);
5817         if (!status)
5818                 dev_info(&pdev->dev, "PCIe error reporting enabled\n");
5819
5820         status = be_map_pci_bars(adapter);
5821         if (status)
5822                 goto free_netdev;
5823
5824         status = be_drv_init(adapter);
5825         if (status)
5826                 goto unmap_bars;
5827
5828         status = be_setup(adapter);
5829         if (status)
5830                 goto drv_cleanup;
5831
5832         be_netdev_init(netdev);
5833         status = register_netdev(netdev);
5834         if (status != 0)
5835                 goto unsetup;
5836
5837         be_roce_dev_add(adapter);
5838
5839         be_schedule_err_detection(adapter);
5840
5841         /* On Die temperature not supported for VF. */
5842         if (be_physfn(adapter) && IS_ENABLED(CONFIG_BE2NET_HWMON)) {
5843                 adapter->hwmon_info.hwmon_dev =
5844                         devm_hwmon_device_register_with_groups(&pdev->dev,
5845                                                                DRV_NAME,
5846                                                                adapter,
5847                                                                be_hwmon_groups);
5848                 adapter->hwmon_info.be_on_die_temp = BE_INVALID_DIE_TEMP;
5849         }
5850
5851         dev_info(&pdev->dev, "%s: %s %s port %c\n", nic_name(pdev),
5852                  func_name(adapter), mc_name(adapter), adapter->port_name);
5853
5854         return 0;
5855
5856 unsetup:
5857         be_clear(adapter);
5858 drv_cleanup:
5859         be_drv_cleanup(adapter);
5860 unmap_bars:
5861         be_unmap_pci_bars(adapter);
5862 free_netdev:
5863         free_netdev(netdev);
5864 rel_reg:
5865         pci_release_regions(pdev);
5866 disable_dev:
5867         pci_disable_device(pdev);
5868 do_none:
5869         dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
5870         return status;
5871 }
5872
5873 static int be_suspend(struct pci_dev *pdev, pm_message_t state)
5874 {
5875         struct be_adapter *adapter = pci_get_drvdata(pdev);
5876
5877         if (adapter->wol_en)
5878                 be_setup_wol(adapter, true);
5879
5880         be_intr_set(adapter, false);
5881         be_cancel_err_detection(adapter);
5882
5883         be_cleanup(adapter);
5884
5885         pci_save_state(pdev);
5886         pci_disable_device(pdev);
5887         pci_set_power_state(pdev, pci_choose_state(pdev, state));
5888         return 0;
5889 }
5890
5891 static int be_pci_resume(struct pci_dev *pdev)
5892 {
5893         struct be_adapter *adapter = pci_get_drvdata(pdev);
5894         int status = 0;
5895
5896         status = pci_enable_device(pdev);
5897         if (status)
5898                 return status;
5899
5900         pci_restore_state(pdev);
5901
5902         status = be_resume(adapter);
5903         if (status)
5904                 return status;
5905
5906         be_schedule_err_detection(adapter);
5907
5908         if (adapter->wol_en)
5909                 be_setup_wol(adapter, false);
5910
5911         return 0;
5912 }
5913
5914 /*
5915  * An FLR will stop BE from DMAing any data.
5916  */
5917 static void be_shutdown(struct pci_dev *pdev)
5918 {
5919         struct be_adapter *adapter = pci_get_drvdata(pdev);
5920
5921         if (!adapter)
5922                 return;
5923
5924         be_roce_dev_shutdown(adapter);
5925         cancel_delayed_work_sync(&adapter->work);
5926         be_cancel_err_detection(adapter);
5927
5928         netif_device_detach(adapter->netdev);
5929
5930         be_cmd_reset_function(adapter);
5931
5932         pci_disable_device(pdev);
5933 }
5934
5935 static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
5936                                             pci_channel_state_t state)
5937 {
5938         struct be_adapter *adapter = pci_get_drvdata(pdev);
5939
5940         dev_err(&adapter->pdev->dev, "EEH error detected\n");
5941
5942         if (!be_check_error(adapter, BE_ERROR_EEH)) {
5943                 be_set_error(adapter, BE_ERROR_EEH);
5944
5945                 be_cancel_err_detection(adapter);
5946
5947                 be_cleanup(adapter);
5948         }
5949
5950         if (state == pci_channel_io_perm_failure)
5951                 return PCI_ERS_RESULT_DISCONNECT;
5952
5953         pci_disable_device(pdev);
5954
5955         /* The error could cause the FW to trigger a flash debug dump.
5956          * Resetting the card while flash dump is in progress
5957          * can cause it not to recover; wait for it to finish.
5958          * Wait only for first function as it is needed only once per
5959          * adapter.
5960          */
5961         if (pdev->devfn == 0)
5962                 ssleep(30);
5963
5964         return PCI_ERS_RESULT_NEED_RESET;
5965 }
5966
5967 static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
5968 {
5969         struct be_adapter *adapter = pci_get_drvdata(pdev);
5970         int status;
5971
5972         dev_info(&adapter->pdev->dev, "EEH reset\n");
5973
5974         status = pci_enable_device(pdev);
5975         if (status)
5976                 return PCI_ERS_RESULT_DISCONNECT;
5977
5978         pci_set_master(pdev);
5979         pci_restore_state(pdev);
5980
5981         /* Check if card is ok and fw is ready */
5982         dev_info(&adapter->pdev->dev,
5983                  "Waiting for FW to be ready after EEH reset\n");
5984         status = be_fw_wait_ready(adapter);
5985         if (status)
5986                 return PCI_ERS_RESULT_DISCONNECT;
5987
5988         pci_cleanup_aer_uncorrect_error_status(pdev);
5989         be_clear_error(adapter, BE_CLEAR_ALL);
5990         return PCI_ERS_RESULT_RECOVERED;
5991 }
5992
5993 static void be_eeh_resume(struct pci_dev *pdev)
5994 {
5995         int status = 0;
5996         struct be_adapter *adapter = pci_get_drvdata(pdev);
5997
5998         dev_info(&adapter->pdev->dev, "EEH resume\n");
5999
6000         pci_save_state(pdev);
6001
6002         status = be_resume(adapter);
6003         if (status)
6004                 goto err;
6005
6006         be_schedule_err_detection(adapter);
6007         return;
6008 err:
6009         dev_err(&adapter->pdev->dev, "EEH resume failed\n");
6010 }
6011
6012 static int be_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
6013 {
6014         struct be_adapter *adapter = pci_get_drvdata(pdev);
6015         u16 num_vf_qs;
6016         int status;
6017
6018         if (!num_vfs)
6019                 be_vf_clear(adapter);
6020
6021         adapter->num_vfs = num_vfs;
6022
6023         if (adapter->num_vfs == 0 && pci_vfs_assigned(pdev)) {
6024                 dev_warn(&pdev->dev,
6025                          "Cannot disable VFs while they are assigned\n");
6026                 return -EBUSY;
6027         }
6028
6029         /* When the HW is in SRIOV capable configuration, the PF-pool resources
6030          * are equally distributed across the max-number of VFs. The user may
6031          * request only a subset of the max-vfs to be enabled.
6032          * Based on num_vfs, redistribute the resources across num_vfs so that
6033          * each VF will have access to more number of resources.
6034          * This facility is not available in BE3 FW.
6035          * Also, this is done by FW in Lancer chip.
6036          */
6037         if (skyhawk_chip(adapter) && !pci_num_vf(pdev)) {
6038                 num_vf_qs = be_calculate_vf_qs(adapter, adapter->num_vfs);
6039                 status = be_cmd_set_sriov_config(adapter, adapter->pool_res,
6040                                                  adapter->num_vfs, num_vf_qs);
6041                 if (status)
6042                         dev_err(&pdev->dev,
6043                                 "Failed to optimize SR-IOV resources\n");
6044         }
6045
6046         status = be_get_resources(adapter);
6047         if (status)
6048                 return be_cmd_status(status);
6049
6050         /* Updating real_num_tx/rx_queues() requires rtnl_lock() */
6051         rtnl_lock();
6052         status = be_update_queues(adapter);
6053         rtnl_unlock();
6054         if (status)
6055                 return be_cmd_status(status);
6056
6057         if (adapter->num_vfs)
6058                 status = be_vf_setup(adapter);
6059
6060         if (!status)
6061                 return adapter->num_vfs;
6062
6063         return 0;
6064 }
6065
6066 static const struct pci_error_handlers be_eeh_handlers = {
6067         .error_detected = be_eeh_err_detected,
6068         .slot_reset = be_eeh_reset,
6069         .resume = be_eeh_resume,
6070 };
6071
6072 static struct pci_driver be_driver = {
6073         .name = DRV_NAME,
6074         .id_table = be_dev_ids,
6075         .probe = be_probe,
6076         .remove = be_remove,
6077         .suspend = be_suspend,
6078         .resume = be_pci_resume,
6079         .shutdown = be_shutdown,
6080         .sriov_configure = be_pci_sriov_configure,
6081         .err_handler = &be_eeh_handlers
6082 };
6083
6084 static int __init be_init_module(void)
6085 {
6086         if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
6087             rx_frag_size != 2048) {
6088                 printk(KERN_WARNING DRV_NAME
6089                         " : Module param rx_frag_size must be 2048/4096/8192."
6090                         " Using 2048\n");
6091                 rx_frag_size = 2048;
6092         }
6093
6094         if (num_vfs > 0) {
6095                 pr_info(DRV_NAME " : Module param num_vfs is obsolete.");
6096                 pr_info(DRV_NAME " : Use sysfs method to enable VFs\n");
6097         }
6098
6099         return pci_register_driver(&be_driver);
6100 }
6101 module_init(be_init_module);
6102
6103 static void __exit be_exit_module(void)
6104 {
6105         pci_unregister_driver(&be_driver);
6106 }
6107 module_exit(be_exit_module);