blob: 0e97b6d01c79b6f0bd5a5f6d7c88684ad06926ed [file] [log] [blame]
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001/*
Ajit Khaparded2145cd2011-03-16 08:20:46 +00002 * Copyright (C) 2005 - 2011 Emulex
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:
Ajit Khaparded2145cd2011-03-16 08:20:46 +000011 * linux-drivers@emulex.com
Sathya Perla6b7c5b92009-03-11 23:32:03 -070012 *
Ajit Khaparded2145cd2011-03-16 08:20:46 +000013 * Emulex
14 * 3333 Susan Street
15 * Costa Mesa, CA 92626
Sathya Perla6b7c5b92009-03-11 23:32:03 -070016 */
17
Paul Gortmaker70c71602011-05-22 16:47:17 -040018#include <linux/prefetch.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040019#include <linux/module.h>
Sathya Perla6b7c5b92009-03-11 23:32:03 -070020#include "be.h"
Sathya Perla8788fdc2009-07-27 22:52:03 +000021#include "be_cmds.h"
Stephen Hemminger65f71b82009-03-27 00:25:24 -070022#include <asm/div64.h>
Sathya Perla6b7c5b92009-03-11 23:32:03 -070023
24MODULE_VERSION(DRV_VER);
25MODULE_DEVICE_TABLE(pci, be_dev_ids);
26MODULE_DESCRIPTION(DRV_DESC " " DRV_VER);
27MODULE_AUTHOR("ServerEngines Corporation");
28MODULE_LICENSE("GPL");
29
Sathya Perla2e588f82011-03-11 02:49:26 +000030static ushort rx_frag_size = 2048;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +000031static unsigned int num_vfs;
Sathya Perla2e588f82011-03-11 02:49:26 +000032module_param(rx_frag_size, ushort, S_IRUGO);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +000033module_param(num_vfs, uint, S_IRUGO);
Sathya Perla6b7c5b92009-03-11 23:32:03 -070034MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data.");
Sarveshwar Bandiba343c72010-03-31 02:56:12 +000035MODULE_PARM_DESC(num_vfs, "Number of PCI VFs to initialize");
Sathya Perla6b7c5b92009-03-11 23:32:03 -070036
Sathya Perla6b7c5b92009-03-11 23:32:03 -070037static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
Ajit Khapardec4ca2372009-05-18 15:38:55 -070038 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Ajit Khaparde59fd5d82009-10-29 01:11:06 -070039 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Ajit Khapardec4ca2372009-05-18 15:38:55 -070040 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
41 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
Sathya Perlafe6d2a32010-11-21 23:25:50 +000042 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID3)},
Mammatha Edhala12f4d0a2011-05-18 03:26:22 +000043 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID4)},
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 */
Joe Perches42c8b112011-07-09 02:56:56 -070048static const char * const ue_status_low_desc[] = {
Ajit Khaparde7c185272010-07-29 06:16:33 +000049 "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 */
Joe Perches42c8b112011-07-09 02:56:56 -070083static const char * const ue_status_hi_desc[] = {
Ajit Khaparde7c185272010-07-29 06:16:33 +000084 "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",
Joe Perches42c8b112011-07-09 02:56:56 -0700107 "NETC",
Ajit Khaparde7c185272010-07-29 06:16:33 +0000108 "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 Perla752961a2011-10-24 02:45:03 +0000118/* Is BE in a multi-channel mode */
119static inline bool be_is_mc(struct be_adapter *adapter) {
120 return (adapter->function_mode & FLEX10_MODE ||
121 adapter->function_mode & VNIC_MODE ||
122 adapter->function_mode & UMC_ENABLED);
123}
124
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700125static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
126{
127 struct be_dma_mem *mem = &q->dma_mem;
128 if (mem->va)
Ivan Vecera2b7bceb2011-02-02 08:05:12 +0000129 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
130 mem->dma);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700131}
132
133static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
134 u16 len, u16 entry_size)
135{
136 struct be_dma_mem *mem = &q->dma_mem;
137
138 memset(q, 0, sizeof(*q));
139 q->len = len;
140 q->entry_size = entry_size;
141 mem->size = len * entry_size;
Ivan Vecera2b7bceb2011-02-02 08:05:12 +0000142 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size, &mem->dma,
143 GFP_KERNEL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700144 if (!mem->va)
145 return -1;
146 memset(mem->va, 0, mem->size);
147 return 0;
148}
149
Sathya Perla8788fdc2009-07-27 22:52:03 +0000150static void be_intr_set(struct be_adapter *adapter, bool enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700151{
Sathya Perladb3ea782011-08-22 19:41:52 +0000152 u32 reg, enabled;
Sathya Perla5f0b8492009-07-27 22:52:56 +0000153
Sathya Perlacf588472010-02-14 21:22:01 +0000154 if (adapter->eeh_err)
155 return;
156
Sathya Perladb3ea782011-08-22 19:41:52 +0000157 pci_read_config_dword(adapter->pdev, PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET,
158 &reg);
159 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
160
Sathya Perla5f0b8492009-07-27 22:52:56 +0000161 if (!enabled && enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700162 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +0000163 else if (enabled && !enable)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700164 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Sathya Perla5f0b8492009-07-27 22:52:56 +0000165 else
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700166 return;
Sathya Perla5f0b8492009-07-27 22:52:56 +0000167
Sathya Perladb3ea782011-08-22 19:41:52 +0000168 pci_write_config_dword(adapter->pdev,
169 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET, reg);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700170}
171
Sathya Perla8788fdc2009-07-27 22:52:03 +0000172static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700173{
174 u32 val = 0;
175 val |= qid & DB_RQ_RING_ID_MASK;
176 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
Sathya Perlaf3eb62d2010-06-29 00:11:17 +0000177
178 wmb();
Sathya Perla8788fdc2009-07-27 22:52:03 +0000179 iowrite32(val, adapter->db + DB_RQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700180}
181
Sathya Perla8788fdc2009-07-27 22:52:03 +0000182static void be_txq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700183{
184 u32 val = 0;
185 val |= qid & DB_TXULP_RING_ID_MASK;
186 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
Sathya Perlaf3eb62d2010-06-29 00:11:17 +0000187
188 wmb();
Sathya Perla8788fdc2009-07-27 22:52:03 +0000189 iowrite32(val, adapter->db + DB_TXULP1_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700190}
191
Sathya Perla8788fdc2009-07-27 22:52:03 +0000192static void be_eq_notify(struct be_adapter *adapter, u16 qid,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700193 bool arm, bool clear_int, u16 num_popped)
194{
195 u32 val = 0;
196 val |= qid & DB_EQ_RING_ID_MASK;
Sathya Perlafe6d2a32010-11-21 23:25:50 +0000197 val |= ((qid & DB_EQ_RING_ID_EXT_MASK) <<
198 DB_EQ_RING_ID_EXT_MASK_SHIFT);
Sathya Perlacf588472010-02-14 21:22:01 +0000199
200 if (adapter->eeh_err)
201 return;
202
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700203 if (arm)
204 val |= 1 << DB_EQ_REARM_SHIFT;
205 if (clear_int)
206 val |= 1 << DB_EQ_CLR_SHIFT;
207 val |= 1 << DB_EQ_EVNT_SHIFT;
208 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000209 iowrite32(val, adapter->db + DB_EQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700210}
211
Sathya Perla8788fdc2009-07-27 22:52:03 +0000212void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700213{
214 u32 val = 0;
215 val |= qid & DB_CQ_RING_ID_MASK;
Sathya Perlafe6d2a32010-11-21 23:25:50 +0000216 val |= ((qid & DB_CQ_RING_ID_EXT_MASK) <<
217 DB_CQ_RING_ID_EXT_MASK_SHIFT);
Sathya Perlacf588472010-02-14 21:22:01 +0000218
219 if (adapter->eeh_err)
220 return;
221
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700222 if (arm)
223 val |= 1 << DB_CQ_REARM_SHIFT;
224 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
Sathya Perla8788fdc2009-07-27 22:52:03 +0000225 iowrite32(val, adapter->db + DB_CQ_OFFSET);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700226}
227
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700228static int be_mac_addr_set(struct net_device *netdev, void *p)
229{
230 struct be_adapter *adapter = netdev_priv(netdev);
231 struct sockaddr *addr = p;
232 int status = 0;
Somnath Koture3a7ae22011-10-27 07:14:05 +0000233 u8 current_mac[ETH_ALEN];
234 u32 pmac_id = adapter->pmac_id;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700235
Ajit Khapardeca9e4982009-11-29 17:56:26 +0000236 if (!is_valid_ether_addr(addr->sa_data))
237 return -EADDRNOTAVAIL;
238
Somnath Koture3a7ae22011-10-27 07:14:05 +0000239 status = be_cmd_mac_addr_query(adapter, current_mac,
240 MAC_ADDRESS_TYPE_NETWORK, false, adapter->if_handle);
Sathya Perlaa65027e2009-08-17 00:58:04 +0000241 if (status)
Somnath Koture3a7ae22011-10-27 07:14:05 +0000242 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700243
Somnath Koture3a7ae22011-10-27 07:14:05 +0000244 if (memcmp(addr->sa_data, current_mac, ETH_ALEN)) {
245 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
Ajit Khapardef8617e02011-02-11 13:36:37 +0000246 adapter->if_handle, &adapter->pmac_id, 0);
Somnath Koture3a7ae22011-10-27 07:14:05 +0000247 if (status)
248 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700249
Somnath Koture3a7ae22011-10-27 07:14:05 +0000250 be_cmd_pmac_del(adapter, adapter->if_handle, pmac_id, 0);
251 }
252 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
253 return 0;
254err:
255 dev_err(&adapter->pdev->dev, "MAC %pM set Failed\n", addr->sa_data);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700256 return status;
257}
258
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000259static void populate_be2_stats(struct be_adapter *adapter)
260{
Sathya Perlaac124ff2011-07-25 19:10:14 +0000261 struct be_hw_stats_v0 *hw_stats = hw_stats_from_cmd(adapter);
262 struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
263 struct be_rxf_stats_v0 *rxf_stats = &hw_stats->rxf;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000264 struct be_port_rxf_stats_v0 *port_stats =
Sathya Perlaac124ff2011-07-25 19:10:14 +0000265 &rxf_stats->port[adapter->port_num];
266 struct be_drv_stats *drvs = &adapter->drv_stats;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000267
Sathya Perlaac124ff2011-07-25 19:10:14 +0000268 be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000269 drvs->rx_pause_frames = port_stats->rx_pause_frames;
270 drvs->rx_crc_errors = port_stats->rx_crc_errors;
271 drvs->rx_control_frames = port_stats->rx_control_frames;
272 drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
273 drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
274 drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
275 drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
276 drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
277 drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
278 drvs->rxpp_fifo_overflow_drop = port_stats->rx_fifo_overflow;
279 drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
280 drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
281 drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
282 drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000283 drvs->rx_input_fifo_overflow_drop = port_stats->rx_input_fifo_overflow;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000284 drvs->rx_dropped_header_too_small =
285 port_stats->rx_dropped_header_too_small;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000286 drvs->rx_address_match_errors = port_stats->rx_address_match_errors;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000287 drvs->rx_alignment_symbol_errors =
288 port_stats->rx_alignment_symbol_errors;
289
290 drvs->tx_pauseframes = port_stats->tx_pauseframes;
291 drvs->tx_controlframes = port_stats->tx_controlframes;
292
293 if (adapter->port_num)
Sathya Perlaac124ff2011-07-25 19:10:14 +0000294 drvs->jabber_events = rxf_stats->port1_jabber_events;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000295 else
Sathya Perlaac124ff2011-07-25 19:10:14 +0000296 drvs->jabber_events = rxf_stats->port0_jabber_events;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000297 drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
298 drvs->rx_drops_no_txpb = rxf_stats->rx_drops_no_txpb;
299 drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
300 drvs->rx_drops_invalid_ring = rxf_stats->rx_drops_invalid_ring;
301 drvs->forwarded_packets = rxf_stats->forwarded_packets;
302 drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000303 drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
304 drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000305 adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
306}
307
308static void populate_be3_stats(struct be_adapter *adapter)
309{
Sathya Perlaac124ff2011-07-25 19:10:14 +0000310 struct be_hw_stats_v1 *hw_stats = hw_stats_from_cmd(adapter);
311 struct be_pmem_stats *pmem_sts = &hw_stats->pmem;
312 struct be_rxf_stats_v1 *rxf_stats = &hw_stats->rxf;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000313 struct be_port_rxf_stats_v1 *port_stats =
Sathya Perlaac124ff2011-07-25 19:10:14 +0000314 &rxf_stats->port[adapter->port_num];
315 struct be_drv_stats *drvs = &adapter->drv_stats;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000316
Sathya Perlaac124ff2011-07-25 19:10:14 +0000317 be_dws_le_to_cpu(hw_stats, sizeof(*hw_stats));
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000318 drvs->rx_pause_frames = port_stats->rx_pause_frames;
319 drvs->rx_crc_errors = port_stats->rx_crc_errors;
320 drvs->rx_control_frames = port_stats->rx_control_frames;
321 drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
322 drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
323 drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
324 drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
325 drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
326 drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
327 drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
328 drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
329 drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
330 drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
331 drvs->rx_dropped_header_too_small =
332 port_stats->rx_dropped_header_too_small;
333 drvs->rx_input_fifo_overflow_drop =
334 port_stats->rx_input_fifo_overflow_drop;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000335 drvs->rx_address_match_errors = port_stats->rx_address_match_errors;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000336 drvs->rx_alignment_symbol_errors =
337 port_stats->rx_alignment_symbol_errors;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000338 drvs->rxpp_fifo_overflow_drop = port_stats->rxpp_fifo_overflow_drop;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000339 drvs->tx_pauseframes = port_stats->tx_pauseframes;
340 drvs->tx_controlframes = port_stats->tx_controlframes;
341 drvs->jabber_events = port_stats->jabber_events;
342 drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
343 drvs->rx_drops_no_txpb = rxf_stats->rx_drops_no_txpb;
344 drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
345 drvs->rx_drops_invalid_ring = rxf_stats->rx_drops_invalid_ring;
346 drvs->forwarded_packets = rxf_stats->forwarded_packets;
347 drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000348 drvs->rx_drops_no_tpre_descr = rxf_stats->rx_drops_no_tpre_descr;
349 drvs->rx_drops_too_many_frags = rxf_stats->rx_drops_too_many_frags;
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000350 adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
351}
352
Selvin Xavier005d5692011-05-16 07:36:35 +0000353static void populate_lancer_stats(struct be_adapter *adapter)
354{
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000355
Selvin Xavier005d5692011-05-16 07:36:35 +0000356 struct be_drv_stats *drvs = &adapter->drv_stats;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000357 struct lancer_pport_stats *pport_stats =
358 pport_stats_from_cmd(adapter);
359
360 be_dws_le_to_cpu(pport_stats, sizeof(*pport_stats));
361 drvs->rx_pause_frames = pport_stats->rx_pause_frames_lo;
362 drvs->rx_crc_errors = pport_stats->rx_crc_errors_lo;
363 drvs->rx_control_frames = pport_stats->rx_control_frames_lo;
Selvin Xavier005d5692011-05-16 07:36:35 +0000364 drvs->rx_in_range_errors = pport_stats->rx_in_range_errors;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000365 drvs->rx_frame_too_long = pport_stats->rx_frames_too_long_lo;
Selvin Xavier005d5692011-05-16 07:36:35 +0000366 drvs->rx_dropped_runt = pport_stats->rx_dropped_runt;
367 drvs->rx_ip_checksum_errs = pport_stats->rx_ip_checksum_errors;
368 drvs->rx_tcp_checksum_errs = pport_stats->rx_tcp_checksum_errors;
369 drvs->rx_udp_checksum_errs = pport_stats->rx_udp_checksum_errors;
370 drvs->rx_dropped_tcp_length =
371 pport_stats->rx_dropped_invalid_tcp_length;
372 drvs->rx_dropped_too_small = pport_stats->rx_dropped_too_small;
373 drvs->rx_dropped_too_short = pport_stats->rx_dropped_too_short;
374 drvs->rx_out_range_errors = pport_stats->rx_out_of_range_errors;
375 drvs->rx_dropped_header_too_small =
376 pport_stats->rx_dropped_header_too_small;
377 drvs->rx_input_fifo_overflow_drop = pport_stats->rx_fifo_overflow;
378 drvs->rx_address_match_errors = pport_stats->rx_address_match_errors;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000379 drvs->rx_alignment_symbol_errors = pport_stats->rx_symbol_errors_lo;
Selvin Xavier005d5692011-05-16 07:36:35 +0000380 drvs->rxpp_fifo_overflow_drop = pport_stats->rx_fifo_overflow;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000381 drvs->tx_pauseframes = pport_stats->tx_pause_frames_lo;
382 drvs->tx_controlframes = pport_stats->tx_control_frames_lo;
Selvin Xavier005d5692011-05-16 07:36:35 +0000383 drvs->jabber_events = pport_stats->rx_jabbers;
Selvin Xavier005d5692011-05-16 07:36:35 +0000384 drvs->rx_drops_invalid_ring = pport_stats->rx_drops_invalid_queue;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000385 drvs->forwarded_packets = pport_stats->num_forwards_lo;
386 drvs->rx_drops_mtu = pport_stats->rx_drops_mtu_lo;
Selvin Xavier005d5692011-05-16 07:36:35 +0000387 drvs->rx_drops_too_many_frags =
Sathya Perlaac124ff2011-07-25 19:10:14 +0000388 pport_stats->rx_drops_too_many_frags_lo;
Selvin Xavier005d5692011-05-16 07:36:35 +0000389}
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000390
Sathya Perla09c1c682011-08-22 19:41:53 +0000391static void accumulate_16bit_val(u32 *acc, u16 val)
392{
393#define lo(x) (x & 0xFFFF)
394#define hi(x) (x & 0xFFFF0000)
395 bool wrapped = val < lo(*acc);
396 u32 newacc = hi(*acc) + val;
397
398 if (wrapped)
399 newacc += 65536;
400 ACCESS_ONCE(*acc) = newacc;
401}
402
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000403void be_parse_stats(struct be_adapter *adapter)
404{
Sathya Perlaac124ff2011-07-25 19:10:14 +0000405 struct be_erx_stats_v1 *erx = be_erx_stats_from_cmd(adapter);
406 struct be_rx_obj *rxo;
407 int i;
408
Selvin Xavier005d5692011-05-16 07:36:35 +0000409 if (adapter->generation == BE_GEN3) {
410 if (lancer_chip(adapter))
411 populate_lancer_stats(adapter);
412 else
413 populate_be3_stats(adapter);
414 } else {
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000415 populate_be2_stats(adapter);
Selvin Xavier005d5692011-05-16 07:36:35 +0000416 }
Sathya Perlaac124ff2011-07-25 19:10:14 +0000417
418 /* as erx_v1 is longer than v0, ok to use v1 defn for v0 access */
Sathya Perla09c1c682011-08-22 19:41:53 +0000419 for_all_rx_queues(adapter, rxo, i) {
420 /* below erx HW counter can actually wrap around after
421 * 65535. Driver accumulates a 32-bit value
422 */
423 accumulate_16bit_val(&rx_stats(rxo)->rx_drops_no_frags,
424 (u16)erx->rx_drops_no_fragments[rxo->q.id]);
425 }
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000426}
427
Sathya Perlaab1594e2011-07-25 19:10:15 +0000428static struct rtnl_link_stats64 *be_get_stats64(struct net_device *netdev,
429 struct rtnl_link_stats64 *stats)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700430{
Sathya Perlaab1594e2011-07-25 19:10:15 +0000431 struct be_adapter *adapter = netdev_priv(netdev);
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000432 struct be_drv_stats *drvs = &adapter->drv_stats;
Sathya Perla3abcded2010-10-03 22:12:27 -0700433 struct be_rx_obj *rxo;
Sathya Perla3c8def92011-06-12 20:01:58 +0000434 struct be_tx_obj *txo;
Sathya Perlaab1594e2011-07-25 19:10:15 +0000435 u64 pkts, bytes;
436 unsigned int start;
Sathya Perla3abcded2010-10-03 22:12:27 -0700437 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700438
Sathya Perla3abcded2010-10-03 22:12:27 -0700439 for_all_rx_queues(adapter, rxo, i) {
Sathya Perlaab1594e2011-07-25 19:10:15 +0000440 const struct be_rx_stats *rx_stats = rx_stats(rxo);
441 do {
442 start = u64_stats_fetch_begin_bh(&rx_stats->sync);
443 pkts = rx_stats(rxo)->rx_pkts;
444 bytes = rx_stats(rxo)->rx_bytes;
445 } while (u64_stats_fetch_retry_bh(&rx_stats->sync, start));
446 stats->rx_packets += pkts;
447 stats->rx_bytes += bytes;
448 stats->multicast += rx_stats(rxo)->rx_mcast_pkts;
449 stats->rx_dropped += rx_stats(rxo)->rx_drops_no_skbs +
450 rx_stats(rxo)->rx_drops_no_frags;
Sathya Perla3abcded2010-10-03 22:12:27 -0700451 }
452
Sathya Perla3c8def92011-06-12 20:01:58 +0000453 for_all_tx_queues(adapter, txo, i) {
Sathya Perlaab1594e2011-07-25 19:10:15 +0000454 const struct be_tx_stats *tx_stats = tx_stats(txo);
455 do {
456 start = u64_stats_fetch_begin_bh(&tx_stats->sync);
457 pkts = tx_stats(txo)->tx_pkts;
458 bytes = tx_stats(txo)->tx_bytes;
459 } while (u64_stats_fetch_retry_bh(&tx_stats->sync, start));
460 stats->tx_packets += pkts;
461 stats->tx_bytes += bytes;
Sathya Perla3c8def92011-06-12 20:01:58 +0000462 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700463
464 /* bad pkts received */
Sathya Perlaab1594e2011-07-25 19:10:15 +0000465 stats->rx_errors = drvs->rx_crc_errors +
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000466 drvs->rx_alignment_symbol_errors +
467 drvs->rx_in_range_errors +
468 drvs->rx_out_range_errors +
469 drvs->rx_frame_too_long +
470 drvs->rx_dropped_too_small +
471 drvs->rx_dropped_too_short +
472 drvs->rx_dropped_header_too_small +
473 drvs->rx_dropped_tcp_length +
Sathya Perlaab1594e2011-07-25 19:10:15 +0000474 drvs->rx_dropped_runt;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700475
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700476 /* detailed rx errors */
Sathya Perlaab1594e2011-07-25 19:10:15 +0000477 stats->rx_length_errors = drvs->rx_in_range_errors +
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000478 drvs->rx_out_range_errors +
479 drvs->rx_frame_too_long;
Sathya Perla68110862009-06-10 02:21:16 +0000480
Sathya Perlaab1594e2011-07-25 19:10:15 +0000481 stats->rx_crc_errors = drvs->rx_crc_errors;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700482
483 /* frame alignment errors */
Sathya Perlaab1594e2011-07-25 19:10:15 +0000484 stats->rx_frame_errors = drvs->rx_alignment_symbol_errors;
Sathya Perla68110862009-06-10 02:21:16 +0000485
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700486 /* receiver fifo overrun */
487 /* drops_no_pbuf is no per i/f, it's per BE card */
Sathya Perlaab1594e2011-07-25 19:10:15 +0000488 stats->rx_fifo_errors = drvs->rxpp_fifo_overflow_drop +
Ajit Khaparde89a88ab2011-05-16 07:36:18 +0000489 drvs->rx_input_fifo_overflow_drop +
490 drvs->rx_drops_no_pbuf;
Sathya Perlaab1594e2011-07-25 19:10:15 +0000491 return stats;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700492}
493
Sathya Perlaea172a02011-08-02 19:57:42 +0000494void be_link_status_update(struct be_adapter *adapter, u32 link_status)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700495{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700496 struct net_device *netdev = adapter->netdev;
497
Sathya Perlaea172a02011-08-02 19:57:42 +0000498 /* when link status changes, link speed must be re-queried from card */
499 adapter->link_speed = -1;
500 if ((link_status & LINK_STATUS_MASK) == LINK_UP) {
501 netif_carrier_on(netdev);
502 dev_info(&adapter->pdev->dev, "%s: Link up\n", netdev->name);
503 } else {
504 netif_carrier_off(netdev);
505 dev_info(&adapter->pdev->dev, "%s: Link down\n", netdev->name);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700506 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700507}
508
Sathya Perla3c8def92011-06-12 20:01:58 +0000509static void be_tx_stats_update(struct be_tx_obj *txo,
Ajit Khaparde91992e42010-02-19 13:57:12 +0000510 u32 wrb_cnt, u32 copied, u32 gso_segs, bool stopped)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700511{
Sathya Perla3c8def92011-06-12 20:01:58 +0000512 struct be_tx_stats *stats = tx_stats(txo);
513
Sathya Perlaab1594e2011-07-25 19:10:15 +0000514 u64_stats_update_begin(&stats->sync);
Sathya Perlaac124ff2011-07-25 19:10:14 +0000515 stats->tx_reqs++;
516 stats->tx_wrbs += wrb_cnt;
517 stats->tx_bytes += copied;
518 stats->tx_pkts += (gso_segs ? gso_segs : 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700519 if (stopped)
Sathya Perlaac124ff2011-07-25 19:10:14 +0000520 stats->tx_stops++;
Sathya Perlaab1594e2011-07-25 19:10:15 +0000521 u64_stats_update_end(&stats->sync);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700522}
523
524/* Determine number of WRB entries needed to xmit data in an skb */
Sathya Perlafe6d2a32010-11-21 23:25:50 +0000525static u32 wrb_cnt_for_skb(struct be_adapter *adapter, struct sk_buff *skb,
526 bool *dummy)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700527{
David S. Millerebc8d2a2009-06-09 01:01:31 -0700528 int cnt = (skb->len > skb->data_len);
529
530 cnt += skb_shinfo(skb)->nr_frags;
531
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700532 /* to account for hdr wrb */
533 cnt++;
Sathya Perlafe6d2a32010-11-21 23:25:50 +0000534 if (lancer_chip(adapter) || !(cnt & 1)) {
535 *dummy = false;
536 } else {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700537 /* add a dummy to make it an even num */
538 cnt++;
539 *dummy = true;
Sathya Perlafe6d2a32010-11-21 23:25:50 +0000540 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700541 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
542 return cnt;
543}
544
545static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
546{
547 wrb->frag_pa_hi = upper_32_bits(addr);
548 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
549 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
550}
551
Somnath Koturcc4ce022010-10-21 07:11:14 -0700552static void wrb_fill_hdr(struct be_adapter *adapter, struct be_eth_hdr_wrb *hdr,
553 struct sk_buff *skb, u32 wrb_cnt, u32 len)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700554{
Somnath Koturcc4ce022010-10-21 07:11:14 -0700555 u8 vlan_prio = 0;
556 u16 vlan_tag = 0;
557
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700558 memset(hdr, 0, sizeof(*hdr));
559
560 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
561
Ajit Khaparde49e4b8472010-06-14 04:56:07 +0000562 if (skb_is_gso(skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700563 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
564 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
565 hdr, skb_shinfo(skb)->gso_size);
Sathya Perlafe6d2a32010-11-21 23:25:50 +0000566 if (skb_is_gso_v6(skb) && !lancer_chip(adapter))
Ajit Khaparde49e4b8472010-06-14 04:56:07 +0000567 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso6, hdr, 1);
Sathya Perlafe6d2a32010-11-21 23:25:50 +0000568 if (lancer_chip(adapter) && adapter->sli_family ==
569 LANCER_A0_SLI_FAMILY) {
570 AMAP_SET_BITS(struct amap_eth_hdr_wrb, ipcs, hdr, 1);
571 if (is_tcp_pkt(skb))
572 AMAP_SET_BITS(struct amap_eth_hdr_wrb,
573 tcpcs, hdr, 1);
574 else if (is_udp_pkt(skb))
575 AMAP_SET_BITS(struct amap_eth_hdr_wrb,
576 udpcs, hdr, 1);
577 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700578 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
579 if (is_tcp_pkt(skb))
580 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
581 else if (is_udp_pkt(skb))
582 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
583 }
584
Ajit Khaparde4c5102f2011-07-12 22:10:01 -0700585 if (vlan_tx_tag_present(skb)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700586 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
Somnath Koturcc4ce022010-10-21 07:11:14 -0700587 vlan_tag = vlan_tx_tag_get(skb);
588 vlan_prio = (vlan_tag & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
589 /* If vlan priority provided by OS is NOT in available bmap */
590 if (!(adapter->vlan_prio_bmap & (1 << vlan_prio)))
591 vlan_tag = (vlan_tag & ~VLAN_PRIO_MASK) |
592 adapter->recommended_prio;
593 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag, hdr, vlan_tag);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700594 }
595
596 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
597 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
598 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
599 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
600}
601
Ivan Vecera2b7bceb2011-02-02 08:05:12 +0000602static void unmap_tx_frag(struct device *dev, struct be_eth_wrb *wrb,
Sathya Perla7101e112010-03-22 20:41:12 +0000603 bool unmap_single)
604{
605 dma_addr_t dma;
606
607 be_dws_le_to_cpu(wrb, sizeof(*wrb));
608
609 dma = (u64)wrb->frag_pa_hi << 32 | (u64)wrb->frag_pa_lo;
FUJITA Tomonorib681ee72010-04-04 21:40:18 +0000610 if (wrb->frag_len) {
Sathya Perla7101e112010-03-22 20:41:12 +0000611 if (unmap_single)
Ivan Vecera2b7bceb2011-02-02 08:05:12 +0000612 dma_unmap_single(dev, dma, wrb->frag_len,
613 DMA_TO_DEVICE);
Sathya Perla7101e112010-03-22 20:41:12 +0000614 else
Ivan Vecera2b7bceb2011-02-02 08:05:12 +0000615 dma_unmap_page(dev, dma, wrb->frag_len, DMA_TO_DEVICE);
Sathya Perla7101e112010-03-22 20:41:12 +0000616 }
617}
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700618
Sathya Perla3c8def92011-06-12 20:01:58 +0000619static int make_tx_wrbs(struct be_adapter *adapter, struct be_queue_info *txq,
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700620 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
621{
Sathya Perla7101e112010-03-22 20:41:12 +0000622 dma_addr_t busaddr;
623 int i, copied = 0;
Ivan Vecera2b7bceb2011-02-02 08:05:12 +0000624 struct device *dev = &adapter->pdev->dev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700625 struct sk_buff *first_skb = skb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700626 struct be_eth_wrb *wrb;
627 struct be_eth_hdr_wrb *hdr;
Sathya Perla7101e112010-03-22 20:41:12 +0000628 bool map_single = false;
629 u16 map_head;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700630
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700631 hdr = queue_head_node(txq);
632 queue_head_inc(txq);
Sathya Perla7101e112010-03-22 20:41:12 +0000633 map_head = txq->head;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700634
David S. Millerebc8d2a2009-06-09 01:01:31 -0700635 if (skb->len > skb->data_len) {
Eric Dumazete743d312010-04-14 15:59:40 -0700636 int len = skb_headlen(skb);
Ivan Vecera2b7bceb2011-02-02 08:05:12 +0000637 busaddr = dma_map_single(dev, skb->data, len, DMA_TO_DEVICE);
638 if (dma_mapping_error(dev, busaddr))
Sathya Perla7101e112010-03-22 20:41:12 +0000639 goto dma_err;
640 map_single = true;
David S. Millerebc8d2a2009-06-09 01:01:31 -0700641 wrb = queue_head_node(txq);
642 wrb_fill(wrb, busaddr, len);
643 be_dws_cpu_to_le(wrb, sizeof(*wrb));
644 queue_head_inc(txq);
645 copied += len;
646 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700647
David S. Millerebc8d2a2009-06-09 01:01:31 -0700648 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000649 const struct skb_frag_struct *frag =
David S. Millerebc8d2a2009-06-09 01:01:31 -0700650 &skb_shinfo(skb)->frags[i];
Ian Campbellb061b392011-08-29 23:18:23 +0000651 busaddr = skb_frag_dma_map(dev, frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +0000652 skb_frag_size(frag), DMA_TO_DEVICE);
Ivan Vecera2b7bceb2011-02-02 08:05:12 +0000653 if (dma_mapping_error(dev, busaddr))
Sathya Perla7101e112010-03-22 20:41:12 +0000654 goto dma_err;
David S. Millerebc8d2a2009-06-09 01:01:31 -0700655 wrb = queue_head_node(txq);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000656 wrb_fill(wrb, busaddr, skb_frag_size(frag));
David S. Millerebc8d2a2009-06-09 01:01:31 -0700657 be_dws_cpu_to_le(wrb, sizeof(*wrb));
658 queue_head_inc(txq);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000659 copied += skb_frag_size(frag);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700660 }
661
662 if (dummy_wrb) {
663 wrb = queue_head_node(txq);
664 wrb_fill(wrb, 0, 0);
665 be_dws_cpu_to_le(wrb, sizeof(*wrb));
666 queue_head_inc(txq);
667 }
668
Somnath Koturcc4ce022010-10-21 07:11:14 -0700669 wrb_fill_hdr(adapter, hdr, first_skb, wrb_cnt, copied);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700670 be_dws_cpu_to_le(hdr, sizeof(*hdr));
671
672 return copied;
Sathya Perla7101e112010-03-22 20:41:12 +0000673dma_err:
674 txq->head = map_head;
675 while (copied) {
676 wrb = queue_head_node(txq);
Ivan Vecera2b7bceb2011-02-02 08:05:12 +0000677 unmap_tx_frag(dev, wrb, map_single);
Sathya Perla7101e112010-03-22 20:41:12 +0000678 map_single = false;
679 copied -= wrb->frag_len;
680 queue_head_inc(txq);
681 }
682 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700683}
684
Stephen Hemminger613573252009-08-31 19:50:58 +0000685static netdev_tx_t be_xmit(struct sk_buff *skb,
Sathya Perlab31c50a2009-09-17 10:30:13 -0700686 struct net_device *netdev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700687{
688 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3c8def92011-06-12 20:01:58 +0000689 struct be_tx_obj *txo = &adapter->tx_obj[skb_get_queue_mapping(skb)];
690 struct be_queue_info *txq = &txo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700691 u32 wrb_cnt = 0, copied = 0;
692 u32 start = txq->head;
693 bool dummy_wrb, stopped = false;
694
Sathya Perlafe6d2a32010-11-21 23:25:50 +0000695 wrb_cnt = wrb_cnt_for_skb(adapter, skb, &dummy_wrb);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700696
Sathya Perla3c8def92011-06-12 20:01:58 +0000697 copied = make_tx_wrbs(adapter, txq, skb, wrb_cnt, dummy_wrb);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000698 if (copied) {
699 /* record the sent skb in the sent_skb table */
Sathya Perla3c8def92011-06-12 20:01:58 +0000700 BUG_ON(txo->sent_skb_list[start]);
701 txo->sent_skb_list[start] = skb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700702
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000703 /* Ensure txq has space for the next skb; Else stop the queue
704 * *BEFORE* ringing the tx doorbell, so that we serialze the
705 * tx compls of the current transmit which'll wake up the queue
706 */
Sathya Perla7101e112010-03-22 20:41:12 +0000707 atomic_add(wrb_cnt, &txq->used);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000708 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
709 txq->len) {
Sathya Perla3c8def92011-06-12 20:01:58 +0000710 netif_stop_subqueue(netdev, skb_get_queue_mapping(skb));
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000711 stopped = true;
712 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700713
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000714 be_txq_notify(adapter, txq->id, wrb_cnt);
715
Sathya Perla3c8def92011-06-12 20:01:58 +0000716 be_tx_stats_update(txo, wrb_cnt, copied,
Ajit Khaparde91992e42010-02-19 13:57:12 +0000717 skb_shinfo(skb)->gso_segs, stopped);
Ajit Khapardec190e3c2009-09-04 03:12:29 +0000718 } else {
719 txq->head = start;
720 dev_kfree_skb_any(skb);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700721 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700722 return NETDEV_TX_OK;
723}
724
725static int be_change_mtu(struct net_device *netdev, int new_mtu)
726{
727 struct be_adapter *adapter = netdev_priv(netdev);
728 if (new_mtu < BE_MIN_MTU ||
Ajit Khaparde34a89b82010-02-09 01:32:43 +0000729 new_mtu > (BE_MAX_JUMBO_FRAME_SIZE -
730 (ETH_HLEN + ETH_FCS_LEN))) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700731 dev_info(&adapter->pdev->dev,
732 "MTU must be between %d and %d bytes\n",
Ajit Khaparde34a89b82010-02-09 01:32:43 +0000733 BE_MIN_MTU,
734 (BE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN)));
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700735 return -EINVAL;
736 }
737 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
738 netdev->mtu, new_mtu);
739 netdev->mtu = new_mtu;
740 return 0;
741}
742
743/*
Ajit Khaparde82903e42010-02-09 01:34:57 +0000744 * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
745 * If the user configures more, place BE in vlan promiscuous mode.
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700746 */
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000747static int be_vid_config(struct be_adapter *adapter, bool vf, u32 vf_num)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700748{
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700749 u16 vtag[BE_NUM_VLANS_SUPPORTED];
750 u16 ntags = 0, i;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000751 int status = 0;
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000752 u32 if_handle;
753
754 if (vf) {
755 if_handle = adapter->vf_cfg[vf_num].vf_if_handle;
756 vtag[0] = cpu_to_le16(adapter->vf_cfg[vf_num].vf_vlan_tag);
757 status = be_cmd_vlan_config(adapter, if_handle, vtag, 1, 1, 0);
758 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700759
Sathya Perlac0e64ef2011-08-02 19:57:43 +0000760 /* No need to further configure vids if in promiscuous mode */
761 if (adapter->promiscuous)
762 return 0;
763
Ajit Khaparde82903e42010-02-09 01:34:57 +0000764 if (adapter->vlans_added <= adapter->max_vlans) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700765 /* Construct VLAN Table to give to HW */
Jesse Grossb7381272010-10-20 13:56:02 +0000766 for (i = 0; i < VLAN_N_VID; i++) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700767 if (adapter->vlan_tag[i]) {
768 vtag[ntags] = cpu_to_le16(i);
769 ntags++;
770 }
771 }
Sathya Perlab31c50a2009-09-17 10:30:13 -0700772 status = be_cmd_vlan_config(adapter, adapter->if_handle,
773 vtag, ntags, 1, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700774 } else {
Sathya Perlab31c50a2009-09-17 10:30:13 -0700775 status = be_cmd_vlan_config(adapter, adapter->if_handle,
776 NULL, 0, 1, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700777 }
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000778
Sathya Perlab31c50a2009-09-17 10:30:13 -0700779 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700780}
781
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700782static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
783{
784 struct be_adapter *adapter = netdev_priv(netdev);
785
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000786 adapter->vlans_added++;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000787 if (!be_physfn(adapter))
788 return;
789
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700790 adapter->vlan_tag[vid] = 1;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000791 if (adapter->vlans_added <= (adapter->max_vlans + 1))
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000792 be_vid_config(adapter, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700793}
794
795static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
796{
797 struct be_adapter *adapter = netdev_priv(netdev);
798
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000799 adapter->vlans_added--;
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000800
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000801 if (!be_physfn(adapter))
802 return;
803
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700804 adapter->vlan_tag[vid] = 0;
Ajit Khaparde82903e42010-02-09 01:34:57 +0000805 if (adapter->vlans_added <= adapter->max_vlans)
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000806 be_vid_config(adapter, false, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700807}
808
Sathya Perlaa54769f2011-10-24 02:45:00 +0000809static void be_set_rx_mode(struct net_device *netdev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700810{
811 struct be_adapter *adapter = netdev_priv(netdev);
812
813 if (netdev->flags & IFF_PROMISC) {
Sathya Perla5b8821b2011-08-02 19:57:44 +0000814 be_cmd_rx_filter(adapter, IFF_PROMISC, ON);
Sathya Perla24307ee2009-06-18 00:09:25 +0000815 adapter->promiscuous = true;
816 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700817 }
Sathya Perla24307ee2009-06-18 00:09:25 +0000818
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300819 /* BE was previously in promiscuous mode; disable it */
Sathya Perla24307ee2009-06-18 00:09:25 +0000820 if (adapter->promiscuous) {
821 adapter->promiscuous = false;
Sathya Perla5b8821b2011-08-02 19:57:44 +0000822 be_cmd_rx_filter(adapter, IFF_PROMISC, OFF);
Sathya Perlac0e64ef2011-08-02 19:57:43 +0000823
824 if (adapter->vlans_added)
825 be_vid_config(adapter, false, 0);
Sathya Perla24307ee2009-06-18 00:09:25 +0000826 }
827
Sathya Perlae7b909a2009-11-22 22:01:10 +0000828 /* Enable multicast promisc if num configured exceeds what we support */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000829 if (netdev->flags & IFF_ALLMULTI ||
Sathya Perla5b8821b2011-08-02 19:57:44 +0000830 netdev_mc_count(netdev) > BE_MAX_MC) {
831 be_cmd_rx_filter(adapter, IFF_ALLMULTI, ON);
Sathya Perla24307ee2009-06-18 00:09:25 +0000832 goto done;
833 }
834
Sathya Perla5b8821b2011-08-02 19:57:44 +0000835 be_cmd_rx_filter(adapter, IFF_MULTICAST, ON);
Sathya Perla24307ee2009-06-18 00:09:25 +0000836done:
837 return;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700838}
839
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000840static int be_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
841{
842 struct be_adapter *adapter = netdev_priv(netdev);
843 int status;
844
845 if (!adapter->sriov_enabled)
846 return -EPERM;
847
848 if (!is_valid_ether_addr(mac) || (vf >= num_vfs))
849 return -EINVAL;
850
Sathya Perla30128032011-11-10 19:17:57 +0000851 status = be_cmd_pmac_del(adapter, adapter->vf_cfg[vf].vf_if_handle,
852 adapter->vf_cfg[vf].vf_pmac_id, vf + 1);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000853
Sathya Perla30128032011-11-10 19:17:57 +0000854 status = be_cmd_pmac_add(adapter, mac, adapter->vf_cfg[vf].vf_if_handle,
Ajit Khapardef8617e02011-02-11 13:36:37 +0000855 &adapter->vf_cfg[vf].vf_pmac_id, vf + 1);
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000856 if (status)
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000857 dev_err(&adapter->pdev->dev, "MAC %pM set on VF %d Failed\n",
858 mac, vf);
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000859 else
860 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
861
Sarveshwar Bandiba343c72010-03-31 02:56:12 +0000862 return status;
863}
864
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000865static int be_get_vf_config(struct net_device *netdev, int vf,
866 struct ifla_vf_info *vi)
867{
868 struct be_adapter *adapter = netdev_priv(netdev);
869
870 if (!adapter->sriov_enabled)
871 return -EPERM;
872
873 if (vf >= num_vfs)
874 return -EINVAL;
875
876 vi->vf = vf;
Ajit Khapardee1d18732010-07-23 01:52:13 +0000877 vi->tx_rate = adapter->vf_cfg[vf].vf_tx_rate;
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000878 vi->vlan = adapter->vf_cfg[vf].vf_vlan_tag;
Ajit Khaparde64600ea2010-07-23 01:50:34 +0000879 vi->qos = 0;
880 memcpy(&vi->mac, adapter->vf_cfg[vf].vf_mac_addr, ETH_ALEN);
881
882 return 0;
883}
884
Ajit Khaparde1da87b72010-07-23 01:51:22 +0000885static int be_set_vf_vlan(struct net_device *netdev,
886 int vf, u16 vlan, u8 qos)
887{
888 struct be_adapter *adapter = netdev_priv(netdev);
889 int status = 0;
890
891 if (!adapter->sriov_enabled)
892 return -EPERM;
893
894 if ((vf >= num_vfs) || (vlan > 4095))
895 return -EINVAL;
896
897 if (vlan) {
898 adapter->vf_cfg[vf].vf_vlan_tag = vlan;
899 adapter->vlans_added++;
900 } else {
901 adapter->vf_cfg[vf].vf_vlan_tag = 0;
902 adapter->vlans_added--;
903 }
904
905 status = be_vid_config(adapter, true, vf);
906
907 if (status)
908 dev_info(&adapter->pdev->dev,
909 "VLAN %d config on VF %d failed\n", vlan, vf);
910 return status;
911}
912
Ajit Khapardee1d18732010-07-23 01:52:13 +0000913static int be_set_vf_tx_rate(struct net_device *netdev,
914 int vf, int rate)
915{
916 struct be_adapter *adapter = netdev_priv(netdev);
917 int status = 0;
918
919 if (!adapter->sriov_enabled)
920 return -EPERM;
921
922 if ((vf >= num_vfs) || (rate < 0))
923 return -EINVAL;
924
925 if (rate > 10000)
926 rate = 10000;
927
928 adapter->vf_cfg[vf].vf_tx_rate = rate;
Ajit Khaparde856c4012011-02-11 13:32:32 +0000929 status = be_cmd_set_qos(adapter, rate / 10, vf + 1);
Ajit Khapardee1d18732010-07-23 01:52:13 +0000930
931 if (status)
932 dev_info(&adapter->pdev->dev,
933 "tx rate %d on VF %d failed\n", rate, vf);
934 return status;
935}
936
Sathya Perlaac124ff2011-07-25 19:10:14 +0000937static void be_rx_eqd_update(struct be_adapter *adapter, struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700938{
Sathya Perlaac124ff2011-07-25 19:10:14 +0000939 struct be_eq_obj *rx_eq = &rxo->rx_eq;
940 struct be_rx_stats *stats = rx_stats(rxo);
Sathya Perla4097f662009-03-24 16:40:13 -0700941 ulong now = jiffies;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000942 ulong delta = now - stats->rx_jiffies;
Sathya Perlaab1594e2011-07-25 19:10:15 +0000943 u64 pkts;
944 unsigned int start, eqd;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000945
946 if (!rx_eq->enable_aic)
947 return;
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700948
Sathya Perla4097f662009-03-24 16:40:13 -0700949 /* Wrapped around */
Sathya Perla3abcded2010-10-03 22:12:27 -0700950 if (time_before(now, stats->rx_jiffies)) {
951 stats->rx_jiffies = now;
Sathya Perla4097f662009-03-24 16:40:13 -0700952 return;
953 }
954
Sathya Perlaac124ff2011-07-25 19:10:14 +0000955 /* Update once a second */
956 if (delta < HZ)
Sathya Perla4097f662009-03-24 16:40:13 -0700957 return;
958
Sathya Perlaab1594e2011-07-25 19:10:15 +0000959 do {
960 start = u64_stats_fetch_begin_bh(&stats->sync);
961 pkts = stats->rx_pkts;
962 } while (u64_stats_fetch_retry_bh(&stats->sync, start));
963
Eric Dumazet68c3e5a2011-08-09 06:23:07 +0000964 stats->rx_pps = (unsigned long)(pkts - stats->rx_pkts_prev) / (delta / HZ);
Sathya Perlaab1594e2011-07-25 19:10:15 +0000965 stats->rx_pkts_prev = pkts;
Sathya Perla3abcded2010-10-03 22:12:27 -0700966 stats->rx_jiffies = now;
Sathya Perlaac124ff2011-07-25 19:10:14 +0000967 eqd = stats->rx_pps / 110000;
968 eqd = eqd << 3;
969 if (eqd > rx_eq->max_eqd)
970 eqd = rx_eq->max_eqd;
971 if (eqd < rx_eq->min_eqd)
972 eqd = rx_eq->min_eqd;
973 if (eqd < 10)
974 eqd = 0;
975 if (eqd != rx_eq->cur_eqd) {
976 be_cmd_modify_eqd(adapter, rx_eq->q.id, eqd);
977 rx_eq->cur_eqd = eqd;
978 }
Sathya Perla4097f662009-03-24 16:40:13 -0700979}
980
Sathya Perla3abcded2010-10-03 22:12:27 -0700981static void be_rx_stats_update(struct be_rx_obj *rxo,
Sathya Perla2e588f82011-03-11 02:49:26 +0000982 struct be_rx_compl_info *rxcp)
Sathya Perla4097f662009-03-24 16:40:13 -0700983{
Sathya Perlaac124ff2011-07-25 19:10:14 +0000984 struct be_rx_stats *stats = rx_stats(rxo);
Sathya Perla4097f662009-03-24 16:40:13 -0700985
Sathya Perlaab1594e2011-07-25 19:10:15 +0000986 u64_stats_update_begin(&stats->sync);
Sathya Perla3abcded2010-10-03 22:12:27 -0700987 stats->rx_compl++;
Sathya Perla2e588f82011-03-11 02:49:26 +0000988 stats->rx_bytes += rxcp->pkt_size;
Sathya Perla3abcded2010-10-03 22:12:27 -0700989 stats->rx_pkts++;
Sathya Perla2e588f82011-03-11 02:49:26 +0000990 if (rxcp->pkt_type == BE_MULTICAST_PACKET)
Sathya Perla3abcded2010-10-03 22:12:27 -0700991 stats->rx_mcast_pkts++;
Sathya Perla2e588f82011-03-11 02:49:26 +0000992 if (rxcp->err)
Sathya Perlaac124ff2011-07-25 19:10:14 +0000993 stats->rx_compl_err++;
Sathya Perlaab1594e2011-07-25 19:10:15 +0000994 u64_stats_update_end(&stats->sync);
Sathya Perla6b7c5b92009-03-11 23:32:03 -0700995}
996
Sathya Perla2e588f82011-03-11 02:49:26 +0000997static inline bool csum_passed(struct be_rx_compl_info *rxcp)
Ajit Khaparde728a9972009-04-13 15:41:22 -0700998{
Padmanabh Ratnakar19fad862011-03-07 03:08:16 +0000999 /* L4 checksum is not reliable for non TCP/UDP packets.
1000 * Also ignore ipcksm for ipv6 pkts */
Sathya Perla2e588f82011-03-11 02:49:26 +00001001 return (rxcp->tcpf || rxcp->udpf) && rxcp->l4_csum &&
1002 (rxcp->ip_csum || rxcp->ipv6);
Ajit Khaparde728a9972009-04-13 15:41:22 -07001003}
1004
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001005static struct be_rx_page_info *
Sathya Perla3abcded2010-10-03 22:12:27 -07001006get_rx_page_info(struct be_adapter *adapter,
1007 struct be_rx_obj *rxo,
1008 u16 frag_idx)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001009{
1010 struct be_rx_page_info *rx_page_info;
Sathya Perla3abcded2010-10-03 22:12:27 -07001011 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001012
Sathya Perla3abcded2010-10-03 22:12:27 -07001013 rx_page_info = &rxo->page_info_tbl[frag_idx];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001014 BUG_ON(!rx_page_info->page);
1015
Ajit Khaparde205859a2010-02-09 01:34:21 +00001016 if (rx_page_info->last_page_user) {
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00001017 dma_unmap_page(&adapter->pdev->dev,
1018 dma_unmap_addr(rx_page_info, bus),
1019 adapter->big_page_size, DMA_FROM_DEVICE);
Ajit Khaparde205859a2010-02-09 01:34:21 +00001020 rx_page_info->last_page_user = false;
1021 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001022
1023 atomic_dec(&rxq->used);
1024 return rx_page_info;
1025}
1026
1027/* Throwaway the data in the Rx completion */
1028static void be_rx_compl_discard(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -07001029 struct be_rx_obj *rxo,
Sathya Perla2e588f82011-03-11 02:49:26 +00001030 struct be_rx_compl_info *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001031{
Sathya Perla3abcded2010-10-03 22:12:27 -07001032 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001033 struct be_rx_page_info *page_info;
Sathya Perla2e588f82011-03-11 02:49:26 +00001034 u16 i, num_rcvd = rxcp->num_rcvd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001035
Padmanabh Ratnakare80d9da2011-03-07 03:07:58 +00001036 for (i = 0; i < num_rcvd; i++) {
Sathya Perla2e588f82011-03-11 02:49:26 +00001037 page_info = get_rx_page_info(adapter, rxo, rxcp->rxq_idx);
Padmanabh Ratnakare80d9da2011-03-07 03:07:58 +00001038 put_page(page_info->page);
1039 memset(page_info, 0, sizeof(*page_info));
Sathya Perla2e588f82011-03-11 02:49:26 +00001040 index_inc(&rxcp->rxq_idx, rxq->len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001041 }
1042}
1043
1044/*
1045 * skb_fill_rx_data forms a complete skb for an ether frame
1046 * indicated by rxcp.
1047 */
Sathya Perla3abcded2010-10-03 22:12:27 -07001048static void skb_fill_rx_data(struct be_adapter *adapter, struct be_rx_obj *rxo,
Sathya Perla2e588f82011-03-11 02:49:26 +00001049 struct sk_buff *skb, struct be_rx_compl_info *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001050{
Sathya Perla3abcded2010-10-03 22:12:27 -07001051 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001052 struct be_rx_page_info *page_info;
Sathya Perla2e588f82011-03-11 02:49:26 +00001053 u16 i, j;
1054 u16 hdr_len, curr_frag_len, remaining;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001055 u8 *start;
1056
Sathya Perla2e588f82011-03-11 02:49:26 +00001057 page_info = get_rx_page_info(adapter, rxo, rxcp->rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001058 start = page_address(page_info->page) + page_info->page_offset;
1059 prefetch(start);
1060
1061 /* Copy data in the first descriptor of this completion */
Sathya Perla2e588f82011-03-11 02:49:26 +00001062 curr_frag_len = min(rxcp->pkt_size, rx_frag_size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001063
1064 /* Copy the header portion into skb_data */
Sathya Perla2e588f82011-03-11 02:49:26 +00001065 hdr_len = min(BE_HDR_LEN, curr_frag_len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001066 memcpy(skb->data, start, hdr_len);
1067 skb->len = curr_frag_len;
1068 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
1069 /* Complete packet has now been moved to data */
1070 put_page(page_info->page);
1071 skb->data_len = 0;
1072 skb->tail += curr_frag_len;
1073 } else {
1074 skb_shinfo(skb)->nr_frags = 1;
Ian Campbellb061b392011-08-29 23:18:23 +00001075 skb_frag_set_page(skb, 0, page_info->page);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001076 skb_shinfo(skb)->frags[0].page_offset =
1077 page_info->page_offset + hdr_len;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001078 skb_frag_size_set(&skb_shinfo(skb)->frags[0], curr_frag_len - hdr_len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001079 skb->data_len = curr_frag_len - hdr_len;
Eric Dumazetbdb28a92011-10-13 06:31:02 +00001080 skb->truesize += rx_frag_size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001081 skb->tail += hdr_len;
1082 }
Ajit Khaparde205859a2010-02-09 01:34:21 +00001083 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001084
Sathya Perla2e588f82011-03-11 02:49:26 +00001085 if (rxcp->pkt_size <= rx_frag_size) {
1086 BUG_ON(rxcp->num_rcvd != 1);
1087 return;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001088 }
1089
1090 /* More frags present for this completion */
Sathya Perla2e588f82011-03-11 02:49:26 +00001091 index_inc(&rxcp->rxq_idx, rxq->len);
1092 remaining = rxcp->pkt_size - curr_frag_len;
1093 for (i = 1, j = 0; i < rxcp->num_rcvd; i++) {
1094 page_info = get_rx_page_info(adapter, rxo, rxcp->rxq_idx);
1095 curr_frag_len = min(remaining, rx_frag_size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001096
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001097 /* Coalesce all frags from the same physical page in one slot */
1098 if (page_info->page_offset == 0) {
1099 /* Fresh page */
1100 j++;
Ian Campbellb061b392011-08-29 23:18:23 +00001101 skb_frag_set_page(skb, j, page_info->page);
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001102 skb_shinfo(skb)->frags[j].page_offset =
1103 page_info->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001104 skb_frag_size_set(&skb_shinfo(skb)->frags[j], 0);
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001105 skb_shinfo(skb)->nr_frags++;
1106 } else {
1107 put_page(page_info->page);
1108 }
1109
Eric Dumazet9e903e02011-10-18 21:00:24 +00001110 skb_frag_size_add(&skb_shinfo(skb)->frags[j], curr_frag_len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001111 skb->len += curr_frag_len;
1112 skb->data_len += curr_frag_len;
Eric Dumazetbdb28a92011-10-13 06:31:02 +00001113 skb->truesize += rx_frag_size;
Sathya Perla2e588f82011-03-11 02:49:26 +00001114 remaining -= curr_frag_len;
1115 index_inc(&rxcp->rxq_idx, rxq->len);
Ajit Khaparde205859a2010-02-09 01:34:21 +00001116 page_info->page = NULL;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001117 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001118 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001119}
1120
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001121/* Process the RX completion indicated by rxcp when GRO is disabled */
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001122static void be_rx_compl_process(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -07001123 struct be_rx_obj *rxo,
Sathya Perla2e588f82011-03-11 02:49:26 +00001124 struct be_rx_compl_info *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001125{
Michał Mirosław6332c8d2011-04-07 02:43:48 +00001126 struct net_device *netdev = adapter->netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001127 struct sk_buff *skb;
Sathya Perla89420422010-02-17 01:35:26 +00001128
Michał Mirosław6332c8d2011-04-07 02:43:48 +00001129 skb = netdev_alloc_skb_ip_align(netdev, BE_HDR_LEN);
Sathya Perlaa058a632010-02-17 01:34:22 +00001130 if (unlikely(!skb)) {
Sathya Perlaac124ff2011-07-25 19:10:14 +00001131 rx_stats(rxo)->rx_drops_no_skbs++;
Sathya Perla3abcded2010-10-03 22:12:27 -07001132 be_rx_compl_discard(adapter, rxo, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001133 return;
1134 }
1135
Sathya Perla2e588f82011-03-11 02:49:26 +00001136 skb_fill_rx_data(adapter, rxo, skb, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001137
Michał Mirosław6332c8d2011-04-07 02:43:48 +00001138 if (likely((netdev->features & NETIF_F_RXCSUM) && csum_passed(rxcp)))
Ajit Khaparde728a9972009-04-13 15:41:22 -07001139 skb->ip_summed = CHECKSUM_UNNECESSARY;
Somnath Koturc6ce2f42010-10-25 01:11:58 +00001140 else
1141 skb_checksum_none_assert(skb);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001142
Michał Mirosław6332c8d2011-04-07 02:43:48 +00001143 skb->protocol = eth_type_trans(skb, netdev);
Ajit Khaparde4b972912011-04-06 18:07:43 +00001144 if (adapter->netdev->features & NETIF_F_RXHASH)
1145 skb->rxhash = rxcp->rss_hash;
1146
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001147
Jiri Pirko343e43c2011-08-25 02:50:51 +00001148 if (rxcp->vlanf)
Ajit Khaparde4c5102f2011-07-12 22:10:01 -07001149 __vlan_hwaccel_put_tag(skb, rxcp->vlan_tag);
1150
1151 netif_receive_skb(skb);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001152}
1153
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001154/* Process the RX completion indicated by rxcp when GRO is enabled */
1155static void be_rx_compl_process_gro(struct be_adapter *adapter,
Sathya Perla3abcded2010-10-03 22:12:27 -07001156 struct be_rx_obj *rxo,
Sathya Perla2e588f82011-03-11 02:49:26 +00001157 struct be_rx_compl_info *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001158{
1159 struct be_rx_page_info *page_info;
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001160 struct sk_buff *skb = NULL;
Sathya Perla3abcded2010-10-03 22:12:27 -07001161 struct be_queue_info *rxq = &rxo->q;
1162 struct be_eq_obj *eq_obj = &rxo->rx_eq;
Sathya Perla2e588f82011-03-11 02:49:26 +00001163 u16 remaining, curr_frag_len;
1164 u16 i, j;
Ajit Khaparde3968fa12011-02-20 11:41:53 +00001165
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001166 skb = napi_get_frags(&eq_obj->napi);
1167 if (!skb) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001168 be_rx_compl_discard(adapter, rxo, rxcp);
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001169 return;
1170 }
1171
Sathya Perla2e588f82011-03-11 02:49:26 +00001172 remaining = rxcp->pkt_size;
1173 for (i = 0, j = -1; i < rxcp->num_rcvd; i++) {
1174 page_info = get_rx_page_info(adapter, rxo, rxcp->rxq_idx);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001175
1176 curr_frag_len = min(remaining, rx_frag_size);
1177
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001178 /* Coalesce all frags from the same physical page in one slot */
1179 if (i == 0 || page_info->page_offset == 0) {
1180 /* First frag or Fresh page */
1181 j++;
Ian Campbellb061b392011-08-29 23:18:23 +00001182 skb_frag_set_page(skb, j, page_info->page);
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001183 skb_shinfo(skb)->frags[j].page_offset =
1184 page_info->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001185 skb_frag_size_set(&skb_shinfo(skb)->frags[j], 0);
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001186 } else {
1187 put_page(page_info->page);
1188 }
Eric Dumazet9e903e02011-10-18 21:00:24 +00001189 skb_frag_size_add(&skb_shinfo(skb)->frags[j], curr_frag_len);
Eric Dumazetbdb28a92011-10-13 06:31:02 +00001190 skb->truesize += rx_frag_size;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001191 remaining -= curr_frag_len;
Sathya Perla2e588f82011-03-11 02:49:26 +00001192 index_inc(&rxcp->rxq_idx, rxq->len);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001193 memset(page_info, 0, sizeof(*page_info));
1194 }
Ajit Khapardebd46cb62009-06-26 02:51:07 +00001195 BUG_ON(j > MAX_SKB_FRAGS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001196
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001197 skb_shinfo(skb)->nr_frags = j + 1;
Sathya Perla2e588f82011-03-11 02:49:26 +00001198 skb->len = rxcp->pkt_size;
1199 skb->data_len = rxcp->pkt_size;
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001200 skb->ip_summed = CHECKSUM_UNNECESSARY;
Ajit Khaparde4b972912011-04-06 18:07:43 +00001201 if (adapter->netdev->features & NETIF_F_RXHASH)
1202 skb->rxhash = rxcp->rss_hash;
Ajit Khaparde5be93b92009-07-21 12:36:19 -07001203
Jiri Pirko343e43c2011-08-25 02:50:51 +00001204 if (rxcp->vlanf)
Ajit Khaparde4c5102f2011-07-12 22:10:01 -07001205 __vlan_hwaccel_put_tag(skb, rxcp->vlan_tag);
1206
1207 napi_gro_frags(&eq_obj->napi);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001208}
1209
Sathya Perla2e588f82011-03-11 02:49:26 +00001210static void be_parse_rx_compl_v1(struct be_adapter *adapter,
1211 struct be_eth_rx_compl *compl,
1212 struct be_rx_compl_info *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001213{
Sathya Perla2e588f82011-03-11 02:49:26 +00001214 rxcp->pkt_size =
1215 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, pktsize, compl);
1216 rxcp->vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vtp, compl);
1217 rxcp->err = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, err, compl);
1218 rxcp->tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, tcpf, compl);
Padmanabh Ratnakar9ecb42f2011-03-15 14:57:09 -07001219 rxcp->udpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, udpf, compl);
Sathya Perla2e588f82011-03-11 02:49:26 +00001220 rxcp->ip_csum =
1221 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, ipcksm, compl);
1222 rxcp->l4_csum =
1223 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, l4_cksm, compl);
1224 rxcp->ipv6 =
1225 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, ip_version, compl);
1226 rxcp->rxq_idx =
1227 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, fragndx, compl);
1228 rxcp->num_rcvd =
1229 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, numfrags, compl);
1230 rxcp->pkt_type =
1231 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, cast_enc, compl);
Ajit Khaparde4b972912011-04-06 18:07:43 +00001232 rxcp->rss_hash =
1233 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, rsshash, rxcp);
Sathya Perla15d72182011-03-21 20:49:26 +00001234 if (rxcp->vlanf) {
1235 rxcp->vtm = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vtm,
David S. Miller3c709f82011-05-11 14:26:15 -04001236 compl);
1237 rxcp->vlan_tag = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vlan_tag,
1238 compl);
Sathya Perla15d72182011-03-21 20:49:26 +00001239 }
Sathya Perla12004ae2011-08-02 19:57:46 +00001240 rxcp->port = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, port, compl);
Sathya Perla2e588f82011-03-11 02:49:26 +00001241}
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001242
Sathya Perla2e588f82011-03-11 02:49:26 +00001243static void be_parse_rx_compl_v0(struct be_adapter *adapter,
1244 struct be_eth_rx_compl *compl,
1245 struct be_rx_compl_info *rxcp)
1246{
1247 rxcp->pkt_size =
1248 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, pktsize, compl);
1249 rxcp->vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vtp, compl);
1250 rxcp->err = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, err, compl);
1251 rxcp->tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, tcpf, compl);
Padmanabh Ratnakar9ecb42f2011-03-15 14:57:09 -07001252 rxcp->udpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, udpf, compl);
Sathya Perla2e588f82011-03-11 02:49:26 +00001253 rxcp->ip_csum =
1254 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, ipcksm, compl);
1255 rxcp->l4_csum =
1256 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, l4_cksm, compl);
1257 rxcp->ipv6 =
1258 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, ip_version, compl);
1259 rxcp->rxq_idx =
1260 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, fragndx, compl);
1261 rxcp->num_rcvd =
1262 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, numfrags, compl);
1263 rxcp->pkt_type =
1264 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, cast_enc, compl);
Ajit Khaparde4b972912011-04-06 18:07:43 +00001265 rxcp->rss_hash =
1266 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, rsshash, rxcp);
Sathya Perla15d72182011-03-21 20:49:26 +00001267 if (rxcp->vlanf) {
1268 rxcp->vtm = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vtm,
David S. Miller3c709f82011-05-11 14:26:15 -04001269 compl);
1270 rxcp->vlan_tag = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vlan_tag,
1271 compl);
Sathya Perla15d72182011-03-21 20:49:26 +00001272 }
Sathya Perla12004ae2011-08-02 19:57:46 +00001273 rxcp->port = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, port, compl);
Sathya Perla2e588f82011-03-11 02:49:26 +00001274}
1275
1276static struct be_rx_compl_info *be_rx_compl_get(struct be_rx_obj *rxo)
1277{
1278 struct be_eth_rx_compl *compl = queue_tail_node(&rxo->cq);
1279 struct be_rx_compl_info *rxcp = &rxo->rxcp;
1280 struct be_adapter *adapter = rxo->adapter;
1281
1282 /* For checking the valid bit it is Ok to use either definition as the
1283 * valid bit is at the same position in both v0 and v1 Rx compl */
1284 if (compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] == 0)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001285 return NULL;
1286
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001287 rmb();
Sathya Perla2e588f82011-03-11 02:49:26 +00001288 be_dws_le_to_cpu(compl, sizeof(*compl));
1289
1290 if (adapter->be3_native)
1291 be_parse_rx_compl_v1(adapter, compl, rxcp);
1292 else
1293 be_parse_rx_compl_v0(adapter, compl, rxcp);
1294
Sathya Perla15d72182011-03-21 20:49:26 +00001295 if (rxcp->vlanf) {
1296 /* vlanf could be wrongly set in some cards.
1297 * ignore if vtm is not set */
Sathya Perla752961a2011-10-24 02:45:03 +00001298 if ((adapter->function_mode & FLEX10_MODE) && !rxcp->vtm)
Sathya Perla15d72182011-03-21 20:49:26 +00001299 rxcp->vlanf = 0;
Sathya Perla2e588f82011-03-11 02:49:26 +00001300
Sathya Perla15d72182011-03-21 20:49:26 +00001301 if (!lancer_chip(adapter))
David S. Miller3c709f82011-05-11 14:26:15 -04001302 rxcp->vlan_tag = swab16(rxcp->vlan_tag);
Sathya Perla2e588f82011-03-11 02:49:26 +00001303
Somnath Kotur939cf302011-08-18 21:51:49 -07001304 if (adapter->pvid == (rxcp->vlan_tag & VLAN_VID_MASK) &&
David S. Miller3c709f82011-05-11 14:26:15 -04001305 !adapter->vlan_tag[rxcp->vlan_tag])
Sathya Perla15d72182011-03-21 20:49:26 +00001306 rxcp->vlanf = 0;
1307 }
Sathya Perla2e588f82011-03-11 02:49:26 +00001308
1309 /* As the compl has been parsed, reset it; we wont touch it again */
1310 compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001311
Sathya Perla3abcded2010-10-03 22:12:27 -07001312 queue_tail_inc(&rxo->cq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001313 return rxcp;
1314}
1315
Eric Dumazet1829b082011-03-01 05:48:12 +00001316static inline struct page *be_alloc_pages(u32 size, gfp_t gfp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001317{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001318 u32 order = get_order(size);
Eric Dumazet1829b082011-03-01 05:48:12 +00001319
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001320 if (order > 0)
Eric Dumazet1829b082011-03-01 05:48:12 +00001321 gfp |= __GFP_COMP;
1322 return alloc_pages(gfp, order);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001323}
1324
1325/*
1326 * Allocate a page, split it to fragments of size rx_frag_size and post as
1327 * receive buffers to BE
1328 */
Eric Dumazet1829b082011-03-01 05:48:12 +00001329static void be_post_rx_frags(struct be_rx_obj *rxo, gfp_t gfp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001330{
Sathya Perla3abcded2010-10-03 22:12:27 -07001331 struct be_adapter *adapter = rxo->adapter;
1332 struct be_rx_page_info *page_info_tbl = rxo->page_info_tbl;
Sathya Perla26d92f92010-01-21 22:52:08 -08001333 struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
Sathya Perla3abcded2010-10-03 22:12:27 -07001334 struct be_queue_info *rxq = &rxo->q;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001335 struct page *pagep = NULL;
1336 struct be_eth_rx_d *rxd;
1337 u64 page_dmaaddr = 0, frag_dmaaddr;
1338 u32 posted, page_offset = 0;
1339
Sathya Perla3abcded2010-10-03 22:12:27 -07001340 page_info = &rxo->page_info_tbl[rxq->head];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001341 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
1342 if (!pagep) {
Eric Dumazet1829b082011-03-01 05:48:12 +00001343 pagep = be_alloc_pages(adapter->big_page_size, gfp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001344 if (unlikely(!pagep)) {
Sathya Perlaac124ff2011-07-25 19:10:14 +00001345 rx_stats(rxo)->rx_post_fail++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001346 break;
1347 }
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00001348 page_dmaaddr = dma_map_page(&adapter->pdev->dev, pagep,
1349 0, adapter->big_page_size,
1350 DMA_FROM_DEVICE);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001351 page_info->page_offset = 0;
1352 } else {
1353 get_page(pagep);
1354 page_info->page_offset = page_offset + rx_frag_size;
1355 }
1356 page_offset = page_info->page_offset;
1357 page_info->page = pagep;
FUJITA Tomonorifac6da52010-04-01 16:53:22 +00001358 dma_unmap_addr_set(page_info, bus, page_dmaaddr);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001359 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
1360
1361 rxd = queue_head_node(rxq);
1362 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
1363 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001364
1365 /* Any space left in the current big page for another frag? */
1366 if ((page_offset + rx_frag_size + rx_frag_size) >
1367 adapter->big_page_size) {
1368 pagep = NULL;
1369 page_info->last_page_user = true;
1370 }
Sathya Perla26d92f92010-01-21 22:52:08 -08001371
1372 prev_page_info = page_info;
1373 queue_head_inc(rxq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001374 page_info = &page_info_tbl[rxq->head];
1375 }
1376 if (pagep)
Sathya Perla26d92f92010-01-21 22:52:08 -08001377 prev_page_info->last_page_user = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001378
1379 if (posted) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001380 atomic_add(posted, &rxq->used);
Sathya Perla8788fdc2009-07-27 22:52:03 +00001381 be_rxq_notify(adapter, rxq->id, posted);
Sathya Perlaea1dae12009-03-19 23:56:20 -07001382 } else if (atomic_read(&rxq->used) == 0) {
1383 /* Let be_worker replenish when memory is available */
Sathya Perla3abcded2010-10-03 22:12:27 -07001384 rxo->rx_post_starved = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001385 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001386}
1387
Sathya Perla5fb379e2009-06-18 00:02:59 +00001388static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001389{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001390 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
1391
1392 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
1393 return NULL;
1394
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001395 rmb();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001396 be_dws_le_to_cpu(txcp, sizeof(*txcp));
1397
1398 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
1399
1400 queue_tail_inc(tx_cq);
1401 return txcp;
1402}
1403
Sathya Perla3c8def92011-06-12 20:01:58 +00001404static u16 be_tx_compl_process(struct be_adapter *adapter,
1405 struct be_tx_obj *txo, u16 last_index)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001406{
Sathya Perla3c8def92011-06-12 20:01:58 +00001407 struct be_queue_info *txq = &txo->q;
Alexander Duycka73b7962009-12-02 16:48:18 +00001408 struct be_eth_wrb *wrb;
Sathya Perla3c8def92011-06-12 20:01:58 +00001409 struct sk_buff **sent_skbs = txo->sent_skb_list;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001410 struct sk_buff *sent_skb;
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001411 u16 cur_index, num_wrbs = 1; /* account for hdr wrb */
1412 bool unmap_skb_hdr = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001413
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001414 sent_skb = sent_skbs[txq->tail];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001415 BUG_ON(!sent_skb);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001416 sent_skbs[txq->tail] = NULL;
1417
1418 /* skip header wrb */
Alexander Duycka73b7962009-12-02 16:48:18 +00001419 queue_tail_inc(txq);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001420
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001421 do {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001422 cur_index = txq->tail;
Alexander Duycka73b7962009-12-02 16:48:18 +00001423 wrb = queue_tail_node(txq);
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00001424 unmap_tx_frag(&adapter->pdev->dev, wrb,
1425 (unmap_skb_hdr && skb_headlen(sent_skb)));
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001426 unmap_skb_hdr = false;
1427
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001428 num_wrbs++;
1429 queue_tail_inc(txq);
Sathya Perlaec43b1a2010-03-22 20:41:34 +00001430 } while (cur_index != last_index);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001431
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001432 kfree_skb(sent_skb);
Padmanabh Ratnakar4d586b82011-05-10 05:13:57 +00001433 return num_wrbs;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001434}
1435
Sathya Perla859b1e42009-08-10 03:43:51 +00001436static inline struct be_eq_entry *event_get(struct be_eq_obj *eq_obj)
1437{
1438 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1439
1440 if (!eqe->evt)
1441 return NULL;
1442
Sathya Perlaf3eb62d2010-06-29 00:11:17 +00001443 rmb();
Sathya Perla859b1e42009-08-10 03:43:51 +00001444 eqe->evt = le32_to_cpu(eqe->evt);
1445 queue_tail_inc(&eq_obj->q);
1446 return eqe;
1447}
1448
1449static int event_handle(struct be_adapter *adapter,
Sathya Perla3c8def92011-06-12 20:01:58 +00001450 struct be_eq_obj *eq_obj,
1451 bool rearm)
Sathya Perla859b1e42009-08-10 03:43:51 +00001452{
1453 struct be_eq_entry *eqe;
1454 u16 num = 0;
1455
1456 while ((eqe = event_get(eq_obj)) != NULL) {
1457 eqe->evt = 0;
1458 num++;
1459 }
1460
1461 /* Deal with any spurious interrupts that come
1462 * without events
1463 */
Sathya Perla3c8def92011-06-12 20:01:58 +00001464 if (!num)
1465 rearm = true;
1466
1467 be_eq_notify(adapter, eq_obj->q.id, rearm, true, num);
Sathya Perla859b1e42009-08-10 03:43:51 +00001468 if (num)
1469 napi_schedule(&eq_obj->napi);
1470
1471 return num;
1472}
1473
1474/* Just read and notify events without processing them.
1475 * Used at the time of destroying event queues */
1476static void be_eq_clean(struct be_adapter *adapter,
1477 struct be_eq_obj *eq_obj)
1478{
1479 struct be_eq_entry *eqe;
1480 u16 num = 0;
1481
1482 while ((eqe = event_get(eq_obj)) != NULL) {
1483 eqe->evt = 0;
1484 num++;
1485 }
1486
1487 if (num)
1488 be_eq_notify(adapter, eq_obj->q.id, false, true, num);
1489}
1490
Sathya Perla3abcded2010-10-03 22:12:27 -07001491static void be_rx_q_clean(struct be_adapter *adapter, struct be_rx_obj *rxo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001492{
1493 struct be_rx_page_info *page_info;
Sathya Perla3abcded2010-10-03 22:12:27 -07001494 struct be_queue_info *rxq = &rxo->q;
1495 struct be_queue_info *rx_cq = &rxo->cq;
Sathya Perla2e588f82011-03-11 02:49:26 +00001496 struct be_rx_compl_info *rxcp;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001497 u16 tail;
1498
1499 /* First cleanup pending rx completions */
Sathya Perla3abcded2010-10-03 22:12:27 -07001500 while ((rxcp = be_rx_compl_get(rxo)) != NULL) {
1501 be_rx_compl_discard(adapter, rxo, rxcp);
Sathya Perla64642812010-12-01 01:04:17 +00001502 be_cq_notify(adapter, rx_cq->id, false, 1);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001503 }
1504
1505 /* Then free posted rx buffer that were not used */
1506 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
Sathya Perlacdab23b2009-08-10 03:43:23 +00001507 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001508 page_info = get_rx_page_info(adapter, rxo, tail);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001509 put_page(page_info->page);
1510 memset(page_info, 0, sizeof(*page_info));
1511 }
1512 BUG_ON(atomic_read(&rxq->used));
Sathya Perla482c9e72011-06-29 23:33:17 +00001513 rxq->tail = rxq->head = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001514}
1515
Sathya Perla3c8def92011-06-12 20:01:58 +00001516static void be_tx_compl_clean(struct be_adapter *adapter,
1517 struct be_tx_obj *txo)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001518{
Sathya Perla3c8def92011-06-12 20:01:58 +00001519 struct be_queue_info *tx_cq = &txo->cq;
1520 struct be_queue_info *txq = &txo->q;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001521 struct be_eth_tx_compl *txcp;
Padmanabh Ratnakar4d586b82011-05-10 05:13:57 +00001522 u16 end_idx, cmpl = 0, timeo = 0, num_wrbs = 0;
Sathya Perla3c8def92011-06-12 20:01:58 +00001523 struct sk_buff **sent_skbs = txo->sent_skb_list;
Sathya Perlab03388d2010-02-18 00:37:17 +00001524 struct sk_buff *sent_skb;
1525 bool dummy_wrb;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001526
Sathya Perlaa8e91792009-08-10 03:42:43 +00001527 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1528 do {
1529 while ((txcp = be_tx_compl_get(tx_cq))) {
1530 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1531 wrb_index, txcp);
Sathya Perla3c8def92011-06-12 20:01:58 +00001532 num_wrbs += be_tx_compl_process(adapter, txo, end_idx);
Sathya Perlaa8e91792009-08-10 03:42:43 +00001533 cmpl++;
1534 }
1535 if (cmpl) {
1536 be_cq_notify(adapter, tx_cq->id, false, cmpl);
Padmanabh Ratnakar4d586b82011-05-10 05:13:57 +00001537 atomic_sub(num_wrbs, &txq->used);
Sathya Perlaa8e91792009-08-10 03:42:43 +00001538 cmpl = 0;
Padmanabh Ratnakar4d586b82011-05-10 05:13:57 +00001539 num_wrbs = 0;
Sathya Perlaa8e91792009-08-10 03:42:43 +00001540 }
1541
1542 if (atomic_read(&txq->used) == 0 || ++timeo > 200)
1543 break;
1544
1545 mdelay(1);
1546 } while (true);
1547
1548 if (atomic_read(&txq->used))
1549 dev_err(&adapter->pdev->dev, "%d pending tx-completions\n",
1550 atomic_read(&txq->used));
Sathya Perlab03388d2010-02-18 00:37:17 +00001551
1552 /* free posted tx for which compls will never arrive */
1553 while (atomic_read(&txq->used)) {
1554 sent_skb = sent_skbs[txq->tail];
1555 end_idx = txq->tail;
1556 index_adv(&end_idx,
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001557 wrb_cnt_for_skb(adapter, sent_skb, &dummy_wrb) - 1,
1558 txq->len);
Sathya Perla3c8def92011-06-12 20:01:58 +00001559 num_wrbs = be_tx_compl_process(adapter, txo, end_idx);
Padmanabh Ratnakar4d586b82011-05-10 05:13:57 +00001560 atomic_sub(num_wrbs, &txq->used);
Sathya Perlab03388d2010-02-18 00:37:17 +00001561 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001562}
1563
Sathya Perla5fb379e2009-06-18 00:02:59 +00001564static void be_mcc_queues_destroy(struct be_adapter *adapter)
1565{
1566 struct be_queue_info *q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001567
Sathya Perla8788fdc2009-07-27 22:52:03 +00001568 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001569 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001570 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001571 be_queue_free(adapter, q);
1572
Sathya Perla8788fdc2009-07-27 22:52:03 +00001573 q = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001574 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001575 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001576 be_queue_free(adapter, q);
1577}
1578
1579/* Must be called only after TX qs are created as MCC shares TX EQ */
1580static int be_mcc_queues_create(struct be_adapter *adapter)
1581{
1582 struct be_queue_info *q, *cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001583
1584 /* Alloc MCC compl queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001585 cq = &adapter->mcc_obj.cq;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001586 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
Sathya Perlaefd2e402009-07-27 22:53:10 +00001587 sizeof(struct be_mcc_compl)))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001588 goto err;
1589
1590 /* Ask BE to create MCC compl queue; share TX's eq */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001591 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001592 goto mcc_cq_free;
1593
1594 /* Alloc MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001595 q = &adapter->mcc_obj.q;
Sathya Perla5fb379e2009-06-18 00:02:59 +00001596 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1597 goto mcc_cq_destroy;
1598
1599 /* Ask BE to create MCC queue */
Sathya Perla8788fdc2009-07-27 22:52:03 +00001600 if (be_cmd_mccq_create(adapter, q, cq))
Sathya Perla5fb379e2009-06-18 00:02:59 +00001601 goto mcc_q_free;
1602
1603 return 0;
1604
1605mcc_q_free:
1606 be_queue_free(adapter, q);
1607mcc_cq_destroy:
Sathya Perla8788fdc2009-07-27 22:52:03 +00001608 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001609mcc_cq_free:
1610 be_queue_free(adapter, cq);
1611err:
1612 return -1;
1613}
1614
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001615static void be_tx_queues_destroy(struct be_adapter *adapter)
1616{
1617 struct be_queue_info *q;
Sathya Perla3c8def92011-06-12 20:01:58 +00001618 struct be_tx_obj *txo;
1619 u8 i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001620
Sathya Perla3c8def92011-06-12 20:01:58 +00001621 for_all_tx_queues(adapter, txo, i) {
1622 q = &txo->q;
1623 if (q->created)
1624 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
1625 be_queue_free(adapter, q);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001626
Sathya Perla3c8def92011-06-12 20:01:58 +00001627 q = &txo->cq;
1628 if (q->created)
1629 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1630 be_queue_free(adapter, q);
1631 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001632
Sathya Perla859b1e42009-08-10 03:43:51 +00001633 /* Clear any residual events */
1634 be_eq_clean(adapter, &adapter->tx_eq);
1635
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001636 q = &adapter->tx_eq.q;
1637 if (q->created)
Sathya Perla8788fdc2009-07-27 22:52:03 +00001638 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001639 be_queue_free(adapter, q);
1640}
1641
Sathya Perladafc0fe2011-10-24 02:45:02 +00001642static int be_num_txqs_want(struct be_adapter *adapter)
1643{
1644 if ((num_vfs && adapter->sriov_enabled) ||
Sathya Perla752961a2011-10-24 02:45:03 +00001645 be_is_mc(adapter) ||
Sathya Perladafc0fe2011-10-24 02:45:02 +00001646 lancer_chip(adapter) || !be_physfn(adapter) ||
1647 adapter->generation == BE_GEN2)
1648 return 1;
1649 else
1650 return MAX_TX_QS;
1651}
1652
Sathya Perla3c8def92011-06-12 20:01:58 +00001653/* One TX event queue is shared by all TX compl qs */
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001654static int be_tx_queues_create(struct be_adapter *adapter)
1655{
1656 struct be_queue_info *eq, *q, *cq;
Sathya Perla3c8def92011-06-12 20:01:58 +00001657 struct be_tx_obj *txo;
1658 u8 i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001659
Sathya Perladafc0fe2011-10-24 02:45:02 +00001660 adapter->num_tx_qs = be_num_txqs_want(adapter);
1661 if (adapter->num_tx_qs != MAX_TX_QS)
1662 netif_set_real_num_tx_queues(adapter->netdev,
1663 adapter->num_tx_qs);
1664
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001665 adapter->tx_eq.max_eqd = 0;
1666 adapter->tx_eq.min_eqd = 0;
1667 adapter->tx_eq.cur_eqd = 96;
1668 adapter->tx_eq.enable_aic = false;
Sathya Perla3c8def92011-06-12 20:01:58 +00001669
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001670 eq = &adapter->tx_eq.q;
Sathya Perla3c8def92011-06-12 20:01:58 +00001671 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1672 sizeof(struct be_eq_entry)))
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001673 return -1;
1674
Sathya Perla8788fdc2009-07-27 22:52:03 +00001675 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
Sathya Perla3c8def92011-06-12 20:01:58 +00001676 goto err;
Padmanabh Ratnakarecd62102011-04-03 01:54:11 +00001677 adapter->tx_eq.eq_idx = adapter->eq_next_idx++;
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001678
Sathya Perla3c8def92011-06-12 20:01:58 +00001679 for_all_tx_queues(adapter, txo, i) {
1680 cq = &txo->cq;
1681 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001682 sizeof(struct be_eth_tx_compl)))
Sathya Perla3c8def92011-06-12 20:01:58 +00001683 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001684
Sathya Perla3c8def92011-06-12 20:01:58 +00001685 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
1686 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001687
Sathya Perla3c8def92011-06-12 20:01:58 +00001688 q = &txo->q;
1689 if (be_queue_alloc(adapter, q, TX_Q_LEN,
1690 sizeof(struct be_eth_wrb)))
1691 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001692
Sathya Perla3c8def92011-06-12 20:01:58 +00001693 if (be_cmd_txq_create(adapter, q, cq))
1694 goto err;
1695 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001696 return 0;
1697
Sathya Perla3c8def92011-06-12 20:01:58 +00001698err:
1699 be_tx_queues_destroy(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001700 return -1;
1701}
1702
1703static void be_rx_queues_destroy(struct be_adapter *adapter)
1704{
1705 struct be_queue_info *q;
Sathya Perla3abcded2010-10-03 22:12:27 -07001706 struct be_rx_obj *rxo;
1707 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001708
Sathya Perla3abcded2010-10-03 22:12:27 -07001709 for_all_rx_queues(adapter, rxo, i) {
Sathya Perla482c9e72011-06-29 23:33:17 +00001710 be_queue_free(adapter, &rxo->q);
Sathya Perla89420422010-02-17 01:35:26 +00001711
Sathya Perla3abcded2010-10-03 22:12:27 -07001712 q = &rxo->cq;
1713 if (q->created)
1714 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1715 be_queue_free(adapter, q);
1716
Sathya Perla3abcded2010-10-03 22:12:27 -07001717 q = &rxo->rx_eq.q;
Sathya Perla482c9e72011-06-29 23:33:17 +00001718 if (q->created)
Sathya Perla3abcded2010-10-03 22:12:27 -07001719 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
Sathya Perla3abcded2010-10-03 22:12:27 -07001720 be_queue_free(adapter, q);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001721 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001722}
1723
Sathya Perlaac6a0c42011-03-21 20:49:25 +00001724static u32 be_num_rxqs_want(struct be_adapter *adapter)
1725{
Sathya Perlac814fd32011-06-26 20:41:25 +00001726 if ((adapter->function_caps & BE_FUNCTION_CAPS_RSS) &&
Sathya Perla752961a2011-10-24 02:45:03 +00001727 !adapter->sriov_enabled && be_physfn(adapter) &&
1728 !be_is_mc(adapter)) {
Sathya Perlaac6a0c42011-03-21 20:49:25 +00001729 return 1 + MAX_RSS_QS; /* one default non-RSS queue */
1730 } else {
1731 dev_warn(&adapter->pdev->dev,
1732 "No support for multiple RX queues\n");
1733 return 1;
1734 }
1735}
1736
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001737static int be_rx_queues_create(struct be_adapter *adapter)
1738{
1739 struct be_queue_info *eq, *q, *cq;
Sathya Perla3abcded2010-10-03 22:12:27 -07001740 struct be_rx_obj *rxo;
1741 int rc, i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001742
Sathya Perlaac6a0c42011-03-21 20:49:25 +00001743 adapter->num_rx_qs = min(be_num_rxqs_want(adapter),
1744 msix_enabled(adapter) ?
1745 adapter->num_msix_vec - 1 : 1);
1746 if (adapter->num_rx_qs != MAX_RX_QS)
1747 dev_warn(&adapter->pdev->dev,
1748 "Can create only %d RX queues", adapter->num_rx_qs);
1749
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001750 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
Sathya Perla3abcded2010-10-03 22:12:27 -07001751 for_all_rx_queues(adapter, rxo, i) {
1752 rxo->adapter = adapter;
1753 rxo->rx_eq.max_eqd = BE_MAX_EQD;
1754 rxo->rx_eq.enable_aic = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001755
Sathya Perla3abcded2010-10-03 22:12:27 -07001756 /* EQ */
1757 eq = &rxo->rx_eq.q;
1758 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1759 sizeof(struct be_eq_entry));
1760 if (rc)
1761 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001762
Sathya Perla3abcded2010-10-03 22:12:27 -07001763 rc = be_cmd_eq_create(adapter, eq, rxo->rx_eq.cur_eqd);
1764 if (rc)
1765 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001766
Padmanabh Ratnakarecd62102011-04-03 01:54:11 +00001767 rxo->rx_eq.eq_idx = adapter->eq_next_idx++;
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001768
Sathya Perla3abcded2010-10-03 22:12:27 -07001769 /* CQ */
1770 cq = &rxo->cq;
1771 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1772 sizeof(struct be_eth_rx_compl));
1773 if (rc)
1774 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001775
Sathya Perla3abcded2010-10-03 22:12:27 -07001776 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
1777 if (rc)
1778 goto err;
Sathya Perla482c9e72011-06-29 23:33:17 +00001779
1780 /* Rx Q - will be created in be_open() */
Sathya Perla3abcded2010-10-03 22:12:27 -07001781 q = &rxo->q;
1782 rc = be_queue_alloc(adapter, q, RX_Q_LEN,
1783 sizeof(struct be_eth_rx_d));
1784 if (rc)
1785 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001786
Sathya Perla3abcded2010-10-03 22:12:27 -07001787 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001788
1789 return 0;
Sathya Perla3abcded2010-10-03 22:12:27 -07001790err:
1791 be_rx_queues_destroy(adapter);
1792 return -1;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001793}
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001794
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001795static bool event_peek(struct be_eq_obj *eq_obj)
Sathya Perlab628bde2009-08-17 00:58:26 +00001796{
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001797 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1798 if (!eqe->evt)
1799 return false;
1800 else
1801 return true;
Sathya Perlab628bde2009-08-17 00:58:26 +00001802}
1803
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001804static irqreturn_t be_intx(int irq, void *dev)
1805{
1806 struct be_adapter *adapter = dev;
Sathya Perla3abcded2010-10-03 22:12:27 -07001807 struct be_rx_obj *rxo;
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001808 int isr, i, tx = 0 , rx = 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001809
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001810 if (lancer_chip(adapter)) {
1811 if (event_peek(&adapter->tx_eq))
Sathya Perla3c8def92011-06-12 20:01:58 +00001812 tx = event_handle(adapter, &adapter->tx_eq, false);
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001813 for_all_rx_queues(adapter, rxo, i) {
1814 if (event_peek(&rxo->rx_eq))
Sathya Perla3c8def92011-06-12 20:01:58 +00001815 rx |= event_handle(adapter, &rxo->rx_eq, true);
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001816 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001817
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001818 if (!(tx || rx))
1819 return IRQ_NONE;
Sathya Perla3abcded2010-10-03 22:12:27 -07001820
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001821 } else {
1822 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
1823 (adapter->tx_eq.q.id / 8) * CEV_ISR_SIZE);
1824 if (!isr)
1825 return IRQ_NONE;
1826
Padmanabh Ratnakarecd62102011-04-03 01:54:11 +00001827 if ((1 << adapter->tx_eq.eq_idx & isr))
Sathya Perla3c8def92011-06-12 20:01:58 +00001828 event_handle(adapter, &adapter->tx_eq, false);
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001829
1830 for_all_rx_queues(adapter, rxo, i) {
Padmanabh Ratnakarecd62102011-04-03 01:54:11 +00001831 if ((1 << rxo->rx_eq.eq_idx & isr))
Sathya Perla3c8def92011-06-12 20:01:58 +00001832 event_handle(adapter, &rxo->rx_eq, true);
Sathya Perlafe6d2a32010-11-21 23:25:50 +00001833 }
Sathya Perla3abcded2010-10-03 22:12:27 -07001834 }
Sathya Perlac001c212009-07-01 01:06:07 +00001835
Sathya Perla8788fdc2009-07-27 22:52:03 +00001836 return IRQ_HANDLED;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001837}
1838
1839static irqreturn_t be_msix_rx(int irq, void *dev)
1840{
Sathya Perla3abcded2010-10-03 22:12:27 -07001841 struct be_rx_obj *rxo = dev;
1842 struct be_adapter *adapter = rxo->adapter;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001843
Sathya Perla3c8def92011-06-12 20:01:58 +00001844 event_handle(adapter, &rxo->rx_eq, true);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001845
1846 return IRQ_HANDLED;
1847}
1848
Sathya Perla5fb379e2009-06-18 00:02:59 +00001849static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001850{
1851 struct be_adapter *adapter = dev;
1852
Sathya Perla3c8def92011-06-12 20:01:58 +00001853 event_handle(adapter, &adapter->tx_eq, false);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001854
1855 return IRQ_HANDLED;
1856}
1857
Sathya Perla2e588f82011-03-11 02:49:26 +00001858static inline bool do_gro(struct be_rx_compl_info *rxcp)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001859{
Sathya Perla2e588f82011-03-11 02:49:26 +00001860 return (rxcp->tcpf && !rxcp->err) ? true : false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001861}
1862
stephen hemminger49b05222010-10-21 07:50:48 +00001863static int be_poll_rx(struct napi_struct *napi, int budget)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001864{
1865 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
Sathya Perla3abcded2010-10-03 22:12:27 -07001866 struct be_rx_obj *rxo = container_of(rx_eq, struct be_rx_obj, rx_eq);
1867 struct be_adapter *adapter = rxo->adapter;
1868 struct be_queue_info *rx_cq = &rxo->cq;
Sathya Perla2e588f82011-03-11 02:49:26 +00001869 struct be_rx_compl_info *rxcp;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001870 u32 work_done;
1871
Sathya Perlaac124ff2011-07-25 19:10:14 +00001872 rx_stats(rxo)->rx_polls++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001873 for (work_done = 0; work_done < budget; work_done++) {
Sathya Perla3abcded2010-10-03 22:12:27 -07001874 rxcp = be_rx_compl_get(rxo);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001875 if (!rxcp)
1876 break;
1877
Sathya Perla12004ae2011-08-02 19:57:46 +00001878 /* Is it a flush compl that has no data */
1879 if (unlikely(rxcp->num_rcvd == 0))
1880 goto loop_continue;
1881
1882 /* Discard compl with partial DMA Lancer B0 */
1883 if (unlikely(!rxcp->pkt_size)) {
Padmanabh Ratnakar009dd872011-05-10 05:12:17 +00001884 be_rx_compl_discard(adapter, rxo, rxcp);
Sathya Perla12004ae2011-08-02 19:57:46 +00001885 goto loop_continue;
Sathya Perla64642812010-12-01 01:04:17 +00001886 }
Padmanabh Ratnakar009dd872011-05-10 05:12:17 +00001887
Sathya Perla12004ae2011-08-02 19:57:46 +00001888 /* On BE drop pkts that arrive due to imperfect filtering in
1889 * promiscuous mode on some skews
1890 */
1891 if (unlikely(rxcp->port != adapter->port_num &&
1892 !lancer_chip(adapter))) {
1893 be_rx_compl_discard(adapter, rxo, rxcp);
1894 goto loop_continue;
1895 }
1896
1897 if (do_gro(rxcp))
1898 be_rx_compl_process_gro(adapter, rxo, rxcp);
1899 else
1900 be_rx_compl_process(adapter, rxo, rxcp);
1901loop_continue:
Sathya Perla2e588f82011-03-11 02:49:26 +00001902 be_rx_stats_update(rxo, rxcp);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001903 }
1904
Padmanabh Ratnakar9372cac2011-11-03 01:49:55 +00001905 be_cq_notify(adapter, rx_cq->id, false, work_done);
1906
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001907 /* Refill the queue */
Sathya Perla857c9902011-08-22 19:41:51 +00001908 if (work_done && atomic_read(&rxo->q.used) < RX_FRAGS_REFILL_WM)
Eric Dumazet1829b082011-03-01 05:48:12 +00001909 be_post_rx_frags(rxo, GFP_ATOMIC);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001910
1911 /* All consumed */
1912 if (work_done < budget) {
1913 napi_complete(napi);
Padmanabh Ratnakar9372cac2011-11-03 01:49:55 +00001914 /* Arm CQ */
1915 be_cq_notify(adapter, rx_cq->id, true, 0);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001916 }
1917 return work_done;
1918}
1919
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001920/* As TX and MCC share the same EQ check for both TX and MCC completions.
1921 * For TX/MCC we don't honour budget; consume everything
1922 */
1923static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001924{
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001925 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1926 struct be_adapter *adapter =
1927 container_of(tx_eq, struct be_adapter, tx_eq);
Sathya Perla3c8def92011-06-12 20:01:58 +00001928 struct be_tx_obj *txo;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001929 struct be_eth_tx_compl *txcp;
Sathya Perla3c8def92011-06-12 20:01:58 +00001930 int tx_compl, mcc_compl, status = 0;
1931 u8 i;
1932 u16 num_wrbs;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001933
Sathya Perla3c8def92011-06-12 20:01:58 +00001934 for_all_tx_queues(adapter, txo, i) {
1935 tx_compl = 0;
1936 num_wrbs = 0;
1937 while ((txcp = be_tx_compl_get(&txo->cq))) {
1938 num_wrbs += be_tx_compl_process(adapter, txo,
1939 AMAP_GET_BITS(struct amap_eth_tx_compl,
1940 wrb_index, txcp));
1941 tx_compl++;
1942 }
1943 if (tx_compl) {
1944 be_cq_notify(adapter, txo->cq.id, true, tx_compl);
1945
1946 atomic_sub(num_wrbs, &txo->q.used);
1947
1948 /* As Tx wrbs have been freed up, wake up netdev queue
1949 * if it was stopped due to lack of tx wrbs. */
1950 if (__netif_subqueue_stopped(adapter->netdev, i) &&
1951 atomic_read(&txo->q.used) < txo->q.len / 2) {
1952 netif_wake_subqueue(adapter->netdev, i);
1953 }
1954
Sathya Perlaab1594e2011-07-25 19:10:15 +00001955 u64_stats_update_begin(&tx_stats(txo)->sync_compl);
Sathya Perlaac124ff2011-07-25 19:10:14 +00001956 tx_stats(txo)->tx_compl += tx_compl;
Sathya Perlaab1594e2011-07-25 19:10:15 +00001957 u64_stats_update_end(&tx_stats(txo)->sync_compl);
Sathya Perla3c8def92011-06-12 20:01:58 +00001958 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001959 }
1960
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001961 mcc_compl = be_process_mcc(adapter, &status);
1962
Sathya Perlaf31e50a2010-03-02 03:56:39 -08001963 if (mcc_compl) {
1964 struct be_mcc_obj *mcc_obj = &adapter->mcc_obj;
1965 be_cq_notify(adapter, mcc_obj->cq.id, true, mcc_compl);
1966 }
1967
Sathya Perla3c8def92011-06-12 20:01:58 +00001968 napi_complete(napi);
Sathya Perla5fb379e2009-06-18 00:02:59 +00001969
Sathya Perla3c8def92011-06-12 20:01:58 +00001970 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perlaab1594e2011-07-25 19:10:15 +00001971 adapter->drv_stats.tx_events++;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07001972 return 1;
1973}
1974
Ajit Khaparded053de92010-09-03 06:23:30 +00001975void be_detect_dump_ue(struct be_adapter *adapter)
Ajit Khaparde7c185272010-07-29 06:16:33 +00001976{
Padmanabh Ratnakare1cfb672011-11-03 01:50:08 +00001977 u32 ue_lo = 0, ue_hi = 0, ue_lo_mask = 0, ue_hi_mask = 0;
1978 u32 sliport_status = 0, sliport_err1 = 0, sliport_err2 = 0;
Ajit Khaparde7c185272010-07-29 06:16:33 +00001979 u32 i;
1980
Sathya Perla72f02482011-11-10 19:17:58 +00001981 if (adapter->eeh_err || adapter->ue_detected)
1982 return;
1983
Padmanabh Ratnakare1cfb672011-11-03 01:50:08 +00001984 if (lancer_chip(adapter)) {
1985 sliport_status = ioread32(adapter->db + SLIPORT_STATUS_OFFSET);
1986 if (sliport_status & SLIPORT_STATUS_ERR_MASK) {
1987 sliport_err1 = ioread32(adapter->db +
1988 SLIPORT_ERROR1_OFFSET);
1989 sliport_err2 = ioread32(adapter->db +
1990 SLIPORT_ERROR2_OFFSET);
1991 }
1992 } else {
1993 pci_read_config_dword(adapter->pdev,
1994 PCICFG_UE_STATUS_LOW, &ue_lo);
1995 pci_read_config_dword(adapter->pdev,
1996 PCICFG_UE_STATUS_HIGH, &ue_hi);
1997 pci_read_config_dword(adapter->pdev,
1998 PCICFG_UE_STATUS_LOW_MASK, &ue_lo_mask);
1999 pci_read_config_dword(adapter->pdev,
2000 PCICFG_UE_STATUS_HI_MASK, &ue_hi_mask);
Ajit Khaparde7c185272010-07-29 06:16:33 +00002001
Padmanabh Ratnakare1cfb672011-11-03 01:50:08 +00002002 ue_lo = (ue_lo & (~ue_lo_mask));
2003 ue_hi = (ue_hi & (~ue_hi_mask));
2004 }
Ajit Khaparde7c185272010-07-29 06:16:33 +00002005
Padmanabh Ratnakare1cfb672011-11-03 01:50:08 +00002006 if (ue_lo || ue_hi ||
2007 sliport_status & SLIPORT_STATUS_ERR_MASK) {
Ajit Khaparded053de92010-09-03 06:23:30 +00002008 adapter->ue_detected = true;
Ajit Khaparde7acc2082011-02-11 13:38:17 +00002009 adapter->eeh_err = true;
Sathya Perla434b3642011-11-10 19:17:59 +00002010 dev_err(&adapter->pdev->dev,
2011 "Unrecoverable error in the card\n");
Ajit Khaparded053de92010-09-03 06:23:30 +00002012 }
2013
Padmanabh Ratnakare1cfb672011-11-03 01:50:08 +00002014 if (ue_lo) {
2015 for (i = 0; ue_lo; ue_lo >>= 1, i++) {
2016 if (ue_lo & 1)
Ajit Khaparde7c185272010-07-29 06:16:33 +00002017 dev_err(&adapter->pdev->dev,
2018 "UE: %s bit set\n", ue_status_low_desc[i]);
2019 }
2020 }
Padmanabh Ratnakare1cfb672011-11-03 01:50:08 +00002021 if (ue_hi) {
2022 for (i = 0; ue_hi; ue_hi >>= 1, i++) {
2023 if (ue_hi & 1)
Ajit Khaparde7c185272010-07-29 06:16:33 +00002024 dev_err(&adapter->pdev->dev,
2025 "UE: %s bit set\n", ue_status_hi_desc[i]);
2026 }
2027 }
2028
Padmanabh Ratnakare1cfb672011-11-03 01:50:08 +00002029 if (sliport_status & SLIPORT_STATUS_ERR_MASK) {
2030 dev_err(&adapter->pdev->dev,
2031 "sliport status 0x%x\n", sliport_status);
2032 dev_err(&adapter->pdev->dev,
2033 "sliport error1 0x%x\n", sliport_err1);
2034 dev_err(&adapter->pdev->dev,
2035 "sliport error2 0x%x\n", sliport_err2);
2036 }
Ajit Khaparde7c185272010-07-29 06:16:33 +00002037}
2038
Sathya Perlaea1dae12009-03-19 23:56:20 -07002039static void be_worker(struct work_struct *work)
2040{
2041 struct be_adapter *adapter =
2042 container_of(work, struct be_adapter, work.work);
Sathya Perla3abcded2010-10-03 22:12:27 -07002043 struct be_rx_obj *rxo;
2044 int i;
Sathya Perlaea1dae12009-03-19 23:56:20 -07002045
Sathya Perla72f02482011-11-10 19:17:58 +00002046 be_detect_dump_ue(adapter);
Sathya Perla16da8252011-03-21 20:49:27 +00002047
Somnath Koturf203af72010-10-25 23:01:03 +00002048 /* when interrupts are not yet enabled, just reap any pending
2049 * mcc completions */
2050 if (!netif_running(adapter->netdev)) {
2051 int mcc_compl, status = 0;
2052
2053 mcc_compl = be_process_mcc(adapter, &status);
2054
2055 if (mcc_compl) {
2056 struct be_mcc_obj *mcc_obj = &adapter->mcc_obj;
2057 be_cq_notify(adapter, mcc_obj->cq.id, false, mcc_compl);
2058 }
Ajit Khaparde9b037f32011-02-11 13:38:29 +00002059
Somnath Koturf203af72010-10-25 23:01:03 +00002060 goto reschedule;
2061 }
2062
Selvin Xavier005d5692011-05-16 07:36:35 +00002063 if (!adapter->stats_cmd_sent) {
2064 if (lancer_chip(adapter))
2065 lancer_cmd_get_pport_stats(adapter,
2066 &adapter->stats_cmd);
2067 else
2068 be_cmd_get_stats(adapter, &adapter->stats_cmd);
2069 }
Sathya Perla3c8def92011-06-12 20:01:58 +00002070
Sathya Perla3abcded2010-10-03 22:12:27 -07002071 for_all_rx_queues(adapter, rxo, i) {
Sathya Perla3abcded2010-10-03 22:12:27 -07002072 be_rx_eqd_update(adapter, rxo);
2073
2074 if (rxo->rx_post_starved) {
2075 rxo->rx_post_starved = false;
Eric Dumazet1829b082011-03-01 05:48:12 +00002076 be_post_rx_frags(rxo, GFP_KERNEL);
Sathya Perla3abcded2010-10-03 22:12:27 -07002077 }
Sathya Perlaea1dae12009-03-19 23:56:20 -07002078 }
2079
Somnath Koturf203af72010-10-25 23:01:03 +00002080reschedule:
Ivan Vecerae74fbd032011-04-21 00:20:04 +00002081 adapter->work_counter++;
Sathya Perlaea1dae12009-03-19 23:56:20 -07002082 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
2083}
2084
Sathya Perla8d56ff12009-11-22 22:02:26 +00002085static void be_msix_disable(struct be_adapter *adapter)
2086{
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002087 if (msix_enabled(adapter)) {
Sathya Perla8d56ff12009-11-22 22:02:26 +00002088 pci_disable_msix(adapter->pdev);
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002089 adapter->num_msix_vec = 0;
Sathya Perla3abcded2010-10-03 22:12:27 -07002090 }
2091}
2092
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002093static void be_msix_enable(struct be_adapter *adapter)
2094{
Sathya Perla3abcded2010-10-03 22:12:27 -07002095#define BE_MIN_MSIX_VECTORS (1 + 1) /* Rx + Tx */
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002096 int i, status, num_vec;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002097
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002098 num_vec = be_num_rxqs_want(adapter) + 1;
Sathya Perla3abcded2010-10-03 22:12:27 -07002099
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002100 for (i = 0; i < num_vec; i++)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002101 adapter->msix_entries[i].entry = i;
2102
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002103 status = pci_enable_msix(adapter->pdev, adapter->msix_entries, num_vec);
Sathya Perla3abcded2010-10-03 22:12:27 -07002104 if (status == 0) {
2105 goto done;
2106 } else if (status >= BE_MIN_MSIX_VECTORS) {
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002107 num_vec = status;
Sathya Perla3abcded2010-10-03 22:12:27 -07002108 if (pci_enable_msix(adapter->pdev, adapter->msix_entries,
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002109 num_vec) == 0)
Sathya Perla3abcded2010-10-03 22:12:27 -07002110 goto done;
Sathya Perla3abcded2010-10-03 22:12:27 -07002111 }
2112 return;
2113done:
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002114 adapter->num_msix_vec = num_vec;
2115 return;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002116}
2117
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002118static int be_sriov_enable(struct be_adapter *adapter)
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002119{
Sarveshwar Bandi344dbf12010-07-09 01:43:55 +00002120 be_check_sriov_fn_type(adapter);
Ajit Khaparde6dedec82010-07-29 06:15:32 +00002121#ifdef CONFIG_PCI_IOV
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002122 if (be_physfn(adapter) && num_vfs) {
Ajit Khaparde81be8f02011-04-06 18:08:17 +00002123 int status, pos;
2124 u16 nvfs;
2125
2126 pos = pci_find_ext_capability(adapter->pdev,
2127 PCI_EXT_CAP_ID_SRIOV);
2128 pci_read_config_word(adapter->pdev,
2129 pos + PCI_SRIOV_TOTAL_VF, &nvfs);
2130
2131 if (num_vfs > nvfs) {
2132 dev_info(&adapter->pdev->dev,
2133 "Device supports %d VFs and not %d\n",
2134 nvfs, num_vfs);
2135 num_vfs = nvfs;
2136 }
Ajit Khaparde6dedec82010-07-29 06:15:32 +00002137
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002138 status = pci_enable_sriov(adapter->pdev, num_vfs);
2139 adapter->sriov_enabled = status ? false : true;
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002140
2141 if (adapter->sriov_enabled) {
2142 adapter->vf_cfg = kcalloc(num_vfs,
2143 sizeof(struct be_vf_cfg),
2144 GFP_KERNEL);
2145 if (!adapter->vf_cfg)
2146 return -ENOMEM;
2147 }
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002148 }
2149#endif
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002150 return 0;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002151}
2152
2153static void be_sriov_disable(struct be_adapter *adapter)
2154{
2155#ifdef CONFIG_PCI_IOV
2156 if (adapter->sriov_enabled) {
2157 pci_disable_sriov(adapter->pdev);
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002158 kfree(adapter->vf_cfg);
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002159 adapter->sriov_enabled = false;
2160 }
2161#endif
2162}
2163
Sathya Perlafe6d2a32010-11-21 23:25:50 +00002164static inline int be_msix_vec_get(struct be_adapter *adapter,
2165 struct be_eq_obj *eq_obj)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002166{
Padmanabh Ratnakarecd62102011-04-03 01:54:11 +00002167 return adapter->msix_entries[eq_obj->eq_idx].vector;
Sathya Perlab628bde2009-08-17 00:58:26 +00002168}
2169
2170static int be_request_irq(struct be_adapter *adapter,
2171 struct be_eq_obj *eq_obj,
Sathya Perla3abcded2010-10-03 22:12:27 -07002172 void *handler, char *desc, void *context)
Sathya Perlab628bde2009-08-17 00:58:26 +00002173{
2174 struct net_device *netdev = adapter->netdev;
2175 int vec;
2176
2177 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
Sathya Perlafe6d2a32010-11-21 23:25:50 +00002178 vec = be_msix_vec_get(adapter, eq_obj);
Sathya Perla3abcded2010-10-03 22:12:27 -07002179 return request_irq(vec, handler, 0, eq_obj->desc, context);
Sathya Perlab628bde2009-08-17 00:58:26 +00002180}
2181
Sathya Perla3abcded2010-10-03 22:12:27 -07002182static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj,
2183 void *context)
Sathya Perlab628bde2009-08-17 00:58:26 +00002184{
Sathya Perlafe6d2a32010-11-21 23:25:50 +00002185 int vec = be_msix_vec_get(adapter, eq_obj);
Sathya Perla3abcded2010-10-03 22:12:27 -07002186 free_irq(vec, context);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002187}
2188
2189static int be_msix_register(struct be_adapter *adapter)
2190{
Sathya Perla3abcded2010-10-03 22:12:27 -07002191 struct be_rx_obj *rxo;
2192 int status, i;
2193 char qname[10];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002194
Sathya Perla3abcded2010-10-03 22:12:27 -07002195 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx",
2196 adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002197 if (status)
2198 goto err;
2199
Sathya Perla3abcded2010-10-03 22:12:27 -07002200 for_all_rx_queues(adapter, rxo, i) {
2201 sprintf(qname, "rxq%d", i);
2202 status = be_request_irq(adapter, &rxo->rx_eq, be_msix_rx,
2203 qname, rxo);
2204 if (status)
2205 goto err_msix;
2206 }
Sathya Perlab628bde2009-08-17 00:58:26 +00002207
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002208 return 0;
Sathya Perlab628bde2009-08-17 00:58:26 +00002209
Sathya Perla3abcded2010-10-03 22:12:27 -07002210err_msix:
2211 be_free_irq(adapter, &adapter->tx_eq, adapter);
2212
2213 for (i--, rxo = &adapter->rx_obj[i]; i >= 0; i--, rxo--)
2214 be_free_irq(adapter, &rxo->rx_eq, rxo);
2215
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002216err:
2217 dev_warn(&adapter->pdev->dev,
2218 "MSIX Request IRQ failed - err %d\n", status);
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002219 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002220 return status;
2221}
2222
2223static int be_irq_register(struct be_adapter *adapter)
2224{
2225 struct net_device *netdev = adapter->netdev;
2226 int status;
2227
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002228 if (msix_enabled(adapter)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002229 status = be_msix_register(adapter);
2230 if (status == 0)
2231 goto done;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002232 /* INTx is not supported for VF */
2233 if (!be_physfn(adapter))
2234 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002235 }
2236
2237 /* INTx */
2238 netdev->irq = adapter->pdev->irq;
2239 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
2240 adapter);
2241 if (status) {
2242 dev_err(&adapter->pdev->dev,
2243 "INTx request IRQ failed - err %d\n", status);
2244 return status;
2245 }
2246done:
2247 adapter->isr_registered = true;
2248 return 0;
2249}
2250
2251static void be_irq_unregister(struct be_adapter *adapter)
2252{
2253 struct net_device *netdev = adapter->netdev;
Sathya Perla3abcded2010-10-03 22:12:27 -07002254 struct be_rx_obj *rxo;
2255 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002256
2257 if (!adapter->isr_registered)
2258 return;
2259
2260 /* INTx */
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002261 if (!msix_enabled(adapter)) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002262 free_irq(netdev->irq, adapter);
2263 goto done;
2264 }
2265
2266 /* MSIx */
Sathya Perla3abcded2010-10-03 22:12:27 -07002267 be_free_irq(adapter, &adapter->tx_eq, adapter);
2268
2269 for_all_rx_queues(adapter, rxo, i)
2270 be_free_irq(adapter, &rxo->rx_eq, rxo);
2271
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002272done:
2273 adapter->isr_registered = false;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002274}
2275
Sathya Perla482c9e72011-06-29 23:33:17 +00002276static void be_rx_queues_clear(struct be_adapter *adapter)
2277{
2278 struct be_queue_info *q;
2279 struct be_rx_obj *rxo;
2280 int i;
2281
2282 for_all_rx_queues(adapter, rxo, i) {
2283 q = &rxo->q;
2284 if (q->created) {
2285 be_cmd_rxq_destroy(adapter, q);
2286 /* After the rxq is invalidated, wait for a grace time
2287 * of 1ms for all dma to end and the flush compl to
2288 * arrive
2289 */
2290 mdelay(1);
2291 be_rx_q_clean(adapter, rxo);
2292 }
2293
2294 /* Clear any residual events */
2295 q = &rxo->rx_eq.q;
2296 if (q->created)
2297 be_eq_clean(adapter, &rxo->rx_eq);
2298 }
2299}
2300
Sathya Perla889cd4b2010-05-30 23:33:45 +00002301static int be_close(struct net_device *netdev)
2302{
2303 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3abcded2010-10-03 22:12:27 -07002304 struct be_rx_obj *rxo;
Sathya Perla3c8def92011-06-12 20:01:58 +00002305 struct be_tx_obj *txo;
Sathya Perla889cd4b2010-05-30 23:33:45 +00002306 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla3abcded2010-10-03 22:12:27 -07002307 int vec, i;
Sathya Perla889cd4b2010-05-30 23:33:45 +00002308
Sathya Perla889cd4b2010-05-30 23:33:45 +00002309 be_async_mcc_disable(adapter);
2310
Sathya Perlafe6d2a32010-11-21 23:25:50 +00002311 if (!lancer_chip(adapter))
2312 be_intr_set(adapter, false);
Sathya Perla889cd4b2010-05-30 23:33:45 +00002313
Padmanabh Ratnakar63fcb272011-03-07 03:09:17 +00002314 for_all_rx_queues(adapter, rxo, i)
2315 napi_disable(&rxo->rx_eq.napi);
2316
2317 napi_disable(&tx_eq->napi);
2318
2319 if (lancer_chip(adapter)) {
Padmanabh Ratnakar63fcb272011-03-07 03:09:17 +00002320 be_cq_notify(adapter, adapter->mcc_obj.cq.id, false, 0);
2321 for_all_rx_queues(adapter, rxo, i)
2322 be_cq_notify(adapter, rxo->cq.id, false, 0);
Sathya Perla3c8def92011-06-12 20:01:58 +00002323 for_all_tx_queues(adapter, txo, i)
2324 be_cq_notify(adapter, txo->cq.id, false, 0);
Padmanabh Ratnakar63fcb272011-03-07 03:09:17 +00002325 }
2326
Sathya Perlaac6a0c42011-03-21 20:49:25 +00002327 if (msix_enabled(adapter)) {
Sathya Perlafe6d2a32010-11-21 23:25:50 +00002328 vec = be_msix_vec_get(adapter, tx_eq);
Sathya Perla889cd4b2010-05-30 23:33:45 +00002329 synchronize_irq(vec);
Sathya Perla3abcded2010-10-03 22:12:27 -07002330
2331 for_all_rx_queues(adapter, rxo, i) {
Sathya Perlafe6d2a32010-11-21 23:25:50 +00002332 vec = be_msix_vec_get(adapter, &rxo->rx_eq);
Sathya Perla3abcded2010-10-03 22:12:27 -07002333 synchronize_irq(vec);
2334 }
Sathya Perla889cd4b2010-05-30 23:33:45 +00002335 } else {
2336 synchronize_irq(netdev->irq);
2337 }
2338 be_irq_unregister(adapter);
2339
Sathya Perla889cd4b2010-05-30 23:33:45 +00002340 /* Wait for all pending tx completions to arrive so that
2341 * all tx skbs are freed.
2342 */
Sathya Perla3c8def92011-06-12 20:01:58 +00002343 for_all_tx_queues(adapter, txo, i)
2344 be_tx_compl_clean(adapter, txo);
Sathya Perla889cd4b2010-05-30 23:33:45 +00002345
Sathya Perla482c9e72011-06-29 23:33:17 +00002346 be_rx_queues_clear(adapter);
2347 return 0;
2348}
2349
2350static int be_rx_queues_setup(struct be_adapter *adapter)
2351{
2352 struct be_rx_obj *rxo;
2353 int rc, i;
2354 u8 rsstable[MAX_RSS_QS];
2355
2356 for_all_rx_queues(adapter, rxo, i) {
2357 rc = be_cmd_rxq_create(adapter, &rxo->q, rxo->cq.id,
2358 rx_frag_size, BE_MAX_JUMBO_FRAME_SIZE,
2359 adapter->if_handle,
2360 (i > 0) ? 1 : 0/* rss enable */, &rxo->rss_id);
2361 if (rc)
2362 return rc;
2363 }
2364
2365 if (be_multi_rxq(adapter)) {
2366 for_all_rss_queues(adapter, rxo, i)
2367 rsstable[i] = rxo->rss_id;
2368
2369 rc = be_cmd_rss_config(adapter, rsstable,
2370 adapter->num_rx_qs - 1);
2371 if (rc)
2372 return rc;
2373 }
2374
2375 /* First time posting */
2376 for_all_rx_queues(adapter, rxo, i) {
2377 be_post_rx_frags(rxo, GFP_KERNEL);
2378 napi_enable(&rxo->rx_eq.napi);
2379 }
Sathya Perla889cd4b2010-05-30 23:33:45 +00002380 return 0;
2381}
2382
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002383static int be_open(struct net_device *netdev)
2384{
2385 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002386 struct be_eq_obj *tx_eq = &adapter->tx_eq;
Sathya Perla3abcded2010-10-03 22:12:27 -07002387 struct be_rx_obj *rxo;
Sathya Perla3abcded2010-10-03 22:12:27 -07002388 int status, i;
Sathya Perla5fb379e2009-06-18 00:02:59 +00002389
Sathya Perla482c9e72011-06-29 23:33:17 +00002390 status = be_rx_queues_setup(adapter);
2391 if (status)
2392 goto err;
2393
Sathya Perla5fb379e2009-06-18 00:02:59 +00002394 napi_enable(&tx_eq->napi);
2395
2396 be_irq_register(adapter);
2397
Sathya Perlafe6d2a32010-11-21 23:25:50 +00002398 if (!lancer_chip(adapter))
2399 be_intr_set(adapter, true);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002400
2401 /* The evt queues are created in unarmed state; arm them */
Sathya Perla3abcded2010-10-03 22:12:27 -07002402 for_all_rx_queues(adapter, rxo, i) {
2403 be_eq_notify(adapter, rxo->rx_eq.q.id, true, false, 0);
2404 be_cq_notify(adapter, rxo->cq.id, true, 0);
2405 }
Sathya Perla8788fdc2009-07-27 22:52:03 +00002406 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
Sathya Perla5fb379e2009-06-18 00:02:59 +00002407
Sathya Perla7a1e9b22010-02-17 01:35:11 +00002408 /* Now that interrupts are on we can process async mcc */
2409 be_async_mcc_enable(adapter);
2410
Sathya Perla889cd4b2010-05-30 23:33:45 +00002411 return 0;
2412err:
2413 be_close(adapter->netdev);
2414 return -EIO;
Sathya Perla5fb379e2009-06-18 00:02:59 +00002415}
2416
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002417static int be_setup_wol(struct be_adapter *adapter, bool enable)
2418{
2419 struct be_dma_mem cmd;
2420 int status = 0;
2421 u8 mac[ETH_ALEN];
2422
2423 memset(mac, 0, ETH_ALEN);
2424
2425 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00002426 cmd.va = dma_alloc_coherent(&adapter->pdev->dev, cmd.size, &cmd.dma,
2427 GFP_KERNEL);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002428 if (cmd.va == NULL)
2429 return -1;
2430 memset(cmd.va, 0, cmd.size);
2431
2432 if (enable) {
2433 status = pci_write_config_dword(adapter->pdev,
2434 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
2435 if (status) {
2436 dev_err(&adapter->pdev->dev,
Frans Pop2381a552010-03-24 07:57:36 +00002437 "Could not enable Wake-on-lan\n");
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00002438 dma_free_coherent(&adapter->pdev->dev, cmd.size, cmd.va,
2439 cmd.dma);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002440 return status;
2441 }
2442 status = be_cmd_enable_magic_wol(adapter,
2443 adapter->netdev->dev_addr, &cmd);
2444 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
2445 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
2446 } else {
2447 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
2448 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
2449 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
2450 }
2451
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00002452 dma_free_coherent(&adapter->pdev->dev, cmd.size, cmd.va, cmd.dma);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00002453 return status;
2454}
2455
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002456/*
2457 * Generate a seed MAC address from the PF MAC Address using jhash.
2458 * MAC Address for VFs are assigned incrementally starting from the seed.
2459 * These addresses are programmed in the ASIC by the PF and the VF driver
2460 * queries for the MAC address during its probe.
2461 */
2462static inline int be_vf_eth_addr_config(struct be_adapter *adapter)
2463{
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002464 u32 vf;
Sathya Perla3abcded2010-10-03 22:12:27 -07002465 int status = 0;
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002466 u8 mac[ETH_ALEN];
2467
2468 be_vf_eth_addr_generate(adapter, mac);
2469
2470 for (vf = 0; vf < num_vfs; vf++) {
2471 status = be_cmd_pmac_add(adapter, mac,
2472 adapter->vf_cfg[vf].vf_if_handle,
Ajit Khapardef8617e02011-02-11 13:36:37 +00002473 &adapter->vf_cfg[vf].vf_pmac_id,
2474 vf + 1);
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002475 if (status)
2476 dev_err(&adapter->pdev->dev,
2477 "Mac address add failed for VF %d\n", vf);
2478 else
2479 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
2480
2481 mac[5] += 1;
2482 }
2483 return status;
2484}
2485
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002486static void be_vf_clear(struct be_adapter *adapter)
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002487{
2488 u32 vf;
2489
Sathya Perla30128032011-11-10 19:17:57 +00002490 for (vf = 0; vf < num_vfs; vf++)
2491 be_cmd_pmac_del(adapter, adapter->vf_cfg[vf].vf_if_handle,
2492 adapter->vf_cfg[vf].vf_pmac_id, vf + 1);
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002493
2494 for (vf = 0; vf < num_vfs; vf++)
Sathya Perla30128032011-11-10 19:17:57 +00002495 be_cmd_if_destroy(adapter, adapter->vf_cfg[vf].vf_if_handle,
2496 vf + 1);
Ajit Khaparde6d87f5c2010-08-25 00:32:33 +00002497}
2498
Sathya Perlaa54769f2011-10-24 02:45:00 +00002499static int be_clear(struct be_adapter *adapter)
2500{
Sathya Perlaa54769f2011-10-24 02:45:00 +00002501 if (be_physfn(adapter) && adapter->sriov_enabled)
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002502 be_vf_clear(adapter);
2503
2504 be_cmd_if_destroy(adapter, adapter->if_handle, 0);
Sathya Perlaa54769f2011-10-24 02:45:00 +00002505
2506 be_mcc_queues_destroy(adapter);
2507 be_rx_queues_destroy(adapter);
2508 be_tx_queues_destroy(adapter);
Sathya Perlaa54769f2011-10-24 02:45:00 +00002509
2510 /* tell fw we're done with firing cmds */
2511 be_cmd_fw_clean(adapter);
2512 return 0;
2513}
2514
Sathya Perla30128032011-11-10 19:17:57 +00002515static void be_vf_setup_init(struct be_adapter *adapter)
2516{
2517 int vf;
2518
2519 for (vf = 0; vf < num_vfs; vf++) {
2520 adapter->vf_cfg[vf].vf_if_handle = -1;
2521 adapter->vf_cfg[vf].vf_pmac_id = -1;
2522 }
2523}
2524
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002525static int be_vf_setup(struct be_adapter *adapter)
2526{
2527 u32 cap_flags, en_flags, vf;
2528 u16 lnk_speed;
2529 int status;
2530
Sathya Perla30128032011-11-10 19:17:57 +00002531 be_vf_setup_init(adapter);
2532
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002533 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST;
2534 for (vf = 0; vf < num_vfs; vf++) {
2535 status = be_cmd_if_create(adapter, cap_flags, en_flags, NULL,
2536 &adapter->vf_cfg[vf].vf_if_handle,
2537 NULL, vf+1);
2538 if (status)
2539 goto err;
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002540 }
2541
2542 if (!lancer_chip(adapter)) {
2543 status = be_vf_eth_addr_config(adapter);
2544 if (status)
2545 goto err;
2546 }
2547
2548 for (vf = 0; vf < num_vfs; vf++) {
2549 status = be_cmd_link_status_query(adapter, NULL, &lnk_speed,
2550 vf + 1);
2551 if (status)
2552 goto err;
2553 adapter->vf_cfg[vf].vf_tx_rate = lnk_speed * 10;
2554 }
2555 return 0;
2556err:
2557 return status;
2558}
2559
Sathya Perla30128032011-11-10 19:17:57 +00002560static void be_setup_init(struct be_adapter *adapter)
2561{
2562 adapter->vlan_prio_bmap = 0xff;
2563 adapter->link_speed = -1;
2564 adapter->if_handle = -1;
2565 adapter->be3_native = false;
2566 adapter->promiscuous = false;
2567 adapter->eq_next_idx = 0;
2568}
2569
Sathya Perla5fb379e2009-06-18 00:02:59 +00002570static int be_setup(struct be_adapter *adapter)
2571{
Sathya Perla5fb379e2009-06-18 00:02:59 +00002572 struct net_device *netdev = adapter->netdev;
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002573 u32 cap_flags, en_flags;
Sathya Perlaa54769f2011-10-24 02:45:00 +00002574 u32 tx_fc, rx_fc;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002575 int status;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00002576 u8 mac[ETH_ALEN];
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002577
Sathya Perla30128032011-11-10 19:17:57 +00002578 be_setup_init(adapter);
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002579
Sathya Perla2dc1deb2011-07-19 19:52:33 +00002580 be_cmd_req_native_mode(adapter);
2581
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002582 status = be_tx_queues_create(adapter);
2583 if (status != 0)
Sathya Perlaa54769f2011-10-24 02:45:00 +00002584 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002585
2586 status = be_rx_queues_create(adapter);
2587 if (status != 0)
Sathya Perlaa54769f2011-10-24 02:45:00 +00002588 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002589
Sathya Perla5fb379e2009-06-18 00:02:59 +00002590 status = be_mcc_queues_create(adapter);
2591 if (status != 0)
Sathya Perlaa54769f2011-10-24 02:45:00 +00002592 goto err;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002593
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002594 memset(mac, 0, ETH_ALEN);
2595 status = be_cmd_mac_addr_query(adapter, mac, MAC_ADDRESS_TYPE_NETWORK,
2596 true /*permanent */, 0);
2597 if (status)
2598 return status;
2599 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2600 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2601
2602 en_flags = BE_IF_FLAGS_UNTAGGED | BE_IF_FLAGS_BROADCAST |
2603 BE_IF_FLAGS_MULTICAST | BE_IF_FLAGS_PASS_L3L4_ERRORS;
2604 cap_flags = en_flags | BE_IF_FLAGS_MCAST_PROMISCUOUS |
2605 BE_IF_FLAGS_PROMISCUOUS;
2606 if (adapter->function_caps & BE_FUNCTION_CAPS_RSS) {
2607 cap_flags |= BE_IF_FLAGS_RSS;
2608 en_flags |= BE_IF_FLAGS_RSS;
2609 }
2610 status = be_cmd_if_create(adapter, cap_flags, en_flags,
2611 netdev->dev_addr, &adapter->if_handle,
2612 &adapter->pmac_id, 0);
2613 if (status != 0)
2614 goto err;
2615
2616 /* For BEx, the VF's permanent mac queried from card is incorrect.
2617 * Query the mac configued by the PF using if_handle
2618 */
2619 if (!be_physfn(adapter) && !lancer_chip(adapter)) {
2620 status = be_cmd_mac_addr_query(adapter, mac,
2621 MAC_ADDRESS_TYPE_NETWORK, false, adapter->if_handle);
2622 if (!status) {
2623 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2624 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2625 }
2626 }
Ajit Khaparde0dffc832009-11-29 17:57:46 +00002627
Sathya Perla04b71172011-09-27 13:30:27 -04002628 be_cmd_get_fw_ver(adapter, adapter->fw_ver, NULL);
Somnath Kotur5a56eb12011-09-30 07:24:28 +00002629
Sathya Perlaa54769f2011-10-24 02:45:00 +00002630 status = be_vid_config(adapter, false, 0);
2631 if (status)
2632 goto err;
2633
2634 be_set_rx_mode(adapter->netdev);
2635
2636 status = be_cmd_get_flow_control(adapter, &tx_fc, &rx_fc);
2637 if (status)
2638 goto err;
2639 if (rx_fc != adapter->rx_fc || tx_fc != adapter->tx_fc) {
2640 status = be_cmd_set_flow_control(adapter, adapter->tx_fc,
2641 adapter->rx_fc);
2642 if (status)
2643 goto err;
2644 }
2645
Somnath Kotur5a56eb12011-09-30 07:24:28 +00002646 pcie_set_readrq(adapter->pdev, 4096);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002647
Sathya Perlaf9449ab2011-10-24 02:45:01 +00002648 if (be_physfn(adapter) && adapter->sriov_enabled) {
2649 status = be_vf_setup(adapter);
2650 if (status)
2651 goto err;
2652 }
2653
2654 return 0;
Sathya Perlaa54769f2011-10-24 02:45:00 +00002655err:
2656 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002657 return status;
2658}
2659
Ajit Khaparde84517482009-09-04 03:12:16 +00002660#define FW_FILE_HDR_SIGN "ServerEngines Corp. "
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002661static bool be_flash_redboot(struct be_adapter *adapter,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002662 const u8 *p, u32 img_start, int image_size,
2663 int hdr_size)
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002664{
2665 u32 crc_offset;
2666 u8 flashed_crc[4];
2667 int status;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002668
2669 crc_offset = hdr_size + img_start + image_size - 4;
2670
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002671 p += crc_offset;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002672
2673 status = be_cmd_get_flash_crc(adapter, flashed_crc,
Ajit Khapardef510fc62010-03-31 01:47:45 +00002674 (image_size - 4));
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002675 if (status) {
2676 dev_err(&adapter->pdev->dev,
2677 "could not get crc from flash, not flashing redboot\n");
2678 return false;
2679 }
2680
2681 /*update redboot only if crc does not match*/
2682 if (!memcmp(flashed_crc, p, 4))
2683 return false;
2684 else
2685 return true;
Sarveshwar Bandifa9a6fe2009-11-20 14:23:47 -08002686}
2687
Sathya Perla306f1342011-08-02 19:57:45 +00002688static bool phy_flashing_required(struct be_adapter *adapter)
2689{
2690 int status = 0;
2691 struct be_phy_info phy_info;
2692
2693 status = be_cmd_get_phy_info(adapter, &phy_info);
2694 if (status)
2695 return false;
2696 if ((phy_info.phy_type == TN_8022) &&
2697 (phy_info.interface_type == PHY_TYPE_BASET_10GB)) {
2698 return true;
2699 }
2700 return false;
2701}
2702
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002703static int be_flash_data(struct be_adapter *adapter,
Ajit Khaparde84517482009-09-04 03:12:16 +00002704 const struct firmware *fw,
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002705 struct be_dma_mem *flash_cmd, int num_of_images)
2706
Ajit Khaparde84517482009-09-04 03:12:16 +00002707{
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002708 int status = 0, i, filehdr_size = 0;
2709 u32 total_bytes = 0, flash_op;
Ajit Khaparde84517482009-09-04 03:12:16 +00002710 int num_bytes;
2711 const u8 *p = fw->data;
2712 struct be_cmd_write_flashrom *req = flash_cmd->va;
Joe Perches215faf92010-12-21 02:16:10 -08002713 const struct flash_comp *pflashcomp;
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002714 int num_comp;
Ajit Khaparde84517482009-09-04 03:12:16 +00002715
Sathya Perla306f1342011-08-02 19:57:45 +00002716 static const struct flash_comp gen3_flash_types[10] = {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002717 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, IMG_TYPE_ISCSI_ACTIVE,
2718 FLASH_IMAGE_MAX_SIZE_g3},
2719 { FLASH_REDBOOT_START_g3, IMG_TYPE_REDBOOT,
2720 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3},
2721 { FLASH_iSCSI_BIOS_START_g3, IMG_TYPE_BIOS,
2722 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2723 { FLASH_PXE_BIOS_START_g3, IMG_TYPE_PXE_BIOS,
2724 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2725 { FLASH_FCoE_BIOS_START_g3, IMG_TYPE_FCOE_BIOS,
2726 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2727 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, IMG_TYPE_ISCSI_BACKUP,
2728 FLASH_IMAGE_MAX_SIZE_g3},
2729 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, IMG_TYPE_FCOE_FW_ACTIVE,
2730 FLASH_IMAGE_MAX_SIZE_g3},
2731 { FLASH_FCoE_BACKUP_IMAGE_START_g3, IMG_TYPE_FCOE_FW_BACKUP,
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002732 FLASH_IMAGE_MAX_SIZE_g3},
2733 { FLASH_NCSI_START_g3, IMG_TYPE_NCSI_FW,
Sathya Perla306f1342011-08-02 19:57:45 +00002734 FLASH_NCSI_IMAGE_MAX_SIZE_g3},
2735 { FLASH_PHY_FW_START_g3, IMG_TYPE_PHY_FW,
2736 FLASH_PHY_FW_IMAGE_MAX_SIZE_g3}
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002737 };
Joe Perches215faf92010-12-21 02:16:10 -08002738 static const struct flash_comp gen2_flash_types[8] = {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002739 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, IMG_TYPE_ISCSI_ACTIVE,
2740 FLASH_IMAGE_MAX_SIZE_g2},
2741 { FLASH_REDBOOT_START_g2, IMG_TYPE_REDBOOT,
2742 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2},
2743 { FLASH_iSCSI_BIOS_START_g2, IMG_TYPE_BIOS,
2744 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2745 { FLASH_PXE_BIOS_START_g2, IMG_TYPE_PXE_BIOS,
2746 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2747 { FLASH_FCoE_BIOS_START_g2, IMG_TYPE_FCOE_BIOS,
2748 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2749 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, IMG_TYPE_ISCSI_BACKUP,
2750 FLASH_IMAGE_MAX_SIZE_g2},
2751 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, IMG_TYPE_FCOE_FW_ACTIVE,
2752 FLASH_IMAGE_MAX_SIZE_g2},
2753 { FLASH_FCoE_BACKUP_IMAGE_START_g2, IMG_TYPE_FCOE_FW_BACKUP,
2754 FLASH_IMAGE_MAX_SIZE_g2}
2755 };
2756
2757 if (adapter->generation == BE_GEN3) {
2758 pflashcomp = gen3_flash_types;
2759 filehdr_size = sizeof(struct flash_file_hdr_g3);
Joe Perches215faf92010-12-21 02:16:10 -08002760 num_comp = ARRAY_SIZE(gen3_flash_types);
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002761 } else {
2762 pflashcomp = gen2_flash_types;
2763 filehdr_size = sizeof(struct flash_file_hdr_g2);
Joe Perches215faf92010-12-21 02:16:10 -08002764 num_comp = ARRAY_SIZE(gen2_flash_types);
Ajit Khaparde84517482009-09-04 03:12:16 +00002765 }
Sarveshwar Bandi9fe96932010-03-02 22:37:28 +00002766 for (i = 0; i < num_comp; i++) {
2767 if ((pflashcomp[i].optype == IMG_TYPE_NCSI_FW) &&
2768 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
2769 continue;
Sathya Perla306f1342011-08-02 19:57:45 +00002770 if (pflashcomp[i].optype == IMG_TYPE_PHY_FW) {
2771 if (!phy_flashing_required(adapter))
2772 continue;
2773 }
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002774 if ((pflashcomp[i].optype == IMG_TYPE_REDBOOT) &&
2775 (!be_flash_redboot(adapter, fw->data,
Ajit Khapardefae21a42011-02-11 13:37:42 +00002776 pflashcomp[i].offset, pflashcomp[i].size, filehdr_size +
2777 (num_of_images * sizeof(struct image_hdr)))))
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002778 continue;
2779 p = fw->data;
2780 p += filehdr_size + pflashcomp[i].offset
2781 + (num_of_images * sizeof(struct image_hdr));
Sathya Perla306f1342011-08-02 19:57:45 +00002782 if (p + pflashcomp[i].size > fw->data + fw->size)
2783 return -1;
2784 total_bytes = pflashcomp[i].size;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002785 while (total_bytes) {
2786 if (total_bytes > 32*1024)
2787 num_bytes = 32*1024;
2788 else
2789 num_bytes = total_bytes;
2790 total_bytes -= num_bytes;
Sathya Perla306f1342011-08-02 19:57:45 +00002791 if (!total_bytes) {
2792 if (pflashcomp[i].optype == IMG_TYPE_PHY_FW)
2793 flash_op = FLASHROM_OPER_PHY_FLASH;
2794 else
2795 flash_op = FLASHROM_OPER_FLASH;
2796 } else {
2797 if (pflashcomp[i].optype == IMG_TYPE_PHY_FW)
2798 flash_op = FLASHROM_OPER_PHY_SAVE;
2799 else
2800 flash_op = FLASHROM_OPER_SAVE;
2801 }
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002802 memcpy(req->params.data_buf, p, num_bytes);
2803 p += num_bytes;
2804 status = be_cmd_write_flashrom(adapter, flash_cmd,
2805 pflashcomp[i].optype, flash_op, num_bytes);
2806 if (status) {
Sathya Perla306f1342011-08-02 19:57:45 +00002807 if ((status == ILLEGAL_IOCTL_REQ) &&
2808 (pflashcomp[i].optype ==
2809 IMG_TYPE_PHY_FW))
2810 break;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002811 dev_err(&adapter->pdev->dev,
2812 "cmd to write to flash rom failed.\n");
2813 return -1;
2814 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002815 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002816 }
Ajit Khaparde84517482009-09-04 03:12:16 +00002817 return 0;
2818}
2819
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002820static int get_ufigen_type(struct flash_file_hdr_g2 *fhdr)
2821{
2822 if (fhdr == NULL)
2823 return 0;
2824 if (fhdr->build[0] == '3')
2825 return BE_GEN3;
2826 else if (fhdr->build[0] == '2')
2827 return BE_GEN2;
2828 else
2829 return 0;
2830}
2831
Shripad Nunjundarao485bf562011-05-16 07:36:59 +00002832static int lancer_fw_download(struct be_adapter *adapter,
2833 const struct firmware *fw)
Ajit Khaparde84517482009-09-04 03:12:16 +00002834{
Shripad Nunjundarao485bf562011-05-16 07:36:59 +00002835#define LANCER_FW_DOWNLOAD_CHUNK (32 * 1024)
2836#define LANCER_FW_DOWNLOAD_LOCATION "/prg"
2837 struct be_dma_mem flash_cmd;
Shripad Nunjundarao485bf562011-05-16 07:36:59 +00002838 const u8 *data_ptr = NULL;
2839 u8 *dest_image_ptr = NULL;
2840 size_t image_size = 0;
2841 u32 chunk_size = 0;
2842 u32 data_written = 0;
2843 u32 offset = 0;
2844 int status = 0;
2845 u8 add_status = 0;
2846
2847 if (!IS_ALIGNED(fw->size, sizeof(u32))) {
2848 dev_err(&adapter->pdev->dev,
2849 "FW Image not properly aligned. "
2850 "Length must be 4 byte aligned.\n");
2851 status = -EINVAL;
2852 goto lancer_fw_exit;
2853 }
2854
2855 flash_cmd.size = sizeof(struct lancer_cmd_req_write_object)
2856 + LANCER_FW_DOWNLOAD_CHUNK;
2857 flash_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, flash_cmd.size,
2858 &flash_cmd.dma, GFP_KERNEL);
2859 if (!flash_cmd.va) {
2860 status = -ENOMEM;
2861 dev_err(&adapter->pdev->dev,
2862 "Memory allocation failure while flashing\n");
2863 goto lancer_fw_exit;
2864 }
2865
Shripad Nunjundarao485bf562011-05-16 07:36:59 +00002866 dest_image_ptr = flash_cmd.va +
2867 sizeof(struct lancer_cmd_req_write_object);
2868 image_size = fw->size;
2869 data_ptr = fw->data;
2870
2871 while (image_size) {
2872 chunk_size = min_t(u32, image_size, LANCER_FW_DOWNLOAD_CHUNK);
2873
2874 /* Copy the image chunk content. */
2875 memcpy(dest_image_ptr, data_ptr, chunk_size);
2876
2877 status = lancer_cmd_write_object(adapter, &flash_cmd,
2878 chunk_size, offset, LANCER_FW_DOWNLOAD_LOCATION,
2879 &data_written, &add_status);
2880
2881 if (status)
2882 break;
2883
2884 offset += data_written;
2885 data_ptr += data_written;
2886 image_size -= data_written;
2887 }
2888
2889 if (!status) {
2890 /* Commit the FW written */
2891 status = lancer_cmd_write_object(adapter, &flash_cmd,
2892 0, offset, LANCER_FW_DOWNLOAD_LOCATION,
2893 &data_written, &add_status);
2894 }
2895
2896 dma_free_coherent(&adapter->pdev->dev, flash_cmd.size, flash_cmd.va,
2897 flash_cmd.dma);
2898 if (status) {
2899 dev_err(&adapter->pdev->dev,
2900 "Firmware load error. "
2901 "Status code: 0x%x Additional Status: 0x%x\n",
2902 status, add_status);
2903 goto lancer_fw_exit;
2904 }
2905
2906 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
2907lancer_fw_exit:
2908 return status;
2909}
2910
2911static int be_fw_download(struct be_adapter *adapter, const struct firmware* fw)
2912{
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002913 struct flash_file_hdr_g2 *fhdr;
2914 struct flash_file_hdr_g3 *fhdr3;
2915 struct image_hdr *img_hdr_ptr = NULL;
Ajit Khaparde84517482009-09-04 03:12:16 +00002916 struct be_dma_mem flash_cmd;
Ajit Khaparde84517482009-09-04 03:12:16 +00002917 const u8 *p;
Shripad Nunjundarao485bf562011-05-16 07:36:59 +00002918 int status = 0, i = 0, num_imgs = 0;
Ajit Khaparde84517482009-09-04 03:12:16 +00002919
2920 p = fw->data;
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002921 fhdr = (struct flash_file_hdr_g2 *) p;
Ajit Khaparde84517482009-09-04 03:12:16 +00002922
Ajit Khaparde84517482009-09-04 03:12:16 +00002923 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00002924 flash_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, flash_cmd.size,
2925 &flash_cmd.dma, GFP_KERNEL);
Ajit Khaparde84517482009-09-04 03:12:16 +00002926 if (!flash_cmd.va) {
2927 status = -ENOMEM;
2928 dev_err(&adapter->pdev->dev,
2929 "Memory allocation failure while flashing\n");
Shripad Nunjundarao485bf562011-05-16 07:36:59 +00002930 goto be_fw_exit;
Ajit Khaparde84517482009-09-04 03:12:16 +00002931 }
2932
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002933 if ((adapter->generation == BE_GEN3) &&
2934 (get_ufigen_type(fhdr) == BE_GEN3)) {
2935 fhdr3 = (struct flash_file_hdr_g3 *) fw->data;
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002936 num_imgs = le32_to_cpu(fhdr3->num_imgs);
2937 for (i = 0; i < num_imgs; i++) {
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002938 img_hdr_ptr = (struct image_hdr *) (fw->data +
2939 (sizeof(struct flash_file_hdr_g3) +
Ajit Khaparde8b93b712010-03-31 01:57:10 +00002940 i * sizeof(struct image_hdr)));
2941 if (le32_to_cpu(img_hdr_ptr->imageid) == 1)
2942 status = be_flash_data(adapter, fw, &flash_cmd,
2943 num_imgs);
Ajit Khaparde3f0d4562010-02-09 01:30:35 +00002944 }
2945 } else if ((adapter->generation == BE_GEN2) &&
2946 (get_ufigen_type(fhdr) == BE_GEN2)) {
2947 status = be_flash_data(adapter, fw, &flash_cmd, 0);
2948 } else {
2949 dev_err(&adapter->pdev->dev,
2950 "UFI and Interface are not compatible for flashing\n");
2951 status = -1;
Ajit Khaparde84517482009-09-04 03:12:16 +00002952 }
2953
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00002954 dma_free_coherent(&adapter->pdev->dev, flash_cmd.size, flash_cmd.va,
2955 flash_cmd.dma);
Ajit Khaparde84517482009-09-04 03:12:16 +00002956 if (status) {
2957 dev_err(&adapter->pdev->dev, "Firmware load error\n");
Shripad Nunjundarao485bf562011-05-16 07:36:59 +00002958 goto be_fw_exit;
Ajit Khaparde84517482009-09-04 03:12:16 +00002959 }
2960
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002961 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
Ajit Khaparde84517482009-09-04 03:12:16 +00002962
Shripad Nunjundarao485bf562011-05-16 07:36:59 +00002963be_fw_exit:
2964 return status;
2965}
2966
2967int be_load_fw(struct be_adapter *adapter, u8 *fw_file)
2968{
2969 const struct firmware *fw;
2970 int status;
2971
2972 if (!netif_running(adapter->netdev)) {
2973 dev_err(&adapter->pdev->dev,
2974 "Firmware load not allowed (interface is down)\n");
2975 return -1;
2976 }
2977
2978 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
2979 if (status)
2980 goto fw_exit;
2981
2982 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
2983
2984 if (lancer_chip(adapter))
2985 status = lancer_fw_download(adapter, fw);
2986 else
2987 status = be_fw_download(adapter, fw);
2988
Ajit Khaparde84517482009-09-04 03:12:16 +00002989fw_exit:
2990 release_firmware(fw);
2991 return status;
2992}
2993
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002994static struct net_device_ops be_netdev_ops = {
2995 .ndo_open = be_open,
2996 .ndo_stop = be_close,
2997 .ndo_start_xmit = be_xmit,
Sathya Perlaa54769f2011-10-24 02:45:00 +00002998 .ndo_set_rx_mode = be_set_rx_mode,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07002999 .ndo_set_mac_address = be_mac_addr_set,
3000 .ndo_change_mtu = be_change_mtu,
Sathya Perlaab1594e2011-07-25 19:10:15 +00003001 .ndo_get_stats64 = be_get_stats64,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003002 .ndo_validate_addr = eth_validate_addr,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003003 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
3004 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00003005 .ndo_set_vf_mac = be_set_vf_mac,
Ajit Khaparde1da87b72010-07-23 01:51:22 +00003006 .ndo_set_vf_vlan = be_set_vf_vlan,
Ajit Khapardee1d18732010-07-23 01:52:13 +00003007 .ndo_set_vf_tx_rate = be_set_vf_tx_rate,
Ajit Khaparde64600ea2010-07-23 01:50:34 +00003008 .ndo_get_vf_config = be_get_vf_config
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003009};
3010
3011static void be_netdev_init(struct net_device *netdev)
3012{
3013 struct be_adapter *adapter = netdev_priv(netdev);
Sathya Perla3abcded2010-10-03 22:12:27 -07003014 struct be_rx_obj *rxo;
3015 int i;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003016
Michał Mirosław6332c8d2011-04-07 02:43:48 +00003017 netdev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
Michał Mirosław8b8ddc62011-04-08 02:38:47 +00003018 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM |
3019 NETIF_F_HW_VLAN_TX;
3020 if (be_multi_rxq(adapter))
3021 netdev->hw_features |= NETIF_F_RXHASH;
Michał Mirosław6332c8d2011-04-07 02:43:48 +00003022
3023 netdev->features |= netdev->hw_features |
Michał Mirosław8b8ddc62011-04-08 02:38:47 +00003024 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
Ajit Khaparde4b972912011-04-06 18:07:43 +00003025
Padmanabh Ratnakareb8a50d2011-06-11 15:58:46 -07003026 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
Michał Mirosław79032642010-11-30 06:38:00 +00003027 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
Ajit Khaparde51c59872009-11-29 17:54:54 +00003028
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003029 netdev->flags |= IFF_MULTICAST;
3030
Ajit Khapardec190e3c2009-09-04 03:12:29 +00003031 netif_set_gso_max_size(netdev, 65535);
3032
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003033 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
3034
3035 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
3036
Sathya Perla3abcded2010-10-03 22:12:27 -07003037 for_all_rx_queues(adapter, rxo, i)
3038 netif_napi_add(netdev, &rxo->rx_eq.napi, be_poll_rx,
3039 BE_NAPI_WEIGHT);
3040
Sathya Perla5fb379e2009-06-18 00:02:59 +00003041 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003042 BE_NAPI_WEIGHT);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003043}
3044
3045static void be_unmap_pci_bars(struct be_adapter *adapter)
3046{
Sathya Perla8788fdc2009-07-27 22:52:03 +00003047 if (adapter->csr)
3048 iounmap(adapter->csr);
3049 if (adapter->db)
3050 iounmap(adapter->db);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003051}
3052
3053static int be_map_pci_bars(struct be_adapter *adapter)
3054{
3055 u8 __iomem *addr;
Sathya Perladb3ea782011-08-22 19:41:52 +00003056 int db_reg;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003057
Sathya Perlafe6d2a32010-11-21 23:25:50 +00003058 if (lancer_chip(adapter)) {
3059 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 0),
3060 pci_resource_len(adapter->pdev, 0));
3061 if (addr == NULL)
3062 return -ENOMEM;
3063 adapter->db = addr;
3064 return 0;
3065 }
3066
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003067 if (be_physfn(adapter)) {
3068 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
3069 pci_resource_len(adapter->pdev, 2));
3070 if (addr == NULL)
3071 return -ENOMEM;
3072 adapter->csr = addr;
3073 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003074
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003075 if (adapter->generation == BE_GEN2) {
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003076 db_reg = 4;
3077 } else {
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003078 if (be_physfn(adapter))
3079 db_reg = 4;
3080 else
3081 db_reg = 0;
3082 }
3083 addr = ioremap_nocache(pci_resource_start(adapter->pdev, db_reg),
3084 pci_resource_len(adapter->pdev, db_reg));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003085 if (addr == NULL)
3086 goto pci_map_err;
Sathya Perla8788fdc2009-07-27 22:52:03 +00003087 adapter->db = addr;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003088
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003089 return 0;
3090pci_map_err:
3091 be_unmap_pci_bars(adapter);
3092 return -ENOMEM;
3093}
3094
3095
3096static void be_ctrl_cleanup(struct be_adapter *adapter)
3097{
Sathya Perla8788fdc2009-07-27 22:52:03 +00003098 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003099
3100 be_unmap_pci_bars(adapter);
3101
3102 if (mem->va)
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00003103 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
3104 mem->dma);
Sathya Perlae7b909a2009-11-22 22:01:10 +00003105
Sathya Perla5b8821b2011-08-02 19:57:44 +00003106 mem = &adapter->rx_filter;
Sathya Perlae7b909a2009-11-22 22:01:10 +00003107 if (mem->va)
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00003108 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
3109 mem->dma);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003110}
3111
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003112static int be_ctrl_init(struct be_adapter *adapter)
3113{
Sathya Perla8788fdc2009-07-27 22:52:03 +00003114 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
3115 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
Sathya Perla5b8821b2011-08-02 19:57:44 +00003116 struct be_dma_mem *rx_filter = &adapter->rx_filter;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003117 int status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003118
3119 status = be_map_pci_bars(adapter);
3120 if (status)
Sathya Perlae7b909a2009-11-22 22:01:10 +00003121 goto done;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003122
3123 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00003124 mbox_mem_alloc->va = dma_alloc_coherent(&adapter->pdev->dev,
3125 mbox_mem_alloc->size,
3126 &mbox_mem_alloc->dma,
3127 GFP_KERNEL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003128 if (!mbox_mem_alloc->va) {
Sathya Perlae7b909a2009-11-22 22:01:10 +00003129 status = -ENOMEM;
3130 goto unmap_pci_bars;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003131 }
3132 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
3133 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
3134 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
3135 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Sathya Perlae7b909a2009-11-22 22:01:10 +00003136
Sathya Perla5b8821b2011-08-02 19:57:44 +00003137 rx_filter->size = sizeof(struct be_cmd_req_rx_filter);
3138 rx_filter->va = dma_alloc_coherent(&adapter->pdev->dev, rx_filter->size,
3139 &rx_filter->dma, GFP_KERNEL);
3140 if (rx_filter->va == NULL) {
Sathya Perlae7b909a2009-11-22 22:01:10 +00003141 status = -ENOMEM;
3142 goto free_mbox;
3143 }
Sathya Perla5b8821b2011-08-02 19:57:44 +00003144 memset(rx_filter->va, 0, rx_filter->size);
Sathya Perlae7b909a2009-11-22 22:01:10 +00003145
Ivan Vecera29849612010-12-14 05:43:19 +00003146 mutex_init(&adapter->mbox_lock);
Sathya Perla8788fdc2009-07-27 22:52:03 +00003147 spin_lock_init(&adapter->mcc_lock);
3148 spin_lock_init(&adapter->mcc_cq_lock);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003149
Sarveshwar Bandidd131e72010-05-25 16:16:32 -07003150 init_completion(&adapter->flash_compl);
Sathya Perlacf588472010-02-14 21:22:01 +00003151 pci_save_state(adapter->pdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003152 return 0;
Sathya Perlae7b909a2009-11-22 22:01:10 +00003153
3154free_mbox:
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00003155 dma_free_coherent(&adapter->pdev->dev, mbox_mem_alloc->size,
3156 mbox_mem_alloc->va, mbox_mem_alloc->dma);
Sathya Perlae7b909a2009-11-22 22:01:10 +00003157
3158unmap_pci_bars:
3159 be_unmap_pci_bars(adapter);
3160
3161done:
3162 return status;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003163}
3164
3165static void be_stats_cleanup(struct be_adapter *adapter)
3166{
Sathya Perla3abcded2010-10-03 22:12:27 -07003167 struct be_dma_mem *cmd = &adapter->stats_cmd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003168
3169 if (cmd->va)
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00003170 dma_free_coherent(&adapter->pdev->dev, cmd->size,
3171 cmd->va, cmd->dma);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003172}
3173
3174static int be_stats_init(struct be_adapter *adapter)
3175{
Sathya Perla3abcded2010-10-03 22:12:27 -07003176 struct be_dma_mem *cmd = &adapter->stats_cmd;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003177
Selvin Xavier005d5692011-05-16 07:36:35 +00003178 if (adapter->generation == BE_GEN2) {
Ajit Khaparde89a88ab2011-05-16 07:36:18 +00003179 cmd->size = sizeof(struct be_cmd_req_get_stats_v0);
Selvin Xavier005d5692011-05-16 07:36:35 +00003180 } else {
3181 if (lancer_chip(adapter))
3182 cmd->size = sizeof(struct lancer_cmd_req_pport_stats);
3183 else
3184 cmd->size = sizeof(struct be_cmd_req_get_stats_v1);
3185 }
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00003186 cmd->va = dma_alloc_coherent(&adapter->pdev->dev, cmd->size, &cmd->dma,
3187 GFP_KERNEL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003188 if (cmd->va == NULL)
3189 return -1;
David S. Millerd291b9a2010-01-28 21:36:21 -08003190 memset(cmd->va, 0, cmd->size);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003191 return 0;
3192}
3193
3194static void __devexit be_remove(struct pci_dev *pdev)
3195{
3196 struct be_adapter *adapter = pci_get_drvdata(pdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00003197
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003198 if (!adapter)
3199 return;
3200
Somnath Koturf203af72010-10-25 23:01:03 +00003201 cancel_delayed_work_sync(&adapter->work);
3202
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003203 unregister_netdev(adapter->netdev);
3204
Sathya Perla5fb379e2009-06-18 00:02:59 +00003205 be_clear(adapter);
3206
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003207 be_stats_cleanup(adapter);
3208
3209 be_ctrl_cleanup(adapter);
3210
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003211 be_sriov_disable(adapter);
3212
Sathya Perla8d56ff12009-11-22 22:02:26 +00003213 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003214
3215 pci_set_drvdata(pdev, NULL);
3216 pci_release_regions(pdev);
3217 pci_disable_device(pdev);
3218
3219 free_netdev(adapter->netdev);
3220}
3221
Sathya Perla2243e2e2009-11-22 22:02:03 +00003222static int be_get_config(struct be_adapter *adapter)
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003223{
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003224 int status;
Sathya Perla43a04fdc2009-10-14 20:21:17 +00003225
Sathya Perla3abcded2010-10-03 22:12:27 -07003226 status = be_cmd_query_fw_cfg(adapter, &adapter->port_num,
3227 &adapter->function_mode, &adapter->function_caps);
Sathya Perla2243e2e2009-11-22 22:02:03 +00003228 if (status)
3229 return status;
3230
Sathya Perla752961a2011-10-24 02:45:03 +00003231 if (adapter->function_mode & FLEX10_MODE)
Ajit Khaparde82903e42010-02-09 01:34:57 +00003232 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED/4;
3233 else
3234 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED;
3235
Ajit Khaparde9e1453c2011-02-20 11:42:22 +00003236 status = be_cmd_get_cntl_attributes(adapter);
3237 if (status)
3238 return status;
3239
Sathya Perla2243e2e2009-11-22 22:02:03 +00003240 return 0;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003241}
3242
Sathya Perlafe6d2a32010-11-21 23:25:50 +00003243static int be_dev_family_check(struct be_adapter *adapter)
3244{
3245 struct pci_dev *pdev = adapter->pdev;
3246 u32 sli_intf = 0, if_type;
3247
3248 switch (pdev->device) {
3249 case BE_DEVICE_ID1:
3250 case OC_DEVICE_ID1:
3251 adapter->generation = BE_GEN2;
3252 break;
3253 case BE_DEVICE_ID2:
3254 case OC_DEVICE_ID2:
3255 adapter->generation = BE_GEN3;
3256 break;
3257 case OC_DEVICE_ID3:
Mammatha Edhala12f4d0a2011-05-18 03:26:22 +00003258 case OC_DEVICE_ID4:
Sathya Perlafe6d2a32010-11-21 23:25:50 +00003259 pci_read_config_dword(pdev, SLI_INTF_REG_OFFSET, &sli_intf);
3260 if_type = (sli_intf & SLI_INTF_IF_TYPE_MASK) >>
3261 SLI_INTF_IF_TYPE_SHIFT;
3262
3263 if (((sli_intf & SLI_INTF_VALID_MASK) != SLI_INTF_VALID) ||
3264 if_type != 0x02) {
3265 dev_err(&pdev->dev, "SLI_INTF reg val is not valid\n");
3266 return -EINVAL;
3267 }
Sathya Perlafe6d2a32010-11-21 23:25:50 +00003268 adapter->sli_family = ((sli_intf & SLI_INTF_FAMILY_MASK) >>
3269 SLI_INTF_FAMILY_SHIFT);
3270 adapter->generation = BE_GEN3;
3271 break;
3272 default:
3273 adapter->generation = 0;
3274 }
3275 return 0;
3276}
3277
Padmanabh Ratnakar37eed1c2011-03-07 03:08:36 +00003278static int lancer_wait_ready(struct be_adapter *adapter)
3279{
3280#define SLIPORT_READY_TIMEOUT 500
3281 u32 sliport_status;
3282 int status = 0, i;
3283
3284 for (i = 0; i < SLIPORT_READY_TIMEOUT; i++) {
3285 sliport_status = ioread32(adapter->db + SLIPORT_STATUS_OFFSET);
3286 if (sliport_status & SLIPORT_STATUS_RDY_MASK)
3287 break;
3288
3289 msleep(20);
3290 }
3291
3292 if (i == SLIPORT_READY_TIMEOUT)
3293 status = -1;
3294
3295 return status;
3296}
3297
3298static int lancer_test_and_set_rdy_state(struct be_adapter *adapter)
3299{
3300 int status;
3301 u32 sliport_status, err, reset_needed;
3302 status = lancer_wait_ready(adapter);
3303 if (!status) {
3304 sliport_status = ioread32(adapter->db + SLIPORT_STATUS_OFFSET);
3305 err = sliport_status & SLIPORT_STATUS_ERR_MASK;
3306 reset_needed = sliport_status & SLIPORT_STATUS_RN_MASK;
3307 if (err && reset_needed) {
3308 iowrite32(SLI_PORT_CONTROL_IP_MASK,
3309 adapter->db + SLIPORT_CONTROL_OFFSET);
3310
3311 /* check adapter has corrected the error */
3312 status = lancer_wait_ready(adapter);
3313 sliport_status = ioread32(adapter->db +
3314 SLIPORT_STATUS_OFFSET);
3315 sliport_status &= (SLIPORT_STATUS_ERR_MASK |
3316 SLIPORT_STATUS_RN_MASK);
3317 if (status || sliport_status)
3318 status = -1;
3319 } else if (err || reset_needed) {
3320 status = -1;
3321 }
3322 }
3323 return status;
3324}
3325
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003326static int __devinit be_probe(struct pci_dev *pdev,
3327 const struct pci_device_id *pdev_id)
3328{
3329 int status = 0;
3330 struct be_adapter *adapter;
3331 struct net_device *netdev;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003332
3333 status = pci_enable_device(pdev);
3334 if (status)
3335 goto do_none;
3336
3337 status = pci_request_regions(pdev, DRV_NAME);
3338 if (status)
3339 goto disable_dev;
3340 pci_set_master(pdev);
3341
Sathya Perla3c8def92011-06-12 20:01:58 +00003342 netdev = alloc_etherdev_mq(sizeof(struct be_adapter), MAX_TX_QS);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003343 if (netdev == NULL) {
3344 status = -ENOMEM;
3345 goto rel_reg;
3346 }
3347 adapter = netdev_priv(netdev);
3348 adapter->pdev = pdev;
3349 pci_set_drvdata(pdev, adapter);
Sathya Perlafe6d2a32010-11-21 23:25:50 +00003350
3351 status = be_dev_family_check(adapter);
Sathya Perla63657b92010-12-01 01:02:28 +00003352 if (status)
Sathya Perlafe6d2a32010-11-21 23:25:50 +00003353 goto free_netdev;
3354
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003355 adapter->netdev = netdev;
Sathya Perla2243e2e2009-11-22 22:02:03 +00003356 SET_NETDEV_DEV(netdev, &pdev->dev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003357
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00003358 status = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003359 if (!status) {
3360 netdev->features |= NETIF_F_HIGHDMA;
3361 } else {
Ivan Vecera2b7bceb2011-02-02 08:05:12 +00003362 status = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003363 if (status) {
3364 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
3365 goto free_netdev;
3366 }
3367 }
3368
Sathya Perlaf9449ab2011-10-24 02:45:01 +00003369 status = be_sriov_enable(adapter);
3370 if (status)
3371 goto free_netdev;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003372
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003373 status = be_ctrl_init(adapter);
3374 if (status)
Sathya Perlaf9449ab2011-10-24 02:45:01 +00003375 goto disable_sriov;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003376
Padmanabh Ratnakar37eed1c2011-03-07 03:08:36 +00003377 if (lancer_chip(adapter)) {
3378 status = lancer_test_and_set_rdy_state(adapter);
3379 if (status) {
3380 dev_err(&pdev->dev, "Adapter in non recoverable error\n");
Ajit Khaparde48f5a192011-04-06 18:08:30 +00003381 goto ctrl_clean;
Padmanabh Ratnakar37eed1c2011-03-07 03:08:36 +00003382 }
3383 }
3384
Sathya Perla2243e2e2009-11-22 22:02:03 +00003385 /* sync up with fw's ready state */
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003386 if (be_physfn(adapter)) {
3387 status = be_cmd_POST(adapter);
3388 if (status)
3389 goto ctrl_clean;
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003390 }
Sathya Perla2243e2e2009-11-22 22:02:03 +00003391
3392 /* tell fw we're ready to fire cmds */
3393 status = be_cmd_fw_init(adapter);
3394 if (status)
3395 goto ctrl_clean;
3396
Ajit Khapardea4b4dfa2011-02-11 13:36:57 +00003397 status = be_cmd_reset_function(adapter);
3398 if (status)
3399 goto ctrl_clean;
Sarveshwar Bandi556ae192010-05-24 18:38:25 -07003400
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003401 status = be_stats_init(adapter);
3402 if (status)
3403 goto ctrl_clean;
3404
Sathya Perla2243e2e2009-11-22 22:02:03 +00003405 status = be_get_config(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003406 if (status)
3407 goto stats_clean;
3408
Sathya Perlab9ab82c2011-06-29 23:33:37 +00003409 /* The INTR bit may be set in the card when probed by a kdump kernel
3410 * after a crash.
3411 */
3412 if (!lancer_chip(adapter))
3413 be_intr_set(adapter, false);
3414
Sathya Perla3abcded2010-10-03 22:12:27 -07003415 be_msix_enable(adapter);
3416
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003417 INIT_DELAYED_WORK(&adapter->work, be_worker);
Sathya Perlaa54769f2011-10-24 02:45:00 +00003418 adapter->rx_fc = adapter->tx_fc = true;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003419
Sathya Perla5fb379e2009-06-18 00:02:59 +00003420 status = be_setup(adapter);
3421 if (status)
Sathya Perla3abcded2010-10-03 22:12:27 -07003422 goto msix_disable;
Sathya Perla2243e2e2009-11-22 22:02:03 +00003423
Sathya Perla3abcded2010-10-03 22:12:27 -07003424 be_netdev_init(netdev);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003425 status = register_netdev(netdev);
3426 if (status != 0)
Sathya Perla5fb379e2009-06-18 00:02:59 +00003427 goto unsetup;
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003428
Ajit Khapardec4ca2372009-05-18 15:38:55 -07003429 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
Somnath Kotur34b1ef02011-06-01 00:33:22 +00003430
Somnath Koturf203af72010-10-25 23:01:03 +00003431 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003432 return 0;
3433
Sathya Perla5fb379e2009-06-18 00:02:59 +00003434unsetup:
3435 be_clear(adapter);
Sathya Perla3abcded2010-10-03 22:12:27 -07003436msix_disable:
3437 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003438stats_clean:
3439 be_stats_cleanup(adapter);
3440ctrl_clean:
3441 be_ctrl_cleanup(adapter);
Sathya Perlaf9449ab2011-10-24 02:45:01 +00003442disable_sriov:
Sarveshwar Bandiba343c72010-03-31 02:56:12 +00003443 be_sriov_disable(adapter);
Sathya Perlaf9449ab2011-10-24 02:45:01 +00003444free_netdev:
Sathya Perlafe6d2a32010-11-21 23:25:50 +00003445 free_netdev(netdev);
Sathya Perla8d56ff12009-11-22 22:02:26 +00003446 pci_set_drvdata(pdev, NULL);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003447rel_reg:
3448 pci_release_regions(pdev);
3449disable_dev:
3450 pci_disable_device(pdev);
3451do_none:
Ajit Khapardec4ca2372009-05-18 15:38:55 -07003452 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003453 return status;
3454}
3455
3456static int be_suspend(struct pci_dev *pdev, pm_message_t state)
3457{
3458 struct be_adapter *adapter = pci_get_drvdata(pdev);
3459 struct net_device *netdev = adapter->netdev;
3460
Ajit Khapardea4ca0552011-02-11 13:38:03 +00003461 cancel_delayed_work_sync(&adapter->work);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00003462 if (adapter->wol)
3463 be_setup_wol(adapter, true);
3464
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003465 netif_device_detach(netdev);
3466 if (netif_running(netdev)) {
3467 rtnl_lock();
3468 be_close(netdev);
3469 rtnl_unlock();
3470 }
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00003471 be_clear(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003472
Ajit Khapardea4ca0552011-02-11 13:38:03 +00003473 be_msix_disable(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003474 pci_save_state(pdev);
3475 pci_disable_device(pdev);
3476 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3477 return 0;
3478}
3479
3480static int be_resume(struct pci_dev *pdev)
3481{
3482 int status = 0;
3483 struct be_adapter *adapter = pci_get_drvdata(pdev);
3484 struct net_device *netdev = adapter->netdev;
3485
3486 netif_device_detach(netdev);
3487
3488 status = pci_enable_device(pdev);
3489 if (status)
3490 return status;
3491
3492 pci_set_power_state(pdev, 0);
3493 pci_restore_state(pdev);
3494
Ajit Khapardea4ca0552011-02-11 13:38:03 +00003495 be_msix_enable(adapter);
Sathya Perla2243e2e2009-11-22 22:02:03 +00003496 /* tell fw we're ready to fire cmds */
3497 status = be_cmd_fw_init(adapter);
3498 if (status)
3499 return status;
3500
Sarveshwar Bandi9b0365f2009-08-12 21:01:29 +00003501 be_setup(adapter);
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003502 if (netif_running(netdev)) {
3503 rtnl_lock();
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003504 be_open(netdev);
3505 rtnl_unlock();
3506 }
3507 netif_device_attach(netdev);
Ajit Khaparde71d8d1b2009-12-03 06:16:59 +00003508
3509 if (adapter->wol)
3510 be_setup_wol(adapter, false);
Ajit Khapardea4ca0552011-02-11 13:38:03 +00003511
3512 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003513 return 0;
3514}
3515
Sathya Perla82456b02010-02-17 01:35:37 +00003516/*
3517 * An FLR will stop BE from DMAing any data.
3518 */
3519static void be_shutdown(struct pci_dev *pdev)
3520{
3521 struct be_adapter *adapter = pci_get_drvdata(pdev);
Sathya Perla82456b02010-02-17 01:35:37 +00003522
Ajit Khaparde2d5d4152011-04-06 05:53:13 +00003523 if (!adapter)
3524 return;
Sathya Perla82456b02010-02-17 01:35:37 +00003525
Sathya Perla0f4a6822011-03-21 20:49:28 +00003526 cancel_delayed_work_sync(&adapter->work);
Ajit Khapardea4ca0552011-02-11 13:38:03 +00003527
Ajit Khaparde2d5d4152011-04-06 05:53:13 +00003528 netif_device_detach(adapter->netdev);
Sathya Perla82456b02010-02-17 01:35:37 +00003529
Sathya Perla82456b02010-02-17 01:35:37 +00003530 if (adapter->wol)
3531 be_setup_wol(adapter, true);
3532
Ajit Khaparde57841862011-04-06 18:08:43 +00003533 be_cmd_reset_function(adapter);
3534
Sathya Perla82456b02010-02-17 01:35:37 +00003535 pci_disable_device(pdev);
Sathya Perla82456b02010-02-17 01:35:37 +00003536}
3537
Sathya Perlacf588472010-02-14 21:22:01 +00003538static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
3539 pci_channel_state_t state)
3540{
3541 struct be_adapter *adapter = pci_get_drvdata(pdev);
3542 struct net_device *netdev = adapter->netdev;
3543
3544 dev_err(&adapter->pdev->dev, "EEH error detected\n");
3545
3546 adapter->eeh_err = true;
3547
3548 netif_device_detach(netdev);
3549
3550 if (netif_running(netdev)) {
3551 rtnl_lock();
3552 be_close(netdev);
3553 rtnl_unlock();
3554 }
3555 be_clear(adapter);
3556
3557 if (state == pci_channel_io_perm_failure)
3558 return PCI_ERS_RESULT_DISCONNECT;
3559
3560 pci_disable_device(pdev);
3561
3562 return PCI_ERS_RESULT_NEED_RESET;
3563}
3564
3565static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
3566{
3567 struct be_adapter *adapter = pci_get_drvdata(pdev);
3568 int status;
3569
3570 dev_info(&adapter->pdev->dev, "EEH reset\n");
3571 adapter->eeh_err = false;
3572
3573 status = pci_enable_device(pdev);
3574 if (status)
3575 return PCI_ERS_RESULT_DISCONNECT;
3576
3577 pci_set_master(pdev);
3578 pci_set_power_state(pdev, 0);
3579 pci_restore_state(pdev);
3580
3581 /* Check if card is ok and fw is ready */
3582 status = be_cmd_POST(adapter);
3583 if (status)
3584 return PCI_ERS_RESULT_DISCONNECT;
3585
3586 return PCI_ERS_RESULT_RECOVERED;
3587}
3588
3589static void be_eeh_resume(struct pci_dev *pdev)
3590{
3591 int status = 0;
3592 struct be_adapter *adapter = pci_get_drvdata(pdev);
3593 struct net_device *netdev = adapter->netdev;
3594
3595 dev_info(&adapter->pdev->dev, "EEH resume\n");
3596
3597 pci_save_state(pdev);
3598
3599 /* tell fw we're ready to fire cmds */
3600 status = be_cmd_fw_init(adapter);
3601 if (status)
3602 goto err;
3603
3604 status = be_setup(adapter);
3605 if (status)
3606 goto err;
3607
3608 if (netif_running(netdev)) {
3609 status = be_open(netdev);
3610 if (status)
3611 goto err;
3612 }
3613 netif_device_attach(netdev);
3614 return;
3615err:
3616 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
Sathya Perlacf588472010-02-14 21:22:01 +00003617}
3618
3619static struct pci_error_handlers be_eeh_handlers = {
3620 .error_detected = be_eeh_err_detected,
3621 .slot_reset = be_eeh_reset,
3622 .resume = be_eeh_resume,
3623};
3624
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003625static struct pci_driver be_driver = {
3626 .name = DRV_NAME,
3627 .id_table = be_dev_ids,
3628 .probe = be_probe,
3629 .remove = be_remove,
3630 .suspend = be_suspend,
Sathya Perlacf588472010-02-14 21:22:01 +00003631 .resume = be_resume,
Sathya Perla82456b02010-02-17 01:35:37 +00003632 .shutdown = be_shutdown,
Sathya Perlacf588472010-02-14 21:22:01 +00003633 .err_handler = &be_eeh_handlers
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003634};
3635
3636static int __init be_init_module(void)
3637{
Joe Perches8e95a202009-12-03 07:58:21 +00003638 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
3639 rx_frag_size != 2048) {
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003640 printk(KERN_WARNING DRV_NAME
3641 " : Module param rx_frag_size must be 2048/4096/8192."
3642 " Using 2048\n");
3643 rx_frag_size = 2048;
3644 }
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003645
Sathya Perla6b7c5b92009-03-11 23:32:03 -07003646 return pci_register_driver(&be_driver);
3647}
3648module_init(be_init_module);
3649
3650static void __exit be_exit_module(void)
3651{
3652 pci_unregister_driver(&be_driver);
3653}
3654module_exit(be_exit_module);