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