blob: 916570db3aa4be87aad0c89bd6e5a3cc32fab5e5 [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 *);
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000100static void qlcnic_vlan_rx_add(struct net_device *, u16);
101static void qlcnic_vlan_rx_del(struct net_device *, u16);
102
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))
304 return -EINVAL;
305
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,
328 .ndo_set_multicast_list = qlcnic_set_multi,
329 .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
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000341static struct qlcnic_nic_template qlcnic_ops = {
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000342 .config_bridged_mode = qlcnic_config_bridged_mode,
343 .config_led = qlcnic_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000344 .start_firmware = qlcnic_start_firmware
345};
346
347static struct qlcnic_nic_template qlcnic_vf_ops = {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000348 .config_bridged_mode = qlcnicvf_config_bridged_mode,
349 .config_led = qlcnicvf_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000350 .start_firmware = qlcnicvf_start_firmware
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000351};
352
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000353static int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
354{
355 struct pci_dev *pdev = adapter->pdev;
356 int err = -1;
357
358 adapter->max_sds_rings = 1;
359 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
360 qlcnic_set_msix_bit(pdev, 0);
361
362 if (adapter->msix_supported) {
363 enable_msix:
364 qlcnic_init_msix_entries(adapter, num_msix);
365 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
366 if (err == 0) {
367 adapter->flags |= QLCNIC_MSIX_ENABLED;
368 qlcnic_set_msix_bit(pdev, 1);
369
370 adapter->max_sds_rings = num_msix;
371
372 dev_info(&pdev->dev, "using msi-x interrupts\n");
373 return err;
374 }
375 if (err > 0) {
376 num_msix = rounddown_pow_of_two(err);
377 if (num_msix)
378 goto enable_msix;
379 }
380 }
381 return err;
382}
383
384
385static void qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000386{
387 const struct qlcnic_legacy_intr_set *legacy_intrp;
388 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000389
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000390 if (use_msi && !pci_enable_msi(pdev)) {
391 adapter->flags |= QLCNIC_MSI_ENABLED;
392 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
393 msi_tgt_status[adapter->ahw->pci_func]);
394 dev_info(&pdev->dev, "using msi interrupts\n");
395 adapter->msix_entries[0].vector = pdev->irq;
396 return;
397 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000398
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000399 legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000400
401 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
402 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
403 legacy_intrp->tgt_status_reg);
404 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
405 legacy_intrp->tgt_mask_reg);
406 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
407
408 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
409 ISR_INT_STATE_REG);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000410 dev_info(&pdev->dev, "using legacy interrupts\n");
411 adapter->msix_entries[0].vector = pdev->irq;
412}
413
414static void
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000415qlcnic_setup_intr(struct qlcnic_adapter *adapter)
416{
417 int num_msix;
418
419 if (adapter->msix_supported) {
Sucheta Chakraborty5f6ec292011-06-22 02:52:20 +0000420 num_msix = rounddown_pow_of_two(min_t(int, num_online_cpus(),
421 QLCNIC_DEF_NUM_STS_DESC_RINGS));
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000422 } else
423 num_msix = 1;
424
425 if (!qlcnic_enable_msix(adapter, num_msix))
426 return;
427
428 qlcnic_enable_msi_legacy(adapter);
429}
430
431static void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000432qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
433{
434 if (adapter->flags & QLCNIC_MSIX_ENABLED)
435 pci_disable_msix(adapter->pdev);
436 if (adapter->flags & QLCNIC_MSI_ENABLED)
437 pci_disable_msi(adapter->pdev);
438}
439
440static void
441qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
442{
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000443 if (adapter->ahw->pci_base0 != NULL)
444 iounmap(adapter->ahw->pci_base0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000445}
446
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000447static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000448qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
449{
Dan Carpentere88db3b2010-08-09 21:49:36 +0000450 struct qlcnic_pci_info *pci_info;
Julia Lawall900853a2010-08-16 10:23:51 +0000451 int i, ret = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000452 u8 pfn;
453
Dan Carpentere88db3b2010-08-09 21:49:36 +0000454 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
455 if (!pci_info)
456 return -ENOMEM;
457
Dan Carpenterca315ac2010-08-09 21:47:56 +0000458 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000459 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
Dan Carpentere88db3b2010-08-09 21:49:36 +0000460 if (!adapter->npars) {
Julia Lawall900853a2010-08-16 10:23:51 +0000461 ret = -ENOMEM;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000462 goto err_pci_info;
463 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000464
Dan Carpenterca315ac2010-08-09 21:47:56 +0000465 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000466 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
467 if (!adapter->eswitch) {
Julia Lawall900853a2010-08-16 10:23:51 +0000468 ret = -ENOMEM;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000469 goto err_npars;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000470 }
471
472 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000473 if (ret)
474 goto err_eswitch;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000475
Dan Carpenterca315ac2010-08-09 21:47:56 +0000476 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
477 pfn = pci_info[i].id;
Sony Chackof848d6d2011-04-01 14:27:59 +0000478 if (pfn > QLCNIC_MAX_PCI_FUNC) {
479 ret = QL_STATUS_INVALID_PARAM;
480 goto err_eswitch;
481 }
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000482 adapter->npars[pfn].active = (u8)pci_info[i].active;
483 adapter->npars[pfn].type = (u8)pci_info[i].type;
484 adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000485 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
486 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000487 }
488
Dan Carpenterca315ac2010-08-09 21:47:56 +0000489 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
490 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
491
Dan Carpentere88db3b2010-08-09 21:49:36 +0000492 kfree(pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000493 return 0;
494
495err_eswitch:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000496 kfree(adapter->eswitch);
497 adapter->eswitch = NULL;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000498err_npars:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000499 kfree(adapter->npars);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000500 adapter->npars = NULL;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000501err_pci_info:
502 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000503
504 return ret;
505}
506
507static int
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000508qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
509{
510 u8 id;
511 u32 ref_count;
512 int i, ret = 1;
513 u32 data = QLCNIC_MGMT_FUNC;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000514 void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000515
516 /* If other drivers are not in use set their privilege level */
Amit Kumar Salecha31018e02010-08-25 04:03:05 +0000517 ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000518 ret = qlcnic_api_lock(adapter);
519 if (ret)
520 goto err_lock;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000521
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000522 if (qlcnic_config_npars) {
523 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000524 id = i;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000525 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000526 id == adapter->ahw->pci_func)
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000527 continue;
528 data |= (qlcnic_config_npars &
529 QLC_DEV_SET_DRV(0xf, id));
530 }
531 } else {
532 data = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000533 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000534 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000535 adapter->ahw->pci_func));
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000536 }
537 writel(data, priv_op);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000538 qlcnic_api_unlock(adapter);
539err_lock:
540 return ret;
541}
542
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000543static void
544qlcnic_check_vf(struct qlcnic_adapter *adapter)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000545{
546 void __iomem *msix_base_addr;
547 void __iomem *priv_op;
548 u32 func;
549 u32 msix_base;
550 u32 op_mode, priv_level;
551
552 /* Determine FW API version */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000553 adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
554 QLCNIC_FW_API);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000555
556 /* Find PCI function number */
557 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000558 msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000559 msix_base = readl(msix_base_addr);
560 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000561 adapter->ahw->pci_func = func;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000562
563 /* Determine function privilege level */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000564 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000565 op_mode = readl(priv_op);
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000566 if (op_mode == QLC_DEV_DRV_DEFAULT)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000567 priv_level = QLCNIC_MGMT_FUNC;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000568 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000569 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000570
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000571 if (priv_level == QLCNIC_NON_PRIV_FUNC) {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000572 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
573 dev_info(&adapter->pdev->dev,
574 "HAL Version: %d Non Privileged function\n",
575 adapter->fw_hal_version);
576 adapter->nic_ops = &qlcnic_vf_ops;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000577 } else
578 adapter->nic_ops = &qlcnic_ops;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000579}
580
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000581static int
582qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
583{
584 void __iomem *mem_ptr0 = NULL;
585 resource_size_t mem_base;
586 unsigned long mem_len, pci_len0 = 0;
587
588 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000589
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000590 /* remap phys address */
591 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
592 mem_len = pci_resource_len(pdev, 0);
593
594 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
595
596 mem_ptr0 = pci_ioremap_bar(pdev, 0);
597 if (mem_ptr0 == NULL) {
598 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
599 return -EIO;
600 }
601 pci_len0 = mem_len;
602 } else {
603 return -EIO;
604 }
605
606 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
607
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000608 adapter->ahw->pci_base0 = mem_ptr0;
609 adapter->ahw->pci_len0 = pci_len0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000610
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000611 qlcnic_check_vf(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000612
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000613 adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
614 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
615 adapter->ahw->pci_func)));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000616
617 return 0;
618}
619
620static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
621{
622 struct pci_dev *pdev = adapter->pdev;
623 int i, found = 0;
624
625 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
626 if (qlcnic_boards[i].vendor == pdev->vendor &&
627 qlcnic_boards[i].device == pdev->device &&
628 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
629 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +0000630 sprintf(name, "%pM: %s" ,
631 adapter->mac_addr,
632 qlcnic_boards[i].short_name);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000633 found = 1;
634 break;
635 }
636
637 }
638
639 if (!found)
Sritej Velaga7f9a0c32010-06-17 02:56:39 +0000640 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000641}
642
643static void
644qlcnic_check_options(struct qlcnic_adapter *adapter)
645{
646 u32 fw_major, fw_minor, fw_build;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000647 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000648
649 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
650 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
651 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
652
653 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
654
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000655 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
656 fw_major, fw_minor, fw_build);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000657 if (adapter->ahw->port_type == QLCNIC_XGBE) {
Sony Chacko90d19002010-10-26 17:53:08 +0000658 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
659 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
660 adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
661 } else {
662 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
663 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
664 }
665
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000666 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
Sony Chacko90d19002010-10-26 17:53:08 +0000667 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
668
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000669 } else if (adapter->ahw->port_type == QLCNIC_GBE) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000670 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
671 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
Sony Chacko90d19002010-10-26 17:53:08 +0000672 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
673 adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000674 }
675
676 adapter->msix_supported = !!use_msi_x;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000677
678 adapter->num_txd = MAX_CMD_DESCRIPTORS;
679
Sony Chacko251b0362010-08-19 05:08:24 +0000680 adapter->max_rds_rings = MAX_RDS_RINGS;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000681}
682
Rajesh Borundia174240a2010-08-31 17:17:47 +0000683static int
684qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
685{
686 int err;
687 struct qlcnic_info nic_info;
688
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000689 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
Rajesh Borundia174240a2010-08-31 17:17:47 +0000690 if (err)
691 return err;
692
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000693 adapter->physical_port = (u8)nic_info.phys_port;
Rajesh Borundia174240a2010-08-31 17:17:47 +0000694 adapter->switch_mode = nic_info.switch_mode;
695 adapter->max_tx_ques = nic_info.max_tx_ques;
696 adapter->max_rx_ques = nic_info.max_rx_ques;
697 adapter->capabilities = nic_info.capabilities;
698 adapter->max_mac_filters = nic_info.max_mac_filters;
699 adapter->max_mtu = nic_info.max_mtu;
700
701 if (adapter->capabilities & BIT_6)
702 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
703 else
704 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
705
706 return err;
707}
708
Rajesh Borundia0325d692010-08-19 05:08:26 +0000709static void
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000710qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
711 struct qlcnic_esw_func_cfg *esw_cfg)
712{
713 if (esw_cfg->discard_tagged)
714 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
715 else
716 adapter->flags |= QLCNIC_TAGGING_ENABLED;
717
718 if (esw_cfg->vlan_id)
719 adapter->pvid = esw_cfg->vlan_id;
720 else
721 adapter->pvid = 0;
722}
723
724static void
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000725qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
726{
727 struct qlcnic_adapter *adapter = netdev_priv(netdev);
728 set_bit(vid, adapter->vlans);
729}
730
731static void
732qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
733{
734 struct qlcnic_adapter *adapter = netdev_priv(netdev);
735
736 qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
737 clear_bit(vid, adapter->vlans);
738}
739
740static void
Rajesh Borundia0325d692010-08-19 05:08:26 +0000741qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
742 struct qlcnic_esw_func_cfg *esw_cfg)
743{
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000744 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
745 QLCNIC_PROMISC_DISABLED);
Rajesh Borundia7613c872010-08-31 17:17:48 +0000746
747 if (esw_cfg->mac_anti_spoof)
748 adapter->flags |= QLCNIC_MACSPOOF;
Sony Chackofe4d4342010-08-19 05:08:27 +0000749
Rajesh Borundia73733732010-08-31 17:17:50 +0000750 if (!esw_cfg->mac_override)
751 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
752
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000753 if (!esw_cfg->promisc_mode)
754 adapter->flags |= QLCNIC_PROMISC_DISABLED;
755
Rajesh Borundia0325d692010-08-19 05:08:26 +0000756 qlcnic_set_netdev_features(adapter, esw_cfg);
757}
758
759static int
760qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
761{
762 struct qlcnic_esw_func_cfg esw_cfg;
763
764 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
765 return 0;
766
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000767 esw_cfg.pci_func = adapter->ahw->pci_func;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000768 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
769 return -EIO;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000770 qlcnic_set_vlan_config(adapter, &esw_cfg);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000771 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
772
773 return 0;
774}
775
776static void
777qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
778 struct qlcnic_esw_func_cfg *esw_cfg)
779{
780 struct net_device *netdev = adapter->netdev;
781 unsigned long features, vlan_features;
782
Michał Mirosław135d84a2011-04-19 03:03:57 +0000783 features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
Rajesh Borundia0325d692010-08-19 05:08:26 +0000784 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
785 vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000786 NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000787
788 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
789 features |= (NETIF_F_TSO | NETIF_F_TSO6);
790 vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
791 }
Rajesh Borundiab56421d2011-04-01 14:28:31 +0000792
793 if (netdev->features & NETIF_F_LRO)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000794 features |= NETIF_F_LRO;
795
796 if (esw_cfg->offload_flags & BIT_0) {
797 netdev->features |= features;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000798 if (!(esw_cfg->offload_flags & BIT_1))
799 netdev->features &= ~NETIF_F_TSO;
800 if (!(esw_cfg->offload_flags & BIT_2))
801 netdev->features &= ~NETIF_F_TSO6;
802 } else {
803 netdev->features &= ~features;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000804 }
805
806 netdev->vlan_features = (features & vlan_features);
807}
808
809static int
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000810qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
811{
812 void __iomem *priv_op;
813 u32 op_mode, priv_level;
814 int err = 0;
815
Rajesh Borundia174240a2010-08-31 17:17:47 +0000816 err = qlcnic_initialize_nic(adapter);
817 if (err)
818 return err;
819
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000820 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
821 return 0;
822
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000823 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000824 op_mode = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000825 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000826
827 if (op_mode == QLC_DEV_DRV_DEFAULT)
828 priv_level = QLCNIC_MGMT_FUNC;
829 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000830 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000831
Rajesh Borundia174240a2010-08-31 17:17:47 +0000832 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000833 if (priv_level == QLCNIC_MGMT_FUNC) {
834 adapter->op_mode = QLCNIC_MGMT_FUNC;
835 err = qlcnic_init_pci_info(adapter);
836 if (err)
837 return err;
838 /* Set privilege level for other functions */
839 qlcnic_set_function_modes(adapter);
840 dev_info(&adapter->pdev->dev,
841 "HAL Version: %d, Management function\n",
842 adapter->fw_hal_version);
843 } else if (priv_level == QLCNIC_PRIV_FUNC) {
844 adapter->op_mode = QLCNIC_PRIV_FUNC;
845 dev_info(&adapter->pdev->dev,
846 "HAL Version: %d, Privileged function\n",
847 adapter->fw_hal_version);
848 }
Rajesh Borundia174240a2010-08-31 17:17:47 +0000849 }
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000850
851 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
852
853 return err;
854}
855
856static int
Rajesh Borundia0325d692010-08-19 05:08:26 +0000857qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
858{
859 struct qlcnic_esw_func_cfg esw_cfg;
860 struct qlcnic_npar_info *npar;
861 u8 i;
862
Rajesh Borundia174240a2010-08-31 17:17:47 +0000863 if (adapter->need_fw_reset)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000864 return 0;
865
866 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
867 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
868 continue;
869 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
870 esw_cfg.pci_func = i;
871 esw_cfg.offload_flags = BIT_0;
Rajesh Borundia73733732010-08-31 17:17:50 +0000872 esw_cfg.mac_override = BIT_0;
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000873 esw_cfg.promisc_mode = BIT_0;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000874 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
875 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
876 if (qlcnic_config_switch_port(adapter, &esw_cfg))
877 return -EIO;
878 npar = &adapter->npars[i];
879 npar->pvid = esw_cfg.vlan_id;
Rajesh Borundia73733732010-08-31 17:17:50 +0000880 npar->mac_override = esw_cfg.mac_override;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000881 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
882 npar->discard_tagged = esw_cfg.discard_tagged;
883 npar->promisc_mode = esw_cfg.promisc_mode;
884 npar->offload_flags = esw_cfg.offload_flags;
885 }
886
887 return 0;
888}
889
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000890static int
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000891qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
892 struct qlcnic_npar_info *npar, int pci_func)
893{
894 struct qlcnic_esw_func_cfg esw_cfg;
895 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
896 esw_cfg.pci_func = pci_func;
897 esw_cfg.vlan_id = npar->pvid;
Rajesh Borundia73733732010-08-31 17:17:50 +0000898 esw_cfg.mac_override = npar->mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000899 esw_cfg.discard_tagged = npar->discard_tagged;
900 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
901 esw_cfg.offload_flags = npar->offload_flags;
902 esw_cfg.promisc_mode = npar->promisc_mode;
903 if (qlcnic_config_switch_port(adapter, &esw_cfg))
904 return -EIO;
905
906 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
907 if (qlcnic_config_switch_port(adapter, &esw_cfg))
908 return -EIO;
909
910 return 0;
911}
912
913static int
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000914qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
915{
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000916 int i, err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000917 struct qlcnic_npar_info *npar;
918 struct qlcnic_info nic_info;
919
Rajesh Borundia174240a2010-08-31 17:17:47 +0000920 if (!adapter->need_fw_reset)
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000921 return 0;
922
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000923 /* Set the NPAR config data after FW reset */
924 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
925 npar = &adapter->npars[i];
926 if (npar->type != QLCNIC_TYPE_NIC)
927 continue;
928 err = qlcnic_get_nic_info(adapter, &nic_info, i);
929 if (err)
930 return err;
931 nic_info.min_tx_bw = npar->min_bw;
932 nic_info.max_tx_bw = npar->max_bw;
933 err = qlcnic_set_nic_info(adapter, &nic_info);
934 if (err)
935 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000936
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000937 if (npar->enable_pm) {
938 err = qlcnic_config_port_mirroring(adapter,
939 npar->dest_npar, 1, i);
940 if (err)
941 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000942 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000943 err = qlcnic_reset_eswitch_config(adapter, npar, i);
944 if (err)
945 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000946 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000947 return 0;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000948}
949
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +0000950static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
951{
952 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
953 u32 npar_state;
954
955 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
956 return 0;
957
958 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
959 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
960 msleep(1000);
961 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
962 }
963 if (!npar_opt_timeo) {
964 dev_err(&adapter->pdev->dev,
965 "Waiting for NPAR state to opertional timeout\n");
966 return -EIO;
967 }
968 return 0;
969}
970
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000971static int
Rajesh Borundia174240a2010-08-31 17:17:47 +0000972qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
973{
974 int err;
975
976 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
977 adapter->op_mode != QLCNIC_MGMT_FUNC)
978 return 0;
979
980 err = qlcnic_set_default_offload_settings(adapter);
981 if (err)
982 return err;
983
984 err = qlcnic_reset_npar_config(adapter);
985 if (err)
986 return err;
987
988 qlcnic_dev_set_npar_ready(adapter);
989
990 return err;
991}
992
993static int
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000994qlcnic_start_firmware(struct qlcnic_adapter *adapter)
995{
Sony Chackod4066832010-08-19 05:08:31 +0000996 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000997
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000998 err = qlcnic_can_start_firmware(adapter);
999 if (err < 0)
1000 return err;
1001 else if (!err)
Sony Chackod4066832010-08-19 05:08:31 +00001002 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001003
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +00001004 if (load_fw_file)
1005 qlcnic_request_firmware(adapter);
schacko8f891382010-06-17 02:56:40 +00001006 else {
Sony Chacko8cfdce02010-08-26 14:02:41 +00001007 err = qlcnic_check_flash_fw_ver(adapter);
1008 if (err)
schacko8f891382010-06-17 02:56:40 +00001009 goto err_out;
1010
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +00001011 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
schacko8f891382010-06-17 02:56:40 +00001012 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001013
1014 err = qlcnic_need_fw_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001015 if (err == 0)
Sony Chacko4e708122010-08-31 17:17:44 +00001016 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001017
Sony Chackod4066832010-08-19 05:08:31 +00001018 err = qlcnic_pinit_from_rom(adapter);
1019 if (err)
1020 goto err_out;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001021
1022 err = qlcnic_load_firmware(adapter);
1023 if (err)
1024 goto err_out;
1025
1026 qlcnic_release_firmware(adapter);
Sony Chackod4066832010-08-19 05:08:31 +00001027 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001028
Sony Chackod4066832010-08-19 05:08:31 +00001029check_fw_status:
1030 err = qlcnic_check_fw_status(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001031 if (err)
1032 goto err_out;
1033
1034 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001035 qlcnic_idc_debug_info(adapter, 1);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00001036
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001037 err = qlcnic_check_eswitch_mode(adapter);
1038 if (err) {
1039 dev_err(&adapter->pdev->dev,
1040 "Memory allocation failed for eswitch\n");
1041 goto err_out;
1042 }
Rajesh Borundia174240a2010-08-31 17:17:47 +00001043 err = qlcnic_set_mgmt_operations(adapter);
1044 if (err)
1045 goto err_out;
1046
1047 qlcnic_check_options(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001048 adapter->need_fw_reset = 0;
1049
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001050 qlcnic_release_firmware(adapter);
1051 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001052
1053err_out:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001054 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1055 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001056
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001057 qlcnic_release_firmware(adapter);
1058 return err;
1059}
1060
1061static int
1062qlcnic_request_irq(struct qlcnic_adapter *adapter)
1063{
1064 irq_handler_t handler;
1065 struct qlcnic_host_sds_ring *sds_ring;
1066 int err, ring;
1067
1068 unsigned long flags = 0;
1069 struct net_device *netdev = adapter->netdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001070 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001071
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001072 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1073 handler = qlcnic_tmp_intr;
1074 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1075 flags |= IRQF_SHARED;
1076
1077 } else {
1078 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1079 handler = qlcnic_msix_intr;
1080 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1081 handler = qlcnic_msi_intr;
1082 else {
1083 flags |= IRQF_SHARED;
1084 handler = qlcnic_intr;
1085 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001086 }
1087 adapter->irq = netdev->irq;
1088
1089 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1090 sds_ring = &recv_ctx->sds_rings[ring];
1091 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1092 err = request_irq(sds_ring->irq, handler,
1093 flags, sds_ring->name, sds_ring);
1094 if (err)
1095 return err;
1096 }
1097
1098 return 0;
1099}
1100
1101static void
1102qlcnic_free_irq(struct qlcnic_adapter *adapter)
1103{
1104 int ring;
1105 struct qlcnic_host_sds_ring *sds_ring;
1106
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001107 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001108
1109 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1110 sds_ring = &recv_ctx->sds_rings[ring];
1111 free_irq(sds_ring->irq, sds_ring);
1112 }
1113}
1114
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001115static int
1116__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1117{
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001118 int ring;
1119 struct qlcnic_host_rds_ring *rds_ring;
1120
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001121 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1122 return -EIO;
1123
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001124 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1125 return 0;
Rajesh Borundia0325d692010-08-19 05:08:26 +00001126 if (qlcnic_set_eswitch_port_config(adapter))
1127 return -EIO;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001128
1129 if (qlcnic_fw_create_ctx(adapter))
1130 return -EIO;
1131
1132 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001133 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1134 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001135 }
1136
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001137 qlcnic_set_multi(netdev);
1138 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1139
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001140 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001141
1142 if (adapter->max_sds_rings > 1)
1143 qlcnic_config_rss(adapter, 1);
1144
1145 qlcnic_config_intr_coalesce(adapter);
1146
Sucheta Chakraborty24763d82010-08-17 00:34:25 +00001147 if (netdev->features & NETIF_F_LRO)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001148 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1149
1150 qlcnic_napi_enable(adapter);
1151
1152 qlcnic_linkevent_request(adapter, 1);
1153
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001154 adapter->reset_context = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001155 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1156 return 0;
1157}
1158
1159/* Usage: During resume and firmware recovery module.*/
1160
1161static int
1162qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1163{
1164 int err = 0;
1165
1166 rtnl_lock();
1167 if (netif_running(netdev))
1168 err = __qlcnic_up(adapter, netdev);
1169 rtnl_unlock();
1170
1171 return err;
1172}
1173
1174static void
1175__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1176{
1177 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1178 return;
1179
1180 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1181 return;
1182
1183 smp_mb();
1184 spin_lock(&adapter->tx_clean_lock);
1185 netif_carrier_off(netdev);
1186 netif_tx_disable(netdev);
1187
1188 qlcnic_free_mac_list(adapter);
1189
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001190 if (adapter->fhash.fnum)
1191 qlcnic_delete_lb_filters(adapter);
1192
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001193 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1194
1195 qlcnic_napi_disable(adapter);
1196
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001197 qlcnic_fw_destroy_ctx(adapter);
1198
1199 qlcnic_reset_rx_buffers_list(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001200 qlcnic_release_tx_buffers(adapter);
1201 spin_unlock(&adapter->tx_clean_lock);
1202}
1203
1204/* Usage: During suspend and firmware recovery module */
1205
1206static void
1207qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1208{
1209 rtnl_lock();
1210 if (netif_running(netdev))
1211 __qlcnic_down(adapter, netdev);
1212 rtnl_unlock();
1213
1214}
1215
1216static int
1217qlcnic_attach(struct qlcnic_adapter *adapter)
1218{
1219 struct net_device *netdev = adapter->netdev;
1220 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001221 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001222
1223 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1224 return 0;
1225
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001226 err = qlcnic_napi_add(adapter, netdev);
1227 if (err)
1228 return err;
1229
1230 err = qlcnic_alloc_sw_resources(adapter);
1231 if (err) {
1232 dev_err(&pdev->dev, "Error in setting sw resources\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001233 goto err_out_napi_del;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001234 }
1235
1236 err = qlcnic_alloc_hw_resources(adapter);
1237 if (err) {
1238 dev_err(&pdev->dev, "Error in setting hw resources\n");
1239 goto err_out_free_sw;
1240 }
1241
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001242 err = qlcnic_request_irq(adapter);
1243 if (err) {
1244 dev_err(&pdev->dev, "failed to setup interrupt\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001245 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001246 }
1247
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001248 qlcnic_create_sysfs_entries(adapter);
1249
1250 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1251 return 0;
1252
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001253err_out_free_hw:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001254 qlcnic_free_hw_resources(adapter);
1255err_out_free_sw:
1256 qlcnic_free_sw_resources(adapter);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001257err_out_napi_del:
1258 qlcnic_napi_del(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001259 return err;
1260}
1261
1262static void
1263qlcnic_detach(struct qlcnic_adapter *adapter)
1264{
1265 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1266 return;
1267
1268 qlcnic_remove_sysfs_entries(adapter);
1269
1270 qlcnic_free_hw_resources(adapter);
1271 qlcnic_release_rx_buffers(adapter);
1272 qlcnic_free_irq(adapter);
1273 qlcnic_napi_del(adapter);
1274 qlcnic_free_sw_resources(adapter);
1275
1276 adapter->is_up = 0;
1277}
1278
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001279void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1280{
1281 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1282 struct qlcnic_host_sds_ring *sds_ring;
1283 int ring;
1284
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001285 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001286 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1287 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001288 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001289 qlcnic_disable_int(sds_ring);
1290 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001291 }
1292
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001293 qlcnic_fw_destroy_ctx(adapter);
1294
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001295 qlcnic_detach(adapter);
1296
1297 adapter->diag_test = 0;
1298 adapter->max_sds_rings = max_sds_rings;
1299
1300 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001301 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001302
1303 if (netif_running(netdev))
1304 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001305out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001306 netif_device_attach(netdev);
1307}
1308
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001309static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1310{
1311 int err = 0;
1312 adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1313 GFP_KERNEL);
1314 if (!adapter->ahw) {
1315 dev_err(&adapter->pdev->dev,
1316 "Failed to allocate recv ctx resources for adapter\n");
1317 err = -ENOMEM;
1318 goto err_out;
1319 }
1320 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1321 GFP_KERNEL);
1322 if (!adapter->recv_ctx) {
1323 dev_err(&adapter->pdev->dev,
1324 "Failed to allocate recv ctx resources for adapter\n");
1325 kfree(adapter->ahw);
1326 adapter->ahw = NULL;
1327 err = -ENOMEM;
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001328 goto err_out;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001329 }
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001330 /* Initialize interrupt coalesce parameters */
1331 adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1332 adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1333 adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001334err_out:
1335 return err;
1336}
1337
1338static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1339{
1340 kfree(adapter->recv_ctx);
1341 adapter->recv_ctx = NULL;
1342
Anirban Chakraborty18f2f612011-05-12 12:48:33 +00001343 if (adapter->ahw->fw_dump.tmpl_hdr) {
1344 vfree(adapter->ahw->fw_dump.tmpl_hdr);
1345 adapter->ahw->fw_dump.tmpl_hdr = NULL;
1346 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001347 kfree(adapter->ahw);
1348 adapter->ahw = NULL;
1349}
1350
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001351int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1352{
1353 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1354 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001355 struct qlcnic_host_rds_ring *rds_ring;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001356 int ring;
1357 int ret;
1358
1359 netif_device_detach(netdev);
1360
1361 if (netif_running(netdev))
1362 __qlcnic_down(adapter, netdev);
1363
1364 qlcnic_detach(adapter);
1365
1366 adapter->max_sds_rings = 1;
1367 adapter->diag_test = test;
1368
1369 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001370 if (ret) {
1371 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001372 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001373 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001374
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001375 ret = qlcnic_fw_create_ctx(adapter);
1376 if (ret) {
1377 qlcnic_detach(adapter);
Sony Chacko57e46242010-07-24 18:32:18 +00001378 netif_device_attach(netdev);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001379 return ret;
1380 }
1381
1382 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001383 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1384 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001385 }
1386
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001387 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1388 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001389 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001390 qlcnic_enable_int(sds_ring);
1391 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001392 }
Sucheta Chakraborty22c8c932011-06-22 02:52:23 +00001393
1394 if (adapter->diag_test == QLCNIC_LOOPBACK_TEST) {
1395 adapter->ahw->loopback_state = 0;
1396 qlcnic_linkevent_request(adapter, 1);
1397 }
1398
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001399 set_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001400
1401 return 0;
1402}
1403
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001404/* Reset context in hardware only */
1405static int
1406qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1407{
1408 struct net_device *netdev = adapter->netdev;
1409
1410 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1411 return -EBUSY;
1412
1413 netif_device_detach(netdev);
1414
1415 qlcnic_down(adapter, netdev);
1416
1417 qlcnic_up(adapter, netdev);
1418
1419 netif_device_attach(netdev);
1420
1421 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1422 return 0;
1423}
1424
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001425int
1426qlcnic_reset_context(struct qlcnic_adapter *adapter)
1427{
1428 int err = 0;
1429 struct net_device *netdev = adapter->netdev;
1430
1431 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1432 return -EBUSY;
1433
1434 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1435
1436 netif_device_detach(netdev);
1437
1438 if (netif_running(netdev))
1439 __qlcnic_down(adapter, netdev);
1440
1441 qlcnic_detach(adapter);
1442
1443 if (netif_running(netdev)) {
1444 err = qlcnic_attach(adapter);
1445 if (!err)
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001446 __qlcnic_up(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001447 }
1448
1449 netif_device_attach(netdev);
1450 }
1451
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001452 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1453 return err;
1454}
1455
1456static int
1457qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001458 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001459{
1460 int err;
1461 struct pci_dev *pdev = adapter->pdev;
1462
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001463 adapter->mc_enabled = 0;
1464 adapter->max_mc_count = 38;
1465
1466 netdev->netdev_ops = &qlcnic_netdev_ops;
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001467 netdev->watchdog_timeo = 5*HZ;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001468
1469 qlcnic_change_mtu(netdev, netdev->mtu);
1470
1471 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1472
Michał Mirosław135d84a2011-04-19 03:03:57 +00001473 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1474 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
Anirban Chakrabortyac8d0c42010-07-09 13:14:58 +00001475
Michał Mirosław135d84a2011-04-19 03:03:57 +00001476 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1477 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1478 if (pci_using_dac)
1479 netdev->hw_features |= NETIF_F_HIGHDMA;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001480
Michał Mirosław135d84a2011-04-19 03:03:57 +00001481 netdev->vlan_features = netdev->hw_features;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001482
1483 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
Michał Mirosław135d84a2011-04-19 03:03:57 +00001484 netdev->hw_features |= NETIF_F_HW_VLAN_TX;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001485 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
Michał Mirosław135d84a2011-04-19 03:03:57 +00001486 netdev->hw_features |= NETIF_F_LRO;
1487
1488 netdev->features |= netdev->hw_features |
1489 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1490
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001491 netdev->irq = adapter->msix_entries[0].vector;
1492
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001493 err = register_netdev(netdev);
1494 if (err) {
1495 dev_err(&pdev->dev, "failed to register net device\n");
1496 return err;
1497 }
1498
1499 return 0;
1500}
1501
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001502static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1503{
1504 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1505 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1506 *pci_using_dac = 1;
1507 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1508 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1509 *pci_using_dac = 0;
1510 else {
1511 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1512 return -EIO;
1513 }
1514
1515 return 0;
1516}
1517
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001518static int
1519qlcnic_alloc_msix_entries(struct qlcnic_adapter *adapter, u16 count)
1520{
1521 adapter->msix_entries = kcalloc(count, sizeof(struct msix_entry),
1522 GFP_KERNEL);
1523
1524 if (adapter->msix_entries)
1525 return 0;
1526
1527 dev_err(&adapter->pdev->dev, "failed allocating msix_entries\n");
1528 return -ENOMEM;
1529}
1530
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001531static int __devinit
1532qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1533{
1534 struct net_device *netdev = NULL;
1535 struct qlcnic_adapter *adapter = NULL;
1536 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001537 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001538 uint8_t pci_using_dac;
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001539 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001540
1541 err = pci_enable_device(pdev);
1542 if (err)
1543 return err;
1544
1545 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1546 err = -ENODEV;
1547 goto err_out_disable_pdev;
1548 }
1549
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001550 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1551 if (err)
1552 goto err_out_disable_pdev;
1553
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001554 err = pci_request_regions(pdev, qlcnic_driver_name);
1555 if (err)
1556 goto err_out_disable_pdev;
1557
1558 pci_set_master(pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001559 pci_enable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001560
1561 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1562 if (!netdev) {
1563 dev_err(&pdev->dev, "failed to allocate net_device\n");
1564 err = -ENOMEM;
1565 goto err_out_free_res;
1566 }
1567
1568 SET_NETDEV_DEV(netdev, &pdev->dev);
1569
1570 adapter = netdev_priv(netdev);
1571 adapter->netdev = netdev;
1572 adapter->pdev = pdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001573
1574 if (qlcnic_alloc_adapter_resources(adapter))
1575 goto err_out_free_netdev;
1576
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001577 adapter->dev_rst_time = jiffies;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001578 revision_id = pdev->revision;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001579 adapter->ahw->revision_id = revision_id;
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001580 adapter->mac_learn = qlcnic_mac_learn;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001581
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001582 rwlock_init(&adapter->ahw->crb_lock);
1583 mutex_init(&adapter->ahw->mem_lock);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001584
1585 spin_lock_init(&adapter->tx_clean_lock);
1586 INIT_LIST_HEAD(&adapter->mac_list);
1587
1588 err = qlcnic_setup_pci_map(adapter);
1589 if (err)
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001590 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001591
1592 /* This will be reset for mezz cards */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001593 adapter->portnum = adapter->ahw->pci_func;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001594
1595 err = qlcnic_get_board_info(adapter);
1596 if (err) {
1597 dev_err(&pdev->dev, "Error getting board config info.\n");
1598 goto err_out_iounmap;
1599 }
1600
Sony Chacko8cfdce02010-08-26 14:02:41 +00001601 err = qlcnic_setup_idc_param(adapter);
1602 if (err)
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +00001603 goto err_out_iounmap;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001604
Rajesh Borundia1dc0f3c2010-12-16 22:59:02 +00001605 adapter->flags |= QLCNIC_NEED_FLR;
Rajesh Borundiab0044bc2010-11-23 01:25:21 +00001606
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001607 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001608 if (err) {
1609 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001610 goto err_out_decr_ref;
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001611 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001612
Sritej Velaga602ca6f2011-06-22 02:52:17 +00001613 /* Get FW dump template and store it */
1614 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC)
1615 if (!qlcnic_fw_cmd_get_minidump_temp(adapter))
1616 dev_info(&pdev->dev,
1617 "Supports FW dump capability\n");
1618
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001619 if (qlcnic_read_mac_addr(adapter))
1620 dev_warn(&pdev->dev, "failed to read mac addr\n");
1621
1622 if (adapter->portnum == 0) {
1623 get_brd_name(adapter, brd_name);
1624
1625 pr_info("%s: %s Board Chip rev 0x%x\n",
1626 module_name(THIS_MODULE),
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001627 brd_name, adapter->ahw->revision_id);
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001628 }
1629
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001630 qlcnic_clear_stats(adapter);
1631
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001632 err = qlcnic_alloc_msix_entries(adapter, adapter->max_rx_ques);
1633 if (err)
1634 goto err_out_decr_ref;
1635
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001636 qlcnic_setup_intr(adapter);
1637
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001638 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001639 if (err)
1640 goto err_out_disable_msi;
1641
1642 pci_set_drvdata(pdev, adapter);
1643
1644 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1645
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001646 switch (adapter->ahw->port_type) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001647 case QLCNIC_GBE:
1648 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1649 adapter->netdev->name);
1650 break;
1651 case QLCNIC_XGBE:
1652 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1653 adapter->netdev->name);
1654 break;
1655 }
1656
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001657 if (adapter->mac_learn)
1658 qlcnic_alloc_lb_filters_mem(adapter);
1659
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001660 qlcnic_create_diag_entries(adapter);
1661
1662 return 0;
1663
1664err_out_disable_msi:
1665 qlcnic_teardown_intr(adapter);
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001666 kfree(adapter->msix_entries);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001667
1668err_out_decr_ref:
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001669 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001670
1671err_out_iounmap:
1672 qlcnic_cleanup_pci_map(adapter);
1673
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001674err_out_free_hw:
1675 qlcnic_free_adapter_resources(adapter);
1676
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001677err_out_free_netdev:
1678 free_netdev(netdev);
1679
1680err_out_free_res:
1681 pci_release_regions(pdev);
1682
1683err_out_disable_pdev:
1684 pci_set_drvdata(pdev, NULL);
1685 pci_disable_device(pdev);
1686 return err;
1687}
1688
1689static void __devexit qlcnic_remove(struct pci_dev *pdev)
1690{
1691 struct qlcnic_adapter *adapter;
1692 struct net_device *netdev;
1693
1694 adapter = pci_get_drvdata(pdev);
1695 if (adapter == NULL)
1696 return;
1697
1698 netdev = adapter->netdev;
1699
1700 qlcnic_cancel_fw_work(adapter);
1701
1702 unregister_netdev(netdev);
1703
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001704 qlcnic_detach(adapter);
1705
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001706 if (adapter->npars != NULL)
1707 kfree(adapter->npars);
1708 if (adapter->eswitch != NULL)
1709 kfree(adapter->eswitch);
1710
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001711 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001712
1713 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1714
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001715 qlcnic_free_lb_filters_mem(adapter);
1716
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001717 qlcnic_teardown_intr(adapter);
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001718 kfree(adapter->msix_entries);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001719
1720 qlcnic_remove_diag_entries(adapter);
1721
1722 qlcnic_cleanup_pci_map(adapter);
1723
1724 qlcnic_release_firmware(adapter);
1725
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001726 pci_disable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001727 pci_release_regions(pdev);
1728 pci_disable_device(pdev);
1729 pci_set_drvdata(pdev, NULL);
1730
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001731 qlcnic_free_adapter_resources(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001732 free_netdev(netdev);
1733}
1734static int __qlcnic_shutdown(struct pci_dev *pdev)
1735{
1736 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1737 struct net_device *netdev = adapter->netdev;
1738 int retval;
1739
1740 netif_device_detach(netdev);
1741
1742 qlcnic_cancel_fw_work(adapter);
1743
1744 if (netif_running(netdev))
1745 qlcnic_down(adapter, netdev);
1746
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001747 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001748
1749 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1750
1751 retval = pci_save_state(pdev);
1752 if (retval)
1753 return retval;
1754
1755 if (qlcnic_wol_supported(adapter)) {
1756 pci_enable_wake(pdev, PCI_D3cold, 1);
1757 pci_enable_wake(pdev, PCI_D3hot, 1);
1758 }
1759
1760 return 0;
1761}
1762
1763static void qlcnic_shutdown(struct pci_dev *pdev)
1764{
1765 if (__qlcnic_shutdown(pdev))
1766 return;
1767
1768 pci_disable_device(pdev);
1769}
1770
1771#ifdef CONFIG_PM
1772static int
1773qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1774{
1775 int retval;
1776
1777 retval = __qlcnic_shutdown(pdev);
1778 if (retval)
1779 return retval;
1780
1781 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1782 return 0;
1783}
1784
1785static int
1786qlcnic_resume(struct pci_dev *pdev)
1787{
1788 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1789 struct net_device *netdev = adapter->netdev;
1790 int err;
1791
1792 err = pci_enable_device(pdev);
1793 if (err)
1794 return err;
1795
1796 pci_set_power_state(pdev, PCI_D0);
1797 pci_set_master(pdev);
1798 pci_restore_state(pdev);
1799
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001800 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001801 if (err) {
1802 dev_err(&pdev->dev, "failed to start firmware\n");
1803 return err;
1804 }
1805
1806 if (netif_running(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001807 err = qlcnic_up(adapter, netdev);
1808 if (err)
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001809 goto done;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001810
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00001811 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001812 }
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001813done:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001814 netif_device_attach(netdev);
1815 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1816 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001817}
1818#endif
1819
1820static int qlcnic_open(struct net_device *netdev)
1821{
1822 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1823 int err;
1824
Amit Kumar Salechac55ad8e2011-06-22 02:52:21 +00001825 netif_carrier_off(netdev);
1826
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001827 err = qlcnic_attach(adapter);
1828 if (err)
1829 return err;
1830
1831 err = __qlcnic_up(adapter, netdev);
1832 if (err)
1833 goto err_out;
1834
1835 netif_start_queue(netdev);
1836
1837 return 0;
1838
1839err_out:
1840 qlcnic_detach(adapter);
1841 return err;
1842}
1843
1844/*
1845 * qlcnic_close - Disables a network interface entry point
1846 */
1847static int qlcnic_close(struct net_device *netdev)
1848{
1849 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1850
1851 __qlcnic_down(adapter, netdev);
1852 return 0;
1853}
1854
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001855void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001856{
1857 void *head;
1858 int i;
1859
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00001860 if (adapter->fhash.fmax && adapter->fhash.fhead)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001861 return;
1862
1863 spin_lock_init(&adapter->mac_learn_lock);
1864
1865 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1866 GFP_KERNEL);
1867 if (!head)
1868 return;
1869
1870 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
Joe Perches43d620c2011-06-16 19:08:06 +00001871 adapter->fhash.fhead = head;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001872
1873 for (i = 0; i < adapter->fhash.fmax; i++)
1874 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1875}
1876
1877static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1878{
1879 if (adapter->fhash.fmax && adapter->fhash.fhead)
1880 kfree(adapter->fhash.fhead);
1881
1882 adapter->fhash.fhead = NULL;
1883 adapter->fhash.fmax = 0;
1884}
1885
1886static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001887 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001888{
1889 struct cmd_desc_type0 *hwdesc;
1890 struct qlcnic_nic_req *req;
1891 struct qlcnic_mac_req *mac_req;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001892 struct qlcnic_vlan_req *vlan_req;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001893 u32 producer;
1894 u64 word;
1895
1896 producer = tx_ring->producer;
1897 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1898
1899 req = (struct qlcnic_nic_req *)hwdesc;
1900 memset(req, 0, sizeof(struct qlcnic_nic_req));
1901 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1902
1903 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1904 req->req_hdr = cpu_to_le64(word);
1905
1906 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001907 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001908 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1909
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001910 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1911 vlan_req->vlan_id = vlan_id;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001912
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001913 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001914 smp_mb();
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001915}
1916
1917#define QLCNIC_MAC_HASH(MAC)\
1918 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1919
1920static void
1921qlcnic_send_filter(struct qlcnic_adapter *adapter,
1922 struct qlcnic_host_tx_ring *tx_ring,
1923 struct cmd_desc_type0 *first_desc,
1924 struct sk_buff *skb)
1925{
1926 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1927 struct qlcnic_filter *fil, *tmp_fil;
1928 struct hlist_node *tmp_hnode, *n;
1929 struct hlist_head *head;
1930 u64 src_addr = 0;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001931 __le16 vlan_id = 0;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001932 u8 hindex;
1933
1934 if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1935 return;
1936
1937 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1938 return;
1939
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001940 /* Only NPAR capable devices support vlan based learning*/
1941 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1942 vlan_id = first_desc->vlan_TCI;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001943 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1944 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1945 head = &(adapter->fhash.fhead[hindex]);
1946
1947 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001948 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1949 tmp_fil->vlan_id == vlan_id) {
amit salechae5edb7b2010-10-26 17:53:07 +00001950
1951 if (jiffies >
1952 (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1953 qlcnic_change_filter(adapter, src_addr, vlan_id,
1954 tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001955 tmp_fil->ftime = jiffies;
1956 return;
1957 }
1958 }
1959
1960 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1961 if (!fil)
1962 return;
1963
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001964 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001965
1966 fil->ftime = jiffies;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001967 fil->vlan_id = vlan_id;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001968 memcpy(fil->faddr, &src_addr, ETH_ALEN);
1969 spin_lock(&adapter->mac_learn_lock);
1970 hlist_add_head(&(fil->fnode), head);
1971 adapter->fhash.fnum++;
1972 spin_unlock(&adapter->mac_learn_lock);
1973}
1974
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001975static int
1976qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001977 struct cmd_desc_type0 *first_desc,
1978 struct sk_buff *skb)
1979{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001980 u8 opcode = 0, hdr_len = 0;
1981 u16 flags = 0, vlan_tci = 0;
1982 int copied, offset, copy_len;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001983 struct cmd_desc_type0 *hwdesc;
1984 struct vlan_ethhdr *vh;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001985 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1986 u16 protocol = ntohs(skb->protocol);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001987 u32 producer = tx_ring->producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001988
1989 if (protocol == ETH_P_8021Q) {
1990 vh = (struct vlan_ethhdr *)skb->data;
1991 flags = FLAGS_VLAN_TAGGED;
1992 vlan_tci = vh->h_vlan_TCI;
1993 } else if (vlan_tx_tag_present(skb)) {
1994 flags = FLAGS_VLAN_OOB;
1995 vlan_tci = vlan_tx_tag_get(skb);
1996 }
1997 if (unlikely(adapter->pvid)) {
1998 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
1999 return -EIO;
2000 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
2001 goto set_flags;
2002
2003 flags = FLAGS_VLAN_OOB;
2004 vlan_tci = adapter->pvid;
2005 }
2006set_flags:
2007 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
2008 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002009
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00002010 if (*(skb->data) & BIT_0) {
2011 flags |= BIT_0;
2012 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
2013 }
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002014 opcode = TX_ETHER_PKT;
2015 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002016 skb_shinfo(skb)->gso_size > 0) {
2017
2018 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2019
2020 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2021 first_desc->total_hdr_length = hdr_len;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002022
2023 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
2024
2025 /* For LSO, we need to copy the MAC/IP/TCP headers into
2026 * the descriptor ring */
2027 copied = 0;
2028 offset = 2;
2029
2030 if (flags & FLAGS_VLAN_OOB) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002031 first_desc->total_hdr_length += VLAN_HLEN;
2032 first_desc->tcp_hdr_offset = VLAN_HLEN;
2033 first_desc->ip_hdr_offset = VLAN_HLEN;
2034 /* Only in case of TSO on vlan device */
2035 flags |= FLAGS_VLAN_TAGGED;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002036
2037 /* Create a TSO vlan header template for firmware */
2038
2039 hwdesc = &tx_ring->desc_head[producer];
2040 tx_ring->cmd_buf_arr[producer].skb = NULL;
2041
2042 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2043 offset, hdr_len + VLAN_HLEN);
2044
2045 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
2046 skb_copy_from_linear_data(skb, vh, 12);
2047 vh->h_vlan_proto = htons(ETH_P_8021Q);
2048 vh->h_vlan_TCI = htons(vlan_tci);
2049
2050 skb_copy_from_linear_data_offset(skb, 12,
2051 (char *)vh + 16, copy_len - 16);
2052
2053 copied = copy_len - VLAN_HLEN;
2054 offset = 0;
2055
2056 producer = get_next_index(producer, tx_ring->num_desc);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002057 }
2058
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002059 while (copied < hdr_len) {
2060
2061 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2062 offset, (hdr_len - copied));
2063
2064 hwdesc = &tx_ring->desc_head[producer];
2065 tx_ring->cmd_buf_arr[producer].skb = NULL;
2066
2067 skb_copy_from_linear_data_offset(skb, copied,
2068 (char *) hwdesc + offset, copy_len);
2069
2070 copied += copy_len;
2071 offset = 0;
2072
2073 producer = get_next_index(producer, tx_ring->num_desc);
2074 }
2075
2076 tx_ring->producer = producer;
2077 smp_mb();
2078 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002079
2080 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2081 u8 l4proto;
2082
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002083 if (protocol == ETH_P_IP) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002084 l4proto = ip_hdr(skb)->protocol;
2085
2086 if (l4proto == IPPROTO_TCP)
2087 opcode = TX_TCP_PKT;
2088 else if (l4proto == IPPROTO_UDP)
2089 opcode = TX_UDP_PKT;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002090 } else if (protocol == ETH_P_IPV6) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002091 l4proto = ipv6_hdr(skb)->nexthdr;
2092
2093 if (l4proto == IPPROTO_TCP)
2094 opcode = TX_TCPV6_PKT;
2095 else if (l4proto == IPPROTO_UDP)
2096 opcode = TX_UDPV6_PKT;
2097 }
2098 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002099 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2100 first_desc->ip_hdr_offset += skb_network_offset(skb);
2101 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2102
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002103 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002104}
2105
2106static int
2107qlcnic_map_tx_skb(struct pci_dev *pdev,
2108 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2109{
2110 struct qlcnic_skb_frag *nf;
2111 struct skb_frag_struct *frag;
2112 int i, nr_frags;
2113 dma_addr_t map;
2114
2115 nr_frags = skb_shinfo(skb)->nr_frags;
2116 nf = &pbuf->frag_array[0];
2117
2118 map = pci_map_single(pdev, skb->data,
2119 skb_headlen(skb), PCI_DMA_TODEVICE);
2120 if (pci_dma_mapping_error(pdev, map))
2121 goto out_err;
2122
2123 nf->dma = map;
2124 nf->length = skb_headlen(skb);
2125
2126 for (i = 0; i < nr_frags; i++) {
2127 frag = &skb_shinfo(skb)->frags[i];
2128 nf = &pbuf->frag_array[i+1];
2129
2130 map = pci_map_page(pdev, frag->page, frag->page_offset,
2131 frag->size, PCI_DMA_TODEVICE);
2132 if (pci_dma_mapping_error(pdev, map))
2133 goto unwind;
2134
2135 nf->dma = map;
2136 nf->length = frag->size;
2137 }
2138
2139 return 0;
2140
2141unwind:
2142 while (--i >= 0) {
2143 nf = &pbuf->frag_array[i+1];
2144 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2145 }
2146
2147 nf = &pbuf->frag_array[0];
2148 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2149
2150out_err:
2151 return -ENOMEM;
2152}
2153
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002154static void
2155qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2156 struct qlcnic_cmd_buffer *pbuf)
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002157{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002158 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2159 int nr_frags = skb_shinfo(skb)->nr_frags;
2160 int i;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002161
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002162 for (i = 0; i < nr_frags; i++) {
2163 nf = &pbuf->frag_array[i+1];
2164 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002165 }
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002166
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002167 nf = &pbuf->frag_array[0];
2168 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
Sucheta Chakraborty5b446c62011-06-03 05:52:18 +00002169 pbuf->skb = NULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002170}
2171
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002172static inline void
2173qlcnic_clear_cmddesc(u64 *desc)
2174{
2175 desc[0] = 0ULL;
2176 desc[2] = 0ULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002177 desc[7] = 0ULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002178}
2179
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00002180netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002181qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2182{
2183 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2184 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2185 struct qlcnic_cmd_buffer *pbuf;
2186 struct qlcnic_skb_frag *buffrag;
2187 struct cmd_desc_type0 *hwdesc, *first_desc;
2188 struct pci_dev *pdev;
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002189 struct ethhdr *phdr;
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002190 int delta = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002191 int i, k;
2192
2193 u32 producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002194 int frag_count;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002195 u32 num_txd = tx_ring->num_desc;
2196
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00002197 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2198 netif_stop_queue(netdev);
2199 return NETDEV_TX_BUSY;
2200 }
2201
Sony Chackofe4d4342010-08-19 05:08:27 +00002202 if (adapter->flags & QLCNIC_MACSPOOF) {
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002203 phdr = (struct ethhdr *)skb->data;
2204 if (compare_ether_addr(phdr->h_source,
Sony Chackofe4d4342010-08-19 05:08:27 +00002205 adapter->mac_addr))
2206 goto drop_packet;
2207 }
2208
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002209 frag_count = skb_shinfo(skb)->nr_frags + 1;
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002210 /* 14 frags supported for normal packet and
2211 * 32 frags supported for TSO packet
2212 */
2213 if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
2214
2215 for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
2216 delta += skb_shinfo(skb)->frags[i].size;
2217
2218 if (!__pskb_pull_tail(skb, delta))
2219 goto drop_packet;
2220
2221 frag_count = 1 + skb_shinfo(skb)->nr_frags;
2222 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002223
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002224 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002225 netif_stop_queue(netdev);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002226 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2227 netif_start_queue(netdev);
2228 else {
2229 adapter->stats.xmit_off++;
2230 return NETDEV_TX_BUSY;
2231 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002232 }
2233
2234 producer = tx_ring->producer;
2235 pbuf = &tx_ring->cmd_buf_arr[producer];
2236
2237 pdev = adapter->pdev;
2238
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002239 first_desc = hwdesc = &tx_ring->desc_head[producer];
2240 qlcnic_clear_cmddesc((u64 *)hwdesc);
2241
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002242 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2243 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002244 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002245 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002246
2247 pbuf->skb = skb;
2248 pbuf->frag_count = frag_count;
2249
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002250 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2251 qlcnic_set_tx_port(first_desc, adapter->portnum);
2252
2253 for (i = 0; i < frag_count; i++) {
2254
2255 k = i % 4;
2256
2257 if ((k == 0) && (i > 0)) {
2258 /* move to next desc.*/
2259 producer = get_next_index(producer, num_txd);
2260 hwdesc = &tx_ring->desc_head[producer];
2261 qlcnic_clear_cmddesc((u64 *)hwdesc);
2262 tx_ring->cmd_buf_arr[producer].skb = NULL;
2263 }
2264
2265 buffrag = &pbuf->frag_array[i];
2266
2267 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2268 switch (k) {
2269 case 0:
2270 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2271 break;
2272 case 1:
2273 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2274 break;
2275 case 2:
2276 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2277 break;
2278 case 3:
2279 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2280 break;
2281 }
2282 }
2283
2284 tx_ring->producer = get_next_index(producer, num_txd);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002285 smp_mb();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002286
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002287 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2288 goto unwind_buff;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002289
Sucheta Chakrabortye5dcf6d2011-07-14 03:16:52 +00002290 if (adapter->mac_learn)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002291 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2292
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002293 qlcnic_update_cmd_producer(adapter, tx_ring);
2294
2295 adapter->stats.txbytes += skb->len;
2296 adapter->stats.xmitcalled++;
2297
2298 return NETDEV_TX_OK;
2299
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002300unwind_buff:
2301 qlcnic_unmap_buffers(pdev, skb, pbuf);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002302drop_packet:
2303 adapter->stats.txdropped++;
2304 dev_kfree_skb_any(skb);
2305 return NETDEV_TX_OK;
2306}
2307
2308static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2309{
2310 struct net_device *netdev = adapter->netdev;
2311 u32 temp, temp_state, temp_val;
2312 int rv = 0;
2313
2314 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2315
2316 temp_state = qlcnic_get_temp_state(temp);
2317 temp_val = qlcnic_get_temp_val(temp);
2318
2319 if (temp_state == QLCNIC_TEMP_PANIC) {
2320 dev_err(&netdev->dev,
2321 "Device temperature %d degrees C exceeds"
2322 " maximum allowed. Hardware has been shut down.\n",
2323 temp_val);
2324 rv = 1;
2325 } else if (temp_state == QLCNIC_TEMP_WARN) {
2326 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2327 dev_err(&netdev->dev,
2328 "Device temperature %d degrees C "
2329 "exceeds operating range."
2330 " Immediate action needed.\n",
2331 temp_val);
2332 }
2333 } else {
2334 if (adapter->temp == QLCNIC_TEMP_WARN) {
2335 dev_info(&netdev->dev,
2336 "Device temperature is now %d degrees C"
2337 " in normal range.\n", temp_val);
2338 }
2339 }
2340 adapter->temp = temp_state;
2341 return rv;
2342}
2343
2344void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2345{
2346 struct net_device *netdev = adapter->netdev;
2347
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002348 if (adapter->ahw->linkup && !linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002349 netdev_info(netdev, "NIC Link is down\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002350 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002351 if (netif_running(netdev)) {
2352 netif_carrier_off(netdev);
2353 netif_stop_queue(netdev);
2354 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002355 } else if (!adapter->ahw->linkup && linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002356 netdev_info(netdev, "NIC Link is up\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002357 adapter->ahw->linkup = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002358 if (netif_running(netdev)) {
2359 netif_carrier_on(netdev);
2360 netif_wake_queue(netdev);
2361 }
2362 }
2363}
2364
2365static void qlcnic_tx_timeout(struct net_device *netdev)
2366{
2367 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2368
2369 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2370 return;
2371
2372 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002373
2374 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002375 adapter->need_fw_reset = 1;
2376 else
2377 adapter->reset_context = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002378}
2379
2380static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2381{
2382 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2383 struct net_device_stats *stats = &netdev->stats;
2384
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002385 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2386 stats->tx_packets = adapter->stats.xmitfinished;
Sucheta Chakraborty7e382592010-05-17 01:22:10 +00002387 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002388 stats->tx_bytes = adapter->stats.txbytes;
2389 stats->rx_dropped = adapter->stats.rxdropped;
2390 stats->tx_dropped = adapter->stats.txdropped;
2391
2392 return stats;
2393}
2394
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002395static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002396{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002397 u32 status;
2398
2399 status = readl(adapter->isr_int_vec);
2400
2401 if (!(status & adapter->int_vec_bit))
2402 return IRQ_NONE;
2403
2404 /* check interrupt state machine, to be sure */
2405 status = readl(adapter->crb_int_state_reg);
2406 if (!ISR_LEGACY_INT_TRIGGERED(status))
2407 return IRQ_NONE;
2408
2409 writel(0xffffffff, adapter->tgt_status_reg);
2410 /* read twice to ensure write is flushed */
2411 readl(adapter->isr_int_vec);
2412 readl(adapter->isr_int_vec);
2413
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002414 return IRQ_HANDLED;
2415}
2416
2417static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2418{
2419 struct qlcnic_host_sds_ring *sds_ring = data;
2420 struct qlcnic_adapter *adapter = sds_ring->adapter;
2421
2422 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2423 goto done;
2424 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2425 writel(0xffffffff, adapter->tgt_status_reg);
2426 goto done;
2427 }
2428
2429 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2430 return IRQ_NONE;
2431
2432done:
2433 adapter->diag_cnt++;
2434 qlcnic_enable_int(sds_ring);
2435 return IRQ_HANDLED;
2436}
2437
2438static irqreturn_t qlcnic_intr(int irq, void *data)
2439{
2440 struct qlcnic_host_sds_ring *sds_ring = data;
2441 struct qlcnic_adapter *adapter = sds_ring->adapter;
2442
2443 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2444 return IRQ_NONE;
2445
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002446 napi_schedule(&sds_ring->napi);
2447
2448 return IRQ_HANDLED;
2449}
2450
2451static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2452{
2453 struct qlcnic_host_sds_ring *sds_ring = data;
2454 struct qlcnic_adapter *adapter = sds_ring->adapter;
2455
2456 /* clear interrupt */
2457 writel(0xffffffff, adapter->tgt_status_reg);
2458
2459 napi_schedule(&sds_ring->napi);
2460 return IRQ_HANDLED;
2461}
2462
2463static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2464{
2465 struct qlcnic_host_sds_ring *sds_ring = data;
2466
2467 napi_schedule(&sds_ring->napi);
2468 return IRQ_HANDLED;
2469}
2470
2471static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2472{
2473 u32 sw_consumer, hw_consumer;
2474 int count = 0, i;
2475 struct qlcnic_cmd_buffer *buffer;
2476 struct pci_dev *pdev = adapter->pdev;
2477 struct net_device *netdev = adapter->netdev;
2478 struct qlcnic_skb_frag *frag;
2479 int done;
2480 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2481
2482 if (!spin_trylock(&adapter->tx_clean_lock))
2483 return 1;
2484
2485 sw_consumer = tx_ring->sw_consumer;
2486 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2487
2488 while (sw_consumer != hw_consumer) {
2489 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2490 if (buffer->skb) {
2491 frag = &buffer->frag_array[0];
2492 pci_unmap_single(pdev, frag->dma, frag->length,
2493 PCI_DMA_TODEVICE);
2494 frag->dma = 0ULL;
2495 for (i = 1; i < buffer->frag_count; i++) {
2496 frag++;
2497 pci_unmap_page(pdev, frag->dma, frag->length,
2498 PCI_DMA_TODEVICE);
2499 frag->dma = 0ULL;
2500 }
2501
2502 adapter->stats.xmitfinished++;
2503 dev_kfree_skb_any(buffer->skb);
2504 buffer->skb = NULL;
2505 }
2506
2507 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2508 if (++count >= MAX_STATUS_HANDLE)
2509 break;
2510 }
2511
2512 if (count && netif_running(netdev)) {
2513 tx_ring->sw_consumer = sw_consumer;
2514
2515 smp_mb();
2516
2517 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002518 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2519 netif_wake_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00002520 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002521 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002522 }
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002523 adapter->tx_timeo_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002524 }
2525 /*
2526 * If everything is freed up to consumer then check if the ring is full
2527 * If the ring is full then check if more needs to be freed and
2528 * schedule the call back again.
2529 *
2530 * This happens when there are 2 CPUs. One could be freeing and the
2531 * other filling it. If the ring is full when we get out of here and
2532 * the card has already interrupted the host then the host can miss the
2533 * interrupt.
2534 *
2535 * There is still a possible race condition and the host could miss an
2536 * interrupt. The card has to take care of this.
2537 */
2538 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2539 done = (sw_consumer == hw_consumer);
2540 spin_unlock(&adapter->tx_clean_lock);
2541
2542 return done;
2543}
2544
2545static int qlcnic_poll(struct napi_struct *napi, int budget)
2546{
2547 struct qlcnic_host_sds_ring *sds_ring =
2548 container_of(napi, struct qlcnic_host_sds_ring, napi);
2549
2550 struct qlcnic_adapter *adapter = sds_ring->adapter;
2551
2552 int tx_complete;
2553 int work_done;
2554
2555 tx_complete = qlcnic_process_cmd_ring(adapter);
2556
2557 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2558
2559 if ((work_done < budget) && tx_complete) {
2560 napi_complete(&sds_ring->napi);
2561 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2562 qlcnic_enable_int(sds_ring);
2563 }
2564
2565 return work_done;
2566}
2567
schacko8f891382010-06-17 02:56:40 +00002568static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2569{
2570 struct qlcnic_host_sds_ring *sds_ring =
2571 container_of(napi, struct qlcnic_host_sds_ring, napi);
2572
2573 struct qlcnic_adapter *adapter = sds_ring->adapter;
2574 int work_done;
2575
2576 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2577
2578 if (work_done < budget) {
2579 napi_complete(&sds_ring->napi);
2580 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2581 qlcnic_enable_int(sds_ring);
2582 }
2583
2584 return work_done;
2585}
2586
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002587#ifdef CONFIG_NET_POLL_CONTROLLER
2588static void qlcnic_poll_controller(struct net_device *netdev)
2589{
Yinglin Luanbf827912010-08-22 21:57:56 +00002590 int ring;
2591 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002592 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002593 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Yinglin Luanbf827912010-08-22 21:57:56 +00002594
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002595 disable_irq(adapter->irq);
Yinglin Luanbf827912010-08-22 21:57:56 +00002596 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2597 sds_ring = &recv_ctx->sds_rings[ring];
2598 qlcnic_intr(adapter->irq, sds_ring);
2599 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002600 enable_irq(adapter->irq);
2601}
2602#endif
2603
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002604static void
2605qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2606{
2607 u32 val;
2608
2609 val = adapter->portnum & 0xf;
2610 val |= encoding << 7;
2611 val |= (jiffies - adapter->dev_rst_time) << 8;
2612
2613 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2614 adapter->dev_rst_time = jiffies;
2615}
2616
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002617static int
2618qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002619{
2620 u32 val;
2621
2622 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2623 state != QLCNIC_DEV_NEED_QUISCENT);
2624
2625 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002626 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002627
2628 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2629
2630 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002631 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002632 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002633 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002634
2635 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2636
2637 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002638
2639 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002640}
2641
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002642static int
2643qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2644{
2645 u32 val;
2646
2647 if (qlcnic_api_lock(adapter))
2648 return -EBUSY;
2649
2650 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002651 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002652 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2653
2654 qlcnic_api_unlock(adapter);
2655
2656 return 0;
2657}
2658
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002659static void
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002660qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002661{
2662 u32 val;
2663
2664 if (qlcnic_api_lock(adapter))
2665 goto err;
2666
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002667 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002668 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002669 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002670
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002671 if (failed) {
2672 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2673 dev_info(&adapter->pdev->dev,
2674 "Device state set to Failed. Please Reboot\n");
2675 } else if (!(val & 0x11111111))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002676 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2677
2678 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002679 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002680 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2681
2682 qlcnic_api_unlock(adapter);
2683err:
2684 adapter->fw_fail_cnt = 0;
2685 clear_bit(__QLCNIC_START_FW, &adapter->state);
2686 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2687}
2688
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002689/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002690static int
2691qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2692{
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002693 int act, state, active_mask;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002694
2695 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002696 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002697
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002698 if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
2699 active_mask = (~(1 << (adapter->ahw->pci_func * 4)));
2700 act = act & active_mask;
2701 }
2702
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002703 if (((state & 0x11111111) == (act & 0x11111111)) ||
2704 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2705 return 0;
2706 else
2707 return 1;
2708}
2709
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002710static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2711{
2712 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2713
2714 if (val != QLCNIC_DRV_IDC_VER) {
2715 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2716 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2717 }
2718
2719 return 0;
2720}
2721
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002722static int
2723qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2724{
2725 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002726 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002727 u8 portnum = adapter->portnum;
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002728 u8 ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002729
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002730 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2731 return 1;
2732
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002733 if (qlcnic_api_lock(adapter))
2734 return -1;
2735
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002736 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002737 if (!(val & (1 << (portnum * 4)))) {
2738 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002739 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002740 }
2741
2742 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002743 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002744
2745 switch (prev_state) {
2746 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002747 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002748 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002749 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002750 qlcnic_api_unlock(adapter);
2751 return 1;
2752
2753 case QLCNIC_DEV_READY:
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002754 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002755 qlcnic_api_unlock(adapter);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002756 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002757
2758 case QLCNIC_DEV_NEED_RESET:
2759 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002760 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002761 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2762 break;
2763
2764 case QLCNIC_DEV_NEED_QUISCENT:
2765 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002766 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002767 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2768 break;
2769
2770 case QLCNIC_DEV_FAILED:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00002771 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002772 qlcnic_api_unlock(adapter);
2773 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002774
2775 case QLCNIC_DEV_INITIALIZING:
2776 case QLCNIC_DEV_QUISCENT:
2777 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002778 }
2779
2780 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002781
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002782 do {
2783 msleep(1000);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002784 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2785
2786 if (prev_state == QLCNIC_DEV_QUISCENT)
2787 continue;
2788 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002789
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002790 if (!dev_init_timeo) {
2791 dev_err(&adapter->pdev->dev,
2792 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002793 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002794 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002795
2796 if (qlcnic_api_lock(adapter))
2797 return -1;
2798
2799 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002800 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002801 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2802
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002803 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002804 qlcnic_api_unlock(adapter);
2805
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002806 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002807}
2808
2809static void
2810qlcnic_fwinit_work(struct work_struct *work)
2811{
2812 struct qlcnic_adapter *adapter = container_of(work,
2813 struct qlcnic_adapter, fw_work.work);
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002814 u32 dev_state = 0xf;
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002815 u32 val;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002816
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002817 if (qlcnic_api_lock(adapter))
2818 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002819
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002820 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002821 if (dev_state == QLCNIC_DEV_QUISCENT ||
2822 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002823 qlcnic_api_unlock(adapter);
2824 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2825 FW_POLL_DELAY * 2);
2826 return;
2827 }
2828
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002829 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002830 qlcnic_api_unlock(adapter);
2831 goto wait_npar;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002832 }
2833
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002834 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2835 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2836 adapter->reset_ack_timeo);
2837 goto skip_ack_check;
2838 }
2839
2840 if (!qlcnic_check_drv_state(adapter)) {
2841skip_ack_check:
2842 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002843
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002844 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2845 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2846 QLCNIC_DEV_INITIALIZING);
2847 set_bit(__QLCNIC_START_FW, &adapter->state);
2848 QLCDB(adapter, DRV, "Restarting fw\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002849 qlcnic_idc_debug_info(adapter, 0);
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002850 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2851 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2852 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002853 }
2854
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002855 qlcnic_api_unlock(adapter);
2856
Anirban Chakraborty287e38a2011-06-22 02:52:18 +00002857 rtnl_lock();
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002858 if (adapter->ahw->fw_dump.enable &&
2859 (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
Anirban Chakraborty9d6a6442011-06-22 02:52:22 +00002860 QLCDB(adapter, DRV, "Take FW dump\n");
2861 qlcnic_dump_fw(adapter);
Anirban Chakraborty9d6a6442011-06-22 02:52:22 +00002862 }
Anirban Chakraborty287e38a2011-06-22 02:52:18 +00002863 rtnl_unlock();
Sritej Velaga7b749ff2011-07-14 03:16:50 +00002864
2865 adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002866 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002867 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002868 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002869 return;
2870 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002871 goto err_ret;
2872 }
2873
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002874 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002875
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002876wait_npar:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002877 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002878 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002879
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002880 switch (dev_state) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002881 case QLCNIC_DEV_READY:
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002882 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002883 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002884 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002885 return;
2886 }
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002887 case QLCNIC_DEV_FAILED:
2888 break;
2889 default:
2890 qlcnic_schedule_work(adapter,
2891 qlcnic_fwinit_work, FW_POLL_DELAY);
2892 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002893 }
2894
2895err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002896 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2897 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002898 netif_device_attach(adapter->netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002899 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002900}
2901
2902static void
2903qlcnic_detach_work(struct work_struct *work)
2904{
2905 struct qlcnic_adapter *adapter = container_of(work,
2906 struct qlcnic_adapter, fw_work.work);
2907 struct net_device *netdev = adapter->netdev;
2908 u32 status;
2909
2910 netif_device_detach(netdev);
2911
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002912 /* Dont grab rtnl lock during Quiscent mode */
2913 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2914 if (netif_running(netdev))
2915 __qlcnic_down(adapter, netdev);
2916 } else
2917 qlcnic_down(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002918
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002919 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2920
2921 if (status & QLCNIC_RCODE_FATAL_ERROR)
2922 goto err_ret;
2923
2924 if (adapter->temp == QLCNIC_TEMP_PANIC)
2925 goto err_ret;
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002926 /* Dont ack if this instance is the reset owner */
2927 if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2928 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2929 goto err_ret;
2930 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002931
2932 adapter->fw_wait_cnt = 0;
2933
2934 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2935
2936 return;
2937
2938err_ret:
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002939 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2940 status, adapter->temp);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002941 netif_device_attach(netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002942 qlcnic_clr_all_drv_state(adapter, 1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002943}
2944
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002945/*Transit NPAR state to NON Operational */
2946static void
2947qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2948{
2949 u32 state;
2950
2951 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2952 if (state == QLCNIC_DEV_NPAR_NON_OPER)
2953 return;
2954
2955 if (qlcnic_api_lock(adapter))
2956 return;
2957 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2958 qlcnic_api_unlock(adapter);
2959}
2960
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002961/*Transit to RESET state from READY state only */
Anirban Chakraborty18f2f612011-05-12 12:48:33 +00002962void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002963qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2964{
2965 u32 state;
2966
Anirban Chakrabortycea89752010-07-13 20:33:35 +00002967 adapter->need_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002968 if (qlcnic_api_lock(adapter))
2969 return;
2970
2971 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2972
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002973 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002974 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Sritej Velaga602ca6f2011-06-22 02:52:17 +00002975 adapter->flags |= QLCNIC_FW_RESET_OWNER;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002976 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002977 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002978 }
2979
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002980 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002981 qlcnic_api_unlock(adapter);
2982}
2983
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002984/* Transit to NPAR READY state from NPAR NOT READY state */
2985static void
2986qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2987{
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002988 if (qlcnic_api_lock(adapter))
2989 return;
2990
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002991 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
2992 QLCDB(adapter, DRV, "NPAR operational state set\n");
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002993
2994 qlcnic_api_unlock(adapter);
2995}
2996
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002997static void
2998qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2999 work_func_t func, int delay)
3000{
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003001 if (test_bit(__QLCNIC_AER, &adapter->state))
3002 return;
3003
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003004 INIT_DELAYED_WORK(&adapter->fw_work, func);
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00003005 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
3006 round_jiffies_relative(delay));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003007}
3008
3009static void
3010qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
3011{
3012 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3013 msleep(10);
3014
3015 cancel_delayed_work_sync(&adapter->fw_work);
3016}
3017
3018static void
3019qlcnic_attach_work(struct work_struct *work)
3020{
3021 struct qlcnic_adapter *adapter = container_of(work,
3022 struct qlcnic_adapter, fw_work.work);
3023 struct net_device *netdev = adapter->netdev;
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00003024 u32 npar_state;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003025
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00003026 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
3027 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3028 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3029 qlcnic_clr_all_drv_state(adapter, 0);
3030 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3031 qlcnic_schedule_work(adapter, qlcnic_attach_work,
3032 FW_POLL_DELAY);
3033 else
3034 goto attach;
3035 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3036 return;
3037 }
3038attach:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003039 if (netif_running(netdev)) {
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00003040 if (qlcnic_up(adapter, netdev))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003041 goto done;
3042
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003043 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003044 }
3045
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003046done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00003047 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003048 adapter->fw_fail_cnt = 0;
3049 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00003050
3051 if (!qlcnic_clr_drv_state(adapter))
3052 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3053 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003054}
3055
3056static int
3057qlcnic_check_health(struct qlcnic_adapter *adapter)
3058{
Sony Chacko4e708122010-08-31 17:17:44 +00003059 u32 state = 0, heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003060 struct net_device *netdev = adapter->netdev;
3061
3062 if (qlcnic_check_temp(adapter))
3063 goto detach;
3064
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00003065 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003066 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003067
3068 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00003069 if (state == QLCNIC_DEV_NEED_RESET) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003070 qlcnic_set_npar_non_operational(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003071 adapter->need_fw_reset = 1;
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00003072 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3073 goto detach;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003074
Sony Chacko4e708122010-08-31 17:17:44 +00003075 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3076 if (heartbeat != adapter->heartbeat) {
3077 adapter->heartbeat = heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003078 adapter->fw_fail_cnt = 0;
3079 if (adapter->need_fw_reset)
3080 goto detach;
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003081
amit salecha9ce13ca2011-02-23 03:21:24 +00003082 if (adapter->reset_context && auto_fw_reset) {
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003083 qlcnic_reset_hw_context(adapter);
3084 adapter->netdev->trans_start = jiffies;
3085 }
3086
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003087 return 0;
3088 }
3089
3090 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3091 return 0;
3092
3093 qlcnic_dev_request_reset(adapter);
3094
amit salecha9ce13ca2011-02-23 03:21:24 +00003095 if (auto_fw_reset)
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00003096 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003097
3098 dev_info(&netdev->dev, "firmware hang detected\n");
3099
3100detach:
3101 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3102 QLCNIC_DEV_NEED_RESET;
3103
amit salecha9ce13ca2011-02-23 03:21:24 +00003104 if (auto_fw_reset &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003105 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3106
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003107 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003108 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3109 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003110
3111 return 1;
3112}
3113
3114static void
3115qlcnic_fw_poll_work(struct work_struct *work)
3116{
3117 struct qlcnic_adapter *adapter = container_of(work,
3118 struct qlcnic_adapter, fw_work.work);
3119
3120 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3121 goto reschedule;
3122
3123
3124 if (qlcnic_check_health(adapter))
3125 return;
3126
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00003127 if (adapter->fhash.fnum)
3128 qlcnic_prune_lb_filters(adapter);
3129
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003130reschedule:
3131 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3132}
3133
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003134static int qlcnic_is_first_func(struct pci_dev *pdev)
3135{
3136 struct pci_dev *oth_pdev;
3137 int val = pdev->devfn;
3138
3139 while (val-- > 0) {
3140 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3141 (pdev->bus), pdev->bus->number,
3142 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003143 if (!oth_pdev)
3144 continue;
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003145
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003146 if (oth_pdev->current_state != PCI_D3cold) {
3147 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003148 return 0;
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003149 }
3150 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003151 }
3152 return 1;
3153}
3154
3155static int qlcnic_attach_func(struct pci_dev *pdev)
3156{
3157 int err, first_func;
3158 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3159 struct net_device *netdev = adapter->netdev;
3160
3161 pdev->error_state = pci_channel_io_normal;
3162
3163 err = pci_enable_device(pdev);
3164 if (err)
3165 return err;
3166
3167 pci_set_power_state(pdev, PCI_D0);
3168 pci_set_master(pdev);
3169 pci_restore_state(pdev);
3170
3171 first_func = qlcnic_is_first_func(pdev);
3172
3173 if (qlcnic_api_lock(adapter))
3174 return -EINVAL;
3175
Amit Kumar Salecha933fce122010-08-17 00:34:19 +00003176 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003177 adapter->need_fw_reset = 1;
3178 set_bit(__QLCNIC_START_FW, &adapter->state);
3179 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3180 QLCDB(adapter, DRV, "Restarting fw\n");
3181 }
3182 qlcnic_api_unlock(adapter);
3183
3184 err = adapter->nic_ops->start_firmware(adapter);
3185 if (err)
3186 return err;
3187
3188 qlcnic_clr_drv_state(adapter);
3189 qlcnic_setup_intr(adapter);
3190
3191 if (netif_running(netdev)) {
3192 err = qlcnic_attach(adapter);
3193 if (err) {
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00003194 qlcnic_clr_all_drv_state(adapter, 1);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003195 clear_bit(__QLCNIC_AER, &adapter->state);
3196 netif_device_attach(netdev);
3197 return err;
3198 }
3199
3200 err = qlcnic_up(adapter, netdev);
3201 if (err)
3202 goto done;
3203
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003204 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003205 }
3206 done:
3207 netif_device_attach(netdev);
3208 return err;
3209}
3210
3211static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3212 pci_channel_state_t state)
3213{
3214 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3215 struct net_device *netdev = adapter->netdev;
3216
3217 if (state == pci_channel_io_perm_failure)
3218 return PCI_ERS_RESULT_DISCONNECT;
3219
3220 if (state == pci_channel_io_normal)
3221 return PCI_ERS_RESULT_RECOVERED;
3222
3223 set_bit(__QLCNIC_AER, &adapter->state);
3224 netif_device_detach(netdev);
3225
3226 cancel_delayed_work_sync(&adapter->fw_work);
3227
3228 if (netif_running(netdev))
3229 qlcnic_down(adapter, netdev);
3230
3231 qlcnic_detach(adapter);
3232 qlcnic_teardown_intr(adapter);
3233
3234 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3235
3236 pci_save_state(pdev);
3237 pci_disable_device(pdev);
3238
3239 return PCI_ERS_RESULT_NEED_RESET;
3240}
3241
3242static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3243{
3244 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3245 PCI_ERS_RESULT_RECOVERED;
3246}
3247
3248static void qlcnic_io_resume(struct pci_dev *pdev)
3249{
3250 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3251
3252 pci_cleanup_aer_uncorrect_error_status(pdev);
3253
3254 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3255 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3256 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3257 FW_POLL_DELAY);
3258}
3259
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003260static int
3261qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3262{
3263 int err;
3264
3265 err = qlcnic_can_start_firmware(adapter);
3266 if (err)
3267 return err;
3268
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +00003269 err = qlcnic_check_npar_opertional(adapter);
3270 if (err)
3271 return err;
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003272
Rajesh Borundia174240a2010-08-31 17:17:47 +00003273 err = qlcnic_initialize_nic(adapter);
3274 if (err)
3275 return err;
3276
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003277 qlcnic_check_options(adapter);
3278
Rajesh Borundia73733732010-08-31 17:17:50 +00003279 err = qlcnic_set_eswitch_port_config(adapter);
3280 if (err)
3281 return err;
3282
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003283 adapter->need_fw_reset = 0;
3284
3285 return err;
3286}
3287
3288static int
3289qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3290{
3291 return -EOPNOTSUPP;
3292}
3293
3294static int
3295qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3296{
3297 return -EOPNOTSUPP;
3298}
3299
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003300static ssize_t
3301qlcnic_store_bridged_mode(struct device *dev,
3302 struct device_attribute *attr, const char *buf, size_t len)
3303{
3304 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3305 unsigned long new;
3306 int ret = -EINVAL;
3307
3308 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3309 goto err_out;
3310
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00003311 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003312 goto err_out;
3313
3314 if (strict_strtoul(buf, 2, &new))
3315 goto err_out;
3316
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00003317 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003318 ret = len;
3319
3320err_out:
3321 return ret;
3322}
3323
3324static ssize_t
3325qlcnic_show_bridged_mode(struct device *dev,
3326 struct device_attribute *attr, char *buf)
3327{
3328 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3329 int bridged_mode = 0;
3330
3331 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3332 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3333
3334 return sprintf(buf, "%d\n", bridged_mode);
3335}
3336
3337static struct device_attribute dev_attr_bridged_mode = {
3338 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3339 .show = qlcnic_show_bridged_mode,
3340 .store = qlcnic_store_bridged_mode,
3341};
3342
3343static ssize_t
3344qlcnic_store_diag_mode(struct device *dev,
3345 struct device_attribute *attr, const char *buf, size_t len)
3346{
3347 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3348 unsigned long new;
3349
3350 if (strict_strtoul(buf, 2, &new))
3351 return -EINVAL;
3352
3353 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3354 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3355
3356 return len;
3357}
3358
3359static ssize_t
3360qlcnic_show_diag_mode(struct device *dev,
3361 struct device_attribute *attr, char *buf)
3362{
3363 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3364
3365 return sprintf(buf, "%d\n",
3366 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3367}
3368
3369static struct device_attribute dev_attr_diag_mode = {
3370 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3371 .show = qlcnic_show_diag_mode,
3372 .store = qlcnic_store_diag_mode,
3373};
3374
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00003375int qlcnic_validate_max_rss(struct net_device *netdev, u8 max_hw, u8 val)
3376{
3377 if (!use_msi_x && !use_msi) {
3378 netdev_info(netdev, "no msix or msi support, hence no rss\n");
3379 return -EINVAL;
3380 }
3381
3382 if ((val > max_hw) || (val < 2) || !is_power_of_2(val)) {
3383 netdev_info(netdev, "rss_ring valid range [2 - %x] in "
3384 " powers of 2\n", max_hw);
3385 return -EINVAL;
3386 }
3387 return 0;
3388
3389}
3390
3391int qlcnic_set_max_rss(struct qlcnic_adapter *adapter, u8 data)
3392{
3393 struct net_device *netdev = adapter->netdev;
3394 int err = 0;
3395
3396 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3397 return -EBUSY;
3398
3399 netif_device_detach(netdev);
3400 if (netif_running(netdev))
3401 __qlcnic_down(adapter, netdev);
3402 qlcnic_detach(adapter);
3403 qlcnic_teardown_intr(adapter);
3404
3405 if (qlcnic_enable_msix(adapter, data)) {
3406 netdev_info(netdev, "failed setting max_rss; rss disabled\n");
3407 qlcnic_enable_msi_legacy(adapter);
3408 }
3409
3410 if (netif_running(netdev)) {
3411 err = qlcnic_attach(adapter);
3412 if (err)
3413 goto done;
3414 err = __qlcnic_up(adapter, netdev);
3415 if (err)
3416 goto done;
3417 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3418 }
3419 done:
3420 netif_device_attach(netdev);
3421 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3422 return err;
3423}
3424
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003425static int
3426qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3427 loff_t offset, size_t size)
3428{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003429 size_t crb_size = 4;
3430
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003431 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3432 return -EIO;
3433
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003434 if (offset < QLCNIC_PCI_CRBSPACE) {
3435 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3436 QLCNIC_PCI_CAMQM_END))
3437 crb_size = 8;
3438 else
3439 return -EINVAL;
3440 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003441
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003442 if ((size != crb_size) || (offset & (crb_size-1)))
3443 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003444
3445 return 0;
3446}
3447
3448static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003449qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3450 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003451 char *buf, loff_t offset, size_t size)
3452{
3453 struct device *dev = container_of(kobj, struct device, kobj);
3454 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3455 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003456 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003457 int ret;
3458
3459 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3460 if (ret != 0)
3461 return ret;
3462
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003463 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3464 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3465 memcpy(buf, &qmdata, size);
3466 } else {
3467 data = QLCRD32(adapter, offset);
3468 memcpy(buf, &data, size);
3469 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003470 return size;
3471}
3472
3473static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003474qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3475 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003476 char *buf, loff_t offset, size_t size)
3477{
3478 struct device *dev = container_of(kobj, struct device, kobj);
3479 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3480 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003481 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003482 int ret;
3483
3484 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3485 if (ret != 0)
3486 return ret;
3487
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003488 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3489 memcpy(&qmdata, buf, size);
3490 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3491 } else {
3492 memcpy(&data, buf, size);
3493 QLCWR32(adapter, offset, data);
3494 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003495 return size;
3496}
3497
3498static int
3499qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3500 loff_t offset, size_t size)
3501{
3502 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3503 return -EIO;
3504
3505 if ((size != 8) || (offset & 0x7))
3506 return -EIO;
3507
3508 return 0;
3509}
3510
3511static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003512qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3513 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003514 char *buf, loff_t offset, size_t size)
3515{
3516 struct device *dev = container_of(kobj, struct device, kobj);
3517 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3518 u64 data;
3519 int ret;
3520
3521 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3522 if (ret != 0)
3523 return ret;
3524
3525 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3526 return -EIO;
3527
3528 memcpy(buf, &data, size);
3529
3530 return size;
3531}
3532
3533static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003534qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3535 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003536 char *buf, loff_t offset, size_t size)
3537{
3538 struct device *dev = container_of(kobj, struct device, kobj);
3539 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3540 u64 data;
3541 int ret;
3542
3543 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3544 if (ret != 0)
3545 return ret;
3546
3547 memcpy(&data, buf, size);
3548
3549 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3550 return -EIO;
3551
3552 return size;
3553}
3554
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003555static struct bin_attribute bin_attr_crb = {
3556 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3557 .size = 0,
3558 .read = qlcnic_sysfs_read_crb,
3559 .write = qlcnic_sysfs_write_crb,
3560};
3561
3562static struct bin_attribute bin_attr_mem = {
3563 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3564 .size = 0,
3565 .read = qlcnic_sysfs_read_mem,
3566 .write = qlcnic_sysfs_write_mem,
3567};
3568
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003569static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003570validate_pm_config(struct qlcnic_adapter *adapter,
3571 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3572{
3573
3574 u8 src_pci_func, s_esw_id, d_esw_id;
3575 u8 dest_pci_func;
3576 int i;
3577
3578 for (i = 0; i < count; i++) {
3579 src_pci_func = pm_cfg[i].pci_func;
3580 dest_pci_func = pm_cfg[i].dest_npar;
3581 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3582 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3583 return QL_STATUS_INVALID_PARAM;
3584
3585 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3586 return QL_STATUS_INVALID_PARAM;
3587
3588 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3589 return QL_STATUS_INVALID_PARAM;
3590
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003591 s_esw_id = adapter->npars[src_pci_func].phy_port;
3592 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3593
3594 if (s_esw_id != d_esw_id)
3595 return QL_STATUS_INVALID_PARAM;
3596
3597 }
3598 return 0;
3599
3600}
3601
3602static ssize_t
3603qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3604 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3605{
3606 struct device *dev = container_of(kobj, struct device, kobj);
3607 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3608 struct qlcnic_pm_func_cfg *pm_cfg;
3609 u32 id, action, pci_func;
3610 int count, rem, i, ret;
3611
3612 count = size / sizeof(struct qlcnic_pm_func_cfg);
3613 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3614 if (rem)
3615 return QL_STATUS_INVALID_PARAM;
3616
3617 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3618
3619 ret = validate_pm_config(adapter, pm_cfg, count);
3620 if (ret)
3621 return ret;
3622 for (i = 0; i < count; i++) {
3623 pci_func = pm_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003624 action = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003625 id = adapter->npars[pci_func].phy_port;
3626 ret = qlcnic_config_port_mirroring(adapter, id,
3627 action, pci_func);
3628 if (ret)
3629 return ret;
3630 }
3631
3632 for (i = 0; i < count; i++) {
3633 pci_func = pm_cfg[i].pci_func;
3634 id = adapter->npars[pci_func].phy_port;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003635 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003636 adapter->npars[pci_func].dest_npar = id;
3637 }
3638 return size;
3639}
3640
3641static ssize_t
3642qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3643 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3644{
3645 struct device *dev = container_of(kobj, struct device, kobj);
3646 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3647 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3648 int i;
3649
3650 if (size != sizeof(pm_cfg))
3651 return QL_STATUS_INVALID_PARAM;
3652
3653 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3654 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3655 continue;
3656 pm_cfg[i].action = adapter->npars[i].enable_pm;
3657 pm_cfg[i].dest_npar = 0;
3658 pm_cfg[i].pci_func = i;
3659 }
3660 memcpy(buf, &pm_cfg, size);
3661
3662 return size;
3663}
3664
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003665static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003666validate_esw_config(struct qlcnic_adapter *adapter,
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003667 struct qlcnic_esw_func_cfg *esw_cfg, int count)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003668{
Rajesh Borundia7613c872010-08-31 17:17:48 +00003669 u32 op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003670 u8 pci_func;
3671 int i;
Rajesh Borundia7613c872010-08-31 17:17:48 +00003672
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003673 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
Rajesh Borundia7613c872010-08-31 17:17:48 +00003674
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003675 for (i = 0; i < count; i++) {
3676 pci_func = esw_cfg[i].pci_func;
3677 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3678 return QL_STATUS_INVALID_PARAM;
3679
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003680 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3681 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003682 return QL_STATUS_INVALID_PARAM;
3683
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003684 switch (esw_cfg[i].op_mode) {
3685 case QLCNIC_PORT_DEFAULTS:
Rajesh Borundia7613c872010-08-31 17:17:48 +00003686 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
Rajesh Borundia73733732010-08-31 17:17:50 +00003687 QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha091056b2010-12-02 20:41:43 +00003688 if (esw_cfg[i].mac_anti_spoof != 0)
3689 return QL_STATUS_INVALID_PARAM;
3690 if (esw_cfg[i].mac_override != 1)
3691 return QL_STATUS_INVALID_PARAM;
3692 if (esw_cfg[i].promisc_mode != 1)
3693 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia73733732010-08-31 17:17:50 +00003694 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003695 break;
3696 case QLCNIC_ADD_VLAN:
3697 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3698 return QL_STATUS_INVALID_PARAM;
3699 if (!esw_cfg[i].op_type)
3700 return QL_STATUS_INVALID_PARAM;
3701 break;
3702 case QLCNIC_DEL_VLAN:
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003703 if (!esw_cfg[i].op_type)
3704 return QL_STATUS_INVALID_PARAM;
3705 break;
3706 default:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003707 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003708 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003709 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003710 return 0;
3711}
3712
3713static ssize_t
3714qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3715 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3716{
3717 struct device *dev = container_of(kobj, struct device, kobj);
3718 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3719 struct qlcnic_esw_func_cfg *esw_cfg;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003720 struct qlcnic_npar_info *npar;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003721 int count, rem, i, ret;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003722 u8 pci_func, op_mode = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003723
3724 count = size / sizeof(struct qlcnic_esw_func_cfg);
3725 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3726 if (rem)
3727 return QL_STATUS_INVALID_PARAM;
3728
3729 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3730 ret = validate_esw_config(adapter, esw_cfg, count);
3731 if (ret)
3732 return ret;
3733
3734 for (i = 0; i < count; i++) {
Rajesh Borundia0325d692010-08-19 05:08:26 +00003735 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3736 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3737 return QL_STATUS_INVALID_PARAM;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003738
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003739 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003740 continue;
3741
3742 op_mode = esw_cfg[i].op_mode;
3743 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3744 esw_cfg[i].op_mode = op_mode;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003745 esw_cfg[i].pci_func = adapter->ahw->pci_func;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003746
3747 switch (esw_cfg[i].op_mode) {
3748 case QLCNIC_PORT_DEFAULTS:
3749 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3750 break;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00003751 case QLCNIC_ADD_VLAN:
3752 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3753 break;
3754 case QLCNIC_DEL_VLAN:
3755 esw_cfg[i].vlan_id = 0;
3756 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3757 break;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003758 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003759 }
3760
Rajesh Borundia0325d692010-08-19 05:08:26 +00003761 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3762 goto out;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003763
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003764 for (i = 0; i < count; i++) {
3765 pci_func = esw_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003766 npar = &adapter->npars[pci_func];
3767 switch (esw_cfg[i].op_mode) {
3768 case QLCNIC_PORT_DEFAULTS:
3769 npar->promisc_mode = esw_cfg[i].promisc_mode;
Rajesh Borundia73733732010-08-31 17:17:50 +00003770 npar->mac_override = esw_cfg[i].mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003771 npar->offload_flags = esw_cfg[i].offload_flags;
3772 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3773 npar->discard_tagged = esw_cfg[i].discard_tagged;
3774 break;
3775 case QLCNIC_ADD_VLAN:
3776 npar->pvid = esw_cfg[i].vlan_id;
3777 break;
3778 case QLCNIC_DEL_VLAN:
3779 npar->pvid = 0;
3780 break;
3781 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003782 }
Rajesh Borundia0325d692010-08-19 05:08:26 +00003783out:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003784 return size;
3785}
3786
3787static ssize_t
3788qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3789 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3790{
3791 struct device *dev = container_of(kobj, struct device, kobj);
3792 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3793 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003794 u8 i;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003795
3796 if (size != sizeof(esw_cfg))
3797 return QL_STATUS_INVALID_PARAM;
3798
3799 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3800 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3801 continue;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003802 esw_cfg[i].pci_func = i;
3803 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
3804 return QL_STATUS_INVALID_PARAM;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003805 }
3806 memcpy(buf, &esw_cfg, size);
3807
3808 return size;
3809}
3810
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003811static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003812validate_npar_config(struct qlcnic_adapter *adapter,
3813 struct qlcnic_npar_func_cfg *np_cfg, int count)
3814{
3815 u8 pci_func, i;
3816
3817 for (i = 0; i < count; i++) {
3818 pci_func = np_cfg[i].pci_func;
3819 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3820 return QL_STATUS_INVALID_PARAM;
3821
3822 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3823 return QL_STATUS_INVALID_PARAM;
3824
Rajesh Borundiad12b0d92011-02-23 03:21:25 +00003825 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
3826 !IS_VALID_BW(np_cfg[i].max_bw))
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003827 return QL_STATUS_INVALID_PARAM;
3828 }
3829 return 0;
3830}
3831
3832static ssize_t
3833qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3834 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3835{
3836 struct device *dev = container_of(kobj, struct device, kobj);
3837 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3838 struct qlcnic_info nic_info;
3839 struct qlcnic_npar_func_cfg *np_cfg;
3840 int i, count, rem, ret;
3841 u8 pci_func;
3842
3843 count = size / sizeof(struct qlcnic_npar_func_cfg);
3844 rem = size % sizeof(struct qlcnic_npar_func_cfg);
3845 if (rem)
3846 return QL_STATUS_INVALID_PARAM;
3847
3848 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
3849 ret = validate_npar_config(adapter, np_cfg, count);
3850 if (ret)
3851 return ret;
3852
3853 for (i = 0; i < count ; i++) {
3854 pci_func = np_cfg[i].pci_func;
3855 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
3856 if (ret)
3857 return ret;
3858 nic_info.pci_func = pci_func;
3859 nic_info.min_tx_bw = np_cfg[i].min_bw;
3860 nic_info.max_tx_bw = np_cfg[i].max_bw;
3861 ret = qlcnic_set_nic_info(adapter, &nic_info);
3862 if (ret)
3863 return ret;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003864 adapter->npars[i].min_bw = nic_info.min_tx_bw;
3865 adapter->npars[i].max_bw = nic_info.max_tx_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003866 }
3867
3868 return size;
3869
3870}
3871static ssize_t
3872qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
3873 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3874{
3875 struct device *dev = container_of(kobj, struct device, kobj);
3876 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3877 struct qlcnic_info nic_info;
3878 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
3879 int i, ret;
3880
3881 if (size != sizeof(np_cfg))
3882 return QL_STATUS_INVALID_PARAM;
3883
3884 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3885 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3886 continue;
3887 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
3888 if (ret)
3889 return ret;
3890
3891 np_cfg[i].pci_func = i;
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +00003892 np_cfg[i].op_mode = (u8)nic_info.op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003893 np_cfg[i].port_num = nic_info.phys_port;
3894 np_cfg[i].fw_capab = nic_info.capabilities;
3895 np_cfg[i].min_bw = nic_info.min_tx_bw ;
3896 np_cfg[i].max_bw = nic_info.max_tx_bw;
3897 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
3898 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
3899 }
3900 memcpy(buf, &np_cfg, size);
3901 return size;
3902}
3903
3904static ssize_t
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003905qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
3906 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3907{
3908 struct device *dev = container_of(kobj, struct device, kobj);
3909 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3910 struct qlcnic_esw_statistics port_stats;
3911 int ret;
3912
3913 if (size != sizeof(struct qlcnic_esw_statistics))
3914 return QL_STATUS_INVALID_PARAM;
3915
3916 if (offset >= QLCNIC_MAX_PCI_FUNC)
3917 return QL_STATUS_INVALID_PARAM;
3918
3919 memset(&port_stats, 0, size);
3920 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3921 &port_stats.rx);
3922 if (ret)
3923 return ret;
3924
3925 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3926 &port_stats.tx);
3927 if (ret)
3928 return ret;
3929
3930 memcpy(buf, &port_stats, size);
3931 return size;
3932}
3933
3934static ssize_t
3935qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
3936 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3937{
3938 struct device *dev = container_of(kobj, struct device, kobj);
3939 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3940 struct qlcnic_esw_statistics esw_stats;
3941 int ret;
3942
3943 if (size != sizeof(struct qlcnic_esw_statistics))
3944 return QL_STATUS_INVALID_PARAM;
3945
3946 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3947 return QL_STATUS_INVALID_PARAM;
3948
3949 memset(&esw_stats, 0, size);
3950 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3951 &esw_stats.rx);
3952 if (ret)
3953 return ret;
3954
3955 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3956 &esw_stats.tx);
3957 if (ret)
3958 return ret;
3959
3960 memcpy(buf, &esw_stats, size);
3961 return size;
3962}
3963
3964static ssize_t
3965qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
3966 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3967{
3968 struct device *dev = container_of(kobj, struct device, kobj);
3969 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3970 int ret;
3971
3972 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3973 return QL_STATUS_INVALID_PARAM;
3974
3975 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3976 QLCNIC_QUERY_RX_COUNTER);
3977 if (ret)
3978 return ret;
3979
3980 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3981 QLCNIC_QUERY_TX_COUNTER);
3982 if (ret)
3983 return ret;
3984
3985 return size;
3986}
3987
3988static ssize_t
3989qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
3990 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3991{
3992
3993 struct device *dev = container_of(kobj, struct device, kobj);
3994 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3995 int ret;
3996
3997 if (offset >= QLCNIC_MAX_PCI_FUNC)
3998 return QL_STATUS_INVALID_PARAM;
3999
4000 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4001 QLCNIC_QUERY_RX_COUNTER);
4002 if (ret)
4003 return ret;
4004
4005 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4006 QLCNIC_QUERY_TX_COUNTER);
4007 if (ret)
4008 return ret;
4009
4010 return size;
4011}
4012
4013static ssize_t
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004014qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
4015 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4016{
4017 struct device *dev = container_of(kobj, struct device, kobj);
4018 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4019 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
Dan Carpentere88db3b2010-08-09 21:49:36 +00004020 struct qlcnic_pci_info *pci_info;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004021 int i, ret;
4022
4023 if (size != sizeof(pci_cfg))
4024 return QL_STATUS_INVALID_PARAM;
4025
Dan Carpentere88db3b2010-08-09 21:49:36 +00004026 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
4027 if (!pci_info)
4028 return -ENOMEM;
4029
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004030 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpentere88db3b2010-08-09 21:49:36 +00004031 if (ret) {
4032 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004033 return ret;
Dan Carpentere88db3b2010-08-09 21:49:36 +00004034 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004035
4036 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4037 pci_cfg[i].pci_func = pci_info[i].id;
4038 pci_cfg[i].func_type = pci_info[i].type;
4039 pci_cfg[i].port_num = pci_info[i].default_port;
4040 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
4041 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
4042 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
4043 }
4044 memcpy(buf, &pci_cfg, size);
Dan Carpentere88db3b2010-08-09 21:49:36 +00004045 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004046 return size;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004047}
4048static struct bin_attribute bin_attr_npar_config = {
4049 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
4050 .size = 0,
4051 .read = qlcnic_sysfs_read_npar_config,
4052 .write = qlcnic_sysfs_write_npar_config,
4053};
4054
4055static struct bin_attribute bin_attr_pci_config = {
4056 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
4057 .size = 0,
4058 .read = qlcnic_sysfs_read_pci_config,
4059 .write = NULL,
4060};
4061
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004062static struct bin_attribute bin_attr_port_stats = {
4063 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
4064 .size = 0,
4065 .read = qlcnic_sysfs_get_port_stats,
4066 .write = qlcnic_sysfs_clear_port_stats,
4067};
4068
4069static struct bin_attribute bin_attr_esw_stats = {
4070 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
4071 .size = 0,
4072 .read = qlcnic_sysfs_get_esw_stats,
4073 .write = qlcnic_sysfs_clear_esw_stats,
4074};
4075
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004076static struct bin_attribute bin_attr_esw_config = {
4077 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
4078 .size = 0,
4079 .read = qlcnic_sysfs_read_esw_config,
4080 .write = qlcnic_sysfs_write_esw_config,
4081};
4082
4083static struct bin_attribute bin_attr_pm_config = {
4084 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
4085 .size = 0,
4086 .read = qlcnic_sysfs_read_pm_config,
4087 .write = qlcnic_sysfs_write_pm_config,
4088};
4089
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004090static void
4091qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
4092{
4093 struct device *dev = &adapter->pdev->dev;
4094
4095 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4096 if (device_create_file(dev, &dev_attr_bridged_mode))
4097 dev_warn(dev,
4098 "failed to create bridged_mode sysfs entry\n");
4099}
4100
4101static void
4102qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
4103{
4104 struct device *dev = &adapter->pdev->dev;
4105
4106 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4107 device_remove_file(dev, &dev_attr_bridged_mode);
4108}
4109
4110static void
4111qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
4112{
4113 struct device *dev = &adapter->pdev->dev;
4114
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004115 if (device_create_bin_file(dev, &bin_attr_port_stats))
4116 dev_info(dev, "failed to create port stats sysfs entry");
4117
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004118 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4119 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004120 if (device_create_file(dev, &dev_attr_diag_mode))
4121 dev_info(dev, "failed to create diag_mode sysfs entry\n");
4122 if (device_create_bin_file(dev, &bin_attr_crb))
4123 dev_info(dev, "failed to create crb sysfs entry\n");
4124 if (device_create_bin_file(dev, &bin_attr_mem))
4125 dev_info(dev, "failed to create mem sysfs entry\n");
Sony Chacko53478fe2011-04-01 14:27:47 +00004126 if (device_create_bin_file(dev, &bin_attr_pci_config))
4127 dev_info(dev, "failed to create pci config sysfs entry");
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004128 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4129 return;
4130 if (device_create_bin_file(dev, &bin_attr_esw_config))
4131 dev_info(dev, "failed to create esw config sysfs entry");
4132 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004133 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004134 if (device_create_bin_file(dev, &bin_attr_npar_config))
4135 dev_info(dev, "failed to create npar config sysfs entry");
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004136 if (device_create_bin_file(dev, &bin_attr_pm_config))
4137 dev_info(dev, "failed to create pm config sysfs entry");
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004138 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4139 dev_info(dev, "failed to create eswitch stats sysfs entry");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004140}
4141
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004142static void
4143qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4144{
4145 struct device *dev = &adapter->pdev->dev;
4146
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004147 device_remove_bin_file(dev, &bin_attr_port_stats);
4148
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004149 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4150 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004151 device_remove_file(dev, &dev_attr_diag_mode);
4152 device_remove_bin_file(dev, &bin_attr_crb);
4153 device_remove_bin_file(dev, &bin_attr_mem);
Sony Chacko53478fe2011-04-01 14:27:47 +00004154 device_remove_bin_file(dev, &bin_attr_pci_config);
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004155 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4156 return;
4157 device_remove_bin_file(dev, &bin_attr_esw_config);
4158 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004159 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004160 device_remove_bin_file(dev, &bin_attr_npar_config);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004161 device_remove_bin_file(dev, &bin_attr_pm_config);
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004162 device_remove_bin_file(dev, &bin_attr_esw_stats);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004163}
4164
4165#ifdef CONFIG_INET
4166
4167#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4168
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004169static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004170qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4171 struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004172{
4173 struct in_device *indev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004174
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004175 indev = in_dev_get(dev);
4176 if (!indev)
4177 return;
4178
4179 for_ifa(indev) {
4180 switch (event) {
4181 case NETDEV_UP:
4182 qlcnic_config_ipaddr(adapter,
4183 ifa->ifa_address, QLCNIC_IP_UP);
4184 break;
4185 case NETDEV_DOWN:
4186 qlcnic_config_ipaddr(adapter,
4187 ifa->ifa_address, QLCNIC_IP_DOWN);
4188 break;
4189 default:
4190 break;
4191 }
4192 } endfor_ifa(indev);
4193
4194 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004195}
4196
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004197static void
4198qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4199{
4200 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4201 struct net_device *dev;
4202 u16 vid;
4203
4204 qlcnic_config_indev_addr(adapter, netdev, event);
4205
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +00004206 for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4207 dev = vlan_find_dev(netdev, vid);
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004208 if (!dev)
4209 continue;
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004210 qlcnic_config_indev_addr(adapter, dev, event);
4211 }
4212}
4213
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004214static int qlcnic_netdev_event(struct notifier_block *this,
4215 unsigned long event, void *ptr)
4216{
4217 struct qlcnic_adapter *adapter;
4218 struct net_device *dev = (struct net_device *)ptr;
4219
4220recheck:
4221 if (dev == NULL)
4222 goto done;
4223
4224 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4225 dev = vlan_dev_real_dev(dev);
4226 goto recheck;
4227 }
4228
4229 if (!is_qlcnic_netdev(dev))
4230 goto done;
4231
4232 adapter = netdev_priv(dev);
4233
4234 if (!adapter)
4235 goto done;
4236
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004237 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004238 goto done;
4239
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004240 qlcnic_config_indev_addr(adapter, dev, event);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004241done:
4242 return NOTIFY_DONE;
4243}
4244
4245static int
4246qlcnic_inetaddr_event(struct notifier_block *this,
4247 unsigned long event, void *ptr)
4248{
4249 struct qlcnic_adapter *adapter;
4250 struct net_device *dev;
4251
4252 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4253
4254 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4255
4256recheck:
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004257 if (dev == NULL)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004258 goto done;
4259
4260 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4261 dev = vlan_dev_real_dev(dev);
4262 goto recheck;
4263 }
4264
4265 if (!is_qlcnic_netdev(dev))
4266 goto done;
4267
4268 adapter = netdev_priv(dev);
4269
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00004270 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004271 goto done;
4272
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004273 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004274 goto done;
4275
4276 switch (event) {
4277 case NETDEV_UP:
4278 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4279 break;
4280 case NETDEV_DOWN:
4281 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4282 break;
4283 default:
4284 break;
4285 }
4286
4287done:
4288 return NOTIFY_DONE;
4289}
4290
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004291static struct notifier_block qlcnic_netdev_cb = {
4292 .notifier_call = qlcnic_netdev_event,
4293};
4294
4295static struct notifier_block qlcnic_inetaddr_cb = {
4296 .notifier_call = qlcnic_inetaddr_event,
4297};
4298#else
4299static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004300qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004301{ }
4302#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004303static struct pci_error_handlers qlcnic_err_handler = {
4304 .error_detected = qlcnic_io_error_detected,
4305 .slot_reset = qlcnic_io_slot_reset,
4306 .resume = qlcnic_io_resume,
4307};
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004308
4309static struct pci_driver qlcnic_driver = {
4310 .name = qlcnic_driver_name,
4311 .id_table = qlcnic_pci_tbl,
4312 .probe = qlcnic_probe,
4313 .remove = __devexit_p(qlcnic_remove),
4314#ifdef CONFIG_PM
4315 .suspend = qlcnic_suspend,
4316 .resume = qlcnic_resume,
4317#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004318 .shutdown = qlcnic_shutdown,
4319 .err_handler = &qlcnic_err_handler
4320
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004321};
4322
4323static int __init qlcnic_init_module(void)
4324{
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004325 int ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004326
4327 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4328
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004329 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4330 if (qlcnic_wq == NULL) {
4331 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4332 return -ENOMEM;
4333 }
4334
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004335#ifdef CONFIG_INET
4336 register_netdevice_notifier(&qlcnic_netdev_cb);
4337 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4338#endif
4339
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004340 ret = pci_register_driver(&qlcnic_driver);
4341 if (ret) {
4342#ifdef CONFIG_INET
4343 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4344 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4345#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004346 destroy_workqueue(qlcnic_wq);
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004347 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004348
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004349 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004350}
4351
4352module_init(qlcnic_init_module);
4353
4354static void __exit qlcnic_exit_module(void)
4355{
4356
4357 pci_unregister_driver(&qlcnic_driver);
4358
4359#ifdef CONFIG_INET
4360 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4361 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4362#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004363 destroy_workqueue(qlcnic_wq);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004364}
4365
4366module_exit(qlcnic_exit_module);