blob: 54b14272f333330edc9cddbce502d7e0068103ba [file] [log] [blame]
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001/*
Ajit Khaparde294aedc2010-02-19 13:54:58 +00002 * Copyright (C) 2005 - 2010 ServerEngines
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003 * 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;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +000029static unsigned int num_vfs;
Sathya Perla6b7c5b92009-03-11 23:32:03 -070030module_param(rx_frag_size, uint, S_IRUGO);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +000031module_param(num_vfs, uint, S_IRUGO);
Sathya Perla6b7c5b92009-03-11 23:32:03 -070032MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data.");
Sarveshwar Bandiba343c72010-03-31 02:56:12 +000033MODULE_PARM_DESC(num_vfs, "Number of PCI VFs to initialize");
Sathya Perla6b7c5b92009-03-11 23:32:03 -070034
Sathya Perla6b7c5b92009-03-11 23:32:03 -070035static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
Ajit Khapardec4ca2372009-05-18 15:38:55 -070036 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Ajit Khaparde59fd5d82009-10-29 01:11:06 -070037 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Ajit Khapardec4ca2372009-05-18 15:38:55 -070038 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
39 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
Sathya Perla6b7c5b92009-03-11 23:32:03 -070040 { 0 }
41};
42MODULE_DEVICE_TABLE(pci, be_dev_ids);
43
44static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
45{
46 struct be_dma_mem *mem = &q->dma_mem;
47 if (mem->va)
48 pci_free_consistent(adapter->pdev, mem->size,
49 mem->va, mem->dma);
50}
51
52static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
53 u16 len, u16 entry_size)
54{
55 struct be_dma_mem *mem = &q->dma_mem;
56
57 memset(q, 0, sizeof(*q));
58 q->len = len;
59 q->entry_size = entry_size;
60 mem->size = len * entry_size;
61 mem->va = pci_alloc_consistent(adapter->pdev, mem->size, &mem->dma);
62 if (!mem->va)
63 return -1;
64 memset(mem->va, 0, mem->size);
65 return 0;
66}
67
Sathya Perla8788fdc2009-07-27 22:52:03 +000068static void be_intr_set(struct be_adapter *adapter, bool enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070069{
Sathya Perla8788fdc2009-07-27 22:52:03 +000070 u8 __iomem *addr = adapter->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
Sathya Perla6b7c5b92009-03-11 23:32:03 -070071 u32 reg = ioread32(addr);
72 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +000073
Sathya Perlacf588472010-02-14 21:22:01 +000074 if (adapter->eeh_err)
75 return;
76
Sathya Perla5f0b8492009-07-27 22:52:56 +000077 if (!enabled && enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070078 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +000079 else if (enabled && !enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070080 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +000081 else
Sathya Perla6b7c5b92009-03-11 23:32:03 -070082 return;
Sathya Perla5f0b8492009-07-27 22:52:56 +000083
Sathya Perla6b7c5b92009-03-11 23:32:03 -070084 iowrite32(reg, addr);
85}
86
Sathya Perla8788fdc2009-07-27 22:52:03 +000087static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070088{
89 u32 val = 0;
90 val |= qid & DB_RQ_RING_ID_MASK;
91 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +000092 iowrite32(val, adapter->db + DB_RQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -070093}
94
Sathya Perla8788fdc2009-07-27 22:52:03 +000095static void be_txq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
Sathya Perla6b7c5b92009-03-11 23:32:03 -070096{
97 u32 val = 0;
98 val |= qid & DB_TXULP_RING_ID_MASK;
99 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000100 iowrite32(val, adapter->db + DB_TXULP1_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700101}
102
Sathya Perla8788fdc2009-07-27 22:52:03 +0000103static void be_eq_notify(struct be_adapter *adapter, u16 qid,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700104 bool arm, bool clear_int, u16 num_popped)
105{
106 u32 val = 0;
107 val |= qid & DB_EQ_RING_ID_MASK;
Sathya Perlacf588472010-02-14 21:22:01 +0000108
109 if (adapter->eeh_err)
110 return;
111
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700112 if (arm)
113 val |= 1 << DB_EQ_REARM_SHIFT;
114 if (clear_int)
115 val |= 1 << DB_EQ_CLR_SHIFT;
116 val |= 1 << DB_EQ_EVNT_SHIFT;
117 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000118 iowrite32(val, adapter->db + DB_EQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700119}
120
Sathya Perla8788fdc2009-07-27 22:52:03 +0000121void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700122{
123 u32 val = 0;
124 val |= qid & DB_CQ_RING_ID_MASK;
Sathya Perlacf588472010-02-14 21:22:01 +0000125
126 if (adapter->eeh_err)
127 return;
128
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700129 if (arm)
130 val |= 1 << DB_CQ_REARM_SHIFT;
131 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000132 iowrite32(val, adapter->db + DB_CQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700133}
134
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700135static int be_mac_addr_set(struct net_device *netdev, void *p)
136{
137 struct be_adapter *adapter = netdev_priv(netdev);
138 struct sockaddr *addr = p;
139 int status = 0;
140
Ajit Khapardeca9e4982009-11-29 17:56:26 +0000141 if (!is_valid_ether_addr(addr->sa_data))
142 return -EADDRNOTAVAIL;
143
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000144 /* MAC addr configuration will be done in hardware for VFs
145 * by their corresponding PFs. Just copy to netdev addr here
146 */
147 if (!be_physfn(adapter))
148 goto netdev_addr;
149
Sathya Perlaa65027e2009-08-17 00:58:04 +0000150 status = be_cmd_pmac_del(adapter, adapter->if_handle, adapter->pmac_id);
151 if (status)
152 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700153
Sathya Perlaa65027e2009-08-17 00:58:04 +0000154 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
155 adapter->if_handle, &adapter->pmac_id);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000156netdev_addr:
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700157 if (!status)
158 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
159
160 return status;
161}
162
Sathya Perlab31c50a2009-09-17 10:30:13 -0700163void netdev_stats_update(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700164{
165 struct be_hw_stats *hw_stats = hw_stats_from_cmd(adapter->stats.cmd.va);
166 struct be_rxf_stats *rxf_stats = &hw_stats->rxf;
167 struct be_port_rxf_stats *port_stats =
168 &rxf_stats->port[adapter->port_num];
Ajit Khaparde78122a52009-10-07 03:11:20 -0700169 struct net_device_stats *dev_stats = &adapter->netdev->stats;
Sathya Perla68110862009-06-10 02:21:16 +0000170 struct be_erx_stats *erx_stats = &hw_stats->erx;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700171
Ajit Khaparde91992e42010-02-19 13:57:12 +0000172 dev_stats->rx_packets = drvr_stats(adapter)->be_rx_pkts;
173 dev_stats->tx_packets = drvr_stats(adapter)->be_tx_pkts;
174 dev_stats->rx_bytes = drvr_stats(adapter)->be_rx_bytes;
175 dev_stats->tx_bytes = drvr_stats(adapter)->be_tx_bytes;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700176
177 /* bad pkts received */
178 dev_stats->rx_errors = port_stats->rx_crc_errors +
179 port_stats->rx_alignment_symbol_errors +
180 port_stats->rx_in_range_errors +
Sathya Perla68110862009-06-10 02:21:16 +0000181 port_stats->rx_out_range_errors +
182 port_stats->rx_frame_too_long +
183 port_stats->rx_dropped_too_small +
184 port_stats->rx_dropped_too_short +
185 port_stats->rx_dropped_header_too_small +
186 port_stats->rx_dropped_tcp_length +
187 port_stats->rx_dropped_runt +
188 port_stats->rx_tcp_checksum_errs +
189 port_stats->rx_ip_checksum_errs +
190 port_stats->rx_udp_checksum_errs;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700191
Sathya Perla68110862009-06-10 02:21:16 +0000192 /* no space in linux buffers: best possible approximation */
Sathya Perla01ed30d2009-11-22 22:01:31 +0000193 dev_stats->rx_dropped =
194 erx_stats->rx_drops_no_fragments[adapter->rx_obj.q.id];
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700195
196 /* detailed rx errors */
197 dev_stats->rx_length_errors = port_stats->rx_in_range_errors +
Sathya Perla68110862009-06-10 02:21:16 +0000198 port_stats->rx_out_range_errors +
199 port_stats->rx_frame_too_long;
200
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700201 /* receive ring buffer overflow */
202 dev_stats->rx_over_errors = 0;
Sathya Perla68110862009-06-10 02:21:16 +0000203
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700204 dev_stats->rx_crc_errors = port_stats->rx_crc_errors;
205
206 /* frame alignment errors */
207 dev_stats->rx_frame_errors = port_stats->rx_alignment_symbol_errors;
Sathya Perla68110862009-06-10 02:21:16 +0000208
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700209 /* receiver fifo overrun */
210 /* drops_no_pbuf is no per i/f, it's per BE card */
211 dev_stats->rx_fifo_errors = port_stats->rx_fifo_overflow +
212 port_stats->rx_input_fifo_overflow +
213 rxf_stats->rx_drops_no_pbuf;
214 /* receiver missed packetd */
215 dev_stats->rx_missed_errors = 0;
Sathya Perla68110862009-06-10 02:21:16 +0000216
217 /* packet transmit problems */
218 dev_stats->tx_errors = 0;
219
220 /* no space available in linux */
221 dev_stats->tx_dropped = 0;
222
Ajit Khapardec5b9b922009-10-05 02:21:51 +0000223 dev_stats->multicast = port_stats->rx_multicast_frames;
Sathya Perla68110862009-06-10 02:21:16 +0000224 dev_stats->collisions = 0;
225
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700226 /* detailed tx_errors */
227 dev_stats->tx_aborted_errors = 0;
228 dev_stats->tx_carrier_errors = 0;
229 dev_stats->tx_fifo_errors = 0;
230 dev_stats->tx_heartbeat_errors = 0;
231 dev_stats->tx_window_errors = 0;
232}
233
Sathya Perla8788fdc2009-07-27 22:52:03 +0000234void be_link_status_update(struct be_adapter *adapter, bool link_up)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700235{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700236 struct net_device *netdev = adapter->netdev;
237
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700238 /* If link came up or went down */
Sathya Perlaa8f447b2009-06-18 00:10:27 +0000239 if (adapter->link_up != link_up) {
Ajit Khaparde0dffc832009-11-29 17:57:46 +0000240 adapter->link_speed = -1;
Sathya Perlaa8f447b2009-06-18 00:10:27 +0000241 if (link_up) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700242 netif_start_queue(netdev);
243 netif_carrier_on(netdev);
244 printk(KERN_INFO "%s: Link up\n", netdev->name);
Sathya Perlaa8f447b2009-06-18 00:10:27 +0000245 } else {
246 netif_stop_queue(netdev);
247 netif_carrier_off(netdev);
248 printk(KERN_INFO "%s: Link down\n", netdev->name);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700249 }
Sathya Perlaa8f447b2009-06-18 00:10:27 +0000250 adapter->link_up = link_up;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700251 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700252}
253
254/* Update the EQ delay n BE based on the RX frags consumed / sec */
255static void be_rx_eqd_update(struct be_adapter *adapter)
256{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700257 struct be_eq_obj *rx_eq = &adapter->rx_eq;
258 struct be_drvr_stats *stats = &adapter->stats.drvr_stats;
Sathya Perla4097f662009-03-24 16:40:13 -0700259 ulong now = jiffies;
260 u32 eqd;
261
262 if (!rx_eq->enable_aic)
263 return;
264
265 /* Wrapped around */
266 if (time_before(now, stats->rx_fps_jiffies)) {
267 stats->rx_fps_jiffies = now;
268 return;
269 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700270
271 /* Update once a second */
Sathya Perla4097f662009-03-24 16:40:13 -0700272 if ((now - stats->rx_fps_jiffies) < HZ)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700273 return;
274
275 stats->be_rx_fps = (stats->be_rx_frags - stats->be_prev_rx_frags) /
Sathya Perla4097f662009-03-24 16:40:13 -0700276 ((now - stats->rx_fps_jiffies) / HZ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700277
Sathya Perla4097f662009-03-24 16:40:13 -0700278 stats->rx_fps_jiffies = now;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700279 stats->be_prev_rx_frags = stats->be_rx_frags;
280 eqd = stats->be_rx_fps / 110000;
281 eqd = eqd << 3;
282 if (eqd > rx_eq->max_eqd)
283 eqd = rx_eq->max_eqd;
284 if (eqd < rx_eq->min_eqd)
285 eqd = rx_eq->min_eqd;
286 if (eqd < 10)
287 eqd = 0;
288 if (eqd != rx_eq->cur_eqd)
Sathya Perla8788fdc2009-07-27 22:52:03 +0000289 be_cmd_modify_eqd(adapter, rx_eq->q.id, eqd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700290
291 rx_eq->cur_eqd = eqd;
292}
293
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700294static struct net_device_stats *be_get_stats(struct net_device *dev)
295{
Ajit Khaparde78122a52009-10-07 03:11:20 -0700296 return &dev->stats;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700297}
298
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700299static u32 be_calc_rate(u64 bytes, unsigned long ticks)
300{
301 u64 rate = bytes;
302
303 do_div(rate, ticks / HZ);
304 rate <<= 3; /* bytes/sec -> bits/sec */
305 do_div(rate, 1000000ul); /* MB/Sec */
306
307 return rate;
308}
309
Sathya Perla4097f662009-03-24 16:40:13 -0700310static void be_tx_rate_update(struct be_adapter *adapter)
311{
312 struct be_drvr_stats *stats = drvr_stats(adapter);
313 ulong now = jiffies;
314
315 /* Wrapped around? */
316 if (time_before(now, stats->be_tx_jiffies)) {
317 stats->be_tx_jiffies = now;
318 return;
319 }
320
321 /* Update tx rate once in two seconds */
322 if ((now - stats->be_tx_jiffies) > 2 * HZ) {
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700323 stats->be_tx_rate = be_calc_rate(stats->be_tx_bytes
324 - stats->be_tx_bytes_prev,
325 now - stats->be_tx_jiffies);
Sathya Perla4097f662009-03-24 16:40:13 -0700326 stats->be_tx_jiffies = now;
327 stats->be_tx_bytes_prev = stats->be_tx_bytes;
328 }
329}
330
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700331static void be_tx_stats_update(struct be_adapter *adapter,
Ajit Khaparde91992e42010-02-19 13:57:12 +0000332 u32 wrb_cnt, u32 copied, u32 gso_segs, bool stopped)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700333{
Sathya Perla4097f662009-03-24 16:40:13 -0700334 struct be_drvr_stats *stats = drvr_stats(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700335 stats->be_tx_reqs++;
336 stats->be_tx_wrbs += wrb_cnt;
337 stats->be_tx_bytes += copied;
Ajit Khaparde91992e42010-02-19 13:57:12 +0000338 stats->be_tx_pkts += (gso_segs ? gso_segs : 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700339 if (stopped)
340 stats->be_tx_stops++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700341}
342
343/* Determine number of WRB entries needed to xmit data in an skb */
344static u32 wrb_cnt_for_skb(struct sk_buff *skb, bool *dummy)
345{
David S. Millerebc8d2a2009-06-09 01:01:31 -0700346 int cnt = (skb->len > skb->data_len);
347
348 cnt += skb_shinfo(skb)->nr_frags;
349
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700350 /* to account for hdr wrb */
351 cnt++;
352 if (cnt & 1) {
353 /* add a dummy to make it an even num */
354 cnt++;
355 *dummy = true;
356 } else
357 *dummy = false;
358 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
359 return cnt;
360}
361
362static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
363{
364 wrb->frag_pa_hi = upper_32_bits(addr);
365 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
366 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
367}
368
369static void wrb_fill_hdr(struct be_eth_hdr_wrb *hdr, struct sk_buff *skb,
370 bool vlan, u32 wrb_cnt, u32 len)
371{
372 memset(hdr, 0, sizeof(*hdr));
373
374 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
375
376 if (skb_shinfo(skb)->gso_segs > 1 && skb_shinfo(skb)->gso_size) {
377 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
378 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
379 hdr, skb_shinfo(skb)->gso_size);
380 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
381 if (is_tcp_pkt(skb))
382 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
383 else if (is_udp_pkt(skb))
384 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
385 }
386
387 if (vlan && vlan_tx_tag_present(skb)) {
388 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
389 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag,
390 hdr, vlan_tx_tag_get(skb));
391 }
392
393 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
394 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
395 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
396 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
397}
398
Sathya Perla7101e112010-03-22 20:41:12 +0000399static void unmap_tx_frag(struct pci_dev *pdev, struct be_eth_wrb *wrb,
400 bool unmap_single)
401{
402 dma_addr_t dma;
403
404 be_dws_le_to_cpu(wrb, sizeof(*wrb));
405
406 dma = (u64)wrb->frag_pa_hi << 32 | (u64)wrb->frag_pa_lo;
FUJITA Tomonorib681ee72010-04-04 21:40:18 +0000407 if (wrb->frag_len) {
Sathya Perla7101e112010-03-22 20:41:12 +0000408 if (unmap_single)
409 pci_unmap_single(pdev, dma, wrb->frag_len,
410 PCI_DMA_TODEVICE);
411 else
412 pci_unmap_page(pdev, dma, wrb->frag_len,
413 PCI_DMA_TODEVICE);
414 }
415}
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700416
417static int make_tx_wrbs(struct be_adapter *adapter,
418 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
419{
Sathya Perla7101e112010-03-22 20:41:12 +0000420 dma_addr_t busaddr;
421 int i, copied = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700422 struct pci_dev *pdev = adapter->pdev;
423 struct sk_buff *first_skb = skb;
424 struct be_queue_info *txq = &adapter->tx_obj.q;
425 struct be_eth_wrb *wrb;
426 struct be_eth_hdr_wrb *hdr;
Sathya Perla7101e112010-03-22 20:41:12 +0000427 bool map_single = false;
428 u16 map_head;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700429
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700430 hdr = queue_head_node(txq);
431 queue_head_inc(txq);
Sathya Perla7101e112010-03-22 20:41:12 +0000432 map_head = txq->head;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700433
David S. Millerebc8d2a2009-06-09 01:01:31 -0700434 if (skb->len > skb->data_len) {
Eric Dumazete743d312010-04-14 15:59:40 -0700435 int len = skb_headlen(skb);
Alexander Duycka73b7962009-12-02 16:48:18 +0000436 busaddr = pci_map_single(pdev, skb->data, len,
437 PCI_DMA_TODEVICE);
Sathya Perla7101e112010-03-22 20:41:12 +0000438 if (pci_dma_mapping_error(pdev, busaddr))
439 goto dma_err;
440 map_single = true;
David S. Millerebc8d2a2009-06-09 01:01:31 -0700441 wrb = queue_head_node(txq);
442 wrb_fill(wrb, busaddr, len);
443 be_dws_cpu_to_le(wrb, sizeof(*wrb));
444 queue_head_inc(txq);
445 copied += len;
446 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700447
David S. Millerebc8d2a2009-06-09 01:01:31 -0700448 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
449 struct skb_frag_struct *frag =
450 &skb_shinfo(skb)->frags[i];
Alexander Duycka73b7962009-12-02 16:48:18 +0000451 busaddr = pci_map_page(pdev, frag->page,
452 frag->page_offset,
453 frag->size, PCI_DMA_TODEVICE);
Sathya Perla7101e112010-03-22 20:41:12 +0000454 if (pci_dma_mapping_error(pdev, busaddr))
455 goto dma_err;
David S. Millerebc8d2a2009-06-09 01:01:31 -0700456 wrb = queue_head_node(txq);
457 wrb_fill(wrb, busaddr, frag->size);
458 be_dws_cpu_to_le(wrb, sizeof(*wrb));
459 queue_head_inc(txq);
460 copied += frag->size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700461 }
462
463 if (dummy_wrb) {
464 wrb = queue_head_node(txq);
465 wrb_fill(wrb, 0, 0);
466 be_dws_cpu_to_le(wrb, sizeof(*wrb));
467 queue_head_inc(txq);
468 }
469
470 wrb_fill_hdr(hdr, first_skb, adapter->vlan_grp ? true : false,
471 wrb_cnt, copied);
472 be_dws_cpu_to_le(hdr, sizeof(*hdr));
473
474 return copied;
Sathya Perla7101e112010-03-22 20:41:12 +0000475dma_err:
476 txq->head = map_head;
477 while (copied) {
478 wrb = queue_head_node(txq);
479 unmap_tx_frag(pdev, wrb, map_single);
480 map_single = false;
481 copied -= wrb->frag_len;
482 queue_head_inc(txq);
483 }
484 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700485}
486
Stephen Hemminger613573252009-08-31 19:50:58 +0000487static netdev_tx_t be_xmit(struct sk_buff *skb,
Sathya Perlab31c50a2009-09-17 10:30:13 -0700488 struct net_device *netdev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700489{
490 struct be_adapter *adapter = netdev_priv(netdev);
491 struct be_tx_obj *tx_obj = &adapter->tx_obj;
492 struct be_queue_info *txq = &tx_obj->q;
493 u32 wrb_cnt = 0, copied = 0;
494 u32 start = txq->head;
495 bool dummy_wrb, stopped = false;
496
497 wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb);
498
499 copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000500 if (copied) {
501 /* record the sent skb in the sent_skb table */
502 BUG_ON(tx_obj->sent_skb_list[start]);
503 tx_obj->sent_skb_list[start] = skb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700504
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000505 /* Ensure txq has space for the next skb; Else stop the queue
506 * *BEFORE* ringing the tx doorbell, so that we serialze the
507 * tx compls of the current transmit which'll wake up the queue
508 */
Sathya Perla7101e112010-03-22 20:41:12 +0000509 atomic_add(wrb_cnt, &txq->used);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000510 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
511 txq->len) {
512 netif_stop_queue(netdev);
513 stopped = true;
514 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700515
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000516 be_txq_notify(adapter, txq->id, wrb_cnt);
517
Ajit Khaparde91992e42010-02-19 13:57:12 +0000518 be_tx_stats_update(adapter, wrb_cnt, copied,
519 skb_shinfo(skb)->gso_segs, stopped);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000520 } else {
521 txq->head = start;
522 dev_kfree_skb_any(skb);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700523 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700524 return NETDEV_TX_OK;
525}
526
527static int be_change_mtu(struct net_device *netdev, int new_mtu)
528{
529 struct be_adapter *adapter = netdev_priv(netdev);
530 if (new_mtu < BE_MIN_MTU ||
Ajit Khaparde34a89b82010-02-09 01:32:43 +0000531 new_mtu > (BE_MAX_JUMBO_FRAME_SIZE -
532 (ETH_HLEN + ETH_FCS_LEN))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700533 dev_info(&adapter->pdev->dev,
534 "MTU must be between %d and %d bytes\n",
Ajit Khaparde34a89b82010-02-09 01:32:43 +0000535 BE_MIN_MTU,
536 (BE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN)));
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700537 return -EINVAL;
538 }
539 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
540 netdev->mtu, new_mtu);
541 netdev->mtu = new_mtu;
542 return 0;
543}
544
545/*
Ajit Khaparde82903e42010-02-09 01:34:57 +0000546 * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
547 * If the user configures more, place BE in vlan promiscuous mode.
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700548 */
Sathya Perlab31c50a2009-09-17 10:30:13 -0700549static int be_vid_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700550{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700551 u16 vtag[BE_NUM_VLANS_SUPPORTED];
552 u16 ntags = 0, i;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000553 int status = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700554
Ajit Khaparde82903e42010-02-09 01:34:57 +0000555 if (adapter->vlans_added <= adapter->max_vlans) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700556 /* Construct VLAN Table to give to HW */
557 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
558 if (adapter->vlan_tag[i]) {
559 vtag[ntags] = cpu_to_le16(i);
560 ntags++;
561 }
562 }
Sathya Perlab31c50a2009-09-17 10:30:13 -0700563 status = be_cmd_vlan_config(adapter, adapter->if_handle,
564 vtag, ntags, 1, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700565 } else {
Sathya Perlab31c50a2009-09-17 10:30:13 -0700566 status = be_cmd_vlan_config(adapter, adapter->if_handle,
567 NULL, 0, 1, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700568 }
Sathya Perlab31c50a2009-09-17 10:30:13 -0700569 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700570}
571
572static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
573{
574 struct be_adapter *adapter = netdev_priv(netdev);
575 struct be_eq_obj *rx_eq = &adapter->rx_eq;
576 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700577
Sathya Perla8788fdc2009-07-27 22:52:03 +0000578 be_eq_notify(adapter, rx_eq->q.id, false, false, 0);
579 be_eq_notify(adapter, tx_eq->q.id, false, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700580 adapter->vlan_grp = grp;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000581 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
582 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700583}
584
585static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
586{
587 struct be_adapter *adapter = netdev_priv(netdev);
588
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000589 if (!be_physfn(adapter))
590 return;
591
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700592 adapter->vlan_tag[vid] = 1;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000593 adapter->vlans_added++;
594 if (adapter->vlans_added <= (adapter->max_vlans + 1))
595 be_vid_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700596}
597
598static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
599{
600 struct be_adapter *adapter = netdev_priv(netdev);
601
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000602 if (!be_physfn(adapter))
603 return;
604
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700605 adapter->vlan_tag[vid] = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700606 vlan_group_set_device(adapter->vlan_grp, vid, NULL);
Ajit Khaparde82903e42010-02-09 01:34:57 +0000607 adapter->vlans_added--;
608 if (adapter->vlans_added <= adapter->max_vlans)
609 be_vid_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700610}
611
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700612static void be_set_multicast_list(struct net_device *netdev)
613{
614 struct be_adapter *adapter = netdev_priv(netdev);
615
616 if (netdev->flags & IFF_PROMISC) {
Sathya Perla8788fdc2009-07-27 22:52:03 +0000617 be_cmd_promiscuous_config(adapter, adapter->port_num, 1);
Sathya Perla24307ee2009-06-18 00:09:25 +0000618 adapter->promiscuous = true;
619 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700620 }
Sathya Perla24307ee2009-06-18 00:09:25 +0000621
622 /* BE was previously in promiscous mode; disable it */
623 if (adapter->promiscuous) {
624 adapter->promiscuous = false;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000625 be_cmd_promiscuous_config(adapter, adapter->port_num, 0);
Sathya Perla24307ee2009-06-18 00:09:25 +0000626 }
627
Sathya Perlae7b909a2009-11-22 22:01:10 +0000628 /* Enable multicast promisc if num configured exceeds what we support */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000629 if (netdev->flags & IFF_ALLMULTI ||
630 netdev_mc_count(netdev) > BE_MAX_MC) {
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000631 be_cmd_multicast_set(adapter, adapter->if_handle, NULL,
Sathya Perlae7b909a2009-11-22 22:01:10 +0000632 &adapter->mc_cmd_mem);
Sathya Perla24307ee2009-06-18 00:09:25 +0000633 goto done;
634 }
635
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000636 be_cmd_multicast_set(adapter, adapter->if_handle, netdev,
Sathya Perlaf31e50a2010-03-02 03:56:39 -0800637 &adapter->mc_cmd_mem);
Sathya Perla24307ee2009-06-18 00:09:25 +0000638done:
639 return;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700640}
641
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000642static int be_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
643{
644 struct be_adapter *adapter = netdev_priv(netdev);
645 int status;
646
647 if (!adapter->sriov_enabled)
648 return -EPERM;
649
650 if (!is_valid_ether_addr(mac) || (vf >= num_vfs))
651 return -EINVAL;
652
653 status = be_cmd_pmac_del(adapter, adapter->vf_if_handle[vf],
654 adapter->vf_pmac_id[vf]);
655
656 status = be_cmd_pmac_add(adapter, mac, adapter->vf_if_handle[vf],
657 &adapter->vf_pmac_id[vf]);
658 if (!status)
659 dev_err(&adapter->pdev->dev, "MAC %pM set on VF %d Failed\n",
660 mac, vf);
661 return status;
662}
663
Sathya Perla4097f662009-03-24 16:40:13 -0700664static void be_rx_rate_update(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700665{
Sathya Perla4097f662009-03-24 16:40:13 -0700666 struct be_drvr_stats *stats = drvr_stats(adapter);
667 ulong now = jiffies;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700668
Sathya Perla4097f662009-03-24 16:40:13 -0700669 /* Wrapped around */
670 if (time_before(now, stats->be_rx_jiffies)) {
671 stats->be_rx_jiffies = now;
672 return;
673 }
674
675 /* Update the rate once in two seconds */
676 if ((now - stats->be_rx_jiffies) < 2 * HZ)
677 return;
678
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700679 stats->be_rx_rate = be_calc_rate(stats->be_rx_bytes
680 - stats->be_rx_bytes_prev,
681 now - stats->be_rx_jiffies);
Sathya Perla4097f662009-03-24 16:40:13 -0700682 stats->be_rx_jiffies = now;
683 stats->be_rx_bytes_prev = stats->be_rx_bytes;
684}
685
686static void be_rx_stats_update(struct be_adapter *adapter,
687 u32 pktsize, u16 numfrags)
688{
689 struct be_drvr_stats *stats = drvr_stats(adapter);
690
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700691 stats->be_rx_compl++;
692 stats->be_rx_frags += numfrags;
693 stats->be_rx_bytes += pktsize;
Ajit Khaparde91992e42010-02-19 13:57:12 +0000694 stats->be_rx_pkts++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700695}
696
Ajit Khaparde728a9972009-04-13 15:41:22 -0700697static inline bool do_pkt_csum(struct be_eth_rx_compl *rxcp, bool cso)
698{
699 u8 l4_cksm, ip_version, ipcksm, tcpf = 0, udpf = 0, ipv6_chk;
700
701 l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp);
702 ipcksm = AMAP_GET_BITS(struct amap_eth_rx_compl, ipcksm, rxcp);
703 ip_version = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
704 if (ip_version) {
705 tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
706 udpf = AMAP_GET_BITS(struct amap_eth_rx_compl, udpf, rxcp);
707 }
708 ipv6_chk = (ip_version && (tcpf || udpf));
709
710 return ((l4_cksm && ipv6_chk && ipcksm) && cso) ? false : true;
711}
712
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700713static struct be_rx_page_info *
714get_rx_page_info(struct be_adapter *adapter, u16 frag_idx)
715{
716 struct be_rx_page_info *rx_page_info;
717 struct be_queue_info *rxq = &adapter->rx_obj.q;
718
719 rx_page_info = &adapter->rx_obj.page_info_tbl[frag_idx];
720 BUG_ON(!rx_page_info->page);
721
Ajit Khaparde205859a2010-02-09 01:34:21 +0000722 if (rx_page_info->last_page_user) {
FUJITA Tomonorifac6da52010-04-01 16:53:22 +0000723 pci_unmap_page(adapter->pdev, dma_unmap_addr(rx_page_info, bus),
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700724 adapter->big_page_size, PCI_DMA_FROMDEVICE);
Ajit Khaparde205859a2010-02-09 01:34:21 +0000725 rx_page_info->last_page_user = false;
726 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700727
728 atomic_dec(&rxq->used);
729 return rx_page_info;
730}
731
732/* Throwaway the data in the Rx completion */
733static void be_rx_compl_discard(struct be_adapter *adapter,
734 struct be_eth_rx_compl *rxcp)
735{
736 struct be_queue_info *rxq = &adapter->rx_obj.q;
737 struct be_rx_page_info *page_info;
738 u16 rxq_idx, i, num_rcvd;
739
740 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
741 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
742
743 for (i = 0; i < num_rcvd; i++) {
744 page_info = get_rx_page_info(adapter, rxq_idx);
745 put_page(page_info->page);
746 memset(page_info, 0, sizeof(*page_info));
747 index_inc(&rxq_idx, rxq->len);
748 }
749}
750
751/*
752 * skb_fill_rx_data forms a complete skb for an ether frame
753 * indicated by rxcp.
754 */
755static void skb_fill_rx_data(struct be_adapter *adapter,
Sathya Perla89420422010-02-17 01:35:26 +0000756 struct sk_buff *skb, struct be_eth_rx_compl *rxcp,
757 u16 num_rcvd)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700758{
759 struct be_queue_info *rxq = &adapter->rx_obj.q;
760 struct be_rx_page_info *page_info;
Sathya Perla89420422010-02-17 01:35:26 +0000761 u16 rxq_idx, i, j;
Ajit Khapardefa774062009-07-22 09:28:55 -0700762 u32 pktsize, hdr_len, curr_frag_len, size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700763 u8 *start;
764
765 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
766 pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700767
768 page_info = get_rx_page_info(adapter, rxq_idx);
769
770 start = page_address(page_info->page) + page_info->page_offset;
771 prefetch(start);
772
773 /* Copy data in the first descriptor of this completion */
774 curr_frag_len = min(pktsize, rx_frag_size);
775
776 /* Copy the header portion into skb_data */
777 hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
778 memcpy(skb->data, start, hdr_len);
779 skb->len = curr_frag_len;
780 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
781 /* Complete packet has now been moved to data */
782 put_page(page_info->page);
783 skb->data_len = 0;
784 skb->tail += curr_frag_len;
785 } else {
786 skb_shinfo(skb)->nr_frags = 1;
787 skb_shinfo(skb)->frags[0].page = page_info->page;
788 skb_shinfo(skb)->frags[0].page_offset =
789 page_info->page_offset + hdr_len;
790 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
791 skb->data_len = curr_frag_len - hdr_len;
792 skb->tail += hdr_len;
793 }
Ajit Khaparde205859a2010-02-09 01:34:21 +0000794 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700795
796 if (pktsize <= rx_frag_size) {
797 BUG_ON(num_rcvd != 1);
Sathya Perla76fbb422009-06-10 02:21:56 +0000798 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700799 }
800
801 /* More frags present for this completion */
Ajit Khapardefa774062009-07-22 09:28:55 -0700802 size = pktsize;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000803 for (i = 1, j = 0; i < num_rcvd; i++) {
Ajit Khapardefa774062009-07-22 09:28:55 -0700804 size -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700805 index_inc(&rxq_idx, rxq->len);
806 page_info = get_rx_page_info(adapter, rxq_idx);
807
Ajit Khapardefa774062009-07-22 09:28:55 -0700808 curr_frag_len = min(size, rx_frag_size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700809
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000810 /* Coalesce all frags from the same physical page in one slot */
811 if (page_info->page_offset == 0) {
812 /* Fresh page */
813 j++;
814 skb_shinfo(skb)->frags[j].page = page_info->page;
815 skb_shinfo(skb)->frags[j].page_offset =
816 page_info->page_offset;
817 skb_shinfo(skb)->frags[j].size = 0;
818 skb_shinfo(skb)->nr_frags++;
819 } else {
820 put_page(page_info->page);
821 }
822
823 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700824 skb->len += curr_frag_len;
825 skb->data_len += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700826
Ajit Khaparde205859a2010-02-09 01:34:21 +0000827 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700828 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000829 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700830
Sathya Perla76fbb422009-06-10 02:21:56 +0000831done:
Sathya Perla4097f662009-03-24 16:40:13 -0700832 be_rx_stats_update(adapter, pktsize, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700833}
834
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700835/* Process the RX completion indicated by rxcp when GRO is disabled */
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700836static void be_rx_compl_process(struct be_adapter *adapter,
837 struct be_eth_rx_compl *rxcp)
838{
839 struct sk_buff *skb;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700840 u32 vlanf, vid;
Sathya Perla89420422010-02-17 01:35:26 +0000841 u16 num_rcvd;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700842 u8 vtm;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700843
Sathya Perla89420422010-02-17 01:35:26 +0000844 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
845 /* Is it a flush compl that has no data */
846 if (unlikely(num_rcvd == 0))
847 return;
848
Eric Dumazet89d71a62009-10-13 05:34:20 +0000849 skb = netdev_alloc_skb_ip_align(adapter->netdev, BE_HDR_LEN);
Sathya Perlaa058a632010-02-17 01:34:22 +0000850 if (unlikely(!skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700851 if (net_ratelimit())
852 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
853 be_rx_compl_discard(adapter, rxcp);
854 return;
855 }
856
Sathya Perla89420422010-02-17 01:35:26 +0000857 skb_fill_rx_data(adapter, skb, rxcp, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700858
Ajit Khaparde728a9972009-04-13 15:41:22 -0700859 if (do_pkt_csum(rxcp, adapter->rx_csum))
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700860 skb->ip_summed = CHECKSUM_NONE;
Ajit Khaparde728a9972009-04-13 15:41:22 -0700861 else
862 skb->ip_summed = CHECKSUM_UNNECESSARY;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700863
864 skb->truesize = skb->len + sizeof(struct sk_buff);
865 skb->protocol = eth_type_trans(skb, adapter->netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700866
Sathya Perlaa058a632010-02-17 01:34:22 +0000867 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
868 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
869
870 /* vlanf could be wrongly set in some cards.
871 * ignore if vtm is not set */
872 if ((adapter->cap & 0x400) && !vtm)
873 vlanf = 0;
874
875 if (unlikely(vlanf)) {
Ajit Khaparde82903e42010-02-09 01:34:57 +0000876 if (!adapter->vlan_grp || adapter->vlans_added == 0) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700877 kfree_skb(skb);
878 return;
879 }
880 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
Ajit Khaparde9cae9e42010-03-31 02:00:32 +0000881 vid = swab16(vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700882 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
883 } else {
884 netif_receive_skb(skb);
885 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700886}
887
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700888/* Process the RX completion indicated by rxcp when GRO is enabled */
889static void be_rx_compl_process_gro(struct be_adapter *adapter,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700890 struct be_eth_rx_compl *rxcp)
891{
892 struct be_rx_page_info *page_info;
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700893 struct sk_buff *skb = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700894 struct be_queue_info *rxq = &adapter->rx_obj.q;
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700895 struct be_eq_obj *eq_obj = &adapter->rx_eq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700896 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000897 u16 i, rxq_idx = 0, vid, j;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700898 u8 vtm;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700899
900 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
Sathya Perla89420422010-02-17 01:35:26 +0000901 /* Is it a flush compl that has no data */
902 if (unlikely(num_rcvd == 0))
903 return;
904
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700905 pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
906 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
907 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700908 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
909
910 /* vlanf could be wrongly set in some cards.
911 * ignore if vtm is not set */
Ajit Khapardee1187b32009-12-11 22:59:09 -0800912 if ((adapter->cap & 0x400) && !vtm)
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700913 vlanf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700914
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700915 skb = napi_get_frags(&eq_obj->napi);
916 if (!skb) {
917 be_rx_compl_discard(adapter, rxcp);
918 return;
919 }
920
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700921 remaining = pkt_size;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000922 for (i = 0, j = -1; i < num_rcvd; i++) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700923 page_info = get_rx_page_info(adapter, rxq_idx);
924
925 curr_frag_len = min(remaining, rx_frag_size);
926
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000927 /* Coalesce all frags from the same physical page in one slot */
928 if (i == 0 || page_info->page_offset == 0) {
929 /* First frag or Fresh page */
930 j++;
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700931 skb_shinfo(skb)->frags[j].page = page_info->page;
932 skb_shinfo(skb)->frags[j].page_offset =
933 page_info->page_offset;
934 skb_shinfo(skb)->frags[j].size = 0;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000935 } else {
936 put_page(page_info->page);
937 }
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700938 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000939
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700940 remaining -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700941 index_inc(&rxq_idx, rxq->len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700942 memset(page_info, 0, sizeof(*page_info));
943 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000944 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700945
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700946 skb_shinfo(skb)->nr_frags = j + 1;
947 skb->len = pkt_size;
948 skb->data_len = pkt_size;
949 skb->truesize += pkt_size;
950 skb->ip_summed = CHECKSUM_UNNECESSARY;
951
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700952 if (likely(!vlanf)) {
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700953 napi_gro_frags(&eq_obj->napi);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700954 } else {
955 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
Ajit Khaparde9cae9e42010-03-31 02:00:32 +0000956 vid = swab16(vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700957
Ajit Khaparde82903e42010-02-09 01:34:57 +0000958 if (!adapter->vlan_grp || adapter->vlans_added == 0)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700959 return;
960
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700961 vlan_gro_frags(&eq_obj->napi, adapter->vlan_grp, vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700962 }
963
Sathya Perla4097f662009-03-24 16:40:13 -0700964 be_rx_stats_update(adapter, pkt_size, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700965}
966
967static struct be_eth_rx_compl *be_rx_compl_get(struct be_adapter *adapter)
968{
969 struct be_eth_rx_compl *rxcp = queue_tail_node(&adapter->rx_obj.cq);
970
971 if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
972 return NULL;
973
974 be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
975
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700976 queue_tail_inc(&adapter->rx_obj.cq);
977 return rxcp;
978}
979
Sathya Perlaa7a0ef32009-06-10 02:23:28 +0000980/* To reset the valid bit, we need to reset the whole word as
981 * when walking the queue the valid entries are little-endian
982 * and invalid entries are host endian
983 */
984static inline void be_rx_compl_reset(struct be_eth_rx_compl *rxcp)
985{
986 rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
987}
988
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700989static inline struct page *be_alloc_pages(u32 size)
990{
991 gfp_t alloc_flags = GFP_ATOMIC;
992 u32 order = get_order(size);
993 if (order > 0)
994 alloc_flags |= __GFP_COMP;
995 return alloc_pages(alloc_flags, order);
996}
997
998/*
999 * Allocate a page, split it to fragments of size rx_frag_size and post as
1000 * receive buffers to BE
1001 */
1002static void be_post_rx_frags(struct be_adapter *adapter)
1003{
1004 struct be_rx_page_info *page_info_tbl = adapter->rx_obj.page_info_tbl;
Sathya Perla26d92f92010-01-21 22:52:08 -08001005 struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001006 struct be_queue_info *rxq = &adapter->rx_obj.q;
1007 struct page *pagep = NULL;
1008 struct be_eth_rx_d *rxd;
1009 u64 page_dmaaddr = 0, frag_dmaaddr;
1010 u32 posted, page_offset = 0;
1011
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001012 page_info = &page_info_tbl[rxq->head];
1013 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
1014 if (!pagep) {
1015 pagep = be_alloc_pages(adapter->big_page_size);
1016 if (unlikely(!pagep)) {
1017 drvr_stats(adapter)->be_ethrx_post_fail++;
1018 break;
1019 }
1020 page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
1021 adapter->big_page_size,
1022 PCI_DMA_FROMDEVICE);
1023 page_info->page_offset = 0;
1024 } else {
1025 get_page(pagep);
1026 page_info->page_offset = page_offset + rx_frag_size;
1027 }
1028 page_offset = page_info->page_offset;
1029 page_info->page = pagep;
FUJITA Tomonorifac6da52010-04-01 16:53:22 +00001030 dma_unmap_addr_set(page_info, bus, page_dmaaddr);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001031 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
1032
1033 rxd = queue_head_node(rxq);
1034 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
1035 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001036
1037 /* Any space left in the current big page for another frag? */
1038 if ((page_offset + rx_frag_size + rx_frag_size) >
1039 adapter->big_page_size) {
1040 pagep = NULL;
1041 page_info->last_page_user = true;
1042 }
Sathya Perla26d92f92010-01-21 22:52:08 -08001043
1044 prev_page_info = page_info;
1045 queue_head_inc(rxq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001046 page_info = &page_info_tbl[rxq->head];
1047 }
1048 if (pagep)
Sathya Perla26d92f92010-01-21 22:52:08 -08001049 prev_page_info->last_page_user = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001050
1051 if (posted) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001052 atomic_add(posted, &rxq->used);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001053 be_rxq_notify(adapter, rxq->id, posted);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001054 } else if (atomic_read(&rxq->used) == 0) {
1055 /* Let be_worker replenish when memory is available */
1056 adapter->rx_post_starved = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001057 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001058}
1059
Sathya Perla5fb379e2009-06-18 00:02:59 +00001060static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001061{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001062 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
1063
1064 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
1065 return NULL;
1066
1067 be_dws_le_to_cpu(txcp, sizeof(*txcp));
1068
1069 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
1070
1071 queue_tail_inc(tx_cq);
1072 return txcp;
1073}
1074
1075static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
1076{
1077 struct be_queue_info *txq = &adapter->tx_obj.q;
Alexander Duycka73b7962009-12-02 16:48:18 +00001078 struct be_eth_wrb *wrb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001079 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1080 struct sk_buff *sent_skb;
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001081 u16 cur_index, num_wrbs = 1; /* account for hdr wrb */
1082 bool unmap_skb_hdr = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001083
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001084 sent_skb = sent_skbs[txq->tail];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001085 BUG_ON(!sent_skb);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001086 sent_skbs[txq->tail] = NULL;
1087
1088 /* skip header wrb */
Alexander Duycka73b7962009-12-02 16:48:18 +00001089 queue_tail_inc(txq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001090
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001091 do {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001092 cur_index = txq->tail;
Alexander Duycka73b7962009-12-02 16:48:18 +00001093 wrb = queue_tail_node(txq);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001094 unmap_tx_frag(adapter->pdev, wrb, (unmap_skb_hdr &&
Eric Dumazete743d312010-04-14 15:59:40 -07001095 skb_headlen(sent_skb)));
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001096 unmap_skb_hdr = false;
1097
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001098 num_wrbs++;
1099 queue_tail_inc(txq);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001100 } while (cur_index != last_index);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001101
1102 atomic_sub(num_wrbs, &txq->used);
Alexander Duycka73b7962009-12-02 16:48:18 +00001103
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001104 kfree_skb(sent_skb);
1105}
1106
Sathya Perla859b1e42009-08-10 03:43:51 +00001107static inline struct be_eq_entry *event_get(struct be_eq_obj *eq_obj)
1108{
1109 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1110
1111 if (!eqe->evt)
1112 return NULL;
1113
1114 eqe->evt = le32_to_cpu(eqe->evt);
1115 queue_tail_inc(&eq_obj->q);
1116 return eqe;
1117}
1118
1119static int event_handle(struct be_adapter *adapter,
1120 struct be_eq_obj *eq_obj)
1121{
1122 struct be_eq_entry *eqe;
1123 u16 num = 0;
1124
1125 while ((eqe = event_get(eq_obj)) != NULL) {
1126 eqe->evt = 0;
1127 num++;
1128 }
1129
1130 /* Deal with any spurious interrupts that come
1131 * without events
1132 */
1133 be_eq_notify(adapter, eq_obj->q.id, true, true, num);
1134 if (num)
1135 napi_schedule(&eq_obj->napi);
1136
1137 return num;
1138}
1139
1140/* Just read and notify events without processing them.
1141 * Used at the time of destroying event queues */
1142static void be_eq_clean(struct be_adapter *adapter,
1143 struct be_eq_obj *eq_obj)
1144{
1145 struct be_eq_entry *eqe;
1146 u16 num = 0;
1147
1148 while ((eqe = event_get(eq_obj)) != NULL) {
1149 eqe->evt = 0;
1150 num++;
1151 }
1152
1153 if (num)
1154 be_eq_notify(adapter, eq_obj->q.id, false, true, num);
1155}
1156
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001157static void be_rx_q_clean(struct be_adapter *adapter)
1158{
1159 struct be_rx_page_info *page_info;
1160 struct be_queue_info *rxq = &adapter->rx_obj.q;
1161 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1162 struct be_eth_rx_compl *rxcp;
1163 u16 tail;
1164
1165 /* First cleanup pending rx completions */
1166 while ((rxcp = be_rx_compl_get(adapter)) != NULL) {
1167 be_rx_compl_discard(adapter, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001168 be_rx_compl_reset(rxcp);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001169 be_cq_notify(adapter, rx_cq->id, true, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001170 }
1171
1172 /* Then free posted rx buffer that were not used */
1173 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
Sathya Perlacdab23b2009-08-10 03:43:23 +00001174 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001175 page_info = get_rx_page_info(adapter, tail);
1176 put_page(page_info->page);
1177 memset(page_info, 0, sizeof(*page_info));
1178 }
1179 BUG_ON(atomic_read(&rxq->used));
1180}
1181
Sathya Perlaa8e91792009-08-10 03:42:43 +00001182static void be_tx_compl_clean(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001183{
Sathya Perlaa8e91792009-08-10 03:42:43 +00001184 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001185 struct be_queue_info *txq = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001186 struct be_eth_tx_compl *txcp;
1187 u16 end_idx, cmpl = 0, timeo = 0;
Sathya Perlab03388d2010-02-18 00:37:17 +00001188 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1189 struct sk_buff *sent_skb;
1190 bool dummy_wrb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001191
Sathya Perlaa8e91792009-08-10 03:42:43 +00001192 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1193 do {
1194 while ((txcp = be_tx_compl_get(tx_cq))) {
1195 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1196 wrb_index, txcp);
1197 be_tx_compl_process(adapter, end_idx);
1198 cmpl++;
1199 }
1200 if (cmpl) {
1201 be_cq_notify(adapter, tx_cq->id, false, cmpl);
1202 cmpl = 0;
1203 }
1204
1205 if (atomic_read(&txq->used) == 0 || ++timeo > 200)
1206 break;
1207
1208 mdelay(1);
1209 } while (true);
1210
1211 if (atomic_read(&txq->used))
1212 dev_err(&adapter->pdev->dev, "%d pending tx-completions\n",
1213 atomic_read(&txq->used));
Sathya Perlab03388d2010-02-18 00:37:17 +00001214
1215 /* free posted tx for which compls will never arrive */
1216 while (atomic_read(&txq->used)) {
1217 sent_skb = sent_skbs[txq->tail];
1218 end_idx = txq->tail;
1219 index_adv(&end_idx,
1220 wrb_cnt_for_skb(sent_skb, &dummy_wrb) - 1, txq->len);
1221 be_tx_compl_process(adapter, end_idx);
1222 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001223}
1224
Sathya Perla5fb379e2009-06-18 00:02:59 +00001225static void be_mcc_queues_destroy(struct be_adapter *adapter)
1226{
1227 struct be_queue_info *q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001228
Sathya Perla8788fdc2009-07-27 22:52:03 +00001229 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001230 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001231 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001232 be_queue_free(adapter, q);
1233
Sathya Perla8788fdc2009-07-27 22:52:03 +00001234 q = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001235 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001236 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001237 be_queue_free(adapter, q);
1238}
1239
1240/* Must be called only after TX qs are created as MCC shares TX EQ */
1241static int be_mcc_queues_create(struct be_adapter *adapter)
1242{
1243 struct be_queue_info *q, *cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001244
1245 /* Alloc MCC compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001246 cq = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001247 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
Sathya Perlaefd2e402009-07-27 22:53:10 +00001248 sizeof(struct be_mcc_compl)))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001249 goto err;
1250
1251 /* Ask BE to create MCC compl queue; share TX's eq */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001252 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001253 goto mcc_cq_free;
1254
1255 /* Alloc MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001256 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001257 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1258 goto mcc_cq_destroy;
1259
1260 /* Ask BE to create MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001261 if (be_cmd_mccq_create(adapter, q, cq))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001262 goto mcc_q_free;
1263
1264 return 0;
1265
1266mcc_q_free:
1267 be_queue_free(adapter, q);
1268mcc_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001269 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001270mcc_cq_free:
1271 be_queue_free(adapter, cq);
1272err:
1273 return -1;
1274}
1275
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001276static void be_tx_queues_destroy(struct be_adapter *adapter)
1277{
1278 struct be_queue_info *q;
1279
1280 q = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001281 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001282 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001283 be_queue_free(adapter, q);
1284
1285 q = &adapter->tx_obj.cq;
1286 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001287 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001288 be_queue_free(adapter, q);
1289
Sathya Perla859b1e42009-08-10 03:43:51 +00001290 /* Clear any residual events */
1291 be_eq_clean(adapter, &adapter->tx_eq);
1292
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001293 q = &adapter->tx_eq.q;
1294 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001295 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001296 be_queue_free(adapter, q);
1297}
1298
1299static int be_tx_queues_create(struct be_adapter *adapter)
1300{
1301 struct be_queue_info *eq, *q, *cq;
1302
1303 adapter->tx_eq.max_eqd = 0;
1304 adapter->tx_eq.min_eqd = 0;
1305 adapter->tx_eq.cur_eqd = 96;
1306 adapter->tx_eq.enable_aic = false;
1307 /* Alloc Tx Event queue */
1308 eq = &adapter->tx_eq.q;
1309 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1310 return -1;
1311
1312 /* Ask BE to create Tx Event queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001313 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001314 goto tx_eq_free;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001315 adapter->base_eq_id = adapter->tx_eq.q.id;
1316
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001317 /* Alloc TX eth compl queue */
1318 cq = &adapter->tx_obj.cq;
1319 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1320 sizeof(struct be_eth_tx_compl)))
1321 goto tx_eq_destroy;
1322
1323 /* Ask BE to create Tx eth compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001324 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001325 goto tx_cq_free;
1326
1327 /* Alloc TX eth queue */
1328 q = &adapter->tx_obj.q;
1329 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1330 goto tx_cq_destroy;
1331
1332 /* Ask BE to create Tx eth queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001333 if (be_cmd_txq_create(adapter, q, cq))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001334 goto tx_q_free;
1335 return 0;
1336
1337tx_q_free:
1338 be_queue_free(adapter, q);
1339tx_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001340 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001341tx_cq_free:
1342 be_queue_free(adapter, cq);
1343tx_eq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001344 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001345tx_eq_free:
1346 be_queue_free(adapter, eq);
1347 return -1;
1348}
1349
1350static void be_rx_queues_destroy(struct be_adapter *adapter)
1351{
1352 struct be_queue_info *q;
1353
1354 q = &adapter->rx_obj.q;
1355 if (q->created) {
Sathya Perla8788fdc2009-07-27 22:52:03 +00001356 be_cmd_q_destroy(adapter, q, QTYPE_RXQ);
Sathya Perla89420422010-02-17 01:35:26 +00001357
1358 /* After the rxq is invalidated, wait for a grace time
1359 * of 1ms for all dma to end and the flush compl to arrive
1360 */
1361 mdelay(1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001362 be_rx_q_clean(adapter);
1363 }
1364 be_queue_free(adapter, q);
1365
1366 q = &adapter->rx_obj.cq;
1367 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001368 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001369 be_queue_free(adapter, q);
1370
Sathya Perla859b1e42009-08-10 03:43:51 +00001371 /* Clear any residual events */
1372 be_eq_clean(adapter, &adapter->rx_eq);
1373
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001374 q = &adapter->rx_eq.q;
1375 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001376 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001377 be_queue_free(adapter, q);
1378}
1379
1380static int be_rx_queues_create(struct be_adapter *adapter)
1381{
1382 struct be_queue_info *eq, *q, *cq;
1383 int rc;
1384
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001385 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
1386 adapter->rx_eq.max_eqd = BE_MAX_EQD;
1387 adapter->rx_eq.min_eqd = 0;
1388 adapter->rx_eq.cur_eqd = 0;
1389 adapter->rx_eq.enable_aic = true;
1390
1391 /* Alloc Rx Event queue */
1392 eq = &adapter->rx_eq.q;
1393 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1394 sizeof(struct be_eq_entry));
1395 if (rc)
1396 return rc;
1397
1398 /* Ask BE to create Rx Event queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001399 rc = be_cmd_eq_create(adapter, eq, adapter->rx_eq.cur_eqd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001400 if (rc)
1401 goto rx_eq_free;
1402
1403 /* Alloc RX eth compl queue */
1404 cq = &adapter->rx_obj.cq;
1405 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1406 sizeof(struct be_eth_rx_compl));
1407 if (rc)
1408 goto rx_eq_destroy;
1409
1410 /* Ask BE to create Rx eth compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001411 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001412 if (rc)
1413 goto rx_cq_free;
1414
1415 /* Alloc RX eth queue */
1416 q = &adapter->rx_obj.q;
1417 rc = be_queue_alloc(adapter, q, RX_Q_LEN, sizeof(struct be_eth_rx_d));
1418 if (rc)
1419 goto rx_cq_destroy;
1420
1421 /* Ask BE to create Rx eth queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001422 rc = be_cmd_rxq_create(adapter, q, cq->id, rx_frag_size,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001423 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle, false);
1424 if (rc)
1425 goto rx_q_free;
1426
1427 return 0;
1428rx_q_free:
1429 be_queue_free(adapter, q);
1430rx_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001431 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001432rx_cq_free:
1433 be_queue_free(adapter, cq);
1434rx_eq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001435 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001436rx_eq_free:
1437 be_queue_free(adapter, eq);
1438 return rc;
1439}
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001440
Sathya Perlab628bde2009-08-17 00:58:26 +00001441/* There are 8 evt ids per func. Retruns the evt id's bit number */
1442static inline int be_evt_bit_get(struct be_adapter *adapter, u32 eq_id)
1443{
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001444 return eq_id - adapter->base_eq_id;
Sathya Perlab628bde2009-08-17 00:58:26 +00001445}
1446
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001447static irqreturn_t be_intx(int irq, void *dev)
1448{
1449 struct be_adapter *adapter = dev;
Sathya Perla8788fdc2009-07-27 22:52:03 +00001450 int isr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001451
Sathya Perla8788fdc2009-07-27 22:52:03 +00001452 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
Sathya Perla55bdeed2010-02-02 07:48:40 -08001453 (adapter->tx_eq.q.id/ 8) * CEV_ISR_SIZE);
Sathya Perlac001c212009-07-01 01:06:07 +00001454 if (!isr)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001455 return IRQ_NONE;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001456
Sathya Perla8788fdc2009-07-27 22:52:03 +00001457 event_handle(adapter, &adapter->tx_eq);
1458 event_handle(adapter, &adapter->rx_eq);
Sathya Perlac001c212009-07-01 01:06:07 +00001459
Sathya Perla8788fdc2009-07-27 22:52:03 +00001460 return IRQ_HANDLED;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001461}
1462
1463static irqreturn_t be_msix_rx(int irq, void *dev)
1464{
1465 struct be_adapter *adapter = dev;
1466
Sathya Perla8788fdc2009-07-27 22:52:03 +00001467 event_handle(adapter, &adapter->rx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001468
1469 return IRQ_HANDLED;
1470}
1471
Sathya Perla5fb379e2009-06-18 00:02:59 +00001472static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001473{
1474 struct be_adapter *adapter = dev;
1475
Sathya Perla8788fdc2009-07-27 22:52:03 +00001476 event_handle(adapter, &adapter->tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001477
1478 return IRQ_HANDLED;
1479}
1480
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001481static inline bool do_gro(struct be_adapter *adapter,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001482 struct be_eth_rx_compl *rxcp)
1483{
1484 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1485 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1486
1487 if (err)
1488 drvr_stats(adapter)->be_rxcp_err++;
1489
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001490 return (tcp_frame && !err) ? true : false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001491}
1492
1493int be_poll_rx(struct napi_struct *napi, int budget)
1494{
1495 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
1496 struct be_adapter *adapter =
1497 container_of(rx_eq, struct be_adapter, rx_eq);
1498 struct be_queue_info *rx_cq = &adapter->rx_obj.cq;
1499 struct be_eth_rx_compl *rxcp;
1500 u32 work_done;
1501
Ajit Khapardeb7b83ac2009-11-29 17:57:22 +00001502 adapter->stats.drvr_stats.be_rx_polls++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001503 for (work_done = 0; work_done < budget; work_done++) {
1504 rxcp = be_rx_compl_get(adapter);
1505 if (!rxcp)
1506 break;
1507
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001508 if (do_gro(adapter, rxcp))
1509 be_rx_compl_process_gro(adapter, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001510 else
1511 be_rx_compl_process(adapter, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001512
1513 be_rx_compl_reset(rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001514 }
1515
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001516 /* Refill the queue */
1517 if (atomic_read(&adapter->rx_obj.q.used) < RX_FRAGS_REFILL_WM)
1518 be_post_rx_frags(adapter);
1519
1520 /* All consumed */
1521 if (work_done < budget) {
1522 napi_complete(napi);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001523 be_cq_notify(adapter, rx_cq->id, true, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001524 } else {
1525 /* More to be consumed; continue with interrupts disabled */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001526 be_cq_notify(adapter, rx_cq->id, false, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001527 }
1528 return work_done;
1529}
1530
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001531/* As TX and MCC share the same EQ check for both TX and MCC completions.
1532 * For TX/MCC we don't honour budget; consume everything
1533 */
1534static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001535{
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001536 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1537 struct be_adapter *adapter =
1538 container_of(tx_eq, struct be_adapter, tx_eq);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001539 struct be_queue_info *txq = &adapter->tx_obj.q;
1540 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001541 struct be_eth_tx_compl *txcp;
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001542 int tx_compl = 0, mcc_compl, status = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001543 u16 end_idx;
1544
Sathya Perla5fb379e2009-06-18 00:02:59 +00001545 while ((txcp = be_tx_compl_get(tx_cq))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001546 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001547 wrb_index, txcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001548 be_tx_compl_process(adapter, end_idx);
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001549 tx_compl++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001550 }
1551
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001552 mcc_compl = be_process_mcc(adapter, &status);
1553
1554 napi_complete(napi);
1555
1556 if (mcc_compl) {
1557 struct be_mcc_obj *mcc_obj = &adapter->mcc_obj;
1558 be_cq_notify(adapter, mcc_obj->cq.id, true, mcc_compl);
1559 }
1560
1561 if (tx_compl) {
1562 be_cq_notify(adapter, adapter->tx_obj.cq.id, true, tx_compl);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001563
1564 /* As Tx wrbs have been freed up, wake up netdev queue if
1565 * it was stopped due to lack of tx wrbs.
1566 */
1567 if (netif_queue_stopped(adapter->netdev) &&
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001568 atomic_read(&txq->used) < txq->len / 2) {
Sathya Perla5fb379e2009-06-18 00:02:59 +00001569 netif_wake_queue(adapter->netdev);
1570 }
1571
1572 drvr_stats(adapter)->be_tx_events++;
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001573 drvr_stats(adapter)->be_tx_compl += tx_compl;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001574 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001575
1576 return 1;
1577}
1578
Sathya Perlaea1dae12009-03-19 23:56:20 -07001579static void be_worker(struct work_struct *work)
1580{
1581 struct be_adapter *adapter =
1582 container_of(work, struct be_adapter, work.work);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001583
Sathya Perlab31c50a2009-09-17 10:30:13 -07001584 be_cmd_get_stats(adapter, &adapter->stats.cmd);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001585
1586 /* Set EQ delay */
1587 be_rx_eqd_update(adapter);
1588
Sathya Perla4097f662009-03-24 16:40:13 -07001589 be_tx_rate_update(adapter);
1590 be_rx_rate_update(adapter);
1591
Sathya Perlaea1dae12009-03-19 23:56:20 -07001592 if (adapter->rx_post_starved) {
1593 adapter->rx_post_starved = false;
1594 be_post_rx_frags(adapter);
1595 }
1596
1597 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1598}
1599
Sathya Perla8d56ff12009-11-22 22:02:26 +00001600static void be_msix_disable(struct be_adapter *adapter)
1601{
1602 if (adapter->msix_enabled) {
1603 pci_disable_msix(adapter->pdev);
1604 adapter->msix_enabled = false;
1605 }
1606}
1607
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001608static void be_msix_enable(struct be_adapter *adapter)
1609{
1610 int i, status;
1611
1612 for (i = 0; i < BE_NUM_MSIX_VECTORS; i++)
1613 adapter->msix_entries[i].entry = i;
1614
1615 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1616 BE_NUM_MSIX_VECTORS);
1617 if (status == 0)
1618 adapter->msix_enabled = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001619}
1620
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001621static void be_sriov_enable(struct be_adapter *adapter)
1622{
1623#ifdef CONFIG_PCI_IOV
1624 int status;
1625 if (be_physfn(adapter) && num_vfs) {
1626 status = pci_enable_sriov(adapter->pdev, num_vfs);
1627 adapter->sriov_enabled = status ? false : true;
1628 }
1629#endif
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001630}
1631
1632static void be_sriov_disable(struct be_adapter *adapter)
1633{
1634#ifdef CONFIG_PCI_IOV
1635 if (adapter->sriov_enabled) {
1636 pci_disable_sriov(adapter->pdev);
1637 adapter->sriov_enabled = false;
1638 }
1639#endif
1640}
1641
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001642static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1643{
Sathya Perlab628bde2009-08-17 00:58:26 +00001644 return adapter->msix_entries[
1645 be_evt_bit_get(adapter, eq_id)].vector;
1646}
1647
1648static int be_request_irq(struct be_adapter *adapter,
1649 struct be_eq_obj *eq_obj,
1650 void *handler, char *desc)
1651{
1652 struct net_device *netdev = adapter->netdev;
1653 int vec;
1654
1655 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
1656 vec = be_msix_vec_get(adapter, eq_obj->q.id);
1657 return request_irq(vec, handler, 0, eq_obj->desc, adapter);
1658}
1659
1660static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj)
1661{
1662 int vec = be_msix_vec_get(adapter, eq_obj->q.id);
1663 free_irq(vec, adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001664}
1665
1666static int be_msix_register(struct be_adapter *adapter)
1667{
Sathya Perlab628bde2009-08-17 00:58:26 +00001668 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001669
Sathya Perlab628bde2009-08-17 00:58:26 +00001670 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx");
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001671 if (status)
1672 goto err;
1673
Sathya Perlab628bde2009-08-17 00:58:26 +00001674 status = be_request_irq(adapter, &adapter->rx_eq, be_msix_rx, "rx");
1675 if (status)
1676 goto free_tx_irq;
1677
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001678 return 0;
Sathya Perlab628bde2009-08-17 00:58:26 +00001679
1680free_tx_irq:
1681 be_free_irq(adapter, &adapter->tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001682err:
1683 dev_warn(&adapter->pdev->dev,
1684 "MSIX Request IRQ failed - err %d\n", status);
1685 pci_disable_msix(adapter->pdev);
1686 adapter->msix_enabled = false;
1687 return status;
1688}
1689
1690static int be_irq_register(struct be_adapter *adapter)
1691{
1692 struct net_device *netdev = adapter->netdev;
1693 int status;
1694
1695 if (adapter->msix_enabled) {
1696 status = be_msix_register(adapter);
1697 if (status == 0)
1698 goto done;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001699 /* INTx is not supported for VF */
1700 if (!be_physfn(adapter))
1701 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001702 }
1703
1704 /* INTx */
1705 netdev->irq = adapter->pdev->irq;
1706 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1707 adapter);
1708 if (status) {
1709 dev_err(&adapter->pdev->dev,
1710 "INTx request IRQ failed - err %d\n", status);
1711 return status;
1712 }
1713done:
1714 adapter->isr_registered = true;
1715 return 0;
1716}
1717
1718static void be_irq_unregister(struct be_adapter *adapter)
1719{
1720 struct net_device *netdev = adapter->netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001721
1722 if (!adapter->isr_registered)
1723 return;
1724
1725 /* INTx */
1726 if (!adapter->msix_enabled) {
1727 free_irq(netdev->irq, adapter);
1728 goto done;
1729 }
1730
1731 /* MSIx */
Sathya Perlab628bde2009-08-17 00:58:26 +00001732 be_free_irq(adapter, &adapter->tx_eq);
1733 be_free_irq(adapter, &adapter->rx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001734done:
1735 adapter->isr_registered = false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001736}
1737
1738static int be_open(struct net_device *netdev)
1739{
1740 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001741 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1742 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perlaa8f447b2009-06-18 00:10:27 +00001743 bool link_up;
1744 int status;
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07001745 u8 mac_speed;
1746 u16 link_speed;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001747
1748 /* First time posting */
1749 be_post_rx_frags(adapter);
1750
1751 napi_enable(&rx_eq->napi);
1752 napi_enable(&tx_eq->napi);
1753
1754 be_irq_register(adapter);
1755
Sathya Perla8788fdc2009-07-27 22:52:03 +00001756 be_intr_set(adapter, true);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001757
1758 /* The evt queues are created in unarmed state; arm them */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001759 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
1760 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001761
1762 /* Rx compl queue may be in unarmed state; rearm it */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001763 be_cq_notify(adapter, adapter->rx_obj.cq.id, true, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001764
Sathya Perla7a1e9b22010-02-17 01:35:11 +00001765 /* Now that interrupts are on we can process async mcc */
1766 be_async_mcc_enable(adapter);
1767
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07001768 status = be_cmd_link_status_query(adapter, &link_up, &mac_speed,
1769 &link_speed);
Sathya Perlaa8f447b2009-06-18 00:10:27 +00001770 if (status)
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001771 goto ret_sts;
Sathya Perlaa8f447b2009-06-18 00:10:27 +00001772 be_link_status_update(adapter, link_up);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001773
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001774 if (be_physfn(adapter))
1775 status = be_vid_config(adapter);
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001776 if (status)
1777 goto ret_sts;
1778
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001779 if (be_physfn(adapter)) {
1780 status = be_cmd_set_flow_control(adapter,
1781 adapter->tx_fc, adapter->rx_fc);
1782 if (status)
1783 goto ret_sts;
1784 }
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001785
Sathya Perla5fb379e2009-06-18 00:02:59 +00001786 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001787ret_sts:
1788 return status;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001789}
1790
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00001791static int be_setup_wol(struct be_adapter *adapter, bool enable)
1792{
1793 struct be_dma_mem cmd;
1794 int status = 0;
1795 u8 mac[ETH_ALEN];
1796
1797 memset(mac, 0, ETH_ALEN);
1798
1799 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
1800 cmd.va = pci_alloc_consistent(adapter->pdev, cmd.size, &cmd.dma);
1801 if (cmd.va == NULL)
1802 return -1;
1803 memset(cmd.va, 0, cmd.size);
1804
1805 if (enable) {
1806 status = pci_write_config_dword(adapter->pdev,
1807 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
1808 if (status) {
1809 dev_err(&adapter->pdev->dev,
Frans Pop2381a552010-03-24 07:57:36 +00001810 "Could not enable Wake-on-lan\n");
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00001811 pci_free_consistent(adapter->pdev, cmd.size, cmd.va,
1812 cmd.dma);
1813 return status;
1814 }
1815 status = be_cmd_enable_magic_wol(adapter,
1816 adapter->netdev->dev_addr, &cmd);
1817 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
1818 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
1819 } else {
1820 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
1821 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
1822 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
1823 }
1824
1825 pci_free_consistent(adapter->pdev, cmd.size, cmd.va, cmd.dma);
1826 return status;
1827}
1828
Sathya Perla5fb379e2009-06-18 00:02:59 +00001829static int be_setup(struct be_adapter *adapter)
1830{
Sathya Perla5fb379e2009-06-18 00:02:59 +00001831 struct net_device *netdev = adapter->netdev;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001832 u32 cap_flags, en_flags, vf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001833 int status;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001834 u8 mac[ETH_ALEN];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001835
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001836 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST;
1837
1838 if (be_physfn(adapter)) {
1839 cap_flags |= BE_IF_FLAGS_MCAST_PROMISCUOUS |
1840 BE_IF_FLAGS_PROMISCUOUS |
1841 BE_IF_FLAGS_PASS_L3L4_ERRORS;
1842 en_flags |= BE_IF_FLAGS_PASS_L3L4_ERRORS;
1843 }
Sathya Perla73d540f2009-10-14 20:20:42 +00001844
1845 status = be_cmd_if_create(adapter, cap_flags, en_flags,
1846 netdev->dev_addr, false/* pmac_invalid */,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001847 &adapter->if_handle, &adapter->pmac_id, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001848 if (status != 0)
1849 goto do_none;
1850
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001851 if (be_physfn(adapter)) {
1852 while (vf < num_vfs) {
1853 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED
1854 | BE_IF_FLAGS_BROADCAST;
1855 status = be_cmd_if_create(adapter, cap_flags, en_flags,
1856 mac, true, &adapter->vf_if_handle[vf],
1857 NULL, vf+1);
1858 if (status) {
1859 dev_err(&adapter->pdev->dev,
1860 "Interface Create failed for VF %d\n", vf);
1861 goto if_destroy;
1862 }
1863 vf++;
Sarveshwar Bandi84e5b9f2010-05-27 16:28:15 -07001864 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001865 } else if (!be_physfn(adapter)) {
1866 status = be_cmd_mac_addr_query(adapter, mac,
1867 MAC_ADDRESS_TYPE_NETWORK, false, adapter->if_handle);
1868 if (!status) {
1869 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
1870 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
1871 }
1872 }
1873
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001874 status = be_tx_queues_create(adapter);
1875 if (status != 0)
1876 goto if_destroy;
1877
1878 status = be_rx_queues_create(adapter);
1879 if (status != 0)
1880 goto tx_qs_destroy;
1881
Sathya Perla5fb379e2009-06-18 00:02:59 +00001882 status = be_mcc_queues_create(adapter);
1883 if (status != 0)
1884 goto rx_qs_destroy;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001885
Ajit Khaparde0dffc832009-11-29 17:57:46 +00001886 adapter->link_speed = -1;
1887
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001888 return 0;
1889
Sathya Perla5fb379e2009-06-18 00:02:59 +00001890rx_qs_destroy:
1891 be_rx_queues_destroy(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001892tx_qs_destroy:
1893 be_tx_queues_destroy(adapter);
1894if_destroy:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001895 for (vf = 0; vf < num_vfs; vf++)
1896 if (adapter->vf_if_handle[vf])
1897 be_cmd_if_destroy(adapter, adapter->vf_if_handle[vf]);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001898 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001899do_none:
1900 return status;
1901}
1902
Sathya Perla5fb379e2009-06-18 00:02:59 +00001903static int be_clear(struct be_adapter *adapter)
1904{
Sathya Perla1a8887d2009-08-17 00:58:41 +00001905 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001906 be_rx_queues_destroy(adapter);
1907 be_tx_queues_destroy(adapter);
1908
Sathya Perla8788fdc2009-07-27 22:52:03 +00001909 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001910
Sathya Perla2243e2e2009-11-22 22:02:03 +00001911 /* tell fw we're done with firing cmds */
1912 be_cmd_fw_clean(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001913 return 0;
1914}
1915
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001916static int be_close(struct net_device *netdev)
1917{
1918 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001919 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1920 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1921 int vec;
1922
Sathya Perlab305be72009-06-10 02:18:35 +00001923 cancel_delayed_work_sync(&adapter->work);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001924
Sathya Perla7a1e9b22010-02-17 01:35:11 +00001925 be_async_mcc_disable(adapter);
1926
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001927 netif_stop_queue(netdev);
1928 netif_carrier_off(netdev);
Sathya Perlaa8f447b2009-06-18 00:10:27 +00001929 adapter->link_up = false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001930
Sathya Perla8788fdc2009-07-27 22:52:03 +00001931 be_intr_set(adapter, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001932
1933 if (adapter->msix_enabled) {
1934 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1935 synchronize_irq(vec);
1936 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1937 synchronize_irq(vec);
1938 } else {
1939 synchronize_irq(netdev->irq);
1940 }
1941 be_irq_unregister(adapter);
1942
1943 napi_disable(&rx_eq->napi);
1944 napi_disable(&tx_eq->napi);
1945
Sathya Perlaa8e91792009-08-10 03:42:43 +00001946 /* Wait for all pending tx completions to arrive so that
1947 * all tx skbs are freed.
1948 */
1949 be_tx_compl_clean(adapter);
1950
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001951 return 0;
1952}
1953
Ajit Khaparde84517482009-09-04 03:12:16 +00001954#define FW_FILE_HDR_SIGN "ServerEngines Corp. "
1955char flash_cookie[2][16] = {"*** SE FLAS",
1956 "H DIRECTORY *** "};
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001957
1958static bool be_flash_redboot(struct be_adapter *adapter,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001959 const u8 *p, u32 img_start, int image_size,
1960 int hdr_size)
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001961{
1962 u32 crc_offset;
1963 u8 flashed_crc[4];
1964 int status;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001965
1966 crc_offset = hdr_size + img_start + image_size - 4;
1967
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001968 p += crc_offset;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001969
1970 status = be_cmd_get_flash_crc(adapter, flashed_crc,
Ajit Khapardef510fc62010-03-31 01:47:45 +00001971 (image_size - 4));
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001972 if (status) {
1973 dev_err(&adapter->pdev->dev,
1974 "could not get crc from flash, not flashing redboot\n");
1975 return false;
1976 }
1977
1978 /*update redboot only if crc does not match*/
1979 if (!memcmp(flashed_crc, p, 4))
1980 return false;
1981 else
1982 return true;
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001983}
1984
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001985static int be_flash_data(struct be_adapter *adapter,
Ajit Khaparde84517482009-09-04 03:12:16 +00001986 const struct firmware *fw,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001987 struct be_dma_mem *flash_cmd, int num_of_images)
1988
Ajit Khaparde84517482009-09-04 03:12:16 +00001989{
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001990 int status = 0, i, filehdr_size = 0;
1991 u32 total_bytes = 0, flash_op;
Ajit Khaparde84517482009-09-04 03:12:16 +00001992 int num_bytes;
1993 const u8 *p = fw->data;
1994 struct be_cmd_write_flashrom *req = flash_cmd->va;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001995 struct flash_comp *pflashcomp;
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00001996 int num_comp;
Ajit Khaparde84517482009-09-04 03:12:16 +00001997
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00001998 struct flash_comp gen3_flash_types[9] = {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001999 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, IMG_TYPE_ISCSI_ACTIVE,
2000 FLASH_IMAGE_MAX_SIZE_g3},
2001 { FLASH_REDBOOT_START_g3, IMG_TYPE_REDBOOT,
2002 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3},
2003 { FLASH_iSCSI_BIOS_START_g3, IMG_TYPE_BIOS,
2004 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2005 { FLASH_PXE_BIOS_START_g3, IMG_TYPE_PXE_BIOS,
2006 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2007 { FLASH_FCoE_BIOS_START_g3, IMG_TYPE_FCOE_BIOS,
2008 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2009 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, IMG_TYPE_ISCSI_BACKUP,
2010 FLASH_IMAGE_MAX_SIZE_g3},
2011 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, IMG_TYPE_FCOE_FW_ACTIVE,
2012 FLASH_IMAGE_MAX_SIZE_g3},
2013 { FLASH_FCoE_BACKUP_IMAGE_START_g3, IMG_TYPE_FCOE_FW_BACKUP,
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002014 FLASH_IMAGE_MAX_SIZE_g3},
2015 { FLASH_NCSI_START_g3, IMG_TYPE_NCSI_FW,
2016 FLASH_NCSI_IMAGE_MAX_SIZE_g3}
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002017 };
2018 struct flash_comp gen2_flash_types[8] = {
2019 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, IMG_TYPE_ISCSI_ACTIVE,
2020 FLASH_IMAGE_MAX_SIZE_g2},
2021 { FLASH_REDBOOT_START_g2, IMG_TYPE_REDBOOT,
2022 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2},
2023 { FLASH_iSCSI_BIOS_START_g2, IMG_TYPE_BIOS,
2024 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2025 { FLASH_PXE_BIOS_START_g2, IMG_TYPE_PXE_BIOS,
2026 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2027 { FLASH_FCoE_BIOS_START_g2, IMG_TYPE_FCOE_BIOS,
2028 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2029 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, IMG_TYPE_ISCSI_BACKUP,
2030 FLASH_IMAGE_MAX_SIZE_g2},
2031 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, IMG_TYPE_FCOE_FW_ACTIVE,
2032 FLASH_IMAGE_MAX_SIZE_g2},
2033 { FLASH_FCoE_BACKUP_IMAGE_START_g2, IMG_TYPE_FCOE_FW_BACKUP,
2034 FLASH_IMAGE_MAX_SIZE_g2}
2035 };
2036
2037 if (adapter->generation == BE_GEN3) {
2038 pflashcomp = gen3_flash_types;
2039 filehdr_size = sizeof(struct flash_file_hdr_g3);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002040 num_comp = 9;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002041 } else {
2042 pflashcomp = gen2_flash_types;
2043 filehdr_size = sizeof(struct flash_file_hdr_g2);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002044 num_comp = 8;
Ajit Khaparde84517482009-09-04 03:12:16 +00002045 }
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002046 for (i = 0; i < num_comp; i++) {
2047 if ((pflashcomp[i].optype == IMG_TYPE_NCSI_FW) &&
2048 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
2049 continue;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002050 if ((pflashcomp[i].optype == IMG_TYPE_REDBOOT) &&
2051 (!be_flash_redboot(adapter, fw->data,
2052 pflashcomp[i].offset, pflashcomp[i].size,
2053 filehdr_size)))
2054 continue;
2055 p = fw->data;
2056 p += filehdr_size + pflashcomp[i].offset
2057 + (num_of_images * sizeof(struct image_hdr));
2058 if (p + pflashcomp[i].size > fw->data + fw->size)
Ajit Khaparde84517482009-09-04 03:12:16 +00002059 return -1;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002060 total_bytes = pflashcomp[i].size;
2061 while (total_bytes) {
2062 if (total_bytes > 32*1024)
2063 num_bytes = 32*1024;
2064 else
2065 num_bytes = total_bytes;
2066 total_bytes -= num_bytes;
Ajit Khaparde84517482009-09-04 03:12:16 +00002067
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002068 if (!total_bytes)
2069 flash_op = FLASHROM_OPER_FLASH;
2070 else
2071 flash_op = FLASHROM_OPER_SAVE;
2072 memcpy(req->params.data_buf, p, num_bytes);
2073 p += num_bytes;
2074 status = be_cmd_write_flashrom(adapter, flash_cmd,
2075 pflashcomp[i].optype, flash_op, num_bytes);
2076 if (status) {
2077 dev_err(&adapter->pdev->dev,
2078 "cmd to write to flash rom failed.\n");
2079 return -1;
2080 }
2081 yield();
Ajit Khaparde84517482009-09-04 03:12:16 +00002082 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002083 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002084 return 0;
2085}
2086
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002087static int get_ufigen_type(struct flash_file_hdr_g2 *fhdr)
2088{
2089 if (fhdr == NULL)
2090 return 0;
2091 if (fhdr->build[0] == '3')
2092 return BE_GEN3;
2093 else if (fhdr->build[0] == '2')
2094 return BE_GEN2;
2095 else
2096 return 0;
2097}
2098
Ajit Khaparde84517482009-09-04 03:12:16 +00002099int be_load_fw(struct be_adapter *adapter, u8 *func)
2100{
2101 char fw_file[ETHTOOL_FLASH_MAX_FILENAME];
2102 const struct firmware *fw;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002103 struct flash_file_hdr_g2 *fhdr;
2104 struct flash_file_hdr_g3 *fhdr3;
2105 struct image_hdr *img_hdr_ptr = NULL;
Ajit Khaparde84517482009-09-04 03:12:16 +00002106 struct be_dma_mem flash_cmd;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002107 int status, i = 0, num_imgs = 0;
Ajit Khaparde84517482009-09-04 03:12:16 +00002108 const u8 *p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002109
Ajit Khaparde84517482009-09-04 03:12:16 +00002110 strcpy(fw_file, func);
2111
2112 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
2113 if (status)
2114 goto fw_exit;
2115
2116 p = fw->data;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002117 fhdr = (struct flash_file_hdr_g2 *) p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002118 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
2119
Ajit Khaparde84517482009-09-04 03:12:16 +00002120 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
2121 flash_cmd.va = pci_alloc_consistent(adapter->pdev, flash_cmd.size,
2122 &flash_cmd.dma);
2123 if (!flash_cmd.va) {
2124 status = -ENOMEM;
2125 dev_err(&adapter->pdev->dev,
2126 "Memory allocation failure while flashing\n");
2127 goto fw_exit;
2128 }
2129
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002130 if ((adapter->generation == BE_GEN3) &&
2131 (get_ufigen_type(fhdr) == BE_GEN3)) {
2132 fhdr3 = (struct flash_file_hdr_g3 *) fw->data;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002133 num_imgs = le32_to_cpu(fhdr3->num_imgs);
2134 for (i = 0; i < num_imgs; i++) {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002135 img_hdr_ptr = (struct image_hdr *) (fw->data +
2136 (sizeof(struct flash_file_hdr_g3) +
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002137 i * sizeof(struct image_hdr)));
2138 if (le32_to_cpu(img_hdr_ptr->imageid) == 1)
2139 status = be_flash_data(adapter, fw, &flash_cmd,
2140 num_imgs);
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002141 }
2142 } else if ((adapter->generation == BE_GEN2) &&
2143 (get_ufigen_type(fhdr) == BE_GEN2)) {
2144 status = be_flash_data(adapter, fw, &flash_cmd, 0);
2145 } else {
2146 dev_err(&adapter->pdev->dev,
2147 "UFI and Interface are not compatible for flashing\n");
2148 status = -1;
Ajit Khaparde84517482009-09-04 03:12:16 +00002149 }
2150
2151 pci_free_consistent(adapter->pdev, flash_cmd.size, flash_cmd.va,
2152 flash_cmd.dma);
2153 if (status) {
2154 dev_err(&adapter->pdev->dev, "Firmware load error\n");
2155 goto fw_exit;
2156 }
2157
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002158 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
Ajit Khaparde84517482009-09-04 03:12:16 +00002159
2160fw_exit:
2161 release_firmware(fw);
2162 return status;
2163}
2164
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002165static struct net_device_ops be_netdev_ops = {
2166 .ndo_open = be_open,
2167 .ndo_stop = be_close,
2168 .ndo_start_xmit = be_xmit,
2169 .ndo_get_stats = be_get_stats,
2170 .ndo_set_rx_mode = be_set_multicast_list,
2171 .ndo_set_mac_address = be_mac_addr_set,
2172 .ndo_change_mtu = be_change_mtu,
2173 .ndo_validate_addr = eth_validate_addr,
2174 .ndo_vlan_rx_register = be_vlan_register,
2175 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
2176 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002177 .ndo_set_vf_mac = be_set_vf_mac
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002178};
2179
2180static void be_netdev_init(struct net_device *netdev)
2181{
2182 struct be_adapter *adapter = netdev_priv(netdev);
2183
2184 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
Ajit Khaparde583e3f32009-10-05 02:22:19 +00002185 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_HW_CSUM |
2186 NETIF_F_GRO;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002187
Ajit Khaparde51c59872009-11-29 17:54:54 +00002188 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2189
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002190 netdev->flags |= IFF_MULTICAST;
2191
Ajit Khaparde728a9972009-04-13 15:41:22 -07002192 adapter->rx_csum = true;
2193
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002194 /* Default settings for Rx and Tx flow control */
2195 adapter->rx_fc = true;
2196 adapter->tx_fc = true;
2197
Ajit Khapardec190e3c2009-09-04 03:12:29 +00002198 netif_set_gso_max_size(netdev, 65535);
2199
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002200 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
2201
2202 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
2203
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002204 netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx,
2205 BE_NAPI_WEIGHT);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002206 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002207 BE_NAPI_WEIGHT);
2208
2209 netif_carrier_off(netdev);
2210 netif_stop_queue(netdev);
2211}
2212
2213static void be_unmap_pci_bars(struct be_adapter *adapter)
2214{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002215 if (adapter->csr)
2216 iounmap(adapter->csr);
2217 if (adapter->db)
2218 iounmap(adapter->db);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002219 if (adapter->pcicfg && be_physfn(adapter))
Sathya Perla8788fdc2009-07-27 22:52:03 +00002220 iounmap(adapter->pcicfg);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002221}
2222
2223static int be_map_pci_bars(struct be_adapter *adapter)
2224{
2225 u8 __iomem *addr;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002226 int pcicfg_reg, db_reg;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002227
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002228 if (be_physfn(adapter)) {
2229 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
2230 pci_resource_len(adapter->pdev, 2));
2231 if (addr == NULL)
2232 return -ENOMEM;
2233 adapter->csr = addr;
2234 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002235
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002236 if (adapter->generation == BE_GEN2) {
2237 pcicfg_reg = 1;
2238 db_reg = 4;
2239 } else {
2240 pcicfg_reg = 0;
2241 if (be_physfn(adapter))
2242 db_reg = 4;
2243 else
2244 db_reg = 0;
2245 }
2246 addr = ioremap_nocache(pci_resource_start(adapter->pdev, db_reg),
2247 pci_resource_len(adapter->pdev, db_reg));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002248 if (addr == NULL)
2249 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00002250 adapter->db = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002251
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002252 if (be_physfn(adapter)) {
2253 addr = ioremap_nocache(
2254 pci_resource_start(adapter->pdev, pcicfg_reg),
2255 pci_resource_len(adapter->pdev, pcicfg_reg));
2256 if (addr == NULL)
2257 goto pci_map_err;
2258 adapter->pcicfg = addr;
2259 } else
2260 adapter->pcicfg = adapter->db + SRIOV_VF_PCICFG_OFFSET;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002261
2262 return 0;
2263pci_map_err:
2264 be_unmap_pci_bars(adapter);
2265 return -ENOMEM;
2266}
2267
2268
2269static void be_ctrl_cleanup(struct be_adapter *adapter)
2270{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002271 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002272
2273 be_unmap_pci_bars(adapter);
2274
2275 if (mem->va)
2276 pci_free_consistent(adapter->pdev, mem->size,
2277 mem->va, mem->dma);
Sathya Perlae7b909a2009-11-22 22:01:10 +00002278
2279 mem = &adapter->mc_cmd_mem;
2280 if (mem->va)
2281 pci_free_consistent(adapter->pdev, mem->size,
2282 mem->va, mem->dma);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002283}
2284
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002285static int be_ctrl_init(struct be_adapter *adapter)
2286{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002287 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
2288 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002289 struct be_dma_mem *mc_cmd_mem = &adapter->mc_cmd_mem;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002290 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002291
2292 status = be_map_pci_bars(adapter);
2293 if (status)
Sathya Perlae7b909a2009-11-22 22:01:10 +00002294 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002295
2296 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
2297 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
2298 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
2299 if (!mbox_mem_alloc->va) {
Sathya Perlae7b909a2009-11-22 22:01:10 +00002300 status = -ENOMEM;
2301 goto unmap_pci_bars;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002302 }
Sathya Perlae7b909a2009-11-22 22:01:10 +00002303
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002304 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
2305 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
2306 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
2307 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Sathya Perlae7b909a2009-11-22 22:01:10 +00002308
2309 mc_cmd_mem->size = sizeof(struct be_cmd_req_mcast_mac_config);
2310 mc_cmd_mem->va = pci_alloc_consistent(adapter->pdev, mc_cmd_mem->size,
2311 &mc_cmd_mem->dma);
2312 if (mc_cmd_mem->va == NULL) {
2313 status = -ENOMEM;
2314 goto free_mbox;
2315 }
2316 memset(mc_cmd_mem->va, 0, mc_cmd_mem->size);
2317
Sathya Perla8788fdc2009-07-27 22:52:03 +00002318 spin_lock_init(&adapter->mbox_lock);
2319 spin_lock_init(&adapter->mcc_lock);
2320 spin_lock_init(&adapter->mcc_cq_lock);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002321
Sarveshwar Bandidd131e72010-05-25 16:16:32 -07002322 init_completion(&adapter->flash_compl);
Sathya Perlacf588472010-02-14 21:22:01 +00002323 pci_save_state(adapter->pdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002324 return 0;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002325
2326free_mbox:
2327 pci_free_consistent(adapter->pdev, mbox_mem_alloc->size,
2328 mbox_mem_alloc->va, mbox_mem_alloc->dma);
2329
2330unmap_pci_bars:
2331 be_unmap_pci_bars(adapter);
2332
2333done:
2334 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002335}
2336
2337static void be_stats_cleanup(struct be_adapter *adapter)
2338{
2339 struct be_stats_obj *stats = &adapter->stats;
2340 struct be_dma_mem *cmd = &stats->cmd;
2341
2342 if (cmd->va)
2343 pci_free_consistent(adapter->pdev, cmd->size,
2344 cmd->va, cmd->dma);
2345}
2346
2347static int be_stats_init(struct be_adapter *adapter)
2348{
2349 struct be_stats_obj *stats = &adapter->stats;
2350 struct be_dma_mem *cmd = &stats->cmd;
2351
2352 cmd->size = sizeof(struct be_cmd_req_get_stats);
2353 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
2354 if (cmd->va == NULL)
2355 return -1;
David S. Millerd291b9a2010-01-28 21:36:21 -08002356 memset(cmd->va, 0, cmd->size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002357 return 0;
2358}
2359
2360static void __devexit be_remove(struct pci_dev *pdev)
2361{
2362 struct be_adapter *adapter = pci_get_drvdata(pdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002363
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002364 if (!adapter)
2365 return;
2366
2367 unregister_netdev(adapter->netdev);
2368
Sathya Perla5fb379e2009-06-18 00:02:59 +00002369 be_clear(adapter);
2370
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002371 be_stats_cleanup(adapter);
2372
2373 be_ctrl_cleanup(adapter);
2374
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002375 be_sriov_disable(adapter);
2376
Sathya Perla8d56ff12009-11-22 22:02:26 +00002377 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002378
2379 pci_set_drvdata(pdev, NULL);
2380 pci_release_regions(pdev);
2381 pci_disable_device(pdev);
2382
2383 free_netdev(adapter->netdev);
2384}
2385
Sathya Perla2243e2e2009-11-22 22:02:03 +00002386static int be_get_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002387{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002388 int status;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002389 u8 mac[ETH_ALEN];
Sathya Perla43a04fdc2009-10-14 20:21:17 +00002390
Sathya Perla8788fdc2009-07-27 22:52:03 +00002391 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002392 if (status)
2393 return status;
2394
Ajit Khapardedcb9b562009-09-30 21:58:22 -07002395 status = be_cmd_query_fw_cfg(adapter,
2396 &adapter->port_num, &adapter->cap);
Sathya Perla2243e2e2009-11-22 22:02:03 +00002397 if (status)
2398 return status;
2399
2400 memset(mac, 0, ETH_ALEN);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002401
2402 if (be_physfn(adapter)) {
2403 status = be_cmd_mac_addr_query(adapter, mac,
Sathya Perla2243e2e2009-11-22 22:02:03 +00002404 MAC_ADDRESS_TYPE_NETWORK, true /*permanent */, 0);
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002405
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002406 if (status)
2407 return status;
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002408
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002409 if (!is_valid_ether_addr(mac))
2410 return -EADDRNOTAVAIL;
2411
2412 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2413 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2414 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002415
Ajit Khaparde82903e42010-02-09 01:34:57 +00002416 if (adapter->cap & 0x400)
2417 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED/4;
2418 else
2419 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED;
2420
Sathya Perla2243e2e2009-11-22 22:02:03 +00002421 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002422}
2423
2424static int __devinit be_probe(struct pci_dev *pdev,
2425 const struct pci_device_id *pdev_id)
2426{
2427 int status = 0;
2428 struct be_adapter *adapter;
2429 struct net_device *netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002430
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002431
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002432 status = pci_enable_device(pdev);
2433 if (status)
2434 goto do_none;
2435
2436 status = pci_request_regions(pdev, DRV_NAME);
2437 if (status)
2438 goto disable_dev;
2439 pci_set_master(pdev);
2440
2441 netdev = alloc_etherdev(sizeof(struct be_adapter));
2442 if (netdev == NULL) {
2443 status = -ENOMEM;
2444 goto rel_reg;
2445 }
2446 adapter = netdev_priv(netdev);
Ajit Khaparde7b139c82010-01-27 21:56:44 +00002447
2448 switch (pdev->device) {
2449 case BE_DEVICE_ID1:
2450 case OC_DEVICE_ID1:
2451 adapter->generation = BE_GEN2;
2452 break;
2453 case BE_DEVICE_ID2:
2454 case OC_DEVICE_ID2:
2455 adapter->generation = BE_GEN3;
2456 break;
2457 default:
2458 adapter->generation = 0;
2459 }
2460
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002461 adapter->pdev = pdev;
2462 pci_set_drvdata(pdev, adapter);
2463 adapter->netdev = netdev;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002464 be_netdev_init(netdev);
2465 SET_NETDEV_DEV(netdev, &pdev->dev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002466
2467 be_msix_enable(adapter);
2468
Yang Hongyange9304382009-04-13 14:40:14 -07002469 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002470 if (!status) {
2471 netdev->features |= NETIF_F_HIGHDMA;
2472 } else {
Yang Hongyange9304382009-04-13 14:40:14 -07002473 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002474 if (status) {
2475 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
2476 goto free_netdev;
2477 }
2478 }
2479
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002480 be_sriov_enable(adapter);
2481
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002482 status = be_ctrl_init(adapter);
2483 if (status)
2484 goto free_netdev;
2485
Sathya Perla2243e2e2009-11-22 22:02:03 +00002486 /* sync up with fw's ready state */
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002487 if (be_physfn(adapter)) {
2488 status = be_cmd_POST(adapter);
2489 if (status)
2490 goto ctrl_clean;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002491 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002492
2493 /* tell fw we're ready to fire cmds */
2494 status = be_cmd_fw_init(adapter);
2495 if (status)
2496 goto ctrl_clean;
2497
Sarveshwar Bandi556ae192010-05-24 18:38:25 -07002498 if (be_physfn(adapter)) {
2499 status = be_cmd_reset_function(adapter);
2500 if (status)
2501 goto ctrl_clean;
2502 }
2503
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002504 status = be_stats_init(adapter);
2505 if (status)
2506 goto ctrl_clean;
2507
Sathya Perla2243e2e2009-11-22 22:02:03 +00002508 status = be_get_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002509 if (status)
2510 goto stats_clean;
2511
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002512 INIT_DELAYED_WORK(&adapter->work, be_worker);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002513
Sathya Perla5fb379e2009-06-18 00:02:59 +00002514 status = be_setup(adapter);
2515 if (status)
2516 goto stats_clean;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002517
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002518 status = register_netdev(netdev);
2519 if (status != 0)
Sathya Perla5fb379e2009-06-18 00:02:59 +00002520 goto unsetup;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002521
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002522 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002523 return 0;
2524
Sathya Perla5fb379e2009-06-18 00:02:59 +00002525unsetup:
2526 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002527stats_clean:
2528 be_stats_cleanup(adapter);
2529ctrl_clean:
2530 be_ctrl_cleanup(adapter);
2531free_netdev:
Sathya Perla8d56ff12009-11-22 22:02:26 +00002532 be_msix_disable(adapter);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002533 be_sriov_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002534 free_netdev(adapter->netdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002535 pci_set_drvdata(pdev, NULL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002536rel_reg:
2537 pci_release_regions(pdev);
2538disable_dev:
2539 pci_disable_device(pdev);
2540do_none:
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002541 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002542 return status;
2543}
2544
2545static int be_suspend(struct pci_dev *pdev, pm_message_t state)
2546{
2547 struct be_adapter *adapter = pci_get_drvdata(pdev);
2548 struct net_device *netdev = adapter->netdev;
2549
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002550 if (adapter->wol)
2551 be_setup_wol(adapter, true);
2552
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002553 netif_device_detach(netdev);
2554 if (netif_running(netdev)) {
2555 rtnl_lock();
2556 be_close(netdev);
2557 rtnl_unlock();
2558 }
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002559 be_cmd_get_flow_control(adapter, &adapter->tx_fc, &adapter->rx_fc);
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002560 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002561
2562 pci_save_state(pdev);
2563 pci_disable_device(pdev);
2564 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2565 return 0;
2566}
2567
2568static int be_resume(struct pci_dev *pdev)
2569{
2570 int status = 0;
2571 struct be_adapter *adapter = pci_get_drvdata(pdev);
2572 struct net_device *netdev = adapter->netdev;
2573
2574 netif_device_detach(netdev);
2575
2576 status = pci_enable_device(pdev);
2577 if (status)
2578 return status;
2579
2580 pci_set_power_state(pdev, 0);
2581 pci_restore_state(pdev);
2582
Sathya Perla2243e2e2009-11-22 22:02:03 +00002583 /* tell fw we're ready to fire cmds */
2584 status = be_cmd_fw_init(adapter);
2585 if (status)
2586 return status;
2587
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002588 be_setup(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002589 if (netif_running(netdev)) {
2590 rtnl_lock();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002591 be_open(netdev);
2592 rtnl_unlock();
2593 }
2594 netif_device_attach(netdev);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002595
2596 if (adapter->wol)
2597 be_setup_wol(adapter, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002598 return 0;
2599}
2600
Sathya Perla82456b02010-02-17 01:35:37 +00002601/*
2602 * An FLR will stop BE from DMAing any data.
2603 */
2604static void be_shutdown(struct pci_dev *pdev)
2605{
2606 struct be_adapter *adapter = pci_get_drvdata(pdev);
2607 struct net_device *netdev = adapter->netdev;
2608
2609 netif_device_detach(netdev);
2610
2611 be_cmd_reset_function(adapter);
2612
2613 if (adapter->wol)
2614 be_setup_wol(adapter, true);
2615
2616 pci_disable_device(pdev);
Sathya Perla82456b02010-02-17 01:35:37 +00002617}
2618
Sathya Perlacf588472010-02-14 21:22:01 +00002619static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
2620 pci_channel_state_t state)
2621{
2622 struct be_adapter *adapter = pci_get_drvdata(pdev);
2623 struct net_device *netdev = adapter->netdev;
2624
2625 dev_err(&adapter->pdev->dev, "EEH error detected\n");
2626
2627 adapter->eeh_err = true;
2628
2629 netif_device_detach(netdev);
2630
2631 if (netif_running(netdev)) {
2632 rtnl_lock();
2633 be_close(netdev);
2634 rtnl_unlock();
2635 }
2636 be_clear(adapter);
2637
2638 if (state == pci_channel_io_perm_failure)
2639 return PCI_ERS_RESULT_DISCONNECT;
2640
2641 pci_disable_device(pdev);
2642
2643 return PCI_ERS_RESULT_NEED_RESET;
2644}
2645
2646static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
2647{
2648 struct be_adapter *adapter = pci_get_drvdata(pdev);
2649 int status;
2650
2651 dev_info(&adapter->pdev->dev, "EEH reset\n");
2652 adapter->eeh_err = false;
2653
2654 status = pci_enable_device(pdev);
2655 if (status)
2656 return PCI_ERS_RESULT_DISCONNECT;
2657
2658 pci_set_master(pdev);
2659 pci_set_power_state(pdev, 0);
2660 pci_restore_state(pdev);
2661
2662 /* Check if card is ok and fw is ready */
2663 status = be_cmd_POST(adapter);
2664 if (status)
2665 return PCI_ERS_RESULT_DISCONNECT;
2666
2667 return PCI_ERS_RESULT_RECOVERED;
2668}
2669
2670static void be_eeh_resume(struct pci_dev *pdev)
2671{
2672 int status = 0;
2673 struct be_adapter *adapter = pci_get_drvdata(pdev);
2674 struct net_device *netdev = adapter->netdev;
2675
2676 dev_info(&adapter->pdev->dev, "EEH resume\n");
2677
2678 pci_save_state(pdev);
2679
2680 /* tell fw we're ready to fire cmds */
2681 status = be_cmd_fw_init(adapter);
2682 if (status)
2683 goto err;
2684
2685 status = be_setup(adapter);
2686 if (status)
2687 goto err;
2688
2689 if (netif_running(netdev)) {
2690 status = be_open(netdev);
2691 if (status)
2692 goto err;
2693 }
2694 netif_device_attach(netdev);
2695 return;
2696err:
2697 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
Sathya Perlacf588472010-02-14 21:22:01 +00002698}
2699
2700static struct pci_error_handlers be_eeh_handlers = {
2701 .error_detected = be_eeh_err_detected,
2702 .slot_reset = be_eeh_reset,
2703 .resume = be_eeh_resume,
2704};
2705
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002706static struct pci_driver be_driver = {
2707 .name = DRV_NAME,
2708 .id_table = be_dev_ids,
2709 .probe = be_probe,
2710 .remove = be_remove,
2711 .suspend = be_suspend,
Sathya Perlacf588472010-02-14 21:22:01 +00002712 .resume = be_resume,
Sathya Perla82456b02010-02-17 01:35:37 +00002713 .shutdown = be_shutdown,
Sathya Perlacf588472010-02-14 21:22:01 +00002714 .err_handler = &be_eeh_handlers
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002715};
2716
2717static int __init be_init_module(void)
2718{
Joe Perches8e95a202009-12-03 07:58:21 +00002719 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
2720 rx_frag_size != 2048) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002721 printk(KERN_WARNING DRV_NAME
2722 " : Module param rx_frag_size must be 2048/4096/8192."
2723 " Using 2048\n");
2724 rx_frag_size = 2048;
2725 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002726
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002727 if (num_vfs > 32) {
2728 printk(KERN_WARNING DRV_NAME
2729 " : Module param num_vfs must not be greater than 32."
2730 "Using 32\n");
2731 num_vfs = 32;
2732 }
2733
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002734 return pci_register_driver(&be_driver);
2735}
2736module_init(be_init_module);
2737
2738static void __exit be_exit_module(void)
2739{
2740 pci_unregister_driver(&be_driver);
2741}
2742module_exit(be_exit_module);