blob: 3b740f55ca42de25d951f766b1d35e2eee2c348f [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>
16#include <linux/if_vlan.h>
17#include <net/ip.h>
18#include <linux/ipv6.h>
19#include <linux/inetdevice.h>
20#include <linux/sysfs.h>
Sucheta Chakraborty451724c2010-07-13 20:33:34 +000021#include <linux/aer.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 *);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000101/* PCI Device ID Table */
102#define ENTRY(device) \
103 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
104 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
105
106#define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
107
Amit Kumar Salecha6a902882010-02-01 05:24:54 +0000108static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000109 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
110 {0,}
111};
112
113MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
114
115
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000116inline void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000117qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
118 struct qlcnic_host_tx_ring *tx_ring)
119{
120 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000121}
122
123static const u32 msi_tgt_status[8] = {
124 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
125 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
126 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
127 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
128};
129
130static const
131struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
132
133static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
134{
135 writel(0, sds_ring->crb_intr_mask);
136}
137
138static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
139{
140 struct qlcnic_adapter *adapter = sds_ring->adapter;
141
142 writel(0x1, sds_ring->crb_intr_mask);
143
144 if (!QLCNIC_IS_MSI_FAMILY(adapter))
145 writel(0xfbff, adapter->tgt_mask_reg);
146}
147
148static int
149qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
150{
151 int size = sizeof(struct qlcnic_host_sds_ring) * count;
152
153 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
154
Eric Dumazet807540b2010-09-23 05:40:09 +0000155 return recv_ctx->sds_rings == NULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000156}
157
158static void
159qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
160{
161 if (recv_ctx->sds_rings != NULL)
162 kfree(recv_ctx->sds_rings);
163
164 recv_ctx->sds_rings = NULL;
165}
166
167static int
168qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
169{
170 int ring;
171 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000172 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000173
174 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
175 return -ENOMEM;
176
177 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
178 sds_ring = &recv_ctx->sds_rings[ring];
schacko8f891382010-06-17 02:56:40 +0000179
180 if (ring == adapter->max_sds_rings - 1)
181 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
182 QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
183 else
184 netif_napi_add(netdev, &sds_ring->napi,
185 qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000186 }
187
188 return 0;
189}
190
191static void
192qlcnic_napi_del(struct qlcnic_adapter *adapter)
193{
194 int ring;
195 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000196 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000197
198 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
199 sds_ring = &recv_ctx->sds_rings[ring];
200 netif_napi_del(&sds_ring->napi);
201 }
202
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000203 qlcnic_free_sds_rings(adapter->recv_ctx);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000204}
205
206static void
207qlcnic_napi_enable(struct qlcnic_adapter *adapter)
208{
209 int ring;
210 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000211 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000212
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000213 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
214 return;
215
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000216 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
217 sds_ring = &recv_ctx->sds_rings[ring];
218 napi_enable(&sds_ring->napi);
219 qlcnic_enable_int(sds_ring);
220 }
221}
222
223static void
224qlcnic_napi_disable(struct qlcnic_adapter *adapter)
225{
226 int ring;
227 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000228 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000229
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000230 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
231 return;
232
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000233 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
234 sds_ring = &recv_ctx->sds_rings[ring];
235 qlcnic_disable_int(sds_ring);
236 napi_synchronize(&sds_ring->napi);
237 napi_disable(&sds_ring->napi);
238 }
239}
240
241static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
242{
243 memset(&adapter->stats, 0, sizeof(adapter->stats));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000244}
245
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000246static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
247{
248 u32 control;
249 int pos;
250
251 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
252 if (pos) {
253 pci_read_config_dword(pdev, pos, &control);
254 if (enable)
255 control |= PCI_MSIX_FLAGS_ENABLE;
256 else
257 control = 0;
258 pci_write_config_dword(pdev, pos, control);
259 }
260}
261
262static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
263{
264 int i;
265
266 for (i = 0; i < count; i++)
267 adapter->msix_entries[i].entry = i;
268}
269
270static int
271qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
272{
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000273 u8 mac_addr[ETH_ALEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000274 struct net_device *netdev = adapter->netdev;
275 struct pci_dev *pdev = adapter->pdev;
276
Rajesh Borundiada48e6c2010-08-31 17:17:46 +0000277 if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000278 return -EIO;
279
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000280 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000281 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
282 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
283
284 /* set station address */
285
286 if (!is_valid_ether_addr(netdev->perm_addr))
287 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
288 netdev->dev_addr);
289
290 return 0;
291}
292
293static int qlcnic_set_mac(struct net_device *netdev, void *p)
294{
295 struct qlcnic_adapter *adapter = netdev_priv(netdev);
296 struct sockaddr *addr = p;
297
Rajesh Borundia73733732010-08-31 17:17:50 +0000298 if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
299 return -EOPNOTSUPP;
300
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000301 if (!is_valid_ether_addr(addr->sa_data))
302 return -EINVAL;
303
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000304 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000305 netif_device_detach(netdev);
306 qlcnic_napi_disable(adapter);
307 }
308
309 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
310 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
311 qlcnic_set_multi(adapter->netdev);
312
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000313 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000314 netif_device_attach(netdev);
315 qlcnic_napi_enable(adapter);
316 }
317 return 0;
318}
319
Amit Kumar Salechad5790662010-09-16 19:14:39 +0000320static void qlcnic_vlan_rx_register(struct net_device *netdev,
321 struct vlan_group *grp)
322{
323 struct qlcnic_adapter *adapter = netdev_priv(netdev);
324 adapter->vlgrp = grp;
325}
326
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000327static const struct net_device_ops qlcnic_netdev_ops = {
328 .ndo_open = qlcnic_open,
329 .ndo_stop = qlcnic_close,
330 .ndo_start_xmit = qlcnic_xmit_frame,
331 .ndo_get_stats = qlcnic_get_stats,
332 .ndo_validate_addr = eth_validate_addr,
333 .ndo_set_multicast_list = qlcnic_set_multi,
334 .ndo_set_mac_address = qlcnic_set_mac,
335 .ndo_change_mtu = qlcnic_change_mtu,
336 .ndo_tx_timeout = qlcnic_tx_timeout,
Amit Kumar Salechad5790662010-09-16 19:14:39 +0000337 .ndo_vlan_rx_register = qlcnic_vlan_rx_register,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000338#ifdef CONFIG_NET_POLL_CONTROLLER
339 .ndo_poll_controller = qlcnic_poll_controller,
340#endif
341};
342
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000343static struct qlcnic_nic_template qlcnic_ops = {
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000344 .config_bridged_mode = qlcnic_config_bridged_mode,
345 .config_led = qlcnic_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000346 .start_firmware = qlcnic_start_firmware
347};
348
349static struct qlcnic_nic_template qlcnic_vf_ops = {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000350 .config_bridged_mode = qlcnicvf_config_bridged_mode,
351 .config_led = qlcnicvf_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000352 .start_firmware = qlcnicvf_start_firmware
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000353};
354
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000355static void
356qlcnic_setup_intr(struct qlcnic_adapter *adapter)
357{
358 const struct qlcnic_legacy_intr_set *legacy_intrp;
359 struct pci_dev *pdev = adapter->pdev;
360 int err, num_msix;
361
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000362 if (adapter->msix_supported) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000363 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
364 MSIX_ENTRIES_PER_ADAPTER : 2;
365 } else
366 num_msix = 1;
367
368 adapter->max_sds_rings = 1;
369
370 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
371
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000372 legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000373
374 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
375 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
376 legacy_intrp->tgt_status_reg);
377 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
378 legacy_intrp->tgt_mask_reg);
379 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
380
381 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
382 ISR_INT_STATE_REG);
383
384 qlcnic_set_msix_bit(pdev, 0);
385
386 if (adapter->msix_supported) {
387
388 qlcnic_init_msix_entries(adapter, num_msix);
389 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
390 if (err == 0) {
391 adapter->flags |= QLCNIC_MSIX_ENABLED;
392 qlcnic_set_msix_bit(pdev, 1);
393
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000394 adapter->max_sds_rings = num_msix;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000395
396 dev_info(&pdev->dev, "using msi-x interrupts\n");
397 return;
398 }
399
400 if (err > 0)
401 pci_disable_msix(pdev);
402
403 /* fall through for msi */
404 }
405
406 if (use_msi && !pci_enable_msi(pdev)) {
407 adapter->flags |= QLCNIC_MSI_ENABLED;
408 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000409 msi_tgt_status[adapter->ahw->pci_func]);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000410 dev_info(&pdev->dev, "using msi interrupts\n");
411 adapter->msix_entries[0].vector = pdev->irq;
412 return;
413 }
414
415 dev_info(&pdev->dev, "using legacy interrupts\n");
416 adapter->msix_entries[0].vector = pdev->irq;
417}
418
419static void
420qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
421{
422 if (adapter->flags & QLCNIC_MSIX_ENABLED)
423 pci_disable_msix(adapter->pdev);
424 if (adapter->flags & QLCNIC_MSI_ENABLED)
425 pci_disable_msi(adapter->pdev);
426}
427
428static void
429qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
430{
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000431 if (adapter->ahw->pci_base0 != NULL)
432 iounmap(adapter->ahw->pci_base0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000433}
434
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000435static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000436qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
437{
Dan Carpentere88db3b2010-08-09 21:49:36 +0000438 struct qlcnic_pci_info *pci_info;
Julia Lawall900853a2010-08-16 10:23:51 +0000439 int i, ret = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000440 u8 pfn;
441
Dan Carpentere88db3b2010-08-09 21:49:36 +0000442 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
443 if (!pci_info)
444 return -ENOMEM;
445
Dan Carpenterca315ac2010-08-09 21:47:56 +0000446 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000447 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
Dan Carpentere88db3b2010-08-09 21:49:36 +0000448 if (!adapter->npars) {
Julia Lawall900853a2010-08-16 10:23:51 +0000449 ret = -ENOMEM;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000450 goto err_pci_info;
451 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000452
Dan Carpenterca315ac2010-08-09 21:47:56 +0000453 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000454 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
455 if (!adapter->eswitch) {
Julia Lawall900853a2010-08-16 10:23:51 +0000456 ret = -ENOMEM;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000457 goto err_npars;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000458 }
459
460 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000461 if (ret)
462 goto err_eswitch;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000463
Dan Carpenterca315ac2010-08-09 21:47:56 +0000464 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
465 pfn = pci_info[i].id;
Sony Chackof848d6d2011-04-01 14:27:59 +0000466 if (pfn > QLCNIC_MAX_PCI_FUNC) {
467 ret = QL_STATUS_INVALID_PARAM;
468 goto err_eswitch;
469 }
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000470 adapter->npars[pfn].active = (u8)pci_info[i].active;
471 adapter->npars[pfn].type = (u8)pci_info[i].type;
472 adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000473 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
474 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000475 }
476
Dan Carpenterca315ac2010-08-09 21:47:56 +0000477 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
478 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
479
Dan Carpentere88db3b2010-08-09 21:49:36 +0000480 kfree(pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000481 return 0;
482
483err_eswitch:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000484 kfree(adapter->eswitch);
485 adapter->eswitch = NULL;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000486err_npars:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000487 kfree(adapter->npars);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000488 adapter->npars = NULL;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000489err_pci_info:
490 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000491
492 return ret;
493}
494
495static int
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000496qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
497{
498 u8 id;
499 u32 ref_count;
500 int i, ret = 1;
501 u32 data = QLCNIC_MGMT_FUNC;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000502 void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000503
504 /* If other drivers are not in use set their privilege level */
Amit Kumar Salecha31018e02010-08-25 04:03:05 +0000505 ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000506 ret = qlcnic_api_lock(adapter);
507 if (ret)
508 goto err_lock;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000509
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000510 if (qlcnic_config_npars) {
511 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000512 id = i;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000513 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000514 id == adapter->ahw->pci_func)
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000515 continue;
516 data |= (qlcnic_config_npars &
517 QLC_DEV_SET_DRV(0xf, id));
518 }
519 } else {
520 data = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000521 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000522 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000523 adapter->ahw->pci_func));
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000524 }
525 writel(data, priv_op);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000526 qlcnic_api_unlock(adapter);
527err_lock:
528 return ret;
529}
530
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000531static void
532qlcnic_check_vf(struct qlcnic_adapter *adapter)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000533{
534 void __iomem *msix_base_addr;
535 void __iomem *priv_op;
536 u32 func;
537 u32 msix_base;
538 u32 op_mode, priv_level;
539
540 /* Determine FW API version */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000541 adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
542 QLCNIC_FW_API);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000543
544 /* Find PCI function number */
545 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000546 msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000547 msix_base = readl(msix_base_addr);
548 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000549 adapter->ahw->pci_func = func;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000550
551 /* Determine function privilege level */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000552 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000553 op_mode = readl(priv_op);
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000554 if (op_mode == QLC_DEV_DRV_DEFAULT)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000555 priv_level = QLCNIC_MGMT_FUNC;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000556 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000557 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000558
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000559 if (priv_level == QLCNIC_NON_PRIV_FUNC) {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000560 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
561 dev_info(&adapter->pdev->dev,
562 "HAL Version: %d Non Privileged function\n",
563 adapter->fw_hal_version);
564 adapter->nic_ops = &qlcnic_vf_ops;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000565 } else
566 adapter->nic_ops = &qlcnic_ops;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000567}
568
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000569static int
570qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
571{
572 void __iomem *mem_ptr0 = NULL;
573 resource_size_t mem_base;
574 unsigned long mem_len, pci_len0 = 0;
575
576 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000577
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000578 /* remap phys address */
579 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
580 mem_len = pci_resource_len(pdev, 0);
581
582 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
583
584 mem_ptr0 = pci_ioremap_bar(pdev, 0);
585 if (mem_ptr0 == NULL) {
586 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
587 return -EIO;
588 }
589 pci_len0 = mem_len;
590 } else {
591 return -EIO;
592 }
593
594 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
595
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000596 adapter->ahw->pci_base0 = mem_ptr0;
597 adapter->ahw->pci_len0 = pci_len0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000598
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000599 qlcnic_check_vf(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000600
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000601 adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
602 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
603 adapter->ahw->pci_func)));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000604
605 return 0;
606}
607
608static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
609{
610 struct pci_dev *pdev = adapter->pdev;
611 int i, found = 0;
612
613 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
614 if (qlcnic_boards[i].vendor == pdev->vendor &&
615 qlcnic_boards[i].device == pdev->device &&
616 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
617 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +0000618 sprintf(name, "%pM: %s" ,
619 adapter->mac_addr,
620 qlcnic_boards[i].short_name);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000621 found = 1;
622 break;
623 }
624
625 }
626
627 if (!found)
Sritej Velaga7f9a0c32010-06-17 02:56:39 +0000628 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000629}
630
631static void
632qlcnic_check_options(struct qlcnic_adapter *adapter)
633{
634 u32 fw_major, fw_minor, fw_build;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000635 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000636
637 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
638 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
639 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
640
641 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
642
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000643 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
644 fw_major, fw_minor, fw_build);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000645 if (adapter->ahw->port_type == QLCNIC_XGBE) {
Sony Chacko90d19002010-10-26 17:53:08 +0000646 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
647 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
648 adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
649 } else {
650 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
651 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
652 }
653
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000654 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
Sony Chacko90d19002010-10-26 17:53:08 +0000655 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
656
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000657 } else if (adapter->ahw->port_type == QLCNIC_GBE) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000658 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
659 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
Sony Chacko90d19002010-10-26 17:53:08 +0000660 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
661 adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000662 }
663
664 adapter->msix_supported = !!use_msi_x;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000665
666 adapter->num_txd = MAX_CMD_DESCRIPTORS;
667
Sony Chacko251b0362010-08-19 05:08:24 +0000668 adapter->max_rds_rings = MAX_RDS_RINGS;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000669}
670
Rajesh Borundia174240a2010-08-31 17:17:47 +0000671static int
672qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
673{
674 int err;
675 struct qlcnic_info nic_info;
676
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000677 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
Rajesh Borundia174240a2010-08-31 17:17:47 +0000678 if (err)
679 return err;
680
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000681 adapter->physical_port = (u8)nic_info.phys_port;
Rajesh Borundia174240a2010-08-31 17:17:47 +0000682 adapter->switch_mode = nic_info.switch_mode;
683 adapter->max_tx_ques = nic_info.max_tx_ques;
684 adapter->max_rx_ques = nic_info.max_rx_ques;
685 adapter->capabilities = nic_info.capabilities;
686 adapter->max_mac_filters = nic_info.max_mac_filters;
687 adapter->max_mtu = nic_info.max_mtu;
688
689 if (adapter->capabilities & BIT_6)
690 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
691 else
692 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
693
694 return err;
695}
696
Rajesh Borundia0325d692010-08-19 05:08:26 +0000697static void
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000698qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
699 struct qlcnic_esw_func_cfg *esw_cfg)
700{
701 if (esw_cfg->discard_tagged)
702 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
703 else
704 adapter->flags |= QLCNIC_TAGGING_ENABLED;
705
706 if (esw_cfg->vlan_id)
707 adapter->pvid = esw_cfg->vlan_id;
708 else
709 adapter->pvid = 0;
710}
711
712static void
Rajesh Borundia0325d692010-08-19 05:08:26 +0000713qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
714 struct qlcnic_esw_func_cfg *esw_cfg)
715{
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000716 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
717 QLCNIC_PROMISC_DISABLED);
Rajesh Borundia7613c872010-08-31 17:17:48 +0000718
719 if (esw_cfg->mac_anti_spoof)
720 adapter->flags |= QLCNIC_MACSPOOF;
Sony Chackofe4d4342010-08-19 05:08:27 +0000721
Rajesh Borundia73733732010-08-31 17:17:50 +0000722 if (!esw_cfg->mac_override)
723 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
724
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000725 if (!esw_cfg->promisc_mode)
726 adapter->flags |= QLCNIC_PROMISC_DISABLED;
727
Rajesh Borundia0325d692010-08-19 05:08:26 +0000728 qlcnic_set_netdev_features(adapter, esw_cfg);
729}
730
731static int
732qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
733{
734 struct qlcnic_esw_func_cfg esw_cfg;
735
736 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
737 return 0;
738
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000739 esw_cfg.pci_func = adapter->ahw->pci_func;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000740 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
741 return -EIO;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000742 qlcnic_set_vlan_config(adapter, &esw_cfg);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000743 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
744
745 return 0;
746}
747
748static void
749qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
750 struct qlcnic_esw_func_cfg *esw_cfg)
751{
752 struct net_device *netdev = adapter->netdev;
753 unsigned long features, vlan_features;
754
755 features = (NETIF_F_SG | NETIF_F_IP_CSUM |
756 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
757 vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
758 NETIF_F_IPV6_CSUM);
759
760 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
761 features |= (NETIF_F_TSO | NETIF_F_TSO6);
762 vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
763 }
764 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
765 features |= NETIF_F_LRO;
766
767 if (esw_cfg->offload_flags & BIT_0) {
768 netdev->features |= features;
769 adapter->rx_csum = 1;
770 if (!(esw_cfg->offload_flags & BIT_1))
771 netdev->features &= ~NETIF_F_TSO;
772 if (!(esw_cfg->offload_flags & BIT_2))
773 netdev->features &= ~NETIF_F_TSO6;
774 } else {
775 netdev->features &= ~features;
776 adapter->rx_csum = 0;
777 }
778
779 netdev->vlan_features = (features & vlan_features);
780}
781
782static int
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000783qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
784{
785 void __iomem *priv_op;
786 u32 op_mode, priv_level;
787 int err = 0;
788
Rajesh Borundia174240a2010-08-31 17:17:47 +0000789 err = qlcnic_initialize_nic(adapter);
790 if (err)
791 return err;
792
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000793 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
794 return 0;
795
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000796 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000797 op_mode = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000798 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000799
800 if (op_mode == QLC_DEV_DRV_DEFAULT)
801 priv_level = QLCNIC_MGMT_FUNC;
802 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000803 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000804
Rajesh Borundia174240a2010-08-31 17:17:47 +0000805 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000806 if (priv_level == QLCNIC_MGMT_FUNC) {
807 adapter->op_mode = QLCNIC_MGMT_FUNC;
808 err = qlcnic_init_pci_info(adapter);
809 if (err)
810 return err;
811 /* Set privilege level for other functions */
812 qlcnic_set_function_modes(adapter);
813 dev_info(&adapter->pdev->dev,
814 "HAL Version: %d, Management function\n",
815 adapter->fw_hal_version);
816 } else if (priv_level == QLCNIC_PRIV_FUNC) {
817 adapter->op_mode = QLCNIC_PRIV_FUNC;
818 dev_info(&adapter->pdev->dev,
819 "HAL Version: %d, Privileged function\n",
820 adapter->fw_hal_version);
821 }
Rajesh Borundia174240a2010-08-31 17:17:47 +0000822 }
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000823
824 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
825
826 return err;
827}
828
829static int
Rajesh Borundia0325d692010-08-19 05:08:26 +0000830qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
831{
832 struct qlcnic_esw_func_cfg esw_cfg;
833 struct qlcnic_npar_info *npar;
834 u8 i;
835
Rajesh Borundia174240a2010-08-31 17:17:47 +0000836 if (adapter->need_fw_reset)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000837 return 0;
838
839 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
840 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
841 continue;
842 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
843 esw_cfg.pci_func = i;
844 esw_cfg.offload_flags = BIT_0;
Rajesh Borundia73733732010-08-31 17:17:50 +0000845 esw_cfg.mac_override = BIT_0;
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000846 esw_cfg.promisc_mode = BIT_0;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000847 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
848 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
849 if (qlcnic_config_switch_port(adapter, &esw_cfg))
850 return -EIO;
851 npar = &adapter->npars[i];
852 npar->pvid = esw_cfg.vlan_id;
Rajesh Borundia73733732010-08-31 17:17:50 +0000853 npar->mac_override = esw_cfg.mac_override;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000854 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
855 npar->discard_tagged = esw_cfg.discard_tagged;
856 npar->promisc_mode = esw_cfg.promisc_mode;
857 npar->offload_flags = esw_cfg.offload_flags;
858 }
859
860 return 0;
861}
862
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000863static int
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000864qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
865 struct qlcnic_npar_info *npar, int pci_func)
866{
867 struct qlcnic_esw_func_cfg esw_cfg;
868 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
869 esw_cfg.pci_func = pci_func;
870 esw_cfg.vlan_id = npar->pvid;
Rajesh Borundia73733732010-08-31 17:17:50 +0000871 esw_cfg.mac_override = npar->mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000872 esw_cfg.discard_tagged = npar->discard_tagged;
873 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
874 esw_cfg.offload_flags = npar->offload_flags;
875 esw_cfg.promisc_mode = npar->promisc_mode;
876 if (qlcnic_config_switch_port(adapter, &esw_cfg))
877 return -EIO;
878
879 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
880 if (qlcnic_config_switch_port(adapter, &esw_cfg))
881 return -EIO;
882
883 return 0;
884}
885
886static int
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000887qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
888{
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000889 int i, err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000890 struct qlcnic_npar_info *npar;
891 struct qlcnic_info nic_info;
892
Rajesh Borundia174240a2010-08-31 17:17:47 +0000893 if (!adapter->need_fw_reset)
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000894 return 0;
895
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000896 /* Set the NPAR config data after FW reset */
897 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
898 npar = &adapter->npars[i];
899 if (npar->type != QLCNIC_TYPE_NIC)
900 continue;
901 err = qlcnic_get_nic_info(adapter, &nic_info, i);
902 if (err)
903 return err;
904 nic_info.min_tx_bw = npar->min_bw;
905 nic_info.max_tx_bw = npar->max_bw;
906 err = qlcnic_set_nic_info(adapter, &nic_info);
907 if (err)
908 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000909
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000910 if (npar->enable_pm) {
911 err = qlcnic_config_port_mirroring(adapter,
912 npar->dest_npar, 1, i);
913 if (err)
914 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000915 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000916 err = qlcnic_reset_eswitch_config(adapter, npar, i);
917 if (err)
918 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000919 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000920 return 0;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000921}
922
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +0000923static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
924{
925 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
926 u32 npar_state;
927
928 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
929 return 0;
930
931 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
932 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
933 msleep(1000);
934 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
935 }
936 if (!npar_opt_timeo) {
937 dev_err(&adapter->pdev->dev,
938 "Waiting for NPAR state to opertional timeout\n");
939 return -EIO;
940 }
941 return 0;
942}
943
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000944static int
Rajesh Borundia174240a2010-08-31 17:17:47 +0000945qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
946{
947 int err;
948
949 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
950 adapter->op_mode != QLCNIC_MGMT_FUNC)
951 return 0;
952
953 err = qlcnic_set_default_offload_settings(adapter);
954 if (err)
955 return err;
956
957 err = qlcnic_reset_npar_config(adapter);
958 if (err)
959 return err;
960
961 qlcnic_dev_set_npar_ready(adapter);
962
963 return err;
964}
965
966static int
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000967qlcnic_start_firmware(struct qlcnic_adapter *adapter)
968{
Sony Chackod4066832010-08-19 05:08:31 +0000969 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000970
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000971 err = qlcnic_can_start_firmware(adapter);
972 if (err < 0)
973 return err;
974 else if (!err)
Sony Chackod4066832010-08-19 05:08:31 +0000975 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000976
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000977 if (load_fw_file)
978 qlcnic_request_firmware(adapter);
schacko8f891382010-06-17 02:56:40 +0000979 else {
Sony Chacko8cfdce02010-08-26 14:02:41 +0000980 err = qlcnic_check_flash_fw_ver(adapter);
981 if (err)
schacko8f891382010-06-17 02:56:40 +0000982 goto err_out;
983
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000984 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
schacko8f891382010-06-17 02:56:40 +0000985 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000986
987 err = qlcnic_need_fw_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000988 if (err == 0)
Sony Chacko4e708122010-08-31 17:17:44 +0000989 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000990
Sony Chackod4066832010-08-19 05:08:31 +0000991 err = qlcnic_pinit_from_rom(adapter);
992 if (err)
993 goto err_out;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000994
995 err = qlcnic_load_firmware(adapter);
996 if (err)
997 goto err_out;
998
999 qlcnic_release_firmware(adapter);
Sony Chackod4066832010-08-19 05:08:31 +00001000 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001001
Sony Chackod4066832010-08-19 05:08:31 +00001002check_fw_status:
1003 err = qlcnic_check_fw_status(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001004 if (err)
1005 goto err_out;
1006
1007 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001008 qlcnic_idc_debug_info(adapter, 1);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00001009
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001010 err = qlcnic_check_eswitch_mode(adapter);
1011 if (err) {
1012 dev_err(&adapter->pdev->dev,
1013 "Memory allocation failed for eswitch\n");
1014 goto err_out;
1015 }
Rajesh Borundia174240a2010-08-31 17:17:47 +00001016 err = qlcnic_set_mgmt_operations(adapter);
1017 if (err)
1018 goto err_out;
1019
1020 qlcnic_check_options(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001021 adapter->need_fw_reset = 0;
1022
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001023 qlcnic_release_firmware(adapter);
1024 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001025
1026err_out:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001027 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1028 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001029
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001030 qlcnic_release_firmware(adapter);
1031 return err;
1032}
1033
1034static int
1035qlcnic_request_irq(struct qlcnic_adapter *adapter)
1036{
1037 irq_handler_t handler;
1038 struct qlcnic_host_sds_ring *sds_ring;
1039 int err, ring;
1040
1041 unsigned long flags = 0;
1042 struct net_device *netdev = adapter->netdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001043 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001044
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001045 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1046 handler = qlcnic_tmp_intr;
1047 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1048 flags |= IRQF_SHARED;
1049
1050 } else {
1051 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1052 handler = qlcnic_msix_intr;
1053 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1054 handler = qlcnic_msi_intr;
1055 else {
1056 flags |= IRQF_SHARED;
1057 handler = qlcnic_intr;
1058 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001059 }
1060 adapter->irq = netdev->irq;
1061
1062 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1063 sds_ring = &recv_ctx->sds_rings[ring];
1064 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1065 err = request_irq(sds_ring->irq, handler,
1066 flags, sds_ring->name, sds_ring);
1067 if (err)
1068 return err;
1069 }
1070
1071 return 0;
1072}
1073
1074static void
1075qlcnic_free_irq(struct qlcnic_adapter *adapter)
1076{
1077 int ring;
1078 struct qlcnic_host_sds_ring *sds_ring;
1079
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001080 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001081
1082 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1083 sds_ring = &recv_ctx->sds_rings[ring];
1084 free_irq(sds_ring->irq, sds_ring);
1085 }
1086}
1087
1088static void
1089qlcnic_init_coalesce_defaults(struct qlcnic_adapter *adapter)
1090{
1091 adapter->coal.flags = QLCNIC_INTR_DEFAULT;
1092 adapter->coal.normal.data.rx_time_us =
1093 QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1094 adapter->coal.normal.data.rx_packets =
1095 QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
1096 adapter->coal.normal.data.tx_time_us =
1097 QLCNIC_DEFAULT_INTR_COALESCE_TX_TIME_US;
1098 adapter->coal.normal.data.tx_packets =
1099 QLCNIC_DEFAULT_INTR_COALESCE_TX_PACKETS;
1100}
1101
1102static int
1103__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1104{
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001105 int ring;
1106 struct qlcnic_host_rds_ring *rds_ring;
1107
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001108 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1109 return -EIO;
1110
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001111 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1112 return 0;
Rajesh Borundia0325d692010-08-19 05:08:26 +00001113 if (qlcnic_set_eswitch_port_config(adapter))
1114 return -EIO;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001115
1116 if (qlcnic_fw_create_ctx(adapter))
1117 return -EIO;
1118
1119 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001120 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1121 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001122 }
1123
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001124 qlcnic_set_multi(netdev);
1125 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1126
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001127 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001128
1129 if (adapter->max_sds_rings > 1)
1130 qlcnic_config_rss(adapter, 1);
1131
1132 qlcnic_config_intr_coalesce(adapter);
1133
Sucheta Chakraborty24763d82010-08-17 00:34:25 +00001134 if (netdev->features & NETIF_F_LRO)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001135 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1136
1137 qlcnic_napi_enable(adapter);
1138
1139 qlcnic_linkevent_request(adapter, 1);
1140
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001141 adapter->reset_context = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001142 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1143 return 0;
1144}
1145
1146/* Usage: During resume and firmware recovery module.*/
1147
1148static int
1149qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1150{
1151 int err = 0;
1152
1153 rtnl_lock();
1154 if (netif_running(netdev))
1155 err = __qlcnic_up(adapter, netdev);
1156 rtnl_unlock();
1157
1158 return err;
1159}
1160
1161static void
1162__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1163{
1164 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1165 return;
1166
1167 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1168 return;
1169
1170 smp_mb();
1171 spin_lock(&adapter->tx_clean_lock);
1172 netif_carrier_off(netdev);
1173 netif_tx_disable(netdev);
1174
1175 qlcnic_free_mac_list(adapter);
1176
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001177 if (adapter->fhash.fnum)
1178 qlcnic_delete_lb_filters(adapter);
1179
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001180 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1181
1182 qlcnic_napi_disable(adapter);
1183
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001184 qlcnic_fw_destroy_ctx(adapter);
1185
1186 qlcnic_reset_rx_buffers_list(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001187 qlcnic_release_tx_buffers(adapter);
1188 spin_unlock(&adapter->tx_clean_lock);
1189}
1190
1191/* Usage: During suspend and firmware recovery module */
1192
1193static void
1194qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1195{
1196 rtnl_lock();
1197 if (netif_running(netdev))
1198 __qlcnic_down(adapter, netdev);
1199 rtnl_unlock();
1200
1201}
1202
1203static int
1204qlcnic_attach(struct qlcnic_adapter *adapter)
1205{
1206 struct net_device *netdev = adapter->netdev;
1207 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001208 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001209
1210 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1211 return 0;
1212
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001213 err = qlcnic_napi_add(adapter, netdev);
1214 if (err)
1215 return err;
1216
1217 err = qlcnic_alloc_sw_resources(adapter);
1218 if (err) {
1219 dev_err(&pdev->dev, "Error in setting sw resources\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001220 goto err_out_napi_del;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001221 }
1222
1223 err = qlcnic_alloc_hw_resources(adapter);
1224 if (err) {
1225 dev_err(&pdev->dev, "Error in setting hw resources\n");
1226 goto err_out_free_sw;
1227 }
1228
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001229 err = qlcnic_request_irq(adapter);
1230 if (err) {
1231 dev_err(&pdev->dev, "failed to setup interrupt\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001232 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001233 }
1234
1235 qlcnic_init_coalesce_defaults(adapter);
1236
1237 qlcnic_create_sysfs_entries(adapter);
1238
1239 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1240 return 0;
1241
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001242err_out_free_hw:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001243 qlcnic_free_hw_resources(adapter);
1244err_out_free_sw:
1245 qlcnic_free_sw_resources(adapter);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001246err_out_napi_del:
1247 qlcnic_napi_del(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001248 return err;
1249}
1250
1251static void
1252qlcnic_detach(struct qlcnic_adapter *adapter)
1253{
1254 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1255 return;
1256
1257 qlcnic_remove_sysfs_entries(adapter);
1258
1259 qlcnic_free_hw_resources(adapter);
1260 qlcnic_release_rx_buffers(adapter);
1261 qlcnic_free_irq(adapter);
1262 qlcnic_napi_del(adapter);
1263 qlcnic_free_sw_resources(adapter);
1264
1265 adapter->is_up = 0;
1266}
1267
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001268void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1269{
1270 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1271 struct qlcnic_host_sds_ring *sds_ring;
1272 int ring;
1273
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001274 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001275 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1276 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001277 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001278 qlcnic_disable_int(sds_ring);
1279 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001280 }
1281
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001282 qlcnic_fw_destroy_ctx(adapter);
1283
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001284 qlcnic_detach(adapter);
1285
1286 adapter->diag_test = 0;
1287 adapter->max_sds_rings = max_sds_rings;
1288
1289 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001290 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001291
1292 if (netif_running(netdev))
1293 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001294out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001295 netif_device_attach(netdev);
1296}
1297
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001298static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1299{
1300 int err = 0;
1301 adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1302 GFP_KERNEL);
1303 if (!adapter->ahw) {
1304 dev_err(&adapter->pdev->dev,
1305 "Failed to allocate recv ctx resources for adapter\n");
1306 err = -ENOMEM;
1307 goto err_out;
1308 }
1309 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1310 GFP_KERNEL);
1311 if (!adapter->recv_ctx) {
1312 dev_err(&adapter->pdev->dev,
1313 "Failed to allocate recv ctx resources for adapter\n");
1314 kfree(adapter->ahw);
1315 adapter->ahw = NULL;
1316 err = -ENOMEM;
1317 }
1318err_out:
1319 return err;
1320}
1321
1322static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1323{
1324 kfree(adapter->recv_ctx);
1325 adapter->recv_ctx = NULL;
1326
1327 kfree(adapter->ahw);
1328 adapter->ahw = NULL;
1329}
1330
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001331int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1332{
1333 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1334 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001335 struct qlcnic_host_rds_ring *rds_ring;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001336 int ring;
1337 int ret;
1338
1339 netif_device_detach(netdev);
1340
1341 if (netif_running(netdev))
1342 __qlcnic_down(adapter, netdev);
1343
1344 qlcnic_detach(adapter);
1345
1346 adapter->max_sds_rings = 1;
1347 adapter->diag_test = test;
1348
1349 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001350 if (ret) {
1351 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001352 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001353 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001354
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001355 ret = qlcnic_fw_create_ctx(adapter);
1356 if (ret) {
1357 qlcnic_detach(adapter);
Sony Chacko57e46242010-07-24 18:32:18 +00001358 netif_device_attach(netdev);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001359 return ret;
1360 }
1361
1362 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001363 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1364 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001365 }
1366
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001367 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1368 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001369 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001370 qlcnic_enable_int(sds_ring);
1371 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001372 }
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001373 set_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001374
1375 return 0;
1376}
1377
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001378/* Reset context in hardware only */
1379static int
1380qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1381{
1382 struct net_device *netdev = adapter->netdev;
1383
1384 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1385 return -EBUSY;
1386
1387 netif_device_detach(netdev);
1388
1389 qlcnic_down(adapter, netdev);
1390
1391 qlcnic_up(adapter, netdev);
1392
1393 netif_device_attach(netdev);
1394
1395 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1396 return 0;
1397}
1398
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001399int
1400qlcnic_reset_context(struct qlcnic_adapter *adapter)
1401{
1402 int err = 0;
1403 struct net_device *netdev = adapter->netdev;
1404
1405 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1406 return -EBUSY;
1407
1408 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1409
1410 netif_device_detach(netdev);
1411
1412 if (netif_running(netdev))
1413 __qlcnic_down(adapter, netdev);
1414
1415 qlcnic_detach(adapter);
1416
1417 if (netif_running(netdev)) {
1418 err = qlcnic_attach(adapter);
1419 if (!err)
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001420 __qlcnic_up(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001421 }
1422
1423 netif_device_attach(netdev);
1424 }
1425
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001426 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1427 return err;
1428}
1429
1430static int
1431qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001432 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001433{
1434 int err;
1435 struct pci_dev *pdev = adapter->pdev;
1436
1437 adapter->rx_csum = 1;
1438 adapter->mc_enabled = 0;
1439 adapter->max_mc_count = 38;
1440
1441 netdev->netdev_ops = &qlcnic_netdev_ops;
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001442 netdev->watchdog_timeo = 5*HZ;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001443
1444 qlcnic_change_mtu(netdev, netdev->mtu);
1445
1446 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1447
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001448 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001449 NETIF_F_IPV6_CSUM | NETIF_F_GRO | NETIF_F_HW_VLAN_RX);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001450 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
Anirban Chakrabortyac8d0c42010-07-09 13:14:58 +00001451 NETIF_F_IPV6_CSUM);
1452
1453 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
1454 netdev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
1455 netdev->vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
1456 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001457
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001458 if (pci_using_dac) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001459 netdev->features |= NETIF_F_HIGHDMA;
1460 netdev->vlan_features |= NETIF_F_HIGHDMA;
1461 }
1462
1463 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1464 netdev->features |= (NETIF_F_HW_VLAN_TX);
1465
1466 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1467 netdev->features |= NETIF_F_LRO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001468 netdev->irq = adapter->msix_entries[0].vector;
1469
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001470 netif_carrier_off(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001471
1472 err = register_netdev(netdev);
1473 if (err) {
1474 dev_err(&pdev->dev, "failed to register net device\n");
1475 return err;
1476 }
1477
1478 return 0;
1479}
1480
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001481static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1482{
1483 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1484 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1485 *pci_using_dac = 1;
1486 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1487 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1488 *pci_using_dac = 0;
1489 else {
1490 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1491 return -EIO;
1492 }
1493
1494 return 0;
1495}
1496
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001497static int __devinit
1498qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1499{
1500 struct net_device *netdev = NULL;
1501 struct qlcnic_adapter *adapter = NULL;
1502 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001503 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001504 uint8_t pci_using_dac;
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001505 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001506
1507 err = pci_enable_device(pdev);
1508 if (err)
1509 return err;
1510
1511 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1512 err = -ENODEV;
1513 goto err_out_disable_pdev;
1514 }
1515
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001516 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1517 if (err)
1518 goto err_out_disable_pdev;
1519
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001520 err = pci_request_regions(pdev, qlcnic_driver_name);
1521 if (err)
1522 goto err_out_disable_pdev;
1523
1524 pci_set_master(pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001525 pci_enable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001526
1527 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1528 if (!netdev) {
1529 dev_err(&pdev->dev, "failed to allocate net_device\n");
1530 err = -ENOMEM;
1531 goto err_out_free_res;
1532 }
1533
1534 SET_NETDEV_DEV(netdev, &pdev->dev);
1535
1536 adapter = netdev_priv(netdev);
1537 adapter->netdev = netdev;
1538 adapter->pdev = pdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001539
1540 if (qlcnic_alloc_adapter_resources(adapter))
1541 goto err_out_free_netdev;
1542
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001543 adapter->dev_rst_time = jiffies;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001544 revision_id = pdev->revision;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001545 adapter->ahw->revision_id = revision_id;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001546
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001547 rwlock_init(&adapter->ahw->crb_lock);
1548 mutex_init(&adapter->ahw->mem_lock);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001549
1550 spin_lock_init(&adapter->tx_clean_lock);
1551 INIT_LIST_HEAD(&adapter->mac_list);
1552
1553 err = qlcnic_setup_pci_map(adapter);
1554 if (err)
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001555 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001556
1557 /* This will be reset for mezz cards */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001558 adapter->portnum = adapter->ahw->pci_func;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001559
1560 err = qlcnic_get_board_info(adapter);
1561 if (err) {
1562 dev_err(&pdev->dev, "Error getting board config info.\n");
1563 goto err_out_iounmap;
1564 }
1565
Sony Chacko8cfdce02010-08-26 14:02:41 +00001566 err = qlcnic_setup_idc_param(adapter);
1567 if (err)
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +00001568 goto err_out_iounmap;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001569
Rajesh Borundia1dc0f3c2010-12-16 22:59:02 +00001570 adapter->flags |= QLCNIC_NEED_FLR;
Rajesh Borundiab0044bc2010-11-23 01:25:21 +00001571
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001572 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001573 if (err) {
1574 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001575 goto err_out_decr_ref;
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001576 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001577
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001578 if (qlcnic_read_mac_addr(adapter))
1579 dev_warn(&pdev->dev, "failed to read mac addr\n");
1580
1581 if (adapter->portnum == 0) {
1582 get_brd_name(adapter, brd_name);
1583
1584 pr_info("%s: %s Board Chip rev 0x%x\n",
1585 module_name(THIS_MODULE),
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001586 brd_name, adapter->ahw->revision_id);
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001587 }
1588
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001589 qlcnic_clear_stats(adapter);
1590
1591 qlcnic_setup_intr(adapter);
1592
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001593 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001594 if (err)
1595 goto err_out_disable_msi;
1596
1597 pci_set_drvdata(pdev, adapter);
1598
1599 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1600
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001601 switch (adapter->ahw->port_type) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001602 case QLCNIC_GBE:
1603 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1604 adapter->netdev->name);
1605 break;
1606 case QLCNIC_XGBE:
1607 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1608 adapter->netdev->name);
1609 break;
1610 }
1611
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001612 qlcnic_alloc_lb_filters_mem(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001613 qlcnic_create_diag_entries(adapter);
1614
1615 return 0;
1616
1617err_out_disable_msi:
1618 qlcnic_teardown_intr(adapter);
1619
1620err_out_decr_ref:
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001621 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001622
1623err_out_iounmap:
1624 qlcnic_cleanup_pci_map(adapter);
1625
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001626err_out_free_hw:
1627 qlcnic_free_adapter_resources(adapter);
1628
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001629err_out_free_netdev:
1630 free_netdev(netdev);
1631
1632err_out_free_res:
1633 pci_release_regions(pdev);
1634
1635err_out_disable_pdev:
1636 pci_set_drvdata(pdev, NULL);
1637 pci_disable_device(pdev);
1638 return err;
1639}
1640
1641static void __devexit qlcnic_remove(struct pci_dev *pdev)
1642{
1643 struct qlcnic_adapter *adapter;
1644 struct net_device *netdev;
1645
1646 adapter = pci_get_drvdata(pdev);
1647 if (adapter == NULL)
1648 return;
1649
1650 netdev = adapter->netdev;
1651
1652 qlcnic_cancel_fw_work(adapter);
1653
1654 unregister_netdev(netdev);
1655
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001656 qlcnic_detach(adapter);
1657
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001658 if (adapter->npars != NULL)
1659 kfree(adapter->npars);
1660 if (adapter->eswitch != NULL)
1661 kfree(adapter->eswitch);
1662
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
1665 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1666
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001667 qlcnic_free_lb_filters_mem(adapter);
1668
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001669 qlcnic_teardown_intr(adapter);
1670
1671 qlcnic_remove_diag_entries(adapter);
1672
1673 qlcnic_cleanup_pci_map(adapter);
1674
1675 qlcnic_release_firmware(adapter);
1676
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001677 pci_disable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001678 pci_release_regions(pdev);
1679 pci_disable_device(pdev);
1680 pci_set_drvdata(pdev, NULL);
1681
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001682 qlcnic_free_adapter_resources(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001683 free_netdev(netdev);
1684}
1685static int __qlcnic_shutdown(struct pci_dev *pdev)
1686{
1687 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1688 struct net_device *netdev = adapter->netdev;
1689 int retval;
1690
1691 netif_device_detach(netdev);
1692
1693 qlcnic_cancel_fw_work(adapter);
1694
1695 if (netif_running(netdev))
1696 qlcnic_down(adapter, netdev);
1697
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001698 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001699
1700 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1701
1702 retval = pci_save_state(pdev);
1703 if (retval)
1704 return retval;
1705
1706 if (qlcnic_wol_supported(adapter)) {
1707 pci_enable_wake(pdev, PCI_D3cold, 1);
1708 pci_enable_wake(pdev, PCI_D3hot, 1);
1709 }
1710
1711 return 0;
1712}
1713
1714static void qlcnic_shutdown(struct pci_dev *pdev)
1715{
1716 if (__qlcnic_shutdown(pdev))
1717 return;
1718
1719 pci_disable_device(pdev);
1720}
1721
1722#ifdef CONFIG_PM
1723static int
1724qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1725{
1726 int retval;
1727
1728 retval = __qlcnic_shutdown(pdev);
1729 if (retval)
1730 return retval;
1731
1732 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1733 return 0;
1734}
1735
1736static int
1737qlcnic_resume(struct pci_dev *pdev)
1738{
1739 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1740 struct net_device *netdev = adapter->netdev;
1741 int err;
1742
1743 err = pci_enable_device(pdev);
1744 if (err)
1745 return err;
1746
1747 pci_set_power_state(pdev, PCI_D0);
1748 pci_set_master(pdev);
1749 pci_restore_state(pdev);
1750
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001751 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001752 if (err) {
1753 dev_err(&pdev->dev, "failed to start firmware\n");
1754 return err;
1755 }
1756
1757 if (netif_running(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001758 err = qlcnic_up(adapter, netdev);
1759 if (err)
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001760 goto done;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001761
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00001762 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001763 }
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001764done:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001765 netif_device_attach(netdev);
1766 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1767 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001768}
1769#endif
1770
1771static int qlcnic_open(struct net_device *netdev)
1772{
1773 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1774 int err;
1775
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001776 err = qlcnic_attach(adapter);
1777 if (err)
1778 return err;
1779
1780 err = __qlcnic_up(adapter, netdev);
1781 if (err)
1782 goto err_out;
1783
1784 netif_start_queue(netdev);
1785
1786 return 0;
1787
1788err_out:
1789 qlcnic_detach(adapter);
1790 return err;
1791}
1792
1793/*
1794 * qlcnic_close - Disables a network interface entry point
1795 */
1796static int qlcnic_close(struct net_device *netdev)
1797{
1798 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1799
1800 __qlcnic_down(adapter, netdev);
1801 return 0;
1802}
1803
1804static void
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001805qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1806{
1807 void *head;
1808 int i;
1809
1810 if (!qlcnic_mac_learn)
1811 return;
1812
1813 spin_lock_init(&adapter->mac_learn_lock);
1814
1815 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1816 GFP_KERNEL);
1817 if (!head)
1818 return;
1819
1820 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1821 adapter->fhash.fhead = (struct hlist_head *)head;
1822
1823 for (i = 0; i < adapter->fhash.fmax; i++)
1824 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1825}
1826
1827static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1828{
1829 if (adapter->fhash.fmax && adapter->fhash.fhead)
1830 kfree(adapter->fhash.fhead);
1831
1832 adapter->fhash.fhead = NULL;
1833 adapter->fhash.fmax = 0;
1834}
1835
1836static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001837 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001838{
1839 struct cmd_desc_type0 *hwdesc;
1840 struct qlcnic_nic_req *req;
1841 struct qlcnic_mac_req *mac_req;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001842 struct qlcnic_vlan_req *vlan_req;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001843 u32 producer;
1844 u64 word;
1845
1846 producer = tx_ring->producer;
1847 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1848
1849 req = (struct qlcnic_nic_req *)hwdesc;
1850 memset(req, 0, sizeof(struct qlcnic_nic_req));
1851 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1852
1853 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1854 req->req_hdr = cpu_to_le64(word);
1855
1856 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001857 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001858 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1859
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001860 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1861 vlan_req->vlan_id = vlan_id;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001862
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001863 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001864 smp_mb();
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001865}
1866
1867#define QLCNIC_MAC_HASH(MAC)\
1868 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1869
1870static void
1871qlcnic_send_filter(struct qlcnic_adapter *adapter,
1872 struct qlcnic_host_tx_ring *tx_ring,
1873 struct cmd_desc_type0 *first_desc,
1874 struct sk_buff *skb)
1875{
1876 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1877 struct qlcnic_filter *fil, *tmp_fil;
1878 struct hlist_node *tmp_hnode, *n;
1879 struct hlist_head *head;
1880 u64 src_addr = 0;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001881 __le16 vlan_id = 0;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001882 u8 hindex;
1883
1884 if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1885 return;
1886
1887 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1888 return;
1889
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001890 /* Only NPAR capable devices support vlan based learning*/
1891 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1892 vlan_id = first_desc->vlan_TCI;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001893 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1894 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1895 head = &(adapter->fhash.fhead[hindex]);
1896
1897 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001898 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1899 tmp_fil->vlan_id == vlan_id) {
amit salechae5edb7b2010-10-26 17:53:07 +00001900
1901 if (jiffies >
1902 (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1903 qlcnic_change_filter(adapter, src_addr, vlan_id,
1904 tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001905 tmp_fil->ftime = jiffies;
1906 return;
1907 }
1908 }
1909
1910 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1911 if (!fil)
1912 return;
1913
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001914 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001915
1916 fil->ftime = jiffies;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001917 fil->vlan_id = vlan_id;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001918 memcpy(fil->faddr, &src_addr, ETH_ALEN);
1919 spin_lock(&adapter->mac_learn_lock);
1920 hlist_add_head(&(fil->fnode), head);
1921 adapter->fhash.fnum++;
1922 spin_unlock(&adapter->mac_learn_lock);
1923}
1924
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001925static int
1926qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001927 struct cmd_desc_type0 *first_desc,
1928 struct sk_buff *skb)
1929{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001930 u8 opcode = 0, hdr_len = 0;
1931 u16 flags = 0, vlan_tci = 0;
1932 int copied, offset, copy_len;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001933 struct cmd_desc_type0 *hwdesc;
1934 struct vlan_ethhdr *vh;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001935 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1936 u16 protocol = ntohs(skb->protocol);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001937 u32 producer = tx_ring->producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001938
1939 if (protocol == ETH_P_8021Q) {
1940 vh = (struct vlan_ethhdr *)skb->data;
1941 flags = FLAGS_VLAN_TAGGED;
1942 vlan_tci = vh->h_vlan_TCI;
1943 } else if (vlan_tx_tag_present(skb)) {
1944 flags = FLAGS_VLAN_OOB;
1945 vlan_tci = vlan_tx_tag_get(skb);
1946 }
1947 if (unlikely(adapter->pvid)) {
1948 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
1949 return -EIO;
1950 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
1951 goto set_flags;
1952
1953 flags = FLAGS_VLAN_OOB;
1954 vlan_tci = adapter->pvid;
1955 }
1956set_flags:
1957 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
1958 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001959
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001960 if (*(skb->data) & BIT_0) {
1961 flags |= BIT_0;
1962 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
1963 }
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001964 opcode = TX_ETHER_PKT;
1965 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001966 skb_shinfo(skb)->gso_size > 0) {
1967
1968 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1969
1970 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1971 first_desc->total_hdr_length = hdr_len;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001972
1973 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
1974
1975 /* For LSO, we need to copy the MAC/IP/TCP headers into
1976 * the descriptor ring */
1977 copied = 0;
1978 offset = 2;
1979
1980 if (flags & FLAGS_VLAN_OOB) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001981 first_desc->total_hdr_length += VLAN_HLEN;
1982 first_desc->tcp_hdr_offset = VLAN_HLEN;
1983 first_desc->ip_hdr_offset = VLAN_HLEN;
1984 /* Only in case of TSO on vlan device */
1985 flags |= FLAGS_VLAN_TAGGED;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001986
1987 /* Create a TSO vlan header template for firmware */
1988
1989 hwdesc = &tx_ring->desc_head[producer];
1990 tx_ring->cmd_buf_arr[producer].skb = NULL;
1991
1992 copy_len = min((int)sizeof(struct cmd_desc_type0) -
1993 offset, hdr_len + VLAN_HLEN);
1994
1995 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
1996 skb_copy_from_linear_data(skb, vh, 12);
1997 vh->h_vlan_proto = htons(ETH_P_8021Q);
1998 vh->h_vlan_TCI = htons(vlan_tci);
1999
2000 skb_copy_from_linear_data_offset(skb, 12,
2001 (char *)vh + 16, copy_len - 16);
2002
2003 copied = copy_len - VLAN_HLEN;
2004 offset = 0;
2005
2006 producer = get_next_index(producer, tx_ring->num_desc);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002007 }
2008
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002009 while (copied < hdr_len) {
2010
2011 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2012 offset, (hdr_len - copied));
2013
2014 hwdesc = &tx_ring->desc_head[producer];
2015 tx_ring->cmd_buf_arr[producer].skb = NULL;
2016
2017 skb_copy_from_linear_data_offset(skb, copied,
2018 (char *) hwdesc + offset, copy_len);
2019
2020 copied += copy_len;
2021 offset = 0;
2022
2023 producer = get_next_index(producer, tx_ring->num_desc);
2024 }
2025
2026 tx_ring->producer = producer;
2027 smp_mb();
2028 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002029
2030 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2031 u8 l4proto;
2032
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002033 if (protocol == ETH_P_IP) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002034 l4proto = ip_hdr(skb)->protocol;
2035
2036 if (l4proto == IPPROTO_TCP)
2037 opcode = TX_TCP_PKT;
2038 else if (l4proto == IPPROTO_UDP)
2039 opcode = TX_UDP_PKT;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002040 } else if (protocol == ETH_P_IPV6) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002041 l4proto = ipv6_hdr(skb)->nexthdr;
2042
2043 if (l4proto == IPPROTO_TCP)
2044 opcode = TX_TCPV6_PKT;
2045 else if (l4proto == IPPROTO_UDP)
2046 opcode = TX_UDPV6_PKT;
2047 }
2048 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002049 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2050 first_desc->ip_hdr_offset += skb_network_offset(skb);
2051 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2052
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002053 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002054}
2055
2056static int
2057qlcnic_map_tx_skb(struct pci_dev *pdev,
2058 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2059{
2060 struct qlcnic_skb_frag *nf;
2061 struct skb_frag_struct *frag;
2062 int i, nr_frags;
2063 dma_addr_t map;
2064
2065 nr_frags = skb_shinfo(skb)->nr_frags;
2066 nf = &pbuf->frag_array[0];
2067
2068 map = pci_map_single(pdev, skb->data,
2069 skb_headlen(skb), PCI_DMA_TODEVICE);
2070 if (pci_dma_mapping_error(pdev, map))
2071 goto out_err;
2072
2073 nf->dma = map;
2074 nf->length = skb_headlen(skb);
2075
2076 for (i = 0; i < nr_frags; i++) {
2077 frag = &skb_shinfo(skb)->frags[i];
2078 nf = &pbuf->frag_array[i+1];
2079
2080 map = pci_map_page(pdev, frag->page, frag->page_offset,
2081 frag->size, PCI_DMA_TODEVICE);
2082 if (pci_dma_mapping_error(pdev, map))
2083 goto unwind;
2084
2085 nf->dma = map;
2086 nf->length = frag->size;
2087 }
2088
2089 return 0;
2090
2091unwind:
2092 while (--i >= 0) {
2093 nf = &pbuf->frag_array[i+1];
2094 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2095 }
2096
2097 nf = &pbuf->frag_array[0];
2098 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2099
2100out_err:
2101 return -ENOMEM;
2102}
2103
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002104static void
2105qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2106 struct qlcnic_cmd_buffer *pbuf)
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002107{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002108 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2109 int nr_frags = skb_shinfo(skb)->nr_frags;
2110 int i;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002111
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002112 for (i = 0; i < nr_frags; i++) {
2113 nf = &pbuf->frag_array[i+1];
2114 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002115 }
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002116
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002117 nf = &pbuf->frag_array[0];
2118 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002119}
2120
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002121static inline void
2122qlcnic_clear_cmddesc(u64 *desc)
2123{
2124 desc[0] = 0ULL;
2125 desc[2] = 0ULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002126 desc[7] = 0ULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002127}
2128
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00002129netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002130qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2131{
2132 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2133 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2134 struct qlcnic_cmd_buffer *pbuf;
2135 struct qlcnic_skb_frag *buffrag;
2136 struct cmd_desc_type0 *hwdesc, *first_desc;
2137 struct pci_dev *pdev;
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002138 struct ethhdr *phdr;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002139 int i, k;
2140
2141 u32 producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002142 int frag_count;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002143 u32 num_txd = tx_ring->num_desc;
2144
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00002145 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2146 netif_stop_queue(netdev);
2147 return NETDEV_TX_BUSY;
2148 }
2149
Sony Chackofe4d4342010-08-19 05:08:27 +00002150 if (adapter->flags & QLCNIC_MACSPOOF) {
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002151 phdr = (struct ethhdr *)skb->data;
2152 if (compare_ether_addr(phdr->h_source,
Sony Chackofe4d4342010-08-19 05:08:27 +00002153 adapter->mac_addr))
2154 goto drop_packet;
2155 }
2156
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002157 frag_count = skb_shinfo(skb)->nr_frags + 1;
2158
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002159 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002160 netif_stop_queue(netdev);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002161 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2162 netif_start_queue(netdev);
2163 else {
2164 adapter->stats.xmit_off++;
2165 return NETDEV_TX_BUSY;
2166 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002167 }
2168
2169 producer = tx_ring->producer;
2170 pbuf = &tx_ring->cmd_buf_arr[producer];
2171
2172 pdev = adapter->pdev;
2173
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002174 first_desc = hwdesc = &tx_ring->desc_head[producer];
2175 qlcnic_clear_cmddesc((u64 *)hwdesc);
2176
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002177 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2178 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002179 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002180 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002181
2182 pbuf->skb = skb;
2183 pbuf->frag_count = frag_count;
2184
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002185 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2186 qlcnic_set_tx_port(first_desc, adapter->portnum);
2187
2188 for (i = 0; i < frag_count; i++) {
2189
2190 k = i % 4;
2191
2192 if ((k == 0) && (i > 0)) {
2193 /* move to next desc.*/
2194 producer = get_next_index(producer, num_txd);
2195 hwdesc = &tx_ring->desc_head[producer];
2196 qlcnic_clear_cmddesc((u64 *)hwdesc);
2197 tx_ring->cmd_buf_arr[producer].skb = NULL;
2198 }
2199
2200 buffrag = &pbuf->frag_array[i];
2201
2202 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2203 switch (k) {
2204 case 0:
2205 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2206 break;
2207 case 1:
2208 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2209 break;
2210 case 2:
2211 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2212 break;
2213 case 3:
2214 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2215 break;
2216 }
2217 }
2218
2219 tx_ring->producer = get_next_index(producer, num_txd);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002220 smp_mb();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002221
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002222 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2223 goto unwind_buff;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002224
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002225 if (qlcnic_mac_learn)
2226 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2227
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002228 qlcnic_update_cmd_producer(adapter, tx_ring);
2229
2230 adapter->stats.txbytes += skb->len;
2231 adapter->stats.xmitcalled++;
2232
2233 return NETDEV_TX_OK;
2234
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002235unwind_buff:
2236 qlcnic_unmap_buffers(pdev, skb, pbuf);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002237drop_packet:
2238 adapter->stats.txdropped++;
2239 dev_kfree_skb_any(skb);
2240 return NETDEV_TX_OK;
2241}
2242
2243static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2244{
2245 struct net_device *netdev = adapter->netdev;
2246 u32 temp, temp_state, temp_val;
2247 int rv = 0;
2248
2249 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2250
2251 temp_state = qlcnic_get_temp_state(temp);
2252 temp_val = qlcnic_get_temp_val(temp);
2253
2254 if (temp_state == QLCNIC_TEMP_PANIC) {
2255 dev_err(&netdev->dev,
2256 "Device temperature %d degrees C exceeds"
2257 " maximum allowed. Hardware has been shut down.\n",
2258 temp_val);
2259 rv = 1;
2260 } else if (temp_state == QLCNIC_TEMP_WARN) {
2261 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2262 dev_err(&netdev->dev,
2263 "Device temperature %d degrees C "
2264 "exceeds operating range."
2265 " Immediate action needed.\n",
2266 temp_val);
2267 }
2268 } else {
2269 if (adapter->temp == QLCNIC_TEMP_WARN) {
2270 dev_info(&netdev->dev,
2271 "Device temperature is now %d degrees C"
2272 " in normal range.\n", temp_val);
2273 }
2274 }
2275 adapter->temp = temp_state;
2276 return rv;
2277}
2278
2279void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2280{
2281 struct net_device *netdev = adapter->netdev;
2282
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002283 if (adapter->ahw->linkup && !linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002284 netdev_info(netdev, "NIC Link is down\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002285 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002286 if (netif_running(netdev)) {
2287 netif_carrier_off(netdev);
2288 netif_stop_queue(netdev);
2289 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002290 } else if (!adapter->ahw->linkup && linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002291 netdev_info(netdev, "NIC Link is up\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002292 adapter->ahw->linkup = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002293 if (netif_running(netdev)) {
2294 netif_carrier_on(netdev);
2295 netif_wake_queue(netdev);
2296 }
2297 }
2298}
2299
2300static void qlcnic_tx_timeout(struct net_device *netdev)
2301{
2302 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2303
2304 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2305 return;
2306
2307 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002308
2309 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002310 adapter->need_fw_reset = 1;
2311 else
2312 adapter->reset_context = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002313}
2314
2315static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2316{
2317 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2318 struct net_device_stats *stats = &netdev->stats;
2319
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002320 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2321 stats->tx_packets = adapter->stats.xmitfinished;
Sucheta Chakraborty7e382592010-05-17 01:22:10 +00002322 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002323 stats->tx_bytes = adapter->stats.txbytes;
2324 stats->rx_dropped = adapter->stats.rxdropped;
2325 stats->tx_dropped = adapter->stats.txdropped;
2326
2327 return stats;
2328}
2329
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002330static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002331{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002332 u32 status;
2333
2334 status = readl(adapter->isr_int_vec);
2335
2336 if (!(status & adapter->int_vec_bit))
2337 return IRQ_NONE;
2338
2339 /* check interrupt state machine, to be sure */
2340 status = readl(adapter->crb_int_state_reg);
2341 if (!ISR_LEGACY_INT_TRIGGERED(status))
2342 return IRQ_NONE;
2343
2344 writel(0xffffffff, adapter->tgt_status_reg);
2345 /* read twice to ensure write is flushed */
2346 readl(adapter->isr_int_vec);
2347 readl(adapter->isr_int_vec);
2348
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002349 return IRQ_HANDLED;
2350}
2351
2352static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2353{
2354 struct qlcnic_host_sds_ring *sds_ring = data;
2355 struct qlcnic_adapter *adapter = sds_ring->adapter;
2356
2357 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2358 goto done;
2359 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2360 writel(0xffffffff, adapter->tgt_status_reg);
2361 goto done;
2362 }
2363
2364 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2365 return IRQ_NONE;
2366
2367done:
2368 adapter->diag_cnt++;
2369 qlcnic_enable_int(sds_ring);
2370 return IRQ_HANDLED;
2371}
2372
2373static irqreturn_t qlcnic_intr(int irq, void *data)
2374{
2375 struct qlcnic_host_sds_ring *sds_ring = data;
2376 struct qlcnic_adapter *adapter = sds_ring->adapter;
2377
2378 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2379 return IRQ_NONE;
2380
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002381 napi_schedule(&sds_ring->napi);
2382
2383 return IRQ_HANDLED;
2384}
2385
2386static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2387{
2388 struct qlcnic_host_sds_ring *sds_ring = data;
2389 struct qlcnic_adapter *adapter = sds_ring->adapter;
2390
2391 /* clear interrupt */
2392 writel(0xffffffff, adapter->tgt_status_reg);
2393
2394 napi_schedule(&sds_ring->napi);
2395 return IRQ_HANDLED;
2396}
2397
2398static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2399{
2400 struct qlcnic_host_sds_ring *sds_ring = data;
2401
2402 napi_schedule(&sds_ring->napi);
2403 return IRQ_HANDLED;
2404}
2405
2406static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2407{
2408 u32 sw_consumer, hw_consumer;
2409 int count = 0, i;
2410 struct qlcnic_cmd_buffer *buffer;
2411 struct pci_dev *pdev = adapter->pdev;
2412 struct net_device *netdev = adapter->netdev;
2413 struct qlcnic_skb_frag *frag;
2414 int done;
2415 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2416
2417 if (!spin_trylock(&adapter->tx_clean_lock))
2418 return 1;
2419
2420 sw_consumer = tx_ring->sw_consumer;
2421 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2422
2423 while (sw_consumer != hw_consumer) {
2424 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2425 if (buffer->skb) {
2426 frag = &buffer->frag_array[0];
2427 pci_unmap_single(pdev, frag->dma, frag->length,
2428 PCI_DMA_TODEVICE);
2429 frag->dma = 0ULL;
2430 for (i = 1; i < buffer->frag_count; i++) {
2431 frag++;
2432 pci_unmap_page(pdev, frag->dma, frag->length,
2433 PCI_DMA_TODEVICE);
2434 frag->dma = 0ULL;
2435 }
2436
2437 adapter->stats.xmitfinished++;
2438 dev_kfree_skb_any(buffer->skb);
2439 buffer->skb = NULL;
2440 }
2441
2442 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2443 if (++count >= MAX_STATUS_HANDLE)
2444 break;
2445 }
2446
2447 if (count && netif_running(netdev)) {
2448 tx_ring->sw_consumer = sw_consumer;
2449
2450 smp_mb();
2451
2452 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002453 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2454 netif_wake_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00002455 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002456 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002457 }
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002458 adapter->tx_timeo_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002459 }
2460 /*
2461 * If everything is freed up to consumer then check if the ring is full
2462 * If the ring is full then check if more needs to be freed and
2463 * schedule the call back again.
2464 *
2465 * This happens when there are 2 CPUs. One could be freeing and the
2466 * other filling it. If the ring is full when we get out of here and
2467 * the card has already interrupted the host then the host can miss the
2468 * interrupt.
2469 *
2470 * There is still a possible race condition and the host could miss an
2471 * interrupt. The card has to take care of this.
2472 */
2473 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2474 done = (sw_consumer == hw_consumer);
2475 spin_unlock(&adapter->tx_clean_lock);
2476
2477 return done;
2478}
2479
2480static int qlcnic_poll(struct napi_struct *napi, int budget)
2481{
2482 struct qlcnic_host_sds_ring *sds_ring =
2483 container_of(napi, struct qlcnic_host_sds_ring, napi);
2484
2485 struct qlcnic_adapter *adapter = sds_ring->adapter;
2486
2487 int tx_complete;
2488 int work_done;
2489
2490 tx_complete = qlcnic_process_cmd_ring(adapter);
2491
2492 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2493
2494 if ((work_done < budget) && tx_complete) {
2495 napi_complete(&sds_ring->napi);
2496 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2497 qlcnic_enable_int(sds_ring);
2498 }
2499
2500 return work_done;
2501}
2502
schacko8f891382010-06-17 02:56:40 +00002503static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2504{
2505 struct qlcnic_host_sds_ring *sds_ring =
2506 container_of(napi, struct qlcnic_host_sds_ring, napi);
2507
2508 struct qlcnic_adapter *adapter = sds_ring->adapter;
2509 int work_done;
2510
2511 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2512
2513 if (work_done < budget) {
2514 napi_complete(&sds_ring->napi);
2515 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2516 qlcnic_enable_int(sds_ring);
2517 }
2518
2519 return work_done;
2520}
2521
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002522#ifdef CONFIG_NET_POLL_CONTROLLER
2523static void qlcnic_poll_controller(struct net_device *netdev)
2524{
Yinglin Luanbf827912010-08-22 21:57:56 +00002525 int ring;
2526 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002527 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002528 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Yinglin Luanbf827912010-08-22 21:57:56 +00002529
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002530 disable_irq(adapter->irq);
Yinglin Luanbf827912010-08-22 21:57:56 +00002531 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2532 sds_ring = &recv_ctx->sds_rings[ring];
2533 qlcnic_intr(adapter->irq, sds_ring);
2534 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002535 enable_irq(adapter->irq);
2536}
2537#endif
2538
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002539static void
2540qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2541{
2542 u32 val;
2543
2544 val = adapter->portnum & 0xf;
2545 val |= encoding << 7;
2546 val |= (jiffies - adapter->dev_rst_time) << 8;
2547
2548 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2549 adapter->dev_rst_time = jiffies;
2550}
2551
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002552static int
2553qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002554{
2555 u32 val;
2556
2557 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2558 state != QLCNIC_DEV_NEED_QUISCENT);
2559
2560 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002561 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002562
2563 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2564
2565 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002566 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002567 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002568 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002569
2570 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2571
2572 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002573
2574 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002575}
2576
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002577static int
2578qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2579{
2580 u32 val;
2581
2582 if (qlcnic_api_lock(adapter))
2583 return -EBUSY;
2584
2585 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002586 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002587 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2588
2589 qlcnic_api_unlock(adapter);
2590
2591 return 0;
2592}
2593
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002594static void
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002595qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002596{
2597 u32 val;
2598
2599 if (qlcnic_api_lock(adapter))
2600 goto err;
2601
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002602 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002603 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002604 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002605
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002606 if (failed) {
2607 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2608 dev_info(&adapter->pdev->dev,
2609 "Device state set to Failed. Please Reboot\n");
2610 } else if (!(val & 0x11111111))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002611 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2612
2613 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002614 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002615 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2616
2617 qlcnic_api_unlock(adapter);
2618err:
2619 adapter->fw_fail_cnt = 0;
2620 clear_bit(__QLCNIC_START_FW, &adapter->state);
2621 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2622}
2623
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002624/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002625static int
2626qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2627{
2628 int act, state;
2629
2630 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002631 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002632
2633 if (((state & 0x11111111) == (act & 0x11111111)) ||
2634 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2635 return 0;
2636 else
2637 return 1;
2638}
2639
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002640static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2641{
2642 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2643
2644 if (val != QLCNIC_DRV_IDC_VER) {
2645 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2646 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2647 }
2648
2649 return 0;
2650}
2651
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002652static int
2653qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2654{
2655 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002656 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002657 u8 portnum = adapter->portnum;
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002658 u8 ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002659
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002660 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2661 return 1;
2662
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002663 if (qlcnic_api_lock(adapter))
2664 return -1;
2665
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002666 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002667 if (!(val & (1 << (portnum * 4)))) {
2668 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002669 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002670 }
2671
2672 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002673 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002674
2675 switch (prev_state) {
2676 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002677 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002678 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002679 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002680 qlcnic_api_unlock(adapter);
2681 return 1;
2682
2683 case QLCNIC_DEV_READY:
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002684 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002685 qlcnic_api_unlock(adapter);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002686 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002687
2688 case QLCNIC_DEV_NEED_RESET:
2689 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002690 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002691 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2692 break;
2693
2694 case QLCNIC_DEV_NEED_QUISCENT:
2695 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002696 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002697 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2698 break;
2699
2700 case QLCNIC_DEV_FAILED:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00002701 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002702 qlcnic_api_unlock(adapter);
2703 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002704
2705 case QLCNIC_DEV_INITIALIZING:
2706 case QLCNIC_DEV_QUISCENT:
2707 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002708 }
2709
2710 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002711
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002712 do {
2713 msleep(1000);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002714 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2715
2716 if (prev_state == QLCNIC_DEV_QUISCENT)
2717 continue;
2718 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002719
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002720 if (!dev_init_timeo) {
2721 dev_err(&adapter->pdev->dev,
2722 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002723 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002724 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002725
2726 if (qlcnic_api_lock(adapter))
2727 return -1;
2728
2729 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002730 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002731 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2732
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002733 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002734 qlcnic_api_unlock(adapter);
2735
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002736 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002737}
2738
2739static void
2740qlcnic_fwinit_work(struct work_struct *work)
2741{
2742 struct qlcnic_adapter *adapter = container_of(work,
2743 struct qlcnic_adapter, fw_work.work);
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002744 u32 dev_state = 0xf;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002745
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002746 if (qlcnic_api_lock(adapter))
2747 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002748
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002749 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002750 if (dev_state == QLCNIC_DEV_QUISCENT ||
2751 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002752 qlcnic_api_unlock(adapter);
2753 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2754 FW_POLL_DELAY * 2);
2755 return;
2756 }
2757
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002758 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002759 qlcnic_api_unlock(adapter);
2760 goto wait_npar;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002761 }
2762
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002763 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2764 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2765 adapter->reset_ack_timeo);
2766 goto skip_ack_check;
2767 }
2768
2769 if (!qlcnic_check_drv_state(adapter)) {
2770skip_ack_check:
2771 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002772
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002773 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2774 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2775 QLCNIC_DEV_INITIALIZING);
2776 set_bit(__QLCNIC_START_FW, &adapter->state);
2777 QLCDB(adapter, DRV, "Restarting fw\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002778 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002779 }
2780
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002781 qlcnic_api_unlock(adapter);
2782
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002783 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002784 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002785 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002786 return;
2787 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002788 goto err_ret;
2789 }
2790
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002791 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002792
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002793wait_npar:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002794 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002795 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002796
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002797 switch (dev_state) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002798 case QLCNIC_DEV_READY:
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002799 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002800 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002801 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002802 return;
2803 }
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002804 case QLCNIC_DEV_FAILED:
2805 break;
2806 default:
2807 qlcnic_schedule_work(adapter,
2808 qlcnic_fwinit_work, FW_POLL_DELAY);
2809 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002810 }
2811
2812err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002813 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2814 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002815 netif_device_attach(adapter->netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002816 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002817}
2818
2819static void
2820qlcnic_detach_work(struct work_struct *work)
2821{
2822 struct qlcnic_adapter *adapter = container_of(work,
2823 struct qlcnic_adapter, fw_work.work);
2824 struct net_device *netdev = adapter->netdev;
2825 u32 status;
2826
2827 netif_device_detach(netdev);
2828
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002829 /* Dont grab rtnl lock during Quiscent mode */
2830 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2831 if (netif_running(netdev))
2832 __qlcnic_down(adapter, netdev);
2833 } else
2834 qlcnic_down(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002835
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002836 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2837
2838 if (status & QLCNIC_RCODE_FATAL_ERROR)
2839 goto err_ret;
2840
2841 if (adapter->temp == QLCNIC_TEMP_PANIC)
2842 goto err_ret;
2843
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002844 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2845 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002846
2847 adapter->fw_wait_cnt = 0;
2848
2849 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2850
2851 return;
2852
2853err_ret:
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002854 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2855 status, adapter->temp);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002856 netif_device_attach(netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002857 qlcnic_clr_all_drv_state(adapter, 1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002858}
2859
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002860/*Transit NPAR state to NON Operational */
2861static void
2862qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2863{
2864 u32 state;
2865
2866 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2867 if (state == QLCNIC_DEV_NPAR_NON_OPER)
2868 return;
2869
2870 if (qlcnic_api_lock(adapter))
2871 return;
2872 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2873 qlcnic_api_unlock(adapter);
2874}
2875
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002876/*Transit to RESET state from READY state only */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002877static void
2878qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2879{
2880 u32 state;
2881
Anirban Chakrabortycea89752010-07-13 20:33:35 +00002882 adapter->need_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002883 if (qlcnic_api_lock(adapter))
2884 return;
2885
2886 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2887
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002888 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002889 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002890 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002891 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002892 }
2893
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002894 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002895 qlcnic_api_unlock(adapter);
2896}
2897
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002898/* Transit to NPAR READY state from NPAR NOT READY state */
2899static void
2900qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2901{
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002902 if (qlcnic_api_lock(adapter))
2903 return;
2904
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002905 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
2906 QLCDB(adapter, DRV, "NPAR operational state set\n");
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002907
2908 qlcnic_api_unlock(adapter);
2909}
2910
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002911static void
2912qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2913 work_func_t func, int delay)
2914{
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00002915 if (test_bit(__QLCNIC_AER, &adapter->state))
2916 return;
2917
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002918 INIT_DELAYED_WORK(&adapter->fw_work, func);
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00002919 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
2920 round_jiffies_relative(delay));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002921}
2922
2923static void
2924qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2925{
2926 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2927 msleep(10);
2928
2929 cancel_delayed_work_sync(&adapter->fw_work);
2930}
2931
2932static void
2933qlcnic_attach_work(struct work_struct *work)
2934{
2935 struct qlcnic_adapter *adapter = container_of(work,
2936 struct qlcnic_adapter, fw_work.work);
2937 struct net_device *netdev = adapter->netdev;
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002938 u32 npar_state;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002939
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002940 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
2941 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2942 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
2943 qlcnic_clr_all_drv_state(adapter, 0);
2944 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
2945 qlcnic_schedule_work(adapter, qlcnic_attach_work,
2946 FW_POLL_DELAY);
2947 else
2948 goto attach;
2949 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
2950 return;
2951 }
2952attach:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002953 if (netif_running(netdev)) {
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00002954 if (qlcnic_up(adapter, netdev))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002955 goto done;
2956
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00002957 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002958 }
2959
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002960done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002961 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002962 adapter->fw_fail_cnt = 0;
2963 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002964
2965 if (!qlcnic_clr_drv_state(adapter))
2966 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2967 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002968}
2969
2970static int
2971qlcnic_check_health(struct qlcnic_adapter *adapter)
2972{
Sony Chacko4e708122010-08-31 17:17:44 +00002973 u32 state = 0, heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002974 struct net_device *netdev = adapter->netdev;
2975
2976 if (qlcnic_check_temp(adapter))
2977 goto detach;
2978
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00002979 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002980 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002981
2982 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002983 if (state == QLCNIC_DEV_NEED_RESET) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002984 qlcnic_set_npar_non_operational(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002985 adapter->need_fw_reset = 1;
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002986 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
2987 goto detach;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002988
Sony Chacko4e708122010-08-31 17:17:44 +00002989 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
2990 if (heartbeat != adapter->heartbeat) {
2991 adapter->heartbeat = heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002992 adapter->fw_fail_cnt = 0;
2993 if (adapter->need_fw_reset)
2994 goto detach;
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002995
amit salecha9ce13ca2011-02-23 03:21:24 +00002996 if (adapter->reset_context && auto_fw_reset) {
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002997 qlcnic_reset_hw_context(adapter);
2998 adapter->netdev->trans_start = jiffies;
2999 }
3000
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003001 return 0;
3002 }
3003
3004 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3005 return 0;
3006
3007 qlcnic_dev_request_reset(adapter);
3008
amit salecha9ce13ca2011-02-23 03:21:24 +00003009 if (auto_fw_reset)
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00003010 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003011
3012 dev_info(&netdev->dev, "firmware hang detected\n");
3013
3014detach:
3015 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3016 QLCNIC_DEV_NEED_RESET;
3017
amit salecha9ce13ca2011-02-23 03:21:24 +00003018 if (auto_fw_reset &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003019 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3020
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003021 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003022 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3023 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003024
3025 return 1;
3026}
3027
3028static void
3029qlcnic_fw_poll_work(struct work_struct *work)
3030{
3031 struct qlcnic_adapter *adapter = container_of(work,
3032 struct qlcnic_adapter, fw_work.work);
3033
3034 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3035 goto reschedule;
3036
3037
3038 if (qlcnic_check_health(adapter))
3039 return;
3040
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00003041 if (adapter->fhash.fnum)
3042 qlcnic_prune_lb_filters(adapter);
3043
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003044reschedule:
3045 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3046}
3047
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003048static int qlcnic_is_first_func(struct pci_dev *pdev)
3049{
3050 struct pci_dev *oth_pdev;
3051 int val = pdev->devfn;
3052
3053 while (val-- > 0) {
3054 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3055 (pdev->bus), pdev->bus->number,
3056 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003057 if (!oth_pdev)
3058 continue;
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003059
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003060 if (oth_pdev->current_state != PCI_D3cold) {
3061 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003062 return 0;
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003063 }
3064 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003065 }
3066 return 1;
3067}
3068
3069static int qlcnic_attach_func(struct pci_dev *pdev)
3070{
3071 int err, first_func;
3072 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3073 struct net_device *netdev = adapter->netdev;
3074
3075 pdev->error_state = pci_channel_io_normal;
3076
3077 err = pci_enable_device(pdev);
3078 if (err)
3079 return err;
3080
3081 pci_set_power_state(pdev, PCI_D0);
3082 pci_set_master(pdev);
3083 pci_restore_state(pdev);
3084
3085 first_func = qlcnic_is_first_func(pdev);
3086
3087 if (qlcnic_api_lock(adapter))
3088 return -EINVAL;
3089
Amit Kumar Salecha933fce122010-08-17 00:34:19 +00003090 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003091 adapter->need_fw_reset = 1;
3092 set_bit(__QLCNIC_START_FW, &adapter->state);
3093 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3094 QLCDB(adapter, DRV, "Restarting fw\n");
3095 }
3096 qlcnic_api_unlock(adapter);
3097
3098 err = adapter->nic_ops->start_firmware(adapter);
3099 if (err)
3100 return err;
3101
3102 qlcnic_clr_drv_state(adapter);
3103 qlcnic_setup_intr(adapter);
3104
3105 if (netif_running(netdev)) {
3106 err = qlcnic_attach(adapter);
3107 if (err) {
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00003108 qlcnic_clr_all_drv_state(adapter, 1);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003109 clear_bit(__QLCNIC_AER, &adapter->state);
3110 netif_device_attach(netdev);
3111 return err;
3112 }
3113
3114 err = qlcnic_up(adapter, netdev);
3115 if (err)
3116 goto done;
3117
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003118 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003119 }
3120 done:
3121 netif_device_attach(netdev);
3122 return err;
3123}
3124
3125static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3126 pci_channel_state_t state)
3127{
3128 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3129 struct net_device *netdev = adapter->netdev;
3130
3131 if (state == pci_channel_io_perm_failure)
3132 return PCI_ERS_RESULT_DISCONNECT;
3133
3134 if (state == pci_channel_io_normal)
3135 return PCI_ERS_RESULT_RECOVERED;
3136
3137 set_bit(__QLCNIC_AER, &adapter->state);
3138 netif_device_detach(netdev);
3139
3140 cancel_delayed_work_sync(&adapter->fw_work);
3141
3142 if (netif_running(netdev))
3143 qlcnic_down(adapter, netdev);
3144
3145 qlcnic_detach(adapter);
3146 qlcnic_teardown_intr(adapter);
3147
3148 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3149
3150 pci_save_state(pdev);
3151 pci_disable_device(pdev);
3152
3153 return PCI_ERS_RESULT_NEED_RESET;
3154}
3155
3156static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3157{
3158 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3159 PCI_ERS_RESULT_RECOVERED;
3160}
3161
3162static void qlcnic_io_resume(struct pci_dev *pdev)
3163{
3164 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3165
3166 pci_cleanup_aer_uncorrect_error_status(pdev);
3167
3168 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3169 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3170 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3171 FW_POLL_DELAY);
3172}
3173
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003174static int
3175qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3176{
3177 int err;
3178
3179 err = qlcnic_can_start_firmware(adapter);
3180 if (err)
3181 return err;
3182
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +00003183 err = qlcnic_check_npar_opertional(adapter);
3184 if (err)
3185 return err;
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003186
Rajesh Borundia174240a2010-08-31 17:17:47 +00003187 err = qlcnic_initialize_nic(adapter);
3188 if (err)
3189 return err;
3190
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003191 qlcnic_check_options(adapter);
3192
Rajesh Borundia73733732010-08-31 17:17:50 +00003193 err = qlcnic_set_eswitch_port_config(adapter);
3194 if (err)
3195 return err;
3196
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003197 adapter->need_fw_reset = 0;
3198
3199 return err;
3200}
3201
3202static int
3203qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3204{
3205 return -EOPNOTSUPP;
3206}
3207
3208static int
3209qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3210{
3211 return -EOPNOTSUPP;
3212}
3213
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003214static ssize_t
3215qlcnic_store_bridged_mode(struct device *dev,
3216 struct device_attribute *attr, const char *buf, size_t len)
3217{
3218 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3219 unsigned long new;
3220 int ret = -EINVAL;
3221
3222 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3223 goto err_out;
3224
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00003225 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003226 goto err_out;
3227
3228 if (strict_strtoul(buf, 2, &new))
3229 goto err_out;
3230
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00003231 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003232 ret = len;
3233
3234err_out:
3235 return ret;
3236}
3237
3238static ssize_t
3239qlcnic_show_bridged_mode(struct device *dev,
3240 struct device_attribute *attr, char *buf)
3241{
3242 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3243 int bridged_mode = 0;
3244
3245 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3246 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3247
3248 return sprintf(buf, "%d\n", bridged_mode);
3249}
3250
3251static struct device_attribute dev_attr_bridged_mode = {
3252 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3253 .show = qlcnic_show_bridged_mode,
3254 .store = qlcnic_store_bridged_mode,
3255};
3256
3257static ssize_t
3258qlcnic_store_diag_mode(struct device *dev,
3259 struct device_attribute *attr, const char *buf, size_t len)
3260{
3261 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3262 unsigned long new;
3263
3264 if (strict_strtoul(buf, 2, &new))
3265 return -EINVAL;
3266
3267 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3268 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3269
3270 return len;
3271}
3272
3273static ssize_t
3274qlcnic_show_diag_mode(struct device *dev,
3275 struct device_attribute *attr, char *buf)
3276{
3277 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3278
3279 return sprintf(buf, "%d\n",
3280 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3281}
3282
3283static struct device_attribute dev_attr_diag_mode = {
3284 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3285 .show = qlcnic_show_diag_mode,
3286 .store = qlcnic_store_diag_mode,
3287};
3288
3289static int
3290qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3291 loff_t offset, size_t size)
3292{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003293 size_t crb_size = 4;
3294
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003295 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3296 return -EIO;
3297
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003298 if (offset < QLCNIC_PCI_CRBSPACE) {
3299 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3300 QLCNIC_PCI_CAMQM_END))
3301 crb_size = 8;
3302 else
3303 return -EINVAL;
3304 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003305
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003306 if ((size != crb_size) || (offset & (crb_size-1)))
3307 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003308
3309 return 0;
3310}
3311
3312static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003313qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3314 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003315 char *buf, loff_t offset, size_t size)
3316{
3317 struct device *dev = container_of(kobj, struct device, kobj);
3318 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3319 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003320 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003321 int ret;
3322
3323 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3324 if (ret != 0)
3325 return ret;
3326
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003327 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3328 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3329 memcpy(buf, &qmdata, size);
3330 } else {
3331 data = QLCRD32(adapter, offset);
3332 memcpy(buf, &data, size);
3333 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003334 return size;
3335}
3336
3337static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003338qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3339 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003340 char *buf, loff_t offset, size_t size)
3341{
3342 struct device *dev = container_of(kobj, struct device, kobj);
3343 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3344 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003345 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003346 int ret;
3347
3348 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3349 if (ret != 0)
3350 return ret;
3351
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003352 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3353 memcpy(&qmdata, buf, size);
3354 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3355 } else {
3356 memcpy(&data, buf, size);
3357 QLCWR32(adapter, offset, data);
3358 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003359 return size;
3360}
3361
3362static int
3363qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3364 loff_t offset, size_t size)
3365{
3366 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3367 return -EIO;
3368
3369 if ((size != 8) || (offset & 0x7))
3370 return -EIO;
3371
3372 return 0;
3373}
3374
3375static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003376qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3377 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003378 char *buf, loff_t offset, size_t size)
3379{
3380 struct device *dev = container_of(kobj, struct device, kobj);
3381 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3382 u64 data;
3383 int ret;
3384
3385 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3386 if (ret != 0)
3387 return ret;
3388
3389 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3390 return -EIO;
3391
3392 memcpy(buf, &data, size);
3393
3394 return size;
3395}
3396
3397static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003398qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3399 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003400 char *buf, loff_t offset, size_t size)
3401{
3402 struct device *dev = container_of(kobj, struct device, kobj);
3403 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3404 u64 data;
3405 int ret;
3406
3407 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3408 if (ret != 0)
3409 return ret;
3410
3411 memcpy(&data, buf, size);
3412
3413 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3414 return -EIO;
3415
3416 return size;
3417}
3418
3419
3420static struct bin_attribute bin_attr_crb = {
3421 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3422 .size = 0,
3423 .read = qlcnic_sysfs_read_crb,
3424 .write = qlcnic_sysfs_write_crb,
3425};
3426
3427static struct bin_attribute bin_attr_mem = {
3428 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3429 .size = 0,
3430 .read = qlcnic_sysfs_read_mem,
3431 .write = qlcnic_sysfs_write_mem,
3432};
3433
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003434static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003435validate_pm_config(struct qlcnic_adapter *adapter,
3436 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3437{
3438
3439 u8 src_pci_func, s_esw_id, d_esw_id;
3440 u8 dest_pci_func;
3441 int i;
3442
3443 for (i = 0; i < count; i++) {
3444 src_pci_func = pm_cfg[i].pci_func;
3445 dest_pci_func = pm_cfg[i].dest_npar;
3446 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3447 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3448 return QL_STATUS_INVALID_PARAM;
3449
3450 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3451 return QL_STATUS_INVALID_PARAM;
3452
3453 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3454 return QL_STATUS_INVALID_PARAM;
3455
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003456 s_esw_id = adapter->npars[src_pci_func].phy_port;
3457 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3458
3459 if (s_esw_id != d_esw_id)
3460 return QL_STATUS_INVALID_PARAM;
3461
3462 }
3463 return 0;
3464
3465}
3466
3467static ssize_t
3468qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3469 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3470{
3471 struct device *dev = container_of(kobj, struct device, kobj);
3472 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3473 struct qlcnic_pm_func_cfg *pm_cfg;
3474 u32 id, action, pci_func;
3475 int count, rem, i, ret;
3476
3477 count = size / sizeof(struct qlcnic_pm_func_cfg);
3478 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3479 if (rem)
3480 return QL_STATUS_INVALID_PARAM;
3481
3482 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3483
3484 ret = validate_pm_config(adapter, pm_cfg, count);
3485 if (ret)
3486 return ret;
3487 for (i = 0; i < count; i++) {
3488 pci_func = pm_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003489 action = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003490 id = adapter->npars[pci_func].phy_port;
3491 ret = qlcnic_config_port_mirroring(adapter, id,
3492 action, pci_func);
3493 if (ret)
3494 return ret;
3495 }
3496
3497 for (i = 0; i < count; i++) {
3498 pci_func = pm_cfg[i].pci_func;
3499 id = adapter->npars[pci_func].phy_port;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003500 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003501 adapter->npars[pci_func].dest_npar = id;
3502 }
3503 return size;
3504}
3505
3506static ssize_t
3507qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3508 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3509{
3510 struct device *dev = container_of(kobj, struct device, kobj);
3511 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3512 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3513 int i;
3514
3515 if (size != sizeof(pm_cfg))
3516 return QL_STATUS_INVALID_PARAM;
3517
3518 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3519 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3520 continue;
3521 pm_cfg[i].action = adapter->npars[i].enable_pm;
3522 pm_cfg[i].dest_npar = 0;
3523 pm_cfg[i].pci_func = i;
3524 }
3525 memcpy(buf, &pm_cfg, size);
3526
3527 return size;
3528}
3529
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003530static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003531validate_esw_config(struct qlcnic_adapter *adapter,
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003532 struct qlcnic_esw_func_cfg *esw_cfg, int count)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003533{
Rajesh Borundia7613c872010-08-31 17:17:48 +00003534 u32 op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003535 u8 pci_func;
3536 int i;
Rajesh Borundia7613c872010-08-31 17:17:48 +00003537
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003538 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
Rajesh Borundia7613c872010-08-31 17:17:48 +00003539
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003540 for (i = 0; i < count; i++) {
3541 pci_func = esw_cfg[i].pci_func;
3542 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3543 return QL_STATUS_INVALID_PARAM;
3544
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003545 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3546 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003547 return QL_STATUS_INVALID_PARAM;
3548
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003549 switch (esw_cfg[i].op_mode) {
3550 case QLCNIC_PORT_DEFAULTS:
Rajesh Borundia7613c872010-08-31 17:17:48 +00003551 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
Rajesh Borundia73733732010-08-31 17:17:50 +00003552 QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha091056b2010-12-02 20:41:43 +00003553 if (esw_cfg[i].mac_anti_spoof != 0)
3554 return QL_STATUS_INVALID_PARAM;
3555 if (esw_cfg[i].mac_override != 1)
3556 return QL_STATUS_INVALID_PARAM;
3557 if (esw_cfg[i].promisc_mode != 1)
3558 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia73733732010-08-31 17:17:50 +00003559 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003560 break;
3561 case QLCNIC_ADD_VLAN:
3562 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3563 return QL_STATUS_INVALID_PARAM;
3564 if (!esw_cfg[i].op_type)
3565 return QL_STATUS_INVALID_PARAM;
3566 break;
3567 case QLCNIC_DEL_VLAN:
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003568 if (!esw_cfg[i].op_type)
3569 return QL_STATUS_INVALID_PARAM;
3570 break;
3571 default:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003572 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003573 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003574 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003575 return 0;
3576}
3577
3578static ssize_t
3579qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3580 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3581{
3582 struct device *dev = container_of(kobj, struct device, kobj);
3583 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3584 struct qlcnic_esw_func_cfg *esw_cfg;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003585 struct qlcnic_npar_info *npar;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003586 int count, rem, i, ret;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003587 u8 pci_func, op_mode = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003588
3589 count = size / sizeof(struct qlcnic_esw_func_cfg);
3590 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3591 if (rem)
3592 return QL_STATUS_INVALID_PARAM;
3593
3594 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3595 ret = validate_esw_config(adapter, esw_cfg, count);
3596 if (ret)
3597 return ret;
3598
3599 for (i = 0; i < count; i++) {
Rajesh Borundia0325d692010-08-19 05:08:26 +00003600 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3601 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3602 return QL_STATUS_INVALID_PARAM;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003603
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003604 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003605 continue;
3606
3607 op_mode = esw_cfg[i].op_mode;
3608 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3609 esw_cfg[i].op_mode = op_mode;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003610 esw_cfg[i].pci_func = adapter->ahw->pci_func;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003611
3612 switch (esw_cfg[i].op_mode) {
3613 case QLCNIC_PORT_DEFAULTS:
3614 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3615 break;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00003616 case QLCNIC_ADD_VLAN:
3617 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3618 break;
3619 case QLCNIC_DEL_VLAN:
3620 esw_cfg[i].vlan_id = 0;
3621 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3622 break;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003623 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003624 }
3625
Rajesh Borundia0325d692010-08-19 05:08:26 +00003626 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3627 goto out;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003628
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003629 for (i = 0; i < count; i++) {
3630 pci_func = esw_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003631 npar = &adapter->npars[pci_func];
3632 switch (esw_cfg[i].op_mode) {
3633 case QLCNIC_PORT_DEFAULTS:
3634 npar->promisc_mode = esw_cfg[i].promisc_mode;
Rajesh Borundia73733732010-08-31 17:17:50 +00003635 npar->mac_override = esw_cfg[i].mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003636 npar->offload_flags = esw_cfg[i].offload_flags;
3637 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3638 npar->discard_tagged = esw_cfg[i].discard_tagged;
3639 break;
3640 case QLCNIC_ADD_VLAN:
3641 npar->pvid = esw_cfg[i].vlan_id;
3642 break;
3643 case QLCNIC_DEL_VLAN:
3644 npar->pvid = 0;
3645 break;
3646 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003647 }
Rajesh Borundia0325d692010-08-19 05:08:26 +00003648out:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003649 return size;
3650}
3651
3652static ssize_t
3653qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3654 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3655{
3656 struct device *dev = container_of(kobj, struct device, kobj);
3657 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3658 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003659 u8 i;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003660
3661 if (size != sizeof(esw_cfg))
3662 return QL_STATUS_INVALID_PARAM;
3663
3664 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3665 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3666 continue;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003667 esw_cfg[i].pci_func = i;
3668 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
3669 return QL_STATUS_INVALID_PARAM;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003670 }
3671 memcpy(buf, &esw_cfg, size);
3672
3673 return size;
3674}
3675
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003676static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003677validate_npar_config(struct qlcnic_adapter *adapter,
3678 struct qlcnic_npar_func_cfg *np_cfg, int count)
3679{
3680 u8 pci_func, i;
3681
3682 for (i = 0; i < count; i++) {
3683 pci_func = np_cfg[i].pci_func;
3684 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3685 return QL_STATUS_INVALID_PARAM;
3686
3687 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3688 return QL_STATUS_INVALID_PARAM;
3689
Rajesh Borundiad12b0d92011-02-23 03:21:25 +00003690 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
3691 !IS_VALID_BW(np_cfg[i].max_bw))
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003692 return QL_STATUS_INVALID_PARAM;
3693 }
3694 return 0;
3695}
3696
3697static ssize_t
3698qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3699 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3700{
3701 struct device *dev = container_of(kobj, struct device, kobj);
3702 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3703 struct qlcnic_info nic_info;
3704 struct qlcnic_npar_func_cfg *np_cfg;
3705 int i, count, rem, ret;
3706 u8 pci_func;
3707
3708 count = size / sizeof(struct qlcnic_npar_func_cfg);
3709 rem = size % sizeof(struct qlcnic_npar_func_cfg);
3710 if (rem)
3711 return QL_STATUS_INVALID_PARAM;
3712
3713 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
3714 ret = validate_npar_config(adapter, np_cfg, count);
3715 if (ret)
3716 return ret;
3717
3718 for (i = 0; i < count ; i++) {
3719 pci_func = np_cfg[i].pci_func;
3720 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
3721 if (ret)
3722 return ret;
3723 nic_info.pci_func = pci_func;
3724 nic_info.min_tx_bw = np_cfg[i].min_bw;
3725 nic_info.max_tx_bw = np_cfg[i].max_bw;
3726 ret = qlcnic_set_nic_info(adapter, &nic_info);
3727 if (ret)
3728 return ret;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003729 adapter->npars[i].min_bw = nic_info.min_tx_bw;
3730 adapter->npars[i].max_bw = nic_info.max_tx_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003731 }
3732
3733 return size;
3734
3735}
3736static ssize_t
3737qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
3738 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3739{
3740 struct device *dev = container_of(kobj, struct device, kobj);
3741 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3742 struct qlcnic_info nic_info;
3743 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
3744 int i, ret;
3745
3746 if (size != sizeof(np_cfg))
3747 return QL_STATUS_INVALID_PARAM;
3748
3749 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3750 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3751 continue;
3752 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
3753 if (ret)
3754 return ret;
3755
3756 np_cfg[i].pci_func = i;
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +00003757 np_cfg[i].op_mode = (u8)nic_info.op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003758 np_cfg[i].port_num = nic_info.phys_port;
3759 np_cfg[i].fw_capab = nic_info.capabilities;
3760 np_cfg[i].min_bw = nic_info.min_tx_bw ;
3761 np_cfg[i].max_bw = nic_info.max_tx_bw;
3762 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
3763 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
3764 }
3765 memcpy(buf, &np_cfg, size);
3766 return size;
3767}
3768
3769static ssize_t
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003770qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
3771 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3772{
3773 struct device *dev = container_of(kobj, struct device, kobj);
3774 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3775 struct qlcnic_esw_statistics port_stats;
3776 int ret;
3777
3778 if (size != sizeof(struct qlcnic_esw_statistics))
3779 return QL_STATUS_INVALID_PARAM;
3780
3781 if (offset >= QLCNIC_MAX_PCI_FUNC)
3782 return QL_STATUS_INVALID_PARAM;
3783
3784 memset(&port_stats, 0, size);
3785 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3786 &port_stats.rx);
3787 if (ret)
3788 return ret;
3789
3790 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3791 &port_stats.tx);
3792 if (ret)
3793 return ret;
3794
3795 memcpy(buf, &port_stats, size);
3796 return size;
3797}
3798
3799static ssize_t
3800qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
3801 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3802{
3803 struct device *dev = container_of(kobj, struct device, kobj);
3804 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3805 struct qlcnic_esw_statistics esw_stats;
3806 int ret;
3807
3808 if (size != sizeof(struct qlcnic_esw_statistics))
3809 return QL_STATUS_INVALID_PARAM;
3810
3811 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3812 return QL_STATUS_INVALID_PARAM;
3813
3814 memset(&esw_stats, 0, size);
3815 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3816 &esw_stats.rx);
3817 if (ret)
3818 return ret;
3819
3820 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3821 &esw_stats.tx);
3822 if (ret)
3823 return ret;
3824
3825 memcpy(buf, &esw_stats, size);
3826 return size;
3827}
3828
3829static ssize_t
3830qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
3831 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3832{
3833 struct device *dev = container_of(kobj, struct device, kobj);
3834 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3835 int ret;
3836
3837 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3838 return QL_STATUS_INVALID_PARAM;
3839
3840 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3841 QLCNIC_QUERY_RX_COUNTER);
3842 if (ret)
3843 return ret;
3844
3845 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3846 QLCNIC_QUERY_TX_COUNTER);
3847 if (ret)
3848 return ret;
3849
3850 return size;
3851}
3852
3853static ssize_t
3854qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
3855 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3856{
3857
3858 struct device *dev = container_of(kobj, struct device, kobj);
3859 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3860 int ret;
3861
3862 if (offset >= QLCNIC_MAX_PCI_FUNC)
3863 return QL_STATUS_INVALID_PARAM;
3864
3865 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
3866 QLCNIC_QUERY_RX_COUNTER);
3867 if (ret)
3868 return ret;
3869
3870 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
3871 QLCNIC_QUERY_TX_COUNTER);
3872 if (ret)
3873 return ret;
3874
3875 return size;
3876}
3877
3878static ssize_t
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003879qlcnic_sysfs_read_pci_config(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_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
Dan Carpentere88db3b2010-08-09 21:49:36 +00003885 struct qlcnic_pci_info *pci_info;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003886 int i, ret;
3887
3888 if (size != sizeof(pci_cfg))
3889 return QL_STATUS_INVALID_PARAM;
3890
Dan Carpentere88db3b2010-08-09 21:49:36 +00003891 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
3892 if (!pci_info)
3893 return -ENOMEM;
3894
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003895 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpentere88db3b2010-08-09 21:49:36 +00003896 if (ret) {
3897 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003898 return ret;
Dan Carpentere88db3b2010-08-09 21:49:36 +00003899 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003900
3901 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3902 pci_cfg[i].pci_func = pci_info[i].id;
3903 pci_cfg[i].func_type = pci_info[i].type;
3904 pci_cfg[i].port_num = pci_info[i].default_port;
3905 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
3906 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
3907 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
3908 }
3909 memcpy(buf, &pci_cfg, size);
Dan Carpentere88db3b2010-08-09 21:49:36 +00003910 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003911 return size;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003912}
3913static struct bin_attribute bin_attr_npar_config = {
3914 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
3915 .size = 0,
3916 .read = qlcnic_sysfs_read_npar_config,
3917 .write = qlcnic_sysfs_write_npar_config,
3918};
3919
3920static struct bin_attribute bin_attr_pci_config = {
3921 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
3922 .size = 0,
3923 .read = qlcnic_sysfs_read_pci_config,
3924 .write = NULL,
3925};
3926
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003927static struct bin_attribute bin_attr_port_stats = {
3928 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
3929 .size = 0,
3930 .read = qlcnic_sysfs_get_port_stats,
3931 .write = qlcnic_sysfs_clear_port_stats,
3932};
3933
3934static struct bin_attribute bin_attr_esw_stats = {
3935 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
3936 .size = 0,
3937 .read = qlcnic_sysfs_get_esw_stats,
3938 .write = qlcnic_sysfs_clear_esw_stats,
3939};
3940
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003941static struct bin_attribute bin_attr_esw_config = {
3942 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
3943 .size = 0,
3944 .read = qlcnic_sysfs_read_esw_config,
3945 .write = qlcnic_sysfs_write_esw_config,
3946};
3947
3948static struct bin_attribute bin_attr_pm_config = {
3949 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
3950 .size = 0,
3951 .read = qlcnic_sysfs_read_pm_config,
3952 .write = qlcnic_sysfs_write_pm_config,
3953};
3954
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003955static void
3956qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
3957{
3958 struct device *dev = &adapter->pdev->dev;
3959
3960 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3961 if (device_create_file(dev, &dev_attr_bridged_mode))
3962 dev_warn(dev,
3963 "failed to create bridged_mode sysfs entry\n");
3964}
3965
3966static void
3967qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
3968{
3969 struct device *dev = &adapter->pdev->dev;
3970
3971 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3972 device_remove_file(dev, &dev_attr_bridged_mode);
3973}
3974
3975static void
3976qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
3977{
3978 struct device *dev = &adapter->pdev->dev;
3979
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003980 if (device_create_bin_file(dev, &bin_attr_port_stats))
3981 dev_info(dev, "failed to create port stats sysfs entry");
3982
Anirban Chakraborty132ff002010-07-09 13:15:05 +00003983 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
3984 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003985 if (device_create_file(dev, &dev_attr_diag_mode))
3986 dev_info(dev, "failed to create diag_mode sysfs entry\n");
3987 if (device_create_bin_file(dev, &bin_attr_crb))
3988 dev_info(dev, "failed to create crb sysfs entry\n");
3989 if (device_create_bin_file(dev, &bin_attr_mem))
3990 dev_info(dev, "failed to create mem sysfs entry\n");
Sony Chacko53478fe2011-04-01 14:27:47 +00003991 if (device_create_bin_file(dev, &bin_attr_pci_config))
3992 dev_info(dev, "failed to create pci config sysfs entry");
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003993 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
3994 return;
3995 if (device_create_bin_file(dev, &bin_attr_esw_config))
3996 dev_info(dev, "failed to create esw config sysfs entry");
3997 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003998 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003999 if (device_create_bin_file(dev, &bin_attr_npar_config))
4000 dev_info(dev, "failed to create npar config sysfs entry");
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004001 if (device_create_bin_file(dev, &bin_attr_pm_config))
4002 dev_info(dev, "failed to create pm config sysfs entry");
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004003 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4004 dev_info(dev, "failed to create eswitch stats sysfs entry");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004005}
4006
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004007static void
4008qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4009{
4010 struct device *dev = &adapter->pdev->dev;
4011
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004012 device_remove_bin_file(dev, &bin_attr_port_stats);
4013
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004014 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4015 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004016 device_remove_file(dev, &dev_attr_diag_mode);
4017 device_remove_bin_file(dev, &bin_attr_crb);
4018 device_remove_bin_file(dev, &bin_attr_mem);
Sony Chacko53478fe2011-04-01 14:27:47 +00004019 device_remove_bin_file(dev, &bin_attr_pci_config);
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004020 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4021 return;
4022 device_remove_bin_file(dev, &bin_attr_esw_config);
4023 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004024 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004025 device_remove_bin_file(dev, &bin_attr_npar_config);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004026 device_remove_bin_file(dev, &bin_attr_pm_config);
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004027 device_remove_bin_file(dev, &bin_attr_esw_stats);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004028}
4029
4030#ifdef CONFIG_INET
4031
4032#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4033
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004034static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004035qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4036 struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004037{
4038 struct in_device *indev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004039
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004040 indev = in_dev_get(dev);
4041 if (!indev)
4042 return;
4043
4044 for_ifa(indev) {
4045 switch (event) {
4046 case NETDEV_UP:
4047 qlcnic_config_ipaddr(adapter,
4048 ifa->ifa_address, QLCNIC_IP_UP);
4049 break;
4050 case NETDEV_DOWN:
4051 qlcnic_config_ipaddr(adapter,
4052 ifa->ifa_address, QLCNIC_IP_DOWN);
4053 break;
4054 default:
4055 break;
4056 }
4057 } endfor_ifa(indev);
4058
4059 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004060}
4061
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004062static void
4063qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4064{
4065 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4066 struct net_device *dev;
4067 u16 vid;
4068
4069 qlcnic_config_indev_addr(adapter, netdev, event);
4070
4071 if (!adapter->vlgrp)
4072 return;
4073
Jesse Grossb7381272010-10-20 13:56:02 +00004074 for (vid = 0; vid < VLAN_N_VID; vid++) {
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004075 dev = vlan_group_get_device(adapter->vlgrp, vid);
4076 if (!dev)
4077 continue;
4078
4079 qlcnic_config_indev_addr(adapter, dev, event);
4080 }
4081}
4082
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004083static int qlcnic_netdev_event(struct notifier_block *this,
4084 unsigned long event, void *ptr)
4085{
4086 struct qlcnic_adapter *adapter;
4087 struct net_device *dev = (struct net_device *)ptr;
4088
4089recheck:
4090 if (dev == NULL)
4091 goto done;
4092
4093 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4094 dev = vlan_dev_real_dev(dev);
4095 goto recheck;
4096 }
4097
4098 if (!is_qlcnic_netdev(dev))
4099 goto done;
4100
4101 adapter = netdev_priv(dev);
4102
4103 if (!adapter)
4104 goto done;
4105
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004106 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004107 goto done;
4108
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004109 qlcnic_config_indev_addr(adapter, dev, event);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004110done:
4111 return NOTIFY_DONE;
4112}
4113
4114static int
4115qlcnic_inetaddr_event(struct notifier_block *this,
4116 unsigned long event, void *ptr)
4117{
4118 struct qlcnic_adapter *adapter;
4119 struct net_device *dev;
4120
4121 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4122
4123 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4124
4125recheck:
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004126 if (dev == NULL)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004127 goto done;
4128
4129 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4130 dev = vlan_dev_real_dev(dev);
4131 goto recheck;
4132 }
4133
4134 if (!is_qlcnic_netdev(dev))
4135 goto done;
4136
4137 adapter = netdev_priv(dev);
4138
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00004139 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004140 goto done;
4141
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004142 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004143 goto done;
4144
4145 switch (event) {
4146 case NETDEV_UP:
4147 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4148 break;
4149 case NETDEV_DOWN:
4150 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4151 break;
4152 default:
4153 break;
4154 }
4155
4156done:
4157 return NOTIFY_DONE;
4158}
4159
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004160static struct notifier_block qlcnic_netdev_cb = {
4161 .notifier_call = qlcnic_netdev_event,
4162};
4163
4164static struct notifier_block qlcnic_inetaddr_cb = {
4165 .notifier_call = qlcnic_inetaddr_event,
4166};
4167#else
4168static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004169qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004170{ }
4171#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004172static struct pci_error_handlers qlcnic_err_handler = {
4173 .error_detected = qlcnic_io_error_detected,
4174 .slot_reset = qlcnic_io_slot_reset,
4175 .resume = qlcnic_io_resume,
4176};
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004177
4178static struct pci_driver qlcnic_driver = {
4179 .name = qlcnic_driver_name,
4180 .id_table = qlcnic_pci_tbl,
4181 .probe = qlcnic_probe,
4182 .remove = __devexit_p(qlcnic_remove),
4183#ifdef CONFIG_PM
4184 .suspend = qlcnic_suspend,
4185 .resume = qlcnic_resume,
4186#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004187 .shutdown = qlcnic_shutdown,
4188 .err_handler = &qlcnic_err_handler
4189
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004190};
4191
4192static int __init qlcnic_init_module(void)
4193{
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004194 int ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004195
4196 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4197
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004198 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4199 if (qlcnic_wq == NULL) {
4200 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4201 return -ENOMEM;
4202 }
4203
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004204#ifdef CONFIG_INET
4205 register_netdevice_notifier(&qlcnic_netdev_cb);
4206 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4207#endif
4208
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004209 ret = pci_register_driver(&qlcnic_driver);
4210 if (ret) {
4211#ifdef CONFIG_INET
4212 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4213 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4214#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004215 destroy_workqueue(qlcnic_wq);
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004216 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004217
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004218 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004219}
4220
4221module_init(qlcnic_init_module);
4222
4223static void __exit qlcnic_exit_module(void)
4224{
4225
4226 pci_unregister_driver(&qlcnic_driver);
4227
4228#ifdef CONFIG_INET
4229 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4230 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4231#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004232 destroy_workqueue(qlcnic_wq);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004233}
4234
4235module_exit(qlcnic_exit_module);