2 * Copyright (C) 2005 - 2013 Emulex
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.
10 * Contact Information:
11 * linux-drivers@emulex.com
15 * Costa Mesa, CA 92626
18 #include <linux/prefetch.h>
19 #include <linux/module.h>
22 #include <asm/div64.h>
23 #include <linux/aer.h>
24 #include <linux/if_bridge.h>
26 MODULE_VERSION(DRV_VER);
27 MODULE_DEVICE_TABLE(pci, be_dev_ids);
28 MODULE_DESCRIPTION(DRV_DESC " " DRV_VER);
29 MODULE_AUTHOR("Emulex Corporation");
30 MODULE_LICENSE("GPL");
32 static unsigned int num_vfs;
33 module_param(num_vfs, uint, S_IRUGO);
34 MODULE_PARM_DESC(num_vfs, "Number of PCI VFs to initialize");
36 static ushort rx_frag_size = 2048;
37 module_param(rx_frag_size, ushort, S_IRUGO);
38 MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data.");
40 static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
41 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
42 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
43 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
44 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
45 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID3)},
46 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID4)},
47 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID5)},
48 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID6)},
51 MODULE_DEVICE_TABLE(pci, be_dev_ids);
52 /* UE Status Low CSR */
53 static const char * const ue_status_low_desc[] = {
87 /* UE Status High CSR */
88 static const char * const ue_status_hi_desc[] = {
123 /* Is BE in a multi-channel mode */
124 static inline bool be_is_mc(struct be_adapter *adapter) {
125 return (adapter->function_mode & FLEX10_MODE ||
126 adapter->function_mode & VNIC_MODE ||
127 adapter->function_mode & UMC_ENABLED);
130 static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
132 struct be_dma_mem *mem = &q->dma_mem;
134 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
140 static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
141 u16 len, u16 entry_size)
143 struct be_dma_mem *mem = &q->dma_mem;
145 memset(q, 0, sizeof(*q));
147 q->entry_size = entry_size;
148 mem->size = len * entry_size;
149 mem->va = dma_zalloc_coherent(&adapter->pdev->dev, mem->size, &mem->dma,
156 static void be_reg_intr_set(struct be_adapter *adapter, bool enable)
160 pci_read_config_dword(adapter->pdev, PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET,
162 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
164 if (!enabled && enable)
165 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
166 else if (enabled && !enable)
167 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
171 pci_write_config_dword(adapter->pdev,
172 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET, reg);
175 static void be_intr_set(struct be_adapter *adapter, bool enable)
179 /* On lancer interrupts can't be controlled via this register */
180 if (lancer_chip(adapter))
183 if (adapter->eeh_error)
186 status = be_cmd_intr_set(adapter, enable);
188 be_reg_intr_set(adapter, enable);
191 static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
194 val |= qid & DB_RQ_RING_ID_MASK;
195 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
198 iowrite32(val, adapter->db + DB_RQ_OFFSET);
201 static void be_txq_notify(struct be_adapter *adapter, struct be_tx_obj *txo,
205 val |= txo->q.id & DB_TXULP_RING_ID_MASK;
206 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
209 iowrite32(val, adapter->db + txo->db_offset);
212 static void be_eq_notify(struct be_adapter *adapter, u16 qid,
213 bool arm, bool clear_int, u16 num_popped)
216 val |= qid & DB_EQ_RING_ID_MASK;
217 val |= ((qid & DB_EQ_RING_ID_EXT_MASK) <<
218 DB_EQ_RING_ID_EXT_MASK_SHIFT);
220 if (adapter->eeh_error)
224 val |= 1 << DB_EQ_REARM_SHIFT;
226 val |= 1 << DB_EQ_CLR_SHIFT;
227 val |= 1 << DB_EQ_EVNT_SHIFT;
228 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
229 iowrite32(val, adapter->db + DB_EQ_OFFSET);
232 void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
235 val |= qid & DB_CQ_RING_ID_MASK;
236 val |= ((qid & DB_CQ_RING_ID_EXT_MASK) <<
237 DB_CQ_RING_ID_EXT_MASK_SHIFT);
239 if (adapter->eeh_error)
243 val |= 1 << DB_CQ_REARM_SHIFT;
244 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
245 iowrite32(val, adapter->db + DB_CQ_OFFSET);
248 static int be_mac_addr_set(struct net_device *netdev, void *p)
250 struct be_adapter *adapter = netdev_priv(netdev);
251 struct device *dev = &adapter->pdev->dev;
252 struct sockaddr *addr = p;
255 u32 old_pmac_id = adapter->pmac_id[0], curr_pmac_id = 0;
257 if (!is_valid_ether_addr(addr->sa_data))
258 return -EADDRNOTAVAIL;
260 /* The PMAC_ADD cmd may fail if the VF doesn't have FILTMGMT
261 * privilege or if PF did not provision the new MAC address.
262 * On BE3, this cmd will always fail if the VF doesn't have the
263 * FILTMGMT privilege. This failure is OK, only if the PF programmed
264 * the MAC for the VF.
266 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
267 adapter->if_handle, &adapter->pmac_id[0], 0);
269 curr_pmac_id = adapter->pmac_id[0];
271 /* Delete the old programmed MAC. This call may fail if the
272 * old MAC was already deleted by the PF driver.
274 if (adapter->pmac_id[0] != old_pmac_id)
275 be_cmd_pmac_del(adapter, adapter->if_handle,
279 /* Decide if the new MAC is successfully activated only after
282 status = be_cmd_get_active_mac(adapter, curr_pmac_id, mac);
286 /* The MAC change did not happen, either due to lack of privilege
287 * or PF didn't pre-provision.
289 if (memcmp(addr->sa_data, mac, ETH_ALEN)) {
294 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
295 dev_info(dev, "MAC address changed to %pM\n", mac);
298 dev_warn(dev, "MAC address change to %pM failed\n", addr->sa_data);
302 /* BE2 supports only v0 cmd */
303 static void *hw_stats_from_cmd(struct be_adapter *adapter)
305 if (BE2_chip(adapter)) {
306 struct be_cmd_resp_get_stats_v0 *cmd = adapter->stats_cmd.va;
308 return &cmd->hw_stats;
309 } else if (BE3_chip(adapter)) {
310 struct be_cmd_resp_get_stats_v1 *cmd = adapter->stats_cmd.va;
312 return &cmd->hw_stats;
314 struct be_cmd_resp_get_stats_v2 *cmd = adapter->stats_cmd.va;
316 return &cmd->hw_stats;
320 /* BE2 supports only v0 cmd */
321 static void *be_erx_stats_from_cmd(struct be_adapter *adapter)
323 if (BE2_chip(adapter)) {
324 struct be_hw_stats_v0 *hw_stats = hw_stats_from_cmd(adapter);
326 return &hw_stats->erx;
327 } else if (BE3_chip(adapter)) {
328 struct be_hw_stats_v1 *hw_stats = hw_stats_from_cmd(adapter);
330 return &hw_stats->erx;
332 struct be_hw_stats_v2 *hw_stats = hw_stats_from_cmd(adapter);
334 return &hw_stats->erx;
338 static void populate_be_v0_stats(struct be_adapter *adapter)
340 struct be_hw_stats_v0 *hw_stats = hw_stats_from_cmd(adapter);
341 struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
342 struct be_rxf_stats_v0 *rxf_stats = &hw_stats->rxf;
343 struct be_port_rxf_stats_v0 *port_stats =
344 &rxf_stats->port[adapter->port_num];
345 struct be_drv_stats *drvs = &adapter->drv_stats;
347 be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
348 drvs->rx_pause_frames = port_stats->rx_pause_frames;
349 drvs->rx_crc_errors = port_stats->rx_crc_errors;
350 drvs->rx_control_frames = port_stats->rx_control_frames;
351 drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
352 drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
353 drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
354 drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
355 drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
356 drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
357 drvs->rxpp_fifo_overflow_drop = port_stats->rx_fifo_overflow;
358 drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
359 drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
360 drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
361 drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
362 drvs->rx_input_fifo_overflow_drop = port_stats->rx_input_fifo_overflow;
363 drvs->rx_dropped_header_too_small =
364 port_stats->rx_dropped_header_too_small;
365 drvs->rx_address_filtered =
366 port_stats->rx_address_filtered +
367 port_stats->rx_vlan_filtered;
368 drvs->rx_alignment_symbol_errors =
369 port_stats->rx_alignment_symbol_errors;
371 drvs->tx_pauseframes = port_stats->tx_pauseframes;
372 drvs->tx_controlframes = port_stats->tx_controlframes;
374 if (adapter->port_num)
375 drvs->jabber_events = rxf_stats->port1_jabber_events;
377 drvs->jabber_events = rxf_stats->port0_jabber_events;
378 drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
379 drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
380 drvs->forwarded_packets = rxf_stats->forwarded_packets;
381 drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
382 drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
383 drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
384 adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
387 static void populate_be_v1_stats(struct be_adapter *adapter)
389 struct be_hw_stats_v1 *hw_stats = hw_stats_from_cmd(adapter);
390 struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
391 struct be_rxf_stats_v1 *rxf_stats = &hw_stats->rxf;
392 struct be_port_rxf_stats_v1 *port_stats =
393 &rxf_stats->port[adapter->port_num];
394 struct be_drv_stats *drvs = &adapter->drv_stats;
396 be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
397 drvs->pmem_fifo_overflow_drop = port_stats->pmem_fifo_overflow_drop;
398 drvs->rx_priority_pause_frames = port_stats->rx_priority_pause_frames;
399 drvs->rx_pause_frames = port_stats->rx_pause_frames;
400 drvs->rx_crc_errors = port_stats->rx_crc_errors;
401 drvs->rx_control_frames = port_stats->rx_control_frames;
402 drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
403 drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
404 drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
405 drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
406 drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
407 drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
408 drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
409 drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
410 drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
411 drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
412 drvs->rx_dropped_header_too_small =
413 port_stats->rx_dropped_header_too_small;
414 drvs->rx_input_fifo_overflow_drop =
415 port_stats->rx_input_fifo_overflow_drop;
416 drvs->rx_address_filtered = port_stats->rx_address_filtered;
417 drvs->rx_alignment_symbol_errors =
418 port_stats->rx_alignment_symbol_errors;
419 drvs->rxpp_fifo_overflow_drop = port_stats->rxpp_fifo_overflow_drop;
420 drvs->tx_pauseframes = port_stats->tx_pauseframes;
421 drvs->tx_controlframes = port_stats->tx_controlframes;
422 drvs->tx_priority_pauseframes = port_stats->tx_priority_pauseframes;
423 drvs->jabber_events = port_stats->jabber_events;
424 drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
425 drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
426 drvs->forwarded_packets = rxf_stats->forwarded_packets;
427 drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
428 drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
429 drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
430 adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
433 static void populate_be_v2_stats(struct be_adapter *adapter)
435 struct be_hw_stats_v2 *hw_stats = hw_stats_from_cmd(adapter);
436 struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
437 struct be_rxf_stats_v2 *rxf_stats = &hw_stats->rxf;
438 struct be_port_rxf_stats_v2 *port_stats =
439 &rxf_stats->port[adapter->port_num];
440 struct be_drv_stats *drvs = &adapter->drv_stats;
442 be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
443 drvs->pmem_fifo_overflow_drop = port_stats->pmem_fifo_overflow_drop;
444 drvs->rx_priority_pause_frames = port_stats->rx_priority_pause_frames;
445 drvs->rx_pause_frames = port_stats->rx_pause_frames;
446 drvs->rx_crc_errors = port_stats->rx_crc_errors;
447 drvs->rx_control_frames = port_stats->rx_control_frames;
448 drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
449 drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
450 drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
451 drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
452 drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
453 drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
454 drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
455 drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
456 drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
457 drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
458 drvs->rx_dropped_header_too_small =
459 port_stats->rx_dropped_header_too_small;
460 drvs->rx_input_fifo_overflow_drop =
461 port_stats->rx_input_fifo_overflow_drop;
462 drvs->rx_address_filtered = port_stats->rx_address_filtered;
463 drvs->rx_alignment_symbol_errors =
464 port_stats->rx_alignment_symbol_errors;
465 drvs->rxpp_fifo_overflow_drop = port_stats->rxpp_fifo_overflow_drop;
466 drvs->tx_pauseframes = port_stats->tx_pauseframes;
467 drvs->tx_controlframes = port_stats->tx_controlframes;
468 drvs->tx_priority_pauseframes = port_stats->tx_priority_pauseframes;
469 drvs->jabber_events = port_stats->jabber_events;
470 drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
471 drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
472 drvs->forwarded_packets = rxf_stats->forwarded_packets;
473 drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
474 drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
475 drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
476 adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
477 if (be_roce_supported(adapter)) {
478 drvs->rx_roce_bytes_lsd = port_stats->roce_bytes_received_lsd;
479 drvs->rx_roce_bytes_msd = port_stats->roce_bytes_received_msd;
480 drvs->rx_roce_frames = port_stats->roce_frames_received;
481 drvs->roce_drops_crc = port_stats->roce_drops_crc;
482 drvs->roce_drops_payload_len =
483 port_stats->roce_drops_payload_len;
487 static void populate_lancer_stats(struct be_adapter *adapter)
490 struct be_drv_stats *drvs = &adapter->drv_stats;
491 struct lancer_pport_stats *pport_stats =
492 pport_stats_from_cmd(adapter);
494 be_dws_le_to_cpu(pport_stats, sizeof(*pport_stats));
495 drvs->rx_pause_frames = pport_stats->rx_pause_frames_lo;
496 drvs->rx_crc_errors = pport_stats->rx_crc_errors_lo;
497 drvs->rx_control_frames = pport_stats->rx_control_frames_lo;
498 drvs->rx_in_range_errors = pport_stats->rx_in_range_errors;
499 drvs->rx_frame_too_long = pport_stats->rx_frames_too_long_lo;
500 drvs->rx_dropped_runt = pport_stats->rx_dropped_runt;
501 drvs->rx_ip_checksum_errs = pport_stats->rx_ip_checksum_errors;
502 drvs->rx_tcp_checksum_errs = pport_stats->rx_tcp_checksum_errors;
503 drvs->rx_udp_checksum_errs = pport_stats->rx_udp_checksum_errors;
504 drvs->rx_dropped_tcp_length =
505 pport_stats->rx_dropped_invalid_tcp_length;
506 drvs->rx_dropped_too_small = pport_stats->rx_dropped_too_small;
507 drvs->rx_dropped_too_short = pport_stats->rx_dropped_too_short;
508 drvs->rx_out_range_errors = pport_stats->rx_out_of_range_errors;
509 drvs->rx_dropped_header_too_small =
510 pport_stats->rx_dropped_header_too_small;
511 drvs->rx_input_fifo_overflow_drop = pport_stats->rx_fifo_overflow;
512 drvs->rx_address_filtered =
513 pport_stats->rx_address_filtered +
514 pport_stats->rx_vlan_filtered;
515 drvs->rx_alignment_symbol_errors = pport_stats->rx_symbol_errors_lo;
516 drvs->rxpp_fifo_overflow_drop = pport_stats->rx_fifo_overflow;
517 drvs->tx_pauseframes = pport_stats->tx_pause_frames_lo;
518 drvs->tx_controlframes = pport_stats->tx_control_frames_lo;
519 drvs->jabber_events = pport_stats->rx_jabbers;
520 drvs->forwarded_packets = pport_stats->num_forwards_lo;
521 drvs->rx_drops_mtu = pport_stats->rx_drops_mtu_lo;
522 drvs->rx_drops_too_many_frags =
523 pport_stats->rx_drops_too_many_frags_lo;
526 static void accumulate_16bit_val(u32 *acc, u16 val)
528 #define lo(x) (x & 0xFFFF)
529 #define hi(x) (x & 0xFFFF0000)
530 bool wrapped = val < lo(*acc);
531 u32 newacc = hi(*acc) + val;
535 ACCESS_ONCE(*acc) = newacc;
538 static void populate_erx_stats(struct be_adapter *adapter,
539 struct be_rx_obj *rxo,
542 if (!BEx_chip(adapter))
543 rx_stats(rxo)->rx_drops_no_frags = erx_stat;
545 /* below erx HW counter can actually wrap around after
546 * 65535. Driver accumulates a 32-bit value
548 accumulate_16bit_val(&rx_stats(rxo)->rx_drops_no_frags,
552 void be_parse_stats(struct be_adapter *adapter)
554 struct be_erx_stats_v2 *erx = be_erx_stats_from_cmd(adapter);
555 struct be_rx_obj *rxo;
559 if (lancer_chip(adapter)) {
560 populate_lancer_stats(adapter);
562 if (BE2_chip(adapter))
563 populate_be_v0_stats(adapter);
564 else if (BE3_chip(adapter))
566 populate_be_v1_stats(adapter);
568 populate_be_v2_stats(adapter);
570 /* erx_v2 is longer than v0, v1. use v2 for v0, v1 access */
571 for_all_rx_queues(adapter, rxo, i) {
572 erx_stat = erx->rx_drops_no_fragments[rxo->q.id];
573 populate_erx_stats(adapter, rxo, erx_stat);
578 static struct rtnl_link_stats64 *be_get_stats64(struct net_device *netdev,
579 struct rtnl_link_stats64 *stats)
581 struct be_adapter *adapter = netdev_priv(netdev);
582 struct be_drv_stats *drvs = &adapter->drv_stats;
583 struct be_rx_obj *rxo;
584 struct be_tx_obj *txo;
589 for_all_rx_queues(adapter, rxo, i) {
590 const struct be_rx_stats *rx_stats = rx_stats(rxo);
592 start = u64_stats_fetch_begin_bh(&rx_stats->sync);
593 pkts = rx_stats(rxo)->rx_pkts;
594 bytes = rx_stats(rxo)->rx_bytes;
595 } while (u64_stats_fetch_retry_bh(&rx_stats->sync, start));
596 stats->rx_packets += pkts;
597 stats->rx_bytes += bytes;
598 stats->multicast += rx_stats(rxo)->rx_mcast_pkts;
599 stats->rx_dropped += rx_stats(rxo)->rx_drops_no_skbs +
600 rx_stats(rxo)->rx_drops_no_frags;
603 for_all_tx_queues(adapter, txo, i) {
604 const struct be_tx_stats *tx_stats = tx_stats(txo);
606 start = u64_stats_fetch_begin_bh(&tx_stats->sync);
607 pkts = tx_stats(txo)->tx_pkts;
608 bytes = tx_stats(txo)->tx_bytes;
609 } while (u64_stats_fetch_retry_bh(&tx_stats->sync, start));
610 stats->tx_packets += pkts;
611 stats->tx_bytes += bytes;
614 /* bad pkts received */
615 stats->rx_errors = drvs->rx_crc_errors +
616 drvs->rx_alignment_symbol_errors +
617 drvs->rx_in_range_errors +
618 drvs->rx_out_range_errors +
619 drvs->rx_frame_too_long +
620 drvs->rx_dropped_too_small +
621 drvs->rx_dropped_too_short +
622 drvs->rx_dropped_header_too_small +
623 drvs->rx_dropped_tcp_length +
624 drvs->rx_dropped_runt;
626 /* detailed rx errors */
627 stats->rx_length_errors = drvs->rx_in_range_errors +
628 drvs->rx_out_range_errors +
629 drvs->rx_frame_too_long;
631 stats->rx_crc_errors = drvs->rx_crc_errors;
633 /* frame alignment errors */
634 stats->rx_frame_errors = drvs->rx_alignment_symbol_errors;
636 /* receiver fifo overrun */
637 /* drops_no_pbuf is no per i/f, it's per BE card */
638 stats->rx_fifo_errors = drvs->rxpp_fifo_overflow_drop +
639 drvs->rx_input_fifo_overflow_drop +
640 drvs->rx_drops_no_pbuf;
644 void be_link_status_update(struct be_adapter *adapter, u8 link_status)
646 struct net_device *netdev = adapter->netdev;
648 if (!(adapter->flags & BE_FLAGS_LINK_STATUS_INIT)) {
649 netif_carrier_off(netdev);
650 adapter->flags |= BE_FLAGS_LINK_STATUS_INIT;
653 if ((link_status & LINK_STATUS_MASK) == LINK_UP)
654 netif_carrier_on(netdev);
656 netif_carrier_off(netdev);
659 static void be_tx_stats_update(struct be_tx_obj *txo,
660 u32 wrb_cnt, u32 copied, u32 gso_segs, bool stopped)
662 struct be_tx_stats *stats = tx_stats(txo);
664 u64_stats_update_begin(&stats->sync);
666 stats->tx_wrbs += wrb_cnt;
667 stats->tx_bytes += copied;
668 stats->tx_pkts += (gso_segs ? gso_segs : 1);
671 u64_stats_update_end(&stats->sync);
674 /* Determine number of WRB entries needed to xmit data in an skb */
675 static u32 wrb_cnt_for_skb(struct be_adapter *adapter, struct sk_buff *skb,
678 int cnt = (skb->len > skb->data_len);
680 cnt += skb_shinfo(skb)->nr_frags;
682 /* to account for hdr wrb */
684 if (lancer_chip(adapter) || !(cnt & 1)) {
687 /* add a dummy to make it an even num */
691 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
695 static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
697 wrb->frag_pa_hi = upper_32_bits(addr);
698 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
699 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
703 static inline u16 be_get_tx_vlan_tag(struct be_adapter *adapter,
709 vlan_tag = vlan_tx_tag_get(skb);
710 vlan_prio = (vlan_tag & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
711 /* If vlan priority provided by OS is NOT in available bmap */
712 if (!(adapter->vlan_prio_bmap & (1 << vlan_prio)))
713 vlan_tag = (vlan_tag & ~VLAN_PRIO_MASK) |
714 adapter->recommended_prio;
719 static void wrb_fill_hdr(struct be_adapter *adapter, struct be_eth_hdr_wrb *hdr,
720 struct sk_buff *skb, u32 wrb_cnt, u32 len, bool skip_hw_vlan)
724 memset(hdr, 0, sizeof(*hdr));
726 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
728 if (skb_is_gso(skb)) {
729 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
730 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
731 hdr, skb_shinfo(skb)->gso_size);
732 if (skb_is_gso_v6(skb) && !lancer_chip(adapter))
733 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso6, hdr, 1);
734 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
736 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
737 else if (is_udp_pkt(skb))
738 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
741 if (vlan_tx_tag_present(skb)) {
742 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
743 vlan_tag = be_get_tx_vlan_tag(adapter, skb);
744 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag, hdr, vlan_tag);
747 /* To skip HW VLAN tagging: evt = 1, compl = 0 */
748 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, !skip_hw_vlan);
749 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
750 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
751 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
754 static void unmap_tx_frag(struct device *dev, struct be_eth_wrb *wrb,
759 be_dws_le_to_cpu(wrb, sizeof(*wrb));
761 dma = (u64)wrb->frag_pa_hi << 32 | (u64)wrb->frag_pa_lo;
764 dma_unmap_single(dev, dma, wrb->frag_len,
767 dma_unmap_page(dev, dma, wrb->frag_len, DMA_TO_DEVICE);
771 static int make_tx_wrbs(struct be_adapter *adapter, struct be_queue_info *txq,
772 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb,
777 struct device *dev = &adapter->pdev->dev;
778 struct sk_buff *first_skb = skb;
779 struct be_eth_wrb *wrb;
780 struct be_eth_hdr_wrb *hdr;
781 bool map_single = false;
784 hdr = queue_head_node(txq);
786 map_head = txq->head;
788 if (skb->len > skb->data_len) {
789 int len = skb_headlen(skb);
790 busaddr = dma_map_single(dev, skb->data, len, DMA_TO_DEVICE);
791 if (dma_mapping_error(dev, busaddr))
794 wrb = queue_head_node(txq);
795 wrb_fill(wrb, busaddr, len);
796 be_dws_cpu_to_le(wrb, sizeof(*wrb));
801 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
802 const struct skb_frag_struct *frag =
803 &skb_shinfo(skb)->frags[i];
804 busaddr = skb_frag_dma_map(dev, frag, 0,
805 skb_frag_size(frag), DMA_TO_DEVICE);
806 if (dma_mapping_error(dev, busaddr))
808 wrb = queue_head_node(txq);
809 wrb_fill(wrb, busaddr, skb_frag_size(frag));
810 be_dws_cpu_to_le(wrb, sizeof(*wrb));
812 copied += skb_frag_size(frag);
816 wrb = queue_head_node(txq);
818 be_dws_cpu_to_le(wrb, sizeof(*wrb));
822 wrb_fill_hdr(adapter, hdr, first_skb, wrb_cnt, copied, skip_hw_vlan);
823 be_dws_cpu_to_le(hdr, sizeof(*hdr));
827 txq->head = map_head;
829 wrb = queue_head_node(txq);
830 unmap_tx_frag(dev, wrb, map_single);
832 copied -= wrb->frag_len;
838 static struct sk_buff *be_insert_vlan_in_pkt(struct be_adapter *adapter,
844 skb = skb_share_check(skb, GFP_ATOMIC);
848 if (vlan_tx_tag_present(skb))
849 vlan_tag = be_get_tx_vlan_tag(adapter, skb);
851 if (qnq_async_evt_rcvd(adapter) && adapter->pvid) {
853 vlan_tag = adapter->pvid;
854 /* f/w workaround to set skip_hw_vlan = 1, informs the F/W to
855 * skip VLAN insertion
858 *skip_hw_vlan = true;
862 skb = __vlan_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
868 /* Insert the outer VLAN, if any */
869 if (adapter->qnq_vid) {
870 vlan_tag = adapter->qnq_vid;
871 skb = __vlan_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
875 *skip_hw_vlan = true;
881 static bool be_ipv6_exthdr_check(struct sk_buff *skb)
883 struct ethhdr *eh = (struct ethhdr *)skb->data;
884 u16 offset = ETH_HLEN;
886 if (eh->h_proto == htons(ETH_P_IPV6)) {
887 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + offset);
889 offset += sizeof(struct ipv6hdr);
890 if (ip6h->nexthdr != NEXTHDR_TCP &&
891 ip6h->nexthdr != NEXTHDR_UDP) {
892 struct ipv6_opt_hdr *ehdr =
893 (struct ipv6_opt_hdr *) (skb->data + offset);
895 /* offending pkt: 2nd byte following IPv6 hdr is 0xff */
896 if (ehdr->hdrlen == 0xff)
903 static int be_vlan_tag_tx_chk(struct be_adapter *adapter, struct sk_buff *skb)
905 return vlan_tx_tag_present(skb) || adapter->pvid || adapter->qnq_vid;
908 static int be_ipv6_tx_stall_chk(struct be_adapter *adapter,
911 return BE3_chip(adapter) && be_ipv6_exthdr_check(skb);
914 static struct sk_buff *be_xmit_workarounds(struct be_adapter *adapter,
918 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
919 unsigned int eth_hdr_len;
922 /* Lancer, SH-R ASICs have a bug wherein Packets that are 32 bytes or less
923 * may cause a transmit stall on that port. So the work-around is to
924 * pad short packets (<= 32 bytes) to a 36-byte length.
926 if (unlikely(!BEx_chip(adapter) && skb->len <= 32)) {
927 if (skb_padto(skb, 36))
932 /* For padded packets, BE HW modifies tot_len field in IP header
933 * incorrecly when VLAN tag is inserted by HW.
934 * For padded packets, Lancer computes incorrect checksum.
936 eth_hdr_len = ntohs(skb->protocol) == ETH_P_8021Q ?
937 VLAN_ETH_HLEN : ETH_HLEN;
938 if (skb->len <= 60 &&
939 (lancer_chip(adapter) || vlan_tx_tag_present(skb)) &&
941 ip = (struct iphdr *)ip_hdr(skb);
942 pskb_trim(skb, eth_hdr_len + ntohs(ip->tot_len));
945 /* If vlan tag is already inlined in the packet, skip HW VLAN
946 * tagging in UMC mode
948 if ((adapter->function_mode & UMC_ENABLED) &&
949 veh->h_vlan_proto == htons(ETH_P_8021Q))
950 *skip_hw_vlan = true;
952 /* HW has a bug wherein it will calculate CSUM for VLAN
953 * pkts even though it is disabled.
954 * Manually insert VLAN in pkt.
956 if (skb->ip_summed != CHECKSUM_PARTIAL &&
957 vlan_tx_tag_present(skb)) {
958 skb = be_insert_vlan_in_pkt(adapter, skb, skip_hw_vlan);
963 /* HW may lockup when VLAN HW tagging is requested on
964 * certain ipv6 packets. Drop such pkts if the HW workaround to
965 * skip HW tagging is not enabled by FW.
967 if (unlikely(be_ipv6_tx_stall_chk(adapter, skb) &&
968 (adapter->pvid || adapter->qnq_vid) &&
969 !qnq_async_evt_rcvd(adapter)))
972 /* Manual VLAN tag insertion to prevent:
973 * ASIC lockup when the ASIC inserts VLAN tag into
974 * certain ipv6 packets. Insert VLAN tags in driver,
975 * and set event, completion, vlan bits accordingly
978 if (be_ipv6_tx_stall_chk(adapter, skb) &&
979 be_vlan_tag_tx_chk(adapter, skb)) {
980 skb = be_insert_vlan_in_pkt(adapter, skb, skip_hw_vlan);
987 dev_kfree_skb_any(skb);
991 static netdev_tx_t be_xmit(struct sk_buff *skb, struct net_device *netdev)
993 struct be_adapter *adapter = netdev_priv(netdev);
994 struct be_tx_obj *txo = &adapter->tx_obj[skb_get_queue_mapping(skb)];
995 struct be_queue_info *txq = &txo->q;
996 bool dummy_wrb, stopped = false;
997 u32 wrb_cnt = 0, copied = 0;
998 bool skip_hw_vlan = false;
999 u32 start = txq->head;
1001 skb = be_xmit_workarounds(adapter, skb, &skip_hw_vlan);
1003 tx_stats(txo)->tx_drv_drops++;
1004 return NETDEV_TX_OK;
1007 wrb_cnt = wrb_cnt_for_skb(adapter, skb, &dummy_wrb);
1009 copied = make_tx_wrbs(adapter, txq, skb, wrb_cnt, dummy_wrb,
1012 int gso_segs = skb_shinfo(skb)->gso_segs;
1014 /* record the sent skb in the sent_skb table */
1015 BUG_ON(txo->sent_skb_list[start]);
1016 txo->sent_skb_list[start] = skb;
1018 /* Ensure txq has space for the next skb; Else stop the queue
1019 * *BEFORE* ringing the tx doorbell, so that we serialze the
1020 * tx compls of the current transmit which'll wake up the queue
1022 atomic_add(wrb_cnt, &txq->used);
1023 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
1025 netif_stop_subqueue(netdev, skb_get_queue_mapping(skb));
1029 be_txq_notify(adapter, txo, wrb_cnt);
1031 be_tx_stats_update(txo, wrb_cnt, copied, gso_segs, stopped);
1034 tx_stats(txo)->tx_drv_drops++;
1035 dev_kfree_skb_any(skb);
1037 return NETDEV_TX_OK;
1040 static int be_change_mtu(struct net_device *netdev, int new_mtu)
1042 struct be_adapter *adapter = netdev_priv(netdev);
1043 if (new_mtu < BE_MIN_MTU ||
1044 new_mtu > (BE_MAX_JUMBO_FRAME_SIZE -
1045 (ETH_HLEN + ETH_FCS_LEN))) {
1046 dev_info(&adapter->pdev->dev,
1047 "MTU must be between %d and %d bytes\n",
1049 (BE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN)));
1052 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
1053 netdev->mtu, new_mtu);
1054 netdev->mtu = new_mtu;
1059 * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
1060 * If the user configures more, place BE in vlan promiscuous mode.
1062 static int be_vid_config(struct be_adapter *adapter)
1064 u16 vids[BE_NUM_VLANS_SUPPORTED];
1068 /* No need to further configure vids if in promiscuous mode */
1069 if (adapter->promiscuous)
1072 if (adapter->vlans_added > be_max_vlans(adapter))
1073 goto set_vlan_promisc;
1075 /* Construct VLAN Table to give to HW */
1076 for (i = 0; i < VLAN_N_VID; i++)
1077 if (adapter->vlan_tag[i])
1078 vids[num++] = cpu_to_le16(i);
1080 status = be_cmd_vlan_config(adapter, adapter->if_handle,
1084 /* Set to VLAN promisc mode as setting VLAN filter failed */
1085 if (status == MCC_ADDL_STS_INSUFFICIENT_RESOURCES)
1086 goto set_vlan_promisc;
1087 dev_err(&adapter->pdev->dev,
1088 "Setting HW VLAN filtering failed.\n");
1090 if (adapter->flags & BE_FLAGS_VLAN_PROMISC) {
1091 /* hw VLAN filtering re-enabled. */
1092 status = be_cmd_rx_filter(adapter,
1093 BE_FLAGS_VLAN_PROMISC, OFF);
1095 dev_info(&adapter->pdev->dev,
1096 "Disabling VLAN Promiscuous mode.\n");
1097 adapter->flags &= ~BE_FLAGS_VLAN_PROMISC;
1098 dev_info(&adapter->pdev->dev,
1099 "Re-Enabling HW VLAN filtering\n");
1107 dev_warn(&adapter->pdev->dev, "Exhausted VLAN HW filters.\n");
1109 status = be_cmd_rx_filter(adapter, BE_FLAGS_VLAN_PROMISC, ON);
1111 dev_info(&adapter->pdev->dev, "Enable VLAN Promiscuous mode\n");
1112 dev_info(&adapter->pdev->dev, "Disabling HW VLAN filtering\n");
1113 adapter->flags |= BE_FLAGS_VLAN_PROMISC;
1115 dev_err(&adapter->pdev->dev,
1116 "Failed to enable VLAN Promiscuous mode.\n");
1120 static int be_vlan_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
1122 struct be_adapter *adapter = netdev_priv(netdev);
1126 /* Packets with VID 0 are always received by Lancer by default */
1127 if (lancer_chip(adapter) && vid == 0)
1130 adapter->vlan_tag[vid] = 1;
1131 if (adapter->vlans_added <= (be_max_vlans(adapter) + 1))
1132 status = be_vid_config(adapter);
1135 adapter->vlans_added++;
1137 adapter->vlan_tag[vid] = 0;
1142 static int be_vlan_rem_vid(struct net_device *netdev, __be16 proto, u16 vid)
1144 struct be_adapter *adapter = netdev_priv(netdev);
1147 /* Packets with VID 0 are always received by Lancer by default */
1148 if (lancer_chip(adapter) && vid == 0)
1151 adapter->vlan_tag[vid] = 0;
1152 if (adapter->vlans_added <= be_max_vlans(adapter))
1153 status = be_vid_config(adapter);
1156 adapter->vlans_added--;
1158 adapter->vlan_tag[vid] = 1;
1163 static void be_set_rx_mode(struct net_device *netdev)
1165 struct be_adapter *adapter = netdev_priv(netdev);
1168 if (netdev->flags & IFF_PROMISC) {
1169 be_cmd_rx_filter(adapter, IFF_PROMISC, ON);
1170 adapter->promiscuous = true;
1174 /* BE was previously in promiscuous mode; disable it */
1175 if (adapter->promiscuous) {
1176 adapter->promiscuous = false;
1177 be_cmd_rx_filter(adapter, IFF_PROMISC, OFF);
1179 if (adapter->vlans_added)
1180 be_vid_config(adapter);
1183 /* Enable multicast promisc if num configured exceeds what we support */
1184 if (netdev->flags & IFF_ALLMULTI ||
1185 netdev_mc_count(netdev) > be_max_mc(adapter)) {
1186 be_cmd_rx_filter(adapter, IFF_ALLMULTI, ON);
1190 if (netdev_uc_count(netdev) != adapter->uc_macs) {
1191 struct netdev_hw_addr *ha;
1192 int i = 1; /* First slot is claimed by the Primary MAC */
1194 for (; adapter->uc_macs > 0; adapter->uc_macs--, i++) {
1195 be_cmd_pmac_del(adapter, adapter->if_handle,
1196 adapter->pmac_id[i], 0);
1199 if (netdev_uc_count(netdev) > be_max_uc(adapter)) {
1200 be_cmd_rx_filter(adapter, IFF_PROMISC, ON);
1201 adapter->promiscuous = true;
1205 netdev_for_each_uc_addr(ha, adapter->netdev) {
1206 adapter->uc_macs++; /* First slot is for Primary MAC */
1207 be_cmd_pmac_add(adapter, (u8 *)ha->addr,
1209 &adapter->pmac_id[adapter->uc_macs], 0);
1213 status = be_cmd_rx_filter(adapter, IFF_MULTICAST, ON);
1215 /* Set to MCAST promisc mode if setting MULTICAST address fails */
1217 dev_info(&adapter->pdev->dev, "Exhausted multicast HW filters.\n");
1218 dev_info(&adapter->pdev->dev, "Disabling HW multicast filtering.\n");
1219 be_cmd_rx_filter(adapter, IFF_ALLMULTI, ON);
1225 static int be_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1227 struct be_adapter *adapter = netdev_priv(netdev);
1228 struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1231 if (!sriov_enabled(adapter))
1234 if (!is_valid_ether_addr(mac) || vf >= adapter->num_vfs)
1237 if (BEx_chip(adapter)) {
1238 be_cmd_pmac_del(adapter, vf_cfg->if_handle, vf_cfg->pmac_id,
1241 status = be_cmd_pmac_add(adapter, mac, vf_cfg->if_handle,
1242 &vf_cfg->pmac_id, vf + 1);
1244 status = be_cmd_set_mac(adapter, mac, vf_cfg->if_handle,
1249 dev_err(&adapter->pdev->dev, "MAC %pM set on VF %d Failed\n",
1252 memcpy(vf_cfg->mac_addr, mac, ETH_ALEN);
1257 static int be_get_vf_config(struct net_device *netdev, int vf,
1258 struct ifla_vf_info *vi)
1260 struct be_adapter *adapter = netdev_priv(netdev);
1261 struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1263 if (!sriov_enabled(adapter))
1266 if (vf >= adapter->num_vfs)
1270 vi->tx_rate = vf_cfg->tx_rate;
1271 vi->vlan = vf_cfg->vlan_tag & VLAN_VID_MASK;
1272 vi->qos = vf_cfg->vlan_tag >> VLAN_PRIO_SHIFT;
1273 memcpy(&vi->mac, vf_cfg->mac_addr, ETH_ALEN);
1278 static int be_set_vf_vlan(struct net_device *netdev,
1279 int vf, u16 vlan, u8 qos)
1281 struct be_adapter *adapter = netdev_priv(netdev);
1282 struct be_vf_cfg *vf_cfg = &adapter->vf_cfg[vf];
1285 if (!sriov_enabled(adapter))
1288 if (vf >= adapter->num_vfs || vlan > 4095 || qos > 7)
1292 vlan |= qos << VLAN_PRIO_SHIFT;
1293 if (vf_cfg->vlan_tag != vlan) {
1294 /* If this is new value, program it. Else skip. */
1295 vf_cfg->vlan_tag = vlan;
1296 status = be_cmd_set_hsw_config(adapter, vlan, vf + 1,
1297 vf_cfg->if_handle, 0);
1300 /* Reset Transparent Vlan Tagging. */
1301 vf_cfg->vlan_tag = 0;
1302 vlan = vf_cfg->def_vid;
1303 status = be_cmd_set_hsw_config(adapter, vlan, vf + 1,
1304 vf_cfg->if_handle, 0);
1309 dev_info(&adapter->pdev->dev,
1310 "VLAN %d config on VF %d failed\n", vlan, vf);
1314 static int be_set_vf_tx_rate(struct net_device *netdev,
1317 struct be_adapter *adapter = netdev_priv(netdev);
1320 if (!sriov_enabled(adapter))
1323 if (vf >= adapter->num_vfs)
1326 if (rate < 100 || rate > 10000) {
1327 dev_err(&adapter->pdev->dev,
1328 "tx rate must be between 100 and 10000 Mbps\n");
1332 if (lancer_chip(adapter))
1333 status = be_cmd_set_profile_config(adapter, rate / 10, vf + 1);
1335 status = be_cmd_set_qos(adapter, rate / 10, vf + 1);
1338 dev_err(&adapter->pdev->dev,
1339 "tx rate %d on VF %d failed\n", rate, vf);
1341 adapter->vf_cfg[vf].tx_rate = rate;
1345 static void be_aic_update(struct be_aic_obj *aic, u64 rx_pkts, u64 tx_pkts,
1348 aic->rx_pkts_prev = rx_pkts;
1349 aic->tx_reqs_prev = tx_pkts;
1353 static void be_eqd_update(struct be_adapter *adapter)
1355 struct be_set_eqd set_eqd[MAX_EVT_QS];
1356 int eqd, i, num = 0, start;
1357 struct be_aic_obj *aic;
1358 struct be_eq_obj *eqo;
1359 struct be_rx_obj *rxo;
1360 struct be_tx_obj *txo;
1361 u64 rx_pkts, tx_pkts;
1365 for_all_evt_queues(adapter, eqo, i) {
1366 aic = &adapter->aic_obj[eqo->idx];
1374 rxo = &adapter->rx_obj[eqo->idx];
1376 start = u64_stats_fetch_begin_bh(&rxo->stats.sync);
1377 rx_pkts = rxo->stats.rx_pkts;
1378 } while (u64_stats_fetch_retry_bh(&rxo->stats.sync, start));
1380 txo = &adapter->tx_obj[eqo->idx];
1382 start = u64_stats_fetch_begin_bh(&txo->stats.sync);
1383 tx_pkts = txo->stats.tx_reqs;
1384 } while (u64_stats_fetch_retry_bh(&txo->stats.sync, start));
1387 /* Skip, if wrapped around or first calculation */
1389 if (!aic->jiffies || time_before(now, aic->jiffies) ||
1390 rx_pkts < aic->rx_pkts_prev ||
1391 tx_pkts < aic->tx_reqs_prev) {
1392 be_aic_update(aic, rx_pkts, tx_pkts, now);
1396 delta = jiffies_to_msecs(now - aic->jiffies);
1397 pps = (((u32)(rx_pkts - aic->rx_pkts_prev) * 1000) / delta) +
1398 (((u32)(tx_pkts - aic->tx_reqs_prev) * 1000) / delta);
1399 eqd = (pps / 15000) << 2;
1403 eqd = min_t(u32, eqd, aic->max_eqd);
1404 eqd = max_t(u32, eqd, aic->min_eqd);
1406 be_aic_update(aic, rx_pkts, tx_pkts, now);
1408 if (eqd != aic->prev_eqd) {
1409 set_eqd[num].delay_multiplier = (eqd * 65)/100;
1410 set_eqd[num].eq_id = eqo->q.id;
1411 aic->prev_eqd = eqd;
1417 be_cmd_modify_eqd(adapter, set_eqd, num);
1420 static void be_rx_stats_update(struct be_rx_obj *rxo,
1421 struct be_rx_compl_info *rxcp)
1423 struct be_rx_stats *stats = rx_stats(rxo);
1425 u64_stats_update_begin(&stats->sync);
1427 stats->rx_bytes += rxcp->pkt_size;
1429 if (rxcp->pkt_type == BE_MULTICAST_PACKET)
1430 stats->rx_mcast_pkts++;
1432 stats->rx_compl_err++;
1433 u64_stats_update_end(&stats->sync);
1436 static inline bool csum_passed(struct be_rx_compl_info *rxcp)
1438 /* L4 checksum is not reliable for non TCP/UDP packets.
1439 * Also ignore ipcksm for ipv6 pkts */
1440 return (rxcp->tcpf || rxcp->udpf) && rxcp->l4_csum &&
1441 (rxcp->ip_csum || rxcp->ipv6);
1444 static struct be_rx_page_info *get_rx_page_info(struct be_rx_obj *rxo,
1447 struct be_adapter *adapter = rxo->adapter;
1448 struct be_rx_page_info *rx_page_info;
1449 struct be_queue_info *rxq = &rxo->q;
1451 rx_page_info = &rxo->page_info_tbl[frag_idx];
1452 BUG_ON(!rx_page_info->page);
1454 if (rx_page_info->last_page_user) {
1455 dma_unmap_page(&adapter->pdev->dev,
1456 dma_unmap_addr(rx_page_info, bus),
1457 adapter->big_page_size, DMA_FROM_DEVICE);
1458 rx_page_info->last_page_user = false;
1461 atomic_dec(&rxq->used);
1462 return rx_page_info;
1465 /* Throwaway the data in the Rx completion */
1466 static void be_rx_compl_discard(struct be_rx_obj *rxo,
1467 struct be_rx_compl_info *rxcp)
1469 struct be_queue_info *rxq = &rxo->q;
1470 struct be_rx_page_info *page_info;
1471 u16 i, num_rcvd = rxcp->num_rcvd;
1473 for (i = 0; i < num_rcvd; i++) {
1474 page_info = get_rx_page_info(rxo, rxcp->rxq_idx);
1475 put_page(page_info->page);
1476 memset(page_info, 0, sizeof(*page_info));
1477 index_inc(&rxcp->rxq_idx, rxq->len);
1482 * skb_fill_rx_data forms a complete skb for an ether frame
1483 * indicated by rxcp.
1485 static void skb_fill_rx_data(struct be_rx_obj *rxo, struct sk_buff *skb,
1486 struct be_rx_compl_info *rxcp)
1488 struct be_queue_info *rxq = &rxo->q;
1489 struct be_rx_page_info *page_info;
1491 u16 hdr_len, curr_frag_len, remaining;
1494 page_info = get_rx_page_info(rxo, rxcp->rxq_idx);
1495 start = page_address(page_info->page) + page_info->page_offset;
1498 /* Copy data in the first descriptor of this completion */
1499 curr_frag_len = min(rxcp->pkt_size, rx_frag_size);
1501 skb->len = curr_frag_len;
1502 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
1503 memcpy(skb->data, start, curr_frag_len);
1504 /* Complete packet has now been moved to data */
1505 put_page(page_info->page);
1507 skb->tail += curr_frag_len;
1510 memcpy(skb->data, start, hdr_len);
1511 skb_shinfo(skb)->nr_frags = 1;
1512 skb_frag_set_page(skb, 0, page_info->page);
1513 skb_shinfo(skb)->frags[0].page_offset =
1514 page_info->page_offset + hdr_len;
1515 skb_frag_size_set(&skb_shinfo(skb)->frags[0], curr_frag_len - hdr_len);
1516 skb->data_len = curr_frag_len - hdr_len;
1517 skb->truesize += rx_frag_size;
1518 skb->tail += hdr_len;
1520 page_info->page = NULL;
1522 if (rxcp->pkt_size <= rx_frag_size) {
1523 BUG_ON(rxcp->num_rcvd != 1);
1527 /* More frags present for this completion */
1528 index_inc(&rxcp->rxq_idx, rxq->len);
1529 remaining = rxcp->pkt_size - curr_frag_len;
1530 for (i = 1, j = 0; i < rxcp->num_rcvd; i++) {
1531 page_info = get_rx_page_info(rxo, rxcp->rxq_idx);
1532 curr_frag_len = min(remaining, rx_frag_size);
1534 /* Coalesce all frags from the same physical page in one slot */
1535 if (page_info->page_offset == 0) {
1538 skb_frag_set_page(skb, j, page_info->page);
1539 skb_shinfo(skb)->frags[j].page_offset =
1540 page_info->page_offset;
1541 skb_frag_size_set(&skb_shinfo(skb)->frags[j], 0);
1542 skb_shinfo(skb)->nr_frags++;
1544 put_page(page_info->page);
1547 skb_frag_size_add(&skb_shinfo(skb)->frags[j], curr_frag_len);
1548 skb->len += curr_frag_len;
1549 skb->data_len += curr_frag_len;
1550 skb->truesize += rx_frag_size;
1551 remaining -= curr_frag_len;
1552 index_inc(&rxcp->rxq_idx, rxq->len);
1553 page_info->page = NULL;
1555 BUG_ON(j > MAX_SKB_FRAGS);
1558 /* Process the RX completion indicated by rxcp when GRO is disabled */
1559 static void be_rx_compl_process(struct be_rx_obj *rxo,
1560 struct be_rx_compl_info *rxcp)
1562 struct be_adapter *adapter = rxo->adapter;
1563 struct net_device *netdev = adapter->netdev;
1564 struct sk_buff *skb;
1566 skb = netdev_alloc_skb_ip_align(netdev, BE_RX_SKB_ALLOC_SIZE);
1567 if (unlikely(!skb)) {
1568 rx_stats(rxo)->rx_drops_no_skbs++;
1569 be_rx_compl_discard(rxo, rxcp);
1573 skb_fill_rx_data(rxo, skb, rxcp);
1575 if (likely((netdev->features & NETIF_F_RXCSUM) && csum_passed(rxcp)))
1576 skb->ip_summed = CHECKSUM_UNNECESSARY;
1578 skb_checksum_none_assert(skb);
1580 skb->protocol = eth_type_trans(skb, netdev);
1581 skb_record_rx_queue(skb, rxo - &adapter->rx_obj[0]);
1582 if (netdev->features & NETIF_F_RXHASH)
1583 skb->rxhash = rxcp->rss_hash;
1587 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rxcp->vlan_tag);
1589 netif_receive_skb(skb);
1592 /* Process the RX completion indicated by rxcp when GRO is enabled */
1593 static void be_rx_compl_process_gro(struct be_rx_obj *rxo,
1594 struct napi_struct *napi,
1595 struct be_rx_compl_info *rxcp)
1597 struct be_adapter *adapter = rxo->adapter;
1598 struct be_rx_page_info *page_info;
1599 struct sk_buff *skb = NULL;
1600 struct be_queue_info *rxq = &rxo->q;
1601 u16 remaining, curr_frag_len;
1604 skb = napi_get_frags(napi);
1606 be_rx_compl_discard(rxo, rxcp);
1610 remaining = rxcp->pkt_size;
1611 for (i = 0, j = -1; i < rxcp->num_rcvd; i++) {
1612 page_info = get_rx_page_info(rxo, rxcp->rxq_idx);
1614 curr_frag_len = min(remaining, rx_frag_size);
1616 /* Coalesce all frags from the same physical page in one slot */
1617 if (i == 0 || page_info->page_offset == 0) {
1618 /* First frag or Fresh page */
1620 skb_frag_set_page(skb, j, page_info->page);
1621 skb_shinfo(skb)->frags[j].page_offset =
1622 page_info->page_offset;
1623 skb_frag_size_set(&skb_shinfo(skb)->frags[j], 0);
1625 put_page(page_info->page);
1627 skb_frag_size_add(&skb_shinfo(skb)->frags[j], curr_frag_len);
1628 skb->truesize += rx_frag_size;
1629 remaining -= curr_frag_len;
1630 index_inc(&rxcp->rxq_idx, rxq->len);
1631 memset(page_info, 0, sizeof(*page_info));
1633 BUG_ON(j > MAX_SKB_FRAGS);
1635 skb_shinfo(skb)->nr_frags = j + 1;
1636 skb->len = rxcp->pkt_size;
1637 skb->data_len = rxcp->pkt_size;
1638 skb->ip_summed = CHECKSUM_UNNECESSARY;
1639 skb_record_rx_queue(skb, rxo - &adapter->rx_obj[0]);
1640 if (adapter->netdev->features & NETIF_F_RXHASH)
1641 skb->rxhash = rxcp->rss_hash;
1644 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rxcp->vlan_tag);
1646 napi_gro_frags(napi);
1649 static void be_parse_rx_compl_v1(struct be_eth_rx_compl *compl,
1650 struct be_rx_compl_info *rxcp)
1653 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, pktsize, compl);
1654 rxcp->vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vtp, compl);
1655 rxcp->err = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, err, compl);
1656 rxcp->tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, tcpf, compl);
1657 rxcp->udpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, udpf, compl);
1659 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, ipcksm, compl);
1661 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, l4_cksm, compl);
1663 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, ip_version, compl);
1665 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, fragndx, compl);
1667 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, numfrags, compl);
1669 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, cast_enc, compl);
1671 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, rsshash, compl);
1673 rxcp->vtm = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vtm,
1675 rxcp->vlan_tag = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vlan_tag,
1678 rxcp->port = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, port, compl);
1681 static void be_parse_rx_compl_v0(struct be_eth_rx_compl *compl,
1682 struct be_rx_compl_info *rxcp)
1685 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, pktsize, compl);
1686 rxcp->vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vtp, compl);
1687 rxcp->err = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, err, compl);
1688 rxcp->tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, tcpf, compl);
1689 rxcp->udpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, udpf, compl);
1691 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, ipcksm, compl);
1693 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, l4_cksm, compl);
1695 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, ip_version, compl);
1697 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, fragndx, compl);
1699 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, numfrags, compl);
1701 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, cast_enc, compl);
1703 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, rsshash, compl);
1705 rxcp->vtm = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vtm,
1707 rxcp->vlan_tag = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vlan_tag,
1710 rxcp->port = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, port, compl);
1711 rxcp->ip_frag = AMAP_GET_BITS(struct amap_eth_rx_compl_v0,
1715 static struct be_rx_compl_info *be_rx_compl_get(struct be_rx_obj *rxo)
1717 struct be_eth_rx_compl *compl = queue_tail_node(&rxo->cq);
1718 struct be_rx_compl_info *rxcp = &rxo->rxcp;
1719 struct be_adapter *adapter = rxo->adapter;
1721 /* For checking the valid bit it is Ok to use either definition as the
1722 * valid bit is at the same position in both v0 and v1 Rx compl */
1723 if (compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] == 0)
1727 be_dws_le_to_cpu(compl, sizeof(*compl));
1729 if (adapter->be3_native)
1730 be_parse_rx_compl_v1(compl, rxcp);
1732 be_parse_rx_compl_v0(compl, rxcp);
1738 /* vlanf could be wrongly set in some cards.
1739 * ignore if vtm is not set */
1740 if ((adapter->function_mode & FLEX10_MODE) && !rxcp->vtm)
1743 if (!lancer_chip(adapter))
1744 rxcp->vlan_tag = swab16(rxcp->vlan_tag);
1746 if (adapter->pvid == (rxcp->vlan_tag & VLAN_VID_MASK) &&
1747 !adapter->vlan_tag[rxcp->vlan_tag])
1751 /* As the compl has been parsed, reset it; we wont touch it again */
1752 compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] = 0;
1754 queue_tail_inc(&rxo->cq);
1758 static inline struct page *be_alloc_pages(u32 size, gfp_t gfp)
1760 u32 order = get_order(size);
1764 return alloc_pages(gfp, order);
1768 * Allocate a page, split it to fragments of size rx_frag_size and post as
1769 * receive buffers to BE
1771 static void be_post_rx_frags(struct be_rx_obj *rxo, gfp_t gfp)
1773 struct be_adapter *adapter = rxo->adapter;
1774 struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
1775 struct be_queue_info *rxq = &rxo->q;
1776 struct page *pagep = NULL;
1777 struct be_eth_rx_d *rxd;
1778 u64 page_dmaaddr = 0, frag_dmaaddr;
1779 u32 posted, page_offset = 0;
1781 page_info = &rxo->page_info_tbl[rxq->head];
1782 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
1784 pagep = be_alloc_pages(adapter->big_page_size, gfp);
1785 if (unlikely(!pagep)) {
1786 rx_stats(rxo)->rx_post_fail++;
1789 page_dmaaddr = dma_map_page(&adapter->pdev->dev, pagep,
1790 0, adapter->big_page_size,
1792 page_info->page_offset = 0;
1795 page_info->page_offset = page_offset + rx_frag_size;
1797 page_offset = page_info->page_offset;
1798 page_info->page = pagep;
1799 dma_unmap_addr_set(page_info, bus, page_dmaaddr);
1800 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
1802 rxd = queue_head_node(rxq);
1803 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
1804 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
1806 /* Any space left in the current big page for another frag? */
1807 if ((page_offset + rx_frag_size + rx_frag_size) >
1808 adapter->big_page_size) {
1810 page_info->last_page_user = true;
1813 prev_page_info = page_info;
1814 queue_head_inc(rxq);
1815 page_info = &rxo->page_info_tbl[rxq->head];
1818 prev_page_info->last_page_user = true;
1821 atomic_add(posted, &rxq->used);
1822 be_rxq_notify(adapter, rxq->id, posted);
1823 } else if (atomic_read(&rxq->used) == 0) {
1824 /* Let be_worker replenish when memory is available */
1825 rxo->rx_post_starved = true;
1829 static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
1831 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
1833 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
1837 be_dws_le_to_cpu(txcp, sizeof(*txcp));
1839 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
1841 queue_tail_inc(tx_cq);
1845 static u16 be_tx_compl_process(struct be_adapter *adapter,
1846 struct be_tx_obj *txo, u16 last_index)
1848 struct be_queue_info *txq = &txo->q;
1849 struct be_eth_wrb *wrb;
1850 struct sk_buff **sent_skbs = txo->sent_skb_list;
1851 struct sk_buff *sent_skb;
1852 u16 cur_index, num_wrbs = 1; /* account for hdr wrb */
1853 bool unmap_skb_hdr = true;
1855 sent_skb = sent_skbs[txq->tail];
1857 sent_skbs[txq->tail] = NULL;
1859 /* skip header wrb */
1860 queue_tail_inc(txq);
1863 cur_index = txq->tail;
1864 wrb = queue_tail_node(txq);
1865 unmap_tx_frag(&adapter->pdev->dev, wrb,
1866 (unmap_skb_hdr && skb_headlen(sent_skb)));
1867 unmap_skb_hdr = false;
1870 queue_tail_inc(txq);
1871 } while (cur_index != last_index);
1873 kfree_skb(sent_skb);
1877 /* Return the number of events in the event queue */
1878 static inline int events_get(struct be_eq_obj *eqo)
1880 struct be_eq_entry *eqe;
1884 eqe = queue_tail_node(&eqo->q);
1891 queue_tail_inc(&eqo->q);
1897 /* Leaves the EQ is disarmed state */
1898 static void be_eq_clean(struct be_eq_obj *eqo)
1900 int num = events_get(eqo);
1902 be_eq_notify(eqo->adapter, eqo->q.id, false, true, num);
1905 static void be_rx_cq_clean(struct be_rx_obj *rxo)
1907 struct be_rx_page_info *page_info;
1908 struct be_queue_info *rxq = &rxo->q;
1909 struct be_queue_info *rx_cq = &rxo->cq;
1910 struct be_rx_compl_info *rxcp;
1911 struct be_adapter *adapter = rxo->adapter;
1915 /* Consume pending rx completions.
1916 * Wait for the flush completion (identified by zero num_rcvd)
1917 * to arrive. Notify CQ even when there are no more CQ entries
1918 * for HW to flush partially coalesced CQ entries.
1919 * In Lancer, there is no need to wait for flush compl.
1922 rxcp = be_rx_compl_get(rxo);
1924 if (lancer_chip(adapter))
1927 if (flush_wait++ > 10 || be_hw_error(adapter)) {
1928 dev_warn(&adapter->pdev->dev,
1929 "did not receive flush compl\n");
1932 be_cq_notify(adapter, rx_cq->id, true, 0);
1935 be_rx_compl_discard(rxo, rxcp);
1936 be_cq_notify(adapter, rx_cq->id, false, 1);
1937 if (rxcp->num_rcvd == 0)
1942 /* After cleanup, leave the CQ in unarmed state */
1943 be_cq_notify(adapter, rx_cq->id, false, 0);
1945 /* Then free posted rx buffers that were not used */
1946 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
1947 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
1948 page_info = get_rx_page_info(rxo, tail);
1949 put_page(page_info->page);
1950 memset(page_info, 0, sizeof(*page_info));
1952 BUG_ON(atomic_read(&rxq->used));
1953 rxq->tail = rxq->head = 0;
1956 static void be_tx_compl_clean(struct be_adapter *adapter)
1958 struct be_tx_obj *txo;
1959 struct be_queue_info *txq;
1960 struct be_eth_tx_compl *txcp;
1961 u16 end_idx, cmpl = 0, timeo = 0, num_wrbs = 0;
1962 struct sk_buff *sent_skb;
1964 int i, pending_txqs;
1966 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1968 pending_txqs = adapter->num_tx_qs;
1970 for_all_tx_queues(adapter, txo, i) {
1972 while ((txcp = be_tx_compl_get(&txo->cq))) {
1974 AMAP_GET_BITS(struct amap_eth_tx_compl,
1976 num_wrbs += be_tx_compl_process(adapter, txo,
1981 be_cq_notify(adapter, txo->cq.id, false, cmpl);
1982 atomic_sub(num_wrbs, &txq->used);
1986 if (atomic_read(&txq->used) == 0)
1990 if (pending_txqs == 0 || ++timeo > 200)
1996 for_all_tx_queues(adapter, txo, i) {
1998 if (atomic_read(&txq->used))
1999 dev_err(&adapter->pdev->dev, "%d pending tx-compls\n",
2000 atomic_read(&txq->used));
2002 /* free posted tx for which compls will never arrive */
2003 while (atomic_read(&txq->used)) {
2004 sent_skb = txo->sent_skb_list[txq->tail];
2005 end_idx = txq->tail;
2006 num_wrbs = wrb_cnt_for_skb(adapter, sent_skb,
2008 index_adv(&end_idx, num_wrbs - 1, txq->len);
2009 num_wrbs = be_tx_compl_process(adapter, txo, end_idx);
2010 atomic_sub(num_wrbs, &txq->used);
2015 static void be_evt_queues_destroy(struct be_adapter *adapter)
2017 struct be_eq_obj *eqo;
2020 for_all_evt_queues(adapter, eqo, i) {
2021 if (eqo->q.created) {
2023 be_cmd_q_destroy(adapter, &eqo->q, QTYPE_EQ);
2024 netif_napi_del(&eqo->napi);
2026 be_queue_free(adapter, &eqo->q);
2030 static int be_evt_queues_create(struct be_adapter *adapter)
2032 struct be_queue_info *eq;
2033 struct be_eq_obj *eqo;
2034 struct be_aic_obj *aic;
2037 adapter->num_evt_qs = min_t(u16, num_irqs(adapter),
2038 adapter->cfg_num_qs);
2040 for_all_evt_queues(adapter, eqo, i) {
2041 netif_napi_add(adapter->netdev, &eqo->napi, be_poll,
2043 aic = &adapter->aic_obj[i];
2044 eqo->adapter = adapter;
2045 eqo->tx_budget = BE_TX_BUDGET;
2047 aic->max_eqd = BE_MAX_EQD;
2051 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
2052 sizeof(struct be_eq_entry));
2056 rc = be_cmd_eq_create(adapter, eqo);
2063 static void be_mcc_queues_destroy(struct be_adapter *adapter)
2065 struct be_queue_info *q;
2067 q = &adapter->mcc_obj.q;
2069 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
2070 be_queue_free(adapter, q);
2072 q = &adapter->mcc_obj.cq;
2074 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2075 be_queue_free(adapter, q);
2078 /* Must be called only after TX qs are created as MCC shares TX EQ */
2079 static int be_mcc_queues_create(struct be_adapter *adapter)
2081 struct be_queue_info *q, *cq;
2083 cq = &adapter->mcc_obj.cq;
2084 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
2085 sizeof(struct be_mcc_compl)))
2088 /* Use the default EQ for MCC completions */
2089 if (be_cmd_cq_create(adapter, cq, &mcc_eqo(adapter)->q, true, 0))
2092 q = &adapter->mcc_obj.q;
2093 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
2094 goto mcc_cq_destroy;
2096 if (be_cmd_mccq_create(adapter, q, cq))
2102 be_queue_free(adapter, q);
2104 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
2106 be_queue_free(adapter, cq);
2111 static void be_tx_queues_destroy(struct be_adapter *adapter)
2113 struct be_queue_info *q;
2114 struct be_tx_obj *txo;
2117 for_all_tx_queues(adapter, txo, i) {
2120 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
2121 be_queue_free(adapter, q);
2125 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2126 be_queue_free(adapter, q);
2130 static int be_tx_qs_create(struct be_adapter *adapter)
2132 struct be_queue_info *cq, *eq;
2133 struct be_tx_obj *txo;
2136 adapter->num_tx_qs = min(adapter->num_evt_qs, be_max_txqs(adapter));
2138 for_all_tx_queues(adapter, txo, i) {
2140 status = be_queue_alloc(adapter, cq, TX_CQ_LEN,
2141 sizeof(struct be_eth_tx_compl));
2145 /* If num_evt_qs is less than num_tx_qs, then more than
2146 * one txq share an eq
2148 eq = &adapter->eq_obj[i % adapter->num_evt_qs].q;
2149 status = be_cmd_cq_create(adapter, cq, eq, false, 3);
2153 status = be_queue_alloc(adapter, &txo->q, TX_Q_LEN,
2154 sizeof(struct be_eth_wrb));
2158 status = be_cmd_txq_create(adapter, txo);
2163 dev_info(&adapter->pdev->dev, "created %d TX queue(s)\n",
2164 adapter->num_tx_qs);
2168 static void be_rx_cqs_destroy(struct be_adapter *adapter)
2170 struct be_queue_info *q;
2171 struct be_rx_obj *rxo;
2174 for_all_rx_queues(adapter, rxo, i) {
2177 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
2178 be_queue_free(adapter, q);
2182 static int be_rx_cqs_create(struct be_adapter *adapter)
2184 struct be_queue_info *eq, *cq;
2185 struct be_rx_obj *rxo;
2188 /* We can create as many RSS rings as there are EQs. */
2189 adapter->num_rx_qs = adapter->num_evt_qs;
2191 /* We'll use RSS only if atleast 2 RSS rings are supported.
2192 * When RSS is used, we'll need a default RXQ for non-IP traffic.
2194 if (adapter->num_rx_qs > 1)
2195 adapter->num_rx_qs++;
2197 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
2198 for_all_rx_queues(adapter, rxo, i) {
2199 rxo->adapter = adapter;
2201 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
2202 sizeof(struct be_eth_rx_compl));
2206 eq = &adapter->eq_obj[i % adapter->num_evt_qs].q;
2207 rc = be_cmd_cq_create(adapter, cq, eq, false, 3);
2212 dev_info(&adapter->pdev->dev,
2213 "created %d RSS queue(s) and 1 default RX queue\n",
2214 adapter->num_rx_qs - 1);
2218 static irqreturn_t be_intx(int irq, void *dev)
2220 struct be_eq_obj *eqo = dev;
2221 struct be_adapter *adapter = eqo->adapter;
2224 /* IRQ is not expected when NAPI is scheduled as the EQ
2225 * will not be armed.
2226 * But, this can happen on Lancer INTx where it takes
2227 * a while to de-assert INTx or in BE2 where occasionaly
2228 * an interrupt may be raised even when EQ is unarmed.
2229 * If NAPI is already scheduled, then counting & notifying
2230 * events will orphan them.
2232 if (napi_schedule_prep(&eqo->napi)) {
2233 num_evts = events_get(eqo);
2234 __napi_schedule(&eqo->napi);
2236 eqo->spurious_intr = 0;
2238 be_eq_notify(adapter, eqo->q.id, false, true, num_evts);
2240 /* Return IRQ_HANDLED only for the the first spurious intr
2241 * after a valid intr to stop the kernel from branding
2242 * this irq as a bad one!
2244 if (num_evts || eqo->spurious_intr++ == 0)
2250 static irqreturn_t be_msix(int irq, void *dev)
2252 struct be_eq_obj *eqo = dev;
2254 be_eq_notify(eqo->adapter, eqo->q.id, false, true, 0);
2255 napi_schedule(&eqo->napi);
2259 static inline bool do_gro(struct be_rx_compl_info *rxcp)
2261 return (rxcp->tcpf && !rxcp->err && rxcp->l4_csum) ? true : false;
2264 static int be_process_rx(struct be_rx_obj *rxo, struct napi_struct *napi,
2267 struct be_adapter *adapter = rxo->adapter;
2268 struct be_queue_info *rx_cq = &rxo->cq;
2269 struct be_rx_compl_info *rxcp;
2272 for (work_done = 0; work_done < budget; work_done++) {
2273 rxcp = be_rx_compl_get(rxo);
2277 /* Is it a flush compl that has no data */
2278 if (unlikely(rxcp->num_rcvd == 0))
2281 /* Discard compl with partial DMA Lancer B0 */
2282 if (unlikely(!rxcp->pkt_size)) {
2283 be_rx_compl_discard(rxo, rxcp);
2287 /* On BE drop pkts that arrive due to imperfect filtering in
2288 * promiscuous mode on some skews
2290 if (unlikely(rxcp->port != adapter->port_num &&
2291 !lancer_chip(adapter))) {
2292 be_rx_compl_discard(rxo, rxcp);
2297 be_rx_compl_process_gro(rxo, napi, rxcp);
2299 be_rx_compl_process(rxo, rxcp);
2301 be_rx_stats_update(rxo, rxcp);
2305 be_cq_notify(adapter, rx_cq->id, true, work_done);
2307 if (atomic_read(&rxo->q.used) < RX_FRAGS_REFILL_WM)
2308 be_post_rx_frags(rxo, GFP_ATOMIC);
2314 static bool be_process_tx(struct be_adapter *adapter, struct be_tx_obj *txo,
2315 int budget, int idx)
2317 struct be_eth_tx_compl *txcp;
2318 int num_wrbs = 0, work_done;
2320 for (work_done = 0; work_done < budget; work_done++) {
2321 txcp = be_tx_compl_get(&txo->cq);
2324 num_wrbs += be_tx_compl_process(adapter, txo,
2325 AMAP_GET_BITS(struct amap_eth_tx_compl,
2330 be_cq_notify(adapter, txo->cq.id, true, work_done);
2331 atomic_sub(num_wrbs, &txo->q.used);
2333 /* As Tx wrbs have been freed up, wake up netdev queue
2334 * if it was stopped due to lack of tx wrbs. */
2335 if (__netif_subqueue_stopped(adapter->netdev, idx) &&
2336 atomic_read(&txo->q.used) < txo->q.len / 2) {
2337 netif_wake_subqueue(adapter->netdev, idx);
2340 u64_stats_update_begin(&tx_stats(txo)->sync_compl);
2341 tx_stats(txo)->tx_compl += work_done;
2342 u64_stats_update_end(&tx_stats(txo)->sync_compl);
2344 return (work_done < budget); /* Done */
2347 int be_poll(struct napi_struct *napi, int budget)
2349 struct be_eq_obj *eqo = container_of(napi, struct be_eq_obj, napi);
2350 struct be_adapter *adapter = eqo->adapter;
2351 int max_work = 0, work, i, num_evts;
2354 num_evts = events_get(eqo);
2356 /* Process all TXQs serviced by this EQ */
2357 for (i = eqo->idx; i < adapter->num_tx_qs; i += adapter->num_evt_qs) {
2358 tx_done = be_process_tx(adapter, &adapter->tx_obj[i],
2364 /* This loop will iterate twice for EQ0 in which
2365 * completions of the last RXQ (default one) are also processed
2366 * For other EQs the loop iterates only once
2368 for (i = eqo->idx; i < adapter->num_rx_qs; i += adapter->num_evt_qs) {
2369 work = be_process_rx(&adapter->rx_obj[i], napi, budget);
2370 max_work = max(work, max_work);
2373 if (is_mcc_eqo(eqo))
2374 be_process_mcc(adapter);
2376 if (max_work < budget) {
2377 napi_complete(napi);
2378 be_eq_notify(adapter, eqo->q.id, true, false, num_evts);
2380 /* As we'll continue in polling mode, count and clear events */
2381 be_eq_notify(adapter, eqo->q.id, false, false, num_evts);
2386 void be_detect_error(struct be_adapter *adapter)
2388 u32 ue_lo = 0, ue_hi = 0, ue_lo_mask = 0, ue_hi_mask = 0;
2389 u32 sliport_status = 0, sliport_err1 = 0, sliport_err2 = 0;
2392 if (be_hw_error(adapter))
2395 if (lancer_chip(adapter)) {
2396 sliport_status = ioread32(adapter->db + SLIPORT_STATUS_OFFSET);
2397 if (sliport_status & SLIPORT_STATUS_ERR_MASK) {
2398 sliport_err1 = ioread32(adapter->db +
2399 SLIPORT_ERROR1_OFFSET);
2400 sliport_err2 = ioread32(adapter->db +
2401 SLIPORT_ERROR2_OFFSET);
2404 pci_read_config_dword(adapter->pdev,
2405 PCICFG_UE_STATUS_LOW, &ue_lo);
2406 pci_read_config_dword(adapter->pdev,
2407 PCICFG_UE_STATUS_HIGH, &ue_hi);
2408 pci_read_config_dword(adapter->pdev,
2409 PCICFG_UE_STATUS_LOW_MASK, &ue_lo_mask);
2410 pci_read_config_dword(adapter->pdev,
2411 PCICFG_UE_STATUS_HI_MASK, &ue_hi_mask);
2413 ue_lo = (ue_lo & ~ue_lo_mask);
2414 ue_hi = (ue_hi & ~ue_hi_mask);
2417 /* On certain platforms BE hardware can indicate spurious UEs.
2418 * Allow the h/w to stop working completely in case of a real UE.
2419 * Hence not setting the hw_error for UE detection.
2421 if (sliport_status & SLIPORT_STATUS_ERR_MASK) {
2422 adapter->hw_error = true;
2423 dev_err(&adapter->pdev->dev,
2424 "Error detected in the card\n");
2427 if (sliport_status & SLIPORT_STATUS_ERR_MASK) {
2428 dev_err(&adapter->pdev->dev,
2429 "ERR: sliport status 0x%x\n", sliport_status);
2430 dev_err(&adapter->pdev->dev,
2431 "ERR: sliport error1 0x%x\n", sliport_err1);
2432 dev_err(&adapter->pdev->dev,
2433 "ERR: sliport error2 0x%x\n", sliport_err2);
2437 for (i = 0; ue_lo; ue_lo >>= 1, i++) {
2439 dev_err(&adapter->pdev->dev,
2440 "UE: %s bit set\n", ue_status_low_desc[i]);
2445 for (i = 0; ue_hi; ue_hi >>= 1, i++) {
2447 dev_err(&adapter->pdev->dev,
2448 "UE: %s bit set\n", ue_status_hi_desc[i]);
2454 static void be_msix_disable(struct be_adapter *adapter)
2456 if (msix_enabled(adapter)) {
2457 pci_disable_msix(adapter->pdev);
2458 adapter->num_msix_vec = 0;
2459 adapter->num_msix_roce_vec = 0;
2463 static int be_msix_enable(struct be_adapter *adapter)
2465 int i, status, num_vec;
2466 struct device *dev = &adapter->pdev->dev;
2468 /* If RoCE is supported, program the max number of NIC vectors that
2469 * may be configured via set-channels, along with vectors needed for
2470 * RoCe. Else, just program the number we'll use initially.
2472 if (be_roce_supported(adapter))
2473 num_vec = min_t(int, 2 * be_max_eqs(adapter),
2474 2 * num_online_cpus());
2476 num_vec = adapter->cfg_num_qs;
2478 for (i = 0; i < num_vec; i++)
2479 adapter->msix_entries[i].entry = i;
2481 status = pci_enable_msix(adapter->pdev, adapter->msix_entries, num_vec);
2484 } else if (status >= MIN_MSIX_VECTORS) {
2486 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
2492 dev_warn(dev, "MSIx enable failed\n");
2494 /* INTx is not supported in VFs, so fail probe if enable_msix fails */
2495 if (!be_physfn(adapter))
2499 if (be_roce_supported(adapter) && num_vec > MIN_MSIX_VECTORS) {
2500 adapter->num_msix_roce_vec = num_vec / 2;
2501 dev_info(dev, "enabled %d MSI-x vector(s) for RoCE\n",
2502 adapter->num_msix_roce_vec);
2505 adapter->num_msix_vec = num_vec - adapter->num_msix_roce_vec;
2507 dev_info(dev, "enabled %d MSI-x vector(s) for NIC\n",
2508 adapter->num_msix_vec);
2512 static inline int be_msix_vec_get(struct be_adapter *adapter,
2513 struct be_eq_obj *eqo)
2515 return adapter->msix_entries[eqo->msix_idx].vector;
2518 static int be_msix_register(struct be_adapter *adapter)
2520 struct net_device *netdev = adapter->netdev;
2521 struct be_eq_obj *eqo;
2524 for_all_evt_queues(adapter, eqo, i) {
2525 sprintf(eqo->desc, "%s-q%d", netdev->name, i);
2526 vec = be_msix_vec_get(adapter, eqo);
2527 status = request_irq(vec, be_msix, 0, eqo->desc, eqo);
2534 for (i--, eqo = &adapter->eq_obj[i]; i >= 0; i--, eqo--)
2535 free_irq(be_msix_vec_get(adapter, eqo), eqo);
2536 dev_warn(&adapter->pdev->dev, "MSIX Request IRQ failed - err %d\n",
2538 be_msix_disable(adapter);
2542 static int be_irq_register(struct be_adapter *adapter)
2544 struct net_device *netdev = adapter->netdev;
2547 if (msix_enabled(adapter)) {
2548 status = be_msix_register(adapter);
2551 /* INTx is not supported for VF */
2552 if (!be_physfn(adapter))
2556 /* INTx: only the first EQ is used */
2557 netdev->irq = adapter->pdev->irq;
2558 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
2559 &adapter->eq_obj[0]);
2561 dev_err(&adapter->pdev->dev,
2562 "INTx request IRQ failed - err %d\n", status);
2566 adapter->isr_registered = true;
2570 static void be_irq_unregister(struct be_adapter *adapter)
2572 struct net_device *netdev = adapter->netdev;
2573 struct be_eq_obj *eqo;
2576 if (!adapter->isr_registered)
2580 if (!msix_enabled(adapter)) {
2581 free_irq(netdev->irq, &adapter->eq_obj[0]);
2586 for_all_evt_queues(adapter, eqo, i)
2587 free_irq(be_msix_vec_get(adapter, eqo), eqo);
2590 adapter->isr_registered = false;
2593 static void be_rx_qs_destroy(struct be_adapter *adapter)
2595 struct be_queue_info *q;
2596 struct be_rx_obj *rxo;
2599 for_all_rx_queues(adapter, rxo, i) {
2602 be_cmd_rxq_destroy(adapter, q);
2603 be_rx_cq_clean(rxo);
2605 be_queue_free(adapter, q);
2609 static int be_close(struct net_device *netdev)
2611 struct be_adapter *adapter = netdev_priv(netdev);
2612 struct be_eq_obj *eqo;
2615 be_roce_dev_close(adapter);
2617 if (adapter->flags & BE_FLAGS_NAPI_ENABLED) {
2618 for_all_evt_queues(adapter, eqo, i)
2619 napi_disable(&eqo->napi);
2620 adapter->flags &= ~BE_FLAGS_NAPI_ENABLED;
2623 be_async_mcc_disable(adapter);
2625 /* Wait for all pending tx completions to arrive so that
2626 * all tx skbs are freed.
2628 netif_tx_disable(netdev);
2629 be_tx_compl_clean(adapter);
2631 be_rx_qs_destroy(adapter);
2633 for_all_evt_queues(adapter, eqo, i) {
2634 if (msix_enabled(adapter))
2635 synchronize_irq(be_msix_vec_get(adapter, eqo));
2637 synchronize_irq(netdev->irq);
2641 be_irq_unregister(adapter);
2646 static int be_rx_qs_create(struct be_adapter *adapter)
2648 struct be_rx_obj *rxo;
2652 for_all_rx_queues(adapter, rxo, i) {
2653 rc = be_queue_alloc(adapter, &rxo->q, RX_Q_LEN,
2654 sizeof(struct be_eth_rx_d));
2659 /* The FW would like the default RXQ to be created first */
2660 rxo = default_rxo(adapter);
2661 rc = be_cmd_rxq_create(adapter, &rxo->q, rxo->cq.id, rx_frag_size,
2662 adapter->if_handle, false, &rxo->rss_id);
2666 for_all_rss_queues(adapter, rxo, i) {
2667 rc = be_cmd_rxq_create(adapter, &rxo->q, rxo->cq.id,
2668 rx_frag_size, adapter->if_handle,
2669 true, &rxo->rss_id);
2674 if (be_multi_rxq(adapter)) {
2675 for (j = 0; j < 128; j += adapter->num_rx_qs - 1) {
2676 for_all_rss_queues(adapter, rxo, i) {
2679 rsstable[j + i] = rxo->rss_id;
2682 adapter->rss_flags = RSS_ENABLE_TCP_IPV4 | RSS_ENABLE_IPV4 |
2683 RSS_ENABLE_TCP_IPV6 | RSS_ENABLE_IPV6;
2685 if (!BEx_chip(adapter))
2686 adapter->rss_flags |= RSS_ENABLE_UDP_IPV4 |
2687 RSS_ENABLE_UDP_IPV6;
2689 rc = be_cmd_rss_config(adapter, rsstable, adapter->rss_flags,
2692 adapter->rss_flags = 0;
2697 /* First time posting */
2698 for_all_rx_queues(adapter, rxo, i)
2699 be_post_rx_frags(rxo, GFP_KERNEL);
2703 static int be_open(struct net_device *netdev)
2705 struct be_adapter *adapter = netdev_priv(netdev);
2706 struct be_eq_obj *eqo;
2707 struct be_rx_obj *rxo;
2708 struct be_tx_obj *txo;
2712 status = be_rx_qs_create(adapter);
2716 status = be_irq_register(adapter);
2720 for_all_rx_queues(adapter, rxo, i)
2721 be_cq_notify(adapter, rxo->cq.id, true, 0);
2723 for_all_tx_queues(adapter, txo, i)
2724 be_cq_notify(adapter, txo->cq.id, true, 0);
2726 be_async_mcc_enable(adapter);
2728 for_all_evt_queues(adapter, eqo, i) {
2729 napi_enable(&eqo->napi);
2730 be_eq_notify(adapter, eqo->q.id, true, false, 0);
2732 adapter->flags |= BE_FLAGS_NAPI_ENABLED;
2734 status = be_cmd_link_status_query(adapter, NULL, &link_status, 0);
2736 be_link_status_update(adapter, link_status);
2738 netif_tx_start_all_queues(netdev);
2739 be_roce_dev_open(adapter);
2742 be_close(adapter->netdev);
2746 static int be_setup_wol(struct be_adapter *adapter, bool enable)
2748 struct be_dma_mem cmd;
2752 memset(mac, 0, ETH_ALEN);
2754 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
2755 cmd.va = dma_zalloc_coherent(&adapter->pdev->dev, cmd.size, &cmd.dma,
2761 status = pci_write_config_dword(adapter->pdev,
2762 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
2764 dev_err(&adapter->pdev->dev,
2765 "Could not enable Wake-on-lan\n");
2766 dma_free_coherent(&adapter->pdev->dev, cmd.size, cmd.va,
2770 status = be_cmd_enable_magic_wol(adapter,
2771 adapter->netdev->dev_addr, &cmd);
2772 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
2773 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
2775 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
2776 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
2777 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
2780 dma_free_coherent(&adapter->pdev->dev, cmd.size, cmd.va, cmd.dma);
2785 * Generate a seed MAC address from the PF MAC Address using jhash.
2786 * MAC Address for VFs are assigned incrementally starting from the seed.
2787 * These addresses are programmed in the ASIC by the PF and the VF driver
2788 * queries for the MAC address during its probe.
2790 static int be_vf_eth_addr_config(struct be_adapter *adapter)
2795 struct be_vf_cfg *vf_cfg;
2797 be_vf_eth_addr_generate(adapter, mac);
2799 for_all_vfs(adapter, vf_cfg, vf) {
2800 if (BEx_chip(adapter))
2801 status = be_cmd_pmac_add(adapter, mac,
2803 &vf_cfg->pmac_id, vf + 1);
2805 status = be_cmd_set_mac(adapter, mac, vf_cfg->if_handle,
2809 dev_err(&adapter->pdev->dev,
2810 "Mac address assignment failed for VF %d\n", vf);
2812 memcpy(vf_cfg->mac_addr, mac, ETH_ALEN);
2819 static int be_vfs_mac_query(struct be_adapter *adapter)
2823 struct be_vf_cfg *vf_cfg;
2824 bool active = false;
2826 for_all_vfs(adapter, vf_cfg, vf) {
2827 be_cmd_get_mac_from_list(adapter, mac, &active,
2828 &vf_cfg->pmac_id, 0);
2830 status = be_cmd_mac_addr_query(adapter, mac, false,
2831 vf_cfg->if_handle, 0);
2834 memcpy(vf_cfg->mac_addr, mac, ETH_ALEN);
2839 static void be_vf_clear(struct be_adapter *adapter)
2841 struct be_vf_cfg *vf_cfg;
2844 if (pci_vfs_assigned(adapter->pdev)) {
2845 dev_warn(&adapter->pdev->dev,
2846 "VFs are assigned to VMs: not disabling VFs\n");
2850 pci_disable_sriov(adapter->pdev);
2852 for_all_vfs(adapter, vf_cfg, vf) {
2853 if (BEx_chip(adapter))
2854 be_cmd_pmac_del(adapter, vf_cfg->if_handle,
2855 vf_cfg->pmac_id, vf + 1);
2857 be_cmd_set_mac(adapter, NULL, vf_cfg->if_handle,
2860 be_cmd_if_destroy(adapter, vf_cfg->if_handle, vf + 1);
2863 kfree(adapter->vf_cfg);
2864 adapter->num_vfs = 0;
2867 static void be_clear_queues(struct be_adapter *adapter)
2869 be_mcc_queues_destroy(adapter);
2870 be_rx_cqs_destroy(adapter);
2871 be_tx_queues_destroy(adapter);
2872 be_evt_queues_destroy(adapter);
2875 static void be_cancel_worker(struct be_adapter *adapter)
2877 if (adapter->flags & BE_FLAGS_WORKER_SCHEDULED) {
2878 cancel_delayed_work_sync(&adapter->work);
2879 adapter->flags &= ~BE_FLAGS_WORKER_SCHEDULED;
2883 static int be_clear(struct be_adapter *adapter)
2887 be_cancel_worker(adapter);
2889 if (sriov_enabled(adapter))
2890 be_vf_clear(adapter);
2892 /* delete the primary mac along with the uc-mac list */
2893 for (i = 0; i < (adapter->uc_macs + 1); i++)
2894 be_cmd_pmac_del(adapter, adapter->if_handle,
2895 adapter->pmac_id[i], 0);
2896 adapter->uc_macs = 0;
2898 be_cmd_if_destroy(adapter, adapter->if_handle, 0);
2900 be_clear_queues(adapter);
2902 kfree(adapter->pmac_id);
2903 adapter->pmac_id = NULL;
2905 be_msix_disable(adapter);
2909 static int be_vfs_if_create(struct be_adapter *adapter)
2911 struct be_resources res = {0};
2912 struct be_vf_cfg *vf_cfg;
2913 u32 cap_flags, en_flags, vf;
2916 cap_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
2917 BE_IF_FLAGS_MULTICAST;
2919 for_all_vfs(adapter, vf_cfg, vf) {
2920 if (!BE3_chip(adapter)) {
2921 status = be_cmd_get_profile_config(adapter, &res,
2924 cap_flags = res.if_cap_flags;
2927 /* If a FW profile exists, then cap_flags are updated */
2928 en_flags = cap_flags & (BE_IF_FLAGS_UNTAGGED |
2929 BE_IF_FLAGS_BROADCAST | BE_IF_FLAGS_MULTICAST);
2930 status = be_cmd_if_create(adapter, cap_flags, en_flags,
2931 &vf_cfg->if_handle, vf + 1);
2939 static int be_vf_setup_init(struct be_adapter *adapter)
2941 struct be_vf_cfg *vf_cfg;
2944 adapter->vf_cfg = kcalloc(adapter->num_vfs, sizeof(*vf_cfg),
2946 if (!adapter->vf_cfg)
2949 for_all_vfs(adapter, vf_cfg, vf) {
2950 vf_cfg->if_handle = -1;
2951 vf_cfg->pmac_id = -1;
2956 static int be_vf_setup(struct be_adapter *adapter)
2958 struct be_vf_cfg *vf_cfg;
2959 u16 def_vlan, lnk_speed;
2960 int status, old_vfs, vf;
2961 struct device *dev = &adapter->pdev->dev;
2964 old_vfs = pci_num_vf(adapter->pdev);
2966 dev_info(dev, "%d VFs are already enabled\n", old_vfs);
2967 if (old_vfs != num_vfs)
2968 dev_warn(dev, "Ignoring num_vfs=%d setting\n", num_vfs);
2969 adapter->num_vfs = old_vfs;
2971 if (num_vfs > be_max_vfs(adapter))
2972 dev_info(dev, "Device supports %d VFs and not %d\n",
2973 be_max_vfs(adapter), num_vfs);
2974 adapter->num_vfs = min_t(u16, num_vfs, be_max_vfs(adapter));
2975 if (!adapter->num_vfs)
2979 status = be_vf_setup_init(adapter);
2984 for_all_vfs(adapter, vf_cfg, vf) {
2985 status = be_cmd_get_if_id(adapter, vf_cfg, vf);
2990 status = be_vfs_if_create(adapter);
2996 status = be_vfs_mac_query(adapter);
3000 status = be_vf_eth_addr_config(adapter);
3005 for_all_vfs(adapter, vf_cfg, vf) {
3006 /* Allow VFs to programs MAC/VLAN filters */
3007 status = be_cmd_get_fn_privileges(adapter, &privileges, vf + 1);
3008 if (!status && !(privileges & BE_PRIV_FILTMGMT)) {
3009 status = be_cmd_set_fn_privileges(adapter,
3014 dev_info(dev, "VF%d has FILTMGMT privilege\n",
3018 /* BE3 FW, by default, caps VF TX-rate to 100mbps.
3019 * Allow full available bandwidth
3021 if (BE3_chip(adapter) && !old_vfs)
3022 be_cmd_set_qos(adapter, 1000, vf+1);
3024 status = be_cmd_link_status_query(adapter, &lnk_speed,
3027 vf_cfg->tx_rate = lnk_speed;
3029 status = be_cmd_get_hsw_config(adapter, &def_vlan,
3030 vf + 1, vf_cfg->if_handle, NULL);
3033 vf_cfg->def_vid = def_vlan;
3036 be_cmd_enable_vf(adapter, vf + 1);
3040 status = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
3042 dev_err(dev, "SRIOV enable failed\n");
3043 adapter->num_vfs = 0;
3049 dev_err(dev, "VF setup failed\n");
3050 be_vf_clear(adapter);
3054 /* On BE2/BE3 FW does not suggest the supported limits */
3055 static void BEx_get_resources(struct be_adapter *adapter,
3056 struct be_resources *res)
3058 struct pci_dev *pdev = adapter->pdev;
3059 bool use_sriov = false;
3061 if (BE3_chip(adapter) && sriov_want(adapter)) {
3064 max_vfs = pci_sriov_get_totalvfs(pdev);
3065 res->max_vfs = max_vfs > 0 ? min(MAX_VFS, max_vfs) : 0;
3066 use_sriov = res->max_vfs;
3069 if (be_physfn(adapter))
3070 res->max_uc_mac = BE_UC_PMAC_COUNT;
3072 res->max_uc_mac = BE_VF_UC_PMAC_COUNT;
3074 if (adapter->function_mode & FLEX10_MODE)
3075 res->max_vlans = BE_NUM_VLANS_SUPPORTED/8;
3076 else if (adapter->function_mode & UMC_ENABLED)
3077 res->max_vlans = BE_UMC_NUM_VLANS_SUPPORTED;
3079 res->max_vlans = BE_NUM_VLANS_SUPPORTED;
3080 res->max_mcast_mac = BE_MAX_MC;
3082 /* For BE3 1Gb ports, F/W does not properly support multiple TXQs */
3083 if (BE2_chip(adapter) || use_sriov || be_is_mc(adapter) ||
3084 !be_physfn(adapter) || (adapter->port_num > 1))
3087 res->max_tx_qs = BE3_MAX_TX_QS;
3089 if ((adapter->function_caps & BE_FUNCTION_CAPS_RSS) &&
3090 !use_sriov && be_physfn(adapter))
3091 res->max_rss_qs = (adapter->be3_native) ?
3092 BE3_MAX_RSS_QS : BE2_MAX_RSS_QS;
3093 res->max_rx_qs = res->max_rss_qs + 1;
3095 res->max_evt_qs = be_physfn(adapter) ? BE3_MAX_EVT_QS : 1;
3097 res->if_cap_flags = BE_IF_CAP_FLAGS_WANT;
3098 if (!(adapter->function_caps & BE_FUNCTION_CAPS_RSS))
3099 res->if_cap_flags &= ~BE_IF_FLAGS_RSS;
3102 static void be_setup_init(struct be_adapter *adapter)
3104 adapter->vlan_prio_bmap = 0xff;
3105 adapter->phy.link_speed = -1;
3106 adapter->if_handle = -1;
3107 adapter->be3_native = false;
3108 adapter->promiscuous = false;
3109 if (be_physfn(adapter))
3110 adapter->cmd_privileges = MAX_PRIVILEGES;
3112 adapter->cmd_privileges = MIN_PRIVILEGES;
3115 static int be_get_resources(struct be_adapter *adapter)
3117 struct device *dev = &adapter->pdev->dev;
3118 struct be_resources res = {0};
3121 if (BEx_chip(adapter)) {
3122 BEx_get_resources(adapter, &res);
3126 /* For Lancer, SH etc read per-function resource limits from FW.
3127 * GET_FUNC_CONFIG returns per function guaranteed limits.
3128 * GET_PROFILE_CONFIG returns PCI-E related limits PF-pool limits
3130 if (!BEx_chip(adapter)) {
3131 status = be_cmd_get_func_config(adapter, &res);
3135 /* If RoCE may be enabled stash away half the EQs for RoCE */
3136 if (be_roce_supported(adapter))
3137 res.max_evt_qs /= 2;
3140 if (be_physfn(adapter)) {
3141 status = be_cmd_get_profile_config(adapter, &res, 0);
3144 adapter->res.max_vfs = res.max_vfs;
3147 dev_info(dev, "Max: txqs %d, rxqs %d, rss %d, eqs %d, vfs %d\n",
3148 be_max_txqs(adapter), be_max_rxqs(adapter),
3149 be_max_rss(adapter), be_max_eqs(adapter),
3150 be_max_vfs(adapter));
3151 dev_info(dev, "Max: uc-macs %d, mc-macs %d, vlans %d\n",
3152 be_max_uc(adapter), be_max_mc(adapter),
3153 be_max_vlans(adapter));
3159 /* Routine to query per function resource limits */
3160 static int be_get_config(struct be_adapter *adapter)
3164 status = be_cmd_query_fw_cfg(adapter, &adapter->port_num,
3165 &adapter->function_mode,
3166 &adapter->function_caps,
3167 &adapter->asic_rev);
3171 status = be_get_resources(adapter);
3175 /* primary mac needs 1 pmac entry */
3176 adapter->pmac_id = kcalloc(be_max_uc(adapter) + 1, sizeof(u32),
3178 if (!adapter->pmac_id)
3181 /* Sanitize cfg_num_qs based on HW and platform limits */
3182 adapter->cfg_num_qs = min(adapter->cfg_num_qs, be_max_qs(adapter));
3187 static int be_mac_setup(struct be_adapter *adapter)
3192 if (is_zero_ether_addr(adapter->netdev->dev_addr)) {
3193 status = be_cmd_get_perm_mac(adapter, mac);
3197 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
3198 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
3200 /* Maybe the HW was reset; dev_addr must be re-programmed */
3201 memcpy(mac, adapter->netdev->dev_addr, ETH_ALEN);
3204 /* On BE3 VFs this cmd may fail due to lack of privilege.
3205 * Ignore the failure as in this case pmac_id is fetched
3206 * in the IFACE_CREATE cmd.
3208 be_cmd_pmac_add(adapter, mac, adapter->if_handle,
3209 &adapter->pmac_id[0], 0);
3213 static void be_schedule_worker(struct be_adapter *adapter)
3215 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
3216 adapter->flags |= BE_FLAGS_WORKER_SCHEDULED;
3219 static int be_setup_queues(struct be_adapter *adapter)
3221 struct net_device *netdev = adapter->netdev;
3224 status = be_evt_queues_create(adapter);
3228 status = be_tx_qs_create(adapter);
3232 status = be_rx_cqs_create(adapter);
3236 status = be_mcc_queues_create(adapter);
3240 status = netif_set_real_num_rx_queues(netdev, adapter->num_rx_qs);
3244 status = netif_set_real_num_tx_queues(netdev, adapter->num_tx_qs);
3250 dev_err(&adapter->pdev->dev, "queue_setup failed\n");
3254 int be_update_queues(struct be_adapter *adapter)
3256 struct net_device *netdev = adapter->netdev;
3259 if (netif_running(netdev))
3262 be_cancel_worker(adapter);
3264 /* If any vectors have been shared with RoCE we cannot re-program
3267 if (!adapter->num_msix_roce_vec)
3268 be_msix_disable(adapter);
3270 be_clear_queues(adapter);
3272 if (!msix_enabled(adapter)) {
3273 status = be_msix_enable(adapter);
3278 status = be_setup_queues(adapter);
3282 be_schedule_worker(adapter);
3284 if (netif_running(netdev))
3285 status = be_open(netdev);
3290 static int be_setup(struct be_adapter *adapter)
3292 struct device *dev = &adapter->pdev->dev;
3293 u32 tx_fc, rx_fc, en_flags;
3296 be_setup_init(adapter);
3298 if (!lancer_chip(adapter))
3299 be_cmd_req_native_mode(adapter);
3301 status = be_get_config(adapter);
3305 status = be_msix_enable(adapter);
3309 en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
3310 BE_IF_FLAGS_MULTICAST | BE_IF_FLAGS_PASS_L3L4_ERRORS;
3311 if (adapter->function_caps & BE_FUNCTION_CAPS_RSS)
3312 en_flags |= BE_IF_FLAGS_RSS;
3313 en_flags = en_flags & be_if_cap_flags(adapter);
3314 status = be_cmd_if_create(adapter, be_if_cap_flags(adapter), en_flags,
3315 &adapter->if_handle, 0);
3319 /* Updating real_num_tx/rx_queues() requires rtnl_lock() */
3321 status = be_setup_queues(adapter);
3326 be_cmd_get_fn_privileges(adapter, &adapter->cmd_privileges, 0);
3327 /* In UMC mode FW does not return right privileges.
3328 * Override with correct privilege equivalent to PF.
3330 if (be_is_mc(adapter))
3331 adapter->cmd_privileges = MAX_PRIVILEGES;
3333 status = be_mac_setup(adapter);
3337 be_cmd_get_fw_ver(adapter, adapter->fw_ver, adapter->fw_on_flash);
3339 if (adapter->vlans_added)
3340 be_vid_config(adapter);
3342 be_set_rx_mode(adapter->netdev);
3344 be_cmd_get_flow_control(adapter, &tx_fc, &rx_fc);
3346 if (rx_fc != adapter->rx_fc || tx_fc != adapter->tx_fc)
3347 be_cmd_set_flow_control(adapter, adapter->tx_fc,
3350 if (sriov_want(adapter)) {
3351 if (be_max_vfs(adapter))
3352 be_vf_setup(adapter);
3354 dev_warn(dev, "device doesn't support SRIOV\n");
3357 status = be_cmd_get_phy_info(adapter);
3358 if (!status && be_pause_supported(adapter))
3359 adapter->phy.fc_autoneg = 1;
3361 be_schedule_worker(adapter);
3368 #ifdef CONFIG_NET_POLL_CONTROLLER
3369 static void be_netpoll(struct net_device *netdev)
3371 struct be_adapter *adapter = netdev_priv(netdev);
3372 struct be_eq_obj *eqo;
3375 for_all_evt_queues(adapter, eqo, i) {
3376 be_eq_notify(eqo->adapter, eqo->q.id, false, true, 0);
3377 napi_schedule(&eqo->napi);
3384 #define FW_FILE_HDR_SIGN "ServerEngines Corp. "
3385 static char flash_cookie[2][16] = {"*** SE FLAS", "H DIRECTORY *** "};
3387 static bool be_flash_redboot(struct be_adapter *adapter,
3388 const u8 *p, u32 img_start, int image_size,
3395 crc_offset = hdr_size + img_start + image_size - 4;
3399 status = be_cmd_get_flash_crc(adapter, flashed_crc,
3402 dev_err(&adapter->pdev->dev,
3403 "could not get crc from flash, not flashing redboot\n");
3407 /*update redboot only if crc does not match*/
3408 if (!memcmp(flashed_crc, p, 4))
3414 static bool phy_flashing_required(struct be_adapter *adapter)
3416 return (adapter->phy.phy_type == TN_8022 &&
3417 adapter->phy.interface_type == PHY_TYPE_BASET_10GB);
3420 static bool is_comp_in_ufi(struct be_adapter *adapter,
3421 struct flash_section_info *fsec, int type)
3423 int i = 0, img_type = 0;
3424 struct flash_section_info_g2 *fsec_g2 = NULL;
3426 if (BE2_chip(adapter))
3427 fsec_g2 = (struct flash_section_info_g2 *)fsec;
3429 for (i = 0; i < MAX_FLASH_COMP; i++) {
3431 img_type = le32_to_cpu(fsec_g2->fsec_entry[i].type);
3433 img_type = le32_to_cpu(fsec->fsec_entry[i].type);
3435 if (img_type == type)
3442 static struct flash_section_info *get_fsec_info(struct be_adapter *adapter,
3444 const struct firmware *fw)
3446 struct flash_section_info *fsec = NULL;
3447 const u8 *p = fw->data;
3450 while (p < (fw->data + fw->size)) {
3451 fsec = (struct flash_section_info *)p;
3452 if (!memcmp(flash_cookie, fsec->cookie, sizeof(flash_cookie)))
3459 static int be_flash(struct be_adapter *adapter, const u8 *img,
3460 struct be_dma_mem *flash_cmd, int optype, int img_size)
3462 u32 total_bytes = 0, flash_op, num_bytes = 0;
3464 struct be_cmd_write_flashrom *req = flash_cmd->va;
3466 total_bytes = img_size;
3467 while (total_bytes) {
3468 num_bytes = min_t(u32, 32*1024, total_bytes);
3470 total_bytes -= num_bytes;
3473 if (optype == OPTYPE_PHY_FW)
3474 flash_op = FLASHROM_OPER_PHY_FLASH;
3476 flash_op = FLASHROM_OPER_FLASH;
3478 if (optype == OPTYPE_PHY_FW)
3479 flash_op = FLASHROM_OPER_PHY_SAVE;
3481 flash_op = FLASHROM_OPER_SAVE;
3484 memcpy(req->data_buf, img, num_bytes);
3486 status = be_cmd_write_flashrom(adapter, flash_cmd, optype,
3487 flash_op, num_bytes);
3489 if (status == ILLEGAL_IOCTL_REQ &&
3490 optype == OPTYPE_PHY_FW)
3492 dev_err(&adapter->pdev->dev,
3493 "cmd to write to flash rom failed.\n");
3500 /* For BE2, BE3 and BE3-R */
3501 static int be_flash_BEx(struct be_adapter *adapter,
3502 const struct firmware *fw,
3503 struct be_dma_mem *flash_cmd,
3507 int status = 0, i, filehdr_size = 0;
3508 int img_hdrs_size = (num_of_images * sizeof(struct image_hdr));
3509 const u8 *p = fw->data;
3510 const struct flash_comp *pflashcomp;
3511 int num_comp, redboot;
3512 struct flash_section_info *fsec = NULL;
3514 struct flash_comp gen3_flash_types[] = {
3515 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, OPTYPE_ISCSI_ACTIVE,
3516 FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_iSCSI},
3517 { FLASH_REDBOOT_START_g3, OPTYPE_REDBOOT,
3518 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3, IMAGE_BOOT_CODE},
3519 { FLASH_iSCSI_BIOS_START_g3, OPTYPE_BIOS,
3520 FLASH_BIOS_IMAGE_MAX_SIZE_g3, IMAGE_OPTION_ROM_ISCSI},
3521 { FLASH_PXE_BIOS_START_g3, OPTYPE_PXE_BIOS,
3522 FLASH_BIOS_IMAGE_MAX_SIZE_g3, IMAGE_OPTION_ROM_PXE},
3523 { FLASH_FCoE_BIOS_START_g3, OPTYPE_FCOE_BIOS,
3524 FLASH_BIOS_IMAGE_MAX_SIZE_g3, IMAGE_OPTION_ROM_FCoE},
3525 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, OPTYPE_ISCSI_BACKUP,
3526 FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_BACKUP_iSCSI},
3527 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, OPTYPE_FCOE_FW_ACTIVE,
3528 FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_FCoE},
3529 { FLASH_FCoE_BACKUP_IMAGE_START_g3, OPTYPE_FCOE_FW_BACKUP,
3530 FLASH_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_BACKUP_FCoE},
3531 { FLASH_NCSI_START_g3, OPTYPE_NCSI_FW,
3532 FLASH_NCSI_IMAGE_MAX_SIZE_g3, IMAGE_NCSI},
3533 { FLASH_PHY_FW_START_g3, OPTYPE_PHY_FW,
3534 FLASH_PHY_FW_IMAGE_MAX_SIZE_g3, IMAGE_FIRMWARE_PHY}
3537 struct flash_comp gen2_flash_types[] = {
3538 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, OPTYPE_ISCSI_ACTIVE,
3539 FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_iSCSI},
3540 { FLASH_REDBOOT_START_g2, OPTYPE_REDBOOT,
3541 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2, IMAGE_BOOT_CODE},
3542 { FLASH_iSCSI_BIOS_START_g2, OPTYPE_BIOS,
3543 FLASH_BIOS_IMAGE_MAX_SIZE_g2, IMAGE_OPTION_ROM_ISCSI},
3544 { FLASH_PXE_BIOS_START_g2, OPTYPE_PXE_BIOS,
3545 FLASH_BIOS_IMAGE_MAX_SIZE_g2, IMAGE_OPTION_ROM_PXE},
3546 { FLASH_FCoE_BIOS_START_g2, OPTYPE_FCOE_BIOS,
3547 FLASH_BIOS_IMAGE_MAX_SIZE_g2, IMAGE_OPTION_ROM_FCoE},
3548 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, OPTYPE_ISCSI_BACKUP,
3549 FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_BACKUP_iSCSI},
3550 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, OPTYPE_FCOE_FW_ACTIVE,
3551 FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_FCoE},
3552 { FLASH_FCoE_BACKUP_IMAGE_START_g2, OPTYPE_FCOE_FW_BACKUP,
3553 FLASH_IMAGE_MAX_SIZE_g2, IMAGE_FIRMWARE_BACKUP_FCoE}
3556 if (BE3_chip(adapter)) {
3557 pflashcomp = gen3_flash_types;
3558 filehdr_size = sizeof(struct flash_file_hdr_g3);
3559 num_comp = ARRAY_SIZE(gen3_flash_types);
3561 pflashcomp = gen2_flash_types;
3562 filehdr_size = sizeof(struct flash_file_hdr_g2);
3563 num_comp = ARRAY_SIZE(gen2_flash_types);
3566 /* Get flash section info*/
3567 fsec = get_fsec_info(adapter, filehdr_size + img_hdrs_size, fw);
3569 dev_err(&adapter->pdev->dev,
3570 "Invalid Cookie. UFI corrupted ?\n");
3573 for (i = 0; i < num_comp; i++) {
3574 if (!is_comp_in_ufi(adapter, fsec, pflashcomp[i].img_type))
3577 if ((pflashcomp[i].optype == OPTYPE_NCSI_FW) &&
3578 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
3581 if (pflashcomp[i].optype == OPTYPE_PHY_FW &&
3582 !phy_flashing_required(adapter))
3585 if (pflashcomp[i].optype == OPTYPE_REDBOOT) {
3586 redboot = be_flash_redboot(adapter, fw->data,
3587 pflashcomp[i].offset, pflashcomp[i].size,
3588 filehdr_size + img_hdrs_size);
3594 p += filehdr_size + pflashcomp[i].offset + img_hdrs_size;
3595 if (p + pflashcomp[i].size > fw->data + fw->size)
3598 status = be_flash(adapter, p, flash_cmd, pflashcomp[i].optype,
3599 pflashcomp[i].size);
3601 dev_err(&adapter->pdev->dev,
3602 "Flashing section type %d failed.\n",
3603 pflashcomp[i].img_type);
3610 static int be_flash_skyhawk(struct be_adapter *adapter,
3611 const struct firmware *fw,
3612 struct be_dma_mem *flash_cmd, int num_of_images)
3614 int status = 0, i, filehdr_size = 0;
3615 int img_offset, img_size, img_optype, redboot;
3616 int img_hdrs_size = num_of_images * sizeof(struct image_hdr);
3617 const u8 *p = fw->data;
3618 struct flash_section_info *fsec = NULL;
3620 filehdr_size = sizeof(struct flash_file_hdr_g3);
3621 fsec = get_fsec_info(adapter, filehdr_size + img_hdrs_size, fw);
3623 dev_err(&adapter->pdev->dev,
3624 "Invalid Cookie. UFI corrupted ?\n");
3628 for (i = 0; i < le32_to_cpu(fsec->fsec_hdr.num_images); i++) {
3629 img_offset = le32_to_cpu(fsec->fsec_entry[i].offset);
3630 img_size = le32_to_cpu(fsec->fsec_entry[i].pad_size);
3632 switch (le32_to_cpu(fsec->fsec_entry[i].type)) {
3633 case IMAGE_FIRMWARE_iSCSI:
3634 img_optype = OPTYPE_ISCSI_ACTIVE;
3636 case IMAGE_BOOT_CODE:
3637 img_optype = OPTYPE_REDBOOT;
3639 case IMAGE_OPTION_ROM_ISCSI:
3640 img_optype = OPTYPE_BIOS;
3642 case IMAGE_OPTION_ROM_PXE:
3643 img_optype = OPTYPE_PXE_BIOS;
3645 case IMAGE_OPTION_ROM_FCoE:
3646 img_optype = OPTYPE_FCOE_BIOS;
3648 case IMAGE_FIRMWARE_BACKUP_iSCSI:
3649 img_optype = OPTYPE_ISCSI_BACKUP;
3652 img_optype = OPTYPE_NCSI_FW;
3658 if (img_optype == OPTYPE_REDBOOT) {
3659 redboot = be_flash_redboot(adapter, fw->data,
3660 img_offset, img_size,
3661 filehdr_size + img_hdrs_size);
3667 p += filehdr_size + img_offset + img_hdrs_size;
3668 if (p + img_size > fw->data + fw->size)
3671 status = be_flash(adapter, p, flash_cmd, img_optype, img_size);
3673 dev_err(&adapter->pdev->dev,
3674 "Flashing section type %d failed.\n",
3675 fsec->fsec_entry[i].type);
3682 static int lancer_fw_download(struct be_adapter *adapter,
3683 const struct firmware *fw)
3685 #define LANCER_FW_DOWNLOAD_CHUNK (32 * 1024)
3686 #define LANCER_FW_DOWNLOAD_LOCATION "/prg"
3687 struct be_dma_mem flash_cmd;
3688 const u8 *data_ptr = NULL;
3689 u8 *dest_image_ptr = NULL;
3690 size_t image_size = 0;
3692 u32 data_written = 0;
3698 if (!IS_ALIGNED(fw->size, sizeof(u32))) {
3699 dev_err(&adapter->pdev->dev,
3700 "FW Image not properly aligned. "
3701 "Length must be 4 byte aligned.\n");
3703 goto lancer_fw_exit;
3706 flash_cmd.size = sizeof(struct lancer_cmd_req_write_object)
3707 + LANCER_FW_DOWNLOAD_CHUNK;
3708 flash_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, flash_cmd.size,
3709 &flash_cmd.dma, GFP_KERNEL);
3710 if (!flash_cmd.va) {
3712 goto lancer_fw_exit;
3715 dest_image_ptr = flash_cmd.va +
3716 sizeof(struct lancer_cmd_req_write_object);
3717 image_size = fw->size;
3718 data_ptr = fw->data;
3720 while (image_size) {
3721 chunk_size = min_t(u32, image_size, LANCER_FW_DOWNLOAD_CHUNK);
3723 /* Copy the image chunk content. */
3724 memcpy(dest_image_ptr, data_ptr, chunk_size);
3726 status = lancer_cmd_write_object(adapter, &flash_cmd,
3728 LANCER_FW_DOWNLOAD_LOCATION,
3729 &data_written, &change_status,
3734 offset += data_written;
3735 data_ptr += data_written;
3736 image_size -= data_written;
3740 /* Commit the FW written */
3741 status = lancer_cmd_write_object(adapter, &flash_cmd,
3743 LANCER_FW_DOWNLOAD_LOCATION,
3744 &data_written, &change_status,
3748 dma_free_coherent(&adapter->pdev->dev, flash_cmd.size, flash_cmd.va,
3751 dev_err(&adapter->pdev->dev,
3752 "Firmware load error. "
3753 "Status code: 0x%x Additional Status: 0x%x\n",
3754 status, add_status);
3755 goto lancer_fw_exit;
3758 if (change_status == LANCER_FW_RESET_NEEDED) {
3759 status = lancer_physdev_ctrl(adapter,
3760 PHYSDEV_CONTROL_FW_RESET_MASK);
3762 dev_err(&adapter->pdev->dev,
3763 "Adapter busy for FW reset.\n"
3764 "New FW will not be active.\n");
3765 goto lancer_fw_exit;
3767 } else if (change_status != LANCER_NO_RESET_NEEDED) {
3768 dev_err(&adapter->pdev->dev,
3769 "System reboot required for new FW"
3773 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
3780 #define UFI_TYPE3R 10
3782 static int be_get_ufi_type(struct be_adapter *adapter,
3783 struct flash_file_hdr_g3 *fhdr)
3786 goto be_get_ufi_exit;
3788 if (skyhawk_chip(adapter) && fhdr->build[0] == '4')
3790 else if (BE3_chip(adapter) && fhdr->build[0] == '3') {
3791 if (fhdr->asic_type_rev == 0x10)
3795 } else if (BE2_chip(adapter) && fhdr->build[0] == '2')
3799 dev_err(&adapter->pdev->dev,
3800 "UFI and Interface are not compatible for flashing\n");
3804 static int be_fw_download(struct be_adapter *adapter, const struct firmware* fw)
3806 struct flash_file_hdr_g3 *fhdr3;
3807 struct image_hdr *img_hdr_ptr = NULL;
3808 struct be_dma_mem flash_cmd;
3810 int status = 0, i = 0, num_imgs = 0, ufi_type = 0;
3812 flash_cmd.size = sizeof(struct be_cmd_write_flashrom);
3813 flash_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, flash_cmd.size,
3814 &flash_cmd.dma, GFP_KERNEL);
3815 if (!flash_cmd.va) {
3821 fhdr3 = (struct flash_file_hdr_g3 *)p;
3823 ufi_type = be_get_ufi_type(adapter, fhdr3);
3825 num_imgs = le32_to_cpu(fhdr3->num_imgs);
3826 for (i = 0; i < num_imgs; i++) {
3827 img_hdr_ptr = (struct image_hdr *)(fw->data +
3828 (sizeof(struct flash_file_hdr_g3) +
3829 i * sizeof(struct image_hdr)));
3830 if (le32_to_cpu(img_hdr_ptr->imageid) == 1) {
3833 status = be_flash_skyhawk(adapter, fw,
3834 &flash_cmd, num_imgs);
3837 status = be_flash_BEx(adapter, fw, &flash_cmd,
3841 /* Do not flash this ufi on BE3-R cards */
3842 if (adapter->asic_rev < 0x10)
3843 status = be_flash_BEx(adapter, fw,
3848 dev_err(&adapter->pdev->dev,
3849 "Can't load BE3 UFI on BE3R\n");
3855 if (ufi_type == UFI_TYPE2)
3856 status = be_flash_BEx(adapter, fw, &flash_cmd, 0);
3857 else if (ufi_type == -1)
3860 dma_free_coherent(&adapter->pdev->dev, flash_cmd.size, flash_cmd.va,
3863 dev_err(&adapter->pdev->dev, "Firmware load error\n");
3867 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
3873 int be_load_fw(struct be_adapter *adapter, u8 *fw_file)
3875 const struct firmware *fw;
3878 if (!netif_running(adapter->netdev)) {
3879 dev_err(&adapter->pdev->dev,
3880 "Firmware load not allowed (interface is down)\n");
3884 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
3888 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
3890 if (lancer_chip(adapter))
3891 status = lancer_fw_download(adapter, fw);
3893 status = be_fw_download(adapter, fw);
3896 be_cmd_get_fw_ver(adapter, adapter->fw_ver,
3897 adapter->fw_on_flash);
3900 release_firmware(fw);
3904 static int be_ndo_bridge_setlink(struct net_device *dev,
3905 struct nlmsghdr *nlh)
3907 struct be_adapter *adapter = netdev_priv(dev);
3908 struct nlattr *attr, *br_spec;
3913 if (!sriov_enabled(adapter))
3916 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3918 nla_for_each_nested(attr, br_spec, rem) {
3919 if (nla_type(attr) != IFLA_BRIDGE_MODE)
3922 mode = nla_get_u16(attr);
3923 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
3926 status = be_cmd_set_hsw_config(adapter, 0, 0,
3928 mode == BRIDGE_MODE_VEPA ?
3929 PORT_FWD_TYPE_VEPA :
3934 dev_info(&adapter->pdev->dev, "enabled switch mode: %s\n",
3935 mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
3940 dev_err(&adapter->pdev->dev, "Failed to set switch mode %s\n",
3941 mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
3946 static int be_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3947 struct net_device *dev,
3950 struct be_adapter *adapter = netdev_priv(dev);
3954 if (!sriov_enabled(adapter))
3957 /* BE and Lancer chips support VEB mode only */
3958 if (BEx_chip(adapter) || lancer_chip(adapter)) {
3959 hsw_mode = PORT_FWD_TYPE_VEB;
3961 status = be_cmd_get_hsw_config(adapter, NULL, 0,
3962 adapter->if_handle, &hsw_mode);
3967 return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
3968 hsw_mode == PORT_FWD_TYPE_VEPA ?
3969 BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB);
3972 static const struct net_device_ops be_netdev_ops = {
3973 .ndo_open = be_open,
3974 .ndo_stop = be_close,
3975 .ndo_start_xmit = be_xmit,
3976 .ndo_set_rx_mode = be_set_rx_mode,
3977 .ndo_set_mac_address = be_mac_addr_set,
3978 .ndo_change_mtu = be_change_mtu,
3979 .ndo_get_stats64 = be_get_stats64,
3980 .ndo_validate_addr = eth_validate_addr,
3981 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
3982 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
3983 .ndo_set_vf_mac = be_set_vf_mac,
3984 .ndo_set_vf_vlan = be_set_vf_vlan,
3985 .ndo_set_vf_tx_rate = be_set_vf_tx_rate,
3986 .ndo_get_vf_config = be_get_vf_config,
3987 #ifdef CONFIG_NET_POLL_CONTROLLER
3988 .ndo_poll_controller = be_netpoll,
3990 .ndo_bridge_setlink = be_ndo_bridge_setlink,
3991 .ndo_bridge_getlink = be_ndo_bridge_getlink,
3994 static void be_netdev_init(struct net_device *netdev)
3996 struct be_adapter *adapter = netdev_priv(netdev);
3998 netdev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
3999 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM |
4000 NETIF_F_HW_VLAN_CTAG_TX;
4001 if (be_multi_rxq(adapter))
4002 netdev->hw_features |= NETIF_F_RXHASH;
4004 netdev->features |= netdev->hw_features |
4005 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_FILTER;
4007 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
4008 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
4010 netdev->priv_flags |= IFF_UNICAST_FLT;
4012 netdev->flags |= IFF_MULTICAST;
4014 netif_set_gso_max_size(netdev, 65535 - ETH_HLEN);
4016 netdev->netdev_ops = &be_netdev_ops;
4018 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
4021 static void be_unmap_pci_bars(struct be_adapter *adapter)
4024 pci_iounmap(adapter->pdev, adapter->csr);
4026 pci_iounmap(adapter->pdev, adapter->db);
4029 static int db_bar(struct be_adapter *adapter)
4031 if (lancer_chip(adapter) || !be_physfn(adapter))
4037 static int be_roce_map_pci_bars(struct be_adapter *adapter)
4039 if (skyhawk_chip(adapter)) {
4040 adapter->roce_db.size = 4096;
4041 adapter->roce_db.io_addr = pci_resource_start(adapter->pdev,
4043 adapter->roce_db.total_size = pci_resource_len(adapter->pdev,
4049 static int be_map_pci_bars(struct be_adapter *adapter)
4054 pci_read_config_dword(adapter->pdev, SLI_INTF_REG_OFFSET, &sli_intf);
4055 adapter->if_type = (sli_intf & SLI_INTF_IF_TYPE_MASK) >>
4056 SLI_INTF_IF_TYPE_SHIFT;
4058 if (BEx_chip(adapter) && be_physfn(adapter)) {
4059 adapter->csr = pci_iomap(adapter->pdev, 2, 0);
4060 if (adapter->csr == NULL)
4064 addr = pci_iomap(adapter->pdev, db_bar(adapter), 0);
4069 be_roce_map_pci_bars(adapter);
4073 be_unmap_pci_bars(adapter);
4077 static void be_ctrl_cleanup(struct be_adapter *adapter)
4079 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
4081 be_unmap_pci_bars(adapter);
4084 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
4087 mem = &adapter->rx_filter;
4089 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
4093 static int be_ctrl_init(struct be_adapter *adapter)
4095 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
4096 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
4097 struct be_dma_mem *rx_filter = &adapter->rx_filter;
4101 pci_read_config_dword(adapter->pdev, SLI_INTF_REG_OFFSET, &sli_intf);
4102 adapter->sli_family = (sli_intf & SLI_INTF_FAMILY_MASK) >>
4103 SLI_INTF_FAMILY_SHIFT;
4104 adapter->virtfn = (sli_intf & SLI_INTF_FT_MASK) ? 1 : 0;
4106 status = be_map_pci_bars(adapter);
4110 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
4111 mbox_mem_alloc->va = dma_alloc_coherent(&adapter->pdev->dev,
4112 mbox_mem_alloc->size,
4113 &mbox_mem_alloc->dma,
4115 if (!mbox_mem_alloc->va) {
4117 goto unmap_pci_bars;
4119 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
4120 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
4121 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
4122 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
4124 rx_filter->size = sizeof(struct be_cmd_req_rx_filter);
4125 rx_filter->va = dma_zalloc_coherent(&adapter->pdev->dev,
4126 rx_filter->size, &rx_filter->dma,
4128 if (rx_filter->va == NULL) {
4133 mutex_init(&adapter->mbox_lock);
4134 spin_lock_init(&adapter->mcc_lock);
4135 spin_lock_init(&adapter->mcc_cq_lock);
4137 init_completion(&adapter->flash_compl);
4138 pci_save_state(adapter->pdev);
4142 dma_free_coherent(&adapter->pdev->dev, mbox_mem_alloc->size,
4143 mbox_mem_alloc->va, mbox_mem_alloc->dma);
4146 be_unmap_pci_bars(adapter);
4152 static void be_stats_cleanup(struct be_adapter *adapter)
4154 struct be_dma_mem *cmd = &adapter->stats_cmd;
4157 dma_free_coherent(&adapter->pdev->dev, cmd->size,
4161 static int be_stats_init(struct be_adapter *adapter)
4163 struct be_dma_mem *cmd = &adapter->stats_cmd;
4165 if (lancer_chip(adapter))
4166 cmd->size = sizeof(struct lancer_cmd_req_pport_stats);
4167 else if (BE2_chip(adapter))
4168 cmd->size = sizeof(struct be_cmd_req_get_stats_v0);
4169 else if (BE3_chip(adapter))
4170 cmd->size = sizeof(struct be_cmd_req_get_stats_v1);
4172 /* ALL non-BE ASICs */
4173 cmd->size = sizeof(struct be_cmd_req_get_stats_v2);
4175 cmd->va = dma_zalloc_coherent(&adapter->pdev->dev, cmd->size, &cmd->dma,
4177 if (cmd->va == NULL)
4182 static void be_remove(struct pci_dev *pdev)
4184 struct be_adapter *adapter = pci_get_drvdata(pdev);
4189 be_roce_dev_remove(adapter);
4190 be_intr_set(adapter, false);
4192 cancel_delayed_work_sync(&adapter->func_recovery_work);
4194 unregister_netdev(adapter->netdev);
4198 /* tell fw we're done with firing cmds */
4199 be_cmd_fw_clean(adapter);
4201 be_stats_cleanup(adapter);
4203 be_ctrl_cleanup(adapter);
4205 pci_disable_pcie_error_reporting(pdev);
4207 pci_set_drvdata(pdev, NULL);
4208 pci_release_regions(pdev);
4209 pci_disable_device(pdev);
4211 free_netdev(adapter->netdev);
4214 bool be_is_wol_supported(struct be_adapter *adapter)
4216 return ((adapter->wol_cap & BE_WOL_CAP) &&
4217 !be_is_wol_excluded(adapter)) ? true : false;
4220 u32 be_get_fw_log_level(struct be_adapter *adapter)
4222 struct be_dma_mem extfat_cmd;
4223 struct be_fat_conf_params *cfgs;
4228 if (lancer_chip(adapter))
4231 memset(&extfat_cmd, 0, sizeof(struct be_dma_mem));
4232 extfat_cmd.size = sizeof(struct be_cmd_resp_get_ext_fat_caps);
4233 extfat_cmd.va = pci_alloc_consistent(adapter->pdev, extfat_cmd.size,
4236 if (!extfat_cmd.va) {
4237 dev_err(&adapter->pdev->dev, "%s: Memory allocation failure\n",
4242 status = be_cmd_get_ext_fat_capabilites(adapter, &extfat_cmd);
4244 cfgs = (struct be_fat_conf_params *)(extfat_cmd.va +
4245 sizeof(struct be_cmd_resp_hdr));
4246 for (j = 0; j < le32_to_cpu(cfgs->module[0].num_modes); j++) {
4247 if (cfgs->module[0].trace_lvl[j].mode == MODE_UART)
4248 level = cfgs->module[0].trace_lvl[j].dbg_lvl;
4251 pci_free_consistent(adapter->pdev, extfat_cmd.size, extfat_cmd.va,
4257 static int be_get_initial_config(struct be_adapter *adapter)
4262 status = be_cmd_get_cntl_attributes(adapter);
4266 status = be_cmd_get_acpi_wol_cap(adapter);
4268 /* in case of a failure to get wol capabillities
4269 * check the exclusion list to determine WOL capability */
4270 if (!be_is_wol_excluded(adapter))
4271 adapter->wol_cap |= BE_WOL_CAP;
4274 if (be_is_wol_supported(adapter))
4275 adapter->wol = true;
4277 /* Must be a power of 2 or else MODULO will BUG_ON */
4278 adapter->be_get_temp_freq = 64;
4280 level = be_get_fw_log_level(adapter);
4281 adapter->msg_enable = level <= FW_LOG_LEVEL_DEFAULT ? NETIF_MSG_HW : 0;
4283 adapter->cfg_num_qs = netif_get_num_default_rss_queues();
4287 static int lancer_recover_func(struct be_adapter *adapter)
4289 struct device *dev = &adapter->pdev->dev;
4292 status = lancer_test_and_set_rdy_state(adapter);
4296 if (netif_running(adapter->netdev))
4297 be_close(adapter->netdev);
4301 be_clear_all_error(adapter);
4303 status = be_setup(adapter);
4307 if (netif_running(adapter->netdev)) {
4308 status = be_open(adapter->netdev);
4313 dev_err(dev, "Error recovery successful\n");
4316 if (status == -EAGAIN)
4317 dev_err(dev, "Waiting for resource provisioning\n");
4319 dev_err(dev, "Error recovery failed\n");
4324 static void be_func_recovery_task(struct work_struct *work)
4326 struct be_adapter *adapter =
4327 container_of(work, struct be_adapter, func_recovery_work.work);
4330 be_detect_error(adapter);
4332 if (adapter->hw_error && lancer_chip(adapter)) {
4335 netif_device_detach(adapter->netdev);
4338 status = lancer_recover_func(adapter);
4340 netif_device_attach(adapter->netdev);
4343 /* In Lancer, for all errors other than provisioning error (-EAGAIN),
4344 * no need to attempt further recovery.
4346 if (!status || status == -EAGAIN)
4347 schedule_delayed_work(&adapter->func_recovery_work,
4348 msecs_to_jiffies(1000));
4351 static void be_worker(struct work_struct *work)
4353 struct be_adapter *adapter =
4354 container_of(work, struct be_adapter, work.work);
4355 struct be_rx_obj *rxo;
4358 /* when interrupts are not yet enabled, just reap any pending
4359 * mcc completions */
4360 if (!netif_running(adapter->netdev)) {
4362 be_process_mcc(adapter);
4367 if (!adapter->stats_cmd_sent) {
4368 if (lancer_chip(adapter))
4369 lancer_cmd_get_pport_stats(adapter,
4370 &adapter->stats_cmd);
4372 be_cmd_get_stats(adapter, &adapter->stats_cmd);
4375 if (be_physfn(adapter) &&
4376 MODULO(adapter->work_counter, adapter->be_get_temp_freq) == 0)
4377 be_cmd_get_die_temperature(adapter);
4379 for_all_rx_queues(adapter, rxo, i) {
4380 if (rxo->rx_post_starved) {
4381 rxo->rx_post_starved = false;
4382 be_post_rx_frags(rxo, GFP_KERNEL);
4386 be_eqd_update(adapter);
4389 adapter->work_counter++;
4390 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
4393 /* If any VFs are already enabled don't FLR the PF */
4394 static bool be_reset_required(struct be_adapter *adapter)
4396 return pci_num_vf(adapter->pdev) ? false : true;
4399 static char *mc_name(struct be_adapter *adapter)
4401 if (adapter->function_mode & FLEX10_MODE)
4403 else if (adapter->function_mode & VNIC_MODE)
4405 else if (adapter->function_mode & UMC_ENABLED)
4411 static inline char *func_name(struct be_adapter *adapter)
4413 return be_physfn(adapter) ? "PF" : "VF";
4416 static int be_probe(struct pci_dev *pdev, const struct pci_device_id *pdev_id)
4419 struct be_adapter *adapter;
4420 struct net_device *netdev;
4423 status = pci_enable_device(pdev);
4427 status = pci_request_regions(pdev, DRV_NAME);
4430 pci_set_master(pdev);
4432 netdev = alloc_etherdev_mqs(sizeof(*adapter), MAX_TX_QS, MAX_RX_QS);
4433 if (netdev == NULL) {
4437 adapter = netdev_priv(netdev);
4438 adapter->pdev = pdev;
4439 pci_set_drvdata(pdev, adapter);
4440 adapter->netdev = netdev;
4441 SET_NETDEV_DEV(netdev, &pdev->dev);
4443 status = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
4445 status = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
4447 dev_err(&pdev->dev, "dma_set_coherent_mask failed\n");
4450 netdev->features |= NETIF_F_HIGHDMA;
4452 status = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
4454 status = dma_set_coherent_mask(&pdev->dev,
4457 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
4462 status = pci_enable_pcie_error_reporting(pdev);
4464 dev_info(&pdev->dev, "Could not use PCIe error reporting\n");
4466 status = be_ctrl_init(adapter);
4470 /* sync up with fw's ready state */
4471 if (be_physfn(adapter)) {
4472 status = be_fw_wait_ready(adapter);
4477 if (be_reset_required(adapter)) {
4478 status = be_cmd_reset_function(adapter);
4482 /* Wait for interrupts to quiesce after an FLR */
4486 /* Allow interrupts for other ULPs running on NIC function */
4487 be_intr_set(adapter, true);
4489 /* tell fw we're ready to fire cmds */
4490 status = be_cmd_fw_init(adapter);
4494 status = be_stats_init(adapter);
4498 status = be_get_initial_config(adapter);
4502 INIT_DELAYED_WORK(&adapter->work, be_worker);
4503 INIT_DELAYED_WORK(&adapter->func_recovery_work, be_func_recovery_task);
4504 adapter->rx_fc = adapter->tx_fc = true;
4506 status = be_setup(adapter);
4510 be_netdev_init(netdev);
4511 status = register_netdev(netdev);
4515 be_roce_dev_add(adapter);
4517 schedule_delayed_work(&adapter->func_recovery_work,
4518 msecs_to_jiffies(1000));
4520 be_cmd_query_port_name(adapter, &port_name);
4522 dev_info(&pdev->dev, "%s: %s %s port %c\n", nic_name(pdev),
4523 func_name(adapter), mc_name(adapter), port_name);
4530 be_stats_cleanup(adapter);
4532 be_ctrl_cleanup(adapter);
4534 free_netdev(netdev);
4535 pci_set_drvdata(pdev, NULL);
4537 pci_release_regions(pdev);
4539 pci_disable_device(pdev);
4541 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
4545 static int be_suspend(struct pci_dev *pdev, pm_message_t state)
4547 struct be_adapter *adapter = pci_get_drvdata(pdev);
4548 struct net_device *netdev = adapter->netdev;
4551 be_setup_wol(adapter, true);
4553 cancel_delayed_work_sync(&adapter->func_recovery_work);
4555 netif_device_detach(netdev);
4556 if (netif_running(netdev)) {
4563 pci_save_state(pdev);
4564 pci_disable_device(pdev);
4565 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4569 static int be_resume(struct pci_dev *pdev)
4572 struct be_adapter *adapter = pci_get_drvdata(pdev);
4573 struct net_device *netdev = adapter->netdev;
4575 netif_device_detach(netdev);
4577 status = pci_enable_device(pdev);
4581 pci_set_power_state(pdev, PCI_D0);
4582 pci_restore_state(pdev);
4584 status = be_fw_wait_ready(adapter);
4588 /* tell fw we're ready to fire cmds */
4589 status = be_cmd_fw_init(adapter);
4594 if (netif_running(netdev)) {
4600 schedule_delayed_work(&adapter->func_recovery_work,
4601 msecs_to_jiffies(1000));
4602 netif_device_attach(netdev);
4605 be_setup_wol(adapter, false);
4611 * An FLR will stop BE from DMAing any data.
4613 static void be_shutdown(struct pci_dev *pdev)
4615 struct be_adapter *adapter = pci_get_drvdata(pdev);
4620 cancel_delayed_work_sync(&adapter->work);
4621 cancel_delayed_work_sync(&adapter->func_recovery_work);
4623 netif_device_detach(adapter->netdev);
4625 be_cmd_reset_function(adapter);
4627 pci_disable_device(pdev);
4630 static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
4631 pci_channel_state_t state)
4633 struct be_adapter *adapter = pci_get_drvdata(pdev);
4634 struct net_device *netdev = adapter->netdev;
4636 dev_err(&adapter->pdev->dev, "EEH error detected\n");
4638 if (!adapter->eeh_error) {
4639 adapter->eeh_error = true;
4641 cancel_delayed_work_sync(&adapter->func_recovery_work);
4644 netif_device_detach(netdev);
4645 if (netif_running(netdev))
4652 if (state == pci_channel_io_perm_failure)
4653 return PCI_ERS_RESULT_DISCONNECT;
4655 pci_disable_device(pdev);
4657 /* The error could cause the FW to trigger a flash debug dump.
4658 * Resetting the card while flash dump is in progress
4659 * can cause it not to recover; wait for it to finish.
4660 * Wait only for first function as it is needed only once per
4663 if (pdev->devfn == 0)
4666 return PCI_ERS_RESULT_NEED_RESET;
4669 static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
4671 struct be_adapter *adapter = pci_get_drvdata(pdev);
4674 dev_info(&adapter->pdev->dev, "EEH reset\n");
4676 status = pci_enable_device(pdev);
4678 return PCI_ERS_RESULT_DISCONNECT;
4680 pci_set_master(pdev);
4681 pci_set_power_state(pdev, PCI_D0);
4682 pci_restore_state(pdev);
4684 /* Check if card is ok and fw is ready */
4685 dev_info(&adapter->pdev->dev,
4686 "Waiting for FW to be ready after EEH reset\n");
4687 status = be_fw_wait_ready(adapter);
4689 return PCI_ERS_RESULT_DISCONNECT;
4691 pci_cleanup_aer_uncorrect_error_status(pdev);
4692 be_clear_all_error(adapter);
4693 return PCI_ERS_RESULT_RECOVERED;
4696 static void be_eeh_resume(struct pci_dev *pdev)
4699 struct be_adapter *adapter = pci_get_drvdata(pdev);
4700 struct net_device *netdev = adapter->netdev;
4702 dev_info(&adapter->pdev->dev, "EEH resume\n");
4704 pci_save_state(pdev);
4706 status = be_cmd_reset_function(adapter);
4710 /* tell fw we're ready to fire cmds */
4711 status = be_cmd_fw_init(adapter);
4715 status = be_setup(adapter);
4719 if (netif_running(netdev)) {
4720 status = be_open(netdev);
4725 schedule_delayed_work(&adapter->func_recovery_work,
4726 msecs_to_jiffies(1000));
4727 netif_device_attach(netdev);
4730 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
4733 static const struct pci_error_handlers be_eeh_handlers = {
4734 .error_detected = be_eeh_err_detected,
4735 .slot_reset = be_eeh_reset,
4736 .resume = be_eeh_resume,
4739 static struct pci_driver be_driver = {
4741 .id_table = be_dev_ids,
4743 .remove = be_remove,
4744 .suspend = be_suspend,
4745 .resume = be_resume,
4746 .shutdown = be_shutdown,
4747 .err_handler = &be_eeh_handlers
4750 static int __init be_init_module(void)
4752 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
4753 rx_frag_size != 2048) {
4754 printk(KERN_WARNING DRV_NAME
4755 " : Module param rx_frag_size must be 2048/4096/8192."
4757 rx_frag_size = 2048;
4760 return pci_register_driver(&be_driver);
4762 module_init(be_init_module);
4764 static void __exit be_exit_module(void)
4766 pci_unregister_driver(&be_driver);
4768 module_exit(be_exit_module);