blob: 8bf9a968f7f20516037f04fbe299834a843b1304 [file] [log] [blame]
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001/*
Sritej Velaga40839129f2010-12-02 20:41:56 +00002 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2010 QLogic Corporation
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004 *
Sritej Velaga40839129f2010-12-02 20:41:56 +00005 * See LICENSE.qlcnic for copyright and licensing details.
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00006 */
7
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00009#include <linux/vmalloc.h>
10#include <linux/interrupt.h>
11
12#include "qlcnic.h"
13
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +000014#include <linux/swab.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000015#include <linux/dma-mapping.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000016#include <net/ip.h>
17#include <linux/ipv6.h>
18#include <linux/inetdevice.h>
19#include <linux/sysfs.h>
Sucheta Chakraborty451724c2010-07-13 20:33:34 +000020#include <linux/aer.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000021
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000022MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000023MODULE_LICENSE("GPL");
24MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
25MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
26
27char qlcnic_driver_name[] = "qlcnic";
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000028static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
29 "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000030
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +000031static struct workqueue_struct *qlcnic_wq;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +000032static int qlcnic_mac_learn;
amit salechab11a25a2011-01-10 00:15:23 +000033module_param(qlcnic_mac_learn, int, 0444);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +000034MODULE_PARM_DESC(qlcnic_mac_learn, "Mac Filter (0=disabled, 1=enabled)");
35
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000036static int use_msi = 1;
amit salechab11a25a2011-01-10 00:15:23 +000037module_param(use_msi, int, 0444);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000038MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
39
40static int use_msi_x = 1;
amit salechab11a25a2011-01-10 00:15:23 +000041module_param(use_msi_x, int, 0444);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000042MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
43
amit salecha9ce13ca2011-02-23 03:21:24 +000044static int auto_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000045module_param(auto_fw_reset, int, 0644);
46MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
47
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +000048static int load_fw_file;
amit salechab11a25a2011-01-10 00:15:23 +000049module_param(load_fw_file, int, 0444);
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +000050MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
51
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +000052static int qlcnic_config_npars;
amit salechab11a25a2011-01-10 00:15:23 +000053module_param(qlcnic_config_npars, int, 0444);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +000054MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
55
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000056static int __devinit qlcnic_probe(struct pci_dev *pdev,
57 const struct pci_device_id *ent);
58static void __devexit qlcnic_remove(struct pci_dev *pdev);
59static int qlcnic_open(struct net_device *netdev);
60static int qlcnic_close(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000061static void qlcnic_tx_timeout(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000062static void qlcnic_attach_work(struct work_struct *work);
63static void qlcnic_fwinit_work(struct work_struct *work);
64static void qlcnic_fw_poll_work(struct work_struct *work);
65static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
66 work_func_t func, int delay);
67static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
68static int qlcnic_poll(struct napi_struct *napi, int budget);
schacko8f891382010-06-17 02:56:40 +000069static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000070#ifdef CONFIG_NET_POLL_CONTROLLER
71static void qlcnic_poll_controller(struct net_device *netdev);
72#endif
73
74static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
75static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
76static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
77static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
78
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +000079static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +000080static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000081static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
82
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +000083static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000084static irqreturn_t qlcnic_intr(int irq, void *data);
85static irqreturn_t qlcnic_msi_intr(int irq, void *data);
86static irqreturn_t qlcnic_msix_intr(int irq, void *data);
87
88static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +000089static void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000090static int qlcnic_start_firmware(struct qlcnic_adapter *);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000091
Amit Kumar Salechab5e54922010-08-31 17:17:51 +000092static void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter);
93static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000094static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000095static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
96static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
97static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
Rajesh Borundia0325d692010-08-19 05:08:26 +000098static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
99 struct qlcnic_esw_func_cfg *);
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000100static void qlcnic_vlan_rx_add(struct net_device *, u16);
101static void qlcnic_vlan_rx_del(struct net_device *, u16);
102
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000103/* PCI Device ID Table */
104#define ENTRY(device) \
105 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
106 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
107
108#define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
109
Amit Kumar Salecha6a902882010-02-01 05:24:54 +0000110static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000111 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
112 {0,}
113};
114
115MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
116
117
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000118inline void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000119qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
120 struct qlcnic_host_tx_ring *tx_ring)
121{
122 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000123}
124
125static const u32 msi_tgt_status[8] = {
126 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
127 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
128 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
129 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
130};
131
132static const
133struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
134
135static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
136{
137 writel(0, sds_ring->crb_intr_mask);
138}
139
140static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
141{
142 struct qlcnic_adapter *adapter = sds_ring->adapter;
143
144 writel(0x1, sds_ring->crb_intr_mask);
145
146 if (!QLCNIC_IS_MSI_FAMILY(adapter))
147 writel(0xfbff, adapter->tgt_mask_reg);
148}
149
150static int
151qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
152{
153 int size = sizeof(struct qlcnic_host_sds_ring) * count;
154
155 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
156
Eric Dumazet807540b2010-09-23 05:40:09 +0000157 return recv_ctx->sds_rings == NULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000158}
159
160static void
161qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
162{
163 if (recv_ctx->sds_rings != NULL)
164 kfree(recv_ctx->sds_rings);
165
166 recv_ctx->sds_rings = NULL;
167}
168
169static int
170qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
171{
172 int ring;
173 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000174 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000175
176 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
177 return -ENOMEM;
178
179 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
180 sds_ring = &recv_ctx->sds_rings[ring];
schacko8f891382010-06-17 02:56:40 +0000181
182 if (ring == adapter->max_sds_rings - 1)
183 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
184 QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
185 else
186 netif_napi_add(netdev, &sds_ring->napi,
187 qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000188 }
189
190 return 0;
191}
192
193static void
194qlcnic_napi_del(struct qlcnic_adapter *adapter)
195{
196 int ring;
197 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000198 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000199
200 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
201 sds_ring = &recv_ctx->sds_rings[ring];
202 netif_napi_del(&sds_ring->napi);
203 }
204
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000205 qlcnic_free_sds_rings(adapter->recv_ctx);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000206}
207
208static void
209qlcnic_napi_enable(struct qlcnic_adapter *adapter)
210{
211 int ring;
212 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000213 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000214
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000215 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
216 return;
217
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000218 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
219 sds_ring = &recv_ctx->sds_rings[ring];
220 napi_enable(&sds_ring->napi);
221 qlcnic_enable_int(sds_ring);
222 }
223}
224
225static void
226qlcnic_napi_disable(struct qlcnic_adapter *adapter)
227{
228 int ring;
229 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000230 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000231
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000232 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
233 return;
234
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000235 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
236 sds_ring = &recv_ctx->sds_rings[ring];
237 qlcnic_disable_int(sds_ring);
238 napi_synchronize(&sds_ring->napi);
239 napi_disable(&sds_ring->napi);
240 }
241}
242
243static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
244{
245 memset(&adapter->stats, 0, sizeof(adapter->stats));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000246}
247
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000248static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
249{
250 u32 control;
251 int pos;
252
253 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
254 if (pos) {
255 pci_read_config_dword(pdev, pos, &control);
256 if (enable)
257 control |= PCI_MSIX_FLAGS_ENABLE;
258 else
259 control = 0;
260 pci_write_config_dword(pdev, pos, control);
261 }
262}
263
264static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
265{
266 int i;
267
268 for (i = 0; i < count; i++)
269 adapter->msix_entries[i].entry = i;
270}
271
272static int
273qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
274{
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000275 u8 mac_addr[ETH_ALEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000276 struct net_device *netdev = adapter->netdev;
277 struct pci_dev *pdev = adapter->pdev;
278
Rajesh Borundiada48e6c2010-08-31 17:17:46 +0000279 if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000280 return -EIO;
281
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000282 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000283 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
284 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
285
286 /* set station address */
287
288 if (!is_valid_ether_addr(netdev->perm_addr))
289 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
290 netdev->dev_addr);
291
292 return 0;
293}
294
295static int qlcnic_set_mac(struct net_device *netdev, void *p)
296{
297 struct qlcnic_adapter *adapter = netdev_priv(netdev);
298 struct sockaddr *addr = p;
299
Rajesh Borundia73733732010-08-31 17:17:50 +0000300 if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
301 return -EOPNOTSUPP;
302
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000303 if (!is_valid_ether_addr(addr->sa_data))
304 return -EINVAL;
305
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000306 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000307 netif_device_detach(netdev);
308 qlcnic_napi_disable(adapter);
309 }
310
311 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
312 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
313 qlcnic_set_multi(adapter->netdev);
314
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000315 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000316 netif_device_attach(netdev);
317 qlcnic_napi_enable(adapter);
318 }
319 return 0;
320}
321
322static const struct net_device_ops qlcnic_netdev_ops = {
323 .ndo_open = qlcnic_open,
324 .ndo_stop = qlcnic_close,
325 .ndo_start_xmit = qlcnic_xmit_frame,
326 .ndo_get_stats = qlcnic_get_stats,
327 .ndo_validate_addr = eth_validate_addr,
328 .ndo_set_multicast_list = qlcnic_set_multi,
329 .ndo_set_mac_address = qlcnic_set_mac,
330 .ndo_change_mtu = qlcnic_change_mtu,
331 .ndo_tx_timeout = qlcnic_tx_timeout,
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000332 .ndo_vlan_rx_add_vid = qlcnic_vlan_rx_add,
333 .ndo_vlan_rx_kill_vid = qlcnic_vlan_rx_del,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000334#ifdef CONFIG_NET_POLL_CONTROLLER
335 .ndo_poll_controller = qlcnic_poll_controller,
336#endif
337};
338
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000339static struct qlcnic_nic_template qlcnic_ops = {
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000340 .config_bridged_mode = qlcnic_config_bridged_mode,
341 .config_led = qlcnic_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000342 .start_firmware = qlcnic_start_firmware
343};
344
345static struct qlcnic_nic_template qlcnic_vf_ops = {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000346 .config_bridged_mode = qlcnicvf_config_bridged_mode,
347 .config_led = qlcnicvf_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000348 .start_firmware = qlcnicvf_start_firmware
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000349};
350
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000351static void
352qlcnic_setup_intr(struct qlcnic_adapter *adapter)
353{
354 const struct qlcnic_legacy_intr_set *legacy_intrp;
355 struct pci_dev *pdev = adapter->pdev;
356 int err, num_msix;
357
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000358 if (adapter->msix_supported) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000359 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
360 MSIX_ENTRIES_PER_ADAPTER : 2;
361 } else
362 num_msix = 1;
363
364 adapter->max_sds_rings = 1;
365
366 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
367
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000368 legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000369
370 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
371 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
372 legacy_intrp->tgt_status_reg);
373 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
374 legacy_intrp->tgt_mask_reg);
375 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
376
377 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
378 ISR_INT_STATE_REG);
379
380 qlcnic_set_msix_bit(pdev, 0);
381
382 if (adapter->msix_supported) {
383
384 qlcnic_init_msix_entries(adapter, num_msix);
385 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
386 if (err == 0) {
387 adapter->flags |= QLCNIC_MSIX_ENABLED;
388 qlcnic_set_msix_bit(pdev, 1);
389
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000390 adapter->max_sds_rings = num_msix;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000391
392 dev_info(&pdev->dev, "using msi-x interrupts\n");
393 return;
394 }
395
396 if (err > 0)
397 pci_disable_msix(pdev);
398
399 /* fall through for msi */
400 }
401
402 if (use_msi && !pci_enable_msi(pdev)) {
403 adapter->flags |= QLCNIC_MSI_ENABLED;
404 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000405 msi_tgt_status[adapter->ahw->pci_func]);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000406 dev_info(&pdev->dev, "using msi interrupts\n");
407 adapter->msix_entries[0].vector = pdev->irq;
408 return;
409 }
410
411 dev_info(&pdev->dev, "using legacy interrupts\n");
412 adapter->msix_entries[0].vector = pdev->irq;
413}
414
415static void
416qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
417{
418 if (adapter->flags & QLCNIC_MSIX_ENABLED)
419 pci_disable_msix(adapter->pdev);
420 if (adapter->flags & QLCNIC_MSI_ENABLED)
421 pci_disable_msi(adapter->pdev);
422}
423
424static void
425qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
426{
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000427 if (adapter->ahw->pci_base0 != NULL)
428 iounmap(adapter->ahw->pci_base0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000429}
430
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000431static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000432qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
433{
Dan Carpentere88db3b2010-08-09 21:49:36 +0000434 struct qlcnic_pci_info *pci_info;
Julia Lawall900853a2010-08-16 10:23:51 +0000435 int i, ret = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000436 u8 pfn;
437
Dan Carpentere88db3b2010-08-09 21:49:36 +0000438 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
439 if (!pci_info)
440 return -ENOMEM;
441
Dan Carpenterca315ac2010-08-09 21:47:56 +0000442 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000443 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
Dan Carpentere88db3b2010-08-09 21:49:36 +0000444 if (!adapter->npars) {
Julia Lawall900853a2010-08-16 10:23:51 +0000445 ret = -ENOMEM;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000446 goto err_pci_info;
447 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000448
Dan Carpenterca315ac2010-08-09 21:47:56 +0000449 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000450 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
451 if (!adapter->eswitch) {
Julia Lawall900853a2010-08-16 10:23:51 +0000452 ret = -ENOMEM;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000453 goto err_npars;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000454 }
455
456 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000457 if (ret)
458 goto err_eswitch;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000459
Dan Carpenterca315ac2010-08-09 21:47:56 +0000460 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
461 pfn = pci_info[i].id;
Sony Chackof848d6d2011-04-01 14:27:59 +0000462 if (pfn > QLCNIC_MAX_PCI_FUNC) {
463 ret = QL_STATUS_INVALID_PARAM;
464 goto err_eswitch;
465 }
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000466 adapter->npars[pfn].active = (u8)pci_info[i].active;
467 adapter->npars[pfn].type = (u8)pci_info[i].type;
468 adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000469 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
470 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000471 }
472
Dan Carpenterca315ac2010-08-09 21:47:56 +0000473 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
474 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
475
Dan Carpentere88db3b2010-08-09 21:49:36 +0000476 kfree(pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000477 return 0;
478
479err_eswitch:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000480 kfree(adapter->eswitch);
481 adapter->eswitch = NULL;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000482err_npars:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000483 kfree(adapter->npars);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000484 adapter->npars = NULL;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000485err_pci_info:
486 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000487
488 return ret;
489}
490
491static int
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000492qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
493{
494 u8 id;
495 u32 ref_count;
496 int i, ret = 1;
497 u32 data = QLCNIC_MGMT_FUNC;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000498 void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000499
500 /* If other drivers are not in use set their privilege level */
Amit Kumar Salecha31018e02010-08-25 04:03:05 +0000501 ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000502 ret = qlcnic_api_lock(adapter);
503 if (ret)
504 goto err_lock;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000505
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000506 if (qlcnic_config_npars) {
507 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000508 id = i;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000509 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000510 id == adapter->ahw->pci_func)
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000511 continue;
512 data |= (qlcnic_config_npars &
513 QLC_DEV_SET_DRV(0xf, id));
514 }
515 } else {
516 data = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000517 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000518 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000519 adapter->ahw->pci_func));
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000520 }
521 writel(data, priv_op);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000522 qlcnic_api_unlock(adapter);
523err_lock:
524 return ret;
525}
526
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000527static void
528qlcnic_check_vf(struct qlcnic_adapter *adapter)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000529{
530 void __iomem *msix_base_addr;
531 void __iomem *priv_op;
532 u32 func;
533 u32 msix_base;
534 u32 op_mode, priv_level;
535
536 /* Determine FW API version */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000537 adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
538 QLCNIC_FW_API);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000539
540 /* Find PCI function number */
541 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000542 msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000543 msix_base = readl(msix_base_addr);
544 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000545 adapter->ahw->pci_func = func;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000546
547 /* Determine function privilege level */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000548 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000549 op_mode = readl(priv_op);
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000550 if (op_mode == QLC_DEV_DRV_DEFAULT)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000551 priv_level = QLCNIC_MGMT_FUNC;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000552 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000553 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000554
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000555 if (priv_level == QLCNIC_NON_PRIV_FUNC) {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000556 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
557 dev_info(&adapter->pdev->dev,
558 "HAL Version: %d Non Privileged function\n",
559 adapter->fw_hal_version);
560 adapter->nic_ops = &qlcnic_vf_ops;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000561 } else
562 adapter->nic_ops = &qlcnic_ops;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000563}
564
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000565static int
566qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
567{
568 void __iomem *mem_ptr0 = NULL;
569 resource_size_t mem_base;
570 unsigned long mem_len, pci_len0 = 0;
571
572 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000573
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000574 /* remap phys address */
575 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
576 mem_len = pci_resource_len(pdev, 0);
577
578 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
579
580 mem_ptr0 = pci_ioremap_bar(pdev, 0);
581 if (mem_ptr0 == NULL) {
582 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
583 return -EIO;
584 }
585 pci_len0 = mem_len;
586 } else {
587 return -EIO;
588 }
589
590 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
591
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000592 adapter->ahw->pci_base0 = mem_ptr0;
593 adapter->ahw->pci_len0 = pci_len0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000594
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000595 qlcnic_check_vf(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000596
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000597 adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
598 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
599 adapter->ahw->pci_func)));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000600
601 return 0;
602}
603
604static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
605{
606 struct pci_dev *pdev = adapter->pdev;
607 int i, found = 0;
608
609 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
610 if (qlcnic_boards[i].vendor == pdev->vendor &&
611 qlcnic_boards[i].device == pdev->device &&
612 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
613 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +0000614 sprintf(name, "%pM: %s" ,
615 adapter->mac_addr,
616 qlcnic_boards[i].short_name);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000617 found = 1;
618 break;
619 }
620
621 }
622
623 if (!found)
Sritej Velaga7f9a0c32010-06-17 02:56:39 +0000624 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000625}
626
627static void
628qlcnic_check_options(struct qlcnic_adapter *adapter)
629{
630 u32 fw_major, fw_minor, fw_build;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000631 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000632
633 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
634 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
635 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
636
637 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
638
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000639 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
640 fw_major, fw_minor, fw_build);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000641 if (adapter->ahw->port_type == QLCNIC_XGBE) {
Sony Chacko90d19002010-10-26 17:53:08 +0000642 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
643 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
644 adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
645 } else {
646 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
647 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
648 }
649
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000650 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
Sony Chacko90d19002010-10-26 17:53:08 +0000651 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
652
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000653 } else if (adapter->ahw->port_type == QLCNIC_GBE) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000654 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
655 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
Sony Chacko90d19002010-10-26 17:53:08 +0000656 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
657 adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000658 }
659
660 adapter->msix_supported = !!use_msi_x;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000661
662 adapter->num_txd = MAX_CMD_DESCRIPTORS;
663
Sony Chacko251b0362010-08-19 05:08:24 +0000664 adapter->max_rds_rings = MAX_RDS_RINGS;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000665}
666
Rajesh Borundia174240a2010-08-31 17:17:47 +0000667static int
668qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
669{
670 int err;
671 struct qlcnic_info nic_info;
672
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000673 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
Rajesh Borundia174240a2010-08-31 17:17:47 +0000674 if (err)
675 return err;
676
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000677 adapter->physical_port = (u8)nic_info.phys_port;
Rajesh Borundia174240a2010-08-31 17:17:47 +0000678 adapter->switch_mode = nic_info.switch_mode;
679 adapter->max_tx_ques = nic_info.max_tx_ques;
680 adapter->max_rx_ques = nic_info.max_rx_ques;
681 adapter->capabilities = nic_info.capabilities;
682 adapter->max_mac_filters = nic_info.max_mac_filters;
683 adapter->max_mtu = nic_info.max_mtu;
684
685 if (adapter->capabilities & BIT_6)
686 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
687 else
688 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
689
690 return err;
691}
692
Rajesh Borundia0325d692010-08-19 05:08:26 +0000693static void
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000694qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
695 struct qlcnic_esw_func_cfg *esw_cfg)
696{
697 if (esw_cfg->discard_tagged)
698 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
699 else
700 adapter->flags |= QLCNIC_TAGGING_ENABLED;
701
702 if (esw_cfg->vlan_id)
703 adapter->pvid = esw_cfg->vlan_id;
704 else
705 adapter->pvid = 0;
706}
707
708static void
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000709qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
710{
711 struct qlcnic_adapter *adapter = netdev_priv(netdev);
712 set_bit(vid, adapter->vlans);
713}
714
715static void
716qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
717{
718 struct qlcnic_adapter *adapter = netdev_priv(netdev);
719
720 qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
721 clear_bit(vid, adapter->vlans);
722}
723
724static void
Rajesh Borundia0325d692010-08-19 05:08:26 +0000725qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
726 struct qlcnic_esw_func_cfg *esw_cfg)
727{
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000728 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
729 QLCNIC_PROMISC_DISABLED);
Rajesh Borundia7613c872010-08-31 17:17:48 +0000730
731 if (esw_cfg->mac_anti_spoof)
732 adapter->flags |= QLCNIC_MACSPOOF;
Sony Chackofe4d4342010-08-19 05:08:27 +0000733
Rajesh Borundia73733732010-08-31 17:17:50 +0000734 if (!esw_cfg->mac_override)
735 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
736
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000737 if (!esw_cfg->promisc_mode)
738 adapter->flags |= QLCNIC_PROMISC_DISABLED;
739
Rajesh Borundia0325d692010-08-19 05:08:26 +0000740 qlcnic_set_netdev_features(adapter, esw_cfg);
741}
742
743static int
744qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
745{
746 struct qlcnic_esw_func_cfg esw_cfg;
747
748 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
749 return 0;
750
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000751 esw_cfg.pci_func = adapter->ahw->pci_func;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000752 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
753 return -EIO;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000754 qlcnic_set_vlan_config(adapter, &esw_cfg);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000755 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
756
757 return 0;
758}
759
760static void
761qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
762 struct qlcnic_esw_func_cfg *esw_cfg)
763{
764 struct net_device *netdev = adapter->netdev;
765 unsigned long features, vlan_features;
766
767 features = (NETIF_F_SG | NETIF_F_IP_CSUM |
768 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
769 vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000770 NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000771
772 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
773 features |= (NETIF_F_TSO | NETIF_F_TSO6);
774 vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
775 }
776 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
777 features |= NETIF_F_LRO;
778
779 if (esw_cfg->offload_flags & BIT_0) {
780 netdev->features |= features;
781 adapter->rx_csum = 1;
782 if (!(esw_cfg->offload_flags & BIT_1))
783 netdev->features &= ~NETIF_F_TSO;
784 if (!(esw_cfg->offload_flags & BIT_2))
785 netdev->features &= ~NETIF_F_TSO6;
786 } else {
787 netdev->features &= ~features;
788 adapter->rx_csum = 0;
789 }
790
791 netdev->vlan_features = (features & vlan_features);
792}
793
794static int
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000795qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
796{
797 void __iomem *priv_op;
798 u32 op_mode, priv_level;
799 int err = 0;
800
Rajesh Borundia174240a2010-08-31 17:17:47 +0000801 err = qlcnic_initialize_nic(adapter);
802 if (err)
803 return err;
804
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000805 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
806 return 0;
807
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000808 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000809 op_mode = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000810 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000811
812 if (op_mode == QLC_DEV_DRV_DEFAULT)
813 priv_level = QLCNIC_MGMT_FUNC;
814 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000815 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000816
Rajesh Borundia174240a2010-08-31 17:17:47 +0000817 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000818 if (priv_level == QLCNIC_MGMT_FUNC) {
819 adapter->op_mode = QLCNIC_MGMT_FUNC;
820 err = qlcnic_init_pci_info(adapter);
821 if (err)
822 return err;
823 /* Set privilege level for other functions */
824 qlcnic_set_function_modes(adapter);
825 dev_info(&adapter->pdev->dev,
826 "HAL Version: %d, Management function\n",
827 adapter->fw_hal_version);
828 } else if (priv_level == QLCNIC_PRIV_FUNC) {
829 adapter->op_mode = QLCNIC_PRIV_FUNC;
830 dev_info(&adapter->pdev->dev,
831 "HAL Version: %d, Privileged function\n",
832 adapter->fw_hal_version);
833 }
Rajesh Borundia174240a2010-08-31 17:17:47 +0000834 }
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000835
836 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
837
838 return err;
839}
840
841static int
Rajesh Borundia0325d692010-08-19 05:08:26 +0000842qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
843{
844 struct qlcnic_esw_func_cfg esw_cfg;
845 struct qlcnic_npar_info *npar;
846 u8 i;
847
Rajesh Borundia174240a2010-08-31 17:17:47 +0000848 if (adapter->need_fw_reset)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000849 return 0;
850
851 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
852 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
853 continue;
854 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
855 esw_cfg.pci_func = i;
856 esw_cfg.offload_flags = BIT_0;
Rajesh Borundia73733732010-08-31 17:17:50 +0000857 esw_cfg.mac_override = BIT_0;
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000858 esw_cfg.promisc_mode = BIT_0;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000859 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
860 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
861 if (qlcnic_config_switch_port(adapter, &esw_cfg))
862 return -EIO;
863 npar = &adapter->npars[i];
864 npar->pvid = esw_cfg.vlan_id;
Rajesh Borundia73733732010-08-31 17:17:50 +0000865 npar->mac_override = esw_cfg.mac_override;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000866 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
867 npar->discard_tagged = esw_cfg.discard_tagged;
868 npar->promisc_mode = esw_cfg.promisc_mode;
869 npar->offload_flags = esw_cfg.offload_flags;
870 }
871
872 return 0;
873}
874
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000875static int
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000876qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
877 struct qlcnic_npar_info *npar, int pci_func)
878{
879 struct qlcnic_esw_func_cfg esw_cfg;
880 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
881 esw_cfg.pci_func = pci_func;
882 esw_cfg.vlan_id = npar->pvid;
Rajesh Borundia73733732010-08-31 17:17:50 +0000883 esw_cfg.mac_override = npar->mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000884 esw_cfg.discard_tagged = npar->discard_tagged;
885 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
886 esw_cfg.offload_flags = npar->offload_flags;
887 esw_cfg.promisc_mode = npar->promisc_mode;
888 if (qlcnic_config_switch_port(adapter, &esw_cfg))
889 return -EIO;
890
891 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
892 if (qlcnic_config_switch_port(adapter, &esw_cfg))
893 return -EIO;
894
895 return 0;
896}
897
898static int
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000899qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
900{
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000901 int i, err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000902 struct qlcnic_npar_info *npar;
903 struct qlcnic_info nic_info;
904
Rajesh Borundia174240a2010-08-31 17:17:47 +0000905 if (!adapter->need_fw_reset)
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000906 return 0;
907
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000908 /* Set the NPAR config data after FW reset */
909 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
910 npar = &adapter->npars[i];
911 if (npar->type != QLCNIC_TYPE_NIC)
912 continue;
913 err = qlcnic_get_nic_info(adapter, &nic_info, i);
914 if (err)
915 return err;
916 nic_info.min_tx_bw = npar->min_bw;
917 nic_info.max_tx_bw = npar->max_bw;
918 err = qlcnic_set_nic_info(adapter, &nic_info);
919 if (err)
920 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000921
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000922 if (npar->enable_pm) {
923 err = qlcnic_config_port_mirroring(adapter,
924 npar->dest_npar, 1, i);
925 if (err)
926 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000927 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000928 err = qlcnic_reset_eswitch_config(adapter, npar, i);
929 if (err)
930 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000931 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000932 return 0;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000933}
934
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +0000935static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
936{
937 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
938 u32 npar_state;
939
940 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
941 return 0;
942
943 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
944 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
945 msleep(1000);
946 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
947 }
948 if (!npar_opt_timeo) {
949 dev_err(&adapter->pdev->dev,
950 "Waiting for NPAR state to opertional timeout\n");
951 return -EIO;
952 }
953 return 0;
954}
955
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000956static int
Rajesh Borundia174240a2010-08-31 17:17:47 +0000957qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
958{
959 int err;
960
961 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
962 adapter->op_mode != QLCNIC_MGMT_FUNC)
963 return 0;
964
965 err = qlcnic_set_default_offload_settings(adapter);
966 if (err)
967 return err;
968
969 err = qlcnic_reset_npar_config(adapter);
970 if (err)
971 return err;
972
973 qlcnic_dev_set_npar_ready(adapter);
974
975 return err;
976}
977
978static int
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000979qlcnic_start_firmware(struct qlcnic_adapter *adapter)
980{
Sony Chackod4066832010-08-19 05:08:31 +0000981 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000982
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000983 err = qlcnic_can_start_firmware(adapter);
984 if (err < 0)
985 return err;
986 else if (!err)
Sony Chackod4066832010-08-19 05:08:31 +0000987 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000988
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000989 if (load_fw_file)
990 qlcnic_request_firmware(adapter);
schacko8f891382010-06-17 02:56:40 +0000991 else {
Sony Chacko8cfdce02010-08-26 14:02:41 +0000992 err = qlcnic_check_flash_fw_ver(adapter);
993 if (err)
schacko8f891382010-06-17 02:56:40 +0000994 goto err_out;
995
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000996 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
schacko8f891382010-06-17 02:56:40 +0000997 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000998
999 err = qlcnic_need_fw_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001000 if (err == 0)
Sony Chacko4e708122010-08-31 17:17:44 +00001001 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001002
Sony Chackod4066832010-08-19 05:08:31 +00001003 err = qlcnic_pinit_from_rom(adapter);
1004 if (err)
1005 goto err_out;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001006
1007 err = qlcnic_load_firmware(adapter);
1008 if (err)
1009 goto err_out;
1010
1011 qlcnic_release_firmware(adapter);
Sony Chackod4066832010-08-19 05:08:31 +00001012 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001013
Sony Chackod4066832010-08-19 05:08:31 +00001014check_fw_status:
1015 err = qlcnic_check_fw_status(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001016 if (err)
1017 goto err_out;
1018
1019 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001020 qlcnic_idc_debug_info(adapter, 1);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00001021
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001022 err = qlcnic_check_eswitch_mode(adapter);
1023 if (err) {
1024 dev_err(&adapter->pdev->dev,
1025 "Memory allocation failed for eswitch\n");
1026 goto err_out;
1027 }
Rajesh Borundia174240a2010-08-31 17:17:47 +00001028 err = qlcnic_set_mgmt_operations(adapter);
1029 if (err)
1030 goto err_out;
1031
1032 qlcnic_check_options(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001033 adapter->need_fw_reset = 0;
1034
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001035 qlcnic_release_firmware(adapter);
1036 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001037
1038err_out:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001039 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1040 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001041
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001042 qlcnic_release_firmware(adapter);
1043 return err;
1044}
1045
1046static int
1047qlcnic_request_irq(struct qlcnic_adapter *adapter)
1048{
1049 irq_handler_t handler;
1050 struct qlcnic_host_sds_ring *sds_ring;
1051 int err, ring;
1052
1053 unsigned long flags = 0;
1054 struct net_device *netdev = adapter->netdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001055 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001056
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001057 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1058 handler = qlcnic_tmp_intr;
1059 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1060 flags |= IRQF_SHARED;
1061
1062 } else {
1063 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1064 handler = qlcnic_msix_intr;
1065 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1066 handler = qlcnic_msi_intr;
1067 else {
1068 flags |= IRQF_SHARED;
1069 handler = qlcnic_intr;
1070 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001071 }
1072 adapter->irq = netdev->irq;
1073
1074 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1075 sds_ring = &recv_ctx->sds_rings[ring];
1076 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1077 err = request_irq(sds_ring->irq, handler,
1078 flags, sds_ring->name, sds_ring);
1079 if (err)
1080 return err;
1081 }
1082
1083 return 0;
1084}
1085
1086static void
1087qlcnic_free_irq(struct qlcnic_adapter *adapter)
1088{
1089 int ring;
1090 struct qlcnic_host_sds_ring *sds_ring;
1091
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001092 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001093
1094 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1095 sds_ring = &recv_ctx->sds_rings[ring];
1096 free_irq(sds_ring->irq, sds_ring);
1097 }
1098}
1099
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001100static int
1101__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1102{
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001103 int ring;
1104 struct qlcnic_host_rds_ring *rds_ring;
1105
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001106 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1107 return -EIO;
1108
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001109 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1110 return 0;
Rajesh Borundia0325d692010-08-19 05:08:26 +00001111 if (qlcnic_set_eswitch_port_config(adapter))
1112 return -EIO;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001113
1114 if (qlcnic_fw_create_ctx(adapter))
1115 return -EIO;
1116
1117 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001118 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1119 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001120 }
1121
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001122 qlcnic_set_multi(netdev);
1123 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1124
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001125 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001126
1127 if (adapter->max_sds_rings > 1)
1128 qlcnic_config_rss(adapter, 1);
1129
1130 qlcnic_config_intr_coalesce(adapter);
1131
Sucheta Chakraborty24763d82010-08-17 00:34:25 +00001132 if (netdev->features & NETIF_F_LRO)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001133 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1134
1135 qlcnic_napi_enable(adapter);
1136
1137 qlcnic_linkevent_request(adapter, 1);
1138
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001139 adapter->reset_context = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001140 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1141 return 0;
1142}
1143
1144/* Usage: During resume and firmware recovery module.*/
1145
1146static int
1147qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1148{
1149 int err = 0;
1150
1151 rtnl_lock();
1152 if (netif_running(netdev))
1153 err = __qlcnic_up(adapter, netdev);
1154 rtnl_unlock();
1155
1156 return err;
1157}
1158
1159static void
1160__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1161{
1162 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1163 return;
1164
1165 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1166 return;
1167
1168 smp_mb();
1169 spin_lock(&adapter->tx_clean_lock);
1170 netif_carrier_off(netdev);
1171 netif_tx_disable(netdev);
1172
1173 qlcnic_free_mac_list(adapter);
1174
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001175 if (adapter->fhash.fnum)
1176 qlcnic_delete_lb_filters(adapter);
1177
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001178 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1179
1180 qlcnic_napi_disable(adapter);
1181
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001182 qlcnic_fw_destroy_ctx(adapter);
1183
1184 qlcnic_reset_rx_buffers_list(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001185 qlcnic_release_tx_buffers(adapter);
1186 spin_unlock(&adapter->tx_clean_lock);
1187}
1188
1189/* Usage: During suspend and firmware recovery module */
1190
1191static void
1192qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1193{
1194 rtnl_lock();
1195 if (netif_running(netdev))
1196 __qlcnic_down(adapter, netdev);
1197 rtnl_unlock();
1198
1199}
1200
1201static int
1202qlcnic_attach(struct qlcnic_adapter *adapter)
1203{
1204 struct net_device *netdev = adapter->netdev;
1205 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001206 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001207
1208 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1209 return 0;
1210
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001211 err = qlcnic_napi_add(adapter, netdev);
1212 if (err)
1213 return err;
1214
1215 err = qlcnic_alloc_sw_resources(adapter);
1216 if (err) {
1217 dev_err(&pdev->dev, "Error in setting sw resources\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001218 goto err_out_napi_del;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001219 }
1220
1221 err = qlcnic_alloc_hw_resources(adapter);
1222 if (err) {
1223 dev_err(&pdev->dev, "Error in setting hw resources\n");
1224 goto err_out_free_sw;
1225 }
1226
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001227 err = qlcnic_request_irq(adapter);
1228 if (err) {
1229 dev_err(&pdev->dev, "failed to setup interrupt\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001230 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001231 }
1232
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001233 qlcnic_create_sysfs_entries(adapter);
1234
1235 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1236 return 0;
1237
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001238err_out_free_hw:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001239 qlcnic_free_hw_resources(adapter);
1240err_out_free_sw:
1241 qlcnic_free_sw_resources(adapter);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001242err_out_napi_del:
1243 qlcnic_napi_del(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001244 return err;
1245}
1246
1247static void
1248qlcnic_detach(struct qlcnic_adapter *adapter)
1249{
1250 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1251 return;
1252
1253 qlcnic_remove_sysfs_entries(adapter);
1254
1255 qlcnic_free_hw_resources(adapter);
1256 qlcnic_release_rx_buffers(adapter);
1257 qlcnic_free_irq(adapter);
1258 qlcnic_napi_del(adapter);
1259 qlcnic_free_sw_resources(adapter);
1260
1261 adapter->is_up = 0;
1262}
1263
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001264void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1265{
1266 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1267 struct qlcnic_host_sds_ring *sds_ring;
1268 int ring;
1269
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001270 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001271 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1272 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001273 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001274 qlcnic_disable_int(sds_ring);
1275 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001276 }
1277
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001278 qlcnic_fw_destroy_ctx(adapter);
1279
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001280 qlcnic_detach(adapter);
1281
1282 adapter->diag_test = 0;
1283 adapter->max_sds_rings = max_sds_rings;
1284
1285 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001286 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001287
1288 if (netif_running(netdev))
1289 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001290out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001291 netif_device_attach(netdev);
1292}
1293
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001294static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1295{
1296 int err = 0;
1297 adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1298 GFP_KERNEL);
1299 if (!adapter->ahw) {
1300 dev_err(&adapter->pdev->dev,
1301 "Failed to allocate recv ctx resources for adapter\n");
1302 err = -ENOMEM;
1303 goto err_out;
1304 }
1305 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1306 GFP_KERNEL);
1307 if (!adapter->recv_ctx) {
1308 dev_err(&adapter->pdev->dev,
1309 "Failed to allocate recv ctx resources for adapter\n");
1310 kfree(adapter->ahw);
1311 adapter->ahw = NULL;
1312 err = -ENOMEM;
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001313 goto err_out;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001314 }
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001315 /* Initialize interrupt coalesce parameters */
1316 adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1317 adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1318 adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001319err_out:
1320 return err;
1321}
1322
1323static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1324{
1325 kfree(adapter->recv_ctx);
1326 adapter->recv_ctx = NULL;
1327
1328 kfree(adapter->ahw);
1329 adapter->ahw = NULL;
1330}
1331
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001332int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1333{
1334 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1335 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001336 struct qlcnic_host_rds_ring *rds_ring;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001337 int ring;
1338 int ret;
1339
1340 netif_device_detach(netdev);
1341
1342 if (netif_running(netdev))
1343 __qlcnic_down(adapter, netdev);
1344
1345 qlcnic_detach(adapter);
1346
1347 adapter->max_sds_rings = 1;
1348 adapter->diag_test = test;
1349
1350 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001351 if (ret) {
1352 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001353 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001354 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001355
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001356 ret = qlcnic_fw_create_ctx(adapter);
1357 if (ret) {
1358 qlcnic_detach(adapter);
Sony Chacko57e46242010-07-24 18:32:18 +00001359 netif_device_attach(netdev);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001360 return ret;
1361 }
1362
1363 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001364 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1365 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001366 }
1367
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001368 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1369 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001370 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001371 qlcnic_enable_int(sds_ring);
1372 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001373 }
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001374 set_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001375
1376 return 0;
1377}
1378
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001379/* Reset context in hardware only */
1380static int
1381qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1382{
1383 struct net_device *netdev = adapter->netdev;
1384
1385 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1386 return -EBUSY;
1387
1388 netif_device_detach(netdev);
1389
1390 qlcnic_down(adapter, netdev);
1391
1392 qlcnic_up(adapter, netdev);
1393
1394 netif_device_attach(netdev);
1395
1396 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1397 return 0;
1398}
1399
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001400int
1401qlcnic_reset_context(struct qlcnic_adapter *adapter)
1402{
1403 int err = 0;
1404 struct net_device *netdev = adapter->netdev;
1405
1406 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1407 return -EBUSY;
1408
1409 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1410
1411 netif_device_detach(netdev);
1412
1413 if (netif_running(netdev))
1414 __qlcnic_down(adapter, netdev);
1415
1416 qlcnic_detach(adapter);
1417
1418 if (netif_running(netdev)) {
1419 err = qlcnic_attach(adapter);
1420 if (!err)
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001421 __qlcnic_up(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001422 }
1423
1424 netif_device_attach(netdev);
1425 }
1426
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001427 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1428 return err;
1429}
1430
1431static int
1432qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001433 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001434{
1435 int err;
1436 struct pci_dev *pdev = adapter->pdev;
1437
1438 adapter->rx_csum = 1;
1439 adapter->mc_enabled = 0;
1440 adapter->max_mc_count = 38;
1441
1442 netdev->netdev_ops = &qlcnic_netdev_ops;
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001443 netdev->watchdog_timeo = 5*HZ;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001444
1445 qlcnic_change_mtu(netdev, netdev->mtu);
1446
1447 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1448
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001449 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001450 NETIF_F_IPV6_CSUM | NETIF_F_GRO | NETIF_F_HW_VLAN_RX);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001451 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +00001452 NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
Anirban Chakrabortyac8d0c42010-07-09 13:14:58 +00001453
1454 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
1455 netdev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
1456 netdev->vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
1457 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001458
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001459 if (pci_using_dac) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001460 netdev->features |= NETIF_F_HIGHDMA;
1461 netdev->vlan_features |= NETIF_F_HIGHDMA;
1462 }
1463
1464 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1465 netdev->features |= (NETIF_F_HW_VLAN_TX);
1466
1467 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1468 netdev->features |= NETIF_F_LRO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001469 netdev->irq = adapter->msix_entries[0].vector;
1470
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001471 netif_carrier_off(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001472
1473 err = register_netdev(netdev);
1474 if (err) {
1475 dev_err(&pdev->dev, "failed to register net device\n");
1476 return err;
1477 }
1478
1479 return 0;
1480}
1481
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001482static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1483{
1484 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1485 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1486 *pci_using_dac = 1;
1487 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1488 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1489 *pci_using_dac = 0;
1490 else {
1491 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1492 return -EIO;
1493 }
1494
1495 return 0;
1496}
1497
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001498static int __devinit
1499qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1500{
1501 struct net_device *netdev = NULL;
1502 struct qlcnic_adapter *adapter = NULL;
1503 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001504 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001505 uint8_t pci_using_dac;
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001506 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001507
1508 err = pci_enable_device(pdev);
1509 if (err)
1510 return err;
1511
1512 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1513 err = -ENODEV;
1514 goto err_out_disable_pdev;
1515 }
1516
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001517 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1518 if (err)
1519 goto err_out_disable_pdev;
1520
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001521 err = pci_request_regions(pdev, qlcnic_driver_name);
1522 if (err)
1523 goto err_out_disable_pdev;
1524
1525 pci_set_master(pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001526 pci_enable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001527
1528 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1529 if (!netdev) {
1530 dev_err(&pdev->dev, "failed to allocate net_device\n");
1531 err = -ENOMEM;
1532 goto err_out_free_res;
1533 }
1534
1535 SET_NETDEV_DEV(netdev, &pdev->dev);
1536
1537 adapter = netdev_priv(netdev);
1538 adapter->netdev = netdev;
1539 adapter->pdev = pdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001540
1541 if (qlcnic_alloc_adapter_resources(adapter))
1542 goto err_out_free_netdev;
1543
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001544 adapter->dev_rst_time = jiffies;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001545 revision_id = pdev->revision;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001546 adapter->ahw->revision_id = revision_id;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001547
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001548 rwlock_init(&adapter->ahw->crb_lock);
1549 mutex_init(&adapter->ahw->mem_lock);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001550
1551 spin_lock_init(&adapter->tx_clean_lock);
1552 INIT_LIST_HEAD(&adapter->mac_list);
1553
1554 err = qlcnic_setup_pci_map(adapter);
1555 if (err)
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001556 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001557
1558 /* This will be reset for mezz cards */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001559 adapter->portnum = adapter->ahw->pci_func;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001560
1561 err = qlcnic_get_board_info(adapter);
1562 if (err) {
1563 dev_err(&pdev->dev, "Error getting board config info.\n");
1564 goto err_out_iounmap;
1565 }
1566
Sony Chacko8cfdce02010-08-26 14:02:41 +00001567 err = qlcnic_setup_idc_param(adapter);
1568 if (err)
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +00001569 goto err_out_iounmap;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001570
Rajesh Borundia1dc0f3c2010-12-16 22:59:02 +00001571 adapter->flags |= QLCNIC_NEED_FLR;
Rajesh Borundiab0044bc2010-11-23 01:25:21 +00001572
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001573 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001574 if (err) {
1575 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001576 goto err_out_decr_ref;
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001577 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001578
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001579 if (qlcnic_read_mac_addr(adapter))
1580 dev_warn(&pdev->dev, "failed to read mac addr\n");
1581
1582 if (adapter->portnum == 0) {
1583 get_brd_name(adapter, brd_name);
1584
1585 pr_info("%s: %s Board Chip rev 0x%x\n",
1586 module_name(THIS_MODULE),
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001587 brd_name, adapter->ahw->revision_id);
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001588 }
1589
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001590 qlcnic_clear_stats(adapter);
1591
1592 qlcnic_setup_intr(adapter);
1593
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001594 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001595 if (err)
1596 goto err_out_disable_msi;
1597
1598 pci_set_drvdata(pdev, adapter);
1599
1600 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1601
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001602 switch (adapter->ahw->port_type) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001603 case QLCNIC_GBE:
1604 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1605 adapter->netdev->name);
1606 break;
1607 case QLCNIC_XGBE:
1608 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1609 adapter->netdev->name);
1610 break;
1611 }
1612
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001613 qlcnic_alloc_lb_filters_mem(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001614 qlcnic_create_diag_entries(adapter);
1615
1616 return 0;
1617
1618err_out_disable_msi:
1619 qlcnic_teardown_intr(adapter);
1620
1621err_out_decr_ref:
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001622 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001623
1624err_out_iounmap:
1625 qlcnic_cleanup_pci_map(adapter);
1626
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001627err_out_free_hw:
1628 qlcnic_free_adapter_resources(adapter);
1629
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001630err_out_free_netdev:
1631 free_netdev(netdev);
1632
1633err_out_free_res:
1634 pci_release_regions(pdev);
1635
1636err_out_disable_pdev:
1637 pci_set_drvdata(pdev, NULL);
1638 pci_disable_device(pdev);
1639 return err;
1640}
1641
1642static void __devexit qlcnic_remove(struct pci_dev *pdev)
1643{
1644 struct qlcnic_adapter *adapter;
1645 struct net_device *netdev;
1646
1647 adapter = pci_get_drvdata(pdev);
1648 if (adapter == NULL)
1649 return;
1650
1651 netdev = adapter->netdev;
1652
1653 qlcnic_cancel_fw_work(adapter);
1654
1655 unregister_netdev(netdev);
1656
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001657 qlcnic_detach(adapter);
1658
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001659 if (adapter->npars != NULL)
1660 kfree(adapter->npars);
1661 if (adapter->eswitch != NULL)
1662 kfree(adapter->eswitch);
1663
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001664 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001665
1666 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1667
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001668 qlcnic_free_lb_filters_mem(adapter);
1669
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001670 qlcnic_teardown_intr(adapter);
1671
1672 qlcnic_remove_diag_entries(adapter);
1673
1674 qlcnic_cleanup_pci_map(adapter);
1675
1676 qlcnic_release_firmware(adapter);
1677
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001678 pci_disable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001679 pci_release_regions(pdev);
1680 pci_disable_device(pdev);
1681 pci_set_drvdata(pdev, NULL);
1682
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001683 qlcnic_free_adapter_resources(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001684 free_netdev(netdev);
1685}
1686static int __qlcnic_shutdown(struct pci_dev *pdev)
1687{
1688 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1689 struct net_device *netdev = adapter->netdev;
1690 int retval;
1691
1692 netif_device_detach(netdev);
1693
1694 qlcnic_cancel_fw_work(adapter);
1695
1696 if (netif_running(netdev))
1697 qlcnic_down(adapter, netdev);
1698
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001699 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001700
1701 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1702
1703 retval = pci_save_state(pdev);
1704 if (retval)
1705 return retval;
1706
1707 if (qlcnic_wol_supported(adapter)) {
1708 pci_enable_wake(pdev, PCI_D3cold, 1);
1709 pci_enable_wake(pdev, PCI_D3hot, 1);
1710 }
1711
1712 return 0;
1713}
1714
1715static void qlcnic_shutdown(struct pci_dev *pdev)
1716{
1717 if (__qlcnic_shutdown(pdev))
1718 return;
1719
1720 pci_disable_device(pdev);
1721}
1722
1723#ifdef CONFIG_PM
1724static int
1725qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1726{
1727 int retval;
1728
1729 retval = __qlcnic_shutdown(pdev);
1730 if (retval)
1731 return retval;
1732
1733 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1734 return 0;
1735}
1736
1737static int
1738qlcnic_resume(struct pci_dev *pdev)
1739{
1740 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1741 struct net_device *netdev = adapter->netdev;
1742 int err;
1743
1744 err = pci_enable_device(pdev);
1745 if (err)
1746 return err;
1747
1748 pci_set_power_state(pdev, PCI_D0);
1749 pci_set_master(pdev);
1750 pci_restore_state(pdev);
1751
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001752 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001753 if (err) {
1754 dev_err(&pdev->dev, "failed to start firmware\n");
1755 return err;
1756 }
1757
1758 if (netif_running(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001759 err = qlcnic_up(adapter, netdev);
1760 if (err)
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001761 goto done;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001762
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00001763 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001764 }
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001765done:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001766 netif_device_attach(netdev);
1767 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1768 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001769}
1770#endif
1771
1772static int qlcnic_open(struct net_device *netdev)
1773{
1774 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1775 int err;
1776
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001777 err = qlcnic_attach(adapter);
1778 if (err)
1779 return err;
1780
1781 err = __qlcnic_up(adapter, netdev);
1782 if (err)
1783 goto err_out;
1784
1785 netif_start_queue(netdev);
1786
1787 return 0;
1788
1789err_out:
1790 qlcnic_detach(adapter);
1791 return err;
1792}
1793
1794/*
1795 * qlcnic_close - Disables a network interface entry point
1796 */
1797static int qlcnic_close(struct net_device *netdev)
1798{
1799 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1800
1801 __qlcnic_down(adapter, netdev);
1802 return 0;
1803}
1804
1805static void
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001806qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1807{
1808 void *head;
1809 int i;
1810
1811 if (!qlcnic_mac_learn)
1812 return;
1813
1814 spin_lock_init(&adapter->mac_learn_lock);
1815
1816 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1817 GFP_KERNEL);
1818 if (!head)
1819 return;
1820
1821 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1822 adapter->fhash.fhead = (struct hlist_head *)head;
1823
1824 for (i = 0; i < adapter->fhash.fmax; i++)
1825 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1826}
1827
1828static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1829{
1830 if (adapter->fhash.fmax && adapter->fhash.fhead)
1831 kfree(adapter->fhash.fhead);
1832
1833 adapter->fhash.fhead = NULL;
1834 adapter->fhash.fmax = 0;
1835}
1836
1837static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001838 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001839{
1840 struct cmd_desc_type0 *hwdesc;
1841 struct qlcnic_nic_req *req;
1842 struct qlcnic_mac_req *mac_req;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001843 struct qlcnic_vlan_req *vlan_req;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001844 u32 producer;
1845 u64 word;
1846
1847 producer = tx_ring->producer;
1848 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1849
1850 req = (struct qlcnic_nic_req *)hwdesc;
1851 memset(req, 0, sizeof(struct qlcnic_nic_req));
1852 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1853
1854 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1855 req->req_hdr = cpu_to_le64(word);
1856
1857 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001858 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001859 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1860
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001861 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1862 vlan_req->vlan_id = vlan_id;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001863
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001864 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001865 smp_mb();
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001866}
1867
1868#define QLCNIC_MAC_HASH(MAC)\
1869 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1870
1871static void
1872qlcnic_send_filter(struct qlcnic_adapter *adapter,
1873 struct qlcnic_host_tx_ring *tx_ring,
1874 struct cmd_desc_type0 *first_desc,
1875 struct sk_buff *skb)
1876{
1877 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1878 struct qlcnic_filter *fil, *tmp_fil;
1879 struct hlist_node *tmp_hnode, *n;
1880 struct hlist_head *head;
1881 u64 src_addr = 0;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001882 __le16 vlan_id = 0;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001883 u8 hindex;
1884
1885 if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1886 return;
1887
1888 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1889 return;
1890
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001891 /* Only NPAR capable devices support vlan based learning*/
1892 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1893 vlan_id = first_desc->vlan_TCI;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001894 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1895 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1896 head = &(adapter->fhash.fhead[hindex]);
1897
1898 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001899 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1900 tmp_fil->vlan_id == vlan_id) {
amit salechae5edb7b2010-10-26 17:53:07 +00001901
1902 if (jiffies >
1903 (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1904 qlcnic_change_filter(adapter, src_addr, vlan_id,
1905 tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001906 tmp_fil->ftime = jiffies;
1907 return;
1908 }
1909 }
1910
1911 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1912 if (!fil)
1913 return;
1914
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001915 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001916
1917 fil->ftime = jiffies;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001918 fil->vlan_id = vlan_id;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001919 memcpy(fil->faddr, &src_addr, ETH_ALEN);
1920 spin_lock(&adapter->mac_learn_lock);
1921 hlist_add_head(&(fil->fnode), head);
1922 adapter->fhash.fnum++;
1923 spin_unlock(&adapter->mac_learn_lock);
1924}
1925
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001926static int
1927qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001928 struct cmd_desc_type0 *first_desc,
1929 struct sk_buff *skb)
1930{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001931 u8 opcode = 0, hdr_len = 0;
1932 u16 flags = 0, vlan_tci = 0;
1933 int copied, offset, copy_len;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001934 struct cmd_desc_type0 *hwdesc;
1935 struct vlan_ethhdr *vh;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001936 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1937 u16 protocol = ntohs(skb->protocol);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001938 u32 producer = tx_ring->producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001939
1940 if (protocol == ETH_P_8021Q) {
1941 vh = (struct vlan_ethhdr *)skb->data;
1942 flags = FLAGS_VLAN_TAGGED;
1943 vlan_tci = vh->h_vlan_TCI;
1944 } else if (vlan_tx_tag_present(skb)) {
1945 flags = FLAGS_VLAN_OOB;
1946 vlan_tci = vlan_tx_tag_get(skb);
1947 }
1948 if (unlikely(adapter->pvid)) {
1949 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
1950 return -EIO;
1951 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
1952 goto set_flags;
1953
1954 flags = FLAGS_VLAN_OOB;
1955 vlan_tci = adapter->pvid;
1956 }
1957set_flags:
1958 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
1959 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001960
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001961 if (*(skb->data) & BIT_0) {
1962 flags |= BIT_0;
1963 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
1964 }
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001965 opcode = TX_ETHER_PKT;
1966 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001967 skb_shinfo(skb)->gso_size > 0) {
1968
1969 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1970
1971 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1972 first_desc->total_hdr_length = hdr_len;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001973
1974 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
1975
1976 /* For LSO, we need to copy the MAC/IP/TCP headers into
1977 * the descriptor ring */
1978 copied = 0;
1979 offset = 2;
1980
1981 if (flags & FLAGS_VLAN_OOB) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001982 first_desc->total_hdr_length += VLAN_HLEN;
1983 first_desc->tcp_hdr_offset = VLAN_HLEN;
1984 first_desc->ip_hdr_offset = VLAN_HLEN;
1985 /* Only in case of TSO on vlan device */
1986 flags |= FLAGS_VLAN_TAGGED;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001987
1988 /* Create a TSO vlan header template for firmware */
1989
1990 hwdesc = &tx_ring->desc_head[producer];
1991 tx_ring->cmd_buf_arr[producer].skb = NULL;
1992
1993 copy_len = min((int)sizeof(struct cmd_desc_type0) -
1994 offset, hdr_len + VLAN_HLEN);
1995
1996 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
1997 skb_copy_from_linear_data(skb, vh, 12);
1998 vh->h_vlan_proto = htons(ETH_P_8021Q);
1999 vh->h_vlan_TCI = htons(vlan_tci);
2000
2001 skb_copy_from_linear_data_offset(skb, 12,
2002 (char *)vh + 16, copy_len - 16);
2003
2004 copied = copy_len - VLAN_HLEN;
2005 offset = 0;
2006
2007 producer = get_next_index(producer, tx_ring->num_desc);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002008 }
2009
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002010 while (copied < hdr_len) {
2011
2012 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2013 offset, (hdr_len - copied));
2014
2015 hwdesc = &tx_ring->desc_head[producer];
2016 tx_ring->cmd_buf_arr[producer].skb = NULL;
2017
2018 skb_copy_from_linear_data_offset(skb, copied,
2019 (char *) hwdesc + offset, copy_len);
2020
2021 copied += copy_len;
2022 offset = 0;
2023
2024 producer = get_next_index(producer, tx_ring->num_desc);
2025 }
2026
2027 tx_ring->producer = producer;
2028 smp_mb();
2029 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002030
2031 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2032 u8 l4proto;
2033
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002034 if (protocol == ETH_P_IP) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002035 l4proto = ip_hdr(skb)->protocol;
2036
2037 if (l4proto == IPPROTO_TCP)
2038 opcode = TX_TCP_PKT;
2039 else if (l4proto == IPPROTO_UDP)
2040 opcode = TX_UDP_PKT;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002041 } else if (protocol == ETH_P_IPV6) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002042 l4proto = ipv6_hdr(skb)->nexthdr;
2043
2044 if (l4proto == IPPROTO_TCP)
2045 opcode = TX_TCPV6_PKT;
2046 else if (l4proto == IPPROTO_UDP)
2047 opcode = TX_UDPV6_PKT;
2048 }
2049 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002050 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2051 first_desc->ip_hdr_offset += skb_network_offset(skb);
2052 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2053
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002054 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002055}
2056
2057static int
2058qlcnic_map_tx_skb(struct pci_dev *pdev,
2059 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2060{
2061 struct qlcnic_skb_frag *nf;
2062 struct skb_frag_struct *frag;
2063 int i, nr_frags;
2064 dma_addr_t map;
2065
2066 nr_frags = skb_shinfo(skb)->nr_frags;
2067 nf = &pbuf->frag_array[0];
2068
2069 map = pci_map_single(pdev, skb->data,
2070 skb_headlen(skb), PCI_DMA_TODEVICE);
2071 if (pci_dma_mapping_error(pdev, map))
2072 goto out_err;
2073
2074 nf->dma = map;
2075 nf->length = skb_headlen(skb);
2076
2077 for (i = 0; i < nr_frags; i++) {
2078 frag = &skb_shinfo(skb)->frags[i];
2079 nf = &pbuf->frag_array[i+1];
2080
2081 map = pci_map_page(pdev, frag->page, frag->page_offset,
2082 frag->size, PCI_DMA_TODEVICE);
2083 if (pci_dma_mapping_error(pdev, map))
2084 goto unwind;
2085
2086 nf->dma = map;
2087 nf->length = frag->size;
2088 }
2089
2090 return 0;
2091
2092unwind:
2093 while (--i >= 0) {
2094 nf = &pbuf->frag_array[i+1];
2095 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2096 }
2097
2098 nf = &pbuf->frag_array[0];
2099 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2100
2101out_err:
2102 return -ENOMEM;
2103}
2104
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002105static void
2106qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2107 struct qlcnic_cmd_buffer *pbuf)
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002108{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002109 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2110 int nr_frags = skb_shinfo(skb)->nr_frags;
2111 int i;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002112
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002113 for (i = 0; i < nr_frags; i++) {
2114 nf = &pbuf->frag_array[i+1];
2115 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002116 }
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002117
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002118 nf = &pbuf->frag_array[0];
2119 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002120}
2121
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002122static inline void
2123qlcnic_clear_cmddesc(u64 *desc)
2124{
2125 desc[0] = 0ULL;
2126 desc[2] = 0ULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002127 desc[7] = 0ULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002128}
2129
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00002130netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002131qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2132{
2133 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2134 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2135 struct qlcnic_cmd_buffer *pbuf;
2136 struct qlcnic_skb_frag *buffrag;
2137 struct cmd_desc_type0 *hwdesc, *first_desc;
2138 struct pci_dev *pdev;
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002139 struct ethhdr *phdr;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002140 int i, k;
2141
2142 u32 producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002143 int frag_count;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002144 u32 num_txd = tx_ring->num_desc;
2145
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00002146 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2147 netif_stop_queue(netdev);
2148 return NETDEV_TX_BUSY;
2149 }
2150
Sony Chackofe4d4342010-08-19 05:08:27 +00002151 if (adapter->flags & QLCNIC_MACSPOOF) {
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002152 phdr = (struct ethhdr *)skb->data;
2153 if (compare_ether_addr(phdr->h_source,
Sony Chackofe4d4342010-08-19 05:08:27 +00002154 adapter->mac_addr))
2155 goto drop_packet;
2156 }
2157
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002158 frag_count = skb_shinfo(skb)->nr_frags + 1;
2159
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002160 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002161 netif_stop_queue(netdev);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002162 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2163 netif_start_queue(netdev);
2164 else {
2165 adapter->stats.xmit_off++;
2166 return NETDEV_TX_BUSY;
2167 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002168 }
2169
2170 producer = tx_ring->producer;
2171 pbuf = &tx_ring->cmd_buf_arr[producer];
2172
2173 pdev = adapter->pdev;
2174
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002175 first_desc = hwdesc = &tx_ring->desc_head[producer];
2176 qlcnic_clear_cmddesc((u64 *)hwdesc);
2177
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002178 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2179 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002180 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002181 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002182
2183 pbuf->skb = skb;
2184 pbuf->frag_count = frag_count;
2185
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002186 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2187 qlcnic_set_tx_port(first_desc, adapter->portnum);
2188
2189 for (i = 0; i < frag_count; i++) {
2190
2191 k = i % 4;
2192
2193 if ((k == 0) && (i > 0)) {
2194 /* move to next desc.*/
2195 producer = get_next_index(producer, num_txd);
2196 hwdesc = &tx_ring->desc_head[producer];
2197 qlcnic_clear_cmddesc((u64 *)hwdesc);
2198 tx_ring->cmd_buf_arr[producer].skb = NULL;
2199 }
2200
2201 buffrag = &pbuf->frag_array[i];
2202
2203 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2204 switch (k) {
2205 case 0:
2206 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2207 break;
2208 case 1:
2209 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2210 break;
2211 case 2:
2212 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2213 break;
2214 case 3:
2215 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2216 break;
2217 }
2218 }
2219
2220 tx_ring->producer = get_next_index(producer, num_txd);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002221 smp_mb();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002222
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002223 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2224 goto unwind_buff;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002225
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002226 if (qlcnic_mac_learn)
2227 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2228
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002229 qlcnic_update_cmd_producer(adapter, tx_ring);
2230
2231 adapter->stats.txbytes += skb->len;
2232 adapter->stats.xmitcalled++;
2233
2234 return NETDEV_TX_OK;
2235
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002236unwind_buff:
2237 qlcnic_unmap_buffers(pdev, skb, pbuf);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002238drop_packet:
2239 adapter->stats.txdropped++;
2240 dev_kfree_skb_any(skb);
2241 return NETDEV_TX_OK;
2242}
2243
2244static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2245{
2246 struct net_device *netdev = adapter->netdev;
2247 u32 temp, temp_state, temp_val;
2248 int rv = 0;
2249
2250 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2251
2252 temp_state = qlcnic_get_temp_state(temp);
2253 temp_val = qlcnic_get_temp_val(temp);
2254
2255 if (temp_state == QLCNIC_TEMP_PANIC) {
2256 dev_err(&netdev->dev,
2257 "Device temperature %d degrees C exceeds"
2258 " maximum allowed. Hardware has been shut down.\n",
2259 temp_val);
2260 rv = 1;
2261 } else if (temp_state == QLCNIC_TEMP_WARN) {
2262 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2263 dev_err(&netdev->dev,
2264 "Device temperature %d degrees C "
2265 "exceeds operating range."
2266 " Immediate action needed.\n",
2267 temp_val);
2268 }
2269 } else {
2270 if (adapter->temp == QLCNIC_TEMP_WARN) {
2271 dev_info(&netdev->dev,
2272 "Device temperature is now %d degrees C"
2273 " in normal range.\n", temp_val);
2274 }
2275 }
2276 adapter->temp = temp_state;
2277 return rv;
2278}
2279
2280void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2281{
2282 struct net_device *netdev = adapter->netdev;
2283
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002284 if (adapter->ahw->linkup && !linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002285 netdev_info(netdev, "NIC Link is down\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002286 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002287 if (netif_running(netdev)) {
2288 netif_carrier_off(netdev);
2289 netif_stop_queue(netdev);
2290 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002291 } else if (!adapter->ahw->linkup && linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002292 netdev_info(netdev, "NIC Link is up\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002293 adapter->ahw->linkup = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002294 if (netif_running(netdev)) {
2295 netif_carrier_on(netdev);
2296 netif_wake_queue(netdev);
2297 }
2298 }
2299}
2300
2301static void qlcnic_tx_timeout(struct net_device *netdev)
2302{
2303 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2304
2305 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2306 return;
2307
2308 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002309
2310 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002311 adapter->need_fw_reset = 1;
2312 else
2313 adapter->reset_context = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002314}
2315
2316static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2317{
2318 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2319 struct net_device_stats *stats = &netdev->stats;
2320
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002321 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2322 stats->tx_packets = adapter->stats.xmitfinished;
Sucheta Chakraborty7e382592010-05-17 01:22:10 +00002323 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002324 stats->tx_bytes = adapter->stats.txbytes;
2325 stats->rx_dropped = adapter->stats.rxdropped;
2326 stats->tx_dropped = adapter->stats.txdropped;
2327
2328 return stats;
2329}
2330
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002331static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002332{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002333 u32 status;
2334
2335 status = readl(adapter->isr_int_vec);
2336
2337 if (!(status & adapter->int_vec_bit))
2338 return IRQ_NONE;
2339
2340 /* check interrupt state machine, to be sure */
2341 status = readl(adapter->crb_int_state_reg);
2342 if (!ISR_LEGACY_INT_TRIGGERED(status))
2343 return IRQ_NONE;
2344
2345 writel(0xffffffff, adapter->tgt_status_reg);
2346 /* read twice to ensure write is flushed */
2347 readl(adapter->isr_int_vec);
2348 readl(adapter->isr_int_vec);
2349
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002350 return IRQ_HANDLED;
2351}
2352
2353static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2354{
2355 struct qlcnic_host_sds_ring *sds_ring = data;
2356 struct qlcnic_adapter *adapter = sds_ring->adapter;
2357
2358 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2359 goto done;
2360 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2361 writel(0xffffffff, adapter->tgt_status_reg);
2362 goto done;
2363 }
2364
2365 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2366 return IRQ_NONE;
2367
2368done:
2369 adapter->diag_cnt++;
2370 qlcnic_enable_int(sds_ring);
2371 return IRQ_HANDLED;
2372}
2373
2374static irqreturn_t qlcnic_intr(int irq, void *data)
2375{
2376 struct qlcnic_host_sds_ring *sds_ring = data;
2377 struct qlcnic_adapter *adapter = sds_ring->adapter;
2378
2379 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2380 return IRQ_NONE;
2381
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002382 napi_schedule(&sds_ring->napi);
2383
2384 return IRQ_HANDLED;
2385}
2386
2387static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2388{
2389 struct qlcnic_host_sds_ring *sds_ring = data;
2390 struct qlcnic_adapter *adapter = sds_ring->adapter;
2391
2392 /* clear interrupt */
2393 writel(0xffffffff, adapter->tgt_status_reg);
2394
2395 napi_schedule(&sds_ring->napi);
2396 return IRQ_HANDLED;
2397}
2398
2399static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2400{
2401 struct qlcnic_host_sds_ring *sds_ring = data;
2402
2403 napi_schedule(&sds_ring->napi);
2404 return IRQ_HANDLED;
2405}
2406
2407static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2408{
2409 u32 sw_consumer, hw_consumer;
2410 int count = 0, i;
2411 struct qlcnic_cmd_buffer *buffer;
2412 struct pci_dev *pdev = adapter->pdev;
2413 struct net_device *netdev = adapter->netdev;
2414 struct qlcnic_skb_frag *frag;
2415 int done;
2416 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2417
2418 if (!spin_trylock(&adapter->tx_clean_lock))
2419 return 1;
2420
2421 sw_consumer = tx_ring->sw_consumer;
2422 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2423
2424 while (sw_consumer != hw_consumer) {
2425 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2426 if (buffer->skb) {
2427 frag = &buffer->frag_array[0];
2428 pci_unmap_single(pdev, frag->dma, frag->length,
2429 PCI_DMA_TODEVICE);
2430 frag->dma = 0ULL;
2431 for (i = 1; i < buffer->frag_count; i++) {
2432 frag++;
2433 pci_unmap_page(pdev, frag->dma, frag->length,
2434 PCI_DMA_TODEVICE);
2435 frag->dma = 0ULL;
2436 }
2437
2438 adapter->stats.xmitfinished++;
2439 dev_kfree_skb_any(buffer->skb);
2440 buffer->skb = NULL;
2441 }
2442
2443 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2444 if (++count >= MAX_STATUS_HANDLE)
2445 break;
2446 }
2447
2448 if (count && netif_running(netdev)) {
2449 tx_ring->sw_consumer = sw_consumer;
2450
2451 smp_mb();
2452
2453 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002454 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2455 netif_wake_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00002456 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002457 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002458 }
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002459 adapter->tx_timeo_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002460 }
2461 /*
2462 * If everything is freed up to consumer then check if the ring is full
2463 * If the ring is full then check if more needs to be freed and
2464 * schedule the call back again.
2465 *
2466 * This happens when there are 2 CPUs. One could be freeing and the
2467 * other filling it. If the ring is full when we get out of here and
2468 * the card has already interrupted the host then the host can miss the
2469 * interrupt.
2470 *
2471 * There is still a possible race condition and the host could miss an
2472 * interrupt. The card has to take care of this.
2473 */
2474 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2475 done = (sw_consumer == hw_consumer);
2476 spin_unlock(&adapter->tx_clean_lock);
2477
2478 return done;
2479}
2480
2481static int qlcnic_poll(struct napi_struct *napi, int budget)
2482{
2483 struct qlcnic_host_sds_ring *sds_ring =
2484 container_of(napi, struct qlcnic_host_sds_ring, napi);
2485
2486 struct qlcnic_adapter *adapter = sds_ring->adapter;
2487
2488 int tx_complete;
2489 int work_done;
2490
2491 tx_complete = qlcnic_process_cmd_ring(adapter);
2492
2493 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2494
2495 if ((work_done < budget) && tx_complete) {
2496 napi_complete(&sds_ring->napi);
2497 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2498 qlcnic_enable_int(sds_ring);
2499 }
2500
2501 return work_done;
2502}
2503
schacko8f891382010-06-17 02:56:40 +00002504static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2505{
2506 struct qlcnic_host_sds_ring *sds_ring =
2507 container_of(napi, struct qlcnic_host_sds_ring, napi);
2508
2509 struct qlcnic_adapter *adapter = sds_ring->adapter;
2510 int work_done;
2511
2512 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2513
2514 if (work_done < budget) {
2515 napi_complete(&sds_ring->napi);
2516 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2517 qlcnic_enable_int(sds_ring);
2518 }
2519
2520 return work_done;
2521}
2522
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002523#ifdef CONFIG_NET_POLL_CONTROLLER
2524static void qlcnic_poll_controller(struct net_device *netdev)
2525{
Yinglin Luanbf827912010-08-22 21:57:56 +00002526 int ring;
2527 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002528 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002529 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Yinglin Luanbf827912010-08-22 21:57:56 +00002530
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002531 disable_irq(adapter->irq);
Yinglin Luanbf827912010-08-22 21:57:56 +00002532 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2533 sds_ring = &recv_ctx->sds_rings[ring];
2534 qlcnic_intr(adapter->irq, sds_ring);
2535 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002536 enable_irq(adapter->irq);
2537}
2538#endif
2539
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002540static void
2541qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2542{
2543 u32 val;
2544
2545 val = adapter->portnum & 0xf;
2546 val |= encoding << 7;
2547 val |= (jiffies - adapter->dev_rst_time) << 8;
2548
2549 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2550 adapter->dev_rst_time = jiffies;
2551}
2552
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002553static int
2554qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002555{
2556 u32 val;
2557
2558 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2559 state != QLCNIC_DEV_NEED_QUISCENT);
2560
2561 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002562 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002563
2564 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2565
2566 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002567 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002568 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002569 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002570
2571 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2572
2573 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002574
2575 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002576}
2577
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002578static int
2579qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2580{
2581 u32 val;
2582
2583 if (qlcnic_api_lock(adapter))
2584 return -EBUSY;
2585
2586 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002587 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002588 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2589
2590 qlcnic_api_unlock(adapter);
2591
2592 return 0;
2593}
2594
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002595static void
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002596qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002597{
2598 u32 val;
2599
2600 if (qlcnic_api_lock(adapter))
2601 goto err;
2602
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002603 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002604 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002605 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002606
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002607 if (failed) {
2608 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2609 dev_info(&adapter->pdev->dev,
2610 "Device state set to Failed. Please Reboot\n");
2611 } else if (!(val & 0x11111111))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002612 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2613
2614 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002615 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002616 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2617
2618 qlcnic_api_unlock(adapter);
2619err:
2620 adapter->fw_fail_cnt = 0;
2621 clear_bit(__QLCNIC_START_FW, &adapter->state);
2622 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2623}
2624
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002625/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002626static int
2627qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2628{
2629 int act, state;
2630
2631 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002632 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002633
2634 if (((state & 0x11111111) == (act & 0x11111111)) ||
2635 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2636 return 0;
2637 else
2638 return 1;
2639}
2640
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002641static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2642{
2643 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2644
2645 if (val != QLCNIC_DRV_IDC_VER) {
2646 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2647 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2648 }
2649
2650 return 0;
2651}
2652
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002653static int
2654qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2655{
2656 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002657 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002658 u8 portnum = adapter->portnum;
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002659 u8 ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002660
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002661 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2662 return 1;
2663
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002664 if (qlcnic_api_lock(adapter))
2665 return -1;
2666
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002667 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002668 if (!(val & (1 << (portnum * 4)))) {
2669 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002670 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002671 }
2672
2673 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002674 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002675
2676 switch (prev_state) {
2677 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002678 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002679 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002680 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002681 qlcnic_api_unlock(adapter);
2682 return 1;
2683
2684 case QLCNIC_DEV_READY:
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002685 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002686 qlcnic_api_unlock(adapter);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002687 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002688
2689 case QLCNIC_DEV_NEED_RESET:
2690 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002691 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002692 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2693 break;
2694
2695 case QLCNIC_DEV_NEED_QUISCENT:
2696 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002697 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002698 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2699 break;
2700
2701 case QLCNIC_DEV_FAILED:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00002702 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002703 qlcnic_api_unlock(adapter);
2704 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002705
2706 case QLCNIC_DEV_INITIALIZING:
2707 case QLCNIC_DEV_QUISCENT:
2708 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002709 }
2710
2711 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002712
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002713 do {
2714 msleep(1000);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002715 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2716
2717 if (prev_state == QLCNIC_DEV_QUISCENT)
2718 continue;
2719 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002720
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002721 if (!dev_init_timeo) {
2722 dev_err(&adapter->pdev->dev,
2723 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002724 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002725 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002726
2727 if (qlcnic_api_lock(adapter))
2728 return -1;
2729
2730 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002731 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002732 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2733
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002734 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002735 qlcnic_api_unlock(adapter);
2736
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002737 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002738}
2739
2740static void
2741qlcnic_fwinit_work(struct work_struct *work)
2742{
2743 struct qlcnic_adapter *adapter = container_of(work,
2744 struct qlcnic_adapter, fw_work.work);
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002745 u32 dev_state = 0xf;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002746
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002747 if (qlcnic_api_lock(adapter))
2748 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002749
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002750 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002751 if (dev_state == QLCNIC_DEV_QUISCENT ||
2752 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002753 qlcnic_api_unlock(adapter);
2754 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2755 FW_POLL_DELAY * 2);
2756 return;
2757 }
2758
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002759 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002760 qlcnic_api_unlock(adapter);
2761 goto wait_npar;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002762 }
2763
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002764 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2765 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2766 adapter->reset_ack_timeo);
2767 goto skip_ack_check;
2768 }
2769
2770 if (!qlcnic_check_drv_state(adapter)) {
2771skip_ack_check:
2772 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002773
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002774 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2775 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2776 QLCNIC_DEV_INITIALIZING);
2777 set_bit(__QLCNIC_START_FW, &adapter->state);
2778 QLCDB(adapter, DRV, "Restarting fw\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002779 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002780 }
2781
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002782 qlcnic_api_unlock(adapter);
2783
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002784 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002785 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002786 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002787 return;
2788 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002789 goto err_ret;
2790 }
2791
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002792 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002793
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002794wait_npar:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002795 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002796 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002797
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002798 switch (dev_state) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002799 case QLCNIC_DEV_READY:
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002800 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002801 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002802 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002803 return;
2804 }
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002805 case QLCNIC_DEV_FAILED:
2806 break;
2807 default:
2808 qlcnic_schedule_work(adapter,
2809 qlcnic_fwinit_work, FW_POLL_DELAY);
2810 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002811 }
2812
2813err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002814 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2815 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002816 netif_device_attach(adapter->netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002817 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002818}
2819
2820static void
2821qlcnic_detach_work(struct work_struct *work)
2822{
2823 struct qlcnic_adapter *adapter = container_of(work,
2824 struct qlcnic_adapter, fw_work.work);
2825 struct net_device *netdev = adapter->netdev;
2826 u32 status;
2827
2828 netif_device_detach(netdev);
2829
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002830 /* Dont grab rtnl lock during Quiscent mode */
2831 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2832 if (netif_running(netdev))
2833 __qlcnic_down(adapter, netdev);
2834 } else
2835 qlcnic_down(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002836
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002837 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2838
2839 if (status & QLCNIC_RCODE_FATAL_ERROR)
2840 goto err_ret;
2841
2842 if (adapter->temp == QLCNIC_TEMP_PANIC)
2843 goto err_ret;
2844
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002845 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2846 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002847
2848 adapter->fw_wait_cnt = 0;
2849
2850 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2851
2852 return;
2853
2854err_ret:
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002855 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2856 status, adapter->temp);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002857 netif_device_attach(netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002858 qlcnic_clr_all_drv_state(adapter, 1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002859}
2860
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002861/*Transit NPAR state to NON Operational */
2862static void
2863qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2864{
2865 u32 state;
2866
2867 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2868 if (state == QLCNIC_DEV_NPAR_NON_OPER)
2869 return;
2870
2871 if (qlcnic_api_lock(adapter))
2872 return;
2873 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2874 qlcnic_api_unlock(adapter);
2875}
2876
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002877/*Transit to RESET state from READY state only */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002878static void
2879qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2880{
2881 u32 state;
2882
Anirban Chakrabortycea89752010-07-13 20:33:35 +00002883 adapter->need_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002884 if (qlcnic_api_lock(adapter))
2885 return;
2886
2887 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2888
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002889 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002890 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002891 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002892 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002893 }
2894
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002895 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002896 qlcnic_api_unlock(adapter);
2897}
2898
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002899/* Transit to NPAR READY state from NPAR NOT READY state */
2900static void
2901qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2902{
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002903 if (qlcnic_api_lock(adapter))
2904 return;
2905
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002906 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
2907 QLCDB(adapter, DRV, "NPAR operational state set\n");
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002908
2909 qlcnic_api_unlock(adapter);
2910}
2911
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002912static void
2913qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2914 work_func_t func, int delay)
2915{
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00002916 if (test_bit(__QLCNIC_AER, &adapter->state))
2917 return;
2918
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002919 INIT_DELAYED_WORK(&adapter->fw_work, func);
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00002920 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
2921 round_jiffies_relative(delay));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002922}
2923
2924static void
2925qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2926{
2927 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2928 msleep(10);
2929
2930 cancel_delayed_work_sync(&adapter->fw_work);
2931}
2932
2933static void
2934qlcnic_attach_work(struct work_struct *work)
2935{
2936 struct qlcnic_adapter *adapter = container_of(work,
2937 struct qlcnic_adapter, fw_work.work);
2938 struct net_device *netdev = adapter->netdev;
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002939 u32 npar_state;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002940
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002941 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
2942 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2943 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
2944 qlcnic_clr_all_drv_state(adapter, 0);
2945 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
2946 qlcnic_schedule_work(adapter, qlcnic_attach_work,
2947 FW_POLL_DELAY);
2948 else
2949 goto attach;
2950 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
2951 return;
2952 }
2953attach:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002954 if (netif_running(netdev)) {
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00002955 if (qlcnic_up(adapter, netdev))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002956 goto done;
2957
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00002958 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002959 }
2960
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002961done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002962 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002963 adapter->fw_fail_cnt = 0;
2964 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002965
2966 if (!qlcnic_clr_drv_state(adapter))
2967 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2968 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002969}
2970
2971static int
2972qlcnic_check_health(struct qlcnic_adapter *adapter)
2973{
Sony Chacko4e708122010-08-31 17:17:44 +00002974 u32 state = 0, heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002975 struct net_device *netdev = adapter->netdev;
2976
2977 if (qlcnic_check_temp(adapter))
2978 goto detach;
2979
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00002980 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002981 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002982
2983 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002984 if (state == QLCNIC_DEV_NEED_RESET) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002985 qlcnic_set_npar_non_operational(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002986 adapter->need_fw_reset = 1;
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002987 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
2988 goto detach;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002989
Sony Chacko4e708122010-08-31 17:17:44 +00002990 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
2991 if (heartbeat != adapter->heartbeat) {
2992 adapter->heartbeat = heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002993 adapter->fw_fail_cnt = 0;
2994 if (adapter->need_fw_reset)
2995 goto detach;
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002996
amit salecha9ce13ca2011-02-23 03:21:24 +00002997 if (adapter->reset_context && auto_fw_reset) {
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002998 qlcnic_reset_hw_context(adapter);
2999 adapter->netdev->trans_start = jiffies;
3000 }
3001
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003002 return 0;
3003 }
3004
3005 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3006 return 0;
3007
3008 qlcnic_dev_request_reset(adapter);
3009
amit salecha9ce13ca2011-02-23 03:21:24 +00003010 if (auto_fw_reset)
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00003011 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003012
3013 dev_info(&netdev->dev, "firmware hang detected\n");
3014
3015detach:
3016 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3017 QLCNIC_DEV_NEED_RESET;
3018
amit salecha9ce13ca2011-02-23 03:21:24 +00003019 if (auto_fw_reset &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003020 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3021
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003022 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003023 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3024 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003025
3026 return 1;
3027}
3028
3029static void
3030qlcnic_fw_poll_work(struct work_struct *work)
3031{
3032 struct qlcnic_adapter *adapter = container_of(work,
3033 struct qlcnic_adapter, fw_work.work);
3034
3035 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3036 goto reschedule;
3037
3038
3039 if (qlcnic_check_health(adapter))
3040 return;
3041
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00003042 if (adapter->fhash.fnum)
3043 qlcnic_prune_lb_filters(adapter);
3044
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003045reschedule:
3046 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3047}
3048
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003049static int qlcnic_is_first_func(struct pci_dev *pdev)
3050{
3051 struct pci_dev *oth_pdev;
3052 int val = pdev->devfn;
3053
3054 while (val-- > 0) {
3055 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3056 (pdev->bus), pdev->bus->number,
3057 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003058 if (!oth_pdev)
3059 continue;
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003060
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003061 if (oth_pdev->current_state != PCI_D3cold) {
3062 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003063 return 0;
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003064 }
3065 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003066 }
3067 return 1;
3068}
3069
3070static int qlcnic_attach_func(struct pci_dev *pdev)
3071{
3072 int err, first_func;
3073 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3074 struct net_device *netdev = adapter->netdev;
3075
3076 pdev->error_state = pci_channel_io_normal;
3077
3078 err = pci_enable_device(pdev);
3079 if (err)
3080 return err;
3081
3082 pci_set_power_state(pdev, PCI_D0);
3083 pci_set_master(pdev);
3084 pci_restore_state(pdev);
3085
3086 first_func = qlcnic_is_first_func(pdev);
3087
3088 if (qlcnic_api_lock(adapter))
3089 return -EINVAL;
3090
Amit Kumar Salecha933fce122010-08-17 00:34:19 +00003091 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003092 adapter->need_fw_reset = 1;
3093 set_bit(__QLCNIC_START_FW, &adapter->state);
3094 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3095 QLCDB(adapter, DRV, "Restarting fw\n");
3096 }
3097 qlcnic_api_unlock(adapter);
3098
3099 err = adapter->nic_ops->start_firmware(adapter);
3100 if (err)
3101 return err;
3102
3103 qlcnic_clr_drv_state(adapter);
3104 qlcnic_setup_intr(adapter);
3105
3106 if (netif_running(netdev)) {
3107 err = qlcnic_attach(adapter);
3108 if (err) {
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00003109 qlcnic_clr_all_drv_state(adapter, 1);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003110 clear_bit(__QLCNIC_AER, &adapter->state);
3111 netif_device_attach(netdev);
3112 return err;
3113 }
3114
3115 err = qlcnic_up(adapter, netdev);
3116 if (err)
3117 goto done;
3118
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003119 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003120 }
3121 done:
3122 netif_device_attach(netdev);
3123 return err;
3124}
3125
3126static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3127 pci_channel_state_t state)
3128{
3129 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3130 struct net_device *netdev = adapter->netdev;
3131
3132 if (state == pci_channel_io_perm_failure)
3133 return PCI_ERS_RESULT_DISCONNECT;
3134
3135 if (state == pci_channel_io_normal)
3136 return PCI_ERS_RESULT_RECOVERED;
3137
3138 set_bit(__QLCNIC_AER, &adapter->state);
3139 netif_device_detach(netdev);
3140
3141 cancel_delayed_work_sync(&adapter->fw_work);
3142
3143 if (netif_running(netdev))
3144 qlcnic_down(adapter, netdev);
3145
3146 qlcnic_detach(adapter);
3147 qlcnic_teardown_intr(adapter);
3148
3149 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3150
3151 pci_save_state(pdev);
3152 pci_disable_device(pdev);
3153
3154 return PCI_ERS_RESULT_NEED_RESET;
3155}
3156
3157static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3158{
3159 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3160 PCI_ERS_RESULT_RECOVERED;
3161}
3162
3163static void qlcnic_io_resume(struct pci_dev *pdev)
3164{
3165 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3166
3167 pci_cleanup_aer_uncorrect_error_status(pdev);
3168
3169 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3170 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3171 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3172 FW_POLL_DELAY);
3173}
3174
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003175static int
3176qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3177{
3178 int err;
3179
3180 err = qlcnic_can_start_firmware(adapter);
3181 if (err)
3182 return err;
3183
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +00003184 err = qlcnic_check_npar_opertional(adapter);
3185 if (err)
3186 return err;
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003187
Rajesh Borundia174240a2010-08-31 17:17:47 +00003188 err = qlcnic_initialize_nic(adapter);
3189 if (err)
3190 return err;
3191
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003192 qlcnic_check_options(adapter);
3193
Rajesh Borundia73733732010-08-31 17:17:50 +00003194 err = qlcnic_set_eswitch_port_config(adapter);
3195 if (err)
3196 return err;
3197
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003198 adapter->need_fw_reset = 0;
3199
3200 return err;
3201}
3202
3203static int
3204qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3205{
3206 return -EOPNOTSUPP;
3207}
3208
3209static int
3210qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3211{
3212 return -EOPNOTSUPP;
3213}
3214
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003215static ssize_t
3216qlcnic_store_bridged_mode(struct device *dev,
3217 struct device_attribute *attr, const char *buf, size_t len)
3218{
3219 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3220 unsigned long new;
3221 int ret = -EINVAL;
3222
3223 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3224 goto err_out;
3225
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00003226 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003227 goto err_out;
3228
3229 if (strict_strtoul(buf, 2, &new))
3230 goto err_out;
3231
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00003232 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003233 ret = len;
3234
3235err_out:
3236 return ret;
3237}
3238
3239static ssize_t
3240qlcnic_show_bridged_mode(struct device *dev,
3241 struct device_attribute *attr, char *buf)
3242{
3243 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3244 int bridged_mode = 0;
3245
3246 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3247 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3248
3249 return sprintf(buf, "%d\n", bridged_mode);
3250}
3251
3252static struct device_attribute dev_attr_bridged_mode = {
3253 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3254 .show = qlcnic_show_bridged_mode,
3255 .store = qlcnic_store_bridged_mode,
3256};
3257
3258static ssize_t
3259qlcnic_store_diag_mode(struct device *dev,
3260 struct device_attribute *attr, const char *buf, size_t len)
3261{
3262 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3263 unsigned long new;
3264
3265 if (strict_strtoul(buf, 2, &new))
3266 return -EINVAL;
3267
3268 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3269 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3270
3271 return len;
3272}
3273
3274static ssize_t
3275qlcnic_show_diag_mode(struct device *dev,
3276 struct device_attribute *attr, char *buf)
3277{
3278 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3279
3280 return sprintf(buf, "%d\n",
3281 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3282}
3283
3284static struct device_attribute dev_attr_diag_mode = {
3285 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3286 .show = qlcnic_show_diag_mode,
3287 .store = qlcnic_store_diag_mode,
3288};
3289
3290static int
3291qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3292 loff_t offset, size_t size)
3293{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003294 size_t crb_size = 4;
3295
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003296 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3297 return -EIO;
3298
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003299 if (offset < QLCNIC_PCI_CRBSPACE) {
3300 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3301 QLCNIC_PCI_CAMQM_END))
3302 crb_size = 8;
3303 else
3304 return -EINVAL;
3305 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003306
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003307 if ((size != crb_size) || (offset & (crb_size-1)))
3308 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003309
3310 return 0;
3311}
3312
3313static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003314qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3315 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003316 char *buf, loff_t offset, size_t size)
3317{
3318 struct device *dev = container_of(kobj, struct device, kobj);
3319 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3320 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003321 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003322 int ret;
3323
3324 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3325 if (ret != 0)
3326 return ret;
3327
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003328 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3329 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3330 memcpy(buf, &qmdata, size);
3331 } else {
3332 data = QLCRD32(adapter, offset);
3333 memcpy(buf, &data, size);
3334 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003335 return size;
3336}
3337
3338static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003339qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3340 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003341 char *buf, loff_t offset, size_t size)
3342{
3343 struct device *dev = container_of(kobj, struct device, kobj);
3344 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3345 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003346 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003347 int ret;
3348
3349 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3350 if (ret != 0)
3351 return ret;
3352
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003353 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3354 memcpy(&qmdata, buf, size);
3355 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3356 } else {
3357 memcpy(&data, buf, size);
3358 QLCWR32(adapter, offset, data);
3359 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003360 return size;
3361}
3362
3363static int
3364qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3365 loff_t offset, size_t size)
3366{
3367 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3368 return -EIO;
3369
3370 if ((size != 8) || (offset & 0x7))
3371 return -EIO;
3372
3373 return 0;
3374}
3375
3376static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003377qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3378 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003379 char *buf, loff_t offset, size_t size)
3380{
3381 struct device *dev = container_of(kobj, struct device, kobj);
3382 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3383 u64 data;
3384 int ret;
3385
3386 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3387 if (ret != 0)
3388 return ret;
3389
3390 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3391 return -EIO;
3392
3393 memcpy(buf, &data, size);
3394
3395 return size;
3396}
3397
3398static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003399qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3400 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003401 char *buf, loff_t offset, size_t size)
3402{
3403 struct device *dev = container_of(kobj, struct device, kobj);
3404 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3405 u64 data;
3406 int ret;
3407
3408 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3409 if (ret != 0)
3410 return ret;
3411
3412 memcpy(&data, buf, size);
3413
3414 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3415 return -EIO;
3416
3417 return size;
3418}
3419
3420
3421static struct bin_attribute bin_attr_crb = {
3422 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3423 .size = 0,
3424 .read = qlcnic_sysfs_read_crb,
3425 .write = qlcnic_sysfs_write_crb,
3426};
3427
3428static struct bin_attribute bin_attr_mem = {
3429 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3430 .size = 0,
3431 .read = qlcnic_sysfs_read_mem,
3432 .write = qlcnic_sysfs_write_mem,
3433};
3434
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003435static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003436validate_pm_config(struct qlcnic_adapter *adapter,
3437 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3438{
3439
3440 u8 src_pci_func, s_esw_id, d_esw_id;
3441 u8 dest_pci_func;
3442 int i;
3443
3444 for (i = 0; i < count; i++) {
3445 src_pci_func = pm_cfg[i].pci_func;
3446 dest_pci_func = pm_cfg[i].dest_npar;
3447 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3448 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3449 return QL_STATUS_INVALID_PARAM;
3450
3451 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3452 return QL_STATUS_INVALID_PARAM;
3453
3454 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3455 return QL_STATUS_INVALID_PARAM;
3456
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003457 s_esw_id = adapter->npars[src_pci_func].phy_port;
3458 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3459
3460 if (s_esw_id != d_esw_id)
3461 return QL_STATUS_INVALID_PARAM;
3462
3463 }
3464 return 0;
3465
3466}
3467
3468static ssize_t
3469qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3470 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3471{
3472 struct device *dev = container_of(kobj, struct device, kobj);
3473 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3474 struct qlcnic_pm_func_cfg *pm_cfg;
3475 u32 id, action, pci_func;
3476 int count, rem, i, ret;
3477
3478 count = size / sizeof(struct qlcnic_pm_func_cfg);
3479 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3480 if (rem)
3481 return QL_STATUS_INVALID_PARAM;
3482
3483 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3484
3485 ret = validate_pm_config(adapter, pm_cfg, count);
3486 if (ret)
3487 return ret;
3488 for (i = 0; i < count; i++) {
3489 pci_func = pm_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003490 action = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003491 id = adapter->npars[pci_func].phy_port;
3492 ret = qlcnic_config_port_mirroring(adapter, id,
3493 action, pci_func);
3494 if (ret)
3495 return ret;
3496 }
3497
3498 for (i = 0; i < count; i++) {
3499 pci_func = pm_cfg[i].pci_func;
3500 id = adapter->npars[pci_func].phy_port;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003501 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003502 adapter->npars[pci_func].dest_npar = id;
3503 }
3504 return size;
3505}
3506
3507static ssize_t
3508qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3509 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3510{
3511 struct device *dev = container_of(kobj, struct device, kobj);
3512 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3513 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3514 int i;
3515
3516 if (size != sizeof(pm_cfg))
3517 return QL_STATUS_INVALID_PARAM;
3518
3519 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3520 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3521 continue;
3522 pm_cfg[i].action = adapter->npars[i].enable_pm;
3523 pm_cfg[i].dest_npar = 0;
3524 pm_cfg[i].pci_func = i;
3525 }
3526 memcpy(buf, &pm_cfg, size);
3527
3528 return size;
3529}
3530
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003531static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003532validate_esw_config(struct qlcnic_adapter *adapter,
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003533 struct qlcnic_esw_func_cfg *esw_cfg, int count)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003534{
Rajesh Borundia7613c872010-08-31 17:17:48 +00003535 u32 op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003536 u8 pci_func;
3537 int i;
Rajesh Borundia7613c872010-08-31 17:17:48 +00003538
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003539 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
Rajesh Borundia7613c872010-08-31 17:17:48 +00003540
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003541 for (i = 0; i < count; i++) {
3542 pci_func = esw_cfg[i].pci_func;
3543 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3544 return QL_STATUS_INVALID_PARAM;
3545
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003546 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3547 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003548 return QL_STATUS_INVALID_PARAM;
3549
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003550 switch (esw_cfg[i].op_mode) {
3551 case QLCNIC_PORT_DEFAULTS:
Rajesh Borundia7613c872010-08-31 17:17:48 +00003552 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
Rajesh Borundia73733732010-08-31 17:17:50 +00003553 QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha091056b2010-12-02 20:41:43 +00003554 if (esw_cfg[i].mac_anti_spoof != 0)
3555 return QL_STATUS_INVALID_PARAM;
3556 if (esw_cfg[i].mac_override != 1)
3557 return QL_STATUS_INVALID_PARAM;
3558 if (esw_cfg[i].promisc_mode != 1)
3559 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia73733732010-08-31 17:17:50 +00003560 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003561 break;
3562 case QLCNIC_ADD_VLAN:
3563 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3564 return QL_STATUS_INVALID_PARAM;
3565 if (!esw_cfg[i].op_type)
3566 return QL_STATUS_INVALID_PARAM;
3567 break;
3568 case QLCNIC_DEL_VLAN:
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003569 if (!esw_cfg[i].op_type)
3570 return QL_STATUS_INVALID_PARAM;
3571 break;
3572 default:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003573 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003574 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003575 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003576 return 0;
3577}
3578
3579static ssize_t
3580qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3581 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3582{
3583 struct device *dev = container_of(kobj, struct device, kobj);
3584 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3585 struct qlcnic_esw_func_cfg *esw_cfg;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003586 struct qlcnic_npar_info *npar;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003587 int count, rem, i, ret;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003588 u8 pci_func, op_mode = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003589
3590 count = size / sizeof(struct qlcnic_esw_func_cfg);
3591 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3592 if (rem)
3593 return QL_STATUS_INVALID_PARAM;
3594
3595 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3596 ret = validate_esw_config(adapter, esw_cfg, count);
3597 if (ret)
3598 return ret;
3599
3600 for (i = 0; i < count; i++) {
Rajesh Borundia0325d692010-08-19 05:08:26 +00003601 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3602 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3603 return QL_STATUS_INVALID_PARAM;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003604
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003605 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003606 continue;
3607
3608 op_mode = esw_cfg[i].op_mode;
3609 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3610 esw_cfg[i].op_mode = op_mode;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003611 esw_cfg[i].pci_func = adapter->ahw->pci_func;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003612
3613 switch (esw_cfg[i].op_mode) {
3614 case QLCNIC_PORT_DEFAULTS:
3615 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3616 break;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00003617 case QLCNIC_ADD_VLAN:
3618 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3619 break;
3620 case QLCNIC_DEL_VLAN:
3621 esw_cfg[i].vlan_id = 0;
3622 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3623 break;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003624 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003625 }
3626
Rajesh Borundia0325d692010-08-19 05:08:26 +00003627 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3628 goto out;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003629
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003630 for (i = 0; i < count; i++) {
3631 pci_func = esw_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003632 npar = &adapter->npars[pci_func];
3633 switch (esw_cfg[i].op_mode) {
3634 case QLCNIC_PORT_DEFAULTS:
3635 npar->promisc_mode = esw_cfg[i].promisc_mode;
Rajesh Borundia73733732010-08-31 17:17:50 +00003636 npar->mac_override = esw_cfg[i].mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003637 npar->offload_flags = esw_cfg[i].offload_flags;
3638 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3639 npar->discard_tagged = esw_cfg[i].discard_tagged;
3640 break;
3641 case QLCNIC_ADD_VLAN:
3642 npar->pvid = esw_cfg[i].vlan_id;
3643 break;
3644 case QLCNIC_DEL_VLAN:
3645 npar->pvid = 0;
3646 break;
3647 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003648 }
Rajesh Borundia0325d692010-08-19 05:08:26 +00003649out:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003650 return size;
3651}
3652
3653static ssize_t
3654qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3655 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3656{
3657 struct device *dev = container_of(kobj, struct device, kobj);
3658 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3659 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003660 u8 i;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003661
3662 if (size != sizeof(esw_cfg))
3663 return QL_STATUS_INVALID_PARAM;
3664
3665 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3666 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3667 continue;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003668 esw_cfg[i].pci_func = i;
3669 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
3670 return QL_STATUS_INVALID_PARAM;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003671 }
3672 memcpy(buf, &esw_cfg, size);
3673
3674 return size;
3675}
3676
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003677static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003678validate_npar_config(struct qlcnic_adapter *adapter,
3679 struct qlcnic_npar_func_cfg *np_cfg, int count)
3680{
3681 u8 pci_func, i;
3682
3683 for (i = 0; i < count; i++) {
3684 pci_func = np_cfg[i].pci_func;
3685 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3686 return QL_STATUS_INVALID_PARAM;
3687
3688 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3689 return QL_STATUS_INVALID_PARAM;
3690
Rajesh Borundiad12b0d92011-02-23 03:21:25 +00003691 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
3692 !IS_VALID_BW(np_cfg[i].max_bw))
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003693 return QL_STATUS_INVALID_PARAM;
3694 }
3695 return 0;
3696}
3697
3698static ssize_t
3699qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3700 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3701{
3702 struct device *dev = container_of(kobj, struct device, kobj);
3703 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3704 struct qlcnic_info nic_info;
3705 struct qlcnic_npar_func_cfg *np_cfg;
3706 int i, count, rem, ret;
3707 u8 pci_func;
3708
3709 count = size / sizeof(struct qlcnic_npar_func_cfg);
3710 rem = size % sizeof(struct qlcnic_npar_func_cfg);
3711 if (rem)
3712 return QL_STATUS_INVALID_PARAM;
3713
3714 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
3715 ret = validate_npar_config(adapter, np_cfg, count);
3716 if (ret)
3717 return ret;
3718
3719 for (i = 0; i < count ; i++) {
3720 pci_func = np_cfg[i].pci_func;
3721 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
3722 if (ret)
3723 return ret;
3724 nic_info.pci_func = pci_func;
3725 nic_info.min_tx_bw = np_cfg[i].min_bw;
3726 nic_info.max_tx_bw = np_cfg[i].max_bw;
3727 ret = qlcnic_set_nic_info(adapter, &nic_info);
3728 if (ret)
3729 return ret;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003730 adapter->npars[i].min_bw = nic_info.min_tx_bw;
3731 adapter->npars[i].max_bw = nic_info.max_tx_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003732 }
3733
3734 return size;
3735
3736}
3737static ssize_t
3738qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
3739 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3740{
3741 struct device *dev = container_of(kobj, struct device, kobj);
3742 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3743 struct qlcnic_info nic_info;
3744 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
3745 int i, ret;
3746
3747 if (size != sizeof(np_cfg))
3748 return QL_STATUS_INVALID_PARAM;
3749
3750 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3751 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3752 continue;
3753 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
3754 if (ret)
3755 return ret;
3756
3757 np_cfg[i].pci_func = i;
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +00003758 np_cfg[i].op_mode = (u8)nic_info.op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003759 np_cfg[i].port_num = nic_info.phys_port;
3760 np_cfg[i].fw_capab = nic_info.capabilities;
3761 np_cfg[i].min_bw = nic_info.min_tx_bw ;
3762 np_cfg[i].max_bw = nic_info.max_tx_bw;
3763 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
3764 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
3765 }
3766 memcpy(buf, &np_cfg, size);
3767 return size;
3768}
3769
3770static ssize_t
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003771qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
3772 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3773{
3774 struct device *dev = container_of(kobj, struct device, kobj);
3775 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3776 struct qlcnic_esw_statistics port_stats;
3777 int ret;
3778
3779 if (size != sizeof(struct qlcnic_esw_statistics))
3780 return QL_STATUS_INVALID_PARAM;
3781
3782 if (offset >= QLCNIC_MAX_PCI_FUNC)
3783 return QL_STATUS_INVALID_PARAM;
3784
3785 memset(&port_stats, 0, size);
3786 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3787 &port_stats.rx);
3788 if (ret)
3789 return ret;
3790
3791 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3792 &port_stats.tx);
3793 if (ret)
3794 return ret;
3795
3796 memcpy(buf, &port_stats, size);
3797 return size;
3798}
3799
3800static ssize_t
3801qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
3802 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3803{
3804 struct device *dev = container_of(kobj, struct device, kobj);
3805 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3806 struct qlcnic_esw_statistics esw_stats;
3807 int ret;
3808
3809 if (size != sizeof(struct qlcnic_esw_statistics))
3810 return QL_STATUS_INVALID_PARAM;
3811
3812 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3813 return QL_STATUS_INVALID_PARAM;
3814
3815 memset(&esw_stats, 0, size);
3816 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3817 &esw_stats.rx);
3818 if (ret)
3819 return ret;
3820
3821 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3822 &esw_stats.tx);
3823 if (ret)
3824 return ret;
3825
3826 memcpy(buf, &esw_stats, size);
3827 return size;
3828}
3829
3830static ssize_t
3831qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
3832 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3833{
3834 struct device *dev = container_of(kobj, struct device, kobj);
3835 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3836 int ret;
3837
3838 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3839 return QL_STATUS_INVALID_PARAM;
3840
3841 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3842 QLCNIC_QUERY_RX_COUNTER);
3843 if (ret)
3844 return ret;
3845
3846 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3847 QLCNIC_QUERY_TX_COUNTER);
3848 if (ret)
3849 return ret;
3850
3851 return size;
3852}
3853
3854static ssize_t
3855qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
3856 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3857{
3858
3859 struct device *dev = container_of(kobj, struct device, kobj);
3860 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3861 int ret;
3862
3863 if (offset >= QLCNIC_MAX_PCI_FUNC)
3864 return QL_STATUS_INVALID_PARAM;
3865
3866 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
3867 QLCNIC_QUERY_RX_COUNTER);
3868 if (ret)
3869 return ret;
3870
3871 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
3872 QLCNIC_QUERY_TX_COUNTER);
3873 if (ret)
3874 return ret;
3875
3876 return size;
3877}
3878
3879static ssize_t
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003880qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
3881 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3882{
3883 struct device *dev = container_of(kobj, struct device, kobj);
3884 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3885 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
Dan Carpentere88db3b2010-08-09 21:49:36 +00003886 struct qlcnic_pci_info *pci_info;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003887 int i, ret;
3888
3889 if (size != sizeof(pci_cfg))
3890 return QL_STATUS_INVALID_PARAM;
3891
Dan Carpentere88db3b2010-08-09 21:49:36 +00003892 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
3893 if (!pci_info)
3894 return -ENOMEM;
3895
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003896 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpentere88db3b2010-08-09 21:49:36 +00003897 if (ret) {
3898 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003899 return ret;
Dan Carpentere88db3b2010-08-09 21:49:36 +00003900 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003901
3902 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3903 pci_cfg[i].pci_func = pci_info[i].id;
3904 pci_cfg[i].func_type = pci_info[i].type;
3905 pci_cfg[i].port_num = pci_info[i].default_port;
3906 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
3907 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
3908 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
3909 }
3910 memcpy(buf, &pci_cfg, size);
Dan Carpentere88db3b2010-08-09 21:49:36 +00003911 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003912 return size;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003913}
3914static struct bin_attribute bin_attr_npar_config = {
3915 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
3916 .size = 0,
3917 .read = qlcnic_sysfs_read_npar_config,
3918 .write = qlcnic_sysfs_write_npar_config,
3919};
3920
3921static struct bin_attribute bin_attr_pci_config = {
3922 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
3923 .size = 0,
3924 .read = qlcnic_sysfs_read_pci_config,
3925 .write = NULL,
3926};
3927
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003928static struct bin_attribute bin_attr_port_stats = {
3929 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
3930 .size = 0,
3931 .read = qlcnic_sysfs_get_port_stats,
3932 .write = qlcnic_sysfs_clear_port_stats,
3933};
3934
3935static struct bin_attribute bin_attr_esw_stats = {
3936 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
3937 .size = 0,
3938 .read = qlcnic_sysfs_get_esw_stats,
3939 .write = qlcnic_sysfs_clear_esw_stats,
3940};
3941
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003942static struct bin_attribute bin_attr_esw_config = {
3943 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
3944 .size = 0,
3945 .read = qlcnic_sysfs_read_esw_config,
3946 .write = qlcnic_sysfs_write_esw_config,
3947};
3948
3949static struct bin_attribute bin_attr_pm_config = {
3950 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
3951 .size = 0,
3952 .read = qlcnic_sysfs_read_pm_config,
3953 .write = qlcnic_sysfs_write_pm_config,
3954};
3955
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003956static void
3957qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
3958{
3959 struct device *dev = &adapter->pdev->dev;
3960
3961 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3962 if (device_create_file(dev, &dev_attr_bridged_mode))
3963 dev_warn(dev,
3964 "failed to create bridged_mode sysfs entry\n");
3965}
3966
3967static void
3968qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
3969{
3970 struct device *dev = &adapter->pdev->dev;
3971
3972 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3973 device_remove_file(dev, &dev_attr_bridged_mode);
3974}
3975
3976static void
3977qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
3978{
3979 struct device *dev = &adapter->pdev->dev;
3980
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003981 if (device_create_bin_file(dev, &bin_attr_port_stats))
3982 dev_info(dev, "failed to create port stats sysfs entry");
3983
Anirban Chakraborty132ff002010-07-09 13:15:05 +00003984 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
3985 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003986 if (device_create_file(dev, &dev_attr_diag_mode))
3987 dev_info(dev, "failed to create diag_mode sysfs entry\n");
3988 if (device_create_bin_file(dev, &bin_attr_crb))
3989 dev_info(dev, "failed to create crb sysfs entry\n");
3990 if (device_create_bin_file(dev, &bin_attr_mem))
3991 dev_info(dev, "failed to create mem sysfs entry\n");
Sony Chacko53478fe2011-04-01 14:27:47 +00003992 if (device_create_bin_file(dev, &bin_attr_pci_config))
3993 dev_info(dev, "failed to create pci config sysfs entry");
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003994 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
3995 return;
3996 if (device_create_bin_file(dev, &bin_attr_esw_config))
3997 dev_info(dev, "failed to create esw config sysfs entry");
3998 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003999 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004000 if (device_create_bin_file(dev, &bin_attr_npar_config))
4001 dev_info(dev, "failed to create npar config sysfs entry");
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004002 if (device_create_bin_file(dev, &bin_attr_pm_config))
4003 dev_info(dev, "failed to create pm config sysfs entry");
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004004 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4005 dev_info(dev, "failed to create eswitch stats sysfs entry");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004006}
4007
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004008static void
4009qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4010{
4011 struct device *dev = &adapter->pdev->dev;
4012
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004013 device_remove_bin_file(dev, &bin_attr_port_stats);
4014
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004015 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4016 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004017 device_remove_file(dev, &dev_attr_diag_mode);
4018 device_remove_bin_file(dev, &bin_attr_crb);
4019 device_remove_bin_file(dev, &bin_attr_mem);
Sony Chacko53478fe2011-04-01 14:27:47 +00004020 device_remove_bin_file(dev, &bin_attr_pci_config);
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004021 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4022 return;
4023 device_remove_bin_file(dev, &bin_attr_esw_config);
4024 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004025 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004026 device_remove_bin_file(dev, &bin_attr_npar_config);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004027 device_remove_bin_file(dev, &bin_attr_pm_config);
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004028 device_remove_bin_file(dev, &bin_attr_esw_stats);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004029}
4030
4031#ifdef CONFIG_INET
4032
4033#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4034
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004035static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004036qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4037 struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004038{
4039 struct in_device *indev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004040
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004041 indev = in_dev_get(dev);
4042 if (!indev)
4043 return;
4044
4045 for_ifa(indev) {
4046 switch (event) {
4047 case NETDEV_UP:
4048 qlcnic_config_ipaddr(adapter,
4049 ifa->ifa_address, QLCNIC_IP_UP);
4050 break;
4051 case NETDEV_DOWN:
4052 qlcnic_config_ipaddr(adapter,
4053 ifa->ifa_address, QLCNIC_IP_DOWN);
4054 break;
4055 default:
4056 break;
4057 }
4058 } endfor_ifa(indev);
4059
4060 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004061}
4062
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004063static void
4064qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4065{
4066 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4067 struct net_device *dev;
4068 u16 vid;
4069
4070 qlcnic_config_indev_addr(adapter, netdev, event);
4071
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +00004072 for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4073 dev = vlan_find_dev(netdev, vid);
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004074 if (!dev)
4075 continue;
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004076 qlcnic_config_indev_addr(adapter, dev, event);
4077 }
4078}
4079
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004080static int qlcnic_netdev_event(struct notifier_block *this,
4081 unsigned long event, void *ptr)
4082{
4083 struct qlcnic_adapter *adapter;
4084 struct net_device *dev = (struct net_device *)ptr;
4085
4086recheck:
4087 if (dev == NULL)
4088 goto done;
4089
4090 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4091 dev = vlan_dev_real_dev(dev);
4092 goto recheck;
4093 }
4094
4095 if (!is_qlcnic_netdev(dev))
4096 goto done;
4097
4098 adapter = netdev_priv(dev);
4099
4100 if (!adapter)
4101 goto done;
4102
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004103 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004104 goto done;
4105
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004106 qlcnic_config_indev_addr(adapter, dev, event);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004107done:
4108 return NOTIFY_DONE;
4109}
4110
4111static int
4112qlcnic_inetaddr_event(struct notifier_block *this,
4113 unsigned long event, void *ptr)
4114{
4115 struct qlcnic_adapter *adapter;
4116 struct net_device *dev;
4117
4118 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4119
4120 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4121
4122recheck:
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004123 if (dev == NULL)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004124 goto done;
4125
4126 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4127 dev = vlan_dev_real_dev(dev);
4128 goto recheck;
4129 }
4130
4131 if (!is_qlcnic_netdev(dev))
4132 goto done;
4133
4134 adapter = netdev_priv(dev);
4135
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00004136 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004137 goto done;
4138
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004139 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004140 goto done;
4141
4142 switch (event) {
4143 case NETDEV_UP:
4144 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4145 break;
4146 case NETDEV_DOWN:
4147 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4148 break;
4149 default:
4150 break;
4151 }
4152
4153done:
4154 return NOTIFY_DONE;
4155}
4156
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004157static struct notifier_block qlcnic_netdev_cb = {
4158 .notifier_call = qlcnic_netdev_event,
4159};
4160
4161static struct notifier_block qlcnic_inetaddr_cb = {
4162 .notifier_call = qlcnic_inetaddr_event,
4163};
4164#else
4165static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004166qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004167{ }
4168#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004169static struct pci_error_handlers qlcnic_err_handler = {
4170 .error_detected = qlcnic_io_error_detected,
4171 .slot_reset = qlcnic_io_slot_reset,
4172 .resume = qlcnic_io_resume,
4173};
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004174
4175static struct pci_driver qlcnic_driver = {
4176 .name = qlcnic_driver_name,
4177 .id_table = qlcnic_pci_tbl,
4178 .probe = qlcnic_probe,
4179 .remove = __devexit_p(qlcnic_remove),
4180#ifdef CONFIG_PM
4181 .suspend = qlcnic_suspend,
4182 .resume = qlcnic_resume,
4183#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004184 .shutdown = qlcnic_shutdown,
4185 .err_handler = &qlcnic_err_handler
4186
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004187};
4188
4189static int __init qlcnic_init_module(void)
4190{
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004191 int ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004192
4193 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4194
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004195 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4196 if (qlcnic_wq == NULL) {
4197 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4198 return -ENOMEM;
4199 }
4200
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004201#ifdef CONFIG_INET
4202 register_netdevice_notifier(&qlcnic_netdev_cb);
4203 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4204#endif
4205
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004206 ret = pci_register_driver(&qlcnic_driver);
4207 if (ret) {
4208#ifdef CONFIG_INET
4209 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4210 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4211#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004212 destroy_workqueue(qlcnic_wq);
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004213 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004214
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004215 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004216}
4217
4218module_init(qlcnic_init_module);
4219
4220static void __exit qlcnic_exit_module(void)
4221{
4222
4223 pci_unregister_driver(&qlcnic_driver);
4224
4225#ifdef CONFIG_INET
4226 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4227 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4228#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004229 destroy_workqueue(qlcnic_wq);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004230}
4231
4232module_exit(qlcnic_exit_module);