blob: 46e77a2c51219223909f40d6fb223c0981d37a26 [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;
Sony Chackof848d6d2011-04-01 14:27:59 +0000482 if (pfn > QLCNIC_MAX_PCI_FUNC) {
483 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;
1139 struct qlcnic_host_rds_ring *rds_ring;
1140
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001141 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1142 return -EIO;
1143
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001144 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1145 return 0;
Rajesh Borundia0325d692010-08-19 05:08:26 +00001146 if (qlcnic_set_eswitch_port_config(adapter))
1147 return -EIO;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001148
1149 if (qlcnic_fw_create_ctx(adapter))
1150 return -EIO;
1151
1152 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001153 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1154 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001155 }
1156
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001157 qlcnic_set_multi(netdev);
1158 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1159
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001160 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001161
1162 if (adapter->max_sds_rings > 1)
1163 qlcnic_config_rss(adapter, 1);
1164
1165 qlcnic_config_intr_coalesce(adapter);
1166
Sucheta Chakraborty24763d82010-08-17 00:34:25 +00001167 if (netdev->features & NETIF_F_LRO)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001168 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1169
1170 qlcnic_napi_enable(adapter);
1171
1172 qlcnic_linkevent_request(adapter, 1);
1173
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001174 adapter->reset_context = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001175 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1176 return 0;
1177}
1178
1179/* Usage: During resume and firmware recovery module.*/
1180
1181static int
1182qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1183{
1184 int err = 0;
1185
1186 rtnl_lock();
1187 if (netif_running(netdev))
1188 err = __qlcnic_up(adapter, netdev);
1189 rtnl_unlock();
1190
1191 return err;
1192}
1193
1194static void
1195__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1196{
1197 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1198 return;
1199
1200 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1201 return;
1202
1203 smp_mb();
1204 spin_lock(&adapter->tx_clean_lock);
1205 netif_carrier_off(netdev);
1206 netif_tx_disable(netdev);
1207
1208 qlcnic_free_mac_list(adapter);
1209
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001210 if (adapter->fhash.fnum)
1211 qlcnic_delete_lb_filters(adapter);
1212
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001213 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1214
1215 qlcnic_napi_disable(adapter);
1216
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001217 qlcnic_fw_destroy_ctx(adapter);
1218
1219 qlcnic_reset_rx_buffers_list(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001220 qlcnic_release_tx_buffers(adapter);
1221 spin_unlock(&adapter->tx_clean_lock);
1222}
1223
1224/* Usage: During suspend and firmware recovery module */
1225
1226static void
1227qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1228{
1229 rtnl_lock();
1230 if (netif_running(netdev))
1231 __qlcnic_down(adapter, netdev);
1232 rtnl_unlock();
1233
1234}
1235
1236static int
1237qlcnic_attach(struct qlcnic_adapter *adapter)
1238{
1239 struct net_device *netdev = adapter->netdev;
1240 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001241 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001242
1243 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1244 return 0;
1245
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001246 err = qlcnic_napi_add(adapter, netdev);
1247 if (err)
1248 return err;
1249
1250 err = qlcnic_alloc_sw_resources(adapter);
1251 if (err) {
1252 dev_err(&pdev->dev, "Error in setting sw resources\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001253 goto err_out_napi_del;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001254 }
1255
1256 err = qlcnic_alloc_hw_resources(adapter);
1257 if (err) {
1258 dev_err(&pdev->dev, "Error in setting hw resources\n");
1259 goto err_out_free_sw;
1260 }
1261
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001262 err = qlcnic_request_irq(adapter);
1263 if (err) {
1264 dev_err(&pdev->dev, "failed to setup interrupt\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001265 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001266 }
1267
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001268 qlcnic_create_sysfs_entries(adapter);
1269
1270 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1271 return 0;
1272
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001273err_out_free_hw:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001274 qlcnic_free_hw_resources(adapter);
1275err_out_free_sw:
1276 qlcnic_free_sw_resources(adapter);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001277err_out_napi_del:
1278 qlcnic_napi_del(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001279 return err;
1280}
1281
1282static void
1283qlcnic_detach(struct qlcnic_adapter *adapter)
1284{
1285 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1286 return;
1287
1288 qlcnic_remove_sysfs_entries(adapter);
1289
1290 qlcnic_free_hw_resources(adapter);
1291 qlcnic_release_rx_buffers(adapter);
1292 qlcnic_free_irq(adapter);
1293 qlcnic_napi_del(adapter);
1294 qlcnic_free_sw_resources(adapter);
1295
1296 adapter->is_up = 0;
1297}
1298
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001299void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1300{
1301 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1302 struct qlcnic_host_sds_ring *sds_ring;
1303 int ring;
1304
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001305 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001306 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1307 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001308 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001309 qlcnic_disable_int(sds_ring);
1310 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001311 }
1312
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001313 qlcnic_fw_destroy_ctx(adapter);
1314
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001315 qlcnic_detach(adapter);
1316
1317 adapter->diag_test = 0;
1318 adapter->max_sds_rings = max_sds_rings;
1319
1320 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001321 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001322
1323 if (netif_running(netdev))
1324 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001325out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001326 netif_device_attach(netdev);
1327}
1328
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001329static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1330{
1331 int err = 0;
1332 adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1333 GFP_KERNEL);
1334 if (!adapter->ahw) {
1335 dev_err(&adapter->pdev->dev,
1336 "Failed to allocate recv ctx resources for adapter\n");
1337 err = -ENOMEM;
1338 goto err_out;
1339 }
1340 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1341 GFP_KERNEL);
1342 if (!adapter->recv_ctx) {
1343 dev_err(&adapter->pdev->dev,
1344 "Failed to allocate recv ctx resources for adapter\n");
1345 kfree(adapter->ahw);
1346 adapter->ahw = NULL;
1347 err = -ENOMEM;
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001348 goto err_out;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001349 }
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001350 /* Initialize interrupt coalesce parameters */
1351 adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1352 adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1353 adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001354err_out:
1355 return err;
1356}
1357
1358static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1359{
1360 kfree(adapter->recv_ctx);
1361 adapter->recv_ctx = NULL;
1362
Anirban Chakraborty18f2f612011-05-12 12:48:33 +00001363 if (adapter->ahw->fw_dump.tmpl_hdr) {
1364 vfree(adapter->ahw->fw_dump.tmpl_hdr);
1365 adapter->ahw->fw_dump.tmpl_hdr = NULL;
1366 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001367 kfree(adapter->ahw);
1368 adapter->ahw = NULL;
1369}
1370
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001371int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1372{
1373 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1374 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001375 struct qlcnic_host_rds_ring *rds_ring;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001376 int ring;
1377 int ret;
1378
1379 netif_device_detach(netdev);
1380
1381 if (netif_running(netdev))
1382 __qlcnic_down(adapter, netdev);
1383
1384 qlcnic_detach(adapter);
1385
1386 adapter->max_sds_rings = 1;
1387 adapter->diag_test = test;
1388
1389 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001390 if (ret) {
1391 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001392 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001393 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001394
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001395 ret = qlcnic_fw_create_ctx(adapter);
1396 if (ret) {
1397 qlcnic_detach(adapter);
Sony Chacko57e46242010-07-24 18:32:18 +00001398 netif_device_attach(netdev);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001399 return ret;
1400 }
1401
1402 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001403 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1404 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001405 }
1406
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001407 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1408 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001409 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001410 qlcnic_enable_int(sds_ring);
1411 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001412 }
Sucheta Chakraborty22c8c932011-06-22 02:52:23 +00001413
1414 if (adapter->diag_test == QLCNIC_LOOPBACK_TEST) {
1415 adapter->ahw->loopback_state = 0;
1416 qlcnic_linkevent_request(adapter, 1);
1417 }
1418
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001419 set_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001420
1421 return 0;
1422}
1423
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001424/* Reset context in hardware only */
1425static int
1426qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1427{
1428 struct net_device *netdev = adapter->netdev;
1429
1430 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1431 return -EBUSY;
1432
1433 netif_device_detach(netdev);
1434
1435 qlcnic_down(adapter, netdev);
1436
1437 qlcnic_up(adapter, netdev);
1438
1439 netif_device_attach(netdev);
1440
1441 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1442 return 0;
1443}
1444
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001445int
1446qlcnic_reset_context(struct qlcnic_adapter *adapter)
1447{
1448 int err = 0;
1449 struct net_device *netdev = adapter->netdev;
1450
1451 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1452 return -EBUSY;
1453
1454 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1455
1456 netif_device_detach(netdev);
1457
1458 if (netif_running(netdev))
1459 __qlcnic_down(adapter, netdev);
1460
1461 qlcnic_detach(adapter);
1462
1463 if (netif_running(netdev)) {
1464 err = qlcnic_attach(adapter);
Anirban Chakraborty1d5c88e2012-03-23 06:32:34 +00001465 if (!err) {
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001466 __qlcnic_up(adapter, netdev);
Anirban Chakraborty1d5c88e2012-03-23 06:32:34 +00001467 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
1468 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001469 }
1470
1471 netif_device_attach(netdev);
1472 }
1473
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001474 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1475 return err;
1476}
1477
1478static int
1479qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001480 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001481{
1482 int err;
1483 struct pci_dev *pdev = adapter->pdev;
1484
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001485 adapter->mc_enabled = 0;
1486 adapter->max_mc_count = 38;
1487
1488 netdev->netdev_ops = &qlcnic_netdev_ops;
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001489 netdev->watchdog_timeo = 5*HZ;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001490
1491 qlcnic_change_mtu(netdev, netdev->mtu);
1492
1493 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1494
Michał Mirosław135d84a2011-04-19 03:03:57 +00001495 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1496 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
Anirban Chakrabortyac8d0c42010-07-09 13:14:58 +00001497
Michał Mirosław135d84a2011-04-19 03:03:57 +00001498 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1499 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1500 if (pci_using_dac)
1501 netdev->hw_features |= NETIF_F_HIGHDMA;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001502
Michał Mirosław135d84a2011-04-19 03:03:57 +00001503 netdev->vlan_features = netdev->hw_features;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001504
1505 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
Michał Mirosław135d84a2011-04-19 03:03:57 +00001506 netdev->hw_features |= NETIF_F_HW_VLAN_TX;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001507 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
Michał Mirosław135d84a2011-04-19 03:03:57 +00001508 netdev->hw_features |= NETIF_F_LRO;
1509
1510 netdev->features |= netdev->hw_features |
1511 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1512
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001513 netdev->irq = adapter->msix_entries[0].vector;
1514
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001515 err = register_netdev(netdev);
1516 if (err) {
1517 dev_err(&pdev->dev, "failed to register net device\n");
1518 return err;
1519 }
1520
1521 return 0;
1522}
1523
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001524static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1525{
1526 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1527 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1528 *pci_using_dac = 1;
1529 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1530 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1531 *pci_using_dac = 0;
1532 else {
1533 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1534 return -EIO;
1535 }
1536
1537 return 0;
1538}
1539
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001540static int
1541qlcnic_alloc_msix_entries(struct qlcnic_adapter *adapter, u16 count)
1542{
1543 adapter->msix_entries = kcalloc(count, sizeof(struct msix_entry),
1544 GFP_KERNEL);
1545
1546 if (adapter->msix_entries)
1547 return 0;
1548
1549 dev_err(&adapter->pdev->dev, "failed allocating msix_entries\n");
1550 return -ENOMEM;
1551}
1552
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001553static int __devinit
1554qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1555{
1556 struct net_device *netdev = NULL;
1557 struct qlcnic_adapter *adapter = NULL;
1558 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001559 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001560 uint8_t pci_using_dac;
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001561 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001562
1563 err = pci_enable_device(pdev);
1564 if (err)
1565 return err;
1566
1567 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1568 err = -ENODEV;
1569 goto err_out_disable_pdev;
1570 }
1571
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001572 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1573 if (err)
1574 goto err_out_disable_pdev;
1575
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001576 err = pci_request_regions(pdev, qlcnic_driver_name);
1577 if (err)
1578 goto err_out_disable_pdev;
1579
1580 pci_set_master(pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001581 pci_enable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001582
1583 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1584 if (!netdev) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001585 err = -ENOMEM;
1586 goto err_out_free_res;
1587 }
1588
1589 SET_NETDEV_DEV(netdev, &pdev->dev);
1590
1591 adapter = netdev_priv(netdev);
1592 adapter->netdev = netdev;
1593 adapter->pdev = pdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001594
1595 if (qlcnic_alloc_adapter_resources(adapter))
1596 goto err_out_free_netdev;
1597
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001598 adapter->dev_rst_time = jiffies;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001599 revision_id = pdev->revision;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001600 adapter->ahw->revision_id = revision_id;
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001601 adapter->mac_learn = qlcnic_mac_learn;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001602
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001603 rwlock_init(&adapter->ahw->crb_lock);
1604 mutex_init(&adapter->ahw->mem_lock);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001605
1606 spin_lock_init(&adapter->tx_clean_lock);
1607 INIT_LIST_HEAD(&adapter->mac_list);
1608
1609 err = qlcnic_setup_pci_map(adapter);
1610 if (err)
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001611 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001612
1613 /* This will be reset for mezz cards */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001614 adapter->portnum = adapter->ahw->pci_func;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001615
1616 err = qlcnic_get_board_info(adapter);
1617 if (err) {
1618 dev_err(&pdev->dev, "Error getting board config info.\n");
1619 goto err_out_iounmap;
1620 }
1621
Sony Chacko8cfdce02010-08-26 14:02:41 +00001622 err = qlcnic_setup_idc_param(adapter);
1623 if (err)
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +00001624 goto err_out_iounmap;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001625
Rajesh Borundia1dc0f3c2010-12-16 22:59:02 +00001626 adapter->flags |= QLCNIC_NEED_FLR;
Rajesh Borundiab0044bc2010-11-23 01:25:21 +00001627
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001628 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001629 if (err) {
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00001630 dev_err(&pdev->dev, "Loading fw failed. Please Reboot\n"
1631 "\t\tIf reboot doesn't help, try flashing the card\n");
1632 goto err_out_maintenance_mode;
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001633 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001634
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001635 if (qlcnic_read_mac_addr(adapter))
1636 dev_warn(&pdev->dev, "failed to read mac addr\n");
1637
1638 if (adapter->portnum == 0) {
1639 get_brd_name(adapter, brd_name);
1640
1641 pr_info("%s: %s Board Chip rev 0x%x\n",
1642 module_name(THIS_MODULE),
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001643 brd_name, adapter->ahw->revision_id);
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001644 }
1645
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001646 qlcnic_clear_stats(adapter);
1647
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001648 err = qlcnic_alloc_msix_entries(adapter, adapter->max_rx_ques);
1649 if (err)
1650 goto err_out_decr_ref;
1651
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001652 qlcnic_setup_intr(adapter);
1653
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001654 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001655 if (err)
1656 goto err_out_disable_msi;
1657
1658 pci_set_drvdata(pdev, adapter);
1659
1660 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1661
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001662 switch (adapter->ahw->port_type) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001663 case QLCNIC_GBE:
1664 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1665 adapter->netdev->name);
1666 break;
1667 case QLCNIC_XGBE:
1668 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1669 adapter->netdev->name);
1670 break;
1671 }
1672
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001673 if (adapter->mac_learn)
1674 qlcnic_alloc_lb_filters_mem(adapter);
1675
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001676 qlcnic_create_diag_entries(adapter);
1677
1678 return 0;
1679
1680err_out_disable_msi:
1681 qlcnic_teardown_intr(adapter);
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001682 kfree(adapter->msix_entries);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001683
1684err_out_decr_ref:
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001685 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001686
1687err_out_iounmap:
1688 qlcnic_cleanup_pci_map(adapter);
1689
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001690err_out_free_hw:
1691 qlcnic_free_adapter_resources(adapter);
1692
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001693err_out_free_netdev:
1694 free_netdev(netdev);
1695
1696err_out_free_res:
1697 pci_release_regions(pdev);
1698
1699err_out_disable_pdev:
1700 pci_set_drvdata(pdev, NULL);
1701 pci_disable_device(pdev);
1702 return err;
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00001703
1704err_out_maintenance_mode:
1705 netdev->netdev_ops = &qlcnic_netdev_failed_ops;
1706 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_failed_ops);
1707 err = register_netdev(netdev);
1708 if (err) {
1709 dev_err(&pdev->dev, "failed to register net device\n");
1710 goto err_out_decr_ref;
1711 }
1712 pci_set_drvdata(pdev, adapter);
1713 qlcnic_create_diag_entries(adapter);
1714 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001715}
1716
1717static void __devexit qlcnic_remove(struct pci_dev *pdev)
1718{
1719 struct qlcnic_adapter *adapter;
1720 struct net_device *netdev;
1721
1722 adapter = pci_get_drvdata(pdev);
1723 if (adapter == NULL)
1724 return;
1725
1726 netdev = adapter->netdev;
1727
1728 qlcnic_cancel_fw_work(adapter);
1729
1730 unregister_netdev(netdev);
1731
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001732 qlcnic_detach(adapter);
1733
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001734 if (adapter->npars != NULL)
1735 kfree(adapter->npars);
1736 if (adapter->eswitch != NULL)
1737 kfree(adapter->eswitch);
1738
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001739 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001740
1741 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1742
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001743 qlcnic_free_lb_filters_mem(adapter);
1744
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001745 qlcnic_teardown_intr(adapter);
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001746 kfree(adapter->msix_entries);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001747
1748 qlcnic_remove_diag_entries(adapter);
1749
1750 qlcnic_cleanup_pci_map(adapter);
1751
1752 qlcnic_release_firmware(adapter);
1753
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001754 pci_disable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001755 pci_release_regions(pdev);
1756 pci_disable_device(pdev);
1757 pci_set_drvdata(pdev, NULL);
1758
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001759 qlcnic_free_adapter_resources(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001760 free_netdev(netdev);
1761}
1762static int __qlcnic_shutdown(struct pci_dev *pdev)
1763{
1764 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1765 struct net_device *netdev = adapter->netdev;
1766 int retval;
1767
1768 netif_device_detach(netdev);
1769
1770 qlcnic_cancel_fw_work(adapter);
1771
1772 if (netif_running(netdev))
1773 qlcnic_down(adapter, netdev);
1774
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001775 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001776
1777 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1778
1779 retval = pci_save_state(pdev);
1780 if (retval)
1781 return retval;
1782
1783 if (qlcnic_wol_supported(adapter)) {
1784 pci_enable_wake(pdev, PCI_D3cold, 1);
1785 pci_enable_wake(pdev, PCI_D3hot, 1);
1786 }
1787
1788 return 0;
1789}
1790
1791static void qlcnic_shutdown(struct pci_dev *pdev)
1792{
1793 if (__qlcnic_shutdown(pdev))
1794 return;
1795
1796 pci_disable_device(pdev);
1797}
1798
1799#ifdef CONFIG_PM
1800static int
1801qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1802{
1803 int retval;
1804
1805 retval = __qlcnic_shutdown(pdev);
1806 if (retval)
1807 return retval;
1808
1809 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1810 return 0;
1811}
1812
1813static int
1814qlcnic_resume(struct pci_dev *pdev)
1815{
1816 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1817 struct net_device *netdev = adapter->netdev;
1818 int err;
1819
1820 err = pci_enable_device(pdev);
1821 if (err)
1822 return err;
1823
1824 pci_set_power_state(pdev, PCI_D0);
1825 pci_set_master(pdev);
1826 pci_restore_state(pdev);
1827
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001828 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001829 if (err) {
1830 dev_err(&pdev->dev, "failed to start firmware\n");
1831 return err;
1832 }
1833
1834 if (netif_running(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001835 err = qlcnic_up(adapter, netdev);
1836 if (err)
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001837 goto done;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001838
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00001839 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001840 }
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001841done:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001842 netif_device_attach(netdev);
1843 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1844 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001845}
1846#endif
1847
1848static int qlcnic_open(struct net_device *netdev)
1849{
1850 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00001851 u32 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001852 int err;
1853
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00001854 if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD)) {
1855 netdev_err(netdev, "Device in FAILED state\n");
1856 return -EIO;
1857 }
1858
Amit Kumar Salechac55ad8e2011-06-22 02:52:21 +00001859 netif_carrier_off(netdev);
1860
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001861 err = qlcnic_attach(adapter);
1862 if (err)
1863 return err;
1864
1865 err = __qlcnic_up(adapter, netdev);
1866 if (err)
1867 goto err_out;
1868
1869 netif_start_queue(netdev);
1870
1871 return 0;
1872
1873err_out:
1874 qlcnic_detach(adapter);
1875 return err;
1876}
1877
1878/*
1879 * qlcnic_close - Disables a network interface entry point
1880 */
1881static int qlcnic_close(struct net_device *netdev)
1882{
1883 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1884
1885 __qlcnic_down(adapter, netdev);
1886 return 0;
1887}
1888
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001889void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001890{
1891 void *head;
1892 int i;
1893
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001894 if (adapter->fhash.fmax && adapter->fhash.fhead)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001895 return;
1896
1897 spin_lock_init(&adapter->mac_learn_lock);
1898
1899 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1900 GFP_KERNEL);
1901 if (!head)
1902 return;
1903
1904 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
Joe Perches43d620c2011-06-16 19:08:06 +00001905 adapter->fhash.fhead = head;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001906
1907 for (i = 0; i < adapter->fhash.fmax; i++)
1908 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1909}
1910
1911static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1912{
1913 if (adapter->fhash.fmax && adapter->fhash.fhead)
1914 kfree(adapter->fhash.fhead);
1915
1916 adapter->fhash.fhead = NULL;
1917 adapter->fhash.fmax = 0;
1918}
1919
1920static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001921 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001922{
1923 struct cmd_desc_type0 *hwdesc;
1924 struct qlcnic_nic_req *req;
1925 struct qlcnic_mac_req *mac_req;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001926 struct qlcnic_vlan_req *vlan_req;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001927 u32 producer;
1928 u64 word;
1929
1930 producer = tx_ring->producer;
1931 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1932
1933 req = (struct qlcnic_nic_req *)hwdesc;
1934 memset(req, 0, sizeof(struct qlcnic_nic_req));
1935 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1936
1937 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1938 req->req_hdr = cpu_to_le64(word);
1939
1940 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001941 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001942 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1943
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001944 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1945 vlan_req->vlan_id = vlan_id;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001946
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001947 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001948 smp_mb();
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001949}
1950
1951#define QLCNIC_MAC_HASH(MAC)\
1952 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1953
1954static void
1955qlcnic_send_filter(struct qlcnic_adapter *adapter,
1956 struct qlcnic_host_tx_ring *tx_ring,
1957 struct cmd_desc_type0 *first_desc,
1958 struct sk_buff *skb)
1959{
1960 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1961 struct qlcnic_filter *fil, *tmp_fil;
1962 struct hlist_node *tmp_hnode, *n;
1963 struct hlist_head *head;
1964 u64 src_addr = 0;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001965 __le16 vlan_id = 0;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001966 u8 hindex;
1967
Joe Perches2e42e472012-05-09 17:17:46 +00001968 if (ether_addr_equal(phdr->h_source, adapter->mac_addr))
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001969 return;
1970
1971 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1972 return;
1973
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001974 /* Only NPAR capable devices support vlan based learning*/
1975 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1976 vlan_id = first_desc->vlan_TCI;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001977 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1978 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1979 head = &(adapter->fhash.fhead[hindex]);
1980
1981 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001982 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1983 tmp_fil->vlan_id == vlan_id) {
amit salechae5edb7b2010-10-26 17:53:07 +00001984
1985 if (jiffies >
1986 (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1987 qlcnic_change_filter(adapter, src_addr, vlan_id,
1988 tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001989 tmp_fil->ftime = jiffies;
1990 return;
1991 }
1992 }
1993
1994 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1995 if (!fil)
1996 return;
1997
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001998 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001999
2000 fil->ftime = jiffies;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00002001 fil->vlan_id = vlan_id;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002002 memcpy(fil->faddr, &src_addr, ETH_ALEN);
2003 spin_lock(&adapter->mac_learn_lock);
2004 hlist_add_head(&(fil->fnode), head);
2005 adapter->fhash.fnum++;
2006 spin_unlock(&adapter->mac_learn_lock);
2007}
2008
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002009static int
2010qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002011 struct cmd_desc_type0 *first_desc,
2012 struct sk_buff *skb)
2013{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002014 u8 opcode = 0, hdr_len = 0;
2015 u16 flags = 0, vlan_tci = 0;
2016 int copied, offset, copy_len;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002017 struct cmd_desc_type0 *hwdesc;
2018 struct vlan_ethhdr *vh;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002019 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2020 u16 protocol = ntohs(skb->protocol);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00002021 u32 producer = tx_ring->producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002022
2023 if (protocol == ETH_P_8021Q) {
2024 vh = (struct vlan_ethhdr *)skb->data;
2025 flags = FLAGS_VLAN_TAGGED;
2026 vlan_tci = vh->h_vlan_TCI;
2027 } else if (vlan_tx_tag_present(skb)) {
2028 flags = FLAGS_VLAN_OOB;
2029 vlan_tci = vlan_tx_tag_get(skb);
2030 }
2031 if (unlikely(adapter->pvid)) {
2032 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
2033 return -EIO;
2034 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
2035 goto set_flags;
2036
2037 flags = FLAGS_VLAN_OOB;
2038 vlan_tci = adapter->pvid;
2039 }
2040set_flags:
2041 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
2042 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002043
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00002044 if (*(skb->data) & BIT_0) {
2045 flags |= BIT_0;
2046 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
2047 }
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002048 opcode = TX_ETHER_PKT;
2049 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002050 skb_shinfo(skb)->gso_size > 0) {
2051
2052 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2053
2054 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2055 first_desc->total_hdr_length = hdr_len;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002056
2057 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
2058
2059 /* For LSO, we need to copy the MAC/IP/TCP headers into
2060 * the descriptor ring */
2061 copied = 0;
2062 offset = 2;
2063
2064 if (flags & FLAGS_VLAN_OOB) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002065 first_desc->total_hdr_length += VLAN_HLEN;
2066 first_desc->tcp_hdr_offset = VLAN_HLEN;
2067 first_desc->ip_hdr_offset = VLAN_HLEN;
2068 /* Only in case of TSO on vlan device */
2069 flags |= FLAGS_VLAN_TAGGED;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002070
2071 /* Create a TSO vlan header template for firmware */
2072
2073 hwdesc = &tx_ring->desc_head[producer];
2074 tx_ring->cmd_buf_arr[producer].skb = NULL;
2075
2076 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2077 offset, hdr_len + VLAN_HLEN);
2078
2079 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
2080 skb_copy_from_linear_data(skb, vh, 12);
2081 vh->h_vlan_proto = htons(ETH_P_8021Q);
2082 vh->h_vlan_TCI = htons(vlan_tci);
2083
2084 skb_copy_from_linear_data_offset(skb, 12,
2085 (char *)vh + 16, copy_len - 16);
2086
2087 copied = copy_len - VLAN_HLEN;
2088 offset = 0;
2089
2090 producer = get_next_index(producer, tx_ring->num_desc);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002091 }
2092
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002093 while (copied < hdr_len) {
2094
2095 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2096 offset, (hdr_len - copied));
2097
2098 hwdesc = &tx_ring->desc_head[producer];
2099 tx_ring->cmd_buf_arr[producer].skb = NULL;
2100
2101 skb_copy_from_linear_data_offset(skb, copied,
2102 (char *) hwdesc + offset, copy_len);
2103
2104 copied += copy_len;
2105 offset = 0;
2106
2107 producer = get_next_index(producer, tx_ring->num_desc);
2108 }
2109
2110 tx_ring->producer = producer;
2111 smp_mb();
2112 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002113
2114 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2115 u8 l4proto;
2116
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002117 if (protocol == ETH_P_IP) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002118 l4proto = ip_hdr(skb)->protocol;
2119
2120 if (l4proto == IPPROTO_TCP)
2121 opcode = TX_TCP_PKT;
2122 else if (l4proto == IPPROTO_UDP)
2123 opcode = TX_UDP_PKT;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002124 } else if (protocol == ETH_P_IPV6) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002125 l4proto = ipv6_hdr(skb)->nexthdr;
2126
2127 if (l4proto == IPPROTO_TCP)
2128 opcode = TX_TCPV6_PKT;
2129 else if (l4proto == IPPROTO_UDP)
2130 opcode = TX_UDPV6_PKT;
2131 }
2132 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002133 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2134 first_desc->ip_hdr_offset += skb_network_offset(skb);
2135 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2136
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002137 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002138}
2139
2140static int
2141qlcnic_map_tx_skb(struct pci_dev *pdev,
2142 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2143{
2144 struct qlcnic_skb_frag *nf;
2145 struct skb_frag_struct *frag;
2146 int i, nr_frags;
2147 dma_addr_t map;
2148
2149 nr_frags = skb_shinfo(skb)->nr_frags;
2150 nf = &pbuf->frag_array[0];
2151
2152 map = pci_map_single(pdev, skb->data,
2153 skb_headlen(skb), PCI_DMA_TODEVICE);
2154 if (pci_dma_mapping_error(pdev, map))
2155 goto out_err;
2156
2157 nf->dma = map;
2158 nf->length = skb_headlen(skb);
2159
2160 for (i = 0; i < nr_frags; i++) {
2161 frag = &skb_shinfo(skb)->frags[i];
2162 nf = &pbuf->frag_array[i+1];
2163
Eric Dumazet9e903e02011-10-18 21:00:24 +00002164 map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01002165 DMA_TO_DEVICE);
2166 if (dma_mapping_error(&pdev->dev, map))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002167 goto unwind;
2168
2169 nf->dma = map;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002170 nf->length = skb_frag_size(frag);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002171 }
2172
2173 return 0;
2174
2175unwind:
2176 while (--i >= 0) {
2177 nf = &pbuf->frag_array[i+1];
2178 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2179 }
2180
2181 nf = &pbuf->frag_array[0];
2182 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2183
2184out_err:
2185 return -ENOMEM;
2186}
2187
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002188static void
2189qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2190 struct qlcnic_cmd_buffer *pbuf)
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002191{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002192 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2193 int nr_frags = skb_shinfo(skb)->nr_frags;
2194 int i;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002195
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002196 for (i = 0; i < nr_frags; i++) {
2197 nf = &pbuf->frag_array[i+1];
2198 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002199 }
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002200
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002201 nf = &pbuf->frag_array[0];
2202 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
Sucheta Chakraborty5b446c62011-06-03 05:52:18 +00002203 pbuf->skb = NULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002204}
2205
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002206static inline void
2207qlcnic_clear_cmddesc(u64 *desc)
2208{
2209 desc[0] = 0ULL;
2210 desc[2] = 0ULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002211 desc[7] = 0ULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002212}
2213
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00002214netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002215qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2216{
2217 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2218 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2219 struct qlcnic_cmd_buffer *pbuf;
2220 struct qlcnic_skb_frag *buffrag;
2221 struct cmd_desc_type0 *hwdesc, *first_desc;
2222 struct pci_dev *pdev;
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002223 struct ethhdr *phdr;
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002224 int delta = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002225 int i, k;
2226
2227 u32 producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002228 int frag_count;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002229 u32 num_txd = tx_ring->num_desc;
2230
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00002231 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2232 netif_stop_queue(netdev);
2233 return NETDEV_TX_BUSY;
2234 }
2235
Sony Chackofe4d4342010-08-19 05:08:27 +00002236 if (adapter->flags & QLCNIC_MACSPOOF) {
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002237 phdr = (struct ethhdr *)skb->data;
Joe Perches2e42e472012-05-09 17:17:46 +00002238 if (!ether_addr_equal(phdr->h_source, adapter->mac_addr))
Sony Chackofe4d4342010-08-19 05:08:27 +00002239 goto drop_packet;
2240 }
2241
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002242 frag_count = skb_shinfo(skb)->nr_frags + 1;
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002243 /* 14 frags supported for normal packet and
2244 * 32 frags supported for TSO packet
2245 */
2246 if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
2247
2248 for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
Eric Dumazet9e903e02011-10-18 21:00:24 +00002249 delta += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002250
2251 if (!__pskb_pull_tail(skb, delta))
2252 goto drop_packet;
2253
2254 frag_count = 1 + skb_shinfo(skb)->nr_frags;
2255 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002256
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002257 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002258 netif_stop_queue(netdev);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002259 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2260 netif_start_queue(netdev);
2261 else {
2262 adapter->stats.xmit_off++;
2263 return NETDEV_TX_BUSY;
2264 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002265 }
2266
2267 producer = tx_ring->producer;
2268 pbuf = &tx_ring->cmd_buf_arr[producer];
2269
2270 pdev = adapter->pdev;
2271
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002272 first_desc = hwdesc = &tx_ring->desc_head[producer];
2273 qlcnic_clear_cmddesc((u64 *)hwdesc);
2274
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002275 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2276 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002277 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002278 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002279
2280 pbuf->skb = skb;
2281 pbuf->frag_count = frag_count;
2282
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002283 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2284 qlcnic_set_tx_port(first_desc, adapter->portnum);
2285
2286 for (i = 0; i < frag_count; i++) {
2287
2288 k = i % 4;
2289
2290 if ((k == 0) && (i > 0)) {
2291 /* move to next desc.*/
2292 producer = get_next_index(producer, num_txd);
2293 hwdesc = &tx_ring->desc_head[producer];
2294 qlcnic_clear_cmddesc((u64 *)hwdesc);
2295 tx_ring->cmd_buf_arr[producer].skb = NULL;
2296 }
2297
2298 buffrag = &pbuf->frag_array[i];
2299
2300 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2301 switch (k) {
2302 case 0:
2303 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2304 break;
2305 case 1:
2306 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2307 break;
2308 case 2:
2309 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2310 break;
2311 case 3:
2312 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2313 break;
2314 }
2315 }
2316
2317 tx_ring->producer = get_next_index(producer, num_txd);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002318 smp_mb();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002319
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002320 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2321 goto unwind_buff;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002322
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00002323 if (adapter->mac_learn)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002324 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2325
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002326 adapter->stats.txbytes += skb->len;
2327 adapter->stats.xmitcalled++;
2328
Sucheta Chakrabortyf127f472011-07-14 03:16:53 +00002329 qlcnic_update_cmd_producer(adapter, tx_ring);
2330
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002331 return NETDEV_TX_OK;
2332
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002333unwind_buff:
2334 qlcnic_unmap_buffers(pdev, skb, pbuf);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002335drop_packet:
2336 adapter->stats.txdropped++;
2337 dev_kfree_skb_any(skb);
2338 return NETDEV_TX_OK;
2339}
2340
2341static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2342{
2343 struct net_device *netdev = adapter->netdev;
2344 u32 temp, temp_state, temp_val;
2345 int rv = 0;
2346
2347 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2348
2349 temp_state = qlcnic_get_temp_state(temp);
2350 temp_val = qlcnic_get_temp_val(temp);
2351
2352 if (temp_state == QLCNIC_TEMP_PANIC) {
2353 dev_err(&netdev->dev,
2354 "Device temperature %d degrees C exceeds"
2355 " maximum allowed. Hardware has been shut down.\n",
2356 temp_val);
2357 rv = 1;
2358 } else if (temp_state == QLCNIC_TEMP_WARN) {
2359 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2360 dev_err(&netdev->dev,
2361 "Device temperature %d degrees C "
2362 "exceeds operating range."
2363 " Immediate action needed.\n",
2364 temp_val);
2365 }
2366 } else {
2367 if (adapter->temp == QLCNIC_TEMP_WARN) {
2368 dev_info(&netdev->dev,
2369 "Device temperature is now %d degrees C"
2370 " in normal range.\n", temp_val);
2371 }
2372 }
2373 adapter->temp = temp_state;
2374 return rv;
2375}
2376
2377void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2378{
2379 struct net_device *netdev = adapter->netdev;
2380
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002381 if (adapter->ahw->linkup && !linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002382 netdev_info(netdev, "NIC Link is down\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002383 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002384 if (netif_running(netdev)) {
2385 netif_carrier_off(netdev);
2386 netif_stop_queue(netdev);
2387 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002388 } else if (!adapter->ahw->linkup && linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002389 netdev_info(netdev, "NIC Link is up\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002390 adapter->ahw->linkup = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002391 if (netif_running(netdev)) {
2392 netif_carrier_on(netdev);
2393 netif_wake_queue(netdev);
2394 }
2395 }
2396}
2397
2398static void qlcnic_tx_timeout(struct net_device *netdev)
2399{
2400 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2401
2402 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2403 return;
2404
2405 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002406
2407 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002408 adapter->need_fw_reset = 1;
2409 else
2410 adapter->reset_context = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002411}
2412
2413static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2414{
2415 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2416 struct net_device_stats *stats = &netdev->stats;
2417
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002418 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2419 stats->tx_packets = adapter->stats.xmitfinished;
Sucheta Chakraborty7e382592010-05-17 01:22:10 +00002420 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002421 stats->tx_bytes = adapter->stats.txbytes;
2422 stats->rx_dropped = adapter->stats.rxdropped;
2423 stats->tx_dropped = adapter->stats.txdropped;
2424
2425 return stats;
2426}
2427
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002428static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002429{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002430 u32 status;
2431
2432 status = readl(adapter->isr_int_vec);
2433
2434 if (!(status & adapter->int_vec_bit))
2435 return IRQ_NONE;
2436
2437 /* check interrupt state machine, to be sure */
2438 status = readl(adapter->crb_int_state_reg);
2439 if (!ISR_LEGACY_INT_TRIGGERED(status))
2440 return IRQ_NONE;
2441
2442 writel(0xffffffff, adapter->tgt_status_reg);
2443 /* read twice to ensure write is flushed */
2444 readl(adapter->isr_int_vec);
2445 readl(adapter->isr_int_vec);
2446
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002447 return IRQ_HANDLED;
2448}
2449
2450static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2451{
2452 struct qlcnic_host_sds_ring *sds_ring = data;
2453 struct qlcnic_adapter *adapter = sds_ring->adapter;
2454
2455 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2456 goto done;
2457 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2458 writel(0xffffffff, adapter->tgt_status_reg);
2459 goto done;
2460 }
2461
2462 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2463 return IRQ_NONE;
2464
2465done:
2466 adapter->diag_cnt++;
2467 qlcnic_enable_int(sds_ring);
2468 return IRQ_HANDLED;
2469}
2470
2471static irqreturn_t qlcnic_intr(int irq, void *data)
2472{
2473 struct qlcnic_host_sds_ring *sds_ring = data;
2474 struct qlcnic_adapter *adapter = sds_ring->adapter;
2475
2476 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2477 return IRQ_NONE;
2478
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002479 napi_schedule(&sds_ring->napi);
2480
2481 return IRQ_HANDLED;
2482}
2483
2484static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2485{
2486 struct qlcnic_host_sds_ring *sds_ring = data;
2487 struct qlcnic_adapter *adapter = sds_ring->adapter;
2488
2489 /* clear interrupt */
2490 writel(0xffffffff, adapter->tgt_status_reg);
2491
2492 napi_schedule(&sds_ring->napi);
2493 return IRQ_HANDLED;
2494}
2495
2496static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2497{
2498 struct qlcnic_host_sds_ring *sds_ring = data;
2499
2500 napi_schedule(&sds_ring->napi);
2501 return IRQ_HANDLED;
2502}
2503
2504static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2505{
2506 u32 sw_consumer, hw_consumer;
2507 int count = 0, i;
2508 struct qlcnic_cmd_buffer *buffer;
2509 struct pci_dev *pdev = adapter->pdev;
2510 struct net_device *netdev = adapter->netdev;
2511 struct qlcnic_skb_frag *frag;
2512 int done;
2513 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2514
2515 if (!spin_trylock(&adapter->tx_clean_lock))
2516 return 1;
2517
2518 sw_consumer = tx_ring->sw_consumer;
2519 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2520
2521 while (sw_consumer != hw_consumer) {
2522 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2523 if (buffer->skb) {
2524 frag = &buffer->frag_array[0];
2525 pci_unmap_single(pdev, frag->dma, frag->length,
2526 PCI_DMA_TODEVICE);
2527 frag->dma = 0ULL;
2528 for (i = 1; i < buffer->frag_count; i++) {
2529 frag++;
2530 pci_unmap_page(pdev, frag->dma, frag->length,
2531 PCI_DMA_TODEVICE);
2532 frag->dma = 0ULL;
2533 }
2534
2535 adapter->stats.xmitfinished++;
2536 dev_kfree_skb_any(buffer->skb);
2537 buffer->skb = NULL;
2538 }
2539
2540 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2541 if (++count >= MAX_STATUS_HANDLE)
2542 break;
2543 }
2544
2545 if (count && netif_running(netdev)) {
2546 tx_ring->sw_consumer = sw_consumer;
2547
2548 smp_mb();
2549
2550 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002551 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2552 netif_wake_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00002553 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002554 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002555 }
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002556 adapter->tx_timeo_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002557 }
2558 /*
2559 * If everything is freed up to consumer then check if the ring is full
2560 * If the ring is full then check if more needs to be freed and
2561 * schedule the call back again.
2562 *
2563 * This happens when there are 2 CPUs. One could be freeing and the
2564 * other filling it. If the ring is full when we get out of here and
2565 * the card has already interrupted the host then the host can miss the
2566 * interrupt.
2567 *
2568 * There is still a possible race condition and the host could miss an
2569 * interrupt. The card has to take care of this.
2570 */
2571 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2572 done = (sw_consumer == hw_consumer);
2573 spin_unlock(&adapter->tx_clean_lock);
2574
2575 return done;
2576}
2577
2578static int qlcnic_poll(struct napi_struct *napi, int budget)
2579{
2580 struct qlcnic_host_sds_ring *sds_ring =
2581 container_of(napi, struct qlcnic_host_sds_ring, napi);
2582
2583 struct qlcnic_adapter *adapter = sds_ring->adapter;
2584
2585 int tx_complete;
2586 int work_done;
2587
2588 tx_complete = qlcnic_process_cmd_ring(adapter);
2589
2590 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2591
2592 if ((work_done < budget) && tx_complete) {
2593 napi_complete(&sds_ring->napi);
2594 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2595 qlcnic_enable_int(sds_ring);
2596 }
2597
2598 return work_done;
2599}
2600
schacko8f891382010-06-17 02:56:40 +00002601static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2602{
2603 struct qlcnic_host_sds_ring *sds_ring =
2604 container_of(napi, struct qlcnic_host_sds_ring, napi);
2605
2606 struct qlcnic_adapter *adapter = sds_ring->adapter;
2607 int work_done;
2608
2609 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2610
2611 if (work_done < budget) {
2612 napi_complete(&sds_ring->napi);
2613 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2614 qlcnic_enable_int(sds_ring);
2615 }
2616
2617 return work_done;
2618}
2619
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002620#ifdef CONFIG_NET_POLL_CONTROLLER
2621static void qlcnic_poll_controller(struct net_device *netdev)
2622{
Yinglin Luanbf827912010-08-22 21:57:56 +00002623 int ring;
2624 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002625 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002626 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Yinglin Luanbf827912010-08-22 21:57:56 +00002627
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002628 disable_irq(adapter->irq);
Yinglin Luanbf827912010-08-22 21:57:56 +00002629 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2630 sds_ring = &recv_ctx->sds_rings[ring];
2631 qlcnic_intr(adapter->irq, sds_ring);
2632 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002633 enable_irq(adapter->irq);
2634}
2635#endif
2636
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002637static void
2638qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2639{
2640 u32 val;
2641
2642 val = adapter->portnum & 0xf;
2643 val |= encoding << 7;
2644 val |= (jiffies - adapter->dev_rst_time) << 8;
2645
2646 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2647 adapter->dev_rst_time = jiffies;
2648}
2649
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002650static int
2651qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002652{
2653 u32 val;
2654
2655 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2656 state != QLCNIC_DEV_NEED_QUISCENT);
2657
2658 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002659 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002660
2661 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2662
2663 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002664 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002665 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002666 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002667
2668 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2669
2670 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002671
2672 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002673}
2674
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002675static int
2676qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2677{
2678 u32 val;
2679
2680 if (qlcnic_api_lock(adapter))
2681 return -EBUSY;
2682
2683 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002684 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002685 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2686
2687 qlcnic_api_unlock(adapter);
2688
2689 return 0;
2690}
2691
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002692static void
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002693qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002694{
2695 u32 val;
2696
2697 if (qlcnic_api_lock(adapter))
2698 goto err;
2699
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002700 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002701 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002702 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002703
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002704 if (failed) {
2705 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2706 dev_info(&adapter->pdev->dev,
2707 "Device state set to Failed. Please Reboot\n");
2708 } else if (!(val & 0x11111111))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002709 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2710
2711 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002712 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002713 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2714
2715 qlcnic_api_unlock(adapter);
2716err:
2717 adapter->fw_fail_cnt = 0;
Sritej Velaga032a13c2011-07-29 13:30:27 +00002718 adapter->flags &= ~QLCNIC_FW_HANG;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002719 clear_bit(__QLCNIC_START_FW, &adapter->state);
2720 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2721}
2722
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002723/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002724static int
2725qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2726{
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002727 int act, state, active_mask;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002728
2729 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002730 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002731
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002732 if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
2733 active_mask = (~(1 << (adapter->ahw->pci_func * 4)));
2734 act = act & active_mask;
2735 }
2736
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002737 if (((state & 0x11111111) == (act & 0x11111111)) ||
2738 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2739 return 0;
2740 else
2741 return 1;
2742}
2743
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002744static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2745{
2746 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2747
2748 if (val != QLCNIC_DRV_IDC_VER) {
2749 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2750 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2751 }
2752
2753 return 0;
2754}
2755
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002756static int
2757qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2758{
2759 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002760 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002761 u8 portnum = adapter->portnum;
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002762 u8 ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002763
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002764 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2765 return 1;
2766
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002767 if (qlcnic_api_lock(adapter))
2768 return -1;
2769
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002770 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002771 if (!(val & (1 << (portnum * 4)))) {
2772 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002773 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002774 }
2775
2776 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002777 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002778
2779 switch (prev_state) {
2780 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002781 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002782 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002783 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002784 qlcnic_api_unlock(adapter);
2785 return 1;
2786
2787 case QLCNIC_DEV_READY:
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002788 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002789 qlcnic_api_unlock(adapter);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002790 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002791
2792 case QLCNIC_DEV_NEED_RESET:
2793 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002794 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002795 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2796 break;
2797
2798 case QLCNIC_DEV_NEED_QUISCENT:
2799 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002800 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002801 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2802 break;
2803
2804 case QLCNIC_DEV_FAILED:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00002805 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002806 qlcnic_api_unlock(adapter);
2807 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002808
2809 case QLCNIC_DEV_INITIALIZING:
2810 case QLCNIC_DEV_QUISCENT:
2811 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002812 }
2813
2814 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002815
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002816 do {
2817 msleep(1000);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002818 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2819
2820 if (prev_state == QLCNIC_DEV_QUISCENT)
2821 continue;
2822 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002823
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002824 if (!dev_init_timeo) {
2825 dev_err(&adapter->pdev->dev,
2826 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002827 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002828 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002829
2830 if (qlcnic_api_lock(adapter))
2831 return -1;
2832
2833 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002834 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002835 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2836
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002837 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002838 qlcnic_api_unlock(adapter);
2839
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002840 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002841}
2842
2843static void
2844qlcnic_fwinit_work(struct work_struct *work)
2845{
2846 struct qlcnic_adapter *adapter = container_of(work,
2847 struct qlcnic_adapter, fw_work.work);
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002848 u32 dev_state = 0xf;
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002849 u32 val;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002850
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002851 if (qlcnic_api_lock(adapter))
2852 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002853
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002854 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002855 if (dev_state == QLCNIC_DEV_QUISCENT ||
2856 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002857 qlcnic_api_unlock(adapter);
2858 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2859 FW_POLL_DELAY * 2);
2860 return;
2861 }
2862
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002863 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002864 qlcnic_api_unlock(adapter);
2865 goto wait_npar;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002866 }
2867
Sritej Velaga16e3cf72011-10-28 12:57:11 +00002868 if (dev_state == QLCNIC_DEV_INITIALIZING ||
2869 dev_state == QLCNIC_DEV_READY) {
2870 dev_info(&adapter->pdev->dev, "Detected state change from "
2871 "DEV_NEED_RESET, skipping ack check\n");
2872 goto skip_ack_check;
2873 }
2874
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002875 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
Sritej Velaga16e3cf72011-10-28 12:57:11 +00002876 dev_info(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002877 adapter->reset_ack_timeo);
2878 goto skip_ack_check;
2879 }
2880
2881 if (!qlcnic_check_drv_state(adapter)) {
2882skip_ack_check:
2883 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002884
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002885 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2886 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2887 QLCNIC_DEV_INITIALIZING);
2888 set_bit(__QLCNIC_START_FW, &adapter->state);
2889 QLCDB(adapter, DRV, "Restarting fw\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002890 qlcnic_idc_debug_info(adapter, 0);
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002891 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2892 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2893 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002894 }
2895
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002896 qlcnic_api_unlock(adapter);
2897
Anirban Chakraborty287e38a2011-06-22 02:52:18 +00002898 rtnl_lock();
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002899 if (adapter->ahw->fw_dump.enable &&
2900 (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
Anirban Chakraborty9d6a6442011-06-22 02:52:22 +00002901 QLCDB(adapter, DRV, "Take FW dump\n");
2902 qlcnic_dump_fw(adapter);
Sritej Velaga032a13c2011-07-29 13:30:27 +00002903 adapter->flags |= QLCNIC_FW_HANG;
Anirban Chakraborty9d6a6442011-06-22 02:52:22 +00002904 }
Anirban Chakraborty287e38a2011-06-22 02:52:18 +00002905 rtnl_unlock();
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002906
2907 adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002908 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002909 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002910 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002911 return;
2912 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002913 goto err_ret;
2914 }
2915
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002916 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002917
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002918wait_npar:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002919 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002920 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002921
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002922 switch (dev_state) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002923 case QLCNIC_DEV_READY:
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002924 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002925 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002926 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002927 return;
2928 }
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002929 case QLCNIC_DEV_FAILED:
2930 break;
2931 default:
2932 qlcnic_schedule_work(adapter,
2933 qlcnic_fwinit_work, FW_POLL_DELAY);
2934 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002935 }
2936
2937err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002938 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2939 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002940 netif_device_attach(adapter->netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002941 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002942}
2943
2944static void
2945qlcnic_detach_work(struct work_struct *work)
2946{
2947 struct qlcnic_adapter *adapter = container_of(work,
2948 struct qlcnic_adapter, fw_work.work);
2949 struct net_device *netdev = adapter->netdev;
2950 u32 status;
2951
2952 netif_device_detach(netdev);
2953
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002954 /* Dont grab rtnl lock during Quiscent mode */
2955 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2956 if (netif_running(netdev))
2957 __qlcnic_down(adapter, netdev);
2958 } else
2959 qlcnic_down(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002960
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002961 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2962
Sony Chacko44f65b22011-08-29 12:50:26 +00002963 if (status & QLCNIC_RCODE_FATAL_ERROR) {
2964 dev_err(&adapter->pdev->dev,
2965 "Detaching the device: peg halt status1=0x%x\n",
2966 status);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002967
Sony Chacko44f65b22011-08-29 12:50:26 +00002968 if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
2969 dev_err(&adapter->pdev->dev,
2970 "On board active cooling fan failed. "
2971 "Device has been halted.\n");
2972 dev_err(&adapter->pdev->dev,
2973 "Replace the adapter.\n");
2974 }
2975
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002976 goto err_ret;
Sony Chacko44f65b22011-08-29 12:50:26 +00002977 }
2978
2979 if (adapter->temp == QLCNIC_TEMP_PANIC) {
2980 dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
2981 adapter->temp);
2982 goto err_ret;
2983 }
2984
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002985 /* Dont ack if this instance is the reset owner */
2986 if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
Sony Chacko44f65b22011-08-29 12:50:26 +00002987 if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
2988 dev_err(&adapter->pdev->dev,
2989 "Failed to set driver state,"
2990 "detaching the device.\n");
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002991 goto err_ret;
Sony Chacko44f65b22011-08-29 12:50:26 +00002992 }
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002993 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002994
2995 adapter->fw_wait_cnt = 0;
2996
2997 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2998
2999 return;
3000
3001err_ret:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00003002 netif_device_attach(netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00003003 qlcnic_clr_all_drv_state(adapter, 1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003004}
3005
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003006/*Transit NPAR state to NON Operational */
3007static void
3008qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
3009{
3010 u32 state;
3011
3012 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3013 if (state == QLCNIC_DEV_NPAR_NON_OPER)
3014 return;
3015
3016 if (qlcnic_api_lock(adapter))
3017 return;
3018 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
3019 qlcnic_api_unlock(adapter);
3020}
3021
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00003022/*Transit to RESET state from READY state only */
Anirban Chakraborty18f2f612011-05-12 12:48:33 +00003023void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003024qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
3025{
Sritej Velaga646779f2012-02-03 13:45:41 +00003026 u32 state, xg_val = 0, gb_val = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003027
Sritej Velaga646779f2012-02-03 13:45:41 +00003028 qlcnic_xg_set_xg0_mask(xg_val);
3029 qlcnic_xg_set_xg1_mask(xg_val);
3030 QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, xg_val);
3031 qlcnic_gb_set_gb0_mask(gb_val);
3032 qlcnic_gb_set_gb1_mask(gb_val);
3033 qlcnic_gb_set_gb2_mask(gb_val);
3034 qlcnic_gb_set_gb3_mask(gb_val);
3035 QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, gb_val);
3036 dev_info(&adapter->pdev->dev, "Pause control frames disabled"
3037 " on all ports\n");
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003038 adapter->need_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003039 if (qlcnic_api_lock(adapter))
3040 return;
3041
3042 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00003043 if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD)) {
3044 netdev_err(adapter->netdev,
3045 "Device is in FAILED state, Please Reboot\n");
3046 qlcnic_api_unlock(adapter);
3047 return;
3048 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003049
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00003050 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003051 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Sritej Velaga602ca6f2011-06-22 02:52:17 +00003052 adapter->flags |= QLCNIC_FW_RESET_OWNER;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003053 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00003054 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003055 }
3056
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003057 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003058 qlcnic_api_unlock(adapter);
3059}
3060
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00003061/* Transit to NPAR READY state from NPAR NOT READY state */
3062static void
3063qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
3064{
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00003065 if (qlcnic_api_lock(adapter))
3066 return;
3067
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003068 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
3069 QLCDB(adapter, DRV, "NPAR operational state set\n");
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00003070
3071 qlcnic_api_unlock(adapter);
3072}
3073
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003074static void
3075qlcnic_schedule_work(struct qlcnic_adapter *adapter,
3076 work_func_t func, int delay)
3077{
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003078 if (test_bit(__QLCNIC_AER, &adapter->state))
3079 return;
3080
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003081 INIT_DELAYED_WORK(&adapter->fw_work, func);
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00003082 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
3083 round_jiffies_relative(delay));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003084}
3085
3086static void
3087qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
3088{
3089 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3090 msleep(10);
3091
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00003092 if (!adapter->fw_work.work.func)
3093 return;
3094
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003095 cancel_delayed_work_sync(&adapter->fw_work);
3096}
3097
3098static void
3099qlcnic_attach_work(struct work_struct *work)
3100{
3101 struct qlcnic_adapter *adapter = container_of(work,
3102 struct qlcnic_adapter, fw_work.work);
3103 struct net_device *netdev = adapter->netdev;
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00003104 u32 npar_state;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003105
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00003106 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
3107 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3108 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3109 qlcnic_clr_all_drv_state(adapter, 0);
3110 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3111 qlcnic_schedule_work(adapter, qlcnic_attach_work,
3112 FW_POLL_DELAY);
3113 else
3114 goto attach;
3115 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3116 return;
3117 }
3118attach:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003119 if (netif_running(netdev)) {
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00003120 if (qlcnic_up(adapter, netdev))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003121 goto done;
3122
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003123 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003124 }
3125
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003126done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00003127 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003128 adapter->fw_fail_cnt = 0;
Sritej Velaga032a13c2011-07-29 13:30:27 +00003129 adapter->flags &= ~QLCNIC_FW_HANG;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003130 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00003131
3132 if (!qlcnic_clr_drv_state(adapter))
3133 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3134 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003135}
3136
3137static int
3138qlcnic_check_health(struct qlcnic_adapter *adapter)
3139{
Sony Chacko4e708122010-08-31 17:17:44 +00003140 u32 state = 0, heartbeat;
Ameen Rahman853d4bc2011-09-13 08:06:17 +00003141 u32 peg_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003142
3143 if (qlcnic_check_temp(adapter))
3144 goto detach;
3145
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00003146 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003147 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003148
3149 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00003150 if (state == QLCNIC_DEV_NEED_RESET) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003151 qlcnic_set_npar_non_operational(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003152 adapter->need_fw_reset = 1;
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00003153 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3154 goto detach;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003155
Sony Chacko4e708122010-08-31 17:17:44 +00003156 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3157 if (heartbeat != adapter->heartbeat) {
3158 adapter->heartbeat = heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003159 adapter->fw_fail_cnt = 0;
3160 if (adapter->need_fw_reset)
3161 goto detach;
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003162
amit salecha9ce13ca2011-02-23 03:21:24 +00003163 if (adapter->reset_context && auto_fw_reset) {
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003164 qlcnic_reset_hw_context(adapter);
3165 adapter->netdev->trans_start = jiffies;
3166 }
3167
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003168 return 0;
3169 }
3170
3171 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3172 return 0;
3173
Sritej Velaga032a13c2011-07-29 13:30:27 +00003174 adapter->flags |= QLCNIC_FW_HANG;
3175
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003176 qlcnic_dev_request_reset(adapter);
3177
amit salecha9ce13ca2011-02-23 03:21:24 +00003178 if (auto_fw_reset)
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00003179 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003180
Ameen Rahman853d4bc2011-09-13 08:06:17 +00003181 dev_err(&adapter->pdev->dev, "firmware hang detected\n");
3182 dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
Sritej Velagac76ecb72011-07-29 13:30:29 +00003183 "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
3184 "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
3185 "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
3186 "PEG_NET_4_PC: 0x%x\n",
3187 QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1),
3188 QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS2),
3189 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c),
3190 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c),
3191 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c),
3192 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c),
3193 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c));
Ameen Rahman853d4bc2011-09-13 08:06:17 +00003194 peg_status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
Sritej Velaga97048a12012-02-03 13:45:43 +00003195 if (QLCNIC_FWERROR_CODE(peg_status) == 0x67)
Ameen Rahman853d4bc2011-09-13 08:06:17 +00003196 dev_err(&adapter->pdev->dev,
3197 "Firmware aborted with error code 0x00006700. "
3198 "Device is being reset.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003199detach:
3200 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3201 QLCNIC_DEV_NEED_RESET;
3202
amit salecha9ce13ca2011-02-23 03:21:24 +00003203 if (auto_fw_reset &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003204 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3205
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003206 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003207 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3208 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003209
3210 return 1;
3211}
3212
3213static void
3214qlcnic_fw_poll_work(struct work_struct *work)
3215{
3216 struct qlcnic_adapter *adapter = container_of(work,
3217 struct qlcnic_adapter, fw_work.work);
3218
3219 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3220 goto reschedule;
3221
3222
3223 if (qlcnic_check_health(adapter))
3224 return;
3225
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00003226 if (adapter->fhash.fnum)
3227 qlcnic_prune_lb_filters(adapter);
3228
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003229reschedule:
3230 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3231}
3232
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003233static int qlcnic_is_first_func(struct pci_dev *pdev)
3234{
3235 struct pci_dev *oth_pdev;
3236 int val = pdev->devfn;
3237
3238 while (val-- > 0) {
3239 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3240 (pdev->bus), pdev->bus->number,
3241 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003242 if (!oth_pdev)
3243 continue;
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003244
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003245 if (oth_pdev->current_state != PCI_D3cold) {
3246 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003247 return 0;
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003248 }
3249 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003250 }
3251 return 1;
3252}
3253
3254static int qlcnic_attach_func(struct pci_dev *pdev)
3255{
3256 int err, first_func;
3257 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3258 struct net_device *netdev = adapter->netdev;
3259
3260 pdev->error_state = pci_channel_io_normal;
3261
3262 err = pci_enable_device(pdev);
3263 if (err)
3264 return err;
3265
3266 pci_set_power_state(pdev, PCI_D0);
3267 pci_set_master(pdev);
3268 pci_restore_state(pdev);
3269
3270 first_func = qlcnic_is_first_func(pdev);
3271
3272 if (qlcnic_api_lock(adapter))
3273 return -EINVAL;
3274
Amit Kumar Salecha933fce122010-08-17 00:34:19 +00003275 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003276 adapter->need_fw_reset = 1;
3277 set_bit(__QLCNIC_START_FW, &adapter->state);
3278 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3279 QLCDB(adapter, DRV, "Restarting fw\n");
3280 }
3281 qlcnic_api_unlock(adapter);
3282
3283 err = adapter->nic_ops->start_firmware(adapter);
3284 if (err)
3285 return err;
3286
3287 qlcnic_clr_drv_state(adapter);
3288 qlcnic_setup_intr(adapter);
3289
3290 if (netif_running(netdev)) {
3291 err = qlcnic_attach(adapter);
3292 if (err) {
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00003293 qlcnic_clr_all_drv_state(adapter, 1);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003294 clear_bit(__QLCNIC_AER, &adapter->state);
3295 netif_device_attach(netdev);
3296 return err;
3297 }
3298
3299 err = qlcnic_up(adapter, netdev);
3300 if (err)
3301 goto done;
3302
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003303 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003304 }
3305 done:
3306 netif_device_attach(netdev);
3307 return err;
3308}
3309
3310static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3311 pci_channel_state_t state)
3312{
3313 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3314 struct net_device *netdev = adapter->netdev;
3315
3316 if (state == pci_channel_io_perm_failure)
3317 return PCI_ERS_RESULT_DISCONNECT;
3318
3319 if (state == pci_channel_io_normal)
3320 return PCI_ERS_RESULT_RECOVERED;
3321
3322 set_bit(__QLCNIC_AER, &adapter->state);
3323 netif_device_detach(netdev);
3324
3325 cancel_delayed_work_sync(&adapter->fw_work);
3326
3327 if (netif_running(netdev))
3328 qlcnic_down(adapter, netdev);
3329
3330 qlcnic_detach(adapter);
3331 qlcnic_teardown_intr(adapter);
3332
3333 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3334
3335 pci_save_state(pdev);
3336 pci_disable_device(pdev);
3337
3338 return PCI_ERS_RESULT_NEED_RESET;
3339}
3340
3341static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3342{
3343 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3344 PCI_ERS_RESULT_RECOVERED;
3345}
3346
3347static void qlcnic_io_resume(struct pci_dev *pdev)
3348{
3349 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3350
3351 pci_cleanup_aer_uncorrect_error_status(pdev);
3352
3353 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3354 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3355 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3356 FW_POLL_DELAY);
3357}
3358
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003359static int
3360qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3361{
3362 int err;
3363
3364 err = qlcnic_can_start_firmware(adapter);
3365 if (err)
3366 return err;
3367
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +00003368 err = qlcnic_check_npar_opertional(adapter);
3369 if (err)
3370 return err;
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003371
Rajesh Borundia174240a2010-08-31 17:17:47 +00003372 err = qlcnic_initialize_nic(adapter);
3373 if (err)
3374 return err;
3375
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003376 qlcnic_check_options(adapter);
3377
Rajesh Borundia73733732010-08-31 17:17:50 +00003378 err = qlcnic_set_eswitch_port_config(adapter);
3379 if (err)
3380 return err;
3381
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003382 adapter->need_fw_reset = 0;
3383
3384 return err;
3385}
3386
3387static int
3388qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3389{
3390 return -EOPNOTSUPP;
3391}
3392
3393static int
3394qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3395{
3396 return -EOPNOTSUPP;
3397}
3398
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003399static ssize_t
3400qlcnic_store_bridged_mode(struct device *dev,
3401 struct device_attribute *attr, const char *buf, size_t len)
3402{
3403 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3404 unsigned long new;
3405 int ret = -EINVAL;
3406
3407 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3408 goto err_out;
3409
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00003410 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003411 goto err_out;
3412
3413 if (strict_strtoul(buf, 2, &new))
3414 goto err_out;
3415
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00003416 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003417 ret = len;
3418
3419err_out:
3420 return ret;
3421}
3422
3423static ssize_t
3424qlcnic_show_bridged_mode(struct device *dev,
3425 struct device_attribute *attr, char *buf)
3426{
3427 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3428 int bridged_mode = 0;
3429
3430 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3431 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3432
3433 return sprintf(buf, "%d\n", bridged_mode);
3434}
3435
3436static struct device_attribute dev_attr_bridged_mode = {
3437 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3438 .show = qlcnic_show_bridged_mode,
3439 .store = qlcnic_store_bridged_mode,
3440};
3441
3442static ssize_t
3443qlcnic_store_diag_mode(struct device *dev,
3444 struct device_attribute *attr, const char *buf, size_t len)
3445{
3446 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3447 unsigned long new;
3448
3449 if (strict_strtoul(buf, 2, &new))
3450 return -EINVAL;
3451
3452 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3453 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3454
3455 return len;
3456}
3457
3458static ssize_t
3459qlcnic_show_diag_mode(struct device *dev,
3460 struct device_attribute *attr, char *buf)
3461{
3462 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3463
3464 return sprintf(buf, "%d\n",
3465 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3466}
3467
3468static struct device_attribute dev_attr_diag_mode = {
3469 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3470 .show = qlcnic_show_diag_mode,
3471 .store = qlcnic_store_diag_mode,
3472};
3473
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00003474int qlcnic_validate_max_rss(struct net_device *netdev, u8 max_hw, u8 val)
3475{
3476 if (!use_msi_x && !use_msi) {
3477 netdev_info(netdev, "no msix or msi support, hence no rss\n");
3478 return -EINVAL;
3479 }
3480
3481 if ((val > max_hw) || (val < 2) || !is_power_of_2(val)) {
3482 netdev_info(netdev, "rss_ring valid range [2 - %x] in "
3483 " powers of 2\n", max_hw);
3484 return -EINVAL;
3485 }
3486 return 0;
3487
3488}
3489
3490int qlcnic_set_max_rss(struct qlcnic_adapter *adapter, u8 data)
3491{
3492 struct net_device *netdev = adapter->netdev;
3493 int err = 0;
3494
3495 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3496 return -EBUSY;
3497
3498 netif_device_detach(netdev);
3499 if (netif_running(netdev))
3500 __qlcnic_down(adapter, netdev);
3501 qlcnic_detach(adapter);
3502 qlcnic_teardown_intr(adapter);
3503
3504 if (qlcnic_enable_msix(adapter, data)) {
3505 netdev_info(netdev, "failed setting max_rss; rss disabled\n");
3506 qlcnic_enable_msi_legacy(adapter);
3507 }
3508
3509 if (netif_running(netdev)) {
3510 err = qlcnic_attach(adapter);
3511 if (err)
3512 goto done;
3513 err = __qlcnic_up(adapter, netdev);
3514 if (err)
3515 goto done;
3516 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3517 }
3518 done:
3519 netif_device_attach(netdev);
3520 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3521 return err;
3522}
3523
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003524static int
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003525qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon, u8 *state,
3526 u8 *rate)
3527{
3528 *rate = LSB(beacon);
3529 *state = MSB(beacon);
3530
3531 QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
3532
3533 if (!*state) {
3534 *rate = __QLCNIC_MAX_LED_RATE;
3535 return 0;
3536 } else if (*state > __QLCNIC_MAX_LED_STATE)
3537 return -EINVAL;
3538
3539 if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
3540 return -EINVAL;
3541
3542 return 0;
3543}
3544
3545static ssize_t
3546qlcnic_store_beacon(struct device *dev,
3547 struct device_attribute *attr, const char *buf, size_t len)
3548{
3549 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3550 int max_sds_rings = adapter->max_sds_rings;
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003551 u16 beacon;
3552 u8 b_state, b_rate;
3553 int err;
3554
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003555 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
3556 dev_warn(dev, "LED test not supported for non "
3557 "privilege function\n");
3558 return -EOPNOTSUPP;
3559 }
3560
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003561 if (len != sizeof(u16))
3562 return QL_STATUS_INVALID_PARAM;
3563
3564 memcpy(&beacon, buf, sizeof(u16));
3565 err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
3566 if (err)
3567 return err;
3568
3569 if (adapter->ahw->beacon_state == b_state)
3570 return len;
3571
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003572 rtnl_lock();
3573
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003574 if (!adapter->ahw->beacon_state)
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003575 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
3576 rtnl_unlock();
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003577 return -EBUSY;
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003578 }
3579
3580 if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
3581 err = -EIO;
3582 goto out;
3583 }
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003584
3585 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003586 err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003587 if (err)
3588 goto out;
3589 set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003590 }
3591
3592 err = qlcnic_config_led(adapter, b_state, b_rate);
3593
3594 if (!err) {
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003595 err = len;
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003596 adapter->ahw->beacon_state = b_state;
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003597 }
3598
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003599 if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003600 qlcnic_diag_free_res(adapter->netdev, max_sds_rings);
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003601
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003602 out:
3603 if (!adapter->ahw->beacon_state)
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003604 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
Sucheta Chakraborty10ee0fa2011-10-28 12:57:15 +00003605 rtnl_unlock();
Sucheta Chakraborty728a98b2011-08-29 12:50:30 +00003606
3607 return err;
3608}
3609
3610static ssize_t
3611qlcnic_show_beacon(struct device *dev,
3612 struct device_attribute *attr, char *buf)
3613{
3614 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3615
3616 return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
3617}
3618
3619static struct device_attribute dev_attr_beacon = {
3620 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
3621 .show = qlcnic_show_beacon,
3622 .store = qlcnic_store_beacon,
3623};
3624
3625static int
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003626qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3627 loff_t offset, size_t size)
3628{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003629 size_t crb_size = 4;
3630
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003631 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3632 return -EIO;
3633
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003634 if (offset < QLCNIC_PCI_CRBSPACE) {
3635 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3636 QLCNIC_PCI_CAMQM_END))
3637 crb_size = 8;
3638 else
3639 return -EINVAL;
3640 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003641
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003642 if ((size != crb_size) || (offset & (crb_size-1)))
3643 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003644
3645 return 0;
3646}
3647
3648static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003649qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3650 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003651 char *buf, loff_t offset, size_t size)
3652{
3653 struct device *dev = container_of(kobj, struct device, kobj);
3654 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3655 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003656 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003657 int ret;
3658
3659 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3660 if (ret != 0)
3661 return ret;
3662
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003663 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3664 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3665 memcpy(buf, &qmdata, size);
3666 } else {
3667 data = QLCRD32(adapter, offset);
3668 memcpy(buf, &data, size);
3669 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003670 return size;
3671}
3672
3673static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003674qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3675 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003676 char *buf, loff_t offset, size_t size)
3677{
3678 struct device *dev = container_of(kobj, struct device, kobj);
3679 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3680 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003681 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003682 int ret;
3683
3684 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3685 if (ret != 0)
3686 return ret;
3687
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003688 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3689 memcpy(&qmdata, buf, size);
3690 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3691 } else {
3692 memcpy(&data, buf, size);
3693 QLCWR32(adapter, offset, data);
3694 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003695 return size;
3696}
3697
3698static int
3699qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3700 loff_t offset, size_t size)
3701{
3702 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3703 return -EIO;
3704
3705 if ((size != 8) || (offset & 0x7))
3706 return -EIO;
3707
3708 return 0;
3709}
3710
3711static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003712qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3713 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003714 char *buf, loff_t offset, size_t size)
3715{
3716 struct device *dev = container_of(kobj, struct device, kobj);
3717 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3718 u64 data;
3719 int ret;
3720
3721 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3722 if (ret != 0)
3723 return ret;
3724
3725 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3726 return -EIO;
3727
3728 memcpy(buf, &data, size);
3729
3730 return size;
3731}
3732
3733static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003734qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3735 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003736 char *buf, loff_t offset, size_t size)
3737{
3738 struct device *dev = container_of(kobj, struct device, kobj);
3739 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3740 u64 data;
3741 int ret;
3742
3743 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3744 if (ret != 0)
3745 return ret;
3746
3747 memcpy(&data, buf, size);
3748
3749 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3750 return -EIO;
3751
3752 return size;
3753}
3754
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003755static struct bin_attribute bin_attr_crb = {
3756 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3757 .size = 0,
3758 .read = qlcnic_sysfs_read_crb,
3759 .write = qlcnic_sysfs_write_crb,
3760};
3761
3762static struct bin_attribute bin_attr_mem = {
3763 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3764 .size = 0,
3765 .read = qlcnic_sysfs_read_mem,
3766 .write = qlcnic_sysfs_write_mem,
3767};
3768
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003769static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003770validate_pm_config(struct qlcnic_adapter *adapter,
3771 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3772{
3773
3774 u8 src_pci_func, s_esw_id, d_esw_id;
3775 u8 dest_pci_func;
3776 int i;
3777
3778 for (i = 0; i < count; i++) {
3779 src_pci_func = pm_cfg[i].pci_func;
3780 dest_pci_func = pm_cfg[i].dest_npar;
3781 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3782 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3783 return QL_STATUS_INVALID_PARAM;
3784
3785 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3786 return QL_STATUS_INVALID_PARAM;
3787
3788 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3789 return QL_STATUS_INVALID_PARAM;
3790
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003791 s_esw_id = adapter->npars[src_pci_func].phy_port;
3792 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3793
3794 if (s_esw_id != d_esw_id)
3795 return QL_STATUS_INVALID_PARAM;
3796
3797 }
3798 return 0;
3799
3800}
3801
3802static ssize_t
3803qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3804 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3805{
3806 struct device *dev = container_of(kobj, struct device, kobj);
3807 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3808 struct qlcnic_pm_func_cfg *pm_cfg;
3809 u32 id, action, pci_func;
3810 int count, rem, i, ret;
3811
3812 count = size / sizeof(struct qlcnic_pm_func_cfg);
3813 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3814 if (rem)
3815 return QL_STATUS_INVALID_PARAM;
3816
3817 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3818
3819 ret = validate_pm_config(adapter, pm_cfg, count);
3820 if (ret)
3821 return ret;
3822 for (i = 0; i < count; i++) {
3823 pci_func = pm_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003824 action = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003825 id = adapter->npars[pci_func].phy_port;
3826 ret = qlcnic_config_port_mirroring(adapter, id,
3827 action, pci_func);
3828 if (ret)
3829 return ret;
3830 }
3831
3832 for (i = 0; i < count; i++) {
3833 pci_func = pm_cfg[i].pci_func;
3834 id = adapter->npars[pci_func].phy_port;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003835 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003836 adapter->npars[pci_func].dest_npar = id;
3837 }
3838 return size;
3839}
3840
3841static ssize_t
3842qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3843 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3844{
3845 struct device *dev = container_of(kobj, struct device, kobj);
3846 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3847 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3848 int i;
3849
3850 if (size != sizeof(pm_cfg))
3851 return QL_STATUS_INVALID_PARAM;
3852
3853 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3854 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3855 continue;
3856 pm_cfg[i].action = adapter->npars[i].enable_pm;
3857 pm_cfg[i].dest_npar = 0;
3858 pm_cfg[i].pci_func = i;
3859 }
3860 memcpy(buf, &pm_cfg, size);
3861
3862 return size;
3863}
3864
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003865static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003866validate_esw_config(struct qlcnic_adapter *adapter,
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003867 struct qlcnic_esw_func_cfg *esw_cfg, int count)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003868{
Rajesh Borundia7613c872010-08-31 17:17:48 +00003869 u32 op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003870 u8 pci_func;
3871 int i;
Rajesh Borundia7613c872010-08-31 17:17:48 +00003872
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003873 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
Rajesh Borundia7613c872010-08-31 17:17:48 +00003874
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003875 for (i = 0; i < count; i++) {
3876 pci_func = esw_cfg[i].pci_func;
3877 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3878 return QL_STATUS_INVALID_PARAM;
3879
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003880 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3881 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003882 return QL_STATUS_INVALID_PARAM;
3883
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003884 switch (esw_cfg[i].op_mode) {
3885 case QLCNIC_PORT_DEFAULTS:
Rajesh Borundia7613c872010-08-31 17:17:48 +00003886 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
Rajesh Borundia73733732010-08-31 17:17:50 +00003887 QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha091056b2010-12-02 20:41:43 +00003888 if (esw_cfg[i].mac_anti_spoof != 0)
3889 return QL_STATUS_INVALID_PARAM;
3890 if (esw_cfg[i].mac_override != 1)
3891 return QL_STATUS_INVALID_PARAM;
3892 if (esw_cfg[i].promisc_mode != 1)
3893 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia73733732010-08-31 17:17:50 +00003894 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003895 break;
3896 case QLCNIC_ADD_VLAN:
3897 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3898 return QL_STATUS_INVALID_PARAM;
3899 if (!esw_cfg[i].op_type)
3900 return QL_STATUS_INVALID_PARAM;
3901 break;
3902 case QLCNIC_DEL_VLAN:
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003903 if (!esw_cfg[i].op_type)
3904 return QL_STATUS_INVALID_PARAM;
3905 break;
3906 default:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003907 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003908 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003909 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003910 return 0;
3911}
3912
3913static ssize_t
3914qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3915 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3916{
3917 struct device *dev = container_of(kobj, struct device, kobj);
3918 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3919 struct qlcnic_esw_func_cfg *esw_cfg;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003920 struct qlcnic_npar_info *npar;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003921 int count, rem, i, ret;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003922 u8 pci_func, op_mode = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003923
3924 count = size / sizeof(struct qlcnic_esw_func_cfg);
3925 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3926 if (rem)
3927 return QL_STATUS_INVALID_PARAM;
3928
3929 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3930 ret = validate_esw_config(adapter, esw_cfg, count);
3931 if (ret)
3932 return ret;
3933
3934 for (i = 0; i < count; i++) {
Rajesh Borundia0325d692010-08-19 05:08:26 +00003935 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3936 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3937 return QL_STATUS_INVALID_PARAM;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003938
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003939 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003940 continue;
3941
3942 op_mode = esw_cfg[i].op_mode;
3943 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3944 esw_cfg[i].op_mode = op_mode;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003945 esw_cfg[i].pci_func = adapter->ahw->pci_func;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003946
3947 switch (esw_cfg[i].op_mode) {
3948 case QLCNIC_PORT_DEFAULTS:
3949 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3950 break;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00003951 case QLCNIC_ADD_VLAN:
3952 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3953 break;
3954 case QLCNIC_DEL_VLAN:
3955 esw_cfg[i].vlan_id = 0;
3956 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3957 break;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003958 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003959 }
3960
Rajesh Borundia0325d692010-08-19 05:08:26 +00003961 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3962 goto out;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003963
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003964 for (i = 0; i < count; i++) {
3965 pci_func = esw_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003966 npar = &adapter->npars[pci_func];
3967 switch (esw_cfg[i].op_mode) {
3968 case QLCNIC_PORT_DEFAULTS:
3969 npar->promisc_mode = esw_cfg[i].promisc_mode;
Rajesh Borundia73733732010-08-31 17:17:50 +00003970 npar->mac_override = esw_cfg[i].mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003971 npar->offload_flags = esw_cfg[i].offload_flags;
3972 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3973 npar->discard_tagged = esw_cfg[i].discard_tagged;
3974 break;
3975 case QLCNIC_ADD_VLAN:
3976 npar->pvid = esw_cfg[i].vlan_id;
3977 break;
3978 case QLCNIC_DEL_VLAN:
3979 npar->pvid = 0;
3980 break;
3981 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003982 }
Rajesh Borundia0325d692010-08-19 05:08:26 +00003983out:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003984 return size;
3985}
3986
3987static ssize_t
3988qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3989 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3990{
3991 struct device *dev = container_of(kobj, struct device, kobj);
3992 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3993 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003994 u8 i;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003995
3996 if (size != sizeof(esw_cfg))
3997 return QL_STATUS_INVALID_PARAM;
3998
3999 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
4000 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
4001 continue;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004002 esw_cfg[i].pci_func = i;
4003 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
4004 return QL_STATUS_INVALID_PARAM;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004005 }
4006 memcpy(buf, &esw_cfg, size);
4007
4008 return size;
4009}
4010
Anirban Chakrabortycea89752010-07-13 20:33:35 +00004011static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004012validate_npar_config(struct qlcnic_adapter *adapter,
4013 struct qlcnic_npar_func_cfg *np_cfg, int count)
4014{
4015 u8 pci_func, i;
4016
4017 for (i = 0; i < count; i++) {
4018 pci_func = np_cfg[i].pci_func;
4019 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
4020 return QL_STATUS_INVALID_PARAM;
4021
4022 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
4023 return QL_STATUS_INVALID_PARAM;
4024
Rajesh Borundiad12b0d92011-02-23 03:21:25 +00004025 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
4026 !IS_VALID_BW(np_cfg[i].max_bw))
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004027 return QL_STATUS_INVALID_PARAM;
4028 }
4029 return 0;
4030}
4031
4032static ssize_t
4033qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
4034 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4035{
4036 struct device *dev = container_of(kobj, struct device, kobj);
4037 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4038 struct qlcnic_info nic_info;
4039 struct qlcnic_npar_func_cfg *np_cfg;
4040 int i, count, rem, ret;
4041 u8 pci_func;
4042
4043 count = size / sizeof(struct qlcnic_npar_func_cfg);
4044 rem = size % sizeof(struct qlcnic_npar_func_cfg);
4045 if (rem)
4046 return QL_STATUS_INVALID_PARAM;
4047
4048 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
4049 ret = validate_npar_config(adapter, np_cfg, count);
4050 if (ret)
4051 return ret;
4052
4053 for (i = 0; i < count ; i++) {
4054 pci_func = np_cfg[i].pci_func;
4055 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
4056 if (ret)
4057 return ret;
4058 nic_info.pci_func = pci_func;
4059 nic_info.min_tx_bw = np_cfg[i].min_bw;
4060 nic_info.max_tx_bw = np_cfg[i].max_bw;
4061 ret = qlcnic_set_nic_info(adapter, &nic_info);
4062 if (ret)
4063 return ret;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00004064 adapter->npars[i].min_bw = nic_info.min_tx_bw;
4065 adapter->npars[i].max_bw = nic_info.max_tx_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004066 }
4067
4068 return size;
4069
4070}
4071static ssize_t
4072qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
4073 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4074{
4075 struct device *dev = container_of(kobj, struct device, kobj);
4076 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4077 struct qlcnic_info nic_info;
4078 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
4079 int i, ret;
4080
4081 if (size != sizeof(np_cfg))
4082 return QL_STATUS_INVALID_PARAM;
4083
4084 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4085 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
4086 continue;
4087 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
4088 if (ret)
4089 return ret;
4090
4091 np_cfg[i].pci_func = i;
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +00004092 np_cfg[i].op_mode = (u8)nic_info.op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004093 np_cfg[i].port_num = nic_info.phys_port;
4094 np_cfg[i].fw_capab = nic_info.capabilities;
4095 np_cfg[i].min_bw = nic_info.min_tx_bw ;
4096 np_cfg[i].max_bw = nic_info.max_tx_bw;
4097 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
4098 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
4099 }
4100 memcpy(buf, &np_cfg, size);
4101 return size;
4102}
4103
4104static ssize_t
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004105qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
4106 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4107{
4108 struct device *dev = container_of(kobj, struct device, kobj);
4109 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4110 struct qlcnic_esw_statistics port_stats;
4111 int ret;
4112
4113 if (size != sizeof(struct qlcnic_esw_statistics))
4114 return QL_STATUS_INVALID_PARAM;
4115
4116 if (offset >= QLCNIC_MAX_PCI_FUNC)
4117 return QL_STATUS_INVALID_PARAM;
4118
4119 memset(&port_stats, 0, size);
4120 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4121 &port_stats.rx);
4122 if (ret)
4123 return ret;
4124
4125 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4126 &port_stats.tx);
4127 if (ret)
4128 return ret;
4129
4130 memcpy(buf, &port_stats, size);
4131 return size;
4132}
4133
4134static ssize_t
4135qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
4136 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4137{
4138 struct device *dev = container_of(kobj, struct device, kobj);
4139 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4140 struct qlcnic_esw_statistics esw_stats;
4141 int ret;
4142
4143 if (size != sizeof(struct qlcnic_esw_statistics))
4144 return QL_STATUS_INVALID_PARAM;
4145
4146 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4147 return QL_STATUS_INVALID_PARAM;
4148
4149 memset(&esw_stats, 0, size);
4150 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4151 &esw_stats.rx);
4152 if (ret)
4153 return ret;
4154
4155 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4156 &esw_stats.tx);
4157 if (ret)
4158 return ret;
4159
4160 memcpy(buf, &esw_stats, size);
4161 return size;
4162}
4163
4164static ssize_t
4165qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
4166 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4167{
4168 struct device *dev = container_of(kobj, struct device, kobj);
4169 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4170 int ret;
4171
4172 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4173 return QL_STATUS_INVALID_PARAM;
4174
4175 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4176 QLCNIC_QUERY_RX_COUNTER);
4177 if (ret)
4178 return ret;
4179
4180 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4181 QLCNIC_QUERY_TX_COUNTER);
4182 if (ret)
4183 return ret;
4184
4185 return size;
4186}
4187
4188static ssize_t
4189qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
4190 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4191{
4192
4193 struct device *dev = container_of(kobj, struct device, kobj);
4194 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4195 int ret;
4196
4197 if (offset >= QLCNIC_MAX_PCI_FUNC)
4198 return QL_STATUS_INVALID_PARAM;
4199
4200 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4201 QLCNIC_QUERY_RX_COUNTER);
4202 if (ret)
4203 return ret;
4204
4205 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4206 QLCNIC_QUERY_TX_COUNTER);
4207 if (ret)
4208 return ret;
4209
4210 return size;
4211}
4212
4213static ssize_t
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004214qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
4215 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4216{
4217 struct device *dev = container_of(kobj, struct device, kobj);
4218 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4219 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
Dan Carpentere88db3b2010-08-09 21:49:36 +00004220 struct qlcnic_pci_info *pci_info;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004221 int i, ret;
4222
4223 if (size != sizeof(pci_cfg))
4224 return QL_STATUS_INVALID_PARAM;
4225
Dan Carpentere88db3b2010-08-09 21:49:36 +00004226 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
4227 if (!pci_info)
4228 return -ENOMEM;
4229
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004230 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpentere88db3b2010-08-09 21:49:36 +00004231 if (ret) {
4232 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004233 return ret;
Dan Carpentere88db3b2010-08-09 21:49:36 +00004234 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004235
4236 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4237 pci_cfg[i].pci_func = pci_info[i].id;
4238 pci_cfg[i].func_type = pci_info[i].type;
4239 pci_cfg[i].port_num = pci_info[i].default_port;
4240 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
4241 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
4242 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
4243 }
4244 memcpy(buf, &pci_cfg, size);
Dan Carpentere88db3b2010-08-09 21:49:36 +00004245 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004246 return size;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004247}
4248static struct bin_attribute bin_attr_npar_config = {
4249 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
4250 .size = 0,
4251 .read = qlcnic_sysfs_read_npar_config,
4252 .write = qlcnic_sysfs_write_npar_config,
4253};
4254
4255static struct bin_attribute bin_attr_pci_config = {
4256 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
4257 .size = 0,
4258 .read = qlcnic_sysfs_read_pci_config,
4259 .write = NULL,
4260};
4261
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004262static struct bin_attribute bin_attr_port_stats = {
4263 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
4264 .size = 0,
4265 .read = qlcnic_sysfs_get_port_stats,
4266 .write = qlcnic_sysfs_clear_port_stats,
4267};
4268
4269static struct bin_attribute bin_attr_esw_stats = {
4270 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
4271 .size = 0,
4272 .read = qlcnic_sysfs_get_esw_stats,
4273 .write = qlcnic_sysfs_clear_esw_stats,
4274};
4275
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004276static struct bin_attribute bin_attr_esw_config = {
4277 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
4278 .size = 0,
4279 .read = qlcnic_sysfs_read_esw_config,
4280 .write = qlcnic_sysfs_write_esw_config,
4281};
4282
4283static struct bin_attribute bin_attr_pm_config = {
4284 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
4285 .size = 0,
4286 .read = qlcnic_sysfs_read_pm_config,
4287 .write = qlcnic_sysfs_write_pm_config,
4288};
4289
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004290static void
4291qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
4292{
4293 struct device *dev = &adapter->pdev->dev;
4294
4295 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4296 if (device_create_file(dev, &dev_attr_bridged_mode))
4297 dev_warn(dev,
4298 "failed to create bridged_mode sysfs entry\n");
4299}
4300
4301static void
4302qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
4303{
4304 struct device *dev = &adapter->pdev->dev;
4305
4306 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4307 device_remove_file(dev, &dev_attr_bridged_mode);
4308}
4309
4310static void
4311qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
4312{
4313 struct device *dev = &adapter->pdev->dev;
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004314 u32 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004315
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004316 if (device_create_bin_file(dev, &bin_attr_port_stats))
4317 dev_info(dev, "failed to create port stats sysfs entry");
4318
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004319 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4320 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004321 if (device_create_file(dev, &dev_attr_diag_mode))
4322 dev_info(dev, "failed to create diag_mode sysfs entry\n");
4323 if (device_create_bin_file(dev, &bin_attr_crb))
4324 dev_info(dev, "failed to create crb sysfs entry\n");
4325 if (device_create_bin_file(dev, &bin_attr_mem))
4326 dev_info(dev, "failed to create mem sysfs entry\n");
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004327
4328 if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD))
4329 return;
4330
Sony Chacko53478fe2011-04-01 14:27:47 +00004331 if (device_create_bin_file(dev, &bin_attr_pci_config))
4332 dev_info(dev, "failed to create pci config sysfs entry");
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004333 if (device_create_file(dev, &dev_attr_beacon))
4334 dev_info(dev, "failed to create beacon sysfs entry");
4335
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004336 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4337 return;
4338 if (device_create_bin_file(dev, &bin_attr_esw_config))
4339 dev_info(dev, "failed to create esw config sysfs entry");
4340 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004341 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004342 if (device_create_bin_file(dev, &bin_attr_npar_config))
4343 dev_info(dev, "failed to create npar config sysfs entry");
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004344 if (device_create_bin_file(dev, &bin_attr_pm_config))
4345 dev_info(dev, "failed to create pm config sysfs entry");
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004346 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4347 dev_info(dev, "failed to create eswitch stats sysfs entry");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004348}
4349
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004350static void
4351qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4352{
4353 struct device *dev = &adapter->pdev->dev;
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004354 u32 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004355
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004356 device_remove_bin_file(dev, &bin_attr_port_stats);
4357
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004358 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4359 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004360 device_remove_file(dev, &dev_attr_diag_mode);
4361 device_remove_bin_file(dev, &bin_attr_crb);
4362 device_remove_bin_file(dev, &bin_attr_mem);
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004363 if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD))
4364 return;
Sony Chacko53478fe2011-04-01 14:27:47 +00004365 device_remove_bin_file(dev, &bin_attr_pci_config);
Sucheta Chakrabortyb43e5ee2012-04-26 10:31:29 +00004366 device_remove_file(dev, &dev_attr_beacon);
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004367 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4368 return;
4369 device_remove_bin_file(dev, &bin_attr_esw_config);
4370 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004371 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004372 device_remove_bin_file(dev, &bin_attr_npar_config);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004373 device_remove_bin_file(dev, &bin_attr_pm_config);
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004374 device_remove_bin_file(dev, &bin_attr_esw_stats);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004375}
4376
4377#ifdef CONFIG_INET
4378
4379#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4380
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004381static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004382qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4383 struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004384{
4385 struct in_device *indev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004386
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004387 indev = in_dev_get(dev);
4388 if (!indev)
4389 return;
4390
4391 for_ifa(indev) {
4392 switch (event) {
4393 case NETDEV_UP:
4394 qlcnic_config_ipaddr(adapter,
4395 ifa->ifa_address, QLCNIC_IP_UP);
4396 break;
4397 case NETDEV_DOWN:
4398 qlcnic_config_ipaddr(adapter,
4399 ifa->ifa_address, QLCNIC_IP_DOWN);
4400 break;
4401 default:
4402 break;
4403 }
4404 } endfor_ifa(indev);
4405
4406 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004407}
4408
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004409static void
4410qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4411{
4412 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4413 struct net_device *dev;
4414 u16 vid;
4415
4416 qlcnic_config_indev_addr(adapter, netdev, event);
4417
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +00004418 for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
Jiri Pirko223bb152011-07-20 04:54:44 +00004419 dev = __vlan_find_dev_deep(netdev, vid);
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004420 if (!dev)
4421 continue;
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004422 qlcnic_config_indev_addr(adapter, dev, event);
4423 }
4424}
4425
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004426static int qlcnic_netdev_event(struct notifier_block *this,
4427 unsigned long event, void *ptr)
4428{
4429 struct qlcnic_adapter *adapter;
4430 struct net_device *dev = (struct net_device *)ptr;
4431
4432recheck:
4433 if (dev == NULL)
4434 goto done;
4435
4436 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4437 dev = vlan_dev_real_dev(dev);
4438 goto recheck;
4439 }
4440
4441 if (!is_qlcnic_netdev(dev))
4442 goto done;
4443
4444 adapter = netdev_priv(dev);
4445
4446 if (!adapter)
4447 goto done;
4448
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004449 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004450 goto done;
4451
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004452 qlcnic_config_indev_addr(adapter, dev, event);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004453done:
4454 return NOTIFY_DONE;
4455}
4456
4457static int
4458qlcnic_inetaddr_event(struct notifier_block *this,
4459 unsigned long event, void *ptr)
4460{
4461 struct qlcnic_adapter *adapter;
4462 struct net_device *dev;
4463
4464 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4465
4466 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4467
4468recheck:
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004469 if (dev == NULL)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004470 goto done;
4471
4472 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4473 dev = vlan_dev_real_dev(dev);
4474 goto recheck;
4475 }
4476
4477 if (!is_qlcnic_netdev(dev))
4478 goto done;
4479
4480 adapter = netdev_priv(dev);
4481
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00004482 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004483 goto done;
4484
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004485 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004486 goto done;
4487
4488 switch (event) {
4489 case NETDEV_UP:
4490 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4491 break;
4492 case NETDEV_DOWN:
4493 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4494 break;
4495 default:
4496 break;
4497 }
4498
4499done:
4500 return NOTIFY_DONE;
4501}
4502
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004503static struct notifier_block qlcnic_netdev_cb = {
4504 .notifier_call = qlcnic_netdev_event,
4505};
4506
4507static struct notifier_block qlcnic_inetaddr_cb = {
4508 .notifier_call = qlcnic_inetaddr_event,
4509};
4510#else
4511static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004512qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004513{ }
4514#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004515static struct pci_error_handlers qlcnic_err_handler = {
4516 .error_detected = qlcnic_io_error_detected,
4517 .slot_reset = qlcnic_io_slot_reset,
4518 .resume = qlcnic_io_resume,
4519};
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004520
4521static struct pci_driver qlcnic_driver = {
4522 .name = qlcnic_driver_name,
4523 .id_table = qlcnic_pci_tbl,
4524 .probe = qlcnic_probe,
4525 .remove = __devexit_p(qlcnic_remove),
4526#ifdef CONFIG_PM
4527 .suspend = qlcnic_suspend,
4528 .resume = qlcnic_resume,
4529#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004530 .shutdown = qlcnic_shutdown,
4531 .err_handler = &qlcnic_err_handler
4532
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004533};
4534
4535static int __init qlcnic_init_module(void)
4536{
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004537 int ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004538
4539 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4540
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004541 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4542 if (qlcnic_wq == NULL) {
4543 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4544 return -ENOMEM;
4545 }
4546
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004547#ifdef CONFIG_INET
4548 register_netdevice_notifier(&qlcnic_netdev_cb);
4549 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4550#endif
4551
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004552 ret = pci_register_driver(&qlcnic_driver);
4553 if (ret) {
4554#ifdef CONFIG_INET
4555 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4556 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4557#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004558 destroy_workqueue(qlcnic_wq);
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004559 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004560
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004561 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004562}
4563
4564module_init(qlcnic_init_module);
4565
4566static void __exit qlcnic_exit_module(void)
4567{
4568
4569 pci_unregister_driver(&qlcnic_driver);
4570
4571#ifdef CONFIG_INET
4572 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4573 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4574#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004575 destroy_workqueue(qlcnic_wq);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004576}
4577
4578module_exit(qlcnic_exit_module);