blob: 24ad17ec7fcde46a6f11491873546f8ffbb3cef2 [file] [log] [blame]
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001/*
Sritej Velaga40839129f2010-12-02 20:41:56 +00002 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2010 QLogic Corporation
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004 *
Sritej Velaga40839129f2010-12-02 20:41:56 +00005 * See LICENSE.qlcnic for copyright and licensing details.
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00006 */
7
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00009#include <linux/vmalloc.h>
10#include <linux/interrupt.h>
11
12#include "qlcnic.h"
13
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +000014#include <linux/swab.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000015#include <linux/dma-mapping.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000016#include <net/ip.h>
17#include <linux/ipv6.h>
18#include <linux/inetdevice.h>
19#include <linux/sysfs.h>
Sucheta Chakraborty451724c2010-07-13 20:33:34 +000020#include <linux/aer.h>
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +000021#include <linux/log2.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000022
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000023MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000024MODULE_LICENSE("GPL");
25MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
26MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
27
28char qlcnic_driver_name[] = "qlcnic";
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000029static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
30 "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000031
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +000032static struct workqueue_struct *qlcnic_wq;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +000033static int qlcnic_mac_learn;
amit salechab11a25a2011-01-10 00:15:23 +000034module_param(qlcnic_mac_learn, int, 0444);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +000035MODULE_PARM_DESC(qlcnic_mac_learn, "Mac Filter (0=disabled, 1=enabled)");
36
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000037static int use_msi = 1;
amit salechab11a25a2011-01-10 00:15:23 +000038module_param(use_msi, int, 0444);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000039MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
40
41static int use_msi_x = 1;
amit salechab11a25a2011-01-10 00:15:23 +000042module_param(use_msi_x, int, 0444);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000043MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
44
amit salecha9ce13ca2011-02-23 03:21:24 +000045static int auto_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000046module_param(auto_fw_reset, int, 0644);
47MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
48
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +000049static int load_fw_file;
amit salechab11a25a2011-01-10 00:15:23 +000050module_param(load_fw_file, int, 0444);
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +000051MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
52
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +000053static int qlcnic_config_npars;
amit salechab11a25a2011-01-10 00:15:23 +000054module_param(qlcnic_config_npars, int, 0444);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +000055MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
56
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000057static int __devinit qlcnic_probe(struct pci_dev *pdev,
58 const struct pci_device_id *ent);
59static void __devexit qlcnic_remove(struct pci_dev *pdev);
60static int qlcnic_open(struct net_device *netdev);
61static int qlcnic_close(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000062static void qlcnic_tx_timeout(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000063static void qlcnic_attach_work(struct work_struct *work);
64static void qlcnic_fwinit_work(struct work_struct *work);
65static void qlcnic_fw_poll_work(struct work_struct *work);
66static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
67 work_func_t func, int delay);
68static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
69static int qlcnic_poll(struct napi_struct *napi, int budget);
schacko8f891382010-06-17 02:56:40 +000070static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000071#ifdef CONFIG_NET_POLL_CONTROLLER
72static void qlcnic_poll_controller(struct net_device *netdev);
73#endif
74
75static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
76static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
77static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
78static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
79
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +000080static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +000081static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000082static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
83
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +000084static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000085static irqreturn_t qlcnic_intr(int irq, void *data);
86static irqreturn_t qlcnic_msi_intr(int irq, void *data);
87static irqreturn_t qlcnic_msix_intr(int irq, void *data);
88
89static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +000090static void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000091static int qlcnic_start_firmware(struct qlcnic_adapter *);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000092
Amit Kumar Salechab5e54922010-08-31 17:17:51 +000093static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000094static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000095static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
96static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
97static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
Rajesh Borundia0325d692010-08-19 05:08:26 +000098static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
99 struct qlcnic_esw_func_cfg *);
Jiri Pirko8e586132011-12-08 19:52:37 -0500100static int qlcnic_vlan_rx_add(struct net_device *, u16);
101static int qlcnic_vlan_rx_del(struct net_device *, u16);
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000102
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000103/* PCI Device ID Table */
104#define ENTRY(device) \
105 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
106 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
107
108#define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
109
Amit Kumar Salecha6a902882010-02-01 05:24:54 +0000110static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000111 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
112 {0,}
113};
114
115MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
116
117
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000118inline void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000119qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
120 struct qlcnic_host_tx_ring *tx_ring)
121{
122 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000123}
124
125static const u32 msi_tgt_status[8] = {
126 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
127 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
128 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
129 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
130};
131
132static const
133struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
134
135static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
136{
137 writel(0, sds_ring->crb_intr_mask);
138}
139
140static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
141{
142 struct qlcnic_adapter *adapter = sds_ring->adapter;
143
144 writel(0x1, sds_ring->crb_intr_mask);
145
146 if (!QLCNIC_IS_MSI_FAMILY(adapter))
147 writel(0xfbff, adapter->tgt_mask_reg);
148}
149
150static int
151qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
152{
153 int size = sizeof(struct qlcnic_host_sds_ring) * count;
154
155 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
156
Eric Dumazet807540b2010-09-23 05:40:09 +0000157 return recv_ctx->sds_rings == NULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000158}
159
160static void
161qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
162{
163 if (recv_ctx->sds_rings != NULL)
164 kfree(recv_ctx->sds_rings);
165
166 recv_ctx->sds_rings = NULL;
167}
168
169static int
170qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
171{
172 int ring;
173 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000174 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000175
176 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
177 return -ENOMEM;
178
179 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
180 sds_ring = &recv_ctx->sds_rings[ring];
schacko8f891382010-06-17 02:56:40 +0000181
182 if (ring == adapter->max_sds_rings - 1)
183 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
184 QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
185 else
186 netif_napi_add(netdev, &sds_ring->napi,
187 qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000188 }
189
190 return 0;
191}
192
193static void
194qlcnic_napi_del(struct qlcnic_adapter *adapter)
195{
196 int ring;
197 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000198 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000199
200 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
201 sds_ring = &recv_ctx->sds_rings[ring];
202 netif_napi_del(&sds_ring->napi);
203 }
204
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000205 qlcnic_free_sds_rings(adapter->recv_ctx);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000206}
207
208static void
209qlcnic_napi_enable(struct qlcnic_adapter *adapter)
210{
211 int ring;
212 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000213 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000214
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000215 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
216 return;
217
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000218 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
219 sds_ring = &recv_ctx->sds_rings[ring];
220 napi_enable(&sds_ring->napi);
221 qlcnic_enable_int(sds_ring);
222 }
223}
224
225static void
226qlcnic_napi_disable(struct qlcnic_adapter *adapter)
227{
228 int ring;
229 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000230 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000231
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000232 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
233 return;
234
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000235 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
236 sds_ring = &recv_ctx->sds_rings[ring];
237 qlcnic_disable_int(sds_ring);
238 napi_synchronize(&sds_ring->napi);
239 napi_disable(&sds_ring->napi);
240 }
241}
242
243static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
244{
245 memset(&adapter->stats, 0, sizeof(adapter->stats));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000246}
247
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000248static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
249{
250 u32 control;
251 int pos;
252
253 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
254 if (pos) {
255 pci_read_config_dword(pdev, pos, &control);
256 if (enable)
257 control |= PCI_MSIX_FLAGS_ENABLE;
258 else
259 control = 0;
260 pci_write_config_dword(pdev, pos, control);
261 }
262}
263
264static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
265{
266 int i;
267
268 for (i = 0; i < count; i++)
269 adapter->msix_entries[i].entry = i;
270}
271
272static int
273qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
274{
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000275 u8 mac_addr[ETH_ALEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000276 struct net_device *netdev = adapter->netdev;
277 struct pci_dev *pdev = adapter->pdev;
278
Rajesh Borundiada48e6c2010-08-31 17:17:46 +0000279 if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000280 return -EIO;
281
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000282 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000283 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
284 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
285
286 /* set station address */
287
288 if (!is_valid_ether_addr(netdev->perm_addr))
289 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
290 netdev->dev_addr);
291
292 return 0;
293}
294
295static int qlcnic_set_mac(struct net_device *netdev, void *p)
296{
297 struct qlcnic_adapter *adapter = netdev_priv(netdev);
298 struct sockaddr *addr = p;
299
Rajesh Borundia73733732010-08-31 17:17:50 +0000300 if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
301 return -EOPNOTSUPP;
302
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000303 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +0000304 return -EADDRNOTAVAIL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000305
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000306 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000307 netif_device_detach(netdev);
308 qlcnic_napi_disable(adapter);
309 }
310
311 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
312 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
313 qlcnic_set_multi(adapter->netdev);
314
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000315 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000316 netif_device_attach(netdev);
317 qlcnic_napi_enable(adapter);
318 }
319 return 0;
320}
321
322static const struct net_device_ops qlcnic_netdev_ops = {
323 .ndo_open = qlcnic_open,
324 .ndo_stop = qlcnic_close,
325 .ndo_start_xmit = qlcnic_xmit_frame,
326 .ndo_get_stats = qlcnic_get_stats,
327 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000328 .ndo_set_rx_mode = qlcnic_set_multi,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000329 .ndo_set_mac_address = qlcnic_set_mac,
330 .ndo_change_mtu = qlcnic_change_mtu,
Michał Mirosław135d84a2011-04-19 03:03:57 +0000331 .ndo_fix_features = qlcnic_fix_features,
332 .ndo_set_features = qlcnic_set_features,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000333 .ndo_tx_timeout = qlcnic_tx_timeout,
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000334 .ndo_vlan_rx_add_vid = qlcnic_vlan_rx_add,
335 .ndo_vlan_rx_kill_vid = qlcnic_vlan_rx_del,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000336#ifdef CONFIG_NET_POLL_CONTROLLER
337 .ndo_poll_controller = qlcnic_poll_controller,
338#endif
339};
340
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +0000341static const struct net_device_ops qlcnic_netdev_failed_ops = {
342 .ndo_open = qlcnic_open,
343};
344
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000345static struct qlcnic_nic_template qlcnic_ops = {
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000346 .config_bridged_mode = qlcnic_config_bridged_mode,
347 .config_led = qlcnic_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000348 .start_firmware = qlcnic_start_firmware
349};
350
351static struct qlcnic_nic_template qlcnic_vf_ops = {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000352 .config_bridged_mode = qlcnicvf_config_bridged_mode,
353 .config_led = qlcnicvf_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000354 .start_firmware = qlcnicvf_start_firmware
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000355};
356
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000357static int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
358{
359 struct pci_dev *pdev = adapter->pdev;
360 int err = -1;
361
362 adapter->max_sds_rings = 1;
363 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
364 qlcnic_set_msix_bit(pdev, 0);
365
366 if (adapter->msix_supported) {
367 enable_msix:
368 qlcnic_init_msix_entries(adapter, num_msix);
369 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
370 if (err == 0) {
371 adapter->flags |= QLCNIC_MSIX_ENABLED;
372 qlcnic_set_msix_bit(pdev, 1);
373
374 adapter->max_sds_rings = num_msix;
375
376 dev_info(&pdev->dev, "using msi-x interrupts\n");
377 return err;
378 }
379 if (err > 0) {
380 num_msix = rounddown_pow_of_two(err);
381 if (num_msix)
382 goto enable_msix;
383 }
384 }
385 return err;
386}
387
388
389static void qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000390{
391 const struct qlcnic_legacy_intr_set *legacy_intrp;
392 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000393
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000394 if (use_msi && !pci_enable_msi(pdev)) {
395 adapter->flags |= QLCNIC_MSI_ENABLED;
396 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
397 msi_tgt_status[adapter->ahw->pci_func]);
398 dev_info(&pdev->dev, "using msi interrupts\n");
399 adapter->msix_entries[0].vector = pdev->irq;
400 return;
401 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000402
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000403 legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000404
405 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
406 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
407 legacy_intrp->tgt_status_reg);
408 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
409 legacy_intrp->tgt_mask_reg);
410 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
411
412 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
413 ISR_INT_STATE_REG);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000414 dev_info(&pdev->dev, "using legacy interrupts\n");
415 adapter->msix_entries[0].vector = pdev->irq;
416}
417
418static void
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000419qlcnic_setup_intr(struct qlcnic_adapter *adapter)
420{
421 int num_msix;
422
423 if (adapter->msix_supported) {
Sucheta Chakraborty5f6ec292011-06-22 02:52:20 +0000424 num_msix = rounddown_pow_of_two(min_t(int, num_online_cpus(),
425 QLCNIC_DEF_NUM_STS_DESC_RINGS));
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000426 } else
427 num_msix = 1;
428
429 if (!qlcnic_enable_msix(adapter, num_msix))
430 return;
431
432 qlcnic_enable_msi_legacy(adapter);
433}
434
435static void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000436qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
437{
438 if (adapter->flags & QLCNIC_MSIX_ENABLED)
439 pci_disable_msix(adapter->pdev);
440 if (adapter->flags & QLCNIC_MSI_ENABLED)
441 pci_disable_msi(adapter->pdev);
442}
443
444static void
445qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
446{
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000447 if (adapter->ahw->pci_base0 != NULL)
448 iounmap(adapter->ahw->pci_base0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000449}
450
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000451static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000452qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
453{
Dan Carpentere88db3b2010-08-09 21:49:36 +0000454 struct qlcnic_pci_info *pci_info;
Julia Lawall900853a2010-08-16 10:23:51 +0000455 int i, ret = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000456 u8 pfn;
457
Dan Carpentere88db3b2010-08-09 21:49:36 +0000458 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
459 if (!pci_info)
460 return -ENOMEM;
461
Dan Carpenterca315ac2010-08-09 21:47:56 +0000462 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000463 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
Dan Carpentere88db3b2010-08-09 21:49:36 +0000464 if (!adapter->npars) {
Julia Lawall900853a2010-08-16 10:23:51 +0000465 ret = -ENOMEM;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000466 goto err_pci_info;
467 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000468
Dan Carpenterca315ac2010-08-09 21:47:56 +0000469 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000470 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
471 if (!adapter->eswitch) {
Julia Lawall900853a2010-08-16 10:23:51 +0000472 ret = -ENOMEM;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000473 goto err_npars;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000474 }
475
476 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000477 if (ret)
478 goto err_eswitch;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000479
Dan Carpenterca315ac2010-08-09 21:47:56 +0000480 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
481 pfn = pci_info[i].id;
Dan Carpenter0f6efff92012-06-14 08:34:24 +0000482 if (pfn >= QLCNIC_MAX_PCI_FUNC) {
Sony Chackof848d6d2011-04-01 14:27:59 +0000483 ret = QL_STATUS_INVALID_PARAM;
484 goto err_eswitch;
485 }
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000486 adapter->npars[pfn].active = (u8)pci_info[i].active;
487 adapter->npars[pfn].type = (u8)pci_info[i].type;
488 adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000489 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
490 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000491 }
492
Dan Carpenterca315ac2010-08-09 21:47:56 +0000493 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
494 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
495
Dan Carpentere88db3b2010-08-09 21:49:36 +0000496 kfree(pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000497 return 0;
498
499err_eswitch:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000500 kfree(adapter->eswitch);
501 adapter->eswitch = NULL;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000502err_npars:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000503 kfree(adapter->npars);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000504 adapter->npars = NULL;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000505err_pci_info:
506 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000507
508 return ret;
509}
510
511static int
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000512qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
513{
514 u8 id;
515 u32 ref_count;
516 int i, ret = 1;
517 u32 data = QLCNIC_MGMT_FUNC;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000518 void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000519
520 /* If other drivers are not in use set their privilege level */
Amit Kumar Salecha31018e02010-08-25 04:03:05 +0000521 ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000522 ret = qlcnic_api_lock(adapter);
523 if (ret)
524 goto err_lock;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000525
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000526 if (qlcnic_config_npars) {
527 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000528 id = i;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000529 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000530 id == adapter->ahw->pci_func)
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000531 continue;
532 data |= (qlcnic_config_npars &
533 QLC_DEV_SET_DRV(0xf, id));
534 }
535 } else {
536 data = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000537 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000538 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000539 adapter->ahw->pci_func));
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000540 }
541 writel(data, priv_op);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000542 qlcnic_api_unlock(adapter);
543err_lock:
544 return ret;
545}
546
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000547static void
548qlcnic_check_vf(struct qlcnic_adapter *adapter)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000549{
550 void __iomem *msix_base_addr;
551 void __iomem *priv_op;
552 u32 func;
553 u32 msix_base;
554 u32 op_mode, priv_level;
555
556 /* Determine FW API version */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000557 adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
558 QLCNIC_FW_API);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000559
560 /* Find PCI function number */
561 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000562 msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000563 msix_base = readl(msix_base_addr);
564 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000565 adapter->ahw->pci_func = func;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000566
567 /* Determine function privilege level */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000568 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000569 op_mode = readl(priv_op);
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000570 if (op_mode == QLC_DEV_DRV_DEFAULT)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000571 priv_level = QLCNIC_MGMT_FUNC;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000572 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000573 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000574
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000575 if (priv_level == QLCNIC_NON_PRIV_FUNC) {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000576 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
577 dev_info(&adapter->pdev->dev,
578 "HAL Version: %d Non Privileged function\n",
579 adapter->fw_hal_version);
580 adapter->nic_ops = &qlcnic_vf_ops;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000581 } else
582 adapter->nic_ops = &qlcnic_ops;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000583}
584
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000585static int
586qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
587{
588 void __iomem *mem_ptr0 = NULL;
589 resource_size_t mem_base;
590 unsigned long mem_len, pci_len0 = 0;
591
592 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000593
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000594 /* remap phys address */
595 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
596 mem_len = pci_resource_len(pdev, 0);
597
598 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
599
600 mem_ptr0 = pci_ioremap_bar(pdev, 0);
601 if (mem_ptr0 == NULL) {
602 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
603 return -EIO;
604 }
605 pci_len0 = mem_len;
606 } else {
607 return -EIO;
608 }
609
610 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
611
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000612 adapter->ahw->pci_base0 = mem_ptr0;
613 adapter->ahw->pci_len0 = pci_len0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000614
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000615 qlcnic_check_vf(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000616
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000617 adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
618 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
619 adapter->ahw->pci_func)));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000620
621 return 0;
622}
623
624static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
625{
626 struct pci_dev *pdev = adapter->pdev;
627 int i, found = 0;
628
629 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
630 if (qlcnic_boards[i].vendor == pdev->vendor &&
631 qlcnic_boards[i].device == pdev->device &&
632 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
633 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +0000634 sprintf(name, "%pM: %s" ,
635 adapter->mac_addr,
636 qlcnic_boards[i].short_name);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000637 found = 1;
638 break;
639 }
640
641 }
642
643 if (!found)
Sritej Velaga7f9a0c32010-06-17 02:56:39 +0000644 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000645}
646
647static void
648qlcnic_check_options(struct qlcnic_adapter *adapter)
649{
Sritej Velaga031a4a22011-07-29 13:30:28 +0000650 u32 fw_major, fw_minor, fw_build, prev_fw_version;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000651 struct pci_dev *pdev = adapter->pdev;
Sritej Velaga031a4a22011-07-29 13:30:28 +0000652 struct qlcnic_fw_dump *fw_dump = &adapter->ahw->fw_dump;
653
654 prev_fw_version = adapter->fw_version;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000655
656 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
657 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
658 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
659
660 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
661
Sritej Velaga031a4a22011-07-29 13:30:28 +0000662 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC) {
663 if (fw_dump->tmpl_hdr == NULL ||
664 adapter->fw_version > prev_fw_version) {
665 if (fw_dump->tmpl_hdr)
666 vfree(fw_dump->tmpl_hdr);
667 if (!qlcnic_fw_cmd_get_minidump_temp(adapter))
668 dev_info(&pdev->dev,
669 "Supports FW dump capability\n");
670 }
671 }
672
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000673 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
674 fw_major, fw_minor, fw_build);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000675 if (adapter->ahw->port_type == QLCNIC_XGBE) {
Sony Chacko90d19002010-10-26 17:53:08 +0000676 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
677 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
678 adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
679 } else {
680 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
681 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
682 }
683
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000684 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
Sony Chacko90d19002010-10-26 17:53:08 +0000685 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
686
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000687 } else if (adapter->ahw->port_type == QLCNIC_GBE) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000688 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
689 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
Sony Chacko90d19002010-10-26 17:53:08 +0000690 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
691 adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000692 }
693
694 adapter->msix_supported = !!use_msi_x;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000695
696 adapter->num_txd = MAX_CMD_DESCRIPTORS;
697
Sony Chacko251b0362010-08-19 05:08:24 +0000698 adapter->max_rds_rings = MAX_RDS_RINGS;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000699}
700
Rajesh Borundia174240a2010-08-31 17:17:47 +0000701static int
702qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
703{
704 int err;
705 struct qlcnic_info nic_info;
706
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000707 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
Rajesh Borundia174240a2010-08-31 17:17:47 +0000708 if (err)
709 return err;
710
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000711 adapter->physical_port = (u8)nic_info.phys_port;
Rajesh Borundia174240a2010-08-31 17:17:47 +0000712 adapter->switch_mode = nic_info.switch_mode;
713 adapter->max_tx_ques = nic_info.max_tx_ques;
714 adapter->max_rx_ques = nic_info.max_rx_ques;
715 adapter->capabilities = nic_info.capabilities;
716 adapter->max_mac_filters = nic_info.max_mac_filters;
717 adapter->max_mtu = nic_info.max_mtu;
718
719 if (adapter->capabilities & BIT_6)
720 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
721 else
722 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
723
724 return err;
725}
726
Rajesh Borundia0325d692010-08-19 05:08:26 +0000727static void
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000728qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
729 struct qlcnic_esw_func_cfg *esw_cfg)
730{
731 if (esw_cfg->discard_tagged)
732 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
733 else
734 adapter->flags |= QLCNIC_TAGGING_ENABLED;
735
736 if (esw_cfg->vlan_id)
737 adapter->pvid = esw_cfg->vlan_id;
738 else
739 adapter->pvid = 0;
740}
741
Jiri Pirko8e586132011-12-08 19:52:37 -0500742static int
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000743qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
744{
745 struct qlcnic_adapter *adapter = netdev_priv(netdev);
746 set_bit(vid, adapter->vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -0500747 return 0;
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000748}
749
Jiri Pirko8e586132011-12-08 19:52:37 -0500750static int
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000751qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
752{
753 struct qlcnic_adapter *adapter = netdev_priv(netdev);
754
755 qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
756 clear_bit(vid, adapter->vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -0500757 return 0;
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000758}
759
760static void
Rajesh Borundia0325d692010-08-19 05:08:26 +0000761qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
762 struct qlcnic_esw_func_cfg *esw_cfg)
763{
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000764 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
765 QLCNIC_PROMISC_DISABLED);
Rajesh Borundia7613c872010-08-31 17:17:48 +0000766
767 if (esw_cfg->mac_anti_spoof)
768 adapter->flags |= QLCNIC_MACSPOOF;
Sony Chackofe4d4342010-08-19 05:08:27 +0000769
Rajesh Borundia73733732010-08-31 17:17:50 +0000770 if (!esw_cfg->mac_override)
771 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
772
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000773 if (!esw_cfg->promisc_mode)
774 adapter->flags |= QLCNIC_PROMISC_DISABLED;
775
Rajesh Borundia0325d692010-08-19 05:08:26 +0000776 qlcnic_set_netdev_features(adapter, esw_cfg);
777}
778
779static int
780qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
781{
782 struct qlcnic_esw_func_cfg esw_cfg;
783
784 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
785 return 0;
786
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000787 esw_cfg.pci_func = adapter->ahw->pci_func;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000788 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
789 return -EIO;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000790 qlcnic_set_vlan_config(adapter, &esw_cfg);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000791 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
792
793 return 0;
794}
795
796static void
797qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
798 struct qlcnic_esw_func_cfg *esw_cfg)
799{
800 struct net_device *netdev = adapter->netdev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000801 netdev_features_t features, vlan_features;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000802
Michał Mirosław135d84a2011-04-19 03:03:57 +0000803 features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
Rajesh Borundia0325d692010-08-19 05:08:26 +0000804 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
805 vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000806 NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000807
808 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
809 features |= (NETIF_F_TSO | NETIF_F_TSO6);
810 vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
811 }
Rajesh Borundiab56421d2011-04-01 14:28:31 +0000812
813 if (netdev->features & NETIF_F_LRO)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000814 features |= NETIF_F_LRO;
815
816 if (esw_cfg->offload_flags & BIT_0) {
817 netdev->features |= features;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000818 if (!(esw_cfg->offload_flags & BIT_1))
819 netdev->features &= ~NETIF_F_TSO;
820 if (!(esw_cfg->offload_flags & BIT_2))
821 netdev->features &= ~NETIF_F_TSO6;
822 } else {
823 netdev->features &= ~features;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000824 }
825
826 netdev->vlan_features = (features & vlan_features);
827}
828
829static int
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000830qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
831{
832 void __iomem *priv_op;
833 u32 op_mode, priv_level;
834 int err = 0;
835
Rajesh Borundia174240a2010-08-31 17:17:47 +0000836 err = qlcnic_initialize_nic(adapter);
837 if (err)
838 return err;
839
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000840 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
841 return 0;
842
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000843 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000844 op_mode = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000845 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000846
847 if (op_mode == QLC_DEV_DRV_DEFAULT)
848 priv_level = QLCNIC_MGMT_FUNC;
849 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000850 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000851
Rajesh Borundia174240a2010-08-31 17:17:47 +0000852 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000853 if (priv_level == QLCNIC_MGMT_FUNC) {
854 adapter->op_mode = QLCNIC_MGMT_FUNC;
855 err = qlcnic_init_pci_info(adapter);
856 if (err)
857 return err;
858 /* Set privilege level for other functions */
859 qlcnic_set_function_modes(adapter);
860 dev_info(&adapter->pdev->dev,
861 "HAL Version: %d, Management function\n",
862 adapter->fw_hal_version);
863 } else if (priv_level == QLCNIC_PRIV_FUNC) {
864 adapter->op_mode = QLCNIC_PRIV_FUNC;
865 dev_info(&adapter->pdev->dev,
866 "HAL Version: %d, Privileged function\n",
867 adapter->fw_hal_version);
868 }
Rajesh Borundia174240a2010-08-31 17:17:47 +0000869 }
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000870
871 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
872
873 return err;
874}
875
876static int
Rajesh Borundia0325d692010-08-19 05:08:26 +0000877qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
878{
879 struct qlcnic_esw_func_cfg esw_cfg;
880 struct qlcnic_npar_info *npar;
881 u8 i;
882
Rajesh Borundia174240a2010-08-31 17:17:47 +0000883 if (adapter->need_fw_reset)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000884 return 0;
885
886 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
887 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
888 continue;
889 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
890 esw_cfg.pci_func = i;
891 esw_cfg.offload_flags = BIT_0;
Rajesh Borundia73733732010-08-31 17:17:50 +0000892 esw_cfg.mac_override = BIT_0;
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000893 esw_cfg.promisc_mode = BIT_0;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000894 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
895 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
896 if (qlcnic_config_switch_port(adapter, &esw_cfg))
897 return -EIO;
898 npar = &adapter->npars[i];
899 npar->pvid = esw_cfg.vlan_id;
Rajesh Borundia73733732010-08-31 17:17:50 +0000900 npar->mac_override = esw_cfg.mac_override;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000901 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
902 npar->discard_tagged = esw_cfg.discard_tagged;
903 npar->promisc_mode = esw_cfg.promisc_mode;
904 npar->offload_flags = esw_cfg.offload_flags;
905 }
906
907 return 0;
908}
909
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000910static int
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000911qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
912 struct qlcnic_npar_info *npar, int pci_func)
913{
914 struct qlcnic_esw_func_cfg esw_cfg;
915 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
916 esw_cfg.pci_func = pci_func;
917 esw_cfg.vlan_id = npar->pvid;
Rajesh Borundia73733732010-08-31 17:17:50 +0000918 esw_cfg.mac_override = npar->mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000919 esw_cfg.discard_tagged = npar->discard_tagged;
920 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
921 esw_cfg.offload_flags = npar->offload_flags;
922 esw_cfg.promisc_mode = npar->promisc_mode;
923 if (qlcnic_config_switch_port(adapter, &esw_cfg))
924 return -EIO;
925
926 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
927 if (qlcnic_config_switch_port(adapter, &esw_cfg))
928 return -EIO;
929
930 return 0;
931}
932
933static int
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000934qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
935{
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000936 int i, err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000937 struct qlcnic_npar_info *npar;
938 struct qlcnic_info nic_info;
939
Rajesh Borundia174240a2010-08-31 17:17:47 +0000940 if (!adapter->need_fw_reset)
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000941 return 0;
942
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000943 /* Set the NPAR config data after FW reset */
944 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
945 npar = &adapter->npars[i];
946 if (npar->type != QLCNIC_TYPE_NIC)
947 continue;
948 err = qlcnic_get_nic_info(adapter, &nic_info, i);
949 if (err)
950 return err;
951 nic_info.min_tx_bw = npar->min_bw;
952 nic_info.max_tx_bw = npar->max_bw;
953 err = qlcnic_set_nic_info(adapter, &nic_info);
954 if (err)
955 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000956
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000957 if (npar->enable_pm) {
958 err = qlcnic_config_port_mirroring(adapter,
959 npar->dest_npar, 1, i);
960 if (err)
961 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000962 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000963 err = qlcnic_reset_eswitch_config(adapter, npar, i);
964 if (err)
965 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000966 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000967 return 0;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000968}
969
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +0000970static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
971{
972 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
973 u32 npar_state;
974
975 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
976 return 0;
977
978 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
979 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
980 msleep(1000);
981 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
982 }
983 if (!npar_opt_timeo) {
984 dev_err(&adapter->pdev->dev,
985 "Waiting for NPAR state to opertional timeout\n");
986 return -EIO;
987 }
988 return 0;
989}
990
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000991static int
Rajesh Borundia174240a2010-08-31 17:17:47 +0000992qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
993{
994 int err;
995
996 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
997 adapter->op_mode != QLCNIC_MGMT_FUNC)
998 return 0;
999
1000 err = qlcnic_set_default_offload_settings(adapter);
1001 if (err)
1002 return err;
1003
1004 err = qlcnic_reset_npar_config(adapter);
1005 if (err)
1006 return err;
1007
1008 qlcnic_dev_set_npar_ready(adapter);
1009
1010 return err;
1011}
1012
1013static int
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001014qlcnic_start_firmware(struct qlcnic_adapter *adapter)
1015{
Sony Chackod4066832010-08-19 05:08:31 +00001016 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001017
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00001018 err = qlcnic_can_start_firmware(adapter);
1019 if (err < 0)
1020 return err;
1021 else if (!err)
Sony Chackod4066832010-08-19 05:08:31 +00001022 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001023
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +00001024 if (load_fw_file)
1025 qlcnic_request_firmware(adapter);
schacko8f891382010-06-17 02:56:40 +00001026 else {
Sony Chacko8cfdce02010-08-26 14:02:41 +00001027 err = qlcnic_check_flash_fw_ver(adapter);
1028 if (err)
schacko8f891382010-06-17 02:56:40 +00001029 goto err_out;
1030
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +00001031 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
schacko8f891382010-06-17 02:56:40 +00001032 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001033
1034 err = qlcnic_need_fw_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001035 if (err == 0)
Sony Chacko4e708122010-08-31 17:17:44 +00001036 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001037
Sony Chackod4066832010-08-19 05:08:31 +00001038 err = qlcnic_pinit_from_rom(adapter);
1039 if (err)
1040 goto err_out;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001041
1042 err = qlcnic_load_firmware(adapter);
1043 if (err)
1044 goto err_out;
1045
1046 qlcnic_release_firmware(adapter);
Sony Chackod4066832010-08-19 05:08:31 +00001047 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001048
Sony Chackod4066832010-08-19 05:08:31 +00001049check_fw_status:
1050 err = qlcnic_check_fw_status(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001051 if (err)
1052 goto err_out;
1053
1054 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001055 qlcnic_idc_debug_info(adapter, 1);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00001056
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001057 err = qlcnic_check_eswitch_mode(adapter);
1058 if (err) {
1059 dev_err(&adapter->pdev->dev,
1060 "Memory allocation failed for eswitch\n");
1061 goto err_out;
1062 }
Rajesh Borundia174240a2010-08-31 17:17:47 +00001063 err = qlcnic_set_mgmt_operations(adapter);
1064 if (err)
1065 goto err_out;
1066
1067 qlcnic_check_options(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001068 adapter->need_fw_reset = 0;
1069
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001070 qlcnic_release_firmware(adapter);
1071 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001072
1073err_out:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001074 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1075 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001076
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001077 qlcnic_release_firmware(adapter);
1078 return err;
1079}
1080
1081static int
1082qlcnic_request_irq(struct qlcnic_adapter *adapter)
1083{
1084 irq_handler_t handler;
1085 struct qlcnic_host_sds_ring *sds_ring;
1086 int err, ring;
1087
1088 unsigned long flags = 0;
1089 struct net_device *netdev = adapter->netdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001090 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001091
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001092 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1093 handler = qlcnic_tmp_intr;
1094 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1095 flags |= IRQF_SHARED;
1096
1097 } else {
1098 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1099 handler = qlcnic_msix_intr;
1100 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1101 handler = qlcnic_msi_intr;
1102 else {
1103 flags |= IRQF_SHARED;
1104 handler = qlcnic_intr;
1105 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001106 }
1107 adapter->irq = netdev->irq;
1108
1109 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1110 sds_ring = &recv_ctx->sds_rings[ring];
1111 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1112 err = request_irq(sds_ring->irq, handler,
1113 flags, sds_ring->name, sds_ring);
1114 if (err)
1115 return err;
1116 }
1117
1118 return 0;
1119}
1120
1121static void
1122qlcnic_free_irq(struct qlcnic_adapter *adapter)
1123{
1124 int ring;
1125 struct qlcnic_host_sds_ring *sds_ring;
1126
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001127 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001128
1129 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1130 sds_ring = &recv_ctx->sds_rings[ring];
1131 free_irq(sds_ring->irq, sds_ring);
1132 }
1133}
1134
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001135static int
1136__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1137{
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001138 int ring;
Rajesh Borundiacae82d42012-06-06 07:35:06 +00001139 u32 capab2;
1140
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001141 struct qlcnic_host_rds_ring *rds_ring;
1142
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001143 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1144 return -EIO;
1145
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001146 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1147 return 0;
Rajesh Borundia0325d692010-08-19 05:08:26 +00001148 if (qlcnic_set_eswitch_port_config(adapter))
1149 return -EIO;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001150
Rajesh Borundiacae82d42012-06-06 07:35:06 +00001151 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_MORE_CAPS) {
1152 capab2 = QLCRD32(adapter, CRB_FW_CAPABILITIES_2);
1153 if (capab2 & QLCNIC_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
1154 adapter->flags |= QLCNIC_FW_LRO_MSS_CAP;
1155 }
1156
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001157 if (qlcnic_fw_create_ctx(adapter))
1158 return -EIO;
1159
1160 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001161 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1162 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001163 }
1164
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001165 qlcnic_set_multi(netdev);
1166 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1167
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001168 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001169
1170 if (adapter->max_sds_rings > 1)
1171 qlcnic_config_rss(adapter, 1);
1172
1173 qlcnic_config_intr_coalesce(adapter);
1174
Sucheta Chakraborty24763d82010-08-17 00:34:25 +00001175 if (netdev->features & NETIF_F_LRO)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001176 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1177
1178 qlcnic_napi_enable(adapter);
1179
1180 qlcnic_linkevent_request(adapter, 1);
1181
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001182 adapter->reset_context = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001183 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1184 return 0;
1185}
1186
1187/* Usage: During resume and firmware recovery module.*/
1188
1189static int
1190qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1191{
1192 int err = 0;
1193
1194 rtnl_lock();
1195 if (netif_running(netdev))
1196 err = __qlcnic_up(adapter, netdev);
1197 rtnl_unlock();
1198
1199 return err;
1200}
1201
1202static void
1203__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1204{
1205 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1206 return;
1207
1208 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1209 return;
1210
1211 smp_mb();
1212 spin_lock(&adapter->tx_clean_lock);
1213 netif_carrier_off(netdev);
1214 netif_tx_disable(netdev);
1215
1216 qlcnic_free_mac_list(adapter);
1217
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001218 if (adapter->fhash.fnum)
1219 qlcnic_delete_lb_filters(adapter);
1220
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001221 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1222
1223 qlcnic_napi_disable(adapter);
1224
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001225 qlcnic_fw_destroy_ctx(adapter);
Rajesh Borundiacae82d42012-06-06 07:35:06 +00001226 adapter->flags &= ~QLCNIC_FW_LRO_MSS_CAP;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001227
1228 qlcnic_reset_rx_buffers_list(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001229 qlcnic_release_tx_buffers(adapter);
1230 spin_unlock(&adapter->tx_clean_lock);
1231}
1232
1233/* Usage: During suspend and firmware recovery module */
1234
1235static void
1236qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1237{
1238 rtnl_lock();
1239 if (netif_running(netdev))
1240 __qlcnic_down(adapter, netdev);
1241 rtnl_unlock();
1242
1243}
1244
1245static int
1246qlcnic_attach(struct qlcnic_adapter *adapter)
1247{
1248 struct net_device *netdev = adapter->netdev;
1249 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001250 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001251
1252 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1253 return 0;
1254
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001255 err = qlcnic_napi_add(adapter, netdev);
1256 if (err)
1257 return err;
1258
1259 err = qlcnic_alloc_sw_resources(adapter);
1260 if (err) {
1261 dev_err(&pdev->dev, "Error in setting sw resources\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001262 goto err_out_napi_del;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001263 }
1264
1265 err = qlcnic_alloc_hw_resources(adapter);
1266 if (err) {
1267 dev_err(&pdev->dev, "Error in setting hw resources\n");
1268 goto err_out_free_sw;
1269 }
1270
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001271 err = qlcnic_request_irq(adapter);
1272 if (err) {
1273 dev_err(&pdev->dev, "failed to setup interrupt\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001274 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001275 }
1276
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001277 qlcnic_create_sysfs_entries(adapter);
1278
1279 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1280 return 0;
1281
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001282err_out_free_hw:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001283 qlcnic_free_hw_resources(adapter);
1284err_out_free_sw:
1285 qlcnic_free_sw_resources(adapter);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001286err_out_napi_del:
1287 qlcnic_napi_del(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001288 return err;
1289}
1290
1291static void
1292qlcnic_detach(struct qlcnic_adapter *adapter)
1293{
1294 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1295 return;
1296
1297 qlcnic_remove_sysfs_entries(adapter);
1298
1299 qlcnic_free_hw_resources(adapter);
1300 qlcnic_release_rx_buffers(adapter);
1301 qlcnic_free_irq(adapter);
1302 qlcnic_napi_del(adapter);
1303 qlcnic_free_sw_resources(adapter);
1304
1305 adapter->is_up = 0;
1306}
1307
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001308void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1309{
1310 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1311 struct qlcnic_host_sds_ring *sds_ring;
1312 int ring;
1313
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001314 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001315 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1316 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001317 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001318 qlcnic_disable_int(sds_ring);
1319 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001320 }
1321
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001322 qlcnic_fw_destroy_ctx(adapter);
1323
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001324 qlcnic_detach(adapter);
1325
1326 adapter->diag_test = 0;
1327 adapter->max_sds_rings = max_sds_rings;
1328
1329 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001330 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001331
1332 if (netif_running(netdev))
1333 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001334out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001335 netif_device_attach(netdev);
1336}
1337
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001338static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1339{
1340 int err = 0;
1341 adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1342 GFP_KERNEL);
1343 if (!adapter->ahw) {
1344 dev_err(&adapter->pdev->dev,
1345 "Failed to allocate recv ctx resources for adapter\n");
1346 err = -ENOMEM;
1347 goto err_out;
1348 }
1349 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1350 GFP_KERNEL);
1351 if (!adapter->recv_ctx) {
1352 dev_err(&adapter->pdev->dev,
1353 "Failed to allocate recv ctx resources for adapter\n");
1354 kfree(adapter->ahw);
1355 adapter->ahw = NULL;
1356 err = -ENOMEM;
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001357 goto err_out;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001358 }
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001359 /* Initialize interrupt coalesce parameters */
1360 adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1361 adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1362 adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001363err_out:
1364 return err;
1365}
1366
1367static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1368{
1369 kfree(adapter->recv_ctx);
1370 adapter->recv_ctx = NULL;
1371
Anirban Chakraborty18f2f612011-05-12 12:48:33 +00001372 if (adapter->ahw->fw_dump.tmpl_hdr) {
1373 vfree(adapter->ahw->fw_dump.tmpl_hdr);
1374 adapter->ahw->fw_dump.tmpl_hdr = NULL;
1375 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001376 kfree(adapter->ahw);
1377 adapter->ahw = NULL;
1378}
1379
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001380int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1381{
1382 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1383 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001384 struct qlcnic_host_rds_ring *rds_ring;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001385 int ring;
1386 int ret;
1387
1388 netif_device_detach(netdev);
1389
1390 if (netif_running(netdev))
1391 __qlcnic_down(adapter, netdev);
1392
1393 qlcnic_detach(adapter);
1394
1395 adapter->max_sds_rings = 1;
1396 adapter->diag_test = test;
1397
1398 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001399 if (ret) {
1400 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001401 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001402 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001403
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001404 ret = qlcnic_fw_create_ctx(adapter);
1405 if (ret) {
1406 qlcnic_detach(adapter);
Sony Chacko57e46242010-07-24 18:32:18 +00001407 netif_device_attach(netdev);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001408 return ret;
1409 }
1410
1411 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001412 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1413 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001414 }
1415
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001416 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1417 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001418 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001419 qlcnic_enable_int(sds_ring);
1420 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001421 }
Sucheta Chakraborty22c8c932011-06-22 02:52:23 +00001422
1423 if (adapter->diag_test == QLCNIC_LOOPBACK_TEST) {
1424 adapter->ahw->loopback_state = 0;
1425 qlcnic_linkevent_request(adapter, 1);
1426 }
1427
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001428 set_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001429
1430 return 0;
1431}
1432
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001433/* Reset context in hardware only */
1434static int
1435qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1436{
1437 struct net_device *netdev = adapter->netdev;
1438
1439 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1440 return -EBUSY;
1441
1442 netif_device_detach(netdev);
1443
1444 qlcnic_down(adapter, netdev);
1445
1446 qlcnic_up(adapter, netdev);
1447
1448 netif_device_attach(netdev);
1449
1450 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1451 return 0;
1452}
1453
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001454int
1455qlcnic_reset_context(struct qlcnic_adapter *adapter)
1456{
1457 int err = 0;
1458 struct net_device *netdev = adapter->netdev;
1459
1460 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1461 return -EBUSY;
1462
1463 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1464
1465 netif_device_detach(netdev);
1466
1467 if (netif_running(netdev))
1468 __qlcnic_down(adapter, netdev);
1469
1470 qlcnic_detach(adapter);
1471
1472 if (netif_running(netdev)) {
1473 err = qlcnic_attach(adapter);
Anirban Chakraborty1d5c88e2012-03-23 06:32:34 +00001474 if (!err) {
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001475 __qlcnic_up(adapter, netdev);
Anirban Chakraborty1d5c88e2012-03-23 06:32:34 +00001476 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
1477 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001478 }
1479
1480 netif_device_attach(netdev);
1481 }
1482
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001483 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1484 return err;
1485}
1486
1487static int
1488qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001489 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001490{
1491 int err;
1492 struct pci_dev *pdev = adapter->pdev;
1493
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001494 adapter->mc_enabled = 0;
1495 adapter->max_mc_count = 38;
1496
1497 netdev->netdev_ops = &qlcnic_netdev_ops;
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001498 netdev->watchdog_timeo = 5*HZ;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001499
1500 qlcnic_change_mtu(netdev, netdev->mtu);
1501
1502 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1503
Michał Mirosław135d84a2011-04-19 03:03:57 +00001504 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1505 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
Anirban Chakrabortyac8d0c42010-07-09 13:14:58 +00001506
Michał Mirosław135d84a2011-04-19 03:03:57 +00001507 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1508 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1509 if (pci_using_dac)
1510 netdev->hw_features |= NETIF_F_HIGHDMA;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001511
Michał Mirosław135d84a2011-04-19 03:03:57 +00001512 netdev->vlan_features = netdev->hw_features;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001513
1514 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
Michał Mirosław135d84a2011-04-19 03:03:57 +00001515 netdev->hw_features |= NETIF_F_HW_VLAN_TX;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001516 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
Michał Mirosław135d84a2011-04-19 03:03:57 +00001517 netdev->hw_features |= NETIF_F_LRO;
1518
1519 netdev->features |= netdev->hw_features |
1520 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1521
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001522 netdev->irq = adapter->msix_entries[0].vector;
1523
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001524 err = register_netdev(netdev);
1525 if (err) {
1526 dev_err(&pdev->dev, "failed to register net device\n");
1527 return err;
1528 }
1529
1530 return 0;
1531}
1532
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001533static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1534{
1535 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1536 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1537 *pci_using_dac = 1;
1538 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1539 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1540 *pci_using_dac = 0;
1541 else {
1542 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1543 return -EIO;
1544 }
1545
1546 return 0;
1547}
1548
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001549static int
1550qlcnic_alloc_msix_entries(struct qlcnic_adapter *adapter, u16 count)
1551{
1552 adapter->msix_entries = kcalloc(count, sizeof(struct msix_entry),
1553 GFP_KERNEL);
1554
1555 if (adapter->msix_entries)
1556 return 0;
1557
1558 dev_err(&adapter->pdev->dev, "failed allocating msix_entries\n");
1559 return -ENOMEM;
1560}
1561
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001562static int __devinit
1563qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1564{
1565 struct net_device *netdev = NULL;
1566 struct qlcnic_adapter *adapter = NULL;
1567 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001568 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001569 uint8_t pci_using_dac;
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001570 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001571
1572 err = pci_enable_device(pdev);
1573 if (err)
1574 return err;
1575
1576 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1577 err = -ENODEV;
1578 goto err_out_disable_pdev;
1579 }
1580
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001581 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1582 if (err)
1583 goto err_out_disable_pdev;
1584
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001585 err = pci_request_regions(pdev, qlcnic_driver_name);
1586 if (err)
1587 goto err_out_disable_pdev;
1588
1589 pci_set_master(pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001590 pci_enable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001591
1592 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1593 if (!netdev) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001594 err = -ENOMEM;
1595 goto err_out_free_res;
1596 }
1597
1598 SET_NETDEV_DEV(netdev, &pdev->dev);
1599
1600 adapter = netdev_priv(netdev);
1601 adapter->netdev = netdev;
1602 adapter->pdev = pdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001603
Peter Senna Tschudin2dfc9672012-10-05 12:10:50 +00001604 err = qlcnic_alloc_adapter_resources(adapter);
1605 if (err)
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001606 goto err_out_free_netdev;
1607
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001608 adapter->dev_rst_time = jiffies;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001609 revision_id = pdev->revision;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001610 adapter->ahw->revision_id = revision_id;
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001611 adapter->mac_learn = qlcnic_mac_learn;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001612
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001613 rwlock_init(&adapter->ahw->crb_lock);
1614 mutex_init(&adapter->ahw->mem_lock);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001615
1616 spin_lock_init(&adapter->tx_clean_lock);
1617 INIT_LIST_HEAD(&adapter->mac_list);
1618
1619 err = qlcnic_setup_pci_map(adapter);
1620 if (err)
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001621 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001622
1623 /* This will be reset for mezz cards */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001624 adapter->portnum = adapter->ahw->pci_func;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001625
1626 err = qlcnic_get_board_info(adapter);
1627 if (err) {
1628 dev_err(&pdev->dev, "Error getting board config info.\n");
1629 goto err_out_iounmap;
1630 }
1631
Sony Chacko8cfdce02010-08-26 14:02:41 +00001632 err = qlcnic_setup_idc_param(adapter);
1633 if (err)
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +00001634 goto err_out_iounmap;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001635
Rajesh Borundia1dc0f3c2010-12-16 22:59:02 +00001636 adapter->flags |= QLCNIC_NEED_FLR;
Rajesh Borundiab0044bc2010-11-23 01:25:21 +00001637
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001638 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001639 if (err) {
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00001640 dev_err(&pdev->dev, "Loading fw failed. Please Reboot\n"
1641 "\t\tIf reboot doesn't help, try flashing the card\n");
1642 goto err_out_maintenance_mode;
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001643 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001644
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001645 if (qlcnic_read_mac_addr(adapter))
1646 dev_warn(&pdev->dev, "failed to read mac addr\n");
1647
1648 if (adapter->portnum == 0) {
1649 get_brd_name(adapter, brd_name);
1650
1651 pr_info("%s: %s Board Chip rev 0x%x\n",
1652 module_name(THIS_MODULE),
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001653 brd_name, adapter->ahw->revision_id);
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001654 }
1655
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001656 qlcnic_clear_stats(adapter);
1657
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001658 err = qlcnic_alloc_msix_entries(adapter, adapter->max_rx_ques);
1659 if (err)
1660 goto err_out_decr_ref;
1661
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001662 qlcnic_setup_intr(adapter);
1663
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001664 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001665 if (err)
1666 goto err_out_disable_msi;
1667
1668 pci_set_drvdata(pdev, adapter);
1669
1670 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1671
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001672 switch (adapter->ahw->port_type) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001673 case QLCNIC_GBE:
1674 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1675 adapter->netdev->name);
1676 break;
1677 case QLCNIC_XGBE:
1678 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1679 adapter->netdev->name);
1680 break;
1681 }
1682
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001683 if (adapter->mac_learn)
1684 qlcnic_alloc_lb_filters_mem(adapter);
1685
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001686 qlcnic_create_diag_entries(adapter);
1687
1688 return 0;
1689
1690err_out_disable_msi:
1691 qlcnic_teardown_intr(adapter);
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001692 kfree(adapter->msix_entries);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001693
1694err_out_decr_ref:
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001695 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001696
1697err_out_iounmap:
1698 qlcnic_cleanup_pci_map(adapter);
1699
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001700err_out_free_hw:
1701 qlcnic_free_adapter_resources(adapter);
1702
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001703err_out_free_netdev:
1704 free_netdev(netdev);
1705
1706err_out_free_res:
1707 pci_release_regions(pdev);
1708
1709err_out_disable_pdev:
1710 pci_set_drvdata(pdev, NULL);
1711 pci_disable_device(pdev);
1712 return err;
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00001713
1714err_out_maintenance_mode:
1715 netdev->netdev_ops = &qlcnic_netdev_failed_ops;
1716 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_failed_ops);
1717 err = register_netdev(netdev);
1718 if (err) {
1719 dev_err(&pdev->dev, "failed to register net device\n");
1720 goto err_out_decr_ref;
1721 }
1722 pci_set_drvdata(pdev, adapter);
1723 qlcnic_create_diag_entries(adapter);
1724 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001725}
1726
1727static void __devexit qlcnic_remove(struct pci_dev *pdev)
1728{
1729 struct qlcnic_adapter *adapter;
1730 struct net_device *netdev;
1731
1732 adapter = pci_get_drvdata(pdev);
1733 if (adapter == NULL)
1734 return;
1735
1736 netdev = adapter->netdev;
1737
1738 qlcnic_cancel_fw_work(adapter);
1739
1740 unregister_netdev(netdev);
1741
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001742 qlcnic_detach(adapter);
1743
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001744 if (adapter->npars != NULL)
1745 kfree(adapter->npars);
1746 if (adapter->eswitch != NULL)
1747 kfree(adapter->eswitch);
1748
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001749 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001750
1751 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1752
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001753 qlcnic_free_lb_filters_mem(adapter);
1754
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001755 qlcnic_teardown_intr(adapter);
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001756 kfree(adapter->msix_entries);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001757
1758 qlcnic_remove_diag_entries(adapter);
1759
1760 qlcnic_cleanup_pci_map(adapter);
1761
1762 qlcnic_release_firmware(adapter);
1763
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001764 pci_disable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001765 pci_release_regions(pdev);
1766 pci_disable_device(pdev);
1767 pci_set_drvdata(pdev, NULL);
1768
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001769 qlcnic_free_adapter_resources(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001770 free_netdev(netdev);
1771}
1772static int __qlcnic_shutdown(struct pci_dev *pdev)
1773{
1774 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1775 struct net_device *netdev = adapter->netdev;
1776 int retval;
1777
1778 netif_device_detach(netdev);
1779
1780 qlcnic_cancel_fw_work(adapter);
1781
1782 if (netif_running(netdev))
1783 qlcnic_down(adapter, netdev);
1784
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001785 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001786
1787 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1788
1789 retval = pci_save_state(pdev);
1790 if (retval)
1791 return retval;
1792
1793 if (qlcnic_wol_supported(adapter)) {
1794 pci_enable_wake(pdev, PCI_D3cold, 1);
1795 pci_enable_wake(pdev, PCI_D3hot, 1);
1796 }
1797
1798 return 0;
1799}
1800
1801static void qlcnic_shutdown(struct pci_dev *pdev)
1802{
1803 if (__qlcnic_shutdown(pdev))
1804 return;
1805
1806 pci_disable_device(pdev);
1807}
1808
1809#ifdef CONFIG_PM
1810static int
1811qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1812{
1813 int retval;
1814
1815 retval = __qlcnic_shutdown(pdev);
1816 if (retval)
1817 return retval;
1818
1819 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1820 return 0;
1821}
1822
1823static int
1824qlcnic_resume(struct pci_dev *pdev)
1825{
1826 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1827 struct net_device *netdev = adapter->netdev;
1828 int err;
1829
1830 err = pci_enable_device(pdev);
1831 if (err)
1832 return err;
1833
1834 pci_set_power_state(pdev, PCI_D0);
1835 pci_set_master(pdev);
1836 pci_restore_state(pdev);
1837
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001838 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001839 if (err) {
1840 dev_err(&pdev->dev, "failed to start firmware\n");
1841 return err;
1842 }
1843
1844 if (netif_running(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001845 err = qlcnic_up(adapter, netdev);
1846 if (err)
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001847 goto done;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001848
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00001849 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001850 }
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001851done:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001852 netif_device_attach(netdev);
1853 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1854 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001855}
1856#endif
1857
1858static int qlcnic_open(struct net_device *netdev)
1859{
1860 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00001861 u32 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001862 int err;
1863
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00001864 if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD)) {
1865 netdev_err(netdev, "Device in FAILED state\n");
1866 return -EIO;
1867 }
1868
Amit Kumar Salechac55ad8e2011-06-22 02:52:21 +00001869 netif_carrier_off(netdev);
1870
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001871 err = qlcnic_attach(adapter);
1872 if (err)
1873 return err;
1874
1875 err = __qlcnic_up(adapter, netdev);
1876 if (err)
1877 goto err_out;
1878
1879 netif_start_queue(netdev);
1880
1881 return 0;
1882
1883err_out:
1884 qlcnic_detach(adapter);
1885 return err;
1886}
1887
1888/*
1889 * qlcnic_close - Disables a network interface entry point
1890 */
1891static int qlcnic_close(struct net_device *netdev)
1892{
1893 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1894
1895 __qlcnic_down(adapter, netdev);
1896 return 0;
1897}
1898
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001899void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001900{
1901 void *head;
1902 int i;
1903
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001904 if (adapter->fhash.fmax && adapter->fhash.fhead)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001905 return;
1906
1907 spin_lock_init(&adapter->mac_learn_lock);
1908
1909 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1910 GFP_KERNEL);
1911 if (!head)
1912 return;
1913
1914 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
Joe Perches43d620c2011-06-16 19:08:06 +00001915 adapter->fhash.fhead = head;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001916
1917 for (i = 0; i < adapter->fhash.fmax; i++)
1918 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1919}
1920
1921static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1922{
1923 if (adapter->fhash.fmax && adapter->fhash.fhead)
1924 kfree(adapter->fhash.fhead);
1925
1926 adapter->fhash.fhead = NULL;
1927 adapter->fhash.fmax = 0;
1928}
1929
1930static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001931 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001932{
1933 struct cmd_desc_type0 *hwdesc;
1934 struct qlcnic_nic_req *req;
1935 struct qlcnic_mac_req *mac_req;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001936 struct qlcnic_vlan_req *vlan_req;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001937 u32 producer;
1938 u64 word;
1939
1940 producer = tx_ring->producer;
1941 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1942
1943 req = (struct qlcnic_nic_req *)hwdesc;
1944 memset(req, 0, sizeof(struct qlcnic_nic_req));
1945 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1946
1947 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1948 req->req_hdr = cpu_to_le64(word);
1949
1950 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001951 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001952 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1953
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001954 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1955 vlan_req->vlan_id = vlan_id;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001956
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001957 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001958 smp_mb();
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001959}
1960
1961#define QLCNIC_MAC_HASH(MAC)\
1962 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1963
1964static void
1965qlcnic_send_filter(struct qlcnic_adapter *adapter,
1966 struct qlcnic_host_tx_ring *tx_ring,
1967 struct cmd_desc_type0 *first_desc,
1968 struct sk_buff *skb)
1969{
1970 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1971 struct qlcnic_filter *fil, *tmp_fil;
1972 struct hlist_node *tmp_hnode, *n;
1973 struct hlist_head *head;
1974 u64 src_addr = 0;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001975 __le16 vlan_id = 0;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001976 u8 hindex;
1977
Joe Perches2e42e472012-05-09 17:17:46 +00001978 if (ether_addr_equal(phdr->h_source, adapter->mac_addr))
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001979 return;
1980
1981 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1982 return;
1983
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001984 /* Only NPAR capable devices support vlan based learning*/
1985 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1986 vlan_id = first_desc->vlan_TCI;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001987 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1988 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1989 head = &(adapter->fhash.fhead[hindex]);
1990
1991 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001992 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1993 tmp_fil->vlan_id == vlan_id) {
amit salechae5edb7b2010-10-26 17:53:07 +00001994
1995 if (jiffies >
1996 (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1997 qlcnic_change_filter(adapter, src_addr, vlan_id,
1998 tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001999 tmp_fil->ftime = jiffies;
2000 return;
2001 }
2002 }
2003
2004 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
2005 if (!fil)
2006 return;
2007
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00002008 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002009
2010 fil->ftime = jiffies;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00002011 fil->vlan_id = vlan_id;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002012 memcpy(fil->faddr, &src_addr, ETH_ALEN);
2013 spin_lock(&adapter->mac_learn_lock);
2014 hlist_add_head(&(fil->fnode), head);
2015 adapter->fhash.fnum++;
2016 spin_unlock(&adapter->mac_learn_lock);
2017}
2018
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002019static int
2020qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002021 struct cmd_desc_type0 *first_desc,
2022 struct sk_buff *skb)
2023{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002024 u8 opcode = 0, hdr_len = 0;
2025 u16 flags = 0, vlan_tci = 0;
2026 int copied, offset, copy_len;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002027 struct cmd_desc_type0 *hwdesc;
2028 struct vlan_ethhdr *vh;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002029 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2030 u16 protocol = ntohs(skb->protocol);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00002031 u32 producer = tx_ring->producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002032
2033 if (protocol == ETH_P_8021Q) {
2034 vh = (struct vlan_ethhdr *)skb->data;
2035 flags = FLAGS_VLAN_TAGGED;
2036 vlan_tci = vh->h_vlan_TCI;
Rajesh Borundia0bb79562012-06-06 07:35:08 +00002037 protocol = ntohs(vh->h_vlan_encapsulated_proto);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002038 } else if (vlan_tx_tag_present(skb)) {
2039 flags = FLAGS_VLAN_OOB;
2040 vlan_tci = vlan_tx_tag_get(skb);
2041 }
2042 if (unlikely(adapter->pvid)) {
2043 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
2044 return -EIO;
2045 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
2046 goto set_flags;
2047
2048 flags = FLAGS_VLAN_OOB;
2049 vlan_tci = adapter->pvid;
2050 }
2051set_flags:
2052 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
2053 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002054
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00002055 if (*(skb->data) & BIT_0) {
2056 flags |= BIT_0;
2057 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
2058 }
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002059 opcode = TX_ETHER_PKT;
2060 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002061 skb_shinfo(skb)->gso_size > 0) {
2062
2063 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2064
2065 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2066 first_desc->total_hdr_length = hdr_len;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002067
2068 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
2069
2070 /* For LSO, we need to copy the MAC/IP/TCP headers into
2071 * the descriptor ring */
2072 copied = 0;
2073 offset = 2;
2074
2075 if (flags & FLAGS_VLAN_OOB) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002076 first_desc->total_hdr_length += VLAN_HLEN;
2077 first_desc->tcp_hdr_offset = VLAN_HLEN;
2078 first_desc->ip_hdr_offset = VLAN_HLEN;
2079 /* Only in case of TSO on vlan device */
2080 flags |= FLAGS_VLAN_TAGGED;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002081
2082 /* Create a TSO vlan header template for firmware */
2083
2084 hwdesc = &tx_ring->desc_head[producer];
2085 tx_ring->cmd_buf_arr[producer].skb = NULL;
2086
2087 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2088 offset, hdr_len + VLAN_HLEN);
2089
2090 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
2091 skb_copy_from_linear_data(skb, vh, 12);
2092 vh->h_vlan_proto = htons(ETH_P_8021Q);
2093 vh->h_vlan_TCI = htons(vlan_tci);
2094
2095 skb_copy_from_linear_data_offset(skb, 12,
2096 (char *)vh + 16, copy_len - 16);
2097
2098 copied = copy_len - VLAN_HLEN;
2099 offset = 0;
2100
2101 producer = get_next_index(producer, tx_ring->num_desc);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002102 }
2103
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002104 while (copied < hdr_len) {
2105
2106 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2107 offset, (hdr_len - copied));
2108
2109 hwdesc = &tx_ring->desc_head[producer];
2110 tx_ring->cmd_buf_arr[producer].skb = NULL;
2111
2112 skb_copy_from_linear_data_offset(skb, copied,
2113 (char *) hwdesc + offset, copy_len);
2114
2115 copied += copy_len;
2116 offset = 0;
2117
2118 producer = get_next_index(producer, tx_ring->num_desc);
2119 }
2120
2121 tx_ring->producer = producer;
2122 smp_mb();
2123 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002124
2125 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2126 u8 l4proto;
2127
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002128 if (protocol == ETH_P_IP) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002129 l4proto = ip_hdr(skb)->protocol;
2130
2131 if (l4proto == IPPROTO_TCP)
2132 opcode = TX_TCP_PKT;
2133 else if (l4proto == IPPROTO_UDP)
2134 opcode = TX_UDP_PKT;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002135 } else if (protocol == ETH_P_IPV6) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002136 l4proto = ipv6_hdr(skb)->nexthdr;
2137
2138 if (l4proto == IPPROTO_TCP)
2139 opcode = TX_TCPV6_PKT;
2140 else if (l4proto == IPPROTO_UDP)
2141 opcode = TX_UDPV6_PKT;
2142 }
2143 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002144 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2145 first_desc->ip_hdr_offset += skb_network_offset(skb);
2146 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2147
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002148 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002149}
2150
2151static int
2152qlcnic_map_tx_skb(struct pci_dev *pdev,
2153 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2154{
2155 struct qlcnic_skb_frag *nf;
2156 struct skb_frag_struct *frag;
2157 int i, nr_frags;
2158 dma_addr_t map;
2159
2160 nr_frags = skb_shinfo(skb)->nr_frags;
2161 nf = &pbuf->frag_array[0];
2162
2163 map = pci_map_single(pdev, skb->data,
2164 skb_headlen(skb), PCI_DMA_TODEVICE);
2165 if (pci_dma_mapping_error(pdev, map))
2166 goto out_err;
2167
2168 nf->dma = map;
2169 nf->length = skb_headlen(skb);
2170
2171 for (i = 0; i < nr_frags; i++) {
2172 frag = &skb_shinfo(skb)->frags[i];
2173 nf = &pbuf->frag_array[i+1];
2174
Eric Dumazet9e903e02011-10-18 21:00:24 +00002175 map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01002176 DMA_TO_DEVICE);
2177 if (dma_mapping_error(&pdev->dev, map))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002178 goto unwind;
2179
2180 nf->dma = map;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002181 nf->length = skb_frag_size(frag);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002182 }
2183
2184 return 0;
2185
2186unwind:
2187 while (--i >= 0) {
2188 nf = &pbuf->frag_array[i+1];
2189 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2190 }
2191
2192 nf = &pbuf->frag_array[0];
2193 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2194
2195out_err:
2196 return -ENOMEM;
2197}
2198
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002199static void
2200qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2201 struct qlcnic_cmd_buffer *pbuf)
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002202{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002203 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2204 int nr_frags = skb_shinfo(skb)->nr_frags;
2205 int i;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002206
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002207 for (i = 0; i < nr_frags; i++) {
2208 nf = &pbuf->frag_array[i+1];
2209 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002210 }
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002211
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002212 nf = &pbuf->frag_array[0];
2213 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
Sucheta Chakraborty5b446c62011-06-03 05:52:18 +00002214 pbuf->skb = NULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002215}
2216
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002217static inline void
2218qlcnic_clear_cmddesc(u64 *desc)
2219{
2220 desc[0] = 0ULL;
2221 desc[2] = 0ULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002222 desc[7] = 0ULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002223}
2224
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00002225netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002226qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2227{
2228 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2229 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2230 struct qlcnic_cmd_buffer *pbuf;
2231 struct qlcnic_skb_frag *buffrag;
2232 struct cmd_desc_type0 *hwdesc, *first_desc;
2233 struct pci_dev *pdev;
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002234 struct ethhdr *phdr;
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002235 int delta = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002236 int i, k;
2237
2238 u32 producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002239 int frag_count;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002240 u32 num_txd = tx_ring->num_desc;
2241
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00002242 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2243 netif_stop_queue(netdev);
2244 return NETDEV_TX_BUSY;
2245 }
2246
Sony Chackofe4d4342010-08-19 05:08:27 +00002247 if (adapter->flags & QLCNIC_MACSPOOF) {
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002248 phdr = (struct ethhdr *)skb->data;
Joe Perches2e42e472012-05-09 17:17:46 +00002249 if (!ether_addr_equal(phdr->h_source, adapter->mac_addr))
Sony Chackofe4d4342010-08-19 05:08:27 +00002250 goto drop_packet;
2251 }
2252
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002253 frag_count = skb_shinfo(skb)->nr_frags + 1;
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002254 /* 14 frags supported for normal packet and
2255 * 32 frags supported for TSO packet
2256 */
2257 if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
2258
2259 for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
Eric Dumazet9e903e02011-10-18 21:00:24 +00002260 delta += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002261
2262 if (!__pskb_pull_tail(skb, delta))
2263 goto drop_packet;
2264
2265 frag_count = 1 + skb_shinfo(skb)->nr_frags;
2266 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002267
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002268 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002269 netif_stop_queue(netdev);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002270 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2271 netif_start_queue(netdev);
2272 else {
2273 adapter->stats.xmit_off++;
2274 return NETDEV_TX_BUSY;
2275 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002276 }
2277
2278 producer = tx_ring->producer;
2279 pbuf = &tx_ring->cmd_buf_arr[producer];
2280
2281 pdev = adapter->pdev;
2282
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002283 first_desc = hwdesc = &tx_ring->desc_head[producer];
2284 qlcnic_clear_cmddesc((u64 *)hwdesc);
2285
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002286 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2287 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002288 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002289 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002290
2291 pbuf->skb = skb;
2292 pbuf->frag_count = frag_count;
2293
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002294 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2295 qlcnic_set_tx_port(first_desc, adapter->portnum);
2296
2297 for (i = 0; i < frag_count; i++) {
2298
2299 k = i % 4;
2300
2301 if ((k == 0) && (i > 0)) {
2302 /* move to next desc.*/
2303 producer = get_next_index(producer, num_txd);
2304 hwdesc = &tx_ring->desc_head[producer];
2305 qlcnic_clear_cmddesc((u64 *)hwdesc);
2306 tx_ring->cmd_buf_arr[producer].skb = NULL;
2307 }
2308
2309 buffrag = &pbuf->frag_array[i];
2310
2311 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2312 switch (k) {
2313 case 0:
2314 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2315 break;
2316 case 1:
2317 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2318 break;
2319 case 2:
2320 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2321 break;
2322 case 3:
2323 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2324 break;
2325 }
2326 }
2327
2328 tx_ring->producer = get_next_index(producer, num_txd);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002329 smp_mb();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002330
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002331 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2332 goto unwind_buff;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002333
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00002334 if (adapter->mac_learn)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002335 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2336
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002337 adapter->stats.txbytes += skb->len;
2338 adapter->stats.xmitcalled++;
2339
Sucheta Chakrabortyf127f472011-07-14 03:16:53 +00002340 qlcnic_update_cmd_producer(adapter, tx_ring);
2341
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002342 return NETDEV_TX_OK;
2343
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002344unwind_buff:
2345 qlcnic_unmap_buffers(pdev, skb, pbuf);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002346drop_packet:
2347 adapter->stats.txdropped++;
2348 dev_kfree_skb_any(skb);
2349 return NETDEV_TX_OK;
2350}
2351
2352static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2353{
2354 struct net_device *netdev = adapter->netdev;
2355 u32 temp, temp_state, temp_val;
2356 int rv = 0;
2357
2358 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2359
2360 temp_state = qlcnic_get_temp_state(temp);
2361 temp_val = qlcnic_get_temp_val(temp);
2362
2363 if (temp_state == QLCNIC_TEMP_PANIC) {
2364 dev_err(&netdev->dev,
2365 "Device temperature %d degrees C exceeds"
2366 " maximum allowed. Hardware has been shut down.\n",
2367 temp_val);
2368 rv = 1;
2369 } else if (temp_state == QLCNIC_TEMP_WARN) {
2370 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2371 dev_err(&netdev->dev,
2372 "Device temperature %d degrees C "
2373 "exceeds operating range."
2374 " Immediate action needed.\n",
2375 temp_val);
2376 }
2377 } else {
2378 if (adapter->temp == QLCNIC_TEMP_WARN) {
2379 dev_info(&netdev->dev,
2380 "Device temperature is now %d degrees C"
2381 " in normal range.\n", temp_val);
2382 }
2383 }
2384 adapter->temp = temp_state;
2385 return rv;
2386}
2387
2388void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2389{
2390 struct net_device *netdev = adapter->netdev;
2391
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002392 if (adapter->ahw->linkup && !linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002393 netdev_info(netdev, "NIC Link is down\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002394 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002395 if (netif_running(netdev)) {
2396 netif_carrier_off(netdev);
2397 netif_stop_queue(netdev);
2398 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002399 } else if (!adapter->ahw->linkup && linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002400 netdev_info(netdev, "NIC Link is up\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002401 adapter->ahw->linkup = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002402 if (netif_running(netdev)) {
2403 netif_carrier_on(netdev);
2404 netif_wake_queue(netdev);
2405 }
2406 }
2407}
2408
2409static void qlcnic_tx_timeout(struct net_device *netdev)
2410{
2411 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2412
2413 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2414 return;
2415
2416 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002417
2418 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002419 adapter->need_fw_reset = 1;
2420 else
2421 adapter->reset_context = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002422}
2423
2424static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2425{
2426 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2427 struct net_device_stats *stats = &netdev->stats;
2428
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002429 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2430 stats->tx_packets = adapter->stats.xmitfinished;
Sucheta Chakraborty7e382592010-05-17 01:22:10 +00002431 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002432 stats->tx_bytes = adapter->stats.txbytes;
2433 stats->rx_dropped = adapter->stats.rxdropped;
2434 stats->tx_dropped = adapter->stats.txdropped;
2435
2436 return stats;
2437}
2438
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002439static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002440{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002441 u32 status;
2442
2443 status = readl(adapter->isr_int_vec);
2444
2445 if (!(status & adapter->int_vec_bit))
2446 return IRQ_NONE;
2447
2448 /* check interrupt state machine, to be sure */
2449 status = readl(adapter->crb_int_state_reg);
2450 if (!ISR_LEGACY_INT_TRIGGERED(status))
2451 return IRQ_NONE;
2452
2453 writel(0xffffffff, adapter->tgt_status_reg);
2454 /* read twice to ensure write is flushed */
2455 readl(adapter->isr_int_vec);
2456 readl(adapter->isr_int_vec);
2457
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002458 return IRQ_HANDLED;
2459}
2460
2461static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2462{
2463 struct qlcnic_host_sds_ring *sds_ring = data;
2464 struct qlcnic_adapter *adapter = sds_ring->adapter;
2465
2466 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2467 goto done;
2468 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2469 writel(0xffffffff, adapter->tgt_status_reg);
2470 goto done;
2471 }
2472
2473 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2474 return IRQ_NONE;
2475
2476done:
2477 adapter->diag_cnt++;
2478 qlcnic_enable_int(sds_ring);
2479 return IRQ_HANDLED;
2480}
2481
2482static irqreturn_t qlcnic_intr(int irq, void *data)
2483{
2484 struct qlcnic_host_sds_ring *sds_ring = data;
2485 struct qlcnic_adapter *adapter = sds_ring->adapter;
2486
2487 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2488 return IRQ_NONE;
2489
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002490 napi_schedule(&sds_ring->napi);
2491
2492 return IRQ_HANDLED;
2493}
2494
2495static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2496{
2497 struct qlcnic_host_sds_ring *sds_ring = data;
2498 struct qlcnic_adapter *adapter = sds_ring->adapter;
2499
2500 /* clear interrupt */
2501 writel(0xffffffff, adapter->tgt_status_reg);
2502
2503 napi_schedule(&sds_ring->napi);
2504 return IRQ_HANDLED;
2505}
2506
2507static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2508{
2509 struct qlcnic_host_sds_ring *sds_ring = data;
2510
2511 napi_schedule(&sds_ring->napi);
2512 return IRQ_HANDLED;
2513}
2514
2515static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2516{
2517 u32 sw_consumer, hw_consumer;
2518 int count = 0, i;
2519 struct qlcnic_cmd_buffer *buffer;
2520 struct pci_dev *pdev = adapter->pdev;
2521 struct net_device *netdev = adapter->netdev;
2522 struct qlcnic_skb_frag *frag;
2523 int done;
2524 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2525
2526 if (!spin_trylock(&adapter->tx_clean_lock))
2527 return 1;
2528
2529 sw_consumer = tx_ring->sw_consumer;
2530 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2531
2532 while (sw_consumer != hw_consumer) {
2533 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2534 if (buffer->skb) {
2535 frag = &buffer->frag_array[0];
2536 pci_unmap_single(pdev, frag->dma, frag->length,
2537 PCI_DMA_TODEVICE);
2538 frag->dma = 0ULL;
2539 for (i = 1; i < buffer->frag_count; i++) {
2540 frag++;
2541 pci_unmap_page(pdev, frag->dma, frag->length,
2542 PCI_DMA_TODEVICE);
2543 frag->dma = 0ULL;
2544 }
2545
2546 adapter->stats.xmitfinished++;
2547 dev_kfree_skb_any(buffer->skb);
2548 buffer->skb = NULL;
2549 }
2550
2551 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2552 if (++count >= MAX_STATUS_HANDLE)
2553 break;
2554 }
2555
2556 if (count && netif_running(netdev)) {
2557 tx_ring->sw_consumer = sw_consumer;
2558
2559 smp_mb();
2560
2561 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002562 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2563 netif_wake_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00002564 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002565 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002566 }
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002567 adapter->tx_timeo_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002568 }
2569 /*
2570 * If everything is freed up to consumer then check if the ring is full
2571 * If the ring is full then check if more needs to be freed and
2572 * schedule the call back again.
2573 *
2574 * This happens when there are 2 CPUs. One could be freeing and the
2575 * other filling it. If the ring is full when we get out of here and
2576 * the card has already interrupted the host then the host can miss the
2577 * interrupt.
2578 *
2579 * There is still a possible race condition and the host could miss an
2580 * interrupt. The card has to take care of this.
2581 */
2582 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2583 done = (sw_consumer == hw_consumer);
2584 spin_unlock(&adapter->tx_clean_lock);
2585
2586 return done;
2587}
2588
2589static int qlcnic_poll(struct napi_struct *napi, int budget)
2590{
2591 struct qlcnic_host_sds_ring *sds_ring =
2592 container_of(napi, struct qlcnic_host_sds_ring, napi);
2593
2594 struct qlcnic_adapter *adapter = sds_ring->adapter;
2595
2596 int tx_complete;
2597 int work_done;
2598
2599 tx_complete = qlcnic_process_cmd_ring(adapter);
2600
2601 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2602
2603 if ((work_done < budget) && tx_complete) {
2604 napi_complete(&sds_ring->napi);
2605 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2606 qlcnic_enable_int(sds_ring);
2607 }
2608
2609 return work_done;
2610}
2611
schacko8f891382010-06-17 02:56:40 +00002612static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2613{
2614 struct qlcnic_host_sds_ring *sds_ring =
2615 container_of(napi, struct qlcnic_host_sds_ring, napi);
2616
2617 struct qlcnic_adapter *adapter = sds_ring->adapter;
2618 int work_done;
2619
2620 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2621
2622 if (work_done < budget) {
2623 napi_complete(&sds_ring->napi);
2624 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2625 qlcnic_enable_int(sds_ring);
2626 }
2627
2628 return work_done;
2629}
2630
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002631#ifdef CONFIG_NET_POLL_CONTROLLER
2632static void qlcnic_poll_controller(struct net_device *netdev)
2633{
Yinglin Luanbf827912010-08-22 21:57:56 +00002634 int ring;
2635 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002636 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002637 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Yinglin Luanbf827912010-08-22 21:57:56 +00002638
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002639 disable_irq(adapter->irq);
Yinglin Luanbf827912010-08-22 21:57:56 +00002640 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2641 sds_ring = &recv_ctx->sds_rings[ring];
2642 qlcnic_intr(adapter->irq, sds_ring);
2643 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002644 enable_irq(adapter->irq);
2645}
2646#endif
2647
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002648static void
2649qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2650{
2651 u32 val;
2652
2653 val = adapter->portnum & 0xf;
2654 val |= encoding << 7;
2655 val |= (jiffies - adapter->dev_rst_time) << 8;
2656
2657 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2658 adapter->dev_rst_time = jiffies;
2659}
2660
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002661static int
2662qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002663{
2664 u32 val;
2665
2666 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2667 state != QLCNIC_DEV_NEED_QUISCENT);
2668
2669 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002670 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002671
2672 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2673
2674 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002675 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002676 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002677 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002678
2679 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2680
2681 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002682
2683 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002684}
2685
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002686static int
2687qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2688{
2689 u32 val;
2690
2691 if (qlcnic_api_lock(adapter))
2692 return -EBUSY;
2693
2694 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002695 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002696 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2697
2698 qlcnic_api_unlock(adapter);
2699
2700 return 0;
2701}
2702
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002703static void
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002704qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002705{
2706 u32 val;
2707
2708 if (qlcnic_api_lock(adapter))
2709 goto err;
2710
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002711 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002712 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002713 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002714
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002715 if (failed) {
2716 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2717 dev_info(&adapter->pdev->dev,
2718 "Device state set to Failed. Please Reboot\n");
2719 } else if (!(val & 0x11111111))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002720 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2721
2722 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002723 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002724 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2725
2726 qlcnic_api_unlock(adapter);
2727err:
2728 adapter->fw_fail_cnt = 0;
Sritej Velaga032a13c2011-07-29 13:30:27 +00002729 adapter->flags &= ~QLCNIC_FW_HANG;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002730 clear_bit(__QLCNIC_START_FW, &adapter->state);
2731 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2732}
2733
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002734/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002735static int
2736qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2737{
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002738 int act, state, active_mask;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002739
2740 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002741 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002742
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002743 if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
2744 active_mask = (~(1 << (adapter->ahw->pci_func * 4)));
2745 act = act & active_mask;
2746 }
2747
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002748 if (((state & 0x11111111) == (act & 0x11111111)) ||
2749 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2750 return 0;
2751 else
2752 return 1;
2753}
2754
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002755static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2756{
2757 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2758
2759 if (val != QLCNIC_DRV_IDC_VER) {
2760 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2761 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2762 }
2763
2764 return 0;
2765}
2766
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002767static int
2768qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2769{
2770 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002771 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002772 u8 portnum = adapter->portnum;
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002773 u8 ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002774
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002775 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2776 return 1;
2777
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002778 if (qlcnic_api_lock(adapter))
2779 return -1;
2780
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002781 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002782 if (!(val & (1 << (portnum * 4)))) {
2783 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002784 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002785 }
2786
2787 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002788 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002789
2790 switch (prev_state) {
2791 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002792 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002793 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002794 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002795 qlcnic_api_unlock(adapter);
2796 return 1;
2797
2798 case QLCNIC_DEV_READY:
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002799 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002800 qlcnic_api_unlock(adapter);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002801 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002802
2803 case QLCNIC_DEV_NEED_RESET:
2804 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002805 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002806 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2807 break;
2808
2809 case QLCNIC_DEV_NEED_QUISCENT:
2810 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002811 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002812 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2813 break;
2814
2815 case QLCNIC_DEV_FAILED:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00002816 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002817 qlcnic_api_unlock(adapter);
2818 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002819
2820 case QLCNIC_DEV_INITIALIZING:
2821 case QLCNIC_DEV_QUISCENT:
2822 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002823 }
2824
2825 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002826
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002827 do {
2828 msleep(1000);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002829 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2830
2831 if (prev_state == QLCNIC_DEV_QUISCENT)
2832 continue;
2833 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002834
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002835 if (!dev_init_timeo) {
2836 dev_err(&adapter->pdev->dev,
2837 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002838 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002839 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002840
2841 if (qlcnic_api_lock(adapter))
2842 return -1;
2843
2844 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002845 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002846 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2847
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002848 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002849 qlcnic_api_unlock(adapter);
2850
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002851 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002852}
2853
2854static void
2855qlcnic_fwinit_work(struct work_struct *work)
2856{
2857 struct qlcnic_adapter *adapter = container_of(work,
2858 struct qlcnic_adapter, fw_work.work);
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002859 u32 dev_state = 0xf;
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002860 u32 val;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002861
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002862 if (qlcnic_api_lock(adapter))
2863 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002864
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002865 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002866 if (dev_state == QLCNIC_DEV_QUISCENT ||
2867 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002868 qlcnic_api_unlock(adapter);
2869 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2870 FW_POLL_DELAY * 2);
2871 return;
2872 }
2873
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002874 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002875 qlcnic_api_unlock(adapter);
2876 goto wait_npar;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002877 }
2878
Sritej Velaga16e3cf72011-10-28 12:57:11 +00002879 if (dev_state == QLCNIC_DEV_INITIALIZING ||
2880 dev_state == QLCNIC_DEV_READY) {
2881 dev_info(&adapter->pdev->dev, "Detected state change from "
2882 "DEV_NEED_RESET, skipping ack check\n");
2883 goto skip_ack_check;
2884 }
2885
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002886 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
Sritej Velaga16e3cf72011-10-28 12:57:11 +00002887 dev_info(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002888 adapter->reset_ack_timeo);
2889 goto skip_ack_check;
2890 }
2891
2892 if (!qlcnic_check_drv_state(adapter)) {
2893skip_ack_check:
2894 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002895
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002896 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2897 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2898 QLCNIC_DEV_INITIALIZING);
2899 set_bit(__QLCNIC_START_FW, &adapter->state);
2900 QLCDB(adapter, DRV, "Restarting fw\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002901 qlcnic_idc_debug_info(adapter, 0);
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002902 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2903 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2904 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002905 }
2906
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002907 qlcnic_api_unlock(adapter);
2908
Anirban Chakraborty287e38a2011-06-22 02:52:18 +00002909 rtnl_lock();
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002910 if (adapter->ahw->fw_dump.enable &&
2911 (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
Anirban Chakraborty9d6a6442011-06-22 02:52:22 +00002912 QLCDB(adapter, DRV, "Take FW dump\n");
2913 qlcnic_dump_fw(adapter);
Sritej Velaga032a13c2011-07-29 13:30:27 +00002914 adapter->flags |= QLCNIC_FW_HANG;
Anirban Chakraborty9d6a6442011-06-22 02:52:22 +00002915 }
Anirban Chakraborty287e38a2011-06-22 02:52:18 +00002916 rtnl_unlock();
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002917
2918 adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002919 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002920 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002921 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002922 return;
2923 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002924 goto err_ret;
2925 }
2926
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002927 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002928
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002929wait_npar:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002930 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002931 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002932
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002933 switch (dev_state) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002934 case QLCNIC_DEV_READY:
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002935 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002936 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002937 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002938 return;
2939 }
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002940 case QLCNIC_DEV_FAILED:
2941 break;
2942 default:
2943 qlcnic_schedule_work(adapter,
2944 qlcnic_fwinit_work, FW_POLL_DELAY);
2945 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002946 }
2947
2948err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002949 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2950 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002951 netif_device_attach(adapter->netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002952 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002953}
2954
2955static void
2956qlcnic_detach_work(struct work_struct *work)
2957{
2958 struct qlcnic_adapter *adapter = container_of(work,
2959 struct qlcnic_adapter, fw_work.work);
2960 struct net_device *netdev = adapter->netdev;
2961 u32 status;
2962
2963 netif_device_detach(netdev);
2964
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002965 /* Dont grab rtnl lock during Quiscent mode */
2966 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2967 if (netif_running(netdev))
2968 __qlcnic_down(adapter, netdev);
2969 } else
2970 qlcnic_down(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002971
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002972 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2973
Sony Chacko44f65b22011-08-29 12:50:26 +00002974 if (status & QLCNIC_RCODE_FATAL_ERROR) {
2975 dev_err(&adapter->pdev->dev,
2976 "Detaching the device: peg halt status1=0x%x\n",
2977 status);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002978
Sony Chacko44f65b22011-08-29 12:50:26 +00002979 if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
2980 dev_err(&adapter->pdev->dev,
2981 "On board active cooling fan failed. "
2982 "Device has been halted.\n");
2983 dev_err(&adapter->pdev->dev,
2984 "Replace the adapter.\n");
2985 }
2986
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002987 goto err_ret;
Sony Chacko44f65b22011-08-29 12:50:26 +00002988 }
2989
2990 if (adapter->temp == QLCNIC_TEMP_PANIC) {
2991 dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
2992 adapter->temp);
2993 goto err_ret;
2994 }
2995
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002996 /* Dont ack if this instance is the reset owner */
2997 if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
Sony Chacko44f65b22011-08-29 12:50:26 +00002998 if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
2999 dev_err(&adapter->pdev->dev,
3000 "Failed to set driver state,"
3001 "detaching the device.\n");
Sritej Velaga602ca6f2011-06-22 02:52:17 +00003002 goto err_ret;
Sony Chacko44f65b22011-08-29 12:50:26 +00003003 }
Sritej Velaga602ca6f2011-06-22 02:52:17 +00003004 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003005
3006 adapter->fw_wait_cnt = 0;
3007
3008 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
3009
3010 return;
3011
3012err_ret:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00003013 netif_device_attach(netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00003014 qlcnic_clr_all_drv_state(adapter, 1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003015}
3016
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003017/*Transit NPAR state to NON Operational */
3018static void
3019qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
3020{
3021 u32 state;
3022
3023 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3024 if (state == QLCNIC_DEV_NPAR_NON_OPER)
3025 return;
3026
3027 if (qlcnic_api_lock(adapter))
3028 return;
3029 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
3030 qlcnic_api_unlock(adapter);
3031}
3032
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00003033/*Transit to RESET state from READY state only */
Anirban Chakraborty18f2f612011-05-12 12:48:33 +00003034void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003035qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
3036{
Sritej Velaga646779f2012-02-03 13:45:41 +00003037 u32 state, xg_val = 0, gb_val = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003038
Sritej Velaga646779f2012-02-03 13:45:41 +00003039 qlcnic_xg_set_xg0_mask(xg_val);
3040 qlcnic_xg_set_xg1_mask(xg_val);
3041 QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, xg_val);
3042 qlcnic_gb_set_gb0_mask(gb_val);
3043 qlcnic_gb_set_gb1_mask(gb_val);
3044 qlcnic_gb_set_gb2_mask(gb_val);
3045 qlcnic_gb_set_gb3_mask(gb_val);
3046 QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, gb_val);
3047 dev_info(&adapter->pdev->dev, "Pause control frames disabled"
3048 " on all ports\n");
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003049 adapter->need_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003050 if (qlcnic_api_lock(adapter))
3051 return;
3052
3053 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00003054 if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD)) {
3055 netdev_err(adapter->netdev,
3056 "Device is in FAILED state, Please Reboot\n");
3057 qlcnic_api_unlock(adapter);
3058 return;
3059 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003060
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00003061 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003062 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Sritej Velaga602ca6f2011-06-22 02:52:17 +00003063 adapter->flags |= QLCNIC_FW_RESET_OWNER;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003064 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00003065 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003066 }
3067
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003068 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003069 qlcnic_api_unlock(adapter);
3070}
3071
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00003072/* Transit to NPAR READY state from NPAR NOT READY state */
3073static void
3074qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
3075{
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00003076 if (qlcnic_api_lock(adapter))
3077 return;
3078
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003079 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
3080 QLCDB(adapter, DRV, "NPAR operational state set\n");
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00003081
3082 qlcnic_api_unlock(adapter);
3083}
3084
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003085static void
3086qlcnic_schedule_work(struct qlcnic_adapter *adapter,
3087 work_func_t func, int delay)
3088{
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003089 if (test_bit(__QLCNIC_AER, &adapter->state))
3090 return;
3091
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003092 INIT_DELAYED_WORK(&adapter->fw_work, func);
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00003093 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
3094 round_jiffies_relative(delay));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003095}
3096
3097static void
3098qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
3099{
3100 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3101 msleep(10);
3102
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00003103 if (!adapter->fw_work.work.func)
3104 return;
3105
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003106 cancel_delayed_work_sync(&adapter->fw_work);
3107}
3108
3109static void
3110qlcnic_attach_work(struct work_struct *work)
3111{
3112 struct qlcnic_adapter *adapter = container_of(work,
3113 struct qlcnic_adapter, fw_work.work);
3114 struct net_device *netdev = adapter->netdev;
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00003115 u32 npar_state;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003116
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00003117 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
3118 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3119 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3120 qlcnic_clr_all_drv_state(adapter, 0);
3121 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3122 qlcnic_schedule_work(adapter, qlcnic_attach_work,
3123 FW_POLL_DELAY);
3124 else
3125 goto attach;
3126 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3127 return;
3128 }
3129attach:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003130 if (netif_running(netdev)) {
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00003131 if (qlcnic_up(adapter, netdev))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003132 goto done;
3133
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003134 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003135 }
3136
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003137done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00003138 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003139 adapter->fw_fail_cnt = 0;
Sritej Velaga032a13c2011-07-29 13:30:27 +00003140 adapter->flags &= ~QLCNIC_FW_HANG;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003141 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00003142
3143 if (!qlcnic_clr_drv_state(adapter))
3144 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3145 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003146}
3147
3148static int
3149qlcnic_check_health(struct qlcnic_adapter *adapter)
3150{
Sony Chacko4e708122010-08-31 17:17:44 +00003151 u32 state = 0, heartbeat;
Ameen Rahman853d4bc2011-09-13 08:06:17 +00003152 u32 peg_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003153
3154 if (qlcnic_check_temp(adapter))
3155 goto detach;
3156
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00003157 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003158 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003159
3160 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00003161 if (state == QLCNIC_DEV_NEED_RESET) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003162 qlcnic_set_npar_non_operational(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003163 adapter->need_fw_reset = 1;
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00003164 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3165 goto detach;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003166
Sony Chacko4e708122010-08-31 17:17:44 +00003167 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3168 if (heartbeat != adapter->heartbeat) {
3169 adapter->heartbeat = heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003170 adapter->fw_fail_cnt = 0;
3171 if (adapter->need_fw_reset)
3172 goto detach;
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003173
amit salecha9ce13ca2011-02-23 03:21:24 +00003174 if (adapter->reset_context && auto_fw_reset) {
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003175 qlcnic_reset_hw_context(adapter);
3176 adapter->netdev->trans_start = jiffies;
3177 }
3178
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003179 return 0;
3180 }
3181
3182 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3183 return 0;
3184
Sritej Velaga032a13c2011-07-29 13:30:27 +00003185 adapter->flags |= QLCNIC_FW_HANG;
3186
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003187 qlcnic_dev_request_reset(adapter);
3188
amit salecha9ce13ca2011-02-23 03:21:24 +00003189 if (auto_fw_reset)
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00003190 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003191
Ameen Rahman853d4bc2011-09-13 08:06:17 +00003192 dev_err(&adapter->pdev->dev, "firmware hang detected\n");
3193 dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
Sritej Velagac76ecb72011-07-29 13:30:29 +00003194 "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
3195 "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
3196 "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
3197 "PEG_NET_4_PC: 0x%x\n",
3198 QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1),
3199 QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS2),
3200 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c),
3201 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c),
3202 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c),
3203 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c),
3204 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c));
Ameen Rahman853d4bc2011-09-13 08:06:17 +00003205 peg_status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
Sritej Velaga97048a12012-02-03 13:45:43 +00003206 if (QLCNIC_FWERROR_CODE(peg_status) == 0x67)
Ameen Rahman853d4bc2011-09-13 08:06:17 +00003207 dev_err(&adapter->pdev->dev,
3208 "Firmware aborted with error code 0x00006700. "
3209 "Device is being reset.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003210detach:
3211 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3212 QLCNIC_DEV_NEED_RESET;
3213
amit salecha9ce13ca2011-02-23 03:21:24 +00003214 if (auto_fw_reset &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003215 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3216
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003217 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003218 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3219 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003220
3221 return 1;
3222}
3223
3224static void
3225qlcnic_fw_poll_work(struct work_struct *work)
3226{
3227 struct qlcnic_adapter *adapter = container_of(work,
3228 struct qlcnic_adapter, fw_work.work);
3229
3230 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3231 goto reschedule;
3232
3233
3234 if (qlcnic_check_health(adapter))
3235 return;
3236
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00003237 if (adapter->fhash.fnum)
3238 qlcnic_prune_lb_filters(adapter);
3239
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003240reschedule:
3241 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3242}
3243
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003244static int qlcnic_is_first_func(struct pci_dev *pdev)
3245{
3246 struct pci_dev *oth_pdev;
3247 int val = pdev->devfn;
3248
3249 while (val-- > 0) {
3250 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3251 (pdev->bus), pdev->bus->number,
3252 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003253 if (!oth_pdev)
3254 continue;
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003255
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003256 if (oth_pdev->current_state != PCI_D3cold) {
3257 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003258 return 0;
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003259 }
3260 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003261 }
3262 return 1;
3263}
3264
3265static int qlcnic_attach_func(struct pci_dev *pdev)
3266{
3267 int err, first_func;
3268 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3269 struct net_device *netdev = adapter->netdev;
3270
3271 pdev->error_state = pci_channel_io_normal;
3272
3273 err = pci_enable_device(pdev);
3274 if (err)
3275 return err;
3276
3277 pci_set_power_state(pdev, PCI_D0);
3278 pci_set_master(pdev);
3279 pci_restore_state(pdev);
3280
3281 first_func = qlcnic_is_first_func(pdev);
3282
3283 if (qlcnic_api_lock(adapter))
3284 return -EINVAL;
3285
Amit Kumar Salecha933fce122010-08-17 00:34:19 +00003286 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003287 adapter->need_fw_reset = 1;
3288 set_bit(__QLCNIC_START_FW, &adapter->state);
3289 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3290 QLCDB(adapter, DRV, "Restarting fw\n");
3291 }
3292 qlcnic_api_unlock(adapter);
3293
3294 err = adapter->nic_ops->start_firmware(adapter);
3295 if (err)
3296 return err;
3297
3298 qlcnic_clr_drv_state(adapter);
3299 qlcnic_setup_intr(adapter);
3300
3301 if (netif_running(netdev)) {
3302 err = qlcnic_attach(adapter);
3303 if (err) {
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00003304 qlcnic_clr_all_drv_state(adapter, 1);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003305 clear_bit(__QLCNIC_AER, &adapter->state);
3306 netif_device_attach(netdev);
3307 return err;
3308 }
3309
3310 err = qlcnic_up(adapter, netdev);
3311 if (err)
3312 goto done;
3313
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003314 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003315 }
3316 done:
3317 netif_device_attach(netdev);
3318 return err;
3319}
3320
3321static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3322 pci_channel_state_t state)
3323{
3324 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3325 struct net_device *netdev = adapter->netdev;
3326
3327 if (state == pci_channel_io_perm_failure)
3328 return PCI_ERS_RESULT_DISCONNECT;
3329
3330 if (state == pci_channel_io_normal)
3331 return PCI_ERS_RESULT_RECOVERED;
3332
3333 set_bit(__QLCNIC_AER, &adapter->state);
3334 netif_device_detach(netdev);
3335
3336 cancel_delayed_work_sync(&adapter->fw_work);
3337
3338 if (netif_running(netdev))
3339 qlcnic_down(adapter, netdev);
3340
3341 qlcnic_detach(adapter);
3342 qlcnic_teardown_intr(adapter);
3343
3344 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3345
3346 pci_save_state(pdev);
3347 pci_disable_device(pdev);
3348
3349 return PCI_ERS_RESULT_NEED_RESET;
3350}
3351
3352static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3353{
3354 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3355 PCI_ERS_RESULT_RECOVERED;
3356}
3357
3358static void qlcnic_io_resume(struct pci_dev *pdev)
3359{
3360 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3361
3362 pci_cleanup_aer_uncorrect_error_status(pdev);
3363
3364 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3365 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3366 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3367 FW_POLL_DELAY);
3368}
3369
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003370static int
3371qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3372{
3373 int err;
3374
3375 err = qlcnic_can_start_firmware(adapter);
3376 if (err)
3377 return err;
3378
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +00003379 err = qlcnic_check_npar_opertional(adapter);
3380 if (err)
3381 return err;
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003382
Rajesh Borundia174240a2010-08-31 17:17:47 +00003383 err = qlcnic_initialize_nic(adapter);
3384 if (err)
3385 return err;
3386
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003387 qlcnic_check_options(adapter);
3388
Rajesh Borundia73733732010-08-31 17:17:50 +00003389 err = qlcnic_set_eswitch_port_config(adapter);
3390 if (err)
3391 return err;
3392
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003393 adapter->need_fw_reset = 0;
3394
3395 return err;
3396}
3397
3398static int
3399qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3400{
3401 return -EOPNOTSUPP;
3402}
3403
3404static int
3405qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3406{
3407 return -EOPNOTSUPP;
3408}
3409
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003410static ssize_t
3411qlcnic_store_bridged_mode(struct device *dev,
3412 struct device_attribute *attr, const char *buf, size_t len)
3413{
3414 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3415 unsigned long new;
3416 int ret = -EINVAL;
3417
3418 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3419 goto err_out;
3420
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00003421 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003422 goto err_out;
3423
3424 if (strict_strtoul(buf, 2, &new))
3425 goto err_out;
3426
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00003427 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003428 ret = len;
3429
3430err_out:
3431 return ret;
3432}
3433
3434static ssize_t
3435qlcnic_show_bridged_mode(struct device *dev,
3436 struct device_attribute *attr, char *buf)
3437{
3438 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3439 int bridged_mode = 0;
3440
3441 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3442 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3443
3444 return sprintf(buf, "%d\n", bridged_mode);
3445}
3446
3447static struct device_attribute dev_attr_bridged_mode = {
3448 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3449 .show = qlcnic_show_bridged_mode,
3450 .store = qlcnic_store_bridged_mode,
3451};
3452
3453static ssize_t
3454qlcnic_store_diag_mode(struct device *dev,
3455 struct device_attribute *attr, const char *buf, size_t len)
3456{
3457 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3458 unsigned long new;
3459
3460 if (strict_strtoul(buf, 2, &new))
3461 return -EINVAL;
3462
3463 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3464 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3465
3466 return len;
3467}
3468
3469static ssize_t
3470qlcnic_show_diag_mode(struct device *dev,
3471 struct device_attribute *attr, char *buf)
3472{
3473 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3474
3475 return sprintf(buf, "%d\n",
3476 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3477}
3478
3479static struct device_attribute dev_attr_diag_mode = {
3480 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3481 .show = qlcnic_show_diag_mode,
3482 .store = qlcnic_store_diag_mode,
3483};
3484
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00003485int qlcnic_validate_max_rss(struct net_device *netdev, u8 max_hw, u8 val)
3486{
3487 if (!use_msi_x && !use_msi) {
3488 netdev_info(netdev, "no msix or msi support, hence no rss\n");
3489 return -EINVAL;
3490 }
3491
3492 if ((val > max_hw) || (val < 2) || !is_power_of_2(val)) {
3493 netdev_info(netdev, "rss_ring valid range [2 - %x] in "
3494 " powers of 2\n", max_hw);
3495 return -EINVAL;
3496 }
3497 return 0;
3498
3499}
3500
3501int qlcnic_set_max_rss(struct qlcnic_adapter *adapter, u8 data)
3502{
3503 struct net_device *netdev = adapter->netdev;
3504 int err = 0;
3505
3506 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3507 return -EBUSY;
3508
3509 netif_device_detach(netdev);
3510 if (netif_running(netdev))
3511 __qlcnic_down(adapter, netdev);
3512 qlcnic_detach(adapter);
3513 qlcnic_teardown_intr(adapter);
3514
3515 if (qlcnic_enable_msix(adapter, data)) {
3516 netdev_info(netdev, "failed setting max_rss; rss disabled\n");
3517 qlcnic_enable_msi_legacy(adapter);
3518 }
3519
3520 if (netif_running(netdev)) {
3521 err = qlcnic_attach(adapter);
3522 if (err)
3523 goto done;
3524 err = __qlcnic_up(adapter, netdev);
3525 if (err)
3526 goto done;
3527 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3528 }
3529 done:
3530 netif_device_attach(netdev);
3531 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3532 return err;
3533}
3534
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003535static int
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003536qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon, u8 *state,
3537 u8 *rate)
3538{
3539 *rate = LSB(beacon);
3540 *state = MSB(beacon);
3541
3542 QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
3543
3544 if (!*state) {
3545 *rate = __QLCNIC_MAX_LED_RATE;
3546 return 0;
3547 } else if (*state > __QLCNIC_MAX_LED_STATE)
3548 return -EINVAL;
3549
3550 if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
3551 return -EINVAL;
3552
3553 return 0;
3554}
3555
3556static ssize_t
3557qlcnic_store_beacon(struct device *dev,
3558 struct device_attribute *attr, const char *buf, size_t len)
3559{
3560 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3561 int max_sds_rings = adapter->max_sds_rings;
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003562 u16 beacon;
3563 u8 b_state, b_rate;
3564 int err;
3565
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003566 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
3567 dev_warn(dev, "LED test not supported for non "
3568 "privilege function\n");
3569 return -EOPNOTSUPP;
3570 }
3571
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003572 if (len != sizeof(u16))
3573 return QL_STATUS_INVALID_PARAM;
3574
3575 memcpy(&beacon, buf, sizeof(u16));
3576 err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
3577 if (err)
3578 return err;
3579
3580 if (adapter->ahw->beacon_state == b_state)
3581 return len;
3582
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003583 rtnl_lock();
3584
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003585 if (!adapter->ahw->beacon_state)
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003586 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
3587 rtnl_unlock();
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003588 return -EBUSY;
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003589 }
3590
3591 if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
3592 err = -EIO;
3593 goto out;
3594 }
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003595
3596 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003597 err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003598 if (err)
3599 goto out;
3600 set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003601 }
3602
3603 err = qlcnic_config_led(adapter, b_state, b_rate);
3604
3605 if (!err) {
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003606 err = len;
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003607 adapter->ahw->beacon_state = b_state;
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003608 }
3609
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003610 if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003611 qlcnic_diag_free_res(adapter->netdev, max_sds_rings);
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003612
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003613 out:
3614 if (!adapter->ahw->beacon_state)
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003615 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003616 rtnl_unlock();
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003617
3618 return err;
3619}
3620
3621static ssize_t
3622qlcnic_show_beacon(struct device *dev,
3623 struct device_attribute *attr, char *buf)
3624{
3625 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3626
3627 return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
3628}
3629
3630static struct device_attribute dev_attr_beacon = {
3631 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
3632 .show = qlcnic_show_beacon,
3633 .store = qlcnic_store_beacon,
3634};
3635
3636static int
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003637qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3638 loff_t offset, size_t size)
3639{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003640 size_t crb_size = 4;
3641
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003642 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3643 return -EIO;
3644
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003645 if (offset < QLCNIC_PCI_CRBSPACE) {
3646 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3647 QLCNIC_PCI_CAMQM_END))
3648 crb_size = 8;
3649 else
3650 return -EINVAL;
3651 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003652
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003653 if ((size != crb_size) || (offset & (crb_size-1)))
3654 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003655
3656 return 0;
3657}
3658
3659static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003660qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3661 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003662 char *buf, loff_t offset, size_t size)
3663{
3664 struct device *dev = container_of(kobj, struct device, kobj);
3665 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3666 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003667 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003668 int ret;
3669
3670 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3671 if (ret != 0)
3672 return ret;
3673
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003674 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3675 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3676 memcpy(buf, &qmdata, size);
3677 } else {
3678 data = QLCRD32(adapter, offset);
3679 memcpy(buf, &data, size);
3680 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003681 return size;
3682}
3683
3684static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003685qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3686 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003687 char *buf, loff_t offset, size_t size)
3688{
3689 struct device *dev = container_of(kobj, struct device, kobj);
3690 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3691 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003692 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003693 int ret;
3694
3695 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3696 if (ret != 0)
3697 return ret;
3698
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003699 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3700 memcpy(&qmdata, buf, size);
3701 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3702 } else {
3703 memcpy(&data, buf, size);
3704 QLCWR32(adapter, offset, data);
3705 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003706 return size;
3707}
3708
3709static int
3710qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3711 loff_t offset, size_t size)
3712{
3713 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3714 return -EIO;
3715
3716 if ((size != 8) || (offset & 0x7))
3717 return -EIO;
3718
3719 return 0;
3720}
3721
3722static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003723qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3724 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003725 char *buf, loff_t offset, size_t size)
3726{
3727 struct device *dev = container_of(kobj, struct device, kobj);
3728 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3729 u64 data;
3730 int ret;
3731
3732 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3733 if (ret != 0)
3734 return ret;
3735
3736 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3737 return -EIO;
3738
3739 memcpy(buf, &data, size);
3740
3741 return size;
3742}
3743
3744static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003745qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3746 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003747 char *buf, loff_t offset, size_t size)
3748{
3749 struct device *dev = container_of(kobj, struct device, kobj);
3750 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3751 u64 data;
3752 int ret;
3753
3754 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3755 if (ret != 0)
3756 return ret;
3757
3758 memcpy(&data, buf, size);
3759
3760 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3761 return -EIO;
3762
3763 return size;
3764}
3765
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003766static struct bin_attribute bin_attr_crb = {
3767 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3768 .size = 0,
3769 .read = qlcnic_sysfs_read_crb,
3770 .write = qlcnic_sysfs_write_crb,
3771};
3772
3773static struct bin_attribute bin_attr_mem = {
3774 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3775 .size = 0,
3776 .read = qlcnic_sysfs_read_mem,
3777 .write = qlcnic_sysfs_write_mem,
3778};
3779
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003780static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003781validate_pm_config(struct qlcnic_adapter *adapter,
3782 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3783{
3784
3785 u8 src_pci_func, s_esw_id, d_esw_id;
3786 u8 dest_pci_func;
3787 int i;
3788
3789 for (i = 0; i < count; i++) {
3790 src_pci_func = pm_cfg[i].pci_func;
3791 dest_pci_func = pm_cfg[i].dest_npar;
3792 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3793 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3794 return QL_STATUS_INVALID_PARAM;
3795
3796 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3797 return QL_STATUS_INVALID_PARAM;
3798
3799 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3800 return QL_STATUS_INVALID_PARAM;
3801
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003802 s_esw_id = adapter->npars[src_pci_func].phy_port;
3803 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3804
3805 if (s_esw_id != d_esw_id)
3806 return QL_STATUS_INVALID_PARAM;
3807
3808 }
3809 return 0;
3810
3811}
3812
3813static ssize_t
3814qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3815 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3816{
3817 struct device *dev = container_of(kobj, struct device, kobj);
3818 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3819 struct qlcnic_pm_func_cfg *pm_cfg;
3820 u32 id, action, pci_func;
3821 int count, rem, i, ret;
3822
3823 count = size / sizeof(struct qlcnic_pm_func_cfg);
3824 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3825 if (rem)
3826 return QL_STATUS_INVALID_PARAM;
3827
3828 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3829
3830 ret = validate_pm_config(adapter, pm_cfg, count);
3831 if (ret)
3832 return ret;
3833 for (i = 0; i < count; i++) {
3834 pci_func = pm_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003835 action = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003836 id = adapter->npars[pci_func].phy_port;
3837 ret = qlcnic_config_port_mirroring(adapter, id,
3838 action, pci_func);
3839 if (ret)
3840 return ret;
3841 }
3842
3843 for (i = 0; i < count; i++) {
3844 pci_func = pm_cfg[i].pci_func;
3845 id = adapter->npars[pci_func].phy_port;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003846 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003847 adapter->npars[pci_func].dest_npar = id;
3848 }
3849 return size;
3850}
3851
3852static ssize_t
3853qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3854 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3855{
3856 struct device *dev = container_of(kobj, struct device, kobj);
3857 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3858 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3859 int i;
3860
3861 if (size != sizeof(pm_cfg))
3862 return QL_STATUS_INVALID_PARAM;
3863
3864 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3865 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3866 continue;
3867 pm_cfg[i].action = adapter->npars[i].enable_pm;
3868 pm_cfg[i].dest_npar = 0;
3869 pm_cfg[i].pci_func = i;
3870 }
3871 memcpy(buf, &pm_cfg, size);
3872
3873 return size;
3874}
3875
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003876static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003877validate_esw_config(struct qlcnic_adapter *adapter,
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003878 struct qlcnic_esw_func_cfg *esw_cfg, int count)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003879{
Rajesh Borundia7613c872010-08-31 17:17:48 +00003880 u32 op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003881 u8 pci_func;
3882 int i;
Rajesh Borundia7613c872010-08-31 17:17:48 +00003883
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003884 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
Rajesh Borundia7613c872010-08-31 17:17:48 +00003885
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003886 for (i = 0; i < count; i++) {
3887 pci_func = esw_cfg[i].pci_func;
3888 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3889 return QL_STATUS_INVALID_PARAM;
3890
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003891 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3892 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003893 return QL_STATUS_INVALID_PARAM;
3894
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003895 switch (esw_cfg[i].op_mode) {
3896 case QLCNIC_PORT_DEFAULTS:
Rajesh Borundia7613c872010-08-31 17:17:48 +00003897 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
Rajesh Borundia73733732010-08-31 17:17:50 +00003898 QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha091056b2010-12-02 20:41:43 +00003899 if (esw_cfg[i].mac_anti_spoof != 0)
3900 return QL_STATUS_INVALID_PARAM;
3901 if (esw_cfg[i].mac_override != 1)
3902 return QL_STATUS_INVALID_PARAM;
3903 if (esw_cfg[i].promisc_mode != 1)
3904 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia73733732010-08-31 17:17:50 +00003905 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003906 break;
3907 case QLCNIC_ADD_VLAN:
3908 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3909 return QL_STATUS_INVALID_PARAM;
3910 if (!esw_cfg[i].op_type)
3911 return QL_STATUS_INVALID_PARAM;
3912 break;
3913 case QLCNIC_DEL_VLAN:
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003914 if (!esw_cfg[i].op_type)
3915 return QL_STATUS_INVALID_PARAM;
3916 break;
3917 default:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003918 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003919 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003920 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003921 return 0;
3922}
3923
3924static ssize_t
3925qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3926 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3927{
3928 struct device *dev = container_of(kobj, struct device, kobj);
3929 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3930 struct qlcnic_esw_func_cfg *esw_cfg;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003931 struct qlcnic_npar_info *npar;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003932 int count, rem, i, ret;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003933 u8 pci_func, op_mode = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003934
3935 count = size / sizeof(struct qlcnic_esw_func_cfg);
3936 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3937 if (rem)
3938 return QL_STATUS_INVALID_PARAM;
3939
3940 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3941 ret = validate_esw_config(adapter, esw_cfg, count);
3942 if (ret)
3943 return ret;
3944
3945 for (i = 0; i < count; i++) {
Rajesh Borundia0325d692010-08-19 05:08:26 +00003946 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3947 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3948 return QL_STATUS_INVALID_PARAM;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003949
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003950 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003951 continue;
3952
3953 op_mode = esw_cfg[i].op_mode;
3954 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3955 esw_cfg[i].op_mode = op_mode;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003956 esw_cfg[i].pci_func = adapter->ahw->pci_func;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003957
3958 switch (esw_cfg[i].op_mode) {
3959 case QLCNIC_PORT_DEFAULTS:
3960 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3961 break;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00003962 case QLCNIC_ADD_VLAN:
3963 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3964 break;
3965 case QLCNIC_DEL_VLAN:
3966 esw_cfg[i].vlan_id = 0;
3967 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3968 break;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003969 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003970 }
3971
Rajesh Borundia0325d692010-08-19 05:08:26 +00003972 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3973 goto out;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003974
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003975 for (i = 0; i < count; i++) {
3976 pci_func = esw_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003977 npar = &adapter->npars[pci_func];
3978 switch (esw_cfg[i].op_mode) {
3979 case QLCNIC_PORT_DEFAULTS:
3980 npar->promisc_mode = esw_cfg[i].promisc_mode;
Rajesh Borundia73733732010-08-31 17:17:50 +00003981 npar->mac_override = esw_cfg[i].mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003982 npar->offload_flags = esw_cfg[i].offload_flags;
3983 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3984 npar->discard_tagged = esw_cfg[i].discard_tagged;
3985 break;
3986 case QLCNIC_ADD_VLAN:
3987 npar->pvid = esw_cfg[i].vlan_id;
3988 break;
3989 case QLCNIC_DEL_VLAN:
3990 npar->pvid = 0;
3991 break;
3992 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003993 }
Rajesh Borundia0325d692010-08-19 05:08:26 +00003994out:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003995 return size;
3996}
3997
3998static ssize_t
3999qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
4000 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4001{
4002 struct device *dev = container_of(kobj, struct device, kobj);
4003 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4004 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004005 u8 i;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004006
4007 if (size != sizeof(esw_cfg))
4008 return QL_STATUS_INVALID_PARAM;
4009
4010 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
4011 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
4012 continue;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004013 esw_cfg[i].pci_func = i;
4014 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
4015 return QL_STATUS_INVALID_PARAM;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004016 }
4017 memcpy(buf, &esw_cfg, size);
4018
4019 return size;
4020}
4021
Anirban Chakrabortycea89752010-07-13 20:33:35 +00004022static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004023validate_npar_config(struct qlcnic_adapter *adapter,
4024 struct qlcnic_npar_func_cfg *np_cfg, int count)
4025{
4026 u8 pci_func, i;
4027
4028 for (i = 0; i < count; i++) {
4029 pci_func = np_cfg[i].pci_func;
4030 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
4031 return QL_STATUS_INVALID_PARAM;
4032
4033 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
4034 return QL_STATUS_INVALID_PARAM;
4035
Rajesh Borundiad12b0d92011-02-23 03:21:25 +00004036 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
4037 !IS_VALID_BW(np_cfg[i].max_bw))
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004038 return QL_STATUS_INVALID_PARAM;
4039 }
4040 return 0;
4041}
4042
4043static ssize_t
4044qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
4045 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4046{
4047 struct device *dev = container_of(kobj, struct device, kobj);
4048 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4049 struct qlcnic_info nic_info;
4050 struct qlcnic_npar_func_cfg *np_cfg;
4051 int i, count, rem, ret;
4052 u8 pci_func;
4053
4054 count = size / sizeof(struct qlcnic_npar_func_cfg);
4055 rem = size % sizeof(struct qlcnic_npar_func_cfg);
4056 if (rem)
4057 return QL_STATUS_INVALID_PARAM;
4058
4059 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
4060 ret = validate_npar_config(adapter, np_cfg, count);
4061 if (ret)
4062 return ret;
4063
4064 for (i = 0; i < count ; i++) {
4065 pci_func = np_cfg[i].pci_func;
4066 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
4067 if (ret)
4068 return ret;
4069 nic_info.pci_func = pci_func;
4070 nic_info.min_tx_bw = np_cfg[i].min_bw;
4071 nic_info.max_tx_bw = np_cfg[i].max_bw;
4072 ret = qlcnic_set_nic_info(adapter, &nic_info);
4073 if (ret)
4074 return ret;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00004075 adapter->npars[i].min_bw = nic_info.min_tx_bw;
4076 adapter->npars[i].max_bw = nic_info.max_tx_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004077 }
4078
4079 return size;
4080
4081}
4082static ssize_t
4083qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
4084 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4085{
4086 struct device *dev = container_of(kobj, struct device, kobj);
4087 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4088 struct qlcnic_info nic_info;
4089 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
4090 int i, ret;
4091
4092 if (size != sizeof(np_cfg))
4093 return QL_STATUS_INVALID_PARAM;
4094
4095 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4096 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
4097 continue;
4098 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
4099 if (ret)
4100 return ret;
4101
4102 np_cfg[i].pci_func = i;
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +00004103 np_cfg[i].op_mode = (u8)nic_info.op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004104 np_cfg[i].port_num = nic_info.phys_port;
4105 np_cfg[i].fw_capab = nic_info.capabilities;
4106 np_cfg[i].min_bw = nic_info.min_tx_bw ;
4107 np_cfg[i].max_bw = nic_info.max_tx_bw;
4108 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
4109 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
4110 }
4111 memcpy(buf, &np_cfg, size);
4112 return size;
4113}
4114
4115static ssize_t
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004116qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
4117 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4118{
4119 struct device *dev = container_of(kobj, struct device, kobj);
4120 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4121 struct qlcnic_esw_statistics port_stats;
4122 int ret;
4123
4124 if (size != sizeof(struct qlcnic_esw_statistics))
4125 return QL_STATUS_INVALID_PARAM;
4126
4127 if (offset >= QLCNIC_MAX_PCI_FUNC)
4128 return QL_STATUS_INVALID_PARAM;
4129
4130 memset(&port_stats, 0, size);
4131 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4132 &port_stats.rx);
4133 if (ret)
4134 return ret;
4135
4136 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4137 &port_stats.tx);
4138 if (ret)
4139 return ret;
4140
4141 memcpy(buf, &port_stats, size);
4142 return size;
4143}
4144
4145static ssize_t
4146qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
4147 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4148{
4149 struct device *dev = container_of(kobj, struct device, kobj);
4150 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4151 struct qlcnic_esw_statistics esw_stats;
4152 int ret;
4153
4154 if (size != sizeof(struct qlcnic_esw_statistics))
4155 return QL_STATUS_INVALID_PARAM;
4156
4157 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4158 return QL_STATUS_INVALID_PARAM;
4159
4160 memset(&esw_stats, 0, size);
4161 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4162 &esw_stats.rx);
4163 if (ret)
4164 return ret;
4165
4166 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4167 &esw_stats.tx);
4168 if (ret)
4169 return ret;
4170
4171 memcpy(buf, &esw_stats, size);
4172 return size;
4173}
4174
4175static ssize_t
4176qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
4177 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4178{
4179 struct device *dev = container_of(kobj, struct device, kobj);
4180 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4181 int ret;
4182
4183 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4184 return QL_STATUS_INVALID_PARAM;
4185
4186 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4187 QLCNIC_QUERY_RX_COUNTER);
4188 if (ret)
4189 return ret;
4190
4191 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4192 QLCNIC_QUERY_TX_COUNTER);
4193 if (ret)
4194 return ret;
4195
4196 return size;
4197}
4198
4199static ssize_t
4200qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
4201 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4202{
4203
4204 struct device *dev = container_of(kobj, struct device, kobj);
4205 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4206 int ret;
4207
4208 if (offset >= QLCNIC_MAX_PCI_FUNC)
4209 return QL_STATUS_INVALID_PARAM;
4210
4211 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4212 QLCNIC_QUERY_RX_COUNTER);
4213 if (ret)
4214 return ret;
4215
4216 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4217 QLCNIC_QUERY_TX_COUNTER);
4218 if (ret)
4219 return ret;
4220
4221 return size;
4222}
4223
4224static ssize_t
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004225qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
4226 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4227{
4228 struct device *dev = container_of(kobj, struct device, kobj);
4229 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4230 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
Dan Carpentere88db3b2010-08-09 21:49:36 +00004231 struct qlcnic_pci_info *pci_info;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004232 int i, ret;
4233
4234 if (size != sizeof(pci_cfg))
4235 return QL_STATUS_INVALID_PARAM;
4236
Dan Carpentere88db3b2010-08-09 21:49:36 +00004237 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
4238 if (!pci_info)
4239 return -ENOMEM;
4240
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004241 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpentere88db3b2010-08-09 21:49:36 +00004242 if (ret) {
4243 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004244 return ret;
Dan Carpentere88db3b2010-08-09 21:49:36 +00004245 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004246
4247 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4248 pci_cfg[i].pci_func = pci_info[i].id;
4249 pci_cfg[i].func_type = pci_info[i].type;
4250 pci_cfg[i].port_num = pci_info[i].default_port;
4251 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
4252 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
4253 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
4254 }
4255 memcpy(buf, &pci_cfg, size);
Dan Carpentere88db3b2010-08-09 21:49:36 +00004256 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004257 return size;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004258}
4259static struct bin_attribute bin_attr_npar_config = {
4260 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
4261 .size = 0,
4262 .read = qlcnic_sysfs_read_npar_config,
4263 .write = qlcnic_sysfs_write_npar_config,
4264};
4265
4266static struct bin_attribute bin_attr_pci_config = {
4267 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
4268 .size = 0,
4269 .read = qlcnic_sysfs_read_pci_config,
4270 .write = NULL,
4271};
4272
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004273static struct bin_attribute bin_attr_port_stats = {
4274 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
4275 .size = 0,
4276 .read = qlcnic_sysfs_get_port_stats,
4277 .write = qlcnic_sysfs_clear_port_stats,
4278};
4279
4280static struct bin_attribute bin_attr_esw_stats = {
4281 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
4282 .size = 0,
4283 .read = qlcnic_sysfs_get_esw_stats,
4284 .write = qlcnic_sysfs_clear_esw_stats,
4285};
4286
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004287static struct bin_attribute bin_attr_esw_config = {
4288 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
4289 .size = 0,
4290 .read = qlcnic_sysfs_read_esw_config,
4291 .write = qlcnic_sysfs_write_esw_config,
4292};
4293
4294static struct bin_attribute bin_attr_pm_config = {
4295 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
4296 .size = 0,
4297 .read = qlcnic_sysfs_read_pm_config,
4298 .write = qlcnic_sysfs_write_pm_config,
4299};
4300
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004301static void
4302qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
4303{
4304 struct device *dev = &adapter->pdev->dev;
4305
4306 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4307 if (device_create_file(dev, &dev_attr_bridged_mode))
4308 dev_warn(dev,
4309 "failed to create bridged_mode sysfs entry\n");
4310}
4311
4312static void
4313qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
4314{
4315 struct device *dev = &adapter->pdev->dev;
4316
4317 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4318 device_remove_file(dev, &dev_attr_bridged_mode);
4319}
4320
4321static void
4322qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
4323{
4324 struct device *dev = &adapter->pdev->dev;
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004325 u32 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004326
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004327 if (device_create_bin_file(dev, &bin_attr_port_stats))
4328 dev_info(dev, "failed to create port stats sysfs entry");
4329
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004330 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4331 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004332 if (device_create_file(dev, &dev_attr_diag_mode))
4333 dev_info(dev, "failed to create diag_mode sysfs entry\n");
4334 if (device_create_bin_file(dev, &bin_attr_crb))
4335 dev_info(dev, "failed to create crb sysfs entry\n");
4336 if (device_create_bin_file(dev, &bin_attr_mem))
4337 dev_info(dev, "failed to create mem sysfs entry\n");
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004338
4339 if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD))
4340 return;
4341
Sony Chacko53478fe2011-04-01 14:27:47 +00004342 if (device_create_bin_file(dev, &bin_attr_pci_config))
4343 dev_info(dev, "failed to create pci config sysfs entry");
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004344 if (device_create_file(dev, &dev_attr_beacon))
4345 dev_info(dev, "failed to create beacon sysfs entry");
4346
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004347 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4348 return;
4349 if (device_create_bin_file(dev, &bin_attr_esw_config))
4350 dev_info(dev, "failed to create esw config sysfs entry");
4351 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004352 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004353 if (device_create_bin_file(dev, &bin_attr_npar_config))
4354 dev_info(dev, "failed to create npar config sysfs entry");
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004355 if (device_create_bin_file(dev, &bin_attr_pm_config))
4356 dev_info(dev, "failed to create pm config sysfs entry");
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004357 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4358 dev_info(dev, "failed to create eswitch stats sysfs entry");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004359}
4360
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004361static void
4362qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4363{
4364 struct device *dev = &adapter->pdev->dev;
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004365 u32 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004366
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004367 device_remove_bin_file(dev, &bin_attr_port_stats);
4368
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004369 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4370 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004371 device_remove_file(dev, &dev_attr_diag_mode);
4372 device_remove_bin_file(dev, &bin_attr_crb);
4373 device_remove_bin_file(dev, &bin_attr_mem);
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004374 if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD))
4375 return;
Sony Chacko53478fe2011-04-01 14:27:47 +00004376 device_remove_bin_file(dev, &bin_attr_pci_config);
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004377 device_remove_file(dev, &dev_attr_beacon);
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004378 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4379 return;
4380 device_remove_bin_file(dev, &bin_attr_esw_config);
4381 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004382 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004383 device_remove_bin_file(dev, &bin_attr_npar_config);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004384 device_remove_bin_file(dev, &bin_attr_pm_config);
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004385 device_remove_bin_file(dev, &bin_attr_esw_stats);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004386}
4387
4388#ifdef CONFIG_INET
4389
4390#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4391
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004392static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004393qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4394 struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004395{
4396 struct in_device *indev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004397
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004398 indev = in_dev_get(dev);
4399 if (!indev)
4400 return;
4401
4402 for_ifa(indev) {
4403 switch (event) {
4404 case NETDEV_UP:
4405 qlcnic_config_ipaddr(adapter,
4406 ifa->ifa_address, QLCNIC_IP_UP);
4407 break;
4408 case NETDEV_DOWN:
4409 qlcnic_config_ipaddr(adapter,
4410 ifa->ifa_address, QLCNIC_IP_DOWN);
4411 break;
4412 default:
4413 break;
4414 }
4415 } endfor_ifa(indev);
4416
4417 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004418}
4419
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004420static void
4421qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4422{
4423 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4424 struct net_device *dev;
4425 u16 vid;
4426
4427 qlcnic_config_indev_addr(adapter, netdev, event);
4428
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +00004429 for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
Jiri Pirko223bb152011-07-20 04:54:44 +00004430 dev = __vlan_find_dev_deep(netdev, vid);
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004431 if (!dev)
4432 continue;
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004433 qlcnic_config_indev_addr(adapter, dev, event);
4434 }
4435}
4436
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004437static int qlcnic_netdev_event(struct notifier_block *this,
4438 unsigned long event, void *ptr)
4439{
4440 struct qlcnic_adapter *adapter;
4441 struct net_device *dev = (struct net_device *)ptr;
4442
4443recheck:
4444 if (dev == NULL)
4445 goto done;
4446
4447 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4448 dev = vlan_dev_real_dev(dev);
4449 goto recheck;
4450 }
4451
4452 if (!is_qlcnic_netdev(dev))
4453 goto done;
4454
4455 adapter = netdev_priv(dev);
4456
4457 if (!adapter)
4458 goto done;
4459
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004460 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004461 goto done;
4462
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004463 qlcnic_config_indev_addr(adapter, dev, event);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004464done:
4465 return NOTIFY_DONE;
4466}
4467
4468static int
4469qlcnic_inetaddr_event(struct notifier_block *this,
4470 unsigned long event, void *ptr)
4471{
4472 struct qlcnic_adapter *adapter;
4473 struct net_device *dev;
4474
4475 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4476
4477 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4478
4479recheck:
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004480 if (dev == NULL)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004481 goto done;
4482
4483 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4484 dev = vlan_dev_real_dev(dev);
4485 goto recheck;
4486 }
4487
4488 if (!is_qlcnic_netdev(dev))
4489 goto done;
4490
4491 adapter = netdev_priv(dev);
4492
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00004493 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004494 goto done;
4495
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004496 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004497 goto done;
4498
4499 switch (event) {
4500 case NETDEV_UP:
4501 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4502 break;
4503 case NETDEV_DOWN:
4504 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4505 break;
4506 default:
4507 break;
4508 }
4509
4510done:
4511 return NOTIFY_DONE;
4512}
4513
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004514static struct notifier_block qlcnic_netdev_cb = {
4515 .notifier_call = qlcnic_netdev_event,
4516};
4517
4518static struct notifier_block qlcnic_inetaddr_cb = {
4519 .notifier_call = qlcnic_inetaddr_event,
4520};
4521#else
4522static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004523qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004524{ }
4525#endif
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004526static const struct pci_error_handlers qlcnic_err_handler = {
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004527 .error_detected = qlcnic_io_error_detected,
4528 .slot_reset = qlcnic_io_slot_reset,
4529 .resume = qlcnic_io_resume,
4530};
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004531
4532static struct pci_driver qlcnic_driver = {
4533 .name = qlcnic_driver_name,
4534 .id_table = qlcnic_pci_tbl,
4535 .probe = qlcnic_probe,
4536 .remove = __devexit_p(qlcnic_remove),
4537#ifdef CONFIG_PM
4538 .suspend = qlcnic_suspend,
4539 .resume = qlcnic_resume,
4540#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004541 .shutdown = qlcnic_shutdown,
4542 .err_handler = &qlcnic_err_handler
4543
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004544};
4545
4546static int __init qlcnic_init_module(void)
4547{
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004548 int ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004549
4550 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4551
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004552 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4553 if (qlcnic_wq == NULL) {
4554 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4555 return -ENOMEM;
4556 }
4557
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004558#ifdef CONFIG_INET
4559 register_netdevice_notifier(&qlcnic_netdev_cb);
4560 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4561#endif
4562
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004563 ret = pci_register_driver(&qlcnic_driver);
4564 if (ret) {
4565#ifdef CONFIG_INET
4566 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4567 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4568#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004569 destroy_workqueue(qlcnic_wq);
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004570 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004571
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004572 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004573}
4574
4575module_init(qlcnic_init_module);
4576
4577static void __exit qlcnic_exit_module(void)
4578{
4579
4580 pci_unregister_driver(&qlcnic_driver);
4581
4582#ifdef CONFIG_INET
4583 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4584 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4585#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004586 destroy_workqueue(qlcnic_wq);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004587}
4588
4589module_exit(qlcnic_exit_module);