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