be2net: Fix to reap TX compls till HW doesn't respond for some time
[linux-2.6-block.git] / drivers / net / ethernet / emulex / benet / be_main.c
CommitLineData
6b7c5b94 1/*
40263820 2 * Copyright (C) 2005 - 2014 Emulex
6b7c5b94
SP
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:
d2145cde 11 * linux-drivers@emulex.com
6b7c5b94 12 *
d2145cde
AK
13 * Emulex
14 * 3333 Susan Street
15 * Costa Mesa, CA 92626
6b7c5b94
SP
16 */
17
70c71606 18#include <linux/prefetch.h>
9d9779e7 19#include <linux/module.h>
6b7c5b94 20#include "be.h"
8788fdc2 21#include "be_cmds.h"
65f71b8b 22#include <asm/div64.h>
d6b6d987 23#include <linux/aer.h>
a77dcb8c 24#include <linux/if_bridge.h>
6384a4d0 25#include <net/busy_poll.h>
c9c47142 26#include <net/vxlan.h>
6b7c5b94
SP
27
28MODULE_VERSION(DRV_VER);
29MODULE_DEVICE_TABLE(pci, be_dev_ids);
30MODULE_DESCRIPTION(DRV_DESC " " DRV_VER);
00d3d51e 31MODULE_AUTHOR("Emulex Corporation");
6b7c5b94
SP
32MODULE_LICENSE("GPL");
33
ba343c77 34static unsigned int num_vfs;
ba343c77 35module_param(num_vfs, uint, S_IRUGO);
ba343c77 36MODULE_PARM_DESC(num_vfs, "Number of PCI VFs to initialize");
6b7c5b94 37
11ac75ed
SP
38static ushort rx_frag_size = 2048;
39module_param(rx_frag_size, ushort, S_IRUGO);
40MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data.");
41
6b7c5b94 42static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
c4ca2374 43 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
59fd5d87 44 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
c4ca2374
AK
45 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
46 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
fe6d2a38 47 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID3)},
12f4d0a8 48 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID4)},
ecedb6ae 49 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID5)},
76b73530 50 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID6)},
6b7c5b94
SP
51 { 0 }
52};
53MODULE_DEVICE_TABLE(pci, be_dev_ids);
7c185276 54/* UE Status Low CSR */
42c8b11e 55static const char * const ue_status_low_desc[] = {
7c185276
AK
56 "CEV",
57 "CTX",
58 "DBUF",
59 "ERX",
60 "Host",
61 "MPU",
62 "NDMA",
63 "PTC ",
64 "RDMA ",
65 "RXF ",
66 "RXIPS ",
67 "RXULP0 ",
68 "RXULP1 ",
69 "RXULP2 ",
70 "TIM ",
71 "TPOST ",
72 "TPRE ",
73 "TXIPS ",
74 "TXULP0 ",
75 "TXULP1 ",
76 "UC ",
77 "WDMA ",
78 "TXULP2 ",
79 "HOST1 ",
80 "P0_OB_LINK ",
81 "P1_OB_LINK ",
82 "HOST_GPIO ",
83 "MBOX ",
84 "AXGMAC0",
85 "AXGMAC1",
86 "JTAG",
87 "MPU_INTPEND"
88};
89/* UE Status High CSR */
42c8b11e 90static const char * const ue_status_hi_desc[] = {
7c185276
AK
91 "LPCMEMHOST",
92 "MGMT_MAC",
93 "PCS0ONLINE",
94 "MPU_IRAM",
95 "PCS1ONLINE",
96 "PCTL0",
97 "PCTL1",
98 "PMEM",
99 "RR",
100 "TXPB",
101 "RXPP",
102 "XAUI",
103 "TXP",
104 "ARM",
105 "IPC",
106 "HOST2",
107 "HOST3",
108 "HOST4",
109 "HOST5",
110 "HOST6",
111 "HOST7",
112 "HOST8",
113 "HOST9",
42c8b11e 114 "NETC",
7c185276
AK
115 "Unknown",
116 "Unknown",
117 "Unknown",
118 "Unknown",
119 "Unknown",
120 "Unknown",
121 "Unknown",
122 "Unknown"
123};
6b7c5b94 124
752961a1 125
6b7c5b94
SP
126static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
127{
128 struct be_dma_mem *mem = &q->dma_mem;
1cfafab9 129 if (mem->va) {
2b7bcebf
IV
130 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
131 mem->dma);
1cfafab9
SP
132 mem->va = NULL;
133 }
6b7c5b94
SP
134}
135
136static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
137 u16 len, u16 entry_size)
138{
139 struct be_dma_mem *mem = &q->dma_mem;
140
141 memset(q, 0, sizeof(*q));
142 q->len = len;
143 q->entry_size = entry_size;
144 mem->size = len * entry_size;
ede23fa8
JP
145 mem->va = dma_zalloc_coherent(&adapter->pdev->dev, mem->size, &mem->dma,
146 GFP_KERNEL);
6b7c5b94 147 if (!mem->va)
10ef9ab4 148 return -ENOMEM;
6b7c5b94
SP
149 return 0;
150}
151
68c45a2d 152static void be_reg_intr_set(struct be_adapter *adapter, bool enable)
6b7c5b94 153{
db3ea781 154 u32 reg, enabled;
5f0b849e 155
db3ea781
SP
156 pci_read_config_dword(adapter->pdev, PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET,
157 &reg);
158 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
159
5f0b849e 160 if (!enabled && enable)
6b7c5b94 161 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
5f0b849e 162 else if (enabled && !enable)
6b7c5b94 163 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
5f0b849e 164 else
6b7c5b94 165 return;
5f0b849e 166
db3ea781
SP
167 pci_write_config_dword(adapter->pdev,
168 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET, reg);
6b7c5b94
SP
169}
170
68c45a2d
SK
171static void be_intr_set(struct be_adapter *adapter, bool enable)
172{
173 int status = 0;
174
175 /* On lancer interrupts can't be controlled via this register */
176 if (lancer_chip(adapter))
177 return;
178
179 if (adapter->eeh_error)
180 return;
181
182 status = be_cmd_intr_set(adapter, enable);
183 if (status)
184 be_reg_intr_set(adapter, enable);
185}
186
8788fdc2 187static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
6b7c5b94
SP
188{
189 u32 val = 0;
190 val |= qid & DB_RQ_RING_ID_MASK;
191 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
f3eb62d2
SP
192
193 wmb();
8788fdc2 194 iowrite32(val, adapter->db + DB_RQ_OFFSET);
6b7c5b94
SP
195}
196
94d73aaa
VV
197static void be_txq_notify(struct be_adapter *adapter, struct be_tx_obj *txo,
198 u16 posted)
6b7c5b94
SP
199{
200 u32 val = 0;
94d73aaa 201 val |= txo->q.id & DB_TXULP_RING_ID_MASK;
6b7c5b94 202 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
f3eb62d2
SP
203
204 wmb();
94d73aaa 205 iowrite32(val, adapter->db + txo->db_offset);
6b7c5b94
SP
206}
207
8788fdc2 208static void be_eq_notify(struct be_adapter *adapter, u16 qid,
6b7c5b94
SP
209 bool arm, bool clear_int, u16 num_popped)
210{
211 u32 val = 0;
212 val |= qid & DB_EQ_RING_ID_MASK;
fe6d2a38
SP
213 val |= ((qid & DB_EQ_RING_ID_EXT_MASK) <<
214 DB_EQ_RING_ID_EXT_MASK_SHIFT);
cf588477 215
f67ef7ba 216 if (adapter->eeh_error)
cf588477
SP
217 return;
218
6b7c5b94
SP
219 if (arm)
220 val |= 1 << DB_EQ_REARM_SHIFT;
221 if (clear_int)
222 val |= 1 << DB_EQ_CLR_SHIFT;
223 val |= 1 << DB_EQ_EVNT_SHIFT;
224 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
8788fdc2 225 iowrite32(val, adapter->db + DB_EQ_OFFSET);
6b7c5b94
SP
226}
227
8788fdc2 228void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
6b7c5b94
SP
229{
230 u32 val = 0;
231 val |= qid & DB_CQ_RING_ID_MASK;
fe6d2a38
SP
232 val |= ((qid & DB_CQ_RING_ID_EXT_MASK) <<
233 DB_CQ_RING_ID_EXT_MASK_SHIFT);
cf588477 234
f67ef7ba 235 if (adapter->eeh_error)
cf588477
SP
236 return;
237
6b7c5b94
SP
238 if (arm)
239 val |= 1 << DB_CQ_REARM_SHIFT;
240 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
8788fdc2 241 iowrite32(val, adapter->db + DB_CQ_OFFSET);
6b7c5b94
SP
242}
243
6b7c5b94
SP
244static int be_mac_addr_set(struct net_device *netdev, void *p)
245{
246 struct be_adapter *adapter = netdev_priv(netdev);
5a712c13 247 struct device *dev = &adapter->pdev->dev;
6b7c5b94 248 struct sockaddr *addr = p;
5a712c13
SP
249 int status;
250 u8 mac[ETH_ALEN];
251 u32 old_pmac_id = adapter->pmac_id[0], curr_pmac_id = 0;
6b7c5b94 252
ca9e4988
AK
253 if (!is_valid_ether_addr(addr->sa_data))
254 return -EADDRNOTAVAIL;
255
ff32f8ab
VV
256 /* Proceed further only if, User provided MAC is different
257 * from active MAC
258 */
259 if (ether_addr_equal(addr->sa_data, netdev->dev_addr))
260 return 0;
261
5a712c13
SP
262 /* The PMAC_ADD cmd may fail if the VF doesn't have FILTMGMT
263 * privilege or if PF did not provision the new MAC address.
264 * On BE3, this cmd will always fail if the VF doesn't have the
265 * FILTMGMT privilege. This failure is OK, only if the PF programmed
266 * the MAC for the VF.
704e4c88 267 */
5a712c13
SP
268 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
269 adapter->if_handle, &adapter->pmac_id[0], 0);
270 if (!status) {
271 curr_pmac_id = adapter->pmac_id[0];
272
273 /* Delete the old programmed MAC. This call may fail if the
274 * old MAC was already deleted by the PF driver.
275 */
276 if (adapter->pmac_id[0] != old_pmac_id)
277 be_cmd_pmac_del(adapter, adapter->if_handle,
278 old_pmac_id, 0);
704e4c88
PR
279 }
280
5a712c13
SP
281 /* Decide if the new MAC is successfully activated only after
282 * querying the FW
704e4c88 283 */
b188f090
SR
284 status = be_cmd_get_active_mac(adapter, curr_pmac_id, mac,
285 adapter->if_handle, true, 0);
a65027e4 286 if (status)
e3a7ae2c 287 goto err;
6b7c5b94 288
5a712c13
SP
289 /* The MAC change did not happen, either due to lack of privilege
290 * or PF didn't pre-provision.
291 */
61d23e9f 292 if (!ether_addr_equal(addr->sa_data, mac)) {
5a712c13
SP
293 status = -EPERM;
294 goto err;
295 }
296
e3a7ae2c 297 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
5a712c13 298 dev_info(dev, "MAC address changed to %pM\n", mac);
e3a7ae2c
SK
299 return 0;
300err:
5a712c13 301 dev_warn(dev, "MAC address change to %pM failed\n", addr->sa_data);
6b7c5b94
SP
302 return status;
303}
304
ca34fe38
SP
305/* BE2 supports only v0 cmd */
306static void *hw_stats_from_cmd(struct be_adapter *adapter)
307{
308 if (BE2_chip(adapter)) {
309 struct be_cmd_resp_get_stats_v0 *cmd = adapter->stats_cmd.va;
310
311 return &cmd->hw_stats;
61000861 312 } else if (BE3_chip(adapter)) {
ca34fe38
SP
313 struct be_cmd_resp_get_stats_v1 *cmd = adapter->stats_cmd.va;
314
61000861
AK
315 return &cmd->hw_stats;
316 } else {
317 struct be_cmd_resp_get_stats_v2 *cmd = adapter->stats_cmd.va;
318
ca34fe38
SP
319 return &cmd->hw_stats;
320 }
321}
322
323/* BE2 supports only v0 cmd */
324static void *be_erx_stats_from_cmd(struct be_adapter *adapter)
325{
326 if (BE2_chip(adapter)) {
327 struct be_hw_stats_v0 *hw_stats = hw_stats_from_cmd(adapter);
328
329 return &hw_stats->erx;
61000861 330 } else if (BE3_chip(adapter)) {
ca34fe38
SP
331 struct be_hw_stats_v1 *hw_stats = hw_stats_from_cmd(adapter);
332
61000861
AK
333 return &hw_stats->erx;
334 } else {
335 struct be_hw_stats_v2 *hw_stats = hw_stats_from_cmd(adapter);
336
ca34fe38
SP
337 return &hw_stats->erx;
338 }
339}
340
341static void populate_be_v0_stats(struct be_adapter *adapter)
89a88ab8 342{
ac124ff9
SP
343 struct be_hw_stats_v0 *hw_stats = hw_stats_from_cmd(adapter);
344 struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
345 struct be_rxf_stats_v0 *rxf_stats = &hw_stats->rxf;
89a88ab8 346 struct be_port_rxf_stats_v0 *port_stats =
ac124ff9
SP
347 &rxf_stats->port[adapter->port_num];
348 struct be_drv_stats *drvs = &adapter->drv_stats;
89a88ab8 349
ac124ff9 350 be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
89a88ab8
AK
351 drvs->rx_pause_frames = port_stats->rx_pause_frames;
352 drvs->rx_crc_errors = port_stats->rx_crc_errors;
353 drvs->rx_control_frames = port_stats->rx_control_frames;
354 drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
355 drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
356 drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
357 drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
358 drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
359 drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
360 drvs->rxpp_fifo_overflow_drop = port_stats->rx_fifo_overflow;
361 drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
362 drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
363 drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
364 drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
ac124ff9 365 drvs->rx_input_fifo_overflow_drop = port_stats->rx_input_fifo_overflow;
89a88ab8
AK
366 drvs->rx_dropped_header_too_small =
367 port_stats->rx_dropped_header_too_small;
18fb06a1
SR
368 drvs->rx_address_filtered =
369 port_stats->rx_address_filtered +
370 port_stats->rx_vlan_filtered;
89a88ab8
AK
371 drvs->rx_alignment_symbol_errors =
372 port_stats->rx_alignment_symbol_errors;
373
374 drvs->tx_pauseframes = port_stats->tx_pauseframes;
375 drvs->tx_controlframes = port_stats->tx_controlframes;
376
377 if (adapter->port_num)
ac124ff9 378 drvs->jabber_events = rxf_stats->port1_jabber_events;
89a88ab8 379 else
ac124ff9 380 drvs->jabber_events = rxf_stats->port0_jabber_events;
89a88ab8 381 drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
89a88ab8 382 drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
89a88ab8
AK
383 drvs->forwarded_packets = rxf_stats->forwarded_packets;
384 drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
ac124ff9
SP
385 drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
386 drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
89a88ab8
AK
387 adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
388}
389
ca34fe38 390static void populate_be_v1_stats(struct be_adapter *adapter)
89a88ab8 391{
ac124ff9
SP
392 struct be_hw_stats_v1 *hw_stats = hw_stats_from_cmd(adapter);
393 struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
394 struct be_rxf_stats_v1 *rxf_stats = &hw_stats->rxf;
89a88ab8 395 struct be_port_rxf_stats_v1 *port_stats =
ac124ff9
SP
396 &rxf_stats->port[adapter->port_num];
397 struct be_drv_stats *drvs = &adapter->drv_stats;
89a88ab8 398
ac124ff9 399 be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
02fe7027
AK
400 drvs->pmem_fifo_overflow_drop = port_stats->pmem_fifo_overflow_drop;
401 drvs->rx_priority_pause_frames = port_stats->rx_priority_pause_frames;
89a88ab8
AK
402 drvs->rx_pause_frames = port_stats->rx_pause_frames;
403 drvs->rx_crc_errors = port_stats->rx_crc_errors;
404 drvs->rx_control_frames = port_stats->rx_control_frames;
405 drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
406 drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
407 drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
408 drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
409 drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
410 drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
411 drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
412 drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
413 drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
414 drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
415 drvs->rx_dropped_header_too_small =
416 port_stats->rx_dropped_header_too_small;
417 drvs->rx_input_fifo_overflow_drop =
418 port_stats->rx_input_fifo_overflow_drop;
18fb06a1 419 drvs->rx_address_filtered = port_stats->rx_address_filtered;
89a88ab8
AK
420 drvs->rx_alignment_symbol_errors =
421 port_stats->rx_alignment_symbol_errors;
ac124ff9 422 drvs->rxpp_fifo_overflow_drop = port_stats->rxpp_fifo_overflow_drop;
89a88ab8
AK
423 drvs->tx_pauseframes = port_stats->tx_pauseframes;
424 drvs->tx_controlframes = port_stats->tx_controlframes;
b5adffc4 425 drvs->tx_priority_pauseframes = port_stats->tx_priority_pauseframes;
89a88ab8
AK
426 drvs->jabber_events = port_stats->jabber_events;
427 drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
89a88ab8 428 drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
89a88ab8
AK
429 drvs->forwarded_packets = rxf_stats->forwarded_packets;
430 drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
ac124ff9
SP
431 drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
432 drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
89a88ab8
AK
433 adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
434}
435
61000861
AK
436static void populate_be_v2_stats(struct be_adapter *adapter)
437{
438 struct be_hw_stats_v2 *hw_stats = hw_stats_from_cmd(adapter);
439 struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
440 struct be_rxf_stats_v2 *rxf_stats = &hw_stats->rxf;
441 struct be_port_rxf_stats_v2 *port_stats =
442 &rxf_stats->port[adapter->port_num];
443 struct be_drv_stats *drvs = &adapter->drv_stats;
444
445 be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
446 drvs->pmem_fifo_overflow_drop = port_stats->pmem_fifo_overflow_drop;
447 drvs->rx_priority_pause_frames = port_stats->rx_priority_pause_frames;
448 drvs->rx_pause_frames = port_stats->rx_pause_frames;
449 drvs->rx_crc_errors = port_stats->rx_crc_errors;
450 drvs->rx_control_frames = port_stats->rx_control_frames;
451 drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
452 drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
453 drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
454 drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
455 drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
456 drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
457 drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
458 drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
459 drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
460 drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
461 drvs->rx_dropped_header_too_small =
462 port_stats->rx_dropped_header_too_small;
463 drvs->rx_input_fifo_overflow_drop =
464 port_stats->rx_input_fifo_overflow_drop;
465 drvs->rx_address_filtered = port_stats->rx_address_filtered;
466 drvs->rx_alignment_symbol_errors =
467 port_stats->rx_alignment_symbol_errors;
468 drvs->rxpp_fifo_overflow_drop = port_stats->rxpp_fifo_overflow_drop;
469 drvs->tx_pauseframes = port_stats->tx_pauseframes;
470 drvs->tx_controlframes = port_stats->tx_controlframes;
471 drvs->tx_priority_pauseframes = port_stats->tx_priority_pauseframes;
472 drvs->jabber_events = port_stats->jabber_events;
473 drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
474 drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
475 drvs->forwarded_packets = rxf_stats->forwarded_packets;
476 drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
477 drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
478 drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
479 adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
461ae379
AK
480 if (be_roce_supported(adapter)) {
481 drvs->rx_roce_bytes_lsd = port_stats->roce_bytes_received_lsd;
482 drvs->rx_roce_bytes_msd = port_stats->roce_bytes_received_msd;
483 drvs->rx_roce_frames = port_stats->roce_frames_received;
484 drvs->roce_drops_crc = port_stats->roce_drops_crc;
485 drvs->roce_drops_payload_len =
486 port_stats->roce_drops_payload_len;
487 }
61000861
AK
488}
489
005d5696
SX
490static void populate_lancer_stats(struct be_adapter *adapter)
491{
89a88ab8 492
005d5696 493 struct be_drv_stats *drvs = &adapter->drv_stats;
ac124ff9
SP
494 struct lancer_pport_stats *pport_stats =
495 pport_stats_from_cmd(adapter);
496
497 be_dws_le_to_cpu(pport_stats, sizeof(*pport_stats));
498 drvs->rx_pause_frames = pport_stats->rx_pause_frames_lo;
499 drvs->rx_crc_errors = pport_stats->rx_crc_errors_lo;
500 drvs->rx_control_frames = pport_stats->rx_control_frames_lo;
005d5696 501 drvs->rx_in_range_errors = pport_stats->rx_in_range_errors;
ac124ff9 502 drvs->rx_frame_too_long = pport_stats->rx_frames_too_long_lo;
005d5696
SX
503 drvs->rx_dropped_runt = pport_stats->rx_dropped_runt;
504 drvs->rx_ip_checksum_errs = pport_stats->rx_ip_checksum_errors;
505 drvs->rx_tcp_checksum_errs = pport_stats->rx_tcp_checksum_errors;
506 drvs->rx_udp_checksum_errs = pport_stats->rx_udp_checksum_errors;
507 drvs->rx_dropped_tcp_length =
508 pport_stats->rx_dropped_invalid_tcp_length;
509 drvs->rx_dropped_too_small = pport_stats->rx_dropped_too_small;
510 drvs->rx_dropped_too_short = pport_stats->rx_dropped_too_short;
511 drvs->rx_out_range_errors = pport_stats->rx_out_of_range_errors;
512 drvs->rx_dropped_header_too_small =
513 pport_stats->rx_dropped_header_too_small;
514 drvs->rx_input_fifo_overflow_drop = pport_stats->rx_fifo_overflow;
18fb06a1
SR
515 drvs->rx_address_filtered =
516 pport_stats->rx_address_filtered +
517 pport_stats->rx_vlan_filtered;
ac124ff9 518 drvs->rx_alignment_symbol_errors = pport_stats->rx_symbol_errors_lo;
005d5696 519 drvs->rxpp_fifo_overflow_drop = pport_stats->rx_fifo_overflow;
ac124ff9
SP
520 drvs->tx_pauseframes = pport_stats->tx_pause_frames_lo;
521 drvs->tx_controlframes = pport_stats->tx_control_frames_lo;
005d5696 522 drvs->jabber_events = pport_stats->rx_jabbers;
ac124ff9
SP
523 drvs->forwarded_packets = pport_stats->num_forwards_lo;
524 drvs->rx_drops_mtu = pport_stats->rx_drops_mtu_lo;
005d5696 525 drvs->rx_drops_too_many_frags =
ac124ff9 526 pport_stats->rx_drops_too_many_frags_lo;
005d5696 527}
89a88ab8 528
09c1c68f
SP
529static void accumulate_16bit_val(u32 *acc, u16 val)
530{
531#define lo(x) (x & 0xFFFF)
532#define hi(x) (x & 0xFFFF0000)
533 bool wrapped = val < lo(*acc);
534 u32 newacc = hi(*acc) + val;
535
536 if (wrapped)
537 newacc += 65536;
538 ACCESS_ONCE(*acc) = newacc;
539}
540
4188e7df 541static void populate_erx_stats(struct be_adapter *adapter,
a6c578ef
AK
542 struct be_rx_obj *rxo,
543 u32 erx_stat)
544{
545 if (!BEx_chip(adapter))
546 rx_stats(rxo)->rx_drops_no_frags = erx_stat;
547 else
548 /* below erx HW counter can actually wrap around after
549 * 65535. Driver accumulates a 32-bit value
550 */
551 accumulate_16bit_val(&rx_stats(rxo)->rx_drops_no_frags,
552 (u16)erx_stat);
553}
554
89a88ab8
AK
555void be_parse_stats(struct be_adapter *adapter)
556{
61000861 557 struct be_erx_stats_v2 *erx = be_erx_stats_from_cmd(adapter);
ac124ff9
SP
558 struct be_rx_obj *rxo;
559 int i;
a6c578ef 560 u32 erx_stat;
ac124ff9 561
ca34fe38
SP
562 if (lancer_chip(adapter)) {
563 populate_lancer_stats(adapter);
005d5696 564 } else {
ca34fe38
SP
565 if (BE2_chip(adapter))
566 populate_be_v0_stats(adapter);
61000861
AK
567 else if (BE3_chip(adapter))
568 /* for BE3 */
ca34fe38 569 populate_be_v1_stats(adapter);
61000861
AK
570 else
571 populate_be_v2_stats(adapter);
d51ebd33 572
61000861 573 /* erx_v2 is longer than v0, v1. use v2 for v0, v1 access */
ca34fe38 574 for_all_rx_queues(adapter, rxo, i) {
a6c578ef
AK
575 erx_stat = erx->rx_drops_no_fragments[rxo->q.id];
576 populate_erx_stats(adapter, rxo, erx_stat);
ca34fe38 577 }
09c1c68f 578 }
89a88ab8
AK
579}
580
ab1594e9
SP
581static struct rtnl_link_stats64 *be_get_stats64(struct net_device *netdev,
582 struct rtnl_link_stats64 *stats)
6b7c5b94 583{
ab1594e9 584 struct be_adapter *adapter = netdev_priv(netdev);
89a88ab8 585 struct be_drv_stats *drvs = &adapter->drv_stats;
3abcdeda 586 struct be_rx_obj *rxo;
3c8def97 587 struct be_tx_obj *txo;
ab1594e9
SP
588 u64 pkts, bytes;
589 unsigned int start;
3abcdeda 590 int i;
6b7c5b94 591
3abcdeda 592 for_all_rx_queues(adapter, rxo, i) {
ab1594e9
SP
593 const struct be_rx_stats *rx_stats = rx_stats(rxo);
594 do {
57a7744e 595 start = u64_stats_fetch_begin_irq(&rx_stats->sync);
ab1594e9
SP
596 pkts = rx_stats(rxo)->rx_pkts;
597 bytes = rx_stats(rxo)->rx_bytes;
57a7744e 598 } while (u64_stats_fetch_retry_irq(&rx_stats->sync, start));
ab1594e9
SP
599 stats->rx_packets += pkts;
600 stats->rx_bytes += bytes;
601 stats->multicast += rx_stats(rxo)->rx_mcast_pkts;
602 stats->rx_dropped += rx_stats(rxo)->rx_drops_no_skbs +
603 rx_stats(rxo)->rx_drops_no_frags;
3abcdeda
SP
604 }
605
3c8def97 606 for_all_tx_queues(adapter, txo, i) {
ab1594e9
SP
607 const struct be_tx_stats *tx_stats = tx_stats(txo);
608 do {
57a7744e 609 start = u64_stats_fetch_begin_irq(&tx_stats->sync);
ab1594e9
SP
610 pkts = tx_stats(txo)->tx_pkts;
611 bytes = tx_stats(txo)->tx_bytes;
57a7744e 612 } while (u64_stats_fetch_retry_irq(&tx_stats->sync, start));
ab1594e9
SP
613 stats->tx_packets += pkts;
614 stats->tx_bytes += bytes;
3c8def97 615 }
6b7c5b94
SP
616
617 /* bad pkts received */
ab1594e9 618 stats->rx_errors = drvs->rx_crc_errors +
89a88ab8
AK
619 drvs->rx_alignment_symbol_errors +
620 drvs->rx_in_range_errors +
621 drvs->rx_out_range_errors +
622 drvs->rx_frame_too_long +
623 drvs->rx_dropped_too_small +
624 drvs->rx_dropped_too_short +
625 drvs->rx_dropped_header_too_small +
626 drvs->rx_dropped_tcp_length +
ab1594e9 627 drvs->rx_dropped_runt;
68110868 628
6b7c5b94 629 /* detailed rx errors */
ab1594e9 630 stats->rx_length_errors = drvs->rx_in_range_errors +
89a88ab8
AK
631 drvs->rx_out_range_errors +
632 drvs->rx_frame_too_long;
68110868 633
ab1594e9 634 stats->rx_crc_errors = drvs->rx_crc_errors;
6b7c5b94
SP
635
636 /* frame alignment errors */
ab1594e9 637 stats->rx_frame_errors = drvs->rx_alignment_symbol_errors;
68110868 638
6b7c5b94
SP
639 /* receiver fifo overrun */
640 /* drops_no_pbuf is no per i/f, it's per BE card */
ab1594e9 641 stats->rx_fifo_errors = drvs->rxpp_fifo_overflow_drop +
89a88ab8
AK
642 drvs->rx_input_fifo_overflow_drop +
643 drvs->rx_drops_no_pbuf;
ab1594e9 644 return stats;
6b7c5b94
SP
645}
646
b236916a 647void be_link_status_update(struct be_adapter *adapter, u8 link_status)
6b7c5b94 648{
6b7c5b94
SP
649 struct net_device *netdev = adapter->netdev;
650
b236916a 651 if (!(adapter->flags & BE_FLAGS_LINK_STATUS_INIT)) {
ea172a01 652 netif_carrier_off(netdev);
b236916a 653 adapter->flags |= BE_FLAGS_LINK_STATUS_INIT;
6b7c5b94 654 }
b236916a 655
bdce2ad7 656 if (link_status)
b236916a
AK
657 netif_carrier_on(netdev);
658 else
659 netif_carrier_off(netdev);
6b7c5b94
SP
660}
661
3c8def97 662static void be_tx_stats_update(struct be_tx_obj *txo,
91992e44 663 u32 wrb_cnt, u32 copied, u32 gso_segs, bool stopped)
6b7c5b94 664{
3c8def97
SP
665 struct be_tx_stats *stats = tx_stats(txo);
666
ab1594e9 667 u64_stats_update_begin(&stats->sync);
ac124ff9
SP
668 stats->tx_reqs++;
669 stats->tx_wrbs += wrb_cnt;
670 stats->tx_bytes += copied;
671 stats->tx_pkts += (gso_segs ? gso_segs : 1);
6b7c5b94 672 if (stopped)
ac124ff9 673 stats->tx_stops++;
ab1594e9 674 u64_stats_update_end(&stats->sync);
6b7c5b94
SP
675}
676
677/* Determine number of WRB entries needed to xmit data in an skb */
fe6d2a38
SP
678static u32 wrb_cnt_for_skb(struct be_adapter *adapter, struct sk_buff *skb,
679 bool *dummy)
6b7c5b94 680{
ebc8d2ab
DM
681 int cnt = (skb->len > skb->data_len);
682
683 cnt += skb_shinfo(skb)->nr_frags;
684
6b7c5b94
SP
685 /* to account for hdr wrb */
686 cnt++;
fe6d2a38
SP
687 if (lancer_chip(adapter) || !(cnt & 1)) {
688 *dummy = false;
689 } else {
6b7c5b94
SP
690 /* add a dummy to make it an even num */
691 cnt++;
692 *dummy = true;
fe6d2a38 693 }
6b7c5b94
SP
694 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
695 return cnt;
696}
697
698static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
699{
700 wrb->frag_pa_hi = upper_32_bits(addr);
701 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
702 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
89b1f496 703 wrb->rsvd0 = 0;
6b7c5b94
SP
704}
705
1ded132d
AK
706static inline u16 be_get_tx_vlan_tag(struct be_adapter *adapter,
707 struct sk_buff *skb)
708{
709 u8 vlan_prio;
710 u16 vlan_tag;
711
712 vlan_tag = vlan_tx_tag_get(skb);
713 vlan_prio = (vlan_tag & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
714 /* If vlan priority provided by OS is NOT in available bmap */
715 if (!(adapter->vlan_prio_bmap & (1 << vlan_prio)))
716 vlan_tag = (vlan_tag & ~VLAN_PRIO_MASK) |
717 adapter->recommended_prio;
718
719 return vlan_tag;
720}
721
c9c47142
SP
722/* Used only for IP tunnel packets */
723static u16 skb_inner_ip_proto(struct sk_buff *skb)
724{
725 return (inner_ip_hdr(skb)->version == 4) ?
726 inner_ip_hdr(skb)->protocol : inner_ipv6_hdr(skb)->nexthdr;
727}
728
729static u16 skb_ip_proto(struct sk_buff *skb)
730{
731 return (ip_hdr(skb)->version == 4) ?
732 ip_hdr(skb)->protocol : ipv6_hdr(skb)->nexthdr;
733}
734
cc4ce020 735static void wrb_fill_hdr(struct be_adapter *adapter, struct be_eth_hdr_wrb *hdr,
bc0c3405 736 struct sk_buff *skb, u32 wrb_cnt, u32 len, bool skip_hw_vlan)
6b7c5b94 737{
c9c47142 738 u16 vlan_tag, proto;
cc4ce020 739
6b7c5b94
SP
740 memset(hdr, 0, sizeof(*hdr));
741
742 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
743
49e4b847 744 if (skb_is_gso(skb)) {
6b7c5b94
SP
745 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
746 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
747 hdr, skb_shinfo(skb)->gso_size);
fe6d2a38 748 if (skb_is_gso_v6(skb) && !lancer_chip(adapter))
49e4b847 749 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso6, hdr, 1);
6b7c5b94 750 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
c9c47142
SP
751 if (skb->encapsulation) {
752 AMAP_SET_BITS(struct amap_eth_hdr_wrb, ipcs, hdr, 1);
753 proto = skb_inner_ip_proto(skb);
754 } else {
755 proto = skb_ip_proto(skb);
756 }
757 if (proto == IPPROTO_TCP)
6b7c5b94 758 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
c9c47142 759 else if (proto == IPPROTO_UDP)
6b7c5b94
SP
760 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
761 }
762
4c5102f9 763 if (vlan_tx_tag_present(skb)) {
6b7c5b94 764 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
1ded132d 765 vlan_tag = be_get_tx_vlan_tag(adapter, skb);
cc4ce020 766 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag, hdr, vlan_tag);
6b7c5b94
SP
767 }
768
bc0c3405
AK
769 /* To skip HW VLAN tagging: evt = 1, compl = 0 */
770 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, !skip_hw_vlan);
6b7c5b94 771 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
6b7c5b94
SP
772 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
773 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
774}
775
2b7bcebf 776static void unmap_tx_frag(struct device *dev, struct be_eth_wrb *wrb,
7101e111
SP
777 bool unmap_single)
778{
779 dma_addr_t dma;
780
781 be_dws_le_to_cpu(wrb, sizeof(*wrb));
782
783 dma = (u64)wrb->frag_pa_hi << 32 | (u64)wrb->frag_pa_lo;
b681ee77 784 if (wrb->frag_len) {
7101e111 785 if (unmap_single)
2b7bcebf
IV
786 dma_unmap_single(dev, dma, wrb->frag_len,
787 DMA_TO_DEVICE);
7101e111 788 else
2b7bcebf 789 dma_unmap_page(dev, dma, wrb->frag_len, DMA_TO_DEVICE);
7101e111
SP
790 }
791}
6b7c5b94 792
3c8def97 793static int make_tx_wrbs(struct be_adapter *adapter, struct be_queue_info *txq,
bc0c3405
AK
794 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb,
795 bool skip_hw_vlan)
6b7c5b94 796{
7101e111
SP
797 dma_addr_t busaddr;
798 int i, copied = 0;
2b7bcebf 799 struct device *dev = &adapter->pdev->dev;
6b7c5b94 800 struct sk_buff *first_skb = skb;
6b7c5b94
SP
801 struct be_eth_wrb *wrb;
802 struct be_eth_hdr_wrb *hdr;
7101e111
SP
803 bool map_single = false;
804 u16 map_head;
6b7c5b94 805
6b7c5b94
SP
806 hdr = queue_head_node(txq);
807 queue_head_inc(txq);
7101e111 808 map_head = txq->head;
6b7c5b94 809
ebc8d2ab 810 if (skb->len > skb->data_len) {
e743d313 811 int len = skb_headlen(skb);
2b7bcebf
IV
812 busaddr = dma_map_single(dev, skb->data, len, DMA_TO_DEVICE);
813 if (dma_mapping_error(dev, busaddr))
7101e111
SP
814 goto dma_err;
815 map_single = true;
ebc8d2ab
DM
816 wrb = queue_head_node(txq);
817 wrb_fill(wrb, busaddr, len);
818 be_dws_cpu_to_le(wrb, sizeof(*wrb));
819 queue_head_inc(txq);
820 copied += len;
821 }
6b7c5b94 822
ebc8d2ab 823 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
9e903e08 824 const struct skb_frag_struct *frag =
ebc8d2ab 825 &skb_shinfo(skb)->frags[i];
b061b39e 826 busaddr = skb_frag_dma_map(dev, frag, 0,
9e903e08 827 skb_frag_size(frag), DMA_TO_DEVICE);
2b7bcebf 828 if (dma_mapping_error(dev, busaddr))
7101e111 829 goto dma_err;
ebc8d2ab 830 wrb = queue_head_node(txq);
9e903e08 831 wrb_fill(wrb, busaddr, skb_frag_size(frag));
ebc8d2ab
DM
832 be_dws_cpu_to_le(wrb, sizeof(*wrb));
833 queue_head_inc(txq);
9e903e08 834 copied += skb_frag_size(frag);
6b7c5b94
SP
835 }
836
837 if (dummy_wrb) {
838 wrb = queue_head_node(txq);
839 wrb_fill(wrb, 0, 0);
840 be_dws_cpu_to_le(wrb, sizeof(*wrb));
841 queue_head_inc(txq);
842 }
843
bc0c3405 844 wrb_fill_hdr(adapter, hdr, first_skb, wrb_cnt, copied, skip_hw_vlan);
6b7c5b94
SP
845 be_dws_cpu_to_le(hdr, sizeof(*hdr));
846
847 return copied;
7101e111
SP
848dma_err:
849 txq->head = map_head;
850 while (copied) {
851 wrb = queue_head_node(txq);
2b7bcebf 852 unmap_tx_frag(dev, wrb, map_single);
7101e111
SP
853 map_single = false;
854 copied -= wrb->frag_len;
855 queue_head_inc(txq);
856 }
857 return 0;
6b7c5b94
SP
858}
859
93040ae5 860static struct sk_buff *be_insert_vlan_in_pkt(struct be_adapter *adapter,
bc0c3405
AK
861 struct sk_buff *skb,
862 bool *skip_hw_vlan)
93040ae5
SK
863{
864 u16 vlan_tag = 0;
865
866 skb = skb_share_check(skb, GFP_ATOMIC);
867 if (unlikely(!skb))
868 return skb;
869
efee8e87 870 if (vlan_tx_tag_present(skb))
93040ae5 871 vlan_tag = be_get_tx_vlan_tag(adapter, skb);
52fe29e4
SB
872
873 if (qnq_async_evt_rcvd(adapter) && adapter->pvid) {
874 if (!vlan_tag)
875 vlan_tag = adapter->pvid;
876 /* f/w workaround to set skip_hw_vlan = 1, informs the F/W to
877 * skip VLAN insertion
878 */
879 if (skip_hw_vlan)
880 *skip_hw_vlan = true;
881 }
bc0c3405
AK
882
883 if (vlan_tag) {
58717686 884 skb = __vlan_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
bc0c3405
AK
885 if (unlikely(!skb))
886 return skb;
bc0c3405
AK
887 skb->vlan_tci = 0;
888 }
889
890 /* Insert the outer VLAN, if any */
891 if (adapter->qnq_vid) {
892 vlan_tag = adapter->qnq_vid;
58717686 893 skb = __vlan_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
bc0c3405
AK
894 if (unlikely(!skb))
895 return skb;
896 if (skip_hw_vlan)
897 *skip_hw_vlan = true;
898 }
899
93040ae5
SK
900 return skb;
901}
902
bc0c3405
AK
903static bool be_ipv6_exthdr_check(struct sk_buff *skb)
904{
905 struct ethhdr *eh = (struct ethhdr *)skb->data;
906 u16 offset = ETH_HLEN;
907
908 if (eh->h_proto == htons(ETH_P_IPV6)) {
909 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + offset);
910
911 offset += sizeof(struct ipv6hdr);
912 if (ip6h->nexthdr != NEXTHDR_TCP &&
913 ip6h->nexthdr != NEXTHDR_UDP) {
914 struct ipv6_opt_hdr *ehdr =
915 (struct ipv6_opt_hdr *) (skb->data + offset);
916
917 /* offending pkt: 2nd byte following IPv6 hdr is 0xff */
918 if (ehdr->hdrlen == 0xff)
919 return true;
920 }
921 }
922 return false;
923}
924
925static int be_vlan_tag_tx_chk(struct be_adapter *adapter, struct sk_buff *skb)
926{
927 return vlan_tx_tag_present(skb) || adapter->pvid || adapter->qnq_vid;
928}
929
ee9c799c
SP
930static int be_ipv6_tx_stall_chk(struct be_adapter *adapter,
931 struct sk_buff *skb)
bc0c3405 932{
ee9c799c 933 return BE3_chip(adapter) && be_ipv6_exthdr_check(skb);
bc0c3405
AK
934}
935
ec495fac
VV
936static struct sk_buff *be_lancer_xmit_workarounds(struct be_adapter *adapter,
937 struct sk_buff *skb,
938 bool *skip_hw_vlan)
6b7c5b94 939{
d2cb6ce7 940 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
ee9c799c
SP
941 unsigned int eth_hdr_len;
942 struct iphdr *ip;
93040ae5 943
1297f9db
AK
944 /* For padded packets, BE HW modifies tot_len field in IP header
945 * incorrecly when VLAN tag is inserted by HW.
3904dcc4 946 * For padded packets, Lancer computes incorrect checksum.
1ded132d 947 */
ee9c799c
SP
948 eth_hdr_len = ntohs(skb->protocol) == ETH_P_8021Q ?
949 VLAN_ETH_HLEN : ETH_HLEN;
3904dcc4
SK
950 if (skb->len <= 60 &&
951 (lancer_chip(adapter) || vlan_tx_tag_present(skb)) &&
ee9c799c 952 is_ipv4_pkt(skb)) {
93040ae5
SK
953 ip = (struct iphdr *)ip_hdr(skb);
954 pskb_trim(skb, eth_hdr_len + ntohs(ip->tot_len));
955 }
1ded132d 956
d2cb6ce7 957 /* If vlan tag is already inlined in the packet, skip HW VLAN
f93f160b 958 * tagging in pvid-tagging mode
d2cb6ce7 959 */
f93f160b 960 if (be_pvid_tagging_enabled(adapter) &&
d2cb6ce7 961 veh->h_vlan_proto == htons(ETH_P_8021Q))
ee9c799c 962 *skip_hw_vlan = true;
d2cb6ce7 963
93040ae5
SK
964 /* HW has a bug wherein it will calculate CSUM for VLAN
965 * pkts even though it is disabled.
966 * Manually insert VLAN in pkt.
967 */
968 if (skb->ip_summed != CHECKSUM_PARTIAL &&
ee9c799c
SP
969 vlan_tx_tag_present(skb)) {
970 skb = be_insert_vlan_in_pkt(adapter, skb, skip_hw_vlan);
bc0c3405 971 if (unlikely(!skb))
c9128951 972 goto err;
bc0c3405
AK
973 }
974
975 /* HW may lockup when VLAN HW tagging is requested on
976 * certain ipv6 packets. Drop such pkts if the HW workaround to
977 * skip HW tagging is not enabled by FW.
978 */
979 if (unlikely(be_ipv6_tx_stall_chk(adapter, skb) &&
ee9c799c
SP
980 (adapter->pvid || adapter->qnq_vid) &&
981 !qnq_async_evt_rcvd(adapter)))
bc0c3405
AK
982 goto tx_drop;
983
984 /* Manual VLAN tag insertion to prevent:
985 * ASIC lockup when the ASIC inserts VLAN tag into
986 * certain ipv6 packets. Insert VLAN tags in driver,
987 * and set event, completion, vlan bits accordingly
988 * in the Tx WRB.
989 */
990 if (be_ipv6_tx_stall_chk(adapter, skb) &&
991 be_vlan_tag_tx_chk(adapter, skb)) {
ee9c799c 992 skb = be_insert_vlan_in_pkt(adapter, skb, skip_hw_vlan);
1ded132d 993 if (unlikely(!skb))
c9128951 994 goto err;
1ded132d
AK
995 }
996
ee9c799c
SP
997 return skb;
998tx_drop:
999 dev_kfree_skb_any(skb);
c9128951 1000err:
ee9c799c
SP
1001 return NULL;
1002}
1003
ec495fac
VV
1004static struct sk_buff *be_xmit_workarounds(struct be_adapter *adapter,
1005 struct sk_buff *skb,
1006 bool *skip_hw_vlan)
1007{
1008 /* Lancer, SH-R ASICs have a bug wherein Packets that are 32 bytes or
1009 * less may cause a transmit stall on that port. So the work-around is
1010 * to pad short packets (<= 32 bytes) to a 36-byte length.
1011 */
1012 if (unlikely(!BEx_chip(adapter) && skb->len <= 32)) {
1013 if (skb_padto(skb, 36))
1014 return NULL;
1015 skb->len = 36;
1016 }
1017
1018 if (BEx_chip(adapter) || lancer_chip(adapter)) {
1019 skb = be_lancer_xmit_workarounds(adapter, skb, skip_hw_vlan);
1020 if (!skb)
1021 return NULL;
1022 }
1023
1024 return skb;
1025}
1026
ee9c799c
SP
1027static netdev_tx_t be_xmit(struct sk_buff *skb, struct net_device *netdev)
1028{
1029 struct be_adapter *adapter = netdev_priv(netdev);
1030 struct be_tx_obj *txo = &adapter->tx_obj[skb_get_queue_mapping(skb)];
1031 struct be_queue_info *txq = &txo->q;
1032 bool dummy_wrb, stopped = false;
1033 u32 wrb_cnt = 0, copied = 0;
1034 bool skip_hw_vlan = false;
1035 u32 start = txq->head;
1036
1037 skb = be_xmit_workarounds(adapter, skb, &skip_hw_vlan);
bc617526
SP
1038 if (!skb) {
1039 tx_stats(txo)->tx_drv_drops++;
ee9c799c 1040 return NETDEV_TX_OK;
bc617526 1041 }
ee9c799c 1042
fe6d2a38 1043 wrb_cnt = wrb_cnt_for_skb(adapter, skb, &dummy_wrb);
6b7c5b94 1044
bc0c3405
AK
1045 copied = make_tx_wrbs(adapter, txq, skb, wrb_cnt, dummy_wrb,
1046 skip_hw_vlan);
c190e3c8 1047 if (copied) {
cd8f76c0
ED
1048 int gso_segs = skb_shinfo(skb)->gso_segs;
1049
c190e3c8 1050 /* record the sent skb in the sent_skb table */
3c8def97
SP
1051 BUG_ON(txo->sent_skb_list[start]);
1052 txo->sent_skb_list[start] = skb;
c190e3c8
AK
1053
1054 /* Ensure txq has space for the next skb; Else stop the queue
1055 * *BEFORE* ringing the tx doorbell, so that we serialze the
1056 * tx compls of the current transmit which'll wake up the queue
1057 */
7101e111 1058 atomic_add(wrb_cnt, &txq->used);
c190e3c8
AK
1059 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
1060 txq->len) {
3c8def97 1061 netif_stop_subqueue(netdev, skb_get_queue_mapping(skb));
c190e3c8
AK
1062 stopped = true;
1063 }
6b7c5b94 1064
94d73aaa 1065 be_txq_notify(adapter, txo, wrb_cnt);
6b7c5b94 1066
cd8f76c0 1067 be_tx_stats_update(txo, wrb_cnt, copied, gso_segs, stopped);
c190e3c8
AK
1068 } else {
1069 txq->head = start;
bc617526 1070 tx_stats(txo)->tx_drv_drops++;
c190e3c8 1071 dev_kfree_skb_any(skb);
6b7c5b94 1072 }
6b7c5b94
SP
1073 return NETDEV_TX_OK;
1074}
1075
1076static int be_change_mtu(struct net_device *netdev, int new_mtu)
1077{
1078 struct be_adapter *adapter = netdev_priv(netdev);
1079 if (new_mtu < BE_MIN_MTU ||
34a89b8c
AK
1080 new_mtu > (BE_MAX_JUMBO_FRAME_SIZE -
1081 (ETH_HLEN + ETH_FCS_LEN))) {
6b7c5b94
SP
1082 dev_info(&adapter->pdev->dev,
1083 "MTU must be between %d and %d bytes\n",
34a89b8c
AK
1084 BE_MIN_MTU,
1085 (BE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN)));
6b7c5b94
SP
1086 return -EINVAL;
1087 }
1088 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
1089 netdev->mtu, new_mtu);
1090 netdev->mtu = new_mtu;
1091 return 0;
1092}
1093
1094/*
82903e4b
AK
1095 * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
1096 * If the user configures more, place BE in vlan promiscuous mode.
6b7c5b94 1097 */
10329df8 1098static int be_vid_config(struct be_adapter *adapter)
6b7c5b94 1099{
10329df8
SP
1100 u16 vids[BE_NUM_VLANS_SUPPORTED];
1101 u16 num = 0, i;
82903e4b 1102 int status = 0;
1da87b7f 1103
c0e64ef4
SP
1104 /* No need to further configure vids if in promiscuous mode */
1105 if (adapter->promiscuous)
1106 return 0;
1107
92bf14ab 1108 if (adapter->vlans_added > be_max_vlans(adapter))
0fc16ebf
PR
1109 goto set_vlan_promisc;
1110
1111 /* Construct VLAN Table to give to HW */
1112 for (i = 0; i < VLAN_N_VID; i++)
1113 if (adapter->vlan_tag[i])
10329df8 1114 vids[num++] = cpu_to_le16(i);
0fc16ebf
PR
1115
1116 status = be_cmd_vlan_config(adapter, adapter->if_handle,
012bd387 1117 vids, num, 0);
0fc16ebf 1118
0fc16ebf 1119 if (status) {
d9d604f8
AK
1120 /* Set to VLAN promisc mode as setting VLAN filter failed */
1121 if (status == MCC_ADDL_STS_INSUFFICIENT_RESOURCES)
1122 goto set_vlan_promisc;
1123 dev_err(&adapter->pdev->dev,
1124 "Setting HW VLAN filtering failed.\n");
1125 } else {
1126 if (adapter->flags & BE_FLAGS_VLAN_PROMISC) {
1127 /* hw VLAN filtering re-enabled. */
1128 status = be_cmd_rx_filter(adapter,
1129 BE_FLAGS_VLAN_PROMISC, OFF);
1130 if (!status) {
1131 dev_info(&adapter->pdev->dev,
1132 "Disabling VLAN Promiscuous mode.\n");
1133 adapter->flags &= ~BE_FLAGS_VLAN_PROMISC;
d9d604f8
AK
1134 }
1135 }
6b7c5b94 1136 }
1da87b7f 1137
b31c50a7 1138 return status;
0fc16ebf
PR
1139
1140set_vlan_promisc:
a6b74e01
SK
1141 if (adapter->flags & BE_FLAGS_VLAN_PROMISC)
1142 return 0;
d9d604f8
AK
1143
1144 status = be_cmd_rx_filter(adapter, BE_FLAGS_VLAN_PROMISC, ON);
1145 if (!status) {
1146 dev_info(&adapter->pdev->dev, "Enable VLAN Promiscuous mode\n");
d9d604f8
AK
1147 adapter->flags |= BE_FLAGS_VLAN_PROMISC;
1148 } else
1149 dev_err(&adapter->pdev->dev,
1150 "Failed to enable VLAN Promiscuous mode.\n");
0fc16ebf 1151 return status;
6b7c5b94
SP
1152}
1153
80d5c368 1154static int be_vlan_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
6b7c5b94
SP
1155{
1156 struct be_adapter *adapter = netdev_priv(netdev);
80817cbf 1157 int status = 0;
6b7c5b94 1158
a85e9986
PR
1159 /* Packets with VID 0 are always received by Lancer by default */
1160 if (lancer_chip(adapter) && vid == 0)
48291c22
VV
1161 return status;
1162
1163 if (adapter->vlan_tag[vid])
1164 return status;
a85e9986 1165
6b7c5b94 1166 adapter->vlan_tag[vid] = 1;
a6b74e01 1167 adapter->vlans_added++;
8e586137 1168
a6b74e01
SK
1169 status = be_vid_config(adapter);
1170 if (status) {
1171 adapter->vlans_added--;
80817cbf 1172 adapter->vlan_tag[vid] = 0;
a6b74e01 1173 }
48291c22 1174
80817cbf 1175 return status;
6b7c5b94
SP
1176}
1177
80d5c368 1178static int be_vlan_rem_vid(struct net_device *netdev, __be16 proto, u16 vid)
6b7c5b94
SP
1179{
1180 struct be_adapter *adapter = netdev_priv(netdev);
80817cbf 1181 int status = 0;
6b7c5b94 1182
a85e9986
PR
1183 /* Packets with VID 0 are always received by Lancer by default */
1184 if (lancer_chip(adapter) && vid == 0)
1185 goto ret;
1186
6b7c5b94 1187 adapter->vlan_tag[vid] = 0;
a6b74e01 1188 status = be_vid_config(adapter);
80817cbf
AK
1189 if (!status)
1190 adapter->vlans_added--;
1191 else
1192 adapter->vlan_tag[vid] = 1;
1193ret:
1194 return status;
6b7c5b94
SP
1195}
1196
7ad09458
S
1197static void be_clear_promisc(struct be_adapter *adapter)
1198{
1199 adapter->promiscuous = false;
1200 adapter->flags &= ~BE_FLAGS_VLAN_PROMISC;
1201
1202 be_cmd_rx_filter(adapter, IFF_PROMISC, OFF);
1203}
1204
a54769f5 1205static void be_set_rx_mode(struct net_device *netdev)
6b7c5b94
SP
1206{
1207 struct be_adapter *adapter = netdev_priv(netdev);
0fc16ebf 1208 int status;
6b7c5b94 1209
24307eef 1210 if (netdev->flags & IFF_PROMISC) {
5b8821b7 1211 be_cmd_rx_filter(adapter, IFF_PROMISC, ON);
24307eef
SP
1212 adapter->promiscuous = true;
1213 goto done;
6b7c5b94
SP
1214 }
1215
25985edc 1216 /* BE was previously in promiscuous mode; disable it */
24307eef 1217 if (adapter->promiscuous) {
7ad09458 1218 be_clear_promisc(adapter);
c0e64ef4 1219 if (adapter->vlans_added)
10329df8 1220 be_vid_config(adapter);
6b7c5b94
SP
1221 }
1222
e7b909a6 1223 /* Enable multicast promisc if num configured exceeds what we support */
4cd24eaf 1224 if (netdev->flags & IFF_ALLMULTI ||
92bf14ab 1225 netdev_mc_count(netdev) > be_max_mc(adapter)) {
5b8821b7 1226 be_cmd_rx_filter(adapter, IFF_ALLMULTI, ON);
24307eef 1227 goto done;
6b7c5b94 1228 }
6b7c5b94 1229
fbc13f01
AK
1230 if (netdev_uc_count(netdev) != adapter->uc_macs) {
1231 struct netdev_hw_addr *ha;
1232 int i = 1; /* First slot is claimed by the Primary MAC */
1233
1234 for (; adapter->uc_macs > 0; adapter->uc_macs--, i++) {
1235 be_cmd_pmac_del(adapter, adapter->if_handle,
1236 adapter->pmac_id[i], 0);
1237 }
1238
92bf14ab 1239 if (netdev_uc_count(netdev) > be_max_uc(adapter)) {
fbc13f01
AK
1240 be_cmd_rx_filter(adapter, IFF_PROMISC, ON);
1241 adapter->promiscuous = true;
1242 goto done;
1243 }
1244
1245 netdev_for_each_uc_addr(ha, adapter->netdev) {
1246 adapter->uc_macs++; /* First slot is for Primary MAC */
1247 be_cmd_pmac_add(adapter, (u8 *)ha->addr,
1248 adapter->if_handle,
1249 &adapter->pmac_id[adapter->uc_macs], 0);
1250 }
1251 }
1252
0fc16ebf
PR
1253 status = be_cmd_rx_filter(adapter, IFF_MULTICAST, ON);
1254
1255 /* Set to MCAST promisc mode if setting MULTICAST address fails */
1256 if (status) {
1257 dev_info(&adapter->pdev->dev, "Exhausted multicast HW filters.\n");
1258 dev_info(&adapter->pdev->dev, "Disabling HW multicast filtering.\n");
1259 be_cmd_rx_filter(adapter, IFF_ALLMULTI, ON);
1260 }
24307eef
SP
1261done:
1262 return;
6b7c5b94
SP
1263}
1264
ba343c77
SB
1265static int be_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1266{
1267 struct be_adapter *adapter = netdev_priv(netdev);
11ac75ed 1268 struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
ba343c77
SB
1269 int status;
1270
11ac75ed 1271 if (!sriov_enabled(adapter))
ba343c77
SB
1272 return -EPERM;
1273
11ac75ed 1274 if (!is_valid_ether_addr(mac) || vf >= adapter->num_vfs)
ba343c77
SB
1275 return -EINVAL;
1276
3175d8c2
SP
1277 if (BEx_chip(adapter)) {
1278 be_cmd_pmac_del(adapter, vf_cfg->if_handle, vf_cfg->pmac_id,
1279 vf + 1);
ba343c77 1280
11ac75ed
SP
1281 status = be_cmd_pmac_add(adapter, mac, vf_cfg->if_handle,
1282 &vf_cfg->pmac_id, vf + 1);
3175d8c2
SP
1283 } else {
1284 status = be_cmd_set_mac(adapter, mac, vf_cfg->if_handle,
1285 vf + 1);
590c391d
PR
1286 }
1287
64600ea5 1288 if (status)
ba343c77
SB
1289 dev_err(&adapter->pdev->dev, "MAC %pM set on VF %d Failed\n",
1290 mac, vf);
64600ea5 1291 else
11ac75ed 1292 memcpy(vf_cfg->mac_addr, mac, ETH_ALEN);
64600ea5 1293
ba343c77
SB
1294 return status;
1295}
1296
64600ea5
AK
1297static int be_get_vf_config(struct net_device *netdev, int vf,
1298 struct ifla_vf_info *vi)
1299{
1300 struct be_adapter *adapter = netdev_priv(netdev);
11ac75ed 1301 struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
64600ea5 1302
11ac75ed 1303 if (!sriov_enabled(adapter))
64600ea5
AK
1304 return -EPERM;
1305
11ac75ed 1306 if (vf >= adapter->num_vfs)
64600ea5
AK
1307 return -EINVAL;
1308
1309 vi->vf = vf;
11ac75ed 1310 vi->tx_rate = vf_cfg->tx_rate;
a60b3a13
AK
1311 vi->vlan = vf_cfg->vlan_tag & VLAN_VID_MASK;
1312 vi->qos = vf_cfg->vlan_tag >> VLAN_PRIO_SHIFT;
11ac75ed 1313 memcpy(&vi->mac, vf_cfg->mac_addr, ETH_ALEN);
bdce2ad7 1314 vi->linkstate = adapter->vf_cfg[vf].plink_tracking;
64600ea5
AK
1315
1316 return 0;
1317}
1318
1da87b7f
AK
1319static int be_set_vf_vlan(struct net_device *netdev,
1320 int vf, u16 vlan, u8 qos)
1321{
1322 struct be_adapter *adapter = netdev_priv(netdev);
b9fc0e53 1323 struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1da87b7f
AK
1324 int status = 0;
1325
11ac75ed 1326 if (!sriov_enabled(adapter))
1da87b7f
AK
1327 return -EPERM;
1328
b9fc0e53 1329 if (vf >= adapter->num_vfs || vlan > 4095 || qos > 7)
1da87b7f
AK
1330 return -EINVAL;
1331
b9fc0e53
AK
1332 if (vlan || qos) {
1333 vlan |= qos << VLAN_PRIO_SHIFT;
c502224e 1334 if (vf_cfg->vlan_tag != vlan)
b9fc0e53
AK
1335 status = be_cmd_set_hsw_config(adapter, vlan, vf + 1,
1336 vf_cfg->if_handle, 0);
1da87b7f 1337 } else {
f1f3ee1b 1338 /* Reset Transparent Vlan Tagging. */
c502224e
SK
1339 status = be_cmd_set_hsw_config(adapter, BE_RESET_VLAN_TAG_ID,
1340 vf + 1, vf_cfg->if_handle, 0);
1da87b7f
AK
1341 }
1342
c502224e
SK
1343 if (!status)
1344 vf_cfg->vlan_tag = vlan;
1345 else
1da87b7f 1346 dev_info(&adapter->pdev->dev,
c502224e 1347 "VLAN %d config on VF %d failed\n", vlan, vf);
1da87b7f
AK
1348 return status;
1349}
1350
e1d18735
AK
1351static int be_set_vf_tx_rate(struct net_device *netdev,
1352 int vf, int rate)
1353{
1354 struct be_adapter *adapter = netdev_priv(netdev);
1355 int status = 0;
1356
11ac75ed 1357 if (!sriov_enabled(adapter))
e1d18735
AK
1358 return -EPERM;
1359
94f434c2 1360 if (vf >= adapter->num_vfs)
e1d18735
AK
1361 return -EINVAL;
1362
94f434c2
AK
1363 if (rate < 100 || rate > 10000) {
1364 dev_err(&adapter->pdev->dev,
1365 "tx rate must be between 100 and 10000 Mbps\n");
1366 return -EINVAL;
1367 }
e1d18735 1368
a401801c 1369 status = be_cmd_config_qos(adapter, rate / 10, vf + 1);
e1d18735 1370 if (status)
94f434c2 1371 dev_err(&adapter->pdev->dev,
e1d18735 1372 "tx rate %d on VF %d failed\n", rate, vf);
94f434c2
AK
1373 else
1374 adapter->vf_cfg[vf].tx_rate = rate;
e1d18735
AK
1375 return status;
1376}
bdce2ad7
SR
1377static int be_set_vf_link_state(struct net_device *netdev, int vf,
1378 int link_state)
1379{
1380 struct be_adapter *adapter = netdev_priv(netdev);
1381 int status;
1382
1383 if (!sriov_enabled(adapter))
1384 return -EPERM;
1385
1386 if (vf >= adapter->num_vfs)
1387 return -EINVAL;
1388
1389 status = be_cmd_set_logical_link_config(adapter, link_state, vf+1);
1390 if (!status)
1391 adapter->vf_cfg[vf].plink_tracking = link_state;
1392
1393 return status;
1394}
e1d18735 1395
2632bafd
SP
1396static void be_aic_update(struct be_aic_obj *aic, u64 rx_pkts, u64 tx_pkts,
1397 ulong now)
6b7c5b94 1398{
2632bafd
SP
1399 aic->rx_pkts_prev = rx_pkts;
1400 aic->tx_reqs_prev = tx_pkts;
1401 aic->jiffies = now;
1402}
ac124ff9 1403
2632bafd
SP
1404static void be_eqd_update(struct be_adapter *adapter)
1405{
1406 struct be_set_eqd set_eqd[MAX_EVT_QS];
1407 int eqd, i, num = 0, start;
1408 struct be_aic_obj *aic;
1409 struct be_eq_obj *eqo;
1410 struct be_rx_obj *rxo;
1411 struct be_tx_obj *txo;
1412 u64 rx_pkts, tx_pkts;
1413 ulong now;
1414 u32 pps, delta;
10ef9ab4 1415
2632bafd
SP
1416 for_all_evt_queues(adapter, eqo, i) {
1417 aic = &adapter->aic_obj[eqo->idx];
1418 if (!aic->enable) {
1419 if (aic->jiffies)
1420 aic->jiffies = 0;
1421 eqd = aic->et_eqd;
1422 goto modify_eqd;
1423 }
6b7c5b94 1424
2632bafd
SP
1425 rxo = &adapter->rx_obj[eqo->idx];
1426 do {
57a7744e 1427 start = u64_stats_fetch_begin_irq(&rxo->stats.sync);
2632bafd 1428 rx_pkts = rxo->stats.rx_pkts;
57a7744e 1429 } while (u64_stats_fetch_retry_irq(&rxo->stats.sync, start));
10ef9ab4 1430
2632bafd
SP
1431 txo = &adapter->tx_obj[eqo->idx];
1432 do {
57a7744e 1433 start = u64_stats_fetch_begin_irq(&txo->stats.sync);
2632bafd 1434 tx_pkts = txo->stats.tx_reqs;
57a7744e 1435 } while (u64_stats_fetch_retry_irq(&txo->stats.sync, start));
6b7c5b94 1436
6b7c5b94 1437
2632bafd
SP
1438 /* Skip, if wrapped around or first calculation */
1439 now = jiffies;
1440 if (!aic->jiffies || time_before(now, aic->jiffies) ||
1441 rx_pkts < aic->rx_pkts_prev ||
1442 tx_pkts < aic->tx_reqs_prev) {
1443 be_aic_update(aic, rx_pkts, tx_pkts, now);
1444 continue;
1445 }
1446
1447 delta = jiffies_to_msecs(now - aic->jiffies);
1448 pps = (((u32)(rx_pkts - aic->rx_pkts_prev) * 1000) / delta) +
1449 (((u32)(tx_pkts - aic->tx_reqs_prev) * 1000) / delta);
1450 eqd = (pps / 15000) << 2;
10ef9ab4 1451
2632bafd
SP
1452 if (eqd < 8)
1453 eqd = 0;
1454 eqd = min_t(u32, eqd, aic->max_eqd);
1455 eqd = max_t(u32, eqd, aic->min_eqd);
1456
1457 be_aic_update(aic, rx_pkts, tx_pkts, now);
10ef9ab4 1458modify_eqd:
2632bafd
SP
1459 if (eqd != aic->prev_eqd) {
1460 set_eqd[num].delay_multiplier = (eqd * 65)/100;
1461 set_eqd[num].eq_id = eqo->q.id;
1462 aic->prev_eqd = eqd;
1463 num++;
1464 }
ac124ff9 1465 }
2632bafd
SP
1466
1467 if (num)
1468 be_cmd_modify_eqd(adapter, set_eqd, num);
6b7c5b94
SP
1469}
1470
3abcdeda 1471static void be_rx_stats_update(struct be_rx_obj *rxo,
2e588f84 1472 struct be_rx_compl_info *rxcp)
4097f663 1473{
ac124ff9 1474 struct be_rx_stats *stats = rx_stats(rxo);
1ef78abe 1475
ab1594e9 1476 u64_stats_update_begin(&stats->sync);
3abcdeda 1477 stats->rx_compl++;
2e588f84 1478 stats->rx_bytes += rxcp->pkt_size;
3abcdeda 1479 stats->rx_pkts++;
2e588f84 1480 if (rxcp->pkt_type == BE_MULTICAST_PACKET)
3abcdeda 1481 stats->rx_mcast_pkts++;
2e588f84 1482 if (rxcp->err)
ac124ff9 1483 stats->rx_compl_err++;
ab1594e9 1484 u64_stats_update_end(&stats->sync);
4097f663
SP
1485}
1486
2e588f84 1487static inline bool csum_passed(struct be_rx_compl_info *rxcp)
728a9972 1488{
19fad86f 1489 /* L4 checksum is not reliable for non TCP/UDP packets.
c9c47142
SP
1490 * Also ignore ipcksm for ipv6 pkts
1491 */
2e588f84 1492 return (rxcp->tcpf || rxcp->udpf) && rxcp->l4_csum &&
c9c47142 1493 (rxcp->ip_csum || rxcp->ipv6) && !rxcp->err;
728a9972
AK
1494}
1495
0b0ef1d0 1496static struct be_rx_page_info *get_rx_page_info(struct be_rx_obj *rxo)
6b7c5b94 1497{
10ef9ab4 1498 struct be_adapter *adapter = rxo->adapter;
6b7c5b94 1499 struct be_rx_page_info *rx_page_info;
3abcdeda 1500 struct be_queue_info *rxq = &rxo->q;
0b0ef1d0 1501 u16 frag_idx = rxq->tail;
6b7c5b94 1502
3abcdeda 1503 rx_page_info = &rxo->page_info_tbl[frag_idx];
6b7c5b94
SP
1504 BUG_ON(!rx_page_info->page);
1505
e50287be 1506 if (rx_page_info->last_frag) {
2b7bcebf
IV
1507 dma_unmap_page(&adapter->pdev->dev,
1508 dma_unmap_addr(rx_page_info, bus),
1509 adapter->big_page_size, DMA_FROM_DEVICE);
e50287be
SP
1510 rx_page_info->last_frag = false;
1511 } else {
1512 dma_sync_single_for_cpu(&adapter->pdev->dev,
1513 dma_unmap_addr(rx_page_info, bus),
1514 rx_frag_size, DMA_FROM_DEVICE);
205859a2 1515 }
6b7c5b94 1516
0b0ef1d0 1517 queue_tail_inc(rxq);
6b7c5b94
SP
1518 atomic_dec(&rxq->used);
1519 return rx_page_info;
1520}
1521
1522/* Throwaway the data in the Rx completion */
10ef9ab4
SP
1523static void be_rx_compl_discard(struct be_rx_obj *rxo,
1524 struct be_rx_compl_info *rxcp)
6b7c5b94 1525{
6b7c5b94 1526 struct be_rx_page_info *page_info;
2e588f84 1527 u16 i, num_rcvd = rxcp->num_rcvd;
6b7c5b94 1528
e80d9da6 1529 for (i = 0; i < num_rcvd; i++) {
0b0ef1d0 1530 page_info = get_rx_page_info(rxo);
e80d9da6
PR
1531 put_page(page_info->page);
1532 memset(page_info, 0, sizeof(*page_info));
6b7c5b94
SP
1533 }
1534}
1535
1536/*
1537 * skb_fill_rx_data forms a complete skb for an ether frame
1538 * indicated by rxcp.
1539 */
10ef9ab4
SP
1540static void skb_fill_rx_data(struct be_rx_obj *rxo, struct sk_buff *skb,
1541 struct be_rx_compl_info *rxcp)
6b7c5b94 1542{
6b7c5b94 1543 struct be_rx_page_info *page_info;
2e588f84
SP
1544 u16 i, j;
1545 u16 hdr_len, curr_frag_len, remaining;
6b7c5b94 1546 u8 *start;
6b7c5b94 1547
0b0ef1d0 1548 page_info = get_rx_page_info(rxo);
6b7c5b94
SP
1549 start = page_address(page_info->page) + page_info->page_offset;
1550 prefetch(start);
1551
1552 /* Copy data in the first descriptor of this completion */
2e588f84 1553 curr_frag_len = min(rxcp->pkt_size, rx_frag_size);
6b7c5b94 1554
6b7c5b94
SP
1555 skb->len = curr_frag_len;
1556 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
ac1ae5f3 1557 memcpy(skb->data, start, curr_frag_len);
6b7c5b94
SP
1558 /* Complete packet has now been moved to data */
1559 put_page(page_info->page);
1560 skb->data_len = 0;
1561 skb->tail += curr_frag_len;
1562 } else {
ac1ae5f3
ED
1563 hdr_len = ETH_HLEN;
1564 memcpy(skb->data, start, hdr_len);
6b7c5b94 1565 skb_shinfo(skb)->nr_frags = 1;
b061b39e 1566 skb_frag_set_page(skb, 0, page_info->page);
6b7c5b94
SP
1567 skb_shinfo(skb)->frags[0].page_offset =
1568 page_info->page_offset + hdr_len;
9e903e08 1569 skb_frag_size_set(&skb_shinfo(skb)->frags[0], curr_frag_len - hdr_len);
6b7c5b94 1570 skb->data_len = curr_frag_len - hdr_len;
bdb28a97 1571 skb->truesize += rx_frag_size;
6b7c5b94
SP
1572 skb->tail += hdr_len;
1573 }
205859a2 1574 page_info->page = NULL;
6b7c5b94 1575
2e588f84
SP
1576 if (rxcp->pkt_size <= rx_frag_size) {
1577 BUG_ON(rxcp->num_rcvd != 1);
1578 return;
6b7c5b94
SP
1579 }
1580
1581 /* More frags present for this completion */
2e588f84
SP
1582 remaining = rxcp->pkt_size - curr_frag_len;
1583 for (i = 1, j = 0; i < rxcp->num_rcvd; i++) {
0b0ef1d0 1584 page_info = get_rx_page_info(rxo);
2e588f84 1585 curr_frag_len = min(remaining, rx_frag_size);
6b7c5b94 1586
bd46cb6c
AK
1587 /* Coalesce all frags from the same physical page in one slot */
1588 if (page_info->page_offset == 0) {
1589 /* Fresh page */
1590 j++;
b061b39e 1591 skb_frag_set_page(skb, j, page_info->page);
bd46cb6c
AK
1592 skb_shinfo(skb)->frags[j].page_offset =
1593 page_info->page_offset;
9e903e08 1594 skb_frag_size_set(&skb_shinfo(skb)->frags[j], 0);
bd46cb6c
AK
1595 skb_shinfo(skb)->nr_frags++;
1596 } else {
1597 put_page(page_info->page);
1598 }
1599
9e903e08 1600 skb_frag_size_add(&skb_shinfo(skb)->frags[j], curr_frag_len);
6b7c5b94
SP
1601 skb->len += curr_frag_len;
1602 skb->data_len += curr_frag_len;
bdb28a97 1603 skb->truesize += rx_frag_size;
2e588f84 1604 remaining -= curr_frag_len;
205859a2 1605 page_info->page = NULL;
6b7c5b94 1606 }
bd46cb6c 1607 BUG_ON(j > MAX_SKB_FRAGS);
6b7c5b94
SP
1608}
1609
5be93b9a 1610/* Process the RX completion indicated by rxcp when GRO is disabled */
6384a4d0 1611static void be_rx_compl_process(struct be_rx_obj *rxo, struct napi_struct *napi,
10ef9ab4 1612 struct be_rx_compl_info *rxcp)
6b7c5b94 1613{
10ef9ab4 1614 struct be_adapter *adapter = rxo->adapter;
6332c8d3 1615 struct net_device *netdev = adapter->netdev;
6b7c5b94 1616 struct sk_buff *skb;
89420424 1617
bb349bb4 1618 skb = netdev_alloc_skb_ip_align(netdev, BE_RX_SKB_ALLOC_SIZE);
a058a632 1619 if (unlikely(!skb)) {
ac124ff9 1620 rx_stats(rxo)->rx_drops_no_skbs++;
10ef9ab4 1621 be_rx_compl_discard(rxo, rxcp);
6b7c5b94
SP
1622 return;
1623 }
1624
10ef9ab4 1625 skb_fill_rx_data(rxo, skb, rxcp);
6b7c5b94 1626
6332c8d3 1627 if (likely((netdev->features & NETIF_F_RXCSUM) && csum_passed(rxcp)))
728a9972 1628 skb->ip_summed = CHECKSUM_UNNECESSARY;
c6ce2f4b
SK
1629 else
1630 skb_checksum_none_assert(skb);
6b7c5b94 1631
6332c8d3 1632 skb->protocol = eth_type_trans(skb, netdev);
aaa6daec 1633 skb_record_rx_queue(skb, rxo - &adapter->rx_obj[0]);
10ef9ab4 1634 if (netdev->features & NETIF_F_RXHASH)
d2464c8c 1635 skb_set_hash(skb, rxcp->rss_hash, PKT_HASH_TYPE_L3);
c9c47142
SP
1636
1637 skb->encapsulation = rxcp->tunneled;
6384a4d0 1638 skb_mark_napi_id(skb, napi);
6b7c5b94 1639
343e43c0 1640 if (rxcp->vlanf)
86a9bad3 1641 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rxcp->vlan_tag);
4c5102f9
AK
1642
1643 netif_receive_skb(skb);
6b7c5b94
SP
1644}
1645
5be93b9a 1646/* Process the RX completion indicated by rxcp when GRO is enabled */
4188e7df
JH
1647static void be_rx_compl_process_gro(struct be_rx_obj *rxo,
1648 struct napi_struct *napi,
1649 struct be_rx_compl_info *rxcp)
6b7c5b94 1650{
10ef9ab4 1651 struct be_adapter *adapter = rxo->adapter;
6b7c5b94 1652 struct be_rx_page_info *page_info;
5be93b9a 1653 struct sk_buff *skb = NULL;
2e588f84
SP
1654 u16 remaining, curr_frag_len;
1655 u16 i, j;
3968fa1e 1656
10ef9ab4 1657 skb = napi_get_frags(napi);
5be93b9a 1658 if (!skb) {
10ef9ab4 1659 be_rx_compl_discard(rxo, rxcp);
5be93b9a
AK
1660 return;
1661 }
1662
2e588f84
SP
1663 remaining = rxcp->pkt_size;
1664 for (i = 0, j = -1; i < rxcp->num_rcvd; i++) {
0b0ef1d0 1665 page_info = get_rx_page_info(rxo);
6b7c5b94
SP
1666
1667 curr_frag_len = min(remaining, rx_frag_size);
1668
bd46cb6c
AK
1669 /* Coalesce all frags from the same physical page in one slot */
1670 if (i == 0 || page_info->page_offset == 0) {
1671 /* First frag or Fresh page */
1672 j++;
b061b39e 1673 skb_frag_set_page(skb, j, page_info->page);
5be93b9a
AK
1674 skb_shinfo(skb)->frags[j].page_offset =
1675 page_info->page_offset;
9e903e08 1676 skb_frag_size_set(&skb_shinfo(skb)->frags[j], 0);
bd46cb6c
AK
1677 } else {
1678 put_page(page_info->page);
1679 }
9e903e08 1680 skb_frag_size_add(&skb_shinfo(skb)->frags[j], curr_frag_len);
bdb28a97 1681 skb->truesize += rx_frag_size;
bd46cb6c 1682 remaining -= curr_frag_len;
6b7c5b94
SP
1683 memset(page_info, 0, sizeof(*page_info));
1684 }
bd46cb6c 1685 BUG_ON(j > MAX_SKB_FRAGS);
6b7c5b94 1686
5be93b9a 1687 skb_shinfo(skb)->nr_frags = j + 1;
2e588f84
SP
1688 skb->len = rxcp->pkt_size;
1689 skb->data_len = rxcp->pkt_size;
5be93b9a 1690 skb->ip_summed = CHECKSUM_UNNECESSARY;
aaa6daec 1691 skb_record_rx_queue(skb, rxo - &adapter->rx_obj[0]);
4b972914 1692 if (adapter->netdev->features & NETIF_F_RXHASH)
d2464c8c 1693 skb_set_hash(skb, rxcp->rss_hash, PKT_HASH_TYPE_L3);
c9c47142
SP
1694
1695 skb->encapsulation = rxcp->tunneled;
6384a4d0 1696 skb_mark_napi_id(skb, napi);
5be93b9a 1697
343e43c0 1698 if (rxcp->vlanf)
86a9bad3 1699 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rxcp->vlan_tag);
4c5102f9 1700
10ef9ab4 1701 napi_gro_frags(napi);
2e588f84
SP
1702}
1703
10ef9ab4
SP
1704static void be_parse_rx_compl_v1(struct be_eth_rx_compl *compl,
1705 struct be_rx_compl_info *rxcp)
2e588f84
SP
1706{
1707 rxcp->pkt_size =
1708 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, pktsize, compl);
1709 rxcp->vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vtp, compl);
1710 rxcp->err = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, err, compl);
1711 rxcp->tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, tcpf, compl);
9ecb42fd 1712 rxcp->udpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, udpf, compl);
2e588f84
SP
1713 rxcp->ip_csum =
1714 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, ipcksm, compl);
1715 rxcp->l4_csum =
1716 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, l4_cksm, compl);
1717 rxcp->ipv6 =
1718 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, ip_version, compl);
2e588f84
SP
1719 rxcp->num_rcvd =
1720 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, numfrags, compl);
1721 rxcp->pkt_type =
1722 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, cast_enc, compl);
4b972914 1723 rxcp->rss_hash =
c297977e 1724 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, rsshash, compl);
15d72184 1725 if (rxcp->vlanf) {
f93f160b 1726 rxcp->qnq = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, qnq,
3c709f8f
DM
1727 compl);
1728 rxcp->vlan_tag = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vlan_tag,
1729 compl);
15d72184 1730 }
12004ae9 1731 rxcp->port = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, port, compl);
c9c47142
SP
1732 rxcp->tunneled =
1733 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, tunneled, compl);
2e588f84
SP
1734}
1735
10ef9ab4
SP
1736static void be_parse_rx_compl_v0(struct be_eth_rx_compl *compl,
1737 struct be_rx_compl_info *rxcp)
2e588f84
SP
1738{
1739 rxcp->pkt_size =
1740 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, pktsize, compl);
1741 rxcp->vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vtp, compl);
1742 rxcp->err = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, err, compl);
1743 rxcp->tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, tcpf, compl);
9ecb42fd 1744 rxcp->udpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, udpf, compl);
2e588f84
SP
1745 rxcp->ip_csum =
1746 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, ipcksm, compl);
1747 rxcp->l4_csum =
1748 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, l4_cksm, compl);
1749 rxcp->ipv6 =
1750 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, ip_version, compl);
2e588f84
SP
1751 rxcp->num_rcvd =
1752 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, numfrags, compl);
1753 rxcp->pkt_type =
1754 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, cast_enc, compl);
4b972914 1755 rxcp->rss_hash =
c297977e 1756 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, rsshash, compl);
15d72184 1757 if (rxcp->vlanf) {
f93f160b 1758 rxcp->qnq = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, qnq,
3c709f8f
DM
1759 compl);
1760 rxcp->vlan_tag = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vlan_tag,
1761 compl);
15d72184 1762 }
12004ae9 1763 rxcp->port = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, port, compl);
e38b1706
SK
1764 rxcp->ip_frag = AMAP_GET_BITS(struct amap_eth_rx_compl_v0,
1765 ip_frag, compl);
2e588f84
SP
1766}
1767
1768static struct be_rx_compl_info *be_rx_compl_get(struct be_rx_obj *rxo)
1769{
1770 struct be_eth_rx_compl *compl = queue_tail_node(&rxo->cq);
1771 struct be_rx_compl_info *rxcp = &rxo->rxcp;
1772 struct be_adapter *adapter = rxo->adapter;
6b7c5b94 1773
2e588f84
SP
1774 /* For checking the valid bit it is Ok to use either definition as the
1775 * valid bit is at the same position in both v0 and v1 Rx compl */
1776 if (compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] == 0)
1777 return NULL;
6b7c5b94 1778
2e588f84
SP
1779 rmb();
1780 be_dws_le_to_cpu(compl, sizeof(*compl));
6b7c5b94 1781
2e588f84 1782 if (adapter->be3_native)
10ef9ab4 1783 be_parse_rx_compl_v1(compl, rxcp);
2e588f84 1784 else
10ef9ab4 1785 be_parse_rx_compl_v0(compl, rxcp);
6b7c5b94 1786
e38b1706
SK
1787 if (rxcp->ip_frag)
1788 rxcp->l4_csum = 0;
1789
15d72184 1790 if (rxcp->vlanf) {
f93f160b
VV
1791 /* In QNQ modes, if qnq bit is not set, then the packet was
1792 * tagged only with the transparent outer vlan-tag and must
1793 * not be treated as a vlan packet by host
1794 */
1795 if (be_is_qnq_mode(adapter) && !rxcp->qnq)
15d72184 1796 rxcp->vlanf = 0;
6b7c5b94 1797
15d72184 1798 if (!lancer_chip(adapter))
3c709f8f 1799 rxcp->vlan_tag = swab16(rxcp->vlan_tag);
6b7c5b94 1800
939cf306 1801 if (adapter->pvid == (rxcp->vlan_tag & VLAN_VID_MASK) &&
3c709f8f 1802 !adapter->vlan_tag[rxcp->vlan_tag])
15d72184
SP
1803 rxcp->vlanf = 0;
1804 }
2e588f84
SP
1805
1806 /* As the compl has been parsed, reset it; we wont touch it again */
1807 compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] = 0;
6b7c5b94 1808
3abcdeda 1809 queue_tail_inc(&rxo->cq);
6b7c5b94
SP
1810 return rxcp;
1811}
1812
1829b086 1813static inline struct page *be_alloc_pages(u32 size, gfp_t gfp)
6b7c5b94 1814{
6b7c5b94 1815 u32 order = get_order(size);
1829b086 1816
6b7c5b94 1817 if (order > 0)
1829b086
ED
1818 gfp |= __GFP_COMP;
1819 return alloc_pages(gfp, order);
6b7c5b94
SP
1820}
1821
1822/*
1823 * Allocate a page, split it to fragments of size rx_frag_size and post as
1824 * receive buffers to BE
1825 */
1829b086 1826static void be_post_rx_frags(struct be_rx_obj *rxo, gfp_t gfp)
6b7c5b94 1827{
3abcdeda 1828 struct be_adapter *adapter = rxo->adapter;
26d92f92 1829 struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
3abcdeda 1830 struct be_queue_info *rxq = &rxo->q;
6b7c5b94 1831 struct page *pagep = NULL;
ba42fad0 1832 struct device *dev = &adapter->pdev->dev;
6b7c5b94
SP
1833 struct be_eth_rx_d *rxd;
1834 u64 page_dmaaddr = 0, frag_dmaaddr;
1835 u32 posted, page_offset = 0;
1836
3abcdeda 1837 page_info = &rxo->page_info_tbl[rxq->head];
6b7c5b94
SP
1838 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
1839 if (!pagep) {
1829b086 1840 pagep = be_alloc_pages(adapter->big_page_size, gfp);
6b7c5b94 1841 if (unlikely(!pagep)) {
ac124ff9 1842 rx_stats(rxo)->rx_post_fail++;
6b7c5b94
SP
1843 break;
1844 }
ba42fad0
IV
1845 page_dmaaddr = dma_map_page(dev, pagep, 0,
1846 adapter->big_page_size,
2b7bcebf 1847 DMA_FROM_DEVICE);
ba42fad0
IV
1848 if (dma_mapping_error(dev, page_dmaaddr)) {
1849 put_page(pagep);
1850 pagep = NULL;
1851 rx_stats(rxo)->rx_post_fail++;
1852 break;
1853 }
e50287be 1854 page_offset = 0;
6b7c5b94
SP
1855 } else {
1856 get_page(pagep);
e50287be 1857 page_offset += rx_frag_size;
6b7c5b94 1858 }
e50287be 1859 page_info->page_offset = page_offset;
6b7c5b94 1860 page_info->page = pagep;
6b7c5b94
SP
1861
1862 rxd = queue_head_node(rxq);
e50287be 1863 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
6b7c5b94
SP
1864 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
1865 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
6b7c5b94
SP
1866
1867 /* Any space left in the current big page for another frag? */
1868 if ((page_offset + rx_frag_size + rx_frag_size) >
1869 adapter->big_page_size) {
1870 pagep = NULL;
e50287be
SP
1871 page_info->last_frag = true;
1872 dma_unmap_addr_set(page_info, bus, page_dmaaddr);
1873 } else {
1874 dma_unmap_addr_set(page_info, bus, frag_dmaaddr);
6b7c5b94 1875 }
26d92f92
SP
1876
1877 prev_page_info = page_info;
1878 queue_head_inc(rxq);
10ef9ab4 1879 page_info = &rxo->page_info_tbl[rxq->head];
6b7c5b94 1880 }
e50287be
SP
1881
1882 /* Mark the last frag of a page when we break out of the above loop
1883 * with no more slots available in the RXQ
1884 */
1885 if (pagep) {
1886 prev_page_info->last_frag = true;
1887 dma_unmap_addr_set(prev_page_info, bus, page_dmaaddr);
1888 }
6b7c5b94
SP
1889
1890 if (posted) {
6b7c5b94 1891 atomic_add(posted, &rxq->used);
6384a4d0
SP
1892 if (rxo->rx_post_starved)
1893 rxo->rx_post_starved = false;
8788fdc2 1894 be_rxq_notify(adapter, rxq->id, posted);
ea1dae11
SP
1895 } else if (atomic_read(&rxq->used) == 0) {
1896 /* Let be_worker replenish when memory is available */
3abcdeda 1897 rxo->rx_post_starved = true;
6b7c5b94 1898 }
6b7c5b94
SP
1899}
1900
5fb379ee 1901static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
6b7c5b94 1902{
6b7c5b94
SP
1903 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
1904
1905 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
1906 return NULL;
1907
f3eb62d2 1908 rmb();
6b7c5b94
SP
1909 be_dws_le_to_cpu(txcp, sizeof(*txcp));
1910
1911 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
1912
1913 queue_tail_inc(tx_cq);
1914 return txcp;
1915}
1916
3c8def97
SP
1917static u16 be_tx_compl_process(struct be_adapter *adapter,
1918 struct be_tx_obj *txo, u16 last_index)
6b7c5b94 1919{
3c8def97 1920 struct be_queue_info *txq = &txo->q;
a73b796e 1921 struct be_eth_wrb *wrb;
3c8def97 1922 struct sk_buff **sent_skbs = txo->sent_skb_list;
6b7c5b94 1923 struct sk_buff *sent_skb;
ec43b1a6
SP
1924 u16 cur_index, num_wrbs = 1; /* account for hdr wrb */
1925 bool unmap_skb_hdr = true;
6b7c5b94 1926
ec43b1a6 1927 sent_skb = sent_skbs[txq->tail];
6b7c5b94 1928 BUG_ON(!sent_skb);
ec43b1a6
SP
1929 sent_skbs[txq->tail] = NULL;
1930
1931 /* skip header wrb */
a73b796e 1932 queue_tail_inc(txq);
6b7c5b94 1933
ec43b1a6 1934 do {
6b7c5b94 1935 cur_index = txq->tail;
a73b796e 1936 wrb = queue_tail_node(txq);
2b7bcebf
IV
1937 unmap_tx_frag(&adapter->pdev->dev, wrb,
1938 (unmap_skb_hdr && skb_headlen(sent_skb)));
ec43b1a6
SP
1939 unmap_skb_hdr = false;
1940
6b7c5b94
SP
1941 num_wrbs++;
1942 queue_tail_inc(txq);
ec43b1a6 1943 } while (cur_index != last_index);
6b7c5b94 1944
d8ec2c02 1945 dev_kfree_skb_any(sent_skb);
4d586b82 1946 return num_wrbs;
6b7c5b94
SP
1947}
1948
10ef9ab4
SP
1949/* Return the number of events in the event queue */
1950static inline int events_get(struct be_eq_obj *eqo)
859b1e4e 1951{
10ef9ab4
SP
1952 struct be_eq_entry *eqe;
1953 int num = 0;
859b1e4e 1954
10ef9ab4
SP
1955 do {
1956 eqe = queue_tail_node(&eqo->q);
1957 if (eqe->evt == 0)
1958 break;
859b1e4e 1959
10ef9ab4
SP
1960 rmb();
1961 eqe->evt = 0;
1962 num++;
1963 queue_tail_inc(&eqo->q);
1964 } while (true);
1965
1966 return num;
859b1e4e
SP
1967}
1968
10ef9ab4
SP
1969/* Leaves the EQ is disarmed state */
1970static void be_eq_clean(struct be_eq_obj *eqo)
859b1e4e 1971{
10ef9ab4 1972 int num = events_get(eqo);
859b1e4e 1973
10ef9ab4 1974 be_eq_notify(eqo->adapter, eqo->q.id, false, true, num);
859b1e4e
SP
1975}
1976
10ef9ab4 1977static void be_rx_cq_clean(struct be_rx_obj *rxo)
6b7c5b94
SP
1978{
1979 struct be_rx_page_info *page_info;
3abcdeda
SP
1980 struct be_queue_info *rxq = &rxo->q;
1981 struct be_queue_info *rx_cq = &rxo->cq;
2e588f84 1982 struct be_rx_compl_info *rxcp;
d23e946c
SP
1983 struct be_adapter *adapter = rxo->adapter;
1984 int flush_wait = 0;
6b7c5b94 1985
d23e946c
SP
1986 /* Consume pending rx completions.
1987 * Wait for the flush completion (identified by zero num_rcvd)
1988 * to arrive. Notify CQ even when there are no more CQ entries
1989 * for HW to flush partially coalesced CQ entries.
1990 * In Lancer, there is no need to wait for flush compl.
1991 */
1992 for (;;) {
1993 rxcp = be_rx_compl_get(rxo);
1994 if (rxcp == NULL) {
1995 if (lancer_chip(adapter))
1996 break;
1997
1998 if (flush_wait++ > 10 || be_hw_error(adapter)) {
1999 dev_warn(&adapter->pdev->dev,
2000 "did not receive flush compl\n");
2001 break;
2002 }
2003 be_cq_notify(adapter, rx_cq->id, true, 0);
2004 mdelay(1);
2005 } else {
2006 be_rx_compl_discard(rxo, rxcp);
3f5dffe6 2007 be_cq_notify(adapter, rx_cq->id, false, 1);
d23e946c
SP
2008 if (rxcp->num_rcvd == 0)
2009 break;
2010 }
6b7c5b94
SP
2011 }
2012
d23e946c
SP
2013 /* After cleanup, leave the CQ in unarmed state */
2014 be_cq_notify(adapter, rx_cq->id, false, 0);
2015
2016 /* Then free posted rx buffers that were not used */
0b0ef1d0
SR
2017 while (atomic_read(&rxq->used) > 0) {
2018 page_info = get_rx_page_info(rxo);
6b7c5b94
SP
2019 put_page(page_info->page);
2020 memset(page_info, 0, sizeof(*page_info));
2021 }
2022 BUG_ON(atomic_read(&rxq->used));
482c9e79 2023 rxq->tail = rxq->head = 0;
6b7c5b94
SP
2024}
2025
0ae57bb3 2026static void be_tx_compl_clean(struct be_adapter *adapter)
6b7c5b94 2027{
0ae57bb3
SP
2028 struct be_tx_obj *txo;
2029 struct be_queue_info *txq;
a8e9179a 2030 struct be_eth_tx_compl *txcp;
4d586b82 2031 u16 end_idx, cmpl = 0, timeo = 0, num_wrbs = 0;
b03388d6
SP
2032 struct sk_buff *sent_skb;
2033 bool dummy_wrb;
0ae57bb3 2034 int i, pending_txqs;
a8e9179a 2035
1a3d0717 2036 /* Stop polling for compls when HW has been silent for 10ms */
a8e9179a 2037 do {
0ae57bb3
SP
2038 pending_txqs = adapter->num_tx_qs;
2039
2040 for_all_tx_queues(adapter, txo, i) {
1a3d0717
VV
2041 cmpl = 0;
2042 num_wrbs = 0;
0ae57bb3
SP
2043 txq = &txo->q;
2044 while ((txcp = be_tx_compl_get(&txo->cq))) {
2045 end_idx =
2046 AMAP_GET_BITS(struct amap_eth_tx_compl,
2047 wrb_index, txcp);
2048 num_wrbs += be_tx_compl_process(adapter, txo,
2049 end_idx);
2050 cmpl++;
2051 }
2052 if (cmpl) {
2053 be_cq_notify(adapter, txo->cq.id, false, cmpl);
2054 atomic_sub(num_wrbs, &txq->used);
1a3d0717 2055 timeo = 0;
0ae57bb3
SP
2056 }
2057 if (atomic_read(&txq->used) == 0)
2058 pending_txqs--;
a8e9179a
SP
2059 }
2060
1a3d0717 2061 if (pending_txqs == 0 || ++timeo > 10 || be_hw_error(adapter))
a8e9179a
SP
2062 break;
2063
2064 mdelay(1);
2065 } while (true);
2066
0ae57bb3
SP
2067 for_all_tx_queues(adapter, txo, i) {
2068 txq = &txo->q;
2069 if (atomic_read(&txq->used))
2070 dev_err(&adapter->pdev->dev, "%d pending tx-compls\n",
2071 atomic_read(&txq->used));
2072
2073 /* free posted tx for which compls will never arrive */
2074 while (atomic_read(&txq->used)) {
2075 sent_skb = txo->sent_skb_list[txq->tail];
2076 end_idx = txq->tail;
2077 num_wrbs = wrb_cnt_for_skb(adapter, sent_skb,
2078 &dummy_wrb);
2079 index_adv(&end_idx, num_wrbs - 1, txq->len);
2080 num_wrbs = be_tx_compl_process(adapter, txo, end_idx);
2081 atomic_sub(num_wrbs, &txq->used);
2082 }
b03388d6 2083 }
6b7c5b94
SP
2084}
2085
10ef9ab4
SP
2086static void be_evt_queues_destroy(struct be_adapter *adapter)
2087{
2088 struct be_eq_obj *eqo;
2089 int i;
2090
2091 for_all_evt_queues(adapter, eqo, i) {
19d59aa7
PR
2092 if (eqo->q.created) {
2093 be_eq_clean(eqo);
10ef9ab4 2094 be_cmd_q_destroy(adapter, &eqo->q, QTYPE_EQ);
6384a4d0 2095 napi_hash_del(&eqo->napi);
68d7bdcb 2096 netif_napi_del(&eqo->napi);
19d59aa7 2097 }
10ef9ab4
SP
2098 be_queue_free(adapter, &eqo->q);
2099 }
2100}
2101
2102static int be_evt_queues_create(struct be_adapter *adapter)
2103{
2104 struct be_queue_info *eq;
2105 struct be_eq_obj *eqo;
2632bafd 2106 struct be_aic_obj *aic;
10ef9ab4
SP
2107 int i, rc;
2108
92bf14ab
SP
2109 adapter->num_evt_qs = min_t(u16, num_irqs(adapter),
2110 adapter->cfg_num_qs);
10ef9ab4
SP
2111
2112 for_all_evt_queues(adapter, eqo, i) {
68d7bdcb
SP
2113 netif_napi_add(adapter->netdev, &eqo->napi, be_poll,
2114 BE_NAPI_WEIGHT);
6384a4d0 2115 napi_hash_add(&eqo->napi);
2632bafd 2116 aic = &adapter->aic_obj[i];
10ef9ab4
SP
2117 eqo->adapter = adapter;
2118 eqo->tx_budget = BE_TX_BUDGET;
2119 eqo->idx = i;
2632bafd
SP
2120 aic->max_eqd = BE_MAX_EQD;
2121 aic->enable = true;
10ef9ab4
SP
2122
2123 eq = &eqo->q;
2124 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
2125 sizeof(struct be_eq_entry));
2126 if (rc)
2127 return rc;
2128
f2f781a7 2129 rc = be_cmd_eq_create(adapter, eqo);
10ef9ab4
SP
2130 if (rc)
2131 return rc;
2132 }
1cfafab9 2133 return 0;
10ef9ab4
SP
2134}
2135
5fb379ee
SP
2136static void be_mcc_queues_destroy(struct be_adapter *adapter)
2137{
2138 struct be_queue_info *q;
5fb379ee 2139
8788fdc2 2140 q = &adapter->mcc_obj.q;
5fb379ee 2141 if (q->created)
8788fdc2 2142 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
5fb379ee
SP
2143 be_queue_free(adapter, q);
2144
8788fdc2 2145 q = &adapter->mcc_obj.cq;
5fb379ee 2146 if (q->created)
8788fdc2 2147 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
5fb379ee
SP
2148 be_queue_free(adapter, q);
2149}
2150
2151/* Must be called only after TX qs are created as MCC shares TX EQ */
2152static int be_mcc_queues_create(struct be_adapter *adapter)
2153{
2154 struct be_queue_info *q, *cq;
5fb379ee 2155
8788fdc2 2156 cq = &adapter->mcc_obj.cq;
5fb379ee 2157 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
efd2e40a 2158 sizeof(struct be_mcc_compl)))
5fb379ee
SP
2159 goto err;
2160
10ef9ab4
SP
2161 /* Use the default EQ for MCC completions */
2162 if (be_cmd_cq_create(adapter, cq, &mcc_eqo(adapter)->q, true, 0))
5fb379ee
SP
2163 goto mcc_cq_free;
2164
8788fdc2 2165 q = &adapter->mcc_obj.q;
5fb379ee
SP
2166 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
2167 goto mcc_cq_destroy;
2168
8788fdc2 2169 if (be_cmd_mccq_create(adapter, q, cq))
5fb379ee
SP
2170 goto mcc_q_free;
2171
2172 return 0;
2173
2174mcc_q_free:
2175 be_queue_free(adapter, q);
2176mcc_cq_destroy:
8788fdc2 2177 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
5fb379ee
SP
2178mcc_cq_free:
2179 be_queue_free(adapter, cq);
2180err:
2181 return -1;
2182}
2183
6b7c5b94
SP
2184static void be_tx_queues_destroy(struct be_adapter *adapter)
2185{
2186 struct be_queue_info *q;
3c8def97
SP
2187 struct be_tx_obj *txo;
2188 u8 i;
6b7c5b94 2189
3c8def97
SP
2190 for_all_tx_queues(adapter, txo, i) {
2191 q = &txo->q;
2192 if (q->created)
2193 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
2194 be_queue_free(adapter, q);
6b7c5b94 2195
3c8def97
SP
2196 q = &txo->cq;
2197 if (q->created)
2198 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2199 be_queue_free(adapter, q);
2200 }
6b7c5b94
SP
2201}
2202
7707133c 2203static int be_tx_qs_create(struct be_adapter *adapter)
6b7c5b94 2204{
10ef9ab4 2205 struct be_queue_info *cq, *eq;
3c8def97 2206 struct be_tx_obj *txo;
92bf14ab 2207 int status, i;
6b7c5b94 2208
92bf14ab 2209 adapter->num_tx_qs = min(adapter->num_evt_qs, be_max_txqs(adapter));
dafc0fe3 2210
10ef9ab4
SP
2211 for_all_tx_queues(adapter, txo, i) {
2212 cq = &txo->cq;
2213 status = be_queue_alloc(adapter, cq, TX_CQ_LEN,
2214 sizeof(struct be_eth_tx_compl));
2215 if (status)
2216 return status;
3c8def97 2217
827da44c
JS
2218 u64_stats_init(&txo->stats.sync);
2219 u64_stats_init(&txo->stats.sync_compl);
2220
10ef9ab4
SP
2221 /* If num_evt_qs is less than num_tx_qs, then more than
2222 * one txq share an eq
2223 */
2224 eq = &adapter->eq_obj[i % adapter->num_evt_qs].q;
2225 status = be_cmd_cq_create(adapter, cq, eq, false, 3);
2226 if (status)
2227 return status;
6b7c5b94 2228
10ef9ab4
SP
2229 status = be_queue_alloc(adapter, &txo->q, TX_Q_LEN,
2230 sizeof(struct be_eth_wrb));
2231 if (status)
2232 return status;
6b7c5b94 2233
94d73aaa 2234 status = be_cmd_txq_create(adapter, txo);
10ef9ab4
SP
2235 if (status)
2236 return status;
3c8def97 2237 }
6b7c5b94 2238
d379142b
SP
2239 dev_info(&adapter->pdev->dev, "created %d TX queue(s)\n",
2240 adapter->num_tx_qs);
10ef9ab4 2241 return 0;
6b7c5b94
SP
2242}
2243
10ef9ab4 2244static void be_rx_cqs_destroy(struct be_adapter *adapter)
6b7c5b94
SP
2245{
2246 struct be_queue_info *q;
3abcdeda
SP
2247 struct be_rx_obj *rxo;
2248 int i;
2249
2250 for_all_rx_queues(adapter, rxo, i) {
3abcdeda
SP
2251 q = &rxo->cq;
2252 if (q->created)
2253 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2254 be_queue_free(adapter, q);
ac6a0c4a
SP
2255 }
2256}
2257
10ef9ab4 2258static int be_rx_cqs_create(struct be_adapter *adapter)
6b7c5b94 2259{
10ef9ab4 2260 struct be_queue_info *eq, *cq;
3abcdeda
SP
2261 struct be_rx_obj *rxo;
2262 int rc, i;
6b7c5b94 2263
92bf14ab
SP
2264 /* We can create as many RSS rings as there are EQs. */
2265 adapter->num_rx_qs = adapter->num_evt_qs;
2266
2267 /* We'll use RSS only if atleast 2 RSS rings are supported.
2268 * When RSS is used, we'll need a default RXQ for non-IP traffic.
10ef9ab4 2269 */
92bf14ab
SP
2270 if (adapter->num_rx_qs > 1)
2271 adapter->num_rx_qs++;
2272
6b7c5b94 2273 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
3abcdeda
SP
2274 for_all_rx_queues(adapter, rxo, i) {
2275 rxo->adapter = adapter;
3abcdeda
SP
2276 cq = &rxo->cq;
2277 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
2278 sizeof(struct be_eth_rx_compl));
2279 if (rc)
10ef9ab4 2280 return rc;
3abcdeda 2281
827da44c 2282 u64_stats_init(&rxo->stats.sync);
10ef9ab4
SP
2283 eq = &adapter->eq_obj[i % adapter->num_evt_qs].q;
2284 rc = be_cmd_cq_create(adapter, cq, eq, false, 3);
3abcdeda 2285 if (rc)
10ef9ab4 2286 return rc;
3abcdeda 2287 }
6b7c5b94 2288
d379142b
SP
2289 dev_info(&adapter->pdev->dev,
2290 "created %d RSS queue(s) and 1 default RX queue\n",
2291 adapter->num_rx_qs - 1);
10ef9ab4 2292 return 0;
b628bde2
SP
2293}
2294
6b7c5b94
SP
2295static irqreturn_t be_intx(int irq, void *dev)
2296{
e49cc34f
SP
2297 struct be_eq_obj *eqo = dev;
2298 struct be_adapter *adapter = eqo->adapter;
2299 int num_evts = 0;
6b7c5b94 2300
d0b9cec3
SP
2301 /* IRQ is not expected when NAPI is scheduled as the EQ
2302 * will not be armed.
2303 * But, this can happen on Lancer INTx where it takes
2304 * a while to de-assert INTx or in BE2 where occasionaly
2305 * an interrupt may be raised even when EQ is unarmed.
2306 * If NAPI is already scheduled, then counting & notifying
2307 * events will orphan them.
e49cc34f 2308 */
d0b9cec3 2309 if (napi_schedule_prep(&eqo->napi)) {
e49cc34f 2310 num_evts = events_get(eqo);
d0b9cec3
SP
2311 __napi_schedule(&eqo->napi);
2312 if (num_evts)
2313 eqo->spurious_intr = 0;
2314 }
2315 be_eq_notify(adapter, eqo->q.id, false, true, num_evts);
e49cc34f 2316
d0b9cec3
SP
2317 /* Return IRQ_HANDLED only for the the first spurious intr
2318 * after a valid intr to stop the kernel from branding
2319 * this irq as a bad one!
e49cc34f 2320 */
d0b9cec3
SP
2321 if (num_evts || eqo->spurious_intr++ == 0)
2322 return IRQ_HANDLED;
2323 else
2324 return IRQ_NONE;
6b7c5b94
SP
2325}
2326
10ef9ab4 2327static irqreturn_t be_msix(int irq, void *dev)
6b7c5b94 2328{
10ef9ab4 2329 struct be_eq_obj *eqo = dev;
6b7c5b94 2330
0b545a62
SP
2331 be_eq_notify(eqo->adapter, eqo->q.id, false, true, 0);
2332 napi_schedule(&eqo->napi);
6b7c5b94
SP
2333 return IRQ_HANDLED;
2334}
2335
2e588f84 2336static inline bool do_gro(struct be_rx_compl_info *rxcp)
6b7c5b94 2337{
e38b1706 2338 return (rxcp->tcpf && !rxcp->err && rxcp->l4_csum) ? true : false;
6b7c5b94
SP
2339}
2340
10ef9ab4 2341static int be_process_rx(struct be_rx_obj *rxo, struct napi_struct *napi,
6384a4d0 2342 int budget, int polling)
6b7c5b94 2343{
3abcdeda
SP
2344 struct be_adapter *adapter = rxo->adapter;
2345 struct be_queue_info *rx_cq = &rxo->cq;
2e588f84 2346 struct be_rx_compl_info *rxcp;
6b7c5b94
SP
2347 u32 work_done;
2348
2349 for (work_done = 0; work_done < budget; work_done++) {
3abcdeda 2350 rxcp = be_rx_compl_get(rxo);
6b7c5b94
SP
2351 if (!rxcp)
2352 break;
2353
12004ae9
SP
2354 /* Is it a flush compl that has no data */
2355 if (unlikely(rxcp->num_rcvd == 0))
2356 goto loop_continue;
2357
2358 /* Discard compl with partial DMA Lancer B0 */
2359 if (unlikely(!rxcp->pkt_size)) {
10ef9ab4 2360 be_rx_compl_discard(rxo, rxcp);
12004ae9
SP
2361 goto loop_continue;
2362 }
2363
2364 /* On BE drop pkts that arrive due to imperfect filtering in
2365 * promiscuous mode on some skews
2366 */
2367 if (unlikely(rxcp->port != adapter->port_num &&
2368 !lancer_chip(adapter))) {
10ef9ab4 2369 be_rx_compl_discard(rxo, rxcp);
12004ae9 2370 goto loop_continue;
64642811 2371 }
009dd872 2372
6384a4d0
SP
2373 /* Don't do gro when we're busy_polling */
2374 if (do_gro(rxcp) && polling != BUSY_POLLING)
10ef9ab4 2375 be_rx_compl_process_gro(rxo, napi, rxcp);
12004ae9 2376 else
6384a4d0
SP
2377 be_rx_compl_process(rxo, napi, rxcp);
2378
12004ae9 2379loop_continue:
2e588f84 2380 be_rx_stats_update(rxo, rxcp);
6b7c5b94
SP
2381 }
2382
10ef9ab4
SP
2383 if (work_done) {
2384 be_cq_notify(adapter, rx_cq->id, true, work_done);
9372cacb 2385
6384a4d0
SP
2386 /* When an rx-obj gets into post_starved state, just
2387 * let be_worker do the posting.
2388 */
2389 if (atomic_read(&rxo->q.used) < RX_FRAGS_REFILL_WM &&
2390 !rxo->rx_post_starved)
10ef9ab4 2391 be_post_rx_frags(rxo, GFP_ATOMIC);
6b7c5b94 2392 }
10ef9ab4 2393
6b7c5b94
SP
2394 return work_done;
2395}
2396
10ef9ab4
SP
2397static bool be_process_tx(struct be_adapter *adapter, struct be_tx_obj *txo,
2398 int budget, int idx)
6b7c5b94 2399{
6b7c5b94 2400 struct be_eth_tx_compl *txcp;
10ef9ab4 2401 int num_wrbs = 0, work_done;
3c8def97 2402
10ef9ab4
SP
2403 for (work_done = 0; work_done < budget; work_done++) {
2404 txcp = be_tx_compl_get(&txo->cq);
2405 if (!txcp)
2406 break;
2407 num_wrbs += be_tx_compl_process(adapter, txo,
3c8def97
SP
2408 AMAP_GET_BITS(struct amap_eth_tx_compl,
2409 wrb_index, txcp));
10ef9ab4 2410 }
6b7c5b94 2411
10ef9ab4
SP
2412 if (work_done) {
2413 be_cq_notify(adapter, txo->cq.id, true, work_done);
2414 atomic_sub(num_wrbs, &txo->q.used);
3c8def97 2415
10ef9ab4
SP
2416 /* As Tx wrbs have been freed up, wake up netdev queue
2417 * if it was stopped due to lack of tx wrbs. */
2418 if (__netif_subqueue_stopped(adapter->netdev, idx) &&
2419 atomic_read(&txo->q.used) < txo->q.len / 2) {
2420 netif_wake_subqueue(adapter->netdev, idx);
3c8def97 2421 }
10ef9ab4
SP
2422
2423 u64_stats_update_begin(&tx_stats(txo)->sync_compl);
2424 tx_stats(txo)->tx_compl += work_done;
2425 u64_stats_update_end(&tx_stats(txo)->sync_compl);
6b7c5b94 2426 }
10ef9ab4
SP
2427 return (work_done < budget); /* Done */
2428}
6b7c5b94 2429
68d7bdcb 2430int be_poll(struct napi_struct *napi, int budget)
10ef9ab4
SP
2431{
2432 struct be_eq_obj *eqo = container_of(napi, struct be_eq_obj, napi);
2433 struct be_adapter *adapter = eqo->adapter;
0b545a62 2434 int max_work = 0, work, i, num_evts;
6384a4d0 2435 struct be_rx_obj *rxo;
10ef9ab4 2436 bool tx_done;
f31e50a8 2437
0b545a62
SP
2438 num_evts = events_get(eqo);
2439
10ef9ab4
SP
2440 /* Process all TXQs serviced by this EQ */
2441 for (i = eqo->idx; i < adapter->num_tx_qs; i += adapter->num_evt_qs) {
2442 tx_done = be_process_tx(adapter, &adapter->tx_obj[i],
2443 eqo->tx_budget, i);
2444 if (!tx_done)
2445 max_work = budget;
f31e50a8
SP
2446 }
2447
6384a4d0
SP
2448 if (be_lock_napi(eqo)) {
2449 /* This loop will iterate twice for EQ0 in which
2450 * completions of the last RXQ (default one) are also processed
2451 * For other EQs the loop iterates only once
2452 */
2453 for_all_rx_queues_on_eq(adapter, eqo, rxo, i) {
2454 work = be_process_rx(rxo, napi, budget, NAPI_POLLING);
2455 max_work = max(work, max_work);
2456 }
2457 be_unlock_napi(eqo);
2458 } else {
2459 max_work = budget;
10ef9ab4 2460 }
6b7c5b94 2461
10ef9ab4
SP
2462 if (is_mcc_eqo(eqo))
2463 be_process_mcc(adapter);
93c86700 2464
10ef9ab4
SP
2465 if (max_work < budget) {
2466 napi_complete(napi);
0b545a62 2467 be_eq_notify(adapter, eqo->q.id, true, false, num_evts);
10ef9ab4
SP
2468 } else {
2469 /* As we'll continue in polling mode, count and clear events */
0b545a62 2470 be_eq_notify(adapter, eqo->q.id, false, false, num_evts);
93c86700 2471 }
10ef9ab4 2472 return max_work;
6b7c5b94
SP
2473}
2474
6384a4d0
SP
2475#ifdef CONFIG_NET_RX_BUSY_POLL
2476static int be_busy_poll(struct napi_struct *napi)
2477{
2478 struct be_eq_obj *eqo = container_of(napi, struct be_eq_obj, napi);
2479 struct be_adapter *adapter = eqo->adapter;
2480 struct be_rx_obj *rxo;
2481 int i, work = 0;
2482
2483 if (!be_lock_busy_poll(eqo))
2484 return LL_FLUSH_BUSY;
2485
2486 for_all_rx_queues_on_eq(adapter, eqo, rxo, i) {
2487 work = be_process_rx(rxo, napi, 4, BUSY_POLLING);
2488 if (work)
2489 break;
2490 }
2491
2492 be_unlock_busy_poll(eqo);
2493 return work;
2494}
2495#endif
2496
f67ef7ba 2497void be_detect_error(struct be_adapter *adapter)
7c185276 2498{
e1cfb67a
PR
2499 u32 ue_lo = 0, ue_hi = 0, ue_lo_mask = 0, ue_hi_mask = 0;
2500 u32 sliport_status = 0, sliport_err1 = 0, sliport_err2 = 0;
7c185276 2501 u32 i;
eb0eecc1
SK
2502 bool error_detected = false;
2503 struct device *dev = &adapter->pdev->dev;
2504 struct net_device *netdev = adapter->netdev;
7c185276 2505
d23e946c 2506 if (be_hw_error(adapter))
72f02485
SP
2507 return;
2508
e1cfb67a
PR
2509 if (lancer_chip(adapter)) {
2510 sliport_status = ioread32(adapter->db + SLIPORT_STATUS_OFFSET);
2511 if (sliport_status & SLIPORT_STATUS_ERR_MASK) {
2512 sliport_err1 = ioread32(adapter->db +
2513 SLIPORT_ERROR1_OFFSET);
2514 sliport_err2 = ioread32(adapter->db +
2515 SLIPORT_ERROR2_OFFSET);
eb0eecc1
SK
2516 adapter->hw_error = true;
2517 /* Do not log error messages if its a FW reset */
2518 if (sliport_err1 == SLIPORT_ERROR_FW_RESET1 &&
2519 sliport_err2 == SLIPORT_ERROR_FW_RESET2) {
2520 dev_info(dev, "Firmware update in progress\n");
2521 } else {
2522 error_detected = true;
2523 dev_err(dev, "Error detected in the card\n");
2524 dev_err(dev, "ERR: sliport status 0x%x\n",
2525 sliport_status);
2526 dev_err(dev, "ERR: sliport error1 0x%x\n",
2527 sliport_err1);
2528 dev_err(dev, "ERR: sliport error2 0x%x\n",
2529 sliport_err2);
2530 }
e1cfb67a
PR
2531 }
2532 } else {
2533 pci_read_config_dword(adapter->pdev,
2534 PCICFG_UE_STATUS_LOW, &ue_lo);
2535 pci_read_config_dword(adapter->pdev,
2536 PCICFG_UE_STATUS_HIGH, &ue_hi);
2537 pci_read_config_dword(adapter->pdev,
2538 PCICFG_UE_STATUS_LOW_MASK, &ue_lo_mask);
2539 pci_read_config_dword(adapter->pdev,
2540 PCICFG_UE_STATUS_HI_MASK, &ue_hi_mask);
2541
f67ef7ba
PR
2542 ue_lo = (ue_lo & ~ue_lo_mask);
2543 ue_hi = (ue_hi & ~ue_hi_mask);
7c185276 2544
eb0eecc1
SK
2545 /* On certain platforms BE hardware can indicate spurious UEs.
2546 * Allow HW to stop working completely in case of a real UE.
2547 * Hence not setting the hw_error for UE detection.
2548 */
f67ef7ba 2549
eb0eecc1
SK
2550 if (ue_lo || ue_hi) {
2551 error_detected = true;
2552 dev_err(dev,
2553 "Unrecoverable Error detected in the adapter");
2554 dev_err(dev, "Please reboot server to recover");
2555 if (skyhawk_chip(adapter))
2556 adapter->hw_error = true;
2557 for (i = 0; ue_lo; ue_lo >>= 1, i++) {
2558 if (ue_lo & 1)
2559 dev_err(dev, "UE: %s bit set\n",
2560 ue_status_low_desc[i]);
2561 }
2562 for (i = 0; ue_hi; ue_hi >>= 1, i++) {
2563 if (ue_hi & 1)
2564 dev_err(dev, "UE: %s bit set\n",
2565 ue_status_hi_desc[i]);
2566 }
7c185276
AK
2567 }
2568 }
eb0eecc1
SK
2569 if (error_detected)
2570 netif_carrier_off(netdev);
7c185276
AK
2571}
2572
8d56ff11
SP
2573static void be_msix_disable(struct be_adapter *adapter)
2574{
ac6a0c4a 2575 if (msix_enabled(adapter)) {
8d56ff11 2576 pci_disable_msix(adapter->pdev);
ac6a0c4a 2577 adapter->num_msix_vec = 0;
68d7bdcb 2578 adapter->num_msix_roce_vec = 0;
3abcdeda
SP
2579 }
2580}
2581
c2bba3df 2582static int be_msix_enable(struct be_adapter *adapter)
6b7c5b94 2583{
7dc4c064 2584 int i, num_vec;
d379142b 2585 struct device *dev = &adapter->pdev->dev;
6b7c5b94 2586
92bf14ab
SP
2587 /* If RoCE is supported, program the max number of NIC vectors that
2588 * may be configured via set-channels, along with vectors needed for
2589 * RoCe. Else, just program the number we'll use initially.
2590 */
2591 if (be_roce_supported(adapter))
2592 num_vec = min_t(int, 2 * be_max_eqs(adapter),
2593 2 * num_online_cpus());
2594 else
2595 num_vec = adapter->cfg_num_qs;
3abcdeda 2596
ac6a0c4a 2597 for (i = 0; i < num_vec; i++)
6b7c5b94
SP
2598 adapter->msix_entries[i].entry = i;
2599
7dc4c064
AG
2600 num_vec = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
2601 MIN_MSIX_VECTORS, num_vec);
2602 if (num_vec < 0)
2603 goto fail;
92bf14ab 2604
92bf14ab
SP
2605 if (be_roce_supported(adapter) && num_vec > MIN_MSIX_VECTORS) {
2606 adapter->num_msix_roce_vec = num_vec / 2;
2607 dev_info(dev, "enabled %d MSI-x vector(s) for RoCE\n",
2608 adapter->num_msix_roce_vec);
2609 }
2610
2611 adapter->num_msix_vec = num_vec - adapter->num_msix_roce_vec;
2612
2613 dev_info(dev, "enabled %d MSI-x vector(s) for NIC\n",
2614 adapter->num_msix_vec);
c2bba3df 2615 return 0;
7dc4c064
AG
2616
2617fail:
2618 dev_warn(dev, "MSIx enable failed\n");
2619
2620 /* INTx is not supported in VFs, so fail probe if enable_msix fails */
2621 if (!be_physfn(adapter))
2622 return num_vec;
2623 return 0;
6b7c5b94
SP
2624}
2625
fe6d2a38 2626static inline int be_msix_vec_get(struct be_adapter *adapter,
10ef9ab4 2627 struct be_eq_obj *eqo)
b628bde2 2628{
f2f781a7 2629 return adapter->msix_entries[eqo->msix_idx].vector;
b628bde2 2630}
6b7c5b94 2631
b628bde2
SP
2632static int be_msix_register(struct be_adapter *adapter)
2633{
10ef9ab4
SP
2634 struct net_device *netdev = adapter->netdev;
2635 struct be_eq_obj *eqo;
2636 int status, i, vec;
6b7c5b94 2637
10ef9ab4
SP
2638 for_all_evt_queues(adapter, eqo, i) {
2639 sprintf(eqo->desc, "%s-q%d", netdev->name, i);
2640 vec = be_msix_vec_get(adapter, eqo);
2641 status = request_irq(vec, be_msix, 0, eqo->desc, eqo);
3abcdeda
SP
2642 if (status)
2643 goto err_msix;
2644 }
b628bde2 2645
6b7c5b94 2646 return 0;
3abcdeda 2647err_msix:
10ef9ab4
SP
2648 for (i--, eqo = &adapter->eq_obj[i]; i >= 0; i--, eqo--)
2649 free_irq(be_msix_vec_get(adapter, eqo), eqo);
2650 dev_warn(&adapter->pdev->dev, "MSIX Request IRQ failed - err %d\n",
2651 status);
ac6a0c4a 2652 be_msix_disable(adapter);
6b7c5b94
SP
2653 return status;
2654}
2655
2656static int be_irq_register(struct be_adapter *adapter)
2657{
2658 struct net_device *netdev = adapter->netdev;
2659 int status;
2660
ac6a0c4a 2661 if (msix_enabled(adapter)) {
6b7c5b94
SP
2662 status = be_msix_register(adapter);
2663 if (status == 0)
2664 goto done;
ba343c77
SB
2665 /* INTx is not supported for VF */
2666 if (!be_physfn(adapter))
2667 return status;
6b7c5b94
SP
2668 }
2669
e49cc34f 2670 /* INTx: only the first EQ is used */
6b7c5b94
SP
2671 netdev->irq = adapter->pdev->irq;
2672 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
e49cc34f 2673 &adapter->eq_obj[0]);
6b7c5b94
SP
2674 if (status) {
2675 dev_err(&adapter->pdev->dev,
2676 "INTx request IRQ failed - err %d\n", status);
2677 return status;
2678 }
2679done:
2680 adapter->isr_registered = true;
2681 return 0;
2682}
2683
2684static void be_irq_unregister(struct be_adapter *adapter)
2685{
2686 struct net_device *netdev = adapter->netdev;
10ef9ab4 2687 struct be_eq_obj *eqo;
3abcdeda 2688 int i;
6b7c5b94
SP
2689
2690 if (!adapter->isr_registered)
2691 return;
2692
2693 /* INTx */
ac6a0c4a 2694 if (!msix_enabled(adapter)) {
e49cc34f 2695 free_irq(netdev->irq, &adapter->eq_obj[0]);
6b7c5b94
SP
2696 goto done;
2697 }
2698
2699 /* MSIx */
10ef9ab4
SP
2700 for_all_evt_queues(adapter, eqo, i)
2701 free_irq(be_msix_vec_get(adapter, eqo), eqo);
3abcdeda 2702
6b7c5b94
SP
2703done:
2704 adapter->isr_registered = false;
6b7c5b94
SP
2705}
2706
10ef9ab4 2707static void be_rx_qs_destroy(struct be_adapter *adapter)
482c9e79
SP
2708{
2709 struct be_queue_info *q;
2710 struct be_rx_obj *rxo;
2711 int i;
2712
2713 for_all_rx_queues(adapter, rxo, i) {
2714 q = &rxo->q;
2715 if (q->created) {
2716 be_cmd_rxq_destroy(adapter, q);
10ef9ab4 2717 be_rx_cq_clean(rxo);
482c9e79 2718 }
10ef9ab4 2719 be_queue_free(adapter, q);
482c9e79
SP
2720 }
2721}
2722
889cd4b2
SP
2723static int be_close(struct net_device *netdev)
2724{
2725 struct be_adapter *adapter = netdev_priv(netdev);
10ef9ab4
SP
2726 struct be_eq_obj *eqo;
2727 int i;
889cd4b2 2728
045508a8
PP
2729 be_roce_dev_close(adapter);
2730
dff345c5
IV
2731 if (adapter->flags & BE_FLAGS_NAPI_ENABLED) {
2732 for_all_evt_queues(adapter, eqo, i) {
04d3d624 2733 napi_disable(&eqo->napi);
6384a4d0
SP
2734 be_disable_busy_poll(eqo);
2735 }
71237b6f 2736 adapter->flags &= ~BE_FLAGS_NAPI_ENABLED;
04d3d624 2737 }
a323d9bf
SP
2738
2739 be_async_mcc_disable(adapter);
2740
2741 /* Wait for all pending tx completions to arrive so that
2742 * all tx skbs are freed.
2743 */
fba87559 2744 netif_tx_disable(netdev);
6e1f9975 2745 be_tx_compl_clean(adapter);
a323d9bf
SP
2746
2747 be_rx_qs_destroy(adapter);
2748
d11a347d
AK
2749 for (i = 1; i < (adapter->uc_macs + 1); i++)
2750 be_cmd_pmac_del(adapter, adapter->if_handle,
2751 adapter->pmac_id[i], 0);
2752 adapter->uc_macs = 0;
2753
a323d9bf 2754 for_all_evt_queues(adapter, eqo, i) {
10ef9ab4
SP
2755 if (msix_enabled(adapter))
2756 synchronize_irq(be_msix_vec_get(adapter, eqo));
2757 else
2758 synchronize_irq(netdev->irq);
2759 be_eq_clean(eqo);
63fcb27f
PR
2760 }
2761
889cd4b2
SP
2762 be_irq_unregister(adapter);
2763
482c9e79
SP
2764 return 0;
2765}
2766
10ef9ab4 2767static int be_rx_qs_create(struct be_adapter *adapter)
482c9e79
SP
2768{
2769 struct be_rx_obj *rxo;
e9008ee9
PR
2770 int rc, i, j;
2771 u8 rsstable[128];
482c9e79
SP
2772
2773 for_all_rx_queues(adapter, rxo, i) {
10ef9ab4
SP
2774 rc = be_queue_alloc(adapter, &rxo->q, RX_Q_LEN,
2775 sizeof(struct be_eth_rx_d));
2776 if (rc)
2777 return rc;
2778 }
2779
2780 /* The FW would like the default RXQ to be created first */
2781 rxo = default_rxo(adapter);
2782 rc = be_cmd_rxq_create(adapter, &rxo->q, rxo->cq.id, rx_frag_size,
2783 adapter->if_handle, false, &rxo->rss_id);
2784 if (rc)
2785 return rc;
2786
2787 for_all_rss_queues(adapter, rxo, i) {
482c9e79 2788 rc = be_cmd_rxq_create(adapter, &rxo->q, rxo->cq.id,
10ef9ab4
SP
2789 rx_frag_size, adapter->if_handle,
2790 true, &rxo->rss_id);
482c9e79
SP
2791 if (rc)
2792 return rc;
2793 }
2794
2795 if (be_multi_rxq(adapter)) {
e9008ee9
PR
2796 for (j = 0; j < 128; j += adapter->num_rx_qs - 1) {
2797 for_all_rss_queues(adapter, rxo, i) {
2798 if ((j + i) >= 128)
2799 break;
2800 rsstable[j + i] = rxo->rss_id;
2801 }
2802 }
594ad54a
SR
2803 adapter->rss_flags = RSS_ENABLE_TCP_IPV4 | RSS_ENABLE_IPV4 |
2804 RSS_ENABLE_TCP_IPV6 | RSS_ENABLE_IPV6;
2805
2806 if (!BEx_chip(adapter))
2807 adapter->rss_flags |= RSS_ENABLE_UDP_IPV4 |
2808 RSS_ENABLE_UDP_IPV6;
da1388d6
VV
2809 } else {
2810 /* Disable RSS, if only default RX Q is created */
2811 adapter->rss_flags = RSS_ENABLE_NONE;
2812 }
594ad54a 2813
da1388d6
VV
2814 rc = be_cmd_rss_config(adapter, rsstable, adapter->rss_flags,
2815 128);
2816 if (rc) {
2817 adapter->rss_flags = RSS_ENABLE_NONE;
2818 return rc;
482c9e79
SP
2819 }
2820
2821 /* First time posting */
10ef9ab4 2822 for_all_rx_queues(adapter, rxo, i)
482c9e79 2823 be_post_rx_frags(rxo, GFP_KERNEL);
889cd4b2
SP
2824 return 0;
2825}
2826
6b7c5b94
SP
2827static int be_open(struct net_device *netdev)
2828{
2829 struct be_adapter *adapter = netdev_priv(netdev);
10ef9ab4 2830 struct be_eq_obj *eqo;
3abcdeda 2831 struct be_rx_obj *rxo;
10ef9ab4 2832 struct be_tx_obj *txo;
b236916a 2833 u8 link_status;
3abcdeda 2834 int status, i;
5fb379ee 2835
10ef9ab4 2836 status = be_rx_qs_create(adapter);
482c9e79
SP
2837 if (status)
2838 goto err;
2839
c2bba3df
SK
2840 status = be_irq_register(adapter);
2841 if (status)
2842 goto err;
5fb379ee 2843
10ef9ab4 2844 for_all_rx_queues(adapter, rxo, i)
3abcdeda 2845 be_cq_notify(adapter, rxo->cq.id, true, 0);
5fb379ee 2846
10ef9ab4
SP
2847 for_all_tx_queues(adapter, txo, i)
2848 be_cq_notify(adapter, txo->cq.id, true, 0);
2849
7a1e9b20
SP
2850 be_async_mcc_enable(adapter);
2851
10ef9ab4
SP
2852 for_all_evt_queues(adapter, eqo, i) {
2853 napi_enable(&eqo->napi);
6384a4d0 2854 be_enable_busy_poll(eqo);
10ef9ab4
SP
2855 be_eq_notify(adapter, eqo->q.id, true, false, 0);
2856 }
04d3d624 2857 adapter->flags |= BE_FLAGS_NAPI_ENABLED;
10ef9ab4 2858
323ff71e 2859 status = be_cmd_link_status_query(adapter, NULL, &link_status, 0);
b236916a
AK
2860 if (!status)
2861 be_link_status_update(adapter, link_status);
2862
fba87559 2863 netif_tx_start_all_queues(netdev);
045508a8 2864 be_roce_dev_open(adapter);
c9c47142 2865
c5abe7c0 2866#ifdef CONFIG_BE2NET_VXLAN
c9c47142
SP
2867 if (skyhawk_chip(adapter))
2868 vxlan_get_rx_port(netdev);
c5abe7c0
SP
2869#endif
2870
889cd4b2
SP
2871 return 0;
2872err:
2873 be_close(adapter->netdev);
2874 return -EIO;
5fb379ee
SP
2875}
2876
71d8d1b5
AK
2877static int be_setup_wol(struct be_adapter *adapter, bool enable)
2878{
2879 struct be_dma_mem cmd;
2880 int status = 0;
2881 u8 mac[ETH_ALEN];
2882
2883 memset(mac, 0, ETH_ALEN);
2884
2885 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
ede23fa8
JP
2886 cmd.va = dma_zalloc_coherent(&adapter->pdev->dev, cmd.size, &cmd.dma,
2887 GFP_KERNEL);
71d8d1b5
AK
2888 if (cmd.va == NULL)
2889 return -1;
71d8d1b5
AK
2890
2891 if (enable) {
2892 status = pci_write_config_dword(adapter->pdev,
2893 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
2894 if (status) {
2895 dev_err(&adapter->pdev->dev,
2381a55c 2896 "Could not enable Wake-on-lan\n");
2b7bcebf
IV
2897 dma_free_coherent(&adapter->pdev->dev, cmd.size, cmd.va,
2898 cmd.dma);
71d8d1b5
AK
2899 return status;
2900 }
2901 status = be_cmd_enable_magic_wol(adapter,
2902 adapter->netdev->dev_addr, &cmd);
2903 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
2904 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
2905 } else {
2906 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
2907 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
2908 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
2909 }
2910
2b7bcebf 2911 dma_free_coherent(&adapter->pdev->dev, cmd.size, cmd.va, cmd.dma);
71d8d1b5
AK
2912 return status;
2913}
2914
6d87f5c3
AK
2915/*
2916 * Generate a seed MAC address from the PF MAC Address using jhash.
2917 * MAC Address for VFs are assigned incrementally starting from the seed.
2918 * These addresses are programmed in the ASIC by the PF and the VF driver
2919 * queries for the MAC address during its probe.
2920 */
4c876616 2921static int be_vf_eth_addr_config(struct be_adapter *adapter)
6d87f5c3 2922{
f9449ab7 2923 u32 vf;
3abcdeda 2924 int status = 0;
6d87f5c3 2925 u8 mac[ETH_ALEN];
11ac75ed 2926 struct be_vf_cfg *vf_cfg;
6d87f5c3
AK
2927
2928 be_vf_eth_addr_generate(adapter, mac);
2929
11ac75ed 2930 for_all_vfs(adapter, vf_cfg, vf) {
3175d8c2 2931 if (BEx_chip(adapter))
590c391d 2932 status = be_cmd_pmac_add(adapter, mac,
11ac75ed
SP
2933 vf_cfg->if_handle,
2934 &vf_cfg->pmac_id, vf + 1);
3175d8c2
SP
2935 else
2936 status = be_cmd_set_mac(adapter, mac, vf_cfg->if_handle,
2937 vf + 1);
590c391d 2938
6d87f5c3
AK
2939 if (status)
2940 dev_err(&adapter->pdev->dev,
590c391d 2941 "Mac address assignment failed for VF %d\n", vf);
6d87f5c3 2942 else
11ac75ed 2943 memcpy(vf_cfg->mac_addr, mac, ETH_ALEN);
6d87f5c3
AK
2944
2945 mac[5] += 1;
2946 }
2947 return status;
2948}
2949
4c876616
SP
2950static int be_vfs_mac_query(struct be_adapter *adapter)
2951{
2952 int status, vf;
2953 u8 mac[ETH_ALEN];
2954 struct be_vf_cfg *vf_cfg;
4c876616
SP
2955
2956 for_all_vfs(adapter, vf_cfg, vf) {
b188f090
SR
2957 status = be_cmd_get_active_mac(adapter, vf_cfg->pmac_id,
2958 mac, vf_cfg->if_handle,
2959 false, vf+1);
4c876616
SP
2960 if (status)
2961 return status;
2962 memcpy(vf_cfg->mac_addr, mac, ETH_ALEN);
2963 }
2964 return 0;
2965}
2966
f9449ab7 2967static void be_vf_clear(struct be_adapter *adapter)
6d87f5c3 2968{
11ac75ed 2969 struct be_vf_cfg *vf_cfg;
6d87f5c3
AK
2970 u32 vf;
2971
257a3feb 2972 if (pci_vfs_assigned(adapter->pdev)) {
4c876616
SP
2973 dev_warn(&adapter->pdev->dev,
2974 "VFs are assigned to VMs: not disabling VFs\n");
39f1d94d
SP
2975 goto done;
2976 }
2977
b4c1df93
SP
2978 pci_disable_sriov(adapter->pdev);
2979
11ac75ed 2980 for_all_vfs(adapter, vf_cfg, vf) {
3175d8c2 2981 if (BEx_chip(adapter))
11ac75ed
SP
2982 be_cmd_pmac_del(adapter, vf_cfg->if_handle,
2983 vf_cfg->pmac_id, vf + 1);
3175d8c2
SP
2984 else
2985 be_cmd_set_mac(adapter, NULL, vf_cfg->if_handle,
2986 vf + 1);
f9449ab7 2987
11ac75ed
SP
2988 be_cmd_if_destroy(adapter, vf_cfg->if_handle, vf + 1);
2989 }
39f1d94d
SP
2990done:
2991 kfree(adapter->vf_cfg);
2992 adapter->num_vfs = 0;
6d87f5c3
AK
2993}
2994
7707133c
SP
2995static void be_clear_queues(struct be_adapter *adapter)
2996{
2997 be_mcc_queues_destroy(adapter);
2998 be_rx_cqs_destroy(adapter);
2999 be_tx_queues_destroy(adapter);
3000 be_evt_queues_destroy(adapter);
3001}
3002
68d7bdcb 3003static void be_cancel_worker(struct be_adapter *adapter)
a54769f5 3004{
191eb756
SP
3005 if (adapter->flags & BE_FLAGS_WORKER_SCHEDULED) {
3006 cancel_delayed_work_sync(&adapter->work);
3007 adapter->flags &= ~BE_FLAGS_WORKER_SCHEDULED;
3008 }
68d7bdcb
SP
3009}
3010
b05004ad 3011static void be_mac_clear(struct be_adapter *adapter)
68d7bdcb
SP
3012{
3013 int i;
3014
b05004ad
SK
3015 if (adapter->pmac_id) {
3016 for (i = 0; i < (adapter->uc_macs + 1); i++)
3017 be_cmd_pmac_del(adapter, adapter->if_handle,
3018 adapter->pmac_id[i], 0);
3019 adapter->uc_macs = 0;
3020
3021 kfree(adapter->pmac_id);
3022 adapter->pmac_id = NULL;
3023 }
3024}
3025
c5abe7c0 3026#ifdef CONFIG_BE2NET_VXLAN
c9c47142
SP
3027static void be_disable_vxlan_offloads(struct be_adapter *adapter)
3028{
3029 if (adapter->flags & BE_FLAGS_VXLAN_OFFLOADS)
3030 be_cmd_manage_iface(adapter, adapter->if_handle,
3031 OP_CONVERT_TUNNEL_TO_NORMAL);
3032
3033 if (adapter->vxlan_port)
3034 be_cmd_set_vxlan_port(adapter, 0);
3035
3036 adapter->flags &= ~BE_FLAGS_VXLAN_OFFLOADS;
3037 adapter->vxlan_port = 0;
3038}
c5abe7c0 3039#endif
c9c47142 3040
b05004ad
SK
3041static int be_clear(struct be_adapter *adapter)
3042{
68d7bdcb 3043 be_cancel_worker(adapter);
191eb756 3044
11ac75ed 3045 if (sriov_enabled(adapter))
f9449ab7
SP
3046 be_vf_clear(adapter);
3047
c5abe7c0 3048#ifdef CONFIG_BE2NET_VXLAN
c9c47142 3049 be_disable_vxlan_offloads(adapter);
c5abe7c0 3050#endif
2d17f403 3051 /* delete the primary mac along with the uc-mac list */
b05004ad 3052 be_mac_clear(adapter);
fbc13f01 3053
f9449ab7 3054 be_cmd_if_destroy(adapter, adapter->if_handle, 0);
a54769f5 3055
7707133c 3056 be_clear_queues(adapter);
a54769f5 3057
10ef9ab4 3058 be_msix_disable(adapter);
a54769f5
SP
3059 return 0;
3060}
3061
4c876616 3062static int be_vfs_if_create(struct be_adapter *adapter)
abb93951 3063{
92bf14ab 3064 struct be_resources res = {0};
4c876616
SP
3065 struct be_vf_cfg *vf_cfg;
3066 u32 cap_flags, en_flags, vf;
922bbe88 3067 int status = 0;
abb93951 3068
4c876616
SP
3069 cap_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
3070 BE_IF_FLAGS_MULTICAST;
abb93951 3071
4c876616 3072 for_all_vfs(adapter, vf_cfg, vf) {
92bf14ab
SP
3073 if (!BE3_chip(adapter)) {
3074 status = be_cmd_get_profile_config(adapter, &res,
3075 vf + 1);
3076 if (!status)
3077 cap_flags = res.if_cap_flags;
3078 }
4c876616
SP
3079
3080 /* If a FW profile exists, then cap_flags are updated */
3081 en_flags = cap_flags & (BE_IF_FLAGS_UNTAGGED |
3082 BE_IF_FLAGS_BROADCAST | BE_IF_FLAGS_MULTICAST);
3083 status = be_cmd_if_create(adapter, cap_flags, en_flags,
3084 &vf_cfg->if_handle, vf + 1);
3085 if (status)
3086 goto err;
3087 }
3088err:
3089 return status;
abb93951
PR
3090}
3091
39f1d94d 3092static int be_vf_setup_init(struct be_adapter *adapter)
30128031 3093{
11ac75ed 3094 struct be_vf_cfg *vf_cfg;
30128031
SP
3095 int vf;
3096
39f1d94d
SP
3097 adapter->vf_cfg = kcalloc(adapter->num_vfs, sizeof(*vf_cfg),
3098 GFP_KERNEL);
3099 if (!adapter->vf_cfg)
3100 return -ENOMEM;
3101
11ac75ed
SP
3102 for_all_vfs(adapter, vf_cfg, vf) {
3103 vf_cfg->if_handle = -1;
3104 vf_cfg->pmac_id = -1;
30128031 3105 }
39f1d94d 3106 return 0;
30128031
SP
3107}
3108
f9449ab7
SP
3109static int be_vf_setup(struct be_adapter *adapter)
3110{
c502224e 3111 struct device *dev = &adapter->pdev->dev;
11ac75ed 3112 struct be_vf_cfg *vf_cfg;
4c876616 3113 int status, old_vfs, vf;
04a06028 3114 u32 privileges;
c502224e 3115 u16 lnk_speed;
39f1d94d 3116
257a3feb 3117 old_vfs = pci_num_vf(adapter->pdev);
4c876616
SP
3118 if (old_vfs) {
3119 dev_info(dev, "%d VFs are already enabled\n", old_vfs);
3120 if (old_vfs != num_vfs)
3121 dev_warn(dev, "Ignoring num_vfs=%d setting\n", num_vfs);
3122 adapter->num_vfs = old_vfs;
39f1d94d 3123 } else {
92bf14ab 3124 if (num_vfs > be_max_vfs(adapter))
4c876616 3125 dev_info(dev, "Device supports %d VFs and not %d\n",
92bf14ab
SP
3126 be_max_vfs(adapter), num_vfs);
3127 adapter->num_vfs = min_t(u16, num_vfs, be_max_vfs(adapter));
b4c1df93 3128 if (!adapter->num_vfs)
4c876616 3129 return 0;
39f1d94d
SP
3130 }
3131
3132 status = be_vf_setup_init(adapter);
3133 if (status)
3134 goto err;
30128031 3135
4c876616
SP
3136 if (old_vfs) {
3137 for_all_vfs(adapter, vf_cfg, vf) {
3138 status = be_cmd_get_if_id(adapter, vf_cfg, vf);
3139 if (status)
3140 goto err;
3141 }
3142 } else {
3143 status = be_vfs_if_create(adapter);
f9449ab7
SP
3144 if (status)
3145 goto err;
f9449ab7
SP
3146 }
3147
4c876616
SP
3148 if (old_vfs) {
3149 status = be_vfs_mac_query(adapter);
3150 if (status)
3151 goto err;
3152 } else {
39f1d94d
SP
3153 status = be_vf_eth_addr_config(adapter);
3154 if (status)
3155 goto err;
3156 }
f9449ab7 3157
11ac75ed 3158 for_all_vfs(adapter, vf_cfg, vf) {
04a06028
SP
3159 /* Allow VFs to programs MAC/VLAN filters */
3160 status = be_cmd_get_fn_privileges(adapter, &privileges, vf + 1);
3161 if (!status && !(privileges & BE_PRIV_FILTMGMT)) {
3162 status = be_cmd_set_fn_privileges(adapter,
3163 privileges |
3164 BE_PRIV_FILTMGMT,
3165 vf + 1);
3166 if (!status)
3167 dev_info(dev, "VF%d has FILTMGMT privilege\n",
3168 vf);
3169 }
3170
4c876616
SP
3171 /* BE3 FW, by default, caps VF TX-rate to 100mbps.
3172 * Allow full available bandwidth
3173 */
3174 if (BE3_chip(adapter) && !old_vfs)
a401801c 3175 be_cmd_config_qos(adapter, 1000, vf + 1);
4c876616
SP
3176
3177 status = be_cmd_link_status_query(adapter, &lnk_speed,
3178 NULL, vf + 1);
3179 if (!status)
3180 vf_cfg->tx_rate = lnk_speed;
f1f3ee1b 3181
bdce2ad7 3182 if (!old_vfs) {
0599863d 3183 be_cmd_enable_vf(adapter, vf + 1);
bdce2ad7
SR
3184 be_cmd_set_logical_link_config(adapter,
3185 IFLA_VF_LINK_STATE_AUTO,
3186 vf+1);
3187 }
f9449ab7 3188 }
b4c1df93
SP
3189
3190 if (!old_vfs) {
3191 status = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
3192 if (status) {
3193 dev_err(dev, "SRIOV enable failed\n");
3194 adapter->num_vfs = 0;
3195 goto err;
3196 }
3197 }
f9449ab7
SP
3198 return 0;
3199err:
4c876616
SP
3200 dev_err(dev, "VF setup failed\n");
3201 be_vf_clear(adapter);
f9449ab7
SP
3202 return status;
3203}
3204
f93f160b
VV
3205/* Converting function_mode bits on BE3 to SH mc_type enums */
3206
3207static u8 be_convert_mc_type(u32 function_mode)
3208{
3209 if (function_mode & VNIC_MODE && function_mode & FLEX10_MODE)
3210 return vNIC1;
3211 else if (function_mode & FLEX10_MODE)
3212 return FLEX10;
3213 else if (function_mode & VNIC_MODE)
3214 return vNIC2;
3215 else if (function_mode & UMC_ENABLED)
3216 return UMC;
3217 else
3218 return MC_NONE;
3219}
3220
92bf14ab
SP
3221/* On BE2/BE3 FW does not suggest the supported limits */
3222static void BEx_get_resources(struct be_adapter *adapter,
3223 struct be_resources *res)
3224{
3225 struct pci_dev *pdev = adapter->pdev;
3226 bool use_sriov = false;
ecf1f6e1
SR
3227 int max_vfs = 0;
3228
3229 if (be_physfn(adapter) && BE3_chip(adapter)) {
3230 be_cmd_get_profile_config(adapter, res, 0);
3231 /* Some old versions of BE3 FW don't report max_vfs value */
3232 if (res->max_vfs == 0) {
3233 max_vfs = pci_sriov_get_totalvfs(pdev);
3234 res->max_vfs = max_vfs > 0 ? min(MAX_VFS, max_vfs) : 0;
3235 }
3236 use_sriov = res->max_vfs && sriov_want(adapter);
92bf14ab
SP
3237 }
3238
3239 if (be_physfn(adapter))
3240 res->max_uc_mac = BE_UC_PMAC_COUNT;
3241 else
3242 res->max_uc_mac = BE_VF_UC_PMAC_COUNT;
3243
f93f160b
VV
3244 adapter->mc_type = be_convert_mc_type(adapter->function_mode);
3245
3246 if (be_is_mc(adapter)) {
3247 /* Assuming that there are 4 channels per port,
3248 * when multi-channel is enabled
3249 */
3250 if (be_is_qnq_mode(adapter))
3251 res->max_vlans = BE_NUM_VLANS_SUPPORTED/8;
3252 else
3253 /* In a non-qnq multichannel mode, the pvid
3254 * takes up one vlan entry
3255 */
3256 res->max_vlans = (BE_NUM_VLANS_SUPPORTED / 4) - 1;
3257 } else {
92bf14ab 3258 res->max_vlans = BE_NUM_VLANS_SUPPORTED;
f93f160b
VV
3259 }
3260
92bf14ab
SP
3261 res->max_mcast_mac = BE_MAX_MC;
3262
a5243dab
VV
3263 /* 1) For BE3 1Gb ports, FW does not support multiple TXQs
3264 * 2) Create multiple TX rings on a BE3-R multi-channel interface
3265 * *only* if it is RSS-capable.
3266 */
3267 if (BE2_chip(adapter) || use_sriov || (adapter->port_num > 1) ||
3268 !be_physfn(adapter) || (be_is_mc(adapter) &&
3269 !(adapter->function_caps & BE_FUNCTION_CAPS_RSS)))
92bf14ab
SP
3270 res->max_tx_qs = 1;
3271 else
3272 res->max_tx_qs = BE3_MAX_TX_QS;
3273
3274 if ((adapter->function_caps & BE_FUNCTION_CAPS_RSS) &&
3275 !use_sriov && be_physfn(adapter))
3276 res->max_rss_qs = (adapter->be3_native) ?
3277 BE3_MAX_RSS_QS : BE2_MAX_RSS_QS;
3278 res->max_rx_qs = res->max_rss_qs + 1;
3279
e3dc867c 3280 if (be_physfn(adapter))
ecf1f6e1 3281 res->max_evt_qs = (res->max_vfs > 0) ?
e3dc867c
SR
3282 BE3_SRIOV_MAX_EVT_QS : BE3_MAX_EVT_QS;
3283 else
3284 res->max_evt_qs = 1;
92bf14ab
SP
3285
3286 res->if_cap_flags = BE_IF_CAP_FLAGS_WANT;
3287 if (!(adapter->function_caps & BE_FUNCTION_CAPS_RSS))
3288 res->if_cap_flags &= ~BE_IF_FLAGS_RSS;
3289}
3290
30128031
SP
3291static void be_setup_init(struct be_adapter *adapter)
3292{
3293 adapter->vlan_prio_bmap = 0xff;
42f11cf2 3294 adapter->phy.link_speed = -1;
30128031
SP
3295 adapter->if_handle = -1;
3296 adapter->be3_native = false;
3297 adapter->promiscuous = false;
f25b119c
PR
3298 if (be_physfn(adapter))
3299 adapter->cmd_privileges = MAX_PRIVILEGES;
3300 else
3301 adapter->cmd_privileges = MIN_PRIVILEGES;
30128031
SP
3302}
3303
92bf14ab 3304static int be_get_resources(struct be_adapter *adapter)
abb93951 3305{
92bf14ab
SP
3306 struct device *dev = &adapter->pdev->dev;
3307 struct be_resources res = {0};
3308 int status;
abb93951 3309
92bf14ab
SP
3310 if (BEx_chip(adapter)) {
3311 BEx_get_resources(adapter, &res);
3312 adapter->res = res;
abb93951
PR
3313 }
3314
92bf14ab
SP
3315 /* For Lancer, SH etc read per-function resource limits from FW.
3316 * GET_FUNC_CONFIG returns per function guaranteed limits.
3317 * GET_PROFILE_CONFIG returns PCI-E related limits PF-pool limits
3318 */
3319 if (!BEx_chip(adapter)) {
3320 status = be_cmd_get_func_config(adapter, &res);
3321 if (status)
3322 return status;
abb93951 3323
92bf14ab
SP
3324 /* If RoCE may be enabled stash away half the EQs for RoCE */
3325 if (be_roce_supported(adapter))
3326 res.max_evt_qs /= 2;
3327 adapter->res = res;
abb93951 3328
92bf14ab
SP
3329 if (be_physfn(adapter)) {
3330 status = be_cmd_get_profile_config(adapter, &res, 0);
3331 if (status)
3332 return status;
3333 adapter->res.max_vfs = res.max_vfs;
3334 }
abb93951 3335
92bf14ab
SP
3336 dev_info(dev, "Max: txqs %d, rxqs %d, rss %d, eqs %d, vfs %d\n",
3337 be_max_txqs(adapter), be_max_rxqs(adapter),
3338 be_max_rss(adapter), be_max_eqs(adapter),
3339 be_max_vfs(adapter));
3340 dev_info(dev, "Max: uc-macs %d, mc-macs %d, vlans %d\n",
3341 be_max_uc(adapter), be_max_mc(adapter),
3342 be_max_vlans(adapter));
abb93951 3343 }
4c876616 3344
92bf14ab 3345 return 0;
abb93951
PR
3346}
3347
39f1d94d
SP
3348/* Routine to query per function resource limits */
3349static int be_get_config(struct be_adapter *adapter)
3350{
542963b7 3351 u16 profile_id;
4c876616 3352 int status;
39f1d94d 3353
abb93951
PR
3354 status = be_cmd_query_fw_cfg(adapter, &adapter->port_num,
3355 &adapter->function_mode,
0ad3157e
VV
3356 &adapter->function_caps,
3357 &adapter->asic_rev);
abb93951 3358 if (status)
92bf14ab 3359 return status;
abb93951 3360
542963b7
VV
3361 if (be_physfn(adapter)) {
3362 status = be_cmd_get_active_profile(adapter, &profile_id);
3363 if (!status)
3364 dev_info(&adapter->pdev->dev,
3365 "Using profile 0x%x\n", profile_id);
3366 }
3367
92bf14ab
SP
3368 status = be_get_resources(adapter);
3369 if (status)
3370 return status;
abb93951 3371
46ee9c14
RN
3372 adapter->pmac_id = kcalloc(be_max_uc(adapter),
3373 sizeof(*adapter->pmac_id), GFP_KERNEL);
92bf14ab
SP
3374 if (!adapter->pmac_id)
3375 return -ENOMEM;
abb93951 3376
92bf14ab
SP
3377 /* Sanitize cfg_num_qs based on HW and platform limits */
3378 adapter->cfg_num_qs = min(adapter->cfg_num_qs, be_max_qs(adapter));
3379
3380 return 0;
39f1d94d
SP
3381}
3382
95046b92
SP
3383static int be_mac_setup(struct be_adapter *adapter)
3384{
3385 u8 mac[ETH_ALEN];
3386 int status;
3387
3388 if (is_zero_ether_addr(adapter->netdev->dev_addr)) {
3389 status = be_cmd_get_perm_mac(adapter, mac);
3390 if (status)
3391 return status;
3392
3393 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
3394 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
3395 } else {
3396 /* Maybe the HW was reset; dev_addr must be re-programmed */
3397 memcpy(mac, adapter->netdev->dev_addr, ETH_ALEN);
3398 }
3399
2c7a9dc1
AK
3400 /* For BE3-R VFs, the PF programs the initial MAC address */
3401 if (!(BEx_chip(adapter) && be_virtfn(adapter)))
3402 be_cmd_pmac_add(adapter, mac, adapter->if_handle,
3403 &adapter->pmac_id[0], 0);
95046b92
SP
3404 return 0;
3405}
3406
68d7bdcb
SP
3407static void be_schedule_worker(struct be_adapter *adapter)
3408{
3409 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
3410 adapter->flags |= BE_FLAGS_WORKER_SCHEDULED;
3411}
3412
7707133c 3413static int be_setup_queues(struct be_adapter *adapter)
5fb379ee 3414{
68d7bdcb 3415 struct net_device *netdev = adapter->netdev;
10ef9ab4 3416 int status;
ba343c77 3417
7707133c 3418 status = be_evt_queues_create(adapter);
abb93951
PR
3419 if (status)
3420 goto err;
73d540f2 3421
7707133c 3422 status = be_tx_qs_create(adapter);
c2bba3df
SK
3423 if (status)
3424 goto err;
10ef9ab4 3425
7707133c 3426 status = be_rx_cqs_create(adapter);
10ef9ab4 3427 if (status)
a54769f5 3428 goto err;
6b7c5b94 3429
7707133c 3430 status = be_mcc_queues_create(adapter);
10ef9ab4
SP
3431 if (status)
3432 goto err;
3433
68d7bdcb
SP
3434 status = netif_set_real_num_rx_queues(netdev, adapter->num_rx_qs);
3435 if (status)
3436 goto err;
3437
3438 status = netif_set_real_num_tx_queues(netdev, adapter->num_tx_qs);
3439 if (status)
3440 goto err;
3441
7707133c
SP
3442 return 0;
3443err:
3444 dev_err(&adapter->pdev->dev, "queue_setup failed\n");
3445 return status;
3446}
3447
68d7bdcb
SP
3448int be_update_queues(struct be_adapter *adapter)
3449{
3450 struct net_device *netdev = adapter->netdev;
3451 int status;
3452
3453 if (netif_running(netdev))
3454 be_close(netdev);
3455
3456 be_cancel_worker(adapter);
3457
3458 /* If any vectors have been shared with RoCE we cannot re-program
3459 * the MSIx table.
3460 */
3461 if (!adapter->num_msix_roce_vec)
3462 be_msix_disable(adapter);
3463
3464 be_clear_queues(adapter);
3465
3466 if (!msix_enabled(adapter)) {
3467 status = be_msix_enable(adapter);
3468 if (status)
3469 return status;
3470 }
3471
3472 status = be_setup_queues(adapter);
3473 if (status)
3474 return status;
3475
3476 be_schedule_worker(adapter);
3477
3478 if (netif_running(netdev))
3479 status = be_open(netdev);
3480
3481 return status;
3482}
3483
7707133c
SP
3484static int be_setup(struct be_adapter *adapter)
3485{
3486 struct device *dev = &adapter->pdev->dev;
3487 u32 tx_fc, rx_fc, en_flags;
3488 int status;
3489
3490 be_setup_init(adapter);
3491
3492 if (!lancer_chip(adapter))
3493 be_cmd_req_native_mode(adapter);
3494
3495 status = be_get_config(adapter);
10ef9ab4 3496 if (status)
a54769f5 3497 goto err;
6b7c5b94 3498
7707133c 3499 status = be_msix_enable(adapter);
10ef9ab4 3500 if (status)
a54769f5 3501 goto err;
6b7c5b94 3502
f9449ab7 3503 en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
7707133c 3504 BE_IF_FLAGS_MULTICAST | BE_IF_FLAGS_PASS_L3L4_ERRORS;
abb93951 3505 if (adapter->function_caps & BE_FUNCTION_CAPS_RSS)
f9449ab7 3506 en_flags |= BE_IF_FLAGS_RSS;
92bf14ab
SP
3507 en_flags = en_flags & be_if_cap_flags(adapter);
3508 status = be_cmd_if_create(adapter, be_if_cap_flags(adapter), en_flags,
1578e777 3509 &adapter->if_handle, 0);
7707133c 3510 if (status)
a54769f5 3511 goto err;
6b7c5b94 3512
68d7bdcb
SP
3513 /* Updating real_num_tx/rx_queues() requires rtnl_lock() */
3514 rtnl_lock();
7707133c 3515 status = be_setup_queues(adapter);
68d7bdcb 3516 rtnl_unlock();
95046b92 3517 if (status)
1578e777
PR
3518 goto err;
3519
7707133c 3520 be_cmd_get_fn_privileges(adapter, &adapter->cmd_privileges, 0);
7707133c
SP
3521
3522 status = be_mac_setup(adapter);
10ef9ab4
SP
3523 if (status)
3524 goto err;
3525
eeb65ced 3526 be_cmd_get_fw_ver(adapter, adapter->fw_ver, adapter->fw_on_flash);
5a56eb10 3527
e9e2a904
SK
3528 if (BE2_chip(adapter) && fw_major_num(adapter->fw_ver) < 4) {
3529 dev_err(dev, "Firmware on card is old(%s), IRQs may not work.",
3530 adapter->fw_ver);
3531 dev_err(dev, "Please upgrade firmware to version >= 4.0\n");
3532 }
3533
1d1e9a46 3534 if (adapter->vlans_added)
10329df8 3535 be_vid_config(adapter);
7ab8b0b4 3536
a54769f5 3537 be_set_rx_mode(adapter->netdev);
5fb379ee 3538
76a9e08e
SR
3539 be_cmd_get_acpi_wol_cap(adapter);
3540
ddc3f5cb 3541 be_cmd_get_flow_control(adapter, &tx_fc, &rx_fc);
590c391d 3542
ddc3f5cb
AK
3543 if (rx_fc != adapter->rx_fc || tx_fc != adapter->tx_fc)
3544 be_cmd_set_flow_control(adapter, adapter->tx_fc,
a54769f5 3545 adapter->rx_fc);
2dc1deb6 3546
bdce2ad7
SR
3547 if (be_physfn(adapter))
3548 be_cmd_set_logical_link_config(adapter,
3549 IFLA_VF_LINK_STATE_AUTO, 0);
3550
b905b5d4 3551 if (sriov_want(adapter)) {
92bf14ab 3552 if (be_max_vfs(adapter))
39f1d94d
SP
3553 be_vf_setup(adapter);
3554 else
3555 dev_warn(dev, "device doesn't support SRIOV\n");
f9449ab7
SP
3556 }
3557
f25b119c
PR
3558 status = be_cmd_get_phy_info(adapter);
3559 if (!status && be_pause_supported(adapter))
42f11cf2
AK
3560 adapter->phy.fc_autoneg = 1;
3561
68d7bdcb 3562 be_schedule_worker(adapter);
f9449ab7 3563 return 0;
a54769f5
SP
3564err:
3565 be_clear(adapter);
3566 return status;
3567}
6b7c5b94 3568
66268739
IV
3569#ifdef CONFIG_NET_POLL_CONTROLLER
3570static void be_netpoll(struct net_device *netdev)
3571{
3572 struct be_adapter *adapter = netdev_priv(netdev);
10ef9ab4 3573 struct be_eq_obj *eqo;
66268739
IV
3574 int i;
3575
e49cc34f
SP
3576 for_all_evt_queues(adapter, eqo, i) {
3577 be_eq_notify(eqo->adapter, eqo->q.id, false, true, 0);
3578 napi_schedule(&eqo->napi);
3579 }
10ef9ab4
SP
3580
3581 return;
66268739
IV
3582}
3583#endif
3584
84517482 3585#define FW_FILE_HDR_SIGN "ServerEngines Corp. "
4188e7df 3586static char flash_cookie[2][16] = {"*** SE FLAS", "H DIRECTORY *** "};
c165541e 3587
fa9a6fed 3588static bool be_flash_redboot(struct be_adapter *adapter,
3f0d4560
AK
3589 const u8 *p, u32 img_start, int image_size,
3590 int hdr_size)
fa9a6fed
SB
3591{
3592 u32 crc_offset;
3593 u8 flashed_crc[4];
3594 int status;
3f0d4560
AK
3595
3596 crc_offset = hdr_size + img_start + image_size - 4;
3597
fa9a6fed 3598 p += crc_offset;
3f0d4560
AK
3599
3600 status = be_cmd_get_flash_crc(adapter, flashed_crc,
f510fc64 3601 (image_size - 4));
fa9a6fed
SB
3602 if (status) {
3603 dev_err(&adapter->pdev->dev,
3604 "could not get crc from flash, not flashing redboot\n");
3605 return false;
3606 }
3607
3608 /*update redboot only if crc does not match*/
3609 if (!memcmp(flashed_crc, p, 4))
3610 return false;
3611 else
3612 return true;
fa9a6fed
SB
3613}
3614
306f1348
SP
3615static bool phy_flashing_required(struct be_adapter *adapter)
3616{
42f11cf2
AK
3617 return (adapter->phy.phy_type == TN_8022 &&
3618 adapter->phy.interface_type == PHY_TYPE_BASET_10GB);
306f1348
SP
3619}
3620
c165541e
PR
3621static bool is_comp_in_ufi(struct be_adapter *adapter,
3622 struct flash_section_info *fsec, int type)
3623{
3624 int i = 0, img_type = 0;
3625 struct flash_section_info_g2 *fsec_g2 = NULL;
3626
ca34fe38 3627 if (BE2_chip(adapter))
c165541e
PR
3628 fsec_g2 = (struct flash_section_info_g2 *)fsec;
3629
3630 for (i = 0; i < MAX_FLASH_COMP; i++) {
3631 if (fsec_g2)
3632 img_type = le32_to_cpu(fsec_g2->fsec_entry[i].type);
3633 else
3634 img_type = le32_to_cpu(fsec->fsec_entry[i].type);
3635
3636 if (img_type == type)
3637 return true;
3638 }
3639 return false;
3640
3641}
3642
4188e7df 3643static struct flash_section_info *get_fsec_info(struct be_adapter *adapter,
c165541e
PR
3644 int header_size,
3645 const struct firmware *fw)
3646{
3647 struct flash_section_info *fsec = NULL;
3648 const u8 *p = fw->data;
3649
3650 p += header_size;
3651 while (p < (fw->data + fw->size)) {
3652 fsec = (struct flash_section_info *)p;
3653 if (!memcmp(flash_cookie, fsec->cookie, sizeof(flash_cookie)))
3654 return fsec;
3655 p += 32;
3656 }
3657 return NULL;
3658}
3659
773a2d7c
PR
3660static int be_flash(struct be_adapter *adapter, const u8 *img,
3661 struct be_dma_mem *flash_cmd, int optype, int img_size)
3662{
3663 u32 total_bytes = 0, flash_op, num_bytes = 0;
3664 int status = 0;
3665 struct be_cmd_write_flashrom *req = flash_cmd->va;
3666
3667 total_bytes = img_size;
3668 while (total_bytes) {
3669 num_bytes = min_t(u32, 32*1024, total_bytes);
3670
3671 total_bytes -= num_bytes;
3672
3673 if (!total_bytes) {
3674 if (optype == OPTYPE_PHY_FW)
3675 flash_op = FLASHROM_OPER_PHY_FLASH;
3676 else
3677 flash_op = FLASHROM_OPER_FLASH;
3678 } else {
3679 if (optype == OPTYPE_PHY_FW)
3680 flash_op = FLASHROM_OPER_PHY_SAVE;
3681 else
3682 flash_op = FLASHROM_OPER_SAVE;
3683 }
3684
be716446 3685 memcpy(req->data_buf, img, num_bytes);
773a2d7c
PR
3686 img += num_bytes;
3687 status = be_cmd_write_flashrom(adapter, flash_cmd, optype,
3688 flash_op, num_bytes);
3689 if (status) {
3690 if (status == ILLEGAL_IOCTL_REQ &&
3691 optype == OPTYPE_PHY_FW)
3692 break;
3693 dev_err(&adapter->pdev->dev,
3694 "cmd to write to flash rom failed.\n");
3695 return status;
3696 }
3697 }
3698 return 0;
3699}
3700
0ad3157e 3701/* For BE2, BE3 and BE3-R */
ca34fe38 3702static int be_flash_BEx(struct be_adapter *adapter,
c165541e
PR
3703 const struct firmware *fw,
3704 struct be_dma_mem *flash_cmd,
3705 int num_of_images)
3f0d4560 3706
84517482 3707{
3f0d4560 3708 int status = 0, i, filehdr_size = 0;
c165541e 3709 int img_hdrs_size = (num_of_images * sizeof(struct image_hdr));
84517482 3710 const u8 *p = fw->data;
215faf9c 3711 const struct flash_comp *pflashcomp;
773a2d7c 3712 int num_comp, redboot;
c165541e
PR
3713 struct flash_section_info *fsec = NULL;
3714
3715 struct flash_comp gen3_flash_types[] = {
3716 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, OPTYPE_ISCSI_ACTIVE,
3717 FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_iSCSI},
3718 { FLASH_REDBOOT_START_g3, OPTYPE_REDBOOT,
3719 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3, IMAGE_BOOT_CODE},
3720 { FLASH_iSCSI_BIOS_START_g3, OPTYPE_BIOS,
3721 FLASH_BIOS_IMAGE_MAX_SIZE_g3, IMAGE_OPTION_ROM_ISCSI},
3722 { FLASH_PXE_BIOS_START_g3, OPTYPE_PXE_BIOS,
3723 FLASH_BIOS_IMAGE_MAX_SIZE_g3, IMAGE_OPTION_ROM_PXE},
3724 { FLASH_FCoE_BIOS_START_g3, OPTYPE_FCOE_BIOS,
3725 FLASH_BIOS_IMAGE_MAX_SIZE_g3, IMAGE_OPTION_ROM_FCoE},
3726 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, OPTYPE_ISCSI_BACKUP,
3727 FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_BACKUP_iSCSI},
3728 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, OPTYPE_FCOE_FW_ACTIVE,
3729 FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_FCoE},
3730 { FLASH_FCoE_BACKUP_IMAGE_START_g3, OPTYPE_FCOE_FW_BACKUP,
3731 FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_BACKUP_FCoE},
3732 { FLASH_NCSI_START_g3, OPTYPE_NCSI_FW,
3733 FLASH_NCSI_IMAGE_MAX_SIZE_g3, IMAGE_NCSI},
3734 { FLASH_PHY_FW_START_g3, OPTYPE_PHY_FW,
3735 FLASH_PHY_FW_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_PHY}
3f0d4560 3736 };
c165541e
PR
3737
3738 struct flash_comp gen2_flash_types[] = {
3739 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, OPTYPE_ISCSI_ACTIVE,
3740 FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_iSCSI},
3741 { FLASH_REDBOOT_START_g2, OPTYPE_REDBOOT,
3742 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2, IMAGE_BOOT_CODE},
3743 { FLASH_iSCSI_BIOS_START_g2, OPTYPE_BIOS,
3744 FLASH_BIOS_IMAGE_MAX_SIZE_g2, IMAGE_OPTION_ROM_ISCSI},
3745 { FLASH_PXE_BIOS_START_g2, OPTYPE_PXE_BIOS,
3746 FLASH_BIOS_IMAGE_MAX_SIZE_g2, IMAGE_OPTION_ROM_PXE},
3747 { FLASH_FCoE_BIOS_START_g2, OPTYPE_FCOE_BIOS,
3748 FLASH_BIOS_IMAGE_MAX_SIZE_g2, IMAGE_OPTION_ROM_FCoE},
3749 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, OPTYPE_ISCSI_BACKUP,
3750 FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_BACKUP_iSCSI},
3751 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, OPTYPE_FCOE_FW_ACTIVE,
3752 FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_FCoE},
3753 { FLASH_FCoE_BACKUP_IMAGE_START_g2, OPTYPE_FCOE_FW_BACKUP,
3754 FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_BACKUP_FCoE}
3f0d4560
AK
3755 };
3756
ca34fe38 3757 if (BE3_chip(adapter)) {
3f0d4560
AK
3758 pflashcomp = gen3_flash_types;
3759 filehdr_size = sizeof(struct flash_file_hdr_g3);
215faf9c 3760 num_comp = ARRAY_SIZE(gen3_flash_types);
3f0d4560
AK
3761 } else {
3762 pflashcomp = gen2_flash_types;
3763 filehdr_size = sizeof(struct flash_file_hdr_g2);
215faf9c 3764 num_comp = ARRAY_SIZE(gen2_flash_types);
84517482 3765 }
ca34fe38 3766
c165541e
PR
3767 /* Get flash section info*/
3768 fsec = get_fsec_info(adapter, filehdr_size + img_hdrs_size, fw);
3769 if (!fsec) {
3770 dev_err(&adapter->pdev->dev,
3771 "Invalid Cookie. UFI corrupted ?\n");
3772 return -1;
3773 }
9fe96934 3774 for (i = 0; i < num_comp; i++) {
c165541e 3775 if (!is_comp_in_ufi(adapter, fsec, pflashcomp[i].img_type))
9fe96934 3776 continue;
c165541e
PR
3777
3778 if ((pflashcomp[i].optype == OPTYPE_NCSI_FW) &&
3779 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
3780 continue;
3781
773a2d7c
PR
3782 if (pflashcomp[i].optype == OPTYPE_PHY_FW &&
3783 !phy_flashing_required(adapter))
306f1348 3784 continue;
c165541e 3785
773a2d7c
PR
3786 if (pflashcomp[i].optype == OPTYPE_REDBOOT) {
3787 redboot = be_flash_redboot(adapter, fw->data,
3788 pflashcomp[i].offset, pflashcomp[i].size,
3789 filehdr_size + img_hdrs_size);
3790 if (!redboot)
3791 continue;
3792 }
c165541e 3793
3f0d4560 3794 p = fw->data;
c165541e 3795 p += filehdr_size + pflashcomp[i].offset + img_hdrs_size;
306f1348
SP
3796 if (p + pflashcomp[i].size > fw->data + fw->size)
3797 return -1;
773a2d7c
PR
3798
3799 status = be_flash(adapter, p, flash_cmd, pflashcomp[i].optype,
3800 pflashcomp[i].size);
3801 if (status) {
3802 dev_err(&adapter->pdev->dev,
3803 "Flashing section type %d failed.\n",
3804 pflashcomp[i].img_type);
3805 return status;
84517482 3806 }
84517482 3807 }
84517482
AK
3808 return 0;
3809}
3810
773a2d7c
PR
3811static int be_flash_skyhawk(struct be_adapter *adapter,
3812 const struct firmware *fw,
3813 struct be_dma_mem *flash_cmd, int num_of_images)
3f0d4560 3814{
773a2d7c
PR
3815 int status = 0, i, filehdr_size = 0;
3816 int img_offset, img_size, img_optype, redboot;
3817 int img_hdrs_size = num_of_images * sizeof(struct image_hdr);
3818 const u8 *p = fw->data;
3819 struct flash_section_info *fsec = NULL;
3820
3821 filehdr_size = sizeof(struct flash_file_hdr_g3);
3822 fsec = get_fsec_info(adapter, filehdr_size + img_hdrs_size, fw);
3823 if (!fsec) {
3824 dev_err(&adapter->pdev->dev,
3825 "Invalid Cookie. UFI corrupted ?\n");
3826 return -1;
3827 }
3828
3829 for (i = 0; i < le32_to_cpu(fsec->fsec_hdr.num_images); i++) {
3830 img_offset = le32_to_cpu(fsec->fsec_entry[i].offset);
3831 img_size = le32_to_cpu(fsec->fsec_entry[i].pad_size);
3832
3833 switch (le32_to_cpu(fsec->fsec_entry[i].type)) {
3834 case IMAGE_FIRMWARE_iSCSI:
3835 img_optype = OPTYPE_ISCSI_ACTIVE;
3836 break;
3837 case IMAGE_BOOT_CODE:
3838 img_optype = OPTYPE_REDBOOT;
3839 break;
3840 case IMAGE_OPTION_ROM_ISCSI:
3841 img_optype = OPTYPE_BIOS;
3842 break;
3843 case IMAGE_OPTION_ROM_PXE:
3844 img_optype = OPTYPE_PXE_BIOS;
3845 break;
3846 case IMAGE_OPTION_ROM_FCoE:
3847 img_optype = OPTYPE_FCOE_BIOS;
3848 break;
3849 case IMAGE_FIRMWARE_BACKUP_iSCSI:
3850 img_optype = OPTYPE_ISCSI_BACKUP;
3851 break;
3852 case IMAGE_NCSI:
3853 img_optype = OPTYPE_NCSI_FW;
3854 break;
3855 default:
3856 continue;
3857 }
3858
3859 if (img_optype == OPTYPE_REDBOOT) {
3860 redboot = be_flash_redboot(adapter, fw->data,
3861 img_offset, img_size,
3862 filehdr_size + img_hdrs_size);
3863 if (!redboot)
3864 continue;
3865 }
3866
3867 p = fw->data;
3868 p += filehdr_size + img_offset + img_hdrs_size;
3869 if (p + img_size > fw->data + fw->size)
3870 return -1;
3871
3872 status = be_flash(adapter, p, flash_cmd, img_optype, img_size);
3873 if (status) {
3874 dev_err(&adapter->pdev->dev,
3875 "Flashing section type %d failed.\n",
3876 fsec->fsec_entry[i].type);
3877 return status;
3878 }
3879 }
3880 return 0;
3f0d4560
AK
3881}
3882
485bf569
SN
3883static int lancer_fw_download(struct be_adapter *adapter,
3884 const struct firmware *fw)
84517482 3885{
485bf569
SN
3886#define LANCER_FW_DOWNLOAD_CHUNK (32 * 1024)
3887#define LANCER_FW_DOWNLOAD_LOCATION "/prg"
84517482 3888 struct be_dma_mem flash_cmd;
485bf569
SN
3889 const u8 *data_ptr = NULL;
3890 u8 *dest_image_ptr = NULL;
3891 size_t image_size = 0;
3892 u32 chunk_size = 0;
3893 u32 data_written = 0;
3894 u32 offset = 0;
3895 int status = 0;
3896 u8 add_status = 0;
f67ef7ba 3897 u8 change_status;
84517482 3898
485bf569 3899 if (!IS_ALIGNED(fw->size, sizeof(u32))) {
d9efd2af 3900 dev_err(&adapter->pdev->dev,
485bf569
SN
3901 "FW Image not properly aligned. "
3902 "Length must be 4 byte aligned.\n");
3903 status = -EINVAL;
3904 goto lancer_fw_exit;
d9efd2af
SB
3905 }
3906
485bf569
SN
3907 flash_cmd.size = sizeof(struct lancer_cmd_req_write_object)
3908 + LANCER_FW_DOWNLOAD_CHUNK;
3909 flash_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, flash_cmd.size,
d0320f75 3910 &flash_cmd.dma, GFP_KERNEL);
485bf569
SN
3911 if (!flash_cmd.va) {
3912 status = -ENOMEM;
485bf569
SN
3913 goto lancer_fw_exit;
3914 }
84517482 3915
485bf569
SN
3916 dest_image_ptr = flash_cmd.va +
3917 sizeof(struct lancer_cmd_req_write_object);
3918 image_size = fw->size;
3919 data_ptr = fw->data;
3920
3921 while (image_size) {
3922 chunk_size = min_t(u32, image_size, LANCER_FW_DOWNLOAD_CHUNK);
3923
3924 /* Copy the image chunk content. */
3925 memcpy(dest_image_ptr, data_ptr, chunk_size);
3926
3927 status = lancer_cmd_write_object(adapter, &flash_cmd,
f67ef7ba
PR
3928 chunk_size, offset,
3929 LANCER_FW_DOWNLOAD_LOCATION,
3930 &data_written, &change_status,
3931 &add_status);
485bf569
SN
3932 if (status)
3933 break;
3934
3935 offset += data_written;
3936 data_ptr += data_written;
3937 image_size -= data_written;
3938 }
3939
3940 if (!status) {
3941 /* Commit the FW written */
3942 status = lancer_cmd_write_object(adapter, &flash_cmd,
f67ef7ba
PR
3943 0, offset,
3944 LANCER_FW_DOWNLOAD_LOCATION,
3945 &data_written, &change_status,
3946 &add_status);
485bf569
SN
3947 }
3948
3949 dma_free_coherent(&adapter->pdev->dev, flash_cmd.size, flash_cmd.va,
3950 flash_cmd.dma);
3951 if (status) {
3952 dev_err(&adapter->pdev->dev,
3953 "Firmware load error. "
3954 "Status code: 0x%x Additional Status: 0x%x\n",
3955 status, add_status);
3956 goto lancer_fw_exit;
3957 }
3958
f67ef7ba 3959 if (change_status == LANCER_FW_RESET_NEEDED) {
4bebb56a
SK
3960 dev_info(&adapter->pdev->dev,
3961 "Resetting adapter to activate new FW\n");
5c510811
SK
3962 status = lancer_physdev_ctrl(adapter,
3963 PHYSDEV_CONTROL_FW_RESET_MASK);
f67ef7ba
PR
3964 if (status) {
3965 dev_err(&adapter->pdev->dev,
3966 "Adapter busy for FW reset.\n"
3967 "New FW will not be active.\n");
3968 goto lancer_fw_exit;
3969 }
3970 } else if (change_status != LANCER_NO_RESET_NEEDED) {
3971 dev_err(&adapter->pdev->dev,
3972 "System reboot required for new FW"
3973 " to be active\n");
3974 }
3975
485bf569
SN
3976 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
3977lancer_fw_exit:
3978 return status;
3979}
3980
ca34fe38
SP
3981#define UFI_TYPE2 2
3982#define UFI_TYPE3 3
0ad3157e 3983#define UFI_TYPE3R 10
ca34fe38
SP
3984#define UFI_TYPE4 4
3985static int be_get_ufi_type(struct be_adapter *adapter,
0ad3157e 3986 struct flash_file_hdr_g3 *fhdr)
773a2d7c
PR
3987{
3988 if (fhdr == NULL)
3989 goto be_get_ufi_exit;
3990
ca34fe38
SP
3991 if (skyhawk_chip(adapter) && fhdr->build[0] == '4')
3992 return UFI_TYPE4;
0ad3157e
VV
3993 else if (BE3_chip(adapter) && fhdr->build[0] == '3') {
3994 if (fhdr->asic_type_rev == 0x10)
3995 return UFI_TYPE3R;
3996 else
3997 return UFI_TYPE3;
3998 } else if (BE2_chip(adapter) && fhdr->build[0] == '2')
ca34fe38 3999 return UFI_TYPE2;
773a2d7c
PR
4000
4001be_get_ufi_exit:
4002 dev_err(&adapter->pdev->dev,
4003 "UFI and Interface are not compatible for flashing\n");
4004 return -1;
4005}
4006
485bf569
SN
4007static int be_fw_download(struct be_adapter *adapter, const struct firmware* fw)
4008{
485bf569
SN
4009 struct flash_file_hdr_g3 *fhdr3;
4010 struct image_hdr *img_hdr_ptr = NULL;
4011 struct be_dma_mem flash_cmd;
4012 const u8 *p;
773a2d7c 4013 int status = 0, i = 0, num_imgs = 0, ufi_type = 0;
84517482 4014
be716446 4015 flash_cmd.size = sizeof(struct be_cmd_write_flashrom);
2b7bcebf
IV
4016 flash_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, flash_cmd.size,
4017 &flash_cmd.dma, GFP_KERNEL);
84517482
AK
4018 if (!flash_cmd.va) {
4019 status = -ENOMEM;
485bf569 4020 goto be_fw_exit;
84517482
AK
4021 }
4022
773a2d7c 4023 p = fw->data;
0ad3157e 4024 fhdr3 = (struct flash_file_hdr_g3 *)p;
773a2d7c 4025
0ad3157e 4026 ufi_type = be_get_ufi_type(adapter, fhdr3);
773a2d7c 4027
773a2d7c
PR
4028 num_imgs = le32_to_cpu(fhdr3->num_imgs);
4029 for (i = 0; i < num_imgs; i++) {
4030 img_hdr_ptr = (struct image_hdr *)(fw->data +
4031 (sizeof(struct flash_file_hdr_g3) +
4032 i * sizeof(struct image_hdr)));
4033 if (le32_to_cpu(img_hdr_ptr->imageid) == 1) {
0ad3157e
VV
4034 switch (ufi_type) {
4035 case UFI_TYPE4:
773a2d7c
PR
4036 status = be_flash_skyhawk(adapter, fw,
4037 &flash_cmd, num_imgs);
0ad3157e
VV
4038 break;
4039 case UFI_TYPE3R:
ca34fe38
SP
4040 status = be_flash_BEx(adapter, fw, &flash_cmd,
4041 num_imgs);
0ad3157e
VV
4042 break;
4043 case UFI_TYPE3:
4044 /* Do not flash this ufi on BE3-R cards */
4045 if (adapter->asic_rev < 0x10)
4046 status = be_flash_BEx(adapter, fw,
4047 &flash_cmd,
4048 num_imgs);
4049 else {
4050 status = -1;
4051 dev_err(&adapter->pdev->dev,
4052 "Can't load BE3 UFI on BE3R\n");
4053 }
4054 }
3f0d4560 4055 }
773a2d7c
PR
4056 }
4057
ca34fe38
SP
4058 if (ufi_type == UFI_TYPE2)
4059 status = be_flash_BEx(adapter, fw, &flash_cmd, 0);
773a2d7c 4060 else if (ufi_type == -1)
3f0d4560 4061 status = -1;
84517482 4062
2b7bcebf
IV
4063 dma_free_coherent(&adapter->pdev->dev, flash_cmd.size, flash_cmd.va,
4064 flash_cmd.dma);
84517482
AK
4065 if (status) {
4066 dev_err(&adapter->pdev->dev, "Firmware load error\n");
485bf569 4067 goto be_fw_exit;
84517482
AK
4068 }
4069
af901ca1 4070 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
84517482 4071
485bf569
SN
4072be_fw_exit:
4073 return status;
4074}
4075
4076int be_load_fw(struct be_adapter *adapter, u8 *fw_file)
4077{
4078 const struct firmware *fw;
4079 int status;
4080
4081 if (!netif_running(adapter->netdev)) {
4082 dev_err(&adapter->pdev->dev,
4083 "Firmware load not allowed (interface is down)\n");
4084 return -1;
4085 }
4086
4087 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
4088 if (status)
4089 goto fw_exit;
4090
4091 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
4092
4093 if (lancer_chip(adapter))
4094 status = lancer_fw_download(adapter, fw);
4095 else
4096 status = be_fw_download(adapter, fw);
4097
eeb65ced
SK
4098 if (!status)
4099 be_cmd_get_fw_ver(adapter, adapter->fw_ver,
4100 adapter->fw_on_flash);
4101
84517482
AK
4102fw_exit:
4103 release_firmware(fw);
4104 return status;
4105}
4106
a77dcb8c
AK
4107static int be_ndo_bridge_setlink(struct net_device *dev,
4108 struct nlmsghdr *nlh)
4109{
4110 struct be_adapter *adapter = netdev_priv(dev);
4111 struct nlattr *attr, *br_spec;
4112 int rem;
4113 int status = 0;
4114 u16 mode = 0;
4115
4116 if (!sriov_enabled(adapter))
4117 return -EOPNOTSUPP;
4118
4119 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4120
4121 nla_for_each_nested(attr, br_spec, rem) {
4122 if (nla_type(attr) != IFLA_BRIDGE_MODE)
4123 continue;
4124
4125 mode = nla_get_u16(attr);
4126 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
4127 return -EINVAL;
4128
4129 status = be_cmd_set_hsw_config(adapter, 0, 0,
4130 adapter->if_handle,
4131 mode == BRIDGE_MODE_VEPA ?
4132 PORT_FWD_TYPE_VEPA :
4133 PORT_FWD_TYPE_VEB);
4134 if (status)
4135 goto err;
4136
4137 dev_info(&adapter->pdev->dev, "enabled switch mode: %s\n",
4138 mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
4139
4140 return status;
4141 }
4142err:
4143 dev_err(&adapter->pdev->dev, "Failed to set switch mode %s\n",
4144 mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
4145
4146 return status;
4147}
4148
4149static int be_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4150 struct net_device *dev,
4151 u32 filter_mask)
4152{
4153 struct be_adapter *adapter = netdev_priv(dev);
4154 int status = 0;
4155 u8 hsw_mode;
4156
4157 if (!sriov_enabled(adapter))
4158 return 0;
4159
4160 /* BE and Lancer chips support VEB mode only */
4161 if (BEx_chip(adapter) || lancer_chip(adapter)) {
4162 hsw_mode = PORT_FWD_TYPE_VEB;
4163 } else {
4164 status = be_cmd_get_hsw_config(adapter, NULL, 0,
4165 adapter->if_handle, &hsw_mode);
4166 if (status)
4167 return 0;
4168 }
4169
4170 return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
4171 hsw_mode == PORT_FWD_TYPE_VEPA ?
4172 BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB);
4173}
4174
c5abe7c0 4175#ifdef CONFIG_BE2NET_VXLAN
c9c47142
SP
4176static void be_add_vxlan_port(struct net_device *netdev, sa_family_t sa_family,
4177 __be16 port)
4178{
4179 struct be_adapter *adapter = netdev_priv(netdev);
4180 struct device *dev = &adapter->pdev->dev;
4181 int status;
4182
4183 if (lancer_chip(adapter) || BEx_chip(adapter))
4184 return;
4185
4186 if (adapter->flags & BE_FLAGS_VXLAN_OFFLOADS) {
4187 dev_warn(dev, "Cannot add UDP port %d for VxLAN offloads\n",
4188 be16_to_cpu(port));
4189 dev_info(dev,
4190 "Only one UDP port supported for VxLAN offloads\n");
4191 return;
4192 }
4193
4194 status = be_cmd_manage_iface(adapter, adapter->if_handle,
4195 OP_CONVERT_NORMAL_TO_TUNNEL);
4196 if (status) {
4197 dev_warn(dev, "Failed to convert normal interface to tunnel\n");
4198 goto err;
4199 }
4200
4201 status = be_cmd_set_vxlan_port(adapter, port);
4202 if (status) {
4203 dev_warn(dev, "Failed to add VxLAN port\n");
4204 goto err;
4205 }
4206 adapter->flags |= BE_FLAGS_VXLAN_OFFLOADS;
4207 adapter->vxlan_port = port;
4208
4209 dev_info(dev, "Enabled VxLAN offloads for UDP port %d\n",
4210 be16_to_cpu(port));
4211 return;
4212err:
4213 be_disable_vxlan_offloads(adapter);
4214 return;
4215}
4216
4217static void be_del_vxlan_port(struct net_device *netdev, sa_family_t sa_family,
4218 __be16 port)
4219{
4220 struct be_adapter *adapter = netdev_priv(netdev);
4221
4222 if (lancer_chip(adapter) || BEx_chip(adapter))
4223 return;
4224
4225 if (adapter->vxlan_port != port)
4226 return;
4227
4228 be_disable_vxlan_offloads(adapter);
4229
4230 dev_info(&adapter->pdev->dev,
4231 "Disabled VxLAN offloads for UDP port %d\n",
4232 be16_to_cpu(port));
4233}
c5abe7c0 4234#endif
c9c47142 4235
e5686ad8 4236static const struct net_device_ops be_netdev_ops = {
6b7c5b94
SP
4237 .ndo_open = be_open,
4238 .ndo_stop = be_close,
4239 .ndo_start_xmit = be_xmit,
a54769f5 4240 .ndo_set_rx_mode = be_set_rx_mode,
6b7c5b94
SP
4241 .ndo_set_mac_address = be_mac_addr_set,
4242 .ndo_change_mtu = be_change_mtu,
ab1594e9 4243 .ndo_get_stats64 = be_get_stats64,
6b7c5b94 4244 .ndo_validate_addr = eth_validate_addr,
6b7c5b94
SP
4245 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
4246 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
64600ea5 4247 .ndo_set_vf_mac = be_set_vf_mac,
1da87b7f 4248 .ndo_set_vf_vlan = be_set_vf_vlan,
e1d18735 4249 .ndo_set_vf_tx_rate = be_set_vf_tx_rate,
66268739 4250 .ndo_get_vf_config = be_get_vf_config,
bdce2ad7 4251 .ndo_set_vf_link_state = be_set_vf_link_state,
66268739
IV
4252#ifdef CONFIG_NET_POLL_CONTROLLER
4253 .ndo_poll_controller = be_netpoll,
4254#endif
a77dcb8c
AK
4255 .ndo_bridge_setlink = be_ndo_bridge_setlink,
4256 .ndo_bridge_getlink = be_ndo_bridge_getlink,
6384a4d0 4257#ifdef CONFIG_NET_RX_BUSY_POLL
c9c47142 4258 .ndo_busy_poll = be_busy_poll,
6384a4d0 4259#endif
c5abe7c0 4260#ifdef CONFIG_BE2NET_VXLAN
c9c47142
SP
4261 .ndo_add_vxlan_port = be_add_vxlan_port,
4262 .ndo_del_vxlan_port = be_del_vxlan_port,
c5abe7c0 4263#endif
6b7c5b94
SP
4264};
4265
4266static void be_netdev_init(struct net_device *netdev)
4267{
4268 struct be_adapter *adapter = netdev_priv(netdev);
4269
c9c47142
SP
4270 if (skyhawk_chip(adapter)) {
4271 netdev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4272 NETIF_F_TSO | NETIF_F_TSO6 |
4273 NETIF_F_GSO_UDP_TUNNEL;
4274 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
4275 }
6332c8d3 4276 netdev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
8b8ddc68 4277 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM |
f646968f 4278 NETIF_F_HW_VLAN_CTAG_TX;
8b8ddc68
MM
4279 if (be_multi_rxq(adapter))
4280 netdev->hw_features |= NETIF_F_RXHASH;
6332c8d3
MM
4281
4282 netdev->features |= netdev->hw_features |
f646968f 4283 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_FILTER;
4b972914 4284
eb8a50d9 4285 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
79032644 4286 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
51c59870 4287
fbc13f01
AK
4288 netdev->priv_flags |= IFF_UNICAST_FLT;
4289
6b7c5b94
SP
4290 netdev->flags |= IFF_MULTICAST;
4291
b7e5887e 4292 netif_set_gso_max_size(netdev, 65535 - ETH_HLEN);
c190e3c8 4293
10ef9ab4 4294 netdev->netdev_ops = &be_netdev_ops;
6b7c5b94
SP
4295
4296 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
6b7c5b94
SP
4297}
4298
4299static void be_unmap_pci_bars(struct be_adapter *adapter)
4300{
c5b3ad4c
SP
4301 if (adapter->csr)
4302 pci_iounmap(adapter->pdev, adapter->csr);
8788fdc2 4303 if (adapter->db)
ce66f781 4304 pci_iounmap(adapter->pdev, adapter->db);
045508a8
PP
4305}
4306
ce66f781
SP
4307static int db_bar(struct be_adapter *adapter)
4308{
4309 if (lancer_chip(adapter) || !be_physfn(adapter))
4310 return 0;
4311 else
4312 return 4;
4313}
4314
4315static int be_roce_map_pci_bars(struct be_adapter *adapter)
045508a8 4316{
dbf0f2a7 4317 if (skyhawk_chip(adapter)) {
ce66f781
SP
4318 adapter->roce_db.size = 4096;
4319 adapter->roce_db.io_addr = pci_resource_start(adapter->pdev,
4320 db_bar(adapter));
4321 adapter->roce_db.total_size = pci_resource_len(adapter->pdev,
4322 db_bar(adapter));
4323 }
045508a8 4324 return 0;
6b7c5b94
SP
4325}
4326
4327static int be_map_pci_bars(struct be_adapter *adapter)
4328{
4329 u8 __iomem *addr;
fe6d2a38 4330
c5b3ad4c
SP
4331 if (BEx_chip(adapter) && be_physfn(adapter)) {
4332 adapter->csr = pci_iomap(adapter->pdev, 2, 0);
4333 if (adapter->csr == NULL)
4334 return -ENOMEM;
4335 }
4336
ce66f781 4337 addr = pci_iomap(adapter->pdev, db_bar(adapter), 0);
6b7c5b94
SP
4338 if (addr == NULL)
4339 goto pci_map_err;
ba343c77 4340 adapter->db = addr;
ce66f781
SP
4341
4342 be_roce_map_pci_bars(adapter);
6b7c5b94 4343 return 0;
ce66f781 4344
6b7c5b94
SP
4345pci_map_err:
4346 be_unmap_pci_bars(adapter);
4347 return -ENOMEM;
4348}
4349
6b7c5b94
SP
4350static void be_ctrl_cleanup(struct be_adapter *adapter)
4351{
8788fdc2 4352 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
6b7c5b94
SP
4353
4354 be_unmap_pci_bars(adapter);
4355
4356 if (mem->va)
2b7bcebf
IV
4357 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
4358 mem->dma);
e7b909a6 4359
5b8821b7 4360 mem = &adapter->rx_filter;
e7b909a6 4361 if (mem->va)
2b7bcebf
IV
4362 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
4363 mem->dma);
6b7c5b94
SP
4364}
4365
6b7c5b94
SP
4366static int be_ctrl_init(struct be_adapter *adapter)
4367{
8788fdc2
SP
4368 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
4369 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
5b8821b7 4370 struct be_dma_mem *rx_filter = &adapter->rx_filter;
ce66f781 4371 u32 sli_intf;
6b7c5b94 4372 int status;
6b7c5b94 4373
ce66f781
SP
4374 pci_read_config_dword(adapter->pdev, SLI_INTF_REG_OFFSET, &sli_intf);
4375 adapter->sli_family = (sli_intf & SLI_INTF_FAMILY_MASK) >>
4376 SLI_INTF_FAMILY_SHIFT;
4377 adapter->virtfn = (sli_intf & SLI_INTF_FT_MASK) ? 1 : 0;
4378
6b7c5b94
SP
4379 status = be_map_pci_bars(adapter);
4380 if (status)
e7b909a6 4381 goto done;
6b7c5b94
SP
4382
4383 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
2b7bcebf
IV
4384 mbox_mem_alloc->va = dma_alloc_coherent(&adapter->pdev->dev,
4385 mbox_mem_alloc->size,
4386 &mbox_mem_alloc->dma,
4387 GFP_KERNEL);
6b7c5b94 4388 if (!mbox_mem_alloc->va) {
e7b909a6
SP
4389 status = -ENOMEM;
4390 goto unmap_pci_bars;
6b7c5b94
SP
4391 }
4392 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
4393 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
4394 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
4395 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
e7b909a6 4396
5b8821b7 4397 rx_filter->size = sizeof(struct be_cmd_req_rx_filter);
ede23fa8
JP
4398 rx_filter->va = dma_zalloc_coherent(&adapter->pdev->dev,
4399 rx_filter->size, &rx_filter->dma,
4400 GFP_KERNEL);
5b8821b7 4401 if (rx_filter->va == NULL) {
e7b909a6
SP
4402 status = -ENOMEM;
4403 goto free_mbox;
4404 }
1f9061d2 4405
2984961c 4406 mutex_init(&adapter->mbox_lock);
8788fdc2
SP
4407 spin_lock_init(&adapter->mcc_lock);
4408 spin_lock_init(&adapter->mcc_cq_lock);
a8f447bd 4409
5eeff635 4410 init_completion(&adapter->et_cmd_compl);
cf588477 4411 pci_save_state(adapter->pdev);
6b7c5b94 4412 return 0;
e7b909a6
SP
4413
4414free_mbox:
2b7bcebf
IV
4415 dma_free_coherent(&adapter->pdev->dev, mbox_mem_alloc->size,
4416 mbox_mem_alloc->va, mbox_mem_alloc->dma);
e7b909a6
SP
4417
4418unmap_pci_bars:
4419 be_unmap_pci_bars(adapter);
4420
4421done:
4422 return status;
6b7c5b94
SP
4423}
4424
4425static void be_stats_cleanup(struct be_adapter *adapter)
4426{
3abcdeda 4427 struct be_dma_mem *cmd = &adapter->stats_cmd;
6b7c5b94
SP
4428
4429 if (cmd->va)
2b7bcebf
IV
4430 dma_free_coherent(&adapter->pdev->dev, cmd->size,
4431 cmd->va, cmd->dma);
6b7c5b94
SP
4432}
4433
4434static int be_stats_init(struct be_adapter *adapter)
4435{
3abcdeda 4436 struct be_dma_mem *cmd = &adapter->stats_cmd;
6b7c5b94 4437
ca34fe38
SP
4438 if (lancer_chip(adapter))
4439 cmd->size = sizeof(struct lancer_cmd_req_pport_stats);
4440 else if (BE2_chip(adapter))
89a88ab8 4441 cmd->size = sizeof(struct be_cmd_req_get_stats_v0);
61000861 4442 else if (BE3_chip(adapter))
ca34fe38 4443 cmd->size = sizeof(struct be_cmd_req_get_stats_v1);
61000861
AK
4444 else
4445 /* ALL non-BE ASICs */
4446 cmd->size = sizeof(struct be_cmd_req_get_stats_v2);
ca34fe38 4447
ede23fa8
JP
4448 cmd->va = dma_zalloc_coherent(&adapter->pdev->dev, cmd->size, &cmd->dma,
4449 GFP_KERNEL);
6b7c5b94
SP
4450 if (cmd->va == NULL)
4451 return -1;
4452 return 0;
4453}
4454
3bc6b06c 4455static void be_remove(struct pci_dev *pdev)
6b7c5b94
SP
4456{
4457 struct be_adapter *adapter = pci_get_drvdata(pdev);
8d56ff11 4458
6b7c5b94
SP
4459 if (!adapter)
4460 return;
4461
045508a8 4462 be_roce_dev_remove(adapter);
8cef7a78 4463 be_intr_set(adapter, false);
045508a8 4464
f67ef7ba
PR
4465 cancel_delayed_work_sync(&adapter->func_recovery_work);
4466
6b7c5b94
SP
4467 unregister_netdev(adapter->netdev);
4468
5fb379ee
SP
4469 be_clear(adapter);
4470
bf99e50d
PR
4471 /* tell fw we're done with firing cmds */
4472 be_cmd_fw_clean(adapter);
4473
6b7c5b94
SP
4474 be_stats_cleanup(adapter);
4475
4476 be_ctrl_cleanup(adapter);
4477
d6b6d987
SP
4478 pci_disable_pcie_error_reporting(pdev);
4479
6b7c5b94
SP
4480 pci_release_regions(pdev);
4481 pci_disable_device(pdev);
4482
4483 free_netdev(adapter->netdev);
4484}
4485
39f1d94d 4486static int be_get_initial_config(struct be_adapter *adapter)
6b7c5b94 4487{
baaa08d1 4488 int status, level;
6b7c5b94 4489
9e1453c5
AK
4490 status = be_cmd_get_cntl_attributes(adapter);
4491 if (status)
4492 return status;
4493
7aeb2156
PR
4494 /* Must be a power of 2 or else MODULO will BUG_ON */
4495 adapter->be_get_temp_freq = 64;
4496
baaa08d1
VV
4497 if (BEx_chip(adapter)) {
4498 level = be_cmd_get_fw_log_level(adapter);
4499 adapter->msg_enable =
4500 level <= FW_LOG_LEVEL_DEFAULT ? NETIF_MSG_HW : 0;
4501 }
941a77d5 4502
92bf14ab 4503 adapter->cfg_num_qs = netif_get_num_default_rss_queues();
2243e2e9 4504 return 0;
6b7c5b94
SP
4505}
4506
f67ef7ba 4507static int lancer_recover_func(struct be_adapter *adapter)
d8110f62 4508{
01e5b2c4 4509 struct device *dev = &adapter->pdev->dev;
d8110f62 4510 int status;
d8110f62 4511
f67ef7ba
PR
4512 status = lancer_test_and_set_rdy_state(adapter);
4513 if (status)
4514 goto err;
d8110f62 4515
f67ef7ba
PR
4516 if (netif_running(adapter->netdev))
4517 be_close(adapter->netdev);
d8110f62 4518
f67ef7ba
PR
4519 be_clear(adapter);
4520
01e5b2c4 4521 be_clear_all_error(adapter);
f67ef7ba
PR
4522
4523 status = be_setup(adapter);
4524 if (status)
4525 goto err;
d8110f62 4526
f67ef7ba
PR
4527 if (netif_running(adapter->netdev)) {
4528 status = be_open(adapter->netdev);
d8110f62
PR
4529 if (status)
4530 goto err;
f67ef7ba 4531 }
d8110f62 4532
4bebb56a 4533 dev_err(dev, "Adapter recovery successful\n");
f67ef7ba
PR
4534 return 0;
4535err:
01e5b2c4
SK
4536 if (status == -EAGAIN)
4537 dev_err(dev, "Waiting for resource provisioning\n");
4538 else
4bebb56a 4539 dev_err(dev, "Adapter recovery failed\n");
d8110f62 4540
f67ef7ba
PR
4541 return status;
4542}
4543
4544static void be_func_recovery_task(struct work_struct *work)
4545{
4546 struct be_adapter *adapter =
4547 container_of(work, struct be_adapter, func_recovery_work.work);
01e5b2c4 4548 int status = 0;
d8110f62 4549
f67ef7ba 4550 be_detect_error(adapter);
d8110f62 4551
f67ef7ba 4552 if (adapter->hw_error && lancer_chip(adapter)) {
d8110f62 4553
f67ef7ba
PR
4554 rtnl_lock();
4555 netif_device_detach(adapter->netdev);
4556 rtnl_unlock();
d8110f62 4557
f67ef7ba 4558 status = lancer_recover_func(adapter);
f67ef7ba
PR
4559 if (!status)
4560 netif_device_attach(adapter->netdev);
d8110f62 4561 }
f67ef7ba 4562
01e5b2c4
SK
4563 /* In Lancer, for all errors other than provisioning error (-EAGAIN),
4564 * no need to attempt further recovery.
4565 */
4566 if (!status || status == -EAGAIN)
4567 schedule_delayed_work(&adapter->func_recovery_work,
4568 msecs_to_jiffies(1000));
d8110f62
PR
4569}
4570
4571static void be_worker(struct work_struct *work)
4572{
4573 struct be_adapter *adapter =
4574 container_of(work, struct be_adapter, work.work);
4575 struct be_rx_obj *rxo;
4576 int i;
4577
d8110f62
PR
4578 /* when interrupts are not yet enabled, just reap any pending
4579 * mcc completions */
4580 if (!netif_running(adapter->netdev)) {
072a9c48 4581 local_bh_disable();
10ef9ab4 4582 be_process_mcc(adapter);
072a9c48 4583 local_bh_enable();
d8110f62
PR
4584 goto reschedule;
4585 }
4586
4587 if (!adapter->stats_cmd_sent) {
4588 if (lancer_chip(adapter))
4589 lancer_cmd_get_pport_stats(adapter,
4590 &adapter->stats_cmd);
4591 else
4592 be_cmd_get_stats(adapter, &adapter->stats_cmd);
4593 }
4594
d696b5e2
VV
4595 if (be_physfn(adapter) &&
4596 MODULO(adapter->work_counter, adapter->be_get_temp_freq) == 0)
7aeb2156
PR
4597 be_cmd_get_die_temperature(adapter);
4598
d8110f62 4599 for_all_rx_queues(adapter, rxo, i) {
6384a4d0
SP
4600 /* Replenish RX-queues starved due to memory
4601 * allocation failures.
4602 */
4603 if (rxo->rx_post_starved)
d8110f62 4604 be_post_rx_frags(rxo, GFP_KERNEL);
d8110f62
PR
4605 }
4606
2632bafd 4607 be_eqd_update(adapter);
10ef9ab4 4608
d8110f62
PR
4609reschedule:
4610 adapter->work_counter++;
4611 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
4612}
4613
257a3feb 4614/* If any VFs are already enabled don't FLR the PF */
39f1d94d
SP
4615static bool be_reset_required(struct be_adapter *adapter)
4616{
257a3feb 4617 return pci_num_vf(adapter->pdev) ? false : true;
39f1d94d
SP
4618}
4619
d379142b
SP
4620static char *mc_name(struct be_adapter *adapter)
4621{
f93f160b
VV
4622 char *str = ""; /* default */
4623
4624 switch (adapter->mc_type) {
4625 case UMC:
4626 str = "UMC";
4627 break;
4628 case FLEX10:
4629 str = "FLEX10";
4630 break;
4631 case vNIC1:
4632 str = "vNIC-1";
4633 break;
4634 case nPAR:
4635 str = "nPAR";
4636 break;
4637 case UFP:
4638 str = "UFP";
4639 break;
4640 case vNIC2:
4641 str = "vNIC-2";
4642 break;
4643 default:
4644 str = "";
4645 }
4646
4647 return str;
d379142b
SP
4648}
4649
4650static inline char *func_name(struct be_adapter *adapter)
4651{
4652 return be_physfn(adapter) ? "PF" : "VF";
4653}
4654
1dd06ae8 4655static int be_probe(struct pci_dev *pdev, const struct pci_device_id *pdev_id)
6b7c5b94
SP
4656{
4657 int status = 0;
4658 struct be_adapter *adapter;
4659 struct net_device *netdev;
b4e32a71 4660 char port_name;
6b7c5b94
SP
4661
4662 status = pci_enable_device(pdev);
4663 if (status)
4664 goto do_none;
4665
4666 status = pci_request_regions(pdev, DRV_NAME);
4667 if (status)
4668 goto disable_dev;
4669 pci_set_master(pdev);
4670
7f640062 4671 netdev = alloc_etherdev_mqs(sizeof(*adapter), MAX_TX_QS, MAX_RX_QS);
6b7c5b94
SP
4672 if (netdev == NULL) {
4673 status = -ENOMEM;
4674 goto rel_reg;
4675 }
4676 adapter = netdev_priv(netdev);
4677 adapter->pdev = pdev;
4678 pci_set_drvdata(pdev, adapter);
4679 adapter->netdev = netdev;
2243e2e9 4680 SET_NETDEV_DEV(netdev, &pdev->dev);
6b7c5b94 4681
4c15c243 4682 status = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
6b7c5b94
SP
4683 if (!status) {
4684 netdev->features |= NETIF_F_HIGHDMA;
4685 } else {
4c15c243 4686 status = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
6b7c5b94
SP
4687 if (status) {
4688 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
4689 goto free_netdev;
4690 }
4691 }
4692
ea58c180
AK
4693 if (be_physfn(adapter)) {
4694 status = pci_enable_pcie_error_reporting(pdev);
4695 if (!status)
4696 dev_info(&pdev->dev, "PCIe error reporting enabled\n");
4697 }
d6b6d987 4698
6b7c5b94
SP
4699 status = be_ctrl_init(adapter);
4700 if (status)
39f1d94d 4701 goto free_netdev;
6b7c5b94 4702
2243e2e9 4703 /* sync up with fw's ready state */
ba343c77 4704 if (be_physfn(adapter)) {
bf99e50d 4705 status = be_fw_wait_ready(adapter);
ba343c77
SB
4706 if (status)
4707 goto ctrl_clean;
ba343c77 4708 }
6b7c5b94 4709
39f1d94d
SP
4710 if (be_reset_required(adapter)) {
4711 status = be_cmd_reset_function(adapter);
4712 if (status)
4713 goto ctrl_clean;
556ae191 4714
2d177be8
KA
4715 /* Wait for interrupts to quiesce after an FLR */
4716 msleep(100);
4717 }
8cef7a78
SK
4718
4719 /* Allow interrupts for other ULPs running on NIC function */
4720 be_intr_set(adapter, true);
10ef9ab4 4721
2d177be8
KA
4722 /* tell fw we're ready to fire cmds */
4723 status = be_cmd_fw_init(adapter);
4724 if (status)
4725 goto ctrl_clean;
4726
2243e2e9
SP
4727 status = be_stats_init(adapter);
4728 if (status)
4729 goto ctrl_clean;
4730
39f1d94d 4731 status = be_get_initial_config(adapter);
6b7c5b94
SP
4732 if (status)
4733 goto stats_clean;
6b7c5b94
SP
4734
4735 INIT_DELAYED_WORK(&adapter->work, be_worker);
f67ef7ba 4736 INIT_DELAYED_WORK(&adapter->func_recovery_work, be_func_recovery_task);
a54769f5 4737 adapter->rx_fc = adapter->tx_fc = true;
6b7c5b94 4738
5fb379ee
SP
4739 status = be_setup(adapter);
4740 if (status)
55f5c3c5 4741 goto stats_clean;
2243e2e9 4742
3abcdeda 4743 be_netdev_init(netdev);
6b7c5b94
SP
4744 status = register_netdev(netdev);
4745 if (status != 0)
5fb379ee 4746 goto unsetup;
6b7c5b94 4747
045508a8
PP
4748 be_roce_dev_add(adapter);
4749
f67ef7ba
PR
4750 schedule_delayed_work(&adapter->func_recovery_work,
4751 msecs_to_jiffies(1000));
b4e32a71
PR
4752
4753 be_cmd_query_port_name(adapter, &port_name);
4754
d379142b
SP
4755 dev_info(&pdev->dev, "%s: %s %s port %c\n", nic_name(pdev),
4756 func_name(adapter), mc_name(adapter), port_name);
34b1ef04 4757
6b7c5b94
SP
4758 return 0;
4759
5fb379ee
SP
4760unsetup:
4761 be_clear(adapter);
6b7c5b94
SP
4762stats_clean:
4763 be_stats_cleanup(adapter);
4764ctrl_clean:
4765 be_ctrl_cleanup(adapter);
f9449ab7 4766free_netdev:
fe6d2a38 4767 free_netdev(netdev);
6b7c5b94
SP
4768rel_reg:
4769 pci_release_regions(pdev);
4770disable_dev:
4771 pci_disable_device(pdev);
4772do_none:
c4ca2374 4773 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
6b7c5b94
SP
4774 return status;
4775}
4776
4777static int be_suspend(struct pci_dev *pdev, pm_message_t state)
4778{
4779 struct be_adapter *adapter = pci_get_drvdata(pdev);
4780 struct net_device *netdev = adapter->netdev;
4781
76a9e08e 4782 if (adapter->wol_en)
71d8d1b5
AK
4783 be_setup_wol(adapter, true);
4784
d4360d6f 4785 be_intr_set(adapter, false);
f67ef7ba
PR
4786 cancel_delayed_work_sync(&adapter->func_recovery_work);
4787
6b7c5b94
SP
4788 netif_device_detach(netdev);
4789 if (netif_running(netdev)) {
4790 rtnl_lock();
4791 be_close(netdev);
4792 rtnl_unlock();
4793 }
9b0365f1 4794 be_clear(adapter);
6b7c5b94
SP
4795
4796 pci_save_state(pdev);
4797 pci_disable_device(pdev);
4798 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4799 return 0;
4800}
4801
4802static int be_resume(struct pci_dev *pdev)
4803{
4804 int status = 0;
4805 struct be_adapter *adapter = pci_get_drvdata(pdev);
4806 struct net_device *netdev = adapter->netdev;
4807
4808 netif_device_detach(netdev);
4809
4810 status = pci_enable_device(pdev);
4811 if (status)
4812 return status;
4813
1ca01512 4814 pci_set_power_state(pdev, PCI_D0);
6b7c5b94
SP
4815 pci_restore_state(pdev);
4816
dd5746bf
SB
4817 status = be_fw_wait_ready(adapter);
4818 if (status)
4819 return status;
4820
d4360d6f 4821 be_intr_set(adapter, true);
2243e2e9
SP
4822 /* tell fw we're ready to fire cmds */
4823 status = be_cmd_fw_init(adapter);
4824 if (status)
4825 return status;
4826
9b0365f1 4827 be_setup(adapter);
6b7c5b94
SP
4828 if (netif_running(netdev)) {
4829 rtnl_lock();
4830 be_open(netdev);
4831 rtnl_unlock();
4832 }
f67ef7ba
PR
4833
4834 schedule_delayed_work(&adapter->func_recovery_work,
4835 msecs_to_jiffies(1000));
6b7c5b94 4836 netif_device_attach(netdev);
71d8d1b5 4837
76a9e08e 4838 if (adapter->wol_en)
71d8d1b5 4839 be_setup_wol(adapter, false);
a4ca055f 4840
6b7c5b94
SP
4841 return 0;
4842}
4843
82456b03
SP
4844/*
4845 * An FLR will stop BE from DMAing any data.
4846 */
4847static void be_shutdown(struct pci_dev *pdev)
4848{
4849 struct be_adapter *adapter = pci_get_drvdata(pdev);
82456b03 4850
2d5d4154
AK
4851 if (!adapter)
4852 return;
82456b03 4853
0f4a6828 4854 cancel_delayed_work_sync(&adapter->work);
f67ef7ba 4855 cancel_delayed_work_sync(&adapter->func_recovery_work);
a4ca055f 4856
2d5d4154 4857 netif_device_detach(adapter->netdev);
82456b03 4858
57841869
AK
4859 be_cmd_reset_function(adapter);
4860
82456b03 4861 pci_disable_device(pdev);
82456b03
SP
4862}
4863
cf588477
SP
4864static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
4865 pci_channel_state_t state)
4866{
4867 struct be_adapter *adapter = pci_get_drvdata(pdev);
4868 struct net_device *netdev = adapter->netdev;
4869
4870 dev_err(&adapter->pdev->dev, "EEH error detected\n");
4871
01e5b2c4
SK
4872 if (!adapter->eeh_error) {
4873 adapter->eeh_error = true;
cf588477 4874
01e5b2c4 4875 cancel_delayed_work_sync(&adapter->func_recovery_work);
cf588477 4876
cf588477 4877 rtnl_lock();
01e5b2c4
SK
4878 netif_device_detach(netdev);
4879 if (netif_running(netdev))
4880 be_close(netdev);
cf588477 4881 rtnl_unlock();
01e5b2c4
SK
4882
4883 be_clear(adapter);
cf588477 4884 }
cf588477
SP
4885
4886 if (state == pci_channel_io_perm_failure)
4887 return PCI_ERS_RESULT_DISCONNECT;
4888
4889 pci_disable_device(pdev);
4890
eeb7fc7b
SK
4891 /* The error could cause the FW to trigger a flash debug dump.
4892 * Resetting the card while flash dump is in progress
c8a54163
PR
4893 * can cause it not to recover; wait for it to finish.
4894 * Wait only for first function as it is needed only once per
4895 * adapter.
eeb7fc7b 4896 */
c8a54163
PR
4897 if (pdev->devfn == 0)
4898 ssleep(30);
4899
cf588477
SP
4900 return PCI_ERS_RESULT_NEED_RESET;
4901}
4902
4903static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
4904{
4905 struct be_adapter *adapter = pci_get_drvdata(pdev);
4906 int status;
4907
4908 dev_info(&adapter->pdev->dev, "EEH reset\n");
cf588477
SP
4909
4910 status = pci_enable_device(pdev);
4911 if (status)
4912 return PCI_ERS_RESULT_DISCONNECT;
4913
4914 pci_set_master(pdev);
1ca01512 4915 pci_set_power_state(pdev, PCI_D0);
cf588477
SP
4916 pci_restore_state(pdev);
4917
4918 /* Check if card is ok and fw is ready */
c5b3ad4c
SP
4919 dev_info(&adapter->pdev->dev,
4920 "Waiting for FW to be ready after EEH reset\n");
bf99e50d 4921 status = be_fw_wait_ready(adapter);
cf588477
SP
4922 if (status)
4923 return PCI_ERS_RESULT_DISCONNECT;
4924
d6b6d987 4925 pci_cleanup_aer_uncorrect_error_status(pdev);
01e5b2c4 4926 be_clear_all_error(adapter);
cf588477
SP
4927 return PCI_ERS_RESULT_RECOVERED;
4928}
4929
4930static void be_eeh_resume(struct pci_dev *pdev)
4931{
4932 int status = 0;
4933 struct be_adapter *adapter = pci_get_drvdata(pdev);
4934 struct net_device *netdev = adapter->netdev;
4935
4936 dev_info(&adapter->pdev->dev, "EEH resume\n");
4937
4938 pci_save_state(pdev);
4939
2d177be8 4940 status = be_cmd_reset_function(adapter);
cf588477
SP
4941 if (status)
4942 goto err;
4943
2d177be8
KA
4944 /* tell fw we're ready to fire cmds */
4945 status = be_cmd_fw_init(adapter);
bf99e50d
PR
4946 if (status)
4947 goto err;
4948
cf588477
SP
4949 status = be_setup(adapter);
4950 if (status)
4951 goto err;
4952
4953 if (netif_running(netdev)) {
4954 status = be_open(netdev);
4955 if (status)
4956 goto err;
4957 }
f67ef7ba
PR
4958
4959 schedule_delayed_work(&adapter->func_recovery_work,
4960 msecs_to_jiffies(1000));
cf588477
SP
4961 netif_device_attach(netdev);
4962 return;
4963err:
4964 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
cf588477
SP
4965}
4966
3646f0e5 4967static const struct pci_error_handlers be_eeh_handlers = {
cf588477
SP
4968 .error_detected = be_eeh_err_detected,
4969 .slot_reset = be_eeh_reset,
4970 .resume = be_eeh_resume,
4971};
4972
6b7c5b94
SP
4973static struct pci_driver be_driver = {
4974 .name = DRV_NAME,
4975 .id_table = be_dev_ids,
4976 .probe = be_probe,
4977 .remove = be_remove,
4978 .suspend = be_suspend,
cf588477 4979 .resume = be_resume,
82456b03 4980 .shutdown = be_shutdown,
cf588477 4981 .err_handler = &be_eeh_handlers
6b7c5b94
SP
4982};
4983
4984static int __init be_init_module(void)
4985{
8e95a202
JP
4986 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
4987 rx_frag_size != 2048) {
6b7c5b94
SP
4988 printk(KERN_WARNING DRV_NAME
4989 " : Module param rx_frag_size must be 2048/4096/8192."
4990 " Using 2048\n");
4991 rx_frag_size = 2048;
4992 }
6b7c5b94
SP
4993
4994 return pci_register_driver(&be_driver);
4995}
4996module_init(be_init_module);
4997
4998static void __exit be_exit_module(void)
4999{
5000 pci_unregister_driver(&be_driver);
5001}
5002module_exit(be_exit_module);