blob: 6b80a06eb3a7e6acd919f31f20d5cbd51ffee31d [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
Ajit Khaparde0fc48c32010-07-29 06:18:58 +00001805 if (!adapter->stats_ioctl_sent)
Sathya Perla3abcded2010-10-03 22:12:27 -07001806 be_cmd_get_stats(adapter, &adapter->stats_cmd);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001807
Sathya Perla4097f662009-03-24 16:40:13 -07001808 be_tx_rate_update(adapter);
Sathya Perla4097f662009-03-24 16:40:13 -07001809
Sathya Perla3abcded2010-10-03 22:12:27 -07001810 for_all_rx_queues(adapter, rxo, i) {
1811 be_rx_rate_update(rxo);
1812 be_rx_eqd_update(adapter, rxo);
1813
1814 if (rxo->rx_post_starved) {
1815 rxo->rx_post_starved = false;
1816 be_post_rx_frags(rxo);
1817 }
Sathya Perlaea1dae12009-03-19 23:56:20 -07001818 }
Sathya Perla3abcded2010-10-03 22:12:27 -07001819
Ajit Khaparded053de92010-09-03 06:23:30 +00001820 if (!adapter->ue_detected)
1821 be_detect_dump_ue(adapter);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001822
1823 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1824}
1825
Sathya Perla8d56ff12009-11-22 22:02:26 +00001826static void be_msix_disable(struct be_adapter *adapter)
1827{
1828 if (adapter->msix_enabled) {
1829 pci_disable_msix(adapter->pdev);
1830 adapter->msix_enabled = false;
1831 }
1832}
1833
Sathya Perla3abcded2010-10-03 22:12:27 -07001834static int be_num_rxqs_get(struct be_adapter *adapter)
1835{
1836 if (multi_rxq && (adapter->function_caps & BE_FUNCTION_CAPS_RSS) &&
1837 !adapter->sriov_enabled && !(adapter->function_mode & 0x400)) {
1838 return 1 + MAX_RSS_QS; /* one default non-RSS queue */
1839 } else {
1840 dev_warn(&adapter->pdev->dev,
1841 "No support for multiple RX queues\n");
1842 return 1;
1843 }
1844}
1845
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001846static void be_msix_enable(struct be_adapter *adapter)
1847{
Sathya Perla3abcded2010-10-03 22:12:27 -07001848#define BE_MIN_MSIX_VECTORS (1 + 1) /* Rx + Tx */
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001849 int i, status;
1850
Sathya Perla3abcded2010-10-03 22:12:27 -07001851 adapter->num_rx_qs = be_num_rxqs_get(adapter);
1852
1853 for (i = 0; i < (adapter->num_rx_qs + 1); i++)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001854 adapter->msix_entries[i].entry = i;
1855
1856 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
Sathya Perla3abcded2010-10-03 22:12:27 -07001857 adapter->num_rx_qs + 1);
1858 if (status == 0) {
1859 goto done;
1860 } else if (status >= BE_MIN_MSIX_VECTORS) {
1861 if (pci_enable_msix(adapter->pdev, adapter->msix_entries,
1862 status) == 0) {
1863 adapter->num_rx_qs = status - 1;
1864 dev_warn(&adapter->pdev->dev,
1865 "Could alloc only %d MSIx vectors. "
1866 "Using %d RX Qs\n", status, adapter->num_rx_qs);
1867 goto done;
1868 }
1869 }
1870 return;
1871done:
1872 adapter->msix_enabled = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001873}
1874
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001875static void be_sriov_enable(struct be_adapter *adapter)
1876{
Sarveshwar Bandi344dbf12010-07-09 01:43:55 +00001877 be_check_sriov_fn_type(adapter);
Ajit Khaparde6dedec82010-07-29 06:15:32 +00001878#ifdef CONFIG_PCI_IOV
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001879 if (be_physfn(adapter) && num_vfs) {
Ajit Khaparde6dedec82010-07-29 06:15:32 +00001880 int status;
1881
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001882 status = pci_enable_sriov(adapter->pdev, num_vfs);
1883 adapter->sriov_enabled = status ? false : true;
1884 }
1885#endif
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001886}
1887
1888static void be_sriov_disable(struct be_adapter *adapter)
1889{
1890#ifdef CONFIG_PCI_IOV
1891 if (adapter->sriov_enabled) {
1892 pci_disable_sriov(adapter->pdev);
1893 adapter->sriov_enabled = false;
1894 }
1895#endif
1896}
1897
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001898static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1899{
Sathya Perlab628bde2009-08-17 00:58:26 +00001900 return adapter->msix_entries[
1901 be_evt_bit_get(adapter, eq_id)].vector;
1902}
1903
1904static int be_request_irq(struct be_adapter *adapter,
1905 struct be_eq_obj *eq_obj,
Sathya Perla3abcded2010-10-03 22:12:27 -07001906 void *handler, char *desc, void *context)
Sathya Perlab628bde2009-08-17 00:58:26 +00001907{
1908 struct net_device *netdev = adapter->netdev;
1909 int vec;
1910
1911 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
1912 vec = be_msix_vec_get(adapter, eq_obj->q.id);
Sathya Perla3abcded2010-10-03 22:12:27 -07001913 return request_irq(vec, handler, 0, eq_obj->desc, context);
Sathya Perlab628bde2009-08-17 00:58:26 +00001914}
1915
Sathya Perla3abcded2010-10-03 22:12:27 -07001916static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj,
1917 void *context)
Sathya Perlab628bde2009-08-17 00:58:26 +00001918{
1919 int vec = be_msix_vec_get(adapter, eq_obj->q.id);
Sathya Perla3abcded2010-10-03 22:12:27 -07001920 free_irq(vec, context);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001921}
1922
1923static int be_msix_register(struct be_adapter *adapter)
1924{
Sathya Perla3abcded2010-10-03 22:12:27 -07001925 struct be_rx_obj *rxo;
1926 int status, i;
1927 char qname[10];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001928
Sathya Perla3abcded2010-10-03 22:12:27 -07001929 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx",
1930 adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001931 if (status)
1932 goto err;
1933
Sathya Perla3abcded2010-10-03 22:12:27 -07001934 for_all_rx_queues(adapter, rxo, i) {
1935 sprintf(qname, "rxq%d", i);
1936 status = be_request_irq(adapter, &rxo->rx_eq, be_msix_rx,
1937 qname, rxo);
1938 if (status)
1939 goto err_msix;
1940 }
Sathya Perlab628bde2009-08-17 00:58:26 +00001941
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001942 return 0;
Sathya Perlab628bde2009-08-17 00:58:26 +00001943
Sathya Perla3abcded2010-10-03 22:12:27 -07001944err_msix:
1945 be_free_irq(adapter, &adapter->tx_eq, adapter);
1946
1947 for (i--, rxo = &adapter->rx_obj[i]; i >= 0; i--, rxo--)
1948 be_free_irq(adapter, &rxo->rx_eq, rxo);
1949
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001950err:
1951 dev_warn(&adapter->pdev->dev,
1952 "MSIX Request IRQ failed - err %d\n", status);
1953 pci_disable_msix(adapter->pdev);
1954 adapter->msix_enabled = false;
1955 return status;
1956}
1957
1958static int be_irq_register(struct be_adapter *adapter)
1959{
1960 struct net_device *netdev = adapter->netdev;
1961 int status;
1962
1963 if (adapter->msix_enabled) {
1964 status = be_msix_register(adapter);
1965 if (status == 0)
1966 goto done;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001967 /* INTx is not supported for VF */
1968 if (!be_physfn(adapter))
1969 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001970 }
1971
1972 /* INTx */
1973 netdev->irq = adapter->pdev->irq;
1974 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1975 adapter);
1976 if (status) {
1977 dev_err(&adapter->pdev->dev,
1978 "INTx request IRQ failed - err %d\n", status);
1979 return status;
1980 }
1981done:
1982 adapter->isr_registered = true;
1983 return 0;
1984}
1985
1986static void be_irq_unregister(struct be_adapter *adapter)
1987{
1988 struct net_device *netdev = adapter->netdev;
Sathya Perla3abcded2010-10-03 22:12:27 -07001989 struct be_rx_obj *rxo;
1990 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001991
1992 if (!adapter->isr_registered)
1993 return;
1994
1995 /* INTx */
1996 if (!adapter->msix_enabled) {
1997 free_irq(netdev->irq, adapter);
1998 goto done;
1999 }
2000
2001 /* MSIx */
Sathya Perla3abcded2010-10-03 22:12:27 -07002002 be_free_irq(adapter, &adapter->tx_eq, adapter);
2003
2004 for_all_rx_queues(adapter, rxo, i)
2005 be_free_irq(adapter, &rxo->rx_eq, rxo);
2006
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002007done:
2008 adapter->isr_registered = false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002009}
2010
Sathya Perla889cd4b2010-05-30 23:33:45 +00002011static int be_close(struct net_device *netdev)
2012{
2013 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3abcded2010-10-03 22:12:27 -07002014 struct be_rx_obj *rxo;
Sathya Perla889cd4b2010-05-30 23:33:45 +00002015 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla3abcded2010-10-03 22:12:27 -07002016 int vec, i;
Sathya Perla889cd4b2010-05-30 23:33:45 +00002017
2018 cancel_delayed_work_sync(&adapter->work);
2019
2020 be_async_mcc_disable(adapter);
2021
2022 netif_stop_queue(netdev);
2023 netif_carrier_off(netdev);
2024 adapter->link_up = false;
2025
2026 be_intr_set(adapter, false);
2027
2028 if (adapter->msix_enabled) {
2029 vec = be_msix_vec_get(adapter, tx_eq->q.id);
2030 synchronize_irq(vec);
Sathya Perla3abcded2010-10-03 22:12:27 -07002031
2032 for_all_rx_queues(adapter, rxo, i) {
2033 vec = be_msix_vec_get(adapter, rxo->rx_eq.q.id);
2034 synchronize_irq(vec);
2035 }
Sathya Perla889cd4b2010-05-30 23:33:45 +00002036 } else {
2037 synchronize_irq(netdev->irq);
2038 }
2039 be_irq_unregister(adapter);
2040
Sathya Perla3abcded2010-10-03 22:12:27 -07002041 for_all_rx_queues(adapter, rxo, i)
2042 napi_disable(&rxo->rx_eq.napi);
2043
Sathya Perla889cd4b2010-05-30 23:33:45 +00002044 napi_disable(&tx_eq->napi);
2045
2046 /* Wait for all pending tx completions to arrive so that
2047 * all tx skbs are freed.
2048 */
2049 be_tx_compl_clean(adapter);
2050
2051 return 0;
2052}
2053
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002054static int be_open(struct net_device *netdev)
2055{
2056 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002057 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla3abcded2010-10-03 22:12:27 -07002058 struct be_rx_obj *rxo;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002059 bool link_up;
Sathya Perla3abcded2010-10-03 22:12:27 -07002060 int status, i;
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07002061 u8 mac_speed;
2062 u16 link_speed;
Sathya Perla5fb379e2009-06-18 00:02:59 +00002063
Sathya Perla3abcded2010-10-03 22:12:27 -07002064 for_all_rx_queues(adapter, rxo, i) {
2065 be_post_rx_frags(rxo);
2066 napi_enable(&rxo->rx_eq.napi);
2067 }
Sathya Perla5fb379e2009-06-18 00:02:59 +00002068 napi_enable(&tx_eq->napi);
2069
2070 be_irq_register(adapter);
2071
Sathya Perla8788fdc2009-07-27 22:52:03 +00002072 be_intr_set(adapter, true);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002073
2074 /* The evt queues are created in unarmed state; arm them */
Sathya Perla3abcded2010-10-03 22:12:27 -07002075 for_all_rx_queues(adapter, rxo, i) {
2076 be_eq_notify(adapter, rxo->rx_eq.q.id, true, false, 0);
2077 be_cq_notify(adapter, rxo->cq.id, true, 0);
2078 }
Sathya Perla8788fdc2009-07-27 22:52:03 +00002079 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002080
Sathya Perla7a1e9b22010-02-17 01:35:11 +00002081 /* Now that interrupts are on we can process async mcc */
2082 be_async_mcc_enable(adapter);
2083
Sathya Perla889cd4b2010-05-30 23:33:45 +00002084 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
2085
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07002086 status = be_cmd_link_status_query(adapter, &link_up, &mac_speed,
2087 &link_speed);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002088 if (status)
Sathya Perla889cd4b2010-05-30 23:33:45 +00002089 goto err;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002090 be_link_status_update(adapter, link_up);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002091
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002092 if (be_physfn(adapter)) {
Ajit Khaparde1da87b72010-07-23 01:51:22 +00002093 status = be_vid_config(adapter, false, 0);
Sathya Perla889cd4b2010-05-30 23:33:45 +00002094 if (status)
2095 goto err;
2096
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002097 status = be_cmd_set_flow_control(adapter,
2098 adapter->tx_fc, adapter->rx_fc);
2099 if (status)
Sathya Perla889cd4b2010-05-30 23:33:45 +00002100 goto err;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002101 }
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00002102
Sathya Perla889cd4b2010-05-30 23:33:45 +00002103 return 0;
2104err:
2105 be_close(adapter->netdev);
2106 return -EIO;
Sathya Perla5fb379e2009-06-18 00:02:59 +00002107}
2108
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002109static int be_setup_wol(struct be_adapter *adapter, bool enable)
2110{
2111 struct be_dma_mem cmd;
2112 int status = 0;
2113 u8 mac[ETH_ALEN];
2114
2115 memset(mac, 0, ETH_ALEN);
2116
2117 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
2118 cmd.va = pci_alloc_consistent(adapter->pdev, cmd.size, &cmd.dma);
2119 if (cmd.va == NULL)
2120 return -1;
2121 memset(cmd.va, 0, cmd.size);
2122
2123 if (enable) {
2124 status = pci_write_config_dword(adapter->pdev,
2125 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
2126 if (status) {
2127 dev_err(&adapter->pdev->dev,
Frans Pop2381a552010-03-24 07:57:36 +00002128 "Could not enable Wake-on-lan\n");
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002129 pci_free_consistent(adapter->pdev, cmd.size, cmd.va,
2130 cmd.dma);
2131 return status;
2132 }
2133 status = be_cmd_enable_magic_wol(adapter,
2134 adapter->netdev->dev_addr, &cmd);
2135 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
2136 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
2137 } else {
2138 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
2139 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
2140 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
2141 }
2142
2143 pci_free_consistent(adapter->pdev, cmd.size, cmd.va, cmd.dma);
2144 return status;
2145}
2146
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002147/*
2148 * Generate a seed MAC address from the PF MAC Address using jhash.
2149 * MAC Address for VFs are assigned incrementally starting from the seed.
2150 * These addresses are programmed in the ASIC by the PF and the VF driver
2151 * queries for the MAC address during its probe.
2152 */
2153static inline int be_vf_eth_addr_config(struct be_adapter *adapter)
2154{
2155 u32 vf = 0;
Sathya Perla3abcded2010-10-03 22:12:27 -07002156 int status = 0;
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002157 u8 mac[ETH_ALEN];
2158
2159 be_vf_eth_addr_generate(adapter, mac);
2160
2161 for (vf = 0; vf < num_vfs; vf++) {
2162 status = be_cmd_pmac_add(adapter, mac,
2163 adapter->vf_cfg[vf].vf_if_handle,
2164 &adapter->vf_cfg[vf].vf_pmac_id);
2165 if (status)
2166 dev_err(&adapter->pdev->dev,
2167 "Mac address add failed for VF %d\n", vf);
2168 else
2169 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
2170
2171 mac[5] += 1;
2172 }
2173 return status;
2174}
2175
2176static inline void be_vf_eth_addr_rem(struct be_adapter *adapter)
2177{
2178 u32 vf;
2179
2180 for (vf = 0; vf < num_vfs; vf++) {
2181 if (adapter->vf_cfg[vf].vf_pmac_id != BE_INVALID_PMAC_ID)
2182 be_cmd_pmac_del(adapter,
2183 adapter->vf_cfg[vf].vf_if_handle,
2184 adapter->vf_cfg[vf].vf_pmac_id);
2185 }
2186}
2187
Sathya Perla5fb379e2009-06-18 00:02:59 +00002188static int be_setup(struct be_adapter *adapter)
2189{
Sathya Perla5fb379e2009-06-18 00:02:59 +00002190 struct net_device *netdev = adapter->netdev;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002191 u32 cap_flags, en_flags, vf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002192 int status;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002193 u8 mac[ETH_ALEN];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002194
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002195 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST;
2196
2197 if (be_physfn(adapter)) {
2198 cap_flags |= BE_IF_FLAGS_MCAST_PROMISCUOUS |
2199 BE_IF_FLAGS_PROMISCUOUS |
2200 BE_IF_FLAGS_PASS_L3L4_ERRORS;
2201 en_flags |= BE_IF_FLAGS_PASS_L3L4_ERRORS;
Sathya Perla3abcded2010-10-03 22:12:27 -07002202
2203 if (be_multi_rxq(adapter)) {
2204 cap_flags |= BE_IF_FLAGS_RSS;
2205 en_flags |= BE_IF_FLAGS_RSS;
2206 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002207 }
Sathya Perla73d540f2009-10-14 20:20:42 +00002208
2209 status = be_cmd_if_create(adapter, cap_flags, en_flags,
2210 netdev->dev_addr, false/* pmac_invalid */,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002211 &adapter->if_handle, &adapter->pmac_id, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002212 if (status != 0)
2213 goto do_none;
2214
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002215 if (be_physfn(adapter)) {
2216 while (vf < num_vfs) {
2217 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED
2218 | BE_IF_FLAGS_BROADCAST;
2219 status = be_cmd_if_create(adapter, cap_flags, en_flags,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002220 mac, true,
2221 &adapter->vf_cfg[vf].vf_if_handle,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002222 NULL, vf+1);
2223 if (status) {
2224 dev_err(&adapter->pdev->dev,
2225 "Interface Create failed for VF %d\n", vf);
2226 goto if_destroy;
2227 }
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002228 adapter->vf_cfg[vf].vf_pmac_id = BE_INVALID_PMAC_ID;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002229 vf++;
Sarveshwar Bandi84e5b9f2010-05-27 16:28:15 -07002230 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002231 } else if (!be_physfn(adapter)) {
2232 status = be_cmd_mac_addr_query(adapter, mac,
2233 MAC_ADDRESS_TYPE_NETWORK, false, adapter->if_handle);
2234 if (!status) {
2235 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2236 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2237 }
2238 }
2239
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002240 status = be_tx_queues_create(adapter);
2241 if (status != 0)
2242 goto if_destroy;
2243
2244 status = be_rx_queues_create(adapter);
2245 if (status != 0)
2246 goto tx_qs_destroy;
2247
Sathya Perla5fb379e2009-06-18 00:02:59 +00002248 status = be_mcc_queues_create(adapter);
2249 if (status != 0)
2250 goto rx_qs_destroy;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002251
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002252 if (be_physfn(adapter)) {
2253 status = be_vf_eth_addr_config(adapter);
2254 if (status)
2255 goto mcc_q_destroy;
2256 }
2257
Ajit Khaparde0dffc832009-11-29 17:57:46 +00002258 adapter->link_speed = -1;
2259
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002260 return 0;
2261
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002262mcc_q_destroy:
2263 if (be_physfn(adapter))
2264 be_vf_eth_addr_rem(adapter);
2265 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002266rx_qs_destroy:
2267 be_rx_queues_destroy(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002268tx_qs_destroy:
2269 be_tx_queues_destroy(adapter);
2270if_destroy:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002271 for (vf = 0; vf < num_vfs; vf++)
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002272 if (adapter->vf_cfg[vf].vf_if_handle)
2273 be_cmd_if_destroy(adapter,
2274 adapter->vf_cfg[vf].vf_if_handle);
Sathya Perla8788fdc2009-07-27 22:52:03 +00002275 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002276do_none:
2277 return status;
2278}
2279
Sathya Perla5fb379e2009-06-18 00:02:59 +00002280static int be_clear(struct be_adapter *adapter)
2281{
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002282 if (be_physfn(adapter))
2283 be_vf_eth_addr_rem(adapter);
2284
Sathya Perla1a8887d2009-08-17 00:58:41 +00002285 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002286 be_rx_queues_destroy(adapter);
2287 be_tx_queues_destroy(adapter);
2288
Sathya Perla8788fdc2009-07-27 22:52:03 +00002289 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002290
Sathya Perla2243e2e2009-11-22 22:02:03 +00002291 /* tell fw we're done with firing cmds */
2292 be_cmd_fw_clean(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002293 return 0;
2294}
2295
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002296
Ajit Khaparde84517482009-09-04 03:12:16 +00002297#define FW_FILE_HDR_SIGN "ServerEngines Corp. "
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002298static bool be_flash_redboot(struct be_adapter *adapter,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002299 const u8 *p, u32 img_start, int image_size,
2300 int hdr_size)
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002301{
2302 u32 crc_offset;
2303 u8 flashed_crc[4];
2304 int status;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002305
2306 crc_offset = hdr_size + img_start + image_size - 4;
2307
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002308 p += crc_offset;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002309
2310 status = be_cmd_get_flash_crc(adapter, flashed_crc,
Ajit Khapardef510fc62010-03-31 01:47:45 +00002311 (image_size - 4));
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002312 if (status) {
2313 dev_err(&adapter->pdev->dev,
2314 "could not get crc from flash, not flashing redboot\n");
2315 return false;
2316 }
2317
2318 /*update redboot only if crc does not match*/
2319 if (!memcmp(flashed_crc, p, 4))
2320 return false;
2321 else
2322 return true;
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002323}
2324
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002325static int be_flash_data(struct be_adapter *adapter,
Ajit Khaparde84517482009-09-04 03:12:16 +00002326 const struct firmware *fw,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002327 struct be_dma_mem *flash_cmd, int num_of_images)
2328
Ajit Khaparde84517482009-09-04 03:12:16 +00002329{
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002330 int status = 0, i, filehdr_size = 0;
2331 u32 total_bytes = 0, flash_op;
Ajit Khaparde84517482009-09-04 03:12:16 +00002332 int num_bytes;
2333 const u8 *p = fw->data;
2334 struct be_cmd_write_flashrom *req = flash_cmd->va;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002335 struct flash_comp *pflashcomp;
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002336 int num_comp;
Ajit Khaparde84517482009-09-04 03:12:16 +00002337
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002338 struct flash_comp gen3_flash_types[9] = {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002339 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, IMG_TYPE_ISCSI_ACTIVE,
2340 FLASH_IMAGE_MAX_SIZE_g3},
2341 { FLASH_REDBOOT_START_g3, IMG_TYPE_REDBOOT,
2342 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3},
2343 { FLASH_iSCSI_BIOS_START_g3, IMG_TYPE_BIOS,
2344 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2345 { FLASH_PXE_BIOS_START_g3, IMG_TYPE_PXE_BIOS,
2346 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2347 { FLASH_FCoE_BIOS_START_g3, IMG_TYPE_FCOE_BIOS,
2348 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2349 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, IMG_TYPE_ISCSI_BACKUP,
2350 FLASH_IMAGE_MAX_SIZE_g3},
2351 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, IMG_TYPE_FCOE_FW_ACTIVE,
2352 FLASH_IMAGE_MAX_SIZE_g3},
2353 { FLASH_FCoE_BACKUP_IMAGE_START_g3, IMG_TYPE_FCOE_FW_BACKUP,
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002354 FLASH_IMAGE_MAX_SIZE_g3},
2355 { FLASH_NCSI_START_g3, IMG_TYPE_NCSI_FW,
2356 FLASH_NCSI_IMAGE_MAX_SIZE_g3}
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002357 };
2358 struct flash_comp gen2_flash_types[8] = {
2359 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, IMG_TYPE_ISCSI_ACTIVE,
2360 FLASH_IMAGE_MAX_SIZE_g2},
2361 { FLASH_REDBOOT_START_g2, IMG_TYPE_REDBOOT,
2362 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2},
2363 { FLASH_iSCSI_BIOS_START_g2, IMG_TYPE_BIOS,
2364 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2365 { FLASH_PXE_BIOS_START_g2, IMG_TYPE_PXE_BIOS,
2366 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2367 { FLASH_FCoE_BIOS_START_g2, IMG_TYPE_FCOE_BIOS,
2368 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2369 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, IMG_TYPE_ISCSI_BACKUP,
2370 FLASH_IMAGE_MAX_SIZE_g2},
2371 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, IMG_TYPE_FCOE_FW_ACTIVE,
2372 FLASH_IMAGE_MAX_SIZE_g2},
2373 { FLASH_FCoE_BACKUP_IMAGE_START_g2, IMG_TYPE_FCOE_FW_BACKUP,
2374 FLASH_IMAGE_MAX_SIZE_g2}
2375 };
2376
2377 if (adapter->generation == BE_GEN3) {
2378 pflashcomp = gen3_flash_types;
2379 filehdr_size = sizeof(struct flash_file_hdr_g3);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002380 num_comp = 9;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002381 } else {
2382 pflashcomp = gen2_flash_types;
2383 filehdr_size = sizeof(struct flash_file_hdr_g2);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002384 num_comp = 8;
Ajit Khaparde84517482009-09-04 03:12:16 +00002385 }
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002386 for (i = 0; i < num_comp; i++) {
2387 if ((pflashcomp[i].optype == IMG_TYPE_NCSI_FW) &&
2388 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
2389 continue;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002390 if ((pflashcomp[i].optype == IMG_TYPE_REDBOOT) &&
2391 (!be_flash_redboot(adapter, fw->data,
2392 pflashcomp[i].offset, pflashcomp[i].size,
2393 filehdr_size)))
2394 continue;
2395 p = fw->data;
2396 p += filehdr_size + pflashcomp[i].offset
2397 + (num_of_images * sizeof(struct image_hdr));
2398 if (p + pflashcomp[i].size > fw->data + fw->size)
Ajit Khaparde84517482009-09-04 03:12:16 +00002399 return -1;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002400 total_bytes = pflashcomp[i].size;
2401 while (total_bytes) {
2402 if (total_bytes > 32*1024)
2403 num_bytes = 32*1024;
2404 else
2405 num_bytes = total_bytes;
2406 total_bytes -= num_bytes;
Ajit Khaparde84517482009-09-04 03:12:16 +00002407
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002408 if (!total_bytes)
2409 flash_op = FLASHROM_OPER_FLASH;
2410 else
2411 flash_op = FLASHROM_OPER_SAVE;
2412 memcpy(req->params.data_buf, p, num_bytes);
2413 p += num_bytes;
2414 status = be_cmd_write_flashrom(adapter, flash_cmd,
2415 pflashcomp[i].optype, flash_op, num_bytes);
2416 if (status) {
2417 dev_err(&adapter->pdev->dev,
2418 "cmd to write to flash rom failed.\n");
2419 return -1;
2420 }
2421 yield();
Ajit Khaparde84517482009-09-04 03:12:16 +00002422 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002423 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002424 return 0;
2425}
2426
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002427static int get_ufigen_type(struct flash_file_hdr_g2 *fhdr)
2428{
2429 if (fhdr == NULL)
2430 return 0;
2431 if (fhdr->build[0] == '3')
2432 return BE_GEN3;
2433 else if (fhdr->build[0] == '2')
2434 return BE_GEN2;
2435 else
2436 return 0;
2437}
2438
Ajit Khaparde84517482009-09-04 03:12:16 +00002439int be_load_fw(struct be_adapter *adapter, u8 *func)
2440{
2441 char fw_file[ETHTOOL_FLASH_MAX_FILENAME];
2442 const struct firmware *fw;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002443 struct flash_file_hdr_g2 *fhdr;
2444 struct flash_file_hdr_g3 *fhdr3;
2445 struct image_hdr *img_hdr_ptr = NULL;
Ajit Khaparde84517482009-09-04 03:12:16 +00002446 struct be_dma_mem flash_cmd;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002447 int status, i = 0, num_imgs = 0;
Ajit Khaparde84517482009-09-04 03:12:16 +00002448 const u8 *p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002449
Ajit Khaparde84517482009-09-04 03:12:16 +00002450 strcpy(fw_file, func);
2451
2452 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
2453 if (status)
2454 goto fw_exit;
2455
2456 p = fw->data;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002457 fhdr = (struct flash_file_hdr_g2 *) p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002458 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
2459
Ajit Khaparde84517482009-09-04 03:12:16 +00002460 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
2461 flash_cmd.va = pci_alloc_consistent(adapter->pdev, flash_cmd.size,
2462 &flash_cmd.dma);
2463 if (!flash_cmd.va) {
2464 status = -ENOMEM;
2465 dev_err(&adapter->pdev->dev,
2466 "Memory allocation failure while flashing\n");
2467 goto fw_exit;
2468 }
2469
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002470 if ((adapter->generation == BE_GEN3) &&
2471 (get_ufigen_type(fhdr) == BE_GEN3)) {
2472 fhdr3 = (struct flash_file_hdr_g3 *) fw->data;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002473 num_imgs = le32_to_cpu(fhdr3->num_imgs);
2474 for (i = 0; i < num_imgs; i++) {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002475 img_hdr_ptr = (struct image_hdr *) (fw->data +
2476 (sizeof(struct flash_file_hdr_g3) +
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002477 i * sizeof(struct image_hdr)));
2478 if (le32_to_cpu(img_hdr_ptr->imageid) == 1)
2479 status = be_flash_data(adapter, fw, &flash_cmd,
2480 num_imgs);
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002481 }
2482 } else if ((adapter->generation == BE_GEN2) &&
2483 (get_ufigen_type(fhdr) == BE_GEN2)) {
2484 status = be_flash_data(adapter, fw, &flash_cmd, 0);
2485 } else {
2486 dev_err(&adapter->pdev->dev,
2487 "UFI and Interface are not compatible for flashing\n");
2488 status = -1;
Ajit Khaparde84517482009-09-04 03:12:16 +00002489 }
2490
2491 pci_free_consistent(adapter->pdev, flash_cmd.size, flash_cmd.va,
2492 flash_cmd.dma);
2493 if (status) {
2494 dev_err(&adapter->pdev->dev, "Firmware load error\n");
2495 goto fw_exit;
2496 }
2497
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002498 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
Ajit Khaparde84517482009-09-04 03:12:16 +00002499
2500fw_exit:
2501 release_firmware(fw);
2502 return status;
2503}
2504
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002505static struct net_device_ops be_netdev_ops = {
2506 .ndo_open = be_open,
2507 .ndo_stop = be_close,
2508 .ndo_start_xmit = be_xmit,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002509 .ndo_set_rx_mode = be_set_multicast_list,
2510 .ndo_set_mac_address = be_mac_addr_set,
2511 .ndo_change_mtu = be_change_mtu,
2512 .ndo_validate_addr = eth_validate_addr,
2513 .ndo_vlan_rx_register = be_vlan_register,
2514 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
2515 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002516 .ndo_set_vf_mac = be_set_vf_mac,
Ajit Khaparde1da87b72010-07-23 01:51:22 +00002517 .ndo_set_vf_vlan = be_set_vf_vlan,
Ajit Khapardee1d18732010-07-23 01:52:13 +00002518 .ndo_set_vf_tx_rate = be_set_vf_tx_rate,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002519 .ndo_get_vf_config = be_get_vf_config
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002520};
2521
2522static void be_netdev_init(struct net_device *netdev)
2523{
2524 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3abcded2010-10-03 22:12:27 -07002525 struct be_rx_obj *rxo;
2526 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002527
2528 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
Ajit Khaparde583e3f32009-10-05 02:22:19 +00002529 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_HW_CSUM |
Ajit Khaparde49e4b8472010-06-14 04:56:07 +00002530 NETIF_F_GRO | NETIF_F_TSO6;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002531
Ajit Khaparde51c59872009-11-29 17:54:54 +00002532 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2533
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002534 netdev->flags |= IFF_MULTICAST;
2535
Ajit Khaparde728a9972009-04-13 15:41:22 -07002536 adapter->rx_csum = true;
2537
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002538 /* Default settings for Rx and Tx flow control */
2539 adapter->rx_fc = true;
2540 adapter->tx_fc = true;
2541
Ajit Khapardec190e3c2009-09-04 03:12:29 +00002542 netif_set_gso_max_size(netdev, 65535);
2543
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002544 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
2545
2546 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
2547
Sathya Perla3abcded2010-10-03 22:12:27 -07002548 for_all_rx_queues(adapter, rxo, i)
2549 netif_napi_add(netdev, &rxo->rx_eq.napi, be_poll_rx,
2550 BE_NAPI_WEIGHT);
2551
Sathya Perla5fb379e2009-06-18 00:02:59 +00002552 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002553 BE_NAPI_WEIGHT);
2554
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002555 netif_stop_queue(netdev);
2556}
2557
2558static void be_unmap_pci_bars(struct be_adapter *adapter)
2559{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002560 if (adapter->csr)
2561 iounmap(adapter->csr);
2562 if (adapter->db)
2563 iounmap(adapter->db);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002564 if (adapter->pcicfg && be_physfn(adapter))
Sathya Perla8788fdc2009-07-27 22:52:03 +00002565 iounmap(adapter->pcicfg);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002566}
2567
2568static int be_map_pci_bars(struct be_adapter *adapter)
2569{
2570 u8 __iomem *addr;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002571 int pcicfg_reg, db_reg;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002572
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002573 if (be_physfn(adapter)) {
2574 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
2575 pci_resource_len(adapter->pdev, 2));
2576 if (addr == NULL)
2577 return -ENOMEM;
2578 adapter->csr = addr;
2579 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002580
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002581 if (adapter->generation == BE_GEN2) {
2582 pcicfg_reg = 1;
2583 db_reg = 4;
2584 } else {
2585 pcicfg_reg = 0;
2586 if (be_physfn(adapter))
2587 db_reg = 4;
2588 else
2589 db_reg = 0;
2590 }
2591 addr = ioremap_nocache(pci_resource_start(adapter->pdev, db_reg),
2592 pci_resource_len(adapter->pdev, db_reg));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002593 if (addr == NULL)
2594 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00002595 adapter->db = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002596
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002597 if (be_physfn(adapter)) {
2598 addr = ioremap_nocache(
2599 pci_resource_start(adapter->pdev, pcicfg_reg),
2600 pci_resource_len(adapter->pdev, pcicfg_reg));
2601 if (addr == NULL)
2602 goto pci_map_err;
2603 adapter->pcicfg = addr;
2604 } else
2605 adapter->pcicfg = adapter->db + SRIOV_VF_PCICFG_OFFSET;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002606
2607 return 0;
2608pci_map_err:
2609 be_unmap_pci_bars(adapter);
2610 return -ENOMEM;
2611}
2612
2613
2614static void be_ctrl_cleanup(struct be_adapter *adapter)
2615{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002616 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002617
2618 be_unmap_pci_bars(adapter);
2619
2620 if (mem->va)
2621 pci_free_consistent(adapter->pdev, mem->size,
2622 mem->va, mem->dma);
Sathya Perlae7b909a2009-11-22 22:01:10 +00002623
2624 mem = &adapter->mc_cmd_mem;
2625 if (mem->va)
2626 pci_free_consistent(adapter->pdev, mem->size,
2627 mem->va, mem->dma);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002628}
2629
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002630static int be_ctrl_init(struct be_adapter *adapter)
2631{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002632 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
2633 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002634 struct be_dma_mem *mc_cmd_mem = &adapter->mc_cmd_mem;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002635 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002636
2637 status = be_map_pci_bars(adapter);
2638 if (status)
Sathya Perlae7b909a2009-11-22 22:01:10 +00002639 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002640
2641 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
2642 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
2643 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
2644 if (!mbox_mem_alloc->va) {
Sathya Perlae7b909a2009-11-22 22:01:10 +00002645 status = -ENOMEM;
2646 goto unmap_pci_bars;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002647 }
Sathya Perlae7b909a2009-11-22 22:01:10 +00002648
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002649 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
2650 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
2651 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
2652 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Sathya Perlae7b909a2009-11-22 22:01:10 +00002653
2654 mc_cmd_mem->size = sizeof(struct be_cmd_req_mcast_mac_config);
2655 mc_cmd_mem->va = pci_alloc_consistent(adapter->pdev, mc_cmd_mem->size,
2656 &mc_cmd_mem->dma);
2657 if (mc_cmd_mem->va == NULL) {
2658 status = -ENOMEM;
2659 goto free_mbox;
2660 }
2661 memset(mc_cmd_mem->va, 0, mc_cmd_mem->size);
2662
Sathya Perla8788fdc2009-07-27 22:52:03 +00002663 spin_lock_init(&adapter->mbox_lock);
2664 spin_lock_init(&adapter->mcc_lock);
2665 spin_lock_init(&adapter->mcc_cq_lock);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002666
Sarveshwar Bandidd131e72010-05-25 16:16:32 -07002667 init_completion(&adapter->flash_compl);
Sathya Perlacf588472010-02-14 21:22:01 +00002668 pci_save_state(adapter->pdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002669 return 0;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002670
2671free_mbox:
2672 pci_free_consistent(adapter->pdev, mbox_mem_alloc->size,
2673 mbox_mem_alloc->va, mbox_mem_alloc->dma);
2674
2675unmap_pci_bars:
2676 be_unmap_pci_bars(adapter);
2677
2678done:
2679 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002680}
2681
2682static void be_stats_cleanup(struct be_adapter *adapter)
2683{
Sathya Perla3abcded2010-10-03 22:12:27 -07002684 struct be_dma_mem *cmd = &adapter->stats_cmd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002685
2686 if (cmd->va)
2687 pci_free_consistent(adapter->pdev, cmd->size,
2688 cmd->va, cmd->dma);
2689}
2690
2691static int be_stats_init(struct be_adapter *adapter)
2692{
Sathya Perla3abcded2010-10-03 22:12:27 -07002693 struct be_dma_mem *cmd = &adapter->stats_cmd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002694
2695 cmd->size = sizeof(struct be_cmd_req_get_stats);
2696 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
2697 if (cmd->va == NULL)
2698 return -1;
David S. Millerd291b9a2010-01-28 21:36:21 -08002699 memset(cmd->va, 0, cmd->size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002700 return 0;
2701}
2702
2703static void __devexit be_remove(struct pci_dev *pdev)
2704{
2705 struct be_adapter *adapter = pci_get_drvdata(pdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002706
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002707 if (!adapter)
2708 return;
2709
2710 unregister_netdev(adapter->netdev);
2711
Sathya Perla5fb379e2009-06-18 00:02:59 +00002712 be_clear(adapter);
2713
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002714 be_stats_cleanup(adapter);
2715
2716 be_ctrl_cleanup(adapter);
2717
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002718 be_sriov_disable(adapter);
2719
Sathya Perla8d56ff12009-11-22 22:02:26 +00002720 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002721
2722 pci_set_drvdata(pdev, NULL);
2723 pci_release_regions(pdev);
2724 pci_disable_device(pdev);
2725
2726 free_netdev(adapter->netdev);
2727}
2728
Sathya Perla2243e2e2009-11-22 22:02:03 +00002729static int be_get_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002730{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002731 int status;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002732 u8 mac[ETH_ALEN];
Sathya Perla43a04fdc2009-10-14 20:21:17 +00002733
Sathya Perla8788fdc2009-07-27 22:52:03 +00002734 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002735 if (status)
2736 return status;
2737
Sathya Perla3abcded2010-10-03 22:12:27 -07002738 status = be_cmd_query_fw_cfg(adapter, &adapter->port_num,
2739 &adapter->function_mode, &adapter->function_caps);
Sathya Perla2243e2e2009-11-22 22:02:03 +00002740 if (status)
2741 return status;
2742
2743 memset(mac, 0, ETH_ALEN);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002744
2745 if (be_physfn(adapter)) {
2746 status = be_cmd_mac_addr_query(adapter, mac,
Sathya Perla2243e2e2009-11-22 22:02:03 +00002747 MAC_ADDRESS_TYPE_NETWORK, true /*permanent */, 0);
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002748
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002749 if (status)
2750 return status;
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002751
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002752 if (!is_valid_ether_addr(mac))
2753 return -EADDRNOTAVAIL;
2754
2755 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2756 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2757 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002758
Ajit Khaparde3486be22010-07-23 02:04:54 +00002759 if (adapter->function_mode & 0x400)
Ajit Khaparde82903e42010-02-09 01:34:57 +00002760 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED/4;
2761 else
2762 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED;
2763
Sathya Perla2243e2e2009-11-22 22:02:03 +00002764 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002765}
2766
2767static int __devinit be_probe(struct pci_dev *pdev,
2768 const struct pci_device_id *pdev_id)
2769{
2770 int status = 0;
2771 struct be_adapter *adapter;
2772 struct net_device *netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002773
2774 status = pci_enable_device(pdev);
2775 if (status)
2776 goto do_none;
2777
2778 status = pci_request_regions(pdev, DRV_NAME);
2779 if (status)
2780 goto disable_dev;
2781 pci_set_master(pdev);
2782
2783 netdev = alloc_etherdev(sizeof(struct be_adapter));
2784 if (netdev == NULL) {
2785 status = -ENOMEM;
2786 goto rel_reg;
2787 }
2788 adapter = netdev_priv(netdev);
Ajit Khaparde7b139c82010-01-27 21:56:44 +00002789
2790 switch (pdev->device) {
2791 case BE_DEVICE_ID1:
2792 case OC_DEVICE_ID1:
2793 adapter->generation = BE_GEN2;
2794 break;
2795 case BE_DEVICE_ID2:
2796 case OC_DEVICE_ID2:
2797 adapter->generation = BE_GEN3;
2798 break;
2799 default:
2800 adapter->generation = 0;
2801 }
2802
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002803 adapter->pdev = pdev;
2804 pci_set_drvdata(pdev, adapter);
2805 adapter->netdev = netdev;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002806 SET_NETDEV_DEV(netdev, &pdev->dev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002807
Yang Hongyange9304382009-04-13 14:40:14 -07002808 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002809 if (!status) {
2810 netdev->features |= NETIF_F_HIGHDMA;
2811 } else {
Yang Hongyange9304382009-04-13 14:40:14 -07002812 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002813 if (status) {
2814 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
2815 goto free_netdev;
2816 }
2817 }
2818
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002819 be_sriov_enable(adapter);
2820
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002821 status = be_ctrl_init(adapter);
2822 if (status)
2823 goto free_netdev;
2824
Sathya Perla2243e2e2009-11-22 22:02:03 +00002825 /* sync up with fw's ready state */
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002826 if (be_physfn(adapter)) {
2827 status = be_cmd_POST(adapter);
2828 if (status)
2829 goto ctrl_clean;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002830 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002831
2832 /* tell fw we're ready to fire cmds */
2833 status = be_cmd_fw_init(adapter);
2834 if (status)
2835 goto ctrl_clean;
2836
Sarveshwar Bandi556ae192010-05-24 18:38:25 -07002837 if (be_physfn(adapter)) {
2838 status = be_cmd_reset_function(adapter);
2839 if (status)
2840 goto ctrl_clean;
2841 }
2842
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002843 status = be_stats_init(adapter);
2844 if (status)
2845 goto ctrl_clean;
2846
Sathya Perla2243e2e2009-11-22 22:02:03 +00002847 status = be_get_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002848 if (status)
2849 goto stats_clean;
2850
Sathya Perla3abcded2010-10-03 22:12:27 -07002851 be_msix_enable(adapter);
2852
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002853 INIT_DELAYED_WORK(&adapter->work, be_worker);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002854
Sathya Perla5fb379e2009-06-18 00:02:59 +00002855 status = be_setup(adapter);
2856 if (status)
Sathya Perla3abcded2010-10-03 22:12:27 -07002857 goto msix_disable;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002858
Sathya Perla3abcded2010-10-03 22:12:27 -07002859 be_netdev_init(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002860 status = register_netdev(netdev);
2861 if (status != 0)
Sathya Perla5fb379e2009-06-18 00:02:59 +00002862 goto unsetup;
Somnath Kotur63a76942010-10-25 01:11:10 +00002863 netif_carrier_off(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002864
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002865 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002866 return 0;
2867
Sathya Perla5fb379e2009-06-18 00:02:59 +00002868unsetup:
2869 be_clear(adapter);
Sathya Perla3abcded2010-10-03 22:12:27 -07002870msix_disable:
2871 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002872stats_clean:
2873 be_stats_cleanup(adapter);
2874ctrl_clean:
2875 be_ctrl_cleanup(adapter);
2876free_netdev:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002877 be_sriov_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002878 free_netdev(adapter->netdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002879 pci_set_drvdata(pdev, NULL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002880rel_reg:
2881 pci_release_regions(pdev);
2882disable_dev:
2883 pci_disable_device(pdev);
2884do_none:
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002885 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002886 return status;
2887}
2888
2889static int be_suspend(struct pci_dev *pdev, pm_message_t state)
2890{
2891 struct be_adapter *adapter = pci_get_drvdata(pdev);
2892 struct net_device *netdev = adapter->netdev;
2893
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002894 if (adapter->wol)
2895 be_setup_wol(adapter, true);
2896
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002897 netif_device_detach(netdev);
2898 if (netif_running(netdev)) {
2899 rtnl_lock();
2900 be_close(netdev);
2901 rtnl_unlock();
2902 }
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002903 be_cmd_get_flow_control(adapter, &adapter->tx_fc, &adapter->rx_fc);
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002904 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002905
2906 pci_save_state(pdev);
2907 pci_disable_device(pdev);
2908 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2909 return 0;
2910}
2911
2912static int be_resume(struct pci_dev *pdev)
2913{
2914 int status = 0;
2915 struct be_adapter *adapter = pci_get_drvdata(pdev);
2916 struct net_device *netdev = adapter->netdev;
2917
2918 netif_device_detach(netdev);
2919
2920 status = pci_enable_device(pdev);
2921 if (status)
2922 return status;
2923
2924 pci_set_power_state(pdev, 0);
2925 pci_restore_state(pdev);
2926
Sathya Perla2243e2e2009-11-22 22:02:03 +00002927 /* tell fw we're ready to fire cmds */
2928 status = be_cmd_fw_init(adapter);
2929 if (status)
2930 return status;
2931
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002932 be_setup(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002933 if (netif_running(netdev)) {
2934 rtnl_lock();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002935 be_open(netdev);
2936 rtnl_unlock();
2937 }
2938 netif_device_attach(netdev);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002939
2940 if (adapter->wol)
2941 be_setup_wol(adapter, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002942 return 0;
2943}
2944
Sathya Perla82456b02010-02-17 01:35:37 +00002945/*
2946 * An FLR will stop BE from DMAing any data.
2947 */
2948static void be_shutdown(struct pci_dev *pdev)
2949{
2950 struct be_adapter *adapter = pci_get_drvdata(pdev);
2951 struct net_device *netdev = adapter->netdev;
2952
2953 netif_device_detach(netdev);
2954
2955 be_cmd_reset_function(adapter);
2956
2957 if (adapter->wol)
2958 be_setup_wol(adapter, true);
2959
2960 pci_disable_device(pdev);
Sathya Perla82456b02010-02-17 01:35:37 +00002961}
2962
Sathya Perlacf588472010-02-14 21:22:01 +00002963static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
2964 pci_channel_state_t state)
2965{
2966 struct be_adapter *adapter = pci_get_drvdata(pdev);
2967 struct net_device *netdev = adapter->netdev;
2968
2969 dev_err(&adapter->pdev->dev, "EEH error detected\n");
2970
2971 adapter->eeh_err = true;
2972
2973 netif_device_detach(netdev);
2974
2975 if (netif_running(netdev)) {
2976 rtnl_lock();
2977 be_close(netdev);
2978 rtnl_unlock();
2979 }
2980 be_clear(adapter);
2981
2982 if (state == pci_channel_io_perm_failure)
2983 return PCI_ERS_RESULT_DISCONNECT;
2984
2985 pci_disable_device(pdev);
2986
2987 return PCI_ERS_RESULT_NEED_RESET;
2988}
2989
2990static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
2991{
2992 struct be_adapter *adapter = pci_get_drvdata(pdev);
2993 int status;
2994
2995 dev_info(&adapter->pdev->dev, "EEH reset\n");
2996 adapter->eeh_err = false;
2997
2998 status = pci_enable_device(pdev);
2999 if (status)
3000 return PCI_ERS_RESULT_DISCONNECT;
3001
3002 pci_set_master(pdev);
3003 pci_set_power_state(pdev, 0);
3004 pci_restore_state(pdev);
3005
3006 /* Check if card is ok and fw is ready */
3007 status = be_cmd_POST(adapter);
3008 if (status)
3009 return PCI_ERS_RESULT_DISCONNECT;
3010
3011 return PCI_ERS_RESULT_RECOVERED;
3012}
3013
3014static void be_eeh_resume(struct pci_dev *pdev)
3015{
3016 int status = 0;
3017 struct be_adapter *adapter = pci_get_drvdata(pdev);
3018 struct net_device *netdev = adapter->netdev;
3019
3020 dev_info(&adapter->pdev->dev, "EEH resume\n");
3021
3022 pci_save_state(pdev);
3023
3024 /* tell fw we're ready to fire cmds */
3025 status = be_cmd_fw_init(adapter);
3026 if (status)
3027 goto err;
3028
3029 status = be_setup(adapter);
3030 if (status)
3031 goto err;
3032
3033 if (netif_running(netdev)) {
3034 status = be_open(netdev);
3035 if (status)
3036 goto err;
3037 }
3038 netif_device_attach(netdev);
3039 return;
3040err:
3041 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
Sathya Perlacf588472010-02-14 21:22:01 +00003042}
3043
3044static struct pci_error_handlers be_eeh_handlers = {
3045 .error_detected = be_eeh_err_detected,
3046 .slot_reset = be_eeh_reset,
3047 .resume = be_eeh_resume,
3048};
3049
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003050static struct pci_driver be_driver = {
3051 .name = DRV_NAME,
3052 .id_table = be_dev_ids,
3053 .probe = be_probe,
3054 .remove = be_remove,
3055 .suspend = be_suspend,
Sathya Perlacf588472010-02-14 21:22:01 +00003056 .resume = be_resume,
Sathya Perla82456b02010-02-17 01:35:37 +00003057 .shutdown = be_shutdown,
Sathya Perlacf588472010-02-14 21:22:01 +00003058 .err_handler = &be_eeh_handlers
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003059};
3060
3061static int __init be_init_module(void)
3062{
Joe Perches8e95a202009-12-03 07:58:21 +00003063 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
3064 rx_frag_size != 2048) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003065 printk(KERN_WARNING DRV_NAME
3066 " : Module param rx_frag_size must be 2048/4096/8192."
3067 " Using 2048\n");
3068 rx_frag_size = 2048;
3069 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003070
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003071 if (num_vfs > 32) {
3072 printk(KERN_WARNING DRV_NAME
3073 " : Module param num_vfs must not be greater than 32."
3074 "Using 32\n");
3075 num_vfs = 32;
3076 }
3077
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003078 return pci_register_driver(&be_driver);
3079}
3080module_init(be_init_module);
3081
3082static void __exit be_exit_module(void)
3083{
3084 pci_unregister_driver(&be_driver);
3085}
3086module_exit(be_exit_module);