2 * Copyright (C) 2005 - 2009 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
19 #include <asm/div64.h>
21 MODULE_VERSION(DRV_VER);
22 MODULE_DEVICE_TABLE(pci, be_dev_ids);
23 MODULE_DESCRIPTION(DRV_DESC " " DRV_VER);
24 MODULE_AUTHOR("ServerEngines Corporation");
25 MODULE_LICENSE("GPL");
27 static unsigned int rx_frag_size = 2048;
28 module_param(rx_frag_size, uint, S_IRUGO);
29 MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data.");
31 #define BE_VENDOR_ID 0x19a2
32 #define BE2_DEVICE_ID_1 0x0211
33 static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
34 { PCI_DEVICE(BE_VENDOR_ID, BE2_DEVICE_ID_1) },
37 MODULE_DEVICE_TABLE(pci, be_dev_ids);
39 static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
41 struct be_dma_mem *mem = &q->dma_mem;
43 pci_free_consistent(adapter->pdev, mem->size,
47 static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
48 u16 len, u16 entry_size)
50 struct be_dma_mem *mem = &q->dma_mem;
52 memset(q, 0, sizeof(*q));
54 q->entry_size = entry_size;
55 mem->size = len * entry_size;
56 mem->va = pci_alloc_consistent(adapter->pdev, mem->size, &mem->dma);
59 memset(mem->va, 0, mem->size);
63 static inline void *queue_head_node(struct be_queue_info *q)
65 return q->dma_mem.va + q->head * q->entry_size;
68 static inline void *queue_tail_node(struct be_queue_info *q)
70 return q->dma_mem.va + q->tail * q->entry_size;
73 static inline void queue_head_inc(struct be_queue_info *q)
75 index_inc(&q->head, q->len);
78 static inline void queue_tail_inc(struct be_queue_info *q)
80 index_inc(&q->tail, q->len);
83 static void be_intr_set(struct be_ctrl_info *ctrl, bool enable)
85 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
86 u32 reg = ioread32(addr);
87 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
88 if (!enabled && enable) {
89 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
90 } else if (enabled && !enable) {
91 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
93 printk(KERN_WARNING DRV_NAME
94 ": bad value in membar_int_ctrl reg=0x%x\n", reg);
100 static void be_rxq_notify(struct be_ctrl_info *ctrl, u16 qid, u16 posted)
103 val |= qid & DB_RQ_RING_ID_MASK;
104 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
105 iowrite32(val, ctrl->db + DB_RQ_OFFSET);
108 static void be_txq_notify(struct be_ctrl_info *ctrl, u16 qid, u16 posted)
111 val |= qid & DB_TXULP_RING_ID_MASK;
112 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
113 iowrite32(val, ctrl->db + DB_TXULP1_OFFSET);
116 static void be_eq_notify(struct be_ctrl_info *ctrl, u16 qid,
117 bool arm, bool clear_int, u16 num_popped)
120 val |= qid & DB_EQ_RING_ID_MASK;
122 val |= 1 << DB_EQ_REARM_SHIFT;
124 val |= 1 << DB_EQ_CLR_SHIFT;
125 val |= 1 << DB_EQ_EVNT_SHIFT;
126 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
127 iowrite32(val, ctrl->db + DB_EQ_OFFSET);
130 static void be_cq_notify(struct be_ctrl_info *ctrl, u16 qid,
131 bool arm, u16 num_popped)
134 val |= qid & DB_CQ_RING_ID_MASK;
136 val |= 1 << DB_CQ_REARM_SHIFT;
137 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
138 iowrite32(val, ctrl->db + DB_CQ_OFFSET);
142 static int be_mac_addr_set(struct net_device *netdev, void *p)
144 struct be_adapter *adapter = netdev_priv(netdev);
145 struct sockaddr *addr = p;
148 if (netif_running(netdev)) {
149 status = be_cmd_pmac_del(&adapter->ctrl, adapter->if_handle,
154 status = be_cmd_pmac_add(&adapter->ctrl, (u8 *)addr->sa_data,
155 adapter->if_handle, &adapter->pmac_id);
159 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
164 static void netdev_stats_update(struct be_adapter *adapter)
166 struct be_hw_stats *hw_stats = hw_stats_from_cmd(adapter->stats.cmd.va);
167 struct be_rxf_stats *rxf_stats = &hw_stats->rxf;
168 struct be_port_rxf_stats *port_stats =
169 &rxf_stats->port[adapter->port_num];
170 struct net_device_stats *dev_stats = &adapter->stats.net_stats;
172 dev_stats->rx_packets = port_stats->rx_total_frames;
173 dev_stats->tx_packets = port_stats->tx_unicastframes +
174 port_stats->tx_multicastframes + port_stats->tx_broadcastframes;
175 dev_stats->rx_bytes = (u64) port_stats->rx_bytes_msd << 32 |
176 (u64) port_stats->rx_bytes_lsd;
177 dev_stats->tx_bytes = (u64) port_stats->tx_bytes_msd << 32 |
178 (u64) port_stats->tx_bytes_lsd;
180 /* bad pkts received */
181 dev_stats->rx_errors = port_stats->rx_crc_errors +
182 port_stats->rx_alignment_symbol_errors +
183 port_stats->rx_in_range_errors +
184 port_stats->rx_out_range_errors + port_stats->rx_frame_too_long;
186 /* packet transmit problems */
187 dev_stats->tx_errors = 0;
189 /* no space in linux buffers */
190 dev_stats->rx_dropped = 0;
192 /* no space available in linux */
193 dev_stats->tx_dropped = 0;
195 dev_stats->multicast = port_stats->tx_multicastframes;
196 dev_stats->collisions = 0;
198 /* detailed rx errors */
199 dev_stats->rx_length_errors = port_stats->rx_in_range_errors +
200 port_stats->rx_out_range_errors + port_stats->rx_frame_too_long;
201 /* receive ring buffer overflow */
202 dev_stats->rx_over_errors = 0;
203 dev_stats->rx_crc_errors = port_stats->rx_crc_errors;
205 /* frame alignment errors */
206 dev_stats->rx_frame_errors = port_stats->rx_alignment_symbol_errors;
207 /* receiver fifo overrun */
208 /* drops_no_pbuf is no per i/f, it's per BE card */
209 dev_stats->rx_fifo_errors = port_stats->rx_fifo_overflow +
210 port_stats->rx_input_fifo_overflow +
211 rxf_stats->rx_drops_no_pbuf;
212 /* receiver missed packetd */
213 dev_stats->rx_missed_errors = 0;
214 /* detailed tx_errors */
215 dev_stats->tx_aborted_errors = 0;
216 dev_stats->tx_carrier_errors = 0;
217 dev_stats->tx_fifo_errors = 0;
218 dev_stats->tx_heartbeat_errors = 0;
219 dev_stats->tx_window_errors = 0;
222 static void be_link_status_update(struct be_adapter *adapter)
224 struct be_link_info *prev = &adapter->link;
225 struct be_link_info now = { 0 };
226 struct net_device *netdev = adapter->netdev;
228 be_cmd_link_status_query(&adapter->ctrl, &now);
230 /* If link came up or went down */
231 if (now.speed != prev->speed && (now.speed == PHY_LINK_SPEED_ZERO ||
232 prev->speed == PHY_LINK_SPEED_ZERO)) {
233 if (now.speed == PHY_LINK_SPEED_ZERO) {
234 netif_stop_queue(netdev);
235 netif_carrier_off(netdev);
236 printk(KERN_INFO "%s: Link down\n", netdev->name);
238 netif_start_queue(netdev);
239 netif_carrier_on(netdev);
240 printk(KERN_INFO "%s: Link up\n", netdev->name);
246 /* Update the EQ delay n BE based on the RX frags consumed / sec */
247 static void be_rx_eqd_update(struct be_adapter *adapter)
249 struct be_ctrl_info *ctrl = &adapter->ctrl;
250 struct be_eq_obj *rx_eq = &adapter->rx_eq;
251 struct be_drvr_stats *stats = &adapter->stats.drvr_stats;
255 if (!rx_eq->enable_aic)
259 if (time_before(now, stats->rx_fps_jiffies)) {
260 stats->rx_fps_jiffies = now;
264 /* Update once a second */
265 if ((now - stats->rx_fps_jiffies) < HZ)
268 stats->be_rx_fps = (stats->be_rx_frags - stats->be_prev_rx_frags) /
269 ((now - stats->rx_fps_jiffies) / HZ);
271 stats->rx_fps_jiffies = now;
272 stats->be_prev_rx_frags = stats->be_rx_frags;
273 eqd = stats->be_rx_fps / 110000;
275 if (eqd > rx_eq->max_eqd)
276 eqd = rx_eq->max_eqd;
277 if (eqd < rx_eq->min_eqd)
278 eqd = rx_eq->min_eqd;
281 if (eqd != rx_eq->cur_eqd)
282 be_cmd_modify_eqd(ctrl, rx_eq->q.id, eqd);
284 rx_eq->cur_eqd = eqd;
287 static struct net_device_stats *be_get_stats(struct net_device *dev)
289 struct be_adapter *adapter = netdev_priv(dev);
291 return &adapter->stats.net_stats;
294 static u32 be_calc_rate(u64 bytes, unsigned long ticks)
298 do_div(rate, ticks / HZ);
299 rate <<= 3; /* bytes/sec -> bits/sec */
300 do_div(rate, 1000000ul); /* MB/Sec */
305 static void be_tx_rate_update(struct be_adapter *adapter)
307 struct be_drvr_stats *stats = drvr_stats(adapter);
310 /* Wrapped around? */
311 if (time_before(now, stats->be_tx_jiffies)) {
312 stats->be_tx_jiffies = now;
316 /* Update tx rate once in two seconds */
317 if ((now - stats->be_tx_jiffies) > 2 * HZ) {
318 stats->be_tx_rate = be_calc_rate(stats->be_tx_bytes
319 - stats->be_tx_bytes_prev,
320 now - stats->be_tx_jiffies);
321 stats->be_tx_jiffies = now;
322 stats->be_tx_bytes_prev = stats->be_tx_bytes;
326 static void be_tx_stats_update(struct be_adapter *adapter,
327 u32 wrb_cnt, u32 copied, bool stopped)
329 struct be_drvr_stats *stats = drvr_stats(adapter);
331 stats->be_tx_wrbs += wrb_cnt;
332 stats->be_tx_bytes += copied;
334 stats->be_tx_stops++;
337 /* Determine number of WRB entries needed to xmit data in an skb */
338 static u32 wrb_cnt_for_skb(struct sk_buff *skb, bool *dummy)
342 if (skb->len > skb->data_len)
344 cnt += skb_shinfo(skb)->nr_frags;
345 skb = skb_shinfo(skb)->frag_list;
347 /* to account for hdr wrb */
350 /* add a dummy to make it an even num */
355 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
359 static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
361 wrb->frag_pa_hi = upper_32_bits(addr);
362 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
363 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
366 static void wrb_fill_hdr(struct be_eth_hdr_wrb *hdr, struct sk_buff *skb,
367 bool vlan, u32 wrb_cnt, u32 len)
369 memset(hdr, 0, sizeof(*hdr));
371 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
373 if (skb_shinfo(skb)->gso_segs > 1 && skb_shinfo(skb)->gso_size) {
374 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
375 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
376 hdr, skb_shinfo(skb)->gso_size);
377 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
379 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
380 else if (is_udp_pkt(skb))
381 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
384 if (vlan && vlan_tx_tag_present(skb)) {
385 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
386 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag,
387 hdr, vlan_tx_tag_get(skb));
390 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
391 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
392 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
393 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
397 static int make_tx_wrbs(struct be_adapter *adapter,
398 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
402 struct pci_dev *pdev = adapter->pdev;
403 struct sk_buff *first_skb = skb;
404 struct be_queue_info *txq = &adapter->tx_obj.q;
405 struct be_eth_wrb *wrb;
406 struct be_eth_hdr_wrb *hdr;
408 atomic_add(wrb_cnt, &txq->used);
409 hdr = queue_head_node(txq);
413 if (skb->len > skb->data_len) {
414 int len = skb->len - skb->data_len;
415 busaddr = pci_map_single(pdev, skb->data, len,
417 wrb = queue_head_node(txq);
418 wrb_fill(wrb, busaddr, len);
419 be_dws_cpu_to_le(wrb, sizeof(*wrb));
424 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
425 struct skb_frag_struct *frag =
426 &skb_shinfo(skb)->frags[i];
427 busaddr = pci_map_page(pdev, frag->page,
429 frag->size, PCI_DMA_TODEVICE);
430 wrb = queue_head_node(txq);
431 wrb_fill(wrb, busaddr, frag->size);
432 be_dws_cpu_to_le(wrb, sizeof(*wrb));
434 copied += frag->size;
436 skb = skb_shinfo(skb)->frag_list;
440 wrb = queue_head_node(txq);
442 be_dws_cpu_to_le(wrb, sizeof(*wrb));
446 wrb_fill_hdr(hdr, first_skb, adapter->vlan_grp ? true : false,
448 be_dws_cpu_to_le(hdr, sizeof(*hdr));
453 static int be_xmit(struct sk_buff *skb, struct net_device *netdev)
455 struct be_adapter *adapter = netdev_priv(netdev);
456 struct be_tx_obj *tx_obj = &adapter->tx_obj;
457 struct be_queue_info *txq = &tx_obj->q;
458 u32 wrb_cnt = 0, copied = 0;
459 u32 start = txq->head;
460 bool dummy_wrb, stopped = false;
462 wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb);
464 copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb);
466 /* record the sent skb in the sent_skb table */
467 BUG_ON(tx_obj->sent_skb_list[start]);
468 tx_obj->sent_skb_list[start] = skb;
470 /* Ensure that txq has space for the next skb; Else stop the queue
471 * *BEFORE* ringing the tx doorbell, so that we serialze the
472 * tx compls of the current transmit which'll wake up the queue
474 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >= txq->len) {
475 netif_stop_queue(netdev);
479 be_txq_notify(&adapter->ctrl, txq->id, wrb_cnt);
481 netdev->trans_start = jiffies;
483 be_tx_stats_update(adapter, wrb_cnt, copied, stopped);
487 static int be_change_mtu(struct net_device *netdev, int new_mtu)
489 struct be_adapter *adapter = netdev_priv(netdev);
490 if (new_mtu < BE_MIN_MTU ||
491 new_mtu > BE_MAX_JUMBO_FRAME_SIZE) {
492 dev_info(&adapter->pdev->dev,
493 "MTU must be between %d and %d bytes\n",
494 BE_MIN_MTU, BE_MAX_JUMBO_FRAME_SIZE);
497 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
498 netdev->mtu, new_mtu);
499 netdev->mtu = new_mtu;
504 * if there are BE_NUM_VLANS_SUPPORTED or lesser number of VLANS configured,
505 * program them in BE. If more than BE_NUM_VLANS_SUPPORTED are configured,
506 * set the BE in promiscuous VLAN mode.
508 static void be_vid_config(struct net_device *netdev)
510 struct be_adapter *adapter = netdev_priv(netdev);
511 u16 vtag[BE_NUM_VLANS_SUPPORTED];
514 if (adapter->num_vlans <= BE_NUM_VLANS_SUPPORTED) {
515 /* Construct VLAN Table to give to HW */
516 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
517 if (adapter->vlan_tag[i]) {
518 vtag[ntags] = cpu_to_le16(i);
522 be_cmd_vlan_config(&adapter->ctrl, adapter->if_handle,
525 be_cmd_vlan_config(&adapter->ctrl, adapter->if_handle,
530 static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
532 struct be_adapter *adapter = netdev_priv(netdev);
533 struct be_eq_obj *rx_eq = &adapter->rx_eq;
534 struct be_eq_obj *tx_eq = &adapter->tx_eq;
535 struct be_ctrl_info *ctrl = &adapter->ctrl;
537 be_eq_notify(ctrl, rx_eq->q.id, false, false, 0);
538 be_eq_notify(ctrl, tx_eq->q.id, false, false, 0);
539 adapter->vlan_grp = grp;
540 be_eq_notify(ctrl, rx_eq->q.id, true, false, 0);
541 be_eq_notify(ctrl, tx_eq->q.id, true, false, 0);
544 static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
546 struct be_adapter *adapter = netdev_priv(netdev);
548 adapter->num_vlans++;
549 adapter->vlan_tag[vid] = 1;
551 be_vid_config(netdev);
554 static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
556 struct be_adapter *adapter = netdev_priv(netdev);
558 adapter->num_vlans--;
559 adapter->vlan_tag[vid] = 0;
561 vlan_group_set_device(adapter->vlan_grp, vid, NULL);
562 be_vid_config(netdev);
565 static void be_set_multicast_filter(struct net_device *netdev)
567 struct be_adapter *adapter = netdev_priv(netdev);
568 struct dev_mc_list *mc_ptr;
569 u8 mac_addr[32][ETH_ALEN];
572 if (netdev->flags & IFF_ALLMULTI) {
573 /* set BE in Multicast promiscuous */
574 be_cmd_mcast_mac_set(&adapter->ctrl,
575 adapter->if_handle, NULL, 0, true);
579 for (mc_ptr = netdev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
580 memcpy(&mac_addr[i][0], mc_ptr->dmi_addr, ETH_ALEN);
582 be_cmd_mcast_mac_set(&adapter->ctrl,
583 adapter->if_handle, &mac_addr[0][0], i, false);
590 /* reset the promiscuous mode also. */
591 be_cmd_mcast_mac_set(&adapter->ctrl,
592 adapter->if_handle, &mac_addr[0][0], i, false);
596 static void be_set_multicast_list(struct net_device *netdev)
598 struct be_adapter *adapter = netdev_priv(netdev);
600 if (netdev->flags & IFF_PROMISC) {
601 be_cmd_promiscuous_config(&adapter->ctrl, adapter->port_num, 1);
603 be_cmd_promiscuous_config(&adapter->ctrl, adapter->port_num, 0);
604 be_set_multicast_filter(netdev);
608 static void be_rx_rate_update(struct be_adapter *adapter)
610 struct be_drvr_stats *stats = drvr_stats(adapter);
614 if (time_before(now, stats->be_rx_jiffies)) {
615 stats->be_rx_jiffies = now;
619 /* Update the rate once in two seconds */
620 if ((now - stats->be_rx_jiffies) < 2 * HZ)
623 stats->be_rx_rate = be_calc_rate(stats->be_rx_bytes
624 - stats->be_rx_bytes_prev,
625 now - stats->be_rx_jiffies);
626 stats->be_rx_jiffies = now;
627 stats->be_rx_bytes_prev = stats->be_rx_bytes;
630 static void be_rx_stats_update(struct be_adapter *adapter,
631 u32 pktsize, u16 numfrags)
633 struct be_drvr_stats *stats = drvr_stats(adapter);
635 stats->be_rx_compl++;
636 stats->be_rx_frags += numfrags;
637 stats->be_rx_bytes += pktsize;
640 static inline bool do_pkt_csum(struct be_eth_rx_compl *rxcp, bool cso)
642 u8 l4_cksm, ip_version, ipcksm, tcpf = 0, udpf = 0, ipv6_chk;
644 l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp);
645 ipcksm = AMAP_GET_BITS(struct amap_eth_rx_compl, ipcksm, rxcp);
646 ip_version = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
648 tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
649 udpf = AMAP_GET_BITS(struct amap_eth_rx_compl, udpf, rxcp);
651 ipv6_chk = (ip_version && (tcpf || udpf));
653 return ((l4_cksm && ipv6_chk && ipcksm) && cso) ? false : true;
656 static struct be_rx_page_info *
657 get_rx_page_info(struct be_adapter *adapter, u16 frag_idx)
659 struct be_rx_page_info *rx_page_info;
660 struct be_queue_info *rxq = &adapter->rx_obj.q;
662 rx_page_info = &adapter->rx_obj.page_info_tbl[frag_idx];
663 BUG_ON(!rx_page_info->page);
665 if (rx_page_info->last_page_user)
666 pci_unmap_page(adapter->pdev, pci_unmap_addr(rx_page_info, bus),
667 adapter->big_page_size, PCI_DMA_FROMDEVICE);
669 atomic_dec(&rxq->used);
673 /* Throwaway the data in the Rx completion */
674 static void be_rx_compl_discard(struct be_adapter *adapter,
675 struct be_eth_rx_compl *rxcp)
677 struct be_queue_info *rxq = &adapter->rx_obj.q;
678 struct be_rx_page_info *page_info;
679 u16 rxq_idx, i, num_rcvd;
681 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
682 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
684 for (i = 0; i < num_rcvd; i++) {
685 page_info = get_rx_page_info(adapter, rxq_idx);
686 put_page(page_info->page);
687 memset(page_info, 0, sizeof(*page_info));
688 index_inc(&rxq_idx, rxq->len);
693 * skb_fill_rx_data forms a complete skb for an ether frame
696 static void skb_fill_rx_data(struct be_adapter *adapter,
697 struct sk_buff *skb, struct be_eth_rx_compl *rxcp)
699 struct be_queue_info *rxq = &adapter->rx_obj.q;
700 struct be_rx_page_info *page_info;
701 u16 rxq_idx, i, num_rcvd;
702 u32 pktsize, hdr_len, curr_frag_len;
705 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
706 pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
707 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
709 page_info = get_rx_page_info(adapter, rxq_idx);
711 start = page_address(page_info->page) + page_info->page_offset;
714 /* Copy data in the first descriptor of this completion */
715 curr_frag_len = min(pktsize, rx_frag_size);
717 /* Copy the header portion into skb_data */
718 hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
719 memcpy(skb->data, start, hdr_len);
720 skb->len = curr_frag_len;
721 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
722 /* Complete packet has now been moved to data */
723 put_page(page_info->page);
725 skb->tail += curr_frag_len;
727 skb_shinfo(skb)->nr_frags = 1;
728 skb_shinfo(skb)->frags[0].page = page_info->page;
729 skb_shinfo(skb)->frags[0].page_offset =
730 page_info->page_offset + hdr_len;
731 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
732 skb->data_len = curr_frag_len - hdr_len;
733 skb->tail += hdr_len;
735 memset(page_info, 0, sizeof(*page_info));
737 if (pktsize <= rx_frag_size) {
738 BUG_ON(num_rcvd != 1);
742 /* More frags present for this completion */
743 pktsize -= curr_frag_len; /* account for above copied frag */
744 for (i = 1; i < num_rcvd; i++) {
745 index_inc(&rxq_idx, rxq->len);
746 page_info = get_rx_page_info(adapter, rxq_idx);
748 curr_frag_len = min(pktsize, rx_frag_size);
750 skb_shinfo(skb)->frags[i].page = page_info->page;
751 skb_shinfo(skb)->frags[i].page_offset = page_info->page_offset;
752 skb_shinfo(skb)->frags[i].size = curr_frag_len;
753 skb->len += curr_frag_len;
754 skb->data_len += curr_frag_len;
755 skb_shinfo(skb)->nr_frags++;
756 pktsize -= curr_frag_len;
758 memset(page_info, 0, sizeof(*page_info));
761 be_rx_stats_update(adapter, pktsize, num_rcvd);
765 /* Process the RX completion indicated by rxcp when LRO is disabled */
766 static void be_rx_compl_process(struct be_adapter *adapter,
767 struct be_eth_rx_compl *rxcp)
772 vtp = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
774 skb = netdev_alloc_skb(adapter->netdev, BE_HDR_LEN + NET_IP_ALIGN);
777 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
778 be_rx_compl_discard(adapter, rxcp);
782 skb_reserve(skb, NET_IP_ALIGN);
784 skb_fill_rx_data(adapter, skb, rxcp);
786 if (do_pkt_csum(rxcp, adapter->rx_csum))
787 skb->ip_summed = CHECKSUM_NONE;
789 skb->ip_summed = CHECKSUM_UNNECESSARY;
791 skb->truesize = skb->len + sizeof(struct sk_buff);
792 skb->protocol = eth_type_trans(skb, adapter->netdev);
793 skb->dev = adapter->netdev;
796 if (!adapter->vlan_grp || adapter->num_vlans == 0) {
800 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
801 vid = be16_to_cpu(vid);
802 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
804 netif_receive_skb(skb);
807 adapter->netdev->last_rx = jiffies;
812 /* Process the RX completion indicated by rxcp when LRO is enabled */
813 static void be_rx_compl_process_lro(struct be_adapter *adapter,
814 struct be_eth_rx_compl *rxcp)
816 struct be_rx_page_info *page_info;
817 struct skb_frag_struct rx_frags[BE_MAX_FRAGS_PER_FRAME];
818 struct be_queue_info *rxq = &adapter->rx_obj.q;
819 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
820 u16 i, rxq_idx = 0, vid;
822 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
823 pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
824 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
825 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
827 remaining = pkt_size;
828 for (i = 0; i < num_rcvd; i++) {
829 page_info = get_rx_page_info(adapter, rxq_idx);
831 curr_frag_len = min(remaining, rx_frag_size);
833 rx_frags[i].page = page_info->page;
834 rx_frags[i].page_offset = page_info->page_offset;
835 rx_frags[i].size = curr_frag_len;
836 remaining -= curr_frag_len;
838 index_inc(&rxq_idx, rxq->len);
840 memset(page_info, 0, sizeof(*page_info));
843 if (likely(!vlanf)) {
844 lro_receive_frags(&adapter->rx_obj.lro_mgr, rx_frags, pkt_size,
847 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
848 vid = be16_to_cpu(vid);
850 if (!adapter->vlan_grp || adapter->num_vlans == 0)
853 lro_vlan_hwaccel_receive_frags(&adapter->rx_obj.lro_mgr,
854 rx_frags, pkt_size, pkt_size, adapter->vlan_grp,
858 be_rx_stats_update(adapter, pkt_size, num_rcvd);
862 static struct be_eth_rx_compl *be_rx_compl_get(struct be_adapter *adapter)
864 struct be_eth_rx_compl *rxcp = queue_tail_node(&adapter->rx_obj.cq);
866 if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
869 be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
871 rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
873 queue_tail_inc(&adapter->rx_obj.cq);
877 static inline struct page *be_alloc_pages(u32 size)
879 gfp_t alloc_flags = GFP_ATOMIC;
880 u32 order = get_order(size);
882 alloc_flags |= __GFP_COMP;
883 return alloc_pages(alloc_flags, order);
887 * Allocate a page, split it to fragments of size rx_frag_size and post as
888 * receive buffers to BE
890 static void be_post_rx_frags(struct be_adapter *adapter)
892 struct be_rx_page_info *page_info_tbl = adapter->rx_obj.page_info_tbl;
893 struct be_rx_page_info *page_info = NULL;
894 struct be_queue_info *rxq = &adapter->rx_obj.q;
895 struct page *pagep = NULL;
896 struct be_eth_rx_d *rxd;
897 u64 page_dmaaddr = 0, frag_dmaaddr;
898 u32 posted, page_offset = 0;
900 page_info = &page_info_tbl[rxq->head];
901 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
903 pagep = be_alloc_pages(adapter->big_page_size);
904 if (unlikely(!pagep)) {
905 drvr_stats(adapter)->be_ethrx_post_fail++;
908 page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
909 adapter->big_page_size,
911 page_info->page_offset = 0;
914 page_info->page_offset = page_offset + rx_frag_size;
916 page_offset = page_info->page_offset;
917 page_info->page = pagep;
918 pci_unmap_addr_set(page_info, bus, page_dmaaddr);
919 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
921 rxd = queue_head_node(rxq);
922 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
923 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
926 /* Any space left in the current big page for another frag? */
927 if ((page_offset + rx_frag_size + rx_frag_size) >
928 adapter->big_page_size) {
930 page_info->last_page_user = true;
932 page_info = &page_info_tbl[rxq->head];
935 page_info->last_page_user = true;
938 atomic_add(posted, &rxq->used);
939 be_rxq_notify(&adapter->ctrl, rxq->id, posted);
940 } else if (atomic_read(&rxq->used) == 0) {
941 /* Let be_worker replenish when memory is available */
942 adapter->rx_post_starved = true;
948 static struct be_eth_tx_compl *
949 be_tx_compl_get(struct be_adapter *adapter)
951 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
952 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
954 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
957 be_dws_le_to_cpu(txcp, sizeof(*txcp));
959 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
961 queue_tail_inc(tx_cq);
965 static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
967 struct be_queue_info *txq = &adapter->tx_obj.q;
968 struct be_eth_wrb *wrb;
969 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
970 struct sk_buff *sent_skb;
972 u16 cur_index, num_wrbs = 0;
974 cur_index = txq->tail;
975 sent_skb = sent_skbs[cur_index];
977 sent_skbs[cur_index] = NULL;
980 cur_index = txq->tail;
981 wrb = queue_tail_node(txq);
982 be_dws_le_to_cpu(wrb, sizeof(*wrb));
983 busaddr = ((u64)wrb->frag_pa_hi << 32) | (u64)wrb->frag_pa_lo;
985 pci_unmap_single(adapter->pdev, busaddr,
986 wrb->frag_len, PCI_DMA_TODEVICE);
990 } while (cur_index != last_index);
992 atomic_sub(num_wrbs, &txq->used);
997 static void be_rx_q_clean(struct be_adapter *adapter)
999 struct be_rx_page_info *page_info;
1000 struct be_queue_info *rxq = &adapter->rx_obj.q;
1001 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1002 struct be_eth_rx_compl *rxcp;
1005 /* First cleanup pending rx completions */
1006 while ((rxcp = be_rx_compl_get(adapter)) != NULL) {
1007 be_rx_compl_discard(adapter, rxcp);
1008 be_cq_notify(&adapter->ctrl, rx_cq->id, true, 1);
1011 /* Then free posted rx buffer that were not used */
1012 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
1013 for (; tail != rxq->head; index_inc(&tail, rxq->len)) {
1014 page_info = get_rx_page_info(adapter, tail);
1015 put_page(page_info->page);
1016 memset(page_info, 0, sizeof(*page_info));
1018 BUG_ON(atomic_read(&rxq->used));
1021 static void be_tx_q_clean(struct be_adapter *adapter)
1023 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1024 struct sk_buff *sent_skb;
1025 struct be_queue_info *txq = &adapter->tx_obj.q;
1029 while (atomic_read(&txq->used)) {
1030 sent_skb = sent_skbs[txq->tail];
1031 last_index = txq->tail;
1032 index_adv(&last_index,
1033 wrb_cnt_for_skb(sent_skb, &dummy_wrb) - 1, txq->len);
1034 be_tx_compl_process(adapter, last_index);
1038 static void be_tx_queues_destroy(struct be_adapter *adapter)
1040 struct be_queue_info *q;
1042 q = &adapter->tx_obj.q;
1044 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_TXQ);
1045 be_queue_free(adapter, q);
1047 q = &adapter->tx_obj.cq;
1049 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_CQ);
1050 be_queue_free(adapter, q);
1052 /* No more tx completions can be rcvd now; clean up if there are
1053 * any pending completions or pending tx requests */
1054 be_tx_q_clean(adapter);
1056 q = &adapter->tx_eq.q;
1058 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_EQ);
1059 be_queue_free(adapter, q);
1062 static int be_tx_queues_create(struct be_adapter *adapter)
1064 struct be_queue_info *eq, *q, *cq;
1066 adapter->tx_eq.max_eqd = 0;
1067 adapter->tx_eq.min_eqd = 0;
1068 adapter->tx_eq.cur_eqd = 96;
1069 adapter->tx_eq.enable_aic = false;
1070 /* Alloc Tx Event queue */
1071 eq = &adapter->tx_eq.q;
1072 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1075 /* Ask BE to create Tx Event queue */
1076 if (be_cmd_eq_create(&adapter->ctrl, eq, adapter->tx_eq.cur_eqd))
1078 /* Alloc TX eth compl queue */
1079 cq = &adapter->tx_obj.cq;
1080 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1081 sizeof(struct be_eth_tx_compl)))
1084 /* Ask BE to create Tx eth compl queue */
1085 if (be_cmd_cq_create(&adapter->ctrl, cq, eq, false, false, 3))
1088 /* Alloc TX eth queue */
1089 q = &adapter->tx_obj.q;
1090 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1093 /* Ask BE to create Tx eth queue */
1094 if (be_cmd_txq_create(&adapter->ctrl, q, cq))
1099 be_queue_free(adapter, q);
1101 be_cmd_q_destroy(&adapter->ctrl, cq, QTYPE_CQ);
1103 be_queue_free(adapter, cq);
1105 be_cmd_q_destroy(&adapter->ctrl, eq, QTYPE_EQ);
1107 be_queue_free(adapter, eq);
1111 static void be_rx_queues_destroy(struct be_adapter *adapter)
1113 struct be_queue_info *q;
1115 q = &adapter->rx_obj.q;
1117 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_RXQ);
1118 be_rx_q_clean(adapter);
1120 be_queue_free(adapter, q);
1122 q = &adapter->rx_obj.cq;
1124 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_CQ);
1125 be_queue_free(adapter, q);
1127 q = &adapter->rx_eq.q;
1129 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_EQ);
1130 be_queue_free(adapter, q);
1133 static int be_rx_queues_create(struct be_adapter *adapter)
1135 struct be_queue_info *eq, *q, *cq;
1138 adapter->max_rx_coal = BE_MAX_FRAGS_PER_FRAME;
1139 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
1140 adapter->rx_eq.max_eqd = BE_MAX_EQD;
1141 adapter->rx_eq.min_eqd = 0;
1142 adapter->rx_eq.cur_eqd = 0;
1143 adapter->rx_eq.enable_aic = true;
1145 /* Alloc Rx Event queue */
1146 eq = &adapter->rx_eq.q;
1147 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1148 sizeof(struct be_eq_entry));
1152 /* Ask BE to create Rx Event queue */
1153 rc = be_cmd_eq_create(&adapter->ctrl, eq, adapter->rx_eq.cur_eqd);
1157 /* Alloc RX eth compl queue */
1158 cq = &adapter->rx_obj.cq;
1159 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1160 sizeof(struct be_eth_rx_compl));
1164 /* Ask BE to create Rx eth compl queue */
1165 rc = be_cmd_cq_create(&adapter->ctrl, cq, eq, false, false, 3);
1169 /* Alloc RX eth queue */
1170 q = &adapter->rx_obj.q;
1171 rc = be_queue_alloc(adapter, q, RX_Q_LEN, sizeof(struct be_eth_rx_d));
1175 /* Ask BE to create Rx eth queue */
1176 rc = be_cmd_rxq_create(&adapter->ctrl, q, cq->id, rx_frag_size,
1177 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle, false);
1183 be_queue_free(adapter, q);
1185 be_cmd_q_destroy(&adapter->ctrl, cq, QTYPE_CQ);
1187 be_queue_free(adapter, cq);
1189 be_cmd_q_destroy(&adapter->ctrl, eq, QTYPE_EQ);
1191 be_queue_free(adapter, eq);
1194 static bool event_get(struct be_eq_obj *eq_obj, u16 *rid)
1196 struct be_eq_entry *entry = queue_tail_node(&eq_obj->q);
1197 u32 evt = entry->evt;
1202 evt = le32_to_cpu(evt);
1203 *rid = (evt >> EQ_ENTRY_RES_ID_SHIFT) & EQ_ENTRY_RES_ID_MASK;
1205 queue_tail_inc(&eq_obj->q);
1209 static int event_handle(struct be_ctrl_info *ctrl,
1210 struct be_eq_obj *eq_obj)
1212 u16 rid = 0, num = 0;
1214 while (event_get(eq_obj, &rid))
1217 /* We can see an interrupt and no event */
1218 be_eq_notify(ctrl, eq_obj->q.id, true, true, num);
1220 napi_schedule(&eq_obj->napi);
1225 static irqreturn_t be_intx(int irq, void *dev)
1227 struct be_adapter *adapter = dev;
1228 struct be_ctrl_info *ctrl = &adapter->ctrl;
1231 tx = event_handle(ctrl, &adapter->tx_eq);
1232 rx = event_handle(ctrl, &adapter->rx_eq);
1240 static irqreturn_t be_msix_rx(int irq, void *dev)
1242 struct be_adapter *adapter = dev;
1244 event_handle(&adapter->ctrl, &adapter->rx_eq);
1249 static irqreturn_t be_msix_tx(int irq, void *dev)
1251 struct be_adapter *adapter = dev;
1253 event_handle(&adapter->ctrl, &adapter->tx_eq);
1258 static inline bool do_lro(struct be_adapter *adapter,
1259 struct be_eth_rx_compl *rxcp)
1261 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1262 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1265 drvr_stats(adapter)->be_rxcp_err++;
1267 return (!tcp_frame || err || (adapter->max_rx_coal <= 1)) ?
1271 int be_poll_rx(struct napi_struct *napi, int budget)
1273 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
1274 struct be_adapter *adapter =
1275 container_of(rx_eq, struct be_adapter, rx_eq);
1276 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1277 struct be_eth_rx_compl *rxcp;
1280 for (work_done = 0; work_done < budget; work_done++) {
1281 rxcp = be_rx_compl_get(adapter);
1285 if (do_lro(adapter, rxcp))
1286 be_rx_compl_process_lro(adapter, rxcp);
1288 be_rx_compl_process(adapter, rxcp);
1291 lro_flush_all(&adapter->rx_obj.lro_mgr);
1293 /* Refill the queue */
1294 if (atomic_read(&adapter->rx_obj.q.used) < RX_FRAGS_REFILL_WM)
1295 be_post_rx_frags(adapter);
1298 if (work_done < budget) {
1299 napi_complete(napi);
1300 be_cq_notify(&adapter->ctrl, rx_cq->id, true, work_done);
1302 /* More to be consumed; continue with interrupts disabled */
1303 be_cq_notify(&adapter->ctrl, rx_cq->id, false, work_done);
1308 /* For TX we don't honour budget; consume everything */
1309 int be_poll_tx(struct napi_struct *napi, int budget)
1311 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1312 struct be_adapter *adapter =
1313 container_of(tx_eq, struct be_adapter, tx_eq);
1314 struct be_tx_obj *tx_obj = &adapter->tx_obj;
1315 struct be_queue_info *tx_cq = &tx_obj->cq;
1316 struct be_queue_info *txq = &tx_obj->q;
1317 struct be_eth_tx_compl *txcp;
1321 while ((txcp = be_tx_compl_get(adapter))) {
1322 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1324 be_tx_compl_process(adapter, end_idx);
1328 /* As Tx wrbs have been freed up, wake up netdev queue if
1329 * it was stopped due to lack of tx wrbs.
1331 if (netif_queue_stopped(adapter->netdev) &&
1332 atomic_read(&txq->used) < txq->len / 2) {
1333 netif_wake_queue(adapter->netdev);
1336 napi_complete(napi);
1338 be_cq_notify(&adapter->ctrl, tx_cq->id, true, num_cmpl);
1340 drvr_stats(adapter)->be_tx_events++;
1341 drvr_stats(adapter)->be_tx_compl += num_cmpl;
1346 static void be_worker(struct work_struct *work)
1348 struct be_adapter *adapter =
1349 container_of(work, struct be_adapter, work.work);
1353 be_link_status_update(adapter);
1356 status = be_cmd_get_stats(&adapter->ctrl, &adapter->stats.cmd);
1358 netdev_stats_update(adapter);
1361 be_rx_eqd_update(adapter);
1363 be_tx_rate_update(adapter);
1364 be_rx_rate_update(adapter);
1366 if (adapter->rx_post_starved) {
1367 adapter->rx_post_starved = false;
1368 be_post_rx_frags(adapter);
1371 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1374 static void be_msix_enable(struct be_adapter *adapter)
1378 for (i = 0; i < BE_NUM_MSIX_VECTORS; i++)
1379 adapter->msix_entries[i].entry = i;
1381 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1382 BE_NUM_MSIX_VECTORS);
1384 adapter->msix_enabled = true;
1388 static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1390 return adapter->msix_entries[eq_id -
1391 8 * adapter->ctrl.pci_func].vector;
1394 static int be_msix_register(struct be_adapter *adapter)
1396 struct net_device *netdev = adapter->netdev;
1397 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1398 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1401 sprintf(tx_eq->desc, "%s-tx", netdev->name);
1402 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1403 status = request_irq(vec, be_msix_tx, 0, tx_eq->desc, adapter);
1407 sprintf(rx_eq->desc, "%s-rx", netdev->name);
1408 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1409 status = request_irq(vec, be_msix_rx, 0, rx_eq->desc, adapter);
1410 if (status) { /* Free TX IRQ */
1411 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1412 free_irq(vec, adapter);
1417 dev_warn(&adapter->pdev->dev,
1418 "MSIX Request IRQ failed - err %d\n", status);
1419 pci_disable_msix(adapter->pdev);
1420 adapter->msix_enabled = false;
1424 static int be_irq_register(struct be_adapter *adapter)
1426 struct net_device *netdev = adapter->netdev;
1429 if (adapter->msix_enabled) {
1430 status = be_msix_register(adapter);
1436 netdev->irq = adapter->pdev->irq;
1437 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1440 dev_err(&adapter->pdev->dev,
1441 "INTx request IRQ failed - err %d\n", status);
1445 adapter->isr_registered = true;
1449 static void be_irq_unregister(struct be_adapter *adapter)
1451 struct net_device *netdev = adapter->netdev;
1454 if (!adapter->isr_registered)
1458 if (!adapter->msix_enabled) {
1459 free_irq(netdev->irq, adapter);
1464 vec = be_msix_vec_get(adapter, adapter->tx_eq.q.id);
1465 free_irq(vec, adapter);
1466 vec = be_msix_vec_get(adapter, adapter->rx_eq.q.id);
1467 free_irq(vec, adapter);
1469 adapter->isr_registered = false;
1473 static int be_open(struct net_device *netdev)
1475 struct be_adapter *adapter = netdev_priv(netdev);
1476 struct be_ctrl_info *ctrl = &adapter->ctrl;
1477 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1478 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1482 if_flags = BE_IF_FLAGS_BROADCAST | BE_IF_FLAGS_PROMISCUOUS |
1483 BE_IF_FLAGS_MCAST_PROMISCUOUS | BE_IF_FLAGS_UNTAGGED |
1484 BE_IF_FLAGS_PASS_L3L4_ERRORS;
1485 status = be_cmd_if_create(ctrl, if_flags, netdev->dev_addr,
1486 false/* pmac_invalid */, &adapter->if_handle,
1491 be_vid_config(netdev);
1493 status = be_cmd_set_flow_control(ctrl, true, true);
1497 status = be_tx_queues_create(adapter);
1501 status = be_rx_queues_create(adapter);
1505 /* First time posting */
1506 be_post_rx_frags(adapter);
1508 napi_enable(&rx_eq->napi);
1509 napi_enable(&tx_eq->napi);
1511 be_irq_register(adapter);
1513 be_intr_set(ctrl, true);
1515 /* The evt queues are created in the unarmed state; arm them */
1516 be_eq_notify(ctrl, rx_eq->q.id, true, false, 0);
1517 be_eq_notify(ctrl, tx_eq->q.id, true, false, 0);
1519 /* The compl queues are created in the unarmed state; arm them */
1520 be_cq_notify(ctrl, adapter->rx_obj.cq.id, true, 0);
1521 be_cq_notify(ctrl, adapter->tx_obj.cq.id, true, 0);
1523 be_link_status_update(adapter);
1525 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
1529 be_tx_queues_destroy(adapter);
1531 be_cmd_if_destroy(ctrl, adapter->if_handle);
1536 static int be_close(struct net_device *netdev)
1538 struct be_adapter *adapter = netdev_priv(netdev);
1539 struct be_ctrl_info *ctrl = &adapter->ctrl;
1540 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1541 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1544 cancel_delayed_work(&adapter->work);
1546 netif_stop_queue(netdev);
1547 netif_carrier_off(netdev);
1548 adapter->link.speed = PHY_LINK_SPEED_ZERO;
1550 be_intr_set(ctrl, false);
1552 if (adapter->msix_enabled) {
1553 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1554 synchronize_irq(vec);
1555 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1556 synchronize_irq(vec);
1558 synchronize_irq(netdev->irq);
1560 be_irq_unregister(adapter);
1562 napi_disable(&rx_eq->napi);
1563 napi_disable(&tx_eq->napi);
1565 be_rx_queues_destroy(adapter);
1566 be_tx_queues_destroy(adapter);
1568 be_cmd_if_destroy(ctrl, adapter->if_handle);
1572 static int be_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr,
1573 void **ip_hdr, void **tcpudp_hdr,
1574 u64 *hdr_flags, void *priv)
1577 struct vlan_ethhdr *veh;
1579 u8 *va = page_address(frag->page) + frag->page_offset;
1580 unsigned long ll_hlen;
1583 eh = (struct ethhdr *)va;
1586 if (eh->h_proto != htons(ETH_P_IP)) {
1587 if (eh->h_proto == htons(ETH_P_8021Q)) {
1588 veh = (struct vlan_ethhdr *)va;
1589 if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
1592 ll_hlen += VLAN_HLEN;
1597 *hdr_flags = LRO_IPV4;
1598 iph = (struct iphdr *)(va + ll_hlen);
1600 if (iph->protocol != IPPROTO_TCP)
1602 *hdr_flags |= LRO_TCP;
1603 *tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2);
1608 static void be_lro_init(struct be_adapter *adapter, struct net_device *netdev)
1610 struct net_lro_mgr *lro_mgr;
1612 lro_mgr = &adapter->rx_obj.lro_mgr;
1613 lro_mgr->dev = netdev;
1614 lro_mgr->features = LRO_F_NAPI;
1615 lro_mgr->ip_summed = CHECKSUM_UNNECESSARY;
1616 lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
1617 lro_mgr->max_desc = BE_MAX_LRO_DESCRIPTORS;
1618 lro_mgr->lro_arr = adapter->rx_obj.lro_desc;
1619 lro_mgr->get_frag_header = be_get_frag_header;
1620 lro_mgr->max_aggr = BE_MAX_FRAGS_PER_FRAME;
1623 static struct net_device_ops be_netdev_ops = {
1624 .ndo_open = be_open,
1625 .ndo_stop = be_close,
1626 .ndo_start_xmit = be_xmit,
1627 .ndo_get_stats = be_get_stats,
1628 .ndo_set_rx_mode = be_set_multicast_list,
1629 .ndo_set_mac_address = be_mac_addr_set,
1630 .ndo_change_mtu = be_change_mtu,
1631 .ndo_validate_addr = eth_validate_addr,
1632 .ndo_vlan_rx_register = be_vlan_register,
1633 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
1634 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
1637 static void be_netdev_init(struct net_device *netdev)
1639 struct be_adapter *adapter = netdev_priv(netdev);
1641 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
1642 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_IP_CSUM |
1645 netdev->flags |= IFF_MULTICAST;
1647 adapter->rx_csum = true;
1649 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
1651 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
1653 be_lro_init(adapter, netdev);
1655 netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx,
1657 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx,
1660 netif_carrier_off(netdev);
1661 netif_stop_queue(netdev);
1664 static void be_unmap_pci_bars(struct be_adapter *adapter)
1666 struct be_ctrl_info *ctrl = &adapter->ctrl;
1672 iounmap(ctrl->pcicfg);
1675 static int be_map_pci_bars(struct be_adapter *adapter)
1679 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
1680 pci_resource_len(adapter->pdev, 2));
1683 adapter->ctrl.csr = addr;
1685 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 4),
1689 adapter->ctrl.db = addr;
1691 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 1),
1692 pci_resource_len(adapter->pdev, 1));
1695 adapter->ctrl.pcicfg = addr;
1699 be_unmap_pci_bars(adapter);
1704 static void be_ctrl_cleanup(struct be_adapter *adapter)
1706 struct be_dma_mem *mem = &adapter->ctrl.mbox_mem_alloced;
1708 be_unmap_pci_bars(adapter);
1711 pci_free_consistent(adapter->pdev, mem->size,
1715 /* Initialize the mbox required to send cmds to BE */
1716 static int be_ctrl_init(struct be_adapter *adapter)
1718 struct be_ctrl_info *ctrl = &adapter->ctrl;
1719 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
1720 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
1724 status = be_map_pci_bars(adapter);
1728 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
1729 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
1730 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
1731 if (!mbox_mem_alloc->va) {
1732 be_unmap_pci_bars(adapter);
1735 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
1736 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
1737 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
1738 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
1739 spin_lock_init(&ctrl->cmd_lock);
1741 val = ioread32(ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
1742 ctrl->pci_func = (val >> MEMBAR_CTRL_INT_CTRL_PFUNC_SHIFT) &
1743 MEMBAR_CTRL_INT_CTRL_PFUNC_MASK;
1747 static void be_stats_cleanup(struct be_adapter *adapter)
1749 struct be_stats_obj *stats = &adapter->stats;
1750 struct be_dma_mem *cmd = &stats->cmd;
1753 pci_free_consistent(adapter->pdev, cmd->size,
1757 static int be_stats_init(struct be_adapter *adapter)
1759 struct be_stats_obj *stats = &adapter->stats;
1760 struct be_dma_mem *cmd = &stats->cmd;
1762 cmd->size = sizeof(struct be_cmd_req_get_stats);
1763 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
1764 if (cmd->va == NULL)
1769 static void __devexit be_remove(struct pci_dev *pdev)
1771 struct be_adapter *adapter = pci_get_drvdata(pdev);
1775 unregister_netdev(adapter->netdev);
1777 be_stats_cleanup(adapter);
1779 be_ctrl_cleanup(adapter);
1781 if (adapter->msix_enabled) {
1782 pci_disable_msix(adapter->pdev);
1783 adapter->msix_enabled = false;
1786 pci_set_drvdata(pdev, NULL);
1787 pci_release_regions(pdev);
1788 pci_disable_device(pdev);
1790 free_netdev(adapter->netdev);
1793 static int be_hw_up(struct be_adapter *adapter)
1795 struct be_ctrl_info *ctrl = &adapter->ctrl;
1798 status = be_cmd_POST(ctrl);
1802 status = be_cmd_get_fw_ver(ctrl, adapter->fw_ver);
1806 status = be_cmd_query_fw_cfg(ctrl, &adapter->port_num);
1810 static int __devinit be_probe(struct pci_dev *pdev,
1811 const struct pci_device_id *pdev_id)
1814 struct be_adapter *adapter;
1815 struct net_device *netdev;
1816 struct be_ctrl_info *ctrl;
1819 status = pci_enable_device(pdev);
1823 status = pci_request_regions(pdev, DRV_NAME);
1826 pci_set_master(pdev);
1828 netdev = alloc_etherdev(sizeof(struct be_adapter));
1829 if (netdev == NULL) {
1833 adapter = netdev_priv(netdev);
1834 adapter->pdev = pdev;
1835 pci_set_drvdata(pdev, adapter);
1836 adapter->netdev = netdev;
1838 be_msix_enable(adapter);
1840 status = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1842 netdev->features |= NETIF_F_HIGHDMA;
1844 status = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1846 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
1851 ctrl = &adapter->ctrl;
1852 status = be_ctrl_init(adapter);
1856 status = be_stats_init(adapter);
1860 status = be_hw_up(adapter);
1864 status = be_cmd_mac_addr_query(ctrl, mac, MAC_ADDRESS_TYPE_NETWORK,
1865 true /* permanent */, 0);
1868 memcpy(netdev->dev_addr, mac, ETH_ALEN);
1870 INIT_DELAYED_WORK(&adapter->work, be_worker);
1871 be_netdev_init(netdev);
1872 SET_NETDEV_DEV(netdev, &adapter->pdev->dev);
1874 status = register_netdev(netdev);
1878 dev_info(&pdev->dev, BE_NAME " port %d\n", adapter->port_num);
1882 be_stats_cleanup(adapter);
1884 be_ctrl_cleanup(adapter);
1886 free_netdev(adapter->netdev);
1888 pci_release_regions(pdev);
1890 pci_disable_device(pdev);
1892 dev_warn(&pdev->dev, BE_NAME " initialization failed\n");
1896 static int be_suspend(struct pci_dev *pdev, pm_message_t state)
1898 struct be_adapter *adapter = pci_get_drvdata(pdev);
1899 struct net_device *netdev = adapter->netdev;
1901 netif_device_detach(netdev);
1902 if (netif_running(netdev)) {
1908 pci_save_state(pdev);
1909 pci_disable_device(pdev);
1910 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1914 static int be_resume(struct pci_dev *pdev)
1917 struct be_adapter *adapter = pci_get_drvdata(pdev);
1918 struct net_device *netdev = adapter->netdev;
1920 netif_device_detach(netdev);
1922 status = pci_enable_device(pdev);
1926 pci_set_power_state(pdev, 0);
1927 pci_restore_state(pdev);
1929 if (netif_running(netdev)) {
1934 netif_device_attach(netdev);
1938 static struct pci_driver be_driver = {
1940 .id_table = be_dev_ids,
1942 .remove = be_remove,
1943 .suspend = be_suspend,
1947 static int __init be_init_module(void)
1949 if (rx_frag_size != 8192 && rx_frag_size != 4096
1950 && rx_frag_size != 2048) {
1951 printk(KERN_WARNING DRV_NAME
1952 " : Module param rx_frag_size must be 2048/4096/8192."
1954 rx_frag_size = 2048;
1956 /* Ensure rx_frag_size is aligned to chache line */
1957 if (SKB_DATA_ALIGN(rx_frag_size) != rx_frag_size) {
1958 printk(KERN_WARNING DRV_NAME
1959 " : Bad module param rx_frag_size. Using 2048\n");
1960 rx_frag_size = 2048;
1963 return pci_register_driver(&be_driver);
1965 module_init(be_init_module);
1967 static void __exit be_exit_module(void)
1969 pci_unregister_driver(&be_driver);
1971 module_exit(be_exit_module);