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