]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/net/benet/be_main.c
be2net: some name changes for consistency
[mirror_ubuntu-zesty-kernel.git] / drivers / net / benet / be_main.c
CommitLineData
6b7c5b94
SP
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"
8788fdc2 19#include "be_cmds.h"
65f71b8b 20#include <asm/div64.h>
6b7c5b94
SP
21
22MODULE_VERSION(DRV_VER);
23MODULE_DEVICE_TABLE(pci, be_dev_ids);
24MODULE_DESCRIPTION(DRV_DESC " " DRV_VER);
25MODULE_AUTHOR("ServerEngines Corporation");
26MODULE_LICENSE("GPL");
27
28static unsigned int rx_frag_size = 2048;
29module_param(rx_frag_size, uint, S_IRUGO);
30MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data.");
31
6b7c5b94 32static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
c4ca2374
AK
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) },
6b7c5b94
SP
36 { 0 }
37};
38MODULE_DEVICE_TABLE(pci, be_dev_ids);
39
40static 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
48static 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
8788fdc2 64static void be_intr_set(struct be_adapter *adapter, bool enable)
6b7c5b94 65{
8788fdc2 66 u8 __iomem *addr = adapter->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
6b7c5b94
SP
67 u32 reg = ioread32(addr);
68 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
5f0b849e
SP
69
70 if (!enabled && enable)
6b7c5b94 71 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
5f0b849e 72 else if (enabled && !enable)
6b7c5b94 73 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
5f0b849e 74 else
6b7c5b94 75 return;
5f0b849e 76
6b7c5b94
SP
77 iowrite32(reg, addr);
78}
79
8788fdc2 80static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
6b7c5b94
SP
81{
82 u32 val = 0;
83 val |= qid & DB_RQ_RING_ID_MASK;
84 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
8788fdc2 85 iowrite32(val, adapter->db + DB_RQ_OFFSET);
6b7c5b94
SP
86}
87
8788fdc2 88static void be_txq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
6b7c5b94
SP
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;
8788fdc2 93 iowrite32(val, adapter->db + DB_TXULP1_OFFSET);
6b7c5b94
SP
94}
95
8788fdc2 96static void be_eq_notify(struct be_adapter *adapter, u16 qid,
6b7c5b94
SP
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;
8788fdc2 107 iowrite32(val, adapter->db + DB_EQ_OFFSET);
6b7c5b94
SP
108}
109
8788fdc2 110void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
6b7c5b94
SP
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;
8788fdc2 117 iowrite32(val, adapter->db + DB_CQ_OFFSET);
6b7c5b94
SP
118}
119
120
121static 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)) {
8788fdc2 128 status = be_cmd_pmac_del(adapter, adapter->if_handle,
6b7c5b94
SP
129 adapter->pmac_id);
130 if (status)
131 return status;
132
8788fdc2 133 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
6b7c5b94
SP
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
143static 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;
68110868 150 struct be_erx_stats *erx_stats = &hw_stats->erx;
6b7c5b94
SP
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 +
68110868
SP
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];
6b7c5b94
SP
177
178 /* detailed rx errors */
179 dev_stats->rx_length_errors = port_stats->rx_in_range_errors +
68110868
SP
180 port_stats->rx_out_range_errors +
181 port_stats->rx_frame_too_long;
182
6b7c5b94
SP
183 /* receive ring buffer overflow */
184 dev_stats->rx_over_errors = 0;
68110868 185
6b7c5b94
SP
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;
68110868 190
6b7c5b94
SP
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;
68110868
SP
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
6b7c5b94
SP
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
8788fdc2 216void be_link_status_update(struct be_adapter *adapter, bool link_up)
6b7c5b94 217{
6b7c5b94
SP
218 struct net_device *netdev = adapter->netdev;
219
6b7c5b94 220 /* If link came up or went down */
a8f447bd
SP
221 if (adapter->link_up != link_up) {
222 if (link_up) {
6b7c5b94
SP
223 netif_start_queue(netdev);
224 netif_carrier_on(netdev);
225 printk(KERN_INFO "%s: Link up\n", netdev->name);
a8f447bd
SP
226 } else {
227 netif_stop_queue(netdev);
228 netif_carrier_off(netdev);
229 printk(KERN_INFO "%s: Link down\n", netdev->name);
6b7c5b94 230 }
a8f447bd 231 adapter->link_up = link_up;
6b7c5b94 232 }
6b7c5b94
SP
233}
234
235/* Update the EQ delay n BE based on the RX frags consumed / sec */
236static void be_rx_eqd_update(struct be_adapter *adapter)
237{
6b7c5b94
SP
238 struct be_eq_obj *rx_eq = &adapter->rx_eq;
239 struct be_drvr_stats *stats = &adapter->stats.drvr_stats;
4097f663
SP
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 }
6b7c5b94
SP
251
252 /* Update once a second */
4097f663 253 if ((now - stats->rx_fps_jiffies) < HZ)
6b7c5b94
SP
254 return;
255
256 stats->be_rx_fps = (stats->be_rx_frags - stats->be_prev_rx_frags) /
4097f663 257 ((now - stats->rx_fps_jiffies) / HZ);
6b7c5b94 258
4097f663 259 stats->rx_fps_jiffies = now;
6b7c5b94
SP
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)
8788fdc2 270 be_cmd_modify_eqd(adapter, rx_eq->q.id, eqd);
6b7c5b94
SP
271
272 rx_eq->cur_eqd = eqd;
273}
274
6b7c5b94
SP
275static 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
65f71b8b
SH
282static 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
4097f663
SP
293static 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) {
65f71b8b
SH
306 stats->be_tx_rate = be_calc_rate(stats->be_tx_bytes
307 - stats->be_tx_bytes_prev,
308 now - stats->be_tx_jiffies);
4097f663
SP
309 stats->be_tx_jiffies = now;
310 stats->be_tx_bytes_prev = stats->be_tx_bytes;
311 }
312}
313
6b7c5b94
SP
314static void be_tx_stats_update(struct be_adapter *adapter,
315 u32 wrb_cnt, u32 copied, bool stopped)
316{
4097f663 317 struct be_drvr_stats *stats = drvr_stats(adapter);
6b7c5b94
SP
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++;
6b7c5b94
SP
323}
324
325/* Determine number of WRB entries needed to xmit data in an skb */
326static u32 wrb_cnt_for_skb(struct sk_buff *skb, bool *dummy)
327{
ebc8d2ab
DM
328 int cnt = (skb->len > skb->data_len);
329
330 cnt += skb_shinfo(skb)->nr_frags;
331
6b7c5b94
SP
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
344static 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
351static 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
382static 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
ebc8d2ab
DM
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 }
6b7c5b94 407
ebc8d2ab
DM
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,
8788fdc2
SP
412 frag->page_offset,
413 frag->size, PCI_DMA_TODEVICE);
ebc8d2ab
DM
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;
6b7c5b94
SP
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
435static 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
8788fdc2 461 be_txq_notify(adapter, txq->id, wrb_cnt);
6b7c5b94 462
6b7c5b94
SP
463 be_tx_stats_update(adapter, wrb_cnt, copied, stopped);
464 return NETDEV_TX_OK;
465}
466
467static 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 */
1ab1ab75 488static void be_vid_config(struct net_device *netdev)
6b7c5b94
SP
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 }
8788fdc2 502 be_cmd_vlan_config(adapter, adapter->if_handle,
6b7c5b94
SP
503 vtag, ntags, 1, 0);
504 } else {
8788fdc2 505 be_cmd_vlan_config(adapter, adapter->if_handle,
6b7c5b94
SP
506 NULL, 0, 1, 1);
507 }
508}
509
510static 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;
6b7c5b94 515
8788fdc2
SP
516 be_eq_notify(adapter, rx_eq->q.id, false, false, 0);
517 be_eq_notify(adapter, tx_eq->q.id, false, false, 0);
6b7c5b94 518 adapter->vlan_grp = grp;
8788fdc2
SP
519 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
520 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
6b7c5b94
SP
521}
522
523static 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
1ab1ab75 530 be_vid_config(netdev);
6b7c5b94
SP
531}
532
533static 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);
1ab1ab75 541 be_vid_config(netdev);
6b7c5b94
SP
542}
543
24307eef 544static void be_set_multicast_list(struct net_device *netdev)
6b7c5b94
SP
545{
546 struct be_adapter *adapter = netdev_priv(netdev);
6b7c5b94 547
24307eef 548 if (netdev->flags & IFF_PROMISC) {
8788fdc2 549 be_cmd_promiscuous_config(adapter, adapter->port_num, 1);
24307eef
SP
550 adapter->promiscuous = true;
551 goto done;
6b7c5b94
SP
552 }
553
24307eef
SP
554 /* BE was previously in promiscous mode; disable it */
555 if (adapter->promiscuous) {
556 adapter->promiscuous = false;
8788fdc2 557 be_cmd_promiscuous_config(adapter, adapter->port_num, 0);
6b7c5b94
SP
558 }
559
24307eef 560 if (netdev->flags & IFF_ALLMULTI) {
8788fdc2 561 be_cmd_multicast_set(adapter, adapter->if_handle, NULL, 0);
24307eef 562 goto done;
6b7c5b94 563 }
6b7c5b94 564
8788fdc2 565 be_cmd_multicast_set(adapter, adapter->if_handle, netdev->mc_list,
24307eef
SP
566 netdev->mc_count);
567done:
568 return;
6b7c5b94
SP
569}
570
4097f663 571static void be_rx_rate_update(struct be_adapter *adapter)
6b7c5b94 572{
4097f663
SP
573 struct be_drvr_stats *stats = drvr_stats(adapter);
574 ulong now = jiffies;
6b7c5b94 575
4097f663
SP
576 /* Wrapped around */
577 if (time_before(now, stats->be_rx_jiffies)) {
578 stats->be_rx_jiffies = now;
579 return;
580 }
6b7c5b94
SP
581
582 /* Update the rate once in two seconds */
4097f663 583 if ((now - stats->be_rx_jiffies) < 2 * HZ)
6b7c5b94
SP
584 return;
585
65f71b8b
SH
586 stats->be_rx_rate = be_calc_rate(stats->be_rx_bytes
587 - stats->be_rx_bytes_prev,
588 now - stats->be_rx_jiffies);
4097f663 589 stats->be_rx_jiffies = now;
6b7c5b94
SP
590 stats->be_rx_bytes_prev = stats->be_rx_bytes;
591}
592
4097f663
SP
593static 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
728a9972
AK
603static 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
6b7c5b94
SP
619static struct be_rx_page_info *
620get_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 */
637static 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 */
659static 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;
bd46cb6c 664 u16 rxq_idx, i, num_rcvd, j;
fa77406a 665 u32 pktsize, hdr_len, curr_frag_len, size;
6b7c5b94
SP
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);
76fbb429 702 goto done;
6b7c5b94
SP
703 }
704
705 /* More frags present for this completion */
fa77406a 706 size = pktsize;
bd46cb6c 707 for (i = 1, j = 0; i < num_rcvd; i++) {
fa77406a 708 size -= curr_frag_len;
6b7c5b94
SP
709 index_inc(&rxq_idx, rxq->len);
710 page_info = get_rx_page_info(adapter, rxq_idx);
711
fa77406a 712 curr_frag_len = min(size, rx_frag_size);
6b7c5b94 713
bd46cb6c
AK
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;
6b7c5b94
SP
728 skb->len += curr_frag_len;
729 skb->data_len += curr_frag_len;
6b7c5b94
SP
730
731 memset(page_info, 0, sizeof(*page_info));
732 }
bd46cb6c 733 BUG_ON(j > MAX_SKB_FRAGS);
6b7c5b94 734
76fbb429 735done:
4097f663 736 be_rx_stats_update(adapter, pktsize, num_rcvd);
6b7c5b94
SP
737 return;
738}
739
5be93b9a 740/* Process the RX completion indicated by rxcp when GRO is disabled */
6b7c5b94
SP
741static 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;
6b7c5b94 746
6b7c5b94
SP
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
728a9972 761 if (do_pkt_csum(rxcp, adapter->rx_csum))
6b7c5b94 762 skb->ip_summed = CHECKSUM_NONE;
728a9972
AK
763 else
764 skb->ip_summed = CHECKSUM_UNNECESSARY;
6b7c5b94
SP
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
5be93b9a
AK
787/* Process the RX completion indicated by rxcp when GRO is enabled */
788static void be_rx_compl_process_gro(struct be_adapter *adapter,
6b7c5b94
SP
789 struct be_eth_rx_compl *rxcp)
790{
791 struct be_rx_page_info *page_info;
5be93b9a 792 struct sk_buff *skb = NULL;
6b7c5b94 793 struct be_queue_info *rxq = &adapter->rx_obj.q;
5be93b9a 794 struct be_eq_obj *eq_obj = &adapter->rx_eq;
6b7c5b94 795 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
bd46cb6c 796 u16 i, rxq_idx = 0, vid, j;
6b7c5b94
SP
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
5be93b9a
AK
803 skb = napi_get_frags(&eq_obj->napi);
804 if (!skb) {
805 be_rx_compl_discard(adapter, rxcp);
806 return;
807 }
808
6b7c5b94 809 remaining = pkt_size;
bd46cb6c 810 for (i = 0, j = -1; i < num_rcvd; i++) {
6b7c5b94
SP
811 page_info = get_rx_page_info(adapter, rxq_idx);
812
813 curr_frag_len = min(remaining, rx_frag_size);
814
bd46cb6c
AK
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++;
5be93b9a
AK
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;
bd46cb6c
AK
823 } else {
824 put_page(page_info->page);
825 }
5be93b9a 826 skb_shinfo(skb)->frags[j].size += curr_frag_len;
6b7c5b94 827
bd46cb6c 828 remaining -= curr_frag_len;
6b7c5b94 829 index_inc(&rxq_idx, rxq->len);
6b7c5b94
SP
830 memset(page_info, 0, sizeof(*page_info));
831 }
bd46cb6c 832 BUG_ON(j > MAX_SKB_FRAGS);
6b7c5b94 833
5be93b9a
AK
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
6b7c5b94 840 if (likely(!vlanf)) {
5be93b9a 841 napi_gro_frags(&eq_obj->napi);
6b7c5b94
SP
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
5be93b9a 849 vlan_gro_frags(&eq_obj->napi, adapter->vlan_grp, vid);
6b7c5b94
SP
850 }
851
4097f663 852 be_rx_stats_update(adapter, pkt_size, num_rcvd);
6b7c5b94
SP
853 return;
854}
855
856static 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
6b7c5b94
SP
865 queue_tail_inc(&adapter->rx_obj.cq);
866 return rxcp;
867}
868
a7a0ef31
SP
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 */
873static 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
6b7c5b94
SP
878static 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 */
891static 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
6b7c5b94
SP
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) {
6b7c5b94 939 atomic_add(posted, &rxq->used);
8788fdc2 940 be_rxq_notify(adapter, rxq->id, posted);
ea1dae11
SP
941 } else if (atomic_read(&rxq->used) == 0) {
942 /* Let be_worker replenish when memory is available */
943 adapter->rx_post_starved = true;
6b7c5b94
SP
944 }
945
946 return;
947}
948
5fb379ee 949static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
6b7c5b94 950{
6b7c5b94
SP
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
964static 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
996static void be_rx_q_clean(struct be_adapter *adapter)
997{
998 struct be_rx_page_info *page_info;
999 struct be_queue_info *rxq = &adapter->rx_obj.q;
1000 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1001 struct be_eth_rx_compl *rxcp;
1002 u16 tail;
1003
1004 /* First cleanup pending rx completions */
1005 while ((rxcp = be_rx_compl_get(adapter)) != NULL) {
1006 be_rx_compl_discard(adapter, rxcp);
a7a0ef31 1007 be_rx_compl_reset(rxcp);
8788fdc2 1008 be_cq_notify(adapter, rx_cq->id, true, 1);
6b7c5b94
SP
1009 }
1010
1011 /* Then free posted rx buffer that were not used */
1012 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
1013 for (; tail != rxq->head; index_inc(&tail, rxq->len)) {
1014 page_info = get_rx_page_info(adapter, tail);
1015 put_page(page_info->page);
1016 memset(page_info, 0, sizeof(*page_info));
1017 }
1018 BUG_ON(atomic_read(&rxq->used));
1019}
1020
1021static void be_tx_q_clean(struct be_adapter *adapter)
1022{
1023 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1024 struct sk_buff *sent_skb;
1025 struct be_queue_info *txq = &adapter->tx_obj.q;
1026 u16 last_index;
1027 bool dummy_wrb;
1028
1029 while (atomic_read(&txq->used)) {
1030 sent_skb = sent_skbs[txq->tail];
1031 last_index = txq->tail;
1032 index_adv(&last_index,
1033 wrb_cnt_for_skb(sent_skb, &dummy_wrb) - 1, txq->len);
1034 be_tx_compl_process(adapter, last_index);
1035 }
1036}
1037
5fb379ee
SP
1038static void be_mcc_queues_destroy(struct be_adapter *adapter)
1039{
1040 struct be_queue_info *q;
5fb379ee 1041
8788fdc2 1042 q = &adapter->mcc_obj.q;
5fb379ee 1043 if (q->created)
8788fdc2 1044 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
5fb379ee
SP
1045 be_queue_free(adapter, q);
1046
8788fdc2 1047 q = &adapter->mcc_obj.cq;
5fb379ee 1048 if (q->created)
8788fdc2 1049 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
5fb379ee
SP
1050 be_queue_free(adapter, q);
1051}
1052
1053/* Must be called only after TX qs are created as MCC shares TX EQ */
1054static int be_mcc_queues_create(struct be_adapter *adapter)
1055{
1056 struct be_queue_info *q, *cq;
5fb379ee
SP
1057
1058 /* Alloc MCC compl queue */
8788fdc2 1059 cq = &adapter->mcc_obj.cq;
5fb379ee 1060 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
efd2e40a 1061 sizeof(struct be_mcc_compl)))
5fb379ee
SP
1062 goto err;
1063
1064 /* Ask BE to create MCC compl queue; share TX's eq */
8788fdc2 1065 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
5fb379ee
SP
1066 goto mcc_cq_free;
1067
1068 /* Alloc MCC queue */
8788fdc2 1069 q = &adapter->mcc_obj.q;
5fb379ee
SP
1070 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1071 goto mcc_cq_destroy;
1072
1073 /* Ask BE to create MCC queue */
8788fdc2 1074 if (be_cmd_mccq_create(adapter, q, cq))
5fb379ee
SP
1075 goto mcc_q_free;
1076
1077 return 0;
1078
1079mcc_q_free:
1080 be_queue_free(adapter, q);
1081mcc_cq_destroy:
8788fdc2 1082 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
5fb379ee
SP
1083mcc_cq_free:
1084 be_queue_free(adapter, cq);
1085err:
1086 return -1;
1087}
1088
6b7c5b94
SP
1089static void be_tx_queues_destroy(struct be_adapter *adapter)
1090{
1091 struct be_queue_info *q;
1092
1093 q = &adapter->tx_obj.q;
934037bc 1094 if (q->created) {
8788fdc2 1095 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
934037bc
SP
1096
1097 /* No more tx completions can be rcvd now; clean up if there
1098 * are any pending completions or pending tx requests */
1099 be_tx_q_clean(adapter);
1100 }
6b7c5b94
SP
1101 be_queue_free(adapter, q);
1102
1103 q = &adapter->tx_obj.cq;
1104 if (q->created)
8788fdc2 1105 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
6b7c5b94
SP
1106 be_queue_free(adapter, q);
1107
6b7c5b94
SP
1108 q = &adapter->tx_eq.q;
1109 if (q->created)
8788fdc2 1110 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
6b7c5b94
SP
1111 be_queue_free(adapter, q);
1112}
1113
1114static int be_tx_queues_create(struct be_adapter *adapter)
1115{
1116 struct be_queue_info *eq, *q, *cq;
1117
1118 adapter->tx_eq.max_eqd = 0;
1119 adapter->tx_eq.min_eqd = 0;
1120 adapter->tx_eq.cur_eqd = 96;
1121 adapter->tx_eq.enable_aic = false;
1122 /* Alloc Tx Event queue */
1123 eq = &adapter->tx_eq.q;
1124 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1125 return -1;
1126
1127 /* Ask BE to create Tx Event queue */
8788fdc2 1128 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
6b7c5b94
SP
1129 goto tx_eq_free;
1130 /* Alloc TX eth compl queue */
1131 cq = &adapter->tx_obj.cq;
1132 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1133 sizeof(struct be_eth_tx_compl)))
1134 goto tx_eq_destroy;
1135
1136 /* Ask BE to create Tx eth compl queue */
8788fdc2 1137 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
6b7c5b94
SP
1138 goto tx_cq_free;
1139
1140 /* Alloc TX eth queue */
1141 q = &adapter->tx_obj.q;
1142 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1143 goto tx_cq_destroy;
1144
1145 /* Ask BE to create Tx eth queue */
8788fdc2 1146 if (be_cmd_txq_create(adapter, q, cq))
6b7c5b94
SP
1147 goto tx_q_free;
1148 return 0;
1149
1150tx_q_free:
1151 be_queue_free(adapter, q);
1152tx_cq_destroy:
8788fdc2 1153 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
6b7c5b94
SP
1154tx_cq_free:
1155 be_queue_free(adapter, cq);
1156tx_eq_destroy:
8788fdc2 1157 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
6b7c5b94
SP
1158tx_eq_free:
1159 be_queue_free(adapter, eq);
1160 return -1;
1161}
1162
1163static void be_rx_queues_destroy(struct be_adapter *adapter)
1164{
1165 struct be_queue_info *q;
1166
1167 q = &adapter->rx_obj.q;
1168 if (q->created) {
8788fdc2 1169 be_cmd_q_destroy(adapter, q, QTYPE_RXQ);
6b7c5b94
SP
1170 be_rx_q_clean(adapter);
1171 }
1172 be_queue_free(adapter, q);
1173
1174 q = &adapter->rx_obj.cq;
1175 if (q->created)
8788fdc2 1176 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
6b7c5b94
SP
1177 be_queue_free(adapter, q);
1178
1179 q = &adapter->rx_eq.q;
1180 if (q->created)
8788fdc2 1181 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
6b7c5b94
SP
1182 be_queue_free(adapter, q);
1183}
1184
1185static int be_rx_queues_create(struct be_adapter *adapter)
1186{
1187 struct be_queue_info *eq, *q, *cq;
1188 int rc;
1189
6b7c5b94
SP
1190 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
1191 adapter->rx_eq.max_eqd = BE_MAX_EQD;
1192 adapter->rx_eq.min_eqd = 0;
1193 adapter->rx_eq.cur_eqd = 0;
1194 adapter->rx_eq.enable_aic = true;
1195
1196 /* Alloc Rx Event queue */
1197 eq = &adapter->rx_eq.q;
1198 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1199 sizeof(struct be_eq_entry));
1200 if (rc)
1201 return rc;
1202
1203 /* Ask BE to create Rx Event queue */
8788fdc2 1204 rc = be_cmd_eq_create(adapter, eq, adapter->rx_eq.cur_eqd);
6b7c5b94
SP
1205 if (rc)
1206 goto rx_eq_free;
1207
1208 /* Alloc RX eth compl queue */
1209 cq = &adapter->rx_obj.cq;
1210 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1211 sizeof(struct be_eth_rx_compl));
1212 if (rc)
1213 goto rx_eq_destroy;
1214
1215 /* Ask BE to create Rx eth compl queue */
8788fdc2 1216 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
6b7c5b94
SP
1217 if (rc)
1218 goto rx_cq_free;
1219
1220 /* Alloc RX eth queue */
1221 q = &adapter->rx_obj.q;
1222 rc = be_queue_alloc(adapter, q, RX_Q_LEN, sizeof(struct be_eth_rx_d));
1223 if (rc)
1224 goto rx_cq_destroy;
1225
1226 /* Ask BE to create Rx eth queue */
8788fdc2 1227 rc = be_cmd_rxq_create(adapter, q, cq->id, rx_frag_size,
6b7c5b94
SP
1228 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle, false);
1229 if (rc)
1230 goto rx_q_free;
1231
1232 return 0;
1233rx_q_free:
1234 be_queue_free(adapter, q);
1235rx_cq_destroy:
8788fdc2 1236 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
6b7c5b94
SP
1237rx_cq_free:
1238 be_queue_free(adapter, cq);
1239rx_eq_destroy:
8788fdc2 1240 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
6b7c5b94
SP
1241rx_eq_free:
1242 be_queue_free(adapter, eq);
1243 return rc;
1244}
1245static bool event_get(struct be_eq_obj *eq_obj, u16 *rid)
1246{
1247 struct be_eq_entry *entry = queue_tail_node(&eq_obj->q);
1248 u32 evt = entry->evt;
1249
1250 if (!evt)
1251 return false;
1252
1253 evt = le32_to_cpu(evt);
1254 *rid = (evt >> EQ_ENTRY_RES_ID_SHIFT) & EQ_ENTRY_RES_ID_MASK;
1255 entry->evt = 0;
1256 queue_tail_inc(&eq_obj->q);
1257 return true;
1258}
1259
8788fdc2 1260static int event_handle(struct be_adapter *adapter, struct be_eq_obj *eq_obj)
6b7c5b94
SP
1261{
1262 u16 rid = 0, num = 0;
1263
1264 while (event_get(eq_obj, &rid))
1265 num++;
1266
1267 /* We can see an interrupt and no event */
8788fdc2 1268 be_eq_notify(adapter, eq_obj->q.id, true, true, num);
6b7c5b94
SP
1269 if (num)
1270 napi_schedule(&eq_obj->napi);
1271
1272 return num;
1273}
1274
1275static irqreturn_t be_intx(int irq, void *dev)
1276{
1277 struct be_adapter *adapter = dev;
8788fdc2 1278 int isr;
6b7c5b94 1279
8788fdc2 1280 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
eec368fb 1281 be_pci_func(adapter) * CEV_ISR_SIZE);
c001c213 1282 if (!isr)
8788fdc2 1283 return IRQ_NONE;
6b7c5b94 1284
8788fdc2
SP
1285 event_handle(adapter, &adapter->tx_eq);
1286 event_handle(adapter, &adapter->rx_eq);
c001c213 1287
8788fdc2 1288 return IRQ_HANDLED;
6b7c5b94
SP
1289}
1290
1291static irqreturn_t be_msix_rx(int irq, void *dev)
1292{
1293 struct be_adapter *adapter = dev;
1294
8788fdc2 1295 event_handle(adapter, &adapter->rx_eq);
6b7c5b94
SP
1296
1297 return IRQ_HANDLED;
1298}
1299
5fb379ee 1300static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
6b7c5b94
SP
1301{
1302 struct be_adapter *adapter = dev;
1303
8788fdc2 1304 event_handle(adapter, &adapter->tx_eq);
6b7c5b94
SP
1305
1306 return IRQ_HANDLED;
1307}
1308
5be93b9a 1309static inline bool do_gro(struct be_adapter *adapter,
6b7c5b94
SP
1310 struct be_eth_rx_compl *rxcp)
1311{
1312 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1313 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1314
1315 if (err)
1316 drvr_stats(adapter)->be_rxcp_err++;
1317
5be93b9a 1318 return (tcp_frame && !err) ? true : false;
6b7c5b94
SP
1319}
1320
1321int be_poll_rx(struct napi_struct *napi, int budget)
1322{
1323 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
1324 struct be_adapter *adapter =
1325 container_of(rx_eq, struct be_adapter, rx_eq);
1326 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1327 struct be_eth_rx_compl *rxcp;
1328 u32 work_done;
1329
1330 for (work_done = 0; work_done < budget; work_done++) {
1331 rxcp = be_rx_compl_get(adapter);
1332 if (!rxcp)
1333 break;
1334
5be93b9a
AK
1335 if (do_gro(adapter, rxcp))
1336 be_rx_compl_process_gro(adapter, rxcp);
6b7c5b94
SP
1337 else
1338 be_rx_compl_process(adapter, rxcp);
a7a0ef31
SP
1339
1340 be_rx_compl_reset(rxcp);
6b7c5b94
SP
1341 }
1342
6b7c5b94
SP
1343 /* Refill the queue */
1344 if (atomic_read(&adapter->rx_obj.q.used) < RX_FRAGS_REFILL_WM)
1345 be_post_rx_frags(adapter);
1346
1347 /* All consumed */
1348 if (work_done < budget) {
1349 napi_complete(napi);
8788fdc2 1350 be_cq_notify(adapter, rx_cq->id, true, work_done);
6b7c5b94
SP
1351 } else {
1352 /* More to be consumed; continue with interrupts disabled */
8788fdc2 1353 be_cq_notify(adapter, rx_cq->id, false, work_done);
6b7c5b94
SP
1354 }
1355 return work_done;
1356}
1357
5fb379ee 1358void be_process_tx(struct be_adapter *adapter)
6b7c5b94 1359{
5fb379ee
SP
1360 struct be_queue_info *txq = &adapter->tx_obj.q;
1361 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
6b7c5b94
SP
1362 struct be_eth_tx_compl *txcp;
1363 u32 num_cmpl = 0;
1364 u16 end_idx;
1365
5fb379ee 1366 while ((txcp = be_tx_compl_get(tx_cq))) {
6b7c5b94
SP
1367 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1368 wrb_index, txcp);
1369 be_tx_compl_process(adapter, end_idx);
1370 num_cmpl++;
1371 }
1372
5fb379ee 1373 if (num_cmpl) {
8788fdc2 1374 be_cq_notify(adapter, tx_cq->id, true, num_cmpl);
5fb379ee
SP
1375
1376 /* As Tx wrbs have been freed up, wake up netdev queue if
1377 * it was stopped due to lack of tx wrbs.
1378 */
1379 if (netif_queue_stopped(adapter->netdev) &&
6b7c5b94 1380 atomic_read(&txq->used) < txq->len / 2) {
5fb379ee
SP
1381 netif_wake_queue(adapter->netdev);
1382 }
1383
1384 drvr_stats(adapter)->be_tx_events++;
1385 drvr_stats(adapter)->be_tx_compl += num_cmpl;
6b7c5b94 1386 }
5fb379ee
SP
1387}
1388
1389/* As TX and MCC share the same EQ check for both TX and MCC completions.
1390 * For TX/MCC we don't honour budget; consume everything
1391 */
1392static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
1393{
1394 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1395 struct be_adapter *adapter =
1396 container_of(tx_eq, struct be_adapter, tx_eq);
6b7c5b94
SP
1397
1398 napi_complete(napi);
1399
5fb379ee 1400 be_process_tx(adapter);
6b7c5b94 1401
8788fdc2 1402 be_process_mcc(adapter);
6b7c5b94
SP
1403
1404 return 1;
1405}
1406
ea1dae11
SP
1407static void be_worker(struct work_struct *work)
1408{
1409 struct be_adapter *adapter =
1410 container_of(work, struct be_adapter, work.work);
1411 int status;
1412
ea1dae11 1413 /* Get Stats */
8788fdc2 1414 status = be_cmd_get_stats(adapter, &adapter->stats.cmd);
ea1dae11
SP
1415 if (!status)
1416 netdev_stats_update(adapter);
1417
1418 /* Set EQ delay */
1419 be_rx_eqd_update(adapter);
1420
4097f663
SP
1421 be_tx_rate_update(adapter);
1422 be_rx_rate_update(adapter);
1423
ea1dae11
SP
1424 if (adapter->rx_post_starved) {
1425 adapter->rx_post_starved = false;
1426 be_post_rx_frags(adapter);
1427 }
1428
1429 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1430}
1431
6b7c5b94
SP
1432static void be_msix_enable(struct be_adapter *adapter)
1433{
1434 int i, status;
1435
1436 for (i = 0; i < BE_NUM_MSIX_VECTORS; i++)
1437 adapter->msix_entries[i].entry = i;
1438
1439 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1440 BE_NUM_MSIX_VECTORS);
1441 if (status == 0)
1442 adapter->msix_enabled = true;
1443 return;
1444}
1445
1446static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1447{
eec368fb 1448 return adapter->msix_entries[eq_id - 8 * be_pci_func(adapter)].vector;
6b7c5b94
SP
1449}
1450
1451static int be_msix_register(struct be_adapter *adapter)
1452{
1453 struct net_device *netdev = adapter->netdev;
1454 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1455 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1456 int status, vec;
1457
1458 sprintf(tx_eq->desc, "%s-tx", netdev->name);
1459 vec = be_msix_vec_get(adapter, tx_eq->q.id);
5fb379ee 1460 status = request_irq(vec, be_msix_tx_mcc, 0, tx_eq->desc, adapter);
6b7c5b94
SP
1461 if (status)
1462 goto err;
1463
1464 sprintf(rx_eq->desc, "%s-rx", netdev->name);
1465 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1466 status = request_irq(vec, be_msix_rx, 0, rx_eq->desc, adapter);
1467 if (status) { /* Free TX IRQ */
1468 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1469 free_irq(vec, adapter);
1470 goto err;
1471 }
1472 return 0;
1473err:
1474 dev_warn(&adapter->pdev->dev,
1475 "MSIX Request IRQ failed - err %d\n", status);
1476 pci_disable_msix(adapter->pdev);
1477 adapter->msix_enabled = false;
1478 return status;
1479}
1480
1481static int be_irq_register(struct be_adapter *adapter)
1482{
1483 struct net_device *netdev = adapter->netdev;
1484 int status;
1485
1486 if (adapter->msix_enabled) {
1487 status = be_msix_register(adapter);
1488 if (status == 0)
1489 goto done;
1490 }
1491
1492 /* INTx */
1493 netdev->irq = adapter->pdev->irq;
1494 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1495 adapter);
1496 if (status) {
1497 dev_err(&adapter->pdev->dev,
1498 "INTx request IRQ failed - err %d\n", status);
1499 return status;
1500 }
1501done:
1502 adapter->isr_registered = true;
1503 return 0;
1504}
1505
1506static void be_irq_unregister(struct be_adapter *adapter)
1507{
1508 struct net_device *netdev = adapter->netdev;
1509 int vec;
1510
1511 if (!adapter->isr_registered)
1512 return;
1513
1514 /* INTx */
1515 if (!adapter->msix_enabled) {
1516 free_irq(netdev->irq, adapter);
1517 goto done;
1518 }
1519
1520 /* MSIx */
1521 vec = be_msix_vec_get(adapter, adapter->tx_eq.q.id);
1522 free_irq(vec, adapter);
1523 vec = be_msix_vec_get(adapter, adapter->rx_eq.q.id);
1524 free_irq(vec, adapter);
1525done:
1526 adapter->isr_registered = false;
1527 return;
1528}
1529
1530static int be_open(struct net_device *netdev)
1531{
1532 struct be_adapter *adapter = netdev_priv(netdev);
6b7c5b94
SP
1533 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1534 struct be_eq_obj *tx_eq = &adapter->tx_eq;
a8f447bd
SP
1535 bool link_up;
1536 int status;
5fb379ee
SP
1537
1538 /* First time posting */
1539 be_post_rx_frags(adapter);
1540
1541 napi_enable(&rx_eq->napi);
1542 napi_enable(&tx_eq->napi);
1543
1544 be_irq_register(adapter);
1545
8788fdc2 1546 be_intr_set(adapter, true);
5fb379ee
SP
1547
1548 /* The evt queues are created in unarmed state; arm them */
8788fdc2
SP
1549 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
1550 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
5fb379ee
SP
1551
1552 /* Rx compl queue may be in unarmed state; rearm it */
8788fdc2 1553 be_cq_notify(adapter, adapter->rx_obj.cq.id, true, 0);
5fb379ee 1554
8788fdc2 1555 status = be_cmd_link_status_query(adapter, &link_up);
a8f447bd
SP
1556 if (status)
1557 return status;
1558 be_link_status_update(adapter, link_up);
5fb379ee
SP
1559
1560 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
1561 return 0;
1562}
1563
1564static int be_setup(struct be_adapter *adapter)
1565{
5fb379ee 1566 struct net_device *netdev = adapter->netdev;
6b7c5b94
SP
1567 u32 if_flags;
1568 int status;
1569
1570 if_flags = BE_IF_FLAGS_BROADCAST | BE_IF_FLAGS_PROMISCUOUS |
1571 BE_IF_FLAGS_MCAST_PROMISCUOUS | BE_IF_FLAGS_UNTAGGED |
1572 BE_IF_FLAGS_PASS_L3L4_ERRORS;
8788fdc2 1573 status = be_cmd_if_create(adapter, if_flags, netdev->dev_addr,
6b7c5b94
SP
1574 false/* pmac_invalid */, &adapter->if_handle,
1575 &adapter->pmac_id);
1576 if (status != 0)
1577 goto do_none;
1578
1ab1ab75
SP
1579 be_vid_config(netdev);
1580
8788fdc2 1581 status = be_cmd_set_flow_control(adapter, true, true);
6b7c5b94
SP
1582 if (status != 0)
1583 goto if_destroy;
1584
1585 status = be_tx_queues_create(adapter);
1586 if (status != 0)
1587 goto if_destroy;
1588
1589 status = be_rx_queues_create(adapter);
1590 if (status != 0)
1591 goto tx_qs_destroy;
1592
5fb379ee
SP
1593 status = be_mcc_queues_create(adapter);
1594 if (status != 0)
1595 goto rx_qs_destroy;
6b7c5b94 1596
6b7c5b94
SP
1597 return 0;
1598
5fb379ee
SP
1599rx_qs_destroy:
1600 be_rx_queues_destroy(adapter);
6b7c5b94
SP
1601tx_qs_destroy:
1602 be_tx_queues_destroy(adapter);
1603if_destroy:
8788fdc2 1604 be_cmd_if_destroy(adapter, adapter->if_handle);
6b7c5b94
SP
1605do_none:
1606 return status;
1607}
1608
5fb379ee
SP
1609static int be_clear(struct be_adapter *adapter)
1610{
5fb379ee
SP
1611 be_rx_queues_destroy(adapter);
1612 be_tx_queues_destroy(adapter);
1613
8788fdc2 1614 be_cmd_if_destroy(adapter, adapter->if_handle);
5fb379ee
SP
1615
1616 be_mcc_queues_destroy(adapter);
1617 return 0;
1618}
1619
6b7c5b94
SP
1620static int be_close(struct net_device *netdev)
1621{
1622 struct be_adapter *adapter = netdev_priv(netdev);
6b7c5b94
SP
1623 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1624 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1625 int vec;
1626
b305be78 1627 cancel_delayed_work_sync(&adapter->work);
6b7c5b94
SP
1628
1629 netif_stop_queue(netdev);
1630 netif_carrier_off(netdev);
a8f447bd 1631 adapter->link_up = false;
6b7c5b94 1632
8788fdc2 1633 be_intr_set(adapter, false);
6b7c5b94
SP
1634
1635 if (adapter->msix_enabled) {
1636 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1637 synchronize_irq(vec);
1638 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1639 synchronize_irq(vec);
1640 } else {
1641 synchronize_irq(netdev->irq);
1642 }
1643 be_irq_unregister(adapter);
1644
1645 napi_disable(&rx_eq->napi);
1646 napi_disable(&tx_eq->napi);
1647
6b7c5b94
SP
1648 return 0;
1649}
1650
6b7c5b94
SP
1651static struct net_device_ops be_netdev_ops = {
1652 .ndo_open = be_open,
1653 .ndo_stop = be_close,
1654 .ndo_start_xmit = be_xmit,
1655 .ndo_get_stats = be_get_stats,
1656 .ndo_set_rx_mode = be_set_multicast_list,
1657 .ndo_set_mac_address = be_mac_addr_set,
1658 .ndo_change_mtu = be_change_mtu,
1659 .ndo_validate_addr = eth_validate_addr,
1660 .ndo_vlan_rx_register = be_vlan_register,
1661 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
1662 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
1663};
1664
1665static void be_netdev_init(struct net_device *netdev)
1666{
1667 struct be_adapter *adapter = netdev_priv(netdev);
1668
1669 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
1670 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_IP_CSUM |
5be93b9a 1671 NETIF_F_IPV6_CSUM | NETIF_F_GRO;
6b7c5b94
SP
1672
1673 netdev->flags |= IFF_MULTICAST;
1674
728a9972
AK
1675 adapter->rx_csum = true;
1676
6b7c5b94
SP
1677 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
1678
1679 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
1680
6b7c5b94
SP
1681 netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx,
1682 BE_NAPI_WEIGHT);
5fb379ee 1683 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
6b7c5b94
SP
1684 BE_NAPI_WEIGHT);
1685
1686 netif_carrier_off(netdev);
1687 netif_stop_queue(netdev);
1688}
1689
1690static void be_unmap_pci_bars(struct be_adapter *adapter)
1691{
8788fdc2
SP
1692 if (adapter->csr)
1693 iounmap(adapter->csr);
1694 if (adapter->db)
1695 iounmap(adapter->db);
1696 if (adapter->pcicfg)
1697 iounmap(adapter->pcicfg);
6b7c5b94
SP
1698}
1699
1700static int be_map_pci_bars(struct be_adapter *adapter)
1701{
1702 u8 __iomem *addr;
1703
1704 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
1705 pci_resource_len(adapter->pdev, 2));
1706 if (addr == NULL)
1707 return -ENOMEM;
8788fdc2 1708 adapter->csr = addr;
6b7c5b94
SP
1709
1710 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 4),
1711 128 * 1024);
1712 if (addr == NULL)
1713 goto pci_map_err;
8788fdc2 1714 adapter->db = addr;
6b7c5b94
SP
1715
1716 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 1),
1717 pci_resource_len(adapter->pdev, 1));
1718 if (addr == NULL)
1719 goto pci_map_err;
8788fdc2 1720 adapter->pcicfg = addr;
6b7c5b94
SP
1721
1722 return 0;
1723pci_map_err:
1724 be_unmap_pci_bars(adapter);
1725 return -ENOMEM;
1726}
1727
1728
1729static void be_ctrl_cleanup(struct be_adapter *adapter)
1730{
8788fdc2 1731 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
6b7c5b94
SP
1732
1733 be_unmap_pci_bars(adapter);
1734
1735 if (mem->va)
1736 pci_free_consistent(adapter->pdev, mem->size,
1737 mem->va, mem->dma);
1738}
1739
6b7c5b94
SP
1740static int be_ctrl_init(struct be_adapter *adapter)
1741{
8788fdc2
SP
1742 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
1743 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
6b7c5b94 1744 int status;
6b7c5b94
SP
1745
1746 status = be_map_pci_bars(adapter);
1747 if (status)
1748 return status;
1749
1750 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
1751 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
1752 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
1753 if (!mbox_mem_alloc->va) {
1754 be_unmap_pci_bars(adapter);
1755 return -1;
1756 }
1757 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
1758 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
1759 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
1760 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
8788fdc2
SP
1761 spin_lock_init(&adapter->mbox_lock);
1762 spin_lock_init(&adapter->mcc_lock);
1763 spin_lock_init(&adapter->mcc_cq_lock);
a8f447bd 1764
6b7c5b94
SP
1765 return 0;
1766}
1767
1768static void be_stats_cleanup(struct be_adapter *adapter)
1769{
1770 struct be_stats_obj *stats = &adapter->stats;
1771 struct be_dma_mem *cmd = &stats->cmd;
1772
1773 if (cmd->va)
1774 pci_free_consistent(adapter->pdev, cmd->size,
1775 cmd->va, cmd->dma);
1776}
1777
1778static int be_stats_init(struct be_adapter *adapter)
1779{
1780 struct be_stats_obj *stats = &adapter->stats;
1781 struct be_dma_mem *cmd = &stats->cmd;
1782
1783 cmd->size = sizeof(struct be_cmd_req_get_stats);
1784 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
1785 if (cmd->va == NULL)
1786 return -1;
1787 return 0;
1788}
1789
1790static void __devexit be_remove(struct pci_dev *pdev)
1791{
1792 struct be_adapter *adapter = pci_get_drvdata(pdev);
1793 if (!adapter)
1794 return;
1795
1796 unregister_netdev(adapter->netdev);
1797
5fb379ee
SP
1798 be_clear(adapter);
1799
6b7c5b94
SP
1800 be_stats_cleanup(adapter);
1801
1802 be_ctrl_cleanup(adapter);
1803
1804 if (adapter->msix_enabled) {
1805 pci_disable_msix(adapter->pdev);
1806 adapter->msix_enabled = false;
1807 }
1808
1809 pci_set_drvdata(pdev, NULL);
1810 pci_release_regions(pdev);
1811 pci_disable_device(pdev);
1812
1813 free_netdev(adapter->netdev);
1814}
1815
1816static int be_hw_up(struct be_adapter *adapter)
1817{
6b7c5b94
SP
1818 int status;
1819
8788fdc2 1820 status = be_cmd_POST(adapter);
6b7c5b94
SP
1821 if (status)
1822 return status;
1823
8788fdc2 1824 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
6b7c5b94
SP
1825 if (status)
1826 return status;
1827
8788fdc2 1828 status = be_cmd_query_fw_cfg(adapter, &adapter->port_num);
6b7c5b94
SP
1829 return status;
1830}
1831
1832static int __devinit be_probe(struct pci_dev *pdev,
1833 const struct pci_device_id *pdev_id)
1834{
1835 int status = 0;
1836 struct be_adapter *adapter;
1837 struct net_device *netdev;
6b7c5b94
SP
1838 u8 mac[ETH_ALEN];
1839
1840 status = pci_enable_device(pdev);
1841 if (status)
1842 goto do_none;
1843
1844 status = pci_request_regions(pdev, DRV_NAME);
1845 if (status)
1846 goto disable_dev;
1847 pci_set_master(pdev);
1848
1849 netdev = alloc_etherdev(sizeof(struct be_adapter));
1850 if (netdev == NULL) {
1851 status = -ENOMEM;
1852 goto rel_reg;
1853 }
1854 adapter = netdev_priv(netdev);
1855 adapter->pdev = pdev;
1856 pci_set_drvdata(pdev, adapter);
1857 adapter->netdev = netdev;
1858
1859 be_msix_enable(adapter);
1860
e930438c 1861 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6b7c5b94
SP
1862 if (!status) {
1863 netdev->features |= NETIF_F_HIGHDMA;
1864 } else {
e930438c 1865 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6b7c5b94
SP
1866 if (status) {
1867 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
1868 goto free_netdev;
1869 }
1870 }
1871
6b7c5b94
SP
1872 status = be_ctrl_init(adapter);
1873 if (status)
1874 goto free_netdev;
1875
1876 status = be_stats_init(adapter);
1877 if (status)
1878 goto ctrl_clean;
1879
1880 status = be_hw_up(adapter);
1881 if (status)
1882 goto stats_clean;
1883
8788fdc2 1884 status = be_cmd_mac_addr_query(adapter, mac, MAC_ADDRESS_TYPE_NETWORK,
6b7c5b94
SP
1885 true /* permanent */, 0);
1886 if (status)
1887 goto stats_clean;
1888 memcpy(netdev->dev_addr, mac, ETH_ALEN);
1889
1890 INIT_DELAYED_WORK(&adapter->work, be_worker);
1891 be_netdev_init(netdev);
1892 SET_NETDEV_DEV(netdev, &adapter->pdev->dev);
1893
5fb379ee
SP
1894 status = be_setup(adapter);
1895 if (status)
1896 goto stats_clean;
6b7c5b94
SP
1897 status = register_netdev(netdev);
1898 if (status != 0)
5fb379ee 1899 goto unsetup;
6b7c5b94 1900
c4ca2374 1901 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
6b7c5b94
SP
1902 return 0;
1903
5fb379ee
SP
1904unsetup:
1905 be_clear(adapter);
6b7c5b94
SP
1906stats_clean:
1907 be_stats_cleanup(adapter);
1908ctrl_clean:
1909 be_ctrl_cleanup(adapter);
1910free_netdev:
1911 free_netdev(adapter->netdev);
1912rel_reg:
1913 pci_release_regions(pdev);
1914disable_dev:
1915 pci_disable_device(pdev);
1916do_none:
c4ca2374 1917 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
6b7c5b94
SP
1918 return status;
1919}
1920
1921static int be_suspend(struct pci_dev *pdev, pm_message_t state)
1922{
1923 struct be_adapter *adapter = pci_get_drvdata(pdev);
1924 struct net_device *netdev = adapter->netdev;
1925
1926 netif_device_detach(netdev);
1927 if (netif_running(netdev)) {
1928 rtnl_lock();
1929 be_close(netdev);
5fb379ee 1930 be_clear(adapter);
6b7c5b94
SP
1931 rtnl_unlock();
1932 }
1933
1934 pci_save_state(pdev);
1935 pci_disable_device(pdev);
1936 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1937 return 0;
1938}
1939
1940static int be_resume(struct pci_dev *pdev)
1941{
1942 int status = 0;
1943 struct be_adapter *adapter = pci_get_drvdata(pdev);
1944 struct net_device *netdev = adapter->netdev;
1945
1946 netif_device_detach(netdev);
1947
1948 status = pci_enable_device(pdev);
1949 if (status)
1950 return status;
1951
1952 pci_set_power_state(pdev, 0);
1953 pci_restore_state(pdev);
1954
6b7c5b94
SP
1955 if (netif_running(netdev)) {
1956 rtnl_lock();
5fb379ee 1957 be_setup(adapter);
6b7c5b94
SP
1958 be_open(netdev);
1959 rtnl_unlock();
1960 }
1961 netif_device_attach(netdev);
1962 return 0;
1963}
1964
1965static struct pci_driver be_driver = {
1966 .name = DRV_NAME,
1967 .id_table = be_dev_ids,
1968 .probe = be_probe,
1969 .remove = be_remove,
1970 .suspend = be_suspend,
1971 .resume = be_resume
1972};
1973
1974static int __init be_init_module(void)
1975{
1976 if (rx_frag_size != 8192 && rx_frag_size != 4096
1977 && rx_frag_size != 2048) {
1978 printk(KERN_WARNING DRV_NAME
1979 " : Module param rx_frag_size must be 2048/4096/8192."
1980 " Using 2048\n");
1981 rx_frag_size = 2048;
1982 }
6b7c5b94
SP
1983
1984 return pci_register_driver(&be_driver);
1985}
1986module_init(be_init_module);
1987
1988static void __exit be_exit_module(void)
1989{
1990 pci_unregister_driver(&be_driver);
1991}
1992module_exit(be_exit_module);