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