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