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