blob: 87673558cde511440dd7708c0b01106e1d7bff1f [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
Ajit Khaparde728a9972009-04-13 15:41:22 -0700852static inline bool do_pkt_csum(struct be_eth_rx_compl *rxcp, bool cso)
853{
854 u8 l4_cksm, ip_version, ipcksm, tcpf = 0, udpf = 0, ipv6_chk;
855
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);
858 ip_version = AMAP_GET_BITS(struct amap_eth_rx_compl, ip_version, rxcp);
859 if (ip_version) {
860 tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
861 udpf = AMAP_GET_BITS(struct amap_eth_rx_compl, udpf, rxcp);
862 }
863 ipv6_chk = (ip_version && (tcpf || udpf));
864
865 return ((l4_cksm && ipv6_chk && ipcksm) && cso) ? false : true;
866}
867
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700868static struct be_rx_page_info *
Sathya Perla3abcded2010-10-03 22:12:27 -0700869get_rx_page_info(struct be_adapter *adapter,
870 struct be_rx_obj *rxo,
871 u16 frag_idx)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700872{
873 struct be_rx_page_info *rx_page_info;
Sathya Perla3abcded2010-10-03 22:12:27 -0700874 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700875
Sathya Perla3abcded2010-10-03 22:12:27 -0700876 rx_page_info = &rxo->page_info_tbl[frag_idx];
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700877 BUG_ON(!rx_page_info->page);
878
Ajit Khaparde205859a2010-02-09 01:34:21 +0000879 if (rx_page_info->last_page_user) {
FUJITA Tomonorifac6da52010-04-01 16:53:22 +0000880 pci_unmap_page(adapter->pdev, dma_unmap_addr(rx_page_info, bus),
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700881 adapter->big_page_size, PCI_DMA_FROMDEVICE);
Ajit Khaparde205859a2010-02-09 01:34:21 +0000882 rx_page_info->last_page_user = false;
883 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700884
885 atomic_dec(&rxq->used);
886 return rx_page_info;
887}
888
889/* Throwaway the data in the Rx completion */
890static void be_rx_compl_discard(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -0700891 struct be_rx_obj *rxo,
892 struct be_eth_rx_compl *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700893{
Sathya Perla3abcded2010-10-03 22:12:27 -0700894 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700895 struct be_rx_page_info *page_info;
896 u16 rxq_idx, i, num_rcvd;
897
898 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
899 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
900
901 for (i = 0; i < num_rcvd; i++) {
Sathya Perla3abcded2010-10-03 22:12:27 -0700902 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700903 put_page(page_info->page);
904 memset(page_info, 0, sizeof(*page_info));
905 index_inc(&rxq_idx, rxq->len);
906 }
907}
908
909/*
910 * skb_fill_rx_data forms a complete skb for an ether frame
911 * indicated by rxcp.
912 */
Sathya Perla3abcded2010-10-03 22:12:27 -0700913static void skb_fill_rx_data(struct be_adapter *adapter, struct be_rx_obj *rxo,
Sathya Perla89420422010-02-17 01:35:26 +0000914 struct sk_buff *skb, struct be_eth_rx_compl *rxcp,
915 u16 num_rcvd)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700916{
Sathya Perla3abcded2010-10-03 22:12:27 -0700917 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700918 struct be_rx_page_info *page_info;
Sathya Perla89420422010-02-17 01:35:26 +0000919 u16 rxq_idx, i, j;
Ajit Khapardefa774062009-07-22 09:28:55 -0700920 u32 pktsize, hdr_len, curr_frag_len, size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700921 u8 *start;
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +0000922 u8 pkt_type;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700923
924 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
925 pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +0000926 pkt_type = AMAP_GET_BITS(struct amap_eth_rx_compl, cast_enc, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700927
Sathya Perla3abcded2010-10-03 22:12:27 -0700928 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700929
930 start = page_address(page_info->page) + page_info->page_offset;
931 prefetch(start);
932
933 /* Copy data in the first descriptor of this completion */
934 curr_frag_len = min(pktsize, rx_frag_size);
935
936 /* Copy the header portion into skb_data */
937 hdr_len = min((u32)BE_HDR_LEN, curr_frag_len);
938 memcpy(skb->data, start, hdr_len);
939 skb->len = curr_frag_len;
940 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
941 /* Complete packet has now been moved to data */
942 put_page(page_info->page);
943 skb->data_len = 0;
944 skb->tail += curr_frag_len;
945 } else {
946 skb_shinfo(skb)->nr_frags = 1;
947 skb_shinfo(skb)->frags[0].page = page_info->page;
948 skb_shinfo(skb)->frags[0].page_offset =
949 page_info->page_offset + hdr_len;
950 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
951 skb->data_len = curr_frag_len - hdr_len;
952 skb->tail += hdr_len;
953 }
Ajit Khaparde205859a2010-02-09 01:34:21 +0000954 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700955
956 if (pktsize <= rx_frag_size) {
957 BUG_ON(num_rcvd != 1);
Sathya Perla76fbb422009-06-10 02:21:56 +0000958 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700959 }
960
961 /* More frags present for this completion */
Ajit Khapardefa774062009-07-22 09:28:55 -0700962 size = pktsize;
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000963 for (i = 1, j = 0; i < num_rcvd; i++) {
Ajit Khapardefa774062009-07-22 09:28:55 -0700964 size -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700965 index_inc(&rxq_idx, rxq->len);
Sathya Perla3abcded2010-10-03 22:12:27 -0700966 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700967
Ajit Khapardefa774062009-07-22 09:28:55 -0700968 curr_frag_len = min(size, rx_frag_size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700969
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000970 /* Coalesce all frags from the same physical page in one slot */
971 if (page_info->page_offset == 0) {
972 /* Fresh page */
973 j++;
974 skb_shinfo(skb)->frags[j].page = page_info->page;
975 skb_shinfo(skb)->frags[j].page_offset =
976 page_info->page_offset;
977 skb_shinfo(skb)->frags[j].size = 0;
978 skb_shinfo(skb)->nr_frags++;
979 } else {
980 put_page(page_info->page);
981 }
982
983 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700984 skb->len += curr_frag_len;
985 skb->data_len += curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700986
Ajit Khaparde205859a2010-02-09 01:34:21 +0000987 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700988 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +0000989 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700990
Sathya Perla76fbb422009-06-10 02:21:56 +0000991done:
Sathya Perla3abcded2010-10-03 22:12:27 -0700992 be_rx_stats_update(rxo, pktsize, num_rcvd, pkt_type);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700993}
994
Ajit Khaparde5be93b92009-07-21 12:36:19 -0700995/* Process the RX completion indicated by rxcp when GRO is disabled */
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700996static void be_rx_compl_process(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -0700997 struct be_rx_obj *rxo,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700998 struct be_eth_rx_compl *rxcp)
999{
1000 struct sk_buff *skb;
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001001 u32 vlanf, vid;
Sathya Perla89420422010-02-17 01:35:26 +00001002 u16 num_rcvd;
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001003 u8 vtm;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001004
Sathya Perla89420422010-02-17 01:35:26 +00001005 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
1006 /* Is it a flush compl that has no data */
1007 if (unlikely(num_rcvd == 0))
1008 return;
1009
Eric Dumazet89d71a62009-10-13 05:34:20 +00001010 skb = netdev_alloc_skb_ip_align(adapter->netdev, BE_HDR_LEN);
Sathya Perlaa058a632010-02-17 01:34:22 +00001011 if (unlikely(!skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001012 if (net_ratelimit())
1013 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
Sathya Perla3abcded2010-10-03 22:12:27 -07001014 be_rx_compl_discard(adapter, rxo, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001015 return;
1016 }
1017
Sathya Perla3abcded2010-10-03 22:12:27 -07001018 skb_fill_rx_data(adapter, rxo, skb, rxcp, num_rcvd);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001019
Ajit Khaparde728a9972009-04-13 15:41:22 -07001020 if (do_pkt_csum(rxcp, adapter->rx_csum))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001021 skb_checksum_none_assert(skb);
Ajit Khaparde728a9972009-04-13 15:41:22 -07001022 else
1023 skb->ip_summed = CHECKSUM_UNNECESSARY;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001024
1025 skb->truesize = skb->len + sizeof(struct sk_buff);
1026 skb->protocol = eth_type_trans(skb, adapter->netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001027
Sathya Perlaa058a632010-02-17 01:34:22 +00001028 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
1029 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
1030
1031 /* vlanf could be wrongly set in some cards.
1032 * ignore if vtm is not set */
Ajit Khaparde3486be22010-07-23 02:04:54 +00001033 if ((adapter->function_mode & 0x400) && !vtm)
Sathya Perlaa058a632010-02-17 01:34:22 +00001034 vlanf = 0;
1035
1036 if (unlikely(vlanf)) {
Ajit Khaparde82903e42010-02-09 01:34:57 +00001037 if (!adapter->vlan_grp || adapter->vlans_added == 0) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001038 kfree_skb(skb);
1039 return;
1040 }
1041 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
Ajit Khaparde9cae9e42010-03-31 02:00:32 +00001042 vid = swab16(vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001043 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid);
1044 } else {
1045 netif_receive_skb(skb);
1046 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001047}
1048
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001049/* Process the RX completion indicated by rxcp when GRO is enabled */
1050static void be_rx_compl_process_gro(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -07001051 struct be_rx_obj *rxo,
1052 struct be_eth_rx_compl *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001053{
1054 struct be_rx_page_info *page_info;
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001055 struct sk_buff *skb = NULL;
Sathya Perla3abcded2010-10-03 22:12:27 -07001056 struct be_queue_info *rxq = &rxo->q;
1057 struct be_eq_obj *eq_obj = &rxo->rx_eq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001058 u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001059 u16 i, rxq_idx = 0, vid, j;
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001060 u8 vtm;
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +00001061 u8 pkt_type;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001062
1063 num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp);
Sathya Perla89420422010-02-17 01:35:26 +00001064 /* Is it a flush compl that has no data */
1065 if (unlikely(num_rcvd == 0))
1066 return;
1067
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001068 pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp);
1069 vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp);
1070 rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp);
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001071 vtm = AMAP_GET_BITS(struct amap_eth_rx_compl, vtm, rxcp);
Ajit Khaparde1ef78ab2010-09-03 06:17:10 +00001072 pkt_type = AMAP_GET_BITS(struct amap_eth_rx_compl, cast_enc, rxcp);
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001073
1074 /* vlanf could be wrongly set in some cards.
1075 * ignore if vtm is not set */
Ajit Khaparde3486be22010-07-23 02:04:54 +00001076 if ((adapter->function_mode & 0x400) && !vtm)
Ajit Khapardedcb9b562009-09-30 21:58:22 -07001077 vlanf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001078
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001079 skb = napi_get_frags(&eq_obj->napi);
1080 if (!skb) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001081 be_rx_compl_discard(adapter, rxo, rxcp);
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001082 return;
1083 }
1084
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001085 remaining = pkt_size;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001086 for (i = 0, j = -1; i < num_rcvd; i++) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001087 page_info = get_rx_page_info(adapter, rxo, rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001088
1089 curr_frag_len = min(remaining, rx_frag_size);
1090
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001091 /* Coalesce all frags from the same physical page in one slot */
1092 if (i == 0 || page_info->page_offset == 0) {
1093 /* First frag or Fresh page */
1094 j++;
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001095 skb_shinfo(skb)->frags[j].page = page_info->page;
1096 skb_shinfo(skb)->frags[j].page_offset =
1097 page_info->page_offset;
1098 skb_shinfo(skb)->frags[j].size = 0;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001099 } else {
1100 put_page(page_info->page);
1101 }
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001102 skb_shinfo(skb)->frags[j].size += curr_frag_len;
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001103
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001104 remaining -= curr_frag_len;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001105 index_inc(&rxq_idx, rxq->len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001106 memset(page_info, 0, sizeof(*page_info));
1107 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001108 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001109
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001110 skb_shinfo(skb)->nr_frags = j + 1;
1111 skb->len = pkt_size;
1112 skb->data_len = pkt_size;
1113 skb->truesize += pkt_size;
1114 skb->ip_summed = CHECKSUM_UNNECESSARY;
1115
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001116 if (likely(!vlanf)) {
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001117 napi_gro_frags(&eq_obj->napi);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001118 } else {
1119 vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp);
Ajit Khaparde9cae9e42010-03-31 02:00:32 +00001120 vid = swab16(vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001121
Ajit Khaparde82903e42010-02-09 01:34:57 +00001122 if (!adapter->vlan_grp || adapter->vlans_added == 0)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001123 return;
1124
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001125 vlan_gro_frags(&eq_obj->napi, adapter->vlan_grp, vid);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001126 }
1127
Sathya Perla3abcded2010-10-03 22:12:27 -07001128 be_rx_stats_update(rxo, pkt_size, num_rcvd, pkt_type);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001129}
1130
Sathya Perla3abcded2010-10-03 22:12:27 -07001131static struct be_eth_rx_compl *be_rx_compl_get(struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001132{
Sathya Perla3abcded2010-10-03 22:12:27 -07001133 struct be_eth_rx_compl *rxcp = queue_tail_node(&rxo->cq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001134
1135 if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0)
1136 return NULL;
1137
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001138 rmb();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001139 be_dws_le_to_cpu(rxcp, sizeof(*rxcp));
1140
Sathya Perla3abcded2010-10-03 22:12:27 -07001141 queue_tail_inc(&rxo->cq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001142 return rxcp;
1143}
1144
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001145/* To reset the valid bit, we need to reset the whole word as
1146 * when walking the queue the valid entries are little-endian
1147 * and invalid entries are host endian
1148 */
1149static inline void be_rx_compl_reset(struct be_eth_rx_compl *rxcp)
1150{
1151 rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0;
1152}
1153
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001154static inline struct page *be_alloc_pages(u32 size)
1155{
1156 gfp_t alloc_flags = GFP_ATOMIC;
1157 u32 order = get_order(size);
1158 if (order > 0)
1159 alloc_flags |= __GFP_COMP;
1160 return alloc_pages(alloc_flags, order);
1161}
1162
1163/*
1164 * Allocate a page, split it to fragments of size rx_frag_size and post as
1165 * receive buffers to BE
1166 */
Sathya Perla3abcded2010-10-03 22:12:27 -07001167static void be_post_rx_frags(struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001168{
Sathya Perla3abcded2010-10-03 22:12:27 -07001169 struct be_adapter *adapter = rxo->adapter;
1170 struct be_rx_page_info *page_info_tbl = rxo->page_info_tbl;
Sathya Perla26d92f92010-01-21 22:52:08 -08001171 struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
Sathya Perla3abcded2010-10-03 22:12:27 -07001172 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001173 struct page *pagep = NULL;
1174 struct be_eth_rx_d *rxd;
1175 u64 page_dmaaddr = 0, frag_dmaaddr;
1176 u32 posted, page_offset = 0;
1177
Sathya Perla3abcded2010-10-03 22:12:27 -07001178 page_info = &rxo->page_info_tbl[rxq->head];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001179 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
1180 if (!pagep) {
1181 pagep = be_alloc_pages(adapter->big_page_size);
1182 if (unlikely(!pagep)) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001183 rxo->stats.rx_post_fail++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001184 break;
1185 }
1186 page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0,
1187 adapter->big_page_size,
1188 PCI_DMA_FROMDEVICE);
1189 page_info->page_offset = 0;
1190 } else {
1191 get_page(pagep);
1192 page_info->page_offset = page_offset + rx_frag_size;
1193 }
1194 page_offset = page_info->page_offset;
1195 page_info->page = pagep;
FUJITA Tomonorifac6da52010-04-01 16:53:22 +00001196 dma_unmap_addr_set(page_info, bus, page_dmaaddr);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001197 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
1198
1199 rxd = queue_head_node(rxq);
1200 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
1201 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001202
1203 /* Any space left in the current big page for another frag? */
1204 if ((page_offset + rx_frag_size + rx_frag_size) >
1205 adapter->big_page_size) {
1206 pagep = NULL;
1207 page_info->last_page_user = true;
1208 }
Sathya Perla26d92f92010-01-21 22:52:08 -08001209
1210 prev_page_info = page_info;
1211 queue_head_inc(rxq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001212 page_info = &page_info_tbl[rxq->head];
1213 }
1214 if (pagep)
Sathya Perla26d92f92010-01-21 22:52:08 -08001215 prev_page_info->last_page_user = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001216
1217 if (posted) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001218 atomic_add(posted, &rxq->used);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001219 be_rxq_notify(adapter, rxq->id, posted);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001220 } else if (atomic_read(&rxq->used) == 0) {
1221 /* Let be_worker replenish when memory is available */
Sathya Perla3abcded2010-10-03 22:12:27 -07001222 rxo->rx_post_starved = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001223 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001224}
1225
Sathya Perla5fb379e2009-06-18 00:02:59 +00001226static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001227{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001228 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
1229
1230 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
1231 return NULL;
1232
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001233 rmb();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001234 be_dws_le_to_cpu(txcp, sizeof(*txcp));
1235
1236 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
1237
1238 queue_tail_inc(tx_cq);
1239 return txcp;
1240}
1241
1242static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index)
1243{
1244 struct be_queue_info *txq = &adapter->tx_obj.q;
Alexander Duycka73b7962009-12-02 16:48:18 +00001245 struct be_eth_wrb *wrb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001246 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1247 struct sk_buff *sent_skb;
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001248 u16 cur_index, num_wrbs = 1; /* account for hdr wrb */
1249 bool unmap_skb_hdr = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001250
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001251 sent_skb = sent_skbs[txq->tail];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001252 BUG_ON(!sent_skb);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001253 sent_skbs[txq->tail] = NULL;
1254
1255 /* skip header wrb */
Alexander Duycka73b7962009-12-02 16:48:18 +00001256 queue_tail_inc(txq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001257
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001258 do {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001259 cur_index = txq->tail;
Alexander Duycka73b7962009-12-02 16:48:18 +00001260 wrb = queue_tail_node(txq);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001261 unmap_tx_frag(adapter->pdev, wrb, (unmap_skb_hdr &&
Eric Dumazete743d312010-04-14 15:59:40 -07001262 skb_headlen(sent_skb)));
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001263 unmap_skb_hdr = false;
1264
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001265 num_wrbs++;
1266 queue_tail_inc(txq);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001267 } while (cur_index != last_index);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001268
1269 atomic_sub(num_wrbs, &txq->used);
Alexander Duycka73b7962009-12-02 16:48:18 +00001270
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001271 kfree_skb(sent_skb);
1272}
1273
Sathya Perla859b1e42009-08-10 03:43:51 +00001274static inline struct be_eq_entry *event_get(struct be_eq_obj *eq_obj)
1275{
1276 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1277
1278 if (!eqe->evt)
1279 return NULL;
1280
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001281 rmb();
Sathya Perla859b1e42009-08-10 03:43:51 +00001282 eqe->evt = le32_to_cpu(eqe->evt);
1283 queue_tail_inc(&eq_obj->q);
1284 return eqe;
1285}
1286
1287static int event_handle(struct be_adapter *adapter,
1288 struct be_eq_obj *eq_obj)
1289{
1290 struct be_eq_entry *eqe;
1291 u16 num = 0;
1292
1293 while ((eqe = event_get(eq_obj)) != NULL) {
1294 eqe->evt = 0;
1295 num++;
1296 }
1297
1298 /* Deal with any spurious interrupts that come
1299 * without events
1300 */
1301 be_eq_notify(adapter, eq_obj->q.id, true, true, num);
1302 if (num)
1303 napi_schedule(&eq_obj->napi);
1304
1305 return num;
1306}
1307
1308/* Just read and notify events without processing them.
1309 * Used at the time of destroying event queues */
1310static void be_eq_clean(struct be_adapter *adapter,
1311 struct be_eq_obj *eq_obj)
1312{
1313 struct be_eq_entry *eqe;
1314 u16 num = 0;
1315
1316 while ((eqe = event_get(eq_obj)) != NULL) {
1317 eqe->evt = 0;
1318 num++;
1319 }
1320
1321 if (num)
1322 be_eq_notify(adapter, eq_obj->q.id, false, true, num);
1323}
1324
Sathya Perla3abcded2010-10-03 22:12:27 -07001325static void be_rx_q_clean(struct be_adapter *adapter, struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001326{
1327 struct be_rx_page_info *page_info;
Sathya Perla3abcded2010-10-03 22:12:27 -07001328 struct be_queue_info *rxq = &rxo->q;
1329 struct be_queue_info *rx_cq = &rxo->cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001330 struct be_eth_rx_compl *rxcp;
1331 u16 tail;
1332
1333 /* First cleanup pending rx completions */
Sathya Perla3abcded2010-10-03 22:12:27 -07001334 while ((rxcp = be_rx_compl_get(rxo)) != NULL) {
1335 be_rx_compl_discard(adapter, rxo, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001336 be_rx_compl_reset(rxcp);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001337 be_cq_notify(adapter, rx_cq->id, true, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001338 }
1339
1340 /* Then free posted rx buffer that were not used */
1341 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
Sathya Perlacdab23b2009-08-10 03:43:23 +00001342 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001343 page_info = get_rx_page_info(adapter, rxo, tail);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001344 put_page(page_info->page);
1345 memset(page_info, 0, sizeof(*page_info));
1346 }
1347 BUG_ON(atomic_read(&rxq->used));
1348}
1349
Sathya Perlaa8e91792009-08-10 03:42:43 +00001350static void be_tx_compl_clean(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001351{
Sathya Perlaa8e91792009-08-10 03:42:43 +00001352 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001353 struct be_queue_info *txq = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001354 struct be_eth_tx_compl *txcp;
1355 u16 end_idx, cmpl = 0, timeo = 0;
Sathya Perlab03388d2010-02-18 00:37:17 +00001356 struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list;
1357 struct sk_buff *sent_skb;
1358 bool dummy_wrb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001359
Sathya Perlaa8e91792009-08-10 03:42:43 +00001360 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1361 do {
1362 while ((txcp = be_tx_compl_get(tx_cq))) {
1363 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1364 wrb_index, txcp);
1365 be_tx_compl_process(adapter, end_idx);
1366 cmpl++;
1367 }
1368 if (cmpl) {
1369 be_cq_notify(adapter, tx_cq->id, false, cmpl);
1370 cmpl = 0;
1371 }
1372
1373 if (atomic_read(&txq->used) == 0 || ++timeo > 200)
1374 break;
1375
1376 mdelay(1);
1377 } while (true);
1378
1379 if (atomic_read(&txq->used))
1380 dev_err(&adapter->pdev->dev, "%d pending tx-completions\n",
1381 atomic_read(&txq->used));
Sathya Perlab03388d2010-02-18 00:37:17 +00001382
1383 /* free posted tx for which compls will never arrive */
1384 while (atomic_read(&txq->used)) {
1385 sent_skb = sent_skbs[txq->tail];
1386 end_idx = txq->tail;
1387 index_adv(&end_idx,
1388 wrb_cnt_for_skb(sent_skb, &dummy_wrb) - 1, txq->len);
1389 be_tx_compl_process(adapter, end_idx);
1390 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001391}
1392
Sathya Perla5fb379e2009-06-18 00:02:59 +00001393static void be_mcc_queues_destroy(struct be_adapter *adapter)
1394{
1395 struct be_queue_info *q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001396
Sathya Perla8788fdc2009-07-27 22:52:03 +00001397 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001398 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001399 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001400 be_queue_free(adapter, q);
1401
Sathya Perla8788fdc2009-07-27 22:52:03 +00001402 q = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001403 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001404 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001405 be_queue_free(adapter, q);
1406}
1407
1408/* Must be called only after TX qs are created as MCC shares TX EQ */
1409static int be_mcc_queues_create(struct be_adapter *adapter)
1410{
1411 struct be_queue_info *q, *cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001412
1413 /* Alloc MCC compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001414 cq = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001415 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
Sathya Perlaefd2e402009-07-27 22:53:10 +00001416 sizeof(struct be_mcc_compl)))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001417 goto err;
1418
1419 /* Ask BE to create MCC compl queue; share TX's eq */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001420 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001421 goto mcc_cq_free;
1422
1423 /* Alloc MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001424 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001425 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1426 goto mcc_cq_destroy;
1427
1428 /* Ask BE to create MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001429 if (be_cmd_mccq_create(adapter, q, cq))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001430 goto mcc_q_free;
1431
1432 return 0;
1433
1434mcc_q_free:
1435 be_queue_free(adapter, q);
1436mcc_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001437 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001438mcc_cq_free:
1439 be_queue_free(adapter, cq);
1440err:
1441 return -1;
1442}
1443
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001444static void be_tx_queues_destroy(struct be_adapter *adapter)
1445{
1446 struct be_queue_info *q;
1447
1448 q = &adapter->tx_obj.q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001449 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001450 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001451 be_queue_free(adapter, q);
1452
1453 q = &adapter->tx_obj.cq;
1454 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001455 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001456 be_queue_free(adapter, q);
1457
Sathya Perla859b1e42009-08-10 03:43:51 +00001458 /* Clear any residual events */
1459 be_eq_clean(adapter, &adapter->tx_eq);
1460
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001461 q = &adapter->tx_eq.q;
1462 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001463 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001464 be_queue_free(adapter, q);
1465}
1466
1467static int be_tx_queues_create(struct be_adapter *adapter)
1468{
1469 struct be_queue_info *eq, *q, *cq;
1470
1471 adapter->tx_eq.max_eqd = 0;
1472 adapter->tx_eq.min_eqd = 0;
1473 adapter->tx_eq.cur_eqd = 96;
1474 adapter->tx_eq.enable_aic = false;
1475 /* Alloc Tx Event queue */
1476 eq = &adapter->tx_eq.q;
1477 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry)))
1478 return -1;
1479
1480 /* Ask BE to create Tx Event queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001481 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001482 goto tx_eq_free;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001483 adapter->base_eq_id = adapter->tx_eq.q.id;
1484
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001485 /* Alloc TX eth compl queue */
1486 cq = &adapter->tx_obj.cq;
1487 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1488 sizeof(struct be_eth_tx_compl)))
1489 goto tx_eq_destroy;
1490
1491 /* Ask BE to create Tx eth compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001492 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001493 goto tx_cq_free;
1494
1495 /* Alloc TX eth queue */
1496 q = &adapter->tx_obj.q;
1497 if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb)))
1498 goto tx_cq_destroy;
1499
1500 /* Ask BE to create Tx eth queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001501 if (be_cmd_txq_create(adapter, q, cq))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001502 goto tx_q_free;
1503 return 0;
1504
1505tx_q_free:
1506 be_queue_free(adapter, q);
1507tx_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001508 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001509tx_cq_free:
1510 be_queue_free(adapter, cq);
1511tx_eq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001512 be_cmd_q_destroy(adapter, eq, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001513tx_eq_free:
1514 be_queue_free(adapter, eq);
1515 return -1;
1516}
1517
1518static void be_rx_queues_destroy(struct be_adapter *adapter)
1519{
1520 struct be_queue_info *q;
Sathya Perla3abcded2010-10-03 22:12:27 -07001521 struct be_rx_obj *rxo;
1522 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001523
Sathya Perla3abcded2010-10-03 22:12:27 -07001524 for_all_rx_queues(adapter, rxo, i) {
1525 q = &rxo->q;
1526 if (q->created) {
1527 be_cmd_q_destroy(adapter, q, QTYPE_RXQ);
1528 /* After the rxq is invalidated, wait for a grace time
1529 * of 1ms for all dma to end and the flush compl to
1530 * arrive
1531 */
1532 mdelay(1);
1533 be_rx_q_clean(adapter, rxo);
1534 }
1535 be_queue_free(adapter, q);
Sathya Perla89420422010-02-17 01:35:26 +00001536
Sathya Perla3abcded2010-10-03 22:12:27 -07001537 q = &rxo->cq;
1538 if (q->created)
1539 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1540 be_queue_free(adapter, q);
1541
1542 /* Clear any residual events */
1543 q = &rxo->rx_eq.q;
1544 if (q->created) {
1545 be_eq_clean(adapter, &rxo->rx_eq);
1546 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
1547 }
1548 be_queue_free(adapter, q);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001549 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001550}
1551
1552static int be_rx_queues_create(struct be_adapter *adapter)
1553{
1554 struct be_queue_info *eq, *q, *cq;
Sathya Perla3abcded2010-10-03 22:12:27 -07001555 struct be_rx_obj *rxo;
1556 int rc, i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001557
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001558 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
Sathya Perla3abcded2010-10-03 22:12:27 -07001559 for_all_rx_queues(adapter, rxo, i) {
1560 rxo->adapter = adapter;
1561 rxo->rx_eq.max_eqd = BE_MAX_EQD;
1562 rxo->rx_eq.enable_aic = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001563
Sathya Perla3abcded2010-10-03 22:12:27 -07001564 /* EQ */
1565 eq = &rxo->rx_eq.q;
1566 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1567 sizeof(struct be_eq_entry));
1568 if (rc)
1569 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001570
Sathya Perla3abcded2010-10-03 22:12:27 -07001571 rc = be_cmd_eq_create(adapter, eq, rxo->rx_eq.cur_eqd);
1572 if (rc)
1573 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001574
Sathya Perla3abcded2010-10-03 22:12:27 -07001575 /* CQ */
1576 cq = &rxo->cq;
1577 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1578 sizeof(struct be_eth_rx_compl));
1579 if (rc)
1580 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001581
Sathya Perla3abcded2010-10-03 22:12:27 -07001582 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
1583 if (rc)
1584 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001585
Sathya Perla3abcded2010-10-03 22:12:27 -07001586 /* Rx Q */
1587 q = &rxo->q;
1588 rc = be_queue_alloc(adapter, q, RX_Q_LEN,
1589 sizeof(struct be_eth_rx_d));
1590 if (rc)
1591 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001592
Sathya Perla3abcded2010-10-03 22:12:27 -07001593 rc = be_cmd_rxq_create(adapter, q, cq->id, rx_frag_size,
1594 BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle,
1595 (i > 0) ? 1 : 0/* rss enable */, &rxo->rss_id);
1596 if (rc)
1597 goto err;
1598 }
1599
1600 if (be_multi_rxq(adapter)) {
1601 u8 rsstable[MAX_RSS_QS];
1602
1603 for_all_rss_queues(adapter, rxo, i)
1604 rsstable[i] = rxo->rss_id;
1605
1606 rc = be_cmd_rss_config(adapter, rsstable,
1607 adapter->num_rx_qs - 1);
1608 if (rc)
1609 goto err;
1610 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001611
1612 return 0;
Sathya Perla3abcded2010-10-03 22:12:27 -07001613err:
1614 be_rx_queues_destroy(adapter);
1615 return -1;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001616}
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001617
Sathya Perlab628bde2009-08-17 00:58:26 +00001618/* There are 8 evt ids per func. Retruns the evt id's bit number */
1619static inline int be_evt_bit_get(struct be_adapter *adapter, u32 eq_id)
1620{
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001621 return eq_id - adapter->base_eq_id;
Sathya Perlab628bde2009-08-17 00:58:26 +00001622}
1623
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001624static irqreturn_t be_intx(int irq, void *dev)
1625{
1626 struct be_adapter *adapter = dev;
Sathya Perla3abcded2010-10-03 22:12:27 -07001627 struct be_rx_obj *rxo;
1628 int isr, i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001629
Sathya Perla8788fdc2009-07-27 22:52:03 +00001630 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
Sathya Perla55bdeed2010-02-02 07:48:40 -08001631 (adapter->tx_eq.q.id/ 8) * CEV_ISR_SIZE);
Sathya Perlac001c212009-07-01 01:06:07 +00001632 if (!isr)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001633 return IRQ_NONE;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001634
Sathya Perla3abcded2010-10-03 22:12:27 -07001635 if ((1 << be_evt_bit_get(adapter, adapter->tx_eq.q.id) & isr))
1636 event_handle(adapter, &adapter->tx_eq);
1637
1638 for_all_rx_queues(adapter, rxo, i) {
1639 if ((1 << be_evt_bit_get(adapter, rxo->rx_eq.q.id) & isr))
1640 event_handle(adapter, &rxo->rx_eq);
1641 }
Sathya Perlac001c212009-07-01 01:06:07 +00001642
Sathya Perla8788fdc2009-07-27 22:52:03 +00001643 return IRQ_HANDLED;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001644}
1645
1646static irqreturn_t be_msix_rx(int irq, void *dev)
1647{
Sathya Perla3abcded2010-10-03 22:12:27 -07001648 struct be_rx_obj *rxo = dev;
1649 struct be_adapter *adapter = rxo->adapter;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001650
Sathya Perla3abcded2010-10-03 22:12:27 -07001651 event_handle(adapter, &rxo->rx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001652
1653 return IRQ_HANDLED;
1654}
1655
Sathya Perla5fb379e2009-06-18 00:02:59 +00001656static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001657{
1658 struct be_adapter *adapter = dev;
1659
Sathya Perla8788fdc2009-07-27 22:52:03 +00001660 event_handle(adapter, &adapter->tx_eq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001661
1662 return IRQ_HANDLED;
1663}
1664
Sathya Perla3abcded2010-10-03 22:12:27 -07001665static inline bool do_gro(struct be_adapter *adapter, struct be_rx_obj *rxo,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001666 struct be_eth_rx_compl *rxcp)
1667{
1668 int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp);
1669 int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp);
1670
1671 if (err)
Sathya Perla3abcded2010-10-03 22:12:27 -07001672 rxo->stats.rxcp_err++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001673
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001674 return (tcp_frame && !err) ? true : false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001675}
1676
stephen hemminger49b05222010-10-21 07:50:48 +00001677static int be_poll_rx(struct napi_struct *napi, int budget)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001678{
1679 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
Sathya Perla3abcded2010-10-03 22:12:27 -07001680 struct be_rx_obj *rxo = container_of(rx_eq, struct be_rx_obj, rx_eq);
1681 struct be_adapter *adapter = rxo->adapter;
1682 struct be_queue_info *rx_cq = &rxo->cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001683 struct be_eth_rx_compl *rxcp;
1684 u32 work_done;
1685
Sathya Perla3abcded2010-10-03 22:12:27 -07001686 rxo->stats.rx_polls++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001687 for (work_done = 0; work_done < budget; work_done++) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001688 rxcp = be_rx_compl_get(rxo);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001689 if (!rxcp)
1690 break;
1691
Sathya Perla3abcded2010-10-03 22:12:27 -07001692 if (do_gro(adapter, rxo, rxcp))
1693 be_rx_compl_process_gro(adapter, rxo, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001694 else
Sathya Perla3abcded2010-10-03 22:12:27 -07001695 be_rx_compl_process(adapter, rxo, rxcp);
Sathya Perlaa7a0ef32009-06-10 02:23:28 +00001696
1697 be_rx_compl_reset(rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001698 }
1699
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001700 /* Refill the queue */
Sathya Perla3abcded2010-10-03 22:12:27 -07001701 if (atomic_read(&rxo->q.used) < RX_FRAGS_REFILL_WM)
1702 be_post_rx_frags(rxo);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001703
1704 /* All consumed */
1705 if (work_done < budget) {
1706 napi_complete(napi);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001707 be_cq_notify(adapter, rx_cq->id, true, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001708 } else {
1709 /* More to be consumed; continue with interrupts disabled */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001710 be_cq_notify(adapter, rx_cq->id, false, work_done);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001711 }
1712 return work_done;
1713}
1714
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001715/* As TX and MCC share the same EQ check for both TX and MCC completions.
1716 * For TX/MCC we don't honour budget; consume everything
1717 */
1718static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001719{
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001720 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1721 struct be_adapter *adapter =
1722 container_of(tx_eq, struct be_adapter, tx_eq);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001723 struct be_queue_info *txq = &adapter->tx_obj.q;
1724 struct be_queue_info *tx_cq = &adapter->tx_obj.cq;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001725 struct be_eth_tx_compl *txcp;
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001726 int tx_compl = 0, mcc_compl, status = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001727 u16 end_idx;
1728
Sathya Perla5fb379e2009-06-18 00:02:59 +00001729 while ((txcp = be_tx_compl_get(tx_cq))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001730 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001731 wrb_index, txcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001732 be_tx_compl_process(adapter, end_idx);
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001733 tx_compl++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001734 }
1735
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001736 mcc_compl = be_process_mcc(adapter, &status);
1737
1738 napi_complete(napi);
1739
1740 if (mcc_compl) {
1741 struct be_mcc_obj *mcc_obj = &adapter->mcc_obj;
1742 be_cq_notify(adapter, mcc_obj->cq.id, true, mcc_compl);
1743 }
1744
1745 if (tx_compl) {
1746 be_cq_notify(adapter, adapter->tx_obj.cq.id, true, tx_compl);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001747
1748 /* As Tx wrbs have been freed up, wake up netdev queue if
1749 * it was stopped due to lack of tx wrbs.
1750 */
1751 if (netif_queue_stopped(adapter->netdev) &&
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001752 atomic_read(&txq->used) < txq->len / 2) {
Sathya Perla5fb379e2009-06-18 00:02:59 +00001753 netif_wake_queue(adapter->netdev);
1754 }
1755
Sathya Perla3abcded2010-10-03 22:12:27 -07001756 tx_stats(adapter)->be_tx_events++;
1757 tx_stats(adapter)->be_tx_compl += tx_compl;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001758 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001759
1760 return 1;
1761}
1762
Ajit Khaparded053de92010-09-03 06:23:30 +00001763void be_detect_dump_ue(struct be_adapter *adapter)
Ajit Khaparde7c185272010-07-29 06:16:33 +00001764{
1765 u32 ue_status_lo, ue_status_hi, ue_status_lo_mask, ue_status_hi_mask;
1766 u32 i;
1767
1768 pci_read_config_dword(adapter->pdev,
1769 PCICFG_UE_STATUS_LOW, &ue_status_lo);
1770 pci_read_config_dword(adapter->pdev,
1771 PCICFG_UE_STATUS_HIGH, &ue_status_hi);
1772 pci_read_config_dword(adapter->pdev,
1773 PCICFG_UE_STATUS_LOW_MASK, &ue_status_lo_mask);
1774 pci_read_config_dword(adapter->pdev,
1775 PCICFG_UE_STATUS_HI_MASK, &ue_status_hi_mask);
1776
1777 ue_status_lo = (ue_status_lo & (~ue_status_lo_mask));
1778 ue_status_hi = (ue_status_hi & (~ue_status_hi_mask));
1779
Ajit Khaparded053de92010-09-03 06:23:30 +00001780 if (ue_status_lo || ue_status_hi) {
1781 adapter->ue_detected = true;
1782 dev_err(&adapter->pdev->dev, "UE Detected!!\n");
1783 }
1784
Ajit Khaparde7c185272010-07-29 06:16:33 +00001785 if (ue_status_lo) {
1786 for (i = 0; ue_status_lo; ue_status_lo >>= 1, i++) {
1787 if (ue_status_lo & 1)
1788 dev_err(&adapter->pdev->dev,
1789 "UE: %s bit set\n", ue_status_low_desc[i]);
1790 }
1791 }
1792 if (ue_status_hi) {
1793 for (i = 0; ue_status_hi; ue_status_hi >>= 1, i++) {
1794 if (ue_status_hi & 1)
1795 dev_err(&adapter->pdev->dev,
1796 "UE: %s bit set\n", ue_status_hi_desc[i]);
1797 }
1798 }
1799
1800}
1801
Sathya Perlaea1dae12009-03-19 23:56:20 -07001802static void be_worker(struct work_struct *work)
1803{
1804 struct be_adapter *adapter =
1805 container_of(work, struct be_adapter, work.work);
Sathya Perla3abcded2010-10-03 22:12:27 -07001806 struct be_rx_obj *rxo;
1807 int i;
Sathya Perlaea1dae12009-03-19 23:56:20 -07001808
Ajit Khaparde0fc48c32010-07-29 06:18:58 +00001809 if (!adapter->stats_ioctl_sent)
Sathya Perla3abcded2010-10-03 22:12:27 -07001810 be_cmd_get_stats(adapter, &adapter->stats_cmd);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001811
Sathya Perla4097f662009-03-24 16:40:13 -07001812 be_tx_rate_update(adapter);
Sathya Perla4097f662009-03-24 16:40:13 -07001813
Sathya Perla3abcded2010-10-03 22:12:27 -07001814 for_all_rx_queues(adapter, rxo, i) {
1815 be_rx_rate_update(rxo);
1816 be_rx_eqd_update(adapter, rxo);
1817
1818 if (rxo->rx_post_starved) {
1819 rxo->rx_post_starved = false;
1820 be_post_rx_frags(rxo);
1821 }
Sathya Perlaea1dae12009-03-19 23:56:20 -07001822 }
Sathya Perla3abcded2010-10-03 22:12:27 -07001823
Ajit Khaparded053de92010-09-03 06:23:30 +00001824 if (!adapter->ue_detected)
1825 be_detect_dump_ue(adapter);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001826
1827 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
1828}
1829
Sathya Perla8d56ff12009-11-22 22:02:26 +00001830static void be_msix_disable(struct be_adapter *adapter)
1831{
1832 if (adapter->msix_enabled) {
1833 pci_disable_msix(adapter->pdev);
1834 adapter->msix_enabled = false;
1835 }
1836}
1837
Sathya Perla3abcded2010-10-03 22:12:27 -07001838static int be_num_rxqs_get(struct be_adapter *adapter)
1839{
1840 if (multi_rxq && (adapter->function_caps & BE_FUNCTION_CAPS_RSS) &&
1841 !adapter->sriov_enabled && !(adapter->function_mode & 0x400)) {
1842 return 1 + MAX_RSS_QS; /* one default non-RSS queue */
1843 } else {
1844 dev_warn(&adapter->pdev->dev,
1845 "No support for multiple RX queues\n");
1846 return 1;
1847 }
1848}
1849
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001850static void be_msix_enable(struct be_adapter *adapter)
1851{
Sathya Perla3abcded2010-10-03 22:12:27 -07001852#define BE_MIN_MSIX_VECTORS (1 + 1) /* Rx + Tx */
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001853 int i, status;
1854
Sathya Perla3abcded2010-10-03 22:12:27 -07001855 adapter->num_rx_qs = be_num_rxqs_get(adapter);
1856
1857 for (i = 0; i < (adapter->num_rx_qs + 1); i++)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001858 adapter->msix_entries[i].entry = i;
1859
1860 status = pci_enable_msix(adapter->pdev, adapter->msix_entries,
Sathya Perla3abcded2010-10-03 22:12:27 -07001861 adapter->num_rx_qs + 1);
1862 if (status == 0) {
1863 goto done;
1864 } else if (status >= BE_MIN_MSIX_VECTORS) {
1865 if (pci_enable_msix(adapter->pdev, adapter->msix_entries,
1866 status) == 0) {
1867 adapter->num_rx_qs = status - 1;
1868 dev_warn(&adapter->pdev->dev,
1869 "Could alloc only %d MSIx vectors. "
1870 "Using %d RX Qs\n", status, adapter->num_rx_qs);
1871 goto done;
1872 }
1873 }
1874 return;
1875done:
1876 adapter->msix_enabled = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001877}
1878
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001879static void be_sriov_enable(struct be_adapter *adapter)
1880{
Sarveshwar Bandi344dbf12010-07-09 01:43:55 +00001881 be_check_sriov_fn_type(adapter);
Ajit Khaparde6dedec82010-07-29 06:15:32 +00001882#ifdef CONFIG_PCI_IOV
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001883 if (be_physfn(adapter) && num_vfs) {
Ajit Khaparde6dedec82010-07-29 06:15:32 +00001884 int status;
1885
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001886 status = pci_enable_sriov(adapter->pdev, num_vfs);
1887 adapter->sriov_enabled = status ? false : true;
1888 }
1889#endif
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001890}
1891
1892static void be_sriov_disable(struct be_adapter *adapter)
1893{
1894#ifdef CONFIG_PCI_IOV
1895 if (adapter->sriov_enabled) {
1896 pci_disable_sriov(adapter->pdev);
1897 adapter->sriov_enabled = false;
1898 }
1899#endif
1900}
1901
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001902static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id)
1903{
Sathya Perlab628bde2009-08-17 00:58:26 +00001904 return adapter->msix_entries[
1905 be_evt_bit_get(adapter, eq_id)].vector;
1906}
1907
1908static int be_request_irq(struct be_adapter *adapter,
1909 struct be_eq_obj *eq_obj,
Sathya Perla3abcded2010-10-03 22:12:27 -07001910 void *handler, char *desc, void *context)
Sathya Perlab628bde2009-08-17 00:58:26 +00001911{
1912 struct net_device *netdev = adapter->netdev;
1913 int vec;
1914
1915 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
1916 vec = be_msix_vec_get(adapter, eq_obj->q.id);
Sathya Perla3abcded2010-10-03 22:12:27 -07001917 return request_irq(vec, handler, 0, eq_obj->desc, context);
Sathya Perlab628bde2009-08-17 00:58:26 +00001918}
1919
Sathya Perla3abcded2010-10-03 22:12:27 -07001920static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj,
1921 void *context)
Sathya Perlab628bde2009-08-17 00:58:26 +00001922{
1923 int vec = be_msix_vec_get(adapter, eq_obj->q.id);
Sathya Perla3abcded2010-10-03 22:12:27 -07001924 free_irq(vec, context);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001925}
1926
1927static int be_msix_register(struct be_adapter *adapter)
1928{
Sathya Perla3abcded2010-10-03 22:12:27 -07001929 struct be_rx_obj *rxo;
1930 int status, i;
1931 char qname[10];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001932
Sathya Perla3abcded2010-10-03 22:12:27 -07001933 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx",
1934 adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001935 if (status)
1936 goto err;
1937
Sathya Perla3abcded2010-10-03 22:12:27 -07001938 for_all_rx_queues(adapter, rxo, i) {
1939 sprintf(qname, "rxq%d", i);
1940 status = be_request_irq(adapter, &rxo->rx_eq, be_msix_rx,
1941 qname, rxo);
1942 if (status)
1943 goto err_msix;
1944 }
Sathya Perlab628bde2009-08-17 00:58:26 +00001945
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001946 return 0;
Sathya Perlab628bde2009-08-17 00:58:26 +00001947
Sathya Perla3abcded2010-10-03 22:12:27 -07001948err_msix:
1949 be_free_irq(adapter, &adapter->tx_eq, adapter);
1950
1951 for (i--, rxo = &adapter->rx_obj[i]; i >= 0; i--, rxo--)
1952 be_free_irq(adapter, &rxo->rx_eq, rxo);
1953
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001954err:
1955 dev_warn(&adapter->pdev->dev,
1956 "MSIX Request IRQ failed - err %d\n", status);
1957 pci_disable_msix(adapter->pdev);
1958 adapter->msix_enabled = false;
1959 return status;
1960}
1961
1962static int be_irq_register(struct be_adapter *adapter)
1963{
1964 struct net_device *netdev = adapter->netdev;
1965 int status;
1966
1967 if (adapter->msix_enabled) {
1968 status = be_msix_register(adapter);
1969 if (status == 0)
1970 goto done;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00001971 /* INTx is not supported for VF */
1972 if (!be_physfn(adapter))
1973 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001974 }
1975
1976 /* INTx */
1977 netdev->irq = adapter->pdev->irq;
1978 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
1979 adapter);
1980 if (status) {
1981 dev_err(&adapter->pdev->dev,
1982 "INTx request IRQ failed - err %d\n", status);
1983 return status;
1984 }
1985done:
1986 adapter->isr_registered = true;
1987 return 0;
1988}
1989
1990static void be_irq_unregister(struct be_adapter *adapter)
1991{
1992 struct net_device *netdev = adapter->netdev;
Sathya Perla3abcded2010-10-03 22:12:27 -07001993 struct be_rx_obj *rxo;
1994 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001995
1996 if (!adapter->isr_registered)
1997 return;
1998
1999 /* INTx */
2000 if (!adapter->msix_enabled) {
2001 free_irq(netdev->irq, adapter);
2002 goto done;
2003 }
2004
2005 /* MSIx */
Sathya Perla3abcded2010-10-03 22:12:27 -07002006 be_free_irq(adapter, &adapter->tx_eq, adapter);
2007
2008 for_all_rx_queues(adapter, rxo, i)
2009 be_free_irq(adapter, &rxo->rx_eq, rxo);
2010
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002011done:
2012 adapter->isr_registered = false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002013}
2014
Sathya Perla889cd4b2010-05-30 23:33:45 +00002015static int be_close(struct net_device *netdev)
2016{
2017 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3abcded2010-10-03 22:12:27 -07002018 struct be_rx_obj *rxo;
Sathya Perla889cd4b2010-05-30 23:33:45 +00002019 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla3abcded2010-10-03 22:12:27 -07002020 int vec, i;
Sathya Perla889cd4b2010-05-30 23:33:45 +00002021
2022 cancel_delayed_work_sync(&adapter->work);
2023
2024 be_async_mcc_disable(adapter);
2025
2026 netif_stop_queue(netdev);
2027 netif_carrier_off(netdev);
2028 adapter->link_up = false;
2029
2030 be_intr_set(adapter, false);
2031
2032 if (adapter->msix_enabled) {
2033 vec = be_msix_vec_get(adapter, tx_eq->q.id);
2034 synchronize_irq(vec);
Sathya Perla3abcded2010-10-03 22:12:27 -07002035
2036 for_all_rx_queues(adapter, rxo, i) {
2037 vec = be_msix_vec_get(adapter, rxo->rx_eq.q.id);
2038 synchronize_irq(vec);
2039 }
Sathya Perla889cd4b2010-05-30 23:33:45 +00002040 } else {
2041 synchronize_irq(netdev->irq);
2042 }
2043 be_irq_unregister(adapter);
2044
Sathya Perla3abcded2010-10-03 22:12:27 -07002045 for_all_rx_queues(adapter, rxo, i)
2046 napi_disable(&rxo->rx_eq.napi);
2047
Sathya Perla889cd4b2010-05-30 23:33:45 +00002048 napi_disable(&tx_eq->napi);
2049
2050 /* Wait for all pending tx completions to arrive so that
2051 * all tx skbs are freed.
2052 */
2053 be_tx_compl_clean(adapter);
2054
2055 return 0;
2056}
2057
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002058static int be_open(struct net_device *netdev)
2059{
2060 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002061 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla3abcded2010-10-03 22:12:27 -07002062 struct be_rx_obj *rxo;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002063 bool link_up;
Sathya Perla3abcded2010-10-03 22:12:27 -07002064 int status, i;
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07002065 u8 mac_speed;
2066 u16 link_speed;
Sathya Perla5fb379e2009-06-18 00:02:59 +00002067
Sathya Perla3abcded2010-10-03 22:12:27 -07002068 for_all_rx_queues(adapter, rxo, i) {
2069 be_post_rx_frags(rxo);
2070 napi_enable(&rxo->rx_eq.napi);
2071 }
Sathya Perla5fb379e2009-06-18 00:02:59 +00002072 napi_enable(&tx_eq->napi);
2073
2074 be_irq_register(adapter);
2075
Sathya Perla8788fdc2009-07-27 22:52:03 +00002076 be_intr_set(adapter, true);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002077
2078 /* The evt queues are created in unarmed state; arm them */
Sathya Perla3abcded2010-10-03 22:12:27 -07002079 for_all_rx_queues(adapter, rxo, i) {
2080 be_eq_notify(adapter, rxo->rx_eq.q.id, true, false, 0);
2081 be_cq_notify(adapter, rxo->cq.id, true, 0);
2082 }
Sathya Perla8788fdc2009-07-27 22:52:03 +00002083 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002084
Sathya Perla7a1e9b22010-02-17 01:35:11 +00002085 /* Now that interrupts are on we can process async mcc */
2086 be_async_mcc_enable(adapter);
2087
Sathya Perla889cd4b2010-05-30 23:33:45 +00002088 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
2089
Sarveshwar Bandi0388f252009-10-28 04:15:20 -07002090 status = be_cmd_link_status_query(adapter, &link_up, &mac_speed,
2091 &link_speed);
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002092 if (status)
Sathya Perla889cd4b2010-05-30 23:33:45 +00002093 goto err;
Sathya Perlaa8f447bd2009-06-18 00:10:27 +00002094 be_link_status_update(adapter, link_up);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002095
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002096 if (be_physfn(adapter)) {
Ajit Khaparde1da87b72010-07-23 01:51:22 +00002097 status = be_vid_config(adapter, false, 0);
Sathya Perla889cd4b2010-05-30 23:33:45 +00002098 if (status)
2099 goto err;
2100
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002101 status = be_cmd_set_flow_control(adapter,
2102 adapter->tx_fc, adapter->rx_fc);
2103 if (status)
Sathya Perla889cd4b2010-05-30 23:33:45 +00002104 goto err;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002105 }
Ajit Khaparde4f2aa892009-11-06 02:07:32 +00002106
Sathya Perla889cd4b2010-05-30 23:33:45 +00002107 return 0;
2108err:
2109 be_close(adapter->netdev);
2110 return -EIO;
Sathya Perla5fb379e2009-06-18 00:02:59 +00002111}
2112
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002113static int be_setup_wol(struct be_adapter *adapter, bool enable)
2114{
2115 struct be_dma_mem cmd;
2116 int status = 0;
2117 u8 mac[ETH_ALEN];
2118
2119 memset(mac, 0, ETH_ALEN);
2120
2121 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
2122 cmd.va = pci_alloc_consistent(adapter->pdev, cmd.size, &cmd.dma);
2123 if (cmd.va == NULL)
2124 return -1;
2125 memset(cmd.va, 0, cmd.size);
2126
2127 if (enable) {
2128 status = pci_write_config_dword(adapter->pdev,
2129 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
2130 if (status) {
2131 dev_err(&adapter->pdev->dev,
Frans Pop2381a552010-03-24 07:57:36 +00002132 "Could not enable Wake-on-lan\n");
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002133 pci_free_consistent(adapter->pdev, cmd.size, cmd.va,
2134 cmd.dma);
2135 return status;
2136 }
2137 status = be_cmd_enable_magic_wol(adapter,
2138 adapter->netdev->dev_addr, &cmd);
2139 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
2140 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
2141 } else {
2142 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
2143 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
2144 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
2145 }
2146
2147 pci_free_consistent(adapter->pdev, cmd.size, cmd.va, cmd.dma);
2148 return status;
2149}
2150
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002151/*
2152 * Generate a seed MAC address from the PF MAC Address using jhash.
2153 * MAC Address for VFs are assigned incrementally starting from the seed.
2154 * These addresses are programmed in the ASIC by the PF and the VF driver
2155 * queries for the MAC address during its probe.
2156 */
2157static inline int be_vf_eth_addr_config(struct be_adapter *adapter)
2158{
2159 u32 vf = 0;
Sathya Perla3abcded2010-10-03 22:12:27 -07002160 int status = 0;
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002161 u8 mac[ETH_ALEN];
2162
2163 be_vf_eth_addr_generate(adapter, mac);
2164
2165 for (vf = 0; vf < num_vfs; vf++) {
2166 status = be_cmd_pmac_add(adapter, mac,
2167 adapter->vf_cfg[vf].vf_if_handle,
2168 &adapter->vf_cfg[vf].vf_pmac_id);
2169 if (status)
2170 dev_err(&adapter->pdev->dev,
2171 "Mac address add failed for VF %d\n", vf);
2172 else
2173 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
2174
2175 mac[5] += 1;
2176 }
2177 return status;
2178}
2179
2180static inline void be_vf_eth_addr_rem(struct be_adapter *adapter)
2181{
2182 u32 vf;
2183
2184 for (vf = 0; vf < num_vfs; vf++) {
2185 if (adapter->vf_cfg[vf].vf_pmac_id != BE_INVALID_PMAC_ID)
2186 be_cmd_pmac_del(adapter,
2187 adapter->vf_cfg[vf].vf_if_handle,
2188 adapter->vf_cfg[vf].vf_pmac_id);
2189 }
2190}
2191
Sathya Perla5fb379e2009-06-18 00:02:59 +00002192static int be_setup(struct be_adapter *adapter)
2193{
Sathya Perla5fb379e2009-06-18 00:02:59 +00002194 struct net_device *netdev = adapter->netdev;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002195 u32 cap_flags, en_flags, vf = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002196 int status;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002197 u8 mac[ETH_ALEN];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002198
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002199 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST;
2200
2201 if (be_physfn(adapter)) {
2202 cap_flags |= BE_IF_FLAGS_MCAST_PROMISCUOUS |
2203 BE_IF_FLAGS_PROMISCUOUS |
2204 BE_IF_FLAGS_PASS_L3L4_ERRORS;
2205 en_flags |= BE_IF_FLAGS_PASS_L3L4_ERRORS;
Sathya Perla3abcded2010-10-03 22:12:27 -07002206
2207 if (be_multi_rxq(adapter)) {
2208 cap_flags |= BE_IF_FLAGS_RSS;
2209 en_flags |= BE_IF_FLAGS_RSS;
2210 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002211 }
Sathya Perla73d540f2009-10-14 20:20:42 +00002212
2213 status = be_cmd_if_create(adapter, cap_flags, en_flags,
2214 netdev->dev_addr, false/* pmac_invalid */,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002215 &adapter->if_handle, &adapter->pmac_id, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002216 if (status != 0)
2217 goto do_none;
2218
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002219 if (be_physfn(adapter)) {
2220 while (vf < num_vfs) {
2221 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED
2222 | BE_IF_FLAGS_BROADCAST;
2223 status = be_cmd_if_create(adapter, cap_flags, en_flags,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002224 mac, true,
2225 &adapter->vf_cfg[vf].vf_if_handle,
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002226 NULL, vf+1);
2227 if (status) {
2228 dev_err(&adapter->pdev->dev,
2229 "Interface Create failed for VF %d\n", vf);
2230 goto if_destroy;
2231 }
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002232 adapter->vf_cfg[vf].vf_pmac_id = BE_INVALID_PMAC_ID;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002233 vf++;
Sarveshwar Bandi84e5b9f2010-05-27 16:28:15 -07002234 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002235 } else if (!be_physfn(adapter)) {
2236 status = be_cmd_mac_addr_query(adapter, mac,
2237 MAC_ADDRESS_TYPE_NETWORK, false, adapter->if_handle);
2238 if (!status) {
2239 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2240 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2241 }
2242 }
2243
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002244 status = be_tx_queues_create(adapter);
2245 if (status != 0)
2246 goto if_destroy;
2247
2248 status = be_rx_queues_create(adapter);
2249 if (status != 0)
2250 goto tx_qs_destroy;
2251
Sathya Perla5fb379e2009-06-18 00:02:59 +00002252 status = be_mcc_queues_create(adapter);
2253 if (status != 0)
2254 goto rx_qs_destroy;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002255
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002256 if (be_physfn(adapter)) {
2257 status = be_vf_eth_addr_config(adapter);
2258 if (status)
2259 goto mcc_q_destroy;
2260 }
2261
Ajit Khaparde0dffc832009-11-29 17:57:46 +00002262 adapter->link_speed = -1;
2263
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002264 return 0;
2265
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002266mcc_q_destroy:
2267 if (be_physfn(adapter))
2268 be_vf_eth_addr_rem(adapter);
2269 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002270rx_qs_destroy:
2271 be_rx_queues_destroy(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002272tx_qs_destroy:
2273 be_tx_queues_destroy(adapter);
2274if_destroy:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002275 for (vf = 0; vf < num_vfs; vf++)
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002276 if (adapter->vf_cfg[vf].vf_if_handle)
2277 be_cmd_if_destroy(adapter,
2278 adapter->vf_cfg[vf].vf_if_handle);
Sathya Perla8788fdc2009-07-27 22:52:03 +00002279 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002280do_none:
2281 return status;
2282}
2283
Sathya Perla5fb379e2009-06-18 00:02:59 +00002284static int be_clear(struct be_adapter *adapter)
2285{
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002286 if (be_physfn(adapter))
2287 be_vf_eth_addr_rem(adapter);
2288
Sathya Perla1a8887d2009-08-17 00:58:41 +00002289 be_mcc_queues_destroy(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002290 be_rx_queues_destroy(adapter);
2291 be_tx_queues_destroy(adapter);
2292
Sathya Perla8788fdc2009-07-27 22:52:03 +00002293 be_cmd_if_destroy(adapter, adapter->if_handle);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002294
Sathya Perla2243e2e2009-11-22 22:02:03 +00002295 /* tell fw we're done with firing cmds */
2296 be_cmd_fw_clean(adapter);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002297 return 0;
2298}
2299
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002300
Ajit Khaparde84517482009-09-04 03:12:16 +00002301#define FW_FILE_HDR_SIGN "ServerEngines Corp. "
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002302static bool be_flash_redboot(struct be_adapter *adapter,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002303 const u8 *p, u32 img_start, int image_size,
2304 int hdr_size)
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002305{
2306 u32 crc_offset;
2307 u8 flashed_crc[4];
2308 int status;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002309
2310 crc_offset = hdr_size + img_start + image_size - 4;
2311
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002312 p += crc_offset;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002313
2314 status = be_cmd_get_flash_crc(adapter, flashed_crc,
Ajit Khapardef510fc62010-03-31 01:47:45 +00002315 (image_size - 4));
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002316 if (status) {
2317 dev_err(&adapter->pdev->dev,
2318 "could not get crc from flash, not flashing redboot\n");
2319 return false;
2320 }
2321
2322 /*update redboot only if crc does not match*/
2323 if (!memcmp(flashed_crc, p, 4))
2324 return false;
2325 else
2326 return true;
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002327}
2328
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002329static int be_flash_data(struct be_adapter *adapter,
Ajit Khaparde84517482009-09-04 03:12:16 +00002330 const struct firmware *fw,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002331 struct be_dma_mem *flash_cmd, int num_of_images)
2332
Ajit Khaparde84517482009-09-04 03:12:16 +00002333{
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002334 int status = 0, i, filehdr_size = 0;
2335 u32 total_bytes = 0, flash_op;
Ajit Khaparde84517482009-09-04 03:12:16 +00002336 int num_bytes;
2337 const u8 *p = fw->data;
2338 struct be_cmd_write_flashrom *req = flash_cmd->va;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002339 struct flash_comp *pflashcomp;
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002340 int num_comp;
Ajit Khaparde84517482009-09-04 03:12:16 +00002341
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002342 struct flash_comp gen3_flash_types[9] = {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002343 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, IMG_TYPE_ISCSI_ACTIVE,
2344 FLASH_IMAGE_MAX_SIZE_g3},
2345 { FLASH_REDBOOT_START_g3, IMG_TYPE_REDBOOT,
2346 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3},
2347 { FLASH_iSCSI_BIOS_START_g3, IMG_TYPE_BIOS,
2348 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2349 { FLASH_PXE_BIOS_START_g3, IMG_TYPE_PXE_BIOS,
2350 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2351 { FLASH_FCoE_BIOS_START_g3, IMG_TYPE_FCOE_BIOS,
2352 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2353 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, IMG_TYPE_ISCSI_BACKUP,
2354 FLASH_IMAGE_MAX_SIZE_g3},
2355 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, IMG_TYPE_FCOE_FW_ACTIVE,
2356 FLASH_IMAGE_MAX_SIZE_g3},
2357 { FLASH_FCoE_BACKUP_IMAGE_START_g3, IMG_TYPE_FCOE_FW_BACKUP,
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002358 FLASH_IMAGE_MAX_SIZE_g3},
2359 { FLASH_NCSI_START_g3, IMG_TYPE_NCSI_FW,
2360 FLASH_NCSI_IMAGE_MAX_SIZE_g3}
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002361 };
2362 struct flash_comp gen2_flash_types[8] = {
2363 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, IMG_TYPE_ISCSI_ACTIVE,
2364 FLASH_IMAGE_MAX_SIZE_g2},
2365 { FLASH_REDBOOT_START_g2, IMG_TYPE_REDBOOT,
2366 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2},
2367 { FLASH_iSCSI_BIOS_START_g2, IMG_TYPE_BIOS,
2368 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2369 { FLASH_PXE_BIOS_START_g2, IMG_TYPE_PXE_BIOS,
2370 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2371 { FLASH_FCoE_BIOS_START_g2, IMG_TYPE_FCOE_BIOS,
2372 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2373 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, IMG_TYPE_ISCSI_BACKUP,
2374 FLASH_IMAGE_MAX_SIZE_g2},
2375 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, IMG_TYPE_FCOE_FW_ACTIVE,
2376 FLASH_IMAGE_MAX_SIZE_g2},
2377 { FLASH_FCoE_BACKUP_IMAGE_START_g2, IMG_TYPE_FCOE_FW_BACKUP,
2378 FLASH_IMAGE_MAX_SIZE_g2}
2379 };
2380
2381 if (adapter->generation == BE_GEN3) {
2382 pflashcomp = gen3_flash_types;
2383 filehdr_size = sizeof(struct flash_file_hdr_g3);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002384 num_comp = 9;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002385 } else {
2386 pflashcomp = gen2_flash_types;
2387 filehdr_size = sizeof(struct flash_file_hdr_g2);
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002388 num_comp = 8;
Ajit Khaparde84517482009-09-04 03:12:16 +00002389 }
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002390 for (i = 0; i < num_comp; i++) {
2391 if ((pflashcomp[i].optype == IMG_TYPE_NCSI_FW) &&
2392 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
2393 continue;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002394 if ((pflashcomp[i].optype == IMG_TYPE_REDBOOT) &&
2395 (!be_flash_redboot(adapter, fw->data,
2396 pflashcomp[i].offset, pflashcomp[i].size,
2397 filehdr_size)))
2398 continue;
2399 p = fw->data;
2400 p += filehdr_size + pflashcomp[i].offset
2401 + (num_of_images * sizeof(struct image_hdr));
2402 if (p + pflashcomp[i].size > fw->data + fw->size)
Ajit Khaparde84517482009-09-04 03:12:16 +00002403 return -1;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002404 total_bytes = pflashcomp[i].size;
2405 while (total_bytes) {
2406 if (total_bytes > 32*1024)
2407 num_bytes = 32*1024;
2408 else
2409 num_bytes = total_bytes;
2410 total_bytes -= num_bytes;
Ajit Khaparde84517482009-09-04 03:12:16 +00002411
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002412 if (!total_bytes)
2413 flash_op = FLASHROM_OPER_FLASH;
2414 else
2415 flash_op = FLASHROM_OPER_SAVE;
2416 memcpy(req->params.data_buf, p, num_bytes);
2417 p += num_bytes;
2418 status = be_cmd_write_flashrom(adapter, flash_cmd,
2419 pflashcomp[i].optype, flash_op, num_bytes);
2420 if (status) {
2421 dev_err(&adapter->pdev->dev,
2422 "cmd to write to flash rom failed.\n");
2423 return -1;
2424 }
2425 yield();
Ajit Khaparde84517482009-09-04 03:12:16 +00002426 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002427 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002428 return 0;
2429}
2430
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002431static int get_ufigen_type(struct flash_file_hdr_g2 *fhdr)
2432{
2433 if (fhdr == NULL)
2434 return 0;
2435 if (fhdr->build[0] == '3')
2436 return BE_GEN3;
2437 else if (fhdr->build[0] == '2')
2438 return BE_GEN2;
2439 else
2440 return 0;
2441}
2442
Ajit Khaparde84517482009-09-04 03:12:16 +00002443int be_load_fw(struct be_adapter *adapter, u8 *func)
2444{
2445 char fw_file[ETHTOOL_FLASH_MAX_FILENAME];
2446 const struct firmware *fw;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002447 struct flash_file_hdr_g2 *fhdr;
2448 struct flash_file_hdr_g3 *fhdr3;
2449 struct image_hdr *img_hdr_ptr = NULL;
Ajit Khaparde84517482009-09-04 03:12:16 +00002450 struct be_dma_mem flash_cmd;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002451 int status, i = 0, num_imgs = 0;
Ajit Khaparde84517482009-09-04 03:12:16 +00002452 const u8 *p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002453
Ajit Khaparde84517482009-09-04 03:12:16 +00002454 strcpy(fw_file, func);
2455
2456 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
2457 if (status)
2458 goto fw_exit;
2459
2460 p = fw->data;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002461 fhdr = (struct flash_file_hdr_g2 *) p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002462 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
2463
Ajit Khaparde84517482009-09-04 03:12:16 +00002464 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
2465 flash_cmd.va = pci_alloc_consistent(adapter->pdev, flash_cmd.size,
2466 &flash_cmd.dma);
2467 if (!flash_cmd.va) {
2468 status = -ENOMEM;
2469 dev_err(&adapter->pdev->dev,
2470 "Memory allocation failure while flashing\n");
2471 goto fw_exit;
2472 }
2473
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002474 if ((adapter->generation == BE_GEN3) &&
2475 (get_ufigen_type(fhdr) == BE_GEN3)) {
2476 fhdr3 = (struct flash_file_hdr_g3 *) fw->data;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002477 num_imgs = le32_to_cpu(fhdr3->num_imgs);
2478 for (i = 0; i < num_imgs; i++) {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002479 img_hdr_ptr = (struct image_hdr *) (fw->data +
2480 (sizeof(struct flash_file_hdr_g3) +
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002481 i * sizeof(struct image_hdr)));
2482 if (le32_to_cpu(img_hdr_ptr->imageid) == 1)
2483 status = be_flash_data(adapter, fw, &flash_cmd,
2484 num_imgs);
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002485 }
2486 } else if ((adapter->generation == BE_GEN2) &&
2487 (get_ufigen_type(fhdr) == BE_GEN2)) {
2488 status = be_flash_data(adapter, fw, &flash_cmd, 0);
2489 } else {
2490 dev_err(&adapter->pdev->dev,
2491 "UFI and Interface are not compatible for flashing\n");
2492 status = -1;
Ajit Khaparde84517482009-09-04 03:12:16 +00002493 }
2494
2495 pci_free_consistent(adapter->pdev, flash_cmd.size, flash_cmd.va,
2496 flash_cmd.dma);
2497 if (status) {
2498 dev_err(&adapter->pdev->dev, "Firmware load error\n");
2499 goto fw_exit;
2500 }
2501
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002502 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
Ajit Khaparde84517482009-09-04 03:12:16 +00002503
2504fw_exit:
2505 release_firmware(fw);
2506 return status;
2507}
2508
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002509static struct net_device_ops be_netdev_ops = {
2510 .ndo_open = be_open,
2511 .ndo_stop = be_close,
2512 .ndo_start_xmit = be_xmit,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002513 .ndo_set_rx_mode = be_set_multicast_list,
2514 .ndo_set_mac_address = be_mac_addr_set,
2515 .ndo_change_mtu = be_change_mtu,
2516 .ndo_validate_addr = eth_validate_addr,
2517 .ndo_vlan_rx_register = be_vlan_register,
2518 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
2519 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002520 .ndo_set_vf_mac = be_set_vf_mac,
Ajit Khaparde1da87b72010-07-23 01:51:22 +00002521 .ndo_set_vf_vlan = be_set_vf_vlan,
Ajit Khapardee1d18732010-07-23 01:52:13 +00002522 .ndo_set_vf_tx_rate = be_set_vf_tx_rate,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00002523 .ndo_get_vf_config = be_get_vf_config
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002524};
2525
2526static void be_netdev_init(struct net_device *netdev)
2527{
2528 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3abcded2010-10-03 22:12:27 -07002529 struct be_rx_obj *rxo;
2530 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002531
2532 netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO |
Ajit Khaparde583e3f32009-10-05 02:22:19 +00002533 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_HW_CSUM |
Ajit Khaparde49e4b8472010-06-14 04:56:07 +00002534 NETIF_F_GRO | NETIF_F_TSO6;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002535
Ajit Khaparde51c59872009-11-29 17:54:54 +00002536 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM;
2537
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002538 netdev->flags |= IFF_MULTICAST;
2539
Ajit Khaparde728a9972009-04-13 15:41:22 -07002540 adapter->rx_csum = true;
2541
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002542 /* Default settings for Rx and Tx flow control */
2543 adapter->rx_fc = true;
2544 adapter->tx_fc = true;
2545
Ajit Khapardec190e3c2009-09-04 03:12:29 +00002546 netif_set_gso_max_size(netdev, 65535);
2547
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002548 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
2549
2550 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
2551
Sathya Perla3abcded2010-10-03 22:12:27 -07002552 for_all_rx_queues(adapter, rxo, i)
2553 netif_napi_add(netdev, &rxo->rx_eq.napi, be_poll_rx,
2554 BE_NAPI_WEIGHT);
2555
Sathya Perla5fb379e2009-06-18 00:02:59 +00002556 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002557 BE_NAPI_WEIGHT);
2558
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002559 netif_stop_queue(netdev);
2560}
2561
2562static void be_unmap_pci_bars(struct be_adapter *adapter)
2563{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002564 if (adapter->csr)
2565 iounmap(adapter->csr);
2566 if (adapter->db)
2567 iounmap(adapter->db);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002568 if (adapter->pcicfg && be_physfn(adapter))
Sathya Perla8788fdc2009-07-27 22:52:03 +00002569 iounmap(adapter->pcicfg);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002570}
2571
2572static int be_map_pci_bars(struct be_adapter *adapter)
2573{
2574 u8 __iomem *addr;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002575 int pcicfg_reg, db_reg;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002576
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002577 if (be_physfn(adapter)) {
2578 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
2579 pci_resource_len(adapter->pdev, 2));
2580 if (addr == NULL)
2581 return -ENOMEM;
2582 adapter->csr = addr;
2583 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002584
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002585 if (adapter->generation == BE_GEN2) {
2586 pcicfg_reg = 1;
2587 db_reg = 4;
2588 } else {
2589 pcicfg_reg = 0;
2590 if (be_physfn(adapter))
2591 db_reg = 4;
2592 else
2593 db_reg = 0;
2594 }
2595 addr = ioremap_nocache(pci_resource_start(adapter->pdev, db_reg),
2596 pci_resource_len(adapter->pdev, db_reg));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002597 if (addr == NULL)
2598 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00002599 adapter->db = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002600
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002601 if (be_physfn(adapter)) {
2602 addr = ioremap_nocache(
2603 pci_resource_start(adapter->pdev, pcicfg_reg),
2604 pci_resource_len(adapter->pdev, pcicfg_reg));
2605 if (addr == NULL)
2606 goto pci_map_err;
2607 adapter->pcicfg = addr;
2608 } else
2609 adapter->pcicfg = adapter->db + SRIOV_VF_PCICFG_OFFSET;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002610
2611 return 0;
2612pci_map_err:
2613 be_unmap_pci_bars(adapter);
2614 return -ENOMEM;
2615}
2616
2617
2618static void be_ctrl_cleanup(struct be_adapter *adapter)
2619{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002620 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002621
2622 be_unmap_pci_bars(adapter);
2623
2624 if (mem->va)
2625 pci_free_consistent(adapter->pdev, mem->size,
2626 mem->va, mem->dma);
Sathya Perlae7b909a2009-11-22 22:01:10 +00002627
2628 mem = &adapter->mc_cmd_mem;
2629 if (mem->va)
2630 pci_free_consistent(adapter->pdev, mem->size,
2631 mem->va, mem->dma);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002632}
2633
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002634static int be_ctrl_init(struct be_adapter *adapter)
2635{
Sathya Perla8788fdc2009-07-27 22:52:03 +00002636 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
2637 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002638 struct be_dma_mem *mc_cmd_mem = &adapter->mc_cmd_mem;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002639 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002640
2641 status = be_map_pci_bars(adapter);
2642 if (status)
Sathya Perlae7b909a2009-11-22 22:01:10 +00002643 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002644
2645 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
2646 mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev,
2647 mbox_mem_alloc->size, &mbox_mem_alloc->dma);
2648 if (!mbox_mem_alloc->va) {
Sathya Perlae7b909a2009-11-22 22:01:10 +00002649 status = -ENOMEM;
2650 goto unmap_pci_bars;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002651 }
Sathya Perlae7b909a2009-11-22 22:01:10 +00002652
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002653 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
2654 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
2655 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
2656 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Sathya Perlae7b909a2009-11-22 22:01:10 +00002657
2658 mc_cmd_mem->size = sizeof(struct be_cmd_req_mcast_mac_config);
2659 mc_cmd_mem->va = pci_alloc_consistent(adapter->pdev, mc_cmd_mem->size,
2660 &mc_cmd_mem->dma);
2661 if (mc_cmd_mem->va == NULL) {
2662 status = -ENOMEM;
2663 goto free_mbox;
2664 }
2665 memset(mc_cmd_mem->va, 0, mc_cmd_mem->size);
2666
Sathya Perla8788fdc2009-07-27 22:52:03 +00002667 spin_lock_init(&adapter->mbox_lock);
2668 spin_lock_init(&adapter->mcc_lock);
2669 spin_lock_init(&adapter->mcc_cq_lock);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002670
Sarveshwar Bandidd131e72010-05-25 16:16:32 -07002671 init_completion(&adapter->flash_compl);
Sathya Perlacf588472010-02-14 21:22:01 +00002672 pci_save_state(adapter->pdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002673 return 0;
Sathya Perlae7b909a2009-11-22 22:01:10 +00002674
2675free_mbox:
2676 pci_free_consistent(adapter->pdev, mbox_mem_alloc->size,
2677 mbox_mem_alloc->va, mbox_mem_alloc->dma);
2678
2679unmap_pci_bars:
2680 be_unmap_pci_bars(adapter);
2681
2682done:
2683 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002684}
2685
2686static void be_stats_cleanup(struct be_adapter *adapter)
2687{
Sathya Perla3abcded2010-10-03 22:12:27 -07002688 struct be_dma_mem *cmd = &adapter->stats_cmd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002689
2690 if (cmd->va)
2691 pci_free_consistent(adapter->pdev, cmd->size,
2692 cmd->va, cmd->dma);
2693}
2694
2695static int be_stats_init(struct be_adapter *adapter)
2696{
Sathya Perla3abcded2010-10-03 22:12:27 -07002697 struct be_dma_mem *cmd = &adapter->stats_cmd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002698
2699 cmd->size = sizeof(struct be_cmd_req_get_stats);
2700 cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma);
2701 if (cmd->va == NULL)
2702 return -1;
David S. Millerd291b9a2010-01-28 21:36:21 -08002703 memset(cmd->va, 0, cmd->size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002704 return 0;
2705}
2706
2707static void __devexit be_remove(struct pci_dev *pdev)
2708{
2709 struct be_adapter *adapter = pci_get_drvdata(pdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002710
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002711 if (!adapter)
2712 return;
2713
2714 unregister_netdev(adapter->netdev);
2715
Sathya Perla5fb379e2009-06-18 00:02:59 +00002716 be_clear(adapter);
2717
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002718 be_stats_cleanup(adapter);
2719
2720 be_ctrl_cleanup(adapter);
2721
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002722 be_sriov_disable(adapter);
2723
Sathya Perla8d56ff12009-11-22 22:02:26 +00002724 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002725
2726 pci_set_drvdata(pdev, NULL);
2727 pci_release_regions(pdev);
2728 pci_disable_device(pdev);
2729
2730 free_netdev(adapter->netdev);
2731}
2732
Sathya Perla2243e2e2009-11-22 22:02:03 +00002733static int be_get_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002734{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002735 int status;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002736 u8 mac[ETH_ALEN];
Sathya Perla43a04fdc2009-10-14 20:21:17 +00002737
Sathya Perla8788fdc2009-07-27 22:52:03 +00002738 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002739 if (status)
2740 return status;
2741
Sathya Perla3abcded2010-10-03 22:12:27 -07002742 status = be_cmd_query_fw_cfg(adapter, &adapter->port_num,
2743 &adapter->function_mode, &adapter->function_caps);
Sathya Perla2243e2e2009-11-22 22:02:03 +00002744 if (status)
2745 return status;
2746
2747 memset(mac, 0, ETH_ALEN);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002748
2749 if (be_physfn(adapter)) {
2750 status = be_cmd_mac_addr_query(adapter, mac,
Sathya Perla2243e2e2009-11-22 22:02:03 +00002751 MAC_ADDRESS_TYPE_NETWORK, true /*permanent */, 0);
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002752
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002753 if (status)
2754 return status;
Ajit Khapardeca9e4982009-11-29 17:56:26 +00002755
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002756 if (!is_valid_ether_addr(mac))
2757 return -EADDRNOTAVAIL;
2758
2759 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2760 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2761 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002762
Ajit Khaparde3486be22010-07-23 02:04:54 +00002763 if (adapter->function_mode & 0x400)
Ajit Khaparde82903e42010-02-09 01:34:57 +00002764 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED/4;
2765 else
2766 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED;
2767
Sathya Perla2243e2e2009-11-22 22:02:03 +00002768 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002769}
2770
2771static int __devinit be_probe(struct pci_dev *pdev,
2772 const struct pci_device_id *pdev_id)
2773{
2774 int status = 0;
2775 struct be_adapter *adapter;
2776 struct net_device *netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002777
2778 status = pci_enable_device(pdev);
2779 if (status)
2780 goto do_none;
2781
2782 status = pci_request_regions(pdev, DRV_NAME);
2783 if (status)
2784 goto disable_dev;
2785 pci_set_master(pdev);
2786
2787 netdev = alloc_etherdev(sizeof(struct be_adapter));
2788 if (netdev == NULL) {
2789 status = -ENOMEM;
2790 goto rel_reg;
2791 }
2792 adapter = netdev_priv(netdev);
Ajit Khaparde7b139c82010-01-27 21:56:44 +00002793
2794 switch (pdev->device) {
2795 case BE_DEVICE_ID1:
2796 case OC_DEVICE_ID1:
2797 adapter->generation = BE_GEN2;
2798 break;
2799 case BE_DEVICE_ID2:
2800 case OC_DEVICE_ID2:
2801 adapter->generation = BE_GEN3;
2802 break;
2803 default:
2804 adapter->generation = 0;
2805 }
2806
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002807 adapter->pdev = pdev;
2808 pci_set_drvdata(pdev, adapter);
2809 adapter->netdev = netdev;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002810 SET_NETDEV_DEV(netdev, &pdev->dev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002811
Yang Hongyange9304382009-04-13 14:40:14 -07002812 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002813 if (!status) {
2814 netdev->features |= NETIF_F_HIGHDMA;
2815 } else {
Yang Hongyange9304382009-04-13 14:40:14 -07002816 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002817 if (status) {
2818 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
2819 goto free_netdev;
2820 }
2821 }
2822
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002823 be_sriov_enable(adapter);
2824
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002825 status = be_ctrl_init(adapter);
2826 if (status)
2827 goto free_netdev;
2828
Sathya Perla2243e2e2009-11-22 22:02:03 +00002829 /* sync up with fw's ready state */
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002830 if (be_physfn(adapter)) {
2831 status = be_cmd_POST(adapter);
2832 if (status)
2833 goto ctrl_clean;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002834 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00002835
2836 /* tell fw we're ready to fire cmds */
2837 status = be_cmd_fw_init(adapter);
2838 if (status)
2839 goto ctrl_clean;
2840
Sarveshwar Bandi556ae192010-05-24 18:38:25 -07002841 if (be_physfn(adapter)) {
2842 status = be_cmd_reset_function(adapter);
2843 if (status)
2844 goto ctrl_clean;
2845 }
2846
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002847 status = be_stats_init(adapter);
2848 if (status)
2849 goto ctrl_clean;
2850
Sathya Perla2243e2e2009-11-22 22:02:03 +00002851 status = be_get_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002852 if (status)
2853 goto stats_clean;
2854
Sathya Perla3abcded2010-10-03 22:12:27 -07002855 be_msix_enable(adapter);
2856
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002857 INIT_DELAYED_WORK(&adapter->work, be_worker);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002858
Sathya Perla5fb379e2009-06-18 00:02:59 +00002859 status = be_setup(adapter);
2860 if (status)
Sathya Perla3abcded2010-10-03 22:12:27 -07002861 goto msix_disable;
Sathya Perla2243e2e2009-11-22 22:02:03 +00002862
Sathya Perla3abcded2010-10-03 22:12:27 -07002863 be_netdev_init(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002864 status = register_netdev(netdev);
2865 if (status != 0)
Sathya Perla5fb379e2009-06-18 00:02:59 +00002866 goto unsetup;
Somnath Kotur63a76942010-10-25 01:11:10 +00002867 netif_carrier_off(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002868
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002869 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002870 return 0;
2871
Sathya Perla5fb379e2009-06-18 00:02:59 +00002872unsetup:
2873 be_clear(adapter);
Sathya Perla3abcded2010-10-03 22:12:27 -07002874msix_disable:
2875 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002876stats_clean:
2877 be_stats_cleanup(adapter);
2878ctrl_clean:
2879 be_ctrl_cleanup(adapter);
2880free_netdev:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002881 be_sriov_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002882 free_netdev(adapter->netdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00002883 pci_set_drvdata(pdev, NULL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002884rel_reg:
2885 pci_release_regions(pdev);
2886disable_dev:
2887 pci_disable_device(pdev);
2888do_none:
Ajit Khapardec4ca2372009-05-18 15:38:55 -07002889 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002890 return status;
2891}
2892
2893static int be_suspend(struct pci_dev *pdev, pm_message_t state)
2894{
2895 struct be_adapter *adapter = pci_get_drvdata(pdev);
2896 struct net_device *netdev = adapter->netdev;
2897
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002898 if (adapter->wol)
2899 be_setup_wol(adapter, true);
2900
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002901 netif_device_detach(netdev);
2902 if (netif_running(netdev)) {
2903 rtnl_lock();
2904 be_close(netdev);
2905 rtnl_unlock();
2906 }
Ajit Khaparde9e90c962009-11-06 02:06:59 +00002907 be_cmd_get_flow_control(adapter, &adapter->tx_fc, &adapter->rx_fc);
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002908 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002909
2910 pci_save_state(pdev);
2911 pci_disable_device(pdev);
2912 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2913 return 0;
2914}
2915
2916static int be_resume(struct pci_dev *pdev)
2917{
2918 int status = 0;
2919 struct be_adapter *adapter = pci_get_drvdata(pdev);
2920 struct net_device *netdev = adapter->netdev;
2921
2922 netif_device_detach(netdev);
2923
2924 status = pci_enable_device(pdev);
2925 if (status)
2926 return status;
2927
2928 pci_set_power_state(pdev, 0);
2929 pci_restore_state(pdev);
2930
Sathya Perla2243e2e2009-11-22 22:02:03 +00002931 /* tell fw we're ready to fire cmds */
2932 status = be_cmd_fw_init(adapter);
2933 if (status)
2934 return status;
2935
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00002936 be_setup(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002937 if (netif_running(netdev)) {
2938 rtnl_lock();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002939 be_open(netdev);
2940 rtnl_unlock();
2941 }
2942 netif_device_attach(netdev);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002943
2944 if (adapter->wol)
2945 be_setup_wol(adapter, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002946 return 0;
2947}
2948
Sathya Perla82456b02010-02-17 01:35:37 +00002949/*
2950 * An FLR will stop BE from DMAing any data.
2951 */
2952static void be_shutdown(struct pci_dev *pdev)
2953{
2954 struct be_adapter *adapter = pci_get_drvdata(pdev);
2955 struct net_device *netdev = adapter->netdev;
2956
2957 netif_device_detach(netdev);
2958
2959 be_cmd_reset_function(adapter);
2960
2961 if (adapter->wol)
2962 be_setup_wol(adapter, true);
2963
2964 pci_disable_device(pdev);
Sathya Perla82456b02010-02-17 01:35:37 +00002965}
2966
Sathya Perlacf588472010-02-14 21:22:01 +00002967static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
2968 pci_channel_state_t state)
2969{
2970 struct be_adapter *adapter = pci_get_drvdata(pdev);
2971 struct net_device *netdev = adapter->netdev;
2972
2973 dev_err(&adapter->pdev->dev, "EEH error detected\n");
2974
2975 adapter->eeh_err = true;
2976
2977 netif_device_detach(netdev);
2978
2979 if (netif_running(netdev)) {
2980 rtnl_lock();
2981 be_close(netdev);
2982 rtnl_unlock();
2983 }
2984 be_clear(adapter);
2985
2986 if (state == pci_channel_io_perm_failure)
2987 return PCI_ERS_RESULT_DISCONNECT;
2988
2989 pci_disable_device(pdev);
2990
2991 return PCI_ERS_RESULT_NEED_RESET;
2992}
2993
2994static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
2995{
2996 struct be_adapter *adapter = pci_get_drvdata(pdev);
2997 int status;
2998
2999 dev_info(&adapter->pdev->dev, "EEH reset\n");
3000 adapter->eeh_err = false;
3001
3002 status = pci_enable_device(pdev);
3003 if (status)
3004 return PCI_ERS_RESULT_DISCONNECT;
3005
3006 pci_set_master(pdev);
3007 pci_set_power_state(pdev, 0);
3008 pci_restore_state(pdev);
3009
3010 /* Check if card is ok and fw is ready */
3011 status = be_cmd_POST(adapter);
3012 if (status)
3013 return PCI_ERS_RESULT_DISCONNECT;
3014
3015 return PCI_ERS_RESULT_RECOVERED;
3016}
3017
3018static void be_eeh_resume(struct pci_dev *pdev)
3019{
3020 int status = 0;
3021 struct be_adapter *adapter = pci_get_drvdata(pdev);
3022 struct net_device *netdev = adapter->netdev;
3023
3024 dev_info(&adapter->pdev->dev, "EEH resume\n");
3025
3026 pci_save_state(pdev);
3027
3028 /* tell fw we're ready to fire cmds */
3029 status = be_cmd_fw_init(adapter);
3030 if (status)
3031 goto err;
3032
3033 status = be_setup(adapter);
3034 if (status)
3035 goto err;
3036
3037 if (netif_running(netdev)) {
3038 status = be_open(netdev);
3039 if (status)
3040 goto err;
3041 }
3042 netif_device_attach(netdev);
3043 return;
3044err:
3045 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
Sathya Perlacf588472010-02-14 21:22:01 +00003046}
3047
3048static struct pci_error_handlers be_eeh_handlers = {
3049 .error_detected = be_eeh_err_detected,
3050 .slot_reset = be_eeh_reset,
3051 .resume = be_eeh_resume,
3052};
3053
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003054static struct pci_driver be_driver = {
3055 .name = DRV_NAME,
3056 .id_table = be_dev_ids,
3057 .probe = be_probe,
3058 .remove = be_remove,
3059 .suspend = be_suspend,
Sathya Perlacf588472010-02-14 21:22:01 +00003060 .resume = be_resume,
Sathya Perla82456b02010-02-17 01:35:37 +00003061 .shutdown = be_shutdown,
Sathya Perlacf588472010-02-14 21:22:01 +00003062 .err_handler = &be_eeh_handlers
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003063};
3064
3065static int __init be_init_module(void)
3066{
Joe Perches8e95a202009-12-03 07:58:21 +00003067 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
3068 rx_frag_size != 2048) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003069 printk(KERN_WARNING DRV_NAME
3070 " : Module param rx_frag_size must be 2048/4096/8192."
3071 " Using 2048\n");
3072 rx_frag_size = 2048;
3073 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003074
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003075 if (num_vfs > 32) {
3076 printk(KERN_WARNING DRV_NAME
3077 " : Module param num_vfs must not be greater than 32."
3078 "Using 32\n");
3079 num_vfs = 32;
3080 }
3081
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003082 return pci_register_driver(&be_driver);
3083}
3084module_init(be_init_module);
3085
3086static void __exit be_exit_module(void)
3087{
3088 pci_unregister_driver(&be_driver);
3089}
3090module_exit(be_exit_module);