]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/qlcnic/qlcnic_main.c
qlcnic: fix for setting function modes
[net-next-2.6.git] / drivers / net / qlcnic / qlcnic_main.c
CommitLineData
af19b491
AKS
1/*
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
5a0e3ad6 25#include <linux/slab.h>
af19b491
AKS
26#include <linux/vmalloc.h>
27#include <linux/interrupt.h>
28
29#include "qlcnic.h"
30
31#include <linux/dma-mapping.h>
32#include <linux/if_vlan.h>
33#include <net/ip.h>
34#include <linux/ipv6.h>
35#include <linux/inetdevice.h>
36#include <linux/sysfs.h>
451724c8 37#include <linux/aer.h>
af19b491 38
7f9a0c34 39MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
af19b491
AKS
40MODULE_LICENSE("GPL");
41MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
42MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
43
44char qlcnic_driver_name[] = "qlcnic";
7f9a0c34
SV
45static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
46 "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
af19b491
AKS
47
48static int port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
49
50/* Default to restricted 1G auto-neg mode */
51static int wol_port_mode = 5;
52
53static int use_msi = 1;
54module_param(use_msi, int, 0644);
55MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
56
57static int use_msi_x = 1;
58module_param(use_msi_x, int, 0644);
59MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
60
61static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
62module_param(auto_fw_reset, int, 0644);
63MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
64
4d5bdb38
AKS
65static int load_fw_file;
66module_param(load_fw_file, int, 0644);
67MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
68
2e9d722d
AC
69static int qlcnic_config_npars;
70module_param(qlcnic_config_npars, int, 0644);
71MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
72
af19b491
AKS
73static int __devinit qlcnic_probe(struct pci_dev *pdev,
74 const struct pci_device_id *ent);
75static void __devexit qlcnic_remove(struct pci_dev *pdev);
76static int qlcnic_open(struct net_device *netdev);
77static int qlcnic_close(struct net_device *netdev);
af19b491 78static void qlcnic_tx_timeout(struct net_device *netdev);
af19b491
AKS
79static void qlcnic_attach_work(struct work_struct *work);
80static void qlcnic_fwinit_work(struct work_struct *work);
81static void qlcnic_fw_poll_work(struct work_struct *work);
82static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
83 work_func_t func, int delay);
84static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
85static int qlcnic_poll(struct napi_struct *napi, int budget);
8f891387 86static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
af19b491
AKS
87#ifdef CONFIG_NET_POLL_CONTROLLER
88static void qlcnic_poll_controller(struct net_device *netdev);
89#endif
90
91static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
92static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
93static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
94static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
95
6df900e9 96static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
af19b491
AKS
97static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter);
98static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
99
7eb9855d 100static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
af19b491
AKS
101static irqreturn_t qlcnic_intr(int irq, void *data);
102static irqreturn_t qlcnic_msi_intr(int irq, void *data);
103static irqreturn_t qlcnic_msix_intr(int irq, void *data);
104
105static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
106static void qlcnic_config_indev_addr(struct net_device *dev, unsigned long);
9f26f547
AC
107static int qlcnic_start_firmware(struct qlcnic_adapter *);
108
109static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
9f26f547
AC
110static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
111static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
112static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
af19b491
AKS
113/* PCI Device ID Table */
114#define ENTRY(device) \
115 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
116 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
117
118#define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
119
6a902881 120static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
af19b491
AKS
121 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
122 {0,}
123};
124
125MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
126
127
128void
129qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
130 struct qlcnic_host_tx_ring *tx_ring)
131{
132 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
af19b491
AKS
133}
134
135static const u32 msi_tgt_status[8] = {
136 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
137 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
138 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
139 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
140};
141
142static const
143struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
144
145static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
146{
147 writel(0, sds_ring->crb_intr_mask);
148}
149
150static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
151{
152 struct qlcnic_adapter *adapter = sds_ring->adapter;
153
154 writel(0x1, sds_ring->crb_intr_mask);
155
156 if (!QLCNIC_IS_MSI_FAMILY(adapter))
157 writel(0xfbff, adapter->tgt_mask_reg);
158}
159
160static int
161qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
162{
163 int size = sizeof(struct qlcnic_host_sds_ring) * count;
164
165 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
166
167 return (recv_ctx->sds_rings == NULL);
168}
169
170static void
171qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
172{
173 if (recv_ctx->sds_rings != NULL)
174 kfree(recv_ctx->sds_rings);
175
176 recv_ctx->sds_rings = NULL;
177}
178
179static int
180qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
181{
182 int ring;
183 struct qlcnic_host_sds_ring *sds_ring;
184 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
185
186 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
187 return -ENOMEM;
188
189 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
190 sds_ring = &recv_ctx->sds_rings[ring];
8f891387 191
192 if (ring == adapter->max_sds_rings - 1)
193 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
194 QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
195 else
196 netif_napi_add(netdev, &sds_ring->napi,
197 qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
af19b491
AKS
198 }
199
200 return 0;
201}
202
203static void
204qlcnic_napi_del(struct qlcnic_adapter *adapter)
205{
206 int ring;
207 struct qlcnic_host_sds_ring *sds_ring;
208 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
209
210 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
211 sds_ring = &recv_ctx->sds_rings[ring];
212 netif_napi_del(&sds_ring->napi);
213 }
214
215 qlcnic_free_sds_rings(&adapter->recv_ctx);
216}
217
218static void
219qlcnic_napi_enable(struct qlcnic_adapter *adapter)
220{
221 int ring;
222 struct qlcnic_host_sds_ring *sds_ring;
223 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
224
780ab790
AKS
225 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
226 return;
227
af19b491
AKS
228 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
229 sds_ring = &recv_ctx->sds_rings[ring];
230 napi_enable(&sds_ring->napi);
231 qlcnic_enable_int(sds_ring);
232 }
233}
234
235static void
236qlcnic_napi_disable(struct qlcnic_adapter *adapter)
237{
238 int ring;
239 struct qlcnic_host_sds_ring *sds_ring;
240 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
241
780ab790
AKS
242 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
243 return;
244
af19b491
AKS
245 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
246 sds_ring = &recv_ctx->sds_rings[ring];
247 qlcnic_disable_int(sds_ring);
248 napi_synchronize(&sds_ring->napi);
249 napi_disable(&sds_ring->napi);
250 }
251}
252
253static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
254{
255 memset(&adapter->stats, 0, sizeof(adapter->stats));
af19b491
AKS
256}
257
af19b491
AKS
258static void qlcnic_set_port_mode(struct qlcnic_adapter *adapter)
259{
260 u32 val, data;
261
262 val = adapter->ahw.board_type;
263 if ((val == QLCNIC_BRDTYPE_P3_HMEZ) ||
264 (val == QLCNIC_BRDTYPE_P3_XG_LOM)) {
265 if (port_mode == QLCNIC_PORT_MODE_802_3_AP) {
266 data = QLCNIC_PORT_MODE_802_3_AP;
267 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
268 } else if (port_mode == QLCNIC_PORT_MODE_XG) {
269 data = QLCNIC_PORT_MODE_XG;
270 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
271 } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_1G) {
272 data = QLCNIC_PORT_MODE_AUTO_NEG_1G;
273 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
274 } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_XG) {
275 data = QLCNIC_PORT_MODE_AUTO_NEG_XG;
276 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
277 } else {
278 data = QLCNIC_PORT_MODE_AUTO_NEG;
279 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
280 }
281
282 if ((wol_port_mode != QLCNIC_PORT_MODE_802_3_AP) &&
283 (wol_port_mode != QLCNIC_PORT_MODE_XG) &&
284 (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_1G) &&
285 (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_XG)) {
286 wol_port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
287 }
288 QLCWR32(adapter, QLCNIC_WOL_PORT_MODE, wol_port_mode);
289 }
290}
291
292static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
293{
294 u32 control;
295 int pos;
296
297 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
298 if (pos) {
299 pci_read_config_dword(pdev, pos, &control);
300 if (enable)
301 control |= PCI_MSIX_FLAGS_ENABLE;
302 else
303 control = 0;
304 pci_write_config_dword(pdev, pos, control);
305 }
306}
307
308static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
309{
310 int i;
311
312 for (i = 0; i < count; i++)
313 adapter->msix_entries[i].entry = i;
314}
315
316static int
317qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
318{
2e9d722d 319 u8 mac_addr[ETH_ALEN];
af19b491
AKS
320 struct net_device *netdev = adapter->netdev;
321 struct pci_dev *pdev = adapter->pdev;
322
2e9d722d 323 if (adapter->nic_ops->get_mac_addr(adapter, mac_addr) != 0)
af19b491
AKS
324 return -EIO;
325
2e9d722d 326 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
af19b491
AKS
327 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
328 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
329
330 /* set station address */
331
332 if (!is_valid_ether_addr(netdev->perm_addr))
333 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
334 netdev->dev_addr);
335
336 return 0;
337}
338
339static int qlcnic_set_mac(struct net_device *netdev, void *p)
340{
341 struct qlcnic_adapter *adapter = netdev_priv(netdev);
342 struct sockaddr *addr = p;
343
344 if (!is_valid_ether_addr(addr->sa_data))
345 return -EINVAL;
346
8a15ad1f 347 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
af19b491
AKS
348 netif_device_detach(netdev);
349 qlcnic_napi_disable(adapter);
350 }
351
352 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
353 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
354 qlcnic_set_multi(adapter->netdev);
355
8a15ad1f 356 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
af19b491
AKS
357 netif_device_attach(netdev);
358 qlcnic_napi_enable(adapter);
359 }
360 return 0;
361}
362
363static const struct net_device_ops qlcnic_netdev_ops = {
364 .ndo_open = qlcnic_open,
365 .ndo_stop = qlcnic_close,
366 .ndo_start_xmit = qlcnic_xmit_frame,
367 .ndo_get_stats = qlcnic_get_stats,
368 .ndo_validate_addr = eth_validate_addr,
369 .ndo_set_multicast_list = qlcnic_set_multi,
370 .ndo_set_mac_address = qlcnic_set_mac,
371 .ndo_change_mtu = qlcnic_change_mtu,
372 .ndo_tx_timeout = qlcnic_tx_timeout,
373#ifdef CONFIG_NET_POLL_CONTROLLER
374 .ndo_poll_controller = qlcnic_poll_controller,
375#endif
376};
377
2e9d722d 378static struct qlcnic_nic_template qlcnic_ops = {
2e9d722d
AC
379 .get_mac_addr = qlcnic_get_mac_address,
380 .config_bridged_mode = qlcnic_config_bridged_mode,
381 .config_led = qlcnic_config_led,
9f26f547
AC
382 .start_firmware = qlcnic_start_firmware
383};
384
385static struct qlcnic_nic_template qlcnic_vf_ops = {
386 .get_mac_addr = qlcnic_get_mac_address,
387 .config_bridged_mode = qlcnicvf_config_bridged_mode,
388 .config_led = qlcnicvf_config_led,
9f26f547 389 .start_firmware = qlcnicvf_start_firmware
2e9d722d
AC
390};
391
af19b491
AKS
392static void
393qlcnic_setup_intr(struct qlcnic_adapter *adapter)
394{
395 const struct qlcnic_legacy_intr_set *legacy_intrp;
396 struct pci_dev *pdev = adapter->pdev;
397 int err, num_msix;
398
399 if (adapter->rss_supported) {
400 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
401 MSIX_ENTRIES_PER_ADAPTER : 2;
402 } else
403 num_msix = 1;
404
405 adapter->max_sds_rings = 1;
406
407 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
408
409 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
410
411 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
412 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
413 legacy_intrp->tgt_status_reg);
414 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
415 legacy_intrp->tgt_mask_reg);
416 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
417
418 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
419 ISR_INT_STATE_REG);
420
421 qlcnic_set_msix_bit(pdev, 0);
422
423 if (adapter->msix_supported) {
424
425 qlcnic_init_msix_entries(adapter, num_msix);
426 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
427 if (err == 0) {
428 adapter->flags |= QLCNIC_MSIX_ENABLED;
429 qlcnic_set_msix_bit(pdev, 1);
430
431 if (adapter->rss_supported)
432 adapter->max_sds_rings = num_msix;
433
434 dev_info(&pdev->dev, "using msi-x interrupts\n");
435 return;
436 }
437
438 if (err > 0)
439 pci_disable_msix(pdev);
440
441 /* fall through for msi */
442 }
443
444 if (use_msi && !pci_enable_msi(pdev)) {
445 adapter->flags |= QLCNIC_MSI_ENABLED;
446 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
447 msi_tgt_status[adapter->ahw.pci_func]);
448 dev_info(&pdev->dev, "using msi interrupts\n");
449 adapter->msix_entries[0].vector = pdev->irq;
450 return;
451 }
452
453 dev_info(&pdev->dev, "using legacy interrupts\n");
454 adapter->msix_entries[0].vector = pdev->irq;
455}
456
457static void
458qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
459{
460 if (adapter->flags & QLCNIC_MSIX_ENABLED)
461 pci_disable_msix(adapter->pdev);
462 if (adapter->flags & QLCNIC_MSI_ENABLED)
463 pci_disable_msi(adapter->pdev);
464}
465
466static void
467qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
468{
469 if (adapter->ahw.pci_base0 != NULL)
470 iounmap(adapter->ahw.pci_base0);
471}
472
346fe763
RB
473static int
474qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
475{
e88db3bd 476 struct qlcnic_pci_info *pci_info;
346fe763
RB
477 int i, ret = 0, err;
478 u8 pfn;
479
e88db3bd
DC
480 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
481 if (!pci_info)
482 return -ENOMEM;
483
ca315ac2 484 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
346fe763 485 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
e88db3bd
DC
486 if (!adapter->npars) {
487 err = -ENOMEM;
488 goto err_pci_info;
489 }
346fe763 490
ca315ac2 491 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
346fe763
RB
492 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
493 if (!adapter->eswitch) {
494 err = -ENOMEM;
ca315ac2 495 goto err_npars;
346fe763
RB
496 }
497
498 ret = qlcnic_get_pci_info(adapter, pci_info);
ca315ac2
DC
499 if (ret)
500 goto err_eswitch;
346fe763 501
ca315ac2
DC
502 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
503 pfn = pci_info[i].id;
504 if (pfn > QLCNIC_MAX_PCI_FUNC)
505 return QL_STATUS_INVALID_PARAM;
506 adapter->npars[pfn].active = pci_info[i].active;
507 adapter->npars[pfn].type = pci_info[i].type;
508 adapter->npars[pfn].phy_port = pci_info[i].default_port;
509 adapter->npars[pfn].mac_learning = DEFAULT_MAC_LEARN;
510 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
511 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
346fe763
RB
512 }
513
ca315ac2
DC
514 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
515 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
516
e88db3bd 517 kfree(pci_info);
ca315ac2
DC
518 return 0;
519
520err_eswitch:
346fe763
RB
521 kfree(adapter->eswitch);
522 adapter->eswitch = NULL;
ca315ac2 523err_npars:
346fe763 524 kfree(adapter->npars);
ca315ac2 525 adapter->npars = NULL;
e88db3bd
DC
526err_pci_info:
527 kfree(pci_info);
346fe763
RB
528
529 return ret;
530}
531
2e9d722d
AC
532static int
533qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
534{
535 u8 id;
536 u32 ref_count;
537 int i, ret = 1;
538 u32 data = QLCNIC_MGMT_FUNC;
539 void __iomem *priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
540
541 /* If other drivers are not in use set their privilege level */
542 ref_count = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
543 ret = qlcnic_api_lock(adapter);
544 if (ret)
545 goto err_lock;
2e9d722d 546
0e33c664
AC
547 if (qlcnic_config_npars) {
548 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
346fe763 549 id = i;
0e33c664
AC
550 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
551 id == adapter->ahw.pci_func)
552 continue;
553 data |= (qlcnic_config_npars &
554 QLC_DEV_SET_DRV(0xf, id));
555 }
556 } else {
557 data = readl(priv_op);
558 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw.pci_func)) |
559 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
560 adapter->ahw.pci_func));
2e9d722d
AC
561 }
562 writel(data, priv_op);
2e9d722d
AC
563 qlcnic_api_unlock(adapter);
564err_lock:
565 return ret;
566}
567
2e9d722d
AC
568static u32
569qlcnic_get_driver_mode(struct qlcnic_adapter *adapter)
570{
571 void __iomem *msix_base_addr;
572 void __iomem *priv_op;
346fe763 573 struct qlcnic_info nic_info;
2e9d722d
AC
574 u32 func;
575 u32 msix_base;
576 u32 op_mode, priv_level;
577
578 /* Determine FW API version */
579 adapter->fw_hal_version = readl(adapter->ahw.pci_base0 + QLCNIC_FW_API);
2e9d722d
AC
580
581 /* Find PCI function number */
582 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
583 msix_base_addr = adapter->ahw.pci_base0 + QLCNIC_MSIX_BASE;
584 msix_base = readl(msix_base_addr);
585 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
586 adapter->ahw.pci_func = func;
587
346fe763
RB
588 if (!qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw.pci_func)) {
589 adapter->capabilities = nic_info.capabilities;
590
591 if (adapter->capabilities & BIT_6)
592 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
593 else
594 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
595 }
0e33c664
AC
596
597 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
598 adapter->nic_ops = &qlcnic_ops;
599 return adapter->fw_hal_version;
600 }
601
2e9d722d
AC
602 /* Determine function privilege level */
603 priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
604 op_mode = readl(priv_op);
0e33c664 605 if (op_mode == QLC_DEV_DRV_DEFAULT)
2e9d722d 606 priv_level = QLCNIC_MGMT_FUNC;
0e33c664 607 else
2e9d722d
AC
608 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw.pci_func);
609
610 switch (priv_level) {
611 case QLCNIC_MGMT_FUNC:
612 adapter->op_mode = QLCNIC_MGMT_FUNC;
45918e2f 613 adapter->nic_ops = &qlcnic_ops;
346fe763 614 qlcnic_init_pci_info(adapter);
2e9d722d 615 /* Set privilege level for other functions */
0e33c664 616 qlcnic_set_function_modes(adapter);
2e9d722d
AC
617 dev_info(&adapter->pdev->dev,
618 "HAL Version: %d, Management function\n",
619 adapter->fw_hal_version);
620 break;
621 case QLCNIC_PRIV_FUNC:
622 adapter->op_mode = QLCNIC_PRIV_FUNC;
623 dev_info(&adapter->pdev->dev,
624 "HAL Version: %d, Privileged function\n",
625 adapter->fw_hal_version);
45918e2f 626 adapter->nic_ops = &qlcnic_ops;
2e9d722d 627 break;
9f26f547
AC
628 case QLCNIC_NON_PRIV_FUNC:
629 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
630 dev_info(&adapter->pdev->dev,
631 "HAL Version: %d Non Privileged function\n",
632 adapter->fw_hal_version);
633 adapter->nic_ops = &qlcnic_vf_ops;
634 break;
2e9d722d
AC
635 default:
636 dev_info(&adapter->pdev->dev, "Unknown function mode: %d\n",
637 priv_level);
638 return 0;
639 }
640 return adapter->fw_hal_version;
641}
642
af19b491
AKS
643static int
644qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
645{
646 void __iomem *mem_ptr0 = NULL;
647 resource_size_t mem_base;
648 unsigned long mem_len, pci_len0 = 0;
649
650 struct pci_dev *pdev = adapter->pdev;
af19b491 651
af19b491
AKS
652 /* remap phys address */
653 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
654 mem_len = pci_resource_len(pdev, 0);
655
656 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
657
658 mem_ptr0 = pci_ioremap_bar(pdev, 0);
659 if (mem_ptr0 == NULL) {
660 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
661 return -EIO;
662 }
663 pci_len0 = mem_len;
664 } else {
665 return -EIO;
666 }
667
668 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
669
670 adapter->ahw.pci_base0 = mem_ptr0;
671 adapter->ahw.pci_len0 = pci_len0;
672
2e9d722d
AC
673 if (!qlcnic_get_driver_mode(adapter)) {
674 iounmap(adapter->ahw.pci_base0);
675 return -EIO;
676 }
677
af19b491 678 adapter->ahw.ocm_win_crb = qlcnic_get_ioaddr(adapter,
2e9d722d 679 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(adapter->ahw.pci_func)));
af19b491
AKS
680
681 return 0;
682}
683
684static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
685{
686 struct pci_dev *pdev = adapter->pdev;
687 int i, found = 0;
688
689 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
690 if (qlcnic_boards[i].vendor == pdev->vendor &&
691 qlcnic_boards[i].device == pdev->device &&
692 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
693 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
02f6e46f
SC
694 sprintf(name, "%pM: %s" ,
695 adapter->mac_addr,
696 qlcnic_boards[i].short_name);
af19b491
AKS
697 found = 1;
698 break;
699 }
700
701 }
702
703 if (!found)
7f9a0c34 704 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
af19b491
AKS
705}
706
707static void
708qlcnic_check_options(struct qlcnic_adapter *adapter)
709{
710 u32 fw_major, fw_minor, fw_build;
711 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
712 char serial_num[32];
713 int i, offset, val;
714 int *ptr32;
715 struct pci_dev *pdev = adapter->pdev;
346fe763 716 struct qlcnic_info nic_info;
af19b491
AKS
717 adapter->driver_mismatch = 0;
718
719 ptr32 = (int *)&serial_num;
720 offset = QLCNIC_FW_SERIAL_NUM_OFFSET;
721 for (i = 0; i < 8; i++) {
722 if (qlcnic_rom_fast_read(adapter, offset, &val) == -1) {
723 dev_err(&pdev->dev, "error reading board info\n");
724 adapter->driver_mismatch = 1;
725 return;
726 }
727 ptr32[i] = cpu_to_le32(val);
728 offset += sizeof(u32);
729 }
730
731 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
732 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
733 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
734
735 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
736
737 if (adapter->portnum == 0) {
738 get_brd_name(adapter, brd_name);
739
740 pr_info("%s: %s Board Chip rev 0x%x\n",
741 module_name(THIS_MODULE),
742 brd_name, adapter->ahw.revision_id);
743 }
744
251a84c9
AKS
745 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
746 fw_major, fw_minor, fw_build);
af19b491 747
af19b491
AKS
748 adapter->flags &= ~QLCNIC_LRO_ENABLED;
749
750 if (adapter->ahw.port_type == QLCNIC_XGBE) {
751 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
752 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
753 } else if (adapter->ahw.port_type == QLCNIC_GBE) {
754 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
755 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
756 }
757
346fe763
RB
758 if (!qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw.pci_func)) {
759 adapter->physical_port = nic_info.phys_port;
760 adapter->switch_mode = nic_info.switch_mode;
761 adapter->max_tx_ques = nic_info.max_tx_ques;
762 adapter->max_rx_ques = nic_info.max_rx_ques;
763 adapter->capabilities = nic_info.capabilities;
764 adapter->max_mac_filters = nic_info.max_mac_filters;
765 adapter->max_mtu = nic_info.max_mtu;
766 }
0e33c664 767
af19b491
AKS
768 adapter->msix_supported = !!use_msi_x;
769 adapter->rss_supported = !!use_msi_x;
770
771 adapter->num_txd = MAX_CMD_DESCRIPTORS;
772
af19b491
AKS
773 adapter->max_rds_rings = 2;
774}
775
cea8975e
AC
776static int
777qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
778{
779 int i, err = 0;
780 struct qlcnic_npar_info *npar;
781 struct qlcnic_info nic_info;
782
783 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
784 !adapter->need_fw_reset)
785 return 0;
786
787 if (adapter->op_mode == QLCNIC_MGMT_FUNC) {
788 /* Set the NPAR config data after FW reset */
789 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
790 npar = &adapter->npars[i];
791 if (npar->type != QLCNIC_TYPE_NIC)
792 continue;
793 err = qlcnic_get_nic_info(adapter, &nic_info, i);
794 if (err)
795 goto err_out;
796 nic_info.min_tx_bw = npar->min_bw;
797 nic_info.max_tx_bw = npar->max_bw;
798 err = qlcnic_set_nic_info(adapter, &nic_info);
799 if (err)
800 goto err_out;
801
802 if (npar->enable_pm) {
803 err = qlcnic_config_port_mirroring(adapter,
804 npar->dest_npar, 1, i);
805 if (err)
806 goto err_out;
807
808 }
809 npar->mac_learning = DEFAULT_MAC_LEARN;
810 npar->host_vlan_tag = 0;
811 npar->promisc_mode = 0;
812 npar->discard_tagged = 0;
813 npar->vlan_id = 0;
814 }
815 }
816err_out:
817 return err;
818}
819
af19b491
AKS
820static int
821qlcnic_start_firmware(struct qlcnic_adapter *adapter)
822{
823 int val, err, first_boot;
824
aa5e18c0
SC
825 err = qlcnic_can_start_firmware(adapter);
826 if (err < 0)
827 return err;
828 else if (!err)
af19b491
AKS
829 goto wait_init;
830
831 first_boot = QLCRD32(adapter, QLCNIC_CAM_RAM(0x1fc));
832 if (first_boot == 0x55555555)
833 /* This is the first boot after power up */
834 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
835
4d5bdb38
AKS
836 if (load_fw_file)
837 qlcnic_request_firmware(adapter);
8f891387 838 else {
839 if (qlcnic_check_flash_fw_ver(adapter))
840 goto err_out;
841
4d5bdb38 842 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
8f891387 843 }
af19b491
AKS
844
845 err = qlcnic_need_fw_reset(adapter);
846 if (err < 0)
847 goto err_out;
848 if (err == 0)
849 goto wait_init;
850
851 if (first_boot != 0x55555555) {
852 QLCWR32(adapter, CRB_CMDPEG_STATE, 0);
900c6cff 853 QLCWR32(adapter, CRB_RCVPEG_STATE, 0);
af19b491
AKS
854 qlcnic_pinit_from_rom(adapter);
855 msleep(1);
856 }
857
af19b491
AKS
858 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0);
859 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0);
860
861 qlcnic_set_port_mode(adapter);
862
863 err = qlcnic_load_firmware(adapter);
864 if (err)
865 goto err_out;
866
867 qlcnic_release_firmware(adapter);
868
869 val = (_QLCNIC_LINUX_MAJOR << 16)
870 | ((_QLCNIC_LINUX_MINOR << 8))
871 | (_QLCNIC_LINUX_SUBVERSION);
872 QLCWR32(adapter, CRB_DRIVER_VERSION, val);
873
874wait_init:
875 /* Handshake with the card before we register the devices. */
900c6cff 876 err = qlcnic_init_firmware(adapter);
af19b491
AKS
877 if (err)
878 goto err_out;
879
880 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
6df900e9 881 qlcnic_idc_debug_info(adapter, 1);
af19b491 882
af19b491 883 qlcnic_check_options(adapter);
cea8975e
AC
884 if (qlcnic_reset_npar_config(adapter))
885 goto err_out;
886 qlcnic_dev_set_npar_ready(adapter);
2e9d722d 887
af19b491
AKS
888 adapter->need_fw_reset = 0;
889
a7fc948f
AKS
890 qlcnic_release_firmware(adapter);
891 return 0;
af19b491
AKS
892
893err_out:
a7fc948f
AKS
894 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
895 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
af19b491
AKS
896 qlcnic_release_firmware(adapter);
897 return err;
898}
899
900static int
901qlcnic_request_irq(struct qlcnic_adapter *adapter)
902{
903 irq_handler_t handler;
904 struct qlcnic_host_sds_ring *sds_ring;
905 int err, ring;
906
907 unsigned long flags = 0;
908 struct net_device *netdev = adapter->netdev;
909 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
910
7eb9855d
AKS
911 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
912 handler = qlcnic_tmp_intr;
913 if (!QLCNIC_IS_MSI_FAMILY(adapter))
914 flags |= IRQF_SHARED;
915
916 } else {
917 if (adapter->flags & QLCNIC_MSIX_ENABLED)
918 handler = qlcnic_msix_intr;
919 else if (adapter->flags & QLCNIC_MSI_ENABLED)
920 handler = qlcnic_msi_intr;
921 else {
922 flags |= IRQF_SHARED;
923 handler = qlcnic_intr;
924 }
af19b491
AKS
925 }
926 adapter->irq = netdev->irq;
927
928 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
929 sds_ring = &recv_ctx->sds_rings[ring];
930 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
931 err = request_irq(sds_ring->irq, handler,
932 flags, sds_ring->name, sds_ring);
933 if (err)
934 return err;
935 }
936
937 return 0;
938}
939
940static void
941qlcnic_free_irq(struct qlcnic_adapter *adapter)
942{
943 int ring;
944 struct qlcnic_host_sds_ring *sds_ring;
945
946 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
947
948 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
949 sds_ring = &recv_ctx->sds_rings[ring];
950 free_irq(sds_ring->irq, sds_ring);
951 }
952}
953
954static void
955qlcnic_init_coalesce_defaults(struct qlcnic_adapter *adapter)
956{
957 adapter->coal.flags = QLCNIC_INTR_DEFAULT;
958 adapter->coal.normal.data.rx_time_us =
959 QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
960 adapter->coal.normal.data.rx_packets =
961 QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
962 adapter->coal.normal.data.tx_time_us =
963 QLCNIC_DEFAULT_INTR_COALESCE_TX_TIME_US;
964 adapter->coal.normal.data.tx_packets =
965 QLCNIC_DEFAULT_INTR_COALESCE_TX_PACKETS;
966}
967
968static int
969__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
970{
8a15ad1f
AKS
971 int ring;
972 struct qlcnic_host_rds_ring *rds_ring;
973
af19b491
AKS
974 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
975 return -EIO;
976
8a15ad1f
AKS
977 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
978 return 0;
979
980 if (qlcnic_fw_create_ctx(adapter))
981 return -EIO;
982
983 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
984 rds_ring = &adapter->recv_ctx.rds_rings[ring];
985 qlcnic_post_rx_buffers(adapter, ring, rds_ring);
986 }
987
af19b491
AKS
988 qlcnic_set_multi(netdev);
989 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
990
991 adapter->ahw.linkup = 0;
992
993 if (adapter->max_sds_rings > 1)
994 qlcnic_config_rss(adapter, 1);
995
996 qlcnic_config_intr_coalesce(adapter);
997
998 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
999 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1000
1001 qlcnic_napi_enable(adapter);
1002
1003 qlcnic_linkevent_request(adapter, 1);
1004
68bf1c68 1005 adapter->reset_context = 0;
af19b491
AKS
1006 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1007 return 0;
1008}
1009
1010/* Usage: During resume and firmware recovery module.*/
1011
1012static int
1013qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1014{
1015 int err = 0;
1016
1017 rtnl_lock();
1018 if (netif_running(netdev))
1019 err = __qlcnic_up(adapter, netdev);
1020 rtnl_unlock();
1021
1022 return err;
1023}
1024
1025static void
1026__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1027{
1028 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1029 return;
1030
1031 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1032 return;
1033
1034 smp_mb();
1035 spin_lock(&adapter->tx_clean_lock);
1036 netif_carrier_off(netdev);
1037 netif_tx_disable(netdev);
1038
1039 qlcnic_free_mac_list(adapter);
1040
1041 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1042
1043 qlcnic_napi_disable(adapter);
1044
8a15ad1f
AKS
1045 qlcnic_fw_destroy_ctx(adapter);
1046
1047 qlcnic_reset_rx_buffers_list(adapter);
af19b491
AKS
1048 qlcnic_release_tx_buffers(adapter);
1049 spin_unlock(&adapter->tx_clean_lock);
1050}
1051
1052/* Usage: During suspend and firmware recovery module */
1053
1054static void
1055qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1056{
1057 rtnl_lock();
1058 if (netif_running(netdev))
1059 __qlcnic_down(adapter, netdev);
1060 rtnl_unlock();
1061
1062}
1063
1064static int
1065qlcnic_attach(struct qlcnic_adapter *adapter)
1066{
1067 struct net_device *netdev = adapter->netdev;
1068 struct pci_dev *pdev = adapter->pdev;
8a15ad1f 1069 int err;
af19b491
AKS
1070
1071 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1072 return 0;
1073
af19b491
AKS
1074 err = qlcnic_napi_add(adapter, netdev);
1075 if (err)
1076 return err;
1077
1078 err = qlcnic_alloc_sw_resources(adapter);
1079 if (err) {
1080 dev_err(&pdev->dev, "Error in setting sw resources\n");
8a15ad1f 1081 goto err_out_napi_del;
af19b491
AKS
1082 }
1083
1084 err = qlcnic_alloc_hw_resources(adapter);
1085 if (err) {
1086 dev_err(&pdev->dev, "Error in setting hw resources\n");
1087 goto err_out_free_sw;
1088 }
1089
af19b491
AKS
1090 err = qlcnic_request_irq(adapter);
1091 if (err) {
1092 dev_err(&pdev->dev, "failed to setup interrupt\n");
8a15ad1f 1093 goto err_out_free_hw;
af19b491
AKS
1094 }
1095
1096 qlcnic_init_coalesce_defaults(adapter);
1097
1098 qlcnic_create_sysfs_entries(adapter);
1099
1100 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1101 return 0;
1102
8a15ad1f 1103err_out_free_hw:
af19b491
AKS
1104 qlcnic_free_hw_resources(adapter);
1105err_out_free_sw:
1106 qlcnic_free_sw_resources(adapter);
8a15ad1f
AKS
1107err_out_napi_del:
1108 qlcnic_napi_del(adapter);
af19b491
AKS
1109 return err;
1110}
1111
1112static void
1113qlcnic_detach(struct qlcnic_adapter *adapter)
1114{
1115 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1116 return;
1117
1118 qlcnic_remove_sysfs_entries(adapter);
1119
1120 qlcnic_free_hw_resources(adapter);
1121 qlcnic_release_rx_buffers(adapter);
1122 qlcnic_free_irq(adapter);
1123 qlcnic_napi_del(adapter);
1124 qlcnic_free_sw_resources(adapter);
1125
1126 adapter->is_up = 0;
1127}
1128
7eb9855d
AKS
1129void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1130{
1131 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1132 struct qlcnic_host_sds_ring *sds_ring;
1133 int ring;
1134
78ad3892 1135 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
cdaff185
AKS
1136 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1137 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1138 sds_ring = &adapter->recv_ctx.sds_rings[ring];
1139 qlcnic_disable_int(sds_ring);
1140 }
7eb9855d
AKS
1141 }
1142
8a15ad1f
AKS
1143 qlcnic_fw_destroy_ctx(adapter);
1144
7eb9855d
AKS
1145 qlcnic_detach(adapter);
1146
1147 adapter->diag_test = 0;
1148 adapter->max_sds_rings = max_sds_rings;
1149
1150 if (qlcnic_attach(adapter))
34ce3626 1151 goto out;
7eb9855d
AKS
1152
1153 if (netif_running(netdev))
1154 __qlcnic_up(adapter, netdev);
34ce3626 1155out:
7eb9855d
AKS
1156 netif_device_attach(netdev);
1157}
1158
1159int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1160{
1161 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1162 struct qlcnic_host_sds_ring *sds_ring;
8a15ad1f 1163 struct qlcnic_host_rds_ring *rds_ring;
7eb9855d
AKS
1164 int ring;
1165 int ret;
1166
1167 netif_device_detach(netdev);
1168
1169 if (netif_running(netdev))
1170 __qlcnic_down(adapter, netdev);
1171
1172 qlcnic_detach(adapter);
1173
1174 adapter->max_sds_rings = 1;
1175 adapter->diag_test = test;
1176
1177 ret = qlcnic_attach(adapter);
34ce3626
AKS
1178 if (ret) {
1179 netif_device_attach(netdev);
7eb9855d 1180 return ret;
34ce3626 1181 }
7eb9855d 1182
8a15ad1f
AKS
1183 ret = qlcnic_fw_create_ctx(adapter);
1184 if (ret) {
1185 qlcnic_detach(adapter);
57e46248 1186 netif_device_attach(netdev);
8a15ad1f
AKS
1187 return ret;
1188 }
1189
1190 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1191 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1192 qlcnic_post_rx_buffers(adapter, ring, rds_ring);
1193 }
1194
cdaff185
AKS
1195 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1196 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1197 sds_ring = &adapter->recv_ctx.sds_rings[ring];
1198 qlcnic_enable_int(sds_ring);
1199 }
7eb9855d 1200 }
78ad3892 1201 set_bit(__QLCNIC_DEV_UP, &adapter->state);
7eb9855d
AKS
1202
1203 return 0;
1204}
1205
68bf1c68
AKS
1206/* Reset context in hardware only */
1207static int
1208qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1209{
1210 struct net_device *netdev = adapter->netdev;
1211
1212 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1213 return -EBUSY;
1214
1215 netif_device_detach(netdev);
1216
1217 qlcnic_down(adapter, netdev);
1218
1219 qlcnic_up(adapter, netdev);
1220
1221 netif_device_attach(netdev);
1222
1223 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1224 return 0;
1225}
1226
af19b491
AKS
1227int
1228qlcnic_reset_context(struct qlcnic_adapter *adapter)
1229{
1230 int err = 0;
1231 struct net_device *netdev = adapter->netdev;
1232
1233 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1234 return -EBUSY;
1235
1236 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1237
1238 netif_device_detach(netdev);
1239
1240 if (netif_running(netdev))
1241 __qlcnic_down(adapter, netdev);
1242
1243 qlcnic_detach(adapter);
1244
1245 if (netif_running(netdev)) {
1246 err = qlcnic_attach(adapter);
1247 if (!err)
34ce3626 1248 __qlcnic_up(adapter, netdev);
af19b491
AKS
1249 }
1250
1251 netif_device_attach(netdev);
1252 }
1253
af19b491
AKS
1254 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1255 return err;
1256}
1257
1258static int
1259qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
1bb09fb9 1260 struct net_device *netdev, u8 pci_using_dac)
af19b491
AKS
1261{
1262 int err;
1263 struct pci_dev *pdev = adapter->pdev;
1264
1265 adapter->rx_csum = 1;
1266 adapter->mc_enabled = 0;
1267 adapter->max_mc_count = 38;
1268
1269 netdev->netdev_ops = &qlcnic_netdev_ops;
ef71ff83 1270 netdev->watchdog_timeo = 5*HZ;
af19b491
AKS
1271
1272 qlcnic_change_mtu(netdev, netdev->mtu);
1273
1274 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1275
2e9d722d 1276 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
ac8d0c4f 1277 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
2e9d722d 1278 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
ac8d0c4f
AC
1279 NETIF_F_IPV6_CSUM);
1280
1281 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
1282 netdev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
1283 netdev->vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
1284 }
af19b491 1285
1bb09fb9 1286 if (pci_using_dac) {
af19b491
AKS
1287 netdev->features |= NETIF_F_HIGHDMA;
1288 netdev->vlan_features |= NETIF_F_HIGHDMA;
1289 }
1290
1291 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1292 netdev->features |= (NETIF_F_HW_VLAN_TX);
1293
1294 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1295 netdev->features |= NETIF_F_LRO;
1296
1297 netdev->irq = adapter->msix_entries[0].vector;
1298
af19b491
AKS
1299 if (qlcnic_read_mac_addr(adapter))
1300 dev_warn(&pdev->dev, "failed to read mac addr\n");
1301
1302 netif_carrier_off(netdev);
1303 netif_stop_queue(netdev);
1304
1305 err = register_netdev(netdev);
1306 if (err) {
1307 dev_err(&pdev->dev, "failed to register net device\n");
1308 return err;
1309 }
1310
1311 return 0;
1312}
1313
1bb09fb9
AKS
1314static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1315{
1316 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1317 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1318 *pci_using_dac = 1;
1319 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1320 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1321 *pci_using_dac = 0;
1322 else {
1323 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1324 return -EIO;
1325 }
1326
1327 return 0;
1328}
1329
af19b491
AKS
1330static int __devinit
1331qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1332{
1333 struct net_device *netdev = NULL;
1334 struct qlcnic_adapter *adapter = NULL;
1335 int err;
af19b491 1336 uint8_t revision_id;
1bb09fb9 1337 uint8_t pci_using_dac;
af19b491
AKS
1338
1339 err = pci_enable_device(pdev);
1340 if (err)
1341 return err;
1342
1343 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1344 err = -ENODEV;
1345 goto err_out_disable_pdev;
1346 }
1347
1bb09fb9
AKS
1348 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1349 if (err)
1350 goto err_out_disable_pdev;
1351
af19b491
AKS
1352 err = pci_request_regions(pdev, qlcnic_driver_name);
1353 if (err)
1354 goto err_out_disable_pdev;
1355
1356 pci_set_master(pdev);
451724c8 1357 pci_enable_pcie_error_reporting(pdev);
af19b491
AKS
1358
1359 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1360 if (!netdev) {
1361 dev_err(&pdev->dev, "failed to allocate net_device\n");
1362 err = -ENOMEM;
1363 goto err_out_free_res;
1364 }
1365
1366 SET_NETDEV_DEV(netdev, &pdev->dev);
1367
1368 adapter = netdev_priv(netdev);
1369 adapter->netdev = netdev;
1370 adapter->pdev = pdev;
6df900e9 1371 adapter->dev_rst_time = jiffies;
af19b491
AKS
1372
1373 revision_id = pdev->revision;
1374 adapter->ahw.revision_id = revision_id;
1375
1376 rwlock_init(&adapter->ahw.crb_lock);
1377 mutex_init(&adapter->ahw.mem_lock);
1378
1379 spin_lock_init(&adapter->tx_clean_lock);
1380 INIT_LIST_HEAD(&adapter->mac_list);
1381
1382 err = qlcnic_setup_pci_map(adapter);
1383 if (err)
1384 goto err_out_free_netdev;
1385
1386 /* This will be reset for mezz cards */
2e9d722d 1387 adapter->portnum = adapter->ahw.pci_func;
af19b491
AKS
1388
1389 err = qlcnic_get_board_info(adapter);
1390 if (err) {
1391 dev_err(&pdev->dev, "Error getting board config info.\n");
1392 goto err_out_iounmap;
1393 }
1394
02f6e46f
SC
1395 if (qlcnic_read_mac_addr(adapter))
1396 dev_warn(&pdev->dev, "failed to read mac addr\n");
1397
b3a24649
SC
1398 if (qlcnic_setup_idc_param(adapter))
1399 goto err_out_iounmap;
af19b491 1400
9f26f547 1401 err = adapter->nic_ops->start_firmware(adapter);
a7fc948f
AKS
1402 if (err) {
1403 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
af19b491 1404 goto err_out_decr_ref;
a7fc948f 1405 }
af19b491 1406
af19b491
AKS
1407 qlcnic_clear_stats(adapter);
1408
1409 qlcnic_setup_intr(adapter);
1410
1bb09fb9 1411 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
af19b491
AKS
1412 if (err)
1413 goto err_out_disable_msi;
1414
1415 pci_set_drvdata(pdev, adapter);
1416
1417 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1418
1419 switch (adapter->ahw.port_type) {
1420 case QLCNIC_GBE:
1421 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1422 adapter->netdev->name);
1423 break;
1424 case QLCNIC_XGBE:
1425 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1426 adapter->netdev->name);
1427 break;
1428 }
1429
1430 qlcnic_create_diag_entries(adapter);
1431
1432 return 0;
1433
1434err_out_disable_msi:
1435 qlcnic_teardown_intr(adapter);
1436
1437err_out_decr_ref:
1438 qlcnic_clr_all_drv_state(adapter);
1439
1440err_out_iounmap:
1441 qlcnic_cleanup_pci_map(adapter);
1442
1443err_out_free_netdev:
1444 free_netdev(netdev);
1445
1446err_out_free_res:
1447 pci_release_regions(pdev);
1448
1449err_out_disable_pdev:
1450 pci_set_drvdata(pdev, NULL);
1451 pci_disable_device(pdev);
1452 return err;
1453}
1454
1455static void __devexit qlcnic_remove(struct pci_dev *pdev)
1456{
1457 struct qlcnic_adapter *adapter;
1458 struct net_device *netdev;
1459
1460 adapter = pci_get_drvdata(pdev);
1461 if (adapter == NULL)
1462 return;
1463
1464 netdev = adapter->netdev;
1465
1466 qlcnic_cancel_fw_work(adapter);
1467
1468 unregister_netdev(netdev);
1469
af19b491
AKS
1470 qlcnic_detach(adapter);
1471
2e9d722d
AC
1472 if (adapter->npars != NULL)
1473 kfree(adapter->npars);
1474 if (adapter->eswitch != NULL)
1475 kfree(adapter->eswitch);
1476
af19b491
AKS
1477 qlcnic_clr_all_drv_state(adapter);
1478
1479 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1480
1481 qlcnic_teardown_intr(adapter);
1482
1483 qlcnic_remove_diag_entries(adapter);
1484
1485 qlcnic_cleanup_pci_map(adapter);
1486
1487 qlcnic_release_firmware(adapter);
1488
451724c8 1489 pci_disable_pcie_error_reporting(pdev);
af19b491
AKS
1490 pci_release_regions(pdev);
1491 pci_disable_device(pdev);
1492 pci_set_drvdata(pdev, NULL);
1493
1494 free_netdev(netdev);
1495}
1496static int __qlcnic_shutdown(struct pci_dev *pdev)
1497{
1498 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1499 struct net_device *netdev = adapter->netdev;
1500 int retval;
1501
1502 netif_device_detach(netdev);
1503
1504 qlcnic_cancel_fw_work(adapter);
1505
1506 if (netif_running(netdev))
1507 qlcnic_down(adapter, netdev);
1508
af19b491
AKS
1509 qlcnic_clr_all_drv_state(adapter);
1510
1511 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1512
1513 retval = pci_save_state(pdev);
1514 if (retval)
1515 return retval;
1516
1517 if (qlcnic_wol_supported(adapter)) {
1518 pci_enable_wake(pdev, PCI_D3cold, 1);
1519 pci_enable_wake(pdev, PCI_D3hot, 1);
1520 }
1521
1522 return 0;
1523}
1524
1525static void qlcnic_shutdown(struct pci_dev *pdev)
1526{
1527 if (__qlcnic_shutdown(pdev))
1528 return;
1529
1530 pci_disable_device(pdev);
1531}
1532
1533#ifdef CONFIG_PM
1534static int
1535qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1536{
1537 int retval;
1538
1539 retval = __qlcnic_shutdown(pdev);
1540 if (retval)
1541 return retval;
1542
1543 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1544 return 0;
1545}
1546
1547static int
1548qlcnic_resume(struct pci_dev *pdev)
1549{
1550 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1551 struct net_device *netdev = adapter->netdev;
1552 int err;
1553
1554 err = pci_enable_device(pdev);
1555 if (err)
1556 return err;
1557
1558 pci_set_power_state(pdev, PCI_D0);
1559 pci_set_master(pdev);
1560 pci_restore_state(pdev);
1561
9f26f547 1562 err = adapter->nic_ops->start_firmware(adapter);
af19b491
AKS
1563 if (err) {
1564 dev_err(&pdev->dev, "failed to start firmware\n");
1565 return err;
1566 }
1567
1568 if (netif_running(netdev)) {
af19b491
AKS
1569 err = qlcnic_up(adapter, netdev);
1570 if (err)
52486a3a 1571 goto done;
af19b491
AKS
1572
1573 qlcnic_config_indev_addr(netdev, NETDEV_UP);
1574 }
52486a3a 1575done:
af19b491
AKS
1576 netif_device_attach(netdev);
1577 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1578 return 0;
af19b491
AKS
1579}
1580#endif
1581
1582static int qlcnic_open(struct net_device *netdev)
1583{
1584 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1585 int err;
1586
1587 if (adapter->driver_mismatch)
1588 return -EIO;
1589
1590 err = qlcnic_attach(adapter);
1591 if (err)
1592 return err;
1593
1594 err = __qlcnic_up(adapter, netdev);
1595 if (err)
1596 goto err_out;
1597
1598 netif_start_queue(netdev);
1599
1600 return 0;
1601
1602err_out:
1603 qlcnic_detach(adapter);
1604 return err;
1605}
1606
1607/*
1608 * qlcnic_close - Disables a network interface entry point
1609 */
1610static int qlcnic_close(struct net_device *netdev)
1611{
1612 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1613
1614 __qlcnic_down(adapter, netdev);
1615 return 0;
1616}
1617
1618static void
1619qlcnic_tso_check(struct net_device *netdev,
1620 struct qlcnic_host_tx_ring *tx_ring,
1621 struct cmd_desc_type0 *first_desc,
1622 struct sk_buff *skb)
1623{
1624 u8 opcode = TX_ETHER_PKT;
1625 __be16 protocol = skb->protocol;
1626 u16 flags = 0, vid = 0;
af19b491
AKS
1627 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1628 struct cmd_desc_type0 *hwdesc;
1629 struct vlan_ethhdr *vh;
8bfe8b91 1630 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2e9d722d 1631 u32 producer = tx_ring->producer;
af19b491
AKS
1632
1633 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1634
1635 vh = (struct vlan_ethhdr *)skb->data;
1636 protocol = vh->h_vlan_encapsulated_proto;
1637 flags = FLAGS_VLAN_TAGGED;
1638
1639 } else if (vlan_tx_tag_present(skb)) {
1640
1641 flags = FLAGS_VLAN_OOB;
1642 vid = vlan_tx_tag_get(skb);
1643 qlcnic_set_tx_vlan_tci(first_desc, vid);
1644 vlan_oob = 1;
1645 }
1646
2e9d722d
AC
1647 if (*(skb->data) & BIT_0) {
1648 flags |= BIT_0;
1649 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
1650 }
1651
af19b491
AKS
1652 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1653 skb_shinfo(skb)->gso_size > 0) {
1654
1655 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1656
1657 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1658 first_desc->total_hdr_length = hdr_len;
1659 if (vlan_oob) {
1660 first_desc->total_hdr_length += VLAN_HLEN;
1661 first_desc->tcp_hdr_offset = VLAN_HLEN;
1662 first_desc->ip_hdr_offset = VLAN_HLEN;
1663 /* Only in case of TSO on vlan device */
1664 flags |= FLAGS_VLAN_TAGGED;
1665 }
1666
1667 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1668 TX_TCP_LSO6 : TX_TCP_LSO;
1669 tso = 1;
1670
1671 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1672 u8 l4proto;
1673
1674 if (protocol == cpu_to_be16(ETH_P_IP)) {
1675 l4proto = ip_hdr(skb)->protocol;
1676
1677 if (l4proto == IPPROTO_TCP)
1678 opcode = TX_TCP_PKT;
1679 else if (l4proto == IPPROTO_UDP)
1680 opcode = TX_UDP_PKT;
1681 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1682 l4proto = ipv6_hdr(skb)->nexthdr;
1683
1684 if (l4proto == IPPROTO_TCP)
1685 opcode = TX_TCPV6_PKT;
1686 else if (l4proto == IPPROTO_UDP)
1687 opcode = TX_UDPV6_PKT;
1688 }
1689 }
1690
1691 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1692 first_desc->ip_hdr_offset += skb_network_offset(skb);
1693 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
1694
1695 if (!tso)
1696 return;
1697
1698 /* For LSO, we need to copy the MAC/IP/TCP headers into
1699 * the descriptor ring
1700 */
af19b491
AKS
1701 copied = 0;
1702 offset = 2;
1703
1704 if (vlan_oob) {
1705 /* Create a TSO vlan header template for firmware */
1706
1707 hwdesc = &tx_ring->desc_head[producer];
1708 tx_ring->cmd_buf_arr[producer].skb = NULL;
1709
1710 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1711 hdr_len + VLAN_HLEN);
1712
1713 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1714 skb_copy_from_linear_data(skb, vh, 12);
1715 vh->h_vlan_proto = htons(ETH_P_8021Q);
1716 vh->h_vlan_TCI = htons(vid);
1717 skb_copy_from_linear_data_offset(skb, 12,
1718 (char *)vh + 16, copy_len - 16);
1719
1720 copied = copy_len - VLAN_HLEN;
1721 offset = 0;
1722
1723 producer = get_next_index(producer, tx_ring->num_desc);
1724 }
1725
1726 while (copied < hdr_len) {
1727
1728 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1729 (hdr_len - copied));
1730
1731 hwdesc = &tx_ring->desc_head[producer];
1732 tx_ring->cmd_buf_arr[producer].skb = NULL;
1733
1734 skb_copy_from_linear_data_offset(skb, copied,
1735 (char *)hwdesc + offset, copy_len);
1736
1737 copied += copy_len;
1738 offset = 0;
1739
1740 producer = get_next_index(producer, tx_ring->num_desc);
1741 }
1742
1743 tx_ring->producer = producer;
1744 barrier();
8bfe8b91 1745 adapter->stats.lso_frames++;
af19b491
AKS
1746}
1747
1748static int
1749qlcnic_map_tx_skb(struct pci_dev *pdev,
1750 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
1751{
1752 struct qlcnic_skb_frag *nf;
1753 struct skb_frag_struct *frag;
1754 int i, nr_frags;
1755 dma_addr_t map;
1756
1757 nr_frags = skb_shinfo(skb)->nr_frags;
1758 nf = &pbuf->frag_array[0];
1759
1760 map = pci_map_single(pdev, skb->data,
1761 skb_headlen(skb), PCI_DMA_TODEVICE);
1762 if (pci_dma_mapping_error(pdev, map))
1763 goto out_err;
1764
1765 nf->dma = map;
1766 nf->length = skb_headlen(skb);
1767
1768 for (i = 0; i < nr_frags; i++) {
1769 frag = &skb_shinfo(skb)->frags[i];
1770 nf = &pbuf->frag_array[i+1];
1771
1772 map = pci_map_page(pdev, frag->page, frag->page_offset,
1773 frag->size, PCI_DMA_TODEVICE);
1774 if (pci_dma_mapping_error(pdev, map))
1775 goto unwind;
1776
1777 nf->dma = map;
1778 nf->length = frag->size;
1779 }
1780
1781 return 0;
1782
1783unwind:
1784 while (--i >= 0) {
1785 nf = &pbuf->frag_array[i+1];
1786 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1787 }
1788
1789 nf = &pbuf->frag_array[0];
1790 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1791
1792out_err:
1793 return -ENOMEM;
1794}
1795
1796static inline void
1797qlcnic_clear_cmddesc(u64 *desc)
1798{
1799 desc[0] = 0ULL;
1800 desc[2] = 0ULL;
1801}
1802
cdaff185 1803netdev_tx_t
af19b491
AKS
1804qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1805{
1806 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1807 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1808 struct qlcnic_cmd_buffer *pbuf;
1809 struct qlcnic_skb_frag *buffrag;
1810 struct cmd_desc_type0 *hwdesc, *first_desc;
1811 struct pci_dev *pdev;
1812 int i, k;
1813
1814 u32 producer;
1815 int frag_count, no_of_desc;
1816 u32 num_txd = tx_ring->num_desc;
1817
780ab790
AKS
1818 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
1819 netif_stop_queue(netdev);
1820 return NETDEV_TX_BUSY;
1821 }
1822
af19b491
AKS
1823 frag_count = skb_shinfo(skb)->nr_frags + 1;
1824
1825 /* 4 fragments per cmd des */
1826 no_of_desc = (frag_count + 3) >> 2;
1827
ef71ff83 1828 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
af19b491 1829 netif_stop_queue(netdev);
ef71ff83
RB
1830 smp_mb();
1831 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
1832 netif_start_queue(netdev);
1833 else {
1834 adapter->stats.xmit_off++;
1835 return NETDEV_TX_BUSY;
1836 }
af19b491
AKS
1837 }
1838
1839 producer = tx_ring->producer;
1840 pbuf = &tx_ring->cmd_buf_arr[producer];
1841
1842 pdev = adapter->pdev;
1843
8ae6df97
AKS
1844 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
1845 adapter->stats.tx_dma_map_error++;
af19b491 1846 goto drop_packet;
8ae6df97 1847 }
af19b491
AKS
1848
1849 pbuf->skb = skb;
1850 pbuf->frag_count = frag_count;
1851
1852 first_desc = hwdesc = &tx_ring->desc_head[producer];
1853 qlcnic_clear_cmddesc((u64 *)hwdesc);
1854
1855 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
1856 qlcnic_set_tx_port(first_desc, adapter->portnum);
1857
1858 for (i = 0; i < frag_count; i++) {
1859
1860 k = i % 4;
1861
1862 if ((k == 0) && (i > 0)) {
1863 /* move to next desc.*/
1864 producer = get_next_index(producer, num_txd);
1865 hwdesc = &tx_ring->desc_head[producer];
1866 qlcnic_clear_cmddesc((u64 *)hwdesc);
1867 tx_ring->cmd_buf_arr[producer].skb = NULL;
1868 }
1869
1870 buffrag = &pbuf->frag_array[i];
1871
1872 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1873 switch (k) {
1874 case 0:
1875 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1876 break;
1877 case 1:
1878 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1879 break;
1880 case 2:
1881 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1882 break;
1883 case 3:
1884 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1885 break;
1886 }
1887 }
1888
1889 tx_ring->producer = get_next_index(producer, num_txd);
1890
1891 qlcnic_tso_check(netdev, tx_ring, first_desc, skb);
1892
1893 qlcnic_update_cmd_producer(adapter, tx_ring);
1894
1895 adapter->stats.txbytes += skb->len;
1896 adapter->stats.xmitcalled++;
1897
1898 return NETDEV_TX_OK;
1899
1900drop_packet:
1901 adapter->stats.txdropped++;
1902 dev_kfree_skb_any(skb);
1903 return NETDEV_TX_OK;
1904}
1905
1906static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
1907{
1908 struct net_device *netdev = adapter->netdev;
1909 u32 temp, temp_state, temp_val;
1910 int rv = 0;
1911
1912 temp = QLCRD32(adapter, CRB_TEMP_STATE);
1913
1914 temp_state = qlcnic_get_temp_state(temp);
1915 temp_val = qlcnic_get_temp_val(temp);
1916
1917 if (temp_state == QLCNIC_TEMP_PANIC) {
1918 dev_err(&netdev->dev,
1919 "Device temperature %d degrees C exceeds"
1920 " maximum allowed. Hardware has been shut down.\n",
1921 temp_val);
1922 rv = 1;
1923 } else if (temp_state == QLCNIC_TEMP_WARN) {
1924 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
1925 dev_err(&netdev->dev,
1926 "Device temperature %d degrees C "
1927 "exceeds operating range."
1928 " Immediate action needed.\n",
1929 temp_val);
1930 }
1931 } else {
1932 if (adapter->temp == QLCNIC_TEMP_WARN) {
1933 dev_info(&netdev->dev,
1934 "Device temperature is now %d degrees C"
1935 " in normal range.\n", temp_val);
1936 }
1937 }
1938 adapter->temp = temp_state;
1939 return rv;
1940}
1941
1942void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
1943{
1944 struct net_device *netdev = adapter->netdev;
1945
1946 if (adapter->ahw.linkup && !linkup) {
1947 dev_info(&netdev->dev, "NIC Link is down\n");
1948 adapter->ahw.linkup = 0;
1949 if (netif_running(netdev)) {
1950 netif_carrier_off(netdev);
1951 netif_stop_queue(netdev);
1952 }
1953 } else if (!adapter->ahw.linkup && linkup) {
1954 dev_info(&netdev->dev, "NIC Link is up\n");
1955 adapter->ahw.linkup = 1;
1956 if (netif_running(netdev)) {
1957 netif_carrier_on(netdev);
1958 netif_wake_queue(netdev);
1959 }
1960 }
1961}
1962
1963static void qlcnic_tx_timeout(struct net_device *netdev)
1964{
1965 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1966
1967 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
1968 return;
1969
1970 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
af19b491
AKS
1971
1972 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
68bf1c68
AKS
1973 adapter->need_fw_reset = 1;
1974 else
1975 adapter->reset_context = 1;
af19b491
AKS
1976}
1977
1978static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
1979{
1980 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1981 struct net_device_stats *stats = &netdev->stats;
1982
1983 memset(stats, 0, sizeof(*stats));
1984
1985 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
1986 stats->tx_packets = adapter->stats.xmitfinished;
7e382594 1987 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
af19b491
AKS
1988 stats->tx_bytes = adapter->stats.txbytes;
1989 stats->rx_dropped = adapter->stats.rxdropped;
1990 stats->tx_dropped = adapter->stats.txdropped;
1991
1992 return stats;
1993}
1994
7eb9855d 1995static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
af19b491 1996{
af19b491
AKS
1997 u32 status;
1998
1999 status = readl(adapter->isr_int_vec);
2000
2001 if (!(status & adapter->int_vec_bit))
2002 return IRQ_NONE;
2003
2004 /* check interrupt state machine, to be sure */
2005 status = readl(adapter->crb_int_state_reg);
2006 if (!ISR_LEGACY_INT_TRIGGERED(status))
2007 return IRQ_NONE;
2008
2009 writel(0xffffffff, adapter->tgt_status_reg);
2010 /* read twice to ensure write is flushed */
2011 readl(adapter->isr_int_vec);
2012 readl(adapter->isr_int_vec);
2013
7eb9855d
AKS
2014 return IRQ_HANDLED;
2015}
2016
2017static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2018{
2019 struct qlcnic_host_sds_ring *sds_ring = data;
2020 struct qlcnic_adapter *adapter = sds_ring->adapter;
2021
2022 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2023 goto done;
2024 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2025 writel(0xffffffff, adapter->tgt_status_reg);
2026 goto done;
2027 }
2028
2029 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2030 return IRQ_NONE;
2031
2032done:
2033 adapter->diag_cnt++;
2034 qlcnic_enable_int(sds_ring);
2035 return IRQ_HANDLED;
2036}
2037
2038static irqreturn_t qlcnic_intr(int irq, void *data)
2039{
2040 struct qlcnic_host_sds_ring *sds_ring = data;
2041 struct qlcnic_adapter *adapter = sds_ring->adapter;
2042
2043 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2044 return IRQ_NONE;
2045
af19b491
AKS
2046 napi_schedule(&sds_ring->napi);
2047
2048 return IRQ_HANDLED;
2049}
2050
2051static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2052{
2053 struct qlcnic_host_sds_ring *sds_ring = data;
2054 struct qlcnic_adapter *adapter = sds_ring->adapter;
2055
2056 /* clear interrupt */
2057 writel(0xffffffff, adapter->tgt_status_reg);
2058
2059 napi_schedule(&sds_ring->napi);
2060 return IRQ_HANDLED;
2061}
2062
2063static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2064{
2065 struct qlcnic_host_sds_ring *sds_ring = data;
2066
2067 napi_schedule(&sds_ring->napi);
2068 return IRQ_HANDLED;
2069}
2070
2071static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2072{
2073 u32 sw_consumer, hw_consumer;
2074 int count = 0, i;
2075 struct qlcnic_cmd_buffer *buffer;
2076 struct pci_dev *pdev = adapter->pdev;
2077 struct net_device *netdev = adapter->netdev;
2078 struct qlcnic_skb_frag *frag;
2079 int done;
2080 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2081
2082 if (!spin_trylock(&adapter->tx_clean_lock))
2083 return 1;
2084
2085 sw_consumer = tx_ring->sw_consumer;
2086 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2087
2088 while (sw_consumer != hw_consumer) {
2089 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2090 if (buffer->skb) {
2091 frag = &buffer->frag_array[0];
2092 pci_unmap_single(pdev, frag->dma, frag->length,
2093 PCI_DMA_TODEVICE);
2094 frag->dma = 0ULL;
2095 for (i = 1; i < buffer->frag_count; i++) {
2096 frag++;
2097 pci_unmap_page(pdev, frag->dma, frag->length,
2098 PCI_DMA_TODEVICE);
2099 frag->dma = 0ULL;
2100 }
2101
2102 adapter->stats.xmitfinished++;
2103 dev_kfree_skb_any(buffer->skb);
2104 buffer->skb = NULL;
2105 }
2106
2107 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2108 if (++count >= MAX_STATUS_HANDLE)
2109 break;
2110 }
2111
2112 if (count && netif_running(netdev)) {
2113 tx_ring->sw_consumer = sw_consumer;
2114
2115 smp_mb();
2116
2117 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
af19b491
AKS
2118 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2119 netif_wake_queue(netdev);
8bfe8b91 2120 adapter->stats.xmit_on++;
af19b491 2121 }
af19b491 2122 }
ef71ff83 2123 adapter->tx_timeo_cnt = 0;
af19b491
AKS
2124 }
2125 /*
2126 * If everything is freed up to consumer then check if the ring is full
2127 * If the ring is full then check if more needs to be freed and
2128 * schedule the call back again.
2129 *
2130 * This happens when there are 2 CPUs. One could be freeing and the
2131 * other filling it. If the ring is full when we get out of here and
2132 * the card has already interrupted the host then the host can miss the
2133 * interrupt.
2134 *
2135 * There is still a possible race condition and the host could miss an
2136 * interrupt. The card has to take care of this.
2137 */
2138 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2139 done = (sw_consumer == hw_consumer);
2140 spin_unlock(&adapter->tx_clean_lock);
2141
2142 return done;
2143}
2144
2145static int qlcnic_poll(struct napi_struct *napi, int budget)
2146{
2147 struct qlcnic_host_sds_ring *sds_ring =
2148 container_of(napi, struct qlcnic_host_sds_ring, napi);
2149
2150 struct qlcnic_adapter *adapter = sds_ring->adapter;
2151
2152 int tx_complete;
2153 int work_done;
2154
2155 tx_complete = qlcnic_process_cmd_ring(adapter);
2156
2157 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2158
2159 if ((work_done < budget) && tx_complete) {
2160 napi_complete(&sds_ring->napi);
2161 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2162 qlcnic_enable_int(sds_ring);
2163 }
2164
2165 return work_done;
2166}
2167
8f891387 2168static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2169{
2170 struct qlcnic_host_sds_ring *sds_ring =
2171 container_of(napi, struct qlcnic_host_sds_ring, napi);
2172
2173 struct qlcnic_adapter *adapter = sds_ring->adapter;
2174 int work_done;
2175
2176 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2177
2178 if (work_done < budget) {
2179 napi_complete(&sds_ring->napi);
2180 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2181 qlcnic_enable_int(sds_ring);
2182 }
2183
2184 return work_done;
2185}
2186
af19b491
AKS
2187#ifdef CONFIG_NET_POLL_CONTROLLER
2188static void qlcnic_poll_controller(struct net_device *netdev)
2189{
2190 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2191 disable_irq(adapter->irq);
2192 qlcnic_intr(adapter->irq, adapter);
2193 enable_irq(adapter->irq);
2194}
2195#endif
2196
6df900e9
SC
2197static void
2198qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2199{
2200 u32 val;
2201
2202 val = adapter->portnum & 0xf;
2203 val |= encoding << 7;
2204 val |= (jiffies - adapter->dev_rst_time) << 8;
2205
2206 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2207 adapter->dev_rst_time = jiffies;
2208}
2209
ade91f8e
AKS
2210static int
2211qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
af19b491
AKS
2212{
2213 u32 val;
2214
2215 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2216 state != QLCNIC_DEV_NEED_QUISCENT);
2217
2218 if (qlcnic_api_lock(adapter))
ade91f8e 2219 return -EIO;
af19b491
AKS
2220
2221 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2222
2223 if (state == QLCNIC_DEV_NEED_RESET)
6d2a4724 2224 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
af19b491 2225 else if (state == QLCNIC_DEV_NEED_QUISCENT)
6d2a4724 2226 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
af19b491
AKS
2227
2228 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2229
2230 qlcnic_api_unlock(adapter);
ade91f8e
AKS
2231
2232 return 0;
af19b491
AKS
2233}
2234
1b95a839
AKS
2235static int
2236qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2237{
2238 u32 val;
2239
2240 if (qlcnic_api_lock(adapter))
2241 return -EBUSY;
2242
2243 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
6d2a4724 2244 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
1b95a839
AKS
2245 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2246
2247 qlcnic_api_unlock(adapter);
2248
2249 return 0;
2250}
2251
af19b491
AKS
2252static void
2253qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter)
2254{
2255 u32 val;
2256
2257 if (qlcnic_api_lock(adapter))
2258 goto err;
2259
2260 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
6d2a4724 2261 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
af19b491
AKS
2262 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
2263
2264 if (!(val & 0x11111111))
2265 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2266
2267 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
6d2a4724 2268 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
af19b491
AKS
2269 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2270
2271 qlcnic_api_unlock(adapter);
2272err:
2273 adapter->fw_fail_cnt = 0;
2274 clear_bit(__QLCNIC_START_FW, &adapter->state);
2275 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2276}
2277
f73dfc50 2278/* Grab api lock, before checking state */
af19b491
AKS
2279static int
2280qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2281{
2282 int act, state;
2283
2284 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2285 act = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
2286
2287 if (((state & 0x11111111) == (act & 0x11111111)) ||
2288 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2289 return 0;
2290 else
2291 return 1;
2292}
2293
96f8118c
SC
2294static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2295{
2296 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2297
2298 if (val != QLCNIC_DRV_IDC_VER) {
2299 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2300 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2301 }
2302
2303 return 0;
2304}
2305
af19b491
AKS
2306static int
2307qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2308{
2309 u32 val, prev_state;
aa5e18c0 2310 u8 dev_init_timeo = adapter->dev_init_timeo;
6d2a4724 2311 u8 portnum = adapter->portnum;
96f8118c 2312 u8 ret;
af19b491 2313
f73dfc50
AKS
2314 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2315 return 1;
2316
af19b491
AKS
2317 if (qlcnic_api_lock(adapter))
2318 return -1;
2319
2320 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
6d2a4724
AKS
2321 if (!(val & (1 << (portnum * 4)))) {
2322 QLC_DEV_SET_REF_CNT(val, portnum);
af19b491 2323 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
af19b491
AKS
2324 }
2325
2326 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
65b5b420 2327 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
af19b491
AKS
2328
2329 switch (prev_state) {
2330 case QLCNIC_DEV_COLD:
bbd8c6a4 2331 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
96f8118c 2332 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
6df900e9 2333 qlcnic_idc_debug_info(adapter, 0);
af19b491
AKS
2334 qlcnic_api_unlock(adapter);
2335 return 1;
2336
2337 case QLCNIC_DEV_READY:
96f8118c 2338 ret = qlcnic_check_idc_ver(adapter);
af19b491 2339 qlcnic_api_unlock(adapter);
96f8118c 2340 return ret;
af19b491
AKS
2341
2342 case QLCNIC_DEV_NEED_RESET:
2343 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
6d2a4724 2344 QLC_DEV_SET_RST_RDY(val, portnum);
af19b491
AKS
2345 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2346 break;
2347
2348 case QLCNIC_DEV_NEED_QUISCENT:
2349 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
6d2a4724 2350 QLC_DEV_SET_QSCNT_RDY(val, portnum);
af19b491
AKS
2351 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2352 break;
2353
2354 case QLCNIC_DEV_FAILED:
a7fc948f 2355 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
af19b491
AKS
2356 qlcnic_api_unlock(adapter);
2357 return -1;
bbd8c6a4
AKS
2358
2359 case QLCNIC_DEV_INITIALIZING:
2360 case QLCNIC_DEV_QUISCENT:
2361 break;
af19b491
AKS
2362 }
2363
2364 qlcnic_api_unlock(adapter);
aa5e18c0
SC
2365
2366 do {
af19b491 2367 msleep(1000);
a5e463d0
SC
2368 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2369
2370 if (prev_state == QLCNIC_DEV_QUISCENT)
2371 continue;
2372 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
af19b491 2373
65b5b420
AKS
2374 if (!dev_init_timeo) {
2375 dev_err(&adapter->pdev->dev,
2376 "Waiting for device to initialize timeout\n");
af19b491 2377 return -1;
65b5b420 2378 }
af19b491
AKS
2379
2380 if (qlcnic_api_lock(adapter))
2381 return -1;
2382
2383 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
6d2a4724 2384 QLC_DEV_CLR_RST_QSCNT(val, portnum);
af19b491
AKS
2385 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2386
96f8118c 2387 ret = qlcnic_check_idc_ver(adapter);
af19b491
AKS
2388 qlcnic_api_unlock(adapter);
2389
96f8118c 2390 return ret;
af19b491
AKS
2391}
2392
2393static void
2394qlcnic_fwinit_work(struct work_struct *work)
2395{
2396 struct qlcnic_adapter *adapter = container_of(work,
2397 struct qlcnic_adapter, fw_work.work);
3c4b23b1 2398 u32 dev_state = 0xf;
af19b491 2399
f73dfc50
AKS
2400 if (qlcnic_api_lock(adapter))
2401 goto err_ret;
af19b491 2402
a5e463d0
SC
2403 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2404 if (dev_state == QLCNIC_DEV_QUISCENT) {
2405 qlcnic_api_unlock(adapter);
2406 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2407 FW_POLL_DELAY * 2);
2408 return;
2409 }
2410
9f26f547 2411 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
3c4b23b1
AKS
2412 qlcnic_api_unlock(adapter);
2413 goto wait_npar;
9f26f547
AC
2414 }
2415
f73dfc50
AKS
2416 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2417 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2418 adapter->reset_ack_timeo);
2419 goto skip_ack_check;
2420 }
2421
2422 if (!qlcnic_check_drv_state(adapter)) {
2423skip_ack_check:
2424 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
a5e463d0
SC
2425
2426 if (dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2427 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2428 QLCNIC_DEV_QUISCENT);
2429 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2430 FW_POLL_DELAY * 2);
2431 QLCDB(adapter, DRV, "Quiscing the driver\n");
6df900e9
SC
2432 qlcnic_idc_debug_info(adapter, 0);
2433
a5e463d0
SC
2434 qlcnic_api_unlock(adapter);
2435 return;
2436 }
2437
f73dfc50
AKS
2438 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2439 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2440 QLCNIC_DEV_INITIALIZING);
2441 set_bit(__QLCNIC_START_FW, &adapter->state);
2442 QLCDB(adapter, DRV, "Restarting fw\n");
6df900e9 2443 qlcnic_idc_debug_info(adapter, 0);
af19b491
AKS
2444 }
2445
f73dfc50
AKS
2446 qlcnic_api_unlock(adapter);
2447
9f26f547 2448 if (!adapter->nic_ops->start_firmware(adapter)) {
af19b491
AKS
2449 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2450 return;
2451 }
af19b491
AKS
2452 goto err_ret;
2453 }
2454
f73dfc50 2455 qlcnic_api_unlock(adapter);
aa5e18c0 2456
9f26f547 2457wait_npar:
af19b491 2458 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
f73dfc50 2459 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
65b5b420 2460
af19b491 2461 switch (dev_state) {
3c4b23b1 2462 case QLCNIC_DEV_READY:
9f26f547 2463 if (!adapter->nic_ops->start_firmware(adapter)) {
f73dfc50
AKS
2464 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2465 return;
2466 }
3c4b23b1
AKS
2467 case QLCNIC_DEV_FAILED:
2468 break;
2469 default:
2470 qlcnic_schedule_work(adapter,
2471 qlcnic_fwinit_work, FW_POLL_DELAY);
2472 return;
af19b491
AKS
2473 }
2474
2475err_ret:
f73dfc50
AKS
2476 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2477 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
34ce3626 2478 netif_device_attach(adapter->netdev);
af19b491
AKS
2479 qlcnic_clr_all_drv_state(adapter);
2480}
2481
2482static void
2483qlcnic_detach_work(struct work_struct *work)
2484{
2485 struct qlcnic_adapter *adapter = container_of(work,
2486 struct qlcnic_adapter, fw_work.work);
2487 struct net_device *netdev = adapter->netdev;
2488 u32 status;
2489
2490 netif_device_detach(netdev);
2491
2492 qlcnic_down(adapter, netdev);
2493
af19b491
AKS
2494 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2495
2496 if (status & QLCNIC_RCODE_FATAL_ERROR)
2497 goto err_ret;
2498
2499 if (adapter->temp == QLCNIC_TEMP_PANIC)
2500 goto err_ret;
2501
ade91f8e
AKS
2502 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2503 goto err_ret;
af19b491
AKS
2504
2505 adapter->fw_wait_cnt = 0;
2506
2507 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2508
2509 return;
2510
2511err_ret:
65b5b420
AKS
2512 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2513 status, adapter->temp);
34ce3626 2514 netif_device_attach(netdev);
af19b491
AKS
2515 qlcnic_clr_all_drv_state(adapter);
2516
2517}
2518
3c4b23b1
AKS
2519/*Transit NPAR state to NON Operational */
2520static void
2521qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2522{
2523 u32 state;
2524
2525 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2526 if (state == QLCNIC_DEV_NPAR_NON_OPER)
2527 return;
2528
2529 if (qlcnic_api_lock(adapter))
2530 return;
2531 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2532 qlcnic_api_unlock(adapter);
2533}
2534
f73dfc50 2535/*Transit to RESET state from READY state only */
af19b491
AKS
2536static void
2537qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2538{
2539 u32 state;
2540
cea8975e 2541 adapter->need_fw_reset = 1;
af19b491
AKS
2542 if (qlcnic_api_lock(adapter))
2543 return;
2544
2545 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2546
f73dfc50 2547 if (state == QLCNIC_DEV_READY) {
af19b491 2548 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
65b5b420 2549 QLCDB(adapter, DRV, "NEED_RESET state set\n");
6df900e9 2550 qlcnic_idc_debug_info(adapter, 0);
af19b491
AKS
2551 }
2552
3c4b23b1 2553 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
af19b491
AKS
2554 qlcnic_api_unlock(adapter);
2555}
2556
9f26f547
AC
2557/* Transit to NPAR READY state from NPAR NOT READY state */
2558static void
2559qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2560{
cea8975e 2561 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
3c4b23b1 2562 adapter->op_mode != QLCNIC_MGMT_FUNC)
cea8975e 2563 return;
9f26f547
AC
2564 if (qlcnic_api_lock(adapter))
2565 return;
2566
3c4b23b1
AKS
2567 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
2568 QLCDB(adapter, DRV, "NPAR operational state set\n");
9f26f547
AC
2569
2570 qlcnic_api_unlock(adapter);
2571}
2572
af19b491
AKS
2573static void
2574qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2575 work_func_t func, int delay)
2576{
451724c8
SC
2577 if (test_bit(__QLCNIC_AER, &adapter->state))
2578 return;
2579
af19b491
AKS
2580 INIT_DELAYED_WORK(&adapter->fw_work, func);
2581 schedule_delayed_work(&adapter->fw_work, round_jiffies_relative(delay));
2582}
2583
2584static void
2585qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2586{
2587 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2588 msleep(10);
2589
2590 cancel_delayed_work_sync(&adapter->fw_work);
2591}
2592
2593static void
2594qlcnic_attach_work(struct work_struct *work)
2595{
2596 struct qlcnic_adapter *adapter = container_of(work,
2597 struct qlcnic_adapter, fw_work.work);
2598 struct net_device *netdev = adapter->netdev;
af19b491
AKS
2599
2600 if (netif_running(netdev)) {
52486a3a 2601 if (qlcnic_up(adapter, netdev))
af19b491 2602 goto done;
af19b491
AKS
2603
2604 qlcnic_config_indev_addr(netdev, NETDEV_UP);
2605 }
2606
af19b491 2607done:
34ce3626 2608 netif_device_attach(netdev);
af19b491
AKS
2609 adapter->fw_fail_cnt = 0;
2610 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1b95a839
AKS
2611
2612 if (!qlcnic_clr_drv_state(adapter))
2613 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2614 FW_POLL_DELAY);
af19b491
AKS
2615}
2616
2617static int
2618qlcnic_check_health(struct qlcnic_adapter *adapter)
2619{
2620 u32 state = 0, heartbit;
2621 struct net_device *netdev = adapter->netdev;
2622
2623 if (qlcnic_check_temp(adapter))
2624 goto detach;
2625
2372a5f1 2626 if (adapter->need_fw_reset)
af19b491 2627 qlcnic_dev_request_reset(adapter);
af19b491
AKS
2628
2629 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
3c4b23b1
AKS
2630 if (state == QLCNIC_DEV_NEED_RESET ||
2631 state == QLCNIC_DEV_NEED_QUISCENT) {
2632 qlcnic_set_npar_non_operational(adapter);
af19b491 2633 adapter->need_fw_reset = 1;
3c4b23b1 2634 }
af19b491
AKS
2635
2636 heartbit = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
2637 if (heartbit != adapter->heartbit) {
2638 adapter->heartbit = heartbit;
2639 adapter->fw_fail_cnt = 0;
2640 if (adapter->need_fw_reset)
2641 goto detach;
68bf1c68 2642
0df170b6
AKS
2643 if (adapter->reset_context &&
2644 auto_fw_reset == AUTO_FW_RESET_ENABLED) {
68bf1c68
AKS
2645 qlcnic_reset_hw_context(adapter);
2646 adapter->netdev->trans_start = jiffies;
2647 }
2648
af19b491
AKS
2649 return 0;
2650 }
2651
2652 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2653 return 0;
2654
2655 qlcnic_dev_request_reset(adapter);
2656
0df170b6
AKS
2657 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED))
2658 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
af19b491
AKS
2659
2660 dev_info(&netdev->dev, "firmware hang detected\n");
2661
2662detach:
2663 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
2664 QLCNIC_DEV_NEED_RESET;
2665
2666 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
65b5b420
AKS
2667 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
2668
af19b491 2669 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
65b5b420
AKS
2670 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
2671 }
af19b491
AKS
2672
2673 return 1;
2674}
2675
2676static void
2677qlcnic_fw_poll_work(struct work_struct *work)
2678{
2679 struct qlcnic_adapter *adapter = container_of(work,
2680 struct qlcnic_adapter, fw_work.work);
2681
2682 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2683 goto reschedule;
2684
2685
2686 if (qlcnic_check_health(adapter))
2687 return;
2688
2689reschedule:
2690 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
2691}
2692
451724c8
SC
2693static int qlcnic_is_first_func(struct pci_dev *pdev)
2694{
2695 struct pci_dev *oth_pdev;
2696 int val = pdev->devfn;
2697
2698 while (val-- > 0) {
2699 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
2700 (pdev->bus), pdev->bus->number,
2701 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
bfc978fa
AKS
2702 if (!oth_pdev)
2703 continue;
451724c8 2704
bfc978fa
AKS
2705 if (oth_pdev->current_state != PCI_D3cold) {
2706 pci_dev_put(oth_pdev);
451724c8 2707 return 0;
bfc978fa
AKS
2708 }
2709 pci_dev_put(oth_pdev);
451724c8
SC
2710 }
2711 return 1;
2712}
2713
2714static int qlcnic_attach_func(struct pci_dev *pdev)
2715{
2716 int err, first_func;
2717 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
2718 struct net_device *netdev = adapter->netdev;
2719
2720 pdev->error_state = pci_channel_io_normal;
2721
2722 err = pci_enable_device(pdev);
2723 if (err)
2724 return err;
2725
2726 pci_set_power_state(pdev, PCI_D0);
2727 pci_set_master(pdev);
2728 pci_restore_state(pdev);
2729
2730 first_func = qlcnic_is_first_func(pdev);
2731
2732 if (qlcnic_api_lock(adapter))
2733 return -EINVAL;
2734
933fce12 2735 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
451724c8
SC
2736 adapter->need_fw_reset = 1;
2737 set_bit(__QLCNIC_START_FW, &adapter->state);
2738 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
2739 QLCDB(adapter, DRV, "Restarting fw\n");
2740 }
2741 qlcnic_api_unlock(adapter);
2742
2743 err = adapter->nic_ops->start_firmware(adapter);
2744 if (err)
2745 return err;
2746
2747 qlcnic_clr_drv_state(adapter);
2748 qlcnic_setup_intr(adapter);
2749
2750 if (netif_running(netdev)) {
2751 err = qlcnic_attach(adapter);
2752 if (err) {
2753 qlcnic_clr_all_drv_state(adapter);
2754 clear_bit(__QLCNIC_AER, &adapter->state);
2755 netif_device_attach(netdev);
2756 return err;
2757 }
2758
2759 err = qlcnic_up(adapter, netdev);
2760 if (err)
2761 goto done;
2762
2763 qlcnic_config_indev_addr(netdev, NETDEV_UP);
2764 }
2765 done:
2766 netif_device_attach(netdev);
2767 return err;
2768}
2769
2770static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
2771 pci_channel_state_t state)
2772{
2773 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
2774 struct net_device *netdev = adapter->netdev;
2775
2776 if (state == pci_channel_io_perm_failure)
2777 return PCI_ERS_RESULT_DISCONNECT;
2778
2779 if (state == pci_channel_io_normal)
2780 return PCI_ERS_RESULT_RECOVERED;
2781
2782 set_bit(__QLCNIC_AER, &adapter->state);
2783 netif_device_detach(netdev);
2784
2785 cancel_delayed_work_sync(&adapter->fw_work);
2786
2787 if (netif_running(netdev))
2788 qlcnic_down(adapter, netdev);
2789
2790 qlcnic_detach(adapter);
2791 qlcnic_teardown_intr(adapter);
2792
2793 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2794
2795 pci_save_state(pdev);
2796 pci_disable_device(pdev);
2797
2798 return PCI_ERS_RESULT_NEED_RESET;
2799}
2800
2801static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
2802{
2803 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
2804 PCI_ERS_RESULT_RECOVERED;
2805}
2806
2807static void qlcnic_io_resume(struct pci_dev *pdev)
2808{
2809 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
2810
2811 pci_cleanup_aer_uncorrect_error_status(pdev);
2812
2813 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
2814 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
2815 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2816 FW_POLL_DELAY);
2817}
2818
2819
87eb743b
AC
2820static int
2821qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
2822{
2823 int err;
3c4b23b1
AKS
2824 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
2825 u32 npar_state;
87eb743b
AC
2826
2827 err = qlcnic_can_start_firmware(adapter);
2828 if (err)
2829 return err;
2830
3c4b23b1
AKS
2831 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2832 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
2833 msleep(1000);
2834 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2835 }
2836
2837 if (!npar_opt_timeo) {
2838 dev_err(&adapter->pdev->dev,
2839 "Waiting for NPAR state to opertional timeout\n");
2840 return -EIO;
2841 }
2842
87eb743b
AC
2843 qlcnic_check_options(adapter);
2844
2845 adapter->need_fw_reset = 0;
2846
2847 return err;
2848}
2849
2850static int
2851qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
2852{
2853 return -EOPNOTSUPP;
2854}
2855
2856static int
2857qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
2858{
2859 return -EOPNOTSUPP;
2860}
2861
af19b491
AKS
2862static ssize_t
2863qlcnic_store_bridged_mode(struct device *dev,
2864 struct device_attribute *attr, const char *buf, size_t len)
2865{
2866 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2867 unsigned long new;
2868 int ret = -EINVAL;
2869
2870 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
2871 goto err_out;
2872
8a15ad1f 2873 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
af19b491
AKS
2874 goto err_out;
2875
2876 if (strict_strtoul(buf, 2, &new))
2877 goto err_out;
2878
2e9d722d 2879 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
af19b491
AKS
2880 ret = len;
2881
2882err_out:
2883 return ret;
2884}
2885
2886static ssize_t
2887qlcnic_show_bridged_mode(struct device *dev,
2888 struct device_attribute *attr, char *buf)
2889{
2890 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2891 int bridged_mode = 0;
2892
2893 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2894 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
2895
2896 return sprintf(buf, "%d\n", bridged_mode);
2897}
2898
2899static struct device_attribute dev_attr_bridged_mode = {
2900 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2901 .show = qlcnic_show_bridged_mode,
2902 .store = qlcnic_store_bridged_mode,
2903};
2904
2905static ssize_t
2906qlcnic_store_diag_mode(struct device *dev,
2907 struct device_attribute *attr, const char *buf, size_t len)
2908{
2909 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2910 unsigned long new;
2911
2912 if (strict_strtoul(buf, 2, &new))
2913 return -EINVAL;
2914
2915 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
2916 adapter->flags ^= QLCNIC_DIAG_ENABLED;
2917
2918 return len;
2919}
2920
2921static ssize_t
2922qlcnic_show_diag_mode(struct device *dev,
2923 struct device_attribute *attr, char *buf)
2924{
2925 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2926
2927 return sprintf(buf, "%d\n",
2928 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
2929}
2930
2931static struct device_attribute dev_attr_diag_mode = {
2932 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2933 .show = qlcnic_show_diag_mode,
2934 .store = qlcnic_store_diag_mode,
2935};
2936
2937static int
2938qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
2939 loff_t offset, size_t size)
2940{
897e8c7c
DP
2941 size_t crb_size = 4;
2942
af19b491
AKS
2943 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2944 return -EIO;
2945
897e8c7c
DP
2946 if (offset < QLCNIC_PCI_CRBSPACE) {
2947 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
2948 QLCNIC_PCI_CAMQM_END))
2949 crb_size = 8;
2950 else
2951 return -EINVAL;
2952 }
af19b491 2953
897e8c7c
DP
2954 if ((size != crb_size) || (offset & (crb_size-1)))
2955 return -EINVAL;
af19b491
AKS
2956
2957 return 0;
2958}
2959
2960static ssize_t
2c3c8bea
CW
2961qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
2962 struct bin_attribute *attr,
af19b491
AKS
2963 char *buf, loff_t offset, size_t size)
2964{
2965 struct device *dev = container_of(kobj, struct device, kobj);
2966 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2967 u32 data;
897e8c7c 2968 u64 qmdata;
af19b491
AKS
2969 int ret;
2970
2971 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2972 if (ret != 0)
2973 return ret;
2974
897e8c7c
DP
2975 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2976 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
2977 memcpy(buf, &qmdata, size);
2978 } else {
2979 data = QLCRD32(adapter, offset);
2980 memcpy(buf, &data, size);
2981 }
af19b491
AKS
2982 return size;
2983}
2984
2985static ssize_t
2c3c8bea
CW
2986qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
2987 struct bin_attribute *attr,
af19b491
AKS
2988 char *buf, loff_t offset, size_t size)
2989{
2990 struct device *dev = container_of(kobj, struct device, kobj);
2991 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2992 u32 data;
897e8c7c 2993 u64 qmdata;
af19b491
AKS
2994 int ret;
2995
2996 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2997 if (ret != 0)
2998 return ret;
2999
897e8c7c
DP
3000 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3001 memcpy(&qmdata, buf, size);
3002 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3003 } else {
3004 memcpy(&data, buf, size);
3005 QLCWR32(adapter, offset, data);
3006 }
af19b491
AKS
3007 return size;
3008}
3009
3010static int
3011qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3012 loff_t offset, size_t size)
3013{
3014 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3015 return -EIO;
3016
3017 if ((size != 8) || (offset & 0x7))
3018 return -EIO;
3019
3020 return 0;
3021}
3022
3023static ssize_t
2c3c8bea
CW
3024qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3025 struct bin_attribute *attr,
af19b491
AKS
3026 char *buf, loff_t offset, size_t size)
3027{
3028 struct device *dev = container_of(kobj, struct device, kobj);
3029 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3030 u64 data;
3031 int ret;
3032
3033 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3034 if (ret != 0)
3035 return ret;
3036
3037 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3038 return -EIO;
3039
3040 memcpy(buf, &data, size);
3041
3042 return size;
3043}
3044
3045static ssize_t
2c3c8bea
CW
3046qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3047 struct bin_attribute *attr,
af19b491
AKS
3048 char *buf, loff_t offset, size_t size)
3049{
3050 struct device *dev = container_of(kobj, struct device, kobj);
3051 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3052 u64 data;
3053 int ret;
3054
3055 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3056 if (ret != 0)
3057 return ret;
3058
3059 memcpy(&data, buf, size);
3060
3061 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3062 return -EIO;
3063
3064 return size;
3065}
3066
3067
3068static struct bin_attribute bin_attr_crb = {
3069 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3070 .size = 0,
3071 .read = qlcnic_sysfs_read_crb,
3072 .write = qlcnic_sysfs_write_crb,
3073};
3074
3075static struct bin_attribute bin_attr_mem = {
3076 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3077 .size = 0,
3078 .read = qlcnic_sysfs_read_mem,
3079 .write = qlcnic_sysfs_write_mem,
3080};
3081
cea8975e 3082static int
346fe763
RB
3083validate_pm_config(struct qlcnic_adapter *adapter,
3084 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3085{
3086
3087 u8 src_pci_func, s_esw_id, d_esw_id;
3088 u8 dest_pci_func;
3089 int i;
3090
3091 for (i = 0; i < count; i++) {
3092 src_pci_func = pm_cfg[i].pci_func;
3093 dest_pci_func = pm_cfg[i].dest_npar;
3094 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3095 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3096 return QL_STATUS_INVALID_PARAM;
3097
3098 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3099 return QL_STATUS_INVALID_PARAM;
3100
3101 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3102 return QL_STATUS_INVALID_PARAM;
3103
3104 if (!IS_VALID_MODE(pm_cfg[i].action))
3105 return QL_STATUS_INVALID_PARAM;
3106
3107 s_esw_id = adapter->npars[src_pci_func].phy_port;
3108 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3109
3110 if (s_esw_id != d_esw_id)
3111 return QL_STATUS_INVALID_PARAM;
3112
3113 }
3114 return 0;
3115
3116}
3117
3118static ssize_t
3119qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3120 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3121{
3122 struct device *dev = container_of(kobj, struct device, kobj);
3123 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3124 struct qlcnic_pm_func_cfg *pm_cfg;
3125 u32 id, action, pci_func;
3126 int count, rem, i, ret;
3127
3128 count = size / sizeof(struct qlcnic_pm_func_cfg);
3129 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3130 if (rem)
3131 return QL_STATUS_INVALID_PARAM;
3132
3133 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3134
3135 ret = validate_pm_config(adapter, pm_cfg, count);
3136 if (ret)
3137 return ret;
3138 for (i = 0; i < count; i++) {
3139 pci_func = pm_cfg[i].pci_func;
3140 action = pm_cfg[i].action;
3141 id = adapter->npars[pci_func].phy_port;
3142 ret = qlcnic_config_port_mirroring(adapter, id,
3143 action, pci_func);
3144 if (ret)
3145 return ret;
3146 }
3147
3148 for (i = 0; i < count; i++) {
3149 pci_func = pm_cfg[i].pci_func;
3150 id = adapter->npars[pci_func].phy_port;
3151 adapter->npars[pci_func].enable_pm = pm_cfg[i].action;
3152 adapter->npars[pci_func].dest_npar = id;
3153 }
3154 return size;
3155}
3156
3157static ssize_t
3158qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3159 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3160{
3161 struct device *dev = container_of(kobj, struct device, kobj);
3162 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3163 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3164 int i;
3165
3166 if (size != sizeof(pm_cfg))
3167 return QL_STATUS_INVALID_PARAM;
3168
3169 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3170 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3171 continue;
3172 pm_cfg[i].action = adapter->npars[i].enable_pm;
3173 pm_cfg[i].dest_npar = 0;
3174 pm_cfg[i].pci_func = i;
3175 }
3176 memcpy(buf, &pm_cfg, size);
3177
3178 return size;
3179}
3180
cea8975e 3181static int
346fe763
RB
3182validate_esw_config(struct qlcnic_adapter *adapter,
3183 struct qlcnic_esw_func_cfg *esw_cfg, int count)
3184{
3185 u8 pci_func;
3186 int i;
3187
3188 for (i = 0; i < count; i++) {
3189 pci_func = esw_cfg[i].pci_func;
3190 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3191 return QL_STATUS_INVALID_PARAM;
3192
3193 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3194 return QL_STATUS_INVALID_PARAM;
3195
3196 if (esw_cfg->host_vlan_tag == 1)
3197 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3198 return QL_STATUS_INVALID_PARAM;
3199
3200 if (!IS_VALID_MODE(esw_cfg[i].promisc_mode)
3201 || !IS_VALID_MODE(esw_cfg[i].host_vlan_tag)
3202 || !IS_VALID_MODE(esw_cfg[i].mac_learning)
3203 || !IS_VALID_MODE(esw_cfg[i].discard_tagged))
3204 return QL_STATUS_INVALID_PARAM;
3205 }
3206
3207 return 0;
3208}
3209
3210static ssize_t
3211qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3212 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3213{
3214 struct device *dev = container_of(kobj, struct device, kobj);
3215 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3216 struct qlcnic_esw_func_cfg *esw_cfg;
346fe763 3217 int count, rem, i, ret;
cea8975e 3218 u8 id, pci_func;
346fe763
RB
3219
3220 count = size / sizeof(struct qlcnic_esw_func_cfg);
3221 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3222 if (rem)
3223 return QL_STATUS_INVALID_PARAM;
3224
3225 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3226 ret = validate_esw_config(adapter, esw_cfg, count);
3227 if (ret)
3228 return ret;
3229
3230 for (i = 0; i < count; i++) {
3231 pci_func = esw_cfg[i].pci_func;
3232 id = adapter->npars[pci_func].phy_port;
cea8975e
AC
3233 ret = qlcnic_config_switch_port(adapter, id,
3234 esw_cfg[i].host_vlan_tag,
3235 esw_cfg[i].discard_tagged,
3236 esw_cfg[i].promisc_mode,
3237 esw_cfg[i].mac_learning,
3238 esw_cfg[i].pci_func,
3239 esw_cfg[i].vlan_id);
346fe763
RB
3240 if (ret)
3241 return ret;
3242 }
3243
3244 for (i = 0; i < count; i++) {
3245 pci_func = esw_cfg[i].pci_func;
3246 adapter->npars[pci_func].promisc_mode = esw_cfg[i].promisc_mode;
3247 adapter->npars[pci_func].mac_learning = esw_cfg[i].mac_learning;
3248 adapter->npars[pci_func].vlan_id = esw_cfg[i].vlan_id;
3249 adapter->npars[pci_func].discard_tagged =
3250 esw_cfg[i].discard_tagged;
3251 adapter->npars[pci_func].host_vlan_tag =
3252 esw_cfg[i].host_vlan_tag;
3253 }
3254
3255 return size;
3256}
3257
3258static ssize_t
3259qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3260 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3261{
3262 struct device *dev = container_of(kobj, struct device, kobj);
3263 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3264 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
3265 int i;
3266
3267 if (size != sizeof(esw_cfg))
3268 return QL_STATUS_INVALID_PARAM;
3269
3270 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3271 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3272 continue;
3273
3274 esw_cfg[i].host_vlan_tag = adapter->npars[i].host_vlan_tag;
3275 esw_cfg[i].promisc_mode = adapter->npars[i].promisc_mode;
3276 esw_cfg[i].discard_tagged = adapter->npars[i].discard_tagged;
3277 esw_cfg[i].vlan_id = adapter->npars[i].vlan_id;
3278 esw_cfg[i].mac_learning = adapter->npars[i].mac_learning;
3279 }
3280 memcpy(buf, &esw_cfg, size);
3281
3282 return size;
3283}
3284
cea8975e 3285static int
346fe763
RB
3286validate_npar_config(struct qlcnic_adapter *adapter,
3287 struct qlcnic_npar_func_cfg *np_cfg, int count)
3288{
3289 u8 pci_func, i;
3290
3291 for (i = 0; i < count; i++) {
3292 pci_func = np_cfg[i].pci_func;
3293 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3294 return QL_STATUS_INVALID_PARAM;
3295
3296 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3297 return QL_STATUS_INVALID_PARAM;
3298
3299 if (!IS_VALID_BW(np_cfg[i].min_bw)
3300 || !IS_VALID_BW(np_cfg[i].max_bw)
3301 || !IS_VALID_RX_QUEUES(np_cfg[i].max_rx_queues)
3302 || !IS_VALID_TX_QUEUES(np_cfg[i].max_tx_queues))
3303 return QL_STATUS_INVALID_PARAM;
3304 }
3305 return 0;
3306}
3307
3308static ssize_t
3309qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3310 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3311{
3312 struct device *dev = container_of(kobj, struct device, kobj);
3313 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3314 struct qlcnic_info nic_info;
3315 struct qlcnic_npar_func_cfg *np_cfg;
3316 int i, count, rem, ret;
3317 u8 pci_func;
3318
3319 count = size / sizeof(struct qlcnic_npar_func_cfg);
3320 rem = size % sizeof(struct qlcnic_npar_func_cfg);
3321 if (rem)
3322 return QL_STATUS_INVALID_PARAM;
3323
3324 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
3325 ret = validate_npar_config(adapter, np_cfg, count);
3326 if (ret)
3327 return ret;
3328
3329 for (i = 0; i < count ; i++) {
3330 pci_func = np_cfg[i].pci_func;
3331 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
3332 if (ret)
3333 return ret;
3334 nic_info.pci_func = pci_func;
3335 nic_info.min_tx_bw = np_cfg[i].min_bw;
3336 nic_info.max_tx_bw = np_cfg[i].max_bw;
3337 ret = qlcnic_set_nic_info(adapter, &nic_info);
3338 if (ret)
3339 return ret;
cea8975e
AC
3340 adapter->npars[i].min_bw = nic_info.min_tx_bw;
3341 adapter->npars[i].max_bw = nic_info.max_tx_bw;
346fe763
RB
3342 }
3343
3344 return size;
3345
3346}
3347static ssize_t
3348qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
3349 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3350{
3351 struct device *dev = container_of(kobj, struct device, kobj);
3352 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3353 struct qlcnic_info nic_info;
3354 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
3355 int i, ret;
3356
3357 if (size != sizeof(np_cfg))
3358 return QL_STATUS_INVALID_PARAM;
3359
3360 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3361 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3362 continue;
3363 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
3364 if (ret)
3365 return ret;
3366
3367 np_cfg[i].pci_func = i;
3368 np_cfg[i].op_mode = nic_info.op_mode;
3369 np_cfg[i].port_num = nic_info.phys_port;
3370 np_cfg[i].fw_capab = nic_info.capabilities;
3371 np_cfg[i].min_bw = nic_info.min_tx_bw ;
3372 np_cfg[i].max_bw = nic_info.max_tx_bw;
3373 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
3374 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
3375 }
3376 memcpy(buf, &np_cfg, size);
3377 return size;
3378}
3379
3380static ssize_t
3381qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
3382 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3383{
3384 struct device *dev = container_of(kobj, struct device, kobj);
3385 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3386 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
e88db3bd 3387 struct qlcnic_pci_info *pci_info;
346fe763
RB
3388 int i, ret;
3389
3390 if (size != sizeof(pci_cfg))
3391 return QL_STATUS_INVALID_PARAM;
3392
e88db3bd
DC
3393 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
3394 if (!pci_info)
3395 return -ENOMEM;
3396
346fe763 3397 ret = qlcnic_get_pci_info(adapter, pci_info);
e88db3bd
DC
3398 if (ret) {
3399 kfree(pci_info);
346fe763 3400 return ret;
e88db3bd 3401 }
346fe763
RB
3402
3403 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3404 pci_cfg[i].pci_func = pci_info[i].id;
3405 pci_cfg[i].func_type = pci_info[i].type;
3406 pci_cfg[i].port_num = pci_info[i].default_port;
3407 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
3408 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
3409 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
3410 }
3411 memcpy(buf, &pci_cfg, size);
e88db3bd 3412 kfree(pci_info);
346fe763 3413 return size;
346fe763
RB
3414}
3415static struct bin_attribute bin_attr_npar_config = {
3416 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
3417 .size = 0,
3418 .read = qlcnic_sysfs_read_npar_config,
3419 .write = qlcnic_sysfs_write_npar_config,
3420};
3421
3422static struct bin_attribute bin_attr_pci_config = {
3423 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
3424 .size = 0,
3425 .read = qlcnic_sysfs_read_pci_config,
3426 .write = NULL,
3427};
3428
3429static struct bin_attribute bin_attr_esw_config = {
3430 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
3431 .size = 0,
3432 .read = qlcnic_sysfs_read_esw_config,
3433 .write = qlcnic_sysfs_write_esw_config,
3434};
3435
3436static struct bin_attribute bin_attr_pm_config = {
3437 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
3438 .size = 0,
3439 .read = qlcnic_sysfs_read_pm_config,
3440 .write = qlcnic_sysfs_write_pm_config,
3441};
3442
af19b491
AKS
3443static void
3444qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
3445{
3446 struct device *dev = &adapter->pdev->dev;
3447
3448 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3449 if (device_create_file(dev, &dev_attr_bridged_mode))
3450 dev_warn(dev,
3451 "failed to create bridged_mode sysfs entry\n");
3452}
3453
3454static void
3455qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
3456{
3457 struct device *dev = &adapter->pdev->dev;
3458
3459 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3460 device_remove_file(dev, &dev_attr_bridged_mode);
3461}
3462
3463static void
3464qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
3465{
3466 struct device *dev = &adapter->pdev->dev;
3467
132ff00a
AC
3468 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
3469 return;
af19b491
AKS
3470 if (device_create_file(dev, &dev_attr_diag_mode))
3471 dev_info(dev, "failed to create diag_mode sysfs entry\n");
3472 if (device_create_bin_file(dev, &bin_attr_crb))
3473 dev_info(dev, "failed to create crb sysfs entry\n");
3474 if (device_create_bin_file(dev, &bin_attr_mem))
3475 dev_info(dev, "failed to create mem sysfs entry\n");
346fe763
RB
3476 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
3477 adapter->op_mode != QLCNIC_MGMT_FUNC)
3478 return;
3479 if (device_create_bin_file(dev, &bin_attr_pci_config))
3480 dev_info(dev, "failed to create pci config sysfs entry");
3481 if (device_create_bin_file(dev, &bin_attr_npar_config))
3482 dev_info(dev, "failed to create npar config sysfs entry");
3483 if (device_create_bin_file(dev, &bin_attr_esw_config))
3484 dev_info(dev, "failed to create esw config sysfs entry");
3485 if (device_create_bin_file(dev, &bin_attr_pm_config))
3486 dev_info(dev, "failed to create pm config sysfs entry");
3487
af19b491
AKS
3488}
3489
af19b491
AKS
3490static void
3491qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
3492{
3493 struct device *dev = &adapter->pdev->dev;
3494
132ff00a
AC
3495 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
3496 return;
af19b491
AKS
3497 device_remove_file(dev, &dev_attr_diag_mode);
3498 device_remove_bin_file(dev, &bin_attr_crb);
3499 device_remove_bin_file(dev, &bin_attr_mem);
346fe763
RB
3500 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
3501 adapter->op_mode != QLCNIC_MGMT_FUNC)
3502 return;
3503 device_remove_bin_file(dev, &bin_attr_pci_config);
3504 device_remove_bin_file(dev, &bin_attr_npar_config);
3505 device_remove_bin_file(dev, &bin_attr_esw_config);
3506 device_remove_bin_file(dev, &bin_attr_pm_config);
af19b491
AKS
3507}
3508
3509#ifdef CONFIG_INET
3510
3511#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
3512
af19b491
AKS
3513static void
3514qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
3515{
3516 struct in_device *indev;
3517 struct qlcnic_adapter *adapter = netdev_priv(dev);
3518
af19b491
AKS
3519 indev = in_dev_get(dev);
3520 if (!indev)
3521 return;
3522
3523 for_ifa(indev) {
3524 switch (event) {
3525 case NETDEV_UP:
3526 qlcnic_config_ipaddr(adapter,
3527 ifa->ifa_address, QLCNIC_IP_UP);
3528 break;
3529 case NETDEV_DOWN:
3530 qlcnic_config_ipaddr(adapter,
3531 ifa->ifa_address, QLCNIC_IP_DOWN);
3532 break;
3533 default:
3534 break;
3535 }
3536 } endfor_ifa(indev);
3537
3538 in_dev_put(indev);
af19b491
AKS
3539}
3540
3541static int qlcnic_netdev_event(struct notifier_block *this,
3542 unsigned long event, void *ptr)
3543{
3544 struct qlcnic_adapter *adapter;
3545 struct net_device *dev = (struct net_device *)ptr;
3546
3547recheck:
3548 if (dev == NULL)
3549 goto done;
3550
3551 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3552 dev = vlan_dev_real_dev(dev);
3553 goto recheck;
3554 }
3555
3556 if (!is_qlcnic_netdev(dev))
3557 goto done;
3558
3559 adapter = netdev_priv(dev);
3560
3561 if (!adapter)
3562 goto done;
3563
8a15ad1f 3564 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
af19b491
AKS
3565 goto done;
3566
3567 qlcnic_config_indev_addr(dev, event);
3568done:
3569 return NOTIFY_DONE;
3570}
3571
3572static int
3573qlcnic_inetaddr_event(struct notifier_block *this,
3574 unsigned long event, void *ptr)
3575{
3576 struct qlcnic_adapter *adapter;
3577 struct net_device *dev;
3578
3579 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3580
3581 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3582
3583recheck:
3584 if (dev == NULL || !netif_running(dev))
3585 goto done;
3586
3587 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3588 dev = vlan_dev_real_dev(dev);
3589 goto recheck;
3590 }
3591
3592 if (!is_qlcnic_netdev(dev))
3593 goto done;
3594
3595 adapter = netdev_priv(dev);
3596
251a84c9 3597 if (!adapter)
af19b491
AKS
3598 goto done;
3599
8a15ad1f 3600 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
af19b491
AKS
3601 goto done;
3602
3603 switch (event) {
3604 case NETDEV_UP:
3605 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
3606 break;
3607 case NETDEV_DOWN:
3608 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
3609 break;
3610 default:
3611 break;
3612 }
3613
3614done:
3615 return NOTIFY_DONE;
3616}
3617
3618static struct notifier_block qlcnic_netdev_cb = {
3619 .notifier_call = qlcnic_netdev_event,
3620};
3621
3622static struct notifier_block qlcnic_inetaddr_cb = {
3623 .notifier_call = qlcnic_inetaddr_event,
3624};
3625#else
3626static void
3627qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
3628{ }
3629#endif
451724c8
SC
3630static struct pci_error_handlers qlcnic_err_handler = {
3631 .error_detected = qlcnic_io_error_detected,
3632 .slot_reset = qlcnic_io_slot_reset,
3633 .resume = qlcnic_io_resume,
3634};
af19b491
AKS
3635
3636static struct pci_driver qlcnic_driver = {
3637 .name = qlcnic_driver_name,
3638 .id_table = qlcnic_pci_tbl,
3639 .probe = qlcnic_probe,
3640 .remove = __devexit_p(qlcnic_remove),
3641#ifdef CONFIG_PM
3642 .suspend = qlcnic_suspend,
3643 .resume = qlcnic_resume,
3644#endif
451724c8
SC
3645 .shutdown = qlcnic_shutdown,
3646 .err_handler = &qlcnic_err_handler
3647
af19b491
AKS
3648};
3649
3650static int __init qlcnic_init_module(void)
3651{
0cf3a14c 3652 int ret;
af19b491
AKS
3653
3654 printk(KERN_INFO "%s\n", qlcnic_driver_string);
3655
3656#ifdef CONFIG_INET
3657 register_netdevice_notifier(&qlcnic_netdev_cb);
3658 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
3659#endif
3660
0cf3a14c
AKS
3661 ret = pci_register_driver(&qlcnic_driver);
3662 if (ret) {
3663#ifdef CONFIG_INET
3664 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
3665 unregister_netdevice_notifier(&qlcnic_netdev_cb);
3666#endif
3667 }
af19b491 3668
0cf3a14c 3669 return ret;
af19b491
AKS
3670}
3671
3672module_init(qlcnic_init_module);
3673
3674static void __exit qlcnic_exit_module(void)
3675{
3676
3677 pci_unregister_driver(&qlcnic_driver);
3678
3679#ifdef CONFIG_INET
3680 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
3681 unregister_netdevice_notifier(&qlcnic_netdev_cb);
3682#endif
3683}
3684
3685module_exit(qlcnic_exit_module);