blob: 93354eee2cfd1e679929054c9cc28fe7b35dea51 [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
Somnath Koturcc4ce022010-10-21 07:11:14 -0700432static void wrb_fill_hdr(struct be_adapter *adapter, struct be_eth_hdr_wrb *hdr,
433 struct sk_buff *skb, u32 wrb_cnt, u32 len)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700434{
Somnath Koturcc4ce022010-10-21 07:11:14 -0700435 u8 vlan_prio = 0;
436 u16 vlan_tag = 0;
437
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700438 memset(hdr, 0, sizeof(*hdr));
439
440 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
441
Ajit Khaparde49e4b8472010-06-14 04:56:07 +0000442 if (skb_is_gso(skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700443 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
444 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
445 hdr, skb_shinfo(skb)->gso_size);
Ajit Khaparde49e4b8472010-06-14 04:56:07 +0000446 if (skb_is_gso_v6(skb))
447 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso6, hdr, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700448 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
449 if (is_tcp_pkt(skb))
450 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
451 else if (is_udp_pkt(skb))
452 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
453 }
454
Somnath Koturcc4ce022010-10-21 07:11:14 -0700455 if (adapter->vlan_grp && vlan_tx_tag_present(skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700456 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
Somnath Koturcc4ce022010-10-21 07:11:14 -0700457 vlan_tag = vlan_tx_tag_get(skb);
458 vlan_prio = (vlan_tag & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
459 /* If vlan priority provided by OS is NOT in available bmap */
460 if (!(adapter->vlan_prio_bmap & (1 << vlan_prio)))
461 vlan_tag = (vlan_tag & ~VLAN_PRIO_MASK) |
462 adapter->recommended_prio;
463 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag, hdr, vlan_tag);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700464 }
465
466 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
467 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
468 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
469 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
470}
471
Sathya Perla7101e112010-03-22 20:41:12 +0000472static void unmap_tx_frag(struct pci_dev *pdev, struct be_eth_wrb *wrb,
473 bool unmap_single)
474{
475 dma_addr_t dma;
476
477 be_dws_le_to_cpu(wrb, sizeof(*wrb));
478
479 dma = (u64)wrb->frag_pa_hi << 32 | (u64)wrb->frag_pa_lo;
FUJITA Tomonorib681ee72010-04-04 21:40:18 +0000480 if (wrb->frag_len) {
Sathya Perla7101e112010-03-22 20:41:12 +0000481 if (unmap_single)
482 pci_unmap_single(pdev, dma, wrb->frag_len,
483 PCI_DMA_TODEVICE);
484 else
485 pci_unmap_page(pdev, dma, wrb->frag_len,
486 PCI_DMA_TODEVICE);
487 }
488}
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700489
490static int make_tx_wrbs(struct be_adapter *adapter,
491 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
492{
Sathya Perla7101e112010-03-22 20:41:12 +0000493 dma_addr_t busaddr;
494 int i, copied = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700495 struct pci_dev *pdev = adapter->pdev;
496 struct sk_buff *first_skb = skb;
497 struct be_queue_info *txq = &adapter->tx_obj.q;
498 struct be_eth_wrb *wrb;
499 struct be_eth_hdr_wrb *hdr;
Sathya Perla7101e112010-03-22 20:41:12 +0000500 bool map_single = false;
501 u16 map_head;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700502
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700503 hdr = queue_head_node(txq);
504 queue_head_inc(txq);
Sathya Perla7101e112010-03-22 20:41:12 +0000505 map_head = txq->head;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700506
David S. Millerebc8d2a2009-06-09 01:01:31 -0700507 if (skb->len > skb->data_len) {
Eric Dumazete743d312010-04-14 15:59:40 -0700508 int len = skb_headlen(skb);
Alexander Duycka73b7962009-12-02 16:48:18 +0000509 busaddr = pci_map_single(pdev, skb->data, len,
510 PCI_DMA_TODEVICE);
Sathya Perla7101e112010-03-22 20:41:12 +0000511 if (pci_dma_mapping_error(pdev, busaddr))
512 goto dma_err;
513 map_single = true;
David S. Millerebc8d2a2009-06-09 01:01:31 -0700514 wrb = queue_head_node(txq);
515 wrb_fill(wrb, busaddr, len);
516 be_dws_cpu_to_le(wrb, sizeof(*wrb));
517 queue_head_inc(txq);
518 copied += len;
519 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700520
David S. Millerebc8d2a2009-06-09 01:01:31 -0700521 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
522 struct skb_frag_struct *frag =
523 &skb_shinfo(skb)->frags[i];
Alexander Duycka73b7962009-12-02 16:48:18 +0000524 busaddr = pci_map_page(pdev, frag->page,
525 frag->page_offset,
526 frag->size, PCI_DMA_TODEVICE);
Sathya Perla7101e112010-03-22 20:41:12 +0000527 if (pci_dma_mapping_error(pdev, busaddr))
528 goto dma_err;
David S. Millerebc8d2a2009-06-09 01:01:31 -0700529 wrb = queue_head_node(txq);
530 wrb_fill(wrb, busaddr, frag->size);
531 be_dws_cpu_to_le(wrb, sizeof(*wrb));
532 queue_head_inc(txq);
533 copied += frag->size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700534 }
535
536 if (dummy_wrb) {
537 wrb = queue_head_node(txq);
538 wrb_fill(wrb, 0, 0);
539 be_dws_cpu_to_le(wrb, sizeof(*wrb));
540 queue_head_inc(txq);
541 }
542
Somnath Koturcc4ce022010-10-21 07:11:14 -0700543 wrb_fill_hdr(adapter, hdr, first_skb, wrb_cnt, copied);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700544 be_dws_cpu_to_le(hdr, sizeof(*hdr));
545
546 return copied;
Sathya Perla7101e112010-03-22 20:41:12 +0000547dma_err:
548 txq->head = map_head;
549 while (copied) {
550 wrb = queue_head_node(txq);
551 unmap_tx_frag(pdev, wrb, map_single);
552 map_single = false;
553 copied -= wrb->frag_len;
554 queue_head_inc(txq);
555 }
556 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700557}
558
Stephen Hemminger613573252009-08-31 19:50:58 +0000559static netdev_tx_t be_xmit(struct sk_buff *skb,
Sathya Perlab31c50a2009-09-17 10:30:13 -0700560 struct net_device *netdev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700561{
562 struct be_adapter *adapter = netdev_priv(netdev);
563 struct be_tx_obj *tx_obj = &adapter->tx_obj;
564 struct be_queue_info *txq = &tx_obj->q;
565 u32 wrb_cnt = 0, copied = 0;
566 u32 start = txq->head;
567 bool dummy_wrb, stopped = false;
568
569 wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb);
570
571 copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000572 if (copied) {
573 /* record the sent skb in the sent_skb table */
574 BUG_ON(tx_obj->sent_skb_list[start]);
575 tx_obj->sent_skb_list[start] = skb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700576
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000577 /* Ensure txq has space for the next skb; Else stop the queue
578 * *BEFORE* ringing the tx doorbell, so that we serialze the
579 * tx compls of the current transmit which'll wake up the queue
580 */
Sathya Perla7101e112010-03-22 20:41:12 +0000581 atomic_add(wrb_cnt, &txq->used);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000582 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
583 txq->len) {
584 netif_stop_queue(netdev);
585 stopped = true;
586 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700587
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000588 be_txq_notify(adapter, txq->id, wrb_cnt);
589
Ajit Khaparde91992e42010-02-19 13:57:12 +0000590 be_tx_stats_update(adapter, wrb_cnt, copied,
591 skb_shinfo(skb)->gso_segs, stopped);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000592 } else {
593 txq->head = start;
594 dev_kfree_skb_any(skb);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700595 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700596 return NETDEV_TX_OK;
597}
598
599static int be_change_mtu(struct net_device *netdev, int new_mtu)
600{
601 struct be_adapter *adapter = netdev_priv(netdev);
602 if (new_mtu < BE_MIN_MTU ||
Ajit Khaparde34a89b82010-02-09 01:32:43 +0000603 new_mtu > (BE_MAX_JUMBO_FRAME_SIZE -
604 (ETH_HLEN + ETH_FCS_LEN))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700605 dev_info(&adapter->pdev->dev,
606 "MTU must be between %d and %d bytes\n",
Ajit Khaparde34a89b82010-02-09 01:32:43 +0000607 BE_MIN_MTU,
608 (BE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN)));
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700609 return -EINVAL;
610 }
611 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
612 netdev->mtu, new_mtu);
613 netdev->mtu = new_mtu;
614 return 0;
615}
616
617/*
Ajit Khaparde82903e42010-02-09 01:34:57 +0000618 * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
619 * If the user configures more, place BE in vlan promiscuous mode.
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700620 */
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000621static int be_vid_config(struct be_adapter *adapter, bool vf, u32 vf_num)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700622{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700623 u16 vtag[BE_NUM_VLANS_SUPPORTED];
624 u16 ntags = 0, i;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000625 int status = 0;
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000626 u32 if_handle;
627
628 if (vf) {
629 if_handle = adapter->vf_cfg[vf_num].vf_if_handle;
630 vtag[0] = cpu_to_le16(adapter->vf_cfg[vf_num].vf_vlan_tag);
631 status = be_cmd_vlan_config(adapter, if_handle, vtag, 1, 1, 0);
632 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700633
Ajit Khaparde82903e42010-02-09 01:34:57 +0000634 if (adapter->vlans_added <= adapter->max_vlans) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700635 /* Construct VLAN Table to give to HW */
Jesse Grossb7381272010-10-20 13:56:02 +0000636 for (i = 0; i < VLAN_N_VID; i++) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700637 if (adapter->vlan_tag[i]) {
638 vtag[ntags] = cpu_to_le16(i);
639 ntags++;
640 }
641 }
Sathya Perlab31c50a2009-09-17 10:30:13 -0700642 status = be_cmd_vlan_config(adapter, adapter->if_handle,
643 vtag, ntags, 1, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700644 } else {
Sathya Perlab31c50a2009-09-17 10:30:13 -0700645 status = be_cmd_vlan_config(adapter, adapter->if_handle,
646 NULL, 0, 1, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700647 }
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000648
Sathya Perlab31c50a2009-09-17 10:30:13 -0700649 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700650}
651
652static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
653{
654 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700655
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700656 adapter->vlan_grp = grp;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700657}
658
659static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
660{
661 struct be_adapter *adapter = netdev_priv(netdev);
662
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000663 adapter->vlans_added++;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000664 if (!be_physfn(adapter))
665 return;
666
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700667 adapter->vlan_tag[vid] = 1;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000668 if (adapter->vlans_added <= (adapter->max_vlans + 1))
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000669 be_vid_config(adapter, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700670}
671
672static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
673{
674 struct be_adapter *adapter = netdev_priv(netdev);
675
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000676 adapter->vlans_added--;
677 vlan_group_set_device(adapter->vlan_grp, vid, NULL);
678
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000679 if (!be_physfn(adapter))
680 return;
681
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700682 adapter->vlan_tag[vid] = 0;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000683 if (adapter->vlans_added <= adapter->max_vlans)
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000684 be_vid_config(adapter, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700685}
686
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700687static void be_set_multicast_list(struct net_device *netdev)
688{
689 struct be_adapter *adapter = netdev_priv(netdev);
690
691 if (netdev->flags & IFF_PROMISC) {
Sathya Perla8788fdc2009-07-27 22:52:03 +0000692 be_cmd_promiscuous_config(adapter, adapter->port_num, 1);
Sathya Perla24307ee2009-06-18 00:09:25 +0000693 adapter->promiscuous = true;
694 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700695 }
Sathya Perla24307ee2009-06-18 00:09:25 +0000696
697 /* BE was previously in promiscous mode; disable it */
698 if (adapter->promiscuous) {
699 adapter->promiscuous = false;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000700 be_cmd_promiscuous_config(adapter, adapter->port_num, 0);
Sathya Perla24307ee2009-06-18 00:09:25 +0000701 }
702
Sathya Perlae7b909a2009-11-22 22:01:10 +0000703 /* Enable multicast promisc if num configured exceeds what we support */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000704 if (netdev->flags & IFF_ALLMULTI ||
705 netdev_mc_count(netdev) > BE_MAX_MC) {
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000706 be_cmd_multicast_set(adapter, adapter->if_handle, NULL,
Sathya Perlae7b909a2009-11-22 22:01:10 +0000707 &adapter->mc_cmd_mem);
Sathya Perla24307ee2009-06-18 00:09:25 +0000708 goto done;
709 }
710
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000711 be_cmd_multicast_set(adapter, adapter->if_handle, netdev,
Sathya Perlaf31e50a2010-03-02 03:56:39 -0800712 &adapter->mc_cmd_mem);
Sathya Perla24307ee2009-06-18 00:09:25 +0000713done:
714 return;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700715}
716
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000717static int be_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
718{
719 struct be_adapter *adapter = netdev_priv(netdev);
720 int status;
721
722 if (!adapter->sriov_enabled)
723 return -EPERM;
724
725 if (!is_valid_ether_addr(mac) || (vf >= num_vfs))
726 return -EINVAL;
727
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000728 if (adapter->vf_cfg[vf].vf_pmac_id != BE_INVALID_PMAC_ID)
729 status = be_cmd_pmac_del(adapter,
730 adapter->vf_cfg[vf].vf_if_handle,
731 adapter->vf_cfg[vf].vf_pmac_id);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000732
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000733 status = be_cmd_pmac_add(adapter, mac,
734 adapter->vf_cfg[vf].vf_if_handle,
735 &adapter->vf_cfg[vf].vf_pmac_id);
736
737 if (status)
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000738 dev_err(&adapter->pdev->dev, "MAC %pM set on VF %d Failed\n",
739 mac, vf);
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000740 else
741 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
742
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000743 return status;
744}
745
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000746static int be_get_vf_config(struct net_device *netdev, int vf,
747 struct ifla_vf_info *vi)
748{
749 struct be_adapter *adapter = netdev_priv(netdev);
750
751 if (!adapter->sriov_enabled)
752 return -EPERM;
753
754 if (vf >= num_vfs)
755 return -EINVAL;
756
757 vi->vf = vf;
Ajit Khapardee1d18732010-07-23 01:52:13 +0000758 vi->tx_rate = adapter->vf_cfg[vf].vf_tx_rate;
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000759 vi->vlan = adapter->vf_cfg[vf].vf_vlan_tag;
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000760 vi->qos = 0;
761 memcpy(&vi->mac, adapter->vf_cfg[vf].vf_mac_addr, ETH_ALEN);
762
763 return 0;
764}
765
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000766static int be_set_vf_vlan(struct net_device *netdev,
767 int vf, u16 vlan, u8 qos)
768{
769 struct be_adapter *adapter = netdev_priv(netdev);
770 int status = 0;
771
772 if (!adapter->sriov_enabled)
773 return -EPERM;
774
775 if ((vf >= num_vfs) || (vlan > 4095))
776 return -EINVAL;
777
778 if (vlan) {
779 adapter->vf_cfg[vf].vf_vlan_tag = vlan;
780 adapter->vlans_added++;
781 } else {
782 adapter->vf_cfg[vf].vf_vlan_tag = 0;
783 adapter->vlans_added--;
784 }
785
786 status = be_vid_config(adapter, true, vf);
787
788 if (status)
789 dev_info(&adapter->pdev->dev,
790 "VLAN %d config on VF %d failed\n", vlan, vf);
791 return status;
792}
793
Ajit Khapardee1d18732010-07-23 01:52:13 +0000794static int be_set_vf_tx_rate(struct net_device *netdev,
795 int vf, int rate)
796{
797 struct be_adapter *adapter = netdev_priv(netdev);
798 int status = 0;
799
800 if (!adapter->sriov_enabled)
801 return -EPERM;
802
803 if ((vf >= num_vfs) || (rate < 0))
804 return -EINVAL;
805
806 if (rate > 10000)
807 rate = 10000;
808
809 adapter->vf_cfg[vf].vf_tx_rate = rate;
810 status = be_cmd_set_qos(adapter, rate / 10, vf);
811
812 if (status)
813 dev_info(&adapter->pdev->dev,
814 "tx rate %d on VF %d failed\n", rate, vf);
815 return status;
816}
817
Sathya Perla3abcded2010-10-03 22:12:27 -0700818static void be_rx_rate_update(struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700819{
Sathya Perla3abcded2010-10-03 22:12:27 -0700820 struct be_rx_stats *stats = &rxo->stats;
Sathya Perla4097f662009-03-24 16:40:13 -0700821 ulong now = jiffies;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700822
Sathya Perla4097f662009-03-24 16:40:13 -0700823 /* Wrapped around */
Sathya Perla3abcded2010-10-03 22:12:27 -0700824 if (time_before(now, stats->rx_jiffies)) {
825 stats->rx_jiffies = now;
Sathya Perla4097f662009-03-24 16:40:13 -0700826 return;
827 }
828
829 /* Update the rate once in two seconds */
Sathya Perla3abcded2010-10-03 22:12:27 -0700830 if ((now - stats->rx_jiffies) < 2 * HZ)
Sathya Perla4097f662009-03-24 16:40:13 -0700831 return;
832
Sathya Perla3abcded2010-10-03 22:12:27 -0700833 stats->rx_rate = be_calc_rate(stats->rx_bytes - stats->rx_bytes_prev,
834 now - stats->rx_jiffies);
835 stats->rx_jiffies = now;
836 stats->rx_bytes_prev = stats->rx_bytes;
Sathya Perla4097f662009-03-24 16:40:13 -0700837}
838
Sathya Perla3abcded2010-10-03 22:12:27 -0700839static void be_rx_stats_update(struct be_rx_obj *rxo,
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +0000840 u32 pktsize, u16 numfrags, u8 pkt_type)
Sathya Perla4097f662009-03-24 16:40:13 -0700841{
Sathya Perla3abcded2010-10-03 22:12:27 -0700842 struct be_rx_stats *stats = &rxo->stats;
Sathya Perla4097f662009-03-24 16:40:13 -0700843
Sathya Perla3abcded2010-10-03 22:12:27 -0700844 stats->rx_compl++;
845 stats->rx_frags += numfrags;
846 stats->rx_bytes += pktsize;
847 stats->rx_pkts++;
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +0000848 if (pkt_type == BE_MULTICAST_PACKET)
Sathya Perla3abcded2010-10-03 22:12:27 -0700849 stats->rx_mcast_pkts++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700850}
851
Somnath Koturc6ce2f42010-10-25 01:11:58 +0000852static inline bool csum_passed(struct be_eth_rx_compl *rxcp)
Ajit Khaparde728a9972009-04-13 15:41:22 -0700853{
Somnath Koturc6ce2f42010-10-25 01:11:58 +0000854 u8 l4_cksm, ipv6, ipcksm;
Ajit Khaparde728a9972009-04-13 15:41:22 -0700855
856 l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp);
857 ipcksm = AMAP_GET_BITS(struct amap_eth_rx_compl, ipcksm, rxcp);
Somnath Koturc6ce2f42010-10-25 01:11:58 +0000858 ipv6 = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
Ajit Khaparde728a9972009-04-13 15:41:22 -0700859
Somnath Koturc6ce2f42010-10-25 01:11:58 +0000860 /* Ignore ipcksm for ipv6 pkts */
861 return l4_cksm && (ipcksm || ipv6);
Ajit Khaparde728a9972009-04-13 15:41:22 -0700862}
863
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700864static struct be_rx_page_info *
Sathya Perla3abcded2010-10-03 22:12:27 -0700865get_rx_page_info(struct be_adapter *adapter,
866 struct be_rx_obj *rxo,
867 u16 frag_idx)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700868{
869 struct be_rx_page_info *rx_page_info;
Sathya Perla3abcded2010-10-03 22:12:27 -0700870 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700871
Sathya Perla3abcded2010-10-03 22:12:27 -0700872 rx_page_info = &rxo->page_info_tbl[frag_idx];
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700873 BUG_ON(!rx_page_info->page);
874
Ajit Khaparde205859a2010-02-09 01:34:21 +0000875 if (rx_page_info->last_page_user) {
FUJITA Tomonorifac6da52010-04-01 16:53:22 +0000876 pci_unmap_page(adapter->pdev, dma_unmap_addr(rx_page_info, bus),
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700877 adapter->big_page_size, PCI_DMA_FROMDEVICE);
Ajit Khaparde205859a2010-02-09 01:34:21 +0000878 rx_page_info->last_page_user = false;
879 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700880
881 atomic_dec(&rxq->used);
882 return rx_page_info;
883}
884
885/* Throwaway the data in the Rx completion */
886static void be_rx_compl_discard(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -0700887 struct be_rx_obj *rxo,
888 struct be_eth_rx_compl *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700889{
Sathya Perla3abcded2010-10-03 22:12:27 -0700890 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700891 struct be_rx_page_info *page_info;
892 u16 rxq_idx, i, num_rcvd;
893
894 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
895 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
896
897 for (i = 0; i < num_rcvd; i++) {
Sathya Perla3abcded2010-10-03 22:12:27 -0700898 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700899 put_page(page_info->page);
900 memset(page_info, 0, sizeof(*page_info));
901 index_inc(&rxq_idx, rxq->len);
902 }
903}
904
905/*
906 * skb_fill_rx_data forms a complete skb for an ether frame
907 * indicated by rxcp.
908 */
Sathya Perla3abcded2010-10-03 22:12:27 -0700909static void skb_fill_rx_data(struct be_adapter *adapter, struct be_rx_obj *rxo,
Sathya Perla89420422010-02-17 01:35:26 +0000910 struct sk_buff *skb, struct be_eth_rx_compl *rxcp,
911 u16 num_rcvd)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700912{
Sathya Perla3abcded2010-10-03 22:12:27 -0700913 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700914 struct be_rx_page_info *page_info;
Sathya Perla89420422010-02-17 01:35:26 +0000915 u16 rxq_idx, i, j;
Ajit Khapardefa774062009-07-22 09:28:55 -0700916 u32 pktsize, hdr_len, curr_frag_len, size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700917 u8 *start;
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +0000918 u8 pkt_type;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700919
920 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
921 pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +0000922 pkt_type = AMAP_GET_BITS(struct amap_eth_rx_compl, cast_enc, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700923
Sathya Perla3abcded2010-10-03 22:12:27 -0700924 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700925
926 start = page_address(page_info->page) + page_info->page_offset;
927 prefetch(start);
928
929 /* Copy data in the first descriptor of this completion */
930 curr_frag_len = min(pktsize, rx_frag_size);
931
932 /* Copy the header portion into skb_data */
933 hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
934 memcpy(skb->data, start, hdr_len);
935 skb->len = curr_frag_len;
936 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
937 /* Complete packet has now been moved to data */
938 put_page(page_info->page);
939 skb->data_len = 0;
940 skb->tail += curr_frag_len;
941 } else {
942 skb_shinfo(skb)->nr_frags = 1;
943 skb_shinfo(skb)->frags[0].page = page_info->page;
944 skb_shinfo(skb)->frags[0].page_offset =
945 page_info->page_offset + hdr_len;
946 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
947 skb->data_len = curr_frag_len - hdr_len;
948 skb->tail += hdr_len;
949 }
Ajit Khaparde205859a2010-02-09 01:34:21 +0000950 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700951
952 if (pktsize <= rx_frag_size) {
953 BUG_ON(num_rcvd != 1);
Sathya Perla76fbb422009-06-10 02:21:56 +0000954 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700955 }
956
957 /* More frags present for this completion */
Ajit Khapardefa774062009-07-22 09:28:55 -0700958 size = pktsize;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000959 for (i = 1, j = 0; i < num_rcvd; i++) {
Ajit Khapardefa774062009-07-22 09:28:55 -0700960 size -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700961 index_inc(&rxq_idx, rxq->len);
Sathya Perla3abcded2010-10-03 22:12:27 -0700962 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700963
Ajit Khapardefa774062009-07-22 09:28:55 -0700964 curr_frag_len = min(size, rx_frag_size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700965
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000966 /* Coalesce all frags from the same physical page in one slot */
967 if (page_info->page_offset == 0) {
968 /* Fresh page */
969 j++;
970 skb_shinfo(skb)->frags[j].page = page_info->page;
971 skb_shinfo(skb)->frags[j].page_offset =
972 page_info->page_offset;
973 skb_shinfo(skb)->frags[j].size = 0;
974 skb_shinfo(skb)->nr_frags++;
975 } else {
976 put_page(page_info->page);
977 }
978
979 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700980 skb->len += curr_frag_len;
981 skb->data_len += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700982
Ajit Khaparde205859a2010-02-09 01:34:21 +0000983 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700984 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000985 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700986
Sathya Perla76fbb422009-06-10 02:21:56 +0000987done:
Sathya Perla3abcded2010-10-03 22:12:27 -0700988 be_rx_stats_update(rxo, pktsize, num_rcvd, pkt_type);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700989}
990
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700991/* Process the RX completion indicated by rxcp when GRO is disabled */
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700992static void be_rx_compl_process(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -0700993 struct be_rx_obj *rxo,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700994 struct be_eth_rx_compl *rxcp)
995{
996 struct sk_buff *skb;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700997 u32 vlanf, vid;
Sathya Perla89420422010-02-17 01:35:26 +0000998 u16 num_rcvd;
Ajit Khapardedcb9b562009-09-30 21:58:22 -0700999 u8 vtm;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001000
Sathya Perla89420422010-02-17 01:35:26 +00001001 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
1002 /* Is it a flush compl that has no data */
1003 if (unlikely(num_rcvd == 0))
1004 return;
1005
Eric Dumazet89d71a62009-10-13 05:34:20 +00001006 skb = netdev_alloc_skb_ip_align(adapter->netdev, BE_HDR_LEN);
Sathya Perlaa058a632010-02-17 01:34:22 +00001007 if (unlikely(!skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001008 if (net_ratelimit())
1009 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
Sathya Perla3abcded2010-10-03 22:12:27 -07001010 be_rx_compl_discard(adapter, rxo, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001011 return;
1012 }
1013
Sathya Perla3abcded2010-10-03 22:12:27 -07001014 skb_fill_rx_data(adapter, rxo, skb, rxcp, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001015
Somnath Koturc6ce2f42010-10-25 01:11:58 +00001016 if (likely(adapter->rx_csum && csum_passed(rxcp)))
Ajit Khaparde728a9972009-04-13 15:41:22 -07001017 skb->ip_summed = CHECKSUM_UNNECESSARY;
Somnath Koturc6ce2f42010-10-25 01:11:58 +00001018 else
1019 skb_checksum_none_assert(skb);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001020
1021 skb->truesize = skb->len + sizeof(struct sk_buff);
1022 skb->protocol = eth_type_trans(skb, adapter->netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001023
Sathya Perlaa058a632010-02-17 01:34:22 +00001024 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
1025 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
1026
1027 /* vlanf could be wrongly set in some cards.
1028 * ignore if vtm is not set */
Ajit Khaparde3486be22010-07-23 02:04:54 +00001029 if ((adapter->function_mode & 0x400) && !vtm)
Sathya Perlaa058a632010-02-17 01:34:22 +00001030 vlanf = 0;
1031
1032 if (unlikely(vlanf)) {
Ajit Khaparde82903e42010-02-09 01:34:57 +00001033 if (!adapter->vlan_grp || adapter->vlans_added == 0) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001034 kfree_skb(skb);
1035 return;
1036 }
1037 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
Ajit Khaparde9cae9e42010-03-31 02:00:32 +00001038 vid = swab16(vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001039 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
1040 } else {
1041 netif_receive_skb(skb);
1042 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001043}
1044
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001045/* Process the RX completion indicated by rxcp when GRO is enabled */
1046static void be_rx_compl_process_gro(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -07001047 struct be_rx_obj *rxo,
1048 struct be_eth_rx_compl *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001049{
1050 struct be_rx_page_info *page_info;
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001051 struct sk_buff *skb = NULL;
Sathya Perla3abcded2010-10-03 22:12:27 -07001052 struct be_queue_info *rxq = &rxo->q;
1053 struct be_eq_obj *eq_obj = &rxo->rx_eq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001054 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001055 u16 i, rxq_idx = 0, vid, j;
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001056 u8 vtm;
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +00001057 u8 pkt_type;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001058
1059 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
Sathya Perla89420422010-02-17 01:35:26 +00001060 /* Is it a flush compl that has no data */
1061 if (unlikely(num_rcvd == 0))
1062 return;
1063
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001064 pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
1065 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
1066 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001067 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +00001068 pkt_type = AMAP_GET_BITS(struct amap_eth_rx_compl, cast_enc, rxcp);
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001069
1070 /* vlanf could be wrongly set in some cards.
1071 * ignore if vtm is not set */
Ajit Khaparde3486be22010-07-23 02:04:54 +00001072 if ((adapter->function_mode & 0x400) && !vtm)
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001073 vlanf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001074
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001075 skb = napi_get_frags(&eq_obj->napi);
1076 if (!skb) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001077 be_rx_compl_discard(adapter, rxo, rxcp);
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001078 return;
1079 }
1080
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001081 remaining = pkt_size;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001082 for (i = 0, j = -1; i < num_rcvd; i++) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001083 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001084
1085 curr_frag_len = min(remaining, rx_frag_size);
1086
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001087 /* Coalesce all frags from the same physical page in one slot */
1088 if (i == 0 || page_info->page_offset == 0) {
1089 /* First frag or Fresh page */
1090 j++;
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001091 skb_shinfo(skb)->frags[j].page = page_info->page;
1092 skb_shinfo(skb)->frags[j].page_offset =
1093 page_info->page_offset;
1094 skb_shinfo(skb)->frags[j].size = 0;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001095 } else {
1096 put_page(page_info->page);
1097 }
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001098 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001099
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001100 remaining -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001101 index_inc(&rxq_idx, rxq->len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001102 memset(page_info, 0, sizeof(*page_info));
1103 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001104 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001105
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001106 skb_shinfo(skb)->nr_frags = j + 1;
1107 skb->len = pkt_size;
1108 skb->data_len = pkt_size;
1109 skb->truesize += pkt_size;
1110 skb->ip_summed = CHECKSUM_UNNECESSARY;
1111
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001112 if (likely(!vlanf)) {
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001113 napi_gro_frags(&eq_obj->napi);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001114 } else {
1115 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
Ajit Khaparde9cae9e42010-03-31 02:00:32 +00001116 vid = swab16(vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001117
Ajit Khaparde82903e42010-02-09 01:34:57 +00001118 if (!adapter->vlan_grp || adapter->vlans_added == 0)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001119 return;
1120
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001121 vlan_gro_frags(&eq_obj->napi, adapter->vlan_grp, vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001122 }
1123
Sathya Perla3abcded2010-10-03 22:12:27 -07001124 be_rx_stats_update(rxo, pkt_size, num_rcvd, pkt_type);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001125}
1126
Sathya Perla3abcded2010-10-03 22:12:27 -07001127static struct be_eth_rx_compl *be_rx_compl_get(struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001128{
Sathya Perla3abcded2010-10-03 22:12:27 -07001129 struct be_eth_rx_compl *rxcp = queue_tail_node(&rxo->cq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001130
1131 if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
1132 return NULL;
1133
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001134 rmb();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001135 be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
1136
Sathya Perla3abcded2010-10-03 22:12:27 -07001137 queue_tail_inc(&rxo->cq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001138 return rxcp;
1139}
1140
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001141/* To reset the valid bit, we need to reset the whole word as
1142 * when walking the queue the valid entries are little-endian
1143 * and invalid entries are host endian
1144 */
1145static inline void be_rx_compl_reset(struct be_eth_rx_compl *rxcp)
1146{
1147 rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
1148}
1149
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001150static inline struct page *be_alloc_pages(u32 size)
1151{
1152 gfp_t alloc_flags = GFP_ATOMIC;
1153 u32 order = get_order(size);
1154 if (order > 0)
1155 alloc_flags |= __GFP_COMP;
1156 return alloc_pages(alloc_flags, order);
1157}
1158
1159/*
1160 * Allocate a page, split it to fragments of size rx_frag_size and post as
1161 * receive buffers to BE
1162 */
Sathya Perla3abcded2010-10-03 22:12:27 -07001163static void be_post_rx_frags(struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001164{
Sathya Perla3abcded2010-10-03 22:12:27 -07001165 struct be_adapter *adapter = rxo->adapter;
1166 struct be_rx_page_info *page_info_tbl = rxo->page_info_tbl;
Sathya Perla26d92f92010-01-21 22:52:08 -08001167 struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
Sathya Perla3abcded2010-10-03 22:12:27 -07001168 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001169 struct page *pagep = NULL;
1170 struct be_eth_rx_d *rxd;
1171 u64 page_dmaaddr = 0, frag_dmaaddr;
1172 u32 posted, page_offset = 0;
1173
Sathya Perla3abcded2010-10-03 22:12:27 -07001174 page_info = &rxo->page_info_tbl[rxq->head];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001175 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
1176 if (!pagep) {
1177 pagep = be_alloc_pages(adapter->big_page_size);
1178 if (unlikely(!pagep)) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001179 rxo->stats.rx_post_fail++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001180 break;
1181 }
1182 page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
1183 adapter->big_page_size,
1184 PCI_DMA_FROMDEVICE);
1185 page_info->page_offset = 0;
1186 } else {
1187 get_page(pagep);
1188 page_info->page_offset = page_offset + rx_frag_size;
1189 }
1190 page_offset = page_info->page_offset;
1191 page_info->page = pagep;
FUJITA Tomonorifac6da52010-04-01 16:53:22 +00001192 dma_unmap_addr_set(page_info, bus, page_dmaaddr);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001193 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
1194
1195 rxd = queue_head_node(rxq);
1196 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
1197 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001198
1199 /* Any space left in the current big page for another frag? */
1200 if ((page_offset + rx_frag_size + rx_frag_size) >
1201 adapter->big_page_size) {
1202 pagep = NULL;
1203 page_info->last_page_user = true;
1204 }
Sathya Perla26d92f92010-01-21 22:52:08 -08001205
1206 prev_page_info = page_info;
1207 queue_head_inc(rxq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001208 page_info = &page_info_tbl[rxq->head];
1209 }
1210 if (pagep)
Sathya Perla26d92f92010-01-21 22:52:08 -08001211 prev_page_info->last_page_user = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001212
1213 if (posted) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001214 atomic_add(posted, &rxq->used);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001215 be_rxq_notify(adapter, rxq->id, posted);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001216 } else if (atomic_read(&rxq->used) == 0) {
1217 /* Let be_worker replenish when memory is available */
Sathya Perla3abcded2010-10-03 22:12:27 -07001218 rxo->rx_post_starved = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001219 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001220}
1221
Sathya Perla5fb379e2009-06-18 00:02:59 +00001222static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001223{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001224 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
1225
1226 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
1227 return NULL;
1228
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001229 rmb();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001230 be_dws_le_to_cpu(txcp, sizeof(*txcp));
1231
1232 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
1233
1234 queue_tail_inc(tx_cq);
1235 return txcp;
1236}
1237
1238static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
1239{
1240 struct be_queue_info *txq = &adapter->tx_obj.q;
Alexander Duycka73b7962009-12-02 16:48:18 +00001241 struct be_eth_wrb *wrb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001242 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1243 struct sk_buff *sent_skb;
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001244 u16 cur_index, num_wrbs = 1; /* account for hdr wrb */
1245 bool unmap_skb_hdr = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001246
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001247 sent_skb = sent_skbs[txq->tail];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001248 BUG_ON(!sent_skb);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001249 sent_skbs[txq->tail] = NULL;
1250
1251 /* skip header wrb */
Alexander Duycka73b7962009-12-02 16:48:18 +00001252 queue_tail_inc(txq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001253
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001254 do {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001255 cur_index = txq->tail;
Alexander Duycka73b7962009-12-02 16:48:18 +00001256 wrb = queue_tail_node(txq);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001257 unmap_tx_frag(adapter->pdev, wrb, (unmap_skb_hdr &&
Eric Dumazete743d312010-04-14 15:59:40 -07001258 skb_headlen(sent_skb)));
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001259 unmap_skb_hdr = false;
1260
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001261 num_wrbs++;
1262 queue_tail_inc(txq);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001263 } while (cur_index != last_index);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001264
1265 atomic_sub(num_wrbs, &txq->used);
Alexander Duycka73b7962009-12-02 16:48:18 +00001266
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001267 kfree_skb(sent_skb);
1268}
1269
Sathya Perla859b1e42009-08-10 03:43:51 +00001270static inline struct be_eq_entry *event_get(struct be_eq_obj *eq_obj)
1271{
1272 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1273
1274 if (!eqe->evt)
1275 return NULL;
1276
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001277 rmb();
Sathya Perla859b1e42009-08-10 03:43:51 +00001278 eqe->evt = le32_to_cpu(eqe->evt);
1279 queue_tail_inc(&eq_obj->q);
1280 return eqe;
1281}
1282
1283static int event_handle(struct be_adapter *adapter,
1284 struct be_eq_obj *eq_obj)
1285{
1286 struct be_eq_entry *eqe;
1287 u16 num = 0;
1288
1289 while ((eqe = event_get(eq_obj)) != NULL) {
1290 eqe->evt = 0;
1291 num++;
1292 }
1293
1294 /* Deal with any spurious interrupts that come
1295 * without events
1296 */
1297 be_eq_notify(adapter, eq_obj->q.id, true, true, num);
1298 if (num)
1299 napi_schedule(&eq_obj->napi);
1300
1301 return num;
1302}
1303
1304/* Just read and notify events without processing them.
1305 * Used at the time of destroying event queues */
1306static void be_eq_clean(struct be_adapter *adapter,
1307 struct be_eq_obj *eq_obj)
1308{
1309 struct be_eq_entry *eqe;
1310 u16 num = 0;
1311
1312 while ((eqe = event_get(eq_obj)) != NULL) {
1313 eqe->evt = 0;
1314 num++;
1315 }
1316
1317 if (num)
1318 be_eq_notify(adapter, eq_obj->q.id, false, true, num);
1319}
1320
Sathya Perla3abcded2010-10-03 22:12:27 -07001321static void be_rx_q_clean(struct be_adapter *adapter, struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001322{
1323 struct be_rx_page_info *page_info;
Sathya Perla3abcded2010-10-03 22:12:27 -07001324 struct be_queue_info *rxq = &rxo->q;
1325 struct be_queue_info *rx_cq = &rxo->cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001326 struct be_eth_rx_compl *rxcp;
1327 u16 tail;
1328
1329 /* First cleanup pending rx completions */
Sathya Perla3abcded2010-10-03 22:12:27 -07001330 while ((rxcp = be_rx_compl_get(rxo)) != NULL) {
1331 be_rx_compl_discard(adapter, rxo, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001332 be_rx_compl_reset(rxcp);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001333 be_cq_notify(adapter, rx_cq->id, true, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001334 }
1335
1336 /* Then free posted rx buffer that were not used */
1337 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
Sathya Perlacdab23b2009-08-10 03:43:23 +00001338 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001339 page_info = get_rx_page_info(adapter, rxo, tail);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001340 put_page(page_info->page);
1341 memset(page_info, 0, sizeof(*page_info));
1342 }
1343 BUG_ON(atomic_read(&rxq->used));
1344}
1345
Sathya Perlaa8e91792009-08-10 03:42:43 +00001346static void be_tx_compl_clean(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001347{
Sathya Perlaa8e91792009-08-10 03:42:43 +00001348 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001349 struct be_queue_info *txq = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001350 struct be_eth_tx_compl *txcp;
1351 u16 end_idx, cmpl = 0, timeo = 0;
Sathya Perlab03388d2010-02-18 00:37:17 +00001352 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1353 struct sk_buff *sent_skb;
1354 bool dummy_wrb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001355
Sathya Perlaa8e91792009-08-10 03:42:43 +00001356 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1357 do {
1358 while ((txcp = be_tx_compl_get(tx_cq))) {
1359 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1360 wrb_index, txcp);
1361 be_tx_compl_process(adapter, end_idx);
1362 cmpl++;
1363 }
1364 if (cmpl) {
1365 be_cq_notify(adapter, tx_cq->id, false, cmpl);
1366 cmpl = 0;
1367 }
1368
1369 if (atomic_read(&txq->used) == 0 || ++timeo > 200)
1370 break;
1371
1372 mdelay(1);
1373 } while (true);
1374
1375 if (atomic_read(&txq->used))
1376 dev_err(&adapter->pdev->dev, "%d pending tx-completions\n",
1377 atomic_read(&txq->used));
Sathya Perlab03388d2010-02-18 00:37:17 +00001378
1379 /* free posted tx for which compls will never arrive */
1380 while (atomic_read(&txq->used)) {
1381 sent_skb = sent_skbs[txq->tail];
1382 end_idx = txq->tail;
1383 index_adv(&end_idx,
1384 wrb_cnt_for_skb(sent_skb, &dummy_wrb) - 1, txq->len);
1385 be_tx_compl_process(adapter, end_idx);
1386 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001387}
1388
Sathya Perla5fb379e2009-06-18 00:02:59 +00001389static void be_mcc_queues_destroy(struct be_adapter *adapter)
1390{
1391 struct be_queue_info *q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001392
Sathya Perla8788fdc2009-07-27 22:52:03 +00001393 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001394 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001395 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001396 be_queue_free(adapter, q);
1397
Sathya Perla8788fdc2009-07-27 22:52:03 +00001398 q = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001399 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001400 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001401 be_queue_free(adapter, q);
1402}
1403
1404/* Must be called only after TX qs are created as MCC shares TX EQ */
1405static int be_mcc_queues_create(struct be_adapter *adapter)
1406{
1407 struct be_queue_info *q, *cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001408
1409 /* Alloc MCC compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001410 cq = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001411 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
Sathya Perlaefd2e402009-07-27 22:53:10 +00001412 sizeof(struct be_mcc_compl)))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001413 goto err;
1414
1415 /* Ask BE to create MCC compl queue; share TX's eq */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001416 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001417 goto mcc_cq_free;
1418
1419 /* Alloc MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001420 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001421 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1422 goto mcc_cq_destroy;
1423
1424 /* Ask BE to create MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001425 if (be_cmd_mccq_create(adapter, q, cq))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001426 goto mcc_q_free;
1427
1428 return 0;
1429
1430mcc_q_free:
1431 be_queue_free(adapter, q);
1432mcc_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001433 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001434mcc_cq_free:
1435 be_queue_free(adapter, cq);
1436err:
1437 return -1;
1438}
1439
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001440static void be_tx_queues_destroy(struct be_adapter *adapter)
1441{
1442 struct be_queue_info *q;
1443
1444 q = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001445 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001446 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001447 be_queue_free(adapter, q);
1448
1449 q = &adapter->tx_obj.cq;
1450 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001451 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001452 be_queue_free(adapter, q);
1453
Sathya Perla859b1e42009-08-10 03:43:51 +00001454 /* Clear any residual events */
1455 be_eq_clean(adapter, &adapter->tx_eq);
1456
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001457 q = &adapter->tx_eq.q;
1458 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001459 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001460 be_queue_free(adapter, q);
1461}
1462
1463static int be_tx_queues_create(struct be_adapter *adapter)
1464{
1465 struct be_queue_info *eq, *q, *cq;
1466
1467 adapter->tx_eq.max_eqd = 0;
1468 adapter->tx_eq.min_eqd = 0;
1469 adapter->tx_eq.cur_eqd = 96;
1470 adapter->tx_eq.enable_aic = false;
1471 /* Alloc Tx Event queue */
1472 eq = &adapter->tx_eq.q;
1473 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1474 return -1;
1475
1476 /* Ask BE to create Tx Event queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001477 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001478 goto tx_eq_free;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001479 adapter->base_eq_id = adapter->tx_eq.q.id;
1480
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001481 /* Alloc TX eth compl queue */
1482 cq = &adapter->tx_obj.cq;
1483 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1484 sizeof(struct be_eth_tx_compl)))
1485 goto tx_eq_destroy;
1486
1487 /* Ask BE to create Tx eth compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001488 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001489 goto tx_cq_free;
1490
1491 /* Alloc TX eth queue */
1492 q = &adapter->tx_obj.q;
1493 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1494 goto tx_cq_destroy;
1495
1496 /* Ask BE to create Tx eth queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001497 if (be_cmd_txq_create(adapter, q, cq))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001498 goto tx_q_free;
1499 return 0;
1500
1501tx_q_free:
1502 be_queue_free(adapter, q);
1503tx_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001504 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001505tx_cq_free:
1506 be_queue_free(adapter, cq);
1507tx_eq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001508 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001509tx_eq_free:
1510 be_queue_free(adapter, eq);
1511 return -1;
1512}
1513
1514static void be_rx_queues_destroy(struct be_adapter *adapter)
1515{
1516 struct be_queue_info *q;
Sathya Perla3abcded2010-10-03 22:12:27 -07001517 struct be_rx_obj *rxo;
1518 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001519
Sathya Perla3abcded2010-10-03 22:12:27 -07001520 for_all_rx_queues(adapter, rxo, i) {
1521 q = &rxo->q;
1522 if (q->created) {
1523 be_cmd_q_destroy(adapter, q, QTYPE_RXQ);
1524 /* After the rxq is invalidated, wait for a grace time
1525 * of 1ms for all dma to end and the flush compl to
1526 * arrive
1527 */
1528 mdelay(1);
1529 be_rx_q_clean(adapter, rxo);
1530 }
1531 be_queue_free(adapter, q);
Sathya Perla89420422010-02-17 01:35:26 +00001532
Sathya Perla3abcded2010-10-03 22:12:27 -07001533 q = &rxo->cq;
1534 if (q->created)
1535 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1536 be_queue_free(adapter, q);
1537
1538 /* Clear any residual events */
1539 q = &rxo->rx_eq.q;
1540 if (q->created) {
1541 be_eq_clean(adapter, &rxo->rx_eq);
1542 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
1543 }
1544 be_queue_free(adapter, q);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001545 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001546}
1547
1548static int be_rx_queues_create(struct be_adapter *adapter)
1549{
1550 struct be_queue_info *eq, *q, *cq;
Sathya Perla3abcded2010-10-03 22:12:27 -07001551 struct be_rx_obj *rxo;
1552 int rc, i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001553
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001554 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
Sathya Perla3abcded2010-10-03 22:12:27 -07001555 for_all_rx_queues(adapter, rxo, i) {
1556 rxo->adapter = adapter;
1557 rxo->rx_eq.max_eqd = BE_MAX_EQD;
1558 rxo->rx_eq.enable_aic = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001559
Sathya Perla3abcded2010-10-03 22:12:27 -07001560 /* EQ */
1561 eq = &rxo->rx_eq.q;
1562 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1563 sizeof(struct be_eq_entry));
1564 if (rc)
1565 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001566
Sathya Perla3abcded2010-10-03 22:12:27 -07001567 rc = be_cmd_eq_create(adapter, eq, rxo->rx_eq.cur_eqd);
1568 if (rc)
1569 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001570
Sathya Perla3abcded2010-10-03 22:12:27 -07001571 /* CQ */
1572 cq = &rxo->cq;
1573 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1574 sizeof(struct be_eth_rx_compl));
1575 if (rc)
1576 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001577
Sathya Perla3abcded2010-10-03 22:12:27 -07001578 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
1579 if (rc)
1580 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001581
Sathya Perla3abcded2010-10-03 22:12:27 -07001582 /* Rx Q */
1583 q = &rxo->q;
1584 rc = be_queue_alloc(adapter, q, RX_Q_LEN,
1585 sizeof(struct be_eth_rx_d));
1586 if (rc)
1587 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001588
Sathya Perla3abcded2010-10-03 22:12:27 -07001589 rc = be_cmd_rxq_create(adapter, q, cq->id, rx_frag_size,
1590 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle,
1591 (i > 0) ? 1 : 0/* rss enable */, &rxo->rss_id);
1592 if (rc)
1593 goto err;
1594 }
1595
1596 if (be_multi_rxq(adapter)) {
1597 u8 rsstable[MAX_RSS_QS];
1598
1599 for_all_rss_queues(adapter, rxo, i)
1600 rsstable[i] = rxo->rss_id;
1601
1602 rc = be_cmd_rss_config(adapter, rsstable,
1603 adapter->num_rx_qs - 1);
1604 if (rc)
1605 goto err;
1606 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001607
1608 return 0;
Sathya Perla3abcded2010-10-03 22:12:27 -07001609err:
1610 be_rx_queues_destroy(adapter);
1611 return -1;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001612}
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001613
Sathya Perlab628bde2009-08-17 00:58:26 +00001614/* There are 8 evt ids per func. Retruns the evt id's bit number */
1615static inline int be_evt_bit_get(struct be_adapter *adapter, u32 eq_id)
1616{
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001617 return eq_id - adapter->base_eq_id;
Sathya Perlab628bde2009-08-17 00:58:26 +00001618}
1619
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001620static irqreturn_t be_intx(int irq, void *dev)
1621{
1622 struct be_adapter *adapter = dev;
Sathya Perla3abcded2010-10-03 22:12:27 -07001623 struct be_rx_obj *rxo;
1624 int isr, i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001625
Sathya Perla8788fdc2009-07-27 22:52:03 +00001626 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
Sathya Perla55bdeed2010-02-02 07:48:40 -08001627 (adapter->tx_eq.q.id/ 8) * CEV_ISR_SIZE);
Sathya Perlac001c212009-07-01 01:06:07 +00001628 if (!isr)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001629 return IRQ_NONE;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001630
Sathya Perla3abcded2010-10-03 22:12:27 -07001631 if ((1 << be_evt_bit_get(adapter, adapter->tx_eq.q.id) & isr))
1632 event_handle(adapter, &adapter->tx_eq);
1633
1634 for_all_rx_queues(adapter, rxo, i) {
1635 if ((1 << be_evt_bit_get(adapter, rxo->rx_eq.q.id) & isr))
1636 event_handle(adapter, &rxo->rx_eq);
1637 }
Sathya Perlac001c212009-07-01 01:06:07 +00001638
Sathya Perla8788fdc2009-07-27 22:52:03 +00001639 return IRQ_HANDLED;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001640}
1641
1642static irqreturn_t be_msix_rx(int irq, void *dev)
1643{
Sathya Perla3abcded2010-10-03 22:12:27 -07001644 struct be_rx_obj *rxo = dev;
1645 struct be_adapter *adapter = rxo->adapter;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001646
Sathya Perla3abcded2010-10-03 22:12:27 -07001647 event_handle(adapter, &rxo->rx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001648
1649 return IRQ_HANDLED;
1650}
1651
Sathya Perla5fb379e2009-06-18 00:02:59 +00001652static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001653{
1654 struct be_adapter *adapter = dev;
1655
Sathya Perla8788fdc2009-07-27 22:52:03 +00001656 event_handle(adapter, &adapter->tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001657
1658 return IRQ_HANDLED;
1659}
1660
Sathya Perla3abcded2010-10-03 22:12:27 -07001661static inline bool do_gro(struct be_adapter *adapter, struct be_rx_obj *rxo,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001662 struct be_eth_rx_compl *rxcp)
1663{
1664 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1665 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1666
1667 if (err)
Sathya Perla3abcded2010-10-03 22:12:27 -07001668 rxo->stats.rxcp_err++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001669
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001670 return (tcp_frame && !err) ? true : false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001671}
1672
stephen hemminger49b05222010-10-21 07:50:48 +00001673static int be_poll_rx(struct napi_struct *napi, int budget)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001674{
1675 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
Sathya Perla3abcded2010-10-03 22:12:27 -07001676 struct be_rx_obj *rxo = container_of(rx_eq, struct be_rx_obj, rx_eq);
1677 struct be_adapter *adapter = rxo->adapter;
1678 struct be_queue_info *rx_cq = &rxo->cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001679 struct be_eth_rx_compl *rxcp;
1680 u32 work_done;
1681
Sathya Perla3abcded2010-10-03 22:12:27 -07001682 rxo->stats.rx_polls++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001683 for (work_done = 0; work_done < budget; work_done++) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001684 rxcp = be_rx_compl_get(rxo);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001685 if (!rxcp)
1686 break;
1687
Sathya Perla3abcded2010-10-03 22:12:27 -07001688 if (do_gro(adapter, rxo, rxcp))
1689 be_rx_compl_process_gro(adapter, rxo, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001690 else
Sathya Perla3abcded2010-10-03 22:12:27 -07001691 be_rx_compl_process(adapter, rxo, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001692
1693 be_rx_compl_reset(rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001694 }
1695
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001696 /* Refill the queue */
Sathya Perla3abcded2010-10-03 22:12:27 -07001697 if (atomic_read(&rxo->q.used) < RX_FRAGS_REFILL_WM)
1698 be_post_rx_frags(rxo);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001699
1700 /* All consumed */
1701 if (work_done < budget) {
1702 napi_complete(napi);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001703 be_cq_notify(adapter, rx_cq->id, true, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001704 } else {
1705 /* More to be consumed; continue with interrupts disabled */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001706 be_cq_notify(adapter, rx_cq->id, false, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001707 }
1708 return work_done;
1709}
1710
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001711/* As TX and MCC share the same EQ check for both TX and MCC completions.
1712 * For TX/MCC we don't honour budget; consume everything
1713 */
1714static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001715{
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001716 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1717 struct be_adapter *adapter =
1718 container_of(tx_eq, struct be_adapter, tx_eq);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001719 struct be_queue_info *txq = &adapter->tx_obj.q;
1720 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001721 struct be_eth_tx_compl *txcp;
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001722 int tx_compl = 0, mcc_compl, status = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001723 u16 end_idx;
1724
Sathya Perla5fb379e2009-06-18 00:02:59 +00001725 while ((txcp = be_tx_compl_get(tx_cq))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001726 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001727 wrb_index, txcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001728 be_tx_compl_process(adapter, end_idx);
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001729 tx_compl++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001730 }
1731
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001732 mcc_compl = be_process_mcc(adapter, &status);
1733
1734 napi_complete(napi);
1735
1736 if (mcc_compl) {
1737 struct be_mcc_obj *mcc_obj = &adapter->mcc_obj;
1738 be_cq_notify(adapter, mcc_obj->cq.id, true, mcc_compl);
1739 }
1740
1741 if (tx_compl) {
1742 be_cq_notify(adapter, adapter->tx_obj.cq.id, true, tx_compl);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001743
1744 /* As Tx wrbs have been freed up, wake up netdev queue if
1745 * it was stopped due to lack of tx wrbs.
1746 */
1747 if (netif_queue_stopped(adapter->netdev) &&
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001748 atomic_read(&txq->used) < txq->len / 2) {
Sathya Perla5fb379e2009-06-18 00:02:59 +00001749 netif_wake_queue(adapter->netdev);
1750 }
1751
Sathya Perla3abcded2010-10-03 22:12:27 -07001752 tx_stats(adapter)->be_tx_events++;
1753 tx_stats(adapter)->be_tx_compl += tx_compl;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001754 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001755
1756 return 1;
1757}
1758
Ajit Khaparded053de92010-09-03 06:23:30 +00001759void be_detect_dump_ue(struct be_adapter *adapter)
Ajit Khaparde7c185272010-07-29 06:16:33 +00001760{
1761 u32 ue_status_lo, ue_status_hi, ue_status_lo_mask, ue_status_hi_mask;
1762 u32 i;
1763
1764 pci_read_config_dword(adapter->pdev,
1765 PCICFG_UE_STATUS_LOW, &ue_status_lo);
1766 pci_read_config_dword(adapter->pdev,
1767 PCICFG_UE_STATUS_HIGH, &ue_status_hi);
1768 pci_read_config_dword(adapter->pdev,
1769 PCICFG_UE_STATUS_LOW_MASK, &ue_status_lo_mask);
1770 pci_read_config_dword(adapter->pdev,
1771 PCICFG_UE_STATUS_HI_MASK, &ue_status_hi_mask);
1772
1773 ue_status_lo = (ue_status_lo & (~ue_status_lo_mask));
1774 ue_status_hi = (ue_status_hi & (~ue_status_hi_mask));
1775
Ajit Khaparded053de92010-09-03 06:23:30 +00001776 if (ue_status_lo || ue_status_hi) {
1777 adapter->ue_detected = true;
1778 dev_err(&adapter->pdev->dev, "UE Detected!!\n");
1779 }
1780
Ajit Khaparde7c185272010-07-29 06:16:33 +00001781 if (ue_status_lo) {
1782 for (i = 0; ue_status_lo; ue_status_lo >>= 1, i++) {
1783 if (ue_status_lo & 1)
1784 dev_err(&adapter->pdev->dev,
1785 "UE: %s bit set\n", ue_status_low_desc[i]);
1786 }
1787 }
1788 if (ue_status_hi) {
1789 for (i = 0; ue_status_hi; ue_status_hi >>= 1, i++) {
1790 if (ue_status_hi & 1)
1791 dev_err(&adapter->pdev->dev,
1792 "UE: %s bit set\n", ue_status_hi_desc[i]);
1793 }
1794 }
1795
1796}
1797
Sathya Perlaea1dae12009-03-19 23:56:20 -07001798static void be_worker(struct work_struct *work)
1799{
1800 struct be_adapter *adapter =
1801 container_of(work, struct be_adapter, work.work);
Sathya Perla3abcded2010-10-03 22:12:27 -07001802 struct be_rx_obj *rxo;
1803 int i;
Sathya Perlaea1dae12009-03-19 23:56:20 -07001804
Somnath Koturf203af72010-10-25 23:01:03 +00001805 /* when interrupts are not yet enabled, just reap any pending
1806 * mcc completions */
1807 if (!netif_running(adapter->netdev)) {
1808 int mcc_compl, status = 0;
1809
1810 mcc_compl = be_process_mcc(adapter, &status);
1811
1812 if (mcc_compl) {
1813 struct be_mcc_obj *mcc_obj = &adapter->mcc_obj;
1814 be_cq_notify(adapter, mcc_obj->cq.id, false, mcc_compl);
1815 }
1816 goto reschedule;
1817 }
1818
Ajit Khaparde0fc48c32010-07-29 06:18:58 +00001819 if (!adapter->stats_ioctl_sent)
Sathya Perla3abcded2010-10-03 22:12:27 -07001820 be_cmd_get_stats(adapter, &adapter->stats_cmd);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001821
Sathya Perla4097f662009-03-24 16:40:13 -07001822 be_tx_rate_update(adapter);
Sathya Perla4097f662009-03-24 16:40:13 -07001823
Sathya Perla3abcded2010-10-03 22:12:27 -07001824 for_all_rx_queues(adapter, rxo, i) {
1825 be_rx_rate_update(rxo);
1826 be_rx_eqd_update(adapter, rxo);
1827
1828 if (rxo->rx_post_starved) {
1829 rxo->rx_post_starved = false;
1830 be_post_rx_frags(rxo);
1831 }
Sathya Perlaea1dae12009-03-19 23:56:20 -07001832 }
Sathya Perla3abcded2010-10-03 22:12:27 -07001833
Ajit Khaparded053de92010-09-03 06:23:30 +00001834 if (!adapter->ue_detected)
1835 be_detect_dump_ue(adapter);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001836
Somnath Koturf203af72010-10-25 23:01:03 +00001837reschedule:
Sathya Perlaea1dae12009-03-19 23:56:20 -07001838 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1839}
1840
Sathya Perla8d56ff12009-11-22 22:02:26 +00001841static void be_msix_disable(struct be_adapter *adapter)
1842{
1843 if (adapter->msix_enabled) {
1844 pci_disable_msix(adapter->pdev);
1845 adapter->msix_enabled = false;
1846 }
1847}
1848
Sathya Perla3abcded2010-10-03 22:12:27 -07001849static int be_num_rxqs_get(struct be_adapter *adapter)
1850{
1851 if (multi_rxq && (adapter->function_caps & BE_FUNCTION_CAPS_RSS) &&
1852 !adapter->sriov_enabled && !(adapter->function_mode & 0x400)) {
1853 return 1 + MAX_RSS_QS; /* one default non-RSS queue */
1854 } else {
1855 dev_warn(&adapter->pdev->dev,
1856 "No support for multiple RX queues\n");
1857 return 1;
1858 }
1859}
1860
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001861static void be_msix_enable(struct be_adapter *adapter)
1862{
Sathya Perla3abcded2010-10-03 22:12:27 -07001863#define BE_MIN_MSIX_VECTORS (1 + 1) /* Rx + Tx */
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001864 int i, status;
1865
Sathya Perla3abcded2010-10-03 22:12:27 -07001866 adapter->num_rx_qs = be_num_rxqs_get(adapter);
1867
1868 for (i = 0; i < (adapter->num_rx_qs + 1); i++)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001869 adapter->msix_entries[i].entry = i;
1870
1871 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
Sathya Perla3abcded2010-10-03 22:12:27 -07001872 adapter->num_rx_qs + 1);
1873 if (status == 0) {
1874 goto done;
1875 } else if (status >= BE_MIN_MSIX_VECTORS) {
1876 if (pci_enable_msix(adapter->pdev, adapter->msix_entries,
1877 status) == 0) {
1878 adapter->num_rx_qs = status - 1;
1879 dev_warn(&adapter->pdev->dev,
1880 "Could alloc only %d MSIx vectors. "
1881 "Using %d RX Qs\n", status, adapter->num_rx_qs);
1882 goto done;
1883 }
1884 }
1885 return;
1886done:
1887 adapter->msix_enabled = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001888}
1889
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001890static void be_sriov_enable(struct be_adapter *adapter)
1891{
Sarveshwar Bandi344dbf12010-07-09 01:43:55 +00001892 be_check_sriov_fn_type(adapter);
Ajit Khaparde6dedec82010-07-29 06:15:32 +00001893#ifdef CONFIG_PCI_IOV
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001894 if (be_physfn(adapter) && num_vfs) {
Ajit Khaparde6dedec82010-07-29 06:15:32 +00001895 int status;
1896
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001897 status = pci_enable_sriov(adapter->pdev, num_vfs);
1898 adapter->sriov_enabled = status ? false : true;
1899 }
1900#endif
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001901}
1902
1903static void be_sriov_disable(struct be_adapter *adapter)
1904{
1905#ifdef CONFIG_PCI_IOV
1906 if (adapter->sriov_enabled) {
1907 pci_disable_sriov(adapter->pdev);
1908 adapter->sriov_enabled = false;
1909 }
1910#endif
1911}
1912
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001913static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1914{
Sathya Perlab628bde2009-08-17 00:58:26 +00001915 return adapter->msix_entries[
1916 be_evt_bit_get(adapter, eq_id)].vector;
1917}
1918
1919static int be_request_irq(struct be_adapter *adapter,
1920 struct be_eq_obj *eq_obj,
Sathya Perla3abcded2010-10-03 22:12:27 -07001921 void *handler, char *desc, void *context)
Sathya Perlab628bde2009-08-17 00:58:26 +00001922{
1923 struct net_device *netdev = adapter->netdev;
1924 int vec;
1925
1926 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
1927 vec = be_msix_vec_get(adapter, eq_obj->q.id);
Sathya Perla3abcded2010-10-03 22:12:27 -07001928 return request_irq(vec, handler, 0, eq_obj->desc, context);
Sathya Perlab628bde2009-08-17 00:58:26 +00001929}
1930
Sathya Perla3abcded2010-10-03 22:12:27 -07001931static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj,
1932 void *context)
Sathya Perlab628bde2009-08-17 00:58:26 +00001933{
1934 int vec = be_msix_vec_get(adapter, eq_obj->q.id);
Sathya Perla3abcded2010-10-03 22:12:27 -07001935 free_irq(vec, context);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001936}
1937
1938static int be_msix_register(struct be_adapter *adapter)
1939{
Sathya Perla3abcded2010-10-03 22:12:27 -07001940 struct be_rx_obj *rxo;
1941 int status, i;
1942 char qname[10];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001943
Sathya Perla3abcded2010-10-03 22:12:27 -07001944 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx",
1945 adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001946 if (status)
1947 goto err;
1948
Sathya Perla3abcded2010-10-03 22:12:27 -07001949 for_all_rx_queues(adapter, rxo, i) {
1950 sprintf(qname, "rxq%d", i);
1951 status = be_request_irq(adapter, &rxo->rx_eq, be_msix_rx,
1952 qname, rxo);
1953 if (status)
1954 goto err_msix;
1955 }
Sathya Perlab628bde2009-08-17 00:58:26 +00001956
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001957 return 0;
Sathya Perlab628bde2009-08-17 00:58:26 +00001958
Sathya Perla3abcded2010-10-03 22:12:27 -07001959err_msix:
1960 be_free_irq(adapter, &adapter->tx_eq, adapter);
1961
1962 for (i--, rxo = &adapter->rx_obj[i]; i >= 0; i--, rxo--)
1963 be_free_irq(adapter, &rxo->rx_eq, rxo);
1964
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001965err:
1966 dev_warn(&adapter->pdev->dev,
1967 "MSIX Request IRQ failed - err %d\n", status);
1968 pci_disable_msix(adapter->pdev);
1969 adapter->msix_enabled = false;
1970 return status;
1971}
1972
1973static int be_irq_register(struct be_adapter *adapter)
1974{
1975 struct net_device *netdev = adapter->netdev;
1976 int status;
1977
1978 if (adapter->msix_enabled) {
1979 status = be_msix_register(adapter);
1980 if (status == 0)
1981 goto done;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001982 /* INTx is not supported for VF */
1983 if (!be_physfn(adapter))
1984 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001985 }
1986
1987 /* INTx */
1988 netdev->irq = adapter->pdev->irq;
1989 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1990 adapter);
1991 if (status) {
1992 dev_err(&adapter->pdev->dev,
1993 "INTx request IRQ failed - err %d\n", status);
1994 return status;
1995 }
1996done:
1997 adapter->isr_registered = true;
1998 return 0;
1999}
2000
2001static void be_irq_unregister(struct be_adapter *adapter)
2002{
2003 struct net_device *netdev = adapter->netdev;
Sathya Perla3abcded2010-10-03 22:12:27 -07002004 struct be_rx_obj *rxo;
2005 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002006
2007 if (!adapter->isr_registered)
2008 return;
2009
2010 /* INTx */
2011 if (!adapter->msix_enabled) {
2012 free_irq(netdev->irq, adapter);
2013 goto done;
2014 }
2015
2016 /* MSIx */
Sathya Perla3abcded2010-10-03 22:12:27 -07002017 be_free_irq(adapter, &adapter->tx_eq, adapter);
2018
2019 for_all_rx_queues(adapter, rxo, i)
2020 be_free_irq(adapter, &rxo->rx_eq, rxo);
2021
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002022done:
2023 adapter->isr_registered = false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002024}
2025
Sathya Perla889cd4b2010-05-30 23:33:45 +00002026static int be_close(struct net_device *netdev)
2027{
2028 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3abcded2010-10-03 22:12:27 -07002029 struct be_rx_obj *rxo;
Sathya Perla889cd4b2010-05-30 23:33:45 +00002030 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla3abcded2010-10-03 22:12:27 -07002031 int vec, i;
Sathya Perla889cd4b2010-05-30 23:33:45 +00002032
Sathya Perla889cd4b2010-05-30 23:33:45 +00002033 be_async_mcc_disable(adapter);
2034
2035 netif_stop_queue(netdev);
2036 netif_carrier_off(netdev);
2037 adapter->link_up = false;
2038
2039 be_intr_set(adapter, false);
2040
2041 if (adapter->msix_enabled) {
2042 vec = be_msix_vec_get(adapter, tx_eq->q.id);
2043 synchronize_irq(vec);
Sathya Perla3abcded2010-10-03 22:12:27 -07002044
2045 for_all_rx_queues(adapter, rxo, i) {
2046 vec = be_msix_vec_get(adapter, rxo->rx_eq.q.id);
2047 synchronize_irq(vec);
2048 }
Sathya Perla889cd4b2010-05-30 23:33:45 +00002049 } else {
2050 synchronize_irq(netdev->irq);
2051 }
2052 be_irq_unregister(adapter);
2053
Sathya Perla3abcded2010-10-03 22:12:27 -07002054 for_all_rx_queues(adapter, rxo, i)
2055 napi_disable(&rxo->rx_eq.napi);
2056
Sathya Perla889cd4b2010-05-30 23:33:45 +00002057 napi_disable(&tx_eq->napi);
2058
2059 /* Wait for all pending tx completions to arrive so that
2060 * all tx skbs are freed.
2061 */
2062 be_tx_compl_clean(adapter);
2063
2064 return 0;
2065}
2066
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002067static int be_open(struct net_device *netdev)
2068{
2069 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002070 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla3abcded2010-10-03 22:12:27 -07002071 struct be_rx_obj *rxo;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002072 bool link_up;
Sathya Perla3abcded2010-10-03 22:12:27 -07002073 int status, i;
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07002074 u8 mac_speed;
2075 u16 link_speed;
Sathya Perla5fb379e2009-06-18 00:02:59 +00002076
Sathya Perla3abcded2010-10-03 22:12:27 -07002077 for_all_rx_queues(adapter, rxo, i) {
2078 be_post_rx_frags(rxo);
2079 napi_enable(&rxo->rx_eq.napi);
2080 }
Sathya Perla5fb379e2009-06-18 00:02:59 +00002081 napi_enable(&tx_eq->napi);
2082
2083 be_irq_register(adapter);
2084
Sathya Perla8788fdc2009-07-27 22:52:03 +00002085 be_intr_set(adapter, true);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002086
2087 /* The evt queues are created in unarmed state; arm them */
Sathya Perla3abcded2010-10-03 22:12:27 -07002088 for_all_rx_queues(adapter, rxo, i) {
2089 be_eq_notify(adapter, rxo->rx_eq.q.id, true, false, 0);
2090 be_cq_notify(adapter, rxo->cq.id, true, 0);
2091 }
Sathya Perla8788fdc2009-07-27 22:52:03 +00002092 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002093
Sathya Perla7a1e9b22010-02-17 01:35:11 +00002094 /* Now that interrupts are on we can process async mcc */
2095 be_async_mcc_enable(adapter);
2096
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07002097 status = be_cmd_link_status_query(adapter, &link_up, &mac_speed,
2098 &link_speed);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002099 if (status)
Sathya Perla889cd4b2010-05-30 23:33:45 +00002100 goto err;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002101 be_link_status_update(adapter, link_up);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002102
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002103 if (be_physfn(adapter)) {
Ajit Khaparde1da87b72010-07-23 01:51:22 +00002104 status = be_vid_config(adapter, false, 0);
Sathya Perla889cd4b2010-05-30 23:33:45 +00002105 if (status)
2106 goto err;
2107
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002108 status = be_cmd_set_flow_control(adapter,
2109 adapter->tx_fc, adapter->rx_fc);
2110 if (status)
Sathya Perla889cd4b2010-05-30 23:33:45 +00002111 goto err;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002112 }
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00002113
Sathya Perla889cd4b2010-05-30 23:33:45 +00002114 return 0;
2115err:
2116 be_close(adapter->netdev);
2117 return -EIO;
Sathya Perla5fb379e2009-06-18 00:02:59 +00002118}
2119
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002120static int be_setup_wol(struct be_adapter *adapter, bool enable)
2121{
2122 struct be_dma_mem cmd;
2123 int status = 0;
2124 u8 mac[ETH_ALEN];
2125
2126 memset(mac, 0, ETH_ALEN);
2127
2128 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
2129 cmd.va = pci_alloc_consistent(adapter->pdev, cmd.size, &cmd.dma);
2130 if (cmd.va == NULL)
2131 return -1;
2132 memset(cmd.va, 0, cmd.size);
2133
2134 if (enable) {
2135 status = pci_write_config_dword(adapter->pdev,
2136 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
2137 if (status) {
2138 dev_err(&adapter->pdev->dev,
Frans Pop2381a552010-03-24 07:57:36 +00002139 "Could not enable Wake-on-lan\n");
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002140 pci_free_consistent(adapter->pdev, cmd.size, cmd.va,
2141 cmd.dma);
2142 return status;
2143 }
2144 status = be_cmd_enable_magic_wol(adapter,
2145 adapter->netdev->dev_addr, &cmd);
2146 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
2147 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
2148 } else {
2149 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
2150 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
2151 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
2152 }
2153
2154 pci_free_consistent(adapter->pdev, cmd.size, cmd.va, cmd.dma);
2155 return status;
2156}
2157
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002158/*
2159 * Generate a seed MAC address from the PF MAC Address using jhash.
2160 * MAC Address for VFs are assigned incrementally starting from the seed.
2161 * These addresses are programmed in the ASIC by the PF and the VF driver
2162 * queries for the MAC address during its probe.
2163 */
2164static inline int be_vf_eth_addr_config(struct be_adapter *adapter)
2165{
2166 u32 vf = 0;
Sathya Perla3abcded2010-10-03 22:12:27 -07002167 int status = 0;
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002168 u8 mac[ETH_ALEN];
2169
2170 be_vf_eth_addr_generate(adapter, mac);
2171
2172 for (vf = 0; vf < num_vfs; vf++) {
2173 status = be_cmd_pmac_add(adapter, mac,
2174 adapter->vf_cfg[vf].vf_if_handle,
2175 &adapter->vf_cfg[vf].vf_pmac_id);
2176 if (status)
2177 dev_err(&adapter->pdev->dev,
2178 "Mac address add failed for VF %d\n", vf);
2179 else
2180 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
2181
2182 mac[5] += 1;
2183 }
2184 return status;
2185}
2186
2187static inline void be_vf_eth_addr_rem(struct be_adapter *adapter)
2188{
2189 u32 vf;
2190
2191 for (vf = 0; vf < num_vfs; vf++) {
2192 if (adapter->vf_cfg[vf].vf_pmac_id != BE_INVALID_PMAC_ID)
2193 be_cmd_pmac_del(adapter,
2194 adapter->vf_cfg[vf].vf_if_handle,
2195 adapter->vf_cfg[vf].vf_pmac_id);
2196 }
2197}
2198
Sathya Perla5fb379e2009-06-18 00:02:59 +00002199static int be_setup(struct be_adapter *adapter)
2200{
Sathya Perla5fb379e2009-06-18 00:02:59 +00002201 struct net_device *netdev = adapter->netdev;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002202 u32 cap_flags, en_flags, vf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002203 int status;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002204 u8 mac[ETH_ALEN];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002205
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002206 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST;
2207
2208 if (be_physfn(adapter)) {
2209 cap_flags |= BE_IF_FLAGS_MCAST_PROMISCUOUS |
2210 BE_IF_FLAGS_PROMISCUOUS |
2211 BE_IF_FLAGS_PASS_L3L4_ERRORS;
2212 en_flags |= BE_IF_FLAGS_PASS_L3L4_ERRORS;
Sathya Perla3abcded2010-10-03 22:12:27 -07002213
2214 if (be_multi_rxq(adapter)) {
2215 cap_flags |= BE_IF_FLAGS_RSS;
2216 en_flags |= BE_IF_FLAGS_RSS;
2217 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002218 }
Sathya Perla73d540f2009-10-14 20:20:42 +00002219
2220 status = be_cmd_if_create(adapter, cap_flags, en_flags,
2221 netdev->dev_addr, false/* pmac_invalid */,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002222 &adapter->if_handle, &adapter->pmac_id, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002223 if (status != 0)
2224 goto do_none;
2225
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002226 if (be_physfn(adapter)) {
2227 while (vf < num_vfs) {
2228 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED
2229 | BE_IF_FLAGS_BROADCAST;
2230 status = be_cmd_if_create(adapter, cap_flags, en_flags,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002231 mac, true,
2232 &adapter->vf_cfg[vf].vf_if_handle,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002233 NULL, vf+1);
2234 if (status) {
2235 dev_err(&adapter->pdev->dev,
2236 "Interface Create failed for VF %d\n", vf);
2237 goto if_destroy;
2238 }
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002239 adapter->vf_cfg[vf].vf_pmac_id = BE_INVALID_PMAC_ID;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002240 vf++;
Sarveshwar Bandi84e5b9f2010-05-27 16:28:15 -07002241 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002242 } else if (!be_physfn(adapter)) {
2243 status = be_cmd_mac_addr_query(adapter, mac,
2244 MAC_ADDRESS_TYPE_NETWORK, false, adapter->if_handle);
2245 if (!status) {
2246 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2247 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2248 }
2249 }
2250
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002251 status = be_tx_queues_create(adapter);
2252 if (status != 0)
2253 goto if_destroy;
2254
2255 status = be_rx_queues_create(adapter);
2256 if (status != 0)
2257 goto tx_qs_destroy;
2258
Sathya Perla5fb379e2009-06-18 00:02:59 +00002259 status = be_mcc_queues_create(adapter);
2260 if (status != 0)
2261 goto rx_qs_destroy;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002262
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002263 if (be_physfn(adapter)) {
2264 status = be_vf_eth_addr_config(adapter);
2265 if (status)
2266 goto mcc_q_destroy;
2267 }
2268
Ajit Khaparde0dffc832009-11-29 17:57:46 +00002269 adapter->link_speed = -1;
2270
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002271 return 0;
2272
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002273mcc_q_destroy:
2274 if (be_physfn(adapter))
2275 be_vf_eth_addr_rem(adapter);
2276 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002277rx_qs_destroy:
2278 be_rx_queues_destroy(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002279tx_qs_destroy:
2280 be_tx_queues_destroy(adapter);
2281if_destroy:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002282 for (vf = 0; vf < num_vfs; vf++)
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002283 if (adapter->vf_cfg[vf].vf_if_handle)
2284 be_cmd_if_destroy(adapter,
2285 adapter->vf_cfg[vf].vf_if_handle);
Sathya Perla8788fdc2009-07-27 22:52:03 +00002286 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002287do_none:
2288 return status;
2289}
2290
Sathya Perla5fb379e2009-06-18 00:02:59 +00002291static int be_clear(struct be_adapter *adapter)
2292{
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002293 if (be_physfn(adapter))
2294 be_vf_eth_addr_rem(adapter);
2295
Sathya Perla1a8887d2009-08-17 00:58:41 +00002296 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002297 be_rx_queues_destroy(adapter);
2298 be_tx_queues_destroy(adapter);
2299
Sathya Perla8788fdc2009-07-27 22:52:03 +00002300 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002301
Sathya Perla2243e2e2009-11-22 22:02:03 +00002302 /* tell fw we're done with firing cmds */
2303 be_cmd_fw_clean(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002304 return 0;
2305}
2306
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002307
Ajit Khaparde84517482009-09-04 03:12:16 +00002308#define FW_FILE_HDR_SIGN "ServerEngines Corp. "
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002309static bool be_flash_redboot(struct be_adapter *adapter,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002310 const u8 *p, u32 img_start, int image_size,
2311 int hdr_size)
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002312{
2313 u32 crc_offset;
2314 u8 flashed_crc[4];
2315 int status;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002316
2317 crc_offset = hdr_size + img_start + image_size - 4;
2318
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002319 p += crc_offset;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002320
2321 status = be_cmd_get_flash_crc(adapter, flashed_crc,
Ajit Khapardef510fc62010-03-31 01:47:45 +00002322 (image_size - 4));
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002323 if (status) {
2324 dev_err(&adapter->pdev->dev,
2325 "could not get crc from flash, not flashing redboot\n");
2326 return false;
2327 }
2328
2329 /*update redboot only if crc does not match*/
2330 if (!memcmp(flashed_crc, p, 4))
2331 return false;
2332 else
2333 return true;
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002334}
2335
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002336static int be_flash_data(struct be_adapter *adapter,
Ajit Khaparde84517482009-09-04 03:12:16 +00002337 const struct firmware *fw,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002338 struct be_dma_mem *flash_cmd, int num_of_images)
2339
Ajit Khaparde84517482009-09-04 03:12:16 +00002340{
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002341 int status = 0, i, filehdr_size = 0;
2342 u32 total_bytes = 0, flash_op;
Ajit Khaparde84517482009-09-04 03:12:16 +00002343 int num_bytes;
2344 const u8 *p = fw->data;
2345 struct be_cmd_write_flashrom *req = flash_cmd->va;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002346 struct flash_comp *pflashcomp;
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002347 int num_comp;
Ajit Khaparde84517482009-09-04 03:12:16 +00002348
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002349 struct flash_comp gen3_flash_types[9] = {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002350 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, IMG_TYPE_ISCSI_ACTIVE,
2351 FLASH_IMAGE_MAX_SIZE_g3},
2352 { FLASH_REDBOOT_START_g3, IMG_TYPE_REDBOOT,
2353 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3},
2354 { FLASH_iSCSI_BIOS_START_g3, IMG_TYPE_BIOS,
2355 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2356 { FLASH_PXE_BIOS_START_g3, IMG_TYPE_PXE_BIOS,
2357 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2358 { FLASH_FCoE_BIOS_START_g3, IMG_TYPE_FCOE_BIOS,
2359 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2360 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, IMG_TYPE_ISCSI_BACKUP,
2361 FLASH_IMAGE_MAX_SIZE_g3},
2362 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, IMG_TYPE_FCOE_FW_ACTIVE,
2363 FLASH_IMAGE_MAX_SIZE_g3},
2364 { FLASH_FCoE_BACKUP_IMAGE_START_g3, IMG_TYPE_FCOE_FW_BACKUP,
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002365 FLASH_IMAGE_MAX_SIZE_g3},
2366 { FLASH_NCSI_START_g3, IMG_TYPE_NCSI_FW,
2367 FLASH_NCSI_IMAGE_MAX_SIZE_g3}
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002368 };
2369 struct flash_comp gen2_flash_types[8] = {
2370 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, IMG_TYPE_ISCSI_ACTIVE,
2371 FLASH_IMAGE_MAX_SIZE_g2},
2372 { FLASH_REDBOOT_START_g2, IMG_TYPE_REDBOOT,
2373 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2},
2374 { FLASH_iSCSI_BIOS_START_g2, IMG_TYPE_BIOS,
2375 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2376 { FLASH_PXE_BIOS_START_g2, IMG_TYPE_PXE_BIOS,
2377 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2378 { FLASH_FCoE_BIOS_START_g2, IMG_TYPE_FCOE_BIOS,
2379 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2380 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, IMG_TYPE_ISCSI_BACKUP,
2381 FLASH_IMAGE_MAX_SIZE_g2},
2382 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, IMG_TYPE_FCOE_FW_ACTIVE,
2383 FLASH_IMAGE_MAX_SIZE_g2},
2384 { FLASH_FCoE_BACKUP_IMAGE_START_g2, IMG_TYPE_FCOE_FW_BACKUP,
2385 FLASH_IMAGE_MAX_SIZE_g2}
2386 };
2387
2388 if (adapter->generation == BE_GEN3) {
2389 pflashcomp = gen3_flash_types;
2390 filehdr_size = sizeof(struct flash_file_hdr_g3);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002391 num_comp = 9;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002392 } else {
2393 pflashcomp = gen2_flash_types;
2394 filehdr_size = sizeof(struct flash_file_hdr_g2);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002395 num_comp = 8;
Ajit Khaparde84517482009-09-04 03:12:16 +00002396 }
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002397 for (i = 0; i < num_comp; i++) {
2398 if ((pflashcomp[i].optype == IMG_TYPE_NCSI_FW) &&
2399 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
2400 continue;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002401 if ((pflashcomp[i].optype == IMG_TYPE_REDBOOT) &&
2402 (!be_flash_redboot(adapter, fw->data,
2403 pflashcomp[i].offset, pflashcomp[i].size,
2404 filehdr_size)))
2405 continue;
2406 p = fw->data;
2407 p += filehdr_size + pflashcomp[i].offset
2408 + (num_of_images * sizeof(struct image_hdr));
2409 if (p + pflashcomp[i].size > fw->data + fw->size)
Ajit Khaparde84517482009-09-04 03:12:16 +00002410 return -1;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002411 total_bytes = pflashcomp[i].size;
2412 while (total_bytes) {
2413 if (total_bytes > 32*1024)
2414 num_bytes = 32*1024;
2415 else
2416 num_bytes = total_bytes;
2417 total_bytes -= num_bytes;
Ajit Khaparde84517482009-09-04 03:12:16 +00002418
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002419 if (!total_bytes)
2420 flash_op = FLASHROM_OPER_FLASH;
2421 else
2422 flash_op = FLASHROM_OPER_SAVE;
2423 memcpy(req->params.data_buf, p, num_bytes);
2424 p += num_bytes;
2425 status = be_cmd_write_flashrom(adapter, flash_cmd,
2426 pflashcomp[i].optype, flash_op, num_bytes);
2427 if (status) {
2428 dev_err(&adapter->pdev->dev,
2429 "cmd to write to flash rom failed.\n");
2430 return -1;
2431 }
2432 yield();
Ajit Khaparde84517482009-09-04 03:12:16 +00002433 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002434 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002435 return 0;
2436}
2437
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002438static int get_ufigen_type(struct flash_file_hdr_g2 *fhdr)
2439{
2440 if (fhdr == NULL)
2441 return 0;
2442 if (fhdr->build[0] == '3')
2443 return BE_GEN3;
2444 else if (fhdr->build[0] == '2')
2445 return BE_GEN2;
2446 else
2447 return 0;
2448}
2449
Ajit Khaparde84517482009-09-04 03:12:16 +00002450int be_load_fw(struct be_adapter *adapter, u8 *func)
2451{
2452 char fw_file[ETHTOOL_FLASH_MAX_FILENAME];
2453 const struct firmware *fw;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002454 struct flash_file_hdr_g2 *fhdr;
2455 struct flash_file_hdr_g3 *fhdr3;
2456 struct image_hdr *img_hdr_ptr = NULL;
Ajit Khaparde84517482009-09-04 03:12:16 +00002457 struct be_dma_mem flash_cmd;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002458 int status, i = 0, num_imgs = 0;
Ajit Khaparde84517482009-09-04 03:12:16 +00002459 const u8 *p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002460
Sarveshwar Bandid9efd2a2010-11-18 23:44:45 +00002461 if (!netif_running(adapter->netdev)) {
2462 dev_err(&adapter->pdev->dev,
2463 "Firmware load not allowed (interface is down)\n");
2464 return -EPERM;
2465 }
2466
Ajit Khaparde84517482009-09-04 03:12:16 +00002467 strcpy(fw_file, func);
2468
2469 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
2470 if (status)
2471 goto fw_exit;
2472
2473 p = fw->data;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002474 fhdr = (struct flash_file_hdr_g2 *) p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002475 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
2476
Ajit Khaparde84517482009-09-04 03:12:16 +00002477 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
2478 flash_cmd.va = pci_alloc_consistent(adapter->pdev, flash_cmd.size,
2479 &flash_cmd.dma);
2480 if (!flash_cmd.va) {
2481 status = -ENOMEM;
2482 dev_err(&adapter->pdev->dev,
2483 "Memory allocation failure while flashing\n");
2484 goto fw_exit;
2485 }
2486
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002487 if ((adapter->generation == BE_GEN3) &&
2488 (get_ufigen_type(fhdr) == BE_GEN3)) {
2489 fhdr3 = (struct flash_file_hdr_g3 *) fw->data;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002490 num_imgs = le32_to_cpu(fhdr3->num_imgs);
2491 for (i = 0; i < num_imgs; i++) {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002492 img_hdr_ptr = (struct image_hdr *) (fw->data +
2493 (sizeof(struct flash_file_hdr_g3) +
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002494 i * sizeof(struct image_hdr)));
2495 if (le32_to_cpu(img_hdr_ptr->imageid) == 1)
2496 status = be_flash_data(adapter, fw, &flash_cmd,
2497 num_imgs);
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002498 }
2499 } else if ((adapter->generation == BE_GEN2) &&
2500 (get_ufigen_type(fhdr) == BE_GEN2)) {
2501 status = be_flash_data(adapter, fw, &flash_cmd, 0);
2502 } else {
2503 dev_err(&adapter->pdev->dev,
2504 "UFI and Interface are not compatible for flashing\n");
2505 status = -1;
Ajit Khaparde84517482009-09-04 03:12:16 +00002506 }
2507
2508 pci_free_consistent(adapter->pdev, flash_cmd.size, flash_cmd.va,
2509 flash_cmd.dma);
2510 if (status) {
2511 dev_err(&adapter->pdev->dev, "Firmware load error\n");
2512 goto fw_exit;
2513 }
2514
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002515 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
Ajit Khaparde84517482009-09-04 03:12:16 +00002516
2517fw_exit:
2518 release_firmware(fw);
2519 return status;
2520}
2521
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002522static struct net_device_ops be_netdev_ops = {
2523 .ndo_open = be_open,
2524 .ndo_stop = be_close,
2525 .ndo_start_xmit = be_xmit,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002526 .ndo_set_rx_mode = be_set_multicast_list,
2527 .ndo_set_mac_address = be_mac_addr_set,
2528 .ndo_change_mtu = be_change_mtu,
2529 .ndo_validate_addr = eth_validate_addr,
2530 .ndo_vlan_rx_register = be_vlan_register,
2531 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
2532 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002533 .ndo_set_vf_mac = be_set_vf_mac,
Ajit Khaparde1da87b72010-07-23 01:51:22 +00002534 .ndo_set_vf_vlan = be_set_vf_vlan,
Ajit Khapardee1d18732010-07-23 01:52:13 +00002535 .ndo_set_vf_tx_rate = be_set_vf_tx_rate,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002536 .ndo_get_vf_config = be_get_vf_config
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002537};
2538
2539static void be_netdev_init(struct net_device *netdev)
2540{
2541 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3abcded2010-10-03 22:12:27 -07002542 struct be_rx_obj *rxo;
2543 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002544
2545 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
Ajit Khaparde583e3f32009-10-05 02:22:19 +00002546 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_HW_CSUM |
Ajit Khaparde49e4b8472010-06-14 04:56:07 +00002547 NETIF_F_GRO | NETIF_F_TSO6;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002548
Ajit Khaparde51c59872009-11-29 17:54:54 +00002549 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2550
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002551 netdev->flags |= IFF_MULTICAST;
2552
Ajit Khaparde728a9972009-04-13 15:41:22 -07002553 adapter->rx_csum = true;
2554
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002555 /* Default settings for Rx and Tx flow control */
2556 adapter->rx_fc = true;
2557 adapter->tx_fc = true;
2558
Ajit Khapardec190e3c2009-09-04 03:12:29 +00002559 netif_set_gso_max_size(netdev, 65535);
2560
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002561 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
2562
2563 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
2564
Sathya Perla3abcded2010-10-03 22:12:27 -07002565 for_all_rx_queues(adapter, rxo, i)
2566 netif_napi_add(netdev, &rxo->rx_eq.napi, be_poll_rx,
2567 BE_NAPI_WEIGHT);
2568
Sathya Perla5fb379e2009-06-18 00:02:59 +00002569 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002570 BE_NAPI_WEIGHT);
2571
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002572 netif_stop_queue(netdev);
2573}
2574
2575static void be_unmap_pci_bars(struct be_adapter *adapter)
2576{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002577 if (adapter->csr)
2578 iounmap(adapter->csr);
2579 if (adapter->db)
2580 iounmap(adapter->db);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002581 if (adapter->pcicfg && be_physfn(adapter))
Sathya Perla8788fdc2009-07-27 22:52:03 +00002582 iounmap(adapter->pcicfg);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002583}
2584
2585static int be_map_pci_bars(struct be_adapter *adapter)
2586{
2587 u8 __iomem *addr;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002588 int pcicfg_reg, db_reg;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002589
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002590 if (be_physfn(adapter)) {
2591 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
2592 pci_resource_len(adapter->pdev, 2));
2593 if (addr == NULL)
2594 return -ENOMEM;
2595 adapter->csr = addr;
2596 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002597
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002598 if (adapter->generation == BE_GEN2) {
2599 pcicfg_reg = 1;
2600 db_reg = 4;
2601 } else {
2602 pcicfg_reg = 0;
2603 if (be_physfn(adapter))
2604 db_reg = 4;
2605 else
2606 db_reg = 0;
2607 }
2608 addr = ioremap_nocache(pci_resource_start(adapter->pdev, db_reg),
2609 pci_resource_len(adapter->pdev, db_reg));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002610 if (addr == NULL)
2611 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00002612 adapter->db = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002613
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002614 if (be_physfn(adapter)) {
2615 addr = ioremap_nocache(
2616 pci_resource_start(adapter->pdev, pcicfg_reg),
2617 pci_resource_len(adapter->pdev, pcicfg_reg));
2618 if (addr == NULL)
2619 goto pci_map_err;
2620 adapter->pcicfg = addr;
2621 } else
2622 adapter->pcicfg = adapter->db + SRIOV_VF_PCICFG_OFFSET;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002623
2624 return 0;
2625pci_map_err:
2626 be_unmap_pci_bars(adapter);
2627 return -ENOMEM;
2628}
2629
2630
2631static void be_ctrl_cleanup(struct be_adapter *adapter)
2632{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002633 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002634
2635 be_unmap_pci_bars(adapter);
2636
2637 if (mem->va)
2638 pci_free_consistent(adapter->pdev, mem->size,
2639 mem->va, mem->dma);
Sathya Perlae7b909a2009-11-22 22:01:10 +00002640
2641 mem = &adapter->mc_cmd_mem;
2642 if (mem->va)
2643 pci_free_consistent(adapter->pdev, mem->size,
2644 mem->va, mem->dma);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002645}
2646
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002647static int be_ctrl_init(struct be_adapter *adapter)
2648{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002649 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
2650 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002651 struct be_dma_mem *mc_cmd_mem = &adapter->mc_cmd_mem;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002652 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002653
2654 status = be_map_pci_bars(adapter);
2655 if (status)
Sathya Perlae7b909a2009-11-22 22:01:10 +00002656 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002657
2658 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
2659 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
2660 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
2661 if (!mbox_mem_alloc->va) {
Sathya Perlae7b909a2009-11-22 22:01:10 +00002662 status = -ENOMEM;
2663 goto unmap_pci_bars;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002664 }
Sathya Perlae7b909a2009-11-22 22:01:10 +00002665
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002666 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
2667 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
2668 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
2669 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Sathya Perlae7b909a2009-11-22 22:01:10 +00002670
2671 mc_cmd_mem->size = sizeof(struct be_cmd_req_mcast_mac_config);
2672 mc_cmd_mem->va = pci_alloc_consistent(adapter->pdev, mc_cmd_mem->size,
2673 &mc_cmd_mem->dma);
2674 if (mc_cmd_mem->va == NULL) {
2675 status = -ENOMEM;
2676 goto free_mbox;
2677 }
2678 memset(mc_cmd_mem->va, 0, mc_cmd_mem->size);
2679
Sathya Perla8788fdc2009-07-27 22:52:03 +00002680 spin_lock_init(&adapter->mbox_lock);
2681 spin_lock_init(&adapter->mcc_lock);
2682 spin_lock_init(&adapter->mcc_cq_lock);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002683
Sarveshwar Bandidd131e72010-05-25 16:16:32 -07002684 init_completion(&adapter->flash_compl);
Sathya Perlacf588472010-02-14 21:22:01 +00002685 pci_save_state(adapter->pdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002686 return 0;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002687
2688free_mbox:
2689 pci_free_consistent(adapter->pdev, mbox_mem_alloc->size,
2690 mbox_mem_alloc->va, mbox_mem_alloc->dma);
2691
2692unmap_pci_bars:
2693 be_unmap_pci_bars(adapter);
2694
2695done:
2696 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002697}
2698
2699static void be_stats_cleanup(struct be_adapter *adapter)
2700{
Sathya Perla3abcded2010-10-03 22:12:27 -07002701 struct be_dma_mem *cmd = &adapter->stats_cmd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002702
2703 if (cmd->va)
2704 pci_free_consistent(adapter->pdev, cmd->size,
2705 cmd->va, cmd->dma);
2706}
2707
2708static int be_stats_init(struct be_adapter *adapter)
2709{
Sathya Perla3abcded2010-10-03 22:12:27 -07002710 struct be_dma_mem *cmd = &adapter->stats_cmd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002711
2712 cmd->size = sizeof(struct be_cmd_req_get_stats);
2713 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
2714 if (cmd->va == NULL)
2715 return -1;
David S. Millerd291b9a2010-01-28 21:36:21 -08002716 memset(cmd->va, 0, cmd->size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002717 return 0;
2718}
2719
2720static void __devexit be_remove(struct pci_dev *pdev)
2721{
2722 struct be_adapter *adapter = pci_get_drvdata(pdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002723
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002724 if (!adapter)
2725 return;
2726
Somnath Koturf203af72010-10-25 23:01:03 +00002727 cancel_delayed_work_sync(&adapter->work);
2728
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002729 unregister_netdev(adapter->netdev);
2730
Sathya Perla5fb379e2009-06-18 00:02:59 +00002731 be_clear(adapter);
2732
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002733 be_stats_cleanup(adapter);
2734
2735 be_ctrl_cleanup(adapter);
2736
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002737 be_sriov_disable(adapter);
2738
Sathya Perla8d56ff12009-11-22 22:02:26 +00002739 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002740
2741 pci_set_drvdata(pdev, NULL);
2742 pci_release_regions(pdev);
2743 pci_disable_device(pdev);
2744
2745 free_netdev(adapter->netdev);
2746}
2747
Sathya Perla2243e2e2009-11-22 22:02:03 +00002748static int be_get_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002749{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002750 int status;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002751 u8 mac[ETH_ALEN];
Sathya Perla43a04fdc2009-10-14 20:21:17 +00002752
Sathya Perla8788fdc2009-07-27 22:52:03 +00002753 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002754 if (status)
2755 return status;
2756
Sathya Perla3abcded2010-10-03 22:12:27 -07002757 status = be_cmd_query_fw_cfg(adapter, &adapter->port_num,
2758 &adapter->function_mode, &adapter->function_caps);
Sathya Perla2243e2e2009-11-22 22:02:03 +00002759 if (status)
2760 return status;
2761
2762 memset(mac, 0, ETH_ALEN);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002763
2764 if (be_physfn(adapter)) {
2765 status = be_cmd_mac_addr_query(adapter, mac,
Sathya Perla2243e2e2009-11-22 22:02:03 +00002766 MAC_ADDRESS_TYPE_NETWORK, true /*permanent */, 0);
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002767
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002768 if (status)
2769 return status;
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002770
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002771 if (!is_valid_ether_addr(mac))
2772 return -EADDRNOTAVAIL;
2773
2774 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2775 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2776 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002777
Ajit Khaparde3486be22010-07-23 02:04:54 +00002778 if (adapter->function_mode & 0x400)
Ajit Khaparde82903e42010-02-09 01:34:57 +00002779 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED/4;
2780 else
2781 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED;
2782
Sathya Perla2243e2e2009-11-22 22:02:03 +00002783 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002784}
2785
2786static int __devinit be_probe(struct pci_dev *pdev,
2787 const struct pci_device_id *pdev_id)
2788{
2789 int status = 0;
2790 struct be_adapter *adapter;
2791 struct net_device *netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002792
2793 status = pci_enable_device(pdev);
2794 if (status)
2795 goto do_none;
2796
2797 status = pci_request_regions(pdev, DRV_NAME);
2798 if (status)
2799 goto disable_dev;
2800 pci_set_master(pdev);
2801
2802 netdev = alloc_etherdev(sizeof(struct be_adapter));
2803 if (netdev == NULL) {
2804 status = -ENOMEM;
2805 goto rel_reg;
2806 }
2807 adapter = netdev_priv(netdev);
Ajit Khaparde7b139c82010-01-27 21:56:44 +00002808
2809 switch (pdev->device) {
2810 case BE_DEVICE_ID1:
2811 case OC_DEVICE_ID1:
2812 adapter->generation = BE_GEN2;
2813 break;
2814 case BE_DEVICE_ID2:
2815 case OC_DEVICE_ID2:
2816 adapter->generation = BE_GEN3;
2817 break;
2818 default:
2819 adapter->generation = 0;
2820 }
2821
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002822 adapter->pdev = pdev;
2823 pci_set_drvdata(pdev, adapter);
2824 adapter->netdev = netdev;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002825 SET_NETDEV_DEV(netdev, &pdev->dev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002826
Yang Hongyange9304382009-04-13 14:40:14 -07002827 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002828 if (!status) {
2829 netdev->features |= NETIF_F_HIGHDMA;
2830 } else {
Yang Hongyange9304382009-04-13 14:40:14 -07002831 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002832 if (status) {
2833 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
2834 goto free_netdev;
2835 }
2836 }
2837
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002838 be_sriov_enable(adapter);
2839
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002840 status = be_ctrl_init(adapter);
2841 if (status)
2842 goto free_netdev;
2843
Sathya Perla2243e2e2009-11-22 22:02:03 +00002844 /* sync up with fw's ready state */
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002845 if (be_physfn(adapter)) {
2846 status = be_cmd_POST(adapter);
2847 if (status)
2848 goto ctrl_clean;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002849 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002850
2851 /* tell fw we're ready to fire cmds */
2852 status = be_cmd_fw_init(adapter);
2853 if (status)
2854 goto ctrl_clean;
2855
Sarveshwar Bandi556ae192010-05-24 18:38:25 -07002856 if (be_physfn(adapter)) {
2857 status = be_cmd_reset_function(adapter);
2858 if (status)
2859 goto ctrl_clean;
2860 }
2861
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002862 status = be_stats_init(adapter);
2863 if (status)
2864 goto ctrl_clean;
2865
Sathya Perla2243e2e2009-11-22 22:02:03 +00002866 status = be_get_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002867 if (status)
2868 goto stats_clean;
2869
Sathya Perla3abcded2010-10-03 22:12:27 -07002870 be_msix_enable(adapter);
2871
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002872 INIT_DELAYED_WORK(&adapter->work, be_worker);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002873
Sathya Perla5fb379e2009-06-18 00:02:59 +00002874 status = be_setup(adapter);
2875 if (status)
Sathya Perla3abcded2010-10-03 22:12:27 -07002876 goto msix_disable;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002877
Sathya Perla3abcded2010-10-03 22:12:27 -07002878 be_netdev_init(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002879 status = register_netdev(netdev);
2880 if (status != 0)
Sathya Perla5fb379e2009-06-18 00:02:59 +00002881 goto unsetup;
Somnath Kotur63a76942010-10-25 01:11:10 +00002882 netif_carrier_off(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002883
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002884 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
Somnath Koturf203af72010-10-25 23:01:03 +00002885 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002886 return 0;
2887
Sathya Perla5fb379e2009-06-18 00:02:59 +00002888unsetup:
2889 be_clear(adapter);
Sathya Perla3abcded2010-10-03 22:12:27 -07002890msix_disable:
2891 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002892stats_clean:
2893 be_stats_cleanup(adapter);
2894ctrl_clean:
2895 be_ctrl_cleanup(adapter);
2896free_netdev:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002897 be_sriov_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002898 free_netdev(adapter->netdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002899 pci_set_drvdata(pdev, NULL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002900rel_reg:
2901 pci_release_regions(pdev);
2902disable_dev:
2903 pci_disable_device(pdev);
2904do_none:
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002905 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002906 return status;
2907}
2908
2909static int be_suspend(struct pci_dev *pdev, pm_message_t state)
2910{
2911 struct be_adapter *adapter = pci_get_drvdata(pdev);
2912 struct net_device *netdev = adapter->netdev;
2913
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002914 if (adapter->wol)
2915 be_setup_wol(adapter, true);
2916
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002917 netif_device_detach(netdev);
2918 if (netif_running(netdev)) {
2919 rtnl_lock();
2920 be_close(netdev);
2921 rtnl_unlock();
2922 }
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002923 be_cmd_get_flow_control(adapter, &adapter->tx_fc, &adapter->rx_fc);
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002924 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002925
2926 pci_save_state(pdev);
2927 pci_disable_device(pdev);
2928 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2929 return 0;
2930}
2931
2932static int be_resume(struct pci_dev *pdev)
2933{
2934 int status = 0;
2935 struct be_adapter *adapter = pci_get_drvdata(pdev);
2936 struct net_device *netdev = adapter->netdev;
2937
2938 netif_device_detach(netdev);
2939
2940 status = pci_enable_device(pdev);
2941 if (status)
2942 return status;
2943
2944 pci_set_power_state(pdev, 0);
2945 pci_restore_state(pdev);
2946
Sathya Perla2243e2e2009-11-22 22:02:03 +00002947 /* tell fw we're ready to fire cmds */
2948 status = be_cmd_fw_init(adapter);
2949 if (status)
2950 return status;
2951
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002952 be_setup(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002953 if (netif_running(netdev)) {
2954 rtnl_lock();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002955 be_open(netdev);
2956 rtnl_unlock();
2957 }
2958 netif_device_attach(netdev);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002959
2960 if (adapter->wol)
2961 be_setup_wol(adapter, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002962 return 0;
2963}
2964
Sathya Perla82456b02010-02-17 01:35:37 +00002965/*
2966 * An FLR will stop BE from DMAing any data.
2967 */
2968static void be_shutdown(struct pci_dev *pdev)
2969{
2970 struct be_adapter *adapter = pci_get_drvdata(pdev);
2971 struct net_device *netdev = adapter->netdev;
2972
2973 netif_device_detach(netdev);
2974
2975 be_cmd_reset_function(adapter);
2976
2977 if (adapter->wol)
2978 be_setup_wol(adapter, true);
2979
2980 pci_disable_device(pdev);
Sathya Perla82456b02010-02-17 01:35:37 +00002981}
2982
Sathya Perlacf588472010-02-14 21:22:01 +00002983static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
2984 pci_channel_state_t state)
2985{
2986 struct be_adapter *adapter = pci_get_drvdata(pdev);
2987 struct net_device *netdev = adapter->netdev;
2988
2989 dev_err(&adapter->pdev->dev, "EEH error detected\n");
2990
2991 adapter->eeh_err = true;
2992
2993 netif_device_detach(netdev);
2994
2995 if (netif_running(netdev)) {
2996 rtnl_lock();
2997 be_close(netdev);
2998 rtnl_unlock();
2999 }
3000 be_clear(adapter);
3001
3002 if (state == pci_channel_io_perm_failure)
3003 return PCI_ERS_RESULT_DISCONNECT;
3004
3005 pci_disable_device(pdev);
3006
3007 return PCI_ERS_RESULT_NEED_RESET;
3008}
3009
3010static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
3011{
3012 struct be_adapter *adapter = pci_get_drvdata(pdev);
3013 int status;
3014
3015 dev_info(&adapter->pdev->dev, "EEH reset\n");
3016 adapter->eeh_err = false;
3017
3018 status = pci_enable_device(pdev);
3019 if (status)
3020 return PCI_ERS_RESULT_DISCONNECT;
3021
3022 pci_set_master(pdev);
3023 pci_set_power_state(pdev, 0);
3024 pci_restore_state(pdev);
3025
3026 /* Check if card is ok and fw is ready */
3027 status = be_cmd_POST(adapter);
3028 if (status)
3029 return PCI_ERS_RESULT_DISCONNECT;
3030
3031 return PCI_ERS_RESULT_RECOVERED;
3032}
3033
3034static void be_eeh_resume(struct pci_dev *pdev)
3035{
3036 int status = 0;
3037 struct be_adapter *adapter = pci_get_drvdata(pdev);
3038 struct net_device *netdev = adapter->netdev;
3039
3040 dev_info(&adapter->pdev->dev, "EEH resume\n");
3041
3042 pci_save_state(pdev);
3043
3044 /* tell fw we're ready to fire cmds */
3045 status = be_cmd_fw_init(adapter);
3046 if (status)
3047 goto err;
3048
3049 status = be_setup(adapter);
3050 if (status)
3051 goto err;
3052
3053 if (netif_running(netdev)) {
3054 status = be_open(netdev);
3055 if (status)
3056 goto err;
3057 }
3058 netif_device_attach(netdev);
3059 return;
3060err:
3061 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
Sathya Perlacf588472010-02-14 21:22:01 +00003062}
3063
3064static struct pci_error_handlers be_eeh_handlers = {
3065 .error_detected = be_eeh_err_detected,
3066 .slot_reset = be_eeh_reset,
3067 .resume = be_eeh_resume,
3068};
3069
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003070static struct pci_driver be_driver = {
3071 .name = DRV_NAME,
3072 .id_table = be_dev_ids,
3073 .probe = be_probe,
3074 .remove = be_remove,
3075 .suspend = be_suspend,
Sathya Perlacf588472010-02-14 21:22:01 +00003076 .resume = be_resume,
Sathya Perla82456b02010-02-17 01:35:37 +00003077 .shutdown = be_shutdown,
Sathya Perlacf588472010-02-14 21:22:01 +00003078 .err_handler = &be_eeh_handlers
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003079};
3080
3081static int __init be_init_module(void)
3082{
Joe Perches8e95a202009-12-03 07:58:21 +00003083 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
3084 rx_frag_size != 2048) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003085 printk(KERN_WARNING DRV_NAME
3086 " : Module param rx_frag_size must be 2048/4096/8192."
3087 " Using 2048\n");
3088 rx_frag_size = 2048;
3089 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003090
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003091 if (num_vfs > 32) {
3092 printk(KERN_WARNING DRV_NAME
3093 " : Module param num_vfs must not be greater than 32."
3094 "Using 32\n");
3095 num_vfs = 32;
3096 }
3097
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003098 return pci_register_driver(&be_driver);
3099}
3100module_init(be_init_module);
3101
3102static void __exit be_exit_module(void)
3103{
3104 pci_unregister_driver(&be_driver);
3105}
3106module_exit(be_exit_module);