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 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
) },
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
)
340 int cnt
= (skb
->len
> skb
->data_len
);
342 cnt
+= skb_shinfo(skb
)->nr_frags
;
344 /* to account for hdr wrb */
347 /* add a dummy to make it an even num */
352 BUG_ON(cnt
> BE_MAX_TX_FRAG_COUNT
);
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
) {
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
)
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
);
409 if (skb
->len
> skb
->data_len
) {
410 int len
= skb
->len
- skb
->data_len
;
411 busaddr
= pci_map_single(pdev
, skb
->data
, len
,
413 wrb
= queue_head_node(txq
);
414 wrb_fill(wrb
, busaddr
, len
);
415 be_dws_cpu_to_le(wrb
, sizeof(*wrb
));
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
,
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
));
430 copied
+= frag
->size
;
434 wrb
= queue_head_node(txq
);
436 be_dws_cpu_to_le(wrb
, sizeof(*wrb
));
440 wrb_fill_hdr(hdr
, first_skb
, adapter
->vlan_grp
? true : false,
442 be_dws_cpu_to_le(hdr
, sizeof(*hdr
));
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
);
473 be_txq_notify(&adapter
->ctrl
, txq
->id
, wrb_cnt
);
475 be_tx_stats_update(adapter
, wrb_cnt
, copied
, stopped
);
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
);
489 dev_info(&adapter
->pdev
->dev
, "MTU changed from %d to %d bytes\n",
490 netdev
->mtu
, new_mtu
);
491 netdev
->mtu
= new_mtu
;
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
];
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
);
514 be_cmd_vlan_config(&adapter
->ctrl
, adapter
->if_handle
,
517 be_cmd_vlan_config(&adapter
->ctrl
, adapter
->if_handle
,
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
];
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);
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
);
574 be_cmd_mcast_mac_set(&adapter
->ctrl
,
575 adapter
->if_handle
, &mac_addr
[0][0], i
, false);
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);
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
);
606 if (time_before(now
, stats
->be_rx_jiffies
)) {
607 stats
->be_rx_jiffies
= now
;
611 /* Update the rate once in two seconds */
612 if ((now
- stats
->be_rx_jiffies
) < 2 * HZ
)
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
);
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
);
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
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
;
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
;
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
);
717 skb
->tail
+= curr_frag_len
;
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);
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
);
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
)
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
);
769 dev_warn(&adapter
->pdev
->dev
, "skb alloc failed\n");
770 be_rx_compl_discard(adapter
, rxcp
);
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
;
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
;
788 if (!adapter
->vlan_grp
|| adapter
->num_vlans
== 0) {
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
);
796 netif_receive_skb(skb
);
799 adapter
->netdev
->last_rx
= jiffies
;
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
,
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)
845 lro_vlan_hwaccel_receive_frags(&adapter
->rx_obj
.lro_mgr
,
846 rx_frags
, pkt_size
, pkt_size
, adapter
->vlan_grp
,
850 be_rx_stats_update(adapter
, pkt_size
, num_rcvd
);
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)
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
);
869 static inline struct page
*be_alloc_pages(u32 size
)
871 gfp_t alloc_flags
= GFP_ATOMIC
;
872 u32 order
= get_order(size
);
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
++) {
895 pagep
= be_alloc_pages(adapter
->big_page_size
);
896 if (unlikely(!pagep
)) {
897 drvr_stats(adapter
)->be_ethrx_post_fail
++;
900 page_dmaaddr
= pci_map_page(adapter
->pdev
, pagep
, 0,
901 adapter
->big_page_size
,
903 page_info
->page_offset
= 0;
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
));
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
) {
922 page_info
->last_page_user
= true;
924 page_info
= &page_info_tbl
[rxq
->head
];
927 page_info
->last_page_user
= true;
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;
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)
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
);
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
;
964 u16 cur_index
, num_wrbs
= 0;
966 cur_index
= txq
->tail
;
967 sent_skb
= sent_skbs
[cur_index
];
969 sent_skbs
[cur_index
] = NULL
;
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
;
977 pci_unmap_single(adapter
->pdev
, busaddr
,
978 wrb
->frag_len
, PCI_DMA_TODEVICE
);
982 } while (cur_index
!= last_index
);
984 atomic_sub(num_wrbs
, &txq
->used
);
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
;
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
;
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
;
1036 be_cmd_q_destroy(&adapter
->ctrl
, q
, QTYPE_TXQ
);
1037 be_queue_free(adapter
, q
);
1039 q
= &adapter
->tx_obj
.cq
;
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
;
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
)))
1067 /* Ask BE to create Tx Event queue */
1068 if (be_cmd_eq_create(&adapter
->ctrl
, eq
, adapter
->tx_eq
.cur_eqd
))
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
)))
1076 /* Ask BE to create Tx eth compl queue */
1077 if (be_cmd_cq_create(&adapter
->ctrl
, cq
, eq
, false, false, 3))
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
)))
1085 /* Ask BE to create Tx eth queue */
1086 if (be_cmd_txq_create(&adapter
->ctrl
, q
, cq
))
1091 be_queue_free(adapter
, q
);
1093 be_cmd_q_destroy(&adapter
->ctrl
, cq
, QTYPE_CQ
);
1095 be_queue_free(adapter
, cq
);
1097 be_cmd_q_destroy(&adapter
->ctrl
, eq
, QTYPE_EQ
);
1099 be_queue_free(adapter
, eq
);
1103 static void be_rx_queues_destroy(struct be_adapter
*adapter
)
1105 struct be_queue_info
*q
;
1107 q
= &adapter
->rx_obj
.q
;
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
;
1116 be_cmd_q_destroy(&adapter
->ctrl
, q
, QTYPE_CQ
);
1117 be_queue_free(adapter
, q
);
1119 q
= &adapter
->rx_eq
.q
;
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
;
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
));
1144 /* Ask BE to create Rx Event queue */
1145 rc
= be_cmd_eq_create(&adapter
->ctrl
, eq
, adapter
->rx_eq
.cur_eqd
);
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
));
1156 /* Ask BE to create Rx eth compl queue */
1157 rc
= be_cmd_cq_create(&adapter
->ctrl
, cq
, eq
, false, false, 3);
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
));
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);
1175 be_queue_free(adapter
, q
);
1177 be_cmd_q_destroy(&adapter
->ctrl
, cq
, QTYPE_CQ
);
1179 be_queue_free(adapter
, cq
);
1181 be_cmd_q_destroy(&adapter
->ctrl
, eq
, QTYPE_EQ
);
1183 be_queue_free(adapter
, eq
);
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
;
1194 evt
= le32_to_cpu(evt
);
1195 *rid
= (evt
>> EQ_ENTRY_RES_ID_SHIFT
) & EQ_ENTRY_RES_ID_MASK
;
1197 queue_tail_inc(&eq_obj
->q
);
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
))
1209 /* We can see an interrupt and no event */
1210 be_eq_notify(ctrl
, eq_obj
->q
.id
, true, true, num
);
1212 napi_schedule(&eq_obj
->napi
);
1217 static irqreturn_t
be_intx(int irq
, void *dev
)
1219 struct be_adapter
*adapter
= dev
;
1220 struct be_ctrl_info
*ctrl
= &adapter
->ctrl
;
1223 tx
= event_handle(ctrl
, &adapter
->tx_eq
);
1224 rx
= event_handle(ctrl
, &adapter
->rx_eq
);
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
);
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
);
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
);
1257 drvr_stats(adapter
)->be_rxcp_err
++;
1259 return (!tcp_frame
|| err
|| (adapter
->max_rx_coal
<= 1)) ?
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
;
1272 for (work_done
= 0; work_done
< budget
; work_done
++) {
1273 rxcp
= be_rx_compl_get(adapter
);
1277 if (do_lro(adapter
, rxcp
))
1278 be_rx_compl_process_lro(adapter
, rxcp
);
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
);
1290 if (work_done
< budget
) {
1291 napi_complete(napi
);
1292 be_cq_notify(&adapter
->ctrl
, rx_cq
->id
, true, work_done
);
1294 /* More to be consumed; continue with interrupts disabled */
1295 be_cq_notify(&adapter
->ctrl
, rx_cq
->id
, false, 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
;
1313 while ((txcp
= be_tx_compl_get(adapter
))) {
1314 end_idx
= AMAP_GET_BITS(struct amap_eth_tx_compl
,
1316 be_tx_compl_process(adapter
, end_idx
);
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
;
1338 static void be_worker(struct work_struct
*work
)
1340 struct be_adapter
*adapter
=
1341 container_of(work
, struct be_adapter
, work
.work
);
1345 be_link_status_update(adapter
);
1348 status
= be_cmd_get_stats(&adapter
->ctrl
, &adapter
->stats
.cmd
);
1350 netdev_stats_update(adapter
);
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
)
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
);
1376 adapter
->msix_enabled
= true;
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
;
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
);
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
);
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;
1416 static int be_irq_register(struct be_adapter
*adapter
)
1418 struct net_device
*netdev
= adapter
->netdev
;
1421 if (adapter
->msix_enabled
) {
1422 status
= be_msix_register(adapter
);
1428 netdev
->irq
= adapter
->pdev
->irq
;
1429 status
= request_irq(netdev
->irq
, be_intx
, IRQF_SHARED
, netdev
->name
,
1432 dev_err(&adapter
->pdev
->dev
,
1433 "INTx request IRQ failed - err %d\n", status
);
1437 adapter
->isr_registered
= true;
1441 static void be_irq_unregister(struct be_adapter
*adapter
)
1443 struct net_device
*netdev
= adapter
->netdev
;
1446 if (!adapter
->isr_registered
)
1450 if (!adapter
->msix_enabled
) {
1451 free_irq(netdev
->irq
, adapter
);
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
);
1461 adapter
->isr_registered
= false;
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
;
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
,
1483 be_vid_config(netdev
);
1485 status
= be_cmd_set_flow_control(ctrl
, true, true);
1489 status
= be_tx_queues_create(adapter
);
1493 status
= be_rx_queues_create(adapter
);
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));
1521 be_tx_queues_destroy(adapter
);
1523 be_cmd_if_destroy(ctrl
, adapter
->if_handle
);
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
;
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
);
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
);
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
)
1569 struct vlan_ethhdr
*veh
;
1571 u8
*va
= page_address(frag
->page
) + frag
->page_offset
;
1572 unsigned long ll_hlen
;
1575 eh
= (struct ethhdr
*)va
;
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
))
1584 ll_hlen
+= VLAN_HLEN
;
1589 *hdr_flags
= LRO_IPV4
;
1590 iph
= (struct iphdr
*)(va
+ ll_hlen
);
1592 if (iph
->protocol
!= IPPROTO_TCP
)
1594 *hdr_flags
|= LRO_TCP
;
1595 *tcpudp_hdr
= (u8
*) (*ip_hdr
) + (iph
->ihl
<< 2);
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
|
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
,
1649 netif_napi_add(netdev
, &adapter
->tx_eq
.napi
, be_poll_tx
,
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
;
1664 iounmap(ctrl
->pcicfg
);
1667 static int be_map_pci_bars(struct be_adapter
*adapter
)
1671 addr
= ioremap_nocache(pci_resource_start(adapter
->pdev
, 2),
1672 pci_resource_len(adapter
->pdev
, 2));
1675 adapter
->ctrl
.csr
= addr
;
1677 addr
= ioremap_nocache(pci_resource_start(adapter
->pdev
, 4),
1681 adapter
->ctrl
.db
= addr
;
1683 addr
= ioremap_nocache(pci_resource_start(adapter
->pdev
, 1),
1684 pci_resource_len(adapter
->pdev
, 1));
1687 adapter
->ctrl
.pcicfg
= addr
;
1691 be_unmap_pci_bars(adapter
);
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
);
1703 pci_free_consistent(adapter
->pdev
, mem
->size
,
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
;
1716 status
= be_map_pci_bars(adapter
);
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
);
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
;
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
;
1745 pci_free_consistent(adapter
->pdev
, cmd
->size
,
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
)
1761 static void __devexit
be_remove(struct pci_dev
*pdev
)
1763 struct be_adapter
*adapter
= pci_get_drvdata(pdev
);
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
;
1790 status
= be_cmd_POST(ctrl
);
1794 status
= be_cmd_get_fw_ver(ctrl
, adapter
->fw_ver
);
1798 status
= be_cmd_query_fw_cfg(ctrl
, &adapter
->port_num
);
1802 static int __devinit
be_probe(struct pci_dev
*pdev
,
1803 const struct pci_device_id
*pdev_id
)
1806 struct be_adapter
*adapter
;
1807 struct net_device
*netdev
;
1808 struct be_ctrl_info
*ctrl
;
1811 status
= pci_enable_device(pdev
);
1815 status
= pci_request_regions(pdev
, DRV_NAME
);
1818 pci_set_master(pdev
);
1820 netdev
= alloc_etherdev(sizeof(struct be_adapter
));
1821 if (netdev
== NULL
) {
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));
1834 netdev
->features
|= NETIF_F_HIGHDMA
;
1836 status
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
1838 dev_err(&pdev
->dev
, "Could not set PCI DMA Mask\n");
1843 ctrl
= &adapter
->ctrl
;
1844 status
= be_ctrl_init(adapter
);
1848 status
= be_stats_init(adapter
);
1852 status
= be_hw_up(adapter
);
1856 status
= be_cmd_mac_addr_query(ctrl
, mac
, MAC_ADDRESS_TYPE_NETWORK
,
1857 true /* permanent */, 0);
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
);
1870 dev_info(&pdev
->dev
, "%s port %d\n", nic_name(pdev
), adapter
->port_num
);
1874 be_stats_cleanup(adapter
);
1876 be_ctrl_cleanup(adapter
);
1878 free_netdev(adapter
->netdev
);
1880 pci_release_regions(pdev
);
1882 pci_disable_device(pdev
);
1884 dev_err(&pdev
->dev
, "%s initialization failed\n", nic_name(pdev
));
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
)) {
1900 pci_save_state(pdev
);
1901 pci_disable_device(pdev
);
1902 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1906 static int be_resume(struct pci_dev
*pdev
)
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
);
1918 pci_set_power_state(pdev
, 0);
1919 pci_restore_state(pdev
);
1921 if (netif_running(netdev
)) {
1926 netif_device_attach(netdev
);
1930 static struct pci_driver be_driver
= {
1932 .id_table
= be_dev_ids
,
1934 .remove
= be_remove
,
1935 .suspend
= be_suspend
,
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."
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
);