2 * Copyright (C) 2005 - 2010 ServerEngines
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@serverengines.com
14 * 209 N. Fair Oaks Ave
20 #include <asm/div64.h>
22 MODULE_VERSION(DRV_VER);
23 MODULE_DEVICE_TABLE(pci, be_dev_ids);
24 MODULE_DESCRIPTION(DRV_DESC " " DRV_VER);
25 MODULE_AUTHOR("ServerEngines Corporation");
26 MODULE_LICENSE("GPL");
28 static unsigned int rx_frag_size = 2048;
29 static unsigned int num_vfs;
30 module_param(rx_frag_size, uint, S_IRUGO);
31 module_param(num_vfs, uint, S_IRUGO);
32 MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data.");
33 MODULE_PARM_DESC(num_vfs, "Number of PCI VFs to initialize");
35 static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
36 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
37 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
38 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
39 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
42 MODULE_DEVICE_TABLE(pci, be_dev_ids);
43 /* UE Status Low CSR */
44 static char *ue_status_low_desc[] = {
78 /* UE Status High CSR */
79 static char *ue_status_hi_desc[] = {
114 static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
116 struct be_dma_mem *mem = &q->dma_mem;
118 pci_free_consistent(adapter->pdev, mem->size,
122 static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
123 u16 len, u16 entry_size)
125 struct be_dma_mem *mem = &q->dma_mem;
127 memset(q, 0, sizeof(*q));
129 q->entry_size = entry_size;
130 mem->size = len * entry_size;
131 mem->va = pci_alloc_consistent(adapter->pdev, mem->size, &mem->dma);
134 memset(mem->va, 0, mem->size);
138 static void be_intr_set(struct be_adapter *adapter, bool enable)
140 u8 __iomem *addr = adapter->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
141 u32 reg = ioread32(addr);
142 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
144 if (adapter->eeh_err)
147 if (!enabled && enable)
148 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
149 else if (enabled && !enable)
150 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
154 iowrite32(reg, addr);
157 static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
160 val |= qid & DB_RQ_RING_ID_MASK;
161 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
164 iowrite32(val, adapter->db + DB_RQ_OFFSET);
167 static void be_txq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
170 val |= qid & DB_TXULP_RING_ID_MASK;
171 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
174 iowrite32(val, adapter->db + DB_TXULP1_OFFSET);
177 static void be_eq_notify(struct be_adapter *adapter, u16 qid,
178 bool arm, bool clear_int, u16 num_popped)
181 val |= qid & DB_EQ_RING_ID_MASK;
183 if (adapter->eeh_err)
187 val |= 1 << DB_EQ_REARM_SHIFT;
189 val |= 1 << DB_EQ_CLR_SHIFT;
190 val |= 1 << DB_EQ_EVNT_SHIFT;
191 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
192 iowrite32(val, adapter->db + DB_EQ_OFFSET);
195 void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
198 val |= qid & DB_CQ_RING_ID_MASK;
200 if (adapter->eeh_err)
204 val |= 1 << DB_CQ_REARM_SHIFT;
205 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
206 iowrite32(val, adapter->db + DB_CQ_OFFSET);
209 static int be_mac_addr_set(struct net_device *netdev, void *p)
211 struct be_adapter *adapter = netdev_priv(netdev);
212 struct sockaddr *addr = p;
215 if (!is_valid_ether_addr(addr->sa_data))
216 return -EADDRNOTAVAIL;
218 /* MAC addr configuration will be done in hardware for VFs
219 * by their corresponding PFs. Just copy to netdev addr here
221 if (!be_physfn(adapter))
224 status = be_cmd_pmac_del(adapter, adapter->if_handle, adapter->pmac_id);
228 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
229 adapter->if_handle, &adapter->pmac_id);
232 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
237 void netdev_stats_update(struct be_adapter *adapter)
239 struct be_hw_stats *hw_stats = hw_stats_from_cmd(adapter->stats.cmd.va);
240 struct be_rxf_stats *rxf_stats = &hw_stats->rxf;
241 struct be_port_rxf_stats *port_stats =
242 &rxf_stats->port[adapter->port_num];
243 struct net_device_stats *dev_stats = &adapter->netdev->stats;
244 struct be_erx_stats *erx_stats = &hw_stats->erx;
246 dev_stats->rx_packets = drvr_stats(adapter)->be_rx_pkts;
247 dev_stats->tx_packets = drvr_stats(adapter)->be_tx_pkts;
248 dev_stats->rx_bytes = drvr_stats(adapter)->be_rx_bytes;
249 dev_stats->tx_bytes = drvr_stats(adapter)->be_tx_bytes;
250 dev_stats->multicast = drvr_stats(adapter)->be_rx_mcast_pkt;
252 /* bad pkts received */
253 dev_stats->rx_errors = port_stats->rx_crc_errors +
254 port_stats->rx_alignment_symbol_errors +
255 port_stats->rx_in_range_errors +
256 port_stats->rx_out_range_errors +
257 port_stats->rx_frame_too_long +
258 port_stats->rx_dropped_too_small +
259 port_stats->rx_dropped_too_short +
260 port_stats->rx_dropped_header_too_small +
261 port_stats->rx_dropped_tcp_length +
262 port_stats->rx_dropped_runt +
263 port_stats->rx_tcp_checksum_errs +
264 port_stats->rx_ip_checksum_errs +
265 port_stats->rx_udp_checksum_errs;
267 /* no space in linux buffers: best possible approximation */
268 dev_stats->rx_dropped =
269 erx_stats->rx_drops_no_fragments[adapter->rx_obj.q.id];
271 /* detailed rx errors */
272 dev_stats->rx_length_errors = port_stats->rx_in_range_errors +
273 port_stats->rx_out_range_errors +
274 port_stats->rx_frame_too_long;
276 /* receive ring buffer overflow */
277 dev_stats->rx_over_errors = 0;
279 dev_stats->rx_crc_errors = port_stats->rx_crc_errors;
281 /* frame alignment errors */
282 dev_stats->rx_frame_errors = port_stats->rx_alignment_symbol_errors;
284 /* receiver fifo overrun */
285 /* drops_no_pbuf is no per i/f, it's per BE card */
286 dev_stats->rx_fifo_errors = port_stats->rx_fifo_overflow +
287 port_stats->rx_input_fifo_overflow +
288 rxf_stats->rx_drops_no_pbuf;
289 /* receiver missed packetd */
290 dev_stats->rx_missed_errors = 0;
292 /* packet transmit problems */
293 dev_stats->tx_errors = 0;
295 /* no space available in linux */
296 dev_stats->tx_dropped = 0;
298 dev_stats->collisions = 0;
300 /* detailed tx_errors */
301 dev_stats->tx_aborted_errors = 0;
302 dev_stats->tx_carrier_errors = 0;
303 dev_stats->tx_fifo_errors = 0;
304 dev_stats->tx_heartbeat_errors = 0;
305 dev_stats->tx_window_errors = 0;
308 void be_link_status_update(struct be_adapter *adapter, bool link_up)
310 struct net_device *netdev = adapter->netdev;
312 /* If link came up or went down */
313 if (adapter->link_up != link_up) {
314 adapter->link_speed = -1;
316 netif_start_queue(netdev);
317 netif_carrier_on(netdev);
318 printk(KERN_INFO "%s: Link up\n", netdev->name);
320 netif_stop_queue(netdev);
321 netif_carrier_off(netdev);
322 printk(KERN_INFO "%s: Link down\n", netdev->name);
324 adapter->link_up = link_up;
328 /* Update the EQ delay n BE based on the RX frags consumed / sec */
329 static void be_rx_eqd_update(struct be_adapter *adapter)
331 struct be_eq_obj *rx_eq = &adapter->rx_eq;
332 struct be_drvr_stats *stats = &adapter->stats.drvr_stats;
336 if (!rx_eq->enable_aic)
340 if (time_before(now, stats->rx_fps_jiffies)) {
341 stats->rx_fps_jiffies = now;
345 /* Update once a second */
346 if ((now - stats->rx_fps_jiffies) < HZ)
349 stats->be_rx_fps = (stats->be_rx_frags - stats->be_prev_rx_frags) /
350 ((now - stats->rx_fps_jiffies) / HZ);
352 stats->rx_fps_jiffies = now;
353 stats->be_prev_rx_frags = stats->be_rx_frags;
354 eqd = stats->be_rx_fps / 110000;
356 if (eqd > rx_eq->max_eqd)
357 eqd = rx_eq->max_eqd;
358 if (eqd < rx_eq->min_eqd)
359 eqd = rx_eq->min_eqd;
362 if (eqd != rx_eq->cur_eqd)
363 be_cmd_modify_eqd(adapter, rx_eq->q.id, eqd);
365 rx_eq->cur_eqd = eqd;
368 static struct net_device_stats *be_get_stats(struct net_device *dev)
373 static u32 be_calc_rate(u64 bytes, unsigned long ticks)
377 do_div(rate, ticks / HZ);
378 rate <<= 3; /* bytes/sec -> bits/sec */
379 do_div(rate, 1000000ul); /* MB/Sec */
384 static void be_tx_rate_update(struct be_adapter *adapter)
386 struct be_drvr_stats *stats = drvr_stats(adapter);
389 /* Wrapped around? */
390 if (time_before(now, stats->be_tx_jiffies)) {
391 stats->be_tx_jiffies = now;
395 /* Update tx rate once in two seconds */
396 if ((now - stats->be_tx_jiffies) > 2 * HZ) {
397 stats->be_tx_rate = be_calc_rate(stats->be_tx_bytes
398 - stats->be_tx_bytes_prev,
399 now - stats->be_tx_jiffies);
400 stats->be_tx_jiffies = now;
401 stats->be_tx_bytes_prev = stats->be_tx_bytes;
405 static void be_tx_stats_update(struct be_adapter *adapter,
406 u32 wrb_cnt, u32 copied, u32 gso_segs, bool stopped)
408 struct be_drvr_stats *stats = drvr_stats(adapter);
410 stats->be_tx_wrbs += wrb_cnt;
411 stats->be_tx_bytes += copied;
412 stats->be_tx_pkts += (gso_segs ? gso_segs : 1);
414 stats->be_tx_stops++;
417 /* Determine number of WRB entries needed to xmit data in an skb */
418 static u32 wrb_cnt_for_skb(struct sk_buff *skb, bool *dummy)
420 int cnt = (skb->len > skb->data_len);
422 cnt += skb_shinfo(skb)->nr_frags;
424 /* to account for hdr wrb */
427 /* add a dummy to make it an even num */
432 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
436 static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
438 wrb->frag_pa_hi = upper_32_bits(addr);
439 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
440 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
443 static void wrb_fill_hdr(struct be_eth_hdr_wrb *hdr, struct sk_buff *skb,
444 bool vlan, u32 wrb_cnt, u32 len)
446 memset(hdr, 0, sizeof(*hdr));
448 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
450 if (skb_is_gso(skb)) {
451 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
452 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
453 hdr, skb_shinfo(skb)->gso_size);
454 if (skb_is_gso_v6(skb))
455 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso6, hdr, 1);
456 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
458 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
459 else if (is_udp_pkt(skb))
460 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
463 if (vlan && vlan_tx_tag_present(skb)) {
464 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
465 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag,
466 hdr, vlan_tx_tag_get(skb));
469 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
470 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
471 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
472 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
475 static void unmap_tx_frag(struct pci_dev *pdev, struct be_eth_wrb *wrb,
480 be_dws_le_to_cpu(wrb, sizeof(*wrb));
482 dma = (u64)wrb->frag_pa_hi << 32 | (u64)wrb->frag_pa_lo;
485 pci_unmap_single(pdev, dma, wrb->frag_len,
488 pci_unmap_page(pdev, dma, wrb->frag_len,
493 static int make_tx_wrbs(struct be_adapter *adapter,
494 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
498 struct pci_dev *pdev = adapter->pdev;
499 struct sk_buff *first_skb = skb;
500 struct be_queue_info *txq = &adapter->tx_obj.q;
501 struct be_eth_wrb *wrb;
502 struct be_eth_hdr_wrb *hdr;
503 bool map_single = false;
506 hdr = queue_head_node(txq);
508 map_head = txq->head;
510 if (skb->len > skb->data_len) {
511 int len = skb_headlen(skb);
512 busaddr = pci_map_single(pdev, skb->data, len,
514 if (pci_dma_mapping_error(pdev, busaddr))
517 wrb = queue_head_node(txq);
518 wrb_fill(wrb, busaddr, len);
519 be_dws_cpu_to_le(wrb, sizeof(*wrb));
524 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
525 struct skb_frag_struct *frag =
526 &skb_shinfo(skb)->frags[i];
527 busaddr = pci_map_page(pdev, frag->page,
529 frag->size, PCI_DMA_TODEVICE);
530 if (pci_dma_mapping_error(pdev, busaddr))
532 wrb = queue_head_node(txq);
533 wrb_fill(wrb, busaddr, frag->size);
534 be_dws_cpu_to_le(wrb, sizeof(*wrb));
536 copied += frag->size;
540 wrb = queue_head_node(txq);
542 be_dws_cpu_to_le(wrb, sizeof(*wrb));
546 wrb_fill_hdr(hdr, first_skb, adapter->vlan_grp ? true : false,
548 be_dws_cpu_to_le(hdr, sizeof(*hdr));
552 txq->head = map_head;
554 wrb = queue_head_node(txq);
555 unmap_tx_frag(pdev, wrb, map_single);
557 copied -= wrb->frag_len;
563 static netdev_tx_t be_xmit(struct sk_buff *skb,
564 struct net_device *netdev)
566 struct be_adapter *adapter = netdev_priv(netdev);
567 struct be_tx_obj *tx_obj = &adapter->tx_obj;
568 struct be_queue_info *txq = &tx_obj->q;
569 u32 wrb_cnt = 0, copied = 0;
570 u32 start = txq->head;
571 bool dummy_wrb, stopped = false;
573 wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb);
575 copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb);
577 /* record the sent skb in the sent_skb table */
578 BUG_ON(tx_obj->sent_skb_list[start]);
579 tx_obj->sent_skb_list[start] = skb;
581 /* Ensure txq has space for the next skb; Else stop the queue
582 * *BEFORE* ringing the tx doorbell, so that we serialze the
583 * tx compls of the current transmit which'll wake up the queue
585 atomic_add(wrb_cnt, &txq->used);
586 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
588 netif_stop_queue(netdev);
592 be_txq_notify(adapter, txq->id, wrb_cnt);
594 be_tx_stats_update(adapter, wrb_cnt, copied,
595 skb_shinfo(skb)->gso_segs, stopped);
598 dev_kfree_skb_any(skb);
603 static int be_change_mtu(struct net_device *netdev, int new_mtu)
605 struct be_adapter *adapter = netdev_priv(netdev);
606 if (new_mtu < BE_MIN_MTU ||
607 new_mtu > (BE_MAX_JUMBO_FRAME_SIZE -
608 (ETH_HLEN + ETH_FCS_LEN))) {
609 dev_info(&adapter->pdev->dev,
610 "MTU must be between %d and %d bytes\n",
612 (BE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN)));
615 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
616 netdev->mtu, new_mtu);
617 netdev->mtu = new_mtu;
622 * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
623 * If the user configures more, place BE in vlan promiscuous mode.
625 static int be_vid_config(struct be_adapter *adapter, bool vf, u32 vf_num)
627 u16 vtag[BE_NUM_VLANS_SUPPORTED];
633 if_handle = adapter->vf_cfg[vf_num].vf_if_handle;
634 vtag[0] = cpu_to_le16(adapter->vf_cfg[vf_num].vf_vlan_tag);
635 status = be_cmd_vlan_config(adapter, if_handle, vtag, 1, 1, 0);
638 if (adapter->vlans_added <= adapter->max_vlans) {
639 /* Construct VLAN Table to give to HW */
640 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
641 if (adapter->vlan_tag[i]) {
642 vtag[ntags] = cpu_to_le16(i);
646 status = be_cmd_vlan_config(adapter, adapter->if_handle,
649 status = be_cmd_vlan_config(adapter, adapter->if_handle,
656 static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
658 struct be_adapter *adapter = netdev_priv(netdev);
659 struct be_eq_obj *rx_eq = &adapter->rx_eq;
660 struct be_eq_obj *tx_eq = &adapter->tx_eq;
662 be_eq_notify(adapter, rx_eq->q.id, false, false, 0);
663 be_eq_notify(adapter, tx_eq->q.id, false, false, 0);
664 adapter->vlan_grp = grp;
665 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
666 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
669 static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
671 struct be_adapter *adapter = netdev_priv(netdev);
673 adapter->vlans_added++;
674 if (!be_physfn(adapter))
677 adapter->vlan_tag[vid] = 1;
678 if (adapter->vlans_added <= (adapter->max_vlans + 1))
679 be_vid_config(adapter, false, 0);
682 static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
684 struct be_adapter *adapter = netdev_priv(netdev);
686 adapter->vlans_added--;
687 vlan_group_set_device(adapter->vlan_grp, vid, NULL);
689 if (!be_physfn(adapter))
692 adapter->vlan_tag[vid] = 0;
693 if (adapter->vlans_added <= adapter->max_vlans)
694 be_vid_config(adapter, false, 0);
697 static void be_set_multicast_list(struct net_device *netdev)
699 struct be_adapter *adapter = netdev_priv(netdev);
701 if (netdev->flags & IFF_PROMISC) {
702 be_cmd_promiscuous_config(adapter, adapter->port_num, 1);
703 adapter->promiscuous = true;
707 /* BE was previously in promiscous mode; disable it */
708 if (adapter->promiscuous) {
709 adapter->promiscuous = false;
710 be_cmd_promiscuous_config(adapter, adapter->port_num, 0);
713 /* Enable multicast promisc if num configured exceeds what we support */
714 if (netdev->flags & IFF_ALLMULTI ||
715 netdev_mc_count(netdev) > BE_MAX_MC) {
716 be_cmd_multicast_set(adapter, adapter->if_handle, NULL,
717 &adapter->mc_cmd_mem);
721 be_cmd_multicast_set(adapter, adapter->if_handle, netdev,
722 &adapter->mc_cmd_mem);
727 static int be_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
729 struct be_adapter *adapter = netdev_priv(netdev);
732 if (!adapter->sriov_enabled)
735 if (!is_valid_ether_addr(mac) || (vf >= num_vfs))
738 if (adapter->vf_cfg[vf].vf_pmac_id != BE_INVALID_PMAC_ID)
739 status = be_cmd_pmac_del(adapter,
740 adapter->vf_cfg[vf].vf_if_handle,
741 adapter->vf_cfg[vf].vf_pmac_id);
743 status = be_cmd_pmac_add(adapter, mac,
744 adapter->vf_cfg[vf].vf_if_handle,
745 &adapter->vf_cfg[vf].vf_pmac_id);
748 dev_err(&adapter->pdev->dev, "MAC %pM set on VF %d Failed\n",
751 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
756 static int be_get_vf_config(struct net_device *netdev, int vf,
757 struct ifla_vf_info *vi)
759 struct be_adapter *adapter = netdev_priv(netdev);
761 if (!adapter->sriov_enabled)
768 vi->tx_rate = adapter->vf_cfg[vf].vf_tx_rate;
769 vi->vlan = adapter->vf_cfg[vf].vf_vlan_tag;
771 memcpy(&vi->mac, adapter->vf_cfg[vf].vf_mac_addr, ETH_ALEN);
776 static int be_set_vf_vlan(struct net_device *netdev,
777 int vf, u16 vlan, u8 qos)
779 struct be_adapter *adapter = netdev_priv(netdev);
782 if (!adapter->sriov_enabled)
785 if ((vf >= num_vfs) || (vlan > 4095))
789 adapter->vf_cfg[vf].vf_vlan_tag = vlan;
790 adapter->vlans_added++;
792 adapter->vf_cfg[vf].vf_vlan_tag = 0;
793 adapter->vlans_added--;
796 status = be_vid_config(adapter, true, vf);
799 dev_info(&adapter->pdev->dev,
800 "VLAN %d config on VF %d failed\n", vlan, vf);
804 static int be_set_vf_tx_rate(struct net_device *netdev,
807 struct be_adapter *adapter = netdev_priv(netdev);
810 if (!adapter->sriov_enabled)
813 if ((vf >= num_vfs) || (rate < 0))
819 adapter->vf_cfg[vf].vf_tx_rate = rate;
820 status = be_cmd_set_qos(adapter, rate / 10, vf);
823 dev_info(&adapter->pdev->dev,
824 "tx rate %d on VF %d failed\n", rate, vf);
828 static void be_rx_rate_update(struct be_adapter *adapter)
830 struct be_drvr_stats *stats = drvr_stats(adapter);
834 if (time_before(now, stats->be_rx_jiffies)) {
835 stats->be_rx_jiffies = now;
839 /* Update the rate once in two seconds */
840 if ((now - stats->be_rx_jiffies) < 2 * HZ)
843 stats->be_rx_rate = be_calc_rate(stats->be_rx_bytes
844 - stats->be_rx_bytes_prev,
845 now - stats->be_rx_jiffies);
846 stats->be_rx_jiffies = now;
847 stats->be_rx_bytes_prev = stats->be_rx_bytes;
850 static void be_rx_stats_update(struct be_adapter *adapter,
851 u32 pktsize, u16 numfrags, u8 pkt_type)
853 struct be_drvr_stats *stats = drvr_stats(adapter);
855 stats->be_rx_compl++;
856 stats->be_rx_frags += numfrags;
857 stats->be_rx_bytes += pktsize;
860 if (pkt_type == BE_MULTICAST_PACKET)
861 stats->be_rx_mcast_pkt++;
864 static inline bool do_pkt_csum(struct be_eth_rx_compl *rxcp, bool cso)
866 u8 l4_cksm, ip_version, ipcksm, tcpf = 0, udpf = 0, ipv6_chk;
868 l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp);
869 ipcksm = AMAP_GET_BITS(struct amap_eth_rx_compl, ipcksm, rxcp);
870 ip_version = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
872 tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
873 udpf = AMAP_GET_BITS(struct amap_eth_rx_compl, udpf, rxcp);
875 ipv6_chk = (ip_version && (tcpf || udpf));
877 return ((l4_cksm && ipv6_chk && ipcksm) && cso) ? false : true;
880 static struct be_rx_page_info *
881 get_rx_page_info(struct be_adapter *adapter, u16 frag_idx)
883 struct be_rx_page_info *rx_page_info;
884 struct be_queue_info *rxq = &adapter->rx_obj.q;
886 rx_page_info = &adapter->rx_obj.page_info_tbl[frag_idx];
887 BUG_ON(!rx_page_info->page);
889 if (rx_page_info->last_page_user) {
890 pci_unmap_page(adapter->pdev, dma_unmap_addr(rx_page_info, bus),
891 adapter->big_page_size, PCI_DMA_FROMDEVICE);
892 rx_page_info->last_page_user = false;
895 atomic_dec(&rxq->used);
899 /* Throwaway the data in the Rx completion */
900 static void be_rx_compl_discard(struct be_adapter *adapter,
901 struct be_eth_rx_compl *rxcp)
903 struct be_queue_info *rxq = &adapter->rx_obj.q;
904 struct be_rx_page_info *page_info;
905 u16 rxq_idx, i, num_rcvd;
907 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
908 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
910 for (i = 0; i < num_rcvd; i++) {
911 page_info = get_rx_page_info(adapter, rxq_idx);
912 put_page(page_info->page);
913 memset(page_info, 0, sizeof(*page_info));
914 index_inc(&rxq_idx, rxq->len);
919 * skb_fill_rx_data forms a complete skb for an ether frame
922 static void skb_fill_rx_data(struct be_adapter *adapter,
923 struct sk_buff *skb, struct be_eth_rx_compl *rxcp,
926 struct be_queue_info *rxq = &adapter->rx_obj.q;
927 struct be_rx_page_info *page_info;
929 u32 pktsize, hdr_len, curr_frag_len, size;
933 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
934 pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
935 pkt_type = AMAP_GET_BITS(struct amap_eth_rx_compl, cast_enc, rxcp);
937 page_info = get_rx_page_info(adapter, rxq_idx);
939 start = page_address(page_info->page) + page_info->page_offset;
942 /* Copy data in the first descriptor of this completion */
943 curr_frag_len = min(pktsize, rx_frag_size);
945 /* Copy the header portion into skb_data */
946 hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
947 memcpy(skb->data, start, hdr_len);
948 skb->len = curr_frag_len;
949 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
950 /* Complete packet has now been moved to data */
951 put_page(page_info->page);
953 skb->tail += curr_frag_len;
955 skb_shinfo(skb)->nr_frags = 1;
956 skb_shinfo(skb)->frags[0].page = page_info->page;
957 skb_shinfo(skb)->frags[0].page_offset =
958 page_info->page_offset + hdr_len;
959 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
960 skb->data_len = curr_frag_len - hdr_len;
961 skb->tail += hdr_len;
963 page_info->page = NULL;
965 if (pktsize <= rx_frag_size) {
966 BUG_ON(num_rcvd != 1);
970 /* More frags present for this completion */
972 for (i = 1, j = 0; i < num_rcvd; i++) {
973 size -= curr_frag_len;
974 index_inc(&rxq_idx, rxq->len);
975 page_info = get_rx_page_info(adapter, rxq_idx);
977 curr_frag_len = min(size, rx_frag_size);
979 /* Coalesce all frags from the same physical page in one slot */
980 if (page_info->page_offset == 0) {
983 skb_shinfo(skb)->frags[j].page = page_info->page;
984 skb_shinfo(skb)->frags[j].page_offset =
985 page_info->page_offset;
986 skb_shinfo(skb)->frags[j].size = 0;
987 skb_shinfo(skb)->nr_frags++;
989 put_page(page_info->page);
992 skb_shinfo(skb)->frags[j].size += curr_frag_len;
993 skb->len += curr_frag_len;
994 skb->data_len += curr_frag_len;
996 page_info->page = NULL;
998 BUG_ON(j > MAX_SKB_FRAGS);
1001 be_rx_stats_update(adapter, pktsize, num_rcvd, pkt_type);
1004 /* Process the RX completion indicated by rxcp when GRO is disabled */
1005 static void be_rx_compl_process(struct be_adapter *adapter,
1006 struct be_eth_rx_compl *rxcp)
1008 struct sk_buff *skb;
1013 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
1014 /* Is it a flush compl that has no data */
1015 if (unlikely(num_rcvd == 0))
1018 skb = netdev_alloc_skb_ip_align(adapter->netdev, BE_HDR_LEN);
1019 if (unlikely(!skb)) {
1020 if (net_ratelimit())
1021 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
1022 be_rx_compl_discard(adapter, rxcp);
1026 skb_fill_rx_data(adapter, skb, rxcp, num_rcvd);
1028 if (do_pkt_csum(rxcp, adapter->rx_csum))
1029 skb->ip_summed = CHECKSUM_NONE;
1031 skb->ip_summed = CHECKSUM_UNNECESSARY;
1033 skb->truesize = skb->len + sizeof(struct sk_buff);
1034 skb->protocol = eth_type_trans(skb, adapter->netdev);
1036 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
1037 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
1039 /* vlanf could be wrongly set in some cards.
1040 * ignore if vtm is not set */
1041 if ((adapter->function_mode & 0x400) && !vtm)
1044 if (unlikely(vlanf)) {
1045 if (!adapter->vlan_grp || adapter->vlans_added == 0) {
1049 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
1051 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
1053 netif_receive_skb(skb);
1057 /* Process the RX completion indicated by rxcp when GRO is enabled */
1058 static void be_rx_compl_process_gro(struct be_adapter *adapter,
1059 struct be_eth_rx_compl *rxcp)
1061 struct be_rx_page_info *page_info;
1062 struct sk_buff *skb = NULL;
1063 struct be_queue_info *rxq = &adapter->rx_obj.q;
1064 struct be_eq_obj *eq_obj = &adapter->rx_eq;
1065 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
1066 u16 i, rxq_idx = 0, vid, j;
1070 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
1071 /* Is it a flush compl that has no data */
1072 if (unlikely(num_rcvd == 0))
1075 pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
1076 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
1077 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
1078 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
1079 pkt_type = AMAP_GET_BITS(struct amap_eth_rx_compl, cast_enc, rxcp);
1081 /* vlanf could be wrongly set in some cards.
1082 * ignore if vtm is not set */
1083 if ((adapter->function_mode & 0x400) && !vtm)
1086 skb = napi_get_frags(&eq_obj->napi);
1088 be_rx_compl_discard(adapter, rxcp);
1092 remaining = pkt_size;
1093 for (i = 0, j = -1; i < num_rcvd; i++) {
1094 page_info = get_rx_page_info(adapter, rxq_idx);
1096 curr_frag_len = min(remaining, rx_frag_size);
1098 /* Coalesce all frags from the same physical page in one slot */
1099 if (i == 0 || page_info->page_offset == 0) {
1100 /* First frag or Fresh page */
1102 skb_shinfo(skb)->frags[j].page = page_info->page;
1103 skb_shinfo(skb)->frags[j].page_offset =
1104 page_info->page_offset;
1105 skb_shinfo(skb)->frags[j].size = 0;
1107 put_page(page_info->page);
1109 skb_shinfo(skb)->frags[j].size += curr_frag_len;
1111 remaining -= curr_frag_len;
1112 index_inc(&rxq_idx, rxq->len);
1113 memset(page_info, 0, sizeof(*page_info));
1115 BUG_ON(j > MAX_SKB_FRAGS);
1117 skb_shinfo(skb)->nr_frags = j + 1;
1118 skb->len = pkt_size;
1119 skb->data_len = pkt_size;
1120 skb->truesize += pkt_size;
1121 skb->ip_summed = CHECKSUM_UNNECESSARY;
1123 if (likely(!vlanf)) {
1124 napi_gro_frags(&eq_obj->napi);
1126 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
1129 if (!adapter->vlan_grp || adapter->vlans_added == 0)
1132 vlan_gro_frags(&eq_obj->napi, adapter->vlan_grp, vid);
1135 be_rx_stats_update(adapter, pkt_size, num_rcvd, pkt_type);
1138 static struct be_eth_rx_compl *be_rx_compl_get(struct be_adapter *adapter)
1140 struct be_eth_rx_compl *rxcp = queue_tail_node(&adapter->rx_obj.cq);
1142 if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
1146 be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
1148 queue_tail_inc(&adapter->rx_obj.cq);
1152 /* To reset the valid bit, we need to reset the whole word as
1153 * when walking the queue the valid entries are little-endian
1154 * and invalid entries are host endian
1156 static inline void be_rx_compl_reset(struct be_eth_rx_compl *rxcp)
1158 rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
1161 static inline struct page *be_alloc_pages(u32 size)
1163 gfp_t alloc_flags = GFP_ATOMIC;
1164 u32 order = get_order(size);
1166 alloc_flags |= __GFP_COMP;
1167 return alloc_pages(alloc_flags, order);
1171 * Allocate a page, split it to fragments of size rx_frag_size and post as
1172 * receive buffers to BE
1174 static void be_post_rx_frags(struct be_adapter *adapter)
1176 struct be_rx_page_info *page_info_tbl = adapter->rx_obj.page_info_tbl;
1177 struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
1178 struct be_queue_info *rxq = &adapter->rx_obj.q;
1179 struct page *pagep = NULL;
1180 struct be_eth_rx_d *rxd;
1181 u64 page_dmaaddr = 0, frag_dmaaddr;
1182 u32 posted, page_offset = 0;
1184 page_info = &page_info_tbl[rxq->head];
1185 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
1187 pagep = be_alloc_pages(adapter->big_page_size);
1188 if (unlikely(!pagep)) {
1189 drvr_stats(adapter)->be_ethrx_post_fail++;
1192 page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
1193 adapter->big_page_size,
1194 PCI_DMA_FROMDEVICE);
1195 page_info->page_offset = 0;
1198 page_info->page_offset = page_offset + rx_frag_size;
1200 page_offset = page_info->page_offset;
1201 page_info->page = pagep;
1202 dma_unmap_addr_set(page_info, bus, page_dmaaddr);
1203 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
1205 rxd = queue_head_node(rxq);
1206 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
1207 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
1209 /* Any space left in the current big page for another frag? */
1210 if ((page_offset + rx_frag_size + rx_frag_size) >
1211 adapter->big_page_size) {
1213 page_info->last_page_user = true;
1216 prev_page_info = page_info;
1217 queue_head_inc(rxq);
1218 page_info = &page_info_tbl[rxq->head];
1221 prev_page_info->last_page_user = true;
1224 atomic_add(posted, &rxq->used);
1225 be_rxq_notify(adapter, rxq->id, posted);
1226 } else if (atomic_read(&rxq->used) == 0) {
1227 /* Let be_worker replenish when memory is available */
1228 adapter->rx_post_starved = true;
1232 static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
1234 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
1236 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
1240 be_dws_le_to_cpu(txcp, sizeof(*txcp));
1242 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
1244 queue_tail_inc(tx_cq);
1248 static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
1250 struct be_queue_info *txq = &adapter->tx_obj.q;
1251 struct be_eth_wrb *wrb;
1252 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1253 struct sk_buff *sent_skb;
1254 u16 cur_index, num_wrbs = 1; /* account for hdr wrb */
1255 bool unmap_skb_hdr = true;
1257 sent_skb = sent_skbs[txq->tail];
1259 sent_skbs[txq->tail] = NULL;
1261 /* skip header wrb */
1262 queue_tail_inc(txq);
1265 cur_index = txq->tail;
1266 wrb = queue_tail_node(txq);
1267 unmap_tx_frag(adapter->pdev, wrb, (unmap_skb_hdr &&
1268 skb_headlen(sent_skb)));
1269 unmap_skb_hdr = false;
1272 queue_tail_inc(txq);
1273 } while (cur_index != last_index);
1275 atomic_sub(num_wrbs, &txq->used);
1277 kfree_skb(sent_skb);
1280 static inline struct be_eq_entry *event_get(struct be_eq_obj *eq_obj)
1282 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1288 eqe->evt = le32_to_cpu(eqe->evt);
1289 queue_tail_inc(&eq_obj->q);
1293 static int event_handle(struct be_adapter *adapter,
1294 struct be_eq_obj *eq_obj)
1296 struct be_eq_entry *eqe;
1299 while ((eqe = event_get(eq_obj)) != NULL) {
1304 /* Deal with any spurious interrupts that come
1307 be_eq_notify(adapter, eq_obj->q.id, true, true, num);
1309 napi_schedule(&eq_obj->napi);
1314 /* Just read and notify events without processing them.
1315 * Used at the time of destroying event queues */
1316 static void be_eq_clean(struct be_adapter *adapter,
1317 struct be_eq_obj *eq_obj)
1319 struct be_eq_entry *eqe;
1322 while ((eqe = event_get(eq_obj)) != NULL) {
1328 be_eq_notify(adapter, eq_obj->q.id, false, true, num);
1331 static void be_rx_q_clean(struct be_adapter *adapter)
1333 struct be_rx_page_info *page_info;
1334 struct be_queue_info *rxq = &adapter->rx_obj.q;
1335 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1336 struct be_eth_rx_compl *rxcp;
1339 /* First cleanup pending rx completions */
1340 while ((rxcp = be_rx_compl_get(adapter)) != NULL) {
1341 be_rx_compl_discard(adapter, rxcp);
1342 be_rx_compl_reset(rxcp);
1343 be_cq_notify(adapter, rx_cq->id, true, 1);
1346 /* Then free posted rx buffer that were not used */
1347 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
1348 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
1349 page_info = get_rx_page_info(adapter, tail);
1350 put_page(page_info->page);
1351 memset(page_info, 0, sizeof(*page_info));
1353 BUG_ON(atomic_read(&rxq->used));
1356 static void be_tx_compl_clean(struct be_adapter *adapter)
1358 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
1359 struct be_queue_info *txq = &adapter->tx_obj.q;
1360 struct be_eth_tx_compl *txcp;
1361 u16 end_idx, cmpl = 0, timeo = 0;
1362 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1363 struct sk_buff *sent_skb;
1366 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1368 while ((txcp = be_tx_compl_get(tx_cq))) {
1369 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1371 be_tx_compl_process(adapter, end_idx);
1375 be_cq_notify(adapter, tx_cq->id, false, cmpl);
1379 if (atomic_read(&txq->used) == 0 || ++timeo > 200)
1385 if (atomic_read(&txq->used))
1386 dev_err(&adapter->pdev->dev, "%d pending tx-completions\n",
1387 atomic_read(&txq->used));
1389 /* free posted tx for which compls will never arrive */
1390 while (atomic_read(&txq->used)) {
1391 sent_skb = sent_skbs[txq->tail];
1392 end_idx = txq->tail;
1394 wrb_cnt_for_skb(sent_skb, &dummy_wrb) - 1, txq->len);
1395 be_tx_compl_process(adapter, end_idx);
1399 static void be_mcc_queues_destroy(struct be_adapter *adapter)
1401 struct be_queue_info *q;
1403 q = &adapter->mcc_obj.q;
1405 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
1406 be_queue_free(adapter, q);
1408 q = &adapter->mcc_obj.cq;
1410 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1411 be_queue_free(adapter, q);
1414 /* Must be called only after TX qs are created as MCC shares TX EQ */
1415 static int be_mcc_queues_create(struct be_adapter *adapter)
1417 struct be_queue_info *q, *cq;
1419 /* Alloc MCC compl queue */
1420 cq = &adapter->mcc_obj.cq;
1421 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
1422 sizeof(struct be_mcc_compl)))
1425 /* Ask BE to create MCC compl queue; share TX's eq */
1426 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
1429 /* Alloc MCC queue */
1430 q = &adapter->mcc_obj.q;
1431 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1432 goto mcc_cq_destroy;
1434 /* Ask BE to create MCC queue */
1435 if (be_cmd_mccq_create(adapter, q, cq))
1441 be_queue_free(adapter, q);
1443 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
1445 be_queue_free(adapter, cq);
1450 static void be_tx_queues_destroy(struct be_adapter *adapter)
1452 struct be_queue_info *q;
1454 q = &adapter->tx_obj.q;
1456 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
1457 be_queue_free(adapter, q);
1459 q = &adapter->tx_obj.cq;
1461 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1462 be_queue_free(adapter, q);
1464 /* Clear any residual events */
1465 be_eq_clean(adapter, &adapter->tx_eq);
1467 q = &adapter->tx_eq.q;
1469 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
1470 be_queue_free(adapter, q);
1473 static int be_tx_queues_create(struct be_adapter *adapter)
1475 struct be_queue_info *eq, *q, *cq;
1477 adapter->tx_eq.max_eqd = 0;
1478 adapter->tx_eq.min_eqd = 0;
1479 adapter->tx_eq.cur_eqd = 96;
1480 adapter->tx_eq.enable_aic = false;
1481 /* Alloc Tx Event queue */
1482 eq = &adapter->tx_eq.q;
1483 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1486 /* Ask BE to create Tx Event queue */
1487 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
1489 adapter->base_eq_id = adapter->tx_eq.q.id;
1491 /* Alloc TX eth compl queue */
1492 cq = &adapter->tx_obj.cq;
1493 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1494 sizeof(struct be_eth_tx_compl)))
1497 /* Ask BE to create Tx eth compl queue */
1498 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
1501 /* Alloc TX eth queue */
1502 q = &adapter->tx_obj.q;
1503 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1506 /* Ask BE to create Tx eth queue */
1507 if (be_cmd_txq_create(adapter, q, cq))
1512 be_queue_free(adapter, q);
1514 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
1516 be_queue_free(adapter, cq);
1518 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
1520 be_queue_free(adapter, eq);
1524 static void be_rx_queues_destroy(struct be_adapter *adapter)
1526 struct be_queue_info *q;
1528 q = &adapter->rx_obj.q;
1530 be_cmd_q_destroy(adapter, q, QTYPE_RXQ);
1532 /* After the rxq is invalidated, wait for a grace time
1533 * of 1ms for all dma to end and the flush compl to arrive
1536 be_rx_q_clean(adapter);
1538 be_queue_free(adapter, q);
1540 q = &adapter->rx_obj.cq;
1542 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1543 be_queue_free(adapter, q);
1545 /* Clear any residual events */
1546 be_eq_clean(adapter, &adapter->rx_eq);
1548 q = &adapter->rx_eq.q;
1550 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
1551 be_queue_free(adapter, q);
1554 static int be_rx_queues_create(struct be_adapter *adapter)
1556 struct be_queue_info *eq, *q, *cq;
1559 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
1560 adapter->rx_eq.max_eqd = BE_MAX_EQD;
1561 adapter->rx_eq.min_eqd = 0;
1562 adapter->rx_eq.cur_eqd = 0;
1563 adapter->rx_eq.enable_aic = true;
1565 /* Alloc Rx Event queue */
1566 eq = &adapter->rx_eq.q;
1567 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1568 sizeof(struct be_eq_entry));
1572 /* Ask BE to create Rx Event queue */
1573 rc = be_cmd_eq_create(adapter, eq, adapter->rx_eq.cur_eqd);
1577 /* Alloc RX eth compl queue */
1578 cq = &adapter->rx_obj.cq;
1579 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1580 sizeof(struct be_eth_rx_compl));
1584 /* Ask BE to create Rx eth compl queue */
1585 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
1589 /* Alloc RX eth queue */
1590 q = &adapter->rx_obj.q;
1591 rc = be_queue_alloc(adapter, q, RX_Q_LEN, sizeof(struct be_eth_rx_d));
1595 /* Ask BE to create Rx eth queue */
1596 rc = be_cmd_rxq_create(adapter, q, cq->id, rx_frag_size,
1597 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle, false);
1603 be_queue_free(adapter, q);
1605 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
1607 be_queue_free(adapter, cq);
1609 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
1611 be_queue_free(adapter, eq);
1615 /* There are 8 evt ids per func. Retruns the evt id's bit number */
1616 static inline int be_evt_bit_get(struct be_adapter *adapter, u32 eq_id)
1618 return eq_id - adapter->base_eq_id;
1621 static irqreturn_t be_intx(int irq, void *dev)
1623 struct be_adapter *adapter = dev;
1626 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
1627 (adapter->tx_eq.q.id/ 8) * CEV_ISR_SIZE);
1631 event_handle(adapter, &adapter->tx_eq);
1632 event_handle(adapter, &adapter->rx_eq);
1637 static irqreturn_t be_msix_rx(int irq, void *dev)
1639 struct be_adapter *adapter = dev;
1641 event_handle(adapter, &adapter->rx_eq);
1646 static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
1648 struct be_adapter *adapter = dev;
1650 event_handle(adapter, &adapter->tx_eq);
1655 static inline bool do_gro(struct be_adapter *adapter,
1656 struct be_eth_rx_compl *rxcp)
1658 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1659 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1662 drvr_stats(adapter)->be_rxcp_err++;
1664 return (tcp_frame && !err) ? true : false;
1667 int be_poll_rx(struct napi_struct *napi, int budget)
1669 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
1670 struct be_adapter *adapter =
1671 container_of(rx_eq, struct be_adapter, rx_eq);
1672 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1673 struct be_eth_rx_compl *rxcp;
1676 adapter->stats.drvr_stats.be_rx_polls++;
1677 for (work_done = 0; work_done < budget; work_done++) {
1678 rxcp = be_rx_compl_get(adapter);
1682 if (do_gro(adapter, rxcp))
1683 be_rx_compl_process_gro(adapter, rxcp);
1685 be_rx_compl_process(adapter, rxcp);
1687 be_rx_compl_reset(rxcp);
1690 /* Refill the queue */
1691 if (atomic_read(&adapter->rx_obj.q.used) < RX_FRAGS_REFILL_WM)
1692 be_post_rx_frags(adapter);
1695 if (work_done < budget) {
1696 napi_complete(napi);
1697 be_cq_notify(adapter, rx_cq->id, true, work_done);
1699 /* More to be consumed; continue with interrupts disabled */
1700 be_cq_notify(adapter, rx_cq->id, false, work_done);
1705 /* As TX and MCC share the same EQ check for both TX and MCC completions.
1706 * For TX/MCC we don't honour budget; consume everything
1708 static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
1710 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1711 struct be_adapter *adapter =
1712 container_of(tx_eq, struct be_adapter, tx_eq);
1713 struct be_queue_info *txq = &adapter->tx_obj.q;
1714 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
1715 struct be_eth_tx_compl *txcp;
1716 int tx_compl = 0, mcc_compl, status = 0;
1719 while ((txcp = be_tx_compl_get(tx_cq))) {
1720 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1722 be_tx_compl_process(adapter, end_idx);
1726 mcc_compl = be_process_mcc(adapter, &status);
1728 napi_complete(napi);
1731 struct be_mcc_obj *mcc_obj = &adapter->mcc_obj;
1732 be_cq_notify(adapter, mcc_obj->cq.id, true, mcc_compl);
1736 be_cq_notify(adapter, adapter->tx_obj.cq.id, true, tx_compl);
1738 /* As Tx wrbs have been freed up, wake up netdev queue if
1739 * it was stopped due to lack of tx wrbs.
1741 if (netif_queue_stopped(adapter->netdev) &&
1742 atomic_read(&txq->used) < txq->len / 2) {
1743 netif_wake_queue(adapter->netdev);
1746 drvr_stats(adapter)->be_tx_events++;
1747 drvr_stats(adapter)->be_tx_compl += tx_compl;
1753 void be_detect_dump_ue(struct be_adapter *adapter)
1755 u32 ue_status_lo, ue_status_hi, ue_status_lo_mask, ue_status_hi_mask;
1758 pci_read_config_dword(adapter->pdev,
1759 PCICFG_UE_STATUS_LOW, &ue_status_lo);
1760 pci_read_config_dword(adapter->pdev,
1761 PCICFG_UE_STATUS_HIGH, &ue_status_hi);
1762 pci_read_config_dword(adapter->pdev,
1763 PCICFG_UE_STATUS_LOW_MASK, &ue_status_lo_mask);
1764 pci_read_config_dword(adapter->pdev,
1765 PCICFG_UE_STATUS_HI_MASK, &ue_status_hi_mask);
1767 ue_status_lo = (ue_status_lo & (~ue_status_lo_mask));
1768 ue_status_hi = (ue_status_hi & (~ue_status_hi_mask));
1770 if (ue_status_lo || ue_status_hi) {
1771 adapter->ue_detected = true;
1772 dev_err(&adapter->pdev->dev, "UE Detected!!\n");
1776 for (i = 0; ue_status_lo; ue_status_lo >>= 1, i++) {
1777 if (ue_status_lo & 1)
1778 dev_err(&adapter->pdev->dev,
1779 "UE: %s bit set\n", ue_status_low_desc[i]);
1783 for (i = 0; ue_status_hi; ue_status_hi >>= 1, i++) {
1784 if (ue_status_hi & 1)
1785 dev_err(&adapter->pdev->dev,
1786 "UE: %s bit set\n", ue_status_hi_desc[i]);
1792 static void be_worker(struct work_struct *work)
1794 struct be_adapter *adapter =
1795 container_of(work, struct be_adapter, work.work);
1797 if (!adapter->stats_ioctl_sent)
1798 be_cmd_get_stats(adapter, &adapter->stats.cmd);
1801 be_rx_eqd_update(adapter);
1803 be_tx_rate_update(adapter);
1804 be_rx_rate_update(adapter);
1806 if (adapter->rx_post_starved) {
1807 adapter->rx_post_starved = false;
1808 be_post_rx_frags(adapter);
1810 if (!adapter->ue_detected)
1811 be_detect_dump_ue(adapter);
1813 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1816 static void be_msix_disable(struct be_adapter *adapter)
1818 if (adapter->msix_enabled) {
1819 pci_disable_msix(adapter->pdev);
1820 adapter->msix_enabled = false;
1824 static void be_msix_enable(struct be_adapter *adapter)
1828 for (i = 0; i < BE_NUM_MSIX_VECTORS; i++)
1829 adapter->msix_entries[i].entry = i;
1831 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1832 BE_NUM_MSIX_VECTORS);
1834 adapter->msix_enabled = true;
1837 static void be_sriov_enable(struct be_adapter *adapter)
1839 be_check_sriov_fn_type(adapter);
1840 #ifdef CONFIG_PCI_IOV
1841 if (be_physfn(adapter) && num_vfs) {
1844 status = pci_enable_sriov(adapter->pdev, num_vfs);
1845 adapter->sriov_enabled = status ? false : true;
1850 static void be_sriov_disable(struct be_adapter *adapter)
1852 #ifdef CONFIG_PCI_IOV
1853 if (adapter->sriov_enabled) {
1854 pci_disable_sriov(adapter->pdev);
1855 adapter->sriov_enabled = false;
1860 static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1862 return adapter->msix_entries[
1863 be_evt_bit_get(adapter, eq_id)].vector;
1866 static int be_request_irq(struct be_adapter *adapter,
1867 struct be_eq_obj *eq_obj,
1868 void *handler, char *desc)
1870 struct net_device *netdev = adapter->netdev;
1873 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
1874 vec = be_msix_vec_get(adapter, eq_obj->q.id);
1875 return request_irq(vec, handler, 0, eq_obj->desc, adapter);
1878 static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj)
1880 int vec = be_msix_vec_get(adapter, eq_obj->q.id);
1881 free_irq(vec, adapter);
1884 static int be_msix_register(struct be_adapter *adapter)
1888 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx");
1892 status = be_request_irq(adapter, &adapter->rx_eq, be_msix_rx, "rx");
1899 be_free_irq(adapter, &adapter->tx_eq);
1901 dev_warn(&adapter->pdev->dev,
1902 "MSIX Request IRQ failed - err %d\n", status);
1903 pci_disable_msix(adapter->pdev);
1904 adapter->msix_enabled = false;
1908 static int be_irq_register(struct be_adapter *adapter)
1910 struct net_device *netdev = adapter->netdev;
1913 if (adapter->msix_enabled) {
1914 status = be_msix_register(adapter);
1917 /* INTx is not supported for VF */
1918 if (!be_physfn(adapter))
1923 netdev->irq = adapter->pdev->irq;
1924 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1927 dev_err(&adapter->pdev->dev,
1928 "INTx request IRQ failed - err %d\n", status);
1932 adapter->isr_registered = true;
1936 static void be_irq_unregister(struct be_adapter *adapter)
1938 struct net_device *netdev = adapter->netdev;
1940 if (!adapter->isr_registered)
1944 if (!adapter->msix_enabled) {
1945 free_irq(netdev->irq, adapter);
1950 be_free_irq(adapter, &adapter->tx_eq);
1951 be_free_irq(adapter, &adapter->rx_eq);
1953 adapter->isr_registered = false;
1956 static int be_close(struct net_device *netdev)
1958 struct be_adapter *adapter = netdev_priv(netdev);
1959 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1960 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1963 cancel_delayed_work_sync(&adapter->work);
1965 be_async_mcc_disable(adapter);
1967 netif_stop_queue(netdev);
1968 netif_carrier_off(netdev);
1969 adapter->link_up = false;
1971 be_intr_set(adapter, false);
1973 if (adapter->msix_enabled) {
1974 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1975 synchronize_irq(vec);
1976 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1977 synchronize_irq(vec);
1979 synchronize_irq(netdev->irq);
1981 be_irq_unregister(adapter);
1983 napi_disable(&rx_eq->napi);
1984 napi_disable(&tx_eq->napi);
1986 /* Wait for all pending tx completions to arrive so that
1987 * all tx skbs are freed.
1989 be_tx_compl_clean(adapter);
1994 static int be_open(struct net_device *netdev)
1996 struct be_adapter *adapter = netdev_priv(netdev);
1997 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1998 struct be_eq_obj *tx_eq = &adapter->tx_eq;
2004 /* First time posting */
2005 be_post_rx_frags(adapter);
2007 napi_enable(&rx_eq->napi);
2008 napi_enable(&tx_eq->napi);
2010 be_irq_register(adapter);
2012 be_intr_set(adapter, true);
2014 /* The evt queues are created in unarmed state; arm them */
2015 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
2016 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
2018 /* Rx compl queue may be in unarmed state; rearm it */
2019 be_cq_notify(adapter, adapter->rx_obj.cq.id, true, 0);
2021 /* Now that interrupts are on we can process async mcc */
2022 be_async_mcc_enable(adapter);
2024 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
2026 status = be_cmd_link_status_query(adapter, &link_up, &mac_speed,
2030 be_link_status_update(adapter, link_up);
2032 if (be_physfn(adapter)) {
2033 status = be_vid_config(adapter, false, 0);
2037 status = be_cmd_set_flow_control(adapter,
2038 adapter->tx_fc, adapter->rx_fc);
2045 be_close(adapter->netdev);
2049 static int be_setup_wol(struct be_adapter *adapter, bool enable)
2051 struct be_dma_mem cmd;
2055 memset(mac, 0, ETH_ALEN);
2057 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
2058 cmd.va = pci_alloc_consistent(adapter->pdev, cmd.size, &cmd.dma);
2061 memset(cmd.va, 0, cmd.size);
2064 status = pci_write_config_dword(adapter->pdev,
2065 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
2067 dev_err(&adapter->pdev->dev,
2068 "Could not enable Wake-on-lan\n");
2069 pci_free_consistent(adapter->pdev, cmd.size, cmd.va,
2073 status = be_cmd_enable_magic_wol(adapter,
2074 adapter->netdev->dev_addr, &cmd);
2075 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
2076 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
2078 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
2079 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
2080 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
2083 pci_free_consistent(adapter->pdev, cmd.size, cmd.va, cmd.dma);
2087 static int be_setup(struct be_adapter *adapter)
2089 struct net_device *netdev = adapter->netdev;
2090 u32 cap_flags, en_flags, vf = 0;
2094 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST;
2096 if (be_physfn(adapter)) {
2097 cap_flags |= BE_IF_FLAGS_MCAST_PROMISCUOUS |
2098 BE_IF_FLAGS_PROMISCUOUS |
2099 BE_IF_FLAGS_PASS_L3L4_ERRORS;
2100 en_flags |= BE_IF_FLAGS_PASS_L3L4_ERRORS;
2103 status = be_cmd_if_create(adapter, cap_flags, en_flags,
2104 netdev->dev_addr, false/* pmac_invalid */,
2105 &adapter->if_handle, &adapter->pmac_id, 0);
2109 if (be_physfn(adapter)) {
2110 while (vf < num_vfs) {
2111 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED
2112 | BE_IF_FLAGS_BROADCAST;
2113 status = be_cmd_if_create(adapter, cap_flags, en_flags,
2115 &adapter->vf_cfg[vf].vf_if_handle,
2118 dev_err(&adapter->pdev->dev,
2119 "Interface Create failed for VF %d\n", vf);
2122 adapter->vf_cfg[vf].vf_pmac_id = BE_INVALID_PMAC_ID;
2125 } else if (!be_physfn(adapter)) {
2126 status = be_cmd_mac_addr_query(adapter, mac,
2127 MAC_ADDRESS_TYPE_NETWORK, false, adapter->if_handle);
2129 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2130 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2134 status = be_tx_queues_create(adapter);
2138 status = be_rx_queues_create(adapter);
2142 status = be_mcc_queues_create(adapter);
2146 adapter->link_speed = -1;
2151 be_rx_queues_destroy(adapter);
2153 be_tx_queues_destroy(adapter);
2155 for (vf = 0; vf < num_vfs; vf++)
2156 if (adapter->vf_cfg[vf].vf_if_handle)
2157 be_cmd_if_destroy(adapter,
2158 adapter->vf_cfg[vf].vf_if_handle);
2159 be_cmd_if_destroy(adapter, adapter->if_handle);
2164 static int be_clear(struct be_adapter *adapter)
2166 be_mcc_queues_destroy(adapter);
2167 be_rx_queues_destroy(adapter);
2168 be_tx_queues_destroy(adapter);
2170 be_cmd_if_destroy(adapter, adapter->if_handle);
2172 /* tell fw we're done with firing cmds */
2173 be_cmd_fw_clean(adapter);
2178 #define FW_FILE_HDR_SIGN "ServerEngines Corp. "
2179 char flash_cookie[2][16] = {"*** SE FLAS",
2180 "H DIRECTORY *** "};
2182 static bool be_flash_redboot(struct be_adapter *adapter,
2183 const u8 *p, u32 img_start, int image_size,
2190 crc_offset = hdr_size + img_start + image_size - 4;
2194 status = be_cmd_get_flash_crc(adapter, flashed_crc,
2197 dev_err(&adapter->pdev->dev,
2198 "could not get crc from flash, not flashing redboot\n");
2202 /*update redboot only if crc does not match*/
2203 if (!memcmp(flashed_crc, p, 4))
2209 static int be_flash_data(struct be_adapter *adapter,
2210 const struct firmware *fw,
2211 struct be_dma_mem *flash_cmd, int num_of_images)
2214 int status = 0, i, filehdr_size = 0;
2215 u32 total_bytes = 0, flash_op;
2217 const u8 *p = fw->data;
2218 struct be_cmd_write_flashrom *req = flash_cmd->va;
2219 struct flash_comp *pflashcomp;
2222 struct flash_comp gen3_flash_types[9] = {
2223 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, IMG_TYPE_ISCSI_ACTIVE,
2224 FLASH_IMAGE_MAX_SIZE_g3},
2225 { FLASH_REDBOOT_START_g3, IMG_TYPE_REDBOOT,
2226 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3},
2227 { FLASH_iSCSI_BIOS_START_g3, IMG_TYPE_BIOS,
2228 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2229 { FLASH_PXE_BIOS_START_g3, IMG_TYPE_PXE_BIOS,
2230 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2231 { FLASH_FCoE_BIOS_START_g3, IMG_TYPE_FCOE_BIOS,
2232 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2233 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, IMG_TYPE_ISCSI_BACKUP,
2234 FLASH_IMAGE_MAX_SIZE_g3},
2235 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, IMG_TYPE_FCOE_FW_ACTIVE,
2236 FLASH_IMAGE_MAX_SIZE_g3},
2237 { FLASH_FCoE_BACKUP_IMAGE_START_g3, IMG_TYPE_FCOE_FW_BACKUP,
2238 FLASH_IMAGE_MAX_SIZE_g3},
2239 { FLASH_NCSI_START_g3, IMG_TYPE_NCSI_FW,
2240 FLASH_NCSI_IMAGE_MAX_SIZE_g3}
2242 struct flash_comp gen2_flash_types[8] = {
2243 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, IMG_TYPE_ISCSI_ACTIVE,
2244 FLASH_IMAGE_MAX_SIZE_g2},
2245 { FLASH_REDBOOT_START_g2, IMG_TYPE_REDBOOT,
2246 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2},
2247 { FLASH_iSCSI_BIOS_START_g2, IMG_TYPE_BIOS,
2248 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2249 { FLASH_PXE_BIOS_START_g2, IMG_TYPE_PXE_BIOS,
2250 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2251 { FLASH_FCoE_BIOS_START_g2, IMG_TYPE_FCOE_BIOS,
2252 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2253 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, IMG_TYPE_ISCSI_BACKUP,
2254 FLASH_IMAGE_MAX_SIZE_g2},
2255 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, IMG_TYPE_FCOE_FW_ACTIVE,
2256 FLASH_IMAGE_MAX_SIZE_g2},
2257 { FLASH_FCoE_BACKUP_IMAGE_START_g2, IMG_TYPE_FCOE_FW_BACKUP,
2258 FLASH_IMAGE_MAX_SIZE_g2}
2261 if (adapter->generation == BE_GEN3) {
2262 pflashcomp = gen3_flash_types;
2263 filehdr_size = sizeof(struct flash_file_hdr_g3);
2266 pflashcomp = gen2_flash_types;
2267 filehdr_size = sizeof(struct flash_file_hdr_g2);
2270 for (i = 0; i < num_comp; i++) {
2271 if ((pflashcomp[i].optype == IMG_TYPE_NCSI_FW) &&
2272 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
2274 if ((pflashcomp[i].optype == IMG_TYPE_REDBOOT) &&
2275 (!be_flash_redboot(adapter, fw->data,
2276 pflashcomp[i].offset, pflashcomp[i].size,
2280 p += filehdr_size + pflashcomp[i].offset
2281 + (num_of_images * sizeof(struct image_hdr));
2282 if (p + pflashcomp[i].size > fw->data + fw->size)
2284 total_bytes = pflashcomp[i].size;
2285 while (total_bytes) {
2286 if (total_bytes > 32*1024)
2287 num_bytes = 32*1024;
2289 num_bytes = total_bytes;
2290 total_bytes -= num_bytes;
2293 flash_op = FLASHROM_OPER_FLASH;
2295 flash_op = FLASHROM_OPER_SAVE;
2296 memcpy(req->params.data_buf, p, num_bytes);
2298 status = be_cmd_write_flashrom(adapter, flash_cmd,
2299 pflashcomp[i].optype, flash_op, num_bytes);
2301 dev_err(&adapter->pdev->dev,
2302 "cmd to write to flash rom failed.\n");
2311 static int get_ufigen_type(struct flash_file_hdr_g2 *fhdr)
2315 if (fhdr->build[0] == '3')
2317 else if (fhdr->build[0] == '2')
2323 int be_load_fw(struct be_adapter *adapter, u8 *func)
2325 char fw_file[ETHTOOL_FLASH_MAX_FILENAME];
2326 const struct firmware *fw;
2327 struct flash_file_hdr_g2 *fhdr;
2328 struct flash_file_hdr_g3 *fhdr3;
2329 struct image_hdr *img_hdr_ptr = NULL;
2330 struct be_dma_mem flash_cmd;
2331 int status, i = 0, num_imgs = 0;
2334 strcpy(fw_file, func);
2336 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
2341 fhdr = (struct flash_file_hdr_g2 *) p;
2342 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
2344 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
2345 flash_cmd.va = pci_alloc_consistent(adapter->pdev, flash_cmd.size,
2347 if (!flash_cmd.va) {
2349 dev_err(&adapter->pdev->dev,
2350 "Memory allocation failure while flashing\n");
2354 if ((adapter->generation == BE_GEN3) &&
2355 (get_ufigen_type(fhdr) == BE_GEN3)) {
2356 fhdr3 = (struct flash_file_hdr_g3 *) fw->data;
2357 num_imgs = le32_to_cpu(fhdr3->num_imgs);
2358 for (i = 0; i < num_imgs; i++) {
2359 img_hdr_ptr = (struct image_hdr *) (fw->data +
2360 (sizeof(struct flash_file_hdr_g3) +
2361 i * sizeof(struct image_hdr)));
2362 if (le32_to_cpu(img_hdr_ptr->imageid) == 1)
2363 status = be_flash_data(adapter, fw, &flash_cmd,
2366 } else if ((adapter->generation == BE_GEN2) &&
2367 (get_ufigen_type(fhdr) == BE_GEN2)) {
2368 status = be_flash_data(adapter, fw, &flash_cmd, 0);
2370 dev_err(&adapter->pdev->dev,
2371 "UFI and Interface are not compatible for flashing\n");
2375 pci_free_consistent(adapter->pdev, flash_cmd.size, flash_cmd.va,
2378 dev_err(&adapter->pdev->dev, "Firmware load error\n");
2382 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
2385 release_firmware(fw);
2389 static struct net_device_ops be_netdev_ops = {
2390 .ndo_open = be_open,
2391 .ndo_stop = be_close,
2392 .ndo_start_xmit = be_xmit,
2393 .ndo_get_stats = be_get_stats,
2394 .ndo_set_rx_mode = be_set_multicast_list,
2395 .ndo_set_mac_address = be_mac_addr_set,
2396 .ndo_change_mtu = be_change_mtu,
2397 .ndo_validate_addr = eth_validate_addr,
2398 .ndo_vlan_rx_register = be_vlan_register,
2399 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
2400 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
2401 .ndo_set_vf_mac = be_set_vf_mac,
2402 .ndo_set_vf_vlan = be_set_vf_vlan,
2403 .ndo_set_vf_tx_rate = be_set_vf_tx_rate,
2404 .ndo_get_vf_config = be_get_vf_config
2407 static void be_netdev_init(struct net_device *netdev)
2409 struct be_adapter *adapter = netdev_priv(netdev);
2411 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
2412 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_HW_CSUM |
2413 NETIF_F_GRO | NETIF_F_TSO6;
2415 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2417 netdev->flags |= IFF_MULTICAST;
2419 adapter->rx_csum = true;
2421 /* Default settings for Rx and Tx flow control */
2422 adapter->rx_fc = true;
2423 adapter->tx_fc = true;
2425 netif_set_gso_max_size(netdev, 65535);
2427 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
2429 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
2431 netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx,
2433 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
2436 netif_carrier_off(netdev);
2437 netif_stop_queue(netdev);
2440 static void be_unmap_pci_bars(struct be_adapter *adapter)
2443 iounmap(adapter->csr);
2445 iounmap(adapter->db);
2446 if (adapter->pcicfg && be_physfn(adapter))
2447 iounmap(adapter->pcicfg);
2450 static int be_map_pci_bars(struct be_adapter *adapter)
2453 int pcicfg_reg, db_reg;
2455 if (be_physfn(adapter)) {
2456 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
2457 pci_resource_len(adapter->pdev, 2));
2460 adapter->csr = addr;
2463 if (adapter->generation == BE_GEN2) {
2468 if (be_physfn(adapter))
2473 addr = ioremap_nocache(pci_resource_start(adapter->pdev, db_reg),
2474 pci_resource_len(adapter->pdev, db_reg));
2479 if (be_physfn(adapter)) {
2480 addr = ioremap_nocache(
2481 pci_resource_start(adapter->pdev, pcicfg_reg),
2482 pci_resource_len(adapter->pdev, pcicfg_reg));
2485 adapter->pcicfg = addr;
2487 adapter->pcicfg = adapter->db + SRIOV_VF_PCICFG_OFFSET;
2491 be_unmap_pci_bars(adapter);
2496 static void be_ctrl_cleanup(struct be_adapter *adapter)
2498 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
2500 be_unmap_pci_bars(adapter);
2503 pci_free_consistent(adapter->pdev, mem->size,
2506 mem = &adapter->mc_cmd_mem;
2508 pci_free_consistent(adapter->pdev, mem->size,
2512 static int be_ctrl_init(struct be_adapter *adapter)
2514 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
2515 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
2516 struct be_dma_mem *mc_cmd_mem = &adapter->mc_cmd_mem;
2519 status = be_map_pci_bars(adapter);
2523 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
2524 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
2525 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
2526 if (!mbox_mem_alloc->va) {
2528 goto unmap_pci_bars;
2531 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
2532 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
2533 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
2534 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
2536 mc_cmd_mem->size = sizeof(struct be_cmd_req_mcast_mac_config);
2537 mc_cmd_mem->va = pci_alloc_consistent(adapter->pdev, mc_cmd_mem->size,
2539 if (mc_cmd_mem->va == NULL) {
2543 memset(mc_cmd_mem->va, 0, mc_cmd_mem->size);
2545 spin_lock_init(&adapter->mbox_lock);
2546 spin_lock_init(&adapter->mcc_lock);
2547 spin_lock_init(&adapter->mcc_cq_lock);
2549 init_completion(&adapter->flash_compl);
2550 pci_save_state(adapter->pdev);
2554 pci_free_consistent(adapter->pdev, mbox_mem_alloc->size,
2555 mbox_mem_alloc->va, mbox_mem_alloc->dma);
2558 be_unmap_pci_bars(adapter);
2564 static void be_stats_cleanup(struct be_adapter *adapter)
2566 struct be_stats_obj *stats = &adapter->stats;
2567 struct be_dma_mem *cmd = &stats->cmd;
2570 pci_free_consistent(adapter->pdev, cmd->size,
2574 static int be_stats_init(struct be_adapter *adapter)
2576 struct be_stats_obj *stats = &adapter->stats;
2577 struct be_dma_mem *cmd = &stats->cmd;
2579 cmd->size = sizeof(struct be_cmd_req_get_stats);
2580 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
2581 if (cmd->va == NULL)
2583 memset(cmd->va, 0, cmd->size);
2587 static void __devexit be_remove(struct pci_dev *pdev)
2589 struct be_adapter *adapter = pci_get_drvdata(pdev);
2594 unregister_netdev(adapter->netdev);
2598 be_stats_cleanup(adapter);
2600 be_ctrl_cleanup(adapter);
2602 be_sriov_disable(adapter);
2604 be_msix_disable(adapter);
2606 pci_set_drvdata(pdev, NULL);
2607 pci_release_regions(pdev);
2608 pci_disable_device(pdev);
2610 free_netdev(adapter->netdev);
2613 static int be_get_config(struct be_adapter *adapter)
2618 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
2622 status = be_cmd_query_fw_cfg(adapter,
2623 &adapter->port_num, &adapter->function_mode);
2627 memset(mac, 0, ETH_ALEN);
2629 if (be_physfn(adapter)) {
2630 status = be_cmd_mac_addr_query(adapter, mac,
2631 MAC_ADDRESS_TYPE_NETWORK, true /*permanent */, 0);
2636 if (!is_valid_ether_addr(mac))
2637 return -EADDRNOTAVAIL;
2639 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2640 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2643 if (adapter->function_mode & 0x400)
2644 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED/4;
2646 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED;
2651 static int __devinit be_probe(struct pci_dev *pdev,
2652 const struct pci_device_id *pdev_id)
2655 struct be_adapter *adapter;
2656 struct net_device *netdev;
2659 status = pci_enable_device(pdev);
2663 status = pci_request_regions(pdev, DRV_NAME);
2666 pci_set_master(pdev);
2668 netdev = alloc_etherdev(sizeof(struct be_adapter));
2669 if (netdev == NULL) {
2673 adapter = netdev_priv(netdev);
2675 switch (pdev->device) {
2678 adapter->generation = BE_GEN2;
2682 adapter->generation = BE_GEN3;
2685 adapter->generation = 0;
2688 adapter->pdev = pdev;
2689 pci_set_drvdata(pdev, adapter);
2690 adapter->netdev = netdev;
2691 be_netdev_init(netdev);
2692 SET_NETDEV_DEV(netdev, &pdev->dev);
2694 be_msix_enable(adapter);
2696 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2698 netdev->features |= NETIF_F_HIGHDMA;
2700 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2702 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
2707 be_sriov_enable(adapter);
2709 status = be_ctrl_init(adapter);
2713 /* sync up with fw's ready state */
2714 if (be_physfn(adapter)) {
2715 status = be_cmd_POST(adapter);
2720 /* tell fw we're ready to fire cmds */
2721 status = be_cmd_fw_init(adapter);
2725 if (be_physfn(adapter)) {
2726 status = be_cmd_reset_function(adapter);
2731 status = be_stats_init(adapter);
2735 status = be_get_config(adapter);
2739 INIT_DELAYED_WORK(&adapter->work, be_worker);
2741 status = be_setup(adapter);
2745 status = register_netdev(netdev);
2749 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
2755 be_stats_cleanup(adapter);
2757 be_ctrl_cleanup(adapter);
2759 be_msix_disable(adapter);
2760 be_sriov_disable(adapter);
2761 free_netdev(adapter->netdev);
2762 pci_set_drvdata(pdev, NULL);
2764 pci_release_regions(pdev);
2766 pci_disable_device(pdev);
2768 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
2772 static int be_suspend(struct pci_dev *pdev, pm_message_t state)
2774 struct be_adapter *adapter = pci_get_drvdata(pdev);
2775 struct net_device *netdev = adapter->netdev;
2778 be_setup_wol(adapter, true);
2780 netif_device_detach(netdev);
2781 if (netif_running(netdev)) {
2786 be_cmd_get_flow_control(adapter, &adapter->tx_fc, &adapter->rx_fc);
2789 pci_save_state(pdev);
2790 pci_disable_device(pdev);
2791 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2795 static int be_resume(struct pci_dev *pdev)
2798 struct be_adapter *adapter = pci_get_drvdata(pdev);
2799 struct net_device *netdev = adapter->netdev;
2801 netif_device_detach(netdev);
2803 status = pci_enable_device(pdev);
2807 pci_set_power_state(pdev, 0);
2808 pci_restore_state(pdev);
2810 /* tell fw we're ready to fire cmds */
2811 status = be_cmd_fw_init(adapter);
2816 if (netif_running(netdev)) {
2821 netif_device_attach(netdev);
2824 be_setup_wol(adapter, false);
2829 * An FLR will stop BE from DMAing any data.
2831 static void be_shutdown(struct pci_dev *pdev)
2833 struct be_adapter *adapter = pci_get_drvdata(pdev);
2834 struct net_device *netdev = adapter->netdev;
2836 netif_device_detach(netdev);
2838 be_cmd_reset_function(adapter);
2841 be_setup_wol(adapter, true);
2843 pci_disable_device(pdev);
2846 static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
2847 pci_channel_state_t state)
2849 struct be_adapter *adapter = pci_get_drvdata(pdev);
2850 struct net_device *netdev = adapter->netdev;
2852 dev_err(&adapter->pdev->dev, "EEH error detected\n");
2854 adapter->eeh_err = true;
2856 netif_device_detach(netdev);
2858 if (netif_running(netdev)) {
2865 if (state == pci_channel_io_perm_failure)
2866 return PCI_ERS_RESULT_DISCONNECT;
2868 pci_disable_device(pdev);
2870 return PCI_ERS_RESULT_NEED_RESET;
2873 static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
2875 struct be_adapter *adapter = pci_get_drvdata(pdev);
2878 dev_info(&adapter->pdev->dev, "EEH reset\n");
2879 adapter->eeh_err = false;
2881 status = pci_enable_device(pdev);
2883 return PCI_ERS_RESULT_DISCONNECT;
2885 pci_set_master(pdev);
2886 pci_set_power_state(pdev, 0);
2887 pci_restore_state(pdev);
2889 /* Check if card is ok and fw is ready */
2890 status = be_cmd_POST(adapter);
2892 return PCI_ERS_RESULT_DISCONNECT;
2894 return PCI_ERS_RESULT_RECOVERED;
2897 static void be_eeh_resume(struct pci_dev *pdev)
2900 struct be_adapter *adapter = pci_get_drvdata(pdev);
2901 struct net_device *netdev = adapter->netdev;
2903 dev_info(&adapter->pdev->dev, "EEH resume\n");
2905 pci_save_state(pdev);
2907 /* tell fw we're ready to fire cmds */
2908 status = be_cmd_fw_init(adapter);
2912 status = be_setup(adapter);
2916 if (netif_running(netdev)) {
2917 status = be_open(netdev);
2921 netif_device_attach(netdev);
2924 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
2927 static struct pci_error_handlers be_eeh_handlers = {
2928 .error_detected = be_eeh_err_detected,
2929 .slot_reset = be_eeh_reset,
2930 .resume = be_eeh_resume,
2933 static struct pci_driver be_driver = {
2935 .id_table = be_dev_ids,
2937 .remove = be_remove,
2938 .suspend = be_suspend,
2939 .resume = be_resume,
2940 .shutdown = be_shutdown,
2941 .err_handler = &be_eeh_handlers
2944 static int __init be_init_module(void)
2946 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
2947 rx_frag_size != 2048) {
2948 printk(KERN_WARNING DRV_NAME
2949 " : Module param rx_frag_size must be 2048/4096/8192."
2951 rx_frag_size = 2048;
2955 printk(KERN_WARNING DRV_NAME
2956 " : Module param num_vfs must not be greater than 32."
2961 return pci_register_driver(&be_driver);
2963 module_init(be_init_module);
2965 static void __exit be_exit_module(void)
2967 pci_unregister_driver(&be_driver);
2969 module_exit(be_exit_module);