blob: f047c7c48314ccd48c76df8d68babb5d24c796d5 [file] [log] [blame]
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001/*
2 * Copyright (C) 2009 - QLogic Corporation.
3 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
19 *
20 * The full GNU General Public License is included in this distribution
21 * in the file called "COPYING".
22 *
23 */
24
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000026#include <linux/vmalloc.h>
27#include <linux/interrupt.h>
28
29#include "qlcnic.h"
30
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +000031#include <linux/swab.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000032#include <linux/dma-mapping.h>
33#include <linux/if_vlan.h>
34#include <net/ip.h>
35#include <linux/ipv6.h>
36#include <linux/inetdevice.h>
37#include <linux/sysfs.h>
Sucheta Chakraborty451724c2010-07-13 20:33:34 +000038#include <linux/aer.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000039
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000040MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000041MODULE_LICENSE("GPL");
42MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
43MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
44
45char qlcnic_driver_name[] = "qlcnic";
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000046static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
47 "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000048
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +000049static struct workqueue_struct *qlcnic_wq;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +000050static int qlcnic_mac_learn;
51module_param(qlcnic_mac_learn, int, 0644);
52MODULE_PARM_DESC(qlcnic_mac_learn, "Mac Filter (0=disabled, 1=enabled)");
53
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000054static int use_msi = 1;
55module_param(use_msi, int, 0644);
56MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
57
58static int use_msi_x = 1;
59module_param(use_msi_x, int, 0644);
60MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
61
62static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
63module_param(auto_fw_reset, int, 0644);
64MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
65
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +000066static int load_fw_file;
67module_param(load_fw_file, int, 0644);
68MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
69
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +000070static int qlcnic_config_npars;
71module_param(qlcnic_config_npars, int, 0644);
72MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
73
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000074static int __devinit qlcnic_probe(struct pci_dev *pdev,
75 const struct pci_device_id *ent);
76static void __devexit qlcnic_remove(struct pci_dev *pdev);
77static int qlcnic_open(struct net_device *netdev);
78static int qlcnic_close(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000079static void qlcnic_tx_timeout(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000080static void qlcnic_attach_work(struct work_struct *work);
81static void qlcnic_fwinit_work(struct work_struct *work);
82static void qlcnic_fw_poll_work(struct work_struct *work);
83static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
84 work_func_t func, int delay);
85static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
86static int qlcnic_poll(struct napi_struct *napi, int budget);
schacko8f891382010-06-17 02:56:40 +000087static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000088#ifdef CONFIG_NET_POLL_CONTROLLER
89static void qlcnic_poll_controller(struct net_device *netdev);
90#endif
91
92static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
93static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
94static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
95static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
96
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +000097static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +000098static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000099static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
100
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000101static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000102static irqreturn_t qlcnic_intr(int irq, void *data);
103static irqreturn_t qlcnic_msi_intr(int irq, void *data);
104static irqreturn_t qlcnic_msix_intr(int irq, void *data);
105
106static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +0000107static void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000108static int qlcnic_start_firmware(struct qlcnic_adapter *);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000109
Amit Kumar Salechab5e54922010-08-31 17:17:51 +0000110static void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter);
111static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000112static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000113static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
114static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
115static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000116static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
117 struct qlcnic_esw_func_cfg *);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000118/* PCI Device ID Table */
119#define ENTRY(device) \
120 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
121 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
122
123#define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
124
Amit Kumar Salecha6a902882010-02-01 05:24:54 +0000125static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000126 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
127 {0,}
128};
129
130MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
131
132
133void
134qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
135 struct qlcnic_host_tx_ring *tx_ring)
136{
137 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000138}
139
140static const u32 msi_tgt_status[8] = {
141 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
142 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
143 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
144 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
145};
146
147static const
148struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
149
150static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
151{
152 writel(0, sds_ring->crb_intr_mask);
153}
154
155static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
156{
157 struct qlcnic_adapter *adapter = sds_ring->adapter;
158
159 writel(0x1, sds_ring->crb_intr_mask);
160
161 if (!QLCNIC_IS_MSI_FAMILY(adapter))
162 writel(0xfbff, adapter->tgt_mask_reg);
163}
164
165static int
166qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
167{
168 int size = sizeof(struct qlcnic_host_sds_ring) * count;
169
170 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
171
Eric Dumazet807540b2010-09-23 05:40:09 +0000172 return recv_ctx->sds_rings == NULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000173}
174
175static void
176qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
177{
178 if (recv_ctx->sds_rings != NULL)
179 kfree(recv_ctx->sds_rings);
180
181 recv_ctx->sds_rings = NULL;
182}
183
184static int
185qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
186{
187 int ring;
188 struct qlcnic_host_sds_ring *sds_ring;
189 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
190
191 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
192 return -ENOMEM;
193
194 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
195 sds_ring = &recv_ctx->sds_rings[ring];
schacko8f891382010-06-17 02:56:40 +0000196
197 if (ring == adapter->max_sds_rings - 1)
198 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
199 QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
200 else
201 netif_napi_add(netdev, &sds_ring->napi,
202 qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000203 }
204
205 return 0;
206}
207
208static void
209qlcnic_napi_del(struct qlcnic_adapter *adapter)
210{
211 int ring;
212 struct qlcnic_host_sds_ring *sds_ring;
213 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
214
215 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
216 sds_ring = &recv_ctx->sds_rings[ring];
217 netif_napi_del(&sds_ring->napi);
218 }
219
220 qlcnic_free_sds_rings(&adapter->recv_ctx);
221}
222
223static void
224qlcnic_napi_enable(struct qlcnic_adapter *adapter)
225{
226 int ring;
227 struct qlcnic_host_sds_ring *sds_ring;
228 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
229
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000230 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
231 return;
232
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000233 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
234 sds_ring = &recv_ctx->sds_rings[ring];
235 napi_enable(&sds_ring->napi);
236 qlcnic_enable_int(sds_ring);
237 }
238}
239
240static void
241qlcnic_napi_disable(struct qlcnic_adapter *adapter)
242{
243 int ring;
244 struct qlcnic_host_sds_ring *sds_ring;
245 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
246
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000247 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
248 return;
249
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000250 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
251 sds_ring = &recv_ctx->sds_rings[ring];
252 qlcnic_disable_int(sds_ring);
253 napi_synchronize(&sds_ring->napi);
254 napi_disable(&sds_ring->napi);
255 }
256}
257
258static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
259{
260 memset(&adapter->stats, 0, sizeof(adapter->stats));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000261}
262
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000263static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
264{
265 u32 control;
266 int pos;
267
268 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
269 if (pos) {
270 pci_read_config_dword(pdev, pos, &control);
271 if (enable)
272 control |= PCI_MSIX_FLAGS_ENABLE;
273 else
274 control = 0;
275 pci_write_config_dword(pdev, pos, control);
276 }
277}
278
279static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
280{
281 int i;
282
283 for (i = 0; i < count; i++)
284 adapter->msix_entries[i].entry = i;
285}
286
287static int
288qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
289{
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000290 u8 mac_addr[ETH_ALEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000291 struct net_device *netdev = adapter->netdev;
292 struct pci_dev *pdev = adapter->pdev;
293
Rajesh Borundiada48e6c2010-08-31 17:17:46 +0000294 if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000295 return -EIO;
296
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000297 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000298 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
299 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
300
301 /* set station address */
302
303 if (!is_valid_ether_addr(netdev->perm_addr))
304 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
305 netdev->dev_addr);
306
307 return 0;
308}
309
310static int qlcnic_set_mac(struct net_device *netdev, void *p)
311{
312 struct qlcnic_adapter *adapter = netdev_priv(netdev);
313 struct sockaddr *addr = p;
314
Rajesh Borundia73733732010-08-31 17:17:50 +0000315 if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
316 return -EOPNOTSUPP;
317
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000318 if (!is_valid_ether_addr(addr->sa_data))
319 return -EINVAL;
320
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000321 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000322 netif_device_detach(netdev);
323 qlcnic_napi_disable(adapter);
324 }
325
326 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
327 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
328 qlcnic_set_multi(adapter->netdev);
329
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000330 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000331 netif_device_attach(netdev);
332 qlcnic_napi_enable(adapter);
333 }
334 return 0;
335}
336
Amit Kumar Salechad5790662010-09-16 19:14:39 +0000337static void qlcnic_vlan_rx_register(struct net_device *netdev,
338 struct vlan_group *grp)
339{
340 struct qlcnic_adapter *adapter = netdev_priv(netdev);
341 adapter->vlgrp = grp;
342}
343
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000344static const struct net_device_ops qlcnic_netdev_ops = {
345 .ndo_open = qlcnic_open,
346 .ndo_stop = qlcnic_close,
347 .ndo_start_xmit = qlcnic_xmit_frame,
348 .ndo_get_stats = qlcnic_get_stats,
349 .ndo_validate_addr = eth_validate_addr,
350 .ndo_set_multicast_list = qlcnic_set_multi,
351 .ndo_set_mac_address = qlcnic_set_mac,
352 .ndo_change_mtu = qlcnic_change_mtu,
353 .ndo_tx_timeout = qlcnic_tx_timeout,
Amit Kumar Salechad5790662010-09-16 19:14:39 +0000354 .ndo_vlan_rx_register = qlcnic_vlan_rx_register,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000355#ifdef CONFIG_NET_POLL_CONTROLLER
356 .ndo_poll_controller = qlcnic_poll_controller,
357#endif
358};
359
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000360static struct qlcnic_nic_template qlcnic_ops = {
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000361 .config_bridged_mode = qlcnic_config_bridged_mode,
362 .config_led = qlcnic_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000363 .start_firmware = qlcnic_start_firmware
364};
365
366static struct qlcnic_nic_template qlcnic_vf_ops = {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000367 .config_bridged_mode = qlcnicvf_config_bridged_mode,
368 .config_led = qlcnicvf_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000369 .start_firmware = qlcnicvf_start_firmware
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000370};
371
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000372static void
373qlcnic_setup_intr(struct qlcnic_adapter *adapter)
374{
375 const struct qlcnic_legacy_intr_set *legacy_intrp;
376 struct pci_dev *pdev = adapter->pdev;
377 int err, num_msix;
378
379 if (adapter->rss_supported) {
380 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
381 MSIX_ENTRIES_PER_ADAPTER : 2;
382 } else
383 num_msix = 1;
384
385 adapter->max_sds_rings = 1;
386
387 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
388
389 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
390
391 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
392 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
393 legacy_intrp->tgt_status_reg);
394 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
395 legacy_intrp->tgt_mask_reg);
396 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
397
398 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
399 ISR_INT_STATE_REG);
400
401 qlcnic_set_msix_bit(pdev, 0);
402
403 if (adapter->msix_supported) {
404
405 qlcnic_init_msix_entries(adapter, num_msix);
406 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
407 if (err == 0) {
408 adapter->flags |= QLCNIC_MSIX_ENABLED;
409 qlcnic_set_msix_bit(pdev, 1);
410
411 if (adapter->rss_supported)
412 adapter->max_sds_rings = num_msix;
413
414 dev_info(&pdev->dev, "using msi-x interrupts\n");
415 return;
416 }
417
418 if (err > 0)
419 pci_disable_msix(pdev);
420
421 /* fall through for msi */
422 }
423
424 if (use_msi && !pci_enable_msi(pdev)) {
425 adapter->flags |= QLCNIC_MSI_ENABLED;
426 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
427 msi_tgt_status[adapter->ahw.pci_func]);
428 dev_info(&pdev->dev, "using msi interrupts\n");
429 adapter->msix_entries[0].vector = pdev->irq;
430 return;
431 }
432
433 dev_info(&pdev->dev, "using legacy interrupts\n");
434 adapter->msix_entries[0].vector = pdev->irq;
435}
436
437static void
438qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
439{
440 if (adapter->flags & QLCNIC_MSIX_ENABLED)
441 pci_disable_msix(adapter->pdev);
442 if (adapter->flags & QLCNIC_MSI_ENABLED)
443 pci_disable_msi(adapter->pdev);
444}
445
446static void
447qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
448{
449 if (adapter->ahw.pci_base0 != NULL)
450 iounmap(adapter->ahw.pci_base0);
451}
452
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000453static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000454qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
455{
Dan Carpentere88db3b2010-08-09 21:49:36 +0000456 struct qlcnic_pci_info *pci_info;
Julia Lawall900853a2010-08-16 10:23:51 +0000457 int i, ret = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000458 u8 pfn;
459
Dan Carpentere88db3b2010-08-09 21:49:36 +0000460 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
461 if (!pci_info)
462 return -ENOMEM;
463
Dan Carpenterca315ac2010-08-09 21:47:56 +0000464 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000465 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
Dan Carpentere88db3b2010-08-09 21:49:36 +0000466 if (!adapter->npars) {
Julia Lawall900853a2010-08-16 10:23:51 +0000467 ret = -ENOMEM;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000468 goto err_pci_info;
469 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000470
Dan Carpenterca315ac2010-08-09 21:47:56 +0000471 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000472 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
473 if (!adapter->eswitch) {
Julia Lawall900853a2010-08-16 10:23:51 +0000474 ret = -ENOMEM;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000475 goto err_npars;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000476 }
477
478 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000479 if (ret)
480 goto err_eswitch;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000481
Dan Carpenterca315ac2010-08-09 21:47:56 +0000482 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
483 pfn = pci_info[i].id;
484 if (pfn > QLCNIC_MAX_PCI_FUNC)
485 return QL_STATUS_INVALID_PARAM;
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000486 adapter->npars[pfn].active = (u8)pci_info[i].active;
487 adapter->npars[pfn].type = (u8)pci_info[i].type;
488 adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000489 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
490 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000491 }
492
Dan Carpenterca315ac2010-08-09 21:47:56 +0000493 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
494 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
495
Dan Carpentere88db3b2010-08-09 21:49:36 +0000496 kfree(pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000497 return 0;
498
499err_eswitch:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000500 kfree(adapter->eswitch);
501 adapter->eswitch = NULL;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000502err_npars:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000503 kfree(adapter->npars);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000504 adapter->npars = NULL;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000505err_pci_info:
506 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000507
508 return ret;
509}
510
511static int
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000512qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
513{
514 u8 id;
515 u32 ref_count;
516 int i, ret = 1;
517 u32 data = QLCNIC_MGMT_FUNC;
518 void __iomem *priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
519
520 /* If other drivers are not in use set their privilege level */
Amit Kumar Salecha31018e02010-08-25 04:03:05 +0000521 ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000522 ret = qlcnic_api_lock(adapter);
523 if (ret)
524 goto err_lock;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000525
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000526 if (qlcnic_config_npars) {
527 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000528 id = i;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000529 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
530 id == adapter->ahw.pci_func)
531 continue;
532 data |= (qlcnic_config_npars &
533 QLC_DEV_SET_DRV(0xf, id));
534 }
535 } else {
536 data = readl(priv_op);
537 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw.pci_func)) |
538 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
539 adapter->ahw.pci_func));
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000540 }
541 writel(data, priv_op);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000542 qlcnic_api_unlock(adapter);
543err_lock:
544 return ret;
545}
546
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000547static void
548qlcnic_check_vf(struct qlcnic_adapter *adapter)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000549{
550 void __iomem *msix_base_addr;
551 void __iomem *priv_op;
552 u32 func;
553 u32 msix_base;
554 u32 op_mode, priv_level;
555
556 /* Determine FW API version */
557 adapter->fw_hal_version = readl(adapter->ahw.pci_base0 + QLCNIC_FW_API);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000558
559 /* Find PCI function number */
560 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
561 msix_base_addr = adapter->ahw.pci_base0 + QLCNIC_MSIX_BASE;
562 msix_base = readl(msix_base_addr);
563 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
564 adapter->ahw.pci_func = func;
565
566 /* Determine function privilege level */
567 priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
568 op_mode = readl(priv_op);
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000569 if (op_mode == QLC_DEV_DRV_DEFAULT)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000570 priv_level = QLCNIC_MGMT_FUNC;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000571 else
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000572 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw.pci_func);
573
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000574 if (priv_level == QLCNIC_NON_PRIV_FUNC) {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000575 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
576 dev_info(&adapter->pdev->dev,
577 "HAL Version: %d Non Privileged function\n",
578 adapter->fw_hal_version);
579 adapter->nic_ops = &qlcnic_vf_ops;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000580 } else
581 adapter->nic_ops = &qlcnic_ops;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000582}
583
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000584static int
585qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
586{
587 void __iomem *mem_ptr0 = NULL;
588 resource_size_t mem_base;
589 unsigned long mem_len, pci_len0 = 0;
590
591 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000592
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000593 /* remap phys address */
594 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
595 mem_len = pci_resource_len(pdev, 0);
596
597 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
598
599 mem_ptr0 = pci_ioremap_bar(pdev, 0);
600 if (mem_ptr0 == NULL) {
601 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
602 return -EIO;
603 }
604 pci_len0 = mem_len;
605 } else {
606 return -EIO;
607 }
608
609 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
610
611 adapter->ahw.pci_base0 = mem_ptr0;
612 adapter->ahw.pci_len0 = pci_len0;
613
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000614 qlcnic_check_vf(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000615
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000616 adapter->ahw.ocm_win_crb = qlcnic_get_ioaddr(adapter,
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000617 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(adapter->ahw.pci_func)));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000618
619 return 0;
620}
621
622static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
623{
624 struct pci_dev *pdev = adapter->pdev;
625 int i, found = 0;
626
627 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
628 if (qlcnic_boards[i].vendor == pdev->vendor &&
629 qlcnic_boards[i].device == pdev->device &&
630 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
631 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +0000632 sprintf(name, "%pM: %s" ,
633 adapter->mac_addr,
634 qlcnic_boards[i].short_name);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000635 found = 1;
636 break;
637 }
638
639 }
640
641 if (!found)
Sritej Velaga7f9a0c32010-06-17 02:56:39 +0000642 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000643}
644
645static void
646qlcnic_check_options(struct qlcnic_adapter *adapter)
647{
648 u32 fw_major, fw_minor, fw_build;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000649 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000650
651 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
652 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
653 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
654
655 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
656
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000657 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
658 fw_major, fw_minor, fw_build);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000659
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000660 if (adapter->ahw.port_type == QLCNIC_XGBE) {
661 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
662 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
663 } else if (adapter->ahw.port_type == QLCNIC_GBE) {
664 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
665 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
666 }
667
668 adapter->msix_supported = !!use_msi_x;
669 adapter->rss_supported = !!use_msi_x;
670
671 adapter->num_txd = MAX_CMD_DESCRIPTORS;
672
Sony Chacko251b0362010-08-19 05:08:24 +0000673 adapter->max_rds_rings = MAX_RDS_RINGS;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000674}
675
Rajesh Borundia174240a2010-08-31 17:17:47 +0000676static int
677qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
678{
679 int err;
680 struct qlcnic_info nic_info;
681
682 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw.pci_func);
683 if (err)
684 return err;
685
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000686 adapter->physical_port = (u8)nic_info.phys_port;
Rajesh Borundia174240a2010-08-31 17:17:47 +0000687 adapter->switch_mode = nic_info.switch_mode;
688 adapter->max_tx_ques = nic_info.max_tx_ques;
689 adapter->max_rx_ques = nic_info.max_rx_ques;
690 adapter->capabilities = nic_info.capabilities;
691 adapter->max_mac_filters = nic_info.max_mac_filters;
692 adapter->max_mtu = nic_info.max_mtu;
693
694 if (adapter->capabilities & BIT_6)
695 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
696 else
697 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
698
699 return err;
700}
701
Rajesh Borundia0325d692010-08-19 05:08:26 +0000702static void
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000703qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
704 struct qlcnic_esw_func_cfg *esw_cfg)
705{
706 if (esw_cfg->discard_tagged)
707 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
708 else
709 adapter->flags |= QLCNIC_TAGGING_ENABLED;
710
711 if (esw_cfg->vlan_id)
712 adapter->pvid = esw_cfg->vlan_id;
713 else
714 adapter->pvid = 0;
715}
716
717static void
Rajesh Borundia0325d692010-08-19 05:08:26 +0000718qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
719 struct qlcnic_esw_func_cfg *esw_cfg)
720{
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000721 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
722 QLCNIC_PROMISC_DISABLED);
Rajesh Borundia7613c872010-08-31 17:17:48 +0000723
724 if (esw_cfg->mac_anti_spoof)
725 adapter->flags |= QLCNIC_MACSPOOF;
Sony Chackofe4d4342010-08-19 05:08:27 +0000726
Rajesh Borundia73733732010-08-31 17:17:50 +0000727 if (!esw_cfg->mac_override)
728 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
729
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000730 if (!esw_cfg->promisc_mode)
731 adapter->flags |= QLCNIC_PROMISC_DISABLED;
732
Rajesh Borundia0325d692010-08-19 05:08:26 +0000733 qlcnic_set_netdev_features(adapter, esw_cfg);
734}
735
736static int
737qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
738{
739 struct qlcnic_esw_func_cfg esw_cfg;
740
741 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
742 return 0;
743
744 esw_cfg.pci_func = adapter->ahw.pci_func;
745 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
746 return -EIO;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000747 qlcnic_set_vlan_config(adapter, &esw_cfg);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000748 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
749
750 return 0;
751}
752
753static void
754qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
755 struct qlcnic_esw_func_cfg *esw_cfg)
756{
757 struct net_device *netdev = adapter->netdev;
758 unsigned long features, vlan_features;
759
760 features = (NETIF_F_SG | NETIF_F_IP_CSUM |
761 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
762 vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
763 NETIF_F_IPV6_CSUM);
764
765 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
766 features |= (NETIF_F_TSO | NETIF_F_TSO6);
767 vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
768 }
769 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
770 features |= NETIF_F_LRO;
771
772 if (esw_cfg->offload_flags & BIT_0) {
773 netdev->features |= features;
774 adapter->rx_csum = 1;
775 if (!(esw_cfg->offload_flags & BIT_1))
776 netdev->features &= ~NETIF_F_TSO;
777 if (!(esw_cfg->offload_flags & BIT_2))
778 netdev->features &= ~NETIF_F_TSO6;
779 } else {
780 netdev->features &= ~features;
781 adapter->rx_csum = 0;
782 }
783
784 netdev->vlan_features = (features & vlan_features);
785}
786
787static int
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000788qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
789{
790 void __iomem *priv_op;
791 u32 op_mode, priv_level;
792 int err = 0;
793
Rajesh Borundia174240a2010-08-31 17:17:47 +0000794 err = qlcnic_initialize_nic(adapter);
795 if (err)
796 return err;
797
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000798 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
799 return 0;
800
801 priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
802 op_mode = readl(priv_op);
803 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw.pci_func);
804
805 if (op_mode == QLC_DEV_DRV_DEFAULT)
806 priv_level = QLCNIC_MGMT_FUNC;
807 else
808 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw.pci_func);
809
Rajesh Borundia174240a2010-08-31 17:17:47 +0000810 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000811 if (priv_level == QLCNIC_MGMT_FUNC) {
812 adapter->op_mode = QLCNIC_MGMT_FUNC;
813 err = qlcnic_init_pci_info(adapter);
814 if (err)
815 return err;
816 /* Set privilege level for other functions */
817 qlcnic_set_function_modes(adapter);
818 dev_info(&adapter->pdev->dev,
819 "HAL Version: %d, Management function\n",
820 adapter->fw_hal_version);
821 } else if (priv_level == QLCNIC_PRIV_FUNC) {
822 adapter->op_mode = QLCNIC_PRIV_FUNC;
823 dev_info(&adapter->pdev->dev,
824 "HAL Version: %d, Privileged function\n",
825 adapter->fw_hal_version);
826 }
Rajesh Borundia174240a2010-08-31 17:17:47 +0000827 }
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000828
829 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
830
831 return err;
832}
833
834static int
Rajesh Borundia0325d692010-08-19 05:08:26 +0000835qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
836{
837 struct qlcnic_esw_func_cfg esw_cfg;
838 struct qlcnic_npar_info *npar;
839 u8 i;
840
Rajesh Borundia174240a2010-08-31 17:17:47 +0000841 if (adapter->need_fw_reset)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000842 return 0;
843
844 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
845 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
846 continue;
847 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
848 esw_cfg.pci_func = i;
849 esw_cfg.offload_flags = BIT_0;
Rajesh Borundia73733732010-08-31 17:17:50 +0000850 esw_cfg.mac_override = BIT_0;
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000851 esw_cfg.promisc_mode = BIT_0;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000852 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
853 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
854 if (qlcnic_config_switch_port(adapter, &esw_cfg))
855 return -EIO;
856 npar = &adapter->npars[i];
857 npar->pvid = esw_cfg.vlan_id;
Rajesh Borundia73733732010-08-31 17:17:50 +0000858 npar->mac_override = esw_cfg.mac_override;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000859 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
860 npar->discard_tagged = esw_cfg.discard_tagged;
861 npar->promisc_mode = esw_cfg.promisc_mode;
862 npar->offload_flags = esw_cfg.offload_flags;
863 }
864
865 return 0;
866}
867
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000868static int
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000869qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
870 struct qlcnic_npar_info *npar, int pci_func)
871{
872 struct qlcnic_esw_func_cfg esw_cfg;
873 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
874 esw_cfg.pci_func = pci_func;
875 esw_cfg.vlan_id = npar->pvid;
Rajesh Borundia73733732010-08-31 17:17:50 +0000876 esw_cfg.mac_override = npar->mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000877 esw_cfg.discard_tagged = npar->discard_tagged;
878 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
879 esw_cfg.offload_flags = npar->offload_flags;
880 esw_cfg.promisc_mode = npar->promisc_mode;
881 if (qlcnic_config_switch_port(adapter, &esw_cfg))
882 return -EIO;
883
884 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
885 if (qlcnic_config_switch_port(adapter, &esw_cfg))
886 return -EIO;
887
888 return 0;
889}
890
891static int
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000892qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
893{
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000894 int i, err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000895 struct qlcnic_npar_info *npar;
896 struct qlcnic_info nic_info;
897
Rajesh Borundia174240a2010-08-31 17:17:47 +0000898 if (!adapter->need_fw_reset)
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000899 return 0;
900
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000901 /* Set the NPAR config data after FW reset */
902 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
903 npar = &adapter->npars[i];
904 if (npar->type != QLCNIC_TYPE_NIC)
905 continue;
906 err = qlcnic_get_nic_info(adapter, &nic_info, i);
907 if (err)
908 return err;
909 nic_info.min_tx_bw = npar->min_bw;
910 nic_info.max_tx_bw = npar->max_bw;
911 err = qlcnic_set_nic_info(adapter, &nic_info);
912 if (err)
913 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000914
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000915 if (npar->enable_pm) {
916 err = qlcnic_config_port_mirroring(adapter,
917 npar->dest_npar, 1, i);
918 if (err)
919 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000920 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000921 err = qlcnic_reset_eswitch_config(adapter, npar, i);
922 if (err)
923 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000924 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000925 return 0;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000926}
927
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +0000928static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
929{
930 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
931 u32 npar_state;
932
933 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
934 return 0;
935
936 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
937 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
938 msleep(1000);
939 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
940 }
941 if (!npar_opt_timeo) {
942 dev_err(&adapter->pdev->dev,
943 "Waiting for NPAR state to opertional timeout\n");
944 return -EIO;
945 }
946 return 0;
947}
948
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000949static int
Rajesh Borundia174240a2010-08-31 17:17:47 +0000950qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
951{
952 int err;
953
954 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
955 adapter->op_mode != QLCNIC_MGMT_FUNC)
956 return 0;
957
958 err = qlcnic_set_default_offload_settings(adapter);
959 if (err)
960 return err;
961
962 err = qlcnic_reset_npar_config(adapter);
963 if (err)
964 return err;
965
966 qlcnic_dev_set_npar_ready(adapter);
967
968 return err;
969}
970
971static int
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000972qlcnic_start_firmware(struct qlcnic_adapter *adapter)
973{
Sony Chackod4066832010-08-19 05:08:31 +0000974 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000975
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000976 err = qlcnic_can_start_firmware(adapter);
977 if (err < 0)
978 return err;
979 else if (!err)
Sony Chackod4066832010-08-19 05:08:31 +0000980 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000981
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000982 if (load_fw_file)
983 qlcnic_request_firmware(adapter);
schacko8f891382010-06-17 02:56:40 +0000984 else {
Sony Chacko8cfdce02010-08-26 14:02:41 +0000985 err = qlcnic_check_flash_fw_ver(adapter);
986 if (err)
schacko8f891382010-06-17 02:56:40 +0000987 goto err_out;
988
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000989 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
schacko8f891382010-06-17 02:56:40 +0000990 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000991
992 err = qlcnic_need_fw_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000993 if (err == 0)
Sony Chacko4e708122010-08-31 17:17:44 +0000994 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000995
Sony Chackod4066832010-08-19 05:08:31 +0000996 err = qlcnic_pinit_from_rom(adapter);
997 if (err)
998 goto err_out;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000999
1000 err = qlcnic_load_firmware(adapter);
1001 if (err)
1002 goto err_out;
1003
1004 qlcnic_release_firmware(adapter);
Sony Chackod4066832010-08-19 05:08:31 +00001005 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001006
Sony Chackod4066832010-08-19 05:08:31 +00001007check_fw_status:
1008 err = qlcnic_check_fw_status(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001009 if (err)
1010 goto err_out;
1011
1012 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001013 qlcnic_idc_debug_info(adapter, 1);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00001014
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001015 err = qlcnic_check_eswitch_mode(adapter);
1016 if (err) {
1017 dev_err(&adapter->pdev->dev,
1018 "Memory allocation failed for eswitch\n");
1019 goto err_out;
1020 }
Rajesh Borundia174240a2010-08-31 17:17:47 +00001021 err = qlcnic_set_mgmt_operations(adapter);
1022 if (err)
1023 goto err_out;
1024
1025 qlcnic_check_options(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001026 adapter->need_fw_reset = 0;
1027
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001028 qlcnic_release_firmware(adapter);
1029 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001030
1031err_out:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001032 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1033 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001034
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001035 qlcnic_release_firmware(adapter);
1036 return err;
1037}
1038
1039static int
1040qlcnic_request_irq(struct qlcnic_adapter *adapter)
1041{
1042 irq_handler_t handler;
1043 struct qlcnic_host_sds_ring *sds_ring;
1044 int err, ring;
1045
1046 unsigned long flags = 0;
1047 struct net_device *netdev = adapter->netdev;
1048 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1049
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001050 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1051 handler = qlcnic_tmp_intr;
1052 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1053 flags |= IRQF_SHARED;
1054
1055 } else {
1056 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1057 handler = qlcnic_msix_intr;
1058 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1059 handler = qlcnic_msi_intr;
1060 else {
1061 flags |= IRQF_SHARED;
1062 handler = qlcnic_intr;
1063 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001064 }
1065 adapter->irq = netdev->irq;
1066
1067 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1068 sds_ring = &recv_ctx->sds_rings[ring];
1069 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1070 err = request_irq(sds_ring->irq, handler,
1071 flags, sds_ring->name, sds_ring);
1072 if (err)
1073 return err;
1074 }
1075
1076 return 0;
1077}
1078
1079static void
1080qlcnic_free_irq(struct qlcnic_adapter *adapter)
1081{
1082 int ring;
1083 struct qlcnic_host_sds_ring *sds_ring;
1084
1085 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
1086
1087 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1088 sds_ring = &recv_ctx->sds_rings[ring];
1089 free_irq(sds_ring->irq, sds_ring);
1090 }
1091}
1092
1093static void
1094qlcnic_init_coalesce_defaults(struct qlcnic_adapter *adapter)
1095{
1096 adapter->coal.flags = QLCNIC_INTR_DEFAULT;
1097 adapter->coal.normal.data.rx_time_us =
1098 QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1099 adapter->coal.normal.data.rx_packets =
1100 QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
1101 adapter->coal.normal.data.tx_time_us =
1102 QLCNIC_DEFAULT_INTR_COALESCE_TX_TIME_US;
1103 adapter->coal.normal.data.tx_packets =
1104 QLCNIC_DEFAULT_INTR_COALESCE_TX_PACKETS;
1105}
1106
1107static int
1108__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1109{
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001110 int ring;
1111 struct qlcnic_host_rds_ring *rds_ring;
1112
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001113 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1114 return -EIO;
1115
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001116 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1117 return 0;
Rajesh Borundia0325d692010-08-19 05:08:26 +00001118 if (qlcnic_set_eswitch_port_config(adapter))
1119 return -EIO;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001120
1121 if (qlcnic_fw_create_ctx(adapter))
1122 return -EIO;
1123
1124 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1125 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1126 qlcnic_post_rx_buffers(adapter, ring, rds_ring);
1127 }
1128
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001129 qlcnic_set_multi(netdev);
1130 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1131
1132 adapter->ahw.linkup = 0;
1133
1134 if (adapter->max_sds_rings > 1)
1135 qlcnic_config_rss(adapter, 1);
1136
1137 qlcnic_config_intr_coalesce(adapter);
1138
Sucheta Chakraborty24763d82010-08-17 00:34:25 +00001139 if (netdev->features & NETIF_F_LRO)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001140 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1141
1142 qlcnic_napi_enable(adapter);
1143
1144 qlcnic_linkevent_request(adapter, 1);
1145
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001146 adapter->reset_context = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001147 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1148 return 0;
1149}
1150
1151/* Usage: During resume and firmware recovery module.*/
1152
1153static int
1154qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1155{
1156 int err = 0;
1157
1158 rtnl_lock();
1159 if (netif_running(netdev))
1160 err = __qlcnic_up(adapter, netdev);
1161 rtnl_unlock();
1162
1163 return err;
1164}
1165
1166static void
1167__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1168{
1169 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1170 return;
1171
1172 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1173 return;
1174
1175 smp_mb();
1176 spin_lock(&adapter->tx_clean_lock);
1177 netif_carrier_off(netdev);
1178 netif_tx_disable(netdev);
1179
1180 qlcnic_free_mac_list(adapter);
1181
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001182 if (adapter->fhash.fnum)
1183 qlcnic_delete_lb_filters(adapter);
1184
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001185 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1186
1187 qlcnic_napi_disable(adapter);
1188
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001189 qlcnic_fw_destroy_ctx(adapter);
1190
1191 qlcnic_reset_rx_buffers_list(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001192 qlcnic_release_tx_buffers(adapter);
1193 spin_unlock(&adapter->tx_clean_lock);
1194}
1195
1196/* Usage: During suspend and firmware recovery module */
1197
1198static void
1199qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1200{
1201 rtnl_lock();
1202 if (netif_running(netdev))
1203 __qlcnic_down(adapter, netdev);
1204 rtnl_unlock();
1205
1206}
1207
1208static int
1209qlcnic_attach(struct qlcnic_adapter *adapter)
1210{
1211 struct net_device *netdev = adapter->netdev;
1212 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001213 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001214
1215 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1216 return 0;
1217
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001218 err = qlcnic_napi_add(adapter, netdev);
1219 if (err)
1220 return err;
1221
1222 err = qlcnic_alloc_sw_resources(adapter);
1223 if (err) {
1224 dev_err(&pdev->dev, "Error in setting sw resources\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001225 goto err_out_napi_del;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001226 }
1227
1228 err = qlcnic_alloc_hw_resources(adapter);
1229 if (err) {
1230 dev_err(&pdev->dev, "Error in setting hw resources\n");
1231 goto err_out_free_sw;
1232 }
1233
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001234 err = qlcnic_request_irq(adapter);
1235 if (err) {
1236 dev_err(&pdev->dev, "failed to setup interrupt\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001237 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001238 }
1239
1240 qlcnic_init_coalesce_defaults(adapter);
1241
1242 qlcnic_create_sysfs_entries(adapter);
1243
1244 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1245 return 0;
1246
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001247err_out_free_hw:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001248 qlcnic_free_hw_resources(adapter);
1249err_out_free_sw:
1250 qlcnic_free_sw_resources(adapter);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001251err_out_napi_del:
1252 qlcnic_napi_del(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001253 return err;
1254}
1255
1256static void
1257qlcnic_detach(struct qlcnic_adapter *adapter)
1258{
1259 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1260 return;
1261
1262 qlcnic_remove_sysfs_entries(adapter);
1263
1264 qlcnic_free_hw_resources(adapter);
1265 qlcnic_release_rx_buffers(adapter);
1266 qlcnic_free_irq(adapter);
1267 qlcnic_napi_del(adapter);
1268 qlcnic_free_sw_resources(adapter);
1269
1270 adapter->is_up = 0;
1271}
1272
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001273void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1274{
1275 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1276 struct qlcnic_host_sds_ring *sds_ring;
1277 int ring;
1278
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001279 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001280 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1281 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1282 sds_ring = &adapter->recv_ctx.sds_rings[ring];
1283 qlcnic_disable_int(sds_ring);
1284 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001285 }
1286
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001287 qlcnic_fw_destroy_ctx(adapter);
1288
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001289 qlcnic_detach(adapter);
1290
1291 adapter->diag_test = 0;
1292 adapter->max_sds_rings = max_sds_rings;
1293
1294 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001295 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001296
1297 if (netif_running(netdev))
1298 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001299out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001300 netif_device_attach(netdev);
1301}
1302
1303int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1304{
1305 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1306 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001307 struct qlcnic_host_rds_ring *rds_ring;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001308 int ring;
1309 int ret;
1310
1311 netif_device_detach(netdev);
1312
1313 if (netif_running(netdev))
1314 __qlcnic_down(adapter, netdev);
1315
1316 qlcnic_detach(adapter);
1317
1318 adapter->max_sds_rings = 1;
1319 adapter->diag_test = test;
1320
1321 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001322 if (ret) {
1323 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001324 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001325 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001326
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001327 ret = qlcnic_fw_create_ctx(adapter);
1328 if (ret) {
1329 qlcnic_detach(adapter);
Sony Chacko57e46242010-07-24 18:32:18 +00001330 netif_device_attach(netdev);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001331 return ret;
1332 }
1333
1334 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1335 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1336 qlcnic_post_rx_buffers(adapter, ring, rds_ring);
1337 }
1338
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001339 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1340 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1341 sds_ring = &adapter->recv_ctx.sds_rings[ring];
1342 qlcnic_enable_int(sds_ring);
1343 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001344 }
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001345 set_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001346
1347 return 0;
1348}
1349
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001350/* Reset context in hardware only */
1351static int
1352qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1353{
1354 struct net_device *netdev = adapter->netdev;
1355
1356 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1357 return -EBUSY;
1358
1359 netif_device_detach(netdev);
1360
1361 qlcnic_down(adapter, netdev);
1362
1363 qlcnic_up(adapter, netdev);
1364
1365 netif_device_attach(netdev);
1366
1367 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1368 return 0;
1369}
1370
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001371int
1372qlcnic_reset_context(struct qlcnic_adapter *adapter)
1373{
1374 int err = 0;
1375 struct net_device *netdev = adapter->netdev;
1376
1377 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1378 return -EBUSY;
1379
1380 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1381
1382 netif_device_detach(netdev);
1383
1384 if (netif_running(netdev))
1385 __qlcnic_down(adapter, netdev);
1386
1387 qlcnic_detach(adapter);
1388
1389 if (netif_running(netdev)) {
1390 err = qlcnic_attach(adapter);
1391 if (!err)
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001392 __qlcnic_up(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001393 }
1394
1395 netif_device_attach(netdev);
1396 }
1397
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001398 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1399 return err;
1400}
1401
1402static int
1403qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001404 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001405{
1406 int err;
1407 struct pci_dev *pdev = adapter->pdev;
1408
1409 adapter->rx_csum = 1;
1410 adapter->mc_enabled = 0;
1411 adapter->max_mc_count = 38;
1412
1413 netdev->netdev_ops = &qlcnic_netdev_ops;
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001414 netdev->watchdog_timeo = 5*HZ;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001415
1416 qlcnic_change_mtu(netdev, netdev->mtu);
1417
1418 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1419
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001420 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
Amit Kumar Salechad5790662010-09-16 19:14:39 +00001421 NETIF_F_IPV6_CSUM | NETIF_F_GRO | NETIF_F_HW_VLAN_RX);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001422 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
Anirban Chakrabortyac8d0c42010-07-09 13:14:58 +00001423 NETIF_F_IPV6_CSUM);
1424
1425 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
1426 netdev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
1427 netdev->vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
1428 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001429
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001430 if (pci_using_dac) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001431 netdev->features |= NETIF_F_HIGHDMA;
1432 netdev->vlan_features |= NETIF_F_HIGHDMA;
1433 }
1434
1435 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1436 netdev->features |= (NETIF_F_HW_VLAN_TX);
1437
1438 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1439 netdev->features |= NETIF_F_LRO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001440 netdev->irq = adapter->msix_entries[0].vector;
1441
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001442 netif_carrier_off(netdev);
1443 netif_stop_queue(netdev);
1444
1445 err = register_netdev(netdev);
1446 if (err) {
1447 dev_err(&pdev->dev, "failed to register net device\n");
1448 return err;
1449 }
1450
1451 return 0;
1452}
1453
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001454static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1455{
1456 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1457 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1458 *pci_using_dac = 1;
1459 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1460 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1461 *pci_using_dac = 0;
1462 else {
1463 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1464 return -EIO;
1465 }
1466
1467 return 0;
1468}
1469
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001470static int __devinit
1471qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1472{
1473 struct net_device *netdev = NULL;
1474 struct qlcnic_adapter *adapter = NULL;
1475 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001476 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001477 uint8_t pci_using_dac;
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001478 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001479
1480 err = pci_enable_device(pdev);
1481 if (err)
1482 return err;
1483
1484 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1485 err = -ENODEV;
1486 goto err_out_disable_pdev;
1487 }
1488
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001489 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1490 if (err)
1491 goto err_out_disable_pdev;
1492
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001493 err = pci_request_regions(pdev, qlcnic_driver_name);
1494 if (err)
1495 goto err_out_disable_pdev;
1496
1497 pci_set_master(pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001498 pci_enable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001499
1500 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1501 if (!netdev) {
1502 dev_err(&pdev->dev, "failed to allocate net_device\n");
1503 err = -ENOMEM;
1504 goto err_out_free_res;
1505 }
1506
1507 SET_NETDEV_DEV(netdev, &pdev->dev);
1508
1509 adapter = netdev_priv(netdev);
1510 adapter->netdev = netdev;
1511 adapter->pdev = pdev;
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001512 adapter->dev_rst_time = jiffies;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001513
1514 revision_id = pdev->revision;
1515 adapter->ahw.revision_id = revision_id;
1516
1517 rwlock_init(&adapter->ahw.crb_lock);
1518 mutex_init(&adapter->ahw.mem_lock);
1519
1520 spin_lock_init(&adapter->tx_clean_lock);
1521 INIT_LIST_HEAD(&adapter->mac_list);
1522
1523 err = qlcnic_setup_pci_map(adapter);
1524 if (err)
1525 goto err_out_free_netdev;
1526
1527 /* This will be reset for mezz cards */
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001528 adapter->portnum = adapter->ahw.pci_func;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001529
1530 err = qlcnic_get_board_info(adapter);
1531 if (err) {
1532 dev_err(&pdev->dev, "Error getting board config info.\n");
1533 goto err_out_iounmap;
1534 }
1535
Sony Chacko8cfdce02010-08-26 14:02:41 +00001536 err = qlcnic_setup_idc_param(adapter);
1537 if (err)
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +00001538 goto err_out_iounmap;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001539
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001540 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001541 if (err) {
1542 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001543 goto err_out_decr_ref;
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001544 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001545
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001546 if (qlcnic_read_mac_addr(adapter))
1547 dev_warn(&pdev->dev, "failed to read mac addr\n");
1548
1549 if (adapter->portnum == 0) {
1550 get_brd_name(adapter, brd_name);
1551
1552 pr_info("%s: %s Board Chip rev 0x%x\n",
1553 module_name(THIS_MODULE),
1554 brd_name, adapter->ahw.revision_id);
1555 }
1556
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001557 qlcnic_clear_stats(adapter);
1558
1559 qlcnic_setup_intr(adapter);
1560
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001561 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001562 if (err)
1563 goto err_out_disable_msi;
1564
1565 pci_set_drvdata(pdev, adapter);
1566
1567 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1568
1569 switch (adapter->ahw.port_type) {
1570 case QLCNIC_GBE:
1571 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1572 adapter->netdev->name);
1573 break;
1574 case QLCNIC_XGBE:
1575 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1576 adapter->netdev->name);
1577 break;
1578 }
1579
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001580 qlcnic_alloc_lb_filters_mem(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001581 qlcnic_create_diag_entries(adapter);
1582
1583 return 0;
1584
1585err_out_disable_msi:
1586 qlcnic_teardown_intr(adapter);
1587
1588err_out_decr_ref:
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001589 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001590
1591err_out_iounmap:
1592 qlcnic_cleanup_pci_map(adapter);
1593
1594err_out_free_netdev:
1595 free_netdev(netdev);
1596
1597err_out_free_res:
1598 pci_release_regions(pdev);
1599
1600err_out_disable_pdev:
1601 pci_set_drvdata(pdev, NULL);
1602 pci_disable_device(pdev);
1603 return err;
1604}
1605
1606static void __devexit qlcnic_remove(struct pci_dev *pdev)
1607{
1608 struct qlcnic_adapter *adapter;
1609 struct net_device *netdev;
1610
1611 adapter = pci_get_drvdata(pdev);
1612 if (adapter == NULL)
1613 return;
1614
1615 netdev = adapter->netdev;
1616
1617 qlcnic_cancel_fw_work(adapter);
1618
1619 unregister_netdev(netdev);
1620
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001621 qlcnic_detach(adapter);
1622
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001623 if (adapter->npars != NULL)
1624 kfree(adapter->npars);
1625 if (adapter->eswitch != NULL)
1626 kfree(adapter->eswitch);
1627
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001628 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001629
1630 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1631
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001632 qlcnic_free_lb_filters_mem(adapter);
1633
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001634 qlcnic_teardown_intr(adapter);
1635
1636 qlcnic_remove_diag_entries(adapter);
1637
1638 qlcnic_cleanup_pci_map(adapter);
1639
1640 qlcnic_release_firmware(adapter);
1641
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001642 pci_disable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001643 pci_release_regions(pdev);
1644 pci_disable_device(pdev);
1645 pci_set_drvdata(pdev, NULL);
1646
1647 free_netdev(netdev);
1648}
1649static int __qlcnic_shutdown(struct pci_dev *pdev)
1650{
1651 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1652 struct net_device *netdev = adapter->netdev;
1653 int retval;
1654
1655 netif_device_detach(netdev);
1656
1657 qlcnic_cancel_fw_work(adapter);
1658
1659 if (netif_running(netdev))
1660 qlcnic_down(adapter, netdev);
1661
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001662 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001663
1664 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1665
1666 retval = pci_save_state(pdev);
1667 if (retval)
1668 return retval;
1669
1670 if (qlcnic_wol_supported(adapter)) {
1671 pci_enable_wake(pdev, PCI_D3cold, 1);
1672 pci_enable_wake(pdev, PCI_D3hot, 1);
1673 }
1674
1675 return 0;
1676}
1677
1678static void qlcnic_shutdown(struct pci_dev *pdev)
1679{
1680 if (__qlcnic_shutdown(pdev))
1681 return;
1682
1683 pci_disable_device(pdev);
1684}
1685
1686#ifdef CONFIG_PM
1687static int
1688qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1689{
1690 int retval;
1691
1692 retval = __qlcnic_shutdown(pdev);
1693 if (retval)
1694 return retval;
1695
1696 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1697 return 0;
1698}
1699
1700static int
1701qlcnic_resume(struct pci_dev *pdev)
1702{
1703 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1704 struct net_device *netdev = adapter->netdev;
1705 int err;
1706
1707 err = pci_enable_device(pdev);
1708 if (err)
1709 return err;
1710
1711 pci_set_power_state(pdev, PCI_D0);
1712 pci_set_master(pdev);
1713 pci_restore_state(pdev);
1714
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001715 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001716 if (err) {
1717 dev_err(&pdev->dev, "failed to start firmware\n");
1718 return err;
1719 }
1720
1721 if (netif_running(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001722 err = qlcnic_up(adapter, netdev);
1723 if (err)
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001724 goto done;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001725
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00001726 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001727 }
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001728done:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001729 netif_device_attach(netdev);
1730 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1731 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001732}
1733#endif
1734
1735static int qlcnic_open(struct net_device *netdev)
1736{
1737 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1738 int err;
1739
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001740 err = qlcnic_attach(adapter);
1741 if (err)
1742 return err;
1743
1744 err = __qlcnic_up(adapter, netdev);
1745 if (err)
1746 goto err_out;
1747
1748 netif_start_queue(netdev);
1749
1750 return 0;
1751
1752err_out:
1753 qlcnic_detach(adapter);
1754 return err;
1755}
1756
1757/*
1758 * qlcnic_close - Disables a network interface entry point
1759 */
1760static int qlcnic_close(struct net_device *netdev)
1761{
1762 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1763
1764 __qlcnic_down(adapter, netdev);
1765 return 0;
1766}
1767
1768static void
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001769qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1770{
1771 void *head;
1772 int i;
1773
1774 if (!qlcnic_mac_learn)
1775 return;
1776
1777 spin_lock_init(&adapter->mac_learn_lock);
1778
1779 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1780 GFP_KERNEL);
1781 if (!head)
1782 return;
1783
1784 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1785 adapter->fhash.fhead = (struct hlist_head *)head;
1786
1787 for (i = 0; i < adapter->fhash.fmax; i++)
1788 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1789}
1790
1791static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1792{
1793 if (adapter->fhash.fmax && adapter->fhash.fhead)
1794 kfree(adapter->fhash.fhead);
1795
1796 adapter->fhash.fhead = NULL;
1797 adapter->fhash.fmax = 0;
1798}
1799
1800static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001801 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001802{
1803 struct cmd_desc_type0 *hwdesc;
1804 struct qlcnic_nic_req *req;
1805 struct qlcnic_mac_req *mac_req;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001806 struct qlcnic_vlan_req *vlan_req;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001807 u32 producer;
1808 u64 word;
1809
1810 producer = tx_ring->producer;
1811 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1812
1813 req = (struct qlcnic_nic_req *)hwdesc;
1814 memset(req, 0, sizeof(struct qlcnic_nic_req));
1815 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1816
1817 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1818 req->req_hdr = cpu_to_le64(word);
1819
1820 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001821 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001822 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1823
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001824 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1825 vlan_req->vlan_id = vlan_id;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001826
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001827 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
1828}
1829
1830#define QLCNIC_MAC_HASH(MAC)\
1831 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1832
1833static void
1834qlcnic_send_filter(struct qlcnic_adapter *adapter,
1835 struct qlcnic_host_tx_ring *tx_ring,
1836 struct cmd_desc_type0 *first_desc,
1837 struct sk_buff *skb)
1838{
1839 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1840 struct qlcnic_filter *fil, *tmp_fil;
1841 struct hlist_node *tmp_hnode, *n;
1842 struct hlist_head *head;
1843 u64 src_addr = 0;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001844 __le16 vlan_id = 0;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001845 u8 hindex;
1846
1847 if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1848 return;
1849
1850 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1851 return;
1852
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001853 /* Only NPAR capable devices support vlan based learning*/
1854 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1855 vlan_id = first_desc->vlan_TCI;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001856 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1857 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1858 head = &(adapter->fhash.fhead[hindex]);
1859
1860 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001861 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1862 tmp_fil->vlan_id == vlan_id) {
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001863 tmp_fil->ftime = jiffies;
1864 return;
1865 }
1866 }
1867
1868 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1869 if (!fil)
1870 return;
1871
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001872 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001873
1874 fil->ftime = jiffies;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001875 fil->vlan_id = vlan_id;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001876 memcpy(fil->faddr, &src_addr, ETH_ALEN);
1877 spin_lock(&adapter->mac_learn_lock);
1878 hlist_add_head(&(fil->fnode), head);
1879 adapter->fhash.fnum++;
1880 spin_unlock(&adapter->mac_learn_lock);
1881}
1882
1883static void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001884qlcnic_tso_check(struct net_device *netdev,
1885 struct qlcnic_host_tx_ring *tx_ring,
1886 struct cmd_desc_type0 *first_desc,
1887 struct sk_buff *skb)
1888{
1889 u8 opcode = TX_ETHER_PKT;
1890 __be16 protocol = skb->protocol;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00001891 u16 flags = 0;
1892 int copied, offset, copy_len, hdr_len = 0, tso = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001893 struct cmd_desc_type0 *hwdesc;
1894 struct vlan_ethhdr *vh;
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001895 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001896 u32 producer = tx_ring->producer;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001897 __le16 vlan_oob = first_desc->flags_opcode &
1898 cpu_to_le16(FLAGS_VLAN_OOB);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001899
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001900 if (*(skb->data) & BIT_0) {
1901 flags |= BIT_0;
1902 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
1903 }
1904
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001905 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1906 skb_shinfo(skb)->gso_size > 0) {
1907
1908 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1909
1910 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1911 first_desc->total_hdr_length = hdr_len;
1912 if (vlan_oob) {
1913 first_desc->total_hdr_length += VLAN_HLEN;
1914 first_desc->tcp_hdr_offset = VLAN_HLEN;
1915 first_desc->ip_hdr_offset = VLAN_HLEN;
1916 /* Only in case of TSO on vlan device */
1917 flags |= FLAGS_VLAN_TAGGED;
1918 }
1919
1920 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1921 TX_TCP_LSO6 : TX_TCP_LSO;
1922 tso = 1;
1923
1924 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1925 u8 l4proto;
1926
1927 if (protocol == cpu_to_be16(ETH_P_IP)) {
1928 l4proto = ip_hdr(skb)->protocol;
1929
1930 if (l4proto == IPPROTO_TCP)
1931 opcode = TX_TCP_PKT;
1932 else if (l4proto == IPPROTO_UDP)
1933 opcode = TX_UDP_PKT;
1934 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1935 l4proto = ipv6_hdr(skb)->nexthdr;
1936
1937 if (l4proto == IPPROTO_TCP)
1938 opcode = TX_TCPV6_PKT;
1939 else if (l4proto == IPPROTO_UDP)
1940 opcode = TX_UDPV6_PKT;
1941 }
1942 }
1943
1944 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1945 first_desc->ip_hdr_offset += skb_network_offset(skb);
1946 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
1947
1948 if (!tso)
1949 return;
1950
1951 /* For LSO, we need to copy the MAC/IP/TCP headers into
1952 * the descriptor ring
1953 */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001954 copied = 0;
1955 offset = 2;
1956
1957 if (vlan_oob) {
1958 /* Create a TSO vlan header template for firmware */
1959
1960 hwdesc = &tx_ring->desc_head[producer];
1961 tx_ring->cmd_buf_arr[producer].skb = NULL;
1962
1963 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1964 hdr_len + VLAN_HLEN);
1965
1966 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1967 skb_copy_from_linear_data(skb, vh, 12);
1968 vh->h_vlan_proto = htons(ETH_P_8021Q);
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001969 vh->h_vlan_TCI = (__be16)swab16((u16)first_desc->vlan_TCI);
1970
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001971 skb_copy_from_linear_data_offset(skb, 12,
1972 (char *)vh + 16, copy_len - 16);
1973
1974 copied = copy_len - VLAN_HLEN;
1975 offset = 0;
1976
1977 producer = get_next_index(producer, tx_ring->num_desc);
1978 }
1979
1980 while (copied < hdr_len) {
1981
1982 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1983 (hdr_len - copied));
1984
1985 hwdesc = &tx_ring->desc_head[producer];
1986 tx_ring->cmd_buf_arr[producer].skb = NULL;
1987
1988 skb_copy_from_linear_data_offset(skb, copied,
1989 (char *)hwdesc + offset, copy_len);
1990
1991 copied += copy_len;
1992 offset = 0;
1993
1994 producer = get_next_index(producer, tx_ring->num_desc);
1995 }
1996
1997 tx_ring->producer = producer;
1998 barrier();
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001999 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002000}
2001
2002static int
2003qlcnic_map_tx_skb(struct pci_dev *pdev,
2004 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2005{
2006 struct qlcnic_skb_frag *nf;
2007 struct skb_frag_struct *frag;
2008 int i, nr_frags;
2009 dma_addr_t map;
2010
2011 nr_frags = skb_shinfo(skb)->nr_frags;
2012 nf = &pbuf->frag_array[0];
2013
2014 map = pci_map_single(pdev, skb->data,
2015 skb_headlen(skb), PCI_DMA_TODEVICE);
2016 if (pci_dma_mapping_error(pdev, map))
2017 goto out_err;
2018
2019 nf->dma = map;
2020 nf->length = skb_headlen(skb);
2021
2022 for (i = 0; i < nr_frags; i++) {
2023 frag = &skb_shinfo(skb)->frags[i];
2024 nf = &pbuf->frag_array[i+1];
2025
2026 map = pci_map_page(pdev, frag->page, frag->page_offset,
2027 frag->size, PCI_DMA_TODEVICE);
2028 if (pci_dma_mapping_error(pdev, map))
2029 goto unwind;
2030
2031 nf->dma = map;
2032 nf->length = frag->size;
2033 }
2034
2035 return 0;
2036
2037unwind:
2038 while (--i >= 0) {
2039 nf = &pbuf->frag_array[i+1];
2040 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2041 }
2042
2043 nf = &pbuf->frag_array[0];
2044 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2045
2046out_err:
2047 return -ENOMEM;
2048}
2049
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002050static int
2051qlcnic_check_tx_tagging(struct qlcnic_adapter *adapter,
2052 struct sk_buff *skb,
2053 struct cmd_desc_type0 *first_desc)
2054{
2055 u8 opcode = 0;
2056 u16 flags = 0;
2057 __be16 protocol = skb->protocol;
2058 struct vlan_ethhdr *vh;
2059
2060 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
2061 vh = (struct vlan_ethhdr *)skb->data;
2062 protocol = vh->h_vlan_encapsulated_proto;
2063 flags = FLAGS_VLAN_TAGGED;
2064 qlcnic_set_tx_vlan_tci(first_desc, ntohs(vh->h_vlan_TCI));
2065 } else if (vlan_tx_tag_present(skb)) {
2066 flags = FLAGS_VLAN_OOB;
2067 qlcnic_set_tx_vlan_tci(first_desc, vlan_tx_tag_get(skb));
2068 }
2069 if (unlikely(adapter->pvid)) {
2070 if (first_desc->vlan_TCI &&
2071 !(adapter->flags & QLCNIC_TAGGING_ENABLED))
2072 return -EIO;
2073 if (first_desc->vlan_TCI &&
2074 (adapter->flags & QLCNIC_TAGGING_ENABLED))
2075 goto set_flags;
2076
2077 flags = FLAGS_VLAN_OOB;
2078 qlcnic_set_tx_vlan_tci(first_desc, adapter->pvid);
2079 }
2080set_flags:
2081 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2082 return 0;
2083}
2084
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002085static inline void
2086qlcnic_clear_cmddesc(u64 *desc)
2087{
2088 desc[0] = 0ULL;
2089 desc[2] = 0ULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002090 desc[7] = 0ULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002091}
2092
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00002093netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002094qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2095{
2096 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2097 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2098 struct qlcnic_cmd_buffer *pbuf;
2099 struct qlcnic_skb_frag *buffrag;
2100 struct cmd_desc_type0 *hwdesc, *first_desc;
2101 struct pci_dev *pdev;
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002102 struct ethhdr *phdr;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002103 int i, k;
2104
2105 u32 producer;
2106 int frag_count, no_of_desc;
2107 u32 num_txd = tx_ring->num_desc;
2108
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00002109 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2110 netif_stop_queue(netdev);
2111 return NETDEV_TX_BUSY;
2112 }
2113
Sony Chackofe4d4342010-08-19 05:08:27 +00002114 if (adapter->flags & QLCNIC_MACSPOOF) {
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002115 phdr = (struct ethhdr *)skb->data;
2116 if (compare_ether_addr(phdr->h_source,
Sony Chackofe4d4342010-08-19 05:08:27 +00002117 adapter->mac_addr))
2118 goto drop_packet;
2119 }
2120
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002121 frag_count = skb_shinfo(skb)->nr_frags + 1;
2122
2123 /* 4 fragments per cmd des */
2124 no_of_desc = (frag_count + 3) >> 2;
2125
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002126 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002127 netif_stop_queue(netdev);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002128 smp_mb();
2129 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2130 netif_start_queue(netdev);
2131 else {
2132 adapter->stats.xmit_off++;
2133 return NETDEV_TX_BUSY;
2134 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002135 }
2136
2137 producer = tx_ring->producer;
2138 pbuf = &tx_ring->cmd_buf_arr[producer];
2139
2140 pdev = adapter->pdev;
2141
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002142 first_desc = hwdesc = &tx_ring->desc_head[producer];
2143 qlcnic_clear_cmddesc((u64 *)hwdesc);
2144
2145 if (qlcnic_check_tx_tagging(adapter, skb, first_desc))
2146 goto drop_packet;
2147
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002148 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2149 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002150 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002151 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002152
2153 pbuf->skb = skb;
2154 pbuf->frag_count = frag_count;
2155
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002156 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2157 qlcnic_set_tx_port(first_desc, adapter->portnum);
2158
2159 for (i = 0; i < frag_count; i++) {
2160
2161 k = i % 4;
2162
2163 if ((k == 0) && (i > 0)) {
2164 /* move to next desc.*/
2165 producer = get_next_index(producer, num_txd);
2166 hwdesc = &tx_ring->desc_head[producer];
2167 qlcnic_clear_cmddesc((u64 *)hwdesc);
2168 tx_ring->cmd_buf_arr[producer].skb = NULL;
2169 }
2170
2171 buffrag = &pbuf->frag_array[i];
2172
2173 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2174 switch (k) {
2175 case 0:
2176 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2177 break;
2178 case 1:
2179 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2180 break;
2181 case 2:
2182 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2183 break;
2184 case 3:
2185 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2186 break;
2187 }
2188 }
2189
2190 tx_ring->producer = get_next_index(producer, num_txd);
2191
2192 qlcnic_tso_check(netdev, tx_ring, first_desc, skb);
2193
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002194 if (qlcnic_mac_learn)
2195 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2196
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002197 qlcnic_update_cmd_producer(adapter, tx_ring);
2198
2199 adapter->stats.txbytes += skb->len;
2200 adapter->stats.xmitcalled++;
2201
2202 return NETDEV_TX_OK;
2203
2204drop_packet:
2205 adapter->stats.txdropped++;
2206 dev_kfree_skb_any(skb);
2207 return NETDEV_TX_OK;
2208}
2209
2210static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2211{
2212 struct net_device *netdev = adapter->netdev;
2213 u32 temp, temp_state, temp_val;
2214 int rv = 0;
2215
2216 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2217
2218 temp_state = qlcnic_get_temp_state(temp);
2219 temp_val = qlcnic_get_temp_val(temp);
2220
2221 if (temp_state == QLCNIC_TEMP_PANIC) {
2222 dev_err(&netdev->dev,
2223 "Device temperature %d degrees C exceeds"
2224 " maximum allowed. Hardware has been shut down.\n",
2225 temp_val);
2226 rv = 1;
2227 } else if (temp_state == QLCNIC_TEMP_WARN) {
2228 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2229 dev_err(&netdev->dev,
2230 "Device temperature %d degrees C "
2231 "exceeds operating range."
2232 " Immediate action needed.\n",
2233 temp_val);
2234 }
2235 } else {
2236 if (adapter->temp == QLCNIC_TEMP_WARN) {
2237 dev_info(&netdev->dev,
2238 "Device temperature is now %d degrees C"
2239 " in normal range.\n", temp_val);
2240 }
2241 }
2242 adapter->temp = temp_state;
2243 return rv;
2244}
2245
2246void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2247{
2248 struct net_device *netdev = adapter->netdev;
2249
2250 if (adapter->ahw.linkup && !linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002251 netdev_info(netdev, "NIC Link is down\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002252 adapter->ahw.linkup = 0;
2253 if (netif_running(netdev)) {
2254 netif_carrier_off(netdev);
2255 netif_stop_queue(netdev);
2256 }
2257 } else if (!adapter->ahw.linkup && linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002258 netdev_info(netdev, "NIC Link is up\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002259 adapter->ahw.linkup = 1;
2260 if (netif_running(netdev)) {
2261 netif_carrier_on(netdev);
2262 netif_wake_queue(netdev);
2263 }
2264 }
2265}
2266
2267static void qlcnic_tx_timeout(struct net_device *netdev)
2268{
2269 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2270
2271 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2272 return;
2273
2274 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002275
2276 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002277 adapter->need_fw_reset = 1;
2278 else
2279 adapter->reset_context = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002280}
2281
2282static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2283{
2284 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2285 struct net_device_stats *stats = &netdev->stats;
2286
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002287 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2288 stats->tx_packets = adapter->stats.xmitfinished;
Sucheta Chakraborty7e382592010-05-17 01:22:10 +00002289 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002290 stats->tx_bytes = adapter->stats.txbytes;
2291 stats->rx_dropped = adapter->stats.rxdropped;
2292 stats->tx_dropped = adapter->stats.txdropped;
2293
2294 return stats;
2295}
2296
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002297static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002298{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002299 u32 status;
2300
2301 status = readl(adapter->isr_int_vec);
2302
2303 if (!(status & adapter->int_vec_bit))
2304 return IRQ_NONE;
2305
2306 /* check interrupt state machine, to be sure */
2307 status = readl(adapter->crb_int_state_reg);
2308 if (!ISR_LEGACY_INT_TRIGGERED(status))
2309 return IRQ_NONE;
2310
2311 writel(0xffffffff, adapter->tgt_status_reg);
2312 /* read twice to ensure write is flushed */
2313 readl(adapter->isr_int_vec);
2314 readl(adapter->isr_int_vec);
2315
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002316 return IRQ_HANDLED;
2317}
2318
2319static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2320{
2321 struct qlcnic_host_sds_ring *sds_ring = data;
2322 struct qlcnic_adapter *adapter = sds_ring->adapter;
2323
2324 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2325 goto done;
2326 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2327 writel(0xffffffff, adapter->tgt_status_reg);
2328 goto done;
2329 }
2330
2331 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2332 return IRQ_NONE;
2333
2334done:
2335 adapter->diag_cnt++;
2336 qlcnic_enable_int(sds_ring);
2337 return IRQ_HANDLED;
2338}
2339
2340static irqreturn_t qlcnic_intr(int irq, void *data)
2341{
2342 struct qlcnic_host_sds_ring *sds_ring = data;
2343 struct qlcnic_adapter *adapter = sds_ring->adapter;
2344
2345 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2346 return IRQ_NONE;
2347
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002348 napi_schedule(&sds_ring->napi);
2349
2350 return IRQ_HANDLED;
2351}
2352
2353static irqreturn_t qlcnic_msi_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 /* clear interrupt */
2359 writel(0xffffffff, adapter->tgt_status_reg);
2360
2361 napi_schedule(&sds_ring->napi);
2362 return IRQ_HANDLED;
2363}
2364
2365static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2366{
2367 struct qlcnic_host_sds_ring *sds_ring = data;
2368
2369 napi_schedule(&sds_ring->napi);
2370 return IRQ_HANDLED;
2371}
2372
2373static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2374{
2375 u32 sw_consumer, hw_consumer;
2376 int count = 0, i;
2377 struct qlcnic_cmd_buffer *buffer;
2378 struct pci_dev *pdev = adapter->pdev;
2379 struct net_device *netdev = adapter->netdev;
2380 struct qlcnic_skb_frag *frag;
2381 int done;
2382 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2383
2384 if (!spin_trylock(&adapter->tx_clean_lock))
2385 return 1;
2386
2387 sw_consumer = tx_ring->sw_consumer;
2388 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2389
2390 while (sw_consumer != hw_consumer) {
2391 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2392 if (buffer->skb) {
2393 frag = &buffer->frag_array[0];
2394 pci_unmap_single(pdev, frag->dma, frag->length,
2395 PCI_DMA_TODEVICE);
2396 frag->dma = 0ULL;
2397 for (i = 1; i < buffer->frag_count; i++) {
2398 frag++;
2399 pci_unmap_page(pdev, frag->dma, frag->length,
2400 PCI_DMA_TODEVICE);
2401 frag->dma = 0ULL;
2402 }
2403
2404 adapter->stats.xmitfinished++;
2405 dev_kfree_skb_any(buffer->skb);
2406 buffer->skb = NULL;
2407 }
2408
2409 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2410 if (++count >= MAX_STATUS_HANDLE)
2411 break;
2412 }
2413
2414 if (count && netif_running(netdev)) {
2415 tx_ring->sw_consumer = sw_consumer;
2416
2417 smp_mb();
2418
2419 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002420 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2421 netif_wake_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00002422 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002423 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002424 }
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002425 adapter->tx_timeo_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002426 }
2427 /*
2428 * If everything is freed up to consumer then check if the ring is full
2429 * If the ring is full then check if more needs to be freed and
2430 * schedule the call back again.
2431 *
2432 * This happens when there are 2 CPUs. One could be freeing and the
2433 * other filling it. If the ring is full when we get out of here and
2434 * the card has already interrupted the host then the host can miss the
2435 * interrupt.
2436 *
2437 * There is still a possible race condition and the host could miss an
2438 * interrupt. The card has to take care of this.
2439 */
2440 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2441 done = (sw_consumer == hw_consumer);
2442 spin_unlock(&adapter->tx_clean_lock);
2443
2444 return done;
2445}
2446
2447static int qlcnic_poll(struct napi_struct *napi, int budget)
2448{
2449 struct qlcnic_host_sds_ring *sds_ring =
2450 container_of(napi, struct qlcnic_host_sds_ring, napi);
2451
2452 struct qlcnic_adapter *adapter = sds_ring->adapter;
2453
2454 int tx_complete;
2455 int work_done;
2456
2457 tx_complete = qlcnic_process_cmd_ring(adapter);
2458
2459 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2460
2461 if ((work_done < budget) && tx_complete) {
2462 napi_complete(&sds_ring->napi);
2463 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2464 qlcnic_enable_int(sds_ring);
2465 }
2466
2467 return work_done;
2468}
2469
schacko8f891382010-06-17 02:56:40 +00002470static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2471{
2472 struct qlcnic_host_sds_ring *sds_ring =
2473 container_of(napi, struct qlcnic_host_sds_ring, napi);
2474
2475 struct qlcnic_adapter *adapter = sds_ring->adapter;
2476 int work_done;
2477
2478 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2479
2480 if (work_done < budget) {
2481 napi_complete(&sds_ring->napi);
2482 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2483 qlcnic_enable_int(sds_ring);
2484 }
2485
2486 return work_done;
2487}
2488
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002489#ifdef CONFIG_NET_POLL_CONTROLLER
2490static void qlcnic_poll_controller(struct net_device *netdev)
2491{
Yinglin Luanbf827912010-08-22 21:57:56 +00002492 int ring;
2493 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002494 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Yinglin Luanbf827912010-08-22 21:57:56 +00002495 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
2496
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002497 disable_irq(adapter->irq);
Yinglin Luanbf827912010-08-22 21:57:56 +00002498 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2499 sds_ring = &recv_ctx->sds_rings[ring];
2500 qlcnic_intr(adapter->irq, sds_ring);
2501 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002502 enable_irq(adapter->irq);
2503}
2504#endif
2505
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002506static void
2507qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2508{
2509 u32 val;
2510
2511 val = adapter->portnum & 0xf;
2512 val |= encoding << 7;
2513 val |= (jiffies - adapter->dev_rst_time) << 8;
2514
2515 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2516 adapter->dev_rst_time = jiffies;
2517}
2518
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002519static int
2520qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002521{
2522 u32 val;
2523
2524 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2525 state != QLCNIC_DEV_NEED_QUISCENT);
2526
2527 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002528 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002529
2530 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2531
2532 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002533 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002534 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002535 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002536
2537 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2538
2539 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002540
2541 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002542}
2543
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002544static int
2545qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2546{
2547 u32 val;
2548
2549 if (qlcnic_api_lock(adapter))
2550 return -EBUSY;
2551
2552 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002553 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002554 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2555
2556 qlcnic_api_unlock(adapter);
2557
2558 return 0;
2559}
2560
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002561static void
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002562qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002563{
2564 u32 val;
2565
2566 if (qlcnic_api_lock(adapter))
2567 goto err;
2568
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002569 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002570 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002571 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002572
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002573 if (failed) {
2574 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2575 dev_info(&adapter->pdev->dev,
2576 "Device state set to Failed. Please Reboot\n");
2577 } else if (!(val & 0x11111111))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002578 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2579
2580 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002581 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002582 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2583
2584 qlcnic_api_unlock(adapter);
2585err:
2586 adapter->fw_fail_cnt = 0;
2587 clear_bit(__QLCNIC_START_FW, &adapter->state);
2588 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2589}
2590
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002591/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002592static int
2593qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2594{
2595 int act, state;
2596
2597 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002598 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002599
2600 if (((state & 0x11111111) == (act & 0x11111111)) ||
2601 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2602 return 0;
2603 else
2604 return 1;
2605}
2606
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002607static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2608{
2609 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2610
2611 if (val != QLCNIC_DRV_IDC_VER) {
2612 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2613 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2614 }
2615
2616 return 0;
2617}
2618
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002619static int
2620qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2621{
2622 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002623 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002624 u8 portnum = adapter->portnum;
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002625 u8 ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002626
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002627 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2628 return 1;
2629
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002630 if (qlcnic_api_lock(adapter))
2631 return -1;
2632
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002633 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002634 if (!(val & (1 << (portnum * 4)))) {
2635 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002636 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002637 }
2638
2639 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002640 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002641
2642 switch (prev_state) {
2643 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002644 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002645 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002646 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002647 qlcnic_api_unlock(adapter);
2648 return 1;
2649
2650 case QLCNIC_DEV_READY:
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002651 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002652 qlcnic_api_unlock(adapter);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002653 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002654
2655 case QLCNIC_DEV_NEED_RESET:
2656 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002657 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002658 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2659 break;
2660
2661 case QLCNIC_DEV_NEED_QUISCENT:
2662 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002663 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002664 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2665 break;
2666
2667 case QLCNIC_DEV_FAILED:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00002668 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002669 qlcnic_api_unlock(adapter);
2670 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002671
2672 case QLCNIC_DEV_INITIALIZING:
2673 case QLCNIC_DEV_QUISCENT:
2674 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002675 }
2676
2677 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002678
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002679 do {
2680 msleep(1000);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002681 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2682
2683 if (prev_state == QLCNIC_DEV_QUISCENT)
2684 continue;
2685 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002686
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002687 if (!dev_init_timeo) {
2688 dev_err(&adapter->pdev->dev,
2689 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002690 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002691 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002692
2693 if (qlcnic_api_lock(adapter))
2694 return -1;
2695
2696 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002697 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002698 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2699
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002700 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002701 qlcnic_api_unlock(adapter);
2702
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002703 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002704}
2705
2706static void
2707qlcnic_fwinit_work(struct work_struct *work)
2708{
2709 struct qlcnic_adapter *adapter = container_of(work,
2710 struct qlcnic_adapter, fw_work.work);
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002711 u32 dev_state = 0xf;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002712
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002713 if (qlcnic_api_lock(adapter))
2714 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002715
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002716 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002717 if (dev_state == QLCNIC_DEV_QUISCENT ||
2718 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002719 qlcnic_api_unlock(adapter);
2720 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2721 FW_POLL_DELAY * 2);
2722 return;
2723 }
2724
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002725 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002726 qlcnic_api_unlock(adapter);
2727 goto wait_npar;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002728 }
2729
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002730 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2731 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2732 adapter->reset_ack_timeo);
2733 goto skip_ack_check;
2734 }
2735
2736 if (!qlcnic_check_drv_state(adapter)) {
2737skip_ack_check:
2738 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002739
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002740 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2741 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2742 QLCNIC_DEV_INITIALIZING);
2743 set_bit(__QLCNIC_START_FW, &adapter->state);
2744 QLCDB(adapter, DRV, "Restarting fw\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002745 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002746 }
2747
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002748 qlcnic_api_unlock(adapter);
2749
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002750 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002751 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002752 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002753 return;
2754 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002755 goto err_ret;
2756 }
2757
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002758 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002759
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002760wait_npar:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002761 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002762 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002763
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002764 switch (dev_state) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002765 case QLCNIC_DEV_READY:
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002766 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002767 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002768 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002769 return;
2770 }
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002771 case QLCNIC_DEV_FAILED:
2772 break;
2773 default:
2774 qlcnic_schedule_work(adapter,
2775 qlcnic_fwinit_work, FW_POLL_DELAY);
2776 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002777 }
2778
2779err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002780 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2781 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002782 netif_device_attach(adapter->netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002783 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002784}
2785
2786static void
2787qlcnic_detach_work(struct work_struct *work)
2788{
2789 struct qlcnic_adapter *adapter = container_of(work,
2790 struct qlcnic_adapter, fw_work.work);
2791 struct net_device *netdev = adapter->netdev;
2792 u32 status;
2793
2794 netif_device_detach(netdev);
2795
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002796 /* Dont grab rtnl lock during Quiscent mode */
2797 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2798 if (netif_running(netdev))
2799 __qlcnic_down(adapter, netdev);
2800 } else
2801 qlcnic_down(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002802
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002803 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2804
2805 if (status & QLCNIC_RCODE_FATAL_ERROR)
2806 goto err_ret;
2807
2808 if (adapter->temp == QLCNIC_TEMP_PANIC)
2809 goto err_ret;
2810
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002811 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2812 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002813
2814 adapter->fw_wait_cnt = 0;
2815
2816 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2817
2818 return;
2819
2820err_ret:
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002821 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2822 status, adapter->temp);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002823 netif_device_attach(netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002824 qlcnic_clr_all_drv_state(adapter, 1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002825}
2826
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002827/*Transit NPAR state to NON Operational */
2828static void
2829qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2830{
2831 u32 state;
2832
2833 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2834 if (state == QLCNIC_DEV_NPAR_NON_OPER)
2835 return;
2836
2837 if (qlcnic_api_lock(adapter))
2838 return;
2839 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2840 qlcnic_api_unlock(adapter);
2841}
2842
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002843/* Caller should held RESETTING bit.
2844 * This should be call in sync with qlcnic_request_quiscent_mode.
2845 */
2846void qlcnic_clear_quiscent_mode(struct qlcnic_adapter *adapter)
2847{
2848 qlcnic_clr_drv_state(adapter);
2849 qlcnic_api_lock(adapter);
2850 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
2851 qlcnic_api_unlock(adapter);
2852}
2853
2854/* Caller should held RESETTING bit.
2855 */
2856int qlcnic_request_quiscent_mode(struct qlcnic_adapter *adapter)
2857{
2858 u8 timeo = adapter->dev_init_timeo / 2;
2859 u32 state;
2860
2861 if (qlcnic_api_lock(adapter))
2862 return -EIO;
2863
2864 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2865 if (state != QLCNIC_DEV_READY)
2866 return -EIO;
2867
2868 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_QUISCENT);
2869 qlcnic_api_unlock(adapter);
2870 QLCDB(adapter, DRV, "NEED QUISCENT state set\n");
2871 qlcnic_idc_debug_info(adapter, 0);
2872
2873 qlcnic_set_drv_state(adapter, QLCNIC_DEV_NEED_QUISCENT);
2874
2875 do {
2876 msleep(2000);
2877 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2878 if (state == QLCNIC_DEV_QUISCENT)
2879 return 0;
2880 if (!qlcnic_check_drv_state(adapter)) {
2881 if (qlcnic_api_lock(adapter))
2882 return -EIO;
2883 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2884 QLCNIC_DEV_QUISCENT);
2885 qlcnic_api_unlock(adapter);
2886 QLCDB(adapter, DRV, "QUISCENT mode set\n");
2887 return 0;
2888 }
2889 } while (--timeo);
2890
2891 dev_err(&adapter->pdev->dev, "Failed to quiesce device, DRV_STATE=%08x"
2892 " DRV_ACTIVE=%08x\n", QLCRD32(adapter, QLCNIC_CRB_DRV_STATE),
2893 QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE));
2894 qlcnic_clear_quiscent_mode(adapter);
2895 return -EIO;
2896}
2897
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002898/*Transit to RESET state from READY state only */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002899static void
2900qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2901{
2902 u32 state;
2903
Anirban Chakrabortycea89752010-07-13 20:33:35 +00002904 adapter->need_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002905 if (qlcnic_api_lock(adapter))
2906 return;
2907
2908 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2909
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002910 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002911 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002912 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002913 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002914 }
2915
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002916 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002917 qlcnic_api_unlock(adapter);
2918}
2919
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002920/* Transit to NPAR READY state from NPAR NOT READY state */
2921static void
2922qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2923{
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002924 if (qlcnic_api_lock(adapter))
2925 return;
2926
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002927 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
2928 QLCDB(adapter, DRV, "NPAR operational state set\n");
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002929
2930 qlcnic_api_unlock(adapter);
2931}
2932
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002933static void
2934qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2935 work_func_t func, int delay)
2936{
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00002937 if (test_bit(__QLCNIC_AER, &adapter->state))
2938 return;
2939
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002940 INIT_DELAYED_WORK(&adapter->fw_work, func);
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00002941 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
2942 round_jiffies_relative(delay));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002943}
2944
2945static void
2946qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2947{
2948 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2949 msleep(10);
2950
2951 cancel_delayed_work_sync(&adapter->fw_work);
2952}
2953
2954static void
2955qlcnic_attach_work(struct work_struct *work)
2956{
2957 struct qlcnic_adapter *adapter = container_of(work,
2958 struct qlcnic_adapter, fw_work.work);
2959 struct net_device *netdev = adapter->netdev;
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002960 u32 npar_state;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002961
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002962 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
2963 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2964 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
2965 qlcnic_clr_all_drv_state(adapter, 0);
2966 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
2967 qlcnic_schedule_work(adapter, qlcnic_attach_work,
2968 FW_POLL_DELAY);
2969 else
2970 goto attach;
2971 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
2972 return;
2973 }
2974attach:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002975 if (netif_running(netdev)) {
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00002976 if (qlcnic_up(adapter, netdev))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002977 goto done;
2978
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00002979 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002980 }
2981
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002982done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002983 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002984 adapter->fw_fail_cnt = 0;
2985 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002986
2987 if (!qlcnic_clr_drv_state(adapter))
2988 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2989 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002990}
2991
2992static int
2993qlcnic_check_health(struct qlcnic_adapter *adapter)
2994{
Sony Chacko4e708122010-08-31 17:17:44 +00002995 u32 state = 0, heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002996 struct net_device *netdev = adapter->netdev;
2997
2998 if (qlcnic_check_temp(adapter))
2999 goto detach;
3000
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00003001 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003002 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003003
3004 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00003005 if (state == QLCNIC_DEV_NEED_RESET) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003006 qlcnic_set_npar_non_operational(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003007 adapter->need_fw_reset = 1;
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00003008 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3009 goto detach;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003010
Sony Chacko4e708122010-08-31 17:17:44 +00003011 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3012 if (heartbeat != adapter->heartbeat) {
3013 adapter->heartbeat = heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003014 adapter->fw_fail_cnt = 0;
3015 if (adapter->need_fw_reset)
3016 goto detach;
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003017
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00003018 if (adapter->reset_context &&
3019 auto_fw_reset == AUTO_FW_RESET_ENABLED) {
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003020 qlcnic_reset_hw_context(adapter);
3021 adapter->netdev->trans_start = jiffies;
3022 }
3023
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003024 return 0;
3025 }
3026
3027 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3028 return 0;
3029
3030 qlcnic_dev_request_reset(adapter);
3031
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00003032 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED))
3033 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003034
3035 dev_info(&netdev->dev, "firmware hang detected\n");
3036
3037detach:
3038 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3039 QLCNIC_DEV_NEED_RESET;
3040
3041 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003042 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3043
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003044 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003045 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3046 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003047
3048 return 1;
3049}
3050
3051static void
3052qlcnic_fw_poll_work(struct work_struct *work)
3053{
3054 struct qlcnic_adapter *adapter = container_of(work,
3055 struct qlcnic_adapter, fw_work.work);
3056
3057 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3058 goto reschedule;
3059
3060
3061 if (qlcnic_check_health(adapter))
3062 return;
3063
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00003064 if (adapter->fhash.fnum)
3065 qlcnic_prune_lb_filters(adapter);
3066
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003067reschedule:
3068 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3069}
3070
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003071static int qlcnic_is_first_func(struct pci_dev *pdev)
3072{
3073 struct pci_dev *oth_pdev;
3074 int val = pdev->devfn;
3075
3076 while (val-- > 0) {
3077 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3078 (pdev->bus), pdev->bus->number,
3079 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003080 if (!oth_pdev)
3081 continue;
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003082
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003083 if (oth_pdev->current_state != PCI_D3cold) {
3084 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003085 return 0;
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003086 }
3087 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003088 }
3089 return 1;
3090}
3091
3092static int qlcnic_attach_func(struct pci_dev *pdev)
3093{
3094 int err, first_func;
3095 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3096 struct net_device *netdev = adapter->netdev;
3097
3098 pdev->error_state = pci_channel_io_normal;
3099
3100 err = pci_enable_device(pdev);
3101 if (err)
3102 return err;
3103
3104 pci_set_power_state(pdev, PCI_D0);
3105 pci_set_master(pdev);
3106 pci_restore_state(pdev);
3107
3108 first_func = qlcnic_is_first_func(pdev);
3109
3110 if (qlcnic_api_lock(adapter))
3111 return -EINVAL;
3112
Amit Kumar Salecha933fce122010-08-17 00:34:19 +00003113 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003114 adapter->need_fw_reset = 1;
3115 set_bit(__QLCNIC_START_FW, &adapter->state);
3116 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3117 QLCDB(adapter, DRV, "Restarting fw\n");
3118 }
3119 qlcnic_api_unlock(adapter);
3120
3121 err = adapter->nic_ops->start_firmware(adapter);
3122 if (err)
3123 return err;
3124
3125 qlcnic_clr_drv_state(adapter);
3126 qlcnic_setup_intr(adapter);
3127
3128 if (netif_running(netdev)) {
3129 err = qlcnic_attach(adapter);
3130 if (err) {
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00003131 qlcnic_clr_all_drv_state(adapter, 1);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003132 clear_bit(__QLCNIC_AER, &adapter->state);
3133 netif_device_attach(netdev);
3134 return err;
3135 }
3136
3137 err = qlcnic_up(adapter, netdev);
3138 if (err)
3139 goto done;
3140
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003141 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003142 }
3143 done:
3144 netif_device_attach(netdev);
3145 return err;
3146}
3147
3148static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3149 pci_channel_state_t state)
3150{
3151 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3152 struct net_device *netdev = adapter->netdev;
3153
3154 if (state == pci_channel_io_perm_failure)
3155 return PCI_ERS_RESULT_DISCONNECT;
3156
3157 if (state == pci_channel_io_normal)
3158 return PCI_ERS_RESULT_RECOVERED;
3159
3160 set_bit(__QLCNIC_AER, &adapter->state);
3161 netif_device_detach(netdev);
3162
3163 cancel_delayed_work_sync(&adapter->fw_work);
3164
3165 if (netif_running(netdev))
3166 qlcnic_down(adapter, netdev);
3167
3168 qlcnic_detach(adapter);
3169 qlcnic_teardown_intr(adapter);
3170
3171 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3172
3173 pci_save_state(pdev);
3174 pci_disable_device(pdev);
3175
3176 return PCI_ERS_RESULT_NEED_RESET;
3177}
3178
3179static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3180{
3181 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3182 PCI_ERS_RESULT_RECOVERED;
3183}
3184
3185static void qlcnic_io_resume(struct pci_dev *pdev)
3186{
3187 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3188
3189 pci_cleanup_aer_uncorrect_error_status(pdev);
3190
3191 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3192 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3193 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3194 FW_POLL_DELAY);
3195}
3196
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003197static int
3198qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3199{
3200 int err;
3201
3202 err = qlcnic_can_start_firmware(adapter);
3203 if (err)
3204 return err;
3205
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +00003206 err = qlcnic_check_npar_opertional(adapter);
3207 if (err)
3208 return err;
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003209
Rajesh Borundia174240a2010-08-31 17:17:47 +00003210 err = qlcnic_initialize_nic(adapter);
3211 if (err)
3212 return err;
3213
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003214 qlcnic_check_options(adapter);
3215
Rajesh Borundia73733732010-08-31 17:17:50 +00003216 err = qlcnic_set_eswitch_port_config(adapter);
3217 if (err)
3218 return err;
3219
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003220 adapter->need_fw_reset = 0;
3221
3222 return err;
3223}
3224
3225static int
3226qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3227{
3228 return -EOPNOTSUPP;
3229}
3230
3231static int
3232qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3233{
3234 return -EOPNOTSUPP;
3235}
3236
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003237static ssize_t
3238qlcnic_store_bridged_mode(struct device *dev,
3239 struct device_attribute *attr, const char *buf, size_t len)
3240{
3241 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3242 unsigned long new;
3243 int ret = -EINVAL;
3244
3245 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3246 goto err_out;
3247
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00003248 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003249 goto err_out;
3250
3251 if (strict_strtoul(buf, 2, &new))
3252 goto err_out;
3253
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00003254 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003255 ret = len;
3256
3257err_out:
3258 return ret;
3259}
3260
3261static ssize_t
3262qlcnic_show_bridged_mode(struct device *dev,
3263 struct device_attribute *attr, char *buf)
3264{
3265 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3266 int bridged_mode = 0;
3267
3268 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3269 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3270
3271 return sprintf(buf, "%d\n", bridged_mode);
3272}
3273
3274static struct device_attribute dev_attr_bridged_mode = {
3275 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3276 .show = qlcnic_show_bridged_mode,
3277 .store = qlcnic_store_bridged_mode,
3278};
3279
3280static ssize_t
3281qlcnic_store_diag_mode(struct device *dev,
3282 struct device_attribute *attr, const char *buf, size_t len)
3283{
3284 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3285 unsigned long new;
3286
3287 if (strict_strtoul(buf, 2, &new))
3288 return -EINVAL;
3289
3290 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3291 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3292
3293 return len;
3294}
3295
3296static ssize_t
3297qlcnic_show_diag_mode(struct device *dev,
3298 struct device_attribute *attr, char *buf)
3299{
3300 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3301
3302 return sprintf(buf, "%d\n",
3303 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3304}
3305
3306static struct device_attribute dev_attr_diag_mode = {
3307 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3308 .show = qlcnic_show_diag_mode,
3309 .store = qlcnic_store_diag_mode,
3310};
3311
3312static int
3313qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3314 loff_t offset, size_t size)
3315{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003316 size_t crb_size = 4;
3317
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003318 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3319 return -EIO;
3320
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003321 if (offset < QLCNIC_PCI_CRBSPACE) {
3322 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3323 QLCNIC_PCI_CAMQM_END))
3324 crb_size = 8;
3325 else
3326 return -EINVAL;
3327 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003328
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003329 if ((size != crb_size) || (offset & (crb_size-1)))
3330 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003331
3332 return 0;
3333}
3334
3335static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003336qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3337 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003338 char *buf, loff_t offset, size_t size)
3339{
3340 struct device *dev = container_of(kobj, struct device, kobj);
3341 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3342 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003343 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003344 int ret;
3345
3346 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3347 if (ret != 0)
3348 return ret;
3349
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003350 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3351 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3352 memcpy(buf, &qmdata, size);
3353 } else {
3354 data = QLCRD32(adapter, offset);
3355 memcpy(buf, &data, size);
3356 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003357 return size;
3358}
3359
3360static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003361qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3362 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003363 char *buf, loff_t offset, size_t size)
3364{
3365 struct device *dev = container_of(kobj, struct device, kobj);
3366 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3367 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003368 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003369 int ret;
3370
3371 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3372 if (ret != 0)
3373 return ret;
3374
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003375 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3376 memcpy(&qmdata, buf, size);
3377 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3378 } else {
3379 memcpy(&data, buf, size);
3380 QLCWR32(adapter, offset, data);
3381 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003382 return size;
3383}
3384
3385static int
3386qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3387 loff_t offset, size_t size)
3388{
3389 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3390 return -EIO;
3391
3392 if ((size != 8) || (offset & 0x7))
3393 return -EIO;
3394
3395 return 0;
3396}
3397
3398static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003399qlcnic_sysfs_read_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 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3413 return -EIO;
3414
3415 memcpy(buf, &data, size);
3416
3417 return size;
3418}
3419
3420static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003421qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3422 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003423 char *buf, loff_t offset, size_t size)
3424{
3425 struct device *dev = container_of(kobj, struct device, kobj);
3426 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3427 u64 data;
3428 int ret;
3429
3430 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3431 if (ret != 0)
3432 return ret;
3433
3434 memcpy(&data, buf, size);
3435
3436 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3437 return -EIO;
3438
3439 return size;
3440}
3441
3442
3443static struct bin_attribute bin_attr_crb = {
3444 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3445 .size = 0,
3446 .read = qlcnic_sysfs_read_crb,
3447 .write = qlcnic_sysfs_write_crb,
3448};
3449
3450static struct bin_attribute bin_attr_mem = {
3451 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3452 .size = 0,
3453 .read = qlcnic_sysfs_read_mem,
3454 .write = qlcnic_sysfs_write_mem,
3455};
3456
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003457static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003458validate_pm_config(struct qlcnic_adapter *adapter,
3459 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3460{
3461
3462 u8 src_pci_func, s_esw_id, d_esw_id;
3463 u8 dest_pci_func;
3464 int i;
3465
3466 for (i = 0; i < count; i++) {
3467 src_pci_func = pm_cfg[i].pci_func;
3468 dest_pci_func = pm_cfg[i].dest_npar;
3469 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3470 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3471 return QL_STATUS_INVALID_PARAM;
3472
3473 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3474 return QL_STATUS_INVALID_PARAM;
3475
3476 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3477 return QL_STATUS_INVALID_PARAM;
3478
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003479 s_esw_id = adapter->npars[src_pci_func].phy_port;
3480 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3481
3482 if (s_esw_id != d_esw_id)
3483 return QL_STATUS_INVALID_PARAM;
3484
3485 }
3486 return 0;
3487
3488}
3489
3490static ssize_t
3491qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3492 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3493{
3494 struct device *dev = container_of(kobj, struct device, kobj);
3495 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3496 struct qlcnic_pm_func_cfg *pm_cfg;
3497 u32 id, action, pci_func;
3498 int count, rem, i, ret;
3499
3500 count = size / sizeof(struct qlcnic_pm_func_cfg);
3501 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3502 if (rem)
3503 return QL_STATUS_INVALID_PARAM;
3504
3505 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3506
3507 ret = validate_pm_config(adapter, pm_cfg, count);
3508 if (ret)
3509 return ret;
3510 for (i = 0; i < count; i++) {
3511 pci_func = pm_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003512 action = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003513 id = adapter->npars[pci_func].phy_port;
3514 ret = qlcnic_config_port_mirroring(adapter, id,
3515 action, pci_func);
3516 if (ret)
3517 return ret;
3518 }
3519
3520 for (i = 0; i < count; i++) {
3521 pci_func = pm_cfg[i].pci_func;
3522 id = adapter->npars[pci_func].phy_port;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003523 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003524 adapter->npars[pci_func].dest_npar = id;
3525 }
3526 return size;
3527}
3528
3529static ssize_t
3530qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3531 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3532{
3533 struct device *dev = container_of(kobj, struct device, kobj);
3534 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3535 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3536 int i;
3537
3538 if (size != sizeof(pm_cfg))
3539 return QL_STATUS_INVALID_PARAM;
3540
3541 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3542 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3543 continue;
3544 pm_cfg[i].action = adapter->npars[i].enable_pm;
3545 pm_cfg[i].dest_npar = 0;
3546 pm_cfg[i].pci_func = i;
3547 }
3548 memcpy(buf, &pm_cfg, size);
3549
3550 return size;
3551}
3552
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003553static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003554validate_esw_config(struct qlcnic_adapter *adapter,
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003555 struct qlcnic_esw_func_cfg *esw_cfg, int count)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003556{
Rajesh Borundia7613c872010-08-31 17:17:48 +00003557 u32 op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003558 u8 pci_func;
3559 int i;
Rajesh Borundia7613c872010-08-31 17:17:48 +00003560
3561 op_mode = readl(adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE);
3562
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003563 for (i = 0; i < count; i++) {
3564 pci_func = esw_cfg[i].pci_func;
3565 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3566 return QL_STATUS_INVALID_PARAM;
3567
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003568 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3569 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003570 return QL_STATUS_INVALID_PARAM;
3571
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003572 switch (esw_cfg[i].op_mode) {
3573 case QLCNIC_PORT_DEFAULTS:
Rajesh Borundia7613c872010-08-31 17:17:48 +00003574 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
Rajesh Borundia73733732010-08-31 17:17:50 +00003575 QLCNIC_NON_PRIV_FUNC) {
Rajesh Borundia7613c872010-08-31 17:17:48 +00003576 esw_cfg[i].mac_anti_spoof = 0;
Rajesh Borundia73733732010-08-31 17:17:50 +00003577 esw_cfg[i].mac_override = 1;
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +00003578 esw_cfg[i].promisc_mode = 1;
Rajesh Borundia73733732010-08-31 17:17:50 +00003579 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003580 break;
3581 case QLCNIC_ADD_VLAN:
3582 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3583 return QL_STATUS_INVALID_PARAM;
3584 if (!esw_cfg[i].op_type)
3585 return QL_STATUS_INVALID_PARAM;
3586 break;
3587 case QLCNIC_DEL_VLAN:
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003588 if (!esw_cfg[i].op_type)
3589 return QL_STATUS_INVALID_PARAM;
3590 break;
3591 default:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003592 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003593 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003594 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003595 return 0;
3596}
3597
3598static ssize_t
3599qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3600 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3601{
3602 struct device *dev = container_of(kobj, struct device, kobj);
3603 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3604 struct qlcnic_esw_func_cfg *esw_cfg;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003605 struct qlcnic_npar_info *npar;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003606 int count, rem, i, ret;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003607 u8 pci_func, op_mode = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003608
3609 count = size / sizeof(struct qlcnic_esw_func_cfg);
3610 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3611 if (rem)
3612 return QL_STATUS_INVALID_PARAM;
3613
3614 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3615 ret = validate_esw_config(adapter, esw_cfg, count);
3616 if (ret)
3617 return ret;
3618
3619 for (i = 0; i < count; i++) {
Rajesh Borundia0325d692010-08-19 05:08:26 +00003620 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3621 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3622 return QL_STATUS_INVALID_PARAM;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003623
3624 if (adapter->ahw.pci_func != esw_cfg[i].pci_func)
3625 continue;
3626
3627 op_mode = esw_cfg[i].op_mode;
3628 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3629 esw_cfg[i].op_mode = op_mode;
3630 esw_cfg[i].pci_func = adapter->ahw.pci_func;
3631
3632 switch (esw_cfg[i].op_mode) {
3633 case QLCNIC_PORT_DEFAULTS:
3634 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3635 break;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00003636 case QLCNIC_ADD_VLAN:
3637 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3638 break;
3639 case QLCNIC_DEL_VLAN:
3640 esw_cfg[i].vlan_id = 0;
3641 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3642 break;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003643 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003644 }
3645
Rajesh Borundia0325d692010-08-19 05:08:26 +00003646 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3647 goto out;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003648
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003649 for (i = 0; i < count; i++) {
3650 pci_func = esw_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003651 npar = &adapter->npars[pci_func];
3652 switch (esw_cfg[i].op_mode) {
3653 case QLCNIC_PORT_DEFAULTS:
3654 npar->promisc_mode = esw_cfg[i].promisc_mode;
Rajesh Borundia73733732010-08-31 17:17:50 +00003655 npar->mac_override = esw_cfg[i].mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003656 npar->offload_flags = esw_cfg[i].offload_flags;
3657 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3658 npar->discard_tagged = esw_cfg[i].discard_tagged;
3659 break;
3660 case QLCNIC_ADD_VLAN:
3661 npar->pvid = esw_cfg[i].vlan_id;
3662 break;
3663 case QLCNIC_DEL_VLAN:
3664 npar->pvid = 0;
3665 break;
3666 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003667 }
Rajesh Borundia0325d692010-08-19 05:08:26 +00003668out:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003669 return size;
3670}
3671
3672static ssize_t
3673qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3674 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3675{
3676 struct device *dev = container_of(kobj, struct device, kobj);
3677 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3678 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003679 u8 i;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003680
3681 if (size != sizeof(esw_cfg))
3682 return QL_STATUS_INVALID_PARAM;
3683
3684 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3685 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3686 continue;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003687 esw_cfg[i].pci_func = i;
3688 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
3689 return QL_STATUS_INVALID_PARAM;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003690 }
3691 memcpy(buf, &esw_cfg, size);
3692
3693 return size;
3694}
3695
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003696static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003697validate_npar_config(struct qlcnic_adapter *adapter,
3698 struct qlcnic_npar_func_cfg *np_cfg, int count)
3699{
3700 u8 pci_func, i;
3701
3702 for (i = 0; i < count; i++) {
3703 pci_func = np_cfg[i].pci_func;
3704 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3705 return QL_STATUS_INVALID_PARAM;
3706
3707 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3708 return QL_STATUS_INVALID_PARAM;
3709
3710 if (!IS_VALID_BW(np_cfg[i].min_bw)
3711 || !IS_VALID_BW(np_cfg[i].max_bw)
3712 || !IS_VALID_RX_QUEUES(np_cfg[i].max_rx_queues)
3713 || !IS_VALID_TX_QUEUES(np_cfg[i].max_tx_queues))
3714 return QL_STATUS_INVALID_PARAM;
3715 }
3716 return 0;
3717}
3718
3719static ssize_t
3720qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3721 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3722{
3723 struct device *dev = container_of(kobj, struct device, kobj);
3724 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3725 struct qlcnic_info nic_info;
3726 struct qlcnic_npar_func_cfg *np_cfg;
3727 int i, count, rem, ret;
3728 u8 pci_func;
3729
3730 count = size / sizeof(struct qlcnic_npar_func_cfg);
3731 rem = size % sizeof(struct qlcnic_npar_func_cfg);
3732 if (rem)
3733 return QL_STATUS_INVALID_PARAM;
3734
3735 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
3736 ret = validate_npar_config(adapter, np_cfg, count);
3737 if (ret)
3738 return ret;
3739
3740 for (i = 0; i < count ; i++) {
3741 pci_func = np_cfg[i].pci_func;
3742 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
3743 if (ret)
3744 return ret;
3745 nic_info.pci_func = pci_func;
3746 nic_info.min_tx_bw = np_cfg[i].min_bw;
3747 nic_info.max_tx_bw = np_cfg[i].max_bw;
3748 ret = qlcnic_set_nic_info(adapter, &nic_info);
3749 if (ret)
3750 return ret;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003751 adapter->npars[i].min_bw = nic_info.min_tx_bw;
3752 adapter->npars[i].max_bw = nic_info.max_tx_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003753 }
3754
3755 return size;
3756
3757}
3758static ssize_t
3759qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
3760 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3761{
3762 struct device *dev = container_of(kobj, struct device, kobj);
3763 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3764 struct qlcnic_info nic_info;
3765 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
3766 int i, ret;
3767
3768 if (size != sizeof(np_cfg))
3769 return QL_STATUS_INVALID_PARAM;
3770
3771 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3772 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3773 continue;
3774 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
3775 if (ret)
3776 return ret;
3777
3778 np_cfg[i].pci_func = i;
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +00003779 np_cfg[i].op_mode = (u8)nic_info.op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003780 np_cfg[i].port_num = nic_info.phys_port;
3781 np_cfg[i].fw_capab = nic_info.capabilities;
3782 np_cfg[i].min_bw = nic_info.min_tx_bw ;
3783 np_cfg[i].max_bw = nic_info.max_tx_bw;
3784 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
3785 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
3786 }
3787 memcpy(buf, &np_cfg, size);
3788 return size;
3789}
3790
3791static ssize_t
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003792qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
3793 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3794{
3795 struct device *dev = container_of(kobj, struct device, kobj);
3796 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3797 struct qlcnic_esw_statistics port_stats;
3798 int ret;
3799
3800 if (size != sizeof(struct qlcnic_esw_statistics))
3801 return QL_STATUS_INVALID_PARAM;
3802
3803 if (offset >= QLCNIC_MAX_PCI_FUNC)
3804 return QL_STATUS_INVALID_PARAM;
3805
3806 memset(&port_stats, 0, size);
3807 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3808 &port_stats.rx);
3809 if (ret)
3810 return ret;
3811
3812 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3813 &port_stats.tx);
3814 if (ret)
3815 return ret;
3816
3817 memcpy(buf, &port_stats, size);
3818 return size;
3819}
3820
3821static ssize_t
3822qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
3823 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3824{
3825 struct device *dev = container_of(kobj, struct device, kobj);
3826 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3827 struct qlcnic_esw_statistics esw_stats;
3828 int ret;
3829
3830 if (size != sizeof(struct qlcnic_esw_statistics))
3831 return QL_STATUS_INVALID_PARAM;
3832
3833 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3834 return QL_STATUS_INVALID_PARAM;
3835
3836 memset(&esw_stats, 0, size);
3837 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3838 &esw_stats.rx);
3839 if (ret)
3840 return ret;
3841
3842 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3843 &esw_stats.tx);
3844 if (ret)
3845 return ret;
3846
3847 memcpy(buf, &esw_stats, size);
3848 return size;
3849}
3850
3851static ssize_t
3852qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
3853 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3854{
3855 struct device *dev = container_of(kobj, struct device, kobj);
3856 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3857 int ret;
3858
3859 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3860 return QL_STATUS_INVALID_PARAM;
3861
3862 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3863 QLCNIC_QUERY_RX_COUNTER);
3864 if (ret)
3865 return ret;
3866
3867 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3868 QLCNIC_QUERY_TX_COUNTER);
3869 if (ret)
3870 return ret;
3871
3872 return size;
3873}
3874
3875static ssize_t
3876qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
3877 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3878{
3879
3880 struct device *dev = container_of(kobj, struct device, kobj);
3881 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3882 int ret;
3883
3884 if (offset >= QLCNIC_MAX_PCI_FUNC)
3885 return QL_STATUS_INVALID_PARAM;
3886
3887 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
3888 QLCNIC_QUERY_RX_COUNTER);
3889 if (ret)
3890 return ret;
3891
3892 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
3893 QLCNIC_QUERY_TX_COUNTER);
3894 if (ret)
3895 return ret;
3896
3897 return size;
3898}
3899
3900static ssize_t
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003901qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
3902 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3903{
3904 struct device *dev = container_of(kobj, struct device, kobj);
3905 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3906 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
Dan Carpentere88db3b2010-08-09 21:49:36 +00003907 struct qlcnic_pci_info *pci_info;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003908 int i, ret;
3909
3910 if (size != sizeof(pci_cfg))
3911 return QL_STATUS_INVALID_PARAM;
3912
Dan Carpentere88db3b2010-08-09 21:49:36 +00003913 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
3914 if (!pci_info)
3915 return -ENOMEM;
3916
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003917 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpentere88db3b2010-08-09 21:49:36 +00003918 if (ret) {
3919 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003920 return ret;
Dan Carpentere88db3b2010-08-09 21:49:36 +00003921 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003922
3923 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3924 pci_cfg[i].pci_func = pci_info[i].id;
3925 pci_cfg[i].func_type = pci_info[i].type;
3926 pci_cfg[i].port_num = pci_info[i].default_port;
3927 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
3928 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
3929 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
3930 }
3931 memcpy(buf, &pci_cfg, size);
Dan Carpentere88db3b2010-08-09 21:49:36 +00003932 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003933 return size;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003934}
3935static struct bin_attribute bin_attr_npar_config = {
3936 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
3937 .size = 0,
3938 .read = qlcnic_sysfs_read_npar_config,
3939 .write = qlcnic_sysfs_write_npar_config,
3940};
3941
3942static struct bin_attribute bin_attr_pci_config = {
3943 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
3944 .size = 0,
3945 .read = qlcnic_sysfs_read_pci_config,
3946 .write = NULL,
3947};
3948
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003949static struct bin_attribute bin_attr_port_stats = {
3950 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
3951 .size = 0,
3952 .read = qlcnic_sysfs_get_port_stats,
3953 .write = qlcnic_sysfs_clear_port_stats,
3954};
3955
3956static struct bin_attribute bin_attr_esw_stats = {
3957 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
3958 .size = 0,
3959 .read = qlcnic_sysfs_get_esw_stats,
3960 .write = qlcnic_sysfs_clear_esw_stats,
3961};
3962
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003963static struct bin_attribute bin_attr_esw_config = {
3964 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
3965 .size = 0,
3966 .read = qlcnic_sysfs_read_esw_config,
3967 .write = qlcnic_sysfs_write_esw_config,
3968};
3969
3970static struct bin_attribute bin_attr_pm_config = {
3971 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
3972 .size = 0,
3973 .read = qlcnic_sysfs_read_pm_config,
3974 .write = qlcnic_sysfs_write_pm_config,
3975};
3976
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003977static void
3978qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
3979{
3980 struct device *dev = &adapter->pdev->dev;
3981
3982 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3983 if (device_create_file(dev, &dev_attr_bridged_mode))
3984 dev_warn(dev,
3985 "failed to create bridged_mode sysfs entry\n");
3986}
3987
3988static void
3989qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
3990{
3991 struct device *dev = &adapter->pdev->dev;
3992
3993 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3994 device_remove_file(dev, &dev_attr_bridged_mode);
3995}
3996
3997static void
3998qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
3999{
4000 struct device *dev = &adapter->pdev->dev;
4001
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004002 if (device_create_bin_file(dev, &bin_attr_port_stats))
4003 dev_info(dev, "failed to create port stats sysfs entry");
4004
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004005 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4006 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004007 if (device_create_file(dev, &dev_attr_diag_mode))
4008 dev_info(dev, "failed to create diag_mode sysfs entry\n");
4009 if (device_create_bin_file(dev, &bin_attr_crb))
4010 dev_info(dev, "failed to create crb sysfs entry\n");
4011 if (device_create_bin_file(dev, &bin_attr_mem))
4012 dev_info(dev, "failed to create mem sysfs entry\n");
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004013 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4014 return;
4015 if (device_create_bin_file(dev, &bin_attr_esw_config))
4016 dev_info(dev, "failed to create esw config sysfs entry");
4017 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004018 return;
4019 if (device_create_bin_file(dev, &bin_attr_pci_config))
4020 dev_info(dev, "failed to create pci config sysfs entry");
4021 if (device_create_bin_file(dev, &bin_attr_npar_config))
4022 dev_info(dev, "failed to create npar config sysfs entry");
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004023 if (device_create_bin_file(dev, &bin_attr_pm_config))
4024 dev_info(dev, "failed to create pm config sysfs entry");
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004025 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4026 dev_info(dev, "failed to create eswitch stats sysfs entry");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004027}
4028
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004029static void
4030qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4031{
4032 struct device *dev = &adapter->pdev->dev;
4033
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004034 device_remove_bin_file(dev, &bin_attr_port_stats);
4035
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004036 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4037 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004038 device_remove_file(dev, &dev_attr_diag_mode);
4039 device_remove_bin_file(dev, &bin_attr_crb);
4040 device_remove_bin_file(dev, &bin_attr_mem);
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004041 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4042 return;
4043 device_remove_bin_file(dev, &bin_attr_esw_config);
4044 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004045 return;
4046 device_remove_bin_file(dev, &bin_attr_pci_config);
4047 device_remove_bin_file(dev, &bin_attr_npar_config);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004048 device_remove_bin_file(dev, &bin_attr_pm_config);
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004049 device_remove_bin_file(dev, &bin_attr_esw_stats);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004050}
4051
4052#ifdef CONFIG_INET
4053
4054#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4055
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004056static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004057qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4058 struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004059{
4060 struct in_device *indev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004061
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004062 indev = in_dev_get(dev);
4063 if (!indev)
4064 return;
4065
4066 for_ifa(indev) {
4067 switch (event) {
4068 case NETDEV_UP:
4069 qlcnic_config_ipaddr(adapter,
4070 ifa->ifa_address, QLCNIC_IP_UP);
4071 break;
4072 case NETDEV_DOWN:
4073 qlcnic_config_ipaddr(adapter,
4074 ifa->ifa_address, QLCNIC_IP_DOWN);
4075 break;
4076 default:
4077 break;
4078 }
4079 } endfor_ifa(indev);
4080
4081 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004082}
4083
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004084static void
4085qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4086{
4087 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4088 struct net_device *dev;
4089 u16 vid;
4090
4091 qlcnic_config_indev_addr(adapter, netdev, event);
4092
4093 if (!adapter->vlgrp)
4094 return;
4095
Jesse Grossb7381272010-10-20 13:56:02 +00004096 for (vid = 0; vid < VLAN_N_VID; vid++) {
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004097 dev = vlan_group_get_device(adapter->vlgrp, vid);
4098 if (!dev)
4099 continue;
4100
4101 qlcnic_config_indev_addr(adapter, dev, event);
4102 }
4103}
4104
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004105static int qlcnic_netdev_event(struct notifier_block *this,
4106 unsigned long event, void *ptr)
4107{
4108 struct qlcnic_adapter *adapter;
4109 struct net_device *dev = (struct net_device *)ptr;
4110
4111recheck:
4112 if (dev == NULL)
4113 goto done;
4114
4115 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4116 dev = vlan_dev_real_dev(dev);
4117 goto recheck;
4118 }
4119
4120 if (!is_qlcnic_netdev(dev))
4121 goto done;
4122
4123 adapter = netdev_priv(dev);
4124
4125 if (!adapter)
4126 goto done;
4127
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004128 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004129 goto done;
4130
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004131 qlcnic_config_indev_addr(adapter, dev, event);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004132done:
4133 return NOTIFY_DONE;
4134}
4135
4136static int
4137qlcnic_inetaddr_event(struct notifier_block *this,
4138 unsigned long event, void *ptr)
4139{
4140 struct qlcnic_adapter *adapter;
4141 struct net_device *dev;
4142
4143 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4144
4145 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4146
4147recheck:
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004148 if (dev == NULL)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004149 goto done;
4150
4151 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4152 dev = vlan_dev_real_dev(dev);
4153 goto recheck;
4154 }
4155
4156 if (!is_qlcnic_netdev(dev))
4157 goto done;
4158
4159 adapter = netdev_priv(dev);
4160
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00004161 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004162 goto done;
4163
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004164 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004165 goto done;
4166
4167 switch (event) {
4168 case NETDEV_UP:
4169 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4170 break;
4171 case NETDEV_DOWN:
4172 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4173 break;
4174 default:
4175 break;
4176 }
4177
4178done:
4179 return NOTIFY_DONE;
4180}
4181
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004182static struct notifier_block qlcnic_netdev_cb = {
4183 .notifier_call = qlcnic_netdev_event,
4184};
4185
4186static struct notifier_block qlcnic_inetaddr_cb = {
4187 .notifier_call = qlcnic_inetaddr_event,
4188};
4189#else
4190static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004191qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004192{ }
4193#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004194static struct pci_error_handlers qlcnic_err_handler = {
4195 .error_detected = qlcnic_io_error_detected,
4196 .slot_reset = qlcnic_io_slot_reset,
4197 .resume = qlcnic_io_resume,
4198};
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004199
4200static struct pci_driver qlcnic_driver = {
4201 .name = qlcnic_driver_name,
4202 .id_table = qlcnic_pci_tbl,
4203 .probe = qlcnic_probe,
4204 .remove = __devexit_p(qlcnic_remove),
4205#ifdef CONFIG_PM
4206 .suspend = qlcnic_suspend,
4207 .resume = qlcnic_resume,
4208#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004209 .shutdown = qlcnic_shutdown,
4210 .err_handler = &qlcnic_err_handler
4211
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004212};
4213
4214static int __init qlcnic_init_module(void)
4215{
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004216 int ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004217
4218 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4219
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004220 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4221 if (qlcnic_wq == NULL) {
4222 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4223 return -ENOMEM;
4224 }
4225
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004226#ifdef CONFIG_INET
4227 register_netdevice_notifier(&qlcnic_netdev_cb);
4228 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4229#endif
4230
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004231 ret = pci_register_driver(&qlcnic_driver);
4232 if (ret) {
4233#ifdef CONFIG_INET
4234 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4235 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4236#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004237 destroy_workqueue(qlcnic_wq);
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004238 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004239
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004240 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004241}
4242
4243module_init(qlcnic_init_module);
4244
4245static void __exit qlcnic_exit_module(void)
4246{
4247
4248 pci_unregister_driver(&qlcnic_driver);
4249
4250#ifdef CONFIG_INET
4251 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4252 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4253#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004254 destroy_workqueue(qlcnic_wq);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004255}
4256
4257module_exit(qlcnic_exit_module);