blob: 68e7848ac0b0ae6f9c4e8d94452c94db3c61b817 [file] [log] [blame]
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001/*
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"
Sathya Perla8788fdc2009-07-27 22:52:03 +000019#include "be_cmds.h"
Stephen Hemminger65f71b82009-03-27 00:25:24 -070020#include <asm/div64.h>
Sathya Perla6b7c5b92009-03-11 23:32:03 -070021
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
Sathya Perla6b7c5b92009-03-11 23:32:03 -070032static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
Ajit Khapardec4ca2372009-05-18 15:38:55 -070033 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Ajit Khaparde59fd5d82009-10-29 01:11:06 -070034 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Ajit Khapardec4ca2372009-05-18 15:38:55 -070035 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
36 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
Sathya Perla6b7c5b92009-03-11 23:32:03 -070037 { 0 }
38};
39MODULE_DEVICE_TABLE(pci, be_dev_ids);
40
41static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
42{
43 struct be_dma_mem *mem = &q->dma_mem;
44 if (mem->va)
45 pci_free_consistent(adapter->pdev, mem->size,
46 mem->va, mem->dma);
47}
48
49static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
50 u16 len, u16 entry_size)
51{
52 struct be_dma_mem *mem = &q->dma_mem;
53
54 memset(q, 0, sizeof(*q));
55 q->len = len;
56 q->entry_size = entry_size;
57 mem->size = len * entry_size;
58 mem->va = pci_alloc_consistent(adapter->pdev, mem->size, &mem->dma);
59 if (!mem->va)
60 return -1;
61 memset(mem->va, 0, mem->size);
62 return 0;
63}
64
Sathya Perla8788fdc2009-07-27 22:52:03 +000065static void be_intr_set(struct be_adapter *adapter, bool enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070066{
Sathya Perla8788fdc2009-07-27 22:52:03 +000067 u8 __iomem *addr = adapter->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
Sathya Perla6b7c5b92009-03-11 23:32:03 -070068 u32 reg = ioread32(addr);
69 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +000070
Sathya Perlacf588472010-02-14 21:22:01 +000071 if (adapter->eeh_err)
72 return;
73
Sathya Perla5f0b8492009-07-27 22:52:56 +000074 if (!enabled && enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070075 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +000076 else if (enabled && !enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070077 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +000078 else
Sathya Perla6b7c5b92009-03-11 23:32:03 -070079 return;
Sathya Perla5f0b8492009-07-27 22:52:56 +000080
Sathya Perla6b7c5b92009-03-11 23:32:03 -070081 iowrite32(reg, addr);
82}
83
Sathya Perla8788fdc2009-07-27 22:52:03 +000084static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070085{
86 u32 val = 0;
87 val |= qid & DB_RQ_RING_ID_MASK;
88 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +000089 iowrite32(val, adapter->db + DB_RQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -070090}
91
Sathya Perla8788fdc2009-07-27 22:52:03 +000092static void be_txq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070093{
94 u32 val = 0;
95 val |= qid & DB_TXULP_RING_ID_MASK;
96 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +000097 iowrite32(val, adapter->db + DB_TXULP1_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -070098}
99
Sathya Perla8788fdc2009-07-27 22:52:03 +0000100static void be_eq_notify(struct be_adapter *adapter, u16 qid,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700101 bool arm, bool clear_int, u16 num_popped)
102{
103 u32 val = 0;
104 val |= qid & DB_EQ_RING_ID_MASK;
Sathya Perlacf588472010-02-14 21:22:01 +0000105
106 if (adapter->eeh_err)
107 return;
108
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700109 if (arm)
110 val |= 1 << DB_EQ_REARM_SHIFT;
111 if (clear_int)
112 val |= 1 << DB_EQ_CLR_SHIFT;
113 val |= 1 << DB_EQ_EVNT_SHIFT;
114 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000115 iowrite32(val, adapter->db + DB_EQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700116}
117
Sathya Perla8788fdc2009-07-27 22:52:03 +0000118void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700119{
120 u32 val = 0;
121 val |= qid & DB_CQ_RING_ID_MASK;
Sathya Perlacf588472010-02-14 21:22:01 +0000122
123 if (adapter->eeh_err)
124 return;
125
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700126 if (arm)
127 val |= 1 << DB_CQ_REARM_SHIFT;
128 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000129 iowrite32(val, adapter->db + DB_CQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700130}
131
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700132static int be_mac_addr_set(struct net_device *netdev, void *p)
133{
134 struct be_adapter *adapter = netdev_priv(netdev);
135 struct sockaddr *addr = p;
136 int status = 0;
137
Ajit Khapardeca9e4982009-11-29 17:56:26 +0000138 if (!is_valid_ether_addr(addr->sa_data))
139 return -EADDRNOTAVAIL;
140
Sathya Perlaa65027e2009-08-17 00:58:04 +0000141 status = be_cmd_pmac_del(adapter, adapter->if_handle, adapter->pmac_id);
142 if (status)
143 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700144
Sathya Perlaa65027e2009-08-17 00:58:04 +0000145 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
146 adapter->if_handle, &adapter->pmac_id);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700147 if (!status)
148 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
149
150 return status;
151}
152
Sathya Perlab31c50a2009-09-17 10:30:13 -0700153void netdev_stats_update(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700154{
155 struct be_hw_stats *hw_stats = hw_stats_from_cmd(adapter->stats.cmd.va);
156 struct be_rxf_stats *rxf_stats = &hw_stats->rxf;
157 struct be_port_rxf_stats *port_stats =
158 &rxf_stats->port[adapter->port_num];
Ajit Khaparde78122a52009-10-07 03:11:20 -0700159 struct net_device_stats *dev_stats = &adapter->netdev->stats;
Sathya Perla68110862009-06-10 02:21:16 +0000160 struct be_erx_stats *erx_stats = &hw_stats->erx;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700161
162 dev_stats->rx_packets = port_stats->rx_total_frames;
163 dev_stats->tx_packets = port_stats->tx_unicastframes +
164 port_stats->tx_multicastframes + port_stats->tx_broadcastframes;
165 dev_stats->rx_bytes = (u64) port_stats->rx_bytes_msd << 32 |
166 (u64) port_stats->rx_bytes_lsd;
167 dev_stats->tx_bytes = (u64) port_stats->tx_bytes_msd << 32 |
168 (u64) port_stats->tx_bytes_lsd;
169
170 /* bad pkts received */
171 dev_stats->rx_errors = port_stats->rx_crc_errors +
172 port_stats->rx_alignment_symbol_errors +
173 port_stats->rx_in_range_errors +
Sathya Perla68110862009-06-10 02:21:16 +0000174 port_stats->rx_out_range_errors +
175 port_stats->rx_frame_too_long +
176 port_stats->rx_dropped_too_small +
177 port_stats->rx_dropped_too_short +
178 port_stats->rx_dropped_header_too_small +
179 port_stats->rx_dropped_tcp_length +
180 port_stats->rx_dropped_runt +
181 port_stats->rx_tcp_checksum_errs +
182 port_stats->rx_ip_checksum_errs +
183 port_stats->rx_udp_checksum_errs;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700184
Sathya Perla68110862009-06-10 02:21:16 +0000185 /* no space in linux buffers: best possible approximation */
Sathya Perla01ed30d2009-11-22 22:01:31 +0000186 dev_stats->rx_dropped =
187 erx_stats->rx_drops_no_fragments[adapter->rx_obj.q.id];
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700188
189 /* detailed rx errors */
190 dev_stats->rx_length_errors = port_stats->rx_in_range_errors +
Sathya Perla68110862009-06-10 02:21:16 +0000191 port_stats->rx_out_range_errors +
192 port_stats->rx_frame_too_long;
193
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700194 /* receive ring buffer overflow */
195 dev_stats->rx_over_errors = 0;
Sathya Perla68110862009-06-10 02:21:16 +0000196
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700197 dev_stats->rx_crc_errors = port_stats->rx_crc_errors;
198
199 /* frame alignment errors */
200 dev_stats->rx_frame_errors = port_stats->rx_alignment_symbol_errors;
Sathya Perla68110862009-06-10 02:21:16 +0000201
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700202 /* receiver fifo overrun */
203 /* drops_no_pbuf is no per i/f, it's per BE card */
204 dev_stats->rx_fifo_errors = port_stats->rx_fifo_overflow +
205 port_stats->rx_input_fifo_overflow +
206 rxf_stats->rx_drops_no_pbuf;
207 /* receiver missed packetd */
208 dev_stats->rx_missed_errors = 0;
Sathya Perla68110862009-06-10 02:21:16 +0000209
210 /* packet transmit problems */
211 dev_stats->tx_errors = 0;
212
213 /* no space available in linux */
214 dev_stats->tx_dropped = 0;
215
Ajit Khapardec5b9b922009-10-05 02:21:51 +0000216 dev_stats->multicast = port_stats->rx_multicast_frames;
Sathya Perla68110862009-06-10 02:21:16 +0000217 dev_stats->collisions = 0;
218
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700219 /* detailed tx_errors */
220 dev_stats->tx_aborted_errors = 0;
221 dev_stats->tx_carrier_errors = 0;
222 dev_stats->tx_fifo_errors = 0;
223 dev_stats->tx_heartbeat_errors = 0;
224 dev_stats->tx_window_errors = 0;
225}
226
Sathya Perla8788fdc2009-07-27 22:52:03 +0000227void be_link_status_update(struct be_adapter *adapter, bool link_up)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700228{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700229 struct net_device *netdev = adapter->netdev;
230
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700231 /* If link came up or went down */
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000232 if (adapter->link_up != link_up) {
Ajit Khaparde0dffc832009-11-29 17:57:46 +0000233 adapter->link_speed = -1;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000234 if (link_up) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700235 netif_start_queue(netdev);
236 netif_carrier_on(netdev);
237 printk(KERN_INFO "%s: Link up\n", netdev->name);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000238 } else {
239 netif_stop_queue(netdev);
240 netif_carrier_off(netdev);
241 printk(KERN_INFO "%s: Link down\n", netdev->name);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700242 }
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000243 adapter->link_up = link_up;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700244 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700245}
246
247/* Update the EQ delay n BE based on the RX frags consumed / sec */
248static void be_rx_eqd_update(struct be_adapter *adapter)
249{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700250 struct be_eq_obj *rx_eq = &adapter->rx_eq;
251 struct be_drvr_stats *stats = &adapter->stats.drvr_stats;
Sathya Perla4097f662009-03-24 16:40:13 -0700252 ulong now = jiffies;
253 u32 eqd;
254
255 if (!rx_eq->enable_aic)
256 return;
257
258 /* Wrapped around */
259 if (time_before(now, stats->rx_fps_jiffies)) {
260 stats->rx_fps_jiffies = now;
261 return;
262 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700263
264 /* Update once a second */
Sathya Perla4097f662009-03-24 16:40:13 -0700265 if ((now - stats->rx_fps_jiffies) < HZ)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700266 return;
267
268 stats->be_rx_fps = (stats->be_rx_frags - stats->be_prev_rx_frags) /
Sathya Perla4097f662009-03-24 16:40:13 -0700269 ((now - stats->rx_fps_jiffies) / HZ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700270
Sathya Perla4097f662009-03-24 16:40:13 -0700271 stats->rx_fps_jiffies = now;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700272 stats->be_prev_rx_frags = stats->be_rx_frags;
273 eqd = stats->be_rx_fps / 110000;
274 eqd = eqd << 3;
275 if (eqd > rx_eq->max_eqd)
276 eqd = rx_eq->max_eqd;
277 if (eqd < rx_eq->min_eqd)
278 eqd = rx_eq->min_eqd;
279 if (eqd < 10)
280 eqd = 0;
281 if (eqd != rx_eq->cur_eqd)
Sathya Perla8788fdc2009-07-27 22:52:03 +0000282 be_cmd_modify_eqd(adapter, rx_eq->q.id, eqd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700283
284 rx_eq->cur_eqd = eqd;
285}
286
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700287static struct net_device_stats *be_get_stats(struct net_device *dev)
288{
Ajit Khaparde78122a52009-10-07 03:11:20 -0700289 return &dev->stats;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700290}
291
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700292static u32 be_calc_rate(u64 bytes, unsigned long ticks)
293{
294 u64 rate = bytes;
295
296 do_div(rate, ticks / HZ);
297 rate <<= 3; /* bytes/sec -> bits/sec */
298 do_div(rate, 1000000ul); /* MB/Sec */
299
300 return rate;
301}
302
Sathya Perla4097f662009-03-24 16:40:13 -0700303static void be_tx_rate_update(struct be_adapter *adapter)
304{
305 struct be_drvr_stats *stats = drvr_stats(adapter);
306 ulong now = jiffies;
307
308 /* Wrapped around? */
309 if (time_before(now, stats->be_tx_jiffies)) {
310 stats->be_tx_jiffies = now;
311 return;
312 }
313
314 /* Update tx rate once in two seconds */
315 if ((now - stats->be_tx_jiffies) > 2 * HZ) {
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700316 stats->be_tx_rate = be_calc_rate(stats->be_tx_bytes
317 - stats->be_tx_bytes_prev,
318 now - stats->be_tx_jiffies);
Sathya Perla4097f662009-03-24 16:40:13 -0700319 stats->be_tx_jiffies = now;
320 stats->be_tx_bytes_prev = stats->be_tx_bytes;
321 }
322}
323
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700324static void be_tx_stats_update(struct be_adapter *adapter,
325 u32 wrb_cnt, u32 copied, bool stopped)
326{
Sathya Perla4097f662009-03-24 16:40:13 -0700327 struct be_drvr_stats *stats = drvr_stats(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700328 stats->be_tx_reqs++;
329 stats->be_tx_wrbs += wrb_cnt;
330 stats->be_tx_bytes += copied;
331 if (stopped)
332 stats->be_tx_stops++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700333}
334
335/* Determine number of WRB entries needed to xmit data in an skb */
336static u32 wrb_cnt_for_skb(struct sk_buff *skb, bool *dummy)
337{
David S. Millerebc8d2a2009-06-09 01:01:31 -0700338 int cnt = (skb->len > skb->data_len);
339
340 cnt += skb_shinfo(skb)->nr_frags;
341
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700342 /* to account for hdr wrb */
343 cnt++;
344 if (cnt & 1) {
345 /* add a dummy to make it an even num */
346 cnt++;
347 *dummy = true;
348 } else
349 *dummy = false;
350 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
351 return cnt;
352}
353
354static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
355{
356 wrb->frag_pa_hi = upper_32_bits(addr);
357 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
358 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
359}
360
361static void wrb_fill_hdr(struct be_eth_hdr_wrb *hdr, struct sk_buff *skb,
362 bool vlan, u32 wrb_cnt, u32 len)
363{
364 memset(hdr, 0, sizeof(*hdr));
365
366 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
367
368 if (skb_shinfo(skb)->gso_segs > 1 && skb_shinfo(skb)->gso_size) {
369 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
370 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
371 hdr, skb_shinfo(skb)->gso_size);
372 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
373 if (is_tcp_pkt(skb))
374 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
375 else if (is_udp_pkt(skb))
376 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
377 }
378
379 if (vlan && vlan_tx_tag_present(skb)) {
380 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
381 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag,
382 hdr, vlan_tx_tag_get(skb));
383 }
384
385 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
386 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
387 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
388 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
389}
390
391
392static int make_tx_wrbs(struct be_adapter *adapter,
393 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
394{
395 u64 busaddr;
396 u32 i, copied = 0;
397 struct pci_dev *pdev = adapter->pdev;
398 struct sk_buff *first_skb = skb;
399 struct be_queue_info *txq = &adapter->tx_obj.q;
400 struct be_eth_wrb *wrb;
401 struct be_eth_hdr_wrb *hdr;
402
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700403 hdr = queue_head_node(txq);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000404 atomic_add(wrb_cnt, &txq->used);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700405 queue_head_inc(txq);
406
David S. Millerebc8d2a2009-06-09 01:01:31 -0700407 if (skb->len > skb->data_len) {
408 int len = skb->len - skb->data_len;
Alexander Duycka73b7962009-12-02 16:48:18 +0000409 busaddr = pci_map_single(pdev, skb->data, len,
410 PCI_DMA_TODEVICE);
David S. Millerebc8d2a2009-06-09 01:01:31 -0700411 wrb = queue_head_node(txq);
412 wrb_fill(wrb, busaddr, len);
413 be_dws_cpu_to_le(wrb, sizeof(*wrb));
414 queue_head_inc(txq);
415 copied += len;
416 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700417
David S. Millerebc8d2a2009-06-09 01:01:31 -0700418 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
419 struct skb_frag_struct *frag =
420 &skb_shinfo(skb)->frags[i];
Alexander Duycka73b7962009-12-02 16:48:18 +0000421 busaddr = pci_map_page(pdev, frag->page,
422 frag->page_offset,
423 frag->size, PCI_DMA_TODEVICE);
David S. Millerebc8d2a2009-06-09 01:01:31 -0700424 wrb = queue_head_node(txq);
425 wrb_fill(wrb, busaddr, frag->size);
426 be_dws_cpu_to_le(wrb, sizeof(*wrb));
427 queue_head_inc(txq);
428 copied += frag->size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700429 }
430
431 if (dummy_wrb) {
432 wrb = queue_head_node(txq);
433 wrb_fill(wrb, 0, 0);
434 be_dws_cpu_to_le(wrb, sizeof(*wrb));
435 queue_head_inc(txq);
436 }
437
438 wrb_fill_hdr(hdr, first_skb, adapter->vlan_grp ? true : false,
439 wrb_cnt, copied);
440 be_dws_cpu_to_le(hdr, sizeof(*hdr));
441
442 return copied;
443}
444
Stephen Hemminger613573252009-08-31 19:50:58 +0000445static netdev_tx_t be_xmit(struct sk_buff *skb,
Sathya Perlab31c50a2009-09-17 10:30:13 -0700446 struct net_device *netdev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700447{
448 struct be_adapter *adapter = netdev_priv(netdev);
449 struct be_tx_obj *tx_obj = &adapter->tx_obj;
450 struct be_queue_info *txq = &tx_obj->q;
451 u32 wrb_cnt = 0, copied = 0;
452 u32 start = txq->head;
453 bool dummy_wrb, stopped = false;
454
455 wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb);
456
457 copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000458 if (copied) {
459 /* record the sent skb in the sent_skb table */
460 BUG_ON(tx_obj->sent_skb_list[start]);
461 tx_obj->sent_skb_list[start] = skb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700462
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000463 /* Ensure txq has space for the next skb; Else stop the queue
464 * *BEFORE* ringing the tx doorbell, so that we serialze the
465 * tx compls of the current transmit which'll wake up the queue
466 */
467 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
468 txq->len) {
469 netif_stop_queue(netdev);
470 stopped = true;
471 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700472
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000473 be_txq_notify(adapter, txq->id, wrb_cnt);
474
475 be_tx_stats_update(adapter, wrb_cnt, copied, stopped);
476 } else {
477 txq->head = start;
478 dev_kfree_skb_any(skb);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700479 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700480 return NETDEV_TX_OK;
481}
482
483static int be_change_mtu(struct net_device *netdev, int new_mtu)
484{
485 struct be_adapter *adapter = netdev_priv(netdev);
486 if (new_mtu < BE_MIN_MTU ||
Ajit Khaparde34a89b82010-02-09 01:32:43 +0000487 new_mtu > (BE_MAX_JUMBO_FRAME_SIZE -
488 (ETH_HLEN + ETH_FCS_LEN))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700489 dev_info(&adapter->pdev->dev,
490 "MTU must be between %d and %d bytes\n",
Ajit Khaparde34a89b82010-02-09 01:32:43 +0000491 BE_MIN_MTU,
492 (BE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN)));
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700493 return -EINVAL;
494 }
495 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
496 netdev->mtu, new_mtu);
497 netdev->mtu = new_mtu;
498 return 0;
499}
500
501/*
Ajit Khaparde82903e42010-02-09 01:34:57 +0000502 * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
503 * If the user configures more, place BE in vlan promiscuous mode.
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700504 */
Sathya Perlab31c50a2009-09-17 10:30:13 -0700505static int be_vid_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700506{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700507 u16 vtag[BE_NUM_VLANS_SUPPORTED];
508 u16 ntags = 0, i;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000509 int status = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700510
Ajit Khaparde82903e42010-02-09 01:34:57 +0000511 if (adapter->vlans_added <= adapter->max_vlans) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700512 /* Construct VLAN Table to give to HW */
513 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
514 if (adapter->vlan_tag[i]) {
515 vtag[ntags] = cpu_to_le16(i);
516 ntags++;
517 }
518 }
Sathya Perlab31c50a2009-09-17 10:30:13 -0700519 status = be_cmd_vlan_config(adapter, adapter->if_handle,
520 vtag, ntags, 1, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700521 } else {
Sathya Perlab31c50a2009-09-17 10:30:13 -0700522 status = be_cmd_vlan_config(adapter, adapter->if_handle,
523 NULL, 0, 1, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700524 }
Sathya Perlab31c50a2009-09-17 10:30:13 -0700525 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700526}
527
528static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
529{
530 struct be_adapter *adapter = netdev_priv(netdev);
531 struct be_eq_obj *rx_eq = &adapter->rx_eq;
532 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700533
Sathya Perla8788fdc2009-07-27 22:52:03 +0000534 be_eq_notify(adapter, rx_eq->q.id, false, false, 0);
535 be_eq_notify(adapter, tx_eq->q.id, false, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700536 adapter->vlan_grp = grp;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000537 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
538 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700539}
540
541static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
542{
543 struct be_adapter *adapter = netdev_priv(netdev);
544
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700545 adapter->vlan_tag[vid] = 1;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000546 adapter->vlans_added++;
547 if (adapter->vlans_added <= (adapter->max_vlans + 1))
548 be_vid_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700549}
550
551static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
552{
553 struct be_adapter *adapter = netdev_priv(netdev);
554
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700555 adapter->vlan_tag[vid] = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700556 vlan_group_set_device(adapter->vlan_grp, vid, NULL);
Ajit Khaparde82903e42010-02-09 01:34:57 +0000557 adapter->vlans_added--;
558 if (adapter->vlans_added <= adapter->max_vlans)
559 be_vid_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700560}
561
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700562static void be_set_multicast_list(struct net_device *netdev)
563{
564 struct be_adapter *adapter = netdev_priv(netdev);
565
566 if (netdev->flags & IFF_PROMISC) {
Sathya Perla8788fdc2009-07-27 22:52:03 +0000567 be_cmd_promiscuous_config(adapter, adapter->port_num, 1);
Sathya Perla24307ee2009-06-18 00:09:25 +0000568 adapter->promiscuous = true;
569 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700570 }
Sathya Perla24307ee2009-06-18 00:09:25 +0000571
572 /* BE was previously in promiscous mode; disable it */
573 if (adapter->promiscuous) {
574 adapter->promiscuous = false;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000575 be_cmd_promiscuous_config(adapter, adapter->port_num, 0);
Sathya Perla24307ee2009-06-18 00:09:25 +0000576 }
577
Sathya Perlae7b909a2009-11-22 22:01:10 +0000578 /* Enable multicast promisc if num configured exceeds what we support */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000579 if (netdev->flags & IFF_ALLMULTI ||
580 netdev_mc_count(netdev) > BE_MAX_MC) {
Sathya Perlae7b909a2009-11-22 22:01:10 +0000581 be_cmd_multicast_set(adapter, adapter->if_handle, NULL, 0,
582 &adapter->mc_cmd_mem);
Sathya Perla24307ee2009-06-18 00:09:25 +0000583 goto done;
584 }
585
Sathya Perla8788fdc2009-07-27 22:52:03 +0000586 be_cmd_multicast_set(adapter, adapter->if_handle, netdev->mc_list,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000587 netdev_mc_count(netdev), &adapter->mc_cmd_mem);
Sathya Perla24307ee2009-06-18 00:09:25 +0000588done:
589 return;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700590}
591
Sathya Perla4097f662009-03-24 16:40:13 -0700592static void be_rx_rate_update(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700593{
Sathya Perla4097f662009-03-24 16:40:13 -0700594 struct be_drvr_stats *stats = drvr_stats(adapter);
595 ulong now = jiffies;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700596
Sathya Perla4097f662009-03-24 16:40:13 -0700597 /* Wrapped around */
598 if (time_before(now, stats->be_rx_jiffies)) {
599 stats->be_rx_jiffies = now;
600 return;
601 }
602
603 /* Update the rate once in two seconds */
604 if ((now - stats->be_rx_jiffies) < 2 * HZ)
605 return;
606
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700607 stats->be_rx_rate = be_calc_rate(stats->be_rx_bytes
608 - stats->be_rx_bytes_prev,
609 now - stats->be_rx_jiffies);
Sathya Perla4097f662009-03-24 16:40:13 -0700610 stats->be_rx_jiffies = now;
611 stats->be_rx_bytes_prev = stats->be_rx_bytes;
612}
613
614static void be_rx_stats_update(struct be_adapter *adapter,
615 u32 pktsize, u16 numfrags)
616{
617 struct be_drvr_stats *stats = drvr_stats(adapter);
618
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700619 stats->be_rx_compl++;
620 stats->be_rx_frags += numfrags;
621 stats->be_rx_bytes += pktsize;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700622}
623
Ajit Khaparde728a9972009-04-13 15:41:22 -0700624static inline bool do_pkt_csum(struct be_eth_rx_compl *rxcp, bool cso)
625{
626 u8 l4_cksm, ip_version, ipcksm, tcpf = 0, udpf = 0, ipv6_chk;
627
628 l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp);
629 ipcksm = AMAP_GET_BITS(struct amap_eth_rx_compl, ipcksm, rxcp);
630 ip_version = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
631 if (ip_version) {
632 tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
633 udpf = AMAP_GET_BITS(struct amap_eth_rx_compl, udpf, rxcp);
634 }
635 ipv6_chk = (ip_version && (tcpf || udpf));
636
637 return ((l4_cksm && ipv6_chk && ipcksm) && cso) ? false : true;
638}
639
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700640static struct be_rx_page_info *
641get_rx_page_info(struct be_adapter *adapter, u16 frag_idx)
642{
643 struct be_rx_page_info *rx_page_info;
644 struct be_queue_info *rxq = &adapter->rx_obj.q;
645
646 rx_page_info = &adapter->rx_obj.page_info_tbl[frag_idx];
647 BUG_ON(!rx_page_info->page);
648
Ajit Khaparde205859a2010-02-09 01:34:21 +0000649 if (rx_page_info->last_page_user) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700650 pci_unmap_page(adapter->pdev, pci_unmap_addr(rx_page_info, bus),
651 adapter->big_page_size, PCI_DMA_FROMDEVICE);
Ajit Khaparde205859a2010-02-09 01:34:21 +0000652 rx_page_info->last_page_user = false;
653 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700654
655 atomic_dec(&rxq->used);
656 return rx_page_info;
657}
658
659/* Throwaway the data in the Rx completion */
660static void be_rx_compl_discard(struct be_adapter *adapter,
661 struct be_eth_rx_compl *rxcp)
662{
663 struct be_queue_info *rxq = &adapter->rx_obj.q;
664 struct be_rx_page_info *page_info;
665 u16 rxq_idx, i, num_rcvd;
666
667 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
668 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
669
670 for (i = 0; i < num_rcvd; i++) {
671 page_info = get_rx_page_info(adapter, rxq_idx);
672 put_page(page_info->page);
673 memset(page_info, 0, sizeof(*page_info));
674 index_inc(&rxq_idx, rxq->len);
675 }
676}
677
678/*
679 * skb_fill_rx_data forms a complete skb for an ether frame
680 * indicated by rxcp.
681 */
682static void skb_fill_rx_data(struct be_adapter *adapter,
Sathya Perla89420422010-02-17 01:35:26 +0000683 struct sk_buff *skb, struct be_eth_rx_compl *rxcp,
684 u16 num_rcvd)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700685{
686 struct be_queue_info *rxq = &adapter->rx_obj.q;
687 struct be_rx_page_info *page_info;
Sathya Perla89420422010-02-17 01:35:26 +0000688 u16 rxq_idx, i, j;
Ajit Khapardefa774062009-07-22 09:28:55 -0700689 u32 pktsize, hdr_len, curr_frag_len, size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700690 u8 *start;
691
692 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
693 pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700694
695 page_info = get_rx_page_info(adapter, rxq_idx);
696
697 start = page_address(page_info->page) + page_info->page_offset;
698 prefetch(start);
699
700 /* Copy data in the first descriptor of this completion */
701 curr_frag_len = min(pktsize, rx_frag_size);
702
703 /* Copy the header portion into skb_data */
704 hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
705 memcpy(skb->data, start, hdr_len);
706 skb->len = curr_frag_len;
707 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
708 /* Complete packet has now been moved to data */
709 put_page(page_info->page);
710 skb->data_len = 0;
711 skb->tail += curr_frag_len;
712 } else {
713 skb_shinfo(skb)->nr_frags = 1;
714 skb_shinfo(skb)->frags[0].page = page_info->page;
715 skb_shinfo(skb)->frags[0].page_offset =
716 page_info->page_offset + hdr_len;
717 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
718 skb->data_len = curr_frag_len - hdr_len;
719 skb->tail += hdr_len;
720 }
Ajit Khaparde205859a2010-02-09 01:34:21 +0000721 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700722
723 if (pktsize <= rx_frag_size) {
724 BUG_ON(num_rcvd != 1);
Sathya Perla76fbb422009-06-10 02:21:56 +0000725 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700726 }
727
728 /* More frags present for this completion */
Ajit Khapardefa774062009-07-22 09:28:55 -0700729 size = pktsize;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000730 for (i = 1, j = 0; i < num_rcvd; i++) {
Ajit Khapardefa774062009-07-22 09:28:55 -0700731 size -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700732 index_inc(&rxq_idx, rxq->len);
733 page_info = get_rx_page_info(adapter, rxq_idx);
734
Ajit Khapardefa774062009-07-22 09:28:55 -0700735 curr_frag_len = min(size, rx_frag_size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700736
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000737 /* Coalesce all frags from the same physical page in one slot */
738 if (page_info->page_offset == 0) {
739 /* Fresh page */
740 j++;
741 skb_shinfo(skb)->frags[j].page = page_info->page;
742 skb_shinfo(skb)->frags[j].page_offset =
743 page_info->page_offset;
744 skb_shinfo(skb)->frags[j].size = 0;
745 skb_shinfo(skb)->nr_frags++;
746 } else {
747 put_page(page_info->page);
748 }
749
750 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700751 skb->len += curr_frag_len;
752 skb->data_len += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700753
Ajit Khaparde205859a2010-02-09 01:34:21 +0000754 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700755 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000756 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700757
Sathya Perla76fbb422009-06-10 02:21:56 +0000758done:
Sathya Perla4097f662009-03-24 16:40:13 -0700759 be_rx_stats_update(adapter, pktsize, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700760 return;
761}
762
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700763/* Process the RX completion indicated by rxcp when GRO is disabled */
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700764static void be_rx_compl_process(struct be_adapter *adapter,
765 struct be_eth_rx_compl *rxcp)
766{
767 struct sk_buff *skb;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700768 u32 vlanf, vid;
Sathya Perla89420422010-02-17 01:35:26 +0000769 u16 num_rcvd;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700770 u8 vtm;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700771
Sathya Perla89420422010-02-17 01:35:26 +0000772 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
773 /* Is it a flush compl that has no data */
774 if (unlikely(num_rcvd == 0))
775 return;
776
Eric Dumazet89d71a62009-10-13 05:34:20 +0000777 skb = netdev_alloc_skb_ip_align(adapter->netdev, BE_HDR_LEN);
Sathya Perlaa058a632010-02-17 01:34:22 +0000778 if (unlikely(!skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700779 if (net_ratelimit())
780 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
781 be_rx_compl_discard(adapter, rxcp);
782 return;
783 }
784
Sathya Perla89420422010-02-17 01:35:26 +0000785 skb_fill_rx_data(adapter, skb, rxcp, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700786
Ajit Khaparde728a9972009-04-13 15:41:22 -0700787 if (do_pkt_csum(rxcp, adapter->rx_csum))
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700788 skb->ip_summed = CHECKSUM_NONE;
Ajit Khaparde728a9972009-04-13 15:41:22 -0700789 else
790 skb->ip_summed = CHECKSUM_UNNECESSARY;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700791
792 skb->truesize = skb->len + sizeof(struct sk_buff);
793 skb->protocol = eth_type_trans(skb, adapter->netdev);
794 skb->dev = adapter->netdev;
795
Sathya Perlaa058a632010-02-17 01:34:22 +0000796 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
797 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
798
799 /* vlanf could be wrongly set in some cards.
800 * ignore if vtm is not set */
801 if ((adapter->cap & 0x400) && !vtm)
802 vlanf = 0;
803
804 if (unlikely(vlanf)) {
Ajit Khaparde82903e42010-02-09 01:34:57 +0000805 if (!adapter->vlan_grp || adapter->vlans_added == 0) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700806 kfree_skb(skb);
807 return;
808 }
809 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
810 vid = be16_to_cpu(vid);
811 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
812 } else {
813 netif_receive_skb(skb);
814 }
815
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700816 return;
817}
818
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700819/* Process the RX completion indicated by rxcp when GRO is enabled */
820static void be_rx_compl_process_gro(struct be_adapter *adapter,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700821 struct be_eth_rx_compl *rxcp)
822{
823 struct be_rx_page_info *page_info;
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700824 struct sk_buff *skb = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700825 struct be_queue_info *rxq = &adapter->rx_obj.q;
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700826 struct be_eq_obj *eq_obj = &adapter->rx_eq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700827 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000828 u16 i, rxq_idx = 0, vid, j;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700829 u8 vtm;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700830
831 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
Sathya Perla89420422010-02-17 01:35:26 +0000832 /* Is it a flush compl that has no data */
833 if (unlikely(num_rcvd == 0))
834 return;
835
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700836 pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
837 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
838 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700839 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
840
841 /* vlanf could be wrongly set in some cards.
842 * ignore if vtm is not set */
Ajit Khapardee1187b32009-12-11 22:59:09 -0800843 if ((adapter->cap & 0x400) && !vtm)
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700844 vlanf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700845
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700846 skb = napi_get_frags(&eq_obj->napi);
847 if (!skb) {
848 be_rx_compl_discard(adapter, rxcp);
849 return;
850 }
851
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700852 remaining = pkt_size;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000853 for (i = 0, j = -1; i < num_rcvd; i++) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700854 page_info = get_rx_page_info(adapter, rxq_idx);
855
856 curr_frag_len = min(remaining, rx_frag_size);
857
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000858 /* Coalesce all frags from the same physical page in one slot */
859 if (i == 0 || page_info->page_offset == 0) {
860 /* First frag or Fresh page */
861 j++;
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700862 skb_shinfo(skb)->frags[j].page = page_info->page;
863 skb_shinfo(skb)->frags[j].page_offset =
864 page_info->page_offset;
865 skb_shinfo(skb)->frags[j].size = 0;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000866 } else {
867 put_page(page_info->page);
868 }
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700869 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000870
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700871 remaining -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700872 index_inc(&rxq_idx, rxq->len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700873 memset(page_info, 0, sizeof(*page_info));
874 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000875 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700876
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700877 skb_shinfo(skb)->nr_frags = j + 1;
878 skb->len = pkt_size;
879 skb->data_len = pkt_size;
880 skb->truesize += pkt_size;
881 skb->ip_summed = CHECKSUM_UNNECESSARY;
882
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700883 if (likely(!vlanf)) {
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700884 napi_gro_frags(&eq_obj->napi);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700885 } else {
886 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
887 vid = be16_to_cpu(vid);
888
Ajit Khaparde82903e42010-02-09 01:34:57 +0000889 if (!adapter->vlan_grp || adapter->vlans_added == 0)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700890 return;
891
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700892 vlan_gro_frags(&eq_obj->napi, adapter->vlan_grp, vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700893 }
894
Sathya Perla4097f662009-03-24 16:40:13 -0700895 be_rx_stats_update(adapter, pkt_size, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700896 return;
897}
898
899static struct be_eth_rx_compl *be_rx_compl_get(struct be_adapter *adapter)
900{
901 struct be_eth_rx_compl *rxcp = queue_tail_node(&adapter->rx_obj.cq);
902
903 if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
904 return NULL;
905
906 be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
907
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700908 queue_tail_inc(&adapter->rx_obj.cq);
909 return rxcp;
910}
911
Sathya Perlaa7a0ef32009-06-10 02:23:28 +0000912/* To reset the valid bit, we need to reset the whole word as
913 * when walking the queue the valid entries are little-endian
914 * and invalid entries are host endian
915 */
916static inline void be_rx_compl_reset(struct be_eth_rx_compl *rxcp)
917{
918 rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
919}
920
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700921static inline struct page *be_alloc_pages(u32 size)
922{
923 gfp_t alloc_flags = GFP_ATOMIC;
924 u32 order = get_order(size);
925 if (order > 0)
926 alloc_flags |= __GFP_COMP;
927 return alloc_pages(alloc_flags, order);
928}
929
930/*
931 * Allocate a page, split it to fragments of size rx_frag_size and post as
932 * receive buffers to BE
933 */
934static void be_post_rx_frags(struct be_adapter *adapter)
935{
936 struct be_rx_page_info *page_info_tbl = adapter->rx_obj.page_info_tbl;
Sathya Perla26d92f92010-01-21 22:52:08 -0800937 struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700938 struct be_queue_info *rxq = &adapter->rx_obj.q;
939 struct page *pagep = NULL;
940 struct be_eth_rx_d *rxd;
941 u64 page_dmaaddr = 0, frag_dmaaddr;
942 u32 posted, page_offset = 0;
943
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700944 page_info = &page_info_tbl[rxq->head];
945 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
946 if (!pagep) {
947 pagep = be_alloc_pages(adapter->big_page_size);
948 if (unlikely(!pagep)) {
949 drvr_stats(adapter)->be_ethrx_post_fail++;
950 break;
951 }
952 page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
953 adapter->big_page_size,
954 PCI_DMA_FROMDEVICE);
955 page_info->page_offset = 0;
956 } else {
957 get_page(pagep);
958 page_info->page_offset = page_offset + rx_frag_size;
959 }
960 page_offset = page_info->page_offset;
961 page_info->page = pagep;
962 pci_unmap_addr_set(page_info, bus, page_dmaaddr);
963 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
964
965 rxd = queue_head_node(rxq);
966 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
967 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700968
969 /* Any space left in the current big page for another frag? */
970 if ((page_offset + rx_frag_size + rx_frag_size) >
971 adapter->big_page_size) {
972 pagep = NULL;
973 page_info->last_page_user = true;
974 }
Sathya Perla26d92f92010-01-21 22:52:08 -0800975
976 prev_page_info = page_info;
977 queue_head_inc(rxq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700978 page_info = &page_info_tbl[rxq->head];
979 }
980 if (pagep)
Sathya Perla26d92f92010-01-21 22:52:08 -0800981 prev_page_info->last_page_user = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700982
983 if (posted) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700984 atomic_add(posted, &rxq->used);
Sathya Perla8788fdc2009-07-27 22:52:03 +0000985 be_rxq_notify(adapter, rxq->id, posted);
Sathya Perlaea1dae12009-03-19 23:56:20 -0700986 } else if (atomic_read(&rxq->used) == 0) {
987 /* Let be_worker replenish when memory is available */
988 adapter->rx_post_starved = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700989 }
990
991 return;
992}
993
Sathya Perla5fb379e2009-06-18 00:02:59 +0000994static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700995{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700996 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
997
998 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
999 return NULL;
1000
1001 be_dws_le_to_cpu(txcp, sizeof(*txcp));
1002
1003 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
1004
1005 queue_tail_inc(tx_cq);
1006 return txcp;
1007}
1008
1009static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
1010{
1011 struct be_queue_info *txq = &adapter->tx_obj.q;
Alexander Duycka73b7962009-12-02 16:48:18 +00001012 struct be_eth_wrb *wrb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001013 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1014 struct sk_buff *sent_skb;
Alexander Duycka73b7962009-12-02 16:48:18 +00001015 u64 busaddr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001016 u16 cur_index, num_wrbs = 0;
1017
1018 cur_index = txq->tail;
1019 sent_skb = sent_skbs[cur_index];
1020 BUG_ON(!sent_skb);
1021 sent_skbs[cur_index] = NULL;
Alexander Duycka73b7962009-12-02 16:48:18 +00001022 wrb = queue_tail_node(txq);
1023 be_dws_le_to_cpu(wrb, sizeof(*wrb));
1024 busaddr = ((u64)wrb->frag_pa_hi << 32) | (u64)wrb->frag_pa_lo;
1025 if (busaddr != 0) {
1026 pci_unmap_single(adapter->pdev, busaddr,
1027 wrb->frag_len, PCI_DMA_TODEVICE);
1028 }
1029 num_wrbs++;
1030 queue_tail_inc(txq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001031
Alexander Duycka73b7962009-12-02 16:48:18 +00001032 while (cur_index != last_index) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001033 cur_index = txq->tail;
Alexander Duycka73b7962009-12-02 16:48:18 +00001034 wrb = queue_tail_node(txq);
1035 be_dws_le_to_cpu(wrb, sizeof(*wrb));
1036 busaddr = ((u64)wrb->frag_pa_hi << 32) | (u64)wrb->frag_pa_lo;
1037 if (busaddr != 0) {
1038 pci_unmap_page(adapter->pdev, busaddr,
1039 wrb->frag_len, PCI_DMA_TODEVICE);
1040 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001041 num_wrbs++;
1042 queue_tail_inc(txq);
Alexander Duycka73b7962009-12-02 16:48:18 +00001043 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001044
1045 atomic_sub(num_wrbs, &txq->used);
Alexander Duycka73b7962009-12-02 16:48:18 +00001046
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001047 kfree_skb(sent_skb);
1048}
1049
Sathya Perla859b1e42009-08-10 03:43:51 +00001050static inline struct be_eq_entry *event_get(struct be_eq_obj *eq_obj)
1051{
1052 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1053
1054 if (!eqe->evt)
1055 return NULL;
1056
1057 eqe->evt = le32_to_cpu(eqe->evt);
1058 queue_tail_inc(&eq_obj->q);
1059 return eqe;
1060}
1061
1062static int event_handle(struct be_adapter *adapter,
1063 struct be_eq_obj *eq_obj)
1064{
1065 struct be_eq_entry *eqe;
1066 u16 num = 0;
1067
1068 while ((eqe = event_get(eq_obj)) != NULL) {
1069 eqe->evt = 0;
1070 num++;
1071 }
1072
1073 /* Deal with any spurious interrupts that come
1074 * without events
1075 */
1076 be_eq_notify(adapter, eq_obj->q.id, true, true, num);
1077 if (num)
1078 napi_schedule(&eq_obj->napi);
1079
1080 return num;
1081}
1082
1083/* Just read and notify events without processing them.
1084 * Used at the time of destroying event queues */
1085static void be_eq_clean(struct be_adapter *adapter,
1086 struct be_eq_obj *eq_obj)
1087{
1088 struct be_eq_entry *eqe;
1089 u16 num = 0;
1090
1091 while ((eqe = event_get(eq_obj)) != NULL) {
1092 eqe->evt = 0;
1093 num++;
1094 }
1095
1096 if (num)
1097 be_eq_notify(adapter, eq_obj->q.id, false, true, num);
1098}
1099
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001100static void be_rx_q_clean(struct be_adapter *adapter)
1101{
1102 struct be_rx_page_info *page_info;
1103 struct be_queue_info *rxq = &adapter->rx_obj.q;
1104 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1105 struct be_eth_rx_compl *rxcp;
1106 u16 tail;
1107
1108 /* First cleanup pending rx completions */
1109 while ((rxcp = be_rx_compl_get(adapter)) != NULL) {
1110 be_rx_compl_discard(adapter, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001111 be_rx_compl_reset(rxcp);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001112 be_cq_notify(adapter, rx_cq->id, true, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001113 }
1114
1115 /* Then free posted rx buffer that were not used */
1116 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
Sathya Perlacdab23b2009-08-10 03:43:23 +00001117 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001118 page_info = get_rx_page_info(adapter, tail);
1119 put_page(page_info->page);
1120 memset(page_info, 0, sizeof(*page_info));
1121 }
1122 BUG_ON(atomic_read(&rxq->used));
1123}
1124
Sathya Perlaa8e91792009-08-10 03:42:43 +00001125static void be_tx_compl_clean(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001126{
Sathya Perlaa8e91792009-08-10 03:42:43 +00001127 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001128 struct be_queue_info *txq = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001129 struct be_eth_tx_compl *txcp;
1130 u16 end_idx, cmpl = 0, timeo = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001131
Sathya Perlaa8e91792009-08-10 03:42:43 +00001132 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1133 do {
1134 while ((txcp = be_tx_compl_get(tx_cq))) {
1135 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1136 wrb_index, txcp);
1137 be_tx_compl_process(adapter, end_idx);
1138 cmpl++;
1139 }
1140 if (cmpl) {
1141 be_cq_notify(adapter, tx_cq->id, false, cmpl);
1142 cmpl = 0;
1143 }
1144
1145 if (atomic_read(&txq->used) == 0 || ++timeo > 200)
1146 break;
1147
1148 mdelay(1);
1149 } while (true);
1150
1151 if (atomic_read(&txq->used))
1152 dev_err(&adapter->pdev->dev, "%d pending tx-completions\n",
1153 atomic_read(&txq->used));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001154}
1155
Sathya Perla5fb379e2009-06-18 00:02:59 +00001156static void be_mcc_queues_destroy(struct be_adapter *adapter)
1157{
1158 struct be_queue_info *q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001159
Sathya Perla8788fdc2009-07-27 22:52:03 +00001160 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001161 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001162 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001163 be_queue_free(adapter, q);
1164
Sathya Perla8788fdc2009-07-27 22:52:03 +00001165 q = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001166 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001167 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001168 be_queue_free(adapter, q);
1169}
1170
1171/* Must be called only after TX qs are created as MCC shares TX EQ */
1172static int be_mcc_queues_create(struct be_adapter *adapter)
1173{
1174 struct be_queue_info *q, *cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001175
1176 /* Alloc MCC compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001177 cq = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001178 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
Sathya Perlaefd2e402009-07-27 22:53:10 +00001179 sizeof(struct be_mcc_compl)))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001180 goto err;
1181
1182 /* Ask BE to create MCC compl queue; share TX's eq */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001183 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001184 goto mcc_cq_free;
1185
1186 /* Alloc MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001187 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001188 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1189 goto mcc_cq_destroy;
1190
1191 /* Ask BE to create MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001192 if (be_cmd_mccq_create(adapter, q, cq))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001193 goto mcc_q_free;
1194
1195 return 0;
1196
1197mcc_q_free:
1198 be_queue_free(adapter, q);
1199mcc_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001200 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001201mcc_cq_free:
1202 be_queue_free(adapter, cq);
1203err:
1204 return -1;
1205}
1206
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001207static void be_tx_queues_destroy(struct be_adapter *adapter)
1208{
1209 struct be_queue_info *q;
1210
1211 q = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001212 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001213 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001214 be_queue_free(adapter, q);
1215
1216 q = &adapter->tx_obj.cq;
1217 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001218 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001219 be_queue_free(adapter, q);
1220
Sathya Perla859b1e42009-08-10 03:43:51 +00001221 /* Clear any residual events */
1222 be_eq_clean(adapter, &adapter->tx_eq);
1223
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001224 q = &adapter->tx_eq.q;
1225 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001226 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001227 be_queue_free(adapter, q);
1228}
1229
1230static int be_tx_queues_create(struct be_adapter *adapter)
1231{
1232 struct be_queue_info *eq, *q, *cq;
1233
1234 adapter->tx_eq.max_eqd = 0;
1235 adapter->tx_eq.min_eqd = 0;
1236 adapter->tx_eq.cur_eqd = 96;
1237 adapter->tx_eq.enable_aic = false;
1238 /* Alloc Tx Event queue */
1239 eq = &adapter->tx_eq.q;
1240 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1241 return -1;
1242
1243 /* Ask BE to create Tx Event queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001244 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001245 goto tx_eq_free;
1246 /* Alloc TX eth compl queue */
1247 cq = &adapter->tx_obj.cq;
1248 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1249 sizeof(struct be_eth_tx_compl)))
1250 goto tx_eq_destroy;
1251
1252 /* Ask BE to create Tx eth compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001253 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001254 goto tx_cq_free;
1255
1256 /* Alloc TX eth queue */
1257 q = &adapter->tx_obj.q;
1258 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1259 goto tx_cq_destroy;
1260
1261 /* Ask BE to create Tx eth queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001262 if (be_cmd_txq_create(adapter, q, cq))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001263 goto tx_q_free;
1264 return 0;
1265
1266tx_q_free:
1267 be_queue_free(adapter, q);
1268tx_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001269 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001270tx_cq_free:
1271 be_queue_free(adapter, cq);
1272tx_eq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001273 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001274tx_eq_free:
1275 be_queue_free(adapter, eq);
1276 return -1;
1277}
1278
1279static void be_rx_queues_destroy(struct be_adapter *adapter)
1280{
1281 struct be_queue_info *q;
1282
1283 q = &adapter->rx_obj.q;
1284 if (q->created) {
Sathya Perla8788fdc2009-07-27 22:52:03 +00001285 be_cmd_q_destroy(adapter, q, QTYPE_RXQ);
Sathya Perla89420422010-02-17 01:35:26 +00001286
1287 /* After the rxq is invalidated, wait for a grace time
1288 * of 1ms for all dma to end and the flush compl to arrive
1289 */
1290 mdelay(1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001291 be_rx_q_clean(adapter);
1292 }
1293 be_queue_free(adapter, q);
1294
1295 q = &adapter->rx_obj.cq;
1296 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001297 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001298 be_queue_free(adapter, q);
1299
Sathya Perla859b1e42009-08-10 03:43:51 +00001300 /* Clear any residual events */
1301 be_eq_clean(adapter, &adapter->rx_eq);
1302
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001303 q = &adapter->rx_eq.q;
1304 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001305 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001306 be_queue_free(adapter, q);
1307}
1308
1309static int be_rx_queues_create(struct be_adapter *adapter)
1310{
1311 struct be_queue_info *eq, *q, *cq;
1312 int rc;
1313
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001314 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
1315 adapter->rx_eq.max_eqd = BE_MAX_EQD;
1316 adapter->rx_eq.min_eqd = 0;
1317 adapter->rx_eq.cur_eqd = 0;
1318 adapter->rx_eq.enable_aic = true;
1319
1320 /* Alloc Rx Event queue */
1321 eq = &adapter->rx_eq.q;
1322 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1323 sizeof(struct be_eq_entry));
1324 if (rc)
1325 return rc;
1326
1327 /* Ask BE to create Rx Event queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001328 rc = be_cmd_eq_create(adapter, eq, adapter->rx_eq.cur_eqd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001329 if (rc)
1330 goto rx_eq_free;
1331
1332 /* Alloc RX eth compl queue */
1333 cq = &adapter->rx_obj.cq;
1334 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1335 sizeof(struct be_eth_rx_compl));
1336 if (rc)
1337 goto rx_eq_destroy;
1338
1339 /* Ask BE to create Rx eth compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001340 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001341 if (rc)
1342 goto rx_cq_free;
1343
1344 /* Alloc RX eth queue */
1345 q = &adapter->rx_obj.q;
1346 rc = be_queue_alloc(adapter, q, RX_Q_LEN, sizeof(struct be_eth_rx_d));
1347 if (rc)
1348 goto rx_cq_destroy;
1349
1350 /* Ask BE to create Rx eth queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001351 rc = be_cmd_rxq_create(adapter, q, cq->id, rx_frag_size,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001352 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle, false);
1353 if (rc)
1354 goto rx_q_free;
1355
1356 return 0;
1357rx_q_free:
1358 be_queue_free(adapter, q);
1359rx_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001360 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001361rx_cq_free:
1362 be_queue_free(adapter, cq);
1363rx_eq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001364 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001365rx_eq_free:
1366 be_queue_free(adapter, eq);
1367 return rc;
1368}
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001369
Sathya Perlab628bde2009-08-17 00:58:26 +00001370/* There are 8 evt ids per func. Retruns the evt id's bit number */
1371static inline int be_evt_bit_get(struct be_adapter *adapter, u32 eq_id)
1372{
1373 return eq_id - 8 * be_pci_func(adapter);
1374}
1375
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001376static irqreturn_t be_intx(int irq, void *dev)
1377{
1378 struct be_adapter *adapter = dev;
Sathya Perla8788fdc2009-07-27 22:52:03 +00001379 int isr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001380
Sathya Perla8788fdc2009-07-27 22:52:03 +00001381 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
Sathya Perla55bdeed2010-02-02 07:48:40 -08001382 (adapter->tx_eq.q.id/ 8) * CEV_ISR_SIZE);
Sathya Perlac001c212009-07-01 01:06:07 +00001383 if (!isr)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001384 return IRQ_NONE;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001385
Sathya Perla8788fdc2009-07-27 22:52:03 +00001386 event_handle(adapter, &adapter->tx_eq);
1387 event_handle(adapter, &adapter->rx_eq);
Sathya Perlac001c212009-07-01 01:06:07 +00001388
Sathya Perla8788fdc2009-07-27 22:52:03 +00001389 return IRQ_HANDLED;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001390}
1391
1392static irqreturn_t be_msix_rx(int irq, void *dev)
1393{
1394 struct be_adapter *adapter = dev;
1395
Sathya Perla8788fdc2009-07-27 22:52:03 +00001396 event_handle(adapter, &adapter->rx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001397
1398 return IRQ_HANDLED;
1399}
1400
Sathya Perla5fb379e2009-06-18 00:02:59 +00001401static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001402{
1403 struct be_adapter *adapter = dev;
1404
Sathya Perla8788fdc2009-07-27 22:52:03 +00001405 event_handle(adapter, &adapter->tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001406
1407 return IRQ_HANDLED;
1408}
1409
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001410static inline bool do_gro(struct be_adapter *adapter,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001411 struct be_eth_rx_compl *rxcp)
1412{
1413 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1414 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1415
1416 if (err)
1417 drvr_stats(adapter)->be_rxcp_err++;
1418
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001419 return (tcp_frame && !err) ? true : false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001420}
1421
1422int be_poll_rx(struct napi_struct *napi, int budget)
1423{
1424 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
1425 struct be_adapter *adapter =
1426 container_of(rx_eq, struct be_adapter, rx_eq);
1427 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1428 struct be_eth_rx_compl *rxcp;
1429 u32 work_done;
1430
Ajit Khapardeb7b83ac2009-11-29 17:57:22 +00001431 adapter->stats.drvr_stats.be_rx_polls++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001432 for (work_done = 0; work_done < budget; work_done++) {
1433 rxcp = be_rx_compl_get(adapter);
1434 if (!rxcp)
1435 break;
1436
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001437 if (do_gro(adapter, rxcp))
1438 be_rx_compl_process_gro(adapter, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001439 else
1440 be_rx_compl_process(adapter, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001441
1442 be_rx_compl_reset(rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001443 }
1444
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001445 /* Refill the queue */
1446 if (atomic_read(&adapter->rx_obj.q.used) < RX_FRAGS_REFILL_WM)
1447 be_post_rx_frags(adapter);
1448
1449 /* All consumed */
1450 if (work_done < budget) {
1451 napi_complete(napi);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001452 be_cq_notify(adapter, rx_cq->id, true, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001453 } else {
1454 /* More to be consumed; continue with interrupts disabled */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001455 be_cq_notify(adapter, rx_cq->id, false, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001456 }
1457 return work_done;
1458}
1459
Sathya Perla5fb379e2009-06-18 00:02:59 +00001460void be_process_tx(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001461{
Sathya Perla5fb379e2009-06-18 00:02:59 +00001462 struct be_queue_info *txq = &adapter->tx_obj.q;
1463 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001464 struct be_eth_tx_compl *txcp;
1465 u32 num_cmpl = 0;
1466 u16 end_idx;
1467
Sathya Perla5fb379e2009-06-18 00:02:59 +00001468 while ((txcp = be_tx_compl_get(tx_cq))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001469 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1470 wrb_index, txcp);
1471 be_tx_compl_process(adapter, end_idx);
1472 num_cmpl++;
1473 }
1474
Sathya Perla5fb379e2009-06-18 00:02:59 +00001475 if (num_cmpl) {
Sathya Perla8788fdc2009-07-27 22:52:03 +00001476 be_cq_notify(adapter, tx_cq->id, true, num_cmpl);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001477
1478 /* As Tx wrbs have been freed up, wake up netdev queue if
1479 * it was stopped due to lack of tx wrbs.
1480 */
1481 if (netif_queue_stopped(adapter->netdev) &&
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001482 atomic_read(&txq->used) < txq->len / 2) {
Sathya Perla5fb379e2009-06-18 00:02:59 +00001483 netif_wake_queue(adapter->netdev);
1484 }
1485
1486 drvr_stats(adapter)->be_tx_events++;
1487 drvr_stats(adapter)->be_tx_compl += num_cmpl;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001488 }
Sathya Perla5fb379e2009-06-18 00:02:59 +00001489}
1490
1491/* As TX and MCC share the same EQ check for both TX and MCC completions.
1492 * For TX/MCC we don't honour budget; consume everything
1493 */
1494static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
1495{
1496 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1497 struct be_adapter *adapter =
1498 container_of(tx_eq, struct be_adapter, tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001499
1500 napi_complete(napi);
1501
Sathya Perla5fb379e2009-06-18 00:02:59 +00001502 be_process_tx(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001503
Sathya Perla8788fdc2009-07-27 22:52:03 +00001504 be_process_mcc(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001505
1506 return 1;
1507}
1508
Sathya Perlaea1dae12009-03-19 23:56:20 -07001509static void be_worker(struct work_struct *work)
1510{
1511 struct be_adapter *adapter =
1512 container_of(work, struct be_adapter, work.work);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001513
Sathya Perlab31c50a2009-09-17 10:30:13 -07001514 be_cmd_get_stats(adapter, &adapter->stats.cmd);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001515
1516 /* Set EQ delay */
1517 be_rx_eqd_update(adapter);
1518
Sathya Perla4097f662009-03-24 16:40:13 -07001519 be_tx_rate_update(adapter);
1520 be_rx_rate_update(adapter);
1521
Sathya Perlaea1dae12009-03-19 23:56:20 -07001522 if (adapter->rx_post_starved) {
1523 adapter->rx_post_starved = false;
1524 be_post_rx_frags(adapter);
1525 }
1526
1527 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1528}
1529
Sathya Perla8d56ff12009-11-22 22:02:26 +00001530static void be_msix_disable(struct be_adapter *adapter)
1531{
1532 if (adapter->msix_enabled) {
1533 pci_disable_msix(adapter->pdev);
1534 adapter->msix_enabled = false;
1535 }
1536}
1537
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001538static void be_msix_enable(struct be_adapter *adapter)
1539{
1540 int i, status;
1541
1542 for (i = 0; i < BE_NUM_MSIX_VECTORS; i++)
1543 adapter->msix_entries[i].entry = i;
1544
1545 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1546 BE_NUM_MSIX_VECTORS);
1547 if (status == 0)
1548 adapter->msix_enabled = true;
1549 return;
1550}
1551
1552static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1553{
Sathya Perlab628bde2009-08-17 00:58:26 +00001554 return adapter->msix_entries[
1555 be_evt_bit_get(adapter, eq_id)].vector;
1556}
1557
1558static int be_request_irq(struct be_adapter *adapter,
1559 struct be_eq_obj *eq_obj,
1560 void *handler, char *desc)
1561{
1562 struct net_device *netdev = adapter->netdev;
1563 int vec;
1564
1565 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
1566 vec = be_msix_vec_get(adapter, eq_obj->q.id);
1567 return request_irq(vec, handler, 0, eq_obj->desc, adapter);
1568}
1569
1570static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj)
1571{
1572 int vec = be_msix_vec_get(adapter, eq_obj->q.id);
1573 free_irq(vec, adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001574}
1575
1576static int be_msix_register(struct be_adapter *adapter)
1577{
Sathya Perlab628bde2009-08-17 00:58:26 +00001578 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001579
Sathya Perlab628bde2009-08-17 00:58:26 +00001580 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx");
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001581 if (status)
1582 goto err;
1583
Sathya Perlab628bde2009-08-17 00:58:26 +00001584 status = be_request_irq(adapter, &adapter->rx_eq, be_msix_rx, "rx");
1585 if (status)
1586 goto free_tx_irq;
1587
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001588 return 0;
Sathya Perlab628bde2009-08-17 00:58:26 +00001589
1590free_tx_irq:
1591 be_free_irq(adapter, &adapter->tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001592err:
1593 dev_warn(&adapter->pdev->dev,
1594 "MSIX Request IRQ failed - err %d\n", status);
1595 pci_disable_msix(adapter->pdev);
1596 adapter->msix_enabled = false;
1597 return status;
1598}
1599
1600static int be_irq_register(struct be_adapter *adapter)
1601{
1602 struct net_device *netdev = adapter->netdev;
1603 int status;
1604
1605 if (adapter->msix_enabled) {
1606 status = be_msix_register(adapter);
1607 if (status == 0)
1608 goto done;
1609 }
1610
1611 /* INTx */
1612 netdev->irq = adapter->pdev->irq;
1613 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1614 adapter);
1615 if (status) {
1616 dev_err(&adapter->pdev->dev,
1617 "INTx request IRQ failed - err %d\n", status);
1618 return status;
1619 }
1620done:
1621 adapter->isr_registered = true;
1622 return 0;
1623}
1624
1625static void be_irq_unregister(struct be_adapter *adapter)
1626{
1627 struct net_device *netdev = adapter->netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001628
1629 if (!adapter->isr_registered)
1630 return;
1631
1632 /* INTx */
1633 if (!adapter->msix_enabled) {
1634 free_irq(netdev->irq, adapter);
1635 goto done;
1636 }
1637
1638 /* MSIx */
Sathya Perlab628bde2009-08-17 00:58:26 +00001639 be_free_irq(adapter, &adapter->tx_eq);
1640 be_free_irq(adapter, &adapter->rx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001641done:
1642 adapter->isr_registered = false;
1643 return;
1644}
1645
1646static int be_open(struct net_device *netdev)
1647{
1648 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001649 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1650 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001651 bool link_up;
1652 int status;
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07001653 u8 mac_speed;
1654 u16 link_speed;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001655
1656 /* First time posting */
1657 be_post_rx_frags(adapter);
1658
1659 napi_enable(&rx_eq->napi);
1660 napi_enable(&tx_eq->napi);
1661
1662 be_irq_register(adapter);
1663
Sathya Perla8788fdc2009-07-27 22:52:03 +00001664 be_intr_set(adapter, true);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001665
1666 /* The evt queues are created in unarmed state; arm them */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001667 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
1668 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001669
1670 /* Rx compl queue may be in unarmed state; rearm it */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001671 be_cq_notify(adapter, adapter->rx_obj.cq.id, true, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001672
Sathya Perla7a1e9b22010-02-17 01:35:11 +00001673 /* Now that interrupts are on we can process async mcc */
1674 be_async_mcc_enable(adapter);
1675
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07001676 status = be_cmd_link_status_query(adapter, &link_up, &mac_speed,
1677 &link_speed);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001678 if (status)
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001679 goto ret_sts;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001680 be_link_status_update(adapter, link_up);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001681
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001682 status = be_vid_config(adapter);
1683 if (status)
1684 goto ret_sts;
1685
1686 status = be_cmd_set_flow_control(adapter,
1687 adapter->tx_fc, adapter->rx_fc);
1688 if (status)
1689 goto ret_sts;
1690
Sathya Perla5fb379e2009-06-18 00:02:59 +00001691 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001692ret_sts:
1693 return status;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001694}
1695
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00001696static int be_setup_wol(struct be_adapter *adapter, bool enable)
1697{
1698 struct be_dma_mem cmd;
1699 int status = 0;
1700 u8 mac[ETH_ALEN];
1701
1702 memset(mac, 0, ETH_ALEN);
1703
1704 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
1705 cmd.va = pci_alloc_consistent(adapter->pdev, cmd.size, &cmd.dma);
1706 if (cmd.va == NULL)
1707 return -1;
1708 memset(cmd.va, 0, cmd.size);
1709
1710 if (enable) {
1711 status = pci_write_config_dword(adapter->pdev,
1712 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
1713 if (status) {
1714 dev_err(&adapter->pdev->dev,
1715 "Could not enable Wake-on-lan \n");
1716 pci_free_consistent(adapter->pdev, cmd.size, cmd.va,
1717 cmd.dma);
1718 return status;
1719 }
1720 status = be_cmd_enable_magic_wol(adapter,
1721 adapter->netdev->dev_addr, &cmd);
1722 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
1723 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
1724 } else {
1725 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
1726 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
1727 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
1728 }
1729
1730 pci_free_consistent(adapter->pdev, cmd.size, cmd.va, cmd.dma);
1731 return status;
1732}
1733
Sathya Perla5fb379e2009-06-18 00:02:59 +00001734static int be_setup(struct be_adapter *adapter)
1735{
Sathya Perla5fb379e2009-06-18 00:02:59 +00001736 struct net_device *netdev = adapter->netdev;
Sathya Perla73d540f2009-10-14 20:20:42 +00001737 u32 cap_flags, en_flags;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001738 int status;
1739
Sathya Perla73d540f2009-10-14 20:20:42 +00001740 cap_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
1741 BE_IF_FLAGS_MCAST_PROMISCUOUS |
1742 BE_IF_FLAGS_PROMISCUOUS |
1743 BE_IF_FLAGS_PASS_L3L4_ERRORS;
1744 en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
1745 BE_IF_FLAGS_PASS_L3L4_ERRORS;
1746
1747 status = be_cmd_if_create(adapter, cap_flags, en_flags,
1748 netdev->dev_addr, false/* pmac_invalid */,
1749 &adapter->if_handle, &adapter->pmac_id);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001750 if (status != 0)
1751 goto do_none;
1752
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001753 status = be_tx_queues_create(adapter);
1754 if (status != 0)
1755 goto if_destroy;
1756
1757 status = be_rx_queues_create(adapter);
1758 if (status != 0)
1759 goto tx_qs_destroy;
1760
Sathya Perla5fb379e2009-06-18 00:02:59 +00001761 status = be_mcc_queues_create(adapter);
1762 if (status != 0)
1763 goto rx_qs_destroy;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001764
Ajit Khaparde0dffc832009-11-29 17:57:46 +00001765 adapter->link_speed = -1;
1766
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001767 return 0;
1768
Sathya Perla5fb379e2009-06-18 00:02:59 +00001769rx_qs_destroy:
1770 be_rx_queues_destroy(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001771tx_qs_destroy:
1772 be_tx_queues_destroy(adapter);
1773if_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001774 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001775do_none:
1776 return status;
1777}
1778
Sathya Perla5fb379e2009-06-18 00:02:59 +00001779static int be_clear(struct be_adapter *adapter)
1780{
Sathya Perla1a8887d2009-08-17 00:58:41 +00001781 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001782 be_rx_queues_destroy(adapter);
1783 be_tx_queues_destroy(adapter);
1784
Sathya Perla8788fdc2009-07-27 22:52:03 +00001785 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001786
Sathya Perla2243e2e2009-11-22 22:02:03 +00001787 /* tell fw we're done with firing cmds */
1788 be_cmd_fw_clean(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001789 return 0;
1790}
1791
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001792static int be_close(struct net_device *netdev)
1793{
1794 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001795 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1796 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1797 int vec;
1798
Sathya Perlab305be72009-06-10 02:18:35 +00001799 cancel_delayed_work_sync(&adapter->work);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001800
Sathya Perla7a1e9b22010-02-17 01:35:11 +00001801 be_async_mcc_disable(adapter);
1802
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001803 netif_stop_queue(netdev);
1804 netif_carrier_off(netdev);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001805 adapter->link_up = false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001806
Sathya Perla8788fdc2009-07-27 22:52:03 +00001807 be_intr_set(adapter, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001808
1809 if (adapter->msix_enabled) {
1810 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1811 synchronize_irq(vec);
1812 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1813 synchronize_irq(vec);
1814 } else {
1815 synchronize_irq(netdev->irq);
1816 }
1817 be_irq_unregister(adapter);
1818
1819 napi_disable(&rx_eq->napi);
1820 napi_disable(&tx_eq->napi);
1821
Sathya Perlaa8e91792009-08-10 03:42:43 +00001822 /* Wait for all pending tx completions to arrive so that
1823 * all tx skbs are freed.
1824 */
1825 be_tx_compl_clean(adapter);
1826
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001827 return 0;
1828}
1829
Ajit Khaparde84517482009-09-04 03:12:16 +00001830#define FW_FILE_HDR_SIGN "ServerEngines Corp. "
1831char flash_cookie[2][16] = {"*** SE FLAS",
1832 "H DIRECTORY *** "};
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001833
1834static bool be_flash_redboot(struct be_adapter *adapter,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001835 const u8 *p, u32 img_start, int image_size,
1836 int hdr_size)
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001837{
1838 u32 crc_offset;
1839 u8 flashed_crc[4];
1840 int status;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001841
1842 crc_offset = hdr_size + img_start + image_size - 4;
1843
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001844 p += crc_offset;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001845
1846 status = be_cmd_get_flash_crc(adapter, flashed_crc,
1847 (img_start + image_size - 4));
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001848 if (status) {
1849 dev_err(&adapter->pdev->dev,
1850 "could not get crc from flash, not flashing redboot\n");
1851 return false;
1852 }
1853
1854 /*update redboot only if crc does not match*/
1855 if (!memcmp(flashed_crc, p, 4))
1856 return false;
1857 else
1858 return true;
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001859}
1860
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001861static int be_flash_data(struct be_adapter *adapter,
Ajit Khaparde84517482009-09-04 03:12:16 +00001862 const struct firmware *fw,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001863 struct be_dma_mem *flash_cmd, int num_of_images)
1864
Ajit Khaparde84517482009-09-04 03:12:16 +00001865{
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001866 int status = 0, i, filehdr_size = 0;
1867 u32 total_bytes = 0, flash_op;
Ajit Khaparde84517482009-09-04 03:12:16 +00001868 int num_bytes;
1869 const u8 *p = fw->data;
1870 struct be_cmd_write_flashrom *req = flash_cmd->va;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001871 struct flash_comp *pflashcomp;
Ajit Khaparde84517482009-09-04 03:12:16 +00001872
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001873 struct flash_comp gen3_flash_types[8] = {
1874 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, IMG_TYPE_ISCSI_ACTIVE,
1875 FLASH_IMAGE_MAX_SIZE_g3},
1876 { FLASH_REDBOOT_START_g3, IMG_TYPE_REDBOOT,
1877 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3},
1878 { FLASH_iSCSI_BIOS_START_g3, IMG_TYPE_BIOS,
1879 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
1880 { FLASH_PXE_BIOS_START_g3, IMG_TYPE_PXE_BIOS,
1881 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
1882 { FLASH_FCoE_BIOS_START_g3, IMG_TYPE_FCOE_BIOS,
1883 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
1884 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, IMG_TYPE_ISCSI_BACKUP,
1885 FLASH_IMAGE_MAX_SIZE_g3},
1886 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, IMG_TYPE_FCOE_FW_ACTIVE,
1887 FLASH_IMAGE_MAX_SIZE_g3},
1888 { FLASH_FCoE_BACKUP_IMAGE_START_g3, IMG_TYPE_FCOE_FW_BACKUP,
1889 FLASH_IMAGE_MAX_SIZE_g3}
1890 };
1891 struct flash_comp gen2_flash_types[8] = {
1892 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, IMG_TYPE_ISCSI_ACTIVE,
1893 FLASH_IMAGE_MAX_SIZE_g2},
1894 { FLASH_REDBOOT_START_g2, IMG_TYPE_REDBOOT,
1895 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2},
1896 { FLASH_iSCSI_BIOS_START_g2, IMG_TYPE_BIOS,
1897 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
1898 { FLASH_PXE_BIOS_START_g2, IMG_TYPE_PXE_BIOS,
1899 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
1900 { FLASH_FCoE_BIOS_START_g2, IMG_TYPE_FCOE_BIOS,
1901 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
1902 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, IMG_TYPE_ISCSI_BACKUP,
1903 FLASH_IMAGE_MAX_SIZE_g2},
1904 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, IMG_TYPE_FCOE_FW_ACTIVE,
1905 FLASH_IMAGE_MAX_SIZE_g2},
1906 { FLASH_FCoE_BACKUP_IMAGE_START_g2, IMG_TYPE_FCOE_FW_BACKUP,
1907 FLASH_IMAGE_MAX_SIZE_g2}
1908 };
1909
1910 if (adapter->generation == BE_GEN3) {
1911 pflashcomp = gen3_flash_types;
1912 filehdr_size = sizeof(struct flash_file_hdr_g3);
1913 } else {
1914 pflashcomp = gen2_flash_types;
1915 filehdr_size = sizeof(struct flash_file_hdr_g2);
Ajit Khaparde84517482009-09-04 03:12:16 +00001916 }
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001917 for (i = 0; i < 8; i++) {
1918 if ((pflashcomp[i].optype == IMG_TYPE_REDBOOT) &&
1919 (!be_flash_redboot(adapter, fw->data,
1920 pflashcomp[i].offset, pflashcomp[i].size,
1921 filehdr_size)))
1922 continue;
1923 p = fw->data;
1924 p += filehdr_size + pflashcomp[i].offset
1925 + (num_of_images * sizeof(struct image_hdr));
1926 if (p + pflashcomp[i].size > fw->data + fw->size)
Ajit Khaparde84517482009-09-04 03:12:16 +00001927 return -1;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001928 total_bytes = pflashcomp[i].size;
1929 while (total_bytes) {
1930 if (total_bytes > 32*1024)
1931 num_bytes = 32*1024;
1932 else
1933 num_bytes = total_bytes;
1934 total_bytes -= num_bytes;
Ajit Khaparde84517482009-09-04 03:12:16 +00001935
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001936 if (!total_bytes)
1937 flash_op = FLASHROM_OPER_FLASH;
1938 else
1939 flash_op = FLASHROM_OPER_SAVE;
1940 memcpy(req->params.data_buf, p, num_bytes);
1941 p += num_bytes;
1942 status = be_cmd_write_flashrom(adapter, flash_cmd,
1943 pflashcomp[i].optype, flash_op, num_bytes);
1944 if (status) {
1945 dev_err(&adapter->pdev->dev,
1946 "cmd to write to flash rom failed.\n");
1947 return -1;
1948 }
1949 yield();
Ajit Khaparde84517482009-09-04 03:12:16 +00001950 }
Ajit Khaparde84517482009-09-04 03:12:16 +00001951 }
Ajit Khaparde84517482009-09-04 03:12:16 +00001952 return 0;
1953}
1954
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001955static int get_ufigen_type(struct flash_file_hdr_g2 *fhdr)
1956{
1957 if (fhdr == NULL)
1958 return 0;
1959 if (fhdr->build[0] == '3')
1960 return BE_GEN3;
1961 else if (fhdr->build[0] == '2')
1962 return BE_GEN2;
1963 else
1964 return 0;
1965}
1966
Ajit Khaparde84517482009-09-04 03:12:16 +00001967int be_load_fw(struct be_adapter *adapter, u8 *func)
1968{
1969 char fw_file[ETHTOOL_FLASH_MAX_FILENAME];
1970 const struct firmware *fw;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001971 struct flash_file_hdr_g2 *fhdr;
1972 struct flash_file_hdr_g3 *fhdr3;
1973 struct image_hdr *img_hdr_ptr = NULL;
Ajit Khaparde84517482009-09-04 03:12:16 +00001974 struct be_dma_mem flash_cmd;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001975 int status, i = 0;
Ajit Khaparde84517482009-09-04 03:12:16 +00001976 const u8 *p;
Ajit Khaparde84517482009-09-04 03:12:16 +00001977 char fw_ver[FW_VER_LEN];
1978 char fw_cfg;
1979
1980 status = be_cmd_get_fw_ver(adapter, fw_ver);
1981 if (status)
1982 return status;
1983
1984 fw_cfg = *(fw_ver + 2);
1985 if (fw_cfg == '0')
1986 fw_cfg = '1';
1987 strcpy(fw_file, func);
1988
1989 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
1990 if (status)
1991 goto fw_exit;
1992
1993 p = fw->data;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001994 fhdr = (struct flash_file_hdr_g2 *) p;
Ajit Khaparde84517482009-09-04 03:12:16 +00001995 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
1996
Ajit Khaparde84517482009-09-04 03:12:16 +00001997 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
1998 flash_cmd.va = pci_alloc_consistent(adapter->pdev, flash_cmd.size,
1999 &flash_cmd.dma);
2000 if (!flash_cmd.va) {
2001 status = -ENOMEM;
2002 dev_err(&adapter->pdev->dev,
2003 "Memory allocation failure while flashing\n");
2004 goto fw_exit;
2005 }
2006
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002007 if ((adapter->generation == BE_GEN3) &&
2008 (get_ufigen_type(fhdr) == BE_GEN3)) {
2009 fhdr3 = (struct flash_file_hdr_g3 *) fw->data;
2010 for (i = 0; i < fhdr3->num_imgs; i++) {
2011 img_hdr_ptr = (struct image_hdr *) (fw->data +
2012 (sizeof(struct flash_file_hdr_g3) +
2013 i * sizeof(struct image_hdr)));
2014 if (img_hdr_ptr->imageid == 1) {
2015 status = be_flash_data(adapter, fw,
2016 &flash_cmd, fhdr3->num_imgs);
2017 }
2018
2019 }
2020 } else if ((adapter->generation == BE_GEN2) &&
2021 (get_ufigen_type(fhdr) == BE_GEN2)) {
2022 status = be_flash_data(adapter, fw, &flash_cmd, 0);
2023 } else {
2024 dev_err(&adapter->pdev->dev,
2025 "UFI and Interface are not compatible for flashing\n");
2026 status = -1;
Ajit Khaparde84517482009-09-04 03:12:16 +00002027 }
2028
2029 pci_free_consistent(adapter->pdev, flash_cmd.size, flash_cmd.va,
2030 flash_cmd.dma);
2031 if (status) {
2032 dev_err(&adapter->pdev->dev, "Firmware load error\n");
2033 goto fw_exit;
2034 }
2035
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002036 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
Ajit Khaparde84517482009-09-04 03:12:16 +00002037
2038fw_exit:
2039 release_firmware(fw);
2040 return status;
2041}
2042
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002043static struct net_device_ops be_netdev_ops = {
2044 .ndo_open = be_open,
2045 .ndo_stop = be_close,
2046 .ndo_start_xmit = be_xmit,
2047 .ndo_get_stats = be_get_stats,
2048 .ndo_set_rx_mode = be_set_multicast_list,
2049 .ndo_set_mac_address = be_mac_addr_set,
2050 .ndo_change_mtu = be_change_mtu,
2051 .ndo_validate_addr = eth_validate_addr,
2052 .ndo_vlan_rx_register = be_vlan_register,
2053 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
2054 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
2055};
2056
2057static void be_netdev_init(struct net_device *netdev)
2058{
2059 struct be_adapter *adapter = netdev_priv(netdev);
2060
2061 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
Ajit Khaparde583e3f32009-10-05 02:22:19 +00002062 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_HW_CSUM |
2063 NETIF_F_GRO;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002064
Ajit Khaparde51c59872009-11-29 17:54:54 +00002065 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2066
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002067 netdev->flags |= IFF_MULTICAST;
2068
Ajit Khaparde728a9972009-04-13 15:41:22 -07002069 adapter->rx_csum = true;
2070
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002071 /* Default settings for Rx and Tx flow control */
2072 adapter->rx_fc = true;
2073 adapter->tx_fc = true;
2074
Ajit Khapardec190e3c2009-09-04 03:12:29 +00002075 netif_set_gso_max_size(netdev, 65535);
2076
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002077 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
2078
2079 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
2080
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002081 netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx,
2082 BE_NAPI_WEIGHT);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002083 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002084 BE_NAPI_WEIGHT);
2085
2086 netif_carrier_off(netdev);
2087 netif_stop_queue(netdev);
2088}
2089
2090static void be_unmap_pci_bars(struct be_adapter *adapter)
2091{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002092 if (adapter->csr)
2093 iounmap(adapter->csr);
2094 if (adapter->db)
2095 iounmap(adapter->db);
2096 if (adapter->pcicfg)
2097 iounmap(adapter->pcicfg);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002098}
2099
2100static int be_map_pci_bars(struct be_adapter *adapter)
2101{
2102 u8 __iomem *addr;
Ajit Khaparde7b139c82010-01-27 21:56:44 +00002103 int pcicfg_reg;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002104
2105 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
2106 pci_resource_len(adapter->pdev, 2));
2107 if (addr == NULL)
2108 return -ENOMEM;
Sathya Perla8788fdc2009-07-27 22:52:03 +00002109 adapter->csr = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002110
2111 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 4),
2112 128 * 1024);
2113 if (addr == NULL)
2114 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00002115 adapter->db = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002116
Ajit Khaparde7b139c82010-01-27 21:56:44 +00002117 if (adapter->generation == BE_GEN2)
2118 pcicfg_reg = 1;
2119 else
2120 pcicfg_reg = 0;
2121
2122 addr = ioremap_nocache(pci_resource_start(adapter->pdev, pcicfg_reg),
2123 pci_resource_len(adapter->pdev, pcicfg_reg));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002124 if (addr == NULL)
2125 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00002126 adapter->pcicfg = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002127
2128 return 0;
2129pci_map_err:
2130 be_unmap_pci_bars(adapter);
2131 return -ENOMEM;
2132}
2133
2134
2135static void be_ctrl_cleanup(struct be_adapter *adapter)
2136{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002137 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002138
2139 be_unmap_pci_bars(adapter);
2140
2141 if (mem->va)
2142 pci_free_consistent(adapter->pdev, mem->size,
2143 mem->va, mem->dma);
Sathya Perlae7b909a2009-11-22 22:01:10 +00002144
2145 mem = &adapter->mc_cmd_mem;
2146 if (mem->va)
2147 pci_free_consistent(adapter->pdev, mem->size,
2148 mem->va, mem->dma);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002149}
2150
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002151static int be_ctrl_init(struct be_adapter *adapter)
2152{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002153 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
2154 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002155 struct be_dma_mem *mc_cmd_mem = &adapter->mc_cmd_mem;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002156 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002157
2158 status = be_map_pci_bars(adapter);
2159 if (status)
Sathya Perlae7b909a2009-11-22 22:01:10 +00002160 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002161
2162 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
2163 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
2164 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
2165 if (!mbox_mem_alloc->va) {
Sathya Perlae7b909a2009-11-22 22:01:10 +00002166 status = -ENOMEM;
2167 goto unmap_pci_bars;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002168 }
Sathya Perlae7b909a2009-11-22 22:01:10 +00002169
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002170 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
2171 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
2172 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
2173 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Sathya Perlae7b909a2009-11-22 22:01:10 +00002174
2175 mc_cmd_mem->size = sizeof(struct be_cmd_req_mcast_mac_config);
2176 mc_cmd_mem->va = pci_alloc_consistent(adapter->pdev, mc_cmd_mem->size,
2177 &mc_cmd_mem->dma);
2178 if (mc_cmd_mem->va == NULL) {
2179 status = -ENOMEM;
2180 goto free_mbox;
2181 }
2182 memset(mc_cmd_mem->va, 0, mc_cmd_mem->size);
2183
Sathya Perla8788fdc2009-07-27 22:52:03 +00002184 spin_lock_init(&adapter->mbox_lock);
2185 spin_lock_init(&adapter->mcc_lock);
2186 spin_lock_init(&adapter->mcc_cq_lock);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002187
Sathya Perlacf588472010-02-14 21:22:01 +00002188 pci_save_state(adapter->pdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002189 return 0;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002190
2191free_mbox:
2192 pci_free_consistent(adapter->pdev, mbox_mem_alloc->size,
2193 mbox_mem_alloc->va, mbox_mem_alloc->dma);
2194
2195unmap_pci_bars:
2196 be_unmap_pci_bars(adapter);
2197
2198done:
2199 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002200}
2201
2202static void be_stats_cleanup(struct be_adapter *adapter)
2203{
2204 struct be_stats_obj *stats = &adapter->stats;
2205 struct be_dma_mem *cmd = &stats->cmd;
2206
2207 if (cmd->va)
2208 pci_free_consistent(adapter->pdev, cmd->size,
2209 cmd->va, cmd->dma);
2210}
2211
2212static int be_stats_init(struct be_adapter *adapter)
2213{
2214 struct be_stats_obj *stats = &adapter->stats;
2215 struct be_dma_mem *cmd = &stats->cmd;
2216
2217 cmd->size = sizeof(struct be_cmd_req_get_stats);
2218 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
2219 if (cmd->va == NULL)
2220 return -1;
David S. Millerd291b9a2010-01-28 21:36:21 -08002221 memset(cmd->va, 0, cmd->size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002222 return 0;
2223}
2224
2225static void __devexit be_remove(struct pci_dev *pdev)
2226{
2227 struct be_adapter *adapter = pci_get_drvdata(pdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002228
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002229 if (!adapter)
2230 return;
2231
2232 unregister_netdev(adapter->netdev);
2233
Sathya Perla5fb379e2009-06-18 00:02:59 +00002234 be_clear(adapter);
2235
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002236 be_stats_cleanup(adapter);
2237
2238 be_ctrl_cleanup(adapter);
2239
Sathya Perla8d56ff12009-11-22 22:02:26 +00002240 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002241
2242 pci_set_drvdata(pdev, NULL);
2243 pci_release_regions(pdev);
2244 pci_disable_device(pdev);
2245
2246 free_netdev(adapter->netdev);
2247}
2248
Sathya Perla2243e2e2009-11-22 22:02:03 +00002249static int be_get_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002250{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002251 int status;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002252 u8 mac[ETH_ALEN];
Sathya Perla43a04fdc2009-10-14 20:21:17 +00002253
Sathya Perla8788fdc2009-07-27 22:52:03 +00002254 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002255 if (status)
2256 return status;
2257
Ajit Khapardedcb9b562009-09-30 21:58:22 -07002258 status = be_cmd_query_fw_cfg(adapter,
2259 &adapter->port_num, &adapter->cap);
Sathya Perla2243e2e2009-11-22 22:02:03 +00002260 if (status)
2261 return status;
2262
2263 memset(mac, 0, ETH_ALEN);
2264 status = be_cmd_mac_addr_query(adapter, mac,
2265 MAC_ADDRESS_TYPE_NETWORK, true /*permanent */, 0);
2266 if (status)
2267 return status;
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002268
2269 if (!is_valid_ether_addr(mac))
2270 return -EADDRNOTAVAIL;
2271
Sathya Perla2243e2e2009-11-22 22:02:03 +00002272 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
Ajit Khaparde35a65282009-11-29 17:55:39 +00002273 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
Sathya Perla2243e2e2009-11-22 22:02:03 +00002274
Ajit Khaparde82903e42010-02-09 01:34:57 +00002275 if (adapter->cap & 0x400)
2276 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED/4;
2277 else
2278 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED;
2279
Sathya Perla2243e2e2009-11-22 22:02:03 +00002280 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002281}
2282
2283static int __devinit be_probe(struct pci_dev *pdev,
2284 const struct pci_device_id *pdev_id)
2285{
2286 int status = 0;
2287 struct be_adapter *adapter;
2288 struct net_device *netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002289
2290 status = pci_enable_device(pdev);
2291 if (status)
2292 goto do_none;
2293
2294 status = pci_request_regions(pdev, DRV_NAME);
2295 if (status)
2296 goto disable_dev;
2297 pci_set_master(pdev);
2298
2299 netdev = alloc_etherdev(sizeof(struct be_adapter));
2300 if (netdev == NULL) {
2301 status = -ENOMEM;
2302 goto rel_reg;
2303 }
2304 adapter = netdev_priv(netdev);
Ajit Khaparde7b139c82010-01-27 21:56:44 +00002305
2306 switch (pdev->device) {
2307 case BE_DEVICE_ID1:
2308 case OC_DEVICE_ID1:
2309 adapter->generation = BE_GEN2;
2310 break;
2311 case BE_DEVICE_ID2:
2312 case OC_DEVICE_ID2:
2313 adapter->generation = BE_GEN3;
2314 break;
2315 default:
2316 adapter->generation = 0;
2317 }
2318
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002319 adapter->pdev = pdev;
2320 pci_set_drvdata(pdev, adapter);
2321 adapter->netdev = netdev;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002322 be_netdev_init(netdev);
2323 SET_NETDEV_DEV(netdev, &pdev->dev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002324
2325 be_msix_enable(adapter);
2326
Yang Hongyange9304382009-04-13 14:40:14 -07002327 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002328 if (!status) {
2329 netdev->features |= NETIF_F_HIGHDMA;
2330 } else {
Yang Hongyange9304382009-04-13 14:40:14 -07002331 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002332 if (status) {
2333 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
2334 goto free_netdev;
2335 }
2336 }
2337
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002338 status = be_ctrl_init(adapter);
2339 if (status)
2340 goto free_netdev;
2341
Sathya Perla2243e2e2009-11-22 22:02:03 +00002342 /* sync up with fw's ready state */
2343 status = be_cmd_POST(adapter);
2344 if (status)
2345 goto ctrl_clean;
2346
2347 /* tell fw we're ready to fire cmds */
2348 status = be_cmd_fw_init(adapter);
2349 if (status)
2350 goto ctrl_clean;
2351
2352 status = be_cmd_reset_function(adapter);
2353 if (status)
2354 goto ctrl_clean;
2355
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002356 status = be_stats_init(adapter);
2357 if (status)
2358 goto ctrl_clean;
2359
Sathya Perla2243e2e2009-11-22 22:02:03 +00002360 status = be_get_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002361 if (status)
2362 goto stats_clean;
2363
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002364 INIT_DELAYED_WORK(&adapter->work, be_worker);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002365
Sathya Perla5fb379e2009-06-18 00:02:59 +00002366 status = be_setup(adapter);
2367 if (status)
2368 goto stats_clean;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002369
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002370 status = register_netdev(netdev);
2371 if (status != 0)
Sathya Perla5fb379e2009-06-18 00:02:59 +00002372 goto unsetup;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002373
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002374 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002375 return 0;
2376
Sathya Perla5fb379e2009-06-18 00:02:59 +00002377unsetup:
2378 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002379stats_clean:
2380 be_stats_cleanup(adapter);
2381ctrl_clean:
2382 be_ctrl_cleanup(adapter);
2383free_netdev:
Sathya Perla8d56ff12009-11-22 22:02:26 +00002384 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002385 free_netdev(adapter->netdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002386 pci_set_drvdata(pdev, NULL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002387rel_reg:
2388 pci_release_regions(pdev);
2389disable_dev:
2390 pci_disable_device(pdev);
2391do_none:
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002392 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002393 return status;
2394}
2395
2396static int be_suspend(struct pci_dev *pdev, pm_message_t state)
2397{
2398 struct be_adapter *adapter = pci_get_drvdata(pdev);
2399 struct net_device *netdev = adapter->netdev;
2400
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002401 if (adapter->wol)
2402 be_setup_wol(adapter, true);
2403
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002404 netif_device_detach(netdev);
2405 if (netif_running(netdev)) {
2406 rtnl_lock();
2407 be_close(netdev);
2408 rtnl_unlock();
2409 }
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002410 be_cmd_get_flow_control(adapter, &adapter->tx_fc, &adapter->rx_fc);
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002411 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002412
2413 pci_save_state(pdev);
2414 pci_disable_device(pdev);
2415 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2416 return 0;
2417}
2418
2419static int be_resume(struct pci_dev *pdev)
2420{
2421 int status = 0;
2422 struct be_adapter *adapter = pci_get_drvdata(pdev);
2423 struct net_device *netdev = adapter->netdev;
2424
2425 netif_device_detach(netdev);
2426
2427 status = pci_enable_device(pdev);
2428 if (status)
2429 return status;
2430
2431 pci_set_power_state(pdev, 0);
2432 pci_restore_state(pdev);
2433
Sathya Perla2243e2e2009-11-22 22:02:03 +00002434 /* tell fw we're ready to fire cmds */
2435 status = be_cmd_fw_init(adapter);
2436 if (status)
2437 return status;
2438
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002439 be_setup(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002440 if (netif_running(netdev)) {
2441 rtnl_lock();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002442 be_open(netdev);
2443 rtnl_unlock();
2444 }
2445 netif_device_attach(netdev);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002446
2447 if (adapter->wol)
2448 be_setup_wol(adapter, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002449 return 0;
2450}
2451
Sathya Perla82456b02010-02-17 01:35:37 +00002452/*
2453 * An FLR will stop BE from DMAing any data.
2454 */
2455static void be_shutdown(struct pci_dev *pdev)
2456{
2457 struct be_adapter *adapter = pci_get_drvdata(pdev);
2458 struct net_device *netdev = adapter->netdev;
2459
2460 netif_device_detach(netdev);
2461
2462 be_cmd_reset_function(adapter);
2463
2464 if (adapter->wol)
2465 be_setup_wol(adapter, true);
2466
2467 pci_disable_device(pdev);
2468
2469 return;
2470}
2471
Sathya Perlacf588472010-02-14 21:22:01 +00002472static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
2473 pci_channel_state_t state)
2474{
2475 struct be_adapter *adapter = pci_get_drvdata(pdev);
2476 struct net_device *netdev = adapter->netdev;
2477
2478 dev_err(&adapter->pdev->dev, "EEH error detected\n");
2479
2480 adapter->eeh_err = true;
2481
2482 netif_device_detach(netdev);
2483
2484 if (netif_running(netdev)) {
2485 rtnl_lock();
2486 be_close(netdev);
2487 rtnl_unlock();
2488 }
2489 be_clear(adapter);
2490
2491 if (state == pci_channel_io_perm_failure)
2492 return PCI_ERS_RESULT_DISCONNECT;
2493
2494 pci_disable_device(pdev);
2495
2496 return PCI_ERS_RESULT_NEED_RESET;
2497}
2498
2499static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
2500{
2501 struct be_adapter *adapter = pci_get_drvdata(pdev);
2502 int status;
2503
2504 dev_info(&adapter->pdev->dev, "EEH reset\n");
2505 adapter->eeh_err = false;
2506
2507 status = pci_enable_device(pdev);
2508 if (status)
2509 return PCI_ERS_RESULT_DISCONNECT;
2510
2511 pci_set_master(pdev);
2512 pci_set_power_state(pdev, 0);
2513 pci_restore_state(pdev);
2514
2515 /* Check if card is ok and fw is ready */
2516 status = be_cmd_POST(adapter);
2517 if (status)
2518 return PCI_ERS_RESULT_DISCONNECT;
2519
2520 return PCI_ERS_RESULT_RECOVERED;
2521}
2522
2523static void be_eeh_resume(struct pci_dev *pdev)
2524{
2525 int status = 0;
2526 struct be_adapter *adapter = pci_get_drvdata(pdev);
2527 struct net_device *netdev = adapter->netdev;
2528
2529 dev_info(&adapter->pdev->dev, "EEH resume\n");
2530
2531 pci_save_state(pdev);
2532
2533 /* tell fw we're ready to fire cmds */
2534 status = be_cmd_fw_init(adapter);
2535 if (status)
2536 goto err;
2537
2538 status = be_setup(adapter);
2539 if (status)
2540 goto err;
2541
2542 if (netif_running(netdev)) {
2543 status = be_open(netdev);
2544 if (status)
2545 goto err;
2546 }
2547 netif_device_attach(netdev);
2548 return;
2549err:
2550 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
2551 return;
2552}
2553
2554static struct pci_error_handlers be_eeh_handlers = {
2555 .error_detected = be_eeh_err_detected,
2556 .slot_reset = be_eeh_reset,
2557 .resume = be_eeh_resume,
2558};
2559
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002560static struct pci_driver be_driver = {
2561 .name = DRV_NAME,
2562 .id_table = be_dev_ids,
2563 .probe = be_probe,
2564 .remove = be_remove,
2565 .suspend = be_suspend,
Sathya Perlacf588472010-02-14 21:22:01 +00002566 .resume = be_resume,
Sathya Perla82456b02010-02-17 01:35:37 +00002567 .shutdown = be_shutdown,
Sathya Perlacf588472010-02-14 21:22:01 +00002568 .err_handler = &be_eeh_handlers
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002569};
2570
2571static int __init be_init_module(void)
2572{
Joe Perches8e95a202009-12-03 07:58:21 +00002573 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
2574 rx_frag_size != 2048) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002575 printk(KERN_WARNING DRV_NAME
2576 " : Module param rx_frag_size must be 2048/4096/8192."
2577 " Using 2048\n");
2578 rx_frag_size = 2048;
2579 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002580
2581 return pci_register_driver(&be_driver);
2582}
2583module_init(be_init_module);
2584
2585static void __exit be_exit_module(void)
2586{
2587 pci_unregister_driver(&be_driver);
2588}
2589module_exit(be_exit_module);