blob: 3225774698520506bdcb55d55b360a4f750a8577 [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 Perlaa8f447bd2009-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 Perlaa8f447bd2009-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 Perlaa8f447bd2009-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 Perlaa8f447bd2009-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
Sathya Perla889cd4b2010-05-30 23:33:45 +00001738static int be_close(struct net_device *netdev)
1739{
1740 struct be_adapter *adapter = netdev_priv(netdev);
1741 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1742 struct be_eq_obj *tx_eq = &adapter->tx_eq;
1743 int vec;
1744
1745 cancel_delayed_work_sync(&adapter->work);
1746
1747 be_async_mcc_disable(adapter);
1748
1749 netif_stop_queue(netdev);
1750 netif_carrier_off(netdev);
1751 adapter->link_up = false;
1752
1753 be_intr_set(adapter, false);
1754
1755 if (adapter->msix_enabled) {
1756 vec = be_msix_vec_get(adapter, tx_eq->q.id);
1757 synchronize_irq(vec);
1758 vec = be_msix_vec_get(adapter, rx_eq->q.id);
1759 synchronize_irq(vec);
1760 } else {
1761 synchronize_irq(netdev->irq);
1762 }
1763 be_irq_unregister(adapter);
1764
1765 napi_disable(&rx_eq->napi);
1766 napi_disable(&tx_eq->napi);
1767
1768 /* Wait for all pending tx completions to arrive so that
1769 * all tx skbs are freed.
1770 */
1771 be_tx_compl_clean(adapter);
1772
1773 return 0;
1774}
1775
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001776static int be_open(struct net_device *netdev)
1777{
1778 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001779 struct be_eq_obj *rx_eq = &adapter->rx_eq;
1780 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001781 bool link_up;
1782 int status;
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07001783 u8 mac_speed;
1784 u16 link_speed;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001785
1786 /* First time posting */
1787 be_post_rx_frags(adapter);
1788
1789 napi_enable(&rx_eq->napi);
1790 napi_enable(&tx_eq->napi);
1791
1792 be_irq_register(adapter);
1793
Sathya Perla8788fdc2009-07-27 22:52:03 +00001794 be_intr_set(adapter, true);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001795
1796 /* The evt queues are created in unarmed state; arm them */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001797 be_eq_notify(adapter, rx_eq->q.id, true, false, 0);
1798 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001799
1800 /* Rx compl queue may be in unarmed state; rearm it */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001801 be_cq_notify(adapter, adapter->rx_obj.cq.id, true, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001802
Sathya Perla7a1e9b22010-02-17 01:35:11 +00001803 /* Now that interrupts are on we can process async mcc */
1804 be_async_mcc_enable(adapter);
1805
Sathya Perla889cd4b2010-05-30 23:33:45 +00001806 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
1807
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07001808 status = be_cmd_link_status_query(adapter, &link_up, &mac_speed,
1809 &link_speed);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001810 if (status)
Sathya Perla889cd4b2010-05-30 23:33:45 +00001811 goto err;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00001812 be_link_status_update(adapter, link_up);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001813
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001814 if (be_physfn(adapter)) {
Sathya Perla889cd4b2010-05-30 23:33:45 +00001815 status = be_vid_config(adapter);
1816 if (status)
1817 goto err;
1818
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001819 status = be_cmd_set_flow_control(adapter,
1820 adapter->tx_fc, adapter->rx_fc);
1821 if (status)
Sathya Perla889cd4b2010-05-30 23:33:45 +00001822 goto err;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001823 }
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00001824
Sathya Perla889cd4b2010-05-30 23:33:45 +00001825 return 0;
1826err:
1827 be_close(adapter->netdev);
1828 return -EIO;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001829}
1830
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00001831static int be_setup_wol(struct be_adapter *adapter, bool enable)
1832{
1833 struct be_dma_mem cmd;
1834 int status = 0;
1835 u8 mac[ETH_ALEN];
1836
1837 memset(mac, 0, ETH_ALEN);
1838
1839 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
1840 cmd.va = pci_alloc_consistent(adapter->pdev, cmd.size, &cmd.dma);
1841 if (cmd.va == NULL)
1842 return -1;
1843 memset(cmd.va, 0, cmd.size);
1844
1845 if (enable) {
1846 status = pci_write_config_dword(adapter->pdev,
1847 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
1848 if (status) {
1849 dev_err(&adapter->pdev->dev,
Frans Pop2381a552010-03-24 07:57:36 +00001850 "Could not enable Wake-on-lan\n");
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00001851 pci_free_consistent(adapter->pdev, cmd.size, cmd.va,
1852 cmd.dma);
1853 return status;
1854 }
1855 status = be_cmd_enable_magic_wol(adapter,
1856 adapter->netdev->dev_addr, &cmd);
1857 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
1858 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
1859 } else {
1860 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
1861 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
1862 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
1863 }
1864
1865 pci_free_consistent(adapter->pdev, cmd.size, cmd.va, cmd.dma);
1866 return status;
1867}
1868
Sathya Perla5fb379e2009-06-18 00:02:59 +00001869static int be_setup(struct be_adapter *adapter)
1870{
Sathya Perla5fb379e2009-06-18 00:02:59 +00001871 struct net_device *netdev = adapter->netdev;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001872 u32 cap_flags, en_flags, vf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001873 int status;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001874 u8 mac[ETH_ALEN];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001875
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001876 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST;
1877
1878 if (be_physfn(adapter)) {
1879 cap_flags |= BE_IF_FLAGS_MCAST_PROMISCUOUS |
1880 BE_IF_FLAGS_PROMISCUOUS |
1881 BE_IF_FLAGS_PASS_L3L4_ERRORS;
1882 en_flags |= BE_IF_FLAGS_PASS_L3L4_ERRORS;
1883 }
Sathya Perla73d540f2009-10-14 20:20:42 +00001884
1885 status = be_cmd_if_create(adapter, cap_flags, en_flags,
1886 netdev->dev_addr, false/* pmac_invalid */,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001887 &adapter->if_handle, &adapter->pmac_id, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001888 if (status != 0)
1889 goto do_none;
1890
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001891 if (be_physfn(adapter)) {
1892 while (vf < num_vfs) {
1893 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED
1894 | BE_IF_FLAGS_BROADCAST;
1895 status = be_cmd_if_create(adapter, cap_flags, en_flags,
1896 mac, true, &adapter->vf_if_handle[vf],
1897 NULL, vf+1);
1898 if (status) {
1899 dev_err(&adapter->pdev->dev,
1900 "Interface Create failed for VF %d\n", vf);
1901 goto if_destroy;
1902 }
1903 vf++;
Sarveshwar Bandi84e5b9f2010-05-27 16:28:15 -07001904 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001905 } else if (!be_physfn(adapter)) {
1906 status = be_cmd_mac_addr_query(adapter, mac,
1907 MAC_ADDRESS_TYPE_NETWORK, false, adapter->if_handle);
1908 if (!status) {
1909 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
1910 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
1911 }
1912 }
1913
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001914 status = be_tx_queues_create(adapter);
1915 if (status != 0)
1916 goto if_destroy;
1917
1918 status = be_rx_queues_create(adapter);
1919 if (status != 0)
1920 goto tx_qs_destroy;
1921
Sathya Perla5fb379e2009-06-18 00:02:59 +00001922 status = be_mcc_queues_create(adapter);
1923 if (status != 0)
1924 goto rx_qs_destroy;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001925
Ajit Khaparde0dffc832009-11-29 17:57:46 +00001926 adapter->link_speed = -1;
1927
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001928 return 0;
1929
Sathya Perla5fb379e2009-06-18 00:02:59 +00001930rx_qs_destroy:
1931 be_rx_queues_destroy(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001932tx_qs_destroy:
1933 be_tx_queues_destroy(adapter);
1934if_destroy:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001935 for (vf = 0; vf < num_vfs; vf++)
1936 if (adapter->vf_if_handle[vf])
1937 be_cmd_if_destroy(adapter, adapter->vf_if_handle[vf]);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001938 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001939do_none:
1940 return status;
1941}
1942
Sathya Perla5fb379e2009-06-18 00:02:59 +00001943static int be_clear(struct be_adapter *adapter)
1944{
Sathya Perla1a8887d2009-08-17 00:58:41 +00001945 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001946 be_rx_queues_destroy(adapter);
1947 be_tx_queues_destroy(adapter);
1948
Sathya Perla8788fdc2009-07-27 22:52:03 +00001949 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001950
Sathya Perla2243e2e2009-11-22 22:02:03 +00001951 /* tell fw we're done with firing cmds */
1952 be_cmd_fw_clean(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001953 return 0;
1954}
1955
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001956
Ajit Khaparde84517482009-09-04 03:12:16 +00001957#define FW_FILE_HDR_SIGN "ServerEngines Corp. "
1958char flash_cookie[2][16] = {"*** SE FLAS",
1959 "H DIRECTORY *** "};
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001960
1961static bool be_flash_redboot(struct be_adapter *adapter,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001962 const u8 *p, u32 img_start, int image_size,
1963 int hdr_size)
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001964{
1965 u32 crc_offset;
1966 u8 flashed_crc[4];
1967 int status;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001968
1969 crc_offset = hdr_size + img_start + image_size - 4;
1970
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001971 p += crc_offset;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001972
1973 status = be_cmd_get_flash_crc(adapter, flashed_crc,
Ajit Khapardef510fc62010-03-31 01:47:45 +00001974 (image_size - 4));
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001975 if (status) {
1976 dev_err(&adapter->pdev->dev,
1977 "could not get crc from flash, not flashing redboot\n");
1978 return false;
1979 }
1980
1981 /*update redboot only if crc does not match*/
1982 if (!memcmp(flashed_crc, p, 4))
1983 return false;
1984 else
1985 return true;
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08001986}
1987
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001988static int be_flash_data(struct be_adapter *adapter,
Ajit Khaparde84517482009-09-04 03:12:16 +00001989 const struct firmware *fw,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001990 struct be_dma_mem *flash_cmd, int num_of_images)
1991
Ajit Khaparde84517482009-09-04 03:12:16 +00001992{
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001993 int status = 0, i, filehdr_size = 0;
1994 u32 total_bytes = 0, flash_op;
Ajit Khaparde84517482009-09-04 03:12:16 +00001995 int num_bytes;
1996 const u8 *p = fw->data;
1997 struct be_cmd_write_flashrom *req = flash_cmd->va;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00001998 struct flash_comp *pflashcomp;
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00001999 int num_comp;
Ajit Khaparde84517482009-09-04 03:12:16 +00002000
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002001 struct flash_comp gen3_flash_types[9] = {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002002 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, IMG_TYPE_ISCSI_ACTIVE,
2003 FLASH_IMAGE_MAX_SIZE_g3},
2004 { FLASH_REDBOOT_START_g3, IMG_TYPE_REDBOOT,
2005 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3},
2006 { FLASH_iSCSI_BIOS_START_g3, IMG_TYPE_BIOS,
2007 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2008 { FLASH_PXE_BIOS_START_g3, IMG_TYPE_PXE_BIOS,
2009 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2010 { FLASH_FCoE_BIOS_START_g3, IMG_TYPE_FCOE_BIOS,
2011 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2012 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, IMG_TYPE_ISCSI_BACKUP,
2013 FLASH_IMAGE_MAX_SIZE_g3},
2014 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, IMG_TYPE_FCOE_FW_ACTIVE,
2015 FLASH_IMAGE_MAX_SIZE_g3},
2016 { FLASH_FCoE_BACKUP_IMAGE_START_g3, IMG_TYPE_FCOE_FW_BACKUP,
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002017 FLASH_IMAGE_MAX_SIZE_g3},
2018 { FLASH_NCSI_START_g3, IMG_TYPE_NCSI_FW,
2019 FLASH_NCSI_IMAGE_MAX_SIZE_g3}
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002020 };
2021 struct flash_comp gen2_flash_types[8] = {
2022 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, IMG_TYPE_ISCSI_ACTIVE,
2023 FLASH_IMAGE_MAX_SIZE_g2},
2024 { FLASH_REDBOOT_START_g2, IMG_TYPE_REDBOOT,
2025 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2},
2026 { FLASH_iSCSI_BIOS_START_g2, IMG_TYPE_BIOS,
2027 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2028 { FLASH_PXE_BIOS_START_g2, IMG_TYPE_PXE_BIOS,
2029 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2030 { FLASH_FCoE_BIOS_START_g2, IMG_TYPE_FCOE_BIOS,
2031 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2032 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, IMG_TYPE_ISCSI_BACKUP,
2033 FLASH_IMAGE_MAX_SIZE_g2},
2034 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, IMG_TYPE_FCOE_FW_ACTIVE,
2035 FLASH_IMAGE_MAX_SIZE_g2},
2036 { FLASH_FCoE_BACKUP_IMAGE_START_g2, IMG_TYPE_FCOE_FW_BACKUP,
2037 FLASH_IMAGE_MAX_SIZE_g2}
2038 };
2039
2040 if (adapter->generation == BE_GEN3) {
2041 pflashcomp = gen3_flash_types;
2042 filehdr_size = sizeof(struct flash_file_hdr_g3);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002043 num_comp = 9;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002044 } else {
2045 pflashcomp = gen2_flash_types;
2046 filehdr_size = sizeof(struct flash_file_hdr_g2);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002047 num_comp = 8;
Ajit Khaparde84517482009-09-04 03:12:16 +00002048 }
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002049 for (i = 0; i < num_comp; i++) {
2050 if ((pflashcomp[i].optype == IMG_TYPE_NCSI_FW) &&
2051 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
2052 continue;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002053 if ((pflashcomp[i].optype == IMG_TYPE_REDBOOT) &&
2054 (!be_flash_redboot(adapter, fw->data,
2055 pflashcomp[i].offset, pflashcomp[i].size,
2056 filehdr_size)))
2057 continue;
2058 p = fw->data;
2059 p += filehdr_size + pflashcomp[i].offset
2060 + (num_of_images * sizeof(struct image_hdr));
2061 if (p + pflashcomp[i].size > fw->data + fw->size)
Ajit Khaparde84517482009-09-04 03:12:16 +00002062 return -1;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002063 total_bytes = pflashcomp[i].size;
2064 while (total_bytes) {
2065 if (total_bytes > 32*1024)
2066 num_bytes = 32*1024;
2067 else
2068 num_bytes = total_bytes;
2069 total_bytes -= num_bytes;
Ajit Khaparde84517482009-09-04 03:12:16 +00002070
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002071 if (!total_bytes)
2072 flash_op = FLASHROM_OPER_FLASH;
2073 else
2074 flash_op = FLASHROM_OPER_SAVE;
2075 memcpy(req->params.data_buf, p, num_bytes);
2076 p += num_bytes;
2077 status = be_cmd_write_flashrom(adapter, flash_cmd,
2078 pflashcomp[i].optype, flash_op, num_bytes);
2079 if (status) {
2080 dev_err(&adapter->pdev->dev,
2081 "cmd to write to flash rom failed.\n");
2082 return -1;
2083 }
2084 yield();
Ajit Khaparde84517482009-09-04 03:12:16 +00002085 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002086 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002087 return 0;
2088}
2089
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002090static int get_ufigen_type(struct flash_file_hdr_g2 *fhdr)
2091{
2092 if (fhdr == NULL)
2093 return 0;
2094 if (fhdr->build[0] == '3')
2095 return BE_GEN3;
2096 else if (fhdr->build[0] == '2')
2097 return BE_GEN2;
2098 else
2099 return 0;
2100}
2101
Ajit Khaparde84517482009-09-04 03:12:16 +00002102int be_load_fw(struct be_adapter *adapter, u8 *func)
2103{
2104 char fw_file[ETHTOOL_FLASH_MAX_FILENAME];
2105 const struct firmware *fw;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002106 struct flash_file_hdr_g2 *fhdr;
2107 struct flash_file_hdr_g3 *fhdr3;
2108 struct image_hdr *img_hdr_ptr = NULL;
Ajit Khaparde84517482009-09-04 03:12:16 +00002109 struct be_dma_mem flash_cmd;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002110 int status, i = 0, num_imgs = 0;
Ajit Khaparde84517482009-09-04 03:12:16 +00002111 const u8 *p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002112
Ajit Khaparde84517482009-09-04 03:12:16 +00002113 strcpy(fw_file, func);
2114
2115 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
2116 if (status)
2117 goto fw_exit;
2118
2119 p = fw->data;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002120 fhdr = (struct flash_file_hdr_g2 *) p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002121 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
2122
Ajit Khaparde84517482009-09-04 03:12:16 +00002123 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
2124 flash_cmd.va = pci_alloc_consistent(adapter->pdev, flash_cmd.size,
2125 &flash_cmd.dma);
2126 if (!flash_cmd.va) {
2127 status = -ENOMEM;
2128 dev_err(&adapter->pdev->dev,
2129 "Memory allocation failure while flashing\n");
2130 goto fw_exit;
2131 }
2132
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002133 if ((adapter->generation == BE_GEN3) &&
2134 (get_ufigen_type(fhdr) == BE_GEN3)) {
2135 fhdr3 = (struct flash_file_hdr_g3 *) fw->data;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002136 num_imgs = le32_to_cpu(fhdr3->num_imgs);
2137 for (i = 0; i < num_imgs; i++) {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002138 img_hdr_ptr = (struct image_hdr *) (fw->data +
2139 (sizeof(struct flash_file_hdr_g3) +
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002140 i * sizeof(struct image_hdr)));
2141 if (le32_to_cpu(img_hdr_ptr->imageid) == 1)
2142 status = be_flash_data(adapter, fw, &flash_cmd,
2143 num_imgs);
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002144 }
2145 } else if ((adapter->generation == BE_GEN2) &&
2146 (get_ufigen_type(fhdr) == BE_GEN2)) {
2147 status = be_flash_data(adapter, fw, &flash_cmd, 0);
2148 } else {
2149 dev_err(&adapter->pdev->dev,
2150 "UFI and Interface are not compatible for flashing\n");
2151 status = -1;
Ajit Khaparde84517482009-09-04 03:12:16 +00002152 }
2153
2154 pci_free_consistent(adapter->pdev, flash_cmd.size, flash_cmd.va,
2155 flash_cmd.dma);
2156 if (status) {
2157 dev_err(&adapter->pdev->dev, "Firmware load error\n");
2158 goto fw_exit;
2159 }
2160
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002161 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
Ajit Khaparde84517482009-09-04 03:12:16 +00002162
2163fw_exit:
2164 release_firmware(fw);
2165 return status;
2166}
2167
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002168static struct net_device_ops be_netdev_ops = {
2169 .ndo_open = be_open,
2170 .ndo_stop = be_close,
2171 .ndo_start_xmit = be_xmit,
2172 .ndo_get_stats = be_get_stats,
2173 .ndo_set_rx_mode = be_set_multicast_list,
2174 .ndo_set_mac_address = be_mac_addr_set,
2175 .ndo_change_mtu = be_change_mtu,
2176 .ndo_validate_addr = eth_validate_addr,
2177 .ndo_vlan_rx_register = be_vlan_register,
2178 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
2179 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002180 .ndo_set_vf_mac = be_set_vf_mac
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002181};
2182
2183static void be_netdev_init(struct net_device *netdev)
2184{
2185 struct be_adapter *adapter = netdev_priv(netdev);
2186
2187 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
Ajit Khaparde583e3f32009-10-05 02:22:19 +00002188 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_HW_CSUM |
2189 NETIF_F_GRO;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002190
Ajit Khaparde51c59872009-11-29 17:54:54 +00002191 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2192
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002193 netdev->flags |= IFF_MULTICAST;
2194
Ajit Khaparde728a9972009-04-13 15:41:22 -07002195 adapter->rx_csum = true;
2196
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002197 /* Default settings for Rx and Tx flow control */
2198 adapter->rx_fc = true;
2199 adapter->tx_fc = true;
2200
Ajit Khapardec190e3c2009-09-04 03:12:29 +00002201 netif_set_gso_max_size(netdev, 65535);
2202
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002203 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
2204
2205 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
2206
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002207 netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx,
2208 BE_NAPI_WEIGHT);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002209 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002210 BE_NAPI_WEIGHT);
2211
2212 netif_carrier_off(netdev);
2213 netif_stop_queue(netdev);
2214}
2215
2216static void be_unmap_pci_bars(struct be_adapter *adapter)
2217{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002218 if (adapter->csr)
2219 iounmap(adapter->csr);
2220 if (adapter->db)
2221 iounmap(adapter->db);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002222 if (adapter->pcicfg && be_physfn(adapter))
Sathya Perla8788fdc2009-07-27 22:52:03 +00002223 iounmap(adapter->pcicfg);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002224}
2225
2226static int be_map_pci_bars(struct be_adapter *adapter)
2227{
2228 u8 __iomem *addr;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002229 int pcicfg_reg, db_reg;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002230
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002231 if (be_physfn(adapter)) {
2232 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
2233 pci_resource_len(adapter->pdev, 2));
2234 if (addr == NULL)
2235 return -ENOMEM;
2236 adapter->csr = addr;
2237 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002238
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002239 if (adapter->generation == BE_GEN2) {
2240 pcicfg_reg = 1;
2241 db_reg = 4;
2242 } else {
2243 pcicfg_reg = 0;
2244 if (be_physfn(adapter))
2245 db_reg = 4;
2246 else
2247 db_reg = 0;
2248 }
2249 addr = ioremap_nocache(pci_resource_start(adapter->pdev, db_reg),
2250 pci_resource_len(adapter->pdev, db_reg));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002251 if (addr == NULL)
2252 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00002253 adapter->db = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002254
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002255 if (be_physfn(adapter)) {
2256 addr = ioremap_nocache(
2257 pci_resource_start(adapter->pdev, pcicfg_reg),
2258 pci_resource_len(adapter->pdev, pcicfg_reg));
2259 if (addr == NULL)
2260 goto pci_map_err;
2261 adapter->pcicfg = addr;
2262 } else
2263 adapter->pcicfg = adapter->db + SRIOV_VF_PCICFG_OFFSET;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002264
2265 return 0;
2266pci_map_err:
2267 be_unmap_pci_bars(adapter);
2268 return -ENOMEM;
2269}
2270
2271
2272static void be_ctrl_cleanup(struct be_adapter *adapter)
2273{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002274 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002275
2276 be_unmap_pci_bars(adapter);
2277
2278 if (mem->va)
2279 pci_free_consistent(adapter->pdev, mem->size,
2280 mem->va, mem->dma);
Sathya Perlae7b909a2009-11-22 22:01:10 +00002281
2282 mem = &adapter->mc_cmd_mem;
2283 if (mem->va)
2284 pci_free_consistent(adapter->pdev, mem->size,
2285 mem->va, mem->dma);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002286}
2287
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002288static int be_ctrl_init(struct be_adapter *adapter)
2289{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002290 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
2291 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002292 struct be_dma_mem *mc_cmd_mem = &adapter->mc_cmd_mem;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002293 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002294
2295 status = be_map_pci_bars(adapter);
2296 if (status)
Sathya Perlae7b909a2009-11-22 22:01:10 +00002297 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002298
2299 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
2300 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
2301 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
2302 if (!mbox_mem_alloc->va) {
Sathya Perlae7b909a2009-11-22 22:01:10 +00002303 status = -ENOMEM;
2304 goto unmap_pci_bars;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002305 }
Sathya Perlae7b909a2009-11-22 22:01:10 +00002306
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002307 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
2308 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
2309 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
2310 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Sathya Perlae7b909a2009-11-22 22:01:10 +00002311
2312 mc_cmd_mem->size = sizeof(struct be_cmd_req_mcast_mac_config);
2313 mc_cmd_mem->va = pci_alloc_consistent(adapter->pdev, mc_cmd_mem->size,
2314 &mc_cmd_mem->dma);
2315 if (mc_cmd_mem->va == NULL) {
2316 status = -ENOMEM;
2317 goto free_mbox;
2318 }
2319 memset(mc_cmd_mem->va, 0, mc_cmd_mem->size);
2320
Sathya Perla8788fdc2009-07-27 22:52:03 +00002321 spin_lock_init(&adapter->mbox_lock);
2322 spin_lock_init(&adapter->mcc_lock);
2323 spin_lock_init(&adapter->mcc_cq_lock);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002324
Sarveshwar Bandidd131e72010-05-25 16:16:32 -07002325 init_completion(&adapter->flash_compl);
Sathya Perlacf588472010-02-14 21:22:01 +00002326 pci_save_state(adapter->pdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002327 return 0;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002328
2329free_mbox:
2330 pci_free_consistent(adapter->pdev, mbox_mem_alloc->size,
2331 mbox_mem_alloc->va, mbox_mem_alloc->dma);
2332
2333unmap_pci_bars:
2334 be_unmap_pci_bars(adapter);
2335
2336done:
2337 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002338}
2339
2340static void be_stats_cleanup(struct be_adapter *adapter)
2341{
2342 struct be_stats_obj *stats = &adapter->stats;
2343 struct be_dma_mem *cmd = &stats->cmd;
2344
2345 if (cmd->va)
2346 pci_free_consistent(adapter->pdev, cmd->size,
2347 cmd->va, cmd->dma);
2348}
2349
2350static int be_stats_init(struct be_adapter *adapter)
2351{
2352 struct be_stats_obj *stats = &adapter->stats;
2353 struct be_dma_mem *cmd = &stats->cmd;
2354
2355 cmd->size = sizeof(struct be_cmd_req_get_stats);
2356 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
2357 if (cmd->va == NULL)
2358 return -1;
David S. Millerd291b9a2010-01-28 21:36:21 -08002359 memset(cmd->va, 0, cmd->size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002360 return 0;
2361}
2362
2363static void __devexit be_remove(struct pci_dev *pdev)
2364{
2365 struct be_adapter *adapter = pci_get_drvdata(pdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002366
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002367 if (!adapter)
2368 return;
2369
2370 unregister_netdev(adapter->netdev);
2371
Sathya Perla5fb379e2009-06-18 00:02:59 +00002372 be_clear(adapter);
2373
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002374 be_stats_cleanup(adapter);
2375
2376 be_ctrl_cleanup(adapter);
2377
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002378 be_sriov_disable(adapter);
2379
Sathya Perla8d56ff12009-11-22 22:02:26 +00002380 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002381
2382 pci_set_drvdata(pdev, NULL);
2383 pci_release_regions(pdev);
2384 pci_disable_device(pdev);
2385
2386 free_netdev(adapter->netdev);
2387}
2388
Sathya Perla2243e2e2009-11-22 22:02:03 +00002389static int be_get_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002390{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002391 int status;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002392 u8 mac[ETH_ALEN];
Sathya Perla43a04fdc2009-10-14 20:21:17 +00002393
Sathya Perla8788fdc2009-07-27 22:52:03 +00002394 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002395 if (status)
2396 return status;
2397
Ajit Khapardedcb9b562009-09-30 21:58:22 -07002398 status = be_cmd_query_fw_cfg(adapter,
2399 &adapter->port_num, &adapter->cap);
Sathya Perla2243e2e2009-11-22 22:02:03 +00002400 if (status)
2401 return status;
2402
2403 memset(mac, 0, ETH_ALEN);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002404
2405 if (be_physfn(adapter)) {
2406 status = be_cmd_mac_addr_query(adapter, mac,
Sathya Perla2243e2e2009-11-22 22:02:03 +00002407 MAC_ADDRESS_TYPE_NETWORK, true /*permanent */, 0);
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002408
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002409 if (status)
2410 return status;
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002411
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002412 if (!is_valid_ether_addr(mac))
2413 return -EADDRNOTAVAIL;
2414
2415 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2416 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2417 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002418
Ajit Khaparde82903e42010-02-09 01:34:57 +00002419 if (adapter->cap & 0x400)
2420 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED/4;
2421 else
2422 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED;
2423
Sathya Perla2243e2e2009-11-22 22:02:03 +00002424 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002425}
2426
2427static int __devinit be_probe(struct pci_dev *pdev,
2428 const struct pci_device_id *pdev_id)
2429{
2430 int status = 0;
2431 struct be_adapter *adapter;
2432 struct net_device *netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002433
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002434
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002435 status = pci_enable_device(pdev);
2436 if (status)
2437 goto do_none;
2438
2439 status = pci_request_regions(pdev, DRV_NAME);
2440 if (status)
2441 goto disable_dev;
2442 pci_set_master(pdev);
2443
2444 netdev = alloc_etherdev(sizeof(struct be_adapter));
2445 if (netdev == NULL) {
2446 status = -ENOMEM;
2447 goto rel_reg;
2448 }
2449 adapter = netdev_priv(netdev);
Ajit Khaparde7b139c82010-01-27 21:56:44 +00002450
2451 switch (pdev->device) {
2452 case BE_DEVICE_ID1:
2453 case OC_DEVICE_ID1:
2454 adapter->generation = BE_GEN2;
2455 break;
2456 case BE_DEVICE_ID2:
2457 case OC_DEVICE_ID2:
2458 adapter->generation = BE_GEN3;
2459 break;
2460 default:
2461 adapter->generation = 0;
2462 }
2463
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002464 adapter->pdev = pdev;
2465 pci_set_drvdata(pdev, adapter);
2466 adapter->netdev = netdev;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002467 be_netdev_init(netdev);
2468 SET_NETDEV_DEV(netdev, &pdev->dev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002469
2470 be_msix_enable(adapter);
2471
Yang Hongyange9304382009-04-13 14:40:14 -07002472 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002473 if (!status) {
2474 netdev->features |= NETIF_F_HIGHDMA;
2475 } else {
Yang Hongyange9304382009-04-13 14:40:14 -07002476 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002477 if (status) {
2478 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
2479 goto free_netdev;
2480 }
2481 }
2482
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002483 be_sriov_enable(adapter);
2484
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002485 status = be_ctrl_init(adapter);
2486 if (status)
2487 goto free_netdev;
2488
Sathya Perla2243e2e2009-11-22 22:02:03 +00002489 /* sync up with fw's ready state */
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002490 if (be_physfn(adapter)) {
2491 status = be_cmd_POST(adapter);
2492 if (status)
2493 goto ctrl_clean;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002494 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002495
2496 /* tell fw we're ready to fire cmds */
2497 status = be_cmd_fw_init(adapter);
2498 if (status)
2499 goto ctrl_clean;
2500
Sarveshwar Bandi556ae192010-05-24 18:38:25 -07002501 if (be_physfn(adapter)) {
2502 status = be_cmd_reset_function(adapter);
2503 if (status)
2504 goto ctrl_clean;
2505 }
2506
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002507 status = be_stats_init(adapter);
2508 if (status)
2509 goto ctrl_clean;
2510
Sathya Perla2243e2e2009-11-22 22:02:03 +00002511 status = be_get_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002512 if (status)
2513 goto stats_clean;
2514
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002515 INIT_DELAYED_WORK(&adapter->work, be_worker);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002516
Sathya Perla5fb379e2009-06-18 00:02:59 +00002517 status = be_setup(adapter);
2518 if (status)
2519 goto stats_clean;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002520
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002521 status = register_netdev(netdev);
2522 if (status != 0)
Sathya Perla5fb379e2009-06-18 00:02:59 +00002523 goto unsetup;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002524
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002525 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002526 return 0;
2527
Sathya Perla5fb379e2009-06-18 00:02:59 +00002528unsetup:
2529 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002530stats_clean:
2531 be_stats_cleanup(adapter);
2532ctrl_clean:
2533 be_ctrl_cleanup(adapter);
2534free_netdev:
Sathya Perla8d56ff12009-11-22 22:02:26 +00002535 be_msix_disable(adapter);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002536 be_sriov_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002537 free_netdev(adapter->netdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002538 pci_set_drvdata(pdev, NULL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002539rel_reg:
2540 pci_release_regions(pdev);
2541disable_dev:
2542 pci_disable_device(pdev);
2543do_none:
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002544 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002545 return status;
2546}
2547
2548static int be_suspend(struct pci_dev *pdev, pm_message_t state)
2549{
2550 struct be_adapter *adapter = pci_get_drvdata(pdev);
2551 struct net_device *netdev = adapter->netdev;
2552
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002553 if (adapter->wol)
2554 be_setup_wol(adapter, true);
2555
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002556 netif_device_detach(netdev);
2557 if (netif_running(netdev)) {
2558 rtnl_lock();
2559 be_close(netdev);
2560 rtnl_unlock();
2561 }
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002562 be_cmd_get_flow_control(adapter, &adapter->tx_fc, &adapter->rx_fc);
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002563 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002564
2565 pci_save_state(pdev);
2566 pci_disable_device(pdev);
2567 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2568 return 0;
2569}
2570
2571static int be_resume(struct pci_dev *pdev)
2572{
2573 int status = 0;
2574 struct be_adapter *adapter = pci_get_drvdata(pdev);
2575 struct net_device *netdev = adapter->netdev;
2576
2577 netif_device_detach(netdev);
2578
2579 status = pci_enable_device(pdev);
2580 if (status)
2581 return status;
2582
2583 pci_set_power_state(pdev, 0);
2584 pci_restore_state(pdev);
2585
Sathya Perla2243e2e2009-11-22 22:02:03 +00002586 /* tell fw we're ready to fire cmds */
2587 status = be_cmd_fw_init(adapter);
2588 if (status)
2589 return status;
2590
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002591 be_setup(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002592 if (netif_running(netdev)) {
2593 rtnl_lock();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002594 be_open(netdev);
2595 rtnl_unlock();
2596 }
2597 netif_device_attach(netdev);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002598
2599 if (adapter->wol)
2600 be_setup_wol(adapter, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002601 return 0;
2602}
2603
Sathya Perla82456b02010-02-17 01:35:37 +00002604/*
2605 * An FLR will stop BE from DMAing any data.
2606 */
2607static void be_shutdown(struct pci_dev *pdev)
2608{
2609 struct be_adapter *adapter = pci_get_drvdata(pdev);
2610 struct net_device *netdev = adapter->netdev;
2611
2612 netif_device_detach(netdev);
2613
2614 be_cmd_reset_function(adapter);
2615
2616 if (adapter->wol)
2617 be_setup_wol(adapter, true);
2618
2619 pci_disable_device(pdev);
Sathya Perla82456b02010-02-17 01:35:37 +00002620}
2621
Sathya Perlacf588472010-02-14 21:22:01 +00002622static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
2623 pci_channel_state_t state)
2624{
2625 struct be_adapter *adapter = pci_get_drvdata(pdev);
2626 struct net_device *netdev = adapter->netdev;
2627
2628 dev_err(&adapter->pdev->dev, "EEH error detected\n");
2629
2630 adapter->eeh_err = true;
2631
2632 netif_device_detach(netdev);
2633
2634 if (netif_running(netdev)) {
2635 rtnl_lock();
2636 be_close(netdev);
2637 rtnl_unlock();
2638 }
2639 be_clear(adapter);
2640
2641 if (state == pci_channel_io_perm_failure)
2642 return PCI_ERS_RESULT_DISCONNECT;
2643
2644 pci_disable_device(pdev);
2645
2646 return PCI_ERS_RESULT_NEED_RESET;
2647}
2648
2649static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
2650{
2651 struct be_adapter *adapter = pci_get_drvdata(pdev);
2652 int status;
2653
2654 dev_info(&adapter->pdev->dev, "EEH reset\n");
2655 adapter->eeh_err = false;
2656
2657 status = pci_enable_device(pdev);
2658 if (status)
2659 return PCI_ERS_RESULT_DISCONNECT;
2660
2661 pci_set_master(pdev);
2662 pci_set_power_state(pdev, 0);
2663 pci_restore_state(pdev);
2664
2665 /* Check if card is ok and fw is ready */
2666 status = be_cmd_POST(adapter);
2667 if (status)
2668 return PCI_ERS_RESULT_DISCONNECT;
2669
2670 return PCI_ERS_RESULT_RECOVERED;
2671}
2672
2673static void be_eeh_resume(struct pci_dev *pdev)
2674{
2675 int status = 0;
2676 struct be_adapter *adapter = pci_get_drvdata(pdev);
2677 struct net_device *netdev = adapter->netdev;
2678
2679 dev_info(&adapter->pdev->dev, "EEH resume\n");
2680
2681 pci_save_state(pdev);
2682
2683 /* tell fw we're ready to fire cmds */
2684 status = be_cmd_fw_init(adapter);
2685 if (status)
2686 goto err;
2687
2688 status = be_setup(adapter);
2689 if (status)
2690 goto err;
2691
2692 if (netif_running(netdev)) {
2693 status = be_open(netdev);
2694 if (status)
2695 goto err;
2696 }
2697 netif_device_attach(netdev);
2698 return;
2699err:
2700 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
Sathya Perlacf588472010-02-14 21:22:01 +00002701}
2702
2703static struct pci_error_handlers be_eeh_handlers = {
2704 .error_detected = be_eeh_err_detected,
2705 .slot_reset = be_eeh_reset,
2706 .resume = be_eeh_resume,
2707};
2708
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002709static struct pci_driver be_driver = {
2710 .name = DRV_NAME,
2711 .id_table = be_dev_ids,
2712 .probe = be_probe,
2713 .remove = be_remove,
2714 .suspend = be_suspend,
Sathya Perlacf588472010-02-14 21:22:01 +00002715 .resume = be_resume,
Sathya Perla82456b02010-02-17 01:35:37 +00002716 .shutdown = be_shutdown,
Sathya Perlacf588472010-02-14 21:22:01 +00002717 .err_handler = &be_eeh_handlers
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002718};
2719
2720static int __init be_init_module(void)
2721{
Joe Perches8e95a202009-12-03 07:58:21 +00002722 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
2723 rx_frag_size != 2048) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002724 printk(KERN_WARNING DRV_NAME
2725 " : Module param rx_frag_size must be 2048/4096/8192."
2726 " Using 2048\n");
2727 rx_frag_size = 2048;
2728 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002729
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002730 if (num_vfs > 32) {
2731 printk(KERN_WARNING DRV_NAME
2732 " : Module param num_vfs must not be greater than 32."
2733 "Using 32\n");
2734 num_vfs = 32;
2735 }
2736
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002737 return pci_register_driver(&be_driver);
2738}
2739module_init(be_init_module);
2740
2741static void __exit be_exit_module(void)
2742{
2743 pci_unregister_driver(&be_driver);
2744}
2745module_exit(be_exit_module);