blob: 876b357101fac9d14a6c5ab77759f9b5f76d9300 [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) },
34 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
35 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
Sathya Perla6b7c5b92009-03-11 23:32:03 -070036 { 0 }
37};
38MODULE_DEVICE_TABLE(pci, be_dev_ids);
39
40static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
41{
42 struct be_dma_mem *mem = &q->dma_mem;
43 if (mem->va)
44 pci_free_consistent(adapter->pdev, mem->size,
45 mem->va, mem->dma);
46}
47
48static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
49 u16 len, u16 entry_size)
50{
51 struct be_dma_mem *mem = &q->dma_mem;
52
53 memset(q, 0, sizeof(*q));
54 q->len = len;
55 q->entry_size = entry_size;
56 mem->size = len * entry_size;
57 mem->va = pci_alloc_consistent(adapter->pdev, mem->size, &mem->dma);
58 if (!mem->va)
59 return -1;
60 memset(mem->va, 0, mem->size);
61 return 0;
62}
63
Sathya Perla8788fdc2009-07-27 22:52:03 +000064static void be_intr_set(struct be_adapter *adapter, bool enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070065{
Sathya Perla8788fdc2009-07-27 22:52:03 +000066 u8 __iomem *addr = adapter->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
Sathya Perla6b7c5b92009-03-11 23:32:03 -070067 u32 reg = ioread32(addr);
68 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +000069
70 if (!enabled && enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070071 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +000072 else if (enabled && !enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070073 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +000074 else
Sathya Perla6b7c5b92009-03-11 23:32:03 -070075 return;
Sathya Perla5f0b8492009-07-27 22:52:56 +000076
Sathya Perla6b7c5b92009-03-11 23:32:03 -070077 iowrite32(reg, addr);
78}
79
Sathya Perla8788fdc2009-07-27 22:52:03 +000080static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070081{
82 u32 val = 0;
83 val |= qid & DB_RQ_RING_ID_MASK;
84 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +000085 iowrite32(val, adapter->db + DB_RQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -070086}
87
Sathya Perla8788fdc2009-07-27 22:52:03 +000088static void be_txq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070089{
90 u32 val = 0;
91 val |= qid & DB_TXULP_RING_ID_MASK;
92 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +000093 iowrite32(val, adapter->db + DB_TXULP1_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -070094}
95
Sathya Perla8788fdc2009-07-27 22:52:03 +000096static void be_eq_notify(struct be_adapter *adapter, u16 qid,
Sathya Perla6b7c5b92009-03-11 23:32:03 -070097 bool arm, bool clear_int, u16 num_popped)
98{
99 u32 val = 0;
100 val |= qid & DB_EQ_RING_ID_MASK;
101 if (arm)
102 val |= 1 << DB_EQ_REARM_SHIFT;
103 if (clear_int)
104 val |= 1 << DB_EQ_CLR_SHIFT;
105 val |= 1 << DB_EQ_EVNT_SHIFT;
106 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000107 iowrite32(val, adapter->db + DB_EQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700108}
109
Sathya Perla8788fdc2009-07-27 22:52:03 +0000110void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700111{
112 u32 val = 0;
113 val |= qid & DB_CQ_RING_ID_MASK;
114 if (arm)
115 val |= 1 << DB_CQ_REARM_SHIFT;
116 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000117 iowrite32(val, adapter->db + DB_CQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700118}
119
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700120static int be_mac_addr_set(struct net_device *netdev, void *p)
121{
122 struct be_adapter *adapter = netdev_priv(netdev);
123 struct sockaddr *addr = p;
124 int status = 0;
125
Sathya Perlaa65027e2009-08-17 00:58:04 +0000126 status = be_cmd_pmac_del(adapter, adapter->if_handle, adapter->pmac_id);
127 if (status)
128 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700129
Sathya Perlaa65027e2009-08-17 00:58:04 +0000130 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
131 adapter->if_handle, &adapter->pmac_id);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700132 if (!status)
133 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
134
135 return status;
136}
137
Sathya Perlab31c50a2009-09-17 10:30:13 -0700138void netdev_stats_update(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700139{
140 struct be_hw_stats *hw_stats = hw_stats_from_cmd(adapter->stats.cmd.va);
141 struct be_rxf_stats *rxf_stats = &hw_stats->rxf;
142 struct be_port_rxf_stats *port_stats =
143 &rxf_stats->port[adapter->port_num];
144 struct net_device_stats *dev_stats = &adapter->stats.net_stats;
Sathya Perla68110862009-06-10 02:21:16 +0000145 struct be_erx_stats *erx_stats = &hw_stats->erx;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700146
147 dev_stats->rx_packets = port_stats->rx_total_frames;
148 dev_stats->tx_packets = port_stats->tx_unicastframes +
149 port_stats->tx_multicastframes + port_stats->tx_broadcastframes;
150 dev_stats->rx_bytes = (u64) port_stats->rx_bytes_msd << 32 |
151 (u64) port_stats->rx_bytes_lsd;
152 dev_stats->tx_bytes = (u64) port_stats->tx_bytes_msd << 32 |
153 (u64) port_stats->tx_bytes_lsd;
154
155 /* bad pkts received */
156 dev_stats->rx_errors = port_stats->rx_crc_errors +
157 port_stats->rx_alignment_symbol_errors +
158 port_stats->rx_in_range_errors +
Sathya Perla68110862009-06-10 02:21:16 +0000159 port_stats->rx_out_range_errors +
160 port_stats->rx_frame_too_long +
161 port_stats->rx_dropped_too_small +
162 port_stats->rx_dropped_too_short +
163 port_stats->rx_dropped_header_too_small +
164 port_stats->rx_dropped_tcp_length +
165 port_stats->rx_dropped_runt +
166 port_stats->rx_tcp_checksum_errs +
167 port_stats->rx_ip_checksum_errs +
168 port_stats->rx_udp_checksum_errs;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700169
Sathya Perla68110862009-06-10 02:21:16 +0000170 /* no space in linux buffers: best possible approximation */
171 dev_stats->rx_dropped = erx_stats->rx_drops_no_fragments[0];
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700172
173 /* detailed rx errors */
174 dev_stats->rx_length_errors = port_stats->rx_in_range_errors +
Sathya Perla68110862009-06-10 02:21:16 +0000175 port_stats->rx_out_range_errors +
176 port_stats->rx_frame_too_long;
177
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700178 /* receive ring buffer overflow */
179 dev_stats->rx_over_errors = 0;
Sathya Perla68110862009-06-10 02:21:16 +0000180
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700181 dev_stats->rx_crc_errors = port_stats->rx_crc_errors;
182
183 /* frame alignment errors */
184 dev_stats->rx_frame_errors = port_stats->rx_alignment_symbol_errors;
Sathya Perla68110862009-06-10 02:21:16 +0000185
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700186 /* receiver fifo overrun */
187 /* drops_no_pbuf is no per i/f, it's per BE card */
188 dev_stats->rx_fifo_errors = port_stats->rx_fifo_overflow +
189 port_stats->rx_input_fifo_overflow +
190 rxf_stats->rx_drops_no_pbuf;
191 /* receiver missed packetd */
192 dev_stats->rx_missed_errors = 0;
Sathya Perla68110862009-06-10 02:21:16 +0000193
194 /* packet transmit problems */
195 dev_stats->tx_errors = 0;
196
197 /* no space available in linux */
198 dev_stats->tx_dropped = 0;
199
Ajit Khapardec5b9b922009-10-05 02:21:51 +0000200 dev_stats->multicast = port_stats->rx_multicast_frames;
Sathya Perla68110862009-06-10 02:21:16 +0000201 dev_stats->collisions = 0;
202
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700203 /* detailed tx_errors */
204 dev_stats->tx_aborted_errors = 0;
205 dev_stats->tx_carrier_errors = 0;
206 dev_stats->tx_fifo_errors = 0;
207 dev_stats->tx_heartbeat_errors = 0;
208 dev_stats->tx_window_errors = 0;
209}
210
Sathya Perla8788fdc2009-07-27 22:52:03 +0000211void be_link_status_update(struct be_adapter *adapter, bool link_up)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700212{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700213 struct net_device *netdev = adapter->netdev;
214
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700215 /* If link came up or went down */
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000216 if (adapter->link_up != link_up) {
217 if (link_up) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700218 netif_start_queue(netdev);
219 netif_carrier_on(netdev);
220 printk(KERN_INFO "%s: Link up\n", netdev->name);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000221 } else {
222 netif_stop_queue(netdev);
223 netif_carrier_off(netdev);
224 printk(KERN_INFO "%s: Link down\n", netdev->name);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700225 }
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000226 adapter->link_up = link_up;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700227 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700228}
229
230/* Update the EQ delay n BE based on the RX frags consumed / sec */
231static void be_rx_eqd_update(struct be_adapter *adapter)
232{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700233 struct be_eq_obj *rx_eq = &adapter->rx_eq;
234 struct be_drvr_stats *stats = &adapter->stats.drvr_stats;
Sathya Perla4097f662009-03-24 16:40:13 -0700235 ulong now = jiffies;
236 u32 eqd;
237
238 if (!rx_eq->enable_aic)
239 return;
240
241 /* Wrapped around */
242 if (time_before(now, stats->rx_fps_jiffies)) {
243 stats->rx_fps_jiffies = now;
244 return;
245 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700246
247 /* Update once a second */
Sathya Perla4097f662009-03-24 16:40:13 -0700248 if ((now - stats->rx_fps_jiffies) < HZ)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700249 return;
250
251 stats->be_rx_fps = (stats->be_rx_frags - stats->be_prev_rx_frags) /
Sathya Perla4097f662009-03-24 16:40:13 -0700252 ((now - stats->rx_fps_jiffies) / HZ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700253
Sathya Perla4097f662009-03-24 16:40:13 -0700254 stats->rx_fps_jiffies = now;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700255 stats->be_prev_rx_frags = stats->be_rx_frags;
256 eqd = stats->be_rx_fps / 110000;
257 eqd = eqd << 3;
258 if (eqd > rx_eq->max_eqd)
259 eqd = rx_eq->max_eqd;
260 if (eqd < rx_eq->min_eqd)
261 eqd = rx_eq->min_eqd;
262 if (eqd < 10)
263 eqd = 0;
264 if (eqd != rx_eq->cur_eqd)
Sathya Perla8788fdc2009-07-27 22:52:03 +0000265 be_cmd_modify_eqd(adapter, rx_eq->q.id, eqd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700266
267 rx_eq->cur_eqd = eqd;
268}
269
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700270static struct net_device_stats *be_get_stats(struct net_device *dev)
271{
272 struct be_adapter *adapter = netdev_priv(dev);
273
274 return &adapter->stats.net_stats;
275}
276
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700277static u32 be_calc_rate(u64 bytes, unsigned long ticks)
278{
279 u64 rate = bytes;
280
281 do_div(rate, ticks / HZ);
282 rate <<= 3; /* bytes/sec -> bits/sec */
283 do_div(rate, 1000000ul); /* MB/Sec */
284
285 return rate;
286}
287
Sathya Perla4097f662009-03-24 16:40:13 -0700288static void be_tx_rate_update(struct be_adapter *adapter)
289{
290 struct be_drvr_stats *stats = drvr_stats(adapter);
291 ulong now = jiffies;
292
293 /* Wrapped around? */
294 if (time_before(now, stats->be_tx_jiffies)) {
295 stats->be_tx_jiffies = now;
296 return;
297 }
298
299 /* Update tx rate once in two seconds */
300 if ((now - stats->be_tx_jiffies) > 2 * HZ) {
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700301 stats->be_tx_rate = be_calc_rate(stats->be_tx_bytes
302 - stats->be_tx_bytes_prev,
303 now - stats->be_tx_jiffies);
Sathya Perla4097f662009-03-24 16:40:13 -0700304 stats->be_tx_jiffies = now;
305 stats->be_tx_bytes_prev = stats->be_tx_bytes;
306 }
307}
308
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700309static void be_tx_stats_update(struct be_adapter *adapter,
310 u32 wrb_cnt, u32 copied, bool stopped)
311{
Sathya Perla4097f662009-03-24 16:40:13 -0700312 struct be_drvr_stats *stats = drvr_stats(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700313 stats->be_tx_reqs++;
314 stats->be_tx_wrbs += wrb_cnt;
315 stats->be_tx_bytes += copied;
316 if (stopped)
317 stats->be_tx_stops++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700318}
319
320/* Determine number of WRB entries needed to xmit data in an skb */
321static u32 wrb_cnt_for_skb(struct sk_buff *skb, bool *dummy)
322{
David S. Millerebc8d2a2009-06-09 01:01:31 -0700323 int cnt = (skb->len > skb->data_len);
324
325 cnt += skb_shinfo(skb)->nr_frags;
326
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700327 /* to account for hdr wrb */
328 cnt++;
329 if (cnt & 1) {
330 /* add a dummy to make it an even num */
331 cnt++;
332 *dummy = true;
333 } else
334 *dummy = false;
335 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
336 return cnt;
337}
338
339static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
340{
341 wrb->frag_pa_hi = upper_32_bits(addr);
342 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
343 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
344}
345
346static void wrb_fill_hdr(struct be_eth_hdr_wrb *hdr, struct sk_buff *skb,
347 bool vlan, u32 wrb_cnt, u32 len)
348{
349 memset(hdr, 0, sizeof(*hdr));
350
351 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
352
353 if (skb_shinfo(skb)->gso_segs > 1 && skb_shinfo(skb)->gso_size) {
354 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
355 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
356 hdr, skb_shinfo(skb)->gso_size);
357 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
358 if (is_tcp_pkt(skb))
359 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
360 else if (is_udp_pkt(skb))
361 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
362 }
363
364 if (vlan && vlan_tx_tag_present(skb)) {
365 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
366 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag,
367 hdr, vlan_tx_tag_get(skb));
368 }
369
370 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
371 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
372 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
373 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
374}
375
376
377static int make_tx_wrbs(struct be_adapter *adapter,
378 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
379{
380 u64 busaddr;
381 u32 i, copied = 0;
382 struct pci_dev *pdev = adapter->pdev;
383 struct sk_buff *first_skb = skb;
384 struct be_queue_info *txq = &adapter->tx_obj.q;
385 struct be_eth_wrb *wrb;
386 struct be_eth_hdr_wrb *hdr;
387
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700388 hdr = queue_head_node(txq);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000389 atomic_add(wrb_cnt, &txq->used);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700390 queue_head_inc(txq);
391
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000392 if (skb_dma_map(&pdev->dev, skb, DMA_TO_DEVICE)) {
393 dev_err(&pdev->dev, "TX DMA mapping failed\n");
394 return 0;
395 }
396
David S. Millerebc8d2a2009-06-09 01:01:31 -0700397 if (skb->len > skb->data_len) {
398 int len = skb->len - skb->data_len;
David S. Millerebc8d2a2009-06-09 01:01:31 -0700399 wrb = queue_head_node(txq);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000400 busaddr = skb_shinfo(skb)->dma_head;
David S. Millerebc8d2a2009-06-09 01:01:31 -0700401 wrb_fill(wrb, busaddr, len);
402 be_dws_cpu_to_le(wrb, sizeof(*wrb));
403 queue_head_inc(txq);
404 copied += len;
405 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700406
David S. Millerebc8d2a2009-06-09 01:01:31 -0700407 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
408 struct skb_frag_struct *frag =
409 &skb_shinfo(skb)->frags[i];
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000410
411 busaddr = skb_shinfo(skb)->dma_maps[i];
David S. Millerebc8d2a2009-06-09 01:01:31 -0700412 wrb = queue_head_node(txq);
413 wrb_fill(wrb, busaddr, frag->size);
414 be_dws_cpu_to_le(wrb, sizeof(*wrb));
415 queue_head_inc(txq);
416 copied += frag->size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700417 }
418
419 if (dummy_wrb) {
420 wrb = queue_head_node(txq);
421 wrb_fill(wrb, 0, 0);
422 be_dws_cpu_to_le(wrb, sizeof(*wrb));
423 queue_head_inc(txq);
424 }
425
426 wrb_fill_hdr(hdr, first_skb, adapter->vlan_grp ? true : false,
427 wrb_cnt, copied);
428 be_dws_cpu_to_le(hdr, sizeof(*hdr));
429
430 return copied;
431}
432
Stephen Hemminger613573252009-08-31 19:50:58 +0000433static netdev_tx_t be_xmit(struct sk_buff *skb,
Sathya Perlab31c50a2009-09-17 10:30:13 -0700434 struct net_device *netdev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700435{
436 struct be_adapter *adapter = netdev_priv(netdev);
437 struct be_tx_obj *tx_obj = &adapter->tx_obj;
438 struct be_queue_info *txq = &tx_obj->q;
439 u32 wrb_cnt = 0, copied = 0;
440 u32 start = txq->head;
441 bool dummy_wrb, stopped = false;
442
443 wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb);
444
445 copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000446 if (copied) {
447 /* record the sent skb in the sent_skb table */
448 BUG_ON(tx_obj->sent_skb_list[start]);
449 tx_obj->sent_skb_list[start] = skb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700450
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000451 /* Ensure txq has space for the next skb; Else stop the queue
452 * *BEFORE* ringing the tx doorbell, so that we serialze the
453 * tx compls of the current transmit which'll wake up the queue
454 */
455 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
456 txq->len) {
457 netif_stop_queue(netdev);
458 stopped = true;
459 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700460
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000461 be_txq_notify(adapter, txq->id, wrb_cnt);
462
463 be_tx_stats_update(adapter, wrb_cnt, copied, stopped);
464 } else {
465 txq->head = start;
466 dev_kfree_skb_any(skb);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700467 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700468 return NETDEV_TX_OK;
469}
470
471static int be_change_mtu(struct net_device *netdev, int new_mtu)
472{
473 struct be_adapter *adapter = netdev_priv(netdev);
474 if (new_mtu < BE_MIN_MTU ||
475 new_mtu > BE_MAX_JUMBO_FRAME_SIZE) {
476 dev_info(&adapter->pdev->dev,
477 "MTU must be between %d and %d bytes\n",
478 BE_MIN_MTU, BE_MAX_JUMBO_FRAME_SIZE);
479 return -EINVAL;
480 }
481 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
482 netdev->mtu, new_mtu);
483 netdev->mtu = new_mtu;
484 return 0;
485}
486
487/*
488 * if there are BE_NUM_VLANS_SUPPORTED or lesser number of VLANS configured,
489 * program them in BE. If more than BE_NUM_VLANS_SUPPORTED are configured,
490 * set the BE in promiscuous VLAN mode.
491 */
Sathya Perlab31c50a2009-09-17 10:30:13 -0700492static int be_vid_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700493{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700494 u16 vtag[BE_NUM_VLANS_SUPPORTED];
495 u16 ntags = 0, i;
Sathya Perlab31c50a2009-09-17 10:30:13 -0700496 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700497
498 if (adapter->num_vlans <= BE_NUM_VLANS_SUPPORTED) {
499 /* Construct VLAN Table to give to HW */
500 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
501 if (adapter->vlan_tag[i]) {
502 vtag[ntags] = cpu_to_le16(i);
503 ntags++;
504 }
505 }
Sathya Perlab31c50a2009-09-17 10:30:13 -0700506 status = be_cmd_vlan_config(adapter, adapter->if_handle,
507 vtag, ntags, 1, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700508 } else {
Sathya Perlab31c50a2009-09-17 10:30:13 -0700509 status = be_cmd_vlan_config(adapter, adapter->if_handle,
510 NULL, 0, 1, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700511 }
Sathya Perlab31c50a2009-09-17 10:30:13 -0700512 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700513}
514
515static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
516{
517 struct be_adapter *adapter = netdev_priv(netdev);
518 struct be_eq_obj *rx_eq = &adapter->rx_eq;
519 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700520
Sathya Perla8788fdc2009-07-27 22:52:03 +0000521 be_eq_notify(adapter, rx_eq->q.id, false, false, 0);
522 be_eq_notify(adapter, tx_eq->q.id, false, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700523 adapter->vlan_grp = grp;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000524 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
525 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700526}
527
528static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
529{
530 struct be_adapter *adapter = netdev_priv(netdev);
531
532 adapter->num_vlans++;
533 adapter->vlan_tag[vid] = 1;
534
Sathya Perlab31c50a2009-09-17 10:30:13 -0700535 be_vid_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700536}
537
538static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
539{
540 struct be_adapter *adapter = netdev_priv(netdev);
541
542 adapter->num_vlans--;
543 adapter->vlan_tag[vid] = 0;
544
545 vlan_group_set_device(adapter->vlan_grp, vid, NULL);
Sathya Perlab31c50a2009-09-17 10:30:13 -0700546 be_vid_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700547}
548
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700549static void be_set_multicast_list(struct net_device *netdev)
550{
551 struct be_adapter *adapter = netdev_priv(netdev);
552
553 if (netdev->flags & IFF_PROMISC) {
Sathya Perla8788fdc2009-07-27 22:52:03 +0000554 be_cmd_promiscuous_config(adapter, adapter->port_num, 1);
Sathya Perla24307ee2009-06-18 00:09:25 +0000555 adapter->promiscuous = true;
556 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700557 }
Sathya Perla24307ee2009-06-18 00:09:25 +0000558
559 /* BE was previously in promiscous mode; disable it */
560 if (adapter->promiscuous) {
561 adapter->promiscuous = false;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000562 be_cmd_promiscuous_config(adapter, adapter->port_num, 0);
Sathya Perla24307ee2009-06-18 00:09:25 +0000563 }
564
565 if (netdev->flags & IFF_ALLMULTI) {
Sathya Perla8788fdc2009-07-27 22:52:03 +0000566 be_cmd_multicast_set(adapter, adapter->if_handle, NULL, 0);
Sathya Perla24307ee2009-06-18 00:09:25 +0000567 goto done;
568 }
569
Sathya Perla8788fdc2009-07-27 22:52:03 +0000570 be_cmd_multicast_set(adapter, adapter->if_handle, netdev->mc_list,
Sathya Perla24307ee2009-06-18 00:09:25 +0000571 netdev->mc_count);
572done:
573 return;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700574}
575
Sathya Perla4097f662009-03-24 16:40:13 -0700576static void be_rx_rate_update(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700577{
Sathya Perla4097f662009-03-24 16:40:13 -0700578 struct be_drvr_stats *stats = drvr_stats(adapter);
579 ulong now = jiffies;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700580
Sathya Perla4097f662009-03-24 16:40:13 -0700581 /* Wrapped around */
582 if (time_before(now, stats->be_rx_jiffies)) {
583 stats->be_rx_jiffies = now;
584 return;
585 }
586
587 /* Update the rate once in two seconds */
588 if ((now - stats->be_rx_jiffies) < 2 * HZ)
589 return;
590
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700591 stats->be_rx_rate = be_calc_rate(stats->be_rx_bytes
592 - stats->be_rx_bytes_prev,
593 now - stats->be_rx_jiffies);
Sathya Perla4097f662009-03-24 16:40:13 -0700594 stats->be_rx_jiffies = now;
595 stats->be_rx_bytes_prev = stats->be_rx_bytes;
596}
597
598static void be_rx_stats_update(struct be_adapter *adapter,
599 u32 pktsize, u16 numfrags)
600{
601 struct be_drvr_stats *stats = drvr_stats(adapter);
602
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700603 stats->be_rx_compl++;
604 stats->be_rx_frags += numfrags;
605 stats->be_rx_bytes += pktsize;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700606}
607
Ajit Khaparde728a9972009-04-13 15:41:22 -0700608static inline bool do_pkt_csum(struct be_eth_rx_compl *rxcp, bool cso)
609{
610 u8 l4_cksm, ip_version, ipcksm, tcpf = 0, udpf = 0, ipv6_chk;
611
612 l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp);
613 ipcksm = AMAP_GET_BITS(struct amap_eth_rx_compl, ipcksm, rxcp);
614 ip_version = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
615 if (ip_version) {
616 tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
617 udpf = AMAP_GET_BITS(struct amap_eth_rx_compl, udpf, rxcp);
618 }
619 ipv6_chk = (ip_version && (tcpf || udpf));
620
621 return ((l4_cksm && ipv6_chk && ipcksm) && cso) ? false : true;
622}
623
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700624static struct be_rx_page_info *
625get_rx_page_info(struct be_adapter *adapter, u16 frag_idx)
626{
627 struct be_rx_page_info *rx_page_info;
628 struct be_queue_info *rxq = &adapter->rx_obj.q;
629
630 rx_page_info = &adapter->rx_obj.page_info_tbl[frag_idx];
631 BUG_ON(!rx_page_info->page);
632
633 if (rx_page_info->last_page_user)
634 pci_unmap_page(adapter->pdev, pci_unmap_addr(rx_page_info, bus),
635 adapter->big_page_size, PCI_DMA_FROMDEVICE);
636
637 atomic_dec(&rxq->used);
638 return rx_page_info;
639}
640
641/* Throwaway the data in the Rx completion */
642static void be_rx_compl_discard(struct be_adapter *adapter,
643 struct be_eth_rx_compl *rxcp)
644{
645 struct be_queue_info *rxq = &adapter->rx_obj.q;
646 struct be_rx_page_info *page_info;
647 u16 rxq_idx, i, num_rcvd;
648
649 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
650 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
651
652 for (i = 0; i < num_rcvd; i++) {
653 page_info = get_rx_page_info(adapter, rxq_idx);
654 put_page(page_info->page);
655 memset(page_info, 0, sizeof(*page_info));
656 index_inc(&rxq_idx, rxq->len);
657 }
658}
659
660/*
661 * skb_fill_rx_data forms a complete skb for an ether frame
662 * indicated by rxcp.
663 */
664static void skb_fill_rx_data(struct be_adapter *adapter,
665 struct sk_buff *skb, struct be_eth_rx_compl *rxcp)
666{
667 struct be_queue_info *rxq = &adapter->rx_obj.q;
668 struct be_rx_page_info *page_info;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000669 u16 rxq_idx, i, num_rcvd, j;
Ajit Khapardefa774062009-07-22 09:28:55 -0700670 u32 pktsize, hdr_len, curr_frag_len, size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700671 u8 *start;
672
673 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
674 pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
675 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
676
677 page_info = get_rx_page_info(adapter, rxq_idx);
678
679 start = page_address(page_info->page) + page_info->page_offset;
680 prefetch(start);
681
682 /* Copy data in the first descriptor of this completion */
683 curr_frag_len = min(pktsize, rx_frag_size);
684
685 /* Copy the header portion into skb_data */
686 hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
687 memcpy(skb->data, start, hdr_len);
688 skb->len = curr_frag_len;
689 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
690 /* Complete packet has now been moved to data */
691 put_page(page_info->page);
692 skb->data_len = 0;
693 skb->tail += curr_frag_len;
694 } else {
695 skb_shinfo(skb)->nr_frags = 1;
696 skb_shinfo(skb)->frags[0].page = page_info->page;
697 skb_shinfo(skb)->frags[0].page_offset =
698 page_info->page_offset + hdr_len;
699 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
700 skb->data_len = curr_frag_len - hdr_len;
701 skb->tail += hdr_len;
702 }
703 memset(page_info, 0, sizeof(*page_info));
704
705 if (pktsize <= rx_frag_size) {
706 BUG_ON(num_rcvd != 1);
Sathya Perla76fbb422009-06-10 02:21:56 +0000707 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700708 }
709
710 /* More frags present for this completion */
Ajit Khapardefa774062009-07-22 09:28:55 -0700711 size = pktsize;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000712 for (i = 1, j = 0; i < num_rcvd; i++) {
Ajit Khapardefa774062009-07-22 09:28:55 -0700713 size -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700714 index_inc(&rxq_idx, rxq->len);
715 page_info = get_rx_page_info(adapter, rxq_idx);
716
Ajit Khapardefa774062009-07-22 09:28:55 -0700717 curr_frag_len = min(size, rx_frag_size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700718
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000719 /* Coalesce all frags from the same physical page in one slot */
720 if (page_info->page_offset == 0) {
721 /* Fresh page */
722 j++;
723 skb_shinfo(skb)->frags[j].page = page_info->page;
724 skb_shinfo(skb)->frags[j].page_offset =
725 page_info->page_offset;
726 skb_shinfo(skb)->frags[j].size = 0;
727 skb_shinfo(skb)->nr_frags++;
728 } else {
729 put_page(page_info->page);
730 }
731
732 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700733 skb->len += curr_frag_len;
734 skb->data_len += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700735
736 memset(page_info, 0, sizeof(*page_info));
737 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000738 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700739
Sathya Perla76fbb422009-06-10 02:21:56 +0000740done:
Sathya Perla4097f662009-03-24 16:40:13 -0700741 be_rx_stats_update(adapter, pktsize, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700742 return;
743}
744
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700745/* Process the RX completion indicated by rxcp when GRO is disabled */
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700746static void be_rx_compl_process(struct be_adapter *adapter,
747 struct be_eth_rx_compl *rxcp)
748{
749 struct sk_buff *skb;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700750 u32 vlanf, vid;
751 u8 vtm;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700752
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700753 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
754 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
755
756 /* vlanf could be wrongly set in some cards.
757 * ignore if vtm is not set */
758 if ((adapter->cap == 0x400) && !vtm)
759 vlanf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700760
761 skb = netdev_alloc_skb(adapter->netdev, BE_HDR_LEN + NET_IP_ALIGN);
762 if (!skb) {
763 if (net_ratelimit())
764 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
765 be_rx_compl_discard(adapter, rxcp);
766 return;
767 }
768
769 skb_reserve(skb, NET_IP_ALIGN);
770
771 skb_fill_rx_data(adapter, skb, rxcp);
772
Ajit Khaparde728a9972009-04-13 15:41:22 -0700773 if (do_pkt_csum(rxcp, adapter->rx_csum))
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700774 skb->ip_summed = CHECKSUM_NONE;
Ajit Khaparde728a9972009-04-13 15:41:22 -0700775 else
776 skb->ip_summed = CHECKSUM_UNNECESSARY;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700777
778 skb->truesize = skb->len + sizeof(struct sk_buff);
779 skb->protocol = eth_type_trans(skb, adapter->netdev);
780 skb->dev = adapter->netdev;
781
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700782 if (vlanf) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700783 if (!adapter->vlan_grp || adapter->num_vlans == 0) {
784 kfree_skb(skb);
785 return;
786 }
787 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
788 vid = be16_to_cpu(vid);
789 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
790 } else {
791 netif_receive_skb(skb);
792 }
793
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700794 return;
795}
796
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700797/* Process the RX completion indicated by rxcp when GRO is enabled */
798static void be_rx_compl_process_gro(struct be_adapter *adapter,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700799 struct be_eth_rx_compl *rxcp)
800{
801 struct be_rx_page_info *page_info;
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700802 struct sk_buff *skb = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700803 struct be_queue_info *rxq = &adapter->rx_obj.q;
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700804 struct be_eq_obj *eq_obj = &adapter->rx_eq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700805 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000806 u16 i, rxq_idx = 0, vid, j;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700807 u8 vtm;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700808
809 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
810 pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
811 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
812 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700813 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
814
815 /* vlanf could be wrongly set in some cards.
816 * ignore if vtm is not set */
817 if ((adapter->cap == 0x400) && !vtm)
818 vlanf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700819
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700820 skb = napi_get_frags(&eq_obj->napi);
821 if (!skb) {
822 be_rx_compl_discard(adapter, rxcp);
823 return;
824 }
825
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700826 remaining = pkt_size;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000827 for (i = 0, j = -1; i < num_rcvd; i++) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700828 page_info = get_rx_page_info(adapter, rxq_idx);
829
830 curr_frag_len = min(remaining, rx_frag_size);
831
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000832 /* Coalesce all frags from the same physical page in one slot */
833 if (i == 0 || page_info->page_offset == 0) {
834 /* First frag or Fresh page */
835 j++;
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700836 skb_shinfo(skb)->frags[j].page = page_info->page;
837 skb_shinfo(skb)->frags[j].page_offset =
838 page_info->page_offset;
839 skb_shinfo(skb)->frags[j].size = 0;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000840 } else {
841 put_page(page_info->page);
842 }
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700843 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000844
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700845 remaining -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700846 index_inc(&rxq_idx, rxq->len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700847 memset(page_info, 0, sizeof(*page_info));
848 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000849 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700850
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700851 skb_shinfo(skb)->nr_frags = j + 1;
852 skb->len = pkt_size;
853 skb->data_len = pkt_size;
854 skb->truesize += pkt_size;
855 skb->ip_summed = CHECKSUM_UNNECESSARY;
856
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700857 if (likely(!vlanf)) {
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700858 napi_gro_frags(&eq_obj->napi);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700859 } else {
860 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
861 vid = be16_to_cpu(vid);
862
863 if (!adapter->vlan_grp || adapter->num_vlans == 0)
864 return;
865
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700866 vlan_gro_frags(&eq_obj->napi, adapter->vlan_grp, vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700867 }
868
Sathya Perla4097f662009-03-24 16:40:13 -0700869 be_rx_stats_update(adapter, pkt_size, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700870 return;
871}
872
873static struct be_eth_rx_compl *be_rx_compl_get(struct be_adapter *adapter)
874{
875 struct be_eth_rx_compl *rxcp = queue_tail_node(&adapter->rx_obj.cq);
876
877 if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
878 return NULL;
879
880 be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
881
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700882 queue_tail_inc(&adapter->rx_obj.cq);
883 return rxcp;
884}
885
Sathya Perlaa7a0ef32009-06-10 02:23:28 +0000886/* To reset the valid bit, we need to reset the whole word as
887 * when walking the queue the valid entries are little-endian
888 * and invalid entries are host endian
889 */
890static inline void be_rx_compl_reset(struct be_eth_rx_compl *rxcp)
891{
892 rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
893}
894
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700895static inline struct page *be_alloc_pages(u32 size)
896{
897 gfp_t alloc_flags = GFP_ATOMIC;
898 u32 order = get_order(size);
899 if (order > 0)
900 alloc_flags |= __GFP_COMP;
901 return alloc_pages(alloc_flags, order);
902}
903
904/*
905 * Allocate a page, split it to fragments of size rx_frag_size and post as
906 * receive buffers to BE
907 */
908static void be_post_rx_frags(struct be_adapter *adapter)
909{
910 struct be_rx_page_info *page_info_tbl = adapter->rx_obj.page_info_tbl;
911 struct be_rx_page_info *page_info = NULL;
912 struct be_queue_info *rxq = &adapter->rx_obj.q;
913 struct page *pagep = NULL;
914 struct be_eth_rx_d *rxd;
915 u64 page_dmaaddr = 0, frag_dmaaddr;
916 u32 posted, page_offset = 0;
917
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700918 page_info = &page_info_tbl[rxq->head];
919 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
920 if (!pagep) {
921 pagep = be_alloc_pages(adapter->big_page_size);
922 if (unlikely(!pagep)) {
923 drvr_stats(adapter)->be_ethrx_post_fail++;
924 break;
925 }
926 page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
927 adapter->big_page_size,
928 PCI_DMA_FROMDEVICE);
929 page_info->page_offset = 0;
930 } else {
931 get_page(pagep);
932 page_info->page_offset = page_offset + rx_frag_size;
933 }
934 page_offset = page_info->page_offset;
935 page_info->page = pagep;
936 pci_unmap_addr_set(page_info, bus, page_dmaaddr);
937 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
938
939 rxd = queue_head_node(rxq);
940 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
941 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
942 queue_head_inc(rxq);
943
944 /* Any space left in the current big page for another frag? */
945 if ((page_offset + rx_frag_size + rx_frag_size) >
946 adapter->big_page_size) {
947 pagep = NULL;
948 page_info->last_page_user = true;
949 }
950 page_info = &page_info_tbl[rxq->head];
951 }
952 if (pagep)
953 page_info->last_page_user = true;
954
955 if (posted) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700956 atomic_add(posted, &rxq->used);
Sathya Perla8788fdc2009-07-27 22:52:03 +0000957 be_rxq_notify(adapter, rxq->id, posted);
Sathya Perlaea1dae12009-03-19 23:56:20 -0700958 } else if (atomic_read(&rxq->used) == 0) {
959 /* Let be_worker replenish when memory is available */
960 adapter->rx_post_starved = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700961 }
962
963 return;
964}
965
Sathya Perla5fb379e2009-06-18 00:02:59 +0000966static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700967{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700968 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
969
970 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
971 return NULL;
972
973 be_dws_le_to_cpu(txcp, sizeof(*txcp));
974
975 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
976
977 queue_tail_inc(tx_cq);
978 return txcp;
979}
980
981static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
982{
983 struct be_queue_info *txq = &adapter->tx_obj.q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700984 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
985 struct sk_buff *sent_skb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700986 u16 cur_index, num_wrbs = 0;
987
988 cur_index = txq->tail;
989 sent_skb = sent_skbs[cur_index];
990 BUG_ON(!sent_skb);
991 sent_skbs[cur_index] = NULL;
992
993 do {
994 cur_index = txq->tail;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700995 num_wrbs++;
996 queue_tail_inc(txq);
997 } while (cur_index != last_index);
998
999 atomic_sub(num_wrbs, &txq->used);
Ajit Khapardec190e3c2009-09-04 03:12:29 +00001000 skb_dma_unmap(&adapter->pdev->dev, sent_skb, DMA_TO_DEVICE);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001001 kfree_skb(sent_skb);
1002}
1003
Sathya Perla859b1e42009-08-10 03:43:51 +00001004static inline struct be_eq_entry *event_get(struct be_eq_obj *eq_obj)
1005{
1006 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1007
1008 if (!eqe->evt)
1009 return NULL;
1010
1011 eqe->evt = le32_to_cpu(eqe->evt);
1012 queue_tail_inc(&eq_obj->q);
1013 return eqe;
1014}
1015
1016static int event_handle(struct be_adapter *adapter,
1017 struct be_eq_obj *eq_obj)
1018{
1019 struct be_eq_entry *eqe;
1020 u16 num = 0;
1021
1022 while ((eqe = event_get(eq_obj)) != NULL) {
1023 eqe->evt = 0;
1024 num++;
1025 }
1026
1027 /* Deal with any spurious interrupts that come
1028 * without events
1029 */
1030 be_eq_notify(adapter, eq_obj->q.id, true, true, num);
1031 if (num)
1032 napi_schedule(&eq_obj->napi);
1033
1034 return num;
1035}
1036
1037/* Just read and notify events without processing them.
1038 * Used at the time of destroying event queues */
1039static void be_eq_clean(struct be_adapter *adapter,
1040 struct be_eq_obj *eq_obj)
1041{
1042 struct be_eq_entry *eqe;
1043 u16 num = 0;
1044
1045 while ((eqe = event_get(eq_obj)) != NULL) {
1046 eqe->evt = 0;
1047 num++;
1048 }
1049
1050 if (num)
1051 be_eq_notify(adapter, eq_obj->q.id, false, true, num);
1052}
1053
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001054static void be_rx_q_clean(struct be_adapter *adapter)
1055{
1056 struct be_rx_page_info *page_info;
1057 struct be_queue_info *rxq = &adapter->rx_obj.q;
1058 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1059 struct be_eth_rx_compl *rxcp;
1060 u16 tail;
1061
1062 /* First cleanup pending rx completions */
1063 while ((rxcp = be_rx_compl_get(adapter)) != NULL) {
1064 be_rx_compl_discard(adapter, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001065 be_rx_compl_reset(rxcp);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001066 be_cq_notify(adapter, rx_cq->id, true, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001067 }
1068
1069 /* Then free posted rx buffer that were not used */
1070 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
Sathya Perlacdab23b2009-08-10 03:43:23 +00001071 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001072 page_info = get_rx_page_info(adapter, tail);
1073 put_page(page_info->page);
1074 memset(page_info, 0, sizeof(*page_info));
1075 }
1076 BUG_ON(atomic_read(&rxq->used));
1077}
1078
Sathya Perlaa8e91792009-08-10 03:42:43 +00001079static void be_tx_compl_clean(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001080{
Sathya Perlaa8e91792009-08-10 03:42:43 +00001081 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001082 struct be_queue_info *txq = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001083 struct be_eth_tx_compl *txcp;
1084 u16 end_idx, cmpl = 0, timeo = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001085
Sathya Perlaa8e91792009-08-10 03:42:43 +00001086 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1087 do {
1088 while ((txcp = be_tx_compl_get(tx_cq))) {
1089 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1090 wrb_index, txcp);
1091 be_tx_compl_process(adapter, end_idx);
1092 cmpl++;
1093 }
1094 if (cmpl) {
1095 be_cq_notify(adapter, tx_cq->id, false, cmpl);
1096 cmpl = 0;
1097 }
1098
1099 if (atomic_read(&txq->used) == 0 || ++timeo > 200)
1100 break;
1101
1102 mdelay(1);
1103 } while (true);
1104
1105 if (atomic_read(&txq->used))
1106 dev_err(&adapter->pdev->dev, "%d pending tx-completions\n",
1107 atomic_read(&txq->used));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001108}
1109
Sathya Perla5fb379e2009-06-18 00:02:59 +00001110static void be_mcc_queues_destroy(struct be_adapter *adapter)
1111{
1112 struct be_queue_info *q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001113
Sathya Perla8788fdc2009-07-27 22:52:03 +00001114 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001115 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001116 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001117 be_queue_free(adapter, q);
1118
Sathya Perla8788fdc2009-07-27 22:52:03 +00001119 q = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001120 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001121 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001122 be_queue_free(adapter, q);
1123}
1124
1125/* Must be called only after TX qs are created as MCC shares TX EQ */
1126static int be_mcc_queues_create(struct be_adapter *adapter)
1127{
1128 struct be_queue_info *q, *cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001129
1130 /* Alloc MCC compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001131 cq = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001132 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
Sathya Perlaefd2e402009-07-27 22:53:10 +00001133 sizeof(struct be_mcc_compl)))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001134 goto err;
1135
1136 /* Ask BE to create MCC compl queue; share TX's eq */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001137 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001138 goto mcc_cq_free;
1139
1140 /* Alloc MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001141 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001142 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1143 goto mcc_cq_destroy;
1144
1145 /* Ask BE to create MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001146 if (be_cmd_mccq_create(adapter, q, cq))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001147 goto mcc_q_free;
1148
1149 return 0;
1150
1151mcc_q_free:
1152 be_queue_free(adapter, q);
1153mcc_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001154 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001155mcc_cq_free:
1156 be_queue_free(adapter, cq);
1157err:
1158 return -1;
1159}
1160
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001161static void be_tx_queues_destroy(struct be_adapter *adapter)
1162{
1163 struct be_queue_info *q;
1164
1165 q = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001166 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001167 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001168 be_queue_free(adapter, q);
1169
1170 q = &adapter->tx_obj.cq;
1171 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001172 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001173 be_queue_free(adapter, q);
1174
Sathya Perla859b1e42009-08-10 03:43:51 +00001175 /* Clear any residual events */
1176 be_eq_clean(adapter, &adapter->tx_eq);
1177
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001178 q = &adapter->tx_eq.q;
1179 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001180 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001181 be_queue_free(adapter, q);
1182}
1183
1184static int be_tx_queues_create(struct be_adapter *adapter)
1185{
1186 struct be_queue_info *eq, *q, *cq;
1187
1188 adapter->tx_eq.max_eqd = 0;
1189 adapter->tx_eq.min_eqd = 0;
1190 adapter->tx_eq.cur_eqd = 96;
1191 adapter->tx_eq.enable_aic = false;
1192 /* Alloc Tx Event queue */
1193 eq = &adapter->tx_eq.q;
1194 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1195 return -1;
1196
1197 /* Ask BE to create Tx Event queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001198 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001199 goto tx_eq_free;
1200 /* Alloc TX eth compl queue */
1201 cq = &adapter->tx_obj.cq;
1202 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1203 sizeof(struct be_eth_tx_compl)))
1204 goto tx_eq_destroy;
1205
1206 /* Ask BE to create Tx eth compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001207 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001208 goto tx_cq_free;
1209
1210 /* Alloc TX eth queue */
1211 q = &adapter->tx_obj.q;
1212 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1213 goto tx_cq_destroy;
1214
1215 /* Ask BE to create Tx eth queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001216 if (be_cmd_txq_create(adapter, q, cq))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001217 goto tx_q_free;
1218 return 0;
1219
1220tx_q_free:
1221 be_queue_free(adapter, q);
1222tx_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001223 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001224tx_cq_free:
1225 be_queue_free(adapter, cq);
1226tx_eq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001227 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001228tx_eq_free:
1229 be_queue_free(adapter, eq);
1230 return -1;
1231}
1232
1233static void be_rx_queues_destroy(struct be_adapter *adapter)
1234{
1235 struct be_queue_info *q;
1236
1237 q = &adapter->rx_obj.q;
1238 if (q->created) {
Sathya Perla8788fdc2009-07-27 22:52:03 +00001239 be_cmd_q_destroy(adapter, q, QTYPE_RXQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001240 be_rx_q_clean(adapter);
1241 }
1242 be_queue_free(adapter, q);
1243
1244 q = &adapter->rx_obj.cq;
1245 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001246 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001247 be_queue_free(adapter, q);
1248
Sathya Perla859b1e42009-08-10 03:43:51 +00001249 /* Clear any residual events */
1250 be_eq_clean(adapter, &adapter->rx_eq);
1251
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001252 q = &adapter->rx_eq.q;
1253 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001254 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001255 be_queue_free(adapter, q);
1256}
1257
1258static int be_rx_queues_create(struct be_adapter *adapter)
1259{
1260 struct be_queue_info *eq, *q, *cq;
1261 int rc;
1262
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001263 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
1264 adapter->rx_eq.max_eqd = BE_MAX_EQD;
1265 adapter->rx_eq.min_eqd = 0;
1266 adapter->rx_eq.cur_eqd = 0;
1267 adapter->rx_eq.enable_aic = true;
1268
1269 /* Alloc Rx Event queue */
1270 eq = &adapter->rx_eq.q;
1271 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1272 sizeof(struct be_eq_entry));
1273 if (rc)
1274 return rc;
1275
1276 /* Ask BE to create Rx Event queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001277 rc = be_cmd_eq_create(adapter, eq, adapter->rx_eq.cur_eqd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001278 if (rc)
1279 goto rx_eq_free;
1280
1281 /* Alloc RX eth compl queue */
1282 cq = &adapter->rx_obj.cq;
1283 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1284 sizeof(struct be_eth_rx_compl));
1285 if (rc)
1286 goto rx_eq_destroy;
1287
1288 /* Ask BE to create Rx eth compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001289 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001290 if (rc)
1291 goto rx_cq_free;
1292
1293 /* Alloc RX eth queue */
1294 q = &adapter->rx_obj.q;
1295 rc = be_queue_alloc(adapter, q, RX_Q_LEN, sizeof(struct be_eth_rx_d));
1296 if (rc)
1297 goto rx_cq_destroy;
1298
1299 /* Ask BE to create Rx eth queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001300 rc = be_cmd_rxq_create(adapter, q, cq->id, rx_frag_size,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001301 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle, false);
1302 if (rc)
1303 goto rx_q_free;
1304
1305 return 0;
1306rx_q_free:
1307 be_queue_free(adapter, q);
1308rx_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001309 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001310rx_cq_free:
1311 be_queue_free(adapter, cq);
1312rx_eq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001313 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001314rx_eq_free:
1315 be_queue_free(adapter, eq);
1316 return rc;
1317}
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001318
Sathya Perlab628bde2009-08-17 00:58:26 +00001319/* There are 8 evt ids per func. Retruns the evt id's bit number */
1320static inline int be_evt_bit_get(struct be_adapter *adapter, u32 eq_id)
1321{
1322 return eq_id - 8 * be_pci_func(adapter);
1323}
1324
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001325static irqreturn_t be_intx(int irq, void *dev)
1326{
1327 struct be_adapter *adapter = dev;
Sathya Perla8788fdc2009-07-27 22:52:03 +00001328 int isr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001329
Sathya Perla8788fdc2009-07-27 22:52:03 +00001330 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
Sathya Perlaeec368f2009-07-27 22:52:23 +00001331 be_pci_func(adapter) * CEV_ISR_SIZE);
Sathya Perlac001c212009-07-01 01:06:07 +00001332 if (!isr)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001333 return IRQ_NONE;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001334
Sathya Perla8788fdc2009-07-27 22:52:03 +00001335 event_handle(adapter, &adapter->tx_eq);
1336 event_handle(adapter, &adapter->rx_eq);
Sathya Perlac001c212009-07-01 01:06:07 +00001337
Sathya Perla8788fdc2009-07-27 22:52:03 +00001338 return IRQ_HANDLED;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001339}
1340
1341static irqreturn_t be_msix_rx(int irq, void *dev)
1342{
1343 struct be_adapter *adapter = dev;
1344
Sathya Perla8788fdc2009-07-27 22:52:03 +00001345 event_handle(adapter, &adapter->rx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001346
1347 return IRQ_HANDLED;
1348}
1349
Sathya Perla5fb379e2009-06-18 00:02:59 +00001350static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001351{
1352 struct be_adapter *adapter = dev;
1353
Sathya Perla8788fdc2009-07-27 22:52:03 +00001354 event_handle(adapter, &adapter->tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001355
1356 return IRQ_HANDLED;
1357}
1358
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001359static inline bool do_gro(struct be_adapter *adapter,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001360 struct be_eth_rx_compl *rxcp)
1361{
1362 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1363 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1364
1365 if (err)
1366 drvr_stats(adapter)->be_rxcp_err++;
1367
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001368 return (tcp_frame && !err) ? true : false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001369}
1370
1371int be_poll_rx(struct napi_struct *napi, int budget)
1372{
1373 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
1374 struct be_adapter *adapter =
1375 container_of(rx_eq, struct be_adapter, rx_eq);
1376 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1377 struct be_eth_rx_compl *rxcp;
1378 u32 work_done;
1379
1380 for (work_done = 0; work_done < budget; work_done++) {
1381 rxcp = be_rx_compl_get(adapter);
1382 if (!rxcp)
1383 break;
1384
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001385 if (do_gro(adapter, rxcp))
1386 be_rx_compl_process_gro(adapter, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001387 else
1388 be_rx_compl_process(adapter, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001389
1390 be_rx_compl_reset(rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001391 }
1392
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001393 /* Refill the queue */
1394 if (atomic_read(&adapter->rx_obj.q.used) < RX_FRAGS_REFILL_WM)
1395 be_post_rx_frags(adapter);
1396
1397 /* All consumed */
1398 if (work_done < budget) {
1399 napi_complete(napi);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001400 be_cq_notify(adapter, rx_cq->id, true, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001401 } else {
1402 /* More to be consumed; continue with interrupts disabled */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001403 be_cq_notify(adapter, rx_cq->id, false, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001404 }
1405 return work_done;
1406}
1407
Sathya Perla5fb379e2009-06-18 00:02:59 +00001408void be_process_tx(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001409{
Sathya Perla5fb379e2009-06-18 00:02:59 +00001410 struct be_queue_info *txq = &adapter->tx_obj.q;
1411 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001412 struct be_eth_tx_compl *txcp;
1413 u32 num_cmpl = 0;
1414 u16 end_idx;
1415
Sathya Perla5fb379e2009-06-18 00:02:59 +00001416 while ((txcp = be_tx_compl_get(tx_cq))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001417 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1418 wrb_index, txcp);
1419 be_tx_compl_process(adapter, end_idx);
1420 num_cmpl++;
1421 }
1422
Sathya Perla5fb379e2009-06-18 00:02:59 +00001423 if (num_cmpl) {
Sathya Perla8788fdc2009-07-27 22:52:03 +00001424 be_cq_notify(adapter, tx_cq->id, true, num_cmpl);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001425
1426 /* As Tx wrbs have been freed up, wake up netdev queue if
1427 * it was stopped due to lack of tx wrbs.
1428 */
1429 if (netif_queue_stopped(adapter->netdev) &&
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001430 atomic_read(&txq->used) < txq->len / 2) {
Sathya Perla5fb379e2009-06-18 00:02:59 +00001431 netif_wake_queue(adapter->netdev);
1432 }
1433
1434 drvr_stats(adapter)->be_tx_events++;
1435 drvr_stats(adapter)->be_tx_compl += num_cmpl;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001436 }
Sathya Perla5fb379e2009-06-18 00:02:59 +00001437}
1438
1439/* As TX and MCC share the same EQ check for both TX and MCC completions.
1440 * For TX/MCC we don't honour budget; consume everything
1441 */
1442static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
1443{
1444 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1445 struct be_adapter *adapter =
1446 container_of(tx_eq, struct be_adapter, tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001447
1448 napi_complete(napi);
1449
Sathya Perla5fb379e2009-06-18 00:02:59 +00001450 be_process_tx(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001451
Sathya Perla8788fdc2009-07-27 22:52:03 +00001452 be_process_mcc(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001453
1454 return 1;
1455}
1456
Sathya Perlaea1dae12009-03-19 23:56:20 -07001457static void be_worker(struct work_struct *work)
1458{
1459 struct be_adapter *adapter =
1460 container_of(work, struct be_adapter, work.work);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001461
Sathya Perlab31c50a2009-09-17 10:30:13 -07001462 be_cmd_get_stats(adapter, &adapter->stats.cmd);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001463
1464 /* Set EQ delay */
1465 be_rx_eqd_update(adapter);
1466
Sathya Perla4097f662009-03-24 16:40:13 -07001467 be_tx_rate_update(adapter);
1468 be_rx_rate_update(adapter);
1469
Sathya Perlaea1dae12009-03-19 23:56:20 -07001470 if (adapter->rx_post_starved) {
1471 adapter->rx_post_starved = false;
1472 be_post_rx_frags(adapter);
1473 }
1474
1475 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1476}
1477
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001478static void be_msix_enable(struct be_adapter *adapter)
1479{
1480 int i, status;
1481
1482 for (i = 0; i < BE_NUM_MSIX_VECTORS; i++)
1483 adapter->msix_entries[i].entry = i;
1484
1485 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1486 BE_NUM_MSIX_VECTORS);
1487 if (status == 0)
1488 adapter->msix_enabled = true;
1489 return;
1490}
1491
1492static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1493{
Sathya Perlab628bde2009-08-17 00:58:26 +00001494 return adapter->msix_entries[
1495 be_evt_bit_get(adapter, eq_id)].vector;
1496}
1497
1498static int be_request_irq(struct be_adapter *adapter,
1499 struct be_eq_obj *eq_obj,
1500 void *handler, char *desc)
1501{
1502 struct net_device *netdev = adapter->netdev;
1503 int vec;
1504
1505 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
1506 vec = be_msix_vec_get(adapter, eq_obj->q.id);
1507 return request_irq(vec, handler, 0, eq_obj->desc, adapter);
1508}
1509
1510static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj)
1511{
1512 int vec = be_msix_vec_get(adapter, eq_obj->q.id);
1513 free_irq(vec, adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001514}
1515
1516static int be_msix_register(struct be_adapter *adapter)
1517{
Sathya Perlab628bde2009-08-17 00:58:26 +00001518 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001519
Sathya Perlab628bde2009-08-17 00:58:26 +00001520 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx");
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001521 if (status)
1522 goto err;
1523
Sathya Perlab628bde2009-08-17 00:58:26 +00001524 status = be_request_irq(adapter, &adapter->rx_eq, be_msix_rx, "rx");
1525 if (status)
1526 goto free_tx_irq;
1527
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001528 return 0;
Sathya Perlab628bde2009-08-17 00:58:26 +00001529
1530free_tx_irq:
1531 be_free_irq(adapter, &adapter->tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001532err:
1533 dev_warn(&adapter->pdev->dev,
1534 "MSIX Request IRQ failed - err %d\n", status);
1535 pci_disable_msix(adapter->pdev);
1536 adapter->msix_enabled = false;
1537 return status;
1538}
1539
1540static int be_irq_register(struct be_adapter *adapter)
1541{
1542 struct net_device *netdev = adapter->netdev;
1543 int status;
1544
1545 if (adapter->msix_enabled) {
1546 status = be_msix_register(adapter);
1547 if (status == 0)
1548 goto done;
1549 }
1550
1551 /* INTx */
1552 netdev->irq = adapter->pdev->irq;
1553 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1554 adapter);
1555 if (status) {
1556 dev_err(&adapter->pdev->dev,
1557 "INTx request IRQ failed - err %d\n", status);
1558 return status;
1559 }
1560done:
1561 adapter->isr_registered = true;
1562 return 0;
1563}
1564
1565static void be_irq_unregister(struct be_adapter *adapter)
1566{
1567 struct net_device *netdev = adapter->netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001568
1569 if (!adapter->isr_registered)
1570 return;
1571
1572 /* INTx */
1573 if (!adapter->msix_enabled) {
1574 free_irq(netdev->irq, adapter);
1575 goto done;
1576 }
1577
1578 /* MSIx */
Sathya Perlab628bde2009-08-17 00:58:26 +00001579 be_free_irq(adapter, &adapter->tx_eq);
1580 be_free_irq(adapter, &adapter->rx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001581done:
1582 adapter->isr_registered = false;
1583 return;
1584}
1585
1586static int be_open(struct net_device *netdev)
1587{
1588 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001589 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1590 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001591 bool link_up;
1592 int status;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001593
1594 /* First time posting */
1595 be_post_rx_frags(adapter);
1596
1597 napi_enable(&rx_eq->napi);
1598 napi_enable(&tx_eq->napi);
1599
1600 be_irq_register(adapter);
1601
Sathya Perla8788fdc2009-07-27 22:52:03 +00001602 be_intr_set(adapter, true);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001603
1604 /* The evt queues are created in unarmed state; arm them */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001605 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
1606 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001607
1608 /* Rx compl queue may be in unarmed state; rearm it */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001609 be_cq_notify(adapter, adapter->rx_obj.cq.id, true, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001610
Sathya Perla8788fdc2009-07-27 22:52:03 +00001611 status = be_cmd_link_status_query(adapter, &link_up);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001612 if (status)
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001613 goto ret_sts;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001614 be_link_status_update(adapter, link_up);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001615
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001616 status = be_vid_config(adapter);
1617 if (status)
1618 goto ret_sts;
1619
1620 status = be_cmd_set_flow_control(adapter,
1621 adapter->tx_fc, adapter->rx_fc);
1622 if (status)
1623 goto ret_sts;
1624
Sathya Perla5fb379e2009-06-18 00:02:59 +00001625 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001626ret_sts:
1627 return status;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001628}
1629
1630static int be_setup(struct be_adapter *adapter)
1631{
Sathya Perla5fb379e2009-06-18 00:02:59 +00001632 struct net_device *netdev = adapter->netdev;
Sathya Perla73d540f2009-10-14 20:20:42 +00001633 u32 cap_flags, en_flags;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001634 int status;
1635
Sathya Perla73d540f2009-10-14 20:20:42 +00001636 cap_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
1637 BE_IF_FLAGS_MCAST_PROMISCUOUS |
1638 BE_IF_FLAGS_PROMISCUOUS |
1639 BE_IF_FLAGS_PASS_L3L4_ERRORS;
1640 en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
1641 BE_IF_FLAGS_PASS_L3L4_ERRORS;
1642
1643 status = be_cmd_if_create(adapter, cap_flags, en_flags,
1644 netdev->dev_addr, false/* pmac_invalid */,
1645 &adapter->if_handle, &adapter->pmac_id);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001646 if (status != 0)
1647 goto do_none;
1648
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001649 status = be_tx_queues_create(adapter);
1650 if (status != 0)
1651 goto if_destroy;
1652
1653 status = be_rx_queues_create(adapter);
1654 if (status != 0)
1655 goto tx_qs_destroy;
1656
Sathya Perla5fb379e2009-06-18 00:02:59 +00001657 status = be_mcc_queues_create(adapter);
1658 if (status != 0)
1659 goto rx_qs_destroy;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001660
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001661 return 0;
1662
Sathya Perla5fb379e2009-06-18 00:02:59 +00001663rx_qs_destroy:
1664 be_rx_queues_destroy(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001665tx_qs_destroy:
1666 be_tx_queues_destroy(adapter);
1667if_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001668 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001669do_none:
1670 return status;
1671}
1672
Sathya Perla5fb379e2009-06-18 00:02:59 +00001673static int be_clear(struct be_adapter *adapter)
1674{
Sathya Perla1a8887d2009-08-17 00:58:41 +00001675 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001676 be_rx_queues_destroy(adapter);
1677 be_tx_queues_destroy(adapter);
1678
Sathya Perla8788fdc2009-07-27 22:52:03 +00001679 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001680
Sathya Perla5fb379e2009-06-18 00:02:59 +00001681 return 0;
1682}
1683
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001684static int be_close(struct net_device *netdev)
1685{
1686 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001687 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1688 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1689 int vec;
1690
Sathya Perlab305be72009-06-10 02:18:35 +00001691 cancel_delayed_work_sync(&adapter->work);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001692
1693 netif_stop_queue(netdev);
1694 netif_carrier_off(netdev);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001695 adapter->link_up = false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001696
Sathya Perla8788fdc2009-07-27 22:52:03 +00001697 be_intr_set(adapter, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001698
1699 if (adapter->msix_enabled) {
1700 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1701 synchronize_irq(vec);
1702 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1703 synchronize_irq(vec);
1704 } else {
1705 synchronize_irq(netdev->irq);
1706 }
1707 be_irq_unregister(adapter);
1708
1709 napi_disable(&rx_eq->napi);
1710 napi_disable(&tx_eq->napi);
1711
Sathya Perlaa8e91792009-08-10 03:42:43 +00001712 /* Wait for all pending tx completions to arrive so that
1713 * all tx skbs are freed.
1714 */
1715 be_tx_compl_clean(adapter);
1716
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001717 return 0;
1718}
1719
Ajit Khaparde84517482009-09-04 03:12:16 +00001720#define FW_FILE_HDR_SIGN "ServerEngines Corp. "
1721char flash_cookie[2][16] = {"*** SE FLAS",
1722 "H DIRECTORY *** "};
1723static int be_flash_image(struct be_adapter *adapter,
1724 const struct firmware *fw,
1725 struct be_dma_mem *flash_cmd, u32 flash_type)
1726{
1727 int status;
1728 u32 flash_op, image_offset = 0, total_bytes, image_size = 0;
1729 int num_bytes;
1730 const u8 *p = fw->data;
1731 struct be_cmd_write_flashrom *req = flash_cmd->va;
1732
1733 switch (flash_type) {
1734 case FLASHROM_TYPE_ISCSI_ACTIVE:
1735 image_offset = FLASH_iSCSI_PRIMARY_IMAGE_START;
1736 image_size = FLASH_IMAGE_MAX_SIZE;
1737 break;
1738 case FLASHROM_TYPE_ISCSI_BACKUP:
1739 image_offset = FLASH_iSCSI_BACKUP_IMAGE_START;
1740 image_size = FLASH_IMAGE_MAX_SIZE;
1741 break;
1742 case FLASHROM_TYPE_FCOE_FW_ACTIVE:
1743 image_offset = FLASH_FCoE_PRIMARY_IMAGE_START;
1744 image_size = FLASH_IMAGE_MAX_SIZE;
1745 break;
1746 case FLASHROM_TYPE_FCOE_FW_BACKUP:
1747 image_offset = FLASH_FCoE_BACKUP_IMAGE_START;
1748 image_size = FLASH_IMAGE_MAX_SIZE;
1749 break;
1750 case FLASHROM_TYPE_BIOS:
1751 image_offset = FLASH_iSCSI_BIOS_START;
1752 image_size = FLASH_BIOS_IMAGE_MAX_SIZE;
1753 break;
1754 case FLASHROM_TYPE_FCOE_BIOS:
1755 image_offset = FLASH_FCoE_BIOS_START;
1756 image_size = FLASH_BIOS_IMAGE_MAX_SIZE;
1757 break;
1758 case FLASHROM_TYPE_PXE_BIOS:
1759 image_offset = FLASH_PXE_BIOS_START;
1760 image_size = FLASH_BIOS_IMAGE_MAX_SIZE;
1761 break;
1762 default:
1763 return 0;
1764 }
1765
1766 p += sizeof(struct flash_file_hdr) + image_offset;
1767 if (p + image_size > fw->data + fw->size)
1768 return -1;
1769
1770 total_bytes = image_size;
1771
1772 while (total_bytes) {
1773 if (total_bytes > 32*1024)
1774 num_bytes = 32*1024;
1775 else
1776 num_bytes = total_bytes;
1777 total_bytes -= num_bytes;
1778
1779 if (!total_bytes)
1780 flash_op = FLASHROM_OPER_FLASH;
1781 else
1782 flash_op = FLASHROM_OPER_SAVE;
1783 memcpy(req->params.data_buf, p, num_bytes);
1784 p += num_bytes;
1785 status = be_cmd_write_flashrom(adapter, flash_cmd,
1786 flash_type, flash_op, num_bytes);
1787 if (status) {
1788 dev_err(&adapter->pdev->dev,
1789 "cmd to write to flash rom failed. type/op %d/%d\n",
1790 flash_type, flash_op);
1791 return -1;
1792 }
1793 yield();
1794 }
1795
1796 return 0;
1797}
1798
1799int be_load_fw(struct be_adapter *adapter, u8 *func)
1800{
1801 char fw_file[ETHTOOL_FLASH_MAX_FILENAME];
1802 const struct firmware *fw;
1803 struct flash_file_hdr *fhdr;
1804 struct flash_section_info *fsec = NULL;
1805 struct be_dma_mem flash_cmd;
1806 int status;
1807 const u8 *p;
1808 bool entry_found = false;
1809 int flash_type;
1810 char fw_ver[FW_VER_LEN];
1811 char fw_cfg;
1812
1813 status = be_cmd_get_fw_ver(adapter, fw_ver);
1814 if (status)
1815 return status;
1816
1817 fw_cfg = *(fw_ver + 2);
1818 if (fw_cfg == '0')
1819 fw_cfg = '1';
1820 strcpy(fw_file, func);
1821
1822 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
1823 if (status)
1824 goto fw_exit;
1825
1826 p = fw->data;
1827 fhdr = (struct flash_file_hdr *) p;
1828 if (memcmp(fhdr->sign, FW_FILE_HDR_SIGN, strlen(FW_FILE_HDR_SIGN))) {
1829 dev_err(&adapter->pdev->dev,
1830 "Firmware(%s) load error (signature did not match)\n",
1831 fw_file);
1832 status = -1;
1833 goto fw_exit;
1834 }
1835
1836 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
1837
1838 p += sizeof(struct flash_file_hdr);
1839 while (p < (fw->data + fw->size)) {
1840 fsec = (struct flash_section_info *)p;
1841 if (!memcmp(flash_cookie, fsec->cookie, sizeof(flash_cookie))) {
1842 entry_found = true;
1843 break;
1844 }
1845 p += 32;
1846 }
1847
1848 if (!entry_found) {
1849 status = -1;
1850 dev_err(&adapter->pdev->dev,
1851 "Flash cookie not found in firmware image\n");
1852 goto fw_exit;
1853 }
1854
1855 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
1856 flash_cmd.va = pci_alloc_consistent(adapter->pdev, flash_cmd.size,
1857 &flash_cmd.dma);
1858 if (!flash_cmd.va) {
1859 status = -ENOMEM;
1860 dev_err(&adapter->pdev->dev,
1861 "Memory allocation failure while flashing\n");
1862 goto fw_exit;
1863 }
1864
1865 for (flash_type = FLASHROM_TYPE_ISCSI_ACTIVE;
1866 flash_type <= FLASHROM_TYPE_FCOE_FW_BACKUP; flash_type++) {
1867 status = be_flash_image(adapter, fw, &flash_cmd,
1868 flash_type);
1869 if (status)
1870 break;
1871 }
1872
1873 pci_free_consistent(adapter->pdev, flash_cmd.size, flash_cmd.va,
1874 flash_cmd.dma);
1875 if (status) {
1876 dev_err(&adapter->pdev->dev, "Firmware load error\n");
1877 goto fw_exit;
1878 }
1879
1880 dev_info(&adapter->pdev->dev, "Firmware flashed succesfully\n");
1881
1882fw_exit:
1883 release_firmware(fw);
1884 return status;
1885}
1886
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001887static struct net_device_ops be_netdev_ops = {
1888 .ndo_open = be_open,
1889 .ndo_stop = be_close,
1890 .ndo_start_xmit = be_xmit,
1891 .ndo_get_stats = be_get_stats,
1892 .ndo_set_rx_mode = be_set_multicast_list,
1893 .ndo_set_mac_address = be_mac_addr_set,
1894 .ndo_change_mtu = be_change_mtu,
1895 .ndo_validate_addr = eth_validate_addr,
1896 .ndo_vlan_rx_register = be_vlan_register,
1897 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
1898 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
1899};
1900
1901static void be_netdev_init(struct net_device *netdev)
1902{
1903 struct be_adapter *adapter = netdev_priv(netdev);
1904
1905 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
Ajit Khaparde583e3f32009-10-05 02:22:19 +00001906 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_HW_CSUM |
1907 NETIF_F_GRO;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001908
1909 netdev->flags |= IFF_MULTICAST;
1910
Ajit Khaparde728a9972009-04-13 15:41:22 -07001911 adapter->rx_csum = true;
1912
Ajit Khaparde9e90c962009-11-06 02:06:59 +00001913 /* Default settings for Rx and Tx flow control */
1914 adapter->rx_fc = true;
1915 adapter->tx_fc = true;
1916
Ajit Khapardec190e3c2009-09-04 03:12:29 +00001917 netif_set_gso_max_size(netdev, 65535);
1918
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001919 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
1920
1921 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
1922
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001923 netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx,
1924 BE_NAPI_WEIGHT);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001925 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001926 BE_NAPI_WEIGHT);
1927
1928 netif_carrier_off(netdev);
1929 netif_stop_queue(netdev);
1930}
1931
1932static void be_unmap_pci_bars(struct be_adapter *adapter)
1933{
Sathya Perla8788fdc2009-07-27 22:52:03 +00001934 if (adapter->csr)
1935 iounmap(adapter->csr);
1936 if (adapter->db)
1937 iounmap(adapter->db);
1938 if (adapter->pcicfg)
1939 iounmap(adapter->pcicfg);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001940}
1941
1942static int be_map_pci_bars(struct be_adapter *adapter)
1943{
1944 u8 __iomem *addr;
1945
1946 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
1947 pci_resource_len(adapter->pdev, 2));
1948 if (addr == NULL)
1949 return -ENOMEM;
Sathya Perla8788fdc2009-07-27 22:52:03 +00001950 adapter->csr = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001951
1952 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 4),
1953 128 * 1024);
1954 if (addr == NULL)
1955 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00001956 adapter->db = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001957
1958 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 1),
1959 pci_resource_len(adapter->pdev, 1));
1960 if (addr == NULL)
1961 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00001962 adapter->pcicfg = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001963
1964 return 0;
1965pci_map_err:
1966 be_unmap_pci_bars(adapter);
1967 return -ENOMEM;
1968}
1969
1970
1971static void be_ctrl_cleanup(struct be_adapter *adapter)
1972{
Sathya Perla8788fdc2009-07-27 22:52:03 +00001973 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001974
1975 be_unmap_pci_bars(adapter);
1976
1977 if (mem->va)
1978 pci_free_consistent(adapter->pdev, mem->size,
1979 mem->va, mem->dma);
1980}
1981
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001982static int be_ctrl_init(struct be_adapter *adapter)
1983{
Sathya Perla8788fdc2009-07-27 22:52:03 +00001984 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
1985 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001986 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001987
1988 status = be_map_pci_bars(adapter);
1989 if (status)
1990 return status;
1991
1992 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
1993 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
1994 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
1995 if (!mbox_mem_alloc->va) {
1996 be_unmap_pci_bars(adapter);
1997 return -1;
1998 }
1999 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
2000 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
2001 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
2002 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Sathya Perla8788fdc2009-07-27 22:52:03 +00002003 spin_lock_init(&adapter->mbox_lock);
2004 spin_lock_init(&adapter->mcc_lock);
2005 spin_lock_init(&adapter->mcc_cq_lock);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002006
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002007 return 0;
2008}
2009
2010static void be_stats_cleanup(struct be_adapter *adapter)
2011{
2012 struct be_stats_obj *stats = &adapter->stats;
2013 struct be_dma_mem *cmd = &stats->cmd;
2014
2015 if (cmd->va)
2016 pci_free_consistent(adapter->pdev, cmd->size,
2017 cmd->va, cmd->dma);
2018}
2019
2020static int be_stats_init(struct be_adapter *adapter)
2021{
2022 struct be_stats_obj *stats = &adapter->stats;
2023 struct be_dma_mem *cmd = &stats->cmd;
2024
2025 cmd->size = sizeof(struct be_cmd_req_get_stats);
2026 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
2027 if (cmd->va == NULL)
2028 return -1;
2029 return 0;
2030}
2031
2032static void __devexit be_remove(struct pci_dev *pdev)
2033{
2034 struct be_adapter *adapter = pci_get_drvdata(pdev);
2035 if (!adapter)
2036 return;
2037
2038 unregister_netdev(adapter->netdev);
2039
Sathya Perla5fb379e2009-06-18 00:02:59 +00002040 be_clear(adapter);
2041
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002042 be_stats_cleanup(adapter);
2043
2044 be_ctrl_cleanup(adapter);
2045
2046 if (adapter->msix_enabled) {
2047 pci_disable_msix(adapter->pdev);
2048 adapter->msix_enabled = false;
2049 }
2050
2051 pci_set_drvdata(pdev, NULL);
2052 pci_release_regions(pdev);
2053 pci_disable_device(pdev);
2054
2055 free_netdev(adapter->netdev);
2056}
2057
2058static int be_hw_up(struct be_adapter *adapter)
2059{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002060 int status;
2061
Sathya Perla8788fdc2009-07-27 22:52:03 +00002062 status = be_cmd_POST(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002063 if (status)
2064 return status;
2065
Sathya Perla43a04fdc2009-10-14 20:21:17 +00002066 status = be_cmd_reset_function(adapter);
2067 if (status)
2068 return status;
2069
Sathya Perla8788fdc2009-07-27 22:52:03 +00002070 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002071 if (status)
2072 return status;
2073
Ajit Khapardedcb9b562009-09-30 21:58:22 -07002074 status = be_cmd_query_fw_cfg(adapter,
2075 &adapter->port_num, &adapter->cap);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002076 return status;
2077}
2078
2079static int __devinit be_probe(struct pci_dev *pdev,
2080 const struct pci_device_id *pdev_id)
2081{
2082 int status = 0;
2083 struct be_adapter *adapter;
2084 struct net_device *netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002085 u8 mac[ETH_ALEN];
2086
2087 status = pci_enable_device(pdev);
2088 if (status)
2089 goto do_none;
2090
2091 status = pci_request_regions(pdev, DRV_NAME);
2092 if (status)
2093 goto disable_dev;
2094 pci_set_master(pdev);
2095
2096 netdev = alloc_etherdev(sizeof(struct be_adapter));
2097 if (netdev == NULL) {
2098 status = -ENOMEM;
2099 goto rel_reg;
2100 }
2101 adapter = netdev_priv(netdev);
2102 adapter->pdev = pdev;
2103 pci_set_drvdata(pdev, adapter);
2104 adapter->netdev = netdev;
2105
2106 be_msix_enable(adapter);
2107
Yang Hongyange9304382009-04-13 14:40:14 -07002108 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002109 if (!status) {
2110 netdev->features |= NETIF_F_HIGHDMA;
2111 } else {
Yang Hongyange9304382009-04-13 14:40:14 -07002112 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002113 if (status) {
2114 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
2115 goto free_netdev;
2116 }
2117 }
2118
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002119 status = be_ctrl_init(adapter);
2120 if (status)
2121 goto free_netdev;
2122
2123 status = be_stats_init(adapter);
2124 if (status)
2125 goto ctrl_clean;
2126
2127 status = be_hw_up(adapter);
2128 if (status)
2129 goto stats_clean;
2130
Sathya Perla8788fdc2009-07-27 22:52:03 +00002131 status = be_cmd_mac_addr_query(adapter, mac, MAC_ADDRESS_TYPE_NETWORK,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002132 true /* permanent */, 0);
2133 if (status)
2134 goto stats_clean;
2135 memcpy(netdev->dev_addr, mac, ETH_ALEN);
2136
2137 INIT_DELAYED_WORK(&adapter->work, be_worker);
2138 be_netdev_init(netdev);
2139 SET_NETDEV_DEV(netdev, &adapter->pdev->dev);
2140
Sathya Perla5fb379e2009-06-18 00:02:59 +00002141 status = be_setup(adapter);
2142 if (status)
2143 goto stats_clean;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002144 status = register_netdev(netdev);
2145 if (status != 0)
Sathya Perla5fb379e2009-06-18 00:02:59 +00002146 goto unsetup;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002147
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002148 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002149 return 0;
2150
Sathya Perla5fb379e2009-06-18 00:02:59 +00002151unsetup:
2152 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002153stats_clean:
2154 be_stats_cleanup(adapter);
2155ctrl_clean:
2156 be_ctrl_cleanup(adapter);
2157free_netdev:
2158 free_netdev(adapter->netdev);
2159rel_reg:
2160 pci_release_regions(pdev);
2161disable_dev:
2162 pci_disable_device(pdev);
2163do_none:
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002164 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002165 return status;
2166}
2167
2168static int be_suspend(struct pci_dev *pdev, pm_message_t state)
2169{
2170 struct be_adapter *adapter = pci_get_drvdata(pdev);
2171 struct net_device *netdev = adapter->netdev;
2172
2173 netif_device_detach(netdev);
2174 if (netif_running(netdev)) {
2175 rtnl_lock();
2176 be_close(netdev);
2177 rtnl_unlock();
2178 }
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002179 be_cmd_get_flow_control(adapter, &adapter->tx_fc, &adapter->rx_fc);
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002180 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002181
2182 pci_save_state(pdev);
2183 pci_disable_device(pdev);
2184 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2185 return 0;
2186}
2187
2188static int be_resume(struct pci_dev *pdev)
2189{
2190 int status = 0;
2191 struct be_adapter *adapter = pci_get_drvdata(pdev);
2192 struct net_device *netdev = adapter->netdev;
2193
2194 netif_device_detach(netdev);
2195
2196 status = pci_enable_device(pdev);
2197 if (status)
2198 return status;
2199
2200 pci_set_power_state(pdev, 0);
2201 pci_restore_state(pdev);
2202
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002203 be_setup(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002204 if (netif_running(netdev)) {
2205 rtnl_lock();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002206 be_open(netdev);
2207 rtnl_unlock();
2208 }
2209 netif_device_attach(netdev);
2210 return 0;
2211}
2212
2213static struct pci_driver be_driver = {
2214 .name = DRV_NAME,
2215 .id_table = be_dev_ids,
2216 .probe = be_probe,
2217 .remove = be_remove,
2218 .suspend = be_suspend,
2219 .resume = be_resume
2220};
2221
2222static int __init be_init_module(void)
2223{
2224 if (rx_frag_size != 8192 && rx_frag_size != 4096
2225 && rx_frag_size != 2048) {
2226 printk(KERN_WARNING DRV_NAME
2227 " : Module param rx_frag_size must be 2048/4096/8192."
2228 " Using 2048\n");
2229 rx_frag_size = 2048;
2230 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002231
2232 return pci_register_driver(&be_driver);
2233}
2234module_init(be_init_module);
2235
2236static void __exit be_exit_module(void)
2237{
2238 pci_unregister_driver(&be_driver);
2239}
2240module_exit(be_exit_module);