blob: d5e796832fdd77270db8935537063d271c598762 [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 Perla3abcded2010-10-03 22:12:27 -070035static bool multi_rxq = true;
36module_param(multi_rxq, bool, S_IRUGO | S_IWUSR);
37MODULE_PARM_DESC(multi_rxq, "Multi Rx Queue support. Enabled by default");
38
Sathya Perla6b7c5b92009-03-11 23:32:03 -070039static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
Ajit Khapardec4ca2372009-05-18 15:38:55 -070040 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Ajit Khaparde59fd5d82009-10-29 01:11:06 -070041 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Ajit Khapardec4ca2372009-05-18 15:38:55 -070042 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
43 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
Sathya Perla6b7c5b92009-03-11 23:32:03 -070044 { 0 }
45};
46MODULE_DEVICE_TABLE(pci, be_dev_ids);
Ajit Khaparde7c185272010-07-29 06:16:33 +000047/* UE Status Low CSR */
48static char *ue_status_low_desc[] = {
49 "CEV",
50 "CTX",
51 "DBUF",
52 "ERX",
53 "Host",
54 "MPU",
55 "NDMA",
56 "PTC ",
57 "RDMA ",
58 "RXF ",
59 "RXIPS ",
60 "RXULP0 ",
61 "RXULP1 ",
62 "RXULP2 ",
63 "TIM ",
64 "TPOST ",
65 "TPRE ",
66 "TXIPS ",
67 "TXULP0 ",
68 "TXULP1 ",
69 "UC ",
70 "WDMA ",
71 "TXULP2 ",
72 "HOST1 ",
73 "P0_OB_LINK ",
74 "P1_OB_LINK ",
75 "HOST_GPIO ",
76 "MBOX ",
77 "AXGMAC0",
78 "AXGMAC1",
79 "JTAG",
80 "MPU_INTPEND"
81};
82/* UE Status High CSR */
83static char *ue_status_hi_desc[] = {
84 "LPCMEMHOST",
85 "MGMT_MAC",
86 "PCS0ONLINE",
87 "MPU_IRAM",
88 "PCS1ONLINE",
89 "PCTL0",
90 "PCTL1",
91 "PMEM",
92 "RR",
93 "TXPB",
94 "RXPP",
95 "XAUI",
96 "TXP",
97 "ARM",
98 "IPC",
99 "HOST2",
100 "HOST3",
101 "HOST4",
102 "HOST5",
103 "HOST6",
104 "HOST7",
105 "HOST8",
106 "HOST9",
107 "NETC"
108 "Unknown",
109 "Unknown",
110 "Unknown",
111 "Unknown",
112 "Unknown",
113 "Unknown",
114 "Unknown",
115 "Unknown"
116};
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700117
Sathya Perla3abcded2010-10-03 22:12:27 -0700118static inline bool be_multi_rxq(struct be_adapter *adapter)
119{
120 return (adapter->num_rx_qs > 1);
121}
122
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700123static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
124{
125 struct be_dma_mem *mem = &q->dma_mem;
126 if (mem->va)
127 pci_free_consistent(adapter->pdev, mem->size,
128 mem->va, mem->dma);
129}
130
131static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
132 u16 len, u16 entry_size)
133{
134 struct be_dma_mem *mem = &q->dma_mem;
135
136 memset(q, 0, sizeof(*q));
137 q->len = len;
138 q->entry_size = entry_size;
139 mem->size = len * entry_size;
140 mem->va = pci_alloc_consistent(adapter->pdev, mem->size, &mem->dma);
141 if (!mem->va)
142 return -1;
143 memset(mem->va, 0, mem->size);
144 return 0;
145}
146
Sathya Perla8788fdc2009-07-27 22:52:03 +0000147static void be_intr_set(struct be_adapter *adapter, bool enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700148{
Sathya Perla8788fdc2009-07-27 22:52:03 +0000149 u8 __iomem *addr = adapter->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700150 u32 reg = ioread32(addr);
151 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +0000152
Sathya Perlacf588472010-02-14 21:22:01 +0000153 if (adapter->eeh_err)
154 return;
155
Sathya Perla5f0b8492009-07-27 22:52:56 +0000156 if (!enabled && enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700157 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +0000158 else if (enabled && !enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700159 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +0000160 else
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700161 return;
Sathya Perla5f0b8492009-07-27 22:52:56 +0000162
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700163 iowrite32(reg, addr);
164}
165
Sathya Perla8788fdc2009-07-27 22:52:03 +0000166static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700167{
168 u32 val = 0;
169 val |= qid & DB_RQ_RING_ID_MASK;
170 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
Sathya Perlaf3eb62d2010-06-29 00:11:17 +0000171
172 wmb();
Sathya Perla8788fdc2009-07-27 22:52:03 +0000173 iowrite32(val, adapter->db + DB_RQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700174}
175
Sathya Perla8788fdc2009-07-27 22:52:03 +0000176static void be_txq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700177{
178 u32 val = 0;
179 val |= qid & DB_TXULP_RING_ID_MASK;
180 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
Sathya Perlaf3eb62d2010-06-29 00:11:17 +0000181
182 wmb();
Sathya Perla8788fdc2009-07-27 22:52:03 +0000183 iowrite32(val, adapter->db + DB_TXULP1_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700184}
185
Sathya Perla8788fdc2009-07-27 22:52:03 +0000186static void be_eq_notify(struct be_adapter *adapter, u16 qid,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700187 bool arm, bool clear_int, u16 num_popped)
188{
189 u32 val = 0;
190 val |= qid & DB_EQ_RING_ID_MASK;
Sathya Perlacf588472010-02-14 21:22:01 +0000191
192 if (adapter->eeh_err)
193 return;
194
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700195 if (arm)
196 val |= 1 << DB_EQ_REARM_SHIFT;
197 if (clear_int)
198 val |= 1 << DB_EQ_CLR_SHIFT;
199 val |= 1 << DB_EQ_EVNT_SHIFT;
200 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000201 iowrite32(val, adapter->db + DB_EQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700202}
203
Sathya Perla8788fdc2009-07-27 22:52:03 +0000204void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700205{
206 u32 val = 0;
207 val |= qid & DB_CQ_RING_ID_MASK;
Sathya Perlacf588472010-02-14 21:22:01 +0000208
209 if (adapter->eeh_err)
210 return;
211
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700212 if (arm)
213 val |= 1 << DB_CQ_REARM_SHIFT;
214 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000215 iowrite32(val, adapter->db + DB_CQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700216}
217
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700218static int be_mac_addr_set(struct net_device *netdev, void *p)
219{
220 struct be_adapter *adapter = netdev_priv(netdev);
221 struct sockaddr *addr = p;
222 int status = 0;
223
Ajit Khapardeca9e4982009-11-29 17:56:26 +0000224 if (!is_valid_ether_addr(addr->sa_data))
225 return -EADDRNOTAVAIL;
226
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000227 /* MAC addr configuration will be done in hardware for VFs
228 * by their corresponding PFs. Just copy to netdev addr here
229 */
230 if (!be_physfn(adapter))
231 goto netdev_addr;
232
Sathya Perlaa65027e2009-08-17 00:58:04 +0000233 status = be_cmd_pmac_del(adapter, adapter->if_handle, adapter->pmac_id);
234 if (status)
235 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700236
Sathya Perlaa65027e2009-08-17 00:58:04 +0000237 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
238 adapter->if_handle, &adapter->pmac_id);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000239netdev_addr:
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700240 if (!status)
241 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
242
243 return status;
244}
245
Sathya Perlab31c50a2009-09-17 10:30:13 -0700246void netdev_stats_update(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700247{
Sathya Perla3abcded2010-10-03 22:12:27 -0700248 struct be_hw_stats *hw_stats = hw_stats_from_cmd(adapter->stats_cmd.va);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700249 struct be_rxf_stats *rxf_stats = &hw_stats->rxf;
250 struct be_port_rxf_stats *port_stats =
251 &rxf_stats->port[adapter->port_num];
Ajit Khaparde78122a52009-10-07 03:11:20 -0700252 struct net_device_stats *dev_stats = &adapter->netdev->stats;
Sathya Perla68110862009-06-10 02:21:16 +0000253 struct be_erx_stats *erx_stats = &hw_stats->erx;
Sathya Perla3abcded2010-10-03 22:12:27 -0700254 struct be_rx_obj *rxo;
255 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700256
Sathya Perla3abcded2010-10-03 22:12:27 -0700257 memset(dev_stats, 0, sizeof(*dev_stats));
258 for_all_rx_queues(adapter, rxo, i) {
259 dev_stats->rx_packets += rx_stats(rxo)->rx_pkts;
260 dev_stats->rx_bytes += rx_stats(rxo)->rx_bytes;
261 dev_stats->multicast += rx_stats(rxo)->rx_mcast_pkts;
262 /* no space in linux buffers: best possible approximation */
263 dev_stats->rx_dropped +=
264 erx_stats->rx_drops_no_fragments[rxo->q.id];
265 }
266
267 dev_stats->tx_packets = tx_stats(adapter)->be_tx_pkts;
268 dev_stats->tx_bytes = tx_stats(adapter)->be_tx_bytes;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700269
270 /* bad pkts received */
271 dev_stats->rx_errors = port_stats->rx_crc_errors +
272 port_stats->rx_alignment_symbol_errors +
273 port_stats->rx_in_range_errors +
Sathya Perla68110862009-06-10 02:21:16 +0000274 port_stats->rx_out_range_errors +
275 port_stats->rx_frame_too_long +
276 port_stats->rx_dropped_too_small +
277 port_stats->rx_dropped_too_short +
278 port_stats->rx_dropped_header_too_small +
279 port_stats->rx_dropped_tcp_length +
280 port_stats->rx_dropped_runt +
281 port_stats->rx_tcp_checksum_errs +
282 port_stats->rx_ip_checksum_errs +
283 port_stats->rx_udp_checksum_errs;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700284
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700285 /* detailed rx errors */
286 dev_stats->rx_length_errors = port_stats->rx_in_range_errors +
Sathya Perla68110862009-06-10 02:21:16 +0000287 port_stats->rx_out_range_errors +
288 port_stats->rx_frame_too_long;
289
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700290 dev_stats->rx_crc_errors = port_stats->rx_crc_errors;
291
292 /* frame alignment errors */
293 dev_stats->rx_frame_errors = port_stats->rx_alignment_symbol_errors;
Sathya Perla68110862009-06-10 02:21:16 +0000294
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700295 /* receiver fifo overrun */
296 /* drops_no_pbuf is no per i/f, it's per BE card */
297 dev_stats->rx_fifo_errors = port_stats->rx_fifo_overflow +
298 port_stats->rx_input_fifo_overflow +
299 rxf_stats->rx_drops_no_pbuf;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700300}
301
Sathya Perla8788fdc2009-07-27 22:52:03 +0000302void be_link_status_update(struct be_adapter *adapter, bool link_up)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700303{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700304 struct net_device *netdev = adapter->netdev;
305
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700306 /* If link came up or went down */
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000307 if (adapter->link_up != link_up) {
Ajit Khaparde0dffc832009-11-29 17:57:46 +0000308 adapter->link_speed = -1;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000309 if (link_up) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700310 netif_start_queue(netdev);
311 netif_carrier_on(netdev);
312 printk(KERN_INFO "%s: Link up\n", netdev->name);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000313 } else {
314 netif_stop_queue(netdev);
315 netif_carrier_off(netdev);
316 printk(KERN_INFO "%s: Link down\n", netdev->name);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700317 }
Sathya Perlaa8f447bd2009-06-18 00:10:27 +0000318 adapter->link_up = link_up;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700319 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700320}
321
322/* Update the EQ delay n BE based on the RX frags consumed / sec */
Sathya Perla3abcded2010-10-03 22:12:27 -0700323static void be_rx_eqd_update(struct be_adapter *adapter, struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700324{
Sathya Perla3abcded2010-10-03 22:12:27 -0700325 struct be_eq_obj *rx_eq = &rxo->rx_eq;
326 struct be_rx_stats *stats = &rxo->stats;
Sathya Perla4097f662009-03-24 16:40:13 -0700327 ulong now = jiffies;
328 u32 eqd;
329
330 if (!rx_eq->enable_aic)
331 return;
332
333 /* Wrapped around */
334 if (time_before(now, stats->rx_fps_jiffies)) {
335 stats->rx_fps_jiffies = now;
336 return;
337 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700338
339 /* Update once a second */
Sathya Perla4097f662009-03-24 16:40:13 -0700340 if ((now - stats->rx_fps_jiffies) < HZ)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700341 return;
342
Sathya Perla3abcded2010-10-03 22:12:27 -0700343 stats->rx_fps = (stats->rx_frags - stats->prev_rx_frags) /
Sathya Perla4097f662009-03-24 16:40:13 -0700344 ((now - stats->rx_fps_jiffies) / HZ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700345
Sathya Perla4097f662009-03-24 16:40:13 -0700346 stats->rx_fps_jiffies = now;
Sathya Perla3abcded2010-10-03 22:12:27 -0700347 stats->prev_rx_frags = stats->rx_frags;
348 eqd = stats->rx_fps / 110000;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700349 eqd = eqd << 3;
350 if (eqd > rx_eq->max_eqd)
351 eqd = rx_eq->max_eqd;
352 if (eqd < rx_eq->min_eqd)
353 eqd = rx_eq->min_eqd;
354 if (eqd < 10)
355 eqd = 0;
356 if (eqd != rx_eq->cur_eqd)
Sathya Perla8788fdc2009-07-27 22:52:03 +0000357 be_cmd_modify_eqd(adapter, rx_eq->q.id, eqd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700358
359 rx_eq->cur_eqd = eqd;
360}
361
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700362static u32 be_calc_rate(u64 bytes, unsigned long ticks)
363{
364 u64 rate = bytes;
365
366 do_div(rate, ticks / HZ);
367 rate <<= 3; /* bytes/sec -> bits/sec */
368 do_div(rate, 1000000ul); /* MB/Sec */
369
370 return rate;
371}
372
Sathya Perla4097f662009-03-24 16:40:13 -0700373static void be_tx_rate_update(struct be_adapter *adapter)
374{
Sathya Perla3abcded2010-10-03 22:12:27 -0700375 struct be_tx_stats *stats = tx_stats(adapter);
Sathya Perla4097f662009-03-24 16:40:13 -0700376 ulong now = jiffies;
377
378 /* Wrapped around? */
379 if (time_before(now, stats->be_tx_jiffies)) {
380 stats->be_tx_jiffies = now;
381 return;
382 }
383
384 /* Update tx rate once in two seconds */
385 if ((now - stats->be_tx_jiffies) > 2 * HZ) {
Stephen Hemminger65f71b82009-03-27 00:25:24 -0700386 stats->be_tx_rate = be_calc_rate(stats->be_tx_bytes
387 - stats->be_tx_bytes_prev,
388 now - stats->be_tx_jiffies);
Sathya Perla4097f662009-03-24 16:40:13 -0700389 stats->be_tx_jiffies = now;
390 stats->be_tx_bytes_prev = stats->be_tx_bytes;
391 }
392}
393
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700394static void be_tx_stats_update(struct be_adapter *adapter,
Ajit Khaparde91992e42010-02-19 13:57:12 +0000395 u32 wrb_cnt, u32 copied, u32 gso_segs, bool stopped)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700396{
Sathya Perla3abcded2010-10-03 22:12:27 -0700397 struct be_tx_stats *stats = tx_stats(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700398 stats->be_tx_reqs++;
399 stats->be_tx_wrbs += wrb_cnt;
400 stats->be_tx_bytes += copied;
Ajit Khaparde91992e42010-02-19 13:57:12 +0000401 stats->be_tx_pkts += (gso_segs ? gso_segs : 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700402 if (stopped)
403 stats->be_tx_stops++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700404}
405
406/* Determine number of WRB entries needed to xmit data in an skb */
407static u32 wrb_cnt_for_skb(struct sk_buff *skb, bool *dummy)
408{
David S. Millerebc8d2a2009-06-09 01:01:31 -0700409 int cnt = (skb->len > skb->data_len);
410
411 cnt += skb_shinfo(skb)->nr_frags;
412
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700413 /* to account for hdr wrb */
414 cnt++;
415 if (cnt & 1) {
416 /* add a dummy to make it an even num */
417 cnt++;
418 *dummy = true;
419 } else
420 *dummy = false;
421 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
422 return cnt;
423}
424
425static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
426{
427 wrb->frag_pa_hi = upper_32_bits(addr);
428 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
429 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
430}
431
432static void wrb_fill_hdr(struct be_eth_hdr_wrb *hdr, struct sk_buff *skb,
Jesse Grosseab6d182010-10-20 13:56:03 +0000433 u32 wrb_cnt, u32 len)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700434{
435 memset(hdr, 0, sizeof(*hdr));
436
437 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
438
Ajit Khaparde49e4b8472010-06-14 04:56:07 +0000439 if (skb_is_gso(skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700440 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
441 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
442 hdr, skb_shinfo(skb)->gso_size);
Ajit Khaparde49e4b8472010-06-14 04:56:07 +0000443 if (skb_is_gso_v6(skb))
444 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso6, hdr, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700445 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
446 if (is_tcp_pkt(skb))
447 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
448 else if (is_udp_pkt(skb))
449 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
450 }
451
Jesse Grosseab6d182010-10-20 13:56:03 +0000452 if (vlan_tx_tag_present(skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700453 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
454 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag,
455 hdr, vlan_tx_tag_get(skb));
456 }
457
458 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
459 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
460 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
461 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
462}
463
Sathya Perla7101e112010-03-22 20:41:12 +0000464static void unmap_tx_frag(struct pci_dev *pdev, struct be_eth_wrb *wrb,
465 bool unmap_single)
466{
467 dma_addr_t dma;
468
469 be_dws_le_to_cpu(wrb, sizeof(*wrb));
470
471 dma = (u64)wrb->frag_pa_hi << 32 | (u64)wrb->frag_pa_lo;
FUJITA Tomonorib681ee72010-04-04 21:40:18 +0000472 if (wrb->frag_len) {
Sathya Perla7101e112010-03-22 20:41:12 +0000473 if (unmap_single)
474 pci_unmap_single(pdev, dma, wrb->frag_len,
475 PCI_DMA_TODEVICE);
476 else
477 pci_unmap_page(pdev, dma, wrb->frag_len,
478 PCI_DMA_TODEVICE);
479 }
480}
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700481
482static int make_tx_wrbs(struct be_adapter *adapter,
483 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
484{
Sathya Perla7101e112010-03-22 20:41:12 +0000485 dma_addr_t busaddr;
486 int i, copied = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700487 struct pci_dev *pdev = adapter->pdev;
488 struct sk_buff *first_skb = skb;
489 struct be_queue_info *txq = &adapter->tx_obj.q;
490 struct be_eth_wrb *wrb;
491 struct be_eth_hdr_wrb *hdr;
Sathya Perla7101e112010-03-22 20:41:12 +0000492 bool map_single = false;
493 u16 map_head;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700494
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700495 hdr = queue_head_node(txq);
496 queue_head_inc(txq);
Sathya Perla7101e112010-03-22 20:41:12 +0000497 map_head = txq->head;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700498
David S. Millerebc8d2a2009-06-09 01:01:31 -0700499 if (skb->len > skb->data_len) {
Eric Dumazete743d312010-04-14 15:59:40 -0700500 int len = skb_headlen(skb);
Alexander Duycka73b7962009-12-02 16:48:18 +0000501 busaddr = pci_map_single(pdev, skb->data, len,
502 PCI_DMA_TODEVICE);
Sathya Perla7101e112010-03-22 20:41:12 +0000503 if (pci_dma_mapping_error(pdev, busaddr))
504 goto dma_err;
505 map_single = true;
David S. Millerebc8d2a2009-06-09 01:01:31 -0700506 wrb = queue_head_node(txq);
507 wrb_fill(wrb, busaddr, len);
508 be_dws_cpu_to_le(wrb, sizeof(*wrb));
509 queue_head_inc(txq);
510 copied += len;
511 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700512
David S. Millerebc8d2a2009-06-09 01:01:31 -0700513 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
514 struct skb_frag_struct *frag =
515 &skb_shinfo(skb)->frags[i];
Alexander Duycka73b7962009-12-02 16:48:18 +0000516 busaddr = pci_map_page(pdev, frag->page,
517 frag->page_offset,
518 frag->size, PCI_DMA_TODEVICE);
Sathya Perla7101e112010-03-22 20:41:12 +0000519 if (pci_dma_mapping_error(pdev, busaddr))
520 goto dma_err;
David S. Millerebc8d2a2009-06-09 01:01:31 -0700521 wrb = queue_head_node(txq);
522 wrb_fill(wrb, busaddr, frag->size);
523 be_dws_cpu_to_le(wrb, sizeof(*wrb));
524 queue_head_inc(txq);
525 copied += frag->size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700526 }
527
528 if (dummy_wrb) {
529 wrb = queue_head_node(txq);
530 wrb_fill(wrb, 0, 0);
531 be_dws_cpu_to_le(wrb, sizeof(*wrb));
532 queue_head_inc(txq);
533 }
534
Jesse Grosseab6d182010-10-20 13:56:03 +0000535 wrb_fill_hdr(hdr, first_skb, wrb_cnt, copied);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700536 be_dws_cpu_to_le(hdr, sizeof(*hdr));
537
538 return copied;
Sathya Perla7101e112010-03-22 20:41:12 +0000539dma_err:
540 txq->head = map_head;
541 while (copied) {
542 wrb = queue_head_node(txq);
543 unmap_tx_frag(pdev, wrb, map_single);
544 map_single = false;
545 copied -= wrb->frag_len;
546 queue_head_inc(txq);
547 }
548 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700549}
550
Stephen Hemminger613573252009-08-31 19:50:58 +0000551static netdev_tx_t be_xmit(struct sk_buff *skb,
Sathya Perlab31c50a2009-09-17 10:30:13 -0700552 struct net_device *netdev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700553{
554 struct be_adapter *adapter = netdev_priv(netdev);
555 struct be_tx_obj *tx_obj = &adapter->tx_obj;
556 struct be_queue_info *txq = &tx_obj->q;
557 u32 wrb_cnt = 0, copied = 0;
558 u32 start = txq->head;
559 bool dummy_wrb, stopped = false;
560
561 wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb);
562
563 copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000564 if (copied) {
565 /* record the sent skb in the sent_skb table */
566 BUG_ON(tx_obj->sent_skb_list[start]);
567 tx_obj->sent_skb_list[start] = skb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700568
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000569 /* Ensure txq has space for the next skb; Else stop the queue
570 * *BEFORE* ringing the tx doorbell, so that we serialze the
571 * tx compls of the current transmit which'll wake up the queue
572 */
Sathya Perla7101e112010-03-22 20:41:12 +0000573 atomic_add(wrb_cnt, &txq->used);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000574 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
575 txq->len) {
576 netif_stop_queue(netdev);
577 stopped = true;
578 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700579
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000580 be_txq_notify(adapter, txq->id, wrb_cnt);
581
Ajit Khaparde91992e42010-02-19 13:57:12 +0000582 be_tx_stats_update(adapter, wrb_cnt, copied,
583 skb_shinfo(skb)->gso_segs, stopped);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000584 } else {
585 txq->head = start;
586 dev_kfree_skb_any(skb);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700587 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700588 return NETDEV_TX_OK;
589}
590
591static int be_change_mtu(struct net_device *netdev, int new_mtu)
592{
593 struct be_adapter *adapter = netdev_priv(netdev);
594 if (new_mtu < BE_MIN_MTU ||
Ajit Khaparde34a89b82010-02-09 01:32:43 +0000595 new_mtu > (BE_MAX_JUMBO_FRAME_SIZE -
596 (ETH_HLEN + ETH_FCS_LEN))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700597 dev_info(&adapter->pdev->dev,
598 "MTU must be between %d and %d bytes\n",
Ajit Khaparde34a89b82010-02-09 01:32:43 +0000599 BE_MIN_MTU,
600 (BE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN)));
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700601 return -EINVAL;
602 }
603 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
604 netdev->mtu, new_mtu);
605 netdev->mtu = new_mtu;
606 return 0;
607}
608
609/*
Ajit Khaparde82903e42010-02-09 01:34:57 +0000610 * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
611 * If the user configures more, place BE in vlan promiscuous mode.
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700612 */
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000613static int be_vid_config(struct be_adapter *adapter, bool vf, u32 vf_num)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700614{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700615 u16 vtag[BE_NUM_VLANS_SUPPORTED];
616 u16 ntags = 0, i;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000617 int status = 0;
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000618 u32 if_handle;
619
620 if (vf) {
621 if_handle = adapter->vf_cfg[vf_num].vf_if_handle;
622 vtag[0] = cpu_to_le16(adapter->vf_cfg[vf_num].vf_vlan_tag);
623 status = be_cmd_vlan_config(adapter, if_handle, vtag, 1, 1, 0);
624 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700625
Ajit Khaparde82903e42010-02-09 01:34:57 +0000626 if (adapter->vlans_added <= adapter->max_vlans) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700627 /* Construct VLAN Table to give to HW */
Jesse Grossb7381272010-10-20 13:56:02 +0000628 for (i = 0; i < VLAN_N_VID; i++) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700629 if (adapter->vlan_tag[i]) {
630 vtag[ntags] = cpu_to_le16(i);
631 ntags++;
632 }
633 }
Sathya Perlab31c50a2009-09-17 10:30:13 -0700634 status = be_cmd_vlan_config(adapter, adapter->if_handle,
635 vtag, ntags, 1, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700636 } else {
Sathya Perlab31c50a2009-09-17 10:30:13 -0700637 status = be_cmd_vlan_config(adapter, adapter->if_handle,
638 NULL, 0, 1, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700639 }
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000640
Sathya Perlab31c50a2009-09-17 10:30:13 -0700641 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700642}
643
644static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
645{
646 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700647
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700648 adapter->vlan_grp = grp;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700649}
650
651static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
652{
653 struct be_adapter *adapter = netdev_priv(netdev);
654
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000655 adapter->vlans_added++;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000656 if (!be_physfn(adapter))
657 return;
658
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700659 adapter->vlan_tag[vid] = 1;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000660 if (adapter->vlans_added <= (adapter->max_vlans + 1))
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000661 be_vid_config(adapter, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700662}
663
664static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
665{
666 struct be_adapter *adapter = netdev_priv(netdev);
667
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000668 adapter->vlans_added--;
669 vlan_group_set_device(adapter->vlan_grp, vid, NULL);
670
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000671 if (!be_physfn(adapter))
672 return;
673
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700674 adapter->vlan_tag[vid] = 0;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000675 if (adapter->vlans_added <= adapter->max_vlans)
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000676 be_vid_config(adapter, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700677}
678
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700679static void be_set_multicast_list(struct net_device *netdev)
680{
681 struct be_adapter *adapter = netdev_priv(netdev);
682
683 if (netdev->flags & IFF_PROMISC) {
Sathya Perla8788fdc2009-07-27 22:52:03 +0000684 be_cmd_promiscuous_config(adapter, adapter->port_num, 1);
Sathya Perla24307ee2009-06-18 00:09:25 +0000685 adapter->promiscuous = true;
686 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700687 }
Sathya Perla24307ee2009-06-18 00:09:25 +0000688
689 /* BE was previously in promiscous mode; disable it */
690 if (adapter->promiscuous) {
691 adapter->promiscuous = false;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000692 be_cmd_promiscuous_config(adapter, adapter->port_num, 0);
Sathya Perla24307ee2009-06-18 00:09:25 +0000693 }
694
Sathya Perlae7b909a2009-11-22 22:01:10 +0000695 /* Enable multicast promisc if num configured exceeds what we support */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000696 if (netdev->flags & IFF_ALLMULTI ||
697 netdev_mc_count(netdev) > BE_MAX_MC) {
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000698 be_cmd_multicast_set(adapter, adapter->if_handle, NULL,
Sathya Perlae7b909a2009-11-22 22:01:10 +0000699 &adapter->mc_cmd_mem);
Sathya Perla24307ee2009-06-18 00:09:25 +0000700 goto done;
701 }
702
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000703 be_cmd_multicast_set(adapter, adapter->if_handle, netdev,
Sathya Perlaf31e50a2010-03-02 03:56:39 -0800704 &adapter->mc_cmd_mem);
Sathya Perla24307ee2009-06-18 00:09:25 +0000705done:
706 return;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700707}
708
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000709static int be_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
710{
711 struct be_adapter *adapter = netdev_priv(netdev);
712 int status;
713
714 if (!adapter->sriov_enabled)
715 return -EPERM;
716
717 if (!is_valid_ether_addr(mac) || (vf >= num_vfs))
718 return -EINVAL;
719
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000720 if (adapter->vf_cfg[vf].vf_pmac_id != BE_INVALID_PMAC_ID)
721 status = be_cmd_pmac_del(adapter,
722 adapter->vf_cfg[vf].vf_if_handle,
723 adapter->vf_cfg[vf].vf_pmac_id);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000724
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000725 status = be_cmd_pmac_add(adapter, mac,
726 adapter->vf_cfg[vf].vf_if_handle,
727 &adapter->vf_cfg[vf].vf_pmac_id);
728
729 if (status)
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000730 dev_err(&adapter->pdev->dev, "MAC %pM set on VF %d Failed\n",
731 mac, vf);
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000732 else
733 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
734
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000735 return status;
736}
737
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000738static int be_get_vf_config(struct net_device *netdev, int vf,
739 struct ifla_vf_info *vi)
740{
741 struct be_adapter *adapter = netdev_priv(netdev);
742
743 if (!adapter->sriov_enabled)
744 return -EPERM;
745
746 if (vf >= num_vfs)
747 return -EINVAL;
748
749 vi->vf = vf;
Ajit Khapardee1d18732010-07-23 01:52:13 +0000750 vi->tx_rate = adapter->vf_cfg[vf].vf_tx_rate;
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000751 vi->vlan = adapter->vf_cfg[vf].vf_vlan_tag;
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000752 vi->qos = 0;
753 memcpy(&vi->mac, adapter->vf_cfg[vf].vf_mac_addr, ETH_ALEN);
754
755 return 0;
756}
757
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000758static int be_set_vf_vlan(struct net_device *netdev,
759 int vf, u16 vlan, u8 qos)
760{
761 struct be_adapter *adapter = netdev_priv(netdev);
762 int status = 0;
763
764 if (!adapter->sriov_enabled)
765 return -EPERM;
766
767 if ((vf >= num_vfs) || (vlan > 4095))
768 return -EINVAL;
769
770 if (vlan) {
771 adapter->vf_cfg[vf].vf_vlan_tag = vlan;
772 adapter->vlans_added++;
773 } else {
774 adapter->vf_cfg[vf].vf_vlan_tag = 0;
775 adapter->vlans_added--;
776 }
777
778 status = be_vid_config(adapter, true, vf);
779
780 if (status)
781 dev_info(&adapter->pdev->dev,
782 "VLAN %d config on VF %d failed\n", vlan, vf);
783 return status;
784}
785
Ajit Khapardee1d18732010-07-23 01:52:13 +0000786static int be_set_vf_tx_rate(struct net_device *netdev,
787 int vf, int rate)
788{
789 struct be_adapter *adapter = netdev_priv(netdev);
790 int status = 0;
791
792 if (!adapter->sriov_enabled)
793 return -EPERM;
794
795 if ((vf >= num_vfs) || (rate < 0))
796 return -EINVAL;
797
798 if (rate > 10000)
799 rate = 10000;
800
801 adapter->vf_cfg[vf].vf_tx_rate = rate;
802 status = be_cmd_set_qos(adapter, rate / 10, vf);
803
804 if (status)
805 dev_info(&adapter->pdev->dev,
806 "tx rate %d on VF %d failed\n", rate, vf);
807 return status;
808}
809
Sathya Perla3abcded2010-10-03 22:12:27 -0700810static void be_rx_rate_update(struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700811{
Sathya Perla3abcded2010-10-03 22:12:27 -0700812 struct be_rx_stats *stats = &rxo->stats;
Sathya Perla4097f662009-03-24 16:40:13 -0700813 ulong now = jiffies;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700814
Sathya Perla4097f662009-03-24 16:40:13 -0700815 /* Wrapped around */
Sathya Perla3abcded2010-10-03 22:12:27 -0700816 if (time_before(now, stats->rx_jiffies)) {
817 stats->rx_jiffies = now;
Sathya Perla4097f662009-03-24 16:40:13 -0700818 return;
819 }
820
821 /* Update the rate once in two seconds */
Sathya Perla3abcded2010-10-03 22:12:27 -0700822 if ((now - stats->rx_jiffies) < 2 * HZ)
Sathya Perla4097f662009-03-24 16:40:13 -0700823 return;
824
Sathya Perla3abcded2010-10-03 22:12:27 -0700825 stats->rx_rate = be_calc_rate(stats->rx_bytes - stats->rx_bytes_prev,
826 now - stats->rx_jiffies);
827 stats->rx_jiffies = now;
828 stats->rx_bytes_prev = stats->rx_bytes;
Sathya Perla4097f662009-03-24 16:40:13 -0700829}
830
Sathya Perla3abcded2010-10-03 22:12:27 -0700831static void be_rx_stats_update(struct be_rx_obj *rxo,
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +0000832 u32 pktsize, u16 numfrags, u8 pkt_type)
Sathya Perla4097f662009-03-24 16:40:13 -0700833{
Sathya Perla3abcded2010-10-03 22:12:27 -0700834 struct be_rx_stats *stats = &rxo->stats;
Sathya Perla4097f662009-03-24 16:40:13 -0700835
Sathya Perla3abcded2010-10-03 22:12:27 -0700836 stats->rx_compl++;
837 stats->rx_frags += numfrags;
838 stats->rx_bytes += pktsize;
839 stats->rx_pkts++;
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +0000840 if (pkt_type == BE_MULTICAST_PACKET)
Sathya Perla3abcded2010-10-03 22:12:27 -0700841 stats->rx_mcast_pkts++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700842}
843
Ajit Khaparde728a9972009-04-13 15:41:22 -0700844static inline bool do_pkt_csum(struct be_eth_rx_compl *rxcp, bool cso)
845{
846 u8 l4_cksm, ip_version, ipcksm, tcpf = 0, udpf = 0, ipv6_chk;
847
848 l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp);
849 ipcksm = AMAP_GET_BITS(struct amap_eth_rx_compl, ipcksm, rxcp);
850 ip_version = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
851 if (ip_version) {
852 tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
853 udpf = AMAP_GET_BITS(struct amap_eth_rx_compl, udpf, rxcp);
854 }
855 ipv6_chk = (ip_version && (tcpf || udpf));
856
857 return ((l4_cksm && ipv6_chk && ipcksm) && cso) ? false : true;
858}
859
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700860static struct be_rx_page_info *
Sathya Perla3abcded2010-10-03 22:12:27 -0700861get_rx_page_info(struct be_adapter *adapter,
862 struct be_rx_obj *rxo,
863 u16 frag_idx)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700864{
865 struct be_rx_page_info *rx_page_info;
Sathya Perla3abcded2010-10-03 22:12:27 -0700866 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700867
Sathya Perla3abcded2010-10-03 22:12:27 -0700868 rx_page_info = &rxo->page_info_tbl[frag_idx];
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700869 BUG_ON(!rx_page_info->page);
870
Ajit Khaparde205859a2010-02-09 01:34:21 +0000871 if (rx_page_info->last_page_user) {
FUJITA Tomonorifac6da52010-04-01 16:53:22 +0000872 pci_unmap_page(adapter->pdev, dma_unmap_addr(rx_page_info, bus),
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700873 adapter->big_page_size, PCI_DMA_FROMDEVICE);
Ajit Khaparde205859a2010-02-09 01:34:21 +0000874 rx_page_info->last_page_user = false;
875 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700876
877 atomic_dec(&rxq->used);
878 return rx_page_info;
879}
880
881/* Throwaway the data in the Rx completion */
882static void be_rx_compl_discard(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -0700883 struct be_rx_obj *rxo,
884 struct be_eth_rx_compl *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700885{
Sathya Perla3abcded2010-10-03 22:12:27 -0700886 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700887 struct be_rx_page_info *page_info;
888 u16 rxq_idx, i, num_rcvd;
889
890 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
891 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
892
893 for (i = 0; i < num_rcvd; i++) {
Sathya Perla3abcded2010-10-03 22:12:27 -0700894 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700895 put_page(page_info->page);
896 memset(page_info, 0, sizeof(*page_info));
897 index_inc(&rxq_idx, rxq->len);
898 }
899}
900
901/*
902 * skb_fill_rx_data forms a complete skb for an ether frame
903 * indicated by rxcp.
904 */
Sathya Perla3abcded2010-10-03 22:12:27 -0700905static void skb_fill_rx_data(struct be_adapter *adapter, struct be_rx_obj *rxo,
Sathya Perla89420422010-02-17 01:35:26 +0000906 struct sk_buff *skb, struct be_eth_rx_compl *rxcp,
907 u16 num_rcvd)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700908{
Sathya Perla3abcded2010-10-03 22:12:27 -0700909 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700910 struct be_rx_page_info *page_info;
Sathya Perla89420422010-02-17 01:35:26 +0000911 u16 rxq_idx, i, j;
Ajit Khapardefa774062009-07-22 09:28:55 -0700912 u32 pktsize, hdr_len, curr_frag_len, size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700913 u8 *start;
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +0000914 u8 pkt_type;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700915
916 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
917 pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +0000918 pkt_type = AMAP_GET_BITS(struct amap_eth_rx_compl, cast_enc, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700919
Sathya Perla3abcded2010-10-03 22:12:27 -0700920 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700921
922 start = page_address(page_info->page) + page_info->page_offset;
923 prefetch(start);
924
925 /* Copy data in the first descriptor of this completion */
926 curr_frag_len = min(pktsize, rx_frag_size);
927
928 /* Copy the header portion into skb_data */
929 hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
930 memcpy(skb->data, start, hdr_len);
931 skb->len = curr_frag_len;
932 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
933 /* Complete packet has now been moved to data */
934 put_page(page_info->page);
935 skb->data_len = 0;
936 skb->tail += curr_frag_len;
937 } else {
938 skb_shinfo(skb)->nr_frags = 1;
939 skb_shinfo(skb)->frags[0].page = page_info->page;
940 skb_shinfo(skb)->frags[0].page_offset =
941 page_info->page_offset + hdr_len;
942 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
943 skb->data_len = curr_frag_len - hdr_len;
944 skb->tail += hdr_len;
945 }
Ajit Khaparde205859a2010-02-09 01:34:21 +0000946 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700947
948 if (pktsize <= rx_frag_size) {
949 BUG_ON(num_rcvd != 1);
Sathya Perla76fbb422009-06-10 02:21:56 +0000950 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700951 }
952
953 /* More frags present for this completion */
Ajit Khapardefa774062009-07-22 09:28:55 -0700954 size = pktsize;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000955 for (i = 1, j = 0; i < num_rcvd; i++) {
Ajit Khapardefa774062009-07-22 09:28:55 -0700956 size -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700957 index_inc(&rxq_idx, rxq->len);
Sathya Perla3abcded2010-10-03 22:12:27 -0700958 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700959
Ajit Khapardefa774062009-07-22 09:28:55 -0700960 curr_frag_len = min(size, rx_frag_size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700961
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000962 /* Coalesce all frags from the same physical page in one slot */
963 if (page_info->page_offset == 0) {
964 /* Fresh page */
965 j++;
966 skb_shinfo(skb)->frags[j].page = page_info->page;
967 skb_shinfo(skb)->frags[j].page_offset =
968 page_info->page_offset;
969 skb_shinfo(skb)->frags[j].size = 0;
970 skb_shinfo(skb)->nr_frags++;
971 } else {
972 put_page(page_info->page);
973 }
974
975 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700976 skb->len += curr_frag_len;
977 skb->data_len += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700978
Ajit Khaparde205859a2010-02-09 01:34:21 +0000979 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700980 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000981 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700982
Sathya Perla76fbb422009-06-10 02:21:56 +0000983done:
Sathya Perla3abcded2010-10-03 22:12:27 -0700984 be_rx_stats_update(rxo, pktsize, num_rcvd, pkt_type);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700985}
986
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700987/* Process the RX completion indicated by rxcp when GRO is disabled */
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700988static void be_rx_compl_process(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -0700989 struct be_rx_obj *rxo,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700990 struct be_eth_rx_compl *rxcp)
991{
992 struct sk_buff *skb;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700993 u32 vlanf, vid;
Sathya Perla89420422010-02-17 01:35:26 +0000994 u16 num_rcvd;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700995 u8 vtm;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700996
Sathya Perla89420422010-02-17 01:35:26 +0000997 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
998 /* Is it a flush compl that has no data */
999 if (unlikely(num_rcvd == 0))
1000 return;
1001
Eric Dumazet89d71a62009-10-13 05:34:20 +00001002 skb = netdev_alloc_skb_ip_align(adapter->netdev, BE_HDR_LEN);
Sathya Perlaa058a632010-02-17 01:34:22 +00001003 if (unlikely(!skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001004 if (net_ratelimit())
1005 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
Sathya Perla3abcded2010-10-03 22:12:27 -07001006 be_rx_compl_discard(adapter, rxo, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001007 return;
1008 }
1009
Sathya Perla3abcded2010-10-03 22:12:27 -07001010 skb_fill_rx_data(adapter, rxo, skb, rxcp, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001011
Ajit Khaparde728a9972009-04-13 15:41:22 -07001012 if (do_pkt_csum(rxcp, adapter->rx_csum))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001013 skb_checksum_none_assert(skb);
Ajit Khaparde728a9972009-04-13 15:41:22 -07001014 else
1015 skb->ip_summed = CHECKSUM_UNNECESSARY;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001016
1017 skb->truesize = skb->len + sizeof(struct sk_buff);
1018 skb->protocol = eth_type_trans(skb, adapter->netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001019
Sathya Perlaa058a632010-02-17 01:34:22 +00001020 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
1021 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
1022
1023 /* vlanf could be wrongly set in some cards.
1024 * ignore if vtm is not set */
Ajit Khaparde3486be22010-07-23 02:04:54 +00001025 if ((adapter->function_mode & 0x400) && !vtm)
Sathya Perlaa058a632010-02-17 01:34:22 +00001026 vlanf = 0;
1027
1028 if (unlikely(vlanf)) {
Ajit Khaparde82903e42010-02-09 01:34:57 +00001029 if (!adapter->vlan_grp || adapter->vlans_added == 0) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001030 kfree_skb(skb);
1031 return;
1032 }
1033 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
Ajit Khaparde9cae9e42010-03-31 02:00:32 +00001034 vid = swab16(vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001035 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
1036 } else {
1037 netif_receive_skb(skb);
1038 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001039}
1040
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001041/* Process the RX completion indicated by rxcp when GRO is enabled */
1042static void be_rx_compl_process_gro(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -07001043 struct be_rx_obj *rxo,
1044 struct be_eth_rx_compl *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001045{
1046 struct be_rx_page_info *page_info;
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001047 struct sk_buff *skb = NULL;
Sathya Perla3abcded2010-10-03 22:12:27 -07001048 struct be_queue_info *rxq = &rxo->q;
1049 struct be_eq_obj *eq_obj = &rxo->rx_eq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001050 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001051 u16 i, rxq_idx = 0, vid, j;
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001052 u8 vtm;
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +00001053 u8 pkt_type;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001054
1055 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
Sathya Perla89420422010-02-17 01:35:26 +00001056 /* Is it a flush compl that has no data */
1057 if (unlikely(num_rcvd == 0))
1058 return;
1059
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001060 pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
1061 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
1062 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001063 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +00001064 pkt_type = AMAP_GET_BITS(struct amap_eth_rx_compl, cast_enc, rxcp);
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001065
1066 /* vlanf could be wrongly set in some cards.
1067 * ignore if vtm is not set */
Ajit Khaparde3486be22010-07-23 02:04:54 +00001068 if ((adapter->function_mode & 0x400) && !vtm)
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001069 vlanf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001070
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001071 skb = napi_get_frags(&eq_obj->napi);
1072 if (!skb) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001073 be_rx_compl_discard(adapter, rxo, rxcp);
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001074 return;
1075 }
1076
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001077 remaining = pkt_size;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001078 for (i = 0, j = -1; i < num_rcvd; i++) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001079 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001080
1081 curr_frag_len = min(remaining, rx_frag_size);
1082
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001083 /* Coalesce all frags from the same physical page in one slot */
1084 if (i == 0 || page_info->page_offset == 0) {
1085 /* First frag or Fresh page */
1086 j++;
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001087 skb_shinfo(skb)->frags[j].page = page_info->page;
1088 skb_shinfo(skb)->frags[j].page_offset =
1089 page_info->page_offset;
1090 skb_shinfo(skb)->frags[j].size = 0;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001091 } else {
1092 put_page(page_info->page);
1093 }
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001094 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001095
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001096 remaining -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001097 index_inc(&rxq_idx, rxq->len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001098 memset(page_info, 0, sizeof(*page_info));
1099 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001100 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001101
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001102 skb_shinfo(skb)->nr_frags = j + 1;
1103 skb->len = pkt_size;
1104 skb->data_len = pkt_size;
1105 skb->truesize += pkt_size;
1106 skb->ip_summed = CHECKSUM_UNNECESSARY;
1107
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001108 if (likely(!vlanf)) {
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001109 napi_gro_frags(&eq_obj->napi);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001110 } else {
1111 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
Ajit Khaparde9cae9e42010-03-31 02:00:32 +00001112 vid = swab16(vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001113
Ajit Khaparde82903e42010-02-09 01:34:57 +00001114 if (!adapter->vlan_grp || adapter->vlans_added == 0)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001115 return;
1116
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001117 vlan_gro_frags(&eq_obj->napi, adapter->vlan_grp, vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001118 }
1119
Sathya Perla3abcded2010-10-03 22:12:27 -07001120 be_rx_stats_update(rxo, pkt_size, num_rcvd, pkt_type);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001121}
1122
Sathya Perla3abcded2010-10-03 22:12:27 -07001123static struct be_eth_rx_compl *be_rx_compl_get(struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001124{
Sathya Perla3abcded2010-10-03 22:12:27 -07001125 struct be_eth_rx_compl *rxcp = queue_tail_node(&rxo->cq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001126
1127 if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
1128 return NULL;
1129
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001130 rmb();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001131 be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
1132
Sathya Perla3abcded2010-10-03 22:12:27 -07001133 queue_tail_inc(&rxo->cq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001134 return rxcp;
1135}
1136
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001137/* To reset the valid bit, we need to reset the whole word as
1138 * when walking the queue the valid entries are little-endian
1139 * and invalid entries are host endian
1140 */
1141static inline void be_rx_compl_reset(struct be_eth_rx_compl *rxcp)
1142{
1143 rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
1144}
1145
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001146static inline struct page *be_alloc_pages(u32 size)
1147{
1148 gfp_t alloc_flags = GFP_ATOMIC;
1149 u32 order = get_order(size);
1150 if (order > 0)
1151 alloc_flags |= __GFP_COMP;
1152 return alloc_pages(alloc_flags, order);
1153}
1154
1155/*
1156 * Allocate a page, split it to fragments of size rx_frag_size and post as
1157 * receive buffers to BE
1158 */
Sathya Perla3abcded2010-10-03 22:12:27 -07001159static void be_post_rx_frags(struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001160{
Sathya Perla3abcded2010-10-03 22:12:27 -07001161 struct be_adapter *adapter = rxo->adapter;
1162 struct be_rx_page_info *page_info_tbl = rxo->page_info_tbl;
Sathya Perla26d92f92010-01-21 22:52:08 -08001163 struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
Sathya Perla3abcded2010-10-03 22:12:27 -07001164 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001165 struct page *pagep = NULL;
1166 struct be_eth_rx_d *rxd;
1167 u64 page_dmaaddr = 0, frag_dmaaddr;
1168 u32 posted, page_offset = 0;
1169
Sathya Perla3abcded2010-10-03 22:12:27 -07001170 page_info = &rxo->page_info_tbl[rxq->head];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001171 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
1172 if (!pagep) {
1173 pagep = be_alloc_pages(adapter->big_page_size);
1174 if (unlikely(!pagep)) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001175 rxo->stats.rx_post_fail++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001176 break;
1177 }
1178 page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
1179 adapter->big_page_size,
1180 PCI_DMA_FROMDEVICE);
1181 page_info->page_offset = 0;
1182 } else {
1183 get_page(pagep);
1184 page_info->page_offset = page_offset + rx_frag_size;
1185 }
1186 page_offset = page_info->page_offset;
1187 page_info->page = pagep;
FUJITA Tomonorifac6da52010-04-01 16:53:22 +00001188 dma_unmap_addr_set(page_info, bus, page_dmaaddr);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001189 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
1190
1191 rxd = queue_head_node(rxq);
1192 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
1193 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001194
1195 /* Any space left in the current big page for another frag? */
1196 if ((page_offset + rx_frag_size + rx_frag_size) >
1197 adapter->big_page_size) {
1198 pagep = NULL;
1199 page_info->last_page_user = true;
1200 }
Sathya Perla26d92f92010-01-21 22:52:08 -08001201
1202 prev_page_info = page_info;
1203 queue_head_inc(rxq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001204 page_info = &page_info_tbl[rxq->head];
1205 }
1206 if (pagep)
Sathya Perla26d92f92010-01-21 22:52:08 -08001207 prev_page_info->last_page_user = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001208
1209 if (posted) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001210 atomic_add(posted, &rxq->used);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001211 be_rxq_notify(adapter, rxq->id, posted);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001212 } else if (atomic_read(&rxq->used) == 0) {
1213 /* Let be_worker replenish when memory is available */
Sathya Perla3abcded2010-10-03 22:12:27 -07001214 rxo->rx_post_starved = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001215 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001216}
1217
Sathya Perla5fb379e2009-06-18 00:02:59 +00001218static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001219{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001220 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
1221
1222 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
1223 return NULL;
1224
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001225 rmb();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001226 be_dws_le_to_cpu(txcp, sizeof(*txcp));
1227
1228 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
1229
1230 queue_tail_inc(tx_cq);
1231 return txcp;
1232}
1233
1234static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
1235{
1236 struct be_queue_info *txq = &adapter->tx_obj.q;
Alexander Duycka73b7962009-12-02 16:48:18 +00001237 struct be_eth_wrb *wrb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001238 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1239 struct sk_buff *sent_skb;
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001240 u16 cur_index, num_wrbs = 1; /* account for hdr wrb */
1241 bool unmap_skb_hdr = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001242
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001243 sent_skb = sent_skbs[txq->tail];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001244 BUG_ON(!sent_skb);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001245 sent_skbs[txq->tail] = NULL;
1246
1247 /* skip header wrb */
Alexander Duycka73b7962009-12-02 16:48:18 +00001248 queue_tail_inc(txq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001249
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001250 do {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001251 cur_index = txq->tail;
Alexander Duycka73b7962009-12-02 16:48:18 +00001252 wrb = queue_tail_node(txq);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001253 unmap_tx_frag(adapter->pdev, wrb, (unmap_skb_hdr &&
Eric Dumazete743d312010-04-14 15:59:40 -07001254 skb_headlen(sent_skb)));
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001255 unmap_skb_hdr = false;
1256
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001257 num_wrbs++;
1258 queue_tail_inc(txq);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001259 } while (cur_index != last_index);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001260
1261 atomic_sub(num_wrbs, &txq->used);
Alexander Duycka73b7962009-12-02 16:48:18 +00001262
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001263 kfree_skb(sent_skb);
1264}
1265
Sathya Perla859b1e42009-08-10 03:43:51 +00001266static inline struct be_eq_entry *event_get(struct be_eq_obj *eq_obj)
1267{
1268 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1269
1270 if (!eqe->evt)
1271 return NULL;
1272
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001273 rmb();
Sathya Perla859b1e42009-08-10 03:43:51 +00001274 eqe->evt = le32_to_cpu(eqe->evt);
1275 queue_tail_inc(&eq_obj->q);
1276 return eqe;
1277}
1278
1279static int event_handle(struct be_adapter *adapter,
1280 struct be_eq_obj *eq_obj)
1281{
1282 struct be_eq_entry *eqe;
1283 u16 num = 0;
1284
1285 while ((eqe = event_get(eq_obj)) != NULL) {
1286 eqe->evt = 0;
1287 num++;
1288 }
1289
1290 /* Deal with any spurious interrupts that come
1291 * without events
1292 */
1293 be_eq_notify(adapter, eq_obj->q.id, true, true, num);
1294 if (num)
1295 napi_schedule(&eq_obj->napi);
1296
1297 return num;
1298}
1299
1300/* Just read and notify events without processing them.
1301 * Used at the time of destroying event queues */
1302static void be_eq_clean(struct be_adapter *adapter,
1303 struct be_eq_obj *eq_obj)
1304{
1305 struct be_eq_entry *eqe;
1306 u16 num = 0;
1307
1308 while ((eqe = event_get(eq_obj)) != NULL) {
1309 eqe->evt = 0;
1310 num++;
1311 }
1312
1313 if (num)
1314 be_eq_notify(adapter, eq_obj->q.id, false, true, num);
1315}
1316
Sathya Perla3abcded2010-10-03 22:12:27 -07001317static void be_rx_q_clean(struct be_adapter *adapter, struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001318{
1319 struct be_rx_page_info *page_info;
Sathya Perla3abcded2010-10-03 22:12:27 -07001320 struct be_queue_info *rxq = &rxo->q;
1321 struct be_queue_info *rx_cq = &rxo->cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001322 struct be_eth_rx_compl *rxcp;
1323 u16 tail;
1324
1325 /* First cleanup pending rx completions */
Sathya Perla3abcded2010-10-03 22:12:27 -07001326 while ((rxcp = be_rx_compl_get(rxo)) != NULL) {
1327 be_rx_compl_discard(adapter, rxo, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001328 be_rx_compl_reset(rxcp);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001329 be_cq_notify(adapter, rx_cq->id, true, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001330 }
1331
1332 /* Then free posted rx buffer that were not used */
1333 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
Sathya Perlacdab23b2009-08-10 03:43:23 +00001334 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001335 page_info = get_rx_page_info(adapter, rxo, tail);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001336 put_page(page_info->page);
1337 memset(page_info, 0, sizeof(*page_info));
1338 }
1339 BUG_ON(atomic_read(&rxq->used));
1340}
1341
Sathya Perlaa8e91792009-08-10 03:42:43 +00001342static void be_tx_compl_clean(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001343{
Sathya Perlaa8e91792009-08-10 03:42:43 +00001344 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001345 struct be_queue_info *txq = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001346 struct be_eth_tx_compl *txcp;
1347 u16 end_idx, cmpl = 0, timeo = 0;
Sathya Perlab03388d2010-02-18 00:37:17 +00001348 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1349 struct sk_buff *sent_skb;
1350 bool dummy_wrb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001351
Sathya Perlaa8e91792009-08-10 03:42:43 +00001352 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1353 do {
1354 while ((txcp = be_tx_compl_get(tx_cq))) {
1355 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1356 wrb_index, txcp);
1357 be_tx_compl_process(adapter, end_idx);
1358 cmpl++;
1359 }
1360 if (cmpl) {
1361 be_cq_notify(adapter, tx_cq->id, false, cmpl);
1362 cmpl = 0;
1363 }
1364
1365 if (atomic_read(&txq->used) == 0 || ++timeo > 200)
1366 break;
1367
1368 mdelay(1);
1369 } while (true);
1370
1371 if (atomic_read(&txq->used))
1372 dev_err(&adapter->pdev->dev, "%d pending tx-completions\n",
1373 atomic_read(&txq->used));
Sathya Perlab03388d2010-02-18 00:37:17 +00001374
1375 /* free posted tx for which compls will never arrive */
1376 while (atomic_read(&txq->used)) {
1377 sent_skb = sent_skbs[txq->tail];
1378 end_idx = txq->tail;
1379 index_adv(&end_idx,
1380 wrb_cnt_for_skb(sent_skb, &dummy_wrb) - 1, txq->len);
1381 be_tx_compl_process(adapter, end_idx);
1382 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001383}
1384
Sathya Perla5fb379e2009-06-18 00:02:59 +00001385static void be_mcc_queues_destroy(struct be_adapter *adapter)
1386{
1387 struct be_queue_info *q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001388
Sathya Perla8788fdc2009-07-27 22:52:03 +00001389 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001390 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001391 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001392 be_queue_free(adapter, q);
1393
Sathya Perla8788fdc2009-07-27 22:52:03 +00001394 q = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001395 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001396 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001397 be_queue_free(adapter, q);
1398}
1399
1400/* Must be called only after TX qs are created as MCC shares TX EQ */
1401static int be_mcc_queues_create(struct be_adapter *adapter)
1402{
1403 struct be_queue_info *q, *cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001404
1405 /* Alloc MCC compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001406 cq = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001407 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
Sathya Perlaefd2e402009-07-27 22:53:10 +00001408 sizeof(struct be_mcc_compl)))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001409 goto err;
1410
1411 /* Ask BE to create MCC compl queue; share TX's eq */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001412 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001413 goto mcc_cq_free;
1414
1415 /* Alloc MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001416 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001417 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1418 goto mcc_cq_destroy;
1419
1420 /* Ask BE to create MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001421 if (be_cmd_mccq_create(adapter, q, cq))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001422 goto mcc_q_free;
1423
1424 return 0;
1425
1426mcc_q_free:
1427 be_queue_free(adapter, q);
1428mcc_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001429 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001430mcc_cq_free:
1431 be_queue_free(adapter, cq);
1432err:
1433 return -1;
1434}
1435
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001436static void be_tx_queues_destroy(struct be_adapter *adapter)
1437{
1438 struct be_queue_info *q;
1439
1440 q = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001441 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001442 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001443 be_queue_free(adapter, q);
1444
1445 q = &adapter->tx_obj.cq;
1446 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001447 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001448 be_queue_free(adapter, q);
1449
Sathya Perla859b1e42009-08-10 03:43:51 +00001450 /* Clear any residual events */
1451 be_eq_clean(adapter, &adapter->tx_eq);
1452
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001453 q = &adapter->tx_eq.q;
1454 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001455 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001456 be_queue_free(adapter, q);
1457}
1458
1459static int be_tx_queues_create(struct be_adapter *adapter)
1460{
1461 struct be_queue_info *eq, *q, *cq;
1462
1463 adapter->tx_eq.max_eqd = 0;
1464 adapter->tx_eq.min_eqd = 0;
1465 adapter->tx_eq.cur_eqd = 96;
1466 adapter->tx_eq.enable_aic = false;
1467 /* Alloc Tx Event queue */
1468 eq = &adapter->tx_eq.q;
1469 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1470 return -1;
1471
1472 /* Ask BE to create Tx Event queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001473 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001474 goto tx_eq_free;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001475 adapter->base_eq_id = adapter->tx_eq.q.id;
1476
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001477 /* Alloc TX eth compl queue */
1478 cq = &adapter->tx_obj.cq;
1479 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1480 sizeof(struct be_eth_tx_compl)))
1481 goto tx_eq_destroy;
1482
1483 /* Ask BE to create Tx eth compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001484 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001485 goto tx_cq_free;
1486
1487 /* Alloc TX eth queue */
1488 q = &adapter->tx_obj.q;
1489 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1490 goto tx_cq_destroy;
1491
1492 /* Ask BE to create Tx eth queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001493 if (be_cmd_txq_create(adapter, q, cq))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001494 goto tx_q_free;
1495 return 0;
1496
1497tx_q_free:
1498 be_queue_free(adapter, q);
1499tx_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001500 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001501tx_cq_free:
1502 be_queue_free(adapter, cq);
1503tx_eq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001504 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001505tx_eq_free:
1506 be_queue_free(adapter, eq);
1507 return -1;
1508}
1509
1510static void be_rx_queues_destroy(struct be_adapter *adapter)
1511{
1512 struct be_queue_info *q;
Sathya Perla3abcded2010-10-03 22:12:27 -07001513 struct be_rx_obj *rxo;
1514 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001515
Sathya Perla3abcded2010-10-03 22:12:27 -07001516 for_all_rx_queues(adapter, rxo, i) {
1517 q = &rxo->q;
1518 if (q->created) {
1519 be_cmd_q_destroy(adapter, q, QTYPE_RXQ);
1520 /* After the rxq is invalidated, wait for a grace time
1521 * of 1ms for all dma to end and the flush compl to
1522 * arrive
1523 */
1524 mdelay(1);
1525 be_rx_q_clean(adapter, rxo);
1526 }
1527 be_queue_free(adapter, q);
Sathya Perla89420422010-02-17 01:35:26 +00001528
Sathya Perla3abcded2010-10-03 22:12:27 -07001529 q = &rxo->cq;
1530 if (q->created)
1531 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1532 be_queue_free(adapter, q);
1533
1534 /* Clear any residual events */
1535 q = &rxo->rx_eq.q;
1536 if (q->created) {
1537 be_eq_clean(adapter, &rxo->rx_eq);
1538 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
1539 }
1540 be_queue_free(adapter, q);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001541 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001542}
1543
1544static int be_rx_queues_create(struct be_adapter *adapter)
1545{
1546 struct be_queue_info *eq, *q, *cq;
Sathya Perla3abcded2010-10-03 22:12:27 -07001547 struct be_rx_obj *rxo;
1548 int rc, i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001549
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001550 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
Sathya Perla3abcded2010-10-03 22:12:27 -07001551 for_all_rx_queues(adapter, rxo, i) {
1552 rxo->adapter = adapter;
1553 rxo->rx_eq.max_eqd = BE_MAX_EQD;
1554 rxo->rx_eq.enable_aic = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001555
Sathya Perla3abcded2010-10-03 22:12:27 -07001556 /* EQ */
1557 eq = &rxo->rx_eq.q;
1558 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1559 sizeof(struct be_eq_entry));
1560 if (rc)
1561 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001562
Sathya Perla3abcded2010-10-03 22:12:27 -07001563 rc = be_cmd_eq_create(adapter, eq, rxo->rx_eq.cur_eqd);
1564 if (rc)
1565 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001566
Sathya Perla3abcded2010-10-03 22:12:27 -07001567 /* CQ */
1568 cq = &rxo->cq;
1569 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1570 sizeof(struct be_eth_rx_compl));
1571 if (rc)
1572 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001573
Sathya Perla3abcded2010-10-03 22:12:27 -07001574 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
1575 if (rc)
1576 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001577
Sathya Perla3abcded2010-10-03 22:12:27 -07001578 /* Rx Q */
1579 q = &rxo->q;
1580 rc = be_queue_alloc(adapter, q, RX_Q_LEN,
1581 sizeof(struct be_eth_rx_d));
1582 if (rc)
1583 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001584
Sathya Perla3abcded2010-10-03 22:12:27 -07001585 rc = be_cmd_rxq_create(adapter, q, cq->id, rx_frag_size,
1586 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle,
1587 (i > 0) ? 1 : 0/* rss enable */, &rxo->rss_id);
1588 if (rc)
1589 goto err;
1590 }
1591
1592 if (be_multi_rxq(adapter)) {
1593 u8 rsstable[MAX_RSS_QS];
1594
1595 for_all_rss_queues(adapter, rxo, i)
1596 rsstable[i] = rxo->rss_id;
1597
1598 rc = be_cmd_rss_config(adapter, rsstable,
1599 adapter->num_rx_qs - 1);
1600 if (rc)
1601 goto err;
1602 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001603
1604 return 0;
Sathya Perla3abcded2010-10-03 22:12:27 -07001605err:
1606 be_rx_queues_destroy(adapter);
1607 return -1;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001608}
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001609
Sathya Perlab628bde2009-08-17 00:58:26 +00001610/* There are 8 evt ids per func. Retruns the evt id's bit number */
1611static inline int be_evt_bit_get(struct be_adapter *adapter, u32 eq_id)
1612{
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001613 return eq_id - adapter->base_eq_id;
Sathya Perlab628bde2009-08-17 00:58:26 +00001614}
1615
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001616static irqreturn_t be_intx(int irq, void *dev)
1617{
1618 struct be_adapter *adapter = dev;
Sathya Perla3abcded2010-10-03 22:12:27 -07001619 struct be_rx_obj *rxo;
1620 int isr, i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001621
Sathya Perla8788fdc2009-07-27 22:52:03 +00001622 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
Sathya Perla55bdeed2010-02-02 07:48:40 -08001623 (adapter->tx_eq.q.id/ 8) * CEV_ISR_SIZE);
Sathya Perlac001c212009-07-01 01:06:07 +00001624 if (!isr)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001625 return IRQ_NONE;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001626
Sathya Perla3abcded2010-10-03 22:12:27 -07001627 if ((1 << be_evt_bit_get(adapter, adapter->tx_eq.q.id) & isr))
1628 event_handle(adapter, &adapter->tx_eq);
1629
1630 for_all_rx_queues(adapter, rxo, i) {
1631 if ((1 << be_evt_bit_get(adapter, rxo->rx_eq.q.id) & isr))
1632 event_handle(adapter, &rxo->rx_eq);
1633 }
Sathya Perlac001c212009-07-01 01:06:07 +00001634
Sathya Perla8788fdc2009-07-27 22:52:03 +00001635 return IRQ_HANDLED;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001636}
1637
1638static irqreturn_t be_msix_rx(int irq, void *dev)
1639{
Sathya Perla3abcded2010-10-03 22:12:27 -07001640 struct be_rx_obj *rxo = dev;
1641 struct be_adapter *adapter = rxo->adapter;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001642
Sathya Perla3abcded2010-10-03 22:12:27 -07001643 event_handle(adapter, &rxo->rx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001644
1645 return IRQ_HANDLED;
1646}
1647
Sathya Perla5fb379e2009-06-18 00:02:59 +00001648static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001649{
1650 struct be_adapter *adapter = dev;
1651
Sathya Perla8788fdc2009-07-27 22:52:03 +00001652 event_handle(adapter, &adapter->tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001653
1654 return IRQ_HANDLED;
1655}
1656
Sathya Perla3abcded2010-10-03 22:12:27 -07001657static inline bool do_gro(struct be_adapter *adapter, struct be_rx_obj *rxo,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001658 struct be_eth_rx_compl *rxcp)
1659{
1660 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1661 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1662
1663 if (err)
Sathya Perla3abcded2010-10-03 22:12:27 -07001664 rxo->stats.rxcp_err++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001665
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001666 return (tcp_frame && !err) ? true : false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001667}
1668
1669int be_poll_rx(struct napi_struct *napi, int budget)
1670{
1671 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
Sathya Perla3abcded2010-10-03 22:12:27 -07001672 struct be_rx_obj *rxo = container_of(rx_eq, struct be_rx_obj, rx_eq);
1673 struct be_adapter *adapter = rxo->adapter;
1674 struct be_queue_info *rx_cq = &rxo->cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001675 struct be_eth_rx_compl *rxcp;
1676 u32 work_done;
1677
Sathya Perla3abcded2010-10-03 22:12:27 -07001678 rxo->stats.rx_polls++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001679 for (work_done = 0; work_done < budget; work_done++) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001680 rxcp = be_rx_compl_get(rxo);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001681 if (!rxcp)
1682 break;
1683
Sathya Perla3abcded2010-10-03 22:12:27 -07001684 if (do_gro(adapter, rxo, rxcp))
1685 be_rx_compl_process_gro(adapter, rxo, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001686 else
Sathya Perla3abcded2010-10-03 22:12:27 -07001687 be_rx_compl_process(adapter, rxo, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001688
1689 be_rx_compl_reset(rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001690 }
1691
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001692 /* Refill the queue */
Sathya Perla3abcded2010-10-03 22:12:27 -07001693 if (atomic_read(&rxo->q.used) < RX_FRAGS_REFILL_WM)
1694 be_post_rx_frags(rxo);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001695
1696 /* All consumed */
1697 if (work_done < budget) {
1698 napi_complete(napi);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001699 be_cq_notify(adapter, rx_cq->id, true, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001700 } else {
1701 /* More to be consumed; continue with interrupts disabled */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001702 be_cq_notify(adapter, rx_cq->id, false, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001703 }
1704 return work_done;
1705}
1706
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001707/* As TX and MCC share the same EQ check for both TX and MCC completions.
1708 * For TX/MCC we don't honour budget; consume everything
1709 */
1710static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001711{
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001712 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1713 struct be_adapter *adapter =
1714 container_of(tx_eq, struct be_adapter, tx_eq);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001715 struct be_queue_info *txq = &adapter->tx_obj.q;
1716 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001717 struct be_eth_tx_compl *txcp;
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001718 int tx_compl = 0, mcc_compl, status = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001719 u16 end_idx;
1720
Sathya Perla5fb379e2009-06-18 00:02:59 +00001721 while ((txcp = be_tx_compl_get(tx_cq))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001722 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001723 wrb_index, txcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001724 be_tx_compl_process(adapter, end_idx);
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001725 tx_compl++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001726 }
1727
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001728 mcc_compl = be_process_mcc(adapter, &status);
1729
1730 napi_complete(napi);
1731
1732 if (mcc_compl) {
1733 struct be_mcc_obj *mcc_obj = &adapter->mcc_obj;
1734 be_cq_notify(adapter, mcc_obj->cq.id, true, mcc_compl);
1735 }
1736
1737 if (tx_compl) {
1738 be_cq_notify(adapter, adapter->tx_obj.cq.id, true, tx_compl);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001739
1740 /* As Tx wrbs have been freed up, wake up netdev queue if
1741 * it was stopped due to lack of tx wrbs.
1742 */
1743 if (netif_queue_stopped(adapter->netdev) &&
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001744 atomic_read(&txq->used) < txq->len / 2) {
Sathya Perla5fb379e2009-06-18 00:02:59 +00001745 netif_wake_queue(adapter->netdev);
1746 }
1747
Sathya Perla3abcded2010-10-03 22:12:27 -07001748 tx_stats(adapter)->be_tx_events++;
1749 tx_stats(adapter)->be_tx_compl += tx_compl;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001750 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001751
1752 return 1;
1753}
1754
Ajit Khaparded053de92010-09-03 06:23:30 +00001755void be_detect_dump_ue(struct be_adapter *adapter)
Ajit Khaparde7c185272010-07-29 06:16:33 +00001756{
1757 u32 ue_status_lo, ue_status_hi, ue_status_lo_mask, ue_status_hi_mask;
1758 u32 i;
1759
1760 pci_read_config_dword(adapter->pdev,
1761 PCICFG_UE_STATUS_LOW, &ue_status_lo);
1762 pci_read_config_dword(adapter->pdev,
1763 PCICFG_UE_STATUS_HIGH, &ue_status_hi);
1764 pci_read_config_dword(adapter->pdev,
1765 PCICFG_UE_STATUS_LOW_MASK, &ue_status_lo_mask);
1766 pci_read_config_dword(adapter->pdev,
1767 PCICFG_UE_STATUS_HI_MASK, &ue_status_hi_mask);
1768
1769 ue_status_lo = (ue_status_lo & (~ue_status_lo_mask));
1770 ue_status_hi = (ue_status_hi & (~ue_status_hi_mask));
1771
Ajit Khaparded053de92010-09-03 06:23:30 +00001772 if (ue_status_lo || ue_status_hi) {
1773 adapter->ue_detected = true;
1774 dev_err(&adapter->pdev->dev, "UE Detected!!\n");
1775 }
1776
Ajit Khaparde7c185272010-07-29 06:16:33 +00001777 if (ue_status_lo) {
1778 for (i = 0; ue_status_lo; ue_status_lo >>= 1, i++) {
1779 if (ue_status_lo & 1)
1780 dev_err(&adapter->pdev->dev,
1781 "UE: %s bit set\n", ue_status_low_desc[i]);
1782 }
1783 }
1784 if (ue_status_hi) {
1785 for (i = 0; ue_status_hi; ue_status_hi >>= 1, i++) {
1786 if (ue_status_hi & 1)
1787 dev_err(&adapter->pdev->dev,
1788 "UE: %s bit set\n", ue_status_hi_desc[i]);
1789 }
1790 }
1791
1792}
1793
Sathya Perlaea1dae12009-03-19 23:56:20 -07001794static void be_worker(struct work_struct *work)
1795{
1796 struct be_adapter *adapter =
1797 container_of(work, struct be_adapter, work.work);
Sathya Perla3abcded2010-10-03 22:12:27 -07001798 struct be_rx_obj *rxo;
1799 int i;
Sathya Perlaea1dae12009-03-19 23:56:20 -07001800
Ajit Khaparde0fc48c32010-07-29 06:18:58 +00001801 if (!adapter->stats_ioctl_sent)
Sathya Perla3abcded2010-10-03 22:12:27 -07001802 be_cmd_get_stats(adapter, &adapter->stats_cmd);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001803
Sathya Perla4097f662009-03-24 16:40:13 -07001804 be_tx_rate_update(adapter);
Sathya Perla4097f662009-03-24 16:40:13 -07001805
Sathya Perla3abcded2010-10-03 22:12:27 -07001806 for_all_rx_queues(adapter, rxo, i) {
1807 be_rx_rate_update(rxo);
1808 be_rx_eqd_update(adapter, rxo);
1809
1810 if (rxo->rx_post_starved) {
1811 rxo->rx_post_starved = false;
1812 be_post_rx_frags(rxo);
1813 }
Sathya Perlaea1dae12009-03-19 23:56:20 -07001814 }
Sathya Perla3abcded2010-10-03 22:12:27 -07001815
Ajit Khaparded053de92010-09-03 06:23:30 +00001816 if (!adapter->ue_detected)
1817 be_detect_dump_ue(adapter);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001818
1819 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1820}
1821
Sathya Perla8d56ff12009-11-22 22:02:26 +00001822static void be_msix_disable(struct be_adapter *adapter)
1823{
1824 if (adapter->msix_enabled) {
1825 pci_disable_msix(adapter->pdev);
1826 adapter->msix_enabled = false;
1827 }
1828}
1829
Sathya Perla3abcded2010-10-03 22:12:27 -07001830static int be_num_rxqs_get(struct be_adapter *adapter)
1831{
1832 if (multi_rxq && (adapter->function_caps & BE_FUNCTION_CAPS_RSS) &&
1833 !adapter->sriov_enabled && !(adapter->function_mode & 0x400)) {
1834 return 1 + MAX_RSS_QS; /* one default non-RSS queue */
1835 } else {
1836 dev_warn(&adapter->pdev->dev,
1837 "No support for multiple RX queues\n");
1838 return 1;
1839 }
1840}
1841
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001842static void be_msix_enable(struct be_adapter *adapter)
1843{
Sathya Perla3abcded2010-10-03 22:12:27 -07001844#define BE_MIN_MSIX_VECTORS (1 + 1) /* Rx + Tx */
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001845 int i, status;
1846
Sathya Perla3abcded2010-10-03 22:12:27 -07001847 adapter->num_rx_qs = be_num_rxqs_get(adapter);
1848
1849 for (i = 0; i < (adapter->num_rx_qs + 1); i++)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001850 adapter->msix_entries[i].entry = i;
1851
1852 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
Sathya Perla3abcded2010-10-03 22:12:27 -07001853 adapter->num_rx_qs + 1);
1854 if (status == 0) {
1855 goto done;
1856 } else if (status >= BE_MIN_MSIX_VECTORS) {
1857 if (pci_enable_msix(adapter->pdev, adapter->msix_entries,
1858 status) == 0) {
1859 adapter->num_rx_qs = status - 1;
1860 dev_warn(&adapter->pdev->dev,
1861 "Could alloc only %d MSIx vectors. "
1862 "Using %d RX Qs\n", status, adapter->num_rx_qs);
1863 goto done;
1864 }
1865 }
1866 return;
1867done:
1868 adapter->msix_enabled = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001869}
1870
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001871static void be_sriov_enable(struct be_adapter *adapter)
1872{
Sarveshwar Bandi344dbf12010-07-09 01:43:55 +00001873 be_check_sriov_fn_type(adapter);
Ajit Khaparde6dedec82010-07-29 06:15:32 +00001874#ifdef CONFIG_PCI_IOV
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001875 if (be_physfn(adapter) && num_vfs) {
Ajit Khaparde6dedec82010-07-29 06:15:32 +00001876 int status;
1877
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001878 status = pci_enable_sriov(adapter->pdev, num_vfs);
1879 adapter->sriov_enabled = status ? false : true;
1880 }
1881#endif
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001882}
1883
1884static void be_sriov_disable(struct be_adapter *adapter)
1885{
1886#ifdef CONFIG_PCI_IOV
1887 if (adapter->sriov_enabled) {
1888 pci_disable_sriov(adapter->pdev);
1889 adapter->sriov_enabled = false;
1890 }
1891#endif
1892}
1893
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001894static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1895{
Sathya Perlab628bde2009-08-17 00:58:26 +00001896 return adapter->msix_entries[
1897 be_evt_bit_get(adapter, eq_id)].vector;
1898}
1899
1900static int be_request_irq(struct be_adapter *adapter,
1901 struct be_eq_obj *eq_obj,
Sathya Perla3abcded2010-10-03 22:12:27 -07001902 void *handler, char *desc, void *context)
Sathya Perlab628bde2009-08-17 00:58:26 +00001903{
1904 struct net_device *netdev = adapter->netdev;
1905 int vec;
1906
1907 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
1908 vec = be_msix_vec_get(adapter, eq_obj->q.id);
Sathya Perla3abcded2010-10-03 22:12:27 -07001909 return request_irq(vec, handler, 0, eq_obj->desc, context);
Sathya Perlab628bde2009-08-17 00:58:26 +00001910}
1911
Sathya Perla3abcded2010-10-03 22:12:27 -07001912static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj,
1913 void *context)
Sathya Perlab628bde2009-08-17 00:58:26 +00001914{
1915 int vec = be_msix_vec_get(adapter, eq_obj->q.id);
Sathya Perla3abcded2010-10-03 22:12:27 -07001916 free_irq(vec, context);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001917}
1918
1919static int be_msix_register(struct be_adapter *adapter)
1920{
Sathya Perla3abcded2010-10-03 22:12:27 -07001921 struct be_rx_obj *rxo;
1922 int status, i;
1923 char qname[10];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001924
Sathya Perla3abcded2010-10-03 22:12:27 -07001925 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx",
1926 adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001927 if (status)
1928 goto err;
1929
Sathya Perla3abcded2010-10-03 22:12:27 -07001930 for_all_rx_queues(adapter, rxo, i) {
1931 sprintf(qname, "rxq%d", i);
1932 status = be_request_irq(adapter, &rxo->rx_eq, be_msix_rx,
1933 qname, rxo);
1934 if (status)
1935 goto err_msix;
1936 }
Sathya Perlab628bde2009-08-17 00:58:26 +00001937
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001938 return 0;
Sathya Perlab628bde2009-08-17 00:58:26 +00001939
Sathya Perla3abcded2010-10-03 22:12:27 -07001940err_msix:
1941 be_free_irq(adapter, &adapter->tx_eq, adapter);
1942
1943 for (i--, rxo = &adapter->rx_obj[i]; i >= 0; i--, rxo--)
1944 be_free_irq(adapter, &rxo->rx_eq, rxo);
1945
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001946err:
1947 dev_warn(&adapter->pdev->dev,
1948 "MSIX Request IRQ failed - err %d\n", status);
1949 pci_disable_msix(adapter->pdev);
1950 adapter->msix_enabled = false;
1951 return status;
1952}
1953
1954static int be_irq_register(struct be_adapter *adapter)
1955{
1956 struct net_device *netdev = adapter->netdev;
1957 int status;
1958
1959 if (adapter->msix_enabled) {
1960 status = be_msix_register(adapter);
1961 if (status == 0)
1962 goto done;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001963 /* INTx is not supported for VF */
1964 if (!be_physfn(adapter))
1965 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001966 }
1967
1968 /* INTx */
1969 netdev->irq = adapter->pdev->irq;
1970 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1971 adapter);
1972 if (status) {
1973 dev_err(&adapter->pdev->dev,
1974 "INTx request IRQ failed - err %d\n", status);
1975 return status;
1976 }
1977done:
1978 adapter->isr_registered = true;
1979 return 0;
1980}
1981
1982static void be_irq_unregister(struct be_adapter *adapter)
1983{
1984 struct net_device *netdev = adapter->netdev;
Sathya Perla3abcded2010-10-03 22:12:27 -07001985 struct be_rx_obj *rxo;
1986 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001987
1988 if (!adapter->isr_registered)
1989 return;
1990
1991 /* INTx */
1992 if (!adapter->msix_enabled) {
1993 free_irq(netdev->irq, adapter);
1994 goto done;
1995 }
1996
1997 /* MSIx */
Sathya Perla3abcded2010-10-03 22:12:27 -07001998 be_free_irq(adapter, &adapter->tx_eq, adapter);
1999
2000 for_all_rx_queues(adapter, rxo, i)
2001 be_free_irq(adapter, &rxo->rx_eq, rxo);
2002
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002003done:
2004 adapter->isr_registered = false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002005}
2006
Sathya Perla889cd4b2010-05-30 23:33:45 +00002007static int be_close(struct net_device *netdev)
2008{
2009 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3abcded2010-10-03 22:12:27 -07002010 struct be_rx_obj *rxo;
Sathya Perla889cd4b2010-05-30 23:33:45 +00002011 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla3abcded2010-10-03 22:12:27 -07002012 int vec, i;
Sathya Perla889cd4b2010-05-30 23:33:45 +00002013
2014 cancel_delayed_work_sync(&adapter->work);
2015
2016 be_async_mcc_disable(adapter);
2017
2018 netif_stop_queue(netdev);
2019 netif_carrier_off(netdev);
2020 adapter->link_up = false;
2021
2022 be_intr_set(adapter, false);
2023
2024 if (adapter->msix_enabled) {
2025 vec = be_msix_vec_get(adapter, tx_eq->q.id);
2026 synchronize_irq(vec);
Sathya Perla3abcded2010-10-03 22:12:27 -07002027
2028 for_all_rx_queues(adapter, rxo, i) {
2029 vec = be_msix_vec_get(adapter, rxo->rx_eq.q.id);
2030 synchronize_irq(vec);
2031 }
Sathya Perla889cd4b2010-05-30 23:33:45 +00002032 } else {
2033 synchronize_irq(netdev->irq);
2034 }
2035 be_irq_unregister(adapter);
2036
Sathya Perla3abcded2010-10-03 22:12:27 -07002037 for_all_rx_queues(adapter, rxo, i)
2038 napi_disable(&rxo->rx_eq.napi);
2039
Sathya Perla889cd4b2010-05-30 23:33:45 +00002040 napi_disable(&tx_eq->napi);
2041
2042 /* Wait for all pending tx completions to arrive so that
2043 * all tx skbs are freed.
2044 */
2045 be_tx_compl_clean(adapter);
2046
2047 return 0;
2048}
2049
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002050static int be_open(struct net_device *netdev)
2051{
2052 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002053 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla3abcded2010-10-03 22:12:27 -07002054 struct be_rx_obj *rxo;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002055 bool link_up;
Sathya Perla3abcded2010-10-03 22:12:27 -07002056 int status, i;
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07002057 u8 mac_speed;
2058 u16 link_speed;
Sathya Perla5fb379e2009-06-18 00:02:59 +00002059
Sathya Perla3abcded2010-10-03 22:12:27 -07002060 for_all_rx_queues(adapter, rxo, i) {
2061 be_post_rx_frags(rxo);
2062 napi_enable(&rxo->rx_eq.napi);
2063 }
Sathya Perla5fb379e2009-06-18 00:02:59 +00002064 napi_enable(&tx_eq->napi);
2065
2066 be_irq_register(adapter);
2067
Sathya Perla8788fdc2009-07-27 22:52:03 +00002068 be_intr_set(adapter, true);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002069
2070 /* The evt queues are created in unarmed state; arm them */
Sathya Perla3abcded2010-10-03 22:12:27 -07002071 for_all_rx_queues(adapter, rxo, i) {
2072 be_eq_notify(adapter, rxo->rx_eq.q.id, true, false, 0);
2073 be_cq_notify(adapter, rxo->cq.id, true, 0);
2074 }
Sathya Perla8788fdc2009-07-27 22:52:03 +00002075 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002076
Sathya Perla7a1e9b22010-02-17 01:35:11 +00002077 /* Now that interrupts are on we can process async mcc */
2078 be_async_mcc_enable(adapter);
2079
Sathya Perla889cd4b2010-05-30 23:33:45 +00002080 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
2081
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07002082 status = be_cmd_link_status_query(adapter, &link_up, &mac_speed,
2083 &link_speed);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002084 if (status)
Sathya Perla889cd4b2010-05-30 23:33:45 +00002085 goto err;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002086 be_link_status_update(adapter, link_up);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002087
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002088 if (be_physfn(adapter)) {
Ajit Khaparde1da87b72010-07-23 01:51:22 +00002089 status = be_vid_config(adapter, false, 0);
Sathya Perla889cd4b2010-05-30 23:33:45 +00002090 if (status)
2091 goto err;
2092
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002093 status = be_cmd_set_flow_control(adapter,
2094 adapter->tx_fc, adapter->rx_fc);
2095 if (status)
Sathya Perla889cd4b2010-05-30 23:33:45 +00002096 goto err;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002097 }
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00002098
Sathya Perla889cd4b2010-05-30 23:33:45 +00002099 return 0;
2100err:
2101 be_close(adapter->netdev);
2102 return -EIO;
Sathya Perla5fb379e2009-06-18 00:02:59 +00002103}
2104
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002105static int be_setup_wol(struct be_adapter *adapter, bool enable)
2106{
2107 struct be_dma_mem cmd;
2108 int status = 0;
2109 u8 mac[ETH_ALEN];
2110
2111 memset(mac, 0, ETH_ALEN);
2112
2113 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
2114 cmd.va = pci_alloc_consistent(adapter->pdev, cmd.size, &cmd.dma);
2115 if (cmd.va == NULL)
2116 return -1;
2117 memset(cmd.va, 0, cmd.size);
2118
2119 if (enable) {
2120 status = pci_write_config_dword(adapter->pdev,
2121 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
2122 if (status) {
2123 dev_err(&adapter->pdev->dev,
Frans Pop2381a552010-03-24 07:57:36 +00002124 "Could not enable Wake-on-lan\n");
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002125 pci_free_consistent(adapter->pdev, cmd.size, cmd.va,
2126 cmd.dma);
2127 return status;
2128 }
2129 status = be_cmd_enable_magic_wol(adapter,
2130 adapter->netdev->dev_addr, &cmd);
2131 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
2132 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
2133 } else {
2134 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
2135 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
2136 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
2137 }
2138
2139 pci_free_consistent(adapter->pdev, cmd.size, cmd.va, cmd.dma);
2140 return status;
2141}
2142
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002143/*
2144 * Generate a seed MAC address from the PF MAC Address using jhash.
2145 * MAC Address for VFs are assigned incrementally starting from the seed.
2146 * These addresses are programmed in the ASIC by the PF and the VF driver
2147 * queries for the MAC address during its probe.
2148 */
2149static inline int be_vf_eth_addr_config(struct be_adapter *adapter)
2150{
2151 u32 vf = 0;
Sathya Perla3abcded2010-10-03 22:12:27 -07002152 int status = 0;
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002153 u8 mac[ETH_ALEN];
2154
2155 be_vf_eth_addr_generate(adapter, mac);
2156
2157 for (vf = 0; vf < num_vfs; vf++) {
2158 status = be_cmd_pmac_add(adapter, mac,
2159 adapter->vf_cfg[vf].vf_if_handle,
2160 &adapter->vf_cfg[vf].vf_pmac_id);
2161 if (status)
2162 dev_err(&adapter->pdev->dev,
2163 "Mac address add failed for VF %d\n", vf);
2164 else
2165 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
2166
2167 mac[5] += 1;
2168 }
2169 return status;
2170}
2171
2172static inline void be_vf_eth_addr_rem(struct be_adapter *adapter)
2173{
2174 u32 vf;
2175
2176 for (vf = 0; vf < num_vfs; vf++) {
2177 if (adapter->vf_cfg[vf].vf_pmac_id != BE_INVALID_PMAC_ID)
2178 be_cmd_pmac_del(adapter,
2179 adapter->vf_cfg[vf].vf_if_handle,
2180 adapter->vf_cfg[vf].vf_pmac_id);
2181 }
2182}
2183
Sathya Perla5fb379e2009-06-18 00:02:59 +00002184static int be_setup(struct be_adapter *adapter)
2185{
Sathya Perla5fb379e2009-06-18 00:02:59 +00002186 struct net_device *netdev = adapter->netdev;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002187 u32 cap_flags, en_flags, vf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002188 int status;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002189 u8 mac[ETH_ALEN];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002190
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002191 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST;
2192
2193 if (be_physfn(adapter)) {
2194 cap_flags |= BE_IF_FLAGS_MCAST_PROMISCUOUS |
2195 BE_IF_FLAGS_PROMISCUOUS |
2196 BE_IF_FLAGS_PASS_L3L4_ERRORS;
2197 en_flags |= BE_IF_FLAGS_PASS_L3L4_ERRORS;
Sathya Perla3abcded2010-10-03 22:12:27 -07002198
2199 if (be_multi_rxq(adapter)) {
2200 cap_flags |= BE_IF_FLAGS_RSS;
2201 en_flags |= BE_IF_FLAGS_RSS;
2202 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002203 }
Sathya Perla73d540f2009-10-14 20:20:42 +00002204
2205 status = be_cmd_if_create(adapter, cap_flags, en_flags,
2206 netdev->dev_addr, false/* pmac_invalid */,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002207 &adapter->if_handle, &adapter->pmac_id, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002208 if (status != 0)
2209 goto do_none;
2210
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002211 if (be_physfn(adapter)) {
2212 while (vf < num_vfs) {
2213 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED
2214 | BE_IF_FLAGS_BROADCAST;
2215 status = be_cmd_if_create(adapter, cap_flags, en_flags,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002216 mac, true,
2217 &adapter->vf_cfg[vf].vf_if_handle,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002218 NULL, vf+1);
2219 if (status) {
2220 dev_err(&adapter->pdev->dev,
2221 "Interface Create failed for VF %d\n", vf);
2222 goto if_destroy;
2223 }
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002224 adapter->vf_cfg[vf].vf_pmac_id = BE_INVALID_PMAC_ID;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002225 vf++;
Sarveshwar Bandi84e5b9f2010-05-27 16:28:15 -07002226 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002227 } else if (!be_physfn(adapter)) {
2228 status = be_cmd_mac_addr_query(adapter, mac,
2229 MAC_ADDRESS_TYPE_NETWORK, false, adapter->if_handle);
2230 if (!status) {
2231 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2232 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2233 }
2234 }
2235
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002236 status = be_tx_queues_create(adapter);
2237 if (status != 0)
2238 goto if_destroy;
2239
2240 status = be_rx_queues_create(adapter);
2241 if (status != 0)
2242 goto tx_qs_destroy;
2243
Sathya Perla5fb379e2009-06-18 00:02:59 +00002244 status = be_mcc_queues_create(adapter);
2245 if (status != 0)
2246 goto rx_qs_destroy;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002247
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002248 if (be_physfn(adapter)) {
2249 status = be_vf_eth_addr_config(adapter);
2250 if (status)
2251 goto mcc_q_destroy;
2252 }
2253
Ajit Khaparde0dffc832009-11-29 17:57:46 +00002254 adapter->link_speed = -1;
2255
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002256 return 0;
2257
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002258mcc_q_destroy:
2259 if (be_physfn(adapter))
2260 be_vf_eth_addr_rem(adapter);
2261 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002262rx_qs_destroy:
2263 be_rx_queues_destroy(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002264tx_qs_destroy:
2265 be_tx_queues_destroy(adapter);
2266if_destroy:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002267 for (vf = 0; vf < num_vfs; vf++)
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002268 if (adapter->vf_cfg[vf].vf_if_handle)
2269 be_cmd_if_destroy(adapter,
2270 adapter->vf_cfg[vf].vf_if_handle);
Sathya Perla8788fdc2009-07-27 22:52:03 +00002271 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002272do_none:
2273 return status;
2274}
2275
Sathya Perla5fb379e2009-06-18 00:02:59 +00002276static int be_clear(struct be_adapter *adapter)
2277{
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002278 if (be_physfn(adapter))
2279 be_vf_eth_addr_rem(adapter);
2280
Sathya Perla1a8887d2009-08-17 00:58:41 +00002281 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002282 be_rx_queues_destroy(adapter);
2283 be_tx_queues_destroy(adapter);
2284
Sathya Perla8788fdc2009-07-27 22:52:03 +00002285 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002286
Sathya Perla2243e2e2009-11-22 22:02:03 +00002287 /* tell fw we're done with firing cmds */
2288 be_cmd_fw_clean(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002289 return 0;
2290}
2291
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002292
Ajit Khaparde84517482009-09-04 03:12:16 +00002293#define FW_FILE_HDR_SIGN "ServerEngines Corp. "
2294char flash_cookie[2][16] = {"*** SE FLAS",
2295 "H DIRECTORY *** "};
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002296
2297static bool be_flash_redboot(struct be_adapter *adapter,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002298 const u8 *p, u32 img_start, int image_size,
2299 int hdr_size)
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002300{
2301 u32 crc_offset;
2302 u8 flashed_crc[4];
2303 int status;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002304
2305 crc_offset = hdr_size + img_start + image_size - 4;
2306
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002307 p += crc_offset;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002308
2309 status = be_cmd_get_flash_crc(adapter, flashed_crc,
Ajit Khapardef510fc62010-03-31 01:47:45 +00002310 (image_size - 4));
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002311 if (status) {
2312 dev_err(&adapter->pdev->dev,
2313 "could not get crc from flash, not flashing redboot\n");
2314 return false;
2315 }
2316
2317 /*update redboot only if crc does not match*/
2318 if (!memcmp(flashed_crc, p, 4))
2319 return false;
2320 else
2321 return true;
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002322}
2323
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002324static int be_flash_data(struct be_adapter *adapter,
Ajit Khaparde84517482009-09-04 03:12:16 +00002325 const struct firmware *fw,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002326 struct be_dma_mem *flash_cmd, int num_of_images)
2327
Ajit Khaparde84517482009-09-04 03:12:16 +00002328{
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002329 int status = 0, i, filehdr_size = 0;
2330 u32 total_bytes = 0, flash_op;
Ajit Khaparde84517482009-09-04 03:12:16 +00002331 int num_bytes;
2332 const u8 *p = fw->data;
2333 struct be_cmd_write_flashrom *req = flash_cmd->va;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002334 struct flash_comp *pflashcomp;
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002335 int num_comp;
Ajit Khaparde84517482009-09-04 03:12:16 +00002336
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002337 struct flash_comp gen3_flash_types[9] = {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002338 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, IMG_TYPE_ISCSI_ACTIVE,
2339 FLASH_IMAGE_MAX_SIZE_g3},
2340 { FLASH_REDBOOT_START_g3, IMG_TYPE_REDBOOT,
2341 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3},
2342 { FLASH_iSCSI_BIOS_START_g3, IMG_TYPE_BIOS,
2343 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2344 { FLASH_PXE_BIOS_START_g3, IMG_TYPE_PXE_BIOS,
2345 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2346 { FLASH_FCoE_BIOS_START_g3, IMG_TYPE_FCOE_BIOS,
2347 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2348 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, IMG_TYPE_ISCSI_BACKUP,
2349 FLASH_IMAGE_MAX_SIZE_g3},
2350 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, IMG_TYPE_FCOE_FW_ACTIVE,
2351 FLASH_IMAGE_MAX_SIZE_g3},
2352 { FLASH_FCoE_BACKUP_IMAGE_START_g3, IMG_TYPE_FCOE_FW_BACKUP,
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002353 FLASH_IMAGE_MAX_SIZE_g3},
2354 { FLASH_NCSI_START_g3, IMG_TYPE_NCSI_FW,
2355 FLASH_NCSI_IMAGE_MAX_SIZE_g3}
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002356 };
2357 struct flash_comp gen2_flash_types[8] = {
2358 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, IMG_TYPE_ISCSI_ACTIVE,
2359 FLASH_IMAGE_MAX_SIZE_g2},
2360 { FLASH_REDBOOT_START_g2, IMG_TYPE_REDBOOT,
2361 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2},
2362 { FLASH_iSCSI_BIOS_START_g2, IMG_TYPE_BIOS,
2363 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2364 { FLASH_PXE_BIOS_START_g2, IMG_TYPE_PXE_BIOS,
2365 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2366 { FLASH_FCoE_BIOS_START_g2, IMG_TYPE_FCOE_BIOS,
2367 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2368 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, IMG_TYPE_ISCSI_BACKUP,
2369 FLASH_IMAGE_MAX_SIZE_g2},
2370 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, IMG_TYPE_FCOE_FW_ACTIVE,
2371 FLASH_IMAGE_MAX_SIZE_g2},
2372 { FLASH_FCoE_BACKUP_IMAGE_START_g2, IMG_TYPE_FCOE_FW_BACKUP,
2373 FLASH_IMAGE_MAX_SIZE_g2}
2374 };
2375
2376 if (adapter->generation == BE_GEN3) {
2377 pflashcomp = gen3_flash_types;
2378 filehdr_size = sizeof(struct flash_file_hdr_g3);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002379 num_comp = 9;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002380 } else {
2381 pflashcomp = gen2_flash_types;
2382 filehdr_size = sizeof(struct flash_file_hdr_g2);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002383 num_comp = 8;
Ajit Khaparde84517482009-09-04 03:12:16 +00002384 }
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002385 for (i = 0; i < num_comp; i++) {
2386 if ((pflashcomp[i].optype == IMG_TYPE_NCSI_FW) &&
2387 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
2388 continue;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002389 if ((pflashcomp[i].optype == IMG_TYPE_REDBOOT) &&
2390 (!be_flash_redboot(adapter, fw->data,
2391 pflashcomp[i].offset, pflashcomp[i].size,
2392 filehdr_size)))
2393 continue;
2394 p = fw->data;
2395 p += filehdr_size + pflashcomp[i].offset
2396 + (num_of_images * sizeof(struct image_hdr));
2397 if (p + pflashcomp[i].size > fw->data + fw->size)
Ajit Khaparde84517482009-09-04 03:12:16 +00002398 return -1;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002399 total_bytes = pflashcomp[i].size;
2400 while (total_bytes) {
2401 if (total_bytes > 32*1024)
2402 num_bytes = 32*1024;
2403 else
2404 num_bytes = total_bytes;
2405 total_bytes -= num_bytes;
Ajit Khaparde84517482009-09-04 03:12:16 +00002406
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002407 if (!total_bytes)
2408 flash_op = FLASHROM_OPER_FLASH;
2409 else
2410 flash_op = FLASHROM_OPER_SAVE;
2411 memcpy(req->params.data_buf, p, num_bytes);
2412 p += num_bytes;
2413 status = be_cmd_write_flashrom(adapter, flash_cmd,
2414 pflashcomp[i].optype, flash_op, num_bytes);
2415 if (status) {
2416 dev_err(&adapter->pdev->dev,
2417 "cmd to write to flash rom failed.\n");
2418 return -1;
2419 }
2420 yield();
Ajit Khaparde84517482009-09-04 03:12:16 +00002421 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002422 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002423 return 0;
2424}
2425
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002426static int get_ufigen_type(struct flash_file_hdr_g2 *fhdr)
2427{
2428 if (fhdr == NULL)
2429 return 0;
2430 if (fhdr->build[0] == '3')
2431 return BE_GEN3;
2432 else if (fhdr->build[0] == '2')
2433 return BE_GEN2;
2434 else
2435 return 0;
2436}
2437
Ajit Khaparde84517482009-09-04 03:12:16 +00002438int be_load_fw(struct be_adapter *adapter, u8 *func)
2439{
2440 char fw_file[ETHTOOL_FLASH_MAX_FILENAME];
2441 const struct firmware *fw;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002442 struct flash_file_hdr_g2 *fhdr;
2443 struct flash_file_hdr_g3 *fhdr3;
2444 struct image_hdr *img_hdr_ptr = NULL;
Ajit Khaparde84517482009-09-04 03:12:16 +00002445 struct be_dma_mem flash_cmd;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002446 int status, i = 0, num_imgs = 0;
Ajit Khaparde84517482009-09-04 03:12:16 +00002447 const u8 *p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002448
Ajit Khaparde84517482009-09-04 03:12:16 +00002449 strcpy(fw_file, func);
2450
2451 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
2452 if (status)
2453 goto fw_exit;
2454
2455 p = fw->data;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002456 fhdr = (struct flash_file_hdr_g2 *) p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002457 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
2458
Ajit Khaparde84517482009-09-04 03:12:16 +00002459 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
2460 flash_cmd.va = pci_alloc_consistent(adapter->pdev, flash_cmd.size,
2461 &flash_cmd.dma);
2462 if (!flash_cmd.va) {
2463 status = -ENOMEM;
2464 dev_err(&adapter->pdev->dev,
2465 "Memory allocation failure while flashing\n");
2466 goto fw_exit;
2467 }
2468
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002469 if ((adapter->generation == BE_GEN3) &&
2470 (get_ufigen_type(fhdr) == BE_GEN3)) {
2471 fhdr3 = (struct flash_file_hdr_g3 *) fw->data;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002472 num_imgs = le32_to_cpu(fhdr3->num_imgs);
2473 for (i = 0; i < num_imgs; i++) {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002474 img_hdr_ptr = (struct image_hdr *) (fw->data +
2475 (sizeof(struct flash_file_hdr_g3) +
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002476 i * sizeof(struct image_hdr)));
2477 if (le32_to_cpu(img_hdr_ptr->imageid) == 1)
2478 status = be_flash_data(adapter, fw, &flash_cmd,
2479 num_imgs);
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002480 }
2481 } else if ((adapter->generation == BE_GEN2) &&
2482 (get_ufigen_type(fhdr) == BE_GEN2)) {
2483 status = be_flash_data(adapter, fw, &flash_cmd, 0);
2484 } else {
2485 dev_err(&adapter->pdev->dev,
2486 "UFI and Interface are not compatible for flashing\n");
2487 status = -1;
Ajit Khaparde84517482009-09-04 03:12:16 +00002488 }
2489
2490 pci_free_consistent(adapter->pdev, flash_cmd.size, flash_cmd.va,
2491 flash_cmd.dma);
2492 if (status) {
2493 dev_err(&adapter->pdev->dev, "Firmware load error\n");
2494 goto fw_exit;
2495 }
2496
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002497 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
Ajit Khaparde84517482009-09-04 03:12:16 +00002498
2499fw_exit:
2500 release_firmware(fw);
2501 return status;
2502}
2503
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002504static struct net_device_ops be_netdev_ops = {
2505 .ndo_open = be_open,
2506 .ndo_stop = be_close,
2507 .ndo_start_xmit = be_xmit,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002508 .ndo_set_rx_mode = be_set_multicast_list,
2509 .ndo_set_mac_address = be_mac_addr_set,
2510 .ndo_change_mtu = be_change_mtu,
2511 .ndo_validate_addr = eth_validate_addr,
2512 .ndo_vlan_rx_register = be_vlan_register,
2513 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
2514 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002515 .ndo_set_vf_mac = be_set_vf_mac,
Ajit Khaparde1da87b72010-07-23 01:51:22 +00002516 .ndo_set_vf_vlan = be_set_vf_vlan,
Ajit Khapardee1d18732010-07-23 01:52:13 +00002517 .ndo_set_vf_tx_rate = be_set_vf_tx_rate,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002518 .ndo_get_vf_config = be_get_vf_config
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002519};
2520
2521static void be_netdev_init(struct net_device *netdev)
2522{
2523 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3abcded2010-10-03 22:12:27 -07002524 struct be_rx_obj *rxo;
2525 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002526
2527 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
Ajit Khaparde583e3f32009-10-05 02:22:19 +00002528 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_HW_CSUM |
Ajit Khaparde49e4b8472010-06-14 04:56:07 +00002529 NETIF_F_GRO | NETIF_F_TSO6;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002530
Ajit Khaparde51c59872009-11-29 17:54:54 +00002531 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2532
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002533 netdev->flags |= IFF_MULTICAST;
2534
Ajit Khaparde728a9972009-04-13 15:41:22 -07002535 adapter->rx_csum = true;
2536
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002537 /* Default settings for Rx and Tx flow control */
2538 adapter->rx_fc = true;
2539 adapter->tx_fc = true;
2540
Ajit Khapardec190e3c2009-09-04 03:12:29 +00002541 netif_set_gso_max_size(netdev, 65535);
2542
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002543 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
2544
2545 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
2546
Sathya Perla3abcded2010-10-03 22:12:27 -07002547 for_all_rx_queues(adapter, rxo, i)
2548 netif_napi_add(netdev, &rxo->rx_eq.napi, be_poll_rx,
2549 BE_NAPI_WEIGHT);
2550
Sathya Perla5fb379e2009-06-18 00:02:59 +00002551 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002552 BE_NAPI_WEIGHT);
2553
2554 netif_carrier_off(netdev);
2555 netif_stop_queue(netdev);
2556}
2557
2558static void be_unmap_pci_bars(struct be_adapter *adapter)
2559{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002560 if (adapter->csr)
2561 iounmap(adapter->csr);
2562 if (adapter->db)
2563 iounmap(adapter->db);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002564 if (adapter->pcicfg && be_physfn(adapter))
Sathya Perla8788fdc2009-07-27 22:52:03 +00002565 iounmap(adapter->pcicfg);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002566}
2567
2568static int be_map_pci_bars(struct be_adapter *adapter)
2569{
2570 u8 __iomem *addr;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002571 int pcicfg_reg, db_reg;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002572
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002573 if (be_physfn(adapter)) {
2574 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
2575 pci_resource_len(adapter->pdev, 2));
2576 if (addr == NULL)
2577 return -ENOMEM;
2578 adapter->csr = addr;
2579 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002580
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002581 if (adapter->generation == BE_GEN2) {
2582 pcicfg_reg = 1;
2583 db_reg = 4;
2584 } else {
2585 pcicfg_reg = 0;
2586 if (be_physfn(adapter))
2587 db_reg = 4;
2588 else
2589 db_reg = 0;
2590 }
2591 addr = ioremap_nocache(pci_resource_start(adapter->pdev, db_reg),
2592 pci_resource_len(adapter->pdev, db_reg));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002593 if (addr == NULL)
2594 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00002595 adapter->db = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002596
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002597 if (be_physfn(adapter)) {
2598 addr = ioremap_nocache(
2599 pci_resource_start(adapter->pdev, pcicfg_reg),
2600 pci_resource_len(adapter->pdev, pcicfg_reg));
2601 if (addr == NULL)
2602 goto pci_map_err;
2603 adapter->pcicfg = addr;
2604 } else
2605 adapter->pcicfg = adapter->db + SRIOV_VF_PCICFG_OFFSET;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002606
2607 return 0;
2608pci_map_err:
2609 be_unmap_pci_bars(adapter);
2610 return -ENOMEM;
2611}
2612
2613
2614static void be_ctrl_cleanup(struct be_adapter *adapter)
2615{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002616 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002617
2618 be_unmap_pci_bars(adapter);
2619
2620 if (mem->va)
2621 pci_free_consistent(adapter->pdev, mem->size,
2622 mem->va, mem->dma);
Sathya Perlae7b909a2009-11-22 22:01:10 +00002623
2624 mem = &adapter->mc_cmd_mem;
2625 if (mem->va)
2626 pci_free_consistent(adapter->pdev, mem->size,
2627 mem->va, mem->dma);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002628}
2629
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002630static int be_ctrl_init(struct be_adapter *adapter)
2631{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002632 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
2633 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002634 struct be_dma_mem *mc_cmd_mem = &adapter->mc_cmd_mem;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002635 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002636
2637 status = be_map_pci_bars(adapter);
2638 if (status)
Sathya Perlae7b909a2009-11-22 22:01:10 +00002639 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002640
2641 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
2642 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
2643 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
2644 if (!mbox_mem_alloc->va) {
Sathya Perlae7b909a2009-11-22 22:01:10 +00002645 status = -ENOMEM;
2646 goto unmap_pci_bars;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002647 }
Sathya Perlae7b909a2009-11-22 22:01:10 +00002648
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002649 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
2650 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
2651 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
2652 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Sathya Perlae7b909a2009-11-22 22:01:10 +00002653
2654 mc_cmd_mem->size = sizeof(struct be_cmd_req_mcast_mac_config);
2655 mc_cmd_mem->va = pci_alloc_consistent(adapter->pdev, mc_cmd_mem->size,
2656 &mc_cmd_mem->dma);
2657 if (mc_cmd_mem->va == NULL) {
2658 status = -ENOMEM;
2659 goto free_mbox;
2660 }
2661 memset(mc_cmd_mem->va, 0, mc_cmd_mem->size);
2662
Sathya Perla8788fdc2009-07-27 22:52:03 +00002663 spin_lock_init(&adapter->mbox_lock);
2664 spin_lock_init(&adapter->mcc_lock);
2665 spin_lock_init(&adapter->mcc_cq_lock);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002666
Sarveshwar Bandidd131e72010-05-25 16:16:32 -07002667 init_completion(&adapter->flash_compl);
Sathya Perlacf588472010-02-14 21:22:01 +00002668 pci_save_state(adapter->pdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002669 return 0;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002670
2671free_mbox:
2672 pci_free_consistent(adapter->pdev, mbox_mem_alloc->size,
2673 mbox_mem_alloc->va, mbox_mem_alloc->dma);
2674
2675unmap_pci_bars:
2676 be_unmap_pci_bars(adapter);
2677
2678done:
2679 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002680}
2681
2682static void be_stats_cleanup(struct be_adapter *adapter)
2683{
Sathya Perla3abcded2010-10-03 22:12:27 -07002684 struct be_dma_mem *cmd = &adapter->stats_cmd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002685
2686 if (cmd->va)
2687 pci_free_consistent(adapter->pdev, cmd->size,
2688 cmd->va, cmd->dma);
2689}
2690
2691static int be_stats_init(struct be_adapter *adapter)
2692{
Sathya Perla3abcded2010-10-03 22:12:27 -07002693 struct be_dma_mem *cmd = &adapter->stats_cmd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002694
2695 cmd->size = sizeof(struct be_cmd_req_get_stats);
2696 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
2697 if (cmd->va == NULL)
2698 return -1;
David S. Millerd291b9a2010-01-28 21:36:21 -08002699 memset(cmd->va, 0, cmd->size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002700 return 0;
2701}
2702
2703static void __devexit be_remove(struct pci_dev *pdev)
2704{
2705 struct be_adapter *adapter = pci_get_drvdata(pdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002706
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002707 if (!adapter)
2708 return;
2709
2710 unregister_netdev(adapter->netdev);
2711
Sathya Perla5fb379e2009-06-18 00:02:59 +00002712 be_clear(adapter);
2713
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002714 be_stats_cleanup(adapter);
2715
2716 be_ctrl_cleanup(adapter);
2717
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002718 be_sriov_disable(adapter);
2719
Sathya Perla8d56ff12009-11-22 22:02:26 +00002720 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002721
2722 pci_set_drvdata(pdev, NULL);
2723 pci_release_regions(pdev);
2724 pci_disable_device(pdev);
2725
2726 free_netdev(adapter->netdev);
2727}
2728
Sathya Perla2243e2e2009-11-22 22:02:03 +00002729static int be_get_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002730{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002731 int status;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002732 u8 mac[ETH_ALEN];
Sathya Perla43a04fdc2009-10-14 20:21:17 +00002733
Sathya Perla8788fdc2009-07-27 22:52:03 +00002734 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002735 if (status)
2736 return status;
2737
Sathya Perla3abcded2010-10-03 22:12:27 -07002738 status = be_cmd_query_fw_cfg(adapter, &adapter->port_num,
2739 &adapter->function_mode, &adapter->function_caps);
Sathya Perla2243e2e2009-11-22 22:02:03 +00002740 if (status)
2741 return status;
2742
2743 memset(mac, 0, ETH_ALEN);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002744
2745 if (be_physfn(adapter)) {
2746 status = be_cmd_mac_addr_query(adapter, mac,
Sathya Perla2243e2e2009-11-22 22:02:03 +00002747 MAC_ADDRESS_TYPE_NETWORK, true /*permanent */, 0);
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002748
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002749 if (status)
2750 return status;
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002751
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002752 if (!is_valid_ether_addr(mac))
2753 return -EADDRNOTAVAIL;
2754
2755 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2756 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2757 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002758
Ajit Khaparde3486be22010-07-23 02:04:54 +00002759 if (adapter->function_mode & 0x400)
Ajit Khaparde82903e42010-02-09 01:34:57 +00002760 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED/4;
2761 else
2762 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED;
2763
Sathya Perla2243e2e2009-11-22 22:02:03 +00002764 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002765}
2766
2767static int __devinit be_probe(struct pci_dev *pdev,
2768 const struct pci_device_id *pdev_id)
2769{
2770 int status = 0;
2771 struct be_adapter *adapter;
2772 struct net_device *netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002773
2774 status = pci_enable_device(pdev);
2775 if (status)
2776 goto do_none;
2777
2778 status = pci_request_regions(pdev, DRV_NAME);
2779 if (status)
2780 goto disable_dev;
2781 pci_set_master(pdev);
2782
2783 netdev = alloc_etherdev(sizeof(struct be_adapter));
2784 if (netdev == NULL) {
2785 status = -ENOMEM;
2786 goto rel_reg;
2787 }
2788 adapter = netdev_priv(netdev);
Ajit Khaparde7b139c82010-01-27 21:56:44 +00002789
2790 switch (pdev->device) {
2791 case BE_DEVICE_ID1:
2792 case OC_DEVICE_ID1:
2793 adapter->generation = BE_GEN2;
2794 break;
2795 case BE_DEVICE_ID2:
2796 case OC_DEVICE_ID2:
2797 adapter->generation = BE_GEN3;
2798 break;
2799 default:
2800 adapter->generation = 0;
2801 }
2802
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002803 adapter->pdev = pdev;
2804 pci_set_drvdata(pdev, adapter);
2805 adapter->netdev = netdev;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002806 SET_NETDEV_DEV(netdev, &pdev->dev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002807
Yang Hongyange9304382009-04-13 14:40:14 -07002808 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002809 if (!status) {
2810 netdev->features |= NETIF_F_HIGHDMA;
2811 } else {
Yang Hongyange9304382009-04-13 14:40:14 -07002812 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002813 if (status) {
2814 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
2815 goto free_netdev;
2816 }
2817 }
2818
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002819 be_sriov_enable(adapter);
2820
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002821 status = be_ctrl_init(adapter);
2822 if (status)
2823 goto free_netdev;
2824
Sathya Perla2243e2e2009-11-22 22:02:03 +00002825 /* sync up with fw's ready state */
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002826 if (be_physfn(adapter)) {
2827 status = be_cmd_POST(adapter);
2828 if (status)
2829 goto ctrl_clean;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002830 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002831
2832 /* tell fw we're ready to fire cmds */
2833 status = be_cmd_fw_init(adapter);
2834 if (status)
2835 goto ctrl_clean;
2836
Sarveshwar Bandi556ae192010-05-24 18:38:25 -07002837 if (be_physfn(adapter)) {
2838 status = be_cmd_reset_function(adapter);
2839 if (status)
2840 goto ctrl_clean;
2841 }
2842
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002843 status = be_stats_init(adapter);
2844 if (status)
2845 goto ctrl_clean;
2846
Sathya Perla2243e2e2009-11-22 22:02:03 +00002847 status = be_get_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002848 if (status)
2849 goto stats_clean;
2850
Sathya Perla3abcded2010-10-03 22:12:27 -07002851 be_msix_enable(adapter);
2852
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002853 INIT_DELAYED_WORK(&adapter->work, be_worker);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002854
Sathya Perla5fb379e2009-06-18 00:02:59 +00002855 status = be_setup(adapter);
2856 if (status)
Sathya Perla3abcded2010-10-03 22:12:27 -07002857 goto msix_disable;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002858
Sathya Perla3abcded2010-10-03 22:12:27 -07002859 be_netdev_init(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002860 status = register_netdev(netdev);
2861 if (status != 0)
Sathya Perla5fb379e2009-06-18 00:02:59 +00002862 goto unsetup;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002863
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002864 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002865 return 0;
2866
Sathya Perla5fb379e2009-06-18 00:02:59 +00002867unsetup:
2868 be_clear(adapter);
Sathya Perla3abcded2010-10-03 22:12:27 -07002869msix_disable:
2870 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002871stats_clean:
2872 be_stats_cleanup(adapter);
2873ctrl_clean:
2874 be_ctrl_cleanup(adapter);
2875free_netdev:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002876 be_sriov_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002877 free_netdev(adapter->netdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002878 pci_set_drvdata(pdev, NULL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002879rel_reg:
2880 pci_release_regions(pdev);
2881disable_dev:
2882 pci_disable_device(pdev);
2883do_none:
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002884 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002885 return status;
2886}
2887
2888static int be_suspend(struct pci_dev *pdev, pm_message_t state)
2889{
2890 struct be_adapter *adapter = pci_get_drvdata(pdev);
2891 struct net_device *netdev = adapter->netdev;
2892
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002893 if (adapter->wol)
2894 be_setup_wol(adapter, true);
2895
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002896 netif_device_detach(netdev);
2897 if (netif_running(netdev)) {
2898 rtnl_lock();
2899 be_close(netdev);
2900 rtnl_unlock();
2901 }
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002902 be_cmd_get_flow_control(adapter, &adapter->tx_fc, &adapter->rx_fc);
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002903 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002904
2905 pci_save_state(pdev);
2906 pci_disable_device(pdev);
2907 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2908 return 0;
2909}
2910
2911static int be_resume(struct pci_dev *pdev)
2912{
2913 int status = 0;
2914 struct be_adapter *adapter = pci_get_drvdata(pdev);
2915 struct net_device *netdev = adapter->netdev;
2916
2917 netif_device_detach(netdev);
2918
2919 status = pci_enable_device(pdev);
2920 if (status)
2921 return status;
2922
2923 pci_set_power_state(pdev, 0);
2924 pci_restore_state(pdev);
2925
Sathya Perla2243e2e2009-11-22 22:02:03 +00002926 /* tell fw we're ready to fire cmds */
2927 status = be_cmd_fw_init(adapter);
2928 if (status)
2929 return status;
2930
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002931 be_setup(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002932 if (netif_running(netdev)) {
2933 rtnl_lock();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002934 be_open(netdev);
2935 rtnl_unlock();
2936 }
2937 netif_device_attach(netdev);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002938
2939 if (adapter->wol)
2940 be_setup_wol(adapter, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002941 return 0;
2942}
2943
Sathya Perla82456b02010-02-17 01:35:37 +00002944/*
2945 * An FLR will stop BE from DMAing any data.
2946 */
2947static void be_shutdown(struct pci_dev *pdev)
2948{
2949 struct be_adapter *adapter = pci_get_drvdata(pdev);
2950 struct net_device *netdev = adapter->netdev;
2951
2952 netif_device_detach(netdev);
2953
2954 be_cmd_reset_function(adapter);
2955
2956 if (adapter->wol)
2957 be_setup_wol(adapter, true);
2958
2959 pci_disable_device(pdev);
Sathya Perla82456b02010-02-17 01:35:37 +00002960}
2961
Sathya Perlacf588472010-02-14 21:22:01 +00002962static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
2963 pci_channel_state_t state)
2964{
2965 struct be_adapter *adapter = pci_get_drvdata(pdev);
2966 struct net_device *netdev = adapter->netdev;
2967
2968 dev_err(&adapter->pdev->dev, "EEH error detected\n");
2969
2970 adapter->eeh_err = true;
2971
2972 netif_device_detach(netdev);
2973
2974 if (netif_running(netdev)) {
2975 rtnl_lock();
2976 be_close(netdev);
2977 rtnl_unlock();
2978 }
2979 be_clear(adapter);
2980
2981 if (state == pci_channel_io_perm_failure)
2982 return PCI_ERS_RESULT_DISCONNECT;
2983
2984 pci_disable_device(pdev);
2985
2986 return PCI_ERS_RESULT_NEED_RESET;
2987}
2988
2989static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
2990{
2991 struct be_adapter *adapter = pci_get_drvdata(pdev);
2992 int status;
2993
2994 dev_info(&adapter->pdev->dev, "EEH reset\n");
2995 adapter->eeh_err = false;
2996
2997 status = pci_enable_device(pdev);
2998 if (status)
2999 return PCI_ERS_RESULT_DISCONNECT;
3000
3001 pci_set_master(pdev);
3002 pci_set_power_state(pdev, 0);
3003 pci_restore_state(pdev);
3004
3005 /* Check if card is ok and fw is ready */
3006 status = be_cmd_POST(adapter);
3007 if (status)
3008 return PCI_ERS_RESULT_DISCONNECT;
3009
3010 return PCI_ERS_RESULT_RECOVERED;
3011}
3012
3013static void be_eeh_resume(struct pci_dev *pdev)
3014{
3015 int status = 0;
3016 struct be_adapter *adapter = pci_get_drvdata(pdev);
3017 struct net_device *netdev = adapter->netdev;
3018
3019 dev_info(&adapter->pdev->dev, "EEH resume\n");
3020
3021 pci_save_state(pdev);
3022
3023 /* tell fw we're ready to fire cmds */
3024 status = be_cmd_fw_init(adapter);
3025 if (status)
3026 goto err;
3027
3028 status = be_setup(adapter);
3029 if (status)
3030 goto err;
3031
3032 if (netif_running(netdev)) {
3033 status = be_open(netdev);
3034 if (status)
3035 goto err;
3036 }
3037 netif_device_attach(netdev);
3038 return;
3039err:
3040 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
Sathya Perlacf588472010-02-14 21:22:01 +00003041}
3042
3043static struct pci_error_handlers be_eeh_handlers = {
3044 .error_detected = be_eeh_err_detected,
3045 .slot_reset = be_eeh_reset,
3046 .resume = be_eeh_resume,
3047};
3048
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003049static struct pci_driver be_driver = {
3050 .name = DRV_NAME,
3051 .id_table = be_dev_ids,
3052 .probe = be_probe,
3053 .remove = be_remove,
3054 .suspend = be_suspend,
Sathya Perlacf588472010-02-14 21:22:01 +00003055 .resume = be_resume,
Sathya Perla82456b02010-02-17 01:35:37 +00003056 .shutdown = be_shutdown,
Sathya Perlacf588472010-02-14 21:22:01 +00003057 .err_handler = &be_eeh_handlers
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003058};
3059
3060static int __init be_init_module(void)
3061{
Joe Perches8e95a202009-12-03 07:58:21 +00003062 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
3063 rx_frag_size != 2048) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003064 printk(KERN_WARNING DRV_NAME
3065 " : Module param rx_frag_size must be 2048/4096/8192."
3066 " Using 2048\n");
3067 rx_frag_size = 2048;
3068 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003069
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003070 if (num_vfs > 32) {
3071 printk(KERN_WARNING DRV_NAME
3072 " : Module param num_vfs must not be greater than 32."
3073 "Using 32\n");
3074 num_vfs = 32;
3075 }
3076
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003077 return pci_register_driver(&be_driver);
3078}
3079module_init(be_init_module);
3080
3081static void __exit be_exit_module(void)
3082{
3083 pci_unregister_driver(&be_driver);
3084}
3085module_exit(be_exit_module);