blob: 0f6af5c61a7ca98b2cf5e0a5a44e5ff41eb9360a [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_alloc_lb_filters_mem(struct qlcnic_adapter *adapter);
94static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000095static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000096static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
97static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
98static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
Rajesh Borundia0325d692010-08-19 05:08:26 +000099static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
100 struct qlcnic_esw_func_cfg *);
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000101static void qlcnic_vlan_rx_add(struct net_device *, u16);
102static void qlcnic_vlan_rx_del(struct net_device *, u16);
103
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000104/* PCI Device ID Table */
105#define ENTRY(device) \
106 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
107 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
108
109#define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
110
Amit Kumar Salecha6a902882010-02-01 05:24:54 +0000111static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000112 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
113 {0,}
114};
115
116MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
117
118
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000119inline void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000120qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
121 struct qlcnic_host_tx_ring *tx_ring)
122{
123 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000124}
125
126static const u32 msi_tgt_status[8] = {
127 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
128 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
129 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
130 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
131};
132
133static const
134struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
135
136static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
137{
138 writel(0, sds_ring->crb_intr_mask);
139}
140
141static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
142{
143 struct qlcnic_adapter *adapter = sds_ring->adapter;
144
145 writel(0x1, sds_ring->crb_intr_mask);
146
147 if (!QLCNIC_IS_MSI_FAMILY(adapter))
148 writel(0xfbff, adapter->tgt_mask_reg);
149}
150
151static int
152qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
153{
154 int size = sizeof(struct qlcnic_host_sds_ring) * count;
155
156 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
157
Eric Dumazet807540b2010-09-23 05:40:09 +0000158 return recv_ctx->sds_rings == NULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000159}
160
161static void
162qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
163{
164 if (recv_ctx->sds_rings != NULL)
165 kfree(recv_ctx->sds_rings);
166
167 recv_ctx->sds_rings = NULL;
168}
169
170static int
171qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
172{
173 int ring;
174 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000175 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000176
177 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
178 return -ENOMEM;
179
180 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
181 sds_ring = &recv_ctx->sds_rings[ring];
schacko8f891382010-06-17 02:56:40 +0000182
183 if (ring == adapter->max_sds_rings - 1)
184 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
185 QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
186 else
187 netif_napi_add(netdev, &sds_ring->napi,
188 qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000189 }
190
191 return 0;
192}
193
194static void
195qlcnic_napi_del(struct qlcnic_adapter *adapter)
196{
197 int ring;
198 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000199 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000200
201 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
202 sds_ring = &recv_ctx->sds_rings[ring];
203 netif_napi_del(&sds_ring->napi);
204 }
205
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000206 qlcnic_free_sds_rings(adapter->recv_ctx);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000207}
208
209static void
210qlcnic_napi_enable(struct qlcnic_adapter *adapter)
211{
212 int ring;
213 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000214 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000215
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000216 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
217 return;
218
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000219 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
220 sds_ring = &recv_ctx->sds_rings[ring];
221 napi_enable(&sds_ring->napi);
222 qlcnic_enable_int(sds_ring);
223 }
224}
225
226static void
227qlcnic_napi_disable(struct qlcnic_adapter *adapter)
228{
229 int ring;
230 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000231 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000232
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000233 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
234 return;
235
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000236 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
237 sds_ring = &recv_ctx->sds_rings[ring];
238 qlcnic_disable_int(sds_ring);
239 napi_synchronize(&sds_ring->napi);
240 napi_disable(&sds_ring->napi);
241 }
242}
243
244static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
245{
246 memset(&adapter->stats, 0, sizeof(adapter->stats));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000247}
248
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000249static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
250{
251 u32 control;
252 int pos;
253
254 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
255 if (pos) {
256 pci_read_config_dword(pdev, pos, &control);
257 if (enable)
258 control |= PCI_MSIX_FLAGS_ENABLE;
259 else
260 control = 0;
261 pci_write_config_dword(pdev, pos, control);
262 }
263}
264
265static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
266{
267 int i;
268
269 for (i = 0; i < count; i++)
270 adapter->msix_entries[i].entry = i;
271}
272
273static int
274qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
275{
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000276 u8 mac_addr[ETH_ALEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000277 struct net_device *netdev = adapter->netdev;
278 struct pci_dev *pdev = adapter->pdev;
279
Rajesh Borundiada48e6c2010-08-31 17:17:46 +0000280 if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000281 return -EIO;
282
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000283 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000284 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
285 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
286
287 /* set station address */
288
289 if (!is_valid_ether_addr(netdev->perm_addr))
290 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
291 netdev->dev_addr);
292
293 return 0;
294}
295
296static int qlcnic_set_mac(struct net_device *netdev, void *p)
297{
298 struct qlcnic_adapter *adapter = netdev_priv(netdev);
299 struct sockaddr *addr = p;
300
Rajesh Borundia73733732010-08-31 17:17:50 +0000301 if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
302 return -EOPNOTSUPP;
303
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000304 if (!is_valid_ether_addr(addr->sa_data))
305 return -EINVAL;
306
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000307 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000308 netif_device_detach(netdev);
309 qlcnic_napi_disable(adapter);
310 }
311
312 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
313 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
314 qlcnic_set_multi(adapter->netdev);
315
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000316 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000317 netif_device_attach(netdev);
318 qlcnic_napi_enable(adapter);
319 }
320 return 0;
321}
322
323static const struct net_device_ops qlcnic_netdev_ops = {
324 .ndo_open = qlcnic_open,
325 .ndo_stop = qlcnic_close,
326 .ndo_start_xmit = qlcnic_xmit_frame,
327 .ndo_get_stats = qlcnic_get_stats,
328 .ndo_validate_addr = eth_validate_addr,
329 .ndo_set_multicast_list = qlcnic_set_multi,
330 .ndo_set_mac_address = qlcnic_set_mac,
331 .ndo_change_mtu = qlcnic_change_mtu,
Michał Mirosław135d84a2011-04-19 03:03:57 +0000332 .ndo_fix_features = qlcnic_fix_features,
333 .ndo_set_features = qlcnic_set_features,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000334 .ndo_tx_timeout = qlcnic_tx_timeout,
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000335 .ndo_vlan_rx_add_vid = qlcnic_vlan_rx_add,
336 .ndo_vlan_rx_kill_vid = qlcnic_vlan_rx_del,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000337#ifdef CONFIG_NET_POLL_CONTROLLER
338 .ndo_poll_controller = qlcnic_poll_controller,
339#endif
340};
341
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000342static struct qlcnic_nic_template qlcnic_ops = {
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000343 .config_bridged_mode = qlcnic_config_bridged_mode,
344 .config_led = qlcnic_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000345 .start_firmware = qlcnic_start_firmware
346};
347
348static struct qlcnic_nic_template qlcnic_vf_ops = {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000349 .config_bridged_mode = qlcnicvf_config_bridged_mode,
350 .config_led = qlcnicvf_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000351 .start_firmware = qlcnicvf_start_firmware
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000352};
353
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000354static int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
355{
356 struct pci_dev *pdev = adapter->pdev;
357 int err = -1;
358
359 adapter->max_sds_rings = 1;
360 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
361 qlcnic_set_msix_bit(pdev, 0);
362
363 if (adapter->msix_supported) {
364 enable_msix:
365 qlcnic_init_msix_entries(adapter, num_msix);
366 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
367 if (err == 0) {
368 adapter->flags |= QLCNIC_MSIX_ENABLED;
369 qlcnic_set_msix_bit(pdev, 1);
370
371 adapter->max_sds_rings = num_msix;
372
373 dev_info(&pdev->dev, "using msi-x interrupts\n");
374 return err;
375 }
376 if (err > 0) {
377 num_msix = rounddown_pow_of_two(err);
378 if (num_msix)
379 goto enable_msix;
380 }
381 }
382 return err;
383}
384
385
386static void qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000387{
388 const struct qlcnic_legacy_intr_set *legacy_intrp;
389 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000390
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000391 if (use_msi && !pci_enable_msi(pdev)) {
392 adapter->flags |= QLCNIC_MSI_ENABLED;
393 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
394 msi_tgt_status[adapter->ahw->pci_func]);
395 dev_info(&pdev->dev, "using msi interrupts\n");
396 adapter->msix_entries[0].vector = pdev->irq;
397 return;
398 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000399
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000400 legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000401
402 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
403 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
404 legacy_intrp->tgt_status_reg);
405 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
406 legacy_intrp->tgt_mask_reg);
407 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
408
409 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
410 ISR_INT_STATE_REG);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000411 dev_info(&pdev->dev, "using legacy interrupts\n");
412 adapter->msix_entries[0].vector = pdev->irq;
413}
414
415static void
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +0000416qlcnic_setup_intr(struct qlcnic_adapter *adapter)
417{
418 int num_msix;
419
420 if (adapter->msix_supported) {
421 num_msix = (num_online_cpus() >=
422 QLCNIC_DEF_NUM_STS_DESC_RINGS) ?
423 QLCNIC_DEF_NUM_STS_DESC_RINGS :
424 QLCNIC_MIN_NUM_RSS_RINGS;
425 } else
426 num_msix = 1;
427
428 if (!qlcnic_enable_msix(adapter, num_msix))
429 return;
430
431 qlcnic_enable_msi_legacy(adapter);
432}
433
434static void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000435qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
436{
437 if (adapter->flags & QLCNIC_MSIX_ENABLED)
438 pci_disable_msix(adapter->pdev);
439 if (adapter->flags & QLCNIC_MSI_ENABLED)
440 pci_disable_msi(adapter->pdev);
441}
442
443static void
444qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
445{
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000446 if (adapter->ahw->pci_base0 != NULL)
447 iounmap(adapter->ahw->pci_base0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000448}
449
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000450static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000451qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
452{
Dan Carpentere88db3b2010-08-09 21:49:36 +0000453 struct qlcnic_pci_info *pci_info;
Julia Lawall900853a2010-08-16 10:23:51 +0000454 int i, ret = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000455 u8 pfn;
456
Dan Carpentere88db3b2010-08-09 21:49:36 +0000457 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
458 if (!pci_info)
459 return -ENOMEM;
460
Dan Carpenterca315ac2010-08-09 21:47:56 +0000461 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000462 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
Dan Carpentere88db3b2010-08-09 21:49:36 +0000463 if (!adapter->npars) {
Julia Lawall900853a2010-08-16 10:23:51 +0000464 ret = -ENOMEM;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000465 goto err_pci_info;
466 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000467
Dan Carpenterca315ac2010-08-09 21:47:56 +0000468 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000469 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
470 if (!adapter->eswitch) {
Julia Lawall900853a2010-08-16 10:23:51 +0000471 ret = -ENOMEM;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000472 goto err_npars;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000473 }
474
475 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000476 if (ret)
477 goto err_eswitch;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000478
Dan Carpenterca315ac2010-08-09 21:47:56 +0000479 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
480 pfn = pci_info[i].id;
Sony Chackof848d6d2011-04-01 14:27:59 +0000481 if (pfn > QLCNIC_MAX_PCI_FUNC) {
482 ret = QL_STATUS_INVALID_PARAM;
483 goto err_eswitch;
484 }
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000485 adapter->npars[pfn].active = (u8)pci_info[i].active;
486 adapter->npars[pfn].type = (u8)pci_info[i].type;
487 adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000488 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
489 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000490 }
491
Dan Carpenterca315ac2010-08-09 21:47:56 +0000492 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
493 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
494
Dan Carpentere88db3b2010-08-09 21:49:36 +0000495 kfree(pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000496 return 0;
497
498err_eswitch:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000499 kfree(adapter->eswitch);
500 adapter->eswitch = NULL;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000501err_npars:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000502 kfree(adapter->npars);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000503 adapter->npars = NULL;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000504err_pci_info:
505 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000506
507 return ret;
508}
509
510static int
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000511qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
512{
513 u8 id;
514 u32 ref_count;
515 int i, ret = 1;
516 u32 data = QLCNIC_MGMT_FUNC;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000517 void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000518
519 /* If other drivers are not in use set their privilege level */
Amit Kumar Salecha31018e02010-08-25 04:03:05 +0000520 ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000521 ret = qlcnic_api_lock(adapter);
522 if (ret)
523 goto err_lock;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000524
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000525 if (qlcnic_config_npars) {
526 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000527 id = i;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000528 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000529 id == adapter->ahw->pci_func)
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000530 continue;
531 data |= (qlcnic_config_npars &
532 QLC_DEV_SET_DRV(0xf, id));
533 }
534 } else {
535 data = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000536 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000537 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000538 adapter->ahw->pci_func));
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000539 }
540 writel(data, priv_op);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000541 qlcnic_api_unlock(adapter);
542err_lock:
543 return ret;
544}
545
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000546static void
547qlcnic_check_vf(struct qlcnic_adapter *adapter)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000548{
549 void __iomem *msix_base_addr;
550 void __iomem *priv_op;
551 u32 func;
552 u32 msix_base;
553 u32 op_mode, priv_level;
554
555 /* Determine FW API version */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000556 adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
557 QLCNIC_FW_API);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000558
559 /* Find PCI function number */
560 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000561 msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000562 msix_base = readl(msix_base_addr);
563 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000564 adapter->ahw->pci_func = func;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000565
566 /* Determine function privilege level */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000567 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000568 op_mode = readl(priv_op);
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000569 if (op_mode == QLC_DEV_DRV_DEFAULT)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000570 priv_level = QLCNIC_MGMT_FUNC;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000571 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000572 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000573
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000574 if (priv_level == QLCNIC_NON_PRIV_FUNC) {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000575 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
576 dev_info(&adapter->pdev->dev,
577 "HAL Version: %d Non Privileged function\n",
578 adapter->fw_hal_version);
579 adapter->nic_ops = &qlcnic_vf_ops;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000580 } else
581 adapter->nic_ops = &qlcnic_ops;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000582}
583
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000584static int
585qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
586{
587 void __iomem *mem_ptr0 = NULL;
588 resource_size_t mem_base;
589 unsigned long mem_len, pci_len0 = 0;
590
591 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000592
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000593 /* remap phys address */
594 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
595 mem_len = pci_resource_len(pdev, 0);
596
597 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
598
599 mem_ptr0 = pci_ioremap_bar(pdev, 0);
600 if (mem_ptr0 == NULL) {
601 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
602 return -EIO;
603 }
604 pci_len0 = mem_len;
605 } else {
606 return -EIO;
607 }
608
609 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
610
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000611 adapter->ahw->pci_base0 = mem_ptr0;
612 adapter->ahw->pci_len0 = pci_len0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000613
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000614 qlcnic_check_vf(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000615
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000616 adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
617 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
618 adapter->ahw->pci_func)));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000619
620 return 0;
621}
622
623static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
624{
625 struct pci_dev *pdev = adapter->pdev;
626 int i, found = 0;
627
628 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
629 if (qlcnic_boards[i].vendor == pdev->vendor &&
630 qlcnic_boards[i].device == pdev->device &&
631 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
632 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +0000633 sprintf(name, "%pM: %s" ,
634 adapter->mac_addr,
635 qlcnic_boards[i].short_name);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000636 found = 1;
637 break;
638 }
639
640 }
641
642 if (!found)
Sritej Velaga7f9a0c32010-06-17 02:56:39 +0000643 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000644}
645
646static void
647qlcnic_check_options(struct qlcnic_adapter *adapter)
648{
649 u32 fw_major, fw_minor, fw_build;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000650 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000651
652 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
653 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
654 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
655
656 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
657
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000658 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
659 fw_major, fw_minor, fw_build);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000660 if (adapter->ahw->port_type == QLCNIC_XGBE) {
Sony Chacko90d19002010-10-26 17:53:08 +0000661 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
662 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
663 adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
664 } else {
665 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
666 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
667 }
668
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000669 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
Sony Chacko90d19002010-10-26 17:53:08 +0000670 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
671
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000672 } else if (adapter->ahw->port_type == QLCNIC_GBE) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000673 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
674 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
Sony Chacko90d19002010-10-26 17:53:08 +0000675 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
676 adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000677 }
678
679 adapter->msix_supported = !!use_msi_x;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000680
681 adapter->num_txd = MAX_CMD_DESCRIPTORS;
682
Sony Chacko251b0362010-08-19 05:08:24 +0000683 adapter->max_rds_rings = MAX_RDS_RINGS;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000684}
685
Rajesh Borundia174240a2010-08-31 17:17:47 +0000686static int
687qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
688{
689 int err;
690 struct qlcnic_info nic_info;
691
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000692 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
Rajesh Borundia174240a2010-08-31 17:17:47 +0000693 if (err)
694 return err;
695
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000696 adapter->physical_port = (u8)nic_info.phys_port;
Rajesh Borundia174240a2010-08-31 17:17:47 +0000697 adapter->switch_mode = nic_info.switch_mode;
698 adapter->max_tx_ques = nic_info.max_tx_ques;
699 adapter->max_rx_ques = nic_info.max_rx_ques;
700 adapter->capabilities = nic_info.capabilities;
701 adapter->max_mac_filters = nic_info.max_mac_filters;
702 adapter->max_mtu = nic_info.max_mtu;
703
704 if (adapter->capabilities & BIT_6)
705 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
706 else
707 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
708
709 return err;
710}
711
Rajesh Borundia0325d692010-08-19 05:08:26 +0000712static void
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000713qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
714 struct qlcnic_esw_func_cfg *esw_cfg)
715{
716 if (esw_cfg->discard_tagged)
717 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
718 else
719 adapter->flags |= QLCNIC_TAGGING_ENABLED;
720
721 if (esw_cfg->vlan_id)
722 adapter->pvid = esw_cfg->vlan_id;
723 else
724 adapter->pvid = 0;
725}
726
727static void
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000728qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
729{
730 struct qlcnic_adapter *adapter = netdev_priv(netdev);
731 set_bit(vid, adapter->vlans);
732}
733
734static void
735qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
736{
737 struct qlcnic_adapter *adapter = netdev_priv(netdev);
738
739 qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
740 clear_bit(vid, adapter->vlans);
741}
742
743static void
Rajesh Borundia0325d692010-08-19 05:08:26 +0000744qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
745 struct qlcnic_esw_func_cfg *esw_cfg)
746{
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000747 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
748 QLCNIC_PROMISC_DISABLED);
Rajesh Borundia7613c872010-08-31 17:17:48 +0000749
750 if (esw_cfg->mac_anti_spoof)
751 adapter->flags |= QLCNIC_MACSPOOF;
Sony Chackofe4d4342010-08-19 05:08:27 +0000752
Rajesh Borundia73733732010-08-31 17:17:50 +0000753 if (!esw_cfg->mac_override)
754 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
755
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000756 if (!esw_cfg->promisc_mode)
757 adapter->flags |= QLCNIC_PROMISC_DISABLED;
758
Rajesh Borundia0325d692010-08-19 05:08:26 +0000759 qlcnic_set_netdev_features(adapter, esw_cfg);
760}
761
762static int
763qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
764{
765 struct qlcnic_esw_func_cfg esw_cfg;
766
767 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
768 return 0;
769
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000770 esw_cfg.pci_func = adapter->ahw->pci_func;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000771 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
772 return -EIO;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000773 qlcnic_set_vlan_config(adapter, &esw_cfg);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000774 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
775
776 return 0;
777}
778
779static void
780qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
781 struct qlcnic_esw_func_cfg *esw_cfg)
782{
783 struct net_device *netdev = adapter->netdev;
784 unsigned long features, vlan_features;
785
Michał Mirosław135d84a2011-04-19 03:03:57 +0000786 features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
Rajesh Borundia0325d692010-08-19 05:08:26 +0000787 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
788 vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000789 NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000790
791 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
792 features |= (NETIF_F_TSO | NETIF_F_TSO6);
793 vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
794 }
Rajesh Borundiab56421d2011-04-01 14:28:31 +0000795
796 if (netdev->features & NETIF_F_LRO)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000797 features |= NETIF_F_LRO;
798
799 if (esw_cfg->offload_flags & BIT_0) {
800 netdev->features |= features;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000801 if (!(esw_cfg->offload_flags & BIT_1))
802 netdev->features &= ~NETIF_F_TSO;
803 if (!(esw_cfg->offload_flags & BIT_2))
804 netdev->features &= ~NETIF_F_TSO6;
805 } else {
806 netdev->features &= ~features;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000807 }
808
809 netdev->vlan_features = (features & vlan_features);
810}
811
812static int
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000813qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
814{
815 void __iomem *priv_op;
816 u32 op_mode, priv_level;
817 int err = 0;
818
Rajesh Borundia174240a2010-08-31 17:17:47 +0000819 err = qlcnic_initialize_nic(adapter);
820 if (err)
821 return err;
822
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000823 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
824 return 0;
825
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000826 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000827 op_mode = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000828 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000829
830 if (op_mode == QLC_DEV_DRV_DEFAULT)
831 priv_level = QLCNIC_MGMT_FUNC;
832 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000833 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000834
Rajesh Borundia174240a2010-08-31 17:17:47 +0000835 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000836 if (priv_level == QLCNIC_MGMT_FUNC) {
837 adapter->op_mode = QLCNIC_MGMT_FUNC;
838 err = qlcnic_init_pci_info(adapter);
839 if (err)
840 return err;
841 /* Set privilege level for other functions */
842 qlcnic_set_function_modes(adapter);
843 dev_info(&adapter->pdev->dev,
844 "HAL Version: %d, Management function\n",
845 adapter->fw_hal_version);
846 } else if (priv_level == QLCNIC_PRIV_FUNC) {
847 adapter->op_mode = QLCNIC_PRIV_FUNC;
848 dev_info(&adapter->pdev->dev,
849 "HAL Version: %d, Privileged function\n",
850 adapter->fw_hal_version);
851 }
Rajesh Borundia174240a2010-08-31 17:17:47 +0000852 }
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000853
854 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
855
856 return err;
857}
858
859static int
Rajesh Borundia0325d692010-08-19 05:08:26 +0000860qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
861{
862 struct qlcnic_esw_func_cfg esw_cfg;
863 struct qlcnic_npar_info *npar;
864 u8 i;
865
Rajesh Borundia174240a2010-08-31 17:17:47 +0000866 if (adapter->need_fw_reset)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000867 return 0;
868
869 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
870 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
871 continue;
872 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
873 esw_cfg.pci_func = i;
874 esw_cfg.offload_flags = BIT_0;
Rajesh Borundia73733732010-08-31 17:17:50 +0000875 esw_cfg.mac_override = BIT_0;
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000876 esw_cfg.promisc_mode = BIT_0;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000877 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
878 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
879 if (qlcnic_config_switch_port(adapter, &esw_cfg))
880 return -EIO;
881 npar = &adapter->npars[i];
882 npar->pvid = esw_cfg.vlan_id;
Rajesh Borundia73733732010-08-31 17:17:50 +0000883 npar->mac_override = esw_cfg.mac_override;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000884 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
885 npar->discard_tagged = esw_cfg.discard_tagged;
886 npar->promisc_mode = esw_cfg.promisc_mode;
887 npar->offload_flags = esw_cfg.offload_flags;
888 }
889
890 return 0;
891}
892
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000893static int
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000894qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
895 struct qlcnic_npar_info *npar, int pci_func)
896{
897 struct qlcnic_esw_func_cfg esw_cfg;
898 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
899 esw_cfg.pci_func = pci_func;
900 esw_cfg.vlan_id = npar->pvid;
Rajesh Borundia73733732010-08-31 17:17:50 +0000901 esw_cfg.mac_override = npar->mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000902 esw_cfg.discard_tagged = npar->discard_tagged;
903 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
904 esw_cfg.offload_flags = npar->offload_flags;
905 esw_cfg.promisc_mode = npar->promisc_mode;
906 if (qlcnic_config_switch_port(adapter, &esw_cfg))
907 return -EIO;
908
909 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
910 if (qlcnic_config_switch_port(adapter, &esw_cfg))
911 return -EIO;
912
913 return 0;
914}
915
916static int
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000917qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
918{
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000919 int i, err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000920 struct qlcnic_npar_info *npar;
921 struct qlcnic_info nic_info;
922
Rajesh Borundia174240a2010-08-31 17:17:47 +0000923 if (!adapter->need_fw_reset)
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000924 return 0;
925
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000926 /* Set the NPAR config data after FW reset */
927 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
928 npar = &adapter->npars[i];
929 if (npar->type != QLCNIC_TYPE_NIC)
930 continue;
931 err = qlcnic_get_nic_info(adapter, &nic_info, i);
932 if (err)
933 return err;
934 nic_info.min_tx_bw = npar->min_bw;
935 nic_info.max_tx_bw = npar->max_bw;
936 err = qlcnic_set_nic_info(adapter, &nic_info);
937 if (err)
938 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000939
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000940 if (npar->enable_pm) {
941 err = qlcnic_config_port_mirroring(adapter,
942 npar->dest_npar, 1, i);
943 if (err)
944 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000945 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000946 err = qlcnic_reset_eswitch_config(adapter, npar, i);
947 if (err)
948 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000949 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000950 return 0;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000951}
952
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +0000953static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
954{
955 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
956 u32 npar_state;
957
958 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
959 return 0;
960
961 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
962 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
963 msleep(1000);
964 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
965 }
966 if (!npar_opt_timeo) {
967 dev_err(&adapter->pdev->dev,
968 "Waiting for NPAR state to opertional timeout\n");
969 return -EIO;
970 }
971 return 0;
972}
973
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000974static int
Rajesh Borundia174240a2010-08-31 17:17:47 +0000975qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
976{
977 int err;
978
979 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
980 adapter->op_mode != QLCNIC_MGMT_FUNC)
981 return 0;
982
983 err = qlcnic_set_default_offload_settings(adapter);
984 if (err)
985 return err;
986
987 err = qlcnic_reset_npar_config(adapter);
988 if (err)
989 return err;
990
991 qlcnic_dev_set_npar_ready(adapter);
992
993 return err;
994}
995
996static int
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000997qlcnic_start_firmware(struct qlcnic_adapter *adapter)
998{
Sony Chackod4066832010-08-19 05:08:31 +0000999 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001000
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00001001 err = qlcnic_can_start_firmware(adapter);
1002 if (err < 0)
1003 return err;
1004 else if (!err)
Sony Chackod4066832010-08-19 05:08:31 +00001005 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001006
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +00001007 if (load_fw_file)
1008 qlcnic_request_firmware(adapter);
schacko8f891382010-06-17 02:56:40 +00001009 else {
Sony Chacko8cfdce02010-08-26 14:02:41 +00001010 err = qlcnic_check_flash_fw_ver(adapter);
1011 if (err)
schacko8f891382010-06-17 02:56:40 +00001012 goto err_out;
1013
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +00001014 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
schacko8f891382010-06-17 02:56:40 +00001015 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001016
1017 err = qlcnic_need_fw_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001018 if (err == 0)
Sony Chacko4e708122010-08-31 17:17:44 +00001019 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001020
Sony Chackod4066832010-08-19 05:08:31 +00001021 err = qlcnic_pinit_from_rom(adapter);
1022 if (err)
1023 goto err_out;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001024
1025 err = qlcnic_load_firmware(adapter);
1026 if (err)
1027 goto err_out;
1028
1029 qlcnic_release_firmware(adapter);
Sony Chackod4066832010-08-19 05:08:31 +00001030 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001031
Sony Chackod4066832010-08-19 05:08:31 +00001032check_fw_status:
1033 err = qlcnic_check_fw_status(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001034 if (err)
1035 goto err_out;
1036
1037 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001038 qlcnic_idc_debug_info(adapter, 1);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00001039
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001040 err = qlcnic_check_eswitch_mode(adapter);
1041 if (err) {
1042 dev_err(&adapter->pdev->dev,
1043 "Memory allocation failed for eswitch\n");
1044 goto err_out;
1045 }
Rajesh Borundia174240a2010-08-31 17:17:47 +00001046 err = qlcnic_set_mgmt_operations(adapter);
1047 if (err)
1048 goto err_out;
1049
1050 qlcnic_check_options(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001051 adapter->need_fw_reset = 0;
1052
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001053 qlcnic_release_firmware(adapter);
1054 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001055
1056err_out:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001057 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1058 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001059
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001060 qlcnic_release_firmware(adapter);
1061 return err;
1062}
1063
1064static int
1065qlcnic_request_irq(struct qlcnic_adapter *adapter)
1066{
1067 irq_handler_t handler;
1068 struct qlcnic_host_sds_ring *sds_ring;
1069 int err, ring;
1070
1071 unsigned long flags = 0;
1072 struct net_device *netdev = adapter->netdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001073 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001074
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001075 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1076 handler = qlcnic_tmp_intr;
1077 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1078 flags |= IRQF_SHARED;
1079
1080 } else {
1081 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1082 handler = qlcnic_msix_intr;
1083 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1084 handler = qlcnic_msi_intr;
1085 else {
1086 flags |= IRQF_SHARED;
1087 handler = qlcnic_intr;
1088 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001089 }
1090 adapter->irq = netdev->irq;
1091
1092 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1093 sds_ring = &recv_ctx->sds_rings[ring];
1094 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1095 err = request_irq(sds_ring->irq, handler,
1096 flags, sds_ring->name, sds_ring);
1097 if (err)
1098 return err;
1099 }
1100
1101 return 0;
1102}
1103
1104static void
1105qlcnic_free_irq(struct qlcnic_adapter *adapter)
1106{
1107 int ring;
1108 struct qlcnic_host_sds_ring *sds_ring;
1109
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001110 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001111
1112 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1113 sds_ring = &recv_ctx->sds_rings[ring];
1114 free_irq(sds_ring->irq, sds_ring);
1115 }
1116}
1117
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001118static int
1119__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1120{
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001121 int ring;
1122 struct qlcnic_host_rds_ring *rds_ring;
1123
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001124 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1125 return -EIO;
1126
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001127 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1128 return 0;
Rajesh Borundia0325d692010-08-19 05:08:26 +00001129 if (qlcnic_set_eswitch_port_config(adapter))
1130 return -EIO;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001131
1132 if (qlcnic_fw_create_ctx(adapter))
1133 return -EIO;
1134
1135 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001136 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1137 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001138 }
1139
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001140 qlcnic_set_multi(netdev);
1141 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1142
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001143 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001144
1145 if (adapter->max_sds_rings > 1)
1146 qlcnic_config_rss(adapter, 1);
1147
1148 qlcnic_config_intr_coalesce(adapter);
1149
Sucheta Chakraborty24763d82010-08-17 00:34:25 +00001150 if (netdev->features & NETIF_F_LRO)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001151 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1152
1153 qlcnic_napi_enable(adapter);
1154
1155 qlcnic_linkevent_request(adapter, 1);
1156
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001157 adapter->reset_context = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001158 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1159 return 0;
1160}
1161
1162/* Usage: During resume and firmware recovery module.*/
1163
1164static int
1165qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1166{
1167 int err = 0;
1168
1169 rtnl_lock();
1170 if (netif_running(netdev))
1171 err = __qlcnic_up(adapter, netdev);
1172 rtnl_unlock();
1173
1174 return err;
1175}
1176
1177static void
1178__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1179{
1180 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1181 return;
1182
1183 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1184 return;
1185
1186 smp_mb();
1187 spin_lock(&adapter->tx_clean_lock);
1188 netif_carrier_off(netdev);
1189 netif_tx_disable(netdev);
1190
1191 qlcnic_free_mac_list(adapter);
1192
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001193 if (adapter->fhash.fnum)
1194 qlcnic_delete_lb_filters(adapter);
1195
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001196 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1197
1198 qlcnic_napi_disable(adapter);
1199
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001200 qlcnic_fw_destroy_ctx(adapter);
1201
1202 qlcnic_reset_rx_buffers_list(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001203 qlcnic_release_tx_buffers(adapter);
1204 spin_unlock(&adapter->tx_clean_lock);
1205}
1206
1207/* Usage: During suspend and firmware recovery module */
1208
1209static void
1210qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1211{
1212 rtnl_lock();
1213 if (netif_running(netdev))
1214 __qlcnic_down(adapter, netdev);
1215 rtnl_unlock();
1216
1217}
1218
1219static int
1220qlcnic_attach(struct qlcnic_adapter *adapter)
1221{
1222 struct net_device *netdev = adapter->netdev;
1223 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001224 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001225
1226 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1227 return 0;
1228
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001229 err = qlcnic_napi_add(adapter, netdev);
1230 if (err)
1231 return err;
1232
1233 err = qlcnic_alloc_sw_resources(adapter);
1234 if (err) {
1235 dev_err(&pdev->dev, "Error in setting sw resources\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001236 goto err_out_napi_del;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001237 }
1238
1239 err = qlcnic_alloc_hw_resources(adapter);
1240 if (err) {
1241 dev_err(&pdev->dev, "Error in setting hw resources\n");
1242 goto err_out_free_sw;
1243 }
1244
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001245 err = qlcnic_request_irq(adapter);
1246 if (err) {
1247 dev_err(&pdev->dev, "failed to setup interrupt\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001248 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001249 }
1250
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001251 qlcnic_create_sysfs_entries(adapter);
1252
1253 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1254 return 0;
1255
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001256err_out_free_hw:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001257 qlcnic_free_hw_resources(adapter);
1258err_out_free_sw:
1259 qlcnic_free_sw_resources(adapter);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001260err_out_napi_del:
1261 qlcnic_napi_del(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001262 return err;
1263}
1264
1265static void
1266qlcnic_detach(struct qlcnic_adapter *adapter)
1267{
1268 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1269 return;
1270
1271 qlcnic_remove_sysfs_entries(adapter);
1272
1273 qlcnic_free_hw_resources(adapter);
1274 qlcnic_release_rx_buffers(adapter);
1275 qlcnic_free_irq(adapter);
1276 qlcnic_napi_del(adapter);
1277 qlcnic_free_sw_resources(adapter);
1278
1279 adapter->is_up = 0;
1280}
1281
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001282void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1283{
1284 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1285 struct qlcnic_host_sds_ring *sds_ring;
1286 int ring;
1287
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001288 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001289 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1290 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001291 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001292 qlcnic_disable_int(sds_ring);
1293 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001294 }
1295
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001296 qlcnic_fw_destroy_ctx(adapter);
1297
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001298 qlcnic_detach(adapter);
1299
1300 adapter->diag_test = 0;
1301 adapter->max_sds_rings = max_sds_rings;
1302
1303 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001304 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001305
1306 if (netif_running(netdev))
1307 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001308out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001309 netif_device_attach(netdev);
1310}
1311
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001312static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1313{
1314 int err = 0;
1315 adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1316 GFP_KERNEL);
1317 if (!adapter->ahw) {
1318 dev_err(&adapter->pdev->dev,
1319 "Failed to allocate recv ctx resources for adapter\n");
1320 err = -ENOMEM;
1321 goto err_out;
1322 }
1323 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1324 GFP_KERNEL);
1325 if (!adapter->recv_ctx) {
1326 dev_err(&adapter->pdev->dev,
1327 "Failed to allocate recv ctx resources for adapter\n");
1328 kfree(adapter->ahw);
1329 adapter->ahw = NULL;
1330 err = -ENOMEM;
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001331 goto err_out;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001332 }
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001333 /* Initialize interrupt coalesce parameters */
1334 adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1335 adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1336 adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001337err_out:
1338 return err;
1339}
1340
1341static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1342{
1343 kfree(adapter->recv_ctx);
1344 adapter->recv_ctx = NULL;
1345
Anirban Chakraborty18f2f612011-05-12 12:48:33 +00001346 if (adapter->ahw->fw_dump.tmpl_hdr) {
1347 vfree(adapter->ahw->fw_dump.tmpl_hdr);
1348 adapter->ahw->fw_dump.tmpl_hdr = NULL;
1349 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001350 kfree(adapter->ahw);
1351 adapter->ahw = NULL;
1352}
1353
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001354int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1355{
1356 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1357 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001358 struct qlcnic_host_rds_ring *rds_ring;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001359 int ring;
1360 int ret;
1361
1362 netif_device_detach(netdev);
1363
1364 if (netif_running(netdev))
1365 __qlcnic_down(adapter, netdev);
1366
1367 qlcnic_detach(adapter);
1368
1369 adapter->max_sds_rings = 1;
1370 adapter->diag_test = test;
1371
1372 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001373 if (ret) {
1374 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001375 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001376 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001377
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001378 ret = qlcnic_fw_create_ctx(adapter);
1379 if (ret) {
1380 qlcnic_detach(adapter);
Sony Chacko57e46242010-07-24 18:32:18 +00001381 netif_device_attach(netdev);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001382 return ret;
1383 }
1384
1385 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001386 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1387 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001388 }
1389
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001390 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1391 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001392 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001393 qlcnic_enable_int(sds_ring);
1394 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001395 }
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001396 set_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001397
1398 return 0;
1399}
1400
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001401/* Reset context in hardware only */
1402static int
1403qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1404{
1405 struct net_device *netdev = adapter->netdev;
1406
1407 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1408 return -EBUSY;
1409
1410 netif_device_detach(netdev);
1411
1412 qlcnic_down(adapter, netdev);
1413
1414 qlcnic_up(adapter, netdev);
1415
1416 netif_device_attach(netdev);
1417
1418 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1419 return 0;
1420}
1421
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001422int
1423qlcnic_reset_context(struct qlcnic_adapter *adapter)
1424{
1425 int err = 0;
1426 struct net_device *netdev = adapter->netdev;
1427
1428 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1429 return -EBUSY;
1430
1431 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1432
1433 netif_device_detach(netdev);
1434
1435 if (netif_running(netdev))
1436 __qlcnic_down(adapter, netdev);
1437
1438 qlcnic_detach(adapter);
1439
1440 if (netif_running(netdev)) {
1441 err = qlcnic_attach(adapter);
1442 if (!err)
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001443 __qlcnic_up(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001444 }
1445
1446 netif_device_attach(netdev);
1447 }
1448
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001449 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1450 return err;
1451}
1452
1453static int
1454qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001455 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001456{
1457 int err;
1458 struct pci_dev *pdev = adapter->pdev;
1459
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001460 adapter->mc_enabled = 0;
1461 adapter->max_mc_count = 38;
1462
1463 netdev->netdev_ops = &qlcnic_netdev_ops;
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001464 netdev->watchdog_timeo = 5*HZ;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001465
1466 qlcnic_change_mtu(netdev, netdev->mtu);
1467
1468 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1469
Michał Mirosław135d84a2011-04-19 03:03:57 +00001470 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1471 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
Anirban Chakrabortyac8d0c42010-07-09 13:14:58 +00001472
Michał Mirosław135d84a2011-04-19 03:03:57 +00001473 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1474 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1475 if (pci_using_dac)
1476 netdev->hw_features |= NETIF_F_HIGHDMA;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001477
Michał Mirosław135d84a2011-04-19 03:03:57 +00001478 netdev->vlan_features = netdev->hw_features;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001479
1480 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
Michał Mirosław135d84a2011-04-19 03:03:57 +00001481 netdev->hw_features |= NETIF_F_HW_VLAN_TX;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001482 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
Michał Mirosław135d84a2011-04-19 03:03:57 +00001483 netdev->hw_features |= NETIF_F_LRO;
1484
1485 netdev->features |= netdev->hw_features |
1486 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1487
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001488 netdev->irq = adapter->msix_entries[0].vector;
1489
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001490 netif_carrier_off(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001491
1492 err = register_netdev(netdev);
1493 if (err) {
1494 dev_err(&pdev->dev, "failed to register net device\n");
1495 return err;
1496 }
1497
1498 return 0;
1499}
1500
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001501static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1502{
1503 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1504 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1505 *pci_using_dac = 1;
1506 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1507 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1508 *pci_using_dac = 0;
1509 else {
1510 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1511 return -EIO;
1512 }
1513
1514 return 0;
1515}
1516
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001517static int
1518qlcnic_alloc_msix_entries(struct qlcnic_adapter *adapter, u16 count)
1519{
1520 adapter->msix_entries = kcalloc(count, sizeof(struct msix_entry),
1521 GFP_KERNEL);
1522
1523 if (adapter->msix_entries)
1524 return 0;
1525
1526 dev_err(&adapter->pdev->dev, "failed allocating msix_entries\n");
1527 return -ENOMEM;
1528}
1529
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001530static int __devinit
1531qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1532{
1533 struct net_device *netdev = NULL;
1534 struct qlcnic_adapter *adapter = NULL;
1535 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001536 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001537 uint8_t pci_using_dac;
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001538 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001539
1540 err = pci_enable_device(pdev);
1541 if (err)
1542 return err;
1543
1544 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1545 err = -ENODEV;
1546 goto err_out_disable_pdev;
1547 }
1548
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001549 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1550 if (err)
1551 goto err_out_disable_pdev;
1552
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001553 err = pci_request_regions(pdev, qlcnic_driver_name);
1554 if (err)
1555 goto err_out_disable_pdev;
1556
1557 pci_set_master(pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001558 pci_enable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001559
1560 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1561 if (!netdev) {
1562 dev_err(&pdev->dev, "failed to allocate net_device\n");
1563 err = -ENOMEM;
1564 goto err_out_free_res;
1565 }
1566
1567 SET_NETDEV_DEV(netdev, &pdev->dev);
1568
1569 adapter = netdev_priv(netdev);
1570 adapter->netdev = netdev;
1571 adapter->pdev = pdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001572
1573 if (qlcnic_alloc_adapter_resources(adapter))
1574 goto err_out_free_netdev;
1575
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001576 adapter->dev_rst_time = jiffies;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001577 revision_id = pdev->revision;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001578 adapter->ahw->revision_id = revision_id;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001579
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001580 rwlock_init(&adapter->ahw->crb_lock);
1581 mutex_init(&adapter->ahw->mem_lock);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001582
1583 spin_lock_init(&adapter->tx_clean_lock);
1584 INIT_LIST_HEAD(&adapter->mac_list);
1585
1586 err = qlcnic_setup_pci_map(adapter);
1587 if (err)
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001588 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001589
1590 /* This will be reset for mezz cards */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001591 adapter->portnum = adapter->ahw->pci_func;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001592
Anirban Chakraborty18f2f612011-05-12 12:48:33 +00001593 /* Get FW dump template and store it */
1594 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC)
1595 qlcnic_fw_cmd_get_minidump_temp(adapter);
1596
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001597 err = qlcnic_get_board_info(adapter);
1598 if (err) {
1599 dev_err(&pdev->dev, "Error getting board config info.\n");
1600 goto err_out_iounmap;
1601 }
1602
Sony Chacko8cfdce02010-08-26 14:02:41 +00001603 err = qlcnic_setup_idc_param(adapter);
1604 if (err)
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +00001605 goto err_out_iounmap;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001606
Rajesh Borundia1dc0f3c2010-12-16 22:59:02 +00001607 adapter->flags |= QLCNIC_NEED_FLR;
Rajesh Borundiab0044bc2010-11-23 01:25:21 +00001608
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001609 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001610 if (err) {
1611 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001612 goto err_out_decr_ref;
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001613 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001614
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001615 if (qlcnic_read_mac_addr(adapter))
1616 dev_warn(&pdev->dev, "failed to read mac addr\n");
1617
1618 if (adapter->portnum == 0) {
1619 get_brd_name(adapter, brd_name);
1620
1621 pr_info("%s: %s Board Chip rev 0x%x\n",
1622 module_name(THIS_MODULE),
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001623 brd_name, adapter->ahw->revision_id);
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001624 }
1625
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001626 qlcnic_clear_stats(adapter);
1627
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001628 err = qlcnic_alloc_msix_entries(adapter, adapter->max_rx_ques);
1629 if (err)
1630 goto err_out_decr_ref;
1631
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001632 qlcnic_setup_intr(adapter);
1633
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001634 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001635 if (err)
1636 goto err_out_disable_msi;
1637
1638 pci_set_drvdata(pdev, adapter);
1639
1640 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1641
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001642 switch (adapter->ahw->port_type) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001643 case QLCNIC_GBE:
1644 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1645 adapter->netdev->name);
1646 break;
1647 case QLCNIC_XGBE:
1648 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1649 adapter->netdev->name);
1650 break;
1651 }
1652
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001653 qlcnic_alloc_lb_filters_mem(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001654 qlcnic_create_diag_entries(adapter);
1655
1656 return 0;
1657
1658err_out_disable_msi:
1659 qlcnic_teardown_intr(adapter);
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001660 kfree(adapter->msix_entries);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001661
1662err_out_decr_ref:
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001663 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001664
1665err_out_iounmap:
1666 qlcnic_cleanup_pci_map(adapter);
1667
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001668err_out_free_hw:
1669 qlcnic_free_adapter_resources(adapter);
1670
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001671err_out_free_netdev:
1672 free_netdev(netdev);
1673
1674err_out_free_res:
1675 pci_release_regions(pdev);
1676
1677err_out_disable_pdev:
1678 pci_set_drvdata(pdev, NULL);
1679 pci_disable_device(pdev);
1680 return err;
1681}
1682
1683static void __devexit qlcnic_remove(struct pci_dev *pdev)
1684{
1685 struct qlcnic_adapter *adapter;
1686 struct net_device *netdev;
1687
1688 adapter = pci_get_drvdata(pdev);
1689 if (adapter == NULL)
1690 return;
1691
1692 netdev = adapter->netdev;
1693
1694 qlcnic_cancel_fw_work(adapter);
1695
1696 unregister_netdev(netdev);
1697
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001698 qlcnic_detach(adapter);
1699
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001700 if (adapter->npars != NULL)
1701 kfree(adapter->npars);
1702 if (adapter->eswitch != NULL)
1703 kfree(adapter->eswitch);
1704
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001705 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001706
1707 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1708
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001709 qlcnic_free_lb_filters_mem(adapter);
1710
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001711 qlcnic_teardown_intr(adapter);
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00001712 kfree(adapter->msix_entries);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001713
1714 qlcnic_remove_diag_entries(adapter);
1715
1716 qlcnic_cleanup_pci_map(adapter);
1717
1718 qlcnic_release_firmware(adapter);
1719
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001720 pci_disable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001721 pci_release_regions(pdev);
1722 pci_disable_device(pdev);
1723 pci_set_drvdata(pdev, NULL);
1724
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001725 qlcnic_free_adapter_resources(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001726 free_netdev(netdev);
1727}
1728static int __qlcnic_shutdown(struct pci_dev *pdev)
1729{
1730 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1731 struct net_device *netdev = adapter->netdev;
1732 int retval;
1733
1734 netif_device_detach(netdev);
1735
1736 qlcnic_cancel_fw_work(adapter);
1737
1738 if (netif_running(netdev))
1739 qlcnic_down(adapter, netdev);
1740
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001741 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001742
1743 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1744
1745 retval = pci_save_state(pdev);
1746 if (retval)
1747 return retval;
1748
1749 if (qlcnic_wol_supported(adapter)) {
1750 pci_enable_wake(pdev, PCI_D3cold, 1);
1751 pci_enable_wake(pdev, PCI_D3hot, 1);
1752 }
1753
1754 return 0;
1755}
1756
1757static void qlcnic_shutdown(struct pci_dev *pdev)
1758{
1759 if (__qlcnic_shutdown(pdev))
1760 return;
1761
1762 pci_disable_device(pdev);
1763}
1764
1765#ifdef CONFIG_PM
1766static int
1767qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1768{
1769 int retval;
1770
1771 retval = __qlcnic_shutdown(pdev);
1772 if (retval)
1773 return retval;
1774
1775 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1776 return 0;
1777}
1778
1779static int
1780qlcnic_resume(struct pci_dev *pdev)
1781{
1782 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1783 struct net_device *netdev = adapter->netdev;
1784 int err;
1785
1786 err = pci_enable_device(pdev);
1787 if (err)
1788 return err;
1789
1790 pci_set_power_state(pdev, PCI_D0);
1791 pci_set_master(pdev);
1792 pci_restore_state(pdev);
1793
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001794 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001795 if (err) {
1796 dev_err(&pdev->dev, "failed to start firmware\n");
1797 return err;
1798 }
1799
1800 if (netif_running(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001801 err = qlcnic_up(adapter, netdev);
1802 if (err)
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001803 goto done;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001804
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00001805 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001806 }
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001807done:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001808 netif_device_attach(netdev);
1809 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1810 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001811}
1812#endif
1813
1814static int qlcnic_open(struct net_device *netdev)
1815{
1816 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1817 int err;
1818
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001819 err = qlcnic_attach(adapter);
1820 if (err)
1821 return err;
1822
1823 err = __qlcnic_up(adapter, netdev);
1824 if (err)
1825 goto err_out;
1826
1827 netif_start_queue(netdev);
1828
1829 return 0;
1830
1831err_out:
1832 qlcnic_detach(adapter);
1833 return err;
1834}
1835
1836/*
1837 * qlcnic_close - Disables a network interface entry point
1838 */
1839static int qlcnic_close(struct net_device *netdev)
1840{
1841 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1842
1843 __qlcnic_down(adapter, netdev);
1844 return 0;
1845}
1846
1847static void
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001848qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1849{
1850 void *head;
1851 int i;
1852
1853 if (!qlcnic_mac_learn)
1854 return;
1855
1856 spin_lock_init(&adapter->mac_learn_lock);
1857
1858 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1859 GFP_KERNEL);
1860 if (!head)
1861 return;
1862
1863 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1864 adapter->fhash.fhead = (struct hlist_head *)head;
1865
1866 for (i = 0; i < adapter->fhash.fmax; i++)
1867 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1868}
1869
1870static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1871{
1872 if (adapter->fhash.fmax && adapter->fhash.fhead)
1873 kfree(adapter->fhash.fhead);
1874
1875 adapter->fhash.fhead = NULL;
1876 adapter->fhash.fmax = 0;
1877}
1878
1879static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001880 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001881{
1882 struct cmd_desc_type0 *hwdesc;
1883 struct qlcnic_nic_req *req;
1884 struct qlcnic_mac_req *mac_req;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001885 struct qlcnic_vlan_req *vlan_req;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001886 u32 producer;
1887 u64 word;
1888
1889 producer = tx_ring->producer;
1890 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1891
1892 req = (struct qlcnic_nic_req *)hwdesc;
1893 memset(req, 0, sizeof(struct qlcnic_nic_req));
1894 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1895
1896 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1897 req->req_hdr = cpu_to_le64(word);
1898
1899 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001900 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001901 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1902
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001903 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1904 vlan_req->vlan_id = vlan_id;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001905
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001906 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001907 smp_mb();
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001908}
1909
1910#define QLCNIC_MAC_HASH(MAC)\
1911 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1912
1913static void
1914qlcnic_send_filter(struct qlcnic_adapter *adapter,
1915 struct qlcnic_host_tx_ring *tx_ring,
1916 struct cmd_desc_type0 *first_desc,
1917 struct sk_buff *skb)
1918{
1919 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1920 struct qlcnic_filter *fil, *tmp_fil;
1921 struct hlist_node *tmp_hnode, *n;
1922 struct hlist_head *head;
1923 u64 src_addr = 0;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001924 __le16 vlan_id = 0;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001925 u8 hindex;
1926
1927 if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1928 return;
1929
1930 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1931 return;
1932
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001933 /* Only NPAR capable devices support vlan based learning*/
1934 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1935 vlan_id = first_desc->vlan_TCI;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001936 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1937 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1938 head = &(adapter->fhash.fhead[hindex]);
1939
1940 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001941 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1942 tmp_fil->vlan_id == vlan_id) {
amit salechae5edb7b2010-10-26 17:53:07 +00001943
1944 if (jiffies >
1945 (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1946 qlcnic_change_filter(adapter, src_addr, vlan_id,
1947 tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001948 tmp_fil->ftime = jiffies;
1949 return;
1950 }
1951 }
1952
1953 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1954 if (!fil)
1955 return;
1956
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001957 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001958
1959 fil->ftime = jiffies;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001960 fil->vlan_id = vlan_id;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001961 memcpy(fil->faddr, &src_addr, ETH_ALEN);
1962 spin_lock(&adapter->mac_learn_lock);
1963 hlist_add_head(&(fil->fnode), head);
1964 adapter->fhash.fnum++;
1965 spin_unlock(&adapter->mac_learn_lock);
1966}
1967
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001968static int
1969qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001970 struct cmd_desc_type0 *first_desc,
1971 struct sk_buff *skb)
1972{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001973 u8 opcode = 0, hdr_len = 0;
1974 u16 flags = 0, vlan_tci = 0;
1975 int copied, offset, copy_len;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001976 struct cmd_desc_type0 *hwdesc;
1977 struct vlan_ethhdr *vh;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001978 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1979 u16 protocol = ntohs(skb->protocol);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001980 u32 producer = tx_ring->producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001981
1982 if (protocol == ETH_P_8021Q) {
1983 vh = (struct vlan_ethhdr *)skb->data;
1984 flags = FLAGS_VLAN_TAGGED;
1985 vlan_tci = vh->h_vlan_TCI;
1986 } else if (vlan_tx_tag_present(skb)) {
1987 flags = FLAGS_VLAN_OOB;
1988 vlan_tci = vlan_tx_tag_get(skb);
1989 }
1990 if (unlikely(adapter->pvid)) {
1991 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
1992 return -EIO;
1993 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
1994 goto set_flags;
1995
1996 flags = FLAGS_VLAN_OOB;
1997 vlan_tci = adapter->pvid;
1998 }
1999set_flags:
2000 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
2001 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002002
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00002003 if (*(skb->data) & BIT_0) {
2004 flags |= BIT_0;
2005 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
2006 }
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002007 opcode = TX_ETHER_PKT;
2008 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002009 skb_shinfo(skb)->gso_size > 0) {
2010
2011 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2012
2013 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2014 first_desc->total_hdr_length = hdr_len;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002015
2016 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
2017
2018 /* For LSO, we need to copy the MAC/IP/TCP headers into
2019 * the descriptor ring */
2020 copied = 0;
2021 offset = 2;
2022
2023 if (flags & FLAGS_VLAN_OOB) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002024 first_desc->total_hdr_length += VLAN_HLEN;
2025 first_desc->tcp_hdr_offset = VLAN_HLEN;
2026 first_desc->ip_hdr_offset = VLAN_HLEN;
2027 /* Only in case of TSO on vlan device */
2028 flags |= FLAGS_VLAN_TAGGED;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002029
2030 /* Create a TSO vlan header template for firmware */
2031
2032 hwdesc = &tx_ring->desc_head[producer];
2033 tx_ring->cmd_buf_arr[producer].skb = NULL;
2034
2035 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2036 offset, hdr_len + VLAN_HLEN);
2037
2038 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
2039 skb_copy_from_linear_data(skb, vh, 12);
2040 vh->h_vlan_proto = htons(ETH_P_8021Q);
2041 vh->h_vlan_TCI = htons(vlan_tci);
2042
2043 skb_copy_from_linear_data_offset(skb, 12,
2044 (char *)vh + 16, copy_len - 16);
2045
2046 copied = copy_len - VLAN_HLEN;
2047 offset = 0;
2048
2049 producer = get_next_index(producer, tx_ring->num_desc);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002050 }
2051
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002052 while (copied < hdr_len) {
2053
2054 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2055 offset, (hdr_len - copied));
2056
2057 hwdesc = &tx_ring->desc_head[producer];
2058 tx_ring->cmd_buf_arr[producer].skb = NULL;
2059
2060 skb_copy_from_linear_data_offset(skb, copied,
2061 (char *) hwdesc + offset, copy_len);
2062
2063 copied += copy_len;
2064 offset = 0;
2065
2066 producer = get_next_index(producer, tx_ring->num_desc);
2067 }
2068
2069 tx_ring->producer = producer;
2070 smp_mb();
2071 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002072
2073 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2074 u8 l4proto;
2075
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002076 if (protocol == ETH_P_IP) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002077 l4proto = ip_hdr(skb)->protocol;
2078
2079 if (l4proto == IPPROTO_TCP)
2080 opcode = TX_TCP_PKT;
2081 else if (l4proto == IPPROTO_UDP)
2082 opcode = TX_UDP_PKT;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002083 } else if (protocol == ETH_P_IPV6) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002084 l4proto = ipv6_hdr(skb)->nexthdr;
2085
2086 if (l4proto == IPPROTO_TCP)
2087 opcode = TX_TCPV6_PKT;
2088 else if (l4proto == IPPROTO_UDP)
2089 opcode = TX_UDPV6_PKT;
2090 }
2091 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002092 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2093 first_desc->ip_hdr_offset += skb_network_offset(skb);
2094 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2095
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002096 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002097}
2098
2099static int
2100qlcnic_map_tx_skb(struct pci_dev *pdev,
2101 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2102{
2103 struct qlcnic_skb_frag *nf;
2104 struct skb_frag_struct *frag;
2105 int i, nr_frags;
2106 dma_addr_t map;
2107
2108 nr_frags = skb_shinfo(skb)->nr_frags;
2109 nf = &pbuf->frag_array[0];
2110
2111 map = pci_map_single(pdev, skb->data,
2112 skb_headlen(skb), PCI_DMA_TODEVICE);
2113 if (pci_dma_mapping_error(pdev, map))
2114 goto out_err;
2115
2116 nf->dma = map;
2117 nf->length = skb_headlen(skb);
2118
2119 for (i = 0; i < nr_frags; i++) {
2120 frag = &skb_shinfo(skb)->frags[i];
2121 nf = &pbuf->frag_array[i+1];
2122
2123 map = pci_map_page(pdev, frag->page, frag->page_offset,
2124 frag->size, PCI_DMA_TODEVICE);
2125 if (pci_dma_mapping_error(pdev, map))
2126 goto unwind;
2127
2128 nf->dma = map;
2129 nf->length = frag->size;
2130 }
2131
2132 return 0;
2133
2134unwind:
2135 while (--i >= 0) {
2136 nf = &pbuf->frag_array[i+1];
2137 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2138 }
2139
2140 nf = &pbuf->frag_array[0];
2141 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2142
2143out_err:
2144 return -ENOMEM;
2145}
2146
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002147static void
2148qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2149 struct qlcnic_cmd_buffer *pbuf)
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002150{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002151 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2152 int nr_frags = skb_shinfo(skb)->nr_frags;
2153 int i;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002154
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002155 for (i = 0; i < nr_frags; i++) {
2156 nf = &pbuf->frag_array[i+1];
2157 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002158 }
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002159
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002160 nf = &pbuf->frag_array[0];
2161 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
Sucheta Chakraborty5b446c62011-06-03 05:52:18 +00002162 pbuf->skb = NULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002163}
2164
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002165static inline void
2166qlcnic_clear_cmddesc(u64 *desc)
2167{
2168 desc[0] = 0ULL;
2169 desc[2] = 0ULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002170 desc[7] = 0ULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002171}
2172
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00002173netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002174qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2175{
2176 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2177 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2178 struct qlcnic_cmd_buffer *pbuf;
2179 struct qlcnic_skb_frag *buffrag;
2180 struct cmd_desc_type0 *hwdesc, *first_desc;
2181 struct pci_dev *pdev;
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002182 struct ethhdr *phdr;
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002183 int delta = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002184 int i, k;
2185
2186 u32 producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002187 int frag_count;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002188 u32 num_txd = tx_ring->num_desc;
2189
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00002190 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2191 netif_stop_queue(netdev);
2192 return NETDEV_TX_BUSY;
2193 }
2194
Sony Chackofe4d4342010-08-19 05:08:27 +00002195 if (adapter->flags & QLCNIC_MACSPOOF) {
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002196 phdr = (struct ethhdr *)skb->data;
2197 if (compare_ether_addr(phdr->h_source,
Sony Chackofe4d4342010-08-19 05:08:27 +00002198 adapter->mac_addr))
2199 goto drop_packet;
2200 }
2201
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002202 frag_count = skb_shinfo(skb)->nr_frags + 1;
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002203 /* 14 frags supported for normal packet and
2204 * 32 frags supported for TSO packet
2205 */
2206 if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
2207
2208 for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
2209 delta += skb_shinfo(skb)->frags[i].size;
2210
2211 if (!__pskb_pull_tail(skb, delta))
2212 goto drop_packet;
2213
2214 frag_count = 1 + skb_shinfo(skb)->nr_frags;
2215 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002216
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002217 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002218 netif_stop_queue(netdev);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002219 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2220 netif_start_queue(netdev);
2221 else {
2222 adapter->stats.xmit_off++;
2223 return NETDEV_TX_BUSY;
2224 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002225 }
2226
2227 producer = tx_ring->producer;
2228 pbuf = &tx_ring->cmd_buf_arr[producer];
2229
2230 pdev = adapter->pdev;
2231
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002232 first_desc = hwdesc = &tx_ring->desc_head[producer];
2233 qlcnic_clear_cmddesc((u64 *)hwdesc);
2234
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002235 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2236 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002237 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002238 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002239
2240 pbuf->skb = skb;
2241 pbuf->frag_count = frag_count;
2242
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002243 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2244 qlcnic_set_tx_port(first_desc, adapter->portnum);
2245
2246 for (i = 0; i < frag_count; i++) {
2247
2248 k = i % 4;
2249
2250 if ((k == 0) && (i > 0)) {
2251 /* move to next desc.*/
2252 producer = get_next_index(producer, num_txd);
2253 hwdesc = &tx_ring->desc_head[producer];
2254 qlcnic_clear_cmddesc((u64 *)hwdesc);
2255 tx_ring->cmd_buf_arr[producer].skb = NULL;
2256 }
2257
2258 buffrag = &pbuf->frag_array[i];
2259
2260 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2261 switch (k) {
2262 case 0:
2263 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2264 break;
2265 case 1:
2266 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2267 break;
2268 case 2:
2269 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2270 break;
2271 case 3:
2272 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2273 break;
2274 }
2275 }
2276
2277 tx_ring->producer = get_next_index(producer, num_txd);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002278 smp_mb();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002279
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002280 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2281 goto unwind_buff;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002282
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002283 if (qlcnic_mac_learn)
2284 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2285
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002286 qlcnic_update_cmd_producer(adapter, tx_ring);
2287
2288 adapter->stats.txbytes += skb->len;
2289 adapter->stats.xmitcalled++;
2290
2291 return NETDEV_TX_OK;
2292
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002293unwind_buff:
2294 qlcnic_unmap_buffers(pdev, skb, pbuf);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002295drop_packet:
2296 adapter->stats.txdropped++;
2297 dev_kfree_skb_any(skb);
2298 return NETDEV_TX_OK;
2299}
2300
2301static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2302{
2303 struct net_device *netdev = adapter->netdev;
2304 u32 temp, temp_state, temp_val;
2305 int rv = 0;
2306
2307 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2308
2309 temp_state = qlcnic_get_temp_state(temp);
2310 temp_val = qlcnic_get_temp_val(temp);
2311
2312 if (temp_state == QLCNIC_TEMP_PANIC) {
2313 dev_err(&netdev->dev,
2314 "Device temperature %d degrees C exceeds"
2315 " maximum allowed. Hardware has been shut down.\n",
2316 temp_val);
2317 rv = 1;
2318 } else if (temp_state == QLCNIC_TEMP_WARN) {
2319 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2320 dev_err(&netdev->dev,
2321 "Device temperature %d degrees C "
2322 "exceeds operating range."
2323 " Immediate action needed.\n",
2324 temp_val);
2325 }
2326 } else {
2327 if (adapter->temp == QLCNIC_TEMP_WARN) {
2328 dev_info(&netdev->dev,
2329 "Device temperature is now %d degrees C"
2330 " in normal range.\n", temp_val);
2331 }
2332 }
2333 adapter->temp = temp_state;
2334 return rv;
2335}
2336
2337void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2338{
2339 struct net_device *netdev = adapter->netdev;
2340
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002341 if (adapter->ahw->linkup && !linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002342 netdev_info(netdev, "NIC Link is down\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002343 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002344 if (netif_running(netdev)) {
2345 netif_carrier_off(netdev);
2346 netif_stop_queue(netdev);
2347 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002348 } else if (!adapter->ahw->linkup && linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002349 netdev_info(netdev, "NIC Link is up\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002350 adapter->ahw->linkup = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002351 if (netif_running(netdev)) {
2352 netif_carrier_on(netdev);
2353 netif_wake_queue(netdev);
2354 }
2355 }
2356}
2357
2358static void qlcnic_tx_timeout(struct net_device *netdev)
2359{
2360 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2361
2362 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2363 return;
2364
2365 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002366
2367 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002368 adapter->need_fw_reset = 1;
2369 else
2370 adapter->reset_context = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002371}
2372
2373static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2374{
2375 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2376 struct net_device_stats *stats = &netdev->stats;
2377
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002378 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2379 stats->tx_packets = adapter->stats.xmitfinished;
Sucheta Chakraborty7e382592010-05-17 01:22:10 +00002380 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002381 stats->tx_bytes = adapter->stats.txbytes;
2382 stats->rx_dropped = adapter->stats.rxdropped;
2383 stats->tx_dropped = adapter->stats.txdropped;
2384
2385 return stats;
2386}
2387
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002388static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002389{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002390 u32 status;
2391
2392 status = readl(adapter->isr_int_vec);
2393
2394 if (!(status & adapter->int_vec_bit))
2395 return IRQ_NONE;
2396
2397 /* check interrupt state machine, to be sure */
2398 status = readl(adapter->crb_int_state_reg);
2399 if (!ISR_LEGACY_INT_TRIGGERED(status))
2400 return IRQ_NONE;
2401
2402 writel(0xffffffff, adapter->tgt_status_reg);
2403 /* read twice to ensure write is flushed */
2404 readl(adapter->isr_int_vec);
2405 readl(adapter->isr_int_vec);
2406
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002407 return IRQ_HANDLED;
2408}
2409
2410static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2411{
2412 struct qlcnic_host_sds_ring *sds_ring = data;
2413 struct qlcnic_adapter *adapter = sds_ring->adapter;
2414
2415 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2416 goto done;
2417 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2418 writel(0xffffffff, adapter->tgt_status_reg);
2419 goto done;
2420 }
2421
2422 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2423 return IRQ_NONE;
2424
2425done:
2426 adapter->diag_cnt++;
2427 qlcnic_enable_int(sds_ring);
2428 return IRQ_HANDLED;
2429}
2430
2431static irqreturn_t qlcnic_intr(int irq, void *data)
2432{
2433 struct qlcnic_host_sds_ring *sds_ring = data;
2434 struct qlcnic_adapter *adapter = sds_ring->adapter;
2435
2436 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2437 return IRQ_NONE;
2438
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002439 napi_schedule(&sds_ring->napi);
2440
2441 return IRQ_HANDLED;
2442}
2443
2444static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2445{
2446 struct qlcnic_host_sds_ring *sds_ring = data;
2447 struct qlcnic_adapter *adapter = sds_ring->adapter;
2448
2449 /* clear interrupt */
2450 writel(0xffffffff, adapter->tgt_status_reg);
2451
2452 napi_schedule(&sds_ring->napi);
2453 return IRQ_HANDLED;
2454}
2455
2456static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2457{
2458 struct qlcnic_host_sds_ring *sds_ring = data;
2459
2460 napi_schedule(&sds_ring->napi);
2461 return IRQ_HANDLED;
2462}
2463
2464static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2465{
2466 u32 sw_consumer, hw_consumer;
2467 int count = 0, i;
2468 struct qlcnic_cmd_buffer *buffer;
2469 struct pci_dev *pdev = adapter->pdev;
2470 struct net_device *netdev = adapter->netdev;
2471 struct qlcnic_skb_frag *frag;
2472 int done;
2473 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2474
2475 if (!spin_trylock(&adapter->tx_clean_lock))
2476 return 1;
2477
2478 sw_consumer = tx_ring->sw_consumer;
2479 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2480
2481 while (sw_consumer != hw_consumer) {
2482 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2483 if (buffer->skb) {
2484 frag = &buffer->frag_array[0];
2485 pci_unmap_single(pdev, frag->dma, frag->length,
2486 PCI_DMA_TODEVICE);
2487 frag->dma = 0ULL;
2488 for (i = 1; i < buffer->frag_count; i++) {
2489 frag++;
2490 pci_unmap_page(pdev, frag->dma, frag->length,
2491 PCI_DMA_TODEVICE);
2492 frag->dma = 0ULL;
2493 }
2494
2495 adapter->stats.xmitfinished++;
2496 dev_kfree_skb_any(buffer->skb);
2497 buffer->skb = NULL;
2498 }
2499
2500 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2501 if (++count >= MAX_STATUS_HANDLE)
2502 break;
2503 }
2504
2505 if (count && netif_running(netdev)) {
2506 tx_ring->sw_consumer = sw_consumer;
2507
2508 smp_mb();
2509
2510 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002511 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2512 netif_wake_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00002513 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002514 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002515 }
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002516 adapter->tx_timeo_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002517 }
2518 /*
2519 * If everything is freed up to consumer then check if the ring is full
2520 * If the ring is full then check if more needs to be freed and
2521 * schedule the call back again.
2522 *
2523 * This happens when there are 2 CPUs. One could be freeing and the
2524 * other filling it. If the ring is full when we get out of here and
2525 * the card has already interrupted the host then the host can miss the
2526 * interrupt.
2527 *
2528 * There is still a possible race condition and the host could miss an
2529 * interrupt. The card has to take care of this.
2530 */
2531 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2532 done = (sw_consumer == hw_consumer);
2533 spin_unlock(&adapter->tx_clean_lock);
2534
2535 return done;
2536}
2537
2538static int qlcnic_poll(struct napi_struct *napi, int budget)
2539{
2540 struct qlcnic_host_sds_ring *sds_ring =
2541 container_of(napi, struct qlcnic_host_sds_ring, napi);
2542
2543 struct qlcnic_adapter *adapter = sds_ring->adapter;
2544
2545 int tx_complete;
2546 int work_done;
2547
2548 tx_complete = qlcnic_process_cmd_ring(adapter);
2549
2550 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2551
2552 if ((work_done < budget) && tx_complete) {
2553 napi_complete(&sds_ring->napi);
2554 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2555 qlcnic_enable_int(sds_ring);
2556 }
2557
2558 return work_done;
2559}
2560
schacko8f891382010-06-17 02:56:40 +00002561static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2562{
2563 struct qlcnic_host_sds_ring *sds_ring =
2564 container_of(napi, struct qlcnic_host_sds_ring, napi);
2565
2566 struct qlcnic_adapter *adapter = sds_ring->adapter;
2567 int work_done;
2568
2569 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2570
2571 if (work_done < budget) {
2572 napi_complete(&sds_ring->napi);
2573 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2574 qlcnic_enable_int(sds_ring);
2575 }
2576
2577 return work_done;
2578}
2579
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002580#ifdef CONFIG_NET_POLL_CONTROLLER
2581static void qlcnic_poll_controller(struct net_device *netdev)
2582{
Yinglin Luanbf827912010-08-22 21:57:56 +00002583 int ring;
2584 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002585 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002586 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Yinglin Luanbf827912010-08-22 21:57:56 +00002587
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002588 disable_irq(adapter->irq);
Yinglin Luanbf827912010-08-22 21:57:56 +00002589 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2590 sds_ring = &recv_ctx->sds_rings[ring];
2591 qlcnic_intr(adapter->irq, sds_ring);
2592 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002593 enable_irq(adapter->irq);
2594}
2595#endif
2596
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002597static void
2598qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2599{
2600 u32 val;
2601
2602 val = adapter->portnum & 0xf;
2603 val |= encoding << 7;
2604 val |= (jiffies - adapter->dev_rst_time) << 8;
2605
2606 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2607 adapter->dev_rst_time = jiffies;
2608}
2609
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002610static int
2611qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002612{
2613 u32 val;
2614
2615 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2616 state != QLCNIC_DEV_NEED_QUISCENT);
2617
2618 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002619 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002620
2621 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2622
2623 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002624 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002625 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002626 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002627
2628 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2629
2630 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002631
2632 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002633}
2634
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002635static int
2636qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2637{
2638 u32 val;
2639
2640 if (qlcnic_api_lock(adapter))
2641 return -EBUSY;
2642
2643 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002644 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002645 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2646
2647 qlcnic_api_unlock(adapter);
2648
2649 return 0;
2650}
2651
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002652static void
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002653qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002654{
2655 u32 val;
2656
2657 if (qlcnic_api_lock(adapter))
2658 goto err;
2659
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002660 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002661 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002662 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002663
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002664 if (failed) {
2665 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2666 dev_info(&adapter->pdev->dev,
2667 "Device state set to Failed. Please Reboot\n");
2668 } else if (!(val & 0x11111111))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002669 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2670
2671 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002672 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002673 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2674
2675 qlcnic_api_unlock(adapter);
2676err:
2677 adapter->fw_fail_cnt = 0;
2678 clear_bit(__QLCNIC_START_FW, &adapter->state);
2679 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2680}
2681
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002682/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002683static int
2684qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2685{
2686 int act, state;
2687
2688 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002689 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002690
2691 if (((state & 0x11111111) == (act & 0x11111111)) ||
2692 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2693 return 0;
2694 else
2695 return 1;
2696}
2697
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002698static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2699{
2700 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2701
2702 if (val != QLCNIC_DRV_IDC_VER) {
2703 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2704 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2705 }
2706
2707 return 0;
2708}
2709
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002710static int
2711qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2712{
2713 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002714 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002715 u8 portnum = adapter->portnum;
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002716 u8 ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002717
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002718 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2719 return 1;
2720
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002721 if (qlcnic_api_lock(adapter))
2722 return -1;
2723
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002724 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002725 if (!(val & (1 << (portnum * 4)))) {
2726 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002727 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002728 }
2729
2730 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002731 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002732
2733 switch (prev_state) {
2734 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002735 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002736 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002737 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002738 qlcnic_api_unlock(adapter);
2739 return 1;
2740
2741 case QLCNIC_DEV_READY:
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002742 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002743 qlcnic_api_unlock(adapter);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002744 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002745
2746 case QLCNIC_DEV_NEED_RESET:
2747 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002748 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002749 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2750 break;
2751
2752 case QLCNIC_DEV_NEED_QUISCENT:
2753 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002754 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002755 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2756 break;
2757
2758 case QLCNIC_DEV_FAILED:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00002759 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002760 qlcnic_api_unlock(adapter);
2761 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002762
2763 case QLCNIC_DEV_INITIALIZING:
2764 case QLCNIC_DEV_QUISCENT:
2765 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002766 }
2767
2768 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002769
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002770 do {
2771 msleep(1000);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002772 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2773
2774 if (prev_state == QLCNIC_DEV_QUISCENT)
2775 continue;
2776 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002777
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002778 if (!dev_init_timeo) {
2779 dev_err(&adapter->pdev->dev,
2780 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002781 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002782 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002783
2784 if (qlcnic_api_lock(adapter))
2785 return -1;
2786
2787 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002788 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002789 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2790
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002791 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002792 qlcnic_api_unlock(adapter);
2793
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002794 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002795}
2796
2797static void
2798qlcnic_fwinit_work(struct work_struct *work)
2799{
2800 struct qlcnic_adapter *adapter = container_of(work,
2801 struct qlcnic_adapter, fw_work.work);
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002802 u32 dev_state = 0xf;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002803
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002804 if (qlcnic_api_lock(adapter))
2805 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002806
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002807 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002808 if (dev_state == QLCNIC_DEV_QUISCENT ||
2809 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002810 qlcnic_api_unlock(adapter);
2811 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2812 FW_POLL_DELAY * 2);
2813 return;
2814 }
2815
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002816 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002817 qlcnic_api_unlock(adapter);
2818 goto wait_npar;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002819 }
2820
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002821 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2822 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2823 adapter->reset_ack_timeo);
2824 goto skip_ack_check;
2825 }
2826
2827 if (!qlcnic_check_drv_state(adapter)) {
2828skip_ack_check:
2829 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002830
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002831 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2832 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2833 QLCNIC_DEV_INITIALIZING);
2834 set_bit(__QLCNIC_START_FW, &adapter->state);
2835 QLCDB(adapter, DRV, "Restarting fw\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002836 qlcnic_idc_debug_info(adapter, 0);
Anirban Chakraborty18f2f612011-05-12 12:48:33 +00002837 QLCDB(adapter, DRV, "Take FW dump\n");
2838 qlcnic_dump_fw(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002839 }
2840
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002841 qlcnic_api_unlock(adapter);
2842
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002843 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002844 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002845 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002846 return;
2847 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002848 goto err_ret;
2849 }
2850
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002851 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002852
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002853wait_npar:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002854 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002855 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002856
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002857 switch (dev_state) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002858 case QLCNIC_DEV_READY:
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002859 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002860 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002861 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002862 return;
2863 }
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002864 case QLCNIC_DEV_FAILED:
2865 break;
2866 default:
2867 qlcnic_schedule_work(adapter,
2868 qlcnic_fwinit_work, FW_POLL_DELAY);
2869 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002870 }
2871
2872err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002873 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2874 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002875 netif_device_attach(adapter->netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002876 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002877}
2878
2879static void
2880qlcnic_detach_work(struct work_struct *work)
2881{
2882 struct qlcnic_adapter *adapter = container_of(work,
2883 struct qlcnic_adapter, fw_work.work);
2884 struct net_device *netdev = adapter->netdev;
2885 u32 status;
2886
2887 netif_device_detach(netdev);
2888
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002889 /* Dont grab rtnl lock during Quiscent mode */
2890 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2891 if (netif_running(netdev))
2892 __qlcnic_down(adapter, netdev);
2893 } else
2894 qlcnic_down(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002895
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002896 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2897
2898 if (status & QLCNIC_RCODE_FATAL_ERROR)
2899 goto err_ret;
2900
2901 if (adapter->temp == QLCNIC_TEMP_PANIC)
2902 goto err_ret;
2903
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002904 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2905 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002906
2907 adapter->fw_wait_cnt = 0;
2908
2909 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2910
2911 return;
2912
2913err_ret:
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002914 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2915 status, adapter->temp);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002916 netif_device_attach(netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002917 qlcnic_clr_all_drv_state(adapter, 1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002918}
2919
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002920/*Transit NPAR state to NON Operational */
2921static void
2922qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2923{
2924 u32 state;
2925
2926 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2927 if (state == QLCNIC_DEV_NPAR_NON_OPER)
2928 return;
2929
2930 if (qlcnic_api_lock(adapter))
2931 return;
2932 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2933 qlcnic_api_unlock(adapter);
2934}
2935
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002936/*Transit to RESET state from READY state only */
Anirban Chakraborty18f2f612011-05-12 12:48:33 +00002937void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002938qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2939{
2940 u32 state;
2941
Anirban Chakrabortycea89752010-07-13 20:33:35 +00002942 adapter->need_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002943 if (qlcnic_api_lock(adapter))
2944 return;
2945
2946 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2947
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002948 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002949 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002950 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002951 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002952 }
2953
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002954 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002955 qlcnic_api_unlock(adapter);
2956}
2957
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002958/* Transit to NPAR READY state from NPAR NOT READY state */
2959static void
2960qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2961{
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002962 if (qlcnic_api_lock(adapter))
2963 return;
2964
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002965 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
2966 QLCDB(adapter, DRV, "NPAR operational state set\n");
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002967
2968 qlcnic_api_unlock(adapter);
2969}
2970
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002971static void
2972qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2973 work_func_t func, int delay)
2974{
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00002975 if (test_bit(__QLCNIC_AER, &adapter->state))
2976 return;
2977
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002978 INIT_DELAYED_WORK(&adapter->fw_work, func);
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00002979 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
2980 round_jiffies_relative(delay));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002981}
2982
2983static void
2984qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2985{
2986 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2987 msleep(10);
2988
2989 cancel_delayed_work_sync(&adapter->fw_work);
2990}
2991
2992static void
2993qlcnic_attach_work(struct work_struct *work)
2994{
2995 struct qlcnic_adapter *adapter = container_of(work,
2996 struct qlcnic_adapter, fw_work.work);
2997 struct net_device *netdev = adapter->netdev;
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002998 u32 npar_state;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002999
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00003000 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
3001 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3002 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3003 qlcnic_clr_all_drv_state(adapter, 0);
3004 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3005 qlcnic_schedule_work(adapter, qlcnic_attach_work,
3006 FW_POLL_DELAY);
3007 else
3008 goto attach;
3009 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3010 return;
3011 }
3012attach:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003013 if (netif_running(netdev)) {
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00003014 if (qlcnic_up(adapter, netdev))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003015 goto done;
3016
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003017 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003018 }
3019
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003020done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00003021 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003022 adapter->fw_fail_cnt = 0;
3023 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00003024
3025 if (!qlcnic_clr_drv_state(adapter))
3026 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3027 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003028}
3029
3030static int
3031qlcnic_check_health(struct qlcnic_adapter *adapter)
3032{
Sony Chacko4e708122010-08-31 17:17:44 +00003033 u32 state = 0, heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003034 struct net_device *netdev = adapter->netdev;
3035
3036 if (qlcnic_check_temp(adapter))
3037 goto detach;
3038
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00003039 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003040 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003041
3042 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00003043 if (state == QLCNIC_DEV_NEED_RESET) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003044 qlcnic_set_npar_non_operational(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003045 adapter->need_fw_reset = 1;
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00003046 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3047 goto detach;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003048
Sony Chacko4e708122010-08-31 17:17:44 +00003049 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3050 if (heartbeat != adapter->heartbeat) {
3051 adapter->heartbeat = heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003052 adapter->fw_fail_cnt = 0;
3053 if (adapter->need_fw_reset)
3054 goto detach;
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003055
amit salecha9ce13ca2011-02-23 03:21:24 +00003056 if (adapter->reset_context && auto_fw_reset) {
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003057 qlcnic_reset_hw_context(adapter);
3058 adapter->netdev->trans_start = jiffies;
3059 }
3060
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003061 return 0;
3062 }
3063
3064 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3065 return 0;
3066
3067 qlcnic_dev_request_reset(adapter);
3068
amit salecha9ce13ca2011-02-23 03:21:24 +00003069 if (auto_fw_reset)
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00003070 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003071
3072 dev_info(&netdev->dev, "firmware hang detected\n");
3073
3074detach:
3075 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3076 QLCNIC_DEV_NEED_RESET;
3077
amit salecha9ce13ca2011-02-23 03:21:24 +00003078 if (auto_fw_reset &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003079 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3080
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003081 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003082 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3083 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003084
3085 return 1;
3086}
3087
3088static void
3089qlcnic_fw_poll_work(struct work_struct *work)
3090{
3091 struct qlcnic_adapter *adapter = container_of(work,
3092 struct qlcnic_adapter, fw_work.work);
3093
3094 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3095 goto reschedule;
3096
3097
3098 if (qlcnic_check_health(adapter))
3099 return;
3100
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00003101 if (adapter->fhash.fnum)
3102 qlcnic_prune_lb_filters(adapter);
3103
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003104reschedule:
3105 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3106}
3107
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003108static int qlcnic_is_first_func(struct pci_dev *pdev)
3109{
3110 struct pci_dev *oth_pdev;
3111 int val = pdev->devfn;
3112
3113 while (val-- > 0) {
3114 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3115 (pdev->bus), pdev->bus->number,
3116 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003117 if (!oth_pdev)
3118 continue;
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003119
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003120 if (oth_pdev->current_state != PCI_D3cold) {
3121 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003122 return 0;
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003123 }
3124 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003125 }
3126 return 1;
3127}
3128
3129static int qlcnic_attach_func(struct pci_dev *pdev)
3130{
3131 int err, first_func;
3132 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3133 struct net_device *netdev = adapter->netdev;
3134
3135 pdev->error_state = pci_channel_io_normal;
3136
3137 err = pci_enable_device(pdev);
3138 if (err)
3139 return err;
3140
3141 pci_set_power_state(pdev, PCI_D0);
3142 pci_set_master(pdev);
3143 pci_restore_state(pdev);
3144
3145 first_func = qlcnic_is_first_func(pdev);
3146
3147 if (qlcnic_api_lock(adapter))
3148 return -EINVAL;
3149
Amit Kumar Salecha933fce122010-08-17 00:34:19 +00003150 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003151 adapter->need_fw_reset = 1;
3152 set_bit(__QLCNIC_START_FW, &adapter->state);
3153 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3154 QLCDB(adapter, DRV, "Restarting fw\n");
3155 }
3156 qlcnic_api_unlock(adapter);
3157
3158 err = adapter->nic_ops->start_firmware(adapter);
3159 if (err)
3160 return err;
3161
3162 qlcnic_clr_drv_state(adapter);
3163 qlcnic_setup_intr(adapter);
3164
3165 if (netif_running(netdev)) {
3166 err = qlcnic_attach(adapter);
3167 if (err) {
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00003168 qlcnic_clr_all_drv_state(adapter, 1);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003169 clear_bit(__QLCNIC_AER, &adapter->state);
3170 netif_device_attach(netdev);
3171 return err;
3172 }
3173
3174 err = qlcnic_up(adapter, netdev);
3175 if (err)
3176 goto done;
3177
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003178 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003179 }
3180 done:
3181 netif_device_attach(netdev);
3182 return err;
3183}
3184
3185static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3186 pci_channel_state_t state)
3187{
3188 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3189 struct net_device *netdev = adapter->netdev;
3190
3191 if (state == pci_channel_io_perm_failure)
3192 return PCI_ERS_RESULT_DISCONNECT;
3193
3194 if (state == pci_channel_io_normal)
3195 return PCI_ERS_RESULT_RECOVERED;
3196
3197 set_bit(__QLCNIC_AER, &adapter->state);
3198 netif_device_detach(netdev);
3199
3200 cancel_delayed_work_sync(&adapter->fw_work);
3201
3202 if (netif_running(netdev))
3203 qlcnic_down(adapter, netdev);
3204
3205 qlcnic_detach(adapter);
3206 qlcnic_teardown_intr(adapter);
3207
3208 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3209
3210 pci_save_state(pdev);
3211 pci_disable_device(pdev);
3212
3213 return PCI_ERS_RESULT_NEED_RESET;
3214}
3215
3216static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3217{
3218 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3219 PCI_ERS_RESULT_RECOVERED;
3220}
3221
3222static void qlcnic_io_resume(struct pci_dev *pdev)
3223{
3224 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3225
3226 pci_cleanup_aer_uncorrect_error_status(pdev);
3227
3228 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3229 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3230 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3231 FW_POLL_DELAY);
3232}
3233
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003234static int
3235qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3236{
3237 int err;
3238
3239 err = qlcnic_can_start_firmware(adapter);
3240 if (err)
3241 return err;
3242
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +00003243 err = qlcnic_check_npar_opertional(adapter);
3244 if (err)
3245 return err;
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003246
Rajesh Borundia174240a2010-08-31 17:17:47 +00003247 err = qlcnic_initialize_nic(adapter);
3248 if (err)
3249 return err;
3250
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003251 qlcnic_check_options(adapter);
3252
Rajesh Borundia73733732010-08-31 17:17:50 +00003253 err = qlcnic_set_eswitch_port_config(adapter);
3254 if (err)
3255 return err;
3256
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003257 adapter->need_fw_reset = 0;
3258
3259 return err;
3260}
3261
3262static int
3263qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3264{
3265 return -EOPNOTSUPP;
3266}
3267
3268static int
3269qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3270{
3271 return -EOPNOTSUPP;
3272}
3273
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003274static ssize_t
3275qlcnic_store_bridged_mode(struct device *dev,
3276 struct device_attribute *attr, const char *buf, size_t len)
3277{
3278 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3279 unsigned long new;
3280 int ret = -EINVAL;
3281
3282 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3283 goto err_out;
3284
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00003285 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003286 goto err_out;
3287
3288 if (strict_strtoul(buf, 2, &new))
3289 goto err_out;
3290
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00003291 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003292 ret = len;
3293
3294err_out:
3295 return ret;
3296}
3297
3298static ssize_t
3299qlcnic_show_bridged_mode(struct device *dev,
3300 struct device_attribute *attr, char *buf)
3301{
3302 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3303 int bridged_mode = 0;
3304
3305 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3306 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3307
3308 return sprintf(buf, "%d\n", bridged_mode);
3309}
3310
3311static struct device_attribute dev_attr_bridged_mode = {
3312 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3313 .show = qlcnic_show_bridged_mode,
3314 .store = qlcnic_store_bridged_mode,
3315};
3316
3317static ssize_t
3318qlcnic_store_diag_mode(struct device *dev,
3319 struct device_attribute *attr, const char *buf, size_t len)
3320{
3321 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3322 unsigned long new;
3323
3324 if (strict_strtoul(buf, 2, &new))
3325 return -EINVAL;
3326
3327 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3328 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3329
3330 return len;
3331}
3332
3333static ssize_t
3334qlcnic_show_diag_mode(struct device *dev,
3335 struct device_attribute *attr, char *buf)
3336{
3337 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3338
3339 return sprintf(buf, "%d\n",
3340 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3341}
3342
3343static struct device_attribute dev_attr_diag_mode = {
3344 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3345 .show = qlcnic_show_diag_mode,
3346 .store = qlcnic_store_diag_mode,
3347};
3348
Sucheta Chakrabortyf94bc1e2011-04-28 11:48:18 +00003349int qlcnic_validate_max_rss(struct net_device *netdev, u8 max_hw, u8 val)
3350{
3351 if (!use_msi_x && !use_msi) {
3352 netdev_info(netdev, "no msix or msi support, hence no rss\n");
3353 return -EINVAL;
3354 }
3355
3356 if ((val > max_hw) || (val < 2) || !is_power_of_2(val)) {
3357 netdev_info(netdev, "rss_ring valid range [2 - %x] in "
3358 " powers of 2\n", max_hw);
3359 return -EINVAL;
3360 }
3361 return 0;
3362
3363}
3364
3365int qlcnic_set_max_rss(struct qlcnic_adapter *adapter, u8 data)
3366{
3367 struct net_device *netdev = adapter->netdev;
3368 int err = 0;
3369
3370 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3371 return -EBUSY;
3372
3373 netif_device_detach(netdev);
3374 if (netif_running(netdev))
3375 __qlcnic_down(adapter, netdev);
3376 qlcnic_detach(adapter);
3377 qlcnic_teardown_intr(adapter);
3378
3379 if (qlcnic_enable_msix(adapter, data)) {
3380 netdev_info(netdev, "failed setting max_rss; rss disabled\n");
3381 qlcnic_enable_msi_legacy(adapter);
3382 }
3383
3384 if (netif_running(netdev)) {
3385 err = qlcnic_attach(adapter);
3386 if (err)
3387 goto done;
3388 err = __qlcnic_up(adapter, netdev);
3389 if (err)
3390 goto done;
3391 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3392 }
3393 done:
3394 netif_device_attach(netdev);
3395 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3396 return err;
3397}
3398
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003399static int
3400qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3401 loff_t offset, size_t size)
3402{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003403 size_t crb_size = 4;
3404
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003405 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3406 return -EIO;
3407
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003408 if (offset < QLCNIC_PCI_CRBSPACE) {
3409 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3410 QLCNIC_PCI_CAMQM_END))
3411 crb_size = 8;
3412 else
3413 return -EINVAL;
3414 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003415
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003416 if ((size != crb_size) || (offset & (crb_size-1)))
3417 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003418
3419 return 0;
3420}
3421
3422static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003423qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3424 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003425 char *buf, loff_t offset, size_t size)
3426{
3427 struct device *dev = container_of(kobj, struct device, kobj);
3428 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3429 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003430 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003431 int ret;
3432
3433 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3434 if (ret != 0)
3435 return ret;
3436
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003437 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3438 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3439 memcpy(buf, &qmdata, size);
3440 } else {
3441 data = QLCRD32(adapter, offset);
3442 memcpy(buf, &data, size);
3443 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003444 return size;
3445}
3446
3447static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003448qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3449 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003450 char *buf, loff_t offset, size_t size)
3451{
3452 struct device *dev = container_of(kobj, struct device, kobj);
3453 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3454 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003455 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003456 int ret;
3457
3458 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3459 if (ret != 0)
3460 return ret;
3461
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003462 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3463 memcpy(&qmdata, buf, size);
3464 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3465 } else {
3466 memcpy(&data, buf, size);
3467 QLCWR32(adapter, offset, data);
3468 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003469 return size;
3470}
3471
3472static int
3473qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3474 loff_t offset, size_t size)
3475{
3476 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3477 return -EIO;
3478
3479 if ((size != 8) || (offset & 0x7))
3480 return -EIO;
3481
3482 return 0;
3483}
3484
3485static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003486qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3487 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003488 char *buf, loff_t offset, size_t size)
3489{
3490 struct device *dev = container_of(kobj, struct device, kobj);
3491 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3492 u64 data;
3493 int ret;
3494
3495 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3496 if (ret != 0)
3497 return ret;
3498
3499 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3500 return -EIO;
3501
3502 memcpy(buf, &data, size);
3503
3504 return size;
3505}
3506
3507static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003508qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3509 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003510 char *buf, loff_t offset, size_t size)
3511{
3512 struct device *dev = container_of(kobj, struct device, kobj);
3513 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3514 u64 data;
3515 int ret;
3516
3517 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3518 if (ret != 0)
3519 return ret;
3520
3521 memcpy(&data, buf, size);
3522
3523 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3524 return -EIO;
3525
3526 return size;
3527}
3528
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003529static struct bin_attribute bin_attr_crb = {
3530 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3531 .size = 0,
3532 .read = qlcnic_sysfs_read_crb,
3533 .write = qlcnic_sysfs_write_crb,
3534};
3535
3536static struct bin_attribute bin_attr_mem = {
3537 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3538 .size = 0,
3539 .read = qlcnic_sysfs_read_mem,
3540 .write = qlcnic_sysfs_write_mem,
3541};
3542
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003543static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003544validate_pm_config(struct qlcnic_adapter *adapter,
3545 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3546{
3547
3548 u8 src_pci_func, s_esw_id, d_esw_id;
3549 u8 dest_pci_func;
3550 int i;
3551
3552 for (i = 0; i < count; i++) {
3553 src_pci_func = pm_cfg[i].pci_func;
3554 dest_pci_func = pm_cfg[i].dest_npar;
3555 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3556 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3557 return QL_STATUS_INVALID_PARAM;
3558
3559 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3560 return QL_STATUS_INVALID_PARAM;
3561
3562 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3563 return QL_STATUS_INVALID_PARAM;
3564
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003565 s_esw_id = adapter->npars[src_pci_func].phy_port;
3566 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3567
3568 if (s_esw_id != d_esw_id)
3569 return QL_STATUS_INVALID_PARAM;
3570
3571 }
3572 return 0;
3573
3574}
3575
3576static ssize_t
3577qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3578 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3579{
3580 struct device *dev = container_of(kobj, struct device, kobj);
3581 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3582 struct qlcnic_pm_func_cfg *pm_cfg;
3583 u32 id, action, pci_func;
3584 int count, rem, i, ret;
3585
3586 count = size / sizeof(struct qlcnic_pm_func_cfg);
3587 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3588 if (rem)
3589 return QL_STATUS_INVALID_PARAM;
3590
3591 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3592
3593 ret = validate_pm_config(adapter, pm_cfg, count);
3594 if (ret)
3595 return ret;
3596 for (i = 0; i < count; i++) {
3597 pci_func = pm_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003598 action = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003599 id = adapter->npars[pci_func].phy_port;
3600 ret = qlcnic_config_port_mirroring(adapter, id,
3601 action, pci_func);
3602 if (ret)
3603 return ret;
3604 }
3605
3606 for (i = 0; i < count; i++) {
3607 pci_func = pm_cfg[i].pci_func;
3608 id = adapter->npars[pci_func].phy_port;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003609 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003610 adapter->npars[pci_func].dest_npar = id;
3611 }
3612 return size;
3613}
3614
3615static ssize_t
3616qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3617 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3618{
3619 struct device *dev = container_of(kobj, struct device, kobj);
3620 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3621 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3622 int i;
3623
3624 if (size != sizeof(pm_cfg))
3625 return QL_STATUS_INVALID_PARAM;
3626
3627 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3628 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3629 continue;
3630 pm_cfg[i].action = adapter->npars[i].enable_pm;
3631 pm_cfg[i].dest_npar = 0;
3632 pm_cfg[i].pci_func = i;
3633 }
3634 memcpy(buf, &pm_cfg, size);
3635
3636 return size;
3637}
3638
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003639static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003640validate_esw_config(struct qlcnic_adapter *adapter,
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003641 struct qlcnic_esw_func_cfg *esw_cfg, int count)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003642{
Rajesh Borundia7613c872010-08-31 17:17:48 +00003643 u32 op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003644 u8 pci_func;
3645 int i;
Rajesh Borundia7613c872010-08-31 17:17:48 +00003646
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003647 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
Rajesh Borundia7613c872010-08-31 17:17:48 +00003648
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003649 for (i = 0; i < count; i++) {
3650 pci_func = esw_cfg[i].pci_func;
3651 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3652 return QL_STATUS_INVALID_PARAM;
3653
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003654 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3655 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003656 return QL_STATUS_INVALID_PARAM;
3657
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003658 switch (esw_cfg[i].op_mode) {
3659 case QLCNIC_PORT_DEFAULTS:
Rajesh Borundia7613c872010-08-31 17:17:48 +00003660 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
Rajesh Borundia73733732010-08-31 17:17:50 +00003661 QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha091056b2010-12-02 20:41:43 +00003662 if (esw_cfg[i].mac_anti_spoof != 0)
3663 return QL_STATUS_INVALID_PARAM;
3664 if (esw_cfg[i].mac_override != 1)
3665 return QL_STATUS_INVALID_PARAM;
3666 if (esw_cfg[i].promisc_mode != 1)
3667 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia73733732010-08-31 17:17:50 +00003668 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003669 break;
3670 case QLCNIC_ADD_VLAN:
3671 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3672 return QL_STATUS_INVALID_PARAM;
3673 if (!esw_cfg[i].op_type)
3674 return QL_STATUS_INVALID_PARAM;
3675 break;
3676 case QLCNIC_DEL_VLAN:
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003677 if (!esw_cfg[i].op_type)
3678 return QL_STATUS_INVALID_PARAM;
3679 break;
3680 default:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003681 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003682 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003683 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003684 return 0;
3685}
3686
3687static ssize_t
3688qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3689 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3690{
3691 struct device *dev = container_of(kobj, struct device, kobj);
3692 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3693 struct qlcnic_esw_func_cfg *esw_cfg;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003694 struct qlcnic_npar_info *npar;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003695 int count, rem, i, ret;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003696 u8 pci_func, op_mode = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003697
3698 count = size / sizeof(struct qlcnic_esw_func_cfg);
3699 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3700 if (rem)
3701 return QL_STATUS_INVALID_PARAM;
3702
3703 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3704 ret = validate_esw_config(adapter, esw_cfg, count);
3705 if (ret)
3706 return ret;
3707
3708 for (i = 0; i < count; i++) {
Rajesh Borundia0325d692010-08-19 05:08:26 +00003709 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3710 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3711 return QL_STATUS_INVALID_PARAM;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003712
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003713 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003714 continue;
3715
3716 op_mode = esw_cfg[i].op_mode;
3717 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3718 esw_cfg[i].op_mode = op_mode;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003719 esw_cfg[i].pci_func = adapter->ahw->pci_func;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003720
3721 switch (esw_cfg[i].op_mode) {
3722 case QLCNIC_PORT_DEFAULTS:
3723 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3724 break;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00003725 case QLCNIC_ADD_VLAN:
3726 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3727 break;
3728 case QLCNIC_DEL_VLAN:
3729 esw_cfg[i].vlan_id = 0;
3730 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3731 break;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003732 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003733 }
3734
Rajesh Borundia0325d692010-08-19 05:08:26 +00003735 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3736 goto out;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003737
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003738 for (i = 0; i < count; i++) {
3739 pci_func = esw_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003740 npar = &adapter->npars[pci_func];
3741 switch (esw_cfg[i].op_mode) {
3742 case QLCNIC_PORT_DEFAULTS:
3743 npar->promisc_mode = esw_cfg[i].promisc_mode;
Rajesh Borundia73733732010-08-31 17:17:50 +00003744 npar->mac_override = esw_cfg[i].mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003745 npar->offload_flags = esw_cfg[i].offload_flags;
3746 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3747 npar->discard_tagged = esw_cfg[i].discard_tagged;
3748 break;
3749 case QLCNIC_ADD_VLAN:
3750 npar->pvid = esw_cfg[i].vlan_id;
3751 break;
3752 case QLCNIC_DEL_VLAN:
3753 npar->pvid = 0;
3754 break;
3755 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003756 }
Rajesh Borundia0325d692010-08-19 05:08:26 +00003757out:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003758 return size;
3759}
3760
3761static ssize_t
3762qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3763 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3764{
3765 struct device *dev = container_of(kobj, struct device, kobj);
3766 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3767 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003768 u8 i;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003769
3770 if (size != sizeof(esw_cfg))
3771 return QL_STATUS_INVALID_PARAM;
3772
3773 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3774 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3775 continue;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003776 esw_cfg[i].pci_func = i;
3777 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
3778 return QL_STATUS_INVALID_PARAM;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003779 }
3780 memcpy(buf, &esw_cfg, size);
3781
3782 return size;
3783}
3784
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003785static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003786validate_npar_config(struct qlcnic_adapter *adapter,
3787 struct qlcnic_npar_func_cfg *np_cfg, int count)
3788{
3789 u8 pci_func, i;
3790
3791 for (i = 0; i < count; i++) {
3792 pci_func = np_cfg[i].pci_func;
3793 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3794 return QL_STATUS_INVALID_PARAM;
3795
3796 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3797 return QL_STATUS_INVALID_PARAM;
3798
Rajesh Borundiad12b0d92011-02-23 03:21:25 +00003799 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
3800 !IS_VALID_BW(np_cfg[i].max_bw))
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003801 return QL_STATUS_INVALID_PARAM;
3802 }
3803 return 0;
3804}
3805
3806static ssize_t
3807qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3808 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3809{
3810 struct device *dev = container_of(kobj, struct device, kobj);
3811 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3812 struct qlcnic_info nic_info;
3813 struct qlcnic_npar_func_cfg *np_cfg;
3814 int i, count, rem, ret;
3815 u8 pci_func;
3816
3817 count = size / sizeof(struct qlcnic_npar_func_cfg);
3818 rem = size % sizeof(struct qlcnic_npar_func_cfg);
3819 if (rem)
3820 return QL_STATUS_INVALID_PARAM;
3821
3822 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
3823 ret = validate_npar_config(adapter, np_cfg, count);
3824 if (ret)
3825 return ret;
3826
3827 for (i = 0; i < count ; i++) {
3828 pci_func = np_cfg[i].pci_func;
3829 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
3830 if (ret)
3831 return ret;
3832 nic_info.pci_func = pci_func;
3833 nic_info.min_tx_bw = np_cfg[i].min_bw;
3834 nic_info.max_tx_bw = np_cfg[i].max_bw;
3835 ret = qlcnic_set_nic_info(adapter, &nic_info);
3836 if (ret)
3837 return ret;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003838 adapter->npars[i].min_bw = nic_info.min_tx_bw;
3839 adapter->npars[i].max_bw = nic_info.max_tx_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003840 }
3841
3842 return size;
3843
3844}
3845static ssize_t
3846qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
3847 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3848{
3849 struct device *dev = container_of(kobj, struct device, kobj);
3850 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3851 struct qlcnic_info nic_info;
3852 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
3853 int i, ret;
3854
3855 if (size != sizeof(np_cfg))
3856 return QL_STATUS_INVALID_PARAM;
3857
3858 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3859 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3860 continue;
3861 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
3862 if (ret)
3863 return ret;
3864
3865 np_cfg[i].pci_func = i;
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +00003866 np_cfg[i].op_mode = (u8)nic_info.op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003867 np_cfg[i].port_num = nic_info.phys_port;
3868 np_cfg[i].fw_capab = nic_info.capabilities;
3869 np_cfg[i].min_bw = nic_info.min_tx_bw ;
3870 np_cfg[i].max_bw = nic_info.max_tx_bw;
3871 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
3872 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
3873 }
3874 memcpy(buf, &np_cfg, size);
3875 return size;
3876}
3877
3878static ssize_t
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003879qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
3880 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3881{
3882 struct device *dev = container_of(kobj, struct device, kobj);
3883 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3884 struct qlcnic_esw_statistics port_stats;
3885 int ret;
3886
3887 if (size != sizeof(struct qlcnic_esw_statistics))
3888 return QL_STATUS_INVALID_PARAM;
3889
3890 if (offset >= QLCNIC_MAX_PCI_FUNC)
3891 return QL_STATUS_INVALID_PARAM;
3892
3893 memset(&port_stats, 0, size);
3894 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3895 &port_stats.rx);
3896 if (ret)
3897 return ret;
3898
3899 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3900 &port_stats.tx);
3901 if (ret)
3902 return ret;
3903
3904 memcpy(buf, &port_stats, size);
3905 return size;
3906}
3907
3908static ssize_t
3909qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
3910 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3911{
3912 struct device *dev = container_of(kobj, struct device, kobj);
3913 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3914 struct qlcnic_esw_statistics esw_stats;
3915 int ret;
3916
3917 if (size != sizeof(struct qlcnic_esw_statistics))
3918 return QL_STATUS_INVALID_PARAM;
3919
3920 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3921 return QL_STATUS_INVALID_PARAM;
3922
3923 memset(&esw_stats, 0, size);
3924 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3925 &esw_stats.rx);
3926 if (ret)
3927 return ret;
3928
3929 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3930 &esw_stats.tx);
3931 if (ret)
3932 return ret;
3933
3934 memcpy(buf, &esw_stats, size);
3935 return size;
3936}
3937
3938static ssize_t
3939qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
3940 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3941{
3942 struct device *dev = container_of(kobj, struct device, kobj);
3943 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3944 int ret;
3945
3946 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3947 return QL_STATUS_INVALID_PARAM;
3948
3949 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3950 QLCNIC_QUERY_RX_COUNTER);
3951 if (ret)
3952 return ret;
3953
3954 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3955 QLCNIC_QUERY_TX_COUNTER);
3956 if (ret)
3957 return ret;
3958
3959 return size;
3960}
3961
3962static ssize_t
3963qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
3964 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3965{
3966
3967 struct device *dev = container_of(kobj, struct device, kobj);
3968 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3969 int ret;
3970
3971 if (offset >= QLCNIC_MAX_PCI_FUNC)
3972 return QL_STATUS_INVALID_PARAM;
3973
3974 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
3975 QLCNIC_QUERY_RX_COUNTER);
3976 if (ret)
3977 return ret;
3978
3979 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
3980 QLCNIC_QUERY_TX_COUNTER);
3981 if (ret)
3982 return ret;
3983
3984 return size;
3985}
3986
3987static ssize_t
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003988qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
3989 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3990{
3991 struct device *dev = container_of(kobj, struct device, kobj);
3992 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3993 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
Dan Carpentere88db3b2010-08-09 21:49:36 +00003994 struct qlcnic_pci_info *pci_info;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003995 int i, ret;
3996
3997 if (size != sizeof(pci_cfg))
3998 return QL_STATUS_INVALID_PARAM;
3999
Dan Carpentere88db3b2010-08-09 21:49:36 +00004000 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
4001 if (!pci_info)
4002 return -ENOMEM;
4003
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004004 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpentere88db3b2010-08-09 21:49:36 +00004005 if (ret) {
4006 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004007 return ret;
Dan Carpentere88db3b2010-08-09 21:49:36 +00004008 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004009
4010 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4011 pci_cfg[i].pci_func = pci_info[i].id;
4012 pci_cfg[i].func_type = pci_info[i].type;
4013 pci_cfg[i].port_num = pci_info[i].default_port;
4014 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
4015 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
4016 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
4017 }
4018 memcpy(buf, &pci_cfg, size);
Dan Carpentere88db3b2010-08-09 21:49:36 +00004019 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004020 return size;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004021}
4022static struct bin_attribute bin_attr_npar_config = {
4023 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
4024 .size = 0,
4025 .read = qlcnic_sysfs_read_npar_config,
4026 .write = qlcnic_sysfs_write_npar_config,
4027};
4028
4029static struct bin_attribute bin_attr_pci_config = {
4030 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
4031 .size = 0,
4032 .read = qlcnic_sysfs_read_pci_config,
4033 .write = NULL,
4034};
4035
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004036static struct bin_attribute bin_attr_port_stats = {
4037 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
4038 .size = 0,
4039 .read = qlcnic_sysfs_get_port_stats,
4040 .write = qlcnic_sysfs_clear_port_stats,
4041};
4042
4043static struct bin_attribute bin_attr_esw_stats = {
4044 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
4045 .size = 0,
4046 .read = qlcnic_sysfs_get_esw_stats,
4047 .write = qlcnic_sysfs_clear_esw_stats,
4048};
4049
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004050static struct bin_attribute bin_attr_esw_config = {
4051 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
4052 .size = 0,
4053 .read = qlcnic_sysfs_read_esw_config,
4054 .write = qlcnic_sysfs_write_esw_config,
4055};
4056
4057static struct bin_attribute bin_attr_pm_config = {
4058 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
4059 .size = 0,
4060 .read = qlcnic_sysfs_read_pm_config,
4061 .write = qlcnic_sysfs_write_pm_config,
4062};
4063
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004064static void
4065qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
4066{
4067 struct device *dev = &adapter->pdev->dev;
4068
4069 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4070 if (device_create_file(dev, &dev_attr_bridged_mode))
4071 dev_warn(dev,
4072 "failed to create bridged_mode sysfs entry\n");
4073}
4074
4075static void
4076qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
4077{
4078 struct device *dev = &adapter->pdev->dev;
4079
4080 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4081 device_remove_file(dev, &dev_attr_bridged_mode);
4082}
4083
4084static void
4085qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
4086{
4087 struct device *dev = &adapter->pdev->dev;
4088
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004089 if (device_create_bin_file(dev, &bin_attr_port_stats))
4090 dev_info(dev, "failed to create port stats sysfs entry");
4091
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004092 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4093 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004094 if (device_create_file(dev, &dev_attr_diag_mode))
4095 dev_info(dev, "failed to create diag_mode sysfs entry\n");
4096 if (device_create_bin_file(dev, &bin_attr_crb))
4097 dev_info(dev, "failed to create crb sysfs entry\n");
4098 if (device_create_bin_file(dev, &bin_attr_mem))
4099 dev_info(dev, "failed to create mem sysfs entry\n");
Sony Chacko53478fe2011-04-01 14:27:47 +00004100 if (device_create_bin_file(dev, &bin_attr_pci_config))
4101 dev_info(dev, "failed to create pci config sysfs entry");
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004102 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4103 return;
4104 if (device_create_bin_file(dev, &bin_attr_esw_config))
4105 dev_info(dev, "failed to create esw config sysfs entry");
4106 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004107 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004108 if (device_create_bin_file(dev, &bin_attr_npar_config))
4109 dev_info(dev, "failed to create npar config sysfs entry");
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004110 if (device_create_bin_file(dev, &bin_attr_pm_config))
4111 dev_info(dev, "failed to create pm config sysfs entry");
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004112 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4113 dev_info(dev, "failed to create eswitch stats sysfs entry");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004114}
4115
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004116static void
4117qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4118{
4119 struct device *dev = &adapter->pdev->dev;
4120
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004121 device_remove_bin_file(dev, &bin_attr_port_stats);
4122
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004123 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4124 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004125 device_remove_file(dev, &dev_attr_diag_mode);
4126 device_remove_bin_file(dev, &bin_attr_crb);
4127 device_remove_bin_file(dev, &bin_attr_mem);
Sony Chacko53478fe2011-04-01 14:27:47 +00004128 device_remove_bin_file(dev, &bin_attr_pci_config);
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004129 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4130 return;
4131 device_remove_bin_file(dev, &bin_attr_esw_config);
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 device_remove_bin_file(dev, &bin_attr_npar_config);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004135 device_remove_bin_file(dev, &bin_attr_pm_config);
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004136 device_remove_bin_file(dev, &bin_attr_esw_stats);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004137}
4138
4139#ifdef CONFIG_INET
4140
4141#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4142
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004143static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004144qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4145 struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004146{
4147 struct in_device *indev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004148
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004149 indev = in_dev_get(dev);
4150 if (!indev)
4151 return;
4152
4153 for_ifa(indev) {
4154 switch (event) {
4155 case NETDEV_UP:
4156 qlcnic_config_ipaddr(adapter,
4157 ifa->ifa_address, QLCNIC_IP_UP);
4158 break;
4159 case NETDEV_DOWN:
4160 qlcnic_config_ipaddr(adapter,
4161 ifa->ifa_address, QLCNIC_IP_DOWN);
4162 break;
4163 default:
4164 break;
4165 }
4166 } endfor_ifa(indev);
4167
4168 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004169}
4170
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004171static void
4172qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4173{
4174 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4175 struct net_device *dev;
4176 u16 vid;
4177
4178 qlcnic_config_indev_addr(adapter, netdev, event);
4179
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +00004180 for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4181 dev = vlan_find_dev(netdev, vid);
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004182 if (!dev)
4183 continue;
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004184 qlcnic_config_indev_addr(adapter, dev, event);
4185 }
4186}
4187
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004188static int qlcnic_netdev_event(struct notifier_block *this,
4189 unsigned long event, void *ptr)
4190{
4191 struct qlcnic_adapter *adapter;
4192 struct net_device *dev = (struct net_device *)ptr;
4193
4194recheck:
4195 if (dev == NULL)
4196 goto done;
4197
4198 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4199 dev = vlan_dev_real_dev(dev);
4200 goto recheck;
4201 }
4202
4203 if (!is_qlcnic_netdev(dev))
4204 goto done;
4205
4206 adapter = netdev_priv(dev);
4207
4208 if (!adapter)
4209 goto done;
4210
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004211 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004212 goto done;
4213
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004214 qlcnic_config_indev_addr(adapter, dev, event);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004215done:
4216 return NOTIFY_DONE;
4217}
4218
4219static int
4220qlcnic_inetaddr_event(struct notifier_block *this,
4221 unsigned long event, void *ptr)
4222{
4223 struct qlcnic_adapter *adapter;
4224 struct net_device *dev;
4225
4226 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4227
4228 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4229
4230recheck:
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004231 if (dev == NULL)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004232 goto done;
4233
4234 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4235 dev = vlan_dev_real_dev(dev);
4236 goto recheck;
4237 }
4238
4239 if (!is_qlcnic_netdev(dev))
4240 goto done;
4241
4242 adapter = netdev_priv(dev);
4243
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00004244 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004245 goto done;
4246
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004247 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004248 goto done;
4249
4250 switch (event) {
4251 case NETDEV_UP:
4252 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4253 break;
4254 case NETDEV_DOWN:
4255 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4256 break;
4257 default:
4258 break;
4259 }
4260
4261done:
4262 return NOTIFY_DONE;
4263}
4264
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004265static struct notifier_block qlcnic_netdev_cb = {
4266 .notifier_call = qlcnic_netdev_event,
4267};
4268
4269static struct notifier_block qlcnic_inetaddr_cb = {
4270 .notifier_call = qlcnic_inetaddr_event,
4271};
4272#else
4273static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004274qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004275{ }
4276#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004277static struct pci_error_handlers qlcnic_err_handler = {
4278 .error_detected = qlcnic_io_error_detected,
4279 .slot_reset = qlcnic_io_slot_reset,
4280 .resume = qlcnic_io_resume,
4281};
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004282
4283static struct pci_driver qlcnic_driver = {
4284 .name = qlcnic_driver_name,
4285 .id_table = qlcnic_pci_tbl,
4286 .probe = qlcnic_probe,
4287 .remove = __devexit_p(qlcnic_remove),
4288#ifdef CONFIG_PM
4289 .suspend = qlcnic_suspend,
4290 .resume = qlcnic_resume,
4291#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004292 .shutdown = qlcnic_shutdown,
4293 .err_handler = &qlcnic_err_handler
4294
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004295};
4296
4297static int __init qlcnic_init_module(void)
4298{
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004299 int ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004300
4301 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4302
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004303 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4304 if (qlcnic_wq == NULL) {
4305 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4306 return -ENOMEM;
4307 }
4308
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004309#ifdef CONFIG_INET
4310 register_netdevice_notifier(&qlcnic_netdev_cb);
4311 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4312#endif
4313
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004314 ret = pci_register_driver(&qlcnic_driver);
4315 if (ret) {
4316#ifdef CONFIG_INET
4317 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4318 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4319#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004320 destroy_workqueue(qlcnic_wq);
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004321 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004322
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004323 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004324}
4325
4326module_init(qlcnic_init_module);
4327
4328static void __exit qlcnic_exit_module(void)
4329{
4330
4331 pci_unregister_driver(&qlcnic_driver);
4332
4333#ifdef CONFIG_INET
4334 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4335 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4336#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004337 destroy_workqueue(qlcnic_wq);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004338}
4339
4340module_exit(qlcnic_exit_module);