ixgbe: Add FCoE feature header to 82599
[linux-2.6] / drivers / net / benet / be_main.c
1 /*
2  * Copyright (C) 2005 - 2009 ServerEngines
3  * All rights reserved.
4  *
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.
9  *
10  * Contact Information:
11  * linux-drivers@serverengines.com
12  *
13  * ServerEngines
14  * 209 N. Fair Oaks Ave
15  * Sunnyvale, CA 94085
16  */
17
18 #include "be.h"
19 #include <asm/div64.h>
20
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");
26
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.");
30
31 #define BE_VENDOR_ID            0x19a2
32 #define BE2_DEVICE_ID_1         0x0211
33 static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
34         { PCI_DEVICE(BE_VENDOR_ID, BE2_DEVICE_ID_1) },
35         { 0 }
36 };
37 MODULE_DEVICE_TABLE(pci, be_dev_ids);
38
39 static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
40 {
41         struct be_dma_mem *mem = &q->dma_mem;
42         if (mem->va)
43                 pci_free_consistent(adapter->pdev, mem->size,
44                         mem->va, mem->dma);
45 }
46
47 static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
48                 u16 len, u16 entry_size)
49 {
50         struct be_dma_mem *mem = &q->dma_mem;
51
52         memset(q, 0, sizeof(*q));
53         q->len = len;
54         q->entry_size = entry_size;
55         mem->size = len * entry_size;
56         mem->va = pci_alloc_consistent(adapter->pdev, mem->size, &mem->dma);
57         if (!mem->va)
58                 return -1;
59         memset(mem->va, 0, mem->size);
60         return 0;
61 }
62
63 static inline void *queue_head_node(struct be_queue_info *q)
64 {
65         return q->dma_mem.va + q->head * q->entry_size;
66 }
67
68 static inline void *queue_tail_node(struct be_queue_info *q)
69 {
70         return q->dma_mem.va + q->tail * q->entry_size;
71 }
72
73 static inline void queue_head_inc(struct be_queue_info *q)
74 {
75         index_inc(&q->head, q->len);
76 }
77
78 static inline void queue_tail_inc(struct be_queue_info *q)
79 {
80         index_inc(&q->tail, q->len);
81 }
82
83 static void be_intr_set(struct be_ctrl_info *ctrl, bool enable)
84 {
85         u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
86         u32 reg = ioread32(addr);
87         u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
88         if (!enabled && enable) {
89                 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
90         } else if (enabled && !enable) {
91                 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
92         } else {
93                 printk(KERN_WARNING DRV_NAME
94                         ": bad value in membar_int_ctrl reg=0x%x\n", reg);
95                 return;
96         }
97         iowrite32(reg, addr);
98 }
99
100 static void be_rxq_notify(struct be_ctrl_info *ctrl, u16 qid, u16 posted)
101 {
102         u32 val = 0;
103         val |= qid & DB_RQ_RING_ID_MASK;
104         val |= posted << DB_RQ_NUM_POSTED_SHIFT;
105         iowrite32(val, ctrl->db + DB_RQ_OFFSET);
106 }
107
108 static void be_txq_notify(struct be_ctrl_info *ctrl, u16 qid, u16 posted)
109 {
110         u32 val = 0;
111         val |= qid & DB_TXULP_RING_ID_MASK;
112         val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
113         iowrite32(val, ctrl->db + DB_TXULP1_OFFSET);
114 }
115
116 static void be_eq_notify(struct be_ctrl_info *ctrl, u16 qid,
117                 bool arm, bool clear_int, u16 num_popped)
118 {
119         u32 val = 0;
120         val |= qid & DB_EQ_RING_ID_MASK;
121         if (arm)
122                 val |= 1 << DB_EQ_REARM_SHIFT;
123         if (clear_int)
124                 val |= 1 << DB_EQ_CLR_SHIFT;
125         val |= 1 << DB_EQ_EVNT_SHIFT;
126         val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
127         iowrite32(val, ctrl->db + DB_EQ_OFFSET);
128 }
129
130 static void be_cq_notify(struct be_ctrl_info *ctrl, u16 qid,
131                 bool arm, u16 num_popped)
132 {
133         u32 val = 0;
134         val |= qid & DB_CQ_RING_ID_MASK;
135         if (arm)
136                 val |= 1 << DB_CQ_REARM_SHIFT;
137         val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
138         iowrite32(val, ctrl->db + DB_CQ_OFFSET);
139 }
140
141
142 static int be_mac_addr_set(struct net_device *netdev, void *p)
143 {
144         struct be_adapter *adapter = netdev_priv(netdev);
145         struct sockaddr *addr = p;
146         int status = 0;
147
148         if (netif_running(netdev)) {
149                 status = be_cmd_pmac_del(&adapter->ctrl, adapter->if_handle,
150                                 adapter->pmac_id);
151                 if (status)
152                         return status;
153
154                 status = be_cmd_pmac_add(&adapter->ctrl, (u8 *)addr->sa_data,
155                                 adapter->if_handle, &adapter->pmac_id);
156         }
157
158         if (!status)
159                 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
160
161         return status;
162 }
163
164 static void netdev_stats_update(struct be_adapter *adapter)
165 {
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;
171
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;
179
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;
185
186         /*  packet transmit problems */
187         dev_stats->tx_errors = 0;
188
189         /*  no space in linux buffers */
190         dev_stats->rx_dropped = 0;
191
192         /* no space available in linux */
193         dev_stats->tx_dropped = 0;
194
195         dev_stats->multicast = port_stats->tx_multicastframes;
196         dev_stats->collisions = 0;
197
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;
204
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;
220 }
221
222 static void be_link_status_update(struct be_adapter *adapter)
223 {
224         struct be_link_info *prev = &adapter->link;
225         struct be_link_info now = { 0 };
226         struct net_device *netdev = adapter->netdev;
227
228         be_cmd_link_status_query(&adapter->ctrl, &now);
229
230         /* If link came up or went down */
231         if (now.speed != prev->speed && (now.speed == PHY_LINK_SPEED_ZERO ||
232                         prev->speed == PHY_LINK_SPEED_ZERO)) {
233                 if (now.speed == PHY_LINK_SPEED_ZERO) {
234                         netif_stop_queue(netdev);
235                         netif_carrier_off(netdev);
236                         printk(KERN_INFO "%s: Link down\n", netdev->name);
237                 } else {
238                         netif_start_queue(netdev);
239                         netif_carrier_on(netdev);
240                         printk(KERN_INFO "%s: Link up\n", netdev->name);
241                 }
242         }
243         *prev = now;
244 }
245
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)
248 {
249         struct be_ctrl_info *ctrl = &adapter->ctrl;
250         struct be_eq_obj *rx_eq = &adapter->rx_eq;
251         struct be_drvr_stats *stats = &adapter->stats.drvr_stats;
252         ulong now = jiffies;
253         u32 eqd;
254
255         if (!rx_eq->enable_aic)
256                 return;
257
258         /* Wrapped around */
259         if (time_before(now, stats->rx_fps_jiffies)) {
260                 stats->rx_fps_jiffies = now;
261                 return;
262         }
263
264         /* Update once a second */
265         if ((now - stats->rx_fps_jiffies) < HZ)
266                 return;
267
268         stats->be_rx_fps = (stats->be_rx_frags - stats->be_prev_rx_frags) /
269                         ((now - stats->rx_fps_jiffies) / HZ);
270
271         stats->rx_fps_jiffies = now;
272         stats->be_prev_rx_frags = stats->be_rx_frags;
273         eqd = stats->be_rx_fps / 110000;
274         eqd = eqd << 3;
275         if (eqd > rx_eq->max_eqd)
276                 eqd = rx_eq->max_eqd;
277         if (eqd < rx_eq->min_eqd)
278                 eqd = rx_eq->min_eqd;
279         if (eqd < 10)
280                 eqd = 0;
281         if (eqd != rx_eq->cur_eqd)
282                 be_cmd_modify_eqd(ctrl, rx_eq->q.id, eqd);
283
284         rx_eq->cur_eqd = eqd;
285 }
286
287 static struct net_device_stats *be_get_stats(struct net_device *dev)
288 {
289         struct be_adapter *adapter = netdev_priv(dev);
290
291         return &adapter->stats.net_stats;
292 }
293
294 static u32 be_calc_rate(u64 bytes, unsigned long ticks)
295 {
296         u64 rate = bytes;
297
298         do_div(rate, ticks / HZ);
299         rate <<= 3;                     /* bytes/sec -> bits/sec */
300         do_div(rate, 1000000ul);        /* MB/Sec */
301
302         return rate;
303 }
304
305 static void be_tx_rate_update(struct be_adapter *adapter)
306 {
307         struct be_drvr_stats *stats = drvr_stats(adapter);
308         ulong now = jiffies;
309
310         /* Wrapped around? */
311         if (time_before(now, stats->be_tx_jiffies)) {
312                 stats->be_tx_jiffies = now;
313                 return;
314         }
315
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;
323         }
324 }
325
326 static void be_tx_stats_update(struct be_adapter *adapter,
327                         u32 wrb_cnt, u32 copied, bool stopped)
328 {
329         struct be_drvr_stats *stats = drvr_stats(adapter);
330         stats->be_tx_reqs++;
331         stats->be_tx_wrbs += wrb_cnt;
332         stats->be_tx_bytes += copied;
333         if (stopped)
334                 stats->be_tx_stops++;
335 }
336
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)
339 {
340         int cnt = 0;
341         while (skb) {
342                 if (skb->len > skb->data_len)
343                         cnt++;
344                 cnt += skb_shinfo(skb)->nr_frags;
345                 skb = skb_shinfo(skb)->frag_list;
346         }
347         /* to account for hdr wrb */
348         cnt++;
349         if (cnt & 1) {
350                 /* add a dummy to make it an even num */
351                 cnt++;
352                 *dummy = true;
353         } else
354                 *dummy = false;
355         BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
356         return cnt;
357 }
358
359 static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
360 {
361         wrb->frag_pa_hi = upper_32_bits(addr);
362         wrb->frag_pa_lo = addr & 0xFFFFFFFF;
363         wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
364 }
365
366 static void wrb_fill_hdr(struct be_eth_hdr_wrb *hdr, struct sk_buff *skb,
367                 bool vlan, u32 wrb_cnt, u32 len)
368 {
369         memset(hdr, 0, sizeof(*hdr));
370
371         AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
372
373         if (skb_shinfo(skb)->gso_segs > 1 && skb_shinfo(skb)->gso_size) {
374                 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
375                 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
376                         hdr, skb_shinfo(skb)->gso_size);
377         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
378                 if (is_tcp_pkt(skb))
379                         AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
380                 else if (is_udp_pkt(skb))
381                         AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
382         }
383
384         if (vlan && vlan_tx_tag_present(skb)) {
385                 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
386                 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag,
387                         hdr, vlan_tx_tag_get(skb));
388         }
389
390         AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
391         AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
392         AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
393         AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
394 }
395
396
397 static int make_tx_wrbs(struct be_adapter *adapter,
398                 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
399 {
400         u64 busaddr;
401         u32 i, copied = 0;
402         struct pci_dev *pdev = adapter->pdev;
403         struct sk_buff *first_skb = skb;
404         struct be_queue_info *txq = &adapter->tx_obj.q;
405         struct be_eth_wrb *wrb;
406         struct be_eth_hdr_wrb *hdr;
407
408         atomic_add(wrb_cnt, &txq->used);
409         hdr = queue_head_node(txq);
410         queue_head_inc(txq);
411
412         while (skb) {
413                 if (skb->len > skb->data_len) {
414                         int len = skb->len - skb->data_len;
415                         busaddr = pci_map_single(pdev, skb->data, len,
416                                         PCI_DMA_TODEVICE);
417                         wrb = queue_head_node(txq);
418                         wrb_fill(wrb, busaddr, len);
419                         be_dws_cpu_to_le(wrb, sizeof(*wrb));
420                         queue_head_inc(txq);
421                         copied += len;
422                 }
423
424                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
425                         struct skb_frag_struct *frag =
426                                 &skb_shinfo(skb)->frags[i];
427                         busaddr = pci_map_page(pdev, frag->page,
428                                         frag->page_offset,
429                                         frag->size, PCI_DMA_TODEVICE);
430                         wrb = queue_head_node(txq);
431                         wrb_fill(wrb, busaddr, frag->size);
432                         be_dws_cpu_to_le(wrb, sizeof(*wrb));
433                         queue_head_inc(txq);
434                         copied += frag->size;
435                 }
436                 skb = skb_shinfo(skb)->frag_list;
437         }
438
439         if (dummy_wrb) {
440                 wrb = queue_head_node(txq);
441                 wrb_fill(wrb, 0, 0);
442                 be_dws_cpu_to_le(wrb, sizeof(*wrb));
443                 queue_head_inc(txq);
444         }
445
446         wrb_fill_hdr(hdr, first_skb, adapter->vlan_grp ? true : false,
447                 wrb_cnt, copied);
448         be_dws_cpu_to_le(hdr, sizeof(*hdr));
449
450         return copied;
451 }
452
453 static int be_xmit(struct sk_buff *skb, struct net_device *netdev)
454 {
455         struct be_adapter *adapter = netdev_priv(netdev);
456         struct be_tx_obj *tx_obj = &adapter->tx_obj;
457         struct be_queue_info *txq = &tx_obj->q;
458         u32 wrb_cnt = 0, copied = 0;
459         u32 start = txq->head;
460         bool dummy_wrb, stopped = false;
461
462         wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb);
463
464         copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb);
465
466         /* record the sent skb in the sent_skb table */
467         BUG_ON(tx_obj->sent_skb_list[start]);
468         tx_obj->sent_skb_list[start] = skb;
469
470         /* Ensure that txq has space for the next skb; Else stop the queue
471          * *BEFORE* ringing the tx doorbell, so that we serialze the
472          * tx compls of the current transmit which'll wake up the queue
473          */
474         if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >= txq->len) {
475                 netif_stop_queue(netdev);
476                 stopped = true;
477         }
478
479         be_txq_notify(&adapter->ctrl, txq->id, wrb_cnt);
480
481         netdev->trans_start = jiffies;
482
483         be_tx_stats_update(adapter, wrb_cnt, copied, stopped);
484         return NETDEV_TX_OK;
485 }
486
487 static int be_change_mtu(struct net_device *netdev, int new_mtu)
488 {
489         struct be_adapter *adapter = netdev_priv(netdev);
490         if (new_mtu < BE_MIN_MTU ||
491                         new_mtu > BE_MAX_JUMBO_FRAME_SIZE) {
492                 dev_info(&adapter->pdev->dev,
493                         "MTU must be between %d and %d bytes\n",
494                         BE_MIN_MTU, BE_MAX_JUMBO_FRAME_SIZE);
495                 return -EINVAL;
496         }
497         dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
498                         netdev->mtu, new_mtu);
499         netdev->mtu = new_mtu;
500         return 0;
501 }
502
503 /*
504  * if there are BE_NUM_VLANS_SUPPORTED or lesser number of VLANS configured,
505  * program them in BE.  If more than BE_NUM_VLANS_SUPPORTED are configured,
506  * set the BE in promiscuous VLAN mode.
507  */
508 static void be_vid_config(struct net_device *netdev)
509 {
510         struct be_adapter *adapter = netdev_priv(netdev);
511         u16 vtag[BE_NUM_VLANS_SUPPORTED];
512         u16 ntags = 0, i;
513
514         if (adapter->num_vlans <= BE_NUM_VLANS_SUPPORTED)  {
515                 /* Construct VLAN Table to give to HW */
516                 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
517                         if (adapter->vlan_tag[i]) {
518                                 vtag[ntags] = cpu_to_le16(i);
519                                 ntags++;
520                         }
521                 }
522                 be_cmd_vlan_config(&adapter->ctrl, adapter->if_handle,
523                         vtag, ntags, 1, 0);
524         } else {
525                 be_cmd_vlan_config(&adapter->ctrl, adapter->if_handle,
526                         NULL, 0, 1, 1);
527         }
528 }
529
530 static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
531 {
532         struct be_adapter *adapter = netdev_priv(netdev);
533         struct be_eq_obj *rx_eq = &adapter->rx_eq;
534         struct be_eq_obj *tx_eq = &adapter->tx_eq;
535         struct be_ctrl_info *ctrl = &adapter->ctrl;
536
537         be_eq_notify(ctrl, rx_eq->q.id, false, false, 0);
538         be_eq_notify(ctrl, tx_eq->q.id, false, false, 0);
539         adapter->vlan_grp = grp;
540         be_eq_notify(ctrl, rx_eq->q.id, true, false, 0);
541         be_eq_notify(ctrl, tx_eq->q.id, true, false, 0);
542 }
543
544 static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
545 {
546         struct be_adapter *adapter = netdev_priv(netdev);
547
548         adapter->num_vlans++;
549         adapter->vlan_tag[vid] = 1;
550
551         be_vid_config(netdev);
552 }
553
554 static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
555 {
556         struct be_adapter *adapter = netdev_priv(netdev);
557
558         adapter->num_vlans--;
559         adapter->vlan_tag[vid] = 0;
560
561         vlan_group_set_device(adapter->vlan_grp, vid, NULL);
562         be_vid_config(netdev);
563 }
564
565 static void be_set_multicast_filter(struct net_device *netdev)
566 {
567         struct be_adapter *adapter = netdev_priv(netdev);
568         struct dev_mc_list *mc_ptr;
569         u8 mac_addr[32][ETH_ALEN];
570         int i = 0;
571
572         if (netdev->flags & IFF_ALLMULTI) {
573                 /* set BE in Multicast promiscuous */
574                 be_cmd_mcast_mac_set(&adapter->ctrl,
575                                         adapter->if_handle, NULL, 0, true);
576                 return;
577         }
578
579         for (mc_ptr = netdev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
580                 memcpy(&mac_addr[i][0], mc_ptr->dmi_addr, ETH_ALEN);
581                 if (++i >= 32) {
582                         be_cmd_mcast_mac_set(&adapter->ctrl,
583                                 adapter->if_handle, &mac_addr[0][0], i, false);
584                         i = 0;
585                 }
586
587         }
588
589         if (i) {
590                 /* reset the promiscuous mode also. */
591                 be_cmd_mcast_mac_set(&adapter->ctrl,
592                         adapter->if_handle, &mac_addr[0][0], i, false);
593         }
594 }
595
596 static void be_set_multicast_list(struct net_device *netdev)
597 {
598         struct be_adapter *adapter = netdev_priv(netdev);
599
600         if (netdev->flags & IFF_PROMISC) {
601                 be_cmd_promiscuous_config(&adapter->ctrl, adapter->port_num, 1);
602         } else {
603                 be_cmd_promiscuous_config(&adapter->ctrl, adapter->port_num, 0);
604                 be_set_multicast_filter(netdev);
605         }
606 }
607
608 static void be_rx_rate_update(struct be_adapter *adapter)
609 {
610         struct be_drvr_stats *stats = drvr_stats(adapter);
611         ulong now = jiffies;
612
613         /* Wrapped around */
614         if (time_before(now, stats->be_rx_jiffies)) {
615                 stats->be_rx_jiffies = now;
616                 return;
617         }
618
619         /* Update the rate once in two seconds */
620         if ((now - stats->be_rx_jiffies) < 2 * HZ)
621                 return;
622
623         stats->be_rx_rate = be_calc_rate(stats->be_rx_bytes
624                                           - stats->be_rx_bytes_prev,
625                                          now - stats->be_rx_jiffies);
626         stats->be_rx_jiffies = now;
627         stats->be_rx_bytes_prev = stats->be_rx_bytes;
628 }
629
630 static void be_rx_stats_update(struct be_adapter *adapter,
631                 u32 pktsize, u16 numfrags)
632 {
633         struct be_drvr_stats *stats = drvr_stats(adapter);
634
635         stats->be_rx_compl++;
636         stats->be_rx_frags += numfrags;
637         stats->be_rx_bytes += pktsize;
638 }
639
640 static inline bool do_pkt_csum(struct be_eth_rx_compl *rxcp, bool cso)
641 {
642         u8 l4_cksm, ip_version, ipcksm, tcpf = 0, udpf = 0, ipv6_chk;
643
644         l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp);
645         ipcksm = AMAP_GET_BITS(struct amap_eth_rx_compl, ipcksm, rxcp);
646         ip_version = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
647         if (ip_version) {
648                 tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
649                 udpf = AMAP_GET_BITS(struct amap_eth_rx_compl, udpf, rxcp);
650         }
651         ipv6_chk = (ip_version && (tcpf || udpf));
652
653         return ((l4_cksm && ipv6_chk && ipcksm) && cso) ? false : true;
654 }
655
656 static struct be_rx_page_info *
657 get_rx_page_info(struct be_adapter *adapter, u16 frag_idx)
658 {
659         struct be_rx_page_info *rx_page_info;
660         struct be_queue_info *rxq = &adapter->rx_obj.q;
661
662         rx_page_info = &adapter->rx_obj.page_info_tbl[frag_idx];
663         BUG_ON(!rx_page_info->page);
664
665         if (rx_page_info->last_page_user)
666                 pci_unmap_page(adapter->pdev, pci_unmap_addr(rx_page_info, bus),
667                         adapter->big_page_size, PCI_DMA_FROMDEVICE);
668
669         atomic_dec(&rxq->used);
670         return rx_page_info;
671 }
672
673 /* Throwaway the data in the Rx completion */
674 static void be_rx_compl_discard(struct be_adapter *adapter,
675                         struct be_eth_rx_compl *rxcp)
676 {
677         struct be_queue_info *rxq = &adapter->rx_obj.q;
678         struct be_rx_page_info *page_info;
679         u16 rxq_idx, i, num_rcvd;
680
681         rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
682         num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
683
684         for (i = 0; i < num_rcvd; i++) {
685                 page_info = get_rx_page_info(adapter, rxq_idx);
686                 put_page(page_info->page);
687                 memset(page_info, 0, sizeof(*page_info));
688                 index_inc(&rxq_idx, rxq->len);
689         }
690 }
691
692 /*
693  * skb_fill_rx_data forms a complete skb for an ether frame
694  * indicated by rxcp.
695  */
696 static void skb_fill_rx_data(struct be_adapter *adapter,
697                         struct sk_buff *skb, struct be_eth_rx_compl *rxcp)
698 {
699         struct be_queue_info *rxq = &adapter->rx_obj.q;
700         struct be_rx_page_info *page_info;
701         u16 rxq_idx, i, num_rcvd;
702         u32 pktsize, hdr_len, curr_frag_len;
703         u8 *start;
704
705         rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
706         pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
707         num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
708
709         page_info = get_rx_page_info(adapter, rxq_idx);
710
711         start = page_address(page_info->page) + page_info->page_offset;
712         prefetch(start);
713
714         /* Copy data in the first descriptor of this completion */
715         curr_frag_len = min(pktsize, rx_frag_size);
716
717         /* Copy the header portion into skb_data */
718         hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
719         memcpy(skb->data, start, hdr_len);
720         skb->len = curr_frag_len;
721         if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
722                 /* Complete packet has now been moved to data */
723                 put_page(page_info->page);
724                 skb->data_len = 0;
725                 skb->tail += curr_frag_len;
726         } else {
727                 skb_shinfo(skb)->nr_frags = 1;
728                 skb_shinfo(skb)->frags[0].page = page_info->page;
729                 skb_shinfo(skb)->frags[0].page_offset =
730                                         page_info->page_offset + hdr_len;
731                 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
732                 skb->data_len = curr_frag_len - hdr_len;
733                 skb->tail += hdr_len;
734         }
735         memset(page_info, 0, sizeof(*page_info));
736
737         if (pktsize <= rx_frag_size) {
738                 BUG_ON(num_rcvd != 1);
739                 return;
740         }
741
742         /* More frags present for this completion */
743         pktsize -= curr_frag_len; /* account for above copied frag */
744         for (i = 1; i < num_rcvd; i++) {
745                 index_inc(&rxq_idx, rxq->len);
746                 page_info = get_rx_page_info(adapter, rxq_idx);
747
748                 curr_frag_len = min(pktsize, rx_frag_size);
749
750                 skb_shinfo(skb)->frags[i].page = page_info->page;
751                 skb_shinfo(skb)->frags[i].page_offset = page_info->page_offset;
752                 skb_shinfo(skb)->frags[i].size = curr_frag_len;
753                 skb->len += curr_frag_len;
754                 skb->data_len += curr_frag_len;
755                 skb_shinfo(skb)->nr_frags++;
756                 pktsize -= curr_frag_len;
757
758                 memset(page_info, 0, sizeof(*page_info));
759         }
760
761         be_rx_stats_update(adapter, pktsize, num_rcvd);
762         return;
763 }
764
765 /* Process the RX completion indicated by rxcp when LRO is disabled */
766 static void be_rx_compl_process(struct be_adapter *adapter,
767                         struct be_eth_rx_compl *rxcp)
768 {
769         struct sk_buff *skb;
770         u32 vtp, vid;
771
772         vtp = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
773
774         skb = netdev_alloc_skb(adapter->netdev, BE_HDR_LEN + NET_IP_ALIGN);
775         if (!skb) {
776                 if (net_ratelimit())
777                         dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
778                 be_rx_compl_discard(adapter, rxcp);
779                 return;
780         }
781
782         skb_reserve(skb, NET_IP_ALIGN);
783
784         skb_fill_rx_data(adapter, skb, rxcp);
785
786         if (do_pkt_csum(rxcp, adapter->rx_csum))
787                 skb->ip_summed = CHECKSUM_NONE;
788         else
789                 skb->ip_summed = CHECKSUM_UNNECESSARY;
790
791         skb->truesize = skb->len + sizeof(struct sk_buff);
792         skb->protocol = eth_type_trans(skb, adapter->netdev);
793         skb->dev = adapter->netdev;
794
795         if (vtp) {
796                 if (!adapter->vlan_grp || adapter->num_vlans == 0) {
797                         kfree_skb(skb);
798                         return;
799                 }
800                 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
801                 vid = be16_to_cpu(vid);
802                 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
803         } else {
804                 netif_receive_skb(skb);
805         }
806
807         adapter->netdev->last_rx = jiffies;
808
809         return;
810 }
811
812 /* Process the RX completion indicated by rxcp when LRO is enabled */
813 static void be_rx_compl_process_lro(struct be_adapter *adapter,
814                         struct be_eth_rx_compl *rxcp)
815 {
816         struct be_rx_page_info *page_info;
817         struct skb_frag_struct rx_frags[BE_MAX_FRAGS_PER_FRAME];
818         struct be_queue_info *rxq = &adapter->rx_obj.q;
819         u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
820         u16 i, rxq_idx = 0, vid;
821
822         num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
823         pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
824         vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
825         rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
826
827         remaining = pkt_size;
828         for (i = 0; i < num_rcvd; i++) {
829                 page_info = get_rx_page_info(adapter, rxq_idx);
830
831                 curr_frag_len = min(remaining, rx_frag_size);
832
833                 rx_frags[i].page = page_info->page;
834                 rx_frags[i].page_offset = page_info->page_offset;
835                 rx_frags[i].size = curr_frag_len;
836                 remaining -= curr_frag_len;
837
838                 index_inc(&rxq_idx, rxq->len);
839
840                 memset(page_info, 0, sizeof(*page_info));
841         }
842
843         if (likely(!vlanf)) {
844                 lro_receive_frags(&adapter->rx_obj.lro_mgr, rx_frags, pkt_size,
845                                 pkt_size, NULL, 0);
846         } else {
847                 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
848                 vid = be16_to_cpu(vid);
849
850                 if (!adapter->vlan_grp || adapter->num_vlans == 0)
851                         return;
852
853                 lro_vlan_hwaccel_receive_frags(&adapter->rx_obj.lro_mgr,
854                         rx_frags, pkt_size, pkt_size, adapter->vlan_grp,
855                         vid, NULL, 0);
856         }
857
858         be_rx_stats_update(adapter, pkt_size, num_rcvd);
859         return;
860 }
861
862 static struct be_eth_rx_compl *be_rx_compl_get(struct be_adapter *adapter)
863 {
864         struct be_eth_rx_compl *rxcp = queue_tail_node(&adapter->rx_obj.cq);
865
866         if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
867                 return NULL;
868
869         be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
870
871         rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
872
873         queue_tail_inc(&adapter->rx_obj.cq);
874         return rxcp;
875 }
876
877 static inline struct page *be_alloc_pages(u32 size)
878 {
879         gfp_t alloc_flags = GFP_ATOMIC;
880         u32 order = get_order(size);
881         if (order > 0)
882                 alloc_flags |= __GFP_COMP;
883         return  alloc_pages(alloc_flags, order);
884 }
885
886 /*
887  * Allocate a page, split it to fragments of size rx_frag_size and post as
888  * receive buffers to BE
889  */
890 static void be_post_rx_frags(struct be_adapter *adapter)
891 {
892         struct be_rx_page_info *page_info_tbl = adapter->rx_obj.page_info_tbl;
893         struct be_rx_page_info *page_info = NULL;
894         struct be_queue_info *rxq = &adapter->rx_obj.q;
895         struct page *pagep = NULL;
896         struct be_eth_rx_d *rxd;
897         u64 page_dmaaddr = 0, frag_dmaaddr;
898         u32 posted, page_offset = 0;
899
900         page_info = &page_info_tbl[rxq->head];
901         for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
902                 if (!pagep) {
903                         pagep = be_alloc_pages(adapter->big_page_size);
904                         if (unlikely(!pagep)) {
905                                 drvr_stats(adapter)->be_ethrx_post_fail++;
906                                 break;
907                         }
908                         page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
909                                                 adapter->big_page_size,
910                                                 PCI_DMA_FROMDEVICE);
911                         page_info->page_offset = 0;
912                 } else {
913                         get_page(pagep);
914                         page_info->page_offset = page_offset + rx_frag_size;
915                 }
916                 page_offset = page_info->page_offset;
917                 page_info->page = pagep;
918                 pci_unmap_addr_set(page_info, bus, page_dmaaddr);
919                 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
920
921                 rxd = queue_head_node(rxq);
922                 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
923                 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
924                 queue_head_inc(rxq);
925
926                 /* Any space left in the current big page for another frag? */
927                 if ((page_offset + rx_frag_size + rx_frag_size) >
928                                         adapter->big_page_size) {
929                         pagep = NULL;
930                         page_info->last_page_user = true;
931                 }
932                 page_info = &page_info_tbl[rxq->head];
933         }
934         if (pagep)
935                 page_info->last_page_user = true;
936
937         if (posted) {
938                 atomic_add(posted, &rxq->used);
939                 be_rxq_notify(&adapter->ctrl, rxq->id, posted);
940         } else if (atomic_read(&rxq->used) == 0) {
941                 /* Let be_worker replenish when memory is available */
942                 adapter->rx_post_starved = true;
943         }
944
945         return;
946 }
947
948 static struct be_eth_tx_compl *
949 be_tx_compl_get(struct be_adapter *adapter)
950 {
951         struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
952         struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
953
954         if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
955                 return NULL;
956
957         be_dws_le_to_cpu(txcp, sizeof(*txcp));
958
959         txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
960
961         queue_tail_inc(tx_cq);
962         return txcp;
963 }
964
965 static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
966 {
967         struct be_queue_info *txq = &adapter->tx_obj.q;
968         struct be_eth_wrb *wrb;
969         struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
970         struct sk_buff *sent_skb;
971         u64 busaddr;
972         u16 cur_index, num_wrbs = 0;
973
974         cur_index = txq->tail;
975         sent_skb = sent_skbs[cur_index];
976         BUG_ON(!sent_skb);
977         sent_skbs[cur_index] = NULL;
978
979         do {
980                 cur_index = txq->tail;
981                 wrb = queue_tail_node(txq);
982                 be_dws_le_to_cpu(wrb, sizeof(*wrb));
983                 busaddr = ((u64)wrb->frag_pa_hi << 32) | (u64)wrb->frag_pa_lo;
984                 if (busaddr != 0) {
985                         pci_unmap_single(adapter->pdev, busaddr,
986                                 wrb->frag_len, PCI_DMA_TODEVICE);
987                 }
988                 num_wrbs++;
989                 queue_tail_inc(txq);
990         } while (cur_index != last_index);
991
992         atomic_sub(num_wrbs, &txq->used);
993
994         kfree_skb(sent_skb);
995 }
996
997 static void be_rx_q_clean(struct be_adapter *adapter)
998 {
999         struct be_rx_page_info *page_info;
1000         struct be_queue_info *rxq = &adapter->rx_obj.q;
1001         struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1002         struct be_eth_rx_compl *rxcp;
1003         u16 tail;
1004
1005         /* First cleanup pending rx completions */
1006         while ((rxcp = be_rx_compl_get(adapter)) != NULL) {
1007                 be_rx_compl_discard(adapter, rxcp);
1008                 be_cq_notify(&adapter->ctrl, rx_cq->id, true, 1);
1009         }
1010
1011         /* Then free posted rx buffer that were not used */
1012         tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
1013         for (; tail != rxq->head; index_inc(&tail, rxq->len)) {
1014                 page_info = get_rx_page_info(adapter, tail);
1015                 put_page(page_info->page);
1016                 memset(page_info, 0, sizeof(*page_info));
1017         }
1018         BUG_ON(atomic_read(&rxq->used));
1019 }
1020
1021 static void be_tx_q_clean(struct be_adapter *adapter)
1022 {
1023         struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1024         struct sk_buff *sent_skb;
1025         struct be_queue_info *txq = &adapter->tx_obj.q;
1026         u16 last_index;
1027         bool dummy_wrb;
1028
1029         while (atomic_read(&txq->used)) {
1030                 sent_skb = sent_skbs[txq->tail];
1031                 last_index = txq->tail;
1032                 index_adv(&last_index,
1033                         wrb_cnt_for_skb(sent_skb, &dummy_wrb) - 1, txq->len);
1034                 be_tx_compl_process(adapter, last_index);
1035         }
1036 }
1037
1038 static void be_tx_queues_destroy(struct be_adapter *adapter)
1039 {
1040         struct be_queue_info *q;
1041
1042         q = &adapter->tx_obj.q;
1043         if (q->created)
1044                 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_TXQ);
1045         be_queue_free(adapter, q);
1046
1047         q = &adapter->tx_obj.cq;
1048         if (q->created)
1049                 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_CQ);
1050         be_queue_free(adapter, q);
1051
1052         /* No more tx completions can be rcvd now; clean up if there are
1053          * any pending completions or pending tx requests */
1054         be_tx_q_clean(adapter);
1055
1056         q = &adapter->tx_eq.q;
1057         if (q->created)
1058                 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_EQ);
1059         be_queue_free(adapter, q);
1060 }
1061
1062 static int be_tx_queues_create(struct be_adapter *adapter)
1063 {
1064         struct be_queue_info *eq, *q, *cq;
1065
1066         adapter->tx_eq.max_eqd = 0;
1067         adapter->tx_eq.min_eqd = 0;
1068         adapter->tx_eq.cur_eqd = 96;
1069         adapter->tx_eq.enable_aic = false;
1070         /* Alloc Tx Event queue */
1071         eq = &adapter->tx_eq.q;
1072         if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1073                 return -1;
1074
1075         /* Ask BE to create Tx Event queue */
1076         if (be_cmd_eq_create(&adapter->ctrl, eq, adapter->tx_eq.cur_eqd))
1077                 goto tx_eq_free;
1078         /* Alloc TX eth compl queue */
1079         cq = &adapter->tx_obj.cq;
1080         if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1081                         sizeof(struct be_eth_tx_compl)))
1082                 goto tx_eq_destroy;
1083
1084         /* Ask BE to create Tx eth compl queue */
1085         if (be_cmd_cq_create(&adapter->ctrl, cq, eq, false, false, 3))
1086                 goto tx_cq_free;
1087
1088         /* Alloc TX eth queue */
1089         q = &adapter->tx_obj.q;
1090         if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1091                 goto tx_cq_destroy;
1092
1093         /* Ask BE to create Tx eth queue */
1094         if (be_cmd_txq_create(&adapter->ctrl, q, cq))
1095                 goto tx_q_free;
1096         return 0;
1097
1098 tx_q_free:
1099         be_queue_free(adapter, q);
1100 tx_cq_destroy:
1101         be_cmd_q_destroy(&adapter->ctrl, cq, QTYPE_CQ);
1102 tx_cq_free:
1103         be_queue_free(adapter, cq);
1104 tx_eq_destroy:
1105         be_cmd_q_destroy(&adapter->ctrl, eq, QTYPE_EQ);
1106 tx_eq_free:
1107         be_queue_free(adapter, eq);
1108         return -1;
1109 }
1110
1111 static void be_rx_queues_destroy(struct be_adapter *adapter)
1112 {
1113         struct be_queue_info *q;
1114
1115         q = &adapter->rx_obj.q;
1116         if (q->created) {
1117                 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_RXQ);
1118                 be_rx_q_clean(adapter);
1119         }
1120         be_queue_free(adapter, q);
1121
1122         q = &adapter->rx_obj.cq;
1123         if (q->created)
1124                 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_CQ);
1125         be_queue_free(adapter, q);
1126
1127         q = &adapter->rx_eq.q;
1128         if (q->created)
1129                 be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_EQ);
1130         be_queue_free(adapter, q);
1131 }
1132
1133 static int be_rx_queues_create(struct be_adapter *adapter)
1134 {
1135         struct be_queue_info *eq, *q, *cq;
1136         int rc;
1137
1138         adapter->max_rx_coal = BE_MAX_FRAGS_PER_FRAME;
1139         adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
1140         adapter->rx_eq.max_eqd = BE_MAX_EQD;
1141         adapter->rx_eq.min_eqd = 0;
1142         adapter->rx_eq.cur_eqd = 0;
1143         adapter->rx_eq.enable_aic = true;
1144
1145         /* Alloc Rx Event queue */
1146         eq = &adapter->rx_eq.q;
1147         rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1148                                 sizeof(struct be_eq_entry));
1149         if (rc)
1150                 return rc;
1151
1152         /* Ask BE to create Rx Event queue */
1153         rc = be_cmd_eq_create(&adapter->ctrl, eq, adapter->rx_eq.cur_eqd);
1154         if (rc)
1155                 goto rx_eq_free;
1156
1157         /* Alloc RX eth compl queue */
1158         cq = &adapter->rx_obj.cq;
1159         rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1160                         sizeof(struct be_eth_rx_compl));
1161         if (rc)
1162                 goto rx_eq_destroy;
1163
1164         /* Ask BE to create Rx eth compl queue */
1165         rc = be_cmd_cq_create(&adapter->ctrl, cq, eq, false, false, 3);
1166         if (rc)
1167                 goto rx_cq_free;
1168
1169         /* Alloc RX eth queue */
1170         q = &adapter->rx_obj.q;
1171         rc = be_queue_alloc(adapter, q, RX_Q_LEN, sizeof(struct be_eth_rx_d));
1172         if (rc)
1173                 goto rx_cq_destroy;
1174
1175         /* Ask BE to create Rx eth queue */
1176         rc = be_cmd_rxq_create(&adapter->ctrl, q, cq->id, rx_frag_size,
1177                 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle, false);
1178         if (rc)
1179                 goto rx_q_free;
1180
1181         return 0;
1182 rx_q_free:
1183         be_queue_free(adapter, q);
1184 rx_cq_destroy:
1185         be_cmd_q_destroy(&adapter->ctrl, cq, QTYPE_CQ);
1186 rx_cq_free:
1187         be_queue_free(adapter, cq);
1188 rx_eq_destroy:
1189         be_cmd_q_destroy(&adapter->ctrl, eq, QTYPE_EQ);
1190 rx_eq_free:
1191         be_queue_free(adapter, eq);
1192         return rc;
1193 }
1194 static bool event_get(struct be_eq_obj *eq_obj, u16 *rid)
1195 {
1196         struct be_eq_entry *entry = queue_tail_node(&eq_obj->q);
1197         u32 evt = entry->evt;
1198
1199         if (!evt)
1200                 return false;
1201
1202         evt = le32_to_cpu(evt);
1203         *rid = (evt >> EQ_ENTRY_RES_ID_SHIFT) & EQ_ENTRY_RES_ID_MASK;
1204         entry->evt = 0;
1205         queue_tail_inc(&eq_obj->q);
1206         return true;
1207 }
1208
1209 static int event_handle(struct be_ctrl_info *ctrl,
1210                         struct be_eq_obj *eq_obj)
1211 {
1212         u16 rid = 0, num = 0;
1213
1214         while (event_get(eq_obj, &rid))
1215                 num++;
1216
1217         /* We can see an interrupt and no event */
1218         be_eq_notify(ctrl, eq_obj->q.id, true, true, num);
1219         if (num)
1220                 napi_schedule(&eq_obj->napi);
1221
1222         return num;
1223 }
1224
1225 static irqreturn_t be_intx(int irq, void *dev)
1226 {
1227         struct be_adapter *adapter = dev;
1228         struct be_ctrl_info *ctrl = &adapter->ctrl;
1229         int rx, tx;
1230
1231         tx = event_handle(ctrl, &adapter->tx_eq);
1232         rx = event_handle(ctrl, &adapter->rx_eq);
1233
1234         if (rx || tx)
1235                 return IRQ_HANDLED;
1236         else
1237                 return IRQ_NONE;
1238 }
1239
1240 static irqreturn_t be_msix_rx(int irq, void *dev)
1241 {
1242         struct be_adapter *adapter = dev;
1243
1244         event_handle(&adapter->ctrl, &adapter->rx_eq);
1245
1246         return IRQ_HANDLED;
1247 }
1248
1249 static irqreturn_t be_msix_tx(int irq, void *dev)
1250 {
1251         struct be_adapter *adapter = dev;
1252
1253         event_handle(&adapter->ctrl, &adapter->tx_eq);
1254
1255         return IRQ_HANDLED;
1256 }
1257
1258 static inline bool do_lro(struct be_adapter *adapter,
1259                         struct be_eth_rx_compl *rxcp)
1260 {
1261         int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1262         int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1263
1264         if (err)
1265                 drvr_stats(adapter)->be_rxcp_err++;
1266
1267         return (!tcp_frame || err || (adapter->max_rx_coal <= 1)) ?
1268                 false : true;
1269 }
1270
1271 int be_poll_rx(struct napi_struct *napi, int budget)
1272 {
1273         struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
1274         struct be_adapter *adapter =
1275                 container_of(rx_eq, struct be_adapter, rx_eq);
1276         struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1277         struct be_eth_rx_compl *rxcp;
1278         u32 work_done;
1279
1280         for (work_done = 0; work_done < budget; work_done++) {
1281                 rxcp = be_rx_compl_get(adapter);
1282                 if (!rxcp)
1283                         break;
1284
1285                 if (do_lro(adapter, rxcp))
1286                         be_rx_compl_process_lro(adapter, rxcp);
1287                 else
1288                         be_rx_compl_process(adapter, rxcp);
1289         }
1290
1291         lro_flush_all(&adapter->rx_obj.lro_mgr);
1292
1293         /* Refill the queue */
1294         if (atomic_read(&adapter->rx_obj.q.used) < RX_FRAGS_REFILL_WM)
1295                 be_post_rx_frags(adapter);
1296
1297         /* All consumed */
1298         if (work_done < budget) {
1299                 napi_complete(napi);
1300                 be_cq_notify(&adapter->ctrl, rx_cq->id, true, work_done);
1301         } else {
1302                 /* More to be consumed; continue with interrupts disabled */
1303                 be_cq_notify(&adapter->ctrl, rx_cq->id, false, work_done);
1304         }
1305         return work_done;
1306 }
1307
1308 /* For TX we don't honour budget; consume everything */
1309 int be_poll_tx(struct napi_struct *napi, int budget)
1310 {
1311         struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1312         struct be_adapter *adapter =
1313                 container_of(tx_eq, struct be_adapter, tx_eq);
1314         struct be_tx_obj *tx_obj = &adapter->tx_obj;
1315         struct be_queue_info *tx_cq = &tx_obj->cq;
1316         struct be_queue_info *txq = &tx_obj->q;
1317         struct be_eth_tx_compl *txcp;
1318         u32 num_cmpl = 0;
1319         u16 end_idx;
1320
1321         while ((txcp = be_tx_compl_get(adapter))) {
1322                 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1323                                         wrb_index, txcp);
1324                 be_tx_compl_process(adapter, end_idx);
1325                 num_cmpl++;
1326         }
1327
1328         /* As Tx wrbs have been freed up, wake up netdev queue if
1329          * it was stopped due to lack of tx wrbs.
1330          */
1331         if (netif_queue_stopped(adapter->netdev) &&
1332                         atomic_read(&txq->used) < txq->len / 2) {
1333                 netif_wake_queue(adapter->netdev);
1334         }
1335
1336         napi_complete(napi);
1337
1338         be_cq_notify(&adapter->ctrl, tx_cq->id, true, num_cmpl);
1339
1340         drvr_stats(adapter)->be_tx_events++;
1341         drvr_stats(adapter)->be_tx_compl += num_cmpl;
1342
1343         return 1;
1344 }
1345
1346 static void be_worker(struct work_struct *work)
1347 {
1348         struct be_adapter *adapter =
1349                 container_of(work, struct be_adapter, work.work);
1350         int status;
1351
1352         /* Check link */
1353         be_link_status_update(adapter);
1354
1355         /* Get Stats */
1356         status = be_cmd_get_stats(&adapter->ctrl, &adapter->stats.cmd);
1357         if (!status)
1358                 netdev_stats_update(adapter);
1359
1360         /* Set EQ delay */
1361         be_rx_eqd_update(adapter);
1362
1363         be_tx_rate_update(adapter);
1364         be_rx_rate_update(adapter);
1365
1366         if (adapter->rx_post_starved) {
1367                 adapter->rx_post_starved = false;
1368                 be_post_rx_frags(adapter);
1369         }
1370
1371         schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1372 }
1373
1374 static void be_msix_enable(struct be_adapter *adapter)
1375 {
1376         int i, status;
1377
1378         for (i = 0; i < BE_NUM_MSIX_VECTORS; i++)
1379                 adapter->msix_entries[i].entry = i;
1380
1381         status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1382                 BE_NUM_MSIX_VECTORS);
1383         if (status == 0)
1384                 adapter->msix_enabled = true;
1385         return;
1386 }
1387
1388 static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1389 {
1390         return adapter->msix_entries[eq_id -
1391                         8 * adapter->ctrl.pci_func].vector;
1392 }
1393
1394 static int be_msix_register(struct be_adapter *adapter)
1395 {
1396         struct net_device *netdev = adapter->netdev;
1397         struct be_eq_obj *tx_eq = &adapter->tx_eq;
1398         struct be_eq_obj *rx_eq = &adapter->rx_eq;
1399         int status, vec;
1400
1401         sprintf(tx_eq->desc, "%s-tx", netdev->name);
1402         vec = be_msix_vec_get(adapter, tx_eq->q.id);
1403         status = request_irq(vec, be_msix_tx, 0, tx_eq->desc, adapter);
1404         if (status)
1405                 goto err;
1406
1407         sprintf(rx_eq->desc, "%s-rx", netdev->name);
1408         vec = be_msix_vec_get(adapter, rx_eq->q.id);
1409         status = request_irq(vec, be_msix_rx, 0, rx_eq->desc, adapter);
1410         if (status) { /* Free TX IRQ */
1411                 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1412                 free_irq(vec, adapter);
1413                 goto err;
1414         }
1415         return 0;
1416 err:
1417         dev_warn(&adapter->pdev->dev,
1418                 "MSIX Request IRQ failed - err %d\n", status);
1419         pci_disable_msix(adapter->pdev);
1420         adapter->msix_enabled = false;
1421         return status;
1422 }
1423
1424 static int be_irq_register(struct be_adapter *adapter)
1425 {
1426         struct net_device *netdev = adapter->netdev;
1427         int status;
1428
1429         if (adapter->msix_enabled) {
1430                 status = be_msix_register(adapter);
1431                 if (status == 0)
1432                         goto done;
1433         }
1434
1435         /* INTx */
1436         netdev->irq = adapter->pdev->irq;
1437         status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1438                         adapter);
1439         if (status) {
1440                 dev_err(&adapter->pdev->dev,
1441                         "INTx request IRQ failed - err %d\n", status);
1442                 return status;
1443         }
1444 done:
1445         adapter->isr_registered = true;
1446         return 0;
1447 }
1448
1449 static void be_irq_unregister(struct be_adapter *adapter)
1450 {
1451         struct net_device *netdev = adapter->netdev;
1452         int vec;
1453
1454         if (!adapter->isr_registered)
1455                 return;
1456
1457         /* INTx */
1458         if (!adapter->msix_enabled) {
1459                 free_irq(netdev->irq, adapter);
1460                 goto done;
1461         }
1462
1463         /* MSIx */
1464         vec = be_msix_vec_get(adapter, adapter->tx_eq.q.id);
1465         free_irq(vec, adapter);
1466         vec = be_msix_vec_get(adapter, adapter->rx_eq.q.id);
1467         free_irq(vec, adapter);
1468 done:
1469         adapter->isr_registered = false;
1470         return;
1471 }
1472
1473 static int be_open(struct net_device *netdev)
1474 {
1475         struct be_adapter *adapter = netdev_priv(netdev);
1476         struct be_ctrl_info *ctrl = &adapter->ctrl;
1477         struct be_eq_obj *rx_eq = &adapter->rx_eq;
1478         struct be_eq_obj *tx_eq = &adapter->tx_eq;
1479         u32 if_flags;
1480         int status;
1481
1482         if_flags = BE_IF_FLAGS_BROADCAST | BE_IF_FLAGS_PROMISCUOUS |
1483                 BE_IF_FLAGS_MCAST_PROMISCUOUS | BE_IF_FLAGS_UNTAGGED |
1484                 BE_IF_FLAGS_PASS_L3L4_ERRORS;
1485         status = be_cmd_if_create(ctrl, if_flags, netdev->dev_addr,
1486                         false/* pmac_invalid */, &adapter->if_handle,
1487                         &adapter->pmac_id);
1488         if (status != 0)
1489                 goto do_none;
1490
1491         be_vid_config(netdev);
1492
1493         status = be_cmd_set_flow_control(ctrl, true, true);
1494         if (status != 0)
1495                 goto if_destroy;
1496
1497         status = be_tx_queues_create(adapter);
1498         if (status != 0)
1499                 goto if_destroy;
1500
1501         status = be_rx_queues_create(adapter);
1502         if (status != 0)
1503                 goto tx_qs_destroy;
1504
1505         /* First time posting */
1506         be_post_rx_frags(adapter);
1507
1508         napi_enable(&rx_eq->napi);
1509         napi_enable(&tx_eq->napi);
1510
1511         be_irq_register(adapter);
1512
1513         be_intr_set(ctrl, true);
1514
1515         /* The evt queues are created in the unarmed state; arm them */
1516         be_eq_notify(ctrl, rx_eq->q.id, true, false, 0);
1517         be_eq_notify(ctrl, tx_eq->q.id, true, false, 0);
1518
1519         /* The compl queues are created in the unarmed state; arm them */
1520         be_cq_notify(ctrl, adapter->rx_obj.cq.id, true, 0);
1521         be_cq_notify(ctrl, adapter->tx_obj.cq.id, true, 0);
1522
1523         be_link_status_update(adapter);
1524
1525         schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
1526         return 0;
1527
1528 tx_qs_destroy:
1529         be_tx_queues_destroy(adapter);
1530 if_destroy:
1531         be_cmd_if_destroy(ctrl, adapter->if_handle);
1532 do_none:
1533         return status;
1534 }
1535
1536 static int be_close(struct net_device *netdev)
1537 {
1538         struct be_adapter *adapter = netdev_priv(netdev);
1539         struct be_ctrl_info *ctrl = &adapter->ctrl;
1540         struct be_eq_obj *rx_eq = &adapter->rx_eq;
1541         struct be_eq_obj *tx_eq = &adapter->tx_eq;
1542         int vec;
1543
1544         cancel_delayed_work(&adapter->work);
1545
1546         netif_stop_queue(netdev);
1547         netif_carrier_off(netdev);
1548         adapter->link.speed = PHY_LINK_SPEED_ZERO;
1549
1550         be_intr_set(ctrl, false);
1551
1552         if (adapter->msix_enabled) {
1553                 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1554                 synchronize_irq(vec);
1555                 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1556                 synchronize_irq(vec);
1557         } else {
1558                 synchronize_irq(netdev->irq);
1559         }
1560         be_irq_unregister(adapter);
1561
1562         napi_disable(&rx_eq->napi);
1563         napi_disable(&tx_eq->napi);
1564
1565         be_rx_queues_destroy(adapter);
1566         be_tx_queues_destroy(adapter);
1567
1568         be_cmd_if_destroy(ctrl, adapter->if_handle);
1569         return 0;
1570 }
1571
1572 static int be_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr,
1573                                 void **ip_hdr, void **tcpudp_hdr,
1574                                 u64 *hdr_flags, void *priv)
1575 {
1576         struct ethhdr *eh;
1577         struct vlan_ethhdr *veh;
1578         struct iphdr *iph;
1579         u8 *va = page_address(frag->page) + frag->page_offset;
1580         unsigned long ll_hlen;
1581
1582         prefetch(va);
1583         eh = (struct ethhdr *)va;
1584         *mac_hdr = eh;
1585         ll_hlen = ETH_HLEN;
1586         if (eh->h_proto != htons(ETH_P_IP)) {
1587                 if (eh->h_proto == htons(ETH_P_8021Q)) {
1588                         veh = (struct vlan_ethhdr *)va;
1589                         if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
1590                                 return -1;
1591
1592                         ll_hlen += VLAN_HLEN;
1593                 } else {
1594                         return -1;
1595                 }
1596         }
1597         *hdr_flags = LRO_IPV4;
1598         iph = (struct iphdr *)(va + ll_hlen);
1599         *ip_hdr = iph;
1600         if (iph->protocol != IPPROTO_TCP)
1601                 return -1;
1602         *hdr_flags |= LRO_TCP;
1603         *tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2);
1604
1605         return 0;
1606 }
1607
1608 static void be_lro_init(struct be_adapter *adapter, struct net_device *netdev)
1609 {
1610         struct net_lro_mgr *lro_mgr;
1611
1612         lro_mgr = &adapter->rx_obj.lro_mgr;
1613         lro_mgr->dev = netdev;
1614         lro_mgr->features = LRO_F_NAPI;
1615         lro_mgr->ip_summed = CHECKSUM_UNNECESSARY;
1616         lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
1617         lro_mgr->max_desc = BE_MAX_LRO_DESCRIPTORS;
1618         lro_mgr->lro_arr = adapter->rx_obj.lro_desc;
1619         lro_mgr->get_frag_header = be_get_frag_header;
1620         lro_mgr->max_aggr = BE_MAX_FRAGS_PER_FRAME;
1621 }
1622
1623 static struct net_device_ops be_netdev_ops = {
1624         .ndo_open               = be_open,
1625         .ndo_stop               = be_close,
1626         .ndo_start_xmit         = be_xmit,
1627         .ndo_get_stats          = be_get_stats,
1628         .ndo_set_rx_mode        = be_set_multicast_list,
1629         .ndo_set_mac_address    = be_mac_addr_set,
1630         .ndo_change_mtu         = be_change_mtu,
1631         .ndo_validate_addr      = eth_validate_addr,
1632         .ndo_vlan_rx_register   = be_vlan_register,
1633         .ndo_vlan_rx_add_vid    = be_vlan_add_vid,
1634         .ndo_vlan_rx_kill_vid   = be_vlan_rem_vid,
1635 };
1636
1637 static void be_netdev_init(struct net_device *netdev)
1638 {
1639         struct be_adapter *adapter = netdev_priv(netdev);
1640
1641         netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
1642                 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_IP_CSUM |
1643                 NETIF_F_IPV6_CSUM;
1644
1645         netdev->flags |= IFF_MULTICAST;
1646
1647         adapter->rx_csum = true;
1648
1649         BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
1650
1651         SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
1652
1653         be_lro_init(adapter, netdev);
1654
1655         netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx,
1656                 BE_NAPI_WEIGHT);
1657         netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx,
1658                 BE_NAPI_WEIGHT);
1659
1660         netif_carrier_off(netdev);
1661         netif_stop_queue(netdev);
1662 }
1663
1664 static void be_unmap_pci_bars(struct be_adapter *adapter)
1665 {
1666         struct be_ctrl_info *ctrl = &adapter->ctrl;
1667         if (ctrl->csr)
1668                 iounmap(ctrl->csr);
1669         if (ctrl->db)
1670                 iounmap(ctrl->db);
1671         if (ctrl->pcicfg)
1672                 iounmap(ctrl->pcicfg);
1673 }
1674
1675 static int be_map_pci_bars(struct be_adapter *adapter)
1676 {
1677         u8 __iomem *addr;
1678
1679         addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
1680                         pci_resource_len(adapter->pdev, 2));
1681         if (addr == NULL)
1682                 return -ENOMEM;
1683         adapter->ctrl.csr = addr;
1684
1685         addr = ioremap_nocache(pci_resource_start(adapter->pdev, 4),
1686                         128 * 1024);
1687         if (addr == NULL)
1688                 goto pci_map_err;
1689         adapter->ctrl.db = addr;
1690
1691         addr = ioremap_nocache(pci_resource_start(adapter->pdev, 1),
1692                         pci_resource_len(adapter->pdev, 1));
1693         if (addr == NULL)
1694                 goto pci_map_err;
1695         adapter->ctrl.pcicfg = addr;
1696
1697         return 0;
1698 pci_map_err:
1699         be_unmap_pci_bars(adapter);
1700         return -ENOMEM;
1701 }
1702
1703
1704 static void be_ctrl_cleanup(struct be_adapter *adapter)
1705 {
1706         struct be_dma_mem *mem = &adapter->ctrl.mbox_mem_alloced;
1707
1708         be_unmap_pci_bars(adapter);
1709
1710         if (mem->va)
1711                 pci_free_consistent(adapter->pdev, mem->size,
1712                         mem->va, mem->dma);
1713 }
1714
1715 /* Initialize the mbox required to send cmds to BE */
1716 static int be_ctrl_init(struct be_adapter *adapter)
1717 {
1718         struct be_ctrl_info *ctrl = &adapter->ctrl;
1719         struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
1720         struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
1721         int status;
1722         u32 val;
1723
1724         status = be_map_pci_bars(adapter);
1725         if (status)
1726                 return status;
1727
1728         mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
1729         mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
1730                                 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
1731         if (!mbox_mem_alloc->va) {
1732                 be_unmap_pci_bars(adapter);
1733                 return -1;
1734         }
1735         mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
1736         mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
1737         mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
1738         memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
1739         spin_lock_init(&ctrl->cmd_lock);
1740
1741         val = ioread32(ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
1742         ctrl->pci_func = (val >> MEMBAR_CTRL_INT_CTRL_PFUNC_SHIFT) &
1743                                         MEMBAR_CTRL_INT_CTRL_PFUNC_MASK;
1744         return 0;
1745 }
1746
1747 static void be_stats_cleanup(struct be_adapter *adapter)
1748 {
1749         struct be_stats_obj *stats = &adapter->stats;
1750         struct be_dma_mem *cmd = &stats->cmd;
1751
1752         if (cmd->va)
1753                 pci_free_consistent(adapter->pdev, cmd->size,
1754                         cmd->va, cmd->dma);
1755 }
1756
1757 static int be_stats_init(struct be_adapter *adapter)
1758 {
1759         struct be_stats_obj *stats = &adapter->stats;
1760         struct be_dma_mem *cmd = &stats->cmd;
1761
1762         cmd->size = sizeof(struct be_cmd_req_get_stats);
1763         cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
1764         if (cmd->va == NULL)
1765                 return -1;
1766         return 0;
1767 }
1768
1769 static void __devexit be_remove(struct pci_dev *pdev)
1770 {
1771         struct be_adapter *adapter = pci_get_drvdata(pdev);
1772         if (!adapter)
1773                 return;
1774
1775         unregister_netdev(adapter->netdev);
1776
1777         be_stats_cleanup(adapter);
1778
1779         be_ctrl_cleanup(adapter);
1780
1781         if (adapter->msix_enabled) {
1782                 pci_disable_msix(adapter->pdev);
1783                 adapter->msix_enabled = false;
1784         }
1785
1786         pci_set_drvdata(pdev, NULL);
1787         pci_release_regions(pdev);
1788         pci_disable_device(pdev);
1789
1790         free_netdev(adapter->netdev);
1791 }
1792
1793 static int be_hw_up(struct be_adapter *adapter)
1794 {
1795         struct be_ctrl_info *ctrl = &adapter->ctrl;
1796         int status;
1797
1798         status = be_cmd_POST(ctrl);
1799         if (status)
1800                 return status;
1801
1802         status = be_cmd_get_fw_ver(ctrl, adapter->fw_ver);
1803         if (status)
1804                 return status;
1805
1806         status = be_cmd_query_fw_cfg(ctrl, &adapter->port_num);
1807         return status;
1808 }
1809
1810 static int __devinit be_probe(struct pci_dev *pdev,
1811                         const struct pci_device_id *pdev_id)
1812 {
1813         int status = 0;
1814         struct be_adapter *adapter;
1815         struct net_device *netdev;
1816         struct be_ctrl_info *ctrl;
1817         u8 mac[ETH_ALEN];
1818
1819         status = pci_enable_device(pdev);
1820         if (status)
1821                 goto do_none;
1822
1823         status = pci_request_regions(pdev, DRV_NAME);
1824         if (status)
1825                 goto disable_dev;
1826         pci_set_master(pdev);
1827
1828         netdev = alloc_etherdev(sizeof(struct be_adapter));
1829         if (netdev == NULL) {
1830                 status = -ENOMEM;
1831                 goto rel_reg;
1832         }
1833         adapter = netdev_priv(netdev);
1834         adapter->pdev = pdev;
1835         pci_set_drvdata(pdev, adapter);
1836         adapter->netdev = netdev;
1837
1838         be_msix_enable(adapter);
1839
1840         status = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1841         if (!status) {
1842                 netdev->features |= NETIF_F_HIGHDMA;
1843         } else {
1844                 status = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1845                 if (status) {
1846                         dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
1847                         goto free_netdev;
1848                 }
1849         }
1850
1851         ctrl = &adapter->ctrl;
1852         status = be_ctrl_init(adapter);
1853         if (status)
1854                 goto free_netdev;
1855
1856         status = be_stats_init(adapter);
1857         if (status)
1858                 goto ctrl_clean;
1859
1860         status = be_hw_up(adapter);
1861         if (status)
1862                 goto stats_clean;
1863
1864         status = be_cmd_mac_addr_query(ctrl, mac, MAC_ADDRESS_TYPE_NETWORK,
1865                         true /* permanent */, 0);
1866         if (status)
1867                 goto stats_clean;
1868         memcpy(netdev->dev_addr, mac, ETH_ALEN);
1869
1870         INIT_DELAYED_WORK(&adapter->work, be_worker);
1871         be_netdev_init(netdev);
1872         SET_NETDEV_DEV(netdev, &adapter->pdev->dev);
1873
1874         status = register_netdev(netdev);
1875         if (status != 0)
1876                 goto stats_clean;
1877
1878         dev_info(&pdev->dev, BE_NAME " port %d\n", adapter->port_num);
1879         return 0;
1880
1881 stats_clean:
1882         be_stats_cleanup(adapter);
1883 ctrl_clean:
1884         be_ctrl_cleanup(adapter);
1885 free_netdev:
1886         free_netdev(adapter->netdev);
1887 rel_reg:
1888         pci_release_regions(pdev);
1889 disable_dev:
1890         pci_disable_device(pdev);
1891 do_none:
1892         dev_warn(&pdev->dev, BE_NAME " initialization failed\n");
1893         return status;
1894 }
1895
1896 static int be_suspend(struct pci_dev *pdev, pm_message_t state)
1897 {
1898         struct be_adapter *adapter = pci_get_drvdata(pdev);
1899         struct net_device *netdev =  adapter->netdev;
1900
1901         netif_device_detach(netdev);
1902         if (netif_running(netdev)) {
1903                 rtnl_lock();
1904                 be_close(netdev);
1905                 rtnl_unlock();
1906         }
1907
1908         pci_save_state(pdev);
1909         pci_disable_device(pdev);
1910         pci_set_power_state(pdev, pci_choose_state(pdev, state));
1911         return 0;
1912 }
1913
1914 static int be_resume(struct pci_dev *pdev)
1915 {
1916         int status = 0;
1917         struct be_adapter *adapter = pci_get_drvdata(pdev);
1918         struct net_device *netdev =  adapter->netdev;
1919
1920         netif_device_detach(netdev);
1921
1922         status = pci_enable_device(pdev);
1923         if (status)
1924                 return status;
1925
1926         pci_set_power_state(pdev, 0);
1927         pci_restore_state(pdev);
1928
1929         if (netif_running(netdev)) {
1930                 rtnl_lock();
1931                 be_open(netdev);
1932                 rtnl_unlock();
1933         }
1934         netif_device_attach(netdev);
1935         return 0;
1936 }
1937
1938 static struct pci_driver be_driver = {
1939         .name = DRV_NAME,
1940         .id_table = be_dev_ids,
1941         .probe = be_probe,
1942         .remove = be_remove,
1943         .suspend = be_suspend,
1944         .resume = be_resume
1945 };
1946
1947 static int __init be_init_module(void)
1948 {
1949         if (rx_frag_size != 8192 && rx_frag_size != 4096
1950                 && rx_frag_size != 2048) {
1951                 printk(KERN_WARNING DRV_NAME
1952                         " : Module param rx_frag_size must be 2048/4096/8192."
1953                         " Using 2048\n");
1954                 rx_frag_size = 2048;
1955         }
1956         /* Ensure rx_frag_size is aligned to chache line */
1957         if (SKB_DATA_ALIGN(rx_frag_size) != rx_frag_size) {
1958                 printk(KERN_WARNING DRV_NAME
1959                         " : Bad module param rx_frag_size. Using 2048\n");
1960                 rx_frag_size = 2048;
1961         }
1962
1963         return pci_register_driver(&be_driver);
1964 }
1965 module_init(be_init_module);
1966
1967 static void __exit be_exit_module(void)
1968 {
1969         pci_unregister_driver(&be_driver);
1970 }
1971 module_exit(be_exit_module);