be2net: Remove unnecessary frag list traversing.
[linux-2.6/verdex.git] / drivers / net / benet / be_main.c
blob347d002fa4251e37b704eafee089768e484ea3c8
1 /*
2 * Copyright (C) 2005 - 2009 ServerEngines
3 * All rights reserved.
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
13 * ServerEngines
14 * 209 N. Fair Oaks Ave
15 * Sunnyvale, CA 94085
18 #include "be.h"
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 static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
32 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
33 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
34 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
35 { 0 }
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;
42 if (mem->va)
43 pci_free_consistent(adapter->pdev, mem->size,
44 mem->va, mem->dma);
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));
53 q->len = len;
54 q->entry_size = entry_size;
55 mem->size = len * entry_size;
56 mem->va = pci_alloc_consistent(adapter->pdev, mem->size, &mem->dma);
57 if (!mem->va)
58 return -1;
59 memset(mem->va, 0, mem->size);
60 return 0;
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;
92 } else {
93 printk(KERN_WARNING DRV_NAME
94 ": bad value in membar_int_ctrl reg=0x%x\n", reg);
95 return;
97 iowrite32(reg, addr);
100 static void be_rxq_notify(struct be_ctrl_info *ctrl, u16 qid, u16 posted)
102 u32 val = 0;
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)
110 u32 val = 0;
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)
119 u32 val = 0;
120 val |= qid & DB_EQ_RING_ID_MASK;
121 if (arm)
122 val |= 1 << DB_EQ_REARM_SHIFT;
123 if (clear_int)
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)
133 u32 val = 0;
134 val |= qid & DB_CQ_RING_ID_MASK;
135 if (arm)
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;
146 int status = 0;
148 if (netif_running(netdev)) {
149 status = be_cmd_pmac_del(&adapter->ctrl, adapter->if_handle,
150 adapter->pmac_id);
151 if (status)
152 return status;
154 status = be_cmd_pmac_add(&adapter->ctrl, (u8 *)addr->sa_data,
155 adapter->if_handle, &adapter->pmac_id);
158 if (!status)
159 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
161 return status;
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);
237 } else {
238 netif_start_queue(netdev);
239 netif_carrier_on(netdev);
240 printk(KERN_INFO "%s: Link up\n", netdev->name);
243 *prev = now;
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;
252 ulong now = jiffies;
253 u32 eqd;
255 if (!rx_eq->enable_aic)
256 return;
258 /* Wrapped around */
259 if (time_before(now, stats->rx_fps_jiffies)) {
260 stats->rx_fps_jiffies = now;
261 return;
264 /* Update once a second */
265 if ((now - stats->rx_fps_jiffies) < HZ)
266 return;
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;
274 eqd = eqd << 3;
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;
279 if (eqd < 10)
280 eqd = 0;
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)
296 u64 rate = bytes;
298 do_div(rate, ticks / HZ);
299 rate <<= 3; /* bytes/sec -> bits/sec */
300 do_div(rate, 1000000ul); /* MB/Sec */
302 return rate;
305 static void be_tx_rate_update(struct be_adapter *adapter)
307 struct be_drvr_stats *stats = drvr_stats(adapter);
308 ulong now = jiffies;
310 /* Wrapped around? */
311 if (time_before(now, stats->be_tx_jiffies)) {
312 stats->be_tx_jiffies = now;
313 return;
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);
330 stats->be_tx_reqs++;
331 stats->be_tx_wrbs += wrb_cnt;
332 stats->be_tx_bytes += copied;
333 if (stopped)
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)
340 int cnt = (skb->len > skb->data_len);
342 cnt += skb_shinfo(skb)->nr_frags;
344 /* to account for hdr wrb */
345 cnt++;
346 if (cnt & 1) {
347 /* add a dummy to make it an even num */
348 cnt++;
349 *dummy = true;
350 } else
351 *dummy = false;
352 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
353 return cnt;
356 static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
358 wrb->frag_pa_hi = upper_32_bits(addr);
359 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
360 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
363 static void wrb_fill_hdr(struct be_eth_hdr_wrb *hdr, struct sk_buff *skb,
364 bool vlan, u32 wrb_cnt, u32 len)
366 memset(hdr, 0, sizeof(*hdr));
368 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
370 if (skb_shinfo(skb)->gso_segs > 1 && skb_shinfo(skb)->gso_size) {
371 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
372 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
373 hdr, skb_shinfo(skb)->gso_size);
374 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
375 if (is_tcp_pkt(skb))
376 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
377 else if (is_udp_pkt(skb))
378 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
381 if (vlan && vlan_tx_tag_present(skb)) {
382 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
383 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag,
384 hdr, vlan_tx_tag_get(skb));
387 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
388 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
389 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
390 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
394 static int make_tx_wrbs(struct be_adapter *adapter,
395 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
397 u64 busaddr;
398 u32 i, copied = 0;
399 struct pci_dev *pdev = adapter->pdev;
400 struct sk_buff *first_skb = skb;
401 struct be_queue_info *txq = &adapter->tx_obj.q;
402 struct be_eth_wrb *wrb;
403 struct be_eth_hdr_wrb *hdr;
405 atomic_add(wrb_cnt, &txq->used);
406 hdr = queue_head_node(txq);
407 queue_head_inc(txq);
409 if (skb->len > skb->data_len) {
410 int len = skb->len - skb->data_len;
411 busaddr = pci_map_single(pdev, skb->data, len,
412 PCI_DMA_TODEVICE);
413 wrb = queue_head_node(txq);
414 wrb_fill(wrb, busaddr, len);
415 be_dws_cpu_to_le(wrb, sizeof(*wrb));
416 queue_head_inc(txq);
417 copied += len;
420 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
421 struct skb_frag_struct *frag =
422 &skb_shinfo(skb)->frags[i];
423 busaddr = pci_map_page(pdev, frag->page,
424 frag->page_offset,
425 frag->size, PCI_DMA_TODEVICE);
426 wrb = queue_head_node(txq);
427 wrb_fill(wrb, busaddr, frag->size);
428 be_dws_cpu_to_le(wrb, sizeof(*wrb));
429 queue_head_inc(txq);
430 copied += frag->size;
433 if (dummy_wrb) {
434 wrb = queue_head_node(txq);
435 wrb_fill(wrb, 0, 0);
436 be_dws_cpu_to_le(wrb, sizeof(*wrb));
437 queue_head_inc(txq);
440 wrb_fill_hdr(hdr, first_skb, adapter->vlan_grp ? true : false,
441 wrb_cnt, copied);
442 be_dws_cpu_to_le(hdr, sizeof(*hdr));
444 return copied;
447 static int be_xmit(struct sk_buff *skb, struct net_device *netdev)
449 struct be_adapter *adapter = netdev_priv(netdev);
450 struct be_tx_obj *tx_obj = &adapter->tx_obj;
451 struct be_queue_info *txq = &tx_obj->q;
452 u32 wrb_cnt = 0, copied = 0;
453 u32 start = txq->head;
454 bool dummy_wrb, stopped = false;
456 wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb);
458 copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb);
460 /* record the sent skb in the sent_skb table */
461 BUG_ON(tx_obj->sent_skb_list[start]);
462 tx_obj->sent_skb_list[start] = skb;
464 /* Ensure that txq has space for the next skb; Else stop the queue
465 * *BEFORE* ringing the tx doorbell, so that we serialze the
466 * tx compls of the current transmit which'll wake up the queue
468 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >= txq->len) {
469 netif_stop_queue(netdev);
470 stopped = true;
473 be_txq_notify(&adapter->ctrl, txq->id, wrb_cnt);
475 be_tx_stats_update(adapter, wrb_cnt, copied, stopped);
476 return NETDEV_TX_OK;
479 static int be_change_mtu(struct net_device *netdev, int new_mtu)
481 struct be_adapter *adapter = netdev_priv(netdev);
482 if (new_mtu < BE_MIN_MTU ||
483 new_mtu > BE_MAX_JUMBO_FRAME_SIZE) {
484 dev_info(&adapter->pdev->dev,
485 "MTU must be between %d and %d bytes\n",
486 BE_MIN_MTU, BE_MAX_JUMBO_FRAME_SIZE);
487 return -EINVAL;
489 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
490 netdev->mtu, new_mtu);
491 netdev->mtu = new_mtu;
492 return 0;
496 * if there are BE_NUM_VLANS_SUPPORTED or lesser number of VLANS configured,
497 * program them in BE. If more than BE_NUM_VLANS_SUPPORTED are configured,
498 * set the BE in promiscuous VLAN mode.
500 static void be_vid_config(struct net_device *netdev)
502 struct be_adapter *adapter = netdev_priv(netdev);
503 u16 vtag[BE_NUM_VLANS_SUPPORTED];
504 u16 ntags = 0, i;
506 if (adapter->num_vlans <= BE_NUM_VLANS_SUPPORTED) {
507 /* Construct VLAN Table to give to HW */
508 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
509 if (adapter->vlan_tag[i]) {
510 vtag[ntags] = cpu_to_le16(i);
511 ntags++;
514 be_cmd_vlan_config(&adapter->ctrl, adapter->if_handle,
515 vtag, ntags, 1, 0);
516 } else {
517 be_cmd_vlan_config(&adapter->ctrl, adapter->if_handle,
518 NULL, 0, 1, 1);
522 static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
524 struct be_adapter *adapter = netdev_priv(netdev);
525 struct be_eq_obj *rx_eq = &adapter->rx_eq;
526 struct be_eq_obj *tx_eq = &adapter->tx_eq;
527 struct be_ctrl_info *ctrl = &adapter->ctrl;
529 be_eq_notify(ctrl, rx_eq->q.id, false, false, 0);
530 be_eq_notify(ctrl, tx_eq->q.id, false, false, 0);
531 adapter->vlan_grp = grp;
532 be_eq_notify(ctrl, rx_eq->q.id, true, false, 0);
533 be_eq_notify(ctrl, tx_eq->q.id, true, false, 0);
536 static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
538 struct be_adapter *adapter = netdev_priv(netdev);
540 adapter->num_vlans++;
541 adapter->vlan_tag[vid] = 1;
543 be_vid_config(netdev);
546 static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
548 struct be_adapter *adapter = netdev_priv(netdev);
550 adapter->num_vlans--;
551 adapter->vlan_tag[vid] = 0;
553 vlan_group_set_device(adapter->vlan_grp, vid, NULL);
554 be_vid_config(netdev);
557 static void be_set_multicast_filter(struct net_device *netdev)
559 struct be_adapter *adapter = netdev_priv(netdev);
560 struct dev_mc_list *mc_ptr;
561 u8 mac_addr[32][ETH_ALEN];
562 int i = 0;
564 if (netdev->flags & IFF_ALLMULTI) {
565 /* set BE in Multicast promiscuous */
566 be_cmd_mcast_mac_set(&adapter->ctrl,
567 adapter->if_handle, NULL, 0, true);
568 return;
571 for (mc_ptr = netdev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
572 memcpy(&mac_addr[i][0], mc_ptr->dmi_addr, ETH_ALEN);
573 if (++i >= 32) {
574 be_cmd_mcast_mac_set(&adapter->ctrl,
575 adapter->if_handle, &mac_addr[0][0], i, false);
576 i = 0;
581 if (i) {
582 /* reset the promiscuous mode also. */
583 be_cmd_mcast_mac_set(&adapter->ctrl,
584 adapter->if_handle, &mac_addr[0][0], i, false);
588 static void be_set_multicast_list(struct net_device *netdev)
590 struct be_adapter *adapter = netdev_priv(netdev);
592 if (netdev->flags & IFF_PROMISC) {
593 be_cmd_promiscuous_config(&adapter->ctrl, adapter->port_num, 1);
594 } else {
595 be_cmd_promiscuous_config(&adapter->ctrl, adapter->port_num, 0);
596 be_set_multicast_filter(netdev);
600 static void be_rx_rate_update(struct be_adapter *adapter)
602 struct be_drvr_stats *stats = drvr_stats(adapter);
603 ulong now = jiffies;
605 /* Wrapped around */
606 if (time_before(now, stats->be_rx_jiffies)) {
607 stats->be_rx_jiffies = now;
608 return;
611 /* Update the rate once in two seconds */
612 if ((now - stats->be_rx_jiffies) < 2 * HZ)
613 return;
615 stats->be_rx_rate = be_calc_rate(stats->be_rx_bytes
616 - stats->be_rx_bytes_prev,
617 now - stats->be_rx_jiffies);
618 stats->be_rx_jiffies = now;
619 stats->be_rx_bytes_prev = stats->be_rx_bytes;
622 static void be_rx_stats_update(struct be_adapter *adapter,
623 u32 pktsize, u16 numfrags)
625 struct be_drvr_stats *stats = drvr_stats(adapter);
627 stats->be_rx_compl++;
628 stats->be_rx_frags += numfrags;
629 stats->be_rx_bytes += pktsize;
632 static inline bool do_pkt_csum(struct be_eth_rx_compl *rxcp, bool cso)
634 u8 l4_cksm, ip_version, ipcksm, tcpf = 0, udpf = 0, ipv6_chk;
636 l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp);
637 ipcksm = AMAP_GET_BITS(struct amap_eth_rx_compl, ipcksm, rxcp);
638 ip_version = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
639 if (ip_version) {
640 tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
641 udpf = AMAP_GET_BITS(struct amap_eth_rx_compl, udpf, rxcp);
643 ipv6_chk = (ip_version && (tcpf || udpf));
645 return ((l4_cksm && ipv6_chk && ipcksm) && cso) ? false : true;
648 static struct be_rx_page_info *
649 get_rx_page_info(struct be_adapter *adapter, u16 frag_idx)
651 struct be_rx_page_info *rx_page_info;
652 struct be_queue_info *rxq = &adapter->rx_obj.q;
654 rx_page_info = &adapter->rx_obj.page_info_tbl[frag_idx];
655 BUG_ON(!rx_page_info->page);
657 if (rx_page_info->last_page_user)
658 pci_unmap_page(adapter->pdev, pci_unmap_addr(rx_page_info, bus),
659 adapter->big_page_size, PCI_DMA_FROMDEVICE);
661 atomic_dec(&rxq->used);
662 return rx_page_info;
665 /* Throwaway the data in the Rx completion */
666 static void be_rx_compl_discard(struct be_adapter *adapter,
667 struct be_eth_rx_compl *rxcp)
669 struct be_queue_info *rxq = &adapter->rx_obj.q;
670 struct be_rx_page_info *page_info;
671 u16 rxq_idx, i, num_rcvd;
673 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
674 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
676 for (i = 0; i < num_rcvd; i++) {
677 page_info = get_rx_page_info(adapter, rxq_idx);
678 put_page(page_info->page);
679 memset(page_info, 0, sizeof(*page_info));
680 index_inc(&rxq_idx, rxq->len);
685 * skb_fill_rx_data forms a complete skb for an ether frame
686 * indicated by rxcp.
688 static void skb_fill_rx_data(struct be_adapter *adapter,
689 struct sk_buff *skb, struct be_eth_rx_compl *rxcp)
691 struct be_queue_info *rxq = &adapter->rx_obj.q;
692 struct be_rx_page_info *page_info;
693 u16 rxq_idx, i, num_rcvd;
694 u32 pktsize, hdr_len, curr_frag_len;
695 u8 *start;
697 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
698 pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
699 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
701 page_info = get_rx_page_info(adapter, rxq_idx);
703 start = page_address(page_info->page) + page_info->page_offset;
704 prefetch(start);
706 /* Copy data in the first descriptor of this completion */
707 curr_frag_len = min(pktsize, rx_frag_size);
709 /* Copy the header portion into skb_data */
710 hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
711 memcpy(skb->data, start, hdr_len);
712 skb->len = curr_frag_len;
713 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
714 /* Complete packet has now been moved to data */
715 put_page(page_info->page);
716 skb->data_len = 0;
717 skb->tail += curr_frag_len;
718 } else {
719 skb_shinfo(skb)->nr_frags = 1;
720 skb_shinfo(skb)->frags[0].page = page_info->page;
721 skb_shinfo(skb)->frags[0].page_offset =
722 page_info->page_offset + hdr_len;
723 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
724 skb->data_len = curr_frag_len - hdr_len;
725 skb->tail += hdr_len;
727 memset(page_info, 0, sizeof(*page_info));
729 if (pktsize <= rx_frag_size) {
730 BUG_ON(num_rcvd != 1);
731 return;
734 /* More frags present for this completion */
735 pktsize -= curr_frag_len; /* account for above copied frag */
736 for (i = 1; i < num_rcvd; i++) {
737 index_inc(&rxq_idx, rxq->len);
738 page_info = get_rx_page_info(adapter, rxq_idx);
740 curr_frag_len = min(pktsize, rx_frag_size);
742 skb_shinfo(skb)->frags[i].page = page_info->page;
743 skb_shinfo(skb)->frags[i].page_offset = page_info->page_offset;
744 skb_shinfo(skb)->frags[i].size = curr_frag_len;
745 skb->len += curr_frag_len;
746 skb->data_len += curr_frag_len;
747 skb_shinfo(skb)->nr_frags++;
748 pktsize -= curr_frag_len;
750 memset(page_info, 0, sizeof(*page_info));
753 be_rx_stats_update(adapter, pktsize, num_rcvd);
754 return;
757 /* Process the RX completion indicated by rxcp when LRO is disabled */
758 static void be_rx_compl_process(struct be_adapter *adapter,
759 struct be_eth_rx_compl *rxcp)
761 struct sk_buff *skb;
762 u32 vtp, vid;
764 vtp = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
766 skb = netdev_alloc_skb(adapter->netdev, BE_HDR_LEN + NET_IP_ALIGN);
767 if (!skb) {
768 if (net_ratelimit())
769 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
770 be_rx_compl_discard(adapter, rxcp);
771 return;
774 skb_reserve(skb, NET_IP_ALIGN);
776 skb_fill_rx_data(adapter, skb, rxcp);
778 if (do_pkt_csum(rxcp, adapter->rx_csum))
779 skb->ip_summed = CHECKSUM_NONE;
780 else
781 skb->ip_summed = CHECKSUM_UNNECESSARY;
783 skb->truesize = skb->len + sizeof(struct sk_buff);
784 skb->protocol = eth_type_trans(skb, adapter->netdev);
785 skb->dev = adapter->netdev;
787 if (vtp) {
788 if (!adapter->vlan_grp || adapter->num_vlans == 0) {
789 kfree_skb(skb);
790 return;
792 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
793 vid = be16_to_cpu(vid);
794 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
795 } else {
796 netif_receive_skb(skb);
799 adapter->netdev->last_rx = jiffies;
801 return;
804 /* Process the RX completion indicated by rxcp when LRO is enabled */
805 static void be_rx_compl_process_lro(struct be_adapter *adapter,
806 struct be_eth_rx_compl *rxcp)
808 struct be_rx_page_info *page_info;
809 struct skb_frag_struct rx_frags[BE_MAX_FRAGS_PER_FRAME];
810 struct be_queue_info *rxq = &adapter->rx_obj.q;
811 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
812 u16 i, rxq_idx = 0, vid;
814 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
815 pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
816 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
817 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
819 remaining = pkt_size;
820 for (i = 0; i < num_rcvd; i++) {
821 page_info = get_rx_page_info(adapter, rxq_idx);
823 curr_frag_len = min(remaining, rx_frag_size);
825 rx_frags[i].page = page_info->page;
826 rx_frags[i].page_offset = page_info->page_offset;
827 rx_frags[i].size = curr_frag_len;
828 remaining -= curr_frag_len;
830 index_inc(&rxq_idx, rxq->len);
832 memset(page_info, 0, sizeof(*page_info));
835 if (likely(!vlanf)) {
836 lro_receive_frags(&adapter->rx_obj.lro_mgr, rx_frags, pkt_size,
837 pkt_size, NULL, 0);
838 } else {
839 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
840 vid = be16_to_cpu(vid);
842 if (!adapter->vlan_grp || adapter->num_vlans == 0)
843 return;
845 lro_vlan_hwaccel_receive_frags(&adapter->rx_obj.lro_mgr,
846 rx_frags, pkt_size, pkt_size, adapter->vlan_grp,
847 vid, NULL, 0);
850 be_rx_stats_update(adapter, pkt_size, num_rcvd);
851 return;
854 static struct be_eth_rx_compl *be_rx_compl_get(struct be_adapter *adapter)
856 struct be_eth_rx_compl *rxcp = queue_tail_node(&adapter->rx_obj.cq);
858 if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
859 return NULL;
861 be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
863 rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
865 queue_tail_inc(&adapter->rx_obj.cq);
866 return rxcp;
869 static inline struct page *be_alloc_pages(u32 size)
871 gfp_t alloc_flags = GFP_ATOMIC;
872 u32 order = get_order(size);
873 if (order > 0)
874 alloc_flags |= __GFP_COMP;
875 return alloc_pages(alloc_flags, order);
879 * Allocate a page, split it to fragments of size rx_frag_size and post as
880 * receive buffers to BE
882 static void be_post_rx_frags(struct be_adapter *adapter)
884 struct be_rx_page_info *page_info_tbl = adapter->rx_obj.page_info_tbl;
885 struct be_rx_page_info *page_info = NULL;
886 struct be_queue_info *rxq = &adapter->rx_obj.q;
887 struct page *pagep = NULL;
888 struct be_eth_rx_d *rxd;
889 u64 page_dmaaddr = 0, frag_dmaaddr;
890 u32 posted, page_offset = 0;
892 page_info = &page_info_tbl[rxq->head];
893 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
894 if (!pagep) {
895 pagep = be_alloc_pages(adapter->big_page_size);
896 if (unlikely(!pagep)) {
897 drvr_stats(adapter)->be_ethrx_post_fail++;
898 break;
900 page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
901 adapter->big_page_size,
902 PCI_DMA_FROMDEVICE);
903 page_info->page_offset = 0;
904 } else {
905 get_page(pagep);
906 page_info->page_offset = page_offset + rx_frag_size;
908 page_offset = page_info->page_offset;
909 page_info->page = pagep;
910 pci_unmap_addr_set(page_info, bus, page_dmaaddr);
911 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
913 rxd = queue_head_node(rxq);
914 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
915 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
916 queue_head_inc(rxq);
918 /* Any space left in the current big page for another frag? */
919 if ((page_offset + rx_frag_size + rx_frag_size) >
920 adapter->big_page_size) {
921 pagep = NULL;
922 page_info->last_page_user = true;
924 page_info = &page_info_tbl[rxq->head];
926 if (pagep)
927 page_info->last_page_user = true;
929 if (posted) {
930 atomic_add(posted, &rxq->used);
931 be_rxq_notify(&adapter->ctrl, rxq->id, posted);
932 } else if (atomic_read(&rxq->used) == 0) {
933 /* Let be_worker replenish when memory is available */
934 adapter->rx_post_starved = true;
937 return;
940 static struct be_eth_tx_compl *
941 be_tx_compl_get(struct be_adapter *adapter)
943 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
944 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
946 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
947 return NULL;
949 be_dws_le_to_cpu(txcp, sizeof(*txcp));
951 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
953 queue_tail_inc(tx_cq);
954 return txcp;
957 static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
959 struct be_queue_info *txq = &adapter->tx_obj.q;
960 struct be_eth_wrb *wrb;
961 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
962 struct sk_buff *sent_skb;
963 u64 busaddr;
964 u16 cur_index, num_wrbs = 0;
966 cur_index = txq->tail;
967 sent_skb = sent_skbs[cur_index];
968 BUG_ON(!sent_skb);
969 sent_skbs[cur_index] = NULL;
971 do {
972 cur_index = txq->tail;
973 wrb = queue_tail_node(txq);
974 be_dws_le_to_cpu(wrb, sizeof(*wrb));
975 busaddr = ((u64)wrb->frag_pa_hi << 32) | (u64)wrb->frag_pa_lo;
976 if (busaddr != 0) {
977 pci_unmap_single(adapter->pdev, busaddr,
978 wrb->frag_len, PCI_DMA_TODEVICE);
980 num_wrbs++;
981 queue_tail_inc(txq);
982 } while (cur_index != last_index);
984 atomic_sub(num_wrbs, &txq->used);
986 kfree_skb(sent_skb);
989 static void be_rx_q_clean(struct be_adapter *adapter)
991 struct be_rx_page_info *page_info;
992 struct be_queue_info *rxq = &adapter->rx_obj.q;
993 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
994 struct be_eth_rx_compl *rxcp;
995 u16 tail;
997 /* First cleanup pending rx completions */
998 while ((rxcp = be_rx_compl_get(adapter)) != NULL) {
999 be_rx_compl_discard(adapter, rxcp);
1000 be_cq_notify(&adapter->ctrl, rx_cq->id, true, 1);
1003 /* Then free posted rx buffer that were not used */
1004 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
1005 for (; tail != rxq->head; index_inc(&tail, rxq->len)) {
1006 page_info = get_rx_page_info(adapter, tail);
1007 put_page(page_info->page);
1008 memset(page_info, 0, sizeof(*page_info));
1010 BUG_ON(atomic_read(&rxq->used));
1013 static void be_tx_q_clean(struct be_adapter *adapter)
1015 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1016 struct sk_buff *sent_skb;
1017 struct be_queue_info *txq = &adapter->tx_obj.q;
1018 u16 last_index;
1019 bool dummy_wrb;
1021 while (atomic_read(&txq->used)) {
1022 sent_skb = sent_skbs[txq->tail];
1023 last_index = txq->tail;
1024 index_adv(&last_index,
1025 wrb_cnt_for_skb(sent_skb, &dummy_wrb) - 1, txq->len);
1026 be_tx_compl_process(adapter, last_index);
1030 static void be_tx_queues_destroy(struct be_adapter *adapter)
1032 struct be_queue_info *q;
1034 q = &adapter->tx_obj.q;
1035 if (q->created)
1036 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_TXQ);
1037 be_queue_free(adapter, q);
1039 q = &adapter->tx_obj.cq;
1040 if (q->created)
1041 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_CQ);
1042 be_queue_free(adapter, q);
1044 /* No more tx completions can be rcvd now; clean up if there are
1045 * any pending completions or pending tx requests */
1046 be_tx_q_clean(adapter);
1048 q = &adapter->tx_eq.q;
1049 if (q->created)
1050 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_EQ);
1051 be_queue_free(adapter, q);
1054 static int be_tx_queues_create(struct be_adapter *adapter)
1056 struct be_queue_info *eq, *q, *cq;
1058 adapter->tx_eq.max_eqd = 0;
1059 adapter->tx_eq.min_eqd = 0;
1060 adapter->tx_eq.cur_eqd = 96;
1061 adapter->tx_eq.enable_aic = false;
1062 /* Alloc Tx Event queue */
1063 eq = &adapter->tx_eq.q;
1064 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1065 return -1;
1067 /* Ask BE to create Tx Event queue */
1068 if (be_cmd_eq_create(&adapter->ctrl, eq, adapter->tx_eq.cur_eqd))
1069 goto tx_eq_free;
1070 /* Alloc TX eth compl queue */
1071 cq = &adapter->tx_obj.cq;
1072 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1073 sizeof(struct be_eth_tx_compl)))
1074 goto tx_eq_destroy;
1076 /* Ask BE to create Tx eth compl queue */
1077 if (be_cmd_cq_create(&adapter->ctrl, cq, eq, false, false, 3))
1078 goto tx_cq_free;
1080 /* Alloc TX eth queue */
1081 q = &adapter->tx_obj.q;
1082 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1083 goto tx_cq_destroy;
1085 /* Ask BE to create Tx eth queue */
1086 if (be_cmd_txq_create(&adapter->ctrl, q, cq))
1087 goto tx_q_free;
1088 return 0;
1090 tx_q_free:
1091 be_queue_free(adapter, q);
1092 tx_cq_destroy:
1093 be_cmd_q_destroy(&adapter->ctrl, cq, QTYPE_CQ);
1094 tx_cq_free:
1095 be_queue_free(adapter, cq);
1096 tx_eq_destroy:
1097 be_cmd_q_destroy(&adapter->ctrl, eq, QTYPE_EQ);
1098 tx_eq_free:
1099 be_queue_free(adapter, eq);
1100 return -1;
1103 static void be_rx_queues_destroy(struct be_adapter *adapter)
1105 struct be_queue_info *q;
1107 q = &adapter->rx_obj.q;
1108 if (q->created) {
1109 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_RXQ);
1110 be_rx_q_clean(adapter);
1112 be_queue_free(adapter, q);
1114 q = &adapter->rx_obj.cq;
1115 if (q->created)
1116 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_CQ);
1117 be_queue_free(adapter, q);
1119 q = &adapter->rx_eq.q;
1120 if (q->created)
1121 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_EQ);
1122 be_queue_free(adapter, q);
1125 static int be_rx_queues_create(struct be_adapter *adapter)
1127 struct be_queue_info *eq, *q, *cq;
1128 int rc;
1130 adapter->max_rx_coal = BE_MAX_FRAGS_PER_FRAME;
1131 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
1132 adapter->rx_eq.max_eqd = BE_MAX_EQD;
1133 adapter->rx_eq.min_eqd = 0;
1134 adapter->rx_eq.cur_eqd = 0;
1135 adapter->rx_eq.enable_aic = true;
1137 /* Alloc Rx Event queue */
1138 eq = &adapter->rx_eq.q;
1139 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1140 sizeof(struct be_eq_entry));
1141 if (rc)
1142 return rc;
1144 /* Ask BE to create Rx Event queue */
1145 rc = be_cmd_eq_create(&adapter->ctrl, eq, adapter->rx_eq.cur_eqd);
1146 if (rc)
1147 goto rx_eq_free;
1149 /* Alloc RX eth compl queue */
1150 cq = &adapter->rx_obj.cq;
1151 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1152 sizeof(struct be_eth_rx_compl));
1153 if (rc)
1154 goto rx_eq_destroy;
1156 /* Ask BE to create Rx eth compl queue */
1157 rc = be_cmd_cq_create(&adapter->ctrl, cq, eq, false, false, 3);
1158 if (rc)
1159 goto rx_cq_free;
1161 /* Alloc RX eth queue */
1162 q = &adapter->rx_obj.q;
1163 rc = be_queue_alloc(adapter, q, RX_Q_LEN, sizeof(struct be_eth_rx_d));
1164 if (rc)
1165 goto rx_cq_destroy;
1167 /* Ask BE to create Rx eth queue */
1168 rc = be_cmd_rxq_create(&adapter->ctrl, q, cq->id, rx_frag_size,
1169 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle, false);
1170 if (rc)
1171 goto rx_q_free;
1173 return 0;
1174 rx_q_free:
1175 be_queue_free(adapter, q);
1176 rx_cq_destroy:
1177 be_cmd_q_destroy(&adapter->ctrl, cq, QTYPE_CQ);
1178 rx_cq_free:
1179 be_queue_free(adapter, cq);
1180 rx_eq_destroy:
1181 be_cmd_q_destroy(&adapter->ctrl, eq, QTYPE_EQ);
1182 rx_eq_free:
1183 be_queue_free(adapter, eq);
1184 return rc;
1186 static bool event_get(struct be_eq_obj *eq_obj, u16 *rid)
1188 struct be_eq_entry *entry = queue_tail_node(&eq_obj->q);
1189 u32 evt = entry->evt;
1191 if (!evt)
1192 return false;
1194 evt = le32_to_cpu(evt);
1195 *rid = (evt >> EQ_ENTRY_RES_ID_SHIFT) & EQ_ENTRY_RES_ID_MASK;
1196 entry->evt = 0;
1197 queue_tail_inc(&eq_obj->q);
1198 return true;
1201 static int event_handle(struct be_ctrl_info *ctrl,
1202 struct be_eq_obj *eq_obj)
1204 u16 rid = 0, num = 0;
1206 while (event_get(eq_obj, &rid))
1207 num++;
1209 /* We can see an interrupt and no event */
1210 be_eq_notify(ctrl, eq_obj->q.id, true, true, num);
1211 if (num)
1212 napi_schedule(&eq_obj->napi);
1214 return num;
1217 static irqreturn_t be_intx(int irq, void *dev)
1219 struct be_adapter *adapter = dev;
1220 struct be_ctrl_info *ctrl = &adapter->ctrl;
1221 int rx, tx;
1223 tx = event_handle(ctrl, &adapter->tx_eq);
1224 rx = event_handle(ctrl, &adapter->rx_eq);
1226 if (rx || tx)
1227 return IRQ_HANDLED;
1228 else
1229 return IRQ_NONE;
1232 static irqreturn_t be_msix_rx(int irq, void *dev)
1234 struct be_adapter *adapter = dev;
1236 event_handle(&adapter->ctrl, &adapter->rx_eq);
1238 return IRQ_HANDLED;
1241 static irqreturn_t be_msix_tx(int irq, void *dev)
1243 struct be_adapter *adapter = dev;
1245 event_handle(&adapter->ctrl, &adapter->tx_eq);
1247 return IRQ_HANDLED;
1250 static inline bool do_lro(struct be_adapter *adapter,
1251 struct be_eth_rx_compl *rxcp)
1253 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1254 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1256 if (err)
1257 drvr_stats(adapter)->be_rxcp_err++;
1259 return (!tcp_frame || err || (adapter->max_rx_coal <= 1)) ?
1260 false : true;
1263 int be_poll_rx(struct napi_struct *napi, int budget)
1265 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
1266 struct be_adapter *adapter =
1267 container_of(rx_eq, struct be_adapter, rx_eq);
1268 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1269 struct be_eth_rx_compl *rxcp;
1270 u32 work_done;
1272 for (work_done = 0; work_done < budget; work_done++) {
1273 rxcp = be_rx_compl_get(adapter);
1274 if (!rxcp)
1275 break;
1277 if (do_lro(adapter, rxcp))
1278 be_rx_compl_process_lro(adapter, rxcp);
1279 else
1280 be_rx_compl_process(adapter, rxcp);
1283 lro_flush_all(&adapter->rx_obj.lro_mgr);
1285 /* Refill the queue */
1286 if (atomic_read(&adapter->rx_obj.q.used) < RX_FRAGS_REFILL_WM)
1287 be_post_rx_frags(adapter);
1289 /* All consumed */
1290 if (work_done < budget) {
1291 napi_complete(napi);
1292 be_cq_notify(&adapter->ctrl, rx_cq->id, true, work_done);
1293 } else {
1294 /* More to be consumed; continue with interrupts disabled */
1295 be_cq_notify(&adapter->ctrl, rx_cq->id, false, work_done);
1297 return work_done;
1300 /* For TX we don't honour budget; consume everything */
1301 int be_poll_tx(struct napi_struct *napi, int budget)
1303 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1304 struct be_adapter *adapter =
1305 container_of(tx_eq, struct be_adapter, tx_eq);
1306 struct be_tx_obj *tx_obj = &adapter->tx_obj;
1307 struct be_queue_info *tx_cq = &tx_obj->cq;
1308 struct be_queue_info *txq = &tx_obj->q;
1309 struct be_eth_tx_compl *txcp;
1310 u32 num_cmpl = 0;
1311 u16 end_idx;
1313 while ((txcp = be_tx_compl_get(adapter))) {
1314 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1315 wrb_index, txcp);
1316 be_tx_compl_process(adapter, end_idx);
1317 num_cmpl++;
1320 /* As Tx wrbs have been freed up, wake up netdev queue if
1321 * it was stopped due to lack of tx wrbs.
1323 if (netif_queue_stopped(adapter->netdev) &&
1324 atomic_read(&txq->used) < txq->len / 2) {
1325 netif_wake_queue(adapter->netdev);
1328 napi_complete(napi);
1330 be_cq_notify(&adapter->ctrl, tx_cq->id, true, num_cmpl);
1332 drvr_stats(adapter)->be_tx_events++;
1333 drvr_stats(adapter)->be_tx_compl += num_cmpl;
1335 return 1;
1338 static void be_worker(struct work_struct *work)
1340 struct be_adapter *adapter =
1341 container_of(work, struct be_adapter, work.work);
1342 int status;
1344 /* Check link */
1345 be_link_status_update(adapter);
1347 /* Get Stats */
1348 status = be_cmd_get_stats(&adapter->ctrl, &adapter->stats.cmd);
1349 if (!status)
1350 netdev_stats_update(adapter);
1352 /* Set EQ delay */
1353 be_rx_eqd_update(adapter);
1355 be_tx_rate_update(adapter);
1356 be_rx_rate_update(adapter);
1358 if (adapter->rx_post_starved) {
1359 adapter->rx_post_starved = false;
1360 be_post_rx_frags(adapter);
1363 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1366 static void be_msix_enable(struct be_adapter *adapter)
1368 int i, status;
1370 for (i = 0; i < BE_NUM_MSIX_VECTORS; i++)
1371 adapter->msix_entries[i].entry = i;
1373 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1374 BE_NUM_MSIX_VECTORS);
1375 if (status == 0)
1376 adapter->msix_enabled = true;
1377 return;
1380 static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1382 return adapter->msix_entries[eq_id -
1383 8 * adapter->ctrl.pci_func].vector;
1386 static int be_msix_register(struct be_adapter *adapter)
1388 struct net_device *netdev = adapter->netdev;
1389 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1390 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1391 int status, vec;
1393 sprintf(tx_eq->desc, "%s-tx", netdev->name);
1394 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1395 status = request_irq(vec, be_msix_tx, 0, tx_eq->desc, adapter);
1396 if (status)
1397 goto err;
1399 sprintf(rx_eq->desc, "%s-rx", netdev->name);
1400 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1401 status = request_irq(vec, be_msix_rx, 0, rx_eq->desc, adapter);
1402 if (status) { /* Free TX IRQ */
1403 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1404 free_irq(vec, adapter);
1405 goto err;
1407 return 0;
1408 err:
1409 dev_warn(&adapter->pdev->dev,
1410 "MSIX Request IRQ failed - err %d\n", status);
1411 pci_disable_msix(adapter->pdev);
1412 adapter->msix_enabled = false;
1413 return status;
1416 static int be_irq_register(struct be_adapter *adapter)
1418 struct net_device *netdev = adapter->netdev;
1419 int status;
1421 if (adapter->msix_enabled) {
1422 status = be_msix_register(adapter);
1423 if (status == 0)
1424 goto done;
1427 /* INTx */
1428 netdev->irq = adapter->pdev->irq;
1429 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1430 adapter);
1431 if (status) {
1432 dev_err(&adapter->pdev->dev,
1433 "INTx request IRQ failed - err %d\n", status);
1434 return status;
1436 done:
1437 adapter->isr_registered = true;
1438 return 0;
1441 static void be_irq_unregister(struct be_adapter *adapter)
1443 struct net_device *netdev = adapter->netdev;
1444 int vec;
1446 if (!adapter->isr_registered)
1447 return;
1449 /* INTx */
1450 if (!adapter->msix_enabled) {
1451 free_irq(netdev->irq, adapter);
1452 goto done;
1455 /* MSIx */
1456 vec = be_msix_vec_get(adapter, adapter->tx_eq.q.id);
1457 free_irq(vec, adapter);
1458 vec = be_msix_vec_get(adapter, adapter->rx_eq.q.id);
1459 free_irq(vec, adapter);
1460 done:
1461 adapter->isr_registered = false;
1462 return;
1465 static int be_open(struct net_device *netdev)
1467 struct be_adapter *adapter = netdev_priv(netdev);
1468 struct be_ctrl_info *ctrl = &adapter->ctrl;
1469 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1470 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1471 u32 if_flags;
1472 int status;
1474 if_flags = BE_IF_FLAGS_BROADCAST | BE_IF_FLAGS_PROMISCUOUS |
1475 BE_IF_FLAGS_MCAST_PROMISCUOUS | BE_IF_FLAGS_UNTAGGED |
1476 BE_IF_FLAGS_PASS_L3L4_ERRORS;
1477 status = be_cmd_if_create(ctrl, if_flags, netdev->dev_addr,
1478 false/* pmac_invalid */, &adapter->if_handle,
1479 &adapter->pmac_id);
1480 if (status != 0)
1481 goto do_none;
1483 be_vid_config(netdev);
1485 status = be_cmd_set_flow_control(ctrl, true, true);
1486 if (status != 0)
1487 goto if_destroy;
1489 status = be_tx_queues_create(adapter);
1490 if (status != 0)
1491 goto if_destroy;
1493 status = be_rx_queues_create(adapter);
1494 if (status != 0)
1495 goto tx_qs_destroy;
1497 /* First time posting */
1498 be_post_rx_frags(adapter);
1500 napi_enable(&rx_eq->napi);
1501 napi_enable(&tx_eq->napi);
1503 be_irq_register(adapter);
1505 be_intr_set(ctrl, true);
1507 /* The evt queues are created in the unarmed state; arm them */
1508 be_eq_notify(ctrl, rx_eq->q.id, true, false, 0);
1509 be_eq_notify(ctrl, tx_eq->q.id, true, false, 0);
1511 /* The compl queues are created in the unarmed state; arm them */
1512 be_cq_notify(ctrl, adapter->rx_obj.cq.id, true, 0);
1513 be_cq_notify(ctrl, adapter->tx_obj.cq.id, true, 0);
1515 be_link_status_update(adapter);
1517 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
1518 return 0;
1520 tx_qs_destroy:
1521 be_tx_queues_destroy(adapter);
1522 if_destroy:
1523 be_cmd_if_destroy(ctrl, adapter->if_handle);
1524 do_none:
1525 return status;
1528 static int be_close(struct net_device *netdev)
1530 struct be_adapter *adapter = netdev_priv(netdev);
1531 struct be_ctrl_info *ctrl = &adapter->ctrl;
1532 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1533 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1534 int vec;
1536 cancel_delayed_work(&adapter->work);
1538 netif_stop_queue(netdev);
1539 netif_carrier_off(netdev);
1540 adapter->link.speed = PHY_LINK_SPEED_ZERO;
1542 be_intr_set(ctrl, false);
1544 if (adapter->msix_enabled) {
1545 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1546 synchronize_irq(vec);
1547 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1548 synchronize_irq(vec);
1549 } else {
1550 synchronize_irq(netdev->irq);
1552 be_irq_unregister(adapter);
1554 napi_disable(&rx_eq->napi);
1555 napi_disable(&tx_eq->napi);
1557 be_rx_queues_destroy(adapter);
1558 be_tx_queues_destroy(adapter);
1560 be_cmd_if_destroy(ctrl, adapter->if_handle);
1561 return 0;
1564 static int be_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr,
1565 void **ip_hdr, void **tcpudp_hdr,
1566 u64 *hdr_flags, void *priv)
1568 struct ethhdr *eh;
1569 struct vlan_ethhdr *veh;
1570 struct iphdr *iph;
1571 u8 *va = page_address(frag->page) + frag->page_offset;
1572 unsigned long ll_hlen;
1574 prefetch(va);
1575 eh = (struct ethhdr *)va;
1576 *mac_hdr = eh;
1577 ll_hlen = ETH_HLEN;
1578 if (eh->h_proto != htons(ETH_P_IP)) {
1579 if (eh->h_proto == htons(ETH_P_8021Q)) {
1580 veh = (struct vlan_ethhdr *)va;
1581 if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
1582 return -1;
1584 ll_hlen += VLAN_HLEN;
1585 } else {
1586 return -1;
1589 *hdr_flags = LRO_IPV4;
1590 iph = (struct iphdr *)(va + ll_hlen);
1591 *ip_hdr = iph;
1592 if (iph->protocol != IPPROTO_TCP)
1593 return -1;
1594 *hdr_flags |= LRO_TCP;
1595 *tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2);
1597 return 0;
1600 static void be_lro_init(struct be_adapter *adapter, struct net_device *netdev)
1602 struct net_lro_mgr *lro_mgr;
1604 lro_mgr = &adapter->rx_obj.lro_mgr;
1605 lro_mgr->dev = netdev;
1606 lro_mgr->features = LRO_F_NAPI;
1607 lro_mgr->ip_summed = CHECKSUM_UNNECESSARY;
1608 lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
1609 lro_mgr->max_desc = BE_MAX_LRO_DESCRIPTORS;
1610 lro_mgr->lro_arr = adapter->rx_obj.lro_desc;
1611 lro_mgr->get_frag_header = be_get_frag_header;
1612 lro_mgr->max_aggr = BE_MAX_FRAGS_PER_FRAME;
1615 static struct net_device_ops be_netdev_ops = {
1616 .ndo_open = be_open,
1617 .ndo_stop = be_close,
1618 .ndo_start_xmit = be_xmit,
1619 .ndo_get_stats = be_get_stats,
1620 .ndo_set_rx_mode = be_set_multicast_list,
1621 .ndo_set_mac_address = be_mac_addr_set,
1622 .ndo_change_mtu = be_change_mtu,
1623 .ndo_validate_addr = eth_validate_addr,
1624 .ndo_vlan_rx_register = be_vlan_register,
1625 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
1626 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
1629 static void be_netdev_init(struct net_device *netdev)
1631 struct be_adapter *adapter = netdev_priv(netdev);
1633 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
1634 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_IP_CSUM |
1635 NETIF_F_IPV6_CSUM;
1637 netdev->flags |= IFF_MULTICAST;
1639 adapter->rx_csum = true;
1641 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
1643 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
1645 be_lro_init(adapter, netdev);
1647 netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx,
1648 BE_NAPI_WEIGHT);
1649 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx,
1650 BE_NAPI_WEIGHT);
1652 netif_carrier_off(netdev);
1653 netif_stop_queue(netdev);
1656 static void be_unmap_pci_bars(struct be_adapter *adapter)
1658 struct be_ctrl_info *ctrl = &adapter->ctrl;
1659 if (ctrl->csr)
1660 iounmap(ctrl->csr);
1661 if (ctrl->db)
1662 iounmap(ctrl->db);
1663 if (ctrl->pcicfg)
1664 iounmap(ctrl->pcicfg);
1667 static int be_map_pci_bars(struct be_adapter *adapter)
1669 u8 __iomem *addr;
1671 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
1672 pci_resource_len(adapter->pdev, 2));
1673 if (addr == NULL)
1674 return -ENOMEM;
1675 adapter->ctrl.csr = addr;
1677 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 4),
1678 128 * 1024);
1679 if (addr == NULL)
1680 goto pci_map_err;
1681 adapter->ctrl.db = addr;
1683 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 1),
1684 pci_resource_len(adapter->pdev, 1));
1685 if (addr == NULL)
1686 goto pci_map_err;
1687 adapter->ctrl.pcicfg = addr;
1689 return 0;
1690 pci_map_err:
1691 be_unmap_pci_bars(adapter);
1692 return -ENOMEM;
1696 static void be_ctrl_cleanup(struct be_adapter *adapter)
1698 struct be_dma_mem *mem = &adapter->ctrl.mbox_mem_alloced;
1700 be_unmap_pci_bars(adapter);
1702 if (mem->va)
1703 pci_free_consistent(adapter->pdev, mem->size,
1704 mem->va, mem->dma);
1707 /* Initialize the mbox required to send cmds to BE */
1708 static int be_ctrl_init(struct be_adapter *adapter)
1710 struct be_ctrl_info *ctrl = &adapter->ctrl;
1711 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
1712 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
1713 int status;
1714 u32 val;
1716 status = be_map_pci_bars(adapter);
1717 if (status)
1718 return status;
1720 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
1721 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
1722 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
1723 if (!mbox_mem_alloc->va) {
1724 be_unmap_pci_bars(adapter);
1725 return -1;
1727 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
1728 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
1729 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
1730 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
1731 spin_lock_init(&ctrl->cmd_lock);
1733 val = ioread32(ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
1734 ctrl->pci_func = (val >> MEMBAR_CTRL_INT_CTRL_PFUNC_SHIFT) &
1735 MEMBAR_CTRL_INT_CTRL_PFUNC_MASK;
1736 return 0;
1739 static void be_stats_cleanup(struct be_adapter *adapter)
1741 struct be_stats_obj *stats = &adapter->stats;
1742 struct be_dma_mem *cmd = &stats->cmd;
1744 if (cmd->va)
1745 pci_free_consistent(adapter->pdev, cmd->size,
1746 cmd->va, cmd->dma);
1749 static int be_stats_init(struct be_adapter *adapter)
1751 struct be_stats_obj *stats = &adapter->stats;
1752 struct be_dma_mem *cmd = &stats->cmd;
1754 cmd->size = sizeof(struct be_cmd_req_get_stats);
1755 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
1756 if (cmd->va == NULL)
1757 return -1;
1758 return 0;
1761 static void __devexit be_remove(struct pci_dev *pdev)
1763 struct be_adapter *adapter = pci_get_drvdata(pdev);
1764 if (!adapter)
1765 return;
1767 unregister_netdev(adapter->netdev);
1769 be_stats_cleanup(adapter);
1771 be_ctrl_cleanup(adapter);
1773 if (adapter->msix_enabled) {
1774 pci_disable_msix(adapter->pdev);
1775 adapter->msix_enabled = false;
1778 pci_set_drvdata(pdev, NULL);
1779 pci_release_regions(pdev);
1780 pci_disable_device(pdev);
1782 free_netdev(adapter->netdev);
1785 static int be_hw_up(struct be_adapter *adapter)
1787 struct be_ctrl_info *ctrl = &adapter->ctrl;
1788 int status;
1790 status = be_cmd_POST(ctrl);
1791 if (status)
1792 return status;
1794 status = be_cmd_get_fw_ver(ctrl, adapter->fw_ver);
1795 if (status)
1796 return status;
1798 status = be_cmd_query_fw_cfg(ctrl, &adapter->port_num);
1799 return status;
1802 static int __devinit be_probe(struct pci_dev *pdev,
1803 const struct pci_device_id *pdev_id)
1805 int status = 0;
1806 struct be_adapter *adapter;
1807 struct net_device *netdev;
1808 struct be_ctrl_info *ctrl;
1809 u8 mac[ETH_ALEN];
1811 status = pci_enable_device(pdev);
1812 if (status)
1813 goto do_none;
1815 status = pci_request_regions(pdev, DRV_NAME);
1816 if (status)
1817 goto disable_dev;
1818 pci_set_master(pdev);
1820 netdev = alloc_etherdev(sizeof(struct be_adapter));
1821 if (netdev == NULL) {
1822 status = -ENOMEM;
1823 goto rel_reg;
1825 adapter = netdev_priv(netdev);
1826 adapter->pdev = pdev;
1827 pci_set_drvdata(pdev, adapter);
1828 adapter->netdev = netdev;
1830 be_msix_enable(adapter);
1832 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1833 if (!status) {
1834 netdev->features |= NETIF_F_HIGHDMA;
1835 } else {
1836 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1837 if (status) {
1838 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
1839 goto free_netdev;
1843 ctrl = &adapter->ctrl;
1844 status = be_ctrl_init(adapter);
1845 if (status)
1846 goto free_netdev;
1848 status = be_stats_init(adapter);
1849 if (status)
1850 goto ctrl_clean;
1852 status = be_hw_up(adapter);
1853 if (status)
1854 goto stats_clean;
1856 status = be_cmd_mac_addr_query(ctrl, mac, MAC_ADDRESS_TYPE_NETWORK,
1857 true /* permanent */, 0);
1858 if (status)
1859 goto stats_clean;
1860 memcpy(netdev->dev_addr, mac, ETH_ALEN);
1862 INIT_DELAYED_WORK(&adapter->work, be_worker);
1863 be_netdev_init(netdev);
1864 SET_NETDEV_DEV(netdev, &adapter->pdev->dev);
1866 status = register_netdev(netdev);
1867 if (status != 0)
1868 goto stats_clean;
1870 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
1871 return 0;
1873 stats_clean:
1874 be_stats_cleanup(adapter);
1875 ctrl_clean:
1876 be_ctrl_cleanup(adapter);
1877 free_netdev:
1878 free_netdev(adapter->netdev);
1879 rel_reg:
1880 pci_release_regions(pdev);
1881 disable_dev:
1882 pci_disable_device(pdev);
1883 do_none:
1884 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
1885 return status;
1888 static int be_suspend(struct pci_dev *pdev, pm_message_t state)
1890 struct be_adapter *adapter = pci_get_drvdata(pdev);
1891 struct net_device *netdev = adapter->netdev;
1893 netif_device_detach(netdev);
1894 if (netif_running(netdev)) {
1895 rtnl_lock();
1896 be_close(netdev);
1897 rtnl_unlock();
1900 pci_save_state(pdev);
1901 pci_disable_device(pdev);
1902 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1903 return 0;
1906 static int be_resume(struct pci_dev *pdev)
1908 int status = 0;
1909 struct be_adapter *adapter = pci_get_drvdata(pdev);
1910 struct net_device *netdev = adapter->netdev;
1912 netif_device_detach(netdev);
1914 status = pci_enable_device(pdev);
1915 if (status)
1916 return status;
1918 pci_set_power_state(pdev, 0);
1919 pci_restore_state(pdev);
1921 if (netif_running(netdev)) {
1922 rtnl_lock();
1923 be_open(netdev);
1924 rtnl_unlock();
1926 netif_device_attach(netdev);
1927 return 0;
1930 static struct pci_driver be_driver = {
1931 .name = DRV_NAME,
1932 .id_table = be_dev_ids,
1933 .probe = be_probe,
1934 .remove = be_remove,
1935 .suspend = be_suspend,
1936 .resume = be_resume
1939 static int __init be_init_module(void)
1941 if (rx_frag_size != 8192 && rx_frag_size != 4096
1942 && rx_frag_size != 2048) {
1943 printk(KERN_WARNING DRV_NAME
1944 " : Module param rx_frag_size must be 2048/4096/8192."
1945 " Using 2048\n");
1946 rx_frag_size = 2048;
1948 /* Ensure rx_frag_size is aligned to chache line */
1949 if (SKB_DATA_ALIGN(rx_frag_size) != rx_frag_size) {
1950 printk(KERN_WARNING DRV_NAME
1951 " : Bad module param rx_frag_size. Using 2048\n");
1952 rx_frag_size = 2048;
1955 return pci_register_driver(&be_driver);
1957 module_init(be_init_module);
1959 static void __exit be_exit_module(void)
1961 pci_unregister_driver(&be_driver);
1963 module_exit(be_exit_module);