mm-only debug patch...
[mmotm.git] / drivers / net / benet / be_main.c
blob0e92a1f055a2f7fc30da06d9daa4a50903230ead
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 "be_cmds.h"
20 #include <asm/div64.h>
22 MODULE_VERSION(DRV_VER);
23 MODULE_DEVICE_TABLE(pci, be_dev_ids);
24 MODULE_DESCRIPTION(DRV_DESC " " DRV_VER);
25 MODULE_AUTHOR("ServerEngines Corporation");
26 MODULE_LICENSE("GPL");
28 static unsigned int rx_frag_size = 2048;
29 module_param(rx_frag_size, uint, S_IRUGO);
30 MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data.");
32 static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
33 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
34 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
35 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
36 { 0 }
38 MODULE_DEVICE_TABLE(pci, be_dev_ids);
40 static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
42 struct be_dma_mem *mem = &q->dma_mem;
43 if (mem->va)
44 pci_free_consistent(adapter->pdev, mem->size,
45 mem->va, mem->dma);
48 static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
49 u16 len, u16 entry_size)
51 struct be_dma_mem *mem = &q->dma_mem;
53 memset(q, 0, sizeof(*q));
54 q->len = len;
55 q->entry_size = entry_size;
56 mem->size = len * entry_size;
57 mem->va = pci_alloc_consistent(adapter->pdev, mem->size, &mem->dma);
58 if (!mem->va)
59 return -1;
60 memset(mem->va, 0, mem->size);
61 return 0;
64 static void be_intr_set(struct be_adapter *adapter, bool enable)
66 u8 __iomem *addr = adapter->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
67 u32 reg = ioread32(addr);
68 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
70 if (!enabled && enable)
71 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
72 else if (enabled && !enable)
73 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
74 else
75 return;
77 iowrite32(reg, addr);
80 static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
82 u32 val = 0;
83 val |= qid & DB_RQ_RING_ID_MASK;
84 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
85 iowrite32(val, adapter->db + DB_RQ_OFFSET);
88 static void be_txq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
90 u32 val = 0;
91 val |= qid & DB_TXULP_RING_ID_MASK;
92 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
93 iowrite32(val, adapter->db + DB_TXULP1_OFFSET);
96 static void be_eq_notify(struct be_adapter *adapter, u16 qid,
97 bool arm, bool clear_int, u16 num_popped)
99 u32 val = 0;
100 val |= qid & DB_EQ_RING_ID_MASK;
101 if (arm)
102 val |= 1 << DB_EQ_REARM_SHIFT;
103 if (clear_int)
104 val |= 1 << DB_EQ_CLR_SHIFT;
105 val |= 1 << DB_EQ_EVNT_SHIFT;
106 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
107 iowrite32(val, adapter->db + DB_EQ_OFFSET);
110 void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
112 u32 val = 0;
113 val |= qid & DB_CQ_RING_ID_MASK;
114 if (arm)
115 val |= 1 << DB_CQ_REARM_SHIFT;
116 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
117 iowrite32(val, adapter->db + DB_CQ_OFFSET);
120 static int be_mac_addr_set(struct net_device *netdev, void *p)
122 struct be_adapter *adapter = netdev_priv(netdev);
123 struct sockaddr *addr = p;
124 int status = 0;
126 status = be_cmd_pmac_del(adapter, adapter->if_handle, adapter->pmac_id);
127 if (status)
128 return status;
130 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
131 adapter->if_handle, &adapter->pmac_id);
132 if (!status)
133 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
135 return status;
138 void netdev_stats_update(struct be_adapter *adapter)
140 struct be_hw_stats *hw_stats = hw_stats_from_cmd(adapter->stats.cmd.va);
141 struct be_rxf_stats *rxf_stats = &hw_stats->rxf;
142 struct be_port_rxf_stats *port_stats =
143 &rxf_stats->port[adapter->port_num];
144 struct net_device_stats *dev_stats = &adapter->netdev->stats;
145 struct be_erx_stats *erx_stats = &hw_stats->erx;
147 dev_stats->rx_packets = port_stats->rx_total_frames;
148 dev_stats->tx_packets = port_stats->tx_unicastframes +
149 port_stats->tx_multicastframes + port_stats->tx_broadcastframes;
150 dev_stats->rx_bytes = (u64) port_stats->rx_bytes_msd << 32 |
151 (u64) port_stats->rx_bytes_lsd;
152 dev_stats->tx_bytes = (u64) port_stats->tx_bytes_msd << 32 |
153 (u64) port_stats->tx_bytes_lsd;
155 /* bad pkts received */
156 dev_stats->rx_errors = port_stats->rx_crc_errors +
157 port_stats->rx_alignment_symbol_errors +
158 port_stats->rx_in_range_errors +
159 port_stats->rx_out_range_errors +
160 port_stats->rx_frame_too_long +
161 port_stats->rx_dropped_too_small +
162 port_stats->rx_dropped_too_short +
163 port_stats->rx_dropped_header_too_small +
164 port_stats->rx_dropped_tcp_length +
165 port_stats->rx_dropped_runt +
166 port_stats->rx_tcp_checksum_errs +
167 port_stats->rx_ip_checksum_errs +
168 port_stats->rx_udp_checksum_errs;
170 /* no space in linux buffers: best possible approximation */
171 dev_stats->rx_dropped = erx_stats->rx_drops_no_fragments[0];
173 /* detailed rx errors */
174 dev_stats->rx_length_errors = port_stats->rx_in_range_errors +
175 port_stats->rx_out_range_errors +
176 port_stats->rx_frame_too_long;
178 /* receive ring buffer overflow */
179 dev_stats->rx_over_errors = 0;
181 dev_stats->rx_crc_errors = port_stats->rx_crc_errors;
183 /* frame alignment errors */
184 dev_stats->rx_frame_errors = port_stats->rx_alignment_symbol_errors;
186 /* receiver fifo overrun */
187 /* drops_no_pbuf is no per i/f, it's per BE card */
188 dev_stats->rx_fifo_errors = port_stats->rx_fifo_overflow +
189 port_stats->rx_input_fifo_overflow +
190 rxf_stats->rx_drops_no_pbuf;
191 /* receiver missed packetd */
192 dev_stats->rx_missed_errors = 0;
194 /* packet transmit problems */
195 dev_stats->tx_errors = 0;
197 /* no space available in linux */
198 dev_stats->tx_dropped = 0;
200 dev_stats->multicast = port_stats->rx_multicast_frames;
201 dev_stats->collisions = 0;
203 /* detailed tx_errors */
204 dev_stats->tx_aborted_errors = 0;
205 dev_stats->tx_carrier_errors = 0;
206 dev_stats->tx_fifo_errors = 0;
207 dev_stats->tx_heartbeat_errors = 0;
208 dev_stats->tx_window_errors = 0;
211 void be_link_status_update(struct be_adapter *adapter, bool link_up)
213 struct net_device *netdev = adapter->netdev;
215 /* If link came up or went down */
216 if (adapter->link_up != link_up) {
217 if (link_up) {
218 netif_start_queue(netdev);
219 netif_carrier_on(netdev);
220 printk(KERN_INFO "%s: Link up\n", netdev->name);
221 } else {
222 netif_stop_queue(netdev);
223 netif_carrier_off(netdev);
224 printk(KERN_INFO "%s: Link down\n", netdev->name);
226 adapter->link_up = link_up;
230 /* Update the EQ delay n BE based on the RX frags consumed / sec */
231 static void be_rx_eqd_update(struct be_adapter *adapter)
233 struct be_eq_obj *rx_eq = &adapter->rx_eq;
234 struct be_drvr_stats *stats = &adapter->stats.drvr_stats;
235 ulong now = jiffies;
236 u32 eqd;
238 if (!rx_eq->enable_aic)
239 return;
241 /* Wrapped around */
242 if (time_before(now, stats->rx_fps_jiffies)) {
243 stats->rx_fps_jiffies = now;
244 return;
247 /* Update once a second */
248 if ((now - stats->rx_fps_jiffies) < HZ)
249 return;
251 stats->be_rx_fps = (stats->be_rx_frags - stats->be_prev_rx_frags) /
252 ((now - stats->rx_fps_jiffies) / HZ);
254 stats->rx_fps_jiffies = now;
255 stats->be_prev_rx_frags = stats->be_rx_frags;
256 eqd = stats->be_rx_fps / 110000;
257 eqd = eqd << 3;
258 if (eqd > rx_eq->max_eqd)
259 eqd = rx_eq->max_eqd;
260 if (eqd < rx_eq->min_eqd)
261 eqd = rx_eq->min_eqd;
262 if (eqd < 10)
263 eqd = 0;
264 if (eqd != rx_eq->cur_eqd)
265 be_cmd_modify_eqd(adapter, rx_eq->q.id, eqd);
267 rx_eq->cur_eqd = eqd;
270 static struct net_device_stats *be_get_stats(struct net_device *dev)
272 return &dev->stats;
275 static u32 be_calc_rate(u64 bytes, unsigned long ticks)
277 u64 rate = bytes;
279 do_div(rate, ticks / HZ);
280 rate <<= 3; /* bytes/sec -> bits/sec */
281 do_div(rate, 1000000ul); /* MB/Sec */
283 return rate;
286 static void be_tx_rate_update(struct be_adapter *adapter)
288 struct be_drvr_stats *stats = drvr_stats(adapter);
289 ulong now = jiffies;
291 /* Wrapped around? */
292 if (time_before(now, stats->be_tx_jiffies)) {
293 stats->be_tx_jiffies = now;
294 return;
297 /* Update tx rate once in two seconds */
298 if ((now - stats->be_tx_jiffies) > 2 * HZ) {
299 stats->be_tx_rate = be_calc_rate(stats->be_tx_bytes
300 - stats->be_tx_bytes_prev,
301 now - stats->be_tx_jiffies);
302 stats->be_tx_jiffies = now;
303 stats->be_tx_bytes_prev = stats->be_tx_bytes;
307 static void be_tx_stats_update(struct be_adapter *adapter,
308 u32 wrb_cnt, u32 copied, bool stopped)
310 struct be_drvr_stats *stats = drvr_stats(adapter);
311 stats->be_tx_reqs++;
312 stats->be_tx_wrbs += wrb_cnt;
313 stats->be_tx_bytes += copied;
314 if (stopped)
315 stats->be_tx_stops++;
318 /* Determine number of WRB entries needed to xmit data in an skb */
319 static u32 wrb_cnt_for_skb(struct sk_buff *skb, bool *dummy)
321 int cnt = (skb->len > skb->data_len);
323 cnt += skb_shinfo(skb)->nr_frags;
325 /* to account for hdr wrb */
326 cnt++;
327 if (cnt & 1) {
328 /* add a dummy to make it an even num */
329 cnt++;
330 *dummy = true;
331 } else
332 *dummy = false;
333 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
334 return cnt;
337 static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
339 wrb->frag_pa_hi = upper_32_bits(addr);
340 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
341 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
344 static void wrb_fill_hdr(struct be_eth_hdr_wrb *hdr, struct sk_buff *skb,
345 bool vlan, u32 wrb_cnt, u32 len)
347 memset(hdr, 0, sizeof(*hdr));
349 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
351 if (skb_shinfo(skb)->gso_segs > 1 && skb_shinfo(skb)->gso_size) {
352 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
353 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
354 hdr, skb_shinfo(skb)->gso_size);
355 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
356 if (is_tcp_pkt(skb))
357 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
358 else if (is_udp_pkt(skb))
359 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
362 if (vlan && vlan_tx_tag_present(skb)) {
363 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
364 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag,
365 hdr, vlan_tx_tag_get(skb));
368 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
369 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
370 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
371 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
375 static int make_tx_wrbs(struct be_adapter *adapter,
376 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
378 u64 busaddr;
379 u32 i, copied = 0;
380 struct pci_dev *pdev = adapter->pdev;
381 struct sk_buff *first_skb = skb;
382 struct be_queue_info *txq = &adapter->tx_obj.q;
383 struct be_eth_wrb *wrb;
384 struct be_eth_hdr_wrb *hdr;
386 hdr = queue_head_node(txq);
387 atomic_add(wrb_cnt, &txq->used);
388 queue_head_inc(txq);
390 if (skb_dma_map(&pdev->dev, skb, DMA_TO_DEVICE)) {
391 dev_err(&pdev->dev, "TX DMA mapping failed\n");
392 return 0;
395 if (skb->len > skb->data_len) {
396 int len = skb->len - skb->data_len;
397 wrb = queue_head_node(txq);
398 busaddr = skb_shinfo(skb)->dma_head;
399 wrb_fill(wrb, busaddr, len);
400 be_dws_cpu_to_le(wrb, sizeof(*wrb));
401 queue_head_inc(txq);
402 copied += len;
405 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
406 struct skb_frag_struct *frag =
407 &skb_shinfo(skb)->frags[i];
409 busaddr = skb_shinfo(skb)->dma_maps[i];
410 wrb = queue_head_node(txq);
411 wrb_fill(wrb, busaddr, frag->size);
412 be_dws_cpu_to_le(wrb, sizeof(*wrb));
413 queue_head_inc(txq);
414 copied += frag->size;
417 if (dummy_wrb) {
418 wrb = queue_head_node(txq);
419 wrb_fill(wrb, 0, 0);
420 be_dws_cpu_to_le(wrb, sizeof(*wrb));
421 queue_head_inc(txq);
424 wrb_fill_hdr(hdr, first_skb, adapter->vlan_grp ? true : false,
425 wrb_cnt, copied);
426 be_dws_cpu_to_le(hdr, sizeof(*hdr));
428 return copied;
431 static netdev_tx_t be_xmit(struct sk_buff *skb,
432 struct net_device *netdev)
434 struct be_adapter *adapter = netdev_priv(netdev);
435 struct be_tx_obj *tx_obj = &adapter->tx_obj;
436 struct be_queue_info *txq = &tx_obj->q;
437 u32 wrb_cnt = 0, copied = 0;
438 u32 start = txq->head;
439 bool dummy_wrb, stopped = false;
441 wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb);
443 copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb);
444 if (copied) {
445 /* record the sent skb in the sent_skb table */
446 BUG_ON(tx_obj->sent_skb_list[start]);
447 tx_obj->sent_skb_list[start] = skb;
449 /* Ensure txq has space for the next skb; Else stop the queue
450 * *BEFORE* ringing the tx doorbell, so that we serialze the
451 * tx compls of the current transmit which'll wake up the queue
453 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
454 txq->len) {
455 netif_stop_queue(netdev);
456 stopped = true;
459 be_txq_notify(adapter, txq->id, wrb_cnt);
461 be_tx_stats_update(adapter, wrb_cnt, copied, stopped);
462 } else {
463 txq->head = start;
464 dev_kfree_skb_any(skb);
466 return NETDEV_TX_OK;
469 static int be_change_mtu(struct net_device *netdev, int new_mtu)
471 struct be_adapter *adapter = netdev_priv(netdev);
472 if (new_mtu < BE_MIN_MTU ||
473 new_mtu > BE_MAX_JUMBO_FRAME_SIZE) {
474 dev_info(&adapter->pdev->dev,
475 "MTU must be between %d and %d bytes\n",
476 BE_MIN_MTU, BE_MAX_JUMBO_FRAME_SIZE);
477 return -EINVAL;
479 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
480 netdev->mtu, new_mtu);
481 netdev->mtu = new_mtu;
482 return 0;
486 * if there are BE_NUM_VLANS_SUPPORTED or lesser number of VLANS configured,
487 * program them in BE. If more than BE_NUM_VLANS_SUPPORTED are configured,
488 * set the BE in promiscuous VLAN mode.
490 static int be_vid_config(struct be_adapter *adapter)
492 u16 vtag[BE_NUM_VLANS_SUPPORTED];
493 u16 ntags = 0, i;
494 int status;
496 if (adapter->num_vlans <= BE_NUM_VLANS_SUPPORTED) {
497 /* Construct VLAN Table to give to HW */
498 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
499 if (adapter->vlan_tag[i]) {
500 vtag[ntags] = cpu_to_le16(i);
501 ntags++;
504 status = be_cmd_vlan_config(adapter, adapter->if_handle,
505 vtag, ntags, 1, 0);
506 } else {
507 status = be_cmd_vlan_config(adapter, adapter->if_handle,
508 NULL, 0, 1, 1);
510 return status;
513 static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
515 struct be_adapter *adapter = netdev_priv(netdev);
516 struct be_eq_obj *rx_eq = &adapter->rx_eq;
517 struct be_eq_obj *tx_eq = &adapter->tx_eq;
519 be_eq_notify(adapter, rx_eq->q.id, false, false, 0);
520 be_eq_notify(adapter, tx_eq->q.id, false, false, 0);
521 adapter->vlan_grp = grp;
522 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
523 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
526 static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
528 struct be_adapter *adapter = netdev_priv(netdev);
530 adapter->num_vlans++;
531 adapter->vlan_tag[vid] = 1;
533 be_vid_config(adapter);
536 static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
538 struct be_adapter *adapter = netdev_priv(netdev);
540 adapter->num_vlans--;
541 adapter->vlan_tag[vid] = 0;
543 vlan_group_set_device(adapter->vlan_grp, vid, NULL);
544 be_vid_config(adapter);
547 static void be_set_multicast_list(struct net_device *netdev)
549 struct be_adapter *adapter = netdev_priv(netdev);
551 if (netdev->flags & IFF_PROMISC) {
552 be_cmd_promiscuous_config(adapter, adapter->port_num, 1);
553 adapter->promiscuous = true;
554 goto done;
557 /* BE was previously in promiscous mode; disable it */
558 if (adapter->promiscuous) {
559 adapter->promiscuous = false;
560 be_cmd_promiscuous_config(adapter, adapter->port_num, 0);
563 if (netdev->flags & IFF_ALLMULTI) {
564 be_cmd_multicast_set(adapter, adapter->if_handle, NULL, 0);
565 goto done;
568 be_cmd_multicast_set(adapter, adapter->if_handle, netdev->mc_list,
569 netdev->mc_count);
570 done:
571 return;
574 static void be_rx_rate_update(struct be_adapter *adapter)
576 struct be_drvr_stats *stats = drvr_stats(adapter);
577 ulong now = jiffies;
579 /* Wrapped around */
580 if (time_before(now, stats->be_rx_jiffies)) {
581 stats->be_rx_jiffies = now;
582 return;
585 /* Update the rate once in two seconds */
586 if ((now - stats->be_rx_jiffies) < 2 * HZ)
587 return;
589 stats->be_rx_rate = be_calc_rate(stats->be_rx_bytes
590 - stats->be_rx_bytes_prev,
591 now - stats->be_rx_jiffies);
592 stats->be_rx_jiffies = now;
593 stats->be_rx_bytes_prev = stats->be_rx_bytes;
596 static void be_rx_stats_update(struct be_adapter *adapter,
597 u32 pktsize, u16 numfrags)
599 struct be_drvr_stats *stats = drvr_stats(adapter);
601 stats->be_rx_compl++;
602 stats->be_rx_frags += numfrags;
603 stats->be_rx_bytes += pktsize;
606 static inline bool do_pkt_csum(struct be_eth_rx_compl *rxcp, bool cso)
608 u8 l4_cksm, ip_version, ipcksm, tcpf = 0, udpf = 0, ipv6_chk;
610 l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp);
611 ipcksm = AMAP_GET_BITS(struct amap_eth_rx_compl, ipcksm, rxcp);
612 ip_version = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
613 if (ip_version) {
614 tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
615 udpf = AMAP_GET_BITS(struct amap_eth_rx_compl, udpf, rxcp);
617 ipv6_chk = (ip_version && (tcpf || udpf));
619 return ((l4_cksm && ipv6_chk && ipcksm) && cso) ? false : true;
622 static struct be_rx_page_info *
623 get_rx_page_info(struct be_adapter *adapter, u16 frag_idx)
625 struct be_rx_page_info *rx_page_info;
626 struct be_queue_info *rxq = &adapter->rx_obj.q;
628 rx_page_info = &adapter->rx_obj.page_info_tbl[frag_idx];
629 BUG_ON(!rx_page_info->page);
631 if (rx_page_info->last_page_user)
632 pci_unmap_page(adapter->pdev, pci_unmap_addr(rx_page_info, bus),
633 adapter->big_page_size, PCI_DMA_FROMDEVICE);
635 atomic_dec(&rxq->used);
636 return rx_page_info;
639 /* Throwaway the data in the Rx completion */
640 static void be_rx_compl_discard(struct be_adapter *adapter,
641 struct be_eth_rx_compl *rxcp)
643 struct be_queue_info *rxq = &adapter->rx_obj.q;
644 struct be_rx_page_info *page_info;
645 u16 rxq_idx, i, num_rcvd;
647 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
648 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
650 for (i = 0; i < num_rcvd; i++) {
651 page_info = get_rx_page_info(adapter, rxq_idx);
652 put_page(page_info->page);
653 memset(page_info, 0, sizeof(*page_info));
654 index_inc(&rxq_idx, rxq->len);
659 * skb_fill_rx_data forms a complete skb for an ether frame
660 * indicated by rxcp.
662 static void skb_fill_rx_data(struct be_adapter *adapter,
663 struct sk_buff *skb, struct be_eth_rx_compl *rxcp)
665 struct be_queue_info *rxq = &adapter->rx_obj.q;
666 struct be_rx_page_info *page_info;
667 u16 rxq_idx, i, num_rcvd, j;
668 u32 pktsize, hdr_len, curr_frag_len, size;
669 u8 *start;
671 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
672 pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
673 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
675 page_info = get_rx_page_info(adapter, rxq_idx);
677 start = page_address(page_info->page) + page_info->page_offset;
678 prefetch(start);
680 /* Copy data in the first descriptor of this completion */
681 curr_frag_len = min(pktsize, rx_frag_size);
683 /* Copy the header portion into skb_data */
684 hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
685 memcpy(skb->data, start, hdr_len);
686 skb->len = curr_frag_len;
687 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
688 /* Complete packet has now been moved to data */
689 put_page(page_info->page);
690 skb->data_len = 0;
691 skb->tail += curr_frag_len;
692 } else {
693 skb_shinfo(skb)->nr_frags = 1;
694 skb_shinfo(skb)->frags[0].page = page_info->page;
695 skb_shinfo(skb)->frags[0].page_offset =
696 page_info->page_offset + hdr_len;
697 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
698 skb->data_len = curr_frag_len - hdr_len;
699 skb->tail += hdr_len;
701 memset(page_info, 0, sizeof(*page_info));
703 if (pktsize <= rx_frag_size) {
704 BUG_ON(num_rcvd != 1);
705 goto done;
708 /* More frags present for this completion */
709 size = pktsize;
710 for (i = 1, j = 0; i < num_rcvd; i++) {
711 size -= curr_frag_len;
712 index_inc(&rxq_idx, rxq->len);
713 page_info = get_rx_page_info(adapter, rxq_idx);
715 curr_frag_len = min(size, rx_frag_size);
717 /* Coalesce all frags from the same physical page in one slot */
718 if (page_info->page_offset == 0) {
719 /* Fresh page */
720 j++;
721 skb_shinfo(skb)->frags[j].page = page_info->page;
722 skb_shinfo(skb)->frags[j].page_offset =
723 page_info->page_offset;
724 skb_shinfo(skb)->frags[j].size = 0;
725 skb_shinfo(skb)->nr_frags++;
726 } else {
727 put_page(page_info->page);
730 skb_shinfo(skb)->frags[j].size += curr_frag_len;
731 skb->len += curr_frag_len;
732 skb->data_len += curr_frag_len;
734 memset(page_info, 0, sizeof(*page_info));
736 BUG_ON(j > MAX_SKB_FRAGS);
738 done:
739 be_rx_stats_update(adapter, pktsize, num_rcvd);
740 return;
743 /* Process the RX completion indicated by rxcp when GRO is disabled */
744 static void be_rx_compl_process(struct be_adapter *adapter,
745 struct be_eth_rx_compl *rxcp)
747 struct sk_buff *skb;
748 u32 vlanf, vid;
749 u8 vtm;
751 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
752 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
754 /* vlanf could be wrongly set in some cards.
755 * ignore if vtm is not set */
756 if ((adapter->cap == 0x400) && !vtm)
757 vlanf = 0;
759 skb = netdev_alloc_skb(adapter->netdev, BE_HDR_LEN + NET_IP_ALIGN);
760 if (!skb) {
761 if (net_ratelimit())
762 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
763 be_rx_compl_discard(adapter, rxcp);
764 return;
767 skb_reserve(skb, NET_IP_ALIGN);
769 skb_fill_rx_data(adapter, skb, rxcp);
771 if (do_pkt_csum(rxcp, adapter->rx_csum))
772 skb->ip_summed = CHECKSUM_NONE;
773 else
774 skb->ip_summed = CHECKSUM_UNNECESSARY;
776 skb->truesize = skb->len + sizeof(struct sk_buff);
777 skb->protocol = eth_type_trans(skb, adapter->netdev);
778 skb->dev = adapter->netdev;
780 if (vlanf) {
781 if (!adapter->vlan_grp || adapter->num_vlans == 0) {
782 kfree_skb(skb);
783 return;
785 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
786 vid = be16_to_cpu(vid);
787 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
788 } else {
789 netif_receive_skb(skb);
792 return;
795 /* Process the RX completion indicated by rxcp when GRO is enabled */
796 static void be_rx_compl_process_gro(struct be_adapter *adapter,
797 struct be_eth_rx_compl *rxcp)
799 struct be_rx_page_info *page_info;
800 struct sk_buff *skb = NULL;
801 struct be_queue_info *rxq = &adapter->rx_obj.q;
802 struct be_eq_obj *eq_obj = &adapter->rx_eq;
803 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
804 u16 i, rxq_idx = 0, vid, j;
805 u8 vtm;
807 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
808 pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
809 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
810 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
811 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
813 /* vlanf could be wrongly set in some cards.
814 * ignore if vtm is not set */
815 if ((adapter->cap == 0x400) && !vtm)
816 vlanf = 0;
818 skb = napi_get_frags(&eq_obj->napi);
819 if (!skb) {
820 be_rx_compl_discard(adapter, rxcp);
821 return;
824 remaining = pkt_size;
825 for (i = 0, j = -1; i < num_rcvd; i++) {
826 page_info = get_rx_page_info(adapter, rxq_idx);
828 curr_frag_len = min(remaining, rx_frag_size);
830 /* Coalesce all frags from the same physical page in one slot */
831 if (i == 0 || page_info->page_offset == 0) {
832 /* First frag or Fresh page */
833 j++;
834 skb_shinfo(skb)->frags[j].page = page_info->page;
835 skb_shinfo(skb)->frags[j].page_offset =
836 page_info->page_offset;
837 skb_shinfo(skb)->frags[j].size = 0;
838 } else {
839 put_page(page_info->page);
841 skb_shinfo(skb)->frags[j].size += curr_frag_len;
843 remaining -= curr_frag_len;
844 index_inc(&rxq_idx, rxq->len);
845 memset(page_info, 0, sizeof(*page_info));
847 BUG_ON(j > MAX_SKB_FRAGS);
849 skb_shinfo(skb)->nr_frags = j + 1;
850 skb->len = pkt_size;
851 skb->data_len = pkt_size;
852 skb->truesize += pkt_size;
853 skb->ip_summed = CHECKSUM_UNNECESSARY;
855 if (likely(!vlanf)) {
856 napi_gro_frags(&eq_obj->napi);
857 } else {
858 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
859 vid = be16_to_cpu(vid);
861 if (!adapter->vlan_grp || adapter->num_vlans == 0)
862 return;
864 vlan_gro_frags(&eq_obj->napi, adapter->vlan_grp, vid);
867 be_rx_stats_update(adapter, pkt_size, num_rcvd);
868 return;
871 static struct be_eth_rx_compl *be_rx_compl_get(struct be_adapter *adapter)
873 struct be_eth_rx_compl *rxcp = queue_tail_node(&adapter->rx_obj.cq);
875 if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
876 return NULL;
878 be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
880 queue_tail_inc(&adapter->rx_obj.cq);
881 return rxcp;
884 /* To reset the valid bit, we need to reset the whole word as
885 * when walking the queue the valid entries are little-endian
886 * and invalid entries are host endian
888 static inline void be_rx_compl_reset(struct be_eth_rx_compl *rxcp)
890 rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
893 static inline struct page *be_alloc_pages(u32 size)
895 gfp_t alloc_flags = GFP_ATOMIC;
896 u32 order = get_order(size);
897 if (order > 0)
898 alloc_flags |= __GFP_COMP;
899 return alloc_pages(alloc_flags, order);
903 * Allocate a page, split it to fragments of size rx_frag_size and post as
904 * receive buffers to BE
906 static void be_post_rx_frags(struct be_adapter *adapter)
908 struct be_rx_page_info *page_info_tbl = adapter->rx_obj.page_info_tbl;
909 struct be_rx_page_info *page_info = NULL;
910 struct be_queue_info *rxq = &adapter->rx_obj.q;
911 struct page *pagep = NULL;
912 struct be_eth_rx_d *rxd;
913 u64 page_dmaaddr = 0, frag_dmaaddr;
914 u32 posted, page_offset = 0;
916 page_info = &page_info_tbl[rxq->head];
917 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
918 if (!pagep) {
919 pagep = be_alloc_pages(adapter->big_page_size);
920 if (unlikely(!pagep)) {
921 drvr_stats(adapter)->be_ethrx_post_fail++;
922 break;
924 page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
925 adapter->big_page_size,
926 PCI_DMA_FROMDEVICE);
927 page_info->page_offset = 0;
928 } else {
929 get_page(pagep);
930 page_info->page_offset = page_offset + rx_frag_size;
932 page_offset = page_info->page_offset;
933 page_info->page = pagep;
934 pci_unmap_addr_set(page_info, bus, page_dmaaddr);
935 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
937 rxd = queue_head_node(rxq);
938 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
939 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
940 queue_head_inc(rxq);
942 /* Any space left in the current big page for another frag? */
943 if ((page_offset + rx_frag_size + rx_frag_size) >
944 adapter->big_page_size) {
945 pagep = NULL;
946 page_info->last_page_user = true;
948 page_info = &page_info_tbl[rxq->head];
950 if (pagep)
951 page_info->last_page_user = true;
953 if (posted) {
954 atomic_add(posted, &rxq->used);
955 be_rxq_notify(adapter, rxq->id, posted);
956 } else if (atomic_read(&rxq->used) == 0) {
957 /* Let be_worker replenish when memory is available */
958 adapter->rx_post_starved = true;
961 return;
964 static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
966 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
968 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
969 return NULL;
971 be_dws_le_to_cpu(txcp, sizeof(*txcp));
973 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
975 queue_tail_inc(tx_cq);
976 return txcp;
979 static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
981 struct be_queue_info *txq = &adapter->tx_obj.q;
982 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
983 struct sk_buff *sent_skb;
984 u16 cur_index, num_wrbs = 0;
986 cur_index = txq->tail;
987 sent_skb = sent_skbs[cur_index];
988 BUG_ON(!sent_skb);
989 sent_skbs[cur_index] = NULL;
991 do {
992 cur_index = txq->tail;
993 num_wrbs++;
994 queue_tail_inc(txq);
995 } while (cur_index != last_index);
997 atomic_sub(num_wrbs, &txq->used);
998 skb_dma_unmap(&adapter->pdev->dev, sent_skb, DMA_TO_DEVICE);
999 kfree_skb(sent_skb);
1002 static inline struct be_eq_entry *event_get(struct be_eq_obj *eq_obj)
1004 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1006 if (!eqe->evt)
1007 return NULL;
1009 eqe->evt = le32_to_cpu(eqe->evt);
1010 queue_tail_inc(&eq_obj->q);
1011 return eqe;
1014 static int event_handle(struct be_adapter *adapter,
1015 struct be_eq_obj *eq_obj)
1017 struct be_eq_entry *eqe;
1018 u16 num = 0;
1020 while ((eqe = event_get(eq_obj)) != NULL) {
1021 eqe->evt = 0;
1022 num++;
1025 /* Deal with any spurious interrupts that come
1026 * without events
1028 be_eq_notify(adapter, eq_obj->q.id, true, true, num);
1029 if (num)
1030 napi_schedule(&eq_obj->napi);
1032 return num;
1035 /* Just read and notify events without processing them.
1036 * Used at the time of destroying event queues */
1037 static void be_eq_clean(struct be_adapter *adapter,
1038 struct be_eq_obj *eq_obj)
1040 struct be_eq_entry *eqe;
1041 u16 num = 0;
1043 while ((eqe = event_get(eq_obj)) != NULL) {
1044 eqe->evt = 0;
1045 num++;
1048 if (num)
1049 be_eq_notify(adapter, eq_obj->q.id, false, true, num);
1052 static void be_rx_q_clean(struct be_adapter *adapter)
1054 struct be_rx_page_info *page_info;
1055 struct be_queue_info *rxq = &adapter->rx_obj.q;
1056 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1057 struct be_eth_rx_compl *rxcp;
1058 u16 tail;
1060 /* First cleanup pending rx completions */
1061 while ((rxcp = be_rx_compl_get(adapter)) != NULL) {
1062 be_rx_compl_discard(adapter, rxcp);
1063 be_rx_compl_reset(rxcp);
1064 be_cq_notify(adapter, rx_cq->id, true, 1);
1067 /* Then free posted rx buffer that were not used */
1068 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
1069 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
1070 page_info = get_rx_page_info(adapter, tail);
1071 put_page(page_info->page);
1072 memset(page_info, 0, sizeof(*page_info));
1074 BUG_ON(atomic_read(&rxq->used));
1077 static void be_tx_compl_clean(struct be_adapter *adapter)
1079 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
1080 struct be_queue_info *txq = &adapter->tx_obj.q;
1081 struct be_eth_tx_compl *txcp;
1082 u16 end_idx, cmpl = 0, timeo = 0;
1084 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1085 do {
1086 while ((txcp = be_tx_compl_get(tx_cq))) {
1087 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1088 wrb_index, txcp);
1089 be_tx_compl_process(adapter, end_idx);
1090 cmpl++;
1092 if (cmpl) {
1093 be_cq_notify(adapter, tx_cq->id, false, cmpl);
1094 cmpl = 0;
1097 if (atomic_read(&txq->used) == 0 || ++timeo > 200)
1098 break;
1100 mdelay(1);
1101 } while (true);
1103 if (atomic_read(&txq->used))
1104 dev_err(&adapter->pdev->dev, "%d pending tx-completions\n",
1105 atomic_read(&txq->used));
1108 static void be_mcc_queues_destroy(struct be_adapter *adapter)
1110 struct be_queue_info *q;
1112 q = &adapter->mcc_obj.q;
1113 if (q->created)
1114 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
1115 be_queue_free(adapter, q);
1117 q = &adapter->mcc_obj.cq;
1118 if (q->created)
1119 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1120 be_queue_free(adapter, q);
1123 /* Must be called only after TX qs are created as MCC shares TX EQ */
1124 static int be_mcc_queues_create(struct be_adapter *adapter)
1126 struct be_queue_info *q, *cq;
1128 /* Alloc MCC compl queue */
1129 cq = &adapter->mcc_obj.cq;
1130 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
1131 sizeof(struct be_mcc_compl)))
1132 goto err;
1134 /* Ask BE to create MCC compl queue; share TX's eq */
1135 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
1136 goto mcc_cq_free;
1138 /* Alloc MCC queue */
1139 q = &adapter->mcc_obj.q;
1140 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1141 goto mcc_cq_destroy;
1143 /* Ask BE to create MCC queue */
1144 if (be_cmd_mccq_create(adapter, q, cq))
1145 goto mcc_q_free;
1147 return 0;
1149 mcc_q_free:
1150 be_queue_free(adapter, q);
1151 mcc_cq_destroy:
1152 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
1153 mcc_cq_free:
1154 be_queue_free(adapter, cq);
1155 err:
1156 return -1;
1159 static void be_tx_queues_destroy(struct be_adapter *adapter)
1161 struct be_queue_info *q;
1163 q = &adapter->tx_obj.q;
1164 if (q->created)
1165 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
1166 be_queue_free(adapter, q);
1168 q = &adapter->tx_obj.cq;
1169 if (q->created)
1170 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1171 be_queue_free(adapter, q);
1173 /* Clear any residual events */
1174 be_eq_clean(adapter, &adapter->tx_eq);
1176 q = &adapter->tx_eq.q;
1177 if (q->created)
1178 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
1179 be_queue_free(adapter, q);
1182 static int be_tx_queues_create(struct be_adapter *adapter)
1184 struct be_queue_info *eq, *q, *cq;
1186 adapter->tx_eq.max_eqd = 0;
1187 adapter->tx_eq.min_eqd = 0;
1188 adapter->tx_eq.cur_eqd = 96;
1189 adapter->tx_eq.enable_aic = false;
1190 /* Alloc Tx Event queue */
1191 eq = &adapter->tx_eq.q;
1192 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1193 return -1;
1195 /* Ask BE to create Tx Event queue */
1196 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
1197 goto tx_eq_free;
1198 /* Alloc TX eth compl queue */
1199 cq = &adapter->tx_obj.cq;
1200 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1201 sizeof(struct be_eth_tx_compl)))
1202 goto tx_eq_destroy;
1204 /* Ask BE to create Tx eth compl queue */
1205 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
1206 goto tx_cq_free;
1208 /* Alloc TX eth queue */
1209 q = &adapter->tx_obj.q;
1210 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1211 goto tx_cq_destroy;
1213 /* Ask BE to create Tx eth queue */
1214 if (be_cmd_txq_create(adapter, q, cq))
1215 goto tx_q_free;
1216 return 0;
1218 tx_q_free:
1219 be_queue_free(adapter, q);
1220 tx_cq_destroy:
1221 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
1222 tx_cq_free:
1223 be_queue_free(adapter, cq);
1224 tx_eq_destroy:
1225 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
1226 tx_eq_free:
1227 be_queue_free(adapter, eq);
1228 return -1;
1231 static void be_rx_queues_destroy(struct be_adapter *adapter)
1233 struct be_queue_info *q;
1235 q = &adapter->rx_obj.q;
1236 if (q->created) {
1237 be_cmd_q_destroy(adapter, q, QTYPE_RXQ);
1238 be_rx_q_clean(adapter);
1240 be_queue_free(adapter, q);
1242 q = &adapter->rx_obj.cq;
1243 if (q->created)
1244 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1245 be_queue_free(adapter, q);
1247 /* Clear any residual events */
1248 be_eq_clean(adapter, &adapter->rx_eq);
1250 q = &adapter->rx_eq.q;
1251 if (q->created)
1252 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
1253 be_queue_free(adapter, q);
1256 static int be_rx_queues_create(struct be_adapter *adapter)
1258 struct be_queue_info *eq, *q, *cq;
1259 int rc;
1261 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
1262 adapter->rx_eq.max_eqd = BE_MAX_EQD;
1263 adapter->rx_eq.min_eqd = 0;
1264 adapter->rx_eq.cur_eqd = 0;
1265 adapter->rx_eq.enable_aic = true;
1267 /* Alloc Rx Event queue */
1268 eq = &adapter->rx_eq.q;
1269 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1270 sizeof(struct be_eq_entry));
1271 if (rc)
1272 return rc;
1274 /* Ask BE to create Rx Event queue */
1275 rc = be_cmd_eq_create(adapter, eq, adapter->rx_eq.cur_eqd);
1276 if (rc)
1277 goto rx_eq_free;
1279 /* Alloc RX eth compl queue */
1280 cq = &adapter->rx_obj.cq;
1281 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1282 sizeof(struct be_eth_rx_compl));
1283 if (rc)
1284 goto rx_eq_destroy;
1286 /* Ask BE to create Rx eth compl queue */
1287 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
1288 if (rc)
1289 goto rx_cq_free;
1291 /* Alloc RX eth queue */
1292 q = &adapter->rx_obj.q;
1293 rc = be_queue_alloc(adapter, q, RX_Q_LEN, sizeof(struct be_eth_rx_d));
1294 if (rc)
1295 goto rx_cq_destroy;
1297 /* Ask BE to create Rx eth queue */
1298 rc = be_cmd_rxq_create(adapter, q, cq->id, rx_frag_size,
1299 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle, false);
1300 if (rc)
1301 goto rx_q_free;
1303 return 0;
1304 rx_q_free:
1305 be_queue_free(adapter, q);
1306 rx_cq_destroy:
1307 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
1308 rx_cq_free:
1309 be_queue_free(adapter, cq);
1310 rx_eq_destroy:
1311 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
1312 rx_eq_free:
1313 be_queue_free(adapter, eq);
1314 return rc;
1317 /* There are 8 evt ids per func. Retruns the evt id's bit number */
1318 static inline int be_evt_bit_get(struct be_adapter *adapter, u32 eq_id)
1320 return eq_id - 8 * be_pci_func(adapter);
1323 static irqreturn_t be_intx(int irq, void *dev)
1325 struct be_adapter *adapter = dev;
1326 int isr;
1328 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
1329 be_pci_func(adapter) * CEV_ISR_SIZE);
1330 if (!isr)
1331 return IRQ_NONE;
1333 event_handle(adapter, &adapter->tx_eq);
1334 event_handle(adapter, &adapter->rx_eq);
1336 return IRQ_HANDLED;
1339 static irqreturn_t be_msix_rx(int irq, void *dev)
1341 struct be_adapter *adapter = dev;
1343 event_handle(adapter, &adapter->rx_eq);
1345 return IRQ_HANDLED;
1348 static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
1350 struct be_adapter *adapter = dev;
1352 event_handle(adapter, &adapter->tx_eq);
1354 return IRQ_HANDLED;
1357 static inline bool do_gro(struct be_adapter *adapter,
1358 struct be_eth_rx_compl *rxcp)
1360 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1361 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1363 if (err)
1364 drvr_stats(adapter)->be_rxcp_err++;
1366 return (tcp_frame && !err) ? true : false;
1369 int be_poll_rx(struct napi_struct *napi, int budget)
1371 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
1372 struct be_adapter *adapter =
1373 container_of(rx_eq, struct be_adapter, rx_eq);
1374 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1375 struct be_eth_rx_compl *rxcp;
1376 u32 work_done;
1378 for (work_done = 0; work_done < budget; work_done++) {
1379 rxcp = be_rx_compl_get(adapter);
1380 if (!rxcp)
1381 break;
1383 if (do_gro(adapter, rxcp))
1384 be_rx_compl_process_gro(adapter, rxcp);
1385 else
1386 be_rx_compl_process(adapter, rxcp);
1388 be_rx_compl_reset(rxcp);
1391 /* Refill the queue */
1392 if (atomic_read(&adapter->rx_obj.q.used) < RX_FRAGS_REFILL_WM)
1393 be_post_rx_frags(adapter);
1395 /* All consumed */
1396 if (work_done < budget) {
1397 napi_complete(napi);
1398 be_cq_notify(adapter, rx_cq->id, true, work_done);
1399 } else {
1400 /* More to be consumed; continue with interrupts disabled */
1401 be_cq_notify(adapter, rx_cq->id, false, work_done);
1403 return work_done;
1406 void be_process_tx(struct be_adapter *adapter)
1408 struct be_queue_info *txq = &adapter->tx_obj.q;
1409 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
1410 struct be_eth_tx_compl *txcp;
1411 u32 num_cmpl = 0;
1412 u16 end_idx;
1414 while ((txcp = be_tx_compl_get(tx_cq))) {
1415 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1416 wrb_index, txcp);
1417 be_tx_compl_process(adapter, end_idx);
1418 num_cmpl++;
1421 if (num_cmpl) {
1422 be_cq_notify(adapter, tx_cq->id, true, num_cmpl);
1424 /* As Tx wrbs have been freed up, wake up netdev queue if
1425 * it was stopped due to lack of tx wrbs.
1427 if (netif_queue_stopped(adapter->netdev) &&
1428 atomic_read(&txq->used) < txq->len / 2) {
1429 netif_wake_queue(adapter->netdev);
1432 drvr_stats(adapter)->be_tx_events++;
1433 drvr_stats(adapter)->be_tx_compl += num_cmpl;
1437 /* As TX and MCC share the same EQ check for both TX and MCC completions.
1438 * For TX/MCC we don't honour budget; consume everything
1440 static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
1442 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1443 struct be_adapter *adapter =
1444 container_of(tx_eq, struct be_adapter, tx_eq);
1446 napi_complete(napi);
1448 be_process_tx(adapter);
1450 be_process_mcc(adapter);
1452 return 1;
1455 static void be_worker(struct work_struct *work)
1457 struct be_adapter *adapter =
1458 container_of(work, struct be_adapter, work.work);
1460 be_cmd_get_stats(adapter, &adapter->stats.cmd);
1462 /* Set EQ delay */
1463 be_rx_eqd_update(adapter);
1465 be_tx_rate_update(adapter);
1466 be_rx_rate_update(adapter);
1468 if (adapter->rx_post_starved) {
1469 adapter->rx_post_starved = false;
1470 be_post_rx_frags(adapter);
1473 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1476 static void be_msix_enable(struct be_adapter *adapter)
1478 int i, status;
1480 for (i = 0; i < BE_NUM_MSIX_VECTORS; i++)
1481 adapter->msix_entries[i].entry = i;
1483 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1484 BE_NUM_MSIX_VECTORS);
1485 if (status == 0)
1486 adapter->msix_enabled = true;
1487 return;
1490 static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1492 return adapter->msix_entries[
1493 be_evt_bit_get(adapter, eq_id)].vector;
1496 static int be_request_irq(struct be_adapter *adapter,
1497 struct be_eq_obj *eq_obj,
1498 void *handler, char *desc)
1500 struct net_device *netdev = adapter->netdev;
1501 int vec;
1503 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
1504 vec = be_msix_vec_get(adapter, eq_obj->q.id);
1505 return request_irq(vec, handler, 0, eq_obj->desc, adapter);
1508 static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj)
1510 int vec = be_msix_vec_get(adapter, eq_obj->q.id);
1511 free_irq(vec, adapter);
1514 static int be_msix_register(struct be_adapter *adapter)
1516 int status;
1518 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx");
1519 if (status)
1520 goto err;
1522 status = be_request_irq(adapter, &adapter->rx_eq, be_msix_rx, "rx");
1523 if (status)
1524 goto free_tx_irq;
1526 return 0;
1528 free_tx_irq:
1529 be_free_irq(adapter, &adapter->tx_eq);
1530 err:
1531 dev_warn(&adapter->pdev->dev,
1532 "MSIX Request IRQ failed - err %d\n", status);
1533 pci_disable_msix(adapter->pdev);
1534 adapter->msix_enabled = false;
1535 return status;
1538 static int be_irq_register(struct be_adapter *adapter)
1540 struct net_device *netdev = adapter->netdev;
1541 int status;
1543 if (adapter->msix_enabled) {
1544 status = be_msix_register(adapter);
1545 if (status == 0)
1546 goto done;
1549 /* INTx */
1550 netdev->irq = adapter->pdev->irq;
1551 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1552 adapter);
1553 if (status) {
1554 dev_err(&adapter->pdev->dev,
1555 "INTx request IRQ failed - err %d\n", status);
1556 return status;
1558 done:
1559 adapter->isr_registered = true;
1560 return 0;
1563 static void be_irq_unregister(struct be_adapter *adapter)
1565 struct net_device *netdev = adapter->netdev;
1567 if (!adapter->isr_registered)
1568 return;
1570 /* INTx */
1571 if (!adapter->msix_enabled) {
1572 free_irq(netdev->irq, adapter);
1573 goto done;
1576 /* MSIx */
1577 be_free_irq(adapter, &adapter->tx_eq);
1578 be_free_irq(adapter, &adapter->rx_eq);
1579 done:
1580 adapter->isr_registered = false;
1581 return;
1584 static int be_open(struct net_device *netdev)
1586 struct be_adapter *adapter = netdev_priv(netdev);
1587 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1588 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1589 bool link_up;
1590 int status;
1592 /* First time posting */
1593 be_post_rx_frags(adapter);
1595 napi_enable(&rx_eq->napi);
1596 napi_enable(&tx_eq->napi);
1598 be_irq_register(adapter);
1600 be_intr_set(adapter, true);
1602 /* The evt queues are created in unarmed state; arm them */
1603 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
1604 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
1606 /* Rx compl queue may be in unarmed state; rearm it */
1607 be_cq_notify(adapter, adapter->rx_obj.cq.id, true, 0);
1609 status = be_cmd_link_status_query(adapter, &link_up);
1610 if (status)
1611 return status;
1612 be_link_status_update(adapter, link_up);
1614 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
1615 return 0;
1618 static int be_setup(struct be_adapter *adapter)
1620 struct net_device *netdev = adapter->netdev;
1621 u32 if_flags;
1622 int status;
1624 if_flags = BE_IF_FLAGS_BROADCAST | BE_IF_FLAGS_PROMISCUOUS |
1625 BE_IF_FLAGS_MCAST_PROMISCUOUS | BE_IF_FLAGS_UNTAGGED |
1626 BE_IF_FLAGS_PASS_L3L4_ERRORS;
1627 status = be_cmd_if_create(adapter, if_flags, netdev->dev_addr,
1628 false/* pmac_invalid */, &adapter->if_handle,
1629 &adapter->pmac_id);
1630 if (status != 0)
1631 goto do_none;
1634 status = be_tx_queues_create(adapter);
1635 if (status != 0)
1636 goto if_destroy;
1638 status = be_rx_queues_create(adapter);
1639 if (status != 0)
1640 goto tx_qs_destroy;
1642 status = be_mcc_queues_create(adapter);
1643 if (status != 0)
1644 goto rx_qs_destroy;
1646 status = be_vid_config(adapter);
1647 if (status != 0)
1648 goto mccqs_destroy;
1650 status = be_cmd_set_flow_control(adapter, true, true);
1651 if (status != 0)
1652 goto mccqs_destroy;
1653 return 0;
1655 mccqs_destroy:
1656 be_mcc_queues_destroy(adapter);
1657 rx_qs_destroy:
1658 be_rx_queues_destroy(adapter);
1659 tx_qs_destroy:
1660 be_tx_queues_destroy(adapter);
1661 if_destroy:
1662 be_cmd_if_destroy(adapter, adapter->if_handle);
1663 do_none:
1664 return status;
1667 static int be_clear(struct be_adapter *adapter)
1669 be_mcc_queues_destroy(adapter);
1670 be_rx_queues_destroy(adapter);
1671 be_tx_queues_destroy(adapter);
1673 be_cmd_if_destroy(adapter, adapter->if_handle);
1675 return 0;
1678 static int be_close(struct net_device *netdev)
1680 struct be_adapter *adapter = netdev_priv(netdev);
1681 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1682 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1683 int vec;
1685 cancel_delayed_work_sync(&adapter->work);
1687 netif_stop_queue(netdev);
1688 netif_carrier_off(netdev);
1689 adapter->link_up = false;
1691 be_intr_set(adapter, false);
1693 if (adapter->msix_enabled) {
1694 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1695 synchronize_irq(vec);
1696 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1697 synchronize_irq(vec);
1698 } else {
1699 synchronize_irq(netdev->irq);
1701 be_irq_unregister(adapter);
1703 napi_disable(&rx_eq->napi);
1704 napi_disable(&tx_eq->napi);
1706 /* Wait for all pending tx completions to arrive so that
1707 * all tx skbs are freed.
1709 be_tx_compl_clean(adapter);
1711 return 0;
1714 #define FW_FILE_HDR_SIGN "ServerEngines Corp. "
1715 char flash_cookie[2][16] = {"*** SE FLAS",
1716 "H DIRECTORY *** "};
1717 static int be_flash_image(struct be_adapter *adapter,
1718 const struct firmware *fw,
1719 struct be_dma_mem *flash_cmd, u32 flash_type)
1721 int status;
1722 u32 flash_op, image_offset = 0, total_bytes, image_size = 0;
1723 int num_bytes;
1724 const u8 *p = fw->data;
1725 struct be_cmd_write_flashrom *req = flash_cmd->va;
1727 switch (flash_type) {
1728 case FLASHROM_TYPE_ISCSI_ACTIVE:
1729 image_offset = FLASH_iSCSI_PRIMARY_IMAGE_START;
1730 image_size = FLASH_IMAGE_MAX_SIZE;
1731 break;
1732 case FLASHROM_TYPE_ISCSI_BACKUP:
1733 image_offset = FLASH_iSCSI_BACKUP_IMAGE_START;
1734 image_size = FLASH_IMAGE_MAX_SIZE;
1735 break;
1736 case FLASHROM_TYPE_FCOE_FW_ACTIVE:
1737 image_offset = FLASH_FCoE_PRIMARY_IMAGE_START;
1738 image_size = FLASH_IMAGE_MAX_SIZE;
1739 break;
1740 case FLASHROM_TYPE_FCOE_FW_BACKUP:
1741 image_offset = FLASH_FCoE_BACKUP_IMAGE_START;
1742 image_size = FLASH_IMAGE_MAX_SIZE;
1743 break;
1744 case FLASHROM_TYPE_BIOS:
1745 image_offset = FLASH_iSCSI_BIOS_START;
1746 image_size = FLASH_BIOS_IMAGE_MAX_SIZE;
1747 break;
1748 case FLASHROM_TYPE_FCOE_BIOS:
1749 image_offset = FLASH_FCoE_BIOS_START;
1750 image_size = FLASH_BIOS_IMAGE_MAX_SIZE;
1751 break;
1752 case FLASHROM_TYPE_PXE_BIOS:
1753 image_offset = FLASH_PXE_BIOS_START;
1754 image_size = FLASH_BIOS_IMAGE_MAX_SIZE;
1755 break;
1756 default:
1757 return 0;
1760 p += sizeof(struct flash_file_hdr) + image_offset;
1761 if (p + image_size > fw->data + fw->size)
1762 return -1;
1764 total_bytes = image_size;
1766 while (total_bytes) {
1767 if (total_bytes > 32*1024)
1768 num_bytes = 32*1024;
1769 else
1770 num_bytes = total_bytes;
1771 total_bytes -= num_bytes;
1773 if (!total_bytes)
1774 flash_op = FLASHROM_OPER_FLASH;
1775 else
1776 flash_op = FLASHROM_OPER_SAVE;
1777 memcpy(req->params.data_buf, p, num_bytes);
1778 p += num_bytes;
1779 status = be_cmd_write_flashrom(adapter, flash_cmd,
1780 flash_type, flash_op, num_bytes);
1781 if (status) {
1782 dev_err(&adapter->pdev->dev,
1783 "cmd to write to flash rom failed. type/op %d/%d\n",
1784 flash_type, flash_op);
1785 return -1;
1787 yield();
1790 return 0;
1793 int be_load_fw(struct be_adapter *adapter, u8 *func)
1795 char fw_file[ETHTOOL_FLASH_MAX_FILENAME];
1796 const struct firmware *fw;
1797 struct flash_file_hdr *fhdr;
1798 struct flash_section_info *fsec = NULL;
1799 struct be_dma_mem flash_cmd;
1800 int status;
1801 const u8 *p;
1802 bool entry_found = false;
1803 int flash_type;
1804 char fw_ver[FW_VER_LEN];
1805 char fw_cfg;
1807 status = be_cmd_get_fw_ver(adapter, fw_ver);
1808 if (status)
1809 return status;
1811 fw_cfg = *(fw_ver + 2);
1812 if (fw_cfg == '0')
1813 fw_cfg = '1';
1814 strcpy(fw_file, func);
1816 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
1817 if (status)
1818 goto fw_exit;
1820 p = fw->data;
1821 fhdr = (struct flash_file_hdr *) p;
1822 if (memcmp(fhdr->sign, FW_FILE_HDR_SIGN, strlen(FW_FILE_HDR_SIGN))) {
1823 dev_err(&adapter->pdev->dev,
1824 "Firmware(%s) load error (signature did not match)\n",
1825 fw_file);
1826 status = -1;
1827 goto fw_exit;
1830 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
1832 p += sizeof(struct flash_file_hdr);
1833 while (p < (fw->data + fw->size)) {
1834 fsec = (struct flash_section_info *)p;
1835 if (!memcmp(flash_cookie, fsec->cookie, sizeof(flash_cookie))) {
1836 entry_found = true;
1837 break;
1839 p += 32;
1842 if (!entry_found) {
1843 status = -1;
1844 dev_err(&adapter->pdev->dev,
1845 "Flash cookie not found in firmware image\n");
1846 goto fw_exit;
1849 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
1850 flash_cmd.va = pci_alloc_consistent(adapter->pdev, flash_cmd.size,
1851 &flash_cmd.dma);
1852 if (!flash_cmd.va) {
1853 status = -ENOMEM;
1854 dev_err(&adapter->pdev->dev,
1855 "Memory allocation failure while flashing\n");
1856 goto fw_exit;
1859 for (flash_type = FLASHROM_TYPE_ISCSI_ACTIVE;
1860 flash_type <= FLASHROM_TYPE_FCOE_FW_BACKUP; flash_type++) {
1861 status = be_flash_image(adapter, fw, &flash_cmd,
1862 flash_type);
1863 if (status)
1864 break;
1867 pci_free_consistent(adapter->pdev, flash_cmd.size, flash_cmd.va,
1868 flash_cmd.dma);
1869 if (status) {
1870 dev_err(&adapter->pdev->dev, "Firmware load error\n");
1871 goto fw_exit;
1874 dev_info(&adapter->pdev->dev, "Firmware flashed succesfully\n");
1876 fw_exit:
1877 release_firmware(fw);
1878 return status;
1881 static struct net_device_ops be_netdev_ops = {
1882 .ndo_open = be_open,
1883 .ndo_stop = be_close,
1884 .ndo_start_xmit = be_xmit,
1885 .ndo_get_stats = be_get_stats,
1886 .ndo_set_rx_mode = be_set_multicast_list,
1887 .ndo_set_mac_address = be_mac_addr_set,
1888 .ndo_change_mtu = be_change_mtu,
1889 .ndo_validate_addr = eth_validate_addr,
1890 .ndo_vlan_rx_register = be_vlan_register,
1891 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
1892 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
1895 static void be_netdev_init(struct net_device *netdev)
1897 struct be_adapter *adapter = netdev_priv(netdev);
1899 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
1900 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_HW_CSUM |
1901 NETIF_F_GRO;
1903 netdev->flags |= IFF_MULTICAST;
1905 adapter->rx_csum = true;
1907 netif_set_gso_max_size(netdev, 65535);
1909 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
1911 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
1913 netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx,
1914 BE_NAPI_WEIGHT);
1915 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
1916 BE_NAPI_WEIGHT);
1918 netif_carrier_off(netdev);
1919 netif_stop_queue(netdev);
1922 static void be_unmap_pci_bars(struct be_adapter *adapter)
1924 if (adapter->csr)
1925 iounmap(adapter->csr);
1926 if (adapter->db)
1927 iounmap(adapter->db);
1928 if (adapter->pcicfg)
1929 iounmap(adapter->pcicfg);
1932 static int be_map_pci_bars(struct be_adapter *adapter)
1934 u8 __iomem *addr;
1936 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
1937 pci_resource_len(adapter->pdev, 2));
1938 if (addr == NULL)
1939 return -ENOMEM;
1940 adapter->csr = addr;
1942 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 4),
1943 128 * 1024);
1944 if (addr == NULL)
1945 goto pci_map_err;
1946 adapter->db = addr;
1948 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 1),
1949 pci_resource_len(adapter->pdev, 1));
1950 if (addr == NULL)
1951 goto pci_map_err;
1952 adapter->pcicfg = addr;
1954 return 0;
1955 pci_map_err:
1956 be_unmap_pci_bars(adapter);
1957 return -ENOMEM;
1961 static void be_ctrl_cleanup(struct be_adapter *adapter)
1963 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
1965 be_unmap_pci_bars(adapter);
1967 if (mem->va)
1968 pci_free_consistent(adapter->pdev, mem->size,
1969 mem->va, mem->dma);
1972 static int be_ctrl_init(struct be_adapter *adapter)
1974 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
1975 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
1976 int status;
1978 status = be_map_pci_bars(adapter);
1979 if (status)
1980 return status;
1982 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
1983 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
1984 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
1985 if (!mbox_mem_alloc->va) {
1986 be_unmap_pci_bars(adapter);
1987 return -1;
1989 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
1990 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
1991 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
1992 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
1993 spin_lock_init(&adapter->mbox_lock);
1994 spin_lock_init(&adapter->mcc_lock);
1995 spin_lock_init(&adapter->mcc_cq_lock);
1997 return 0;
2000 static void be_stats_cleanup(struct be_adapter *adapter)
2002 struct be_stats_obj *stats = &adapter->stats;
2003 struct be_dma_mem *cmd = &stats->cmd;
2005 if (cmd->va)
2006 pci_free_consistent(adapter->pdev, cmd->size,
2007 cmd->va, cmd->dma);
2010 static int be_stats_init(struct be_adapter *adapter)
2012 struct be_stats_obj *stats = &adapter->stats;
2013 struct be_dma_mem *cmd = &stats->cmd;
2015 cmd->size = sizeof(struct be_cmd_req_get_stats);
2016 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
2017 if (cmd->va == NULL)
2018 return -1;
2019 return 0;
2022 static void __devexit be_remove(struct pci_dev *pdev)
2024 struct be_adapter *adapter = pci_get_drvdata(pdev);
2025 if (!adapter)
2026 return;
2028 unregister_netdev(adapter->netdev);
2030 be_clear(adapter);
2032 be_stats_cleanup(adapter);
2034 be_ctrl_cleanup(adapter);
2036 if (adapter->msix_enabled) {
2037 pci_disable_msix(adapter->pdev);
2038 adapter->msix_enabled = false;
2041 pci_set_drvdata(pdev, NULL);
2042 pci_release_regions(pdev);
2043 pci_disable_device(pdev);
2045 free_netdev(adapter->netdev);
2048 static int be_hw_up(struct be_adapter *adapter)
2050 int status;
2052 status = be_cmd_POST(adapter);
2053 if (status)
2054 return status;
2056 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
2057 if (status)
2058 return status;
2060 status = be_cmd_query_fw_cfg(adapter,
2061 &adapter->port_num, &adapter->cap);
2062 return status;
2065 static int __devinit be_probe(struct pci_dev *pdev,
2066 const struct pci_device_id *pdev_id)
2068 int status = 0;
2069 struct be_adapter *adapter;
2070 struct net_device *netdev;
2071 u8 mac[ETH_ALEN];
2073 status = pci_enable_device(pdev);
2074 if (status)
2075 goto do_none;
2077 status = pci_request_regions(pdev, DRV_NAME);
2078 if (status)
2079 goto disable_dev;
2080 pci_set_master(pdev);
2082 netdev = alloc_etherdev(sizeof(struct be_adapter));
2083 if (netdev == NULL) {
2084 status = -ENOMEM;
2085 goto rel_reg;
2087 adapter = netdev_priv(netdev);
2088 adapter->pdev = pdev;
2089 pci_set_drvdata(pdev, adapter);
2090 adapter->netdev = netdev;
2092 be_msix_enable(adapter);
2094 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2095 if (!status) {
2096 netdev->features |= NETIF_F_HIGHDMA;
2097 } else {
2098 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2099 if (status) {
2100 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
2101 goto free_netdev;
2105 status = be_ctrl_init(adapter);
2106 if (status)
2107 goto free_netdev;
2109 status = be_cmd_reset_function(adapter);
2110 if (status)
2111 goto ctrl_clean;
2113 status = be_stats_init(adapter);
2114 if (status)
2115 goto ctrl_clean;
2117 status = be_hw_up(adapter);
2118 if (status)
2119 goto stats_clean;
2121 status = be_cmd_mac_addr_query(adapter, mac, MAC_ADDRESS_TYPE_NETWORK,
2122 true /* permanent */, 0);
2123 if (status)
2124 goto stats_clean;
2125 memcpy(netdev->dev_addr, mac, ETH_ALEN);
2127 INIT_DELAYED_WORK(&adapter->work, be_worker);
2128 be_netdev_init(netdev);
2129 SET_NETDEV_DEV(netdev, &adapter->pdev->dev);
2131 status = be_setup(adapter);
2132 if (status)
2133 goto stats_clean;
2134 status = register_netdev(netdev);
2135 if (status != 0)
2136 goto unsetup;
2138 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
2139 return 0;
2141 unsetup:
2142 be_clear(adapter);
2143 stats_clean:
2144 be_stats_cleanup(adapter);
2145 ctrl_clean:
2146 be_ctrl_cleanup(adapter);
2147 free_netdev:
2148 free_netdev(adapter->netdev);
2149 rel_reg:
2150 pci_release_regions(pdev);
2151 disable_dev:
2152 pci_disable_device(pdev);
2153 do_none:
2154 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
2155 return status;
2158 static int be_suspend(struct pci_dev *pdev, pm_message_t state)
2160 struct be_adapter *adapter = pci_get_drvdata(pdev);
2161 struct net_device *netdev = adapter->netdev;
2163 netif_device_detach(netdev);
2164 if (netif_running(netdev)) {
2165 rtnl_lock();
2166 be_close(netdev);
2167 rtnl_unlock();
2169 be_clear(adapter);
2171 pci_save_state(pdev);
2172 pci_disable_device(pdev);
2173 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2174 return 0;
2177 static int be_resume(struct pci_dev *pdev)
2179 int status = 0;
2180 struct be_adapter *adapter = pci_get_drvdata(pdev);
2181 struct net_device *netdev = adapter->netdev;
2183 netif_device_detach(netdev);
2185 status = pci_enable_device(pdev);
2186 if (status)
2187 return status;
2189 pci_set_power_state(pdev, 0);
2190 pci_restore_state(pdev);
2192 be_setup(adapter);
2193 if (netif_running(netdev)) {
2194 rtnl_lock();
2195 be_open(netdev);
2196 rtnl_unlock();
2198 netif_device_attach(netdev);
2199 return 0;
2202 static struct pci_driver be_driver = {
2203 .name = DRV_NAME,
2204 .id_table = be_dev_ids,
2205 .probe = be_probe,
2206 .remove = be_remove,
2207 .suspend = be_suspend,
2208 .resume = be_resume
2211 static int __init be_init_module(void)
2213 if (rx_frag_size != 8192 && rx_frag_size != 4096
2214 && rx_frag_size != 2048) {
2215 printk(KERN_WARNING DRV_NAME
2216 " : Module param rx_frag_size must be 2048/4096/8192."
2217 " Using 2048\n");
2218 rx_frag_size = 2048;
2221 return pci_register_driver(&be_driver);
2223 module_init(be_init_module);
2225 static void __exit be_exit_module(void)
2227 pci_unregister_driver(&be_driver);
2229 module_exit(be_exit_module);